Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / include / lustre_lib.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/include/lustre_lib.h
37  *
38  * Basic Lustre library routines.
39  */
40
41 #ifndef _LUSTRE_LIB_H
42 #define _LUSTRE_LIB_H
43
44 #include <libcfs/libcfs.h>
45 #include <lustre/lustre_idl.h>
46 #include <lustre_ver.h>
47 #include <lustre_cfg.h>
48 #if defined(__linux__)
49 #include <linux/lustre_lib.h>
50 #elif defined(__APPLE__)
51 #include <darwin/lustre_lib.h>
52 #elif defined(__WINNT__)
53 #include <winnt/lustre_lib.h>
54 #else
55 #error Unsupported operating system.
56 #endif
57
58 /* prng.c */
59 unsigned int ll_rand(void);        /* returns a random 32-bit integer */
60 void ll_srand(unsigned int, unsigned int);     /* seed the generator */
61 void ll_get_random_bytes(void *buf, int size);
62
63 /* target.c */
64 struct ptlrpc_request;
65 struct recovd_data;
66 struct recovd_obd;
67 struct obd_export;
68 #include <lustre_ha.h>
69 #include <lustre_net.h>
70 #include <lvfs.h>
71
72 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
73                           int error);
74 int target_handle_connect(struct ptlrpc_request *req);
75 int target_handle_disconnect(struct ptlrpc_request *req);
76 void target_destroy_export(struct obd_export *exp);
77 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
78                             struct obd_uuid *cluuid, int);
79 int target_pack_pool_reply(struct ptlrpc_request *req);
80 int target_handle_ping(struct ptlrpc_request *req);
81 void target_committed_to_req(struct ptlrpc_request *req);
82
83 /* quotacheck callback, dqacq/dqrel callback handler */
84 int target_handle_qc_callback(struct ptlrpc_request *req);
85 #ifdef HAVE_QUOTA_SUPPORT
86 int target_handle_dqacq_callback(struct ptlrpc_request *req);
87 #else
88 #define target_handle_dqacq_callback(req) ldlm_callback_reply(req, -ENOTSUPP)
89 #endif
90
91 #define OBD_RECOVERY_MAX_TIME (obd_timeout * 18) /* b13079 */
92
93 void target_cancel_recovery_timer(struct obd_device *obd);
94 int target_start_recovery_thread(struct obd_device *obd,
95                                  svc_handler_t handler);
96 void target_stop_recovery_thread(struct obd_device *obd);
97 void target_cleanup_recovery(struct obd_device *obd);
98 int target_queue_recovery_request(struct ptlrpc_request *req,
99                                   struct obd_device *obd);
100 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
101
102 /* client.c */
103
104 int client_sanobd_setup(struct obd_device *obddev, struct lustre_cfg* lcfg);
105 struct client_obd *client_conn2cli(struct lustre_handle *conn);
106
107 struct md_open_data;
108 struct obd_client_handle {
109         struct lustre_handle  och_fh;
110         struct lu_fid         och_fid;
111         struct md_open_data *och_mod;
112         __u32 och_magic;
113         int och_flags;
114 };
115 #define OBD_CLIENT_HANDLE_MAGIC 0xd15ea5ed
116
117 /* statfs_pack.c */
118 void statfs_pack(struct obd_statfs *osfs, struct kstatfs *sfs);
119 void statfs_unpack(struct kstatfs *sfs, struct obd_statfs *osfs);
120
121 /* l_lock.c */
122 struct lustre_lock {
123         int l_depth;
124         cfs_task_t *l_owner;
125         struct semaphore l_sem;
126         spinlock_t l_spin;
127 };
128
129 void l_lock_init(struct lustre_lock *);
130 void l_lock(struct lustre_lock *);
131 void l_unlock(struct lustre_lock *);
132 int l_has_lock(struct lustre_lock *);
133
134
135 /*
136  *   OBD IOCTLS
137  */
138 #define OBD_IOCTL_VERSION 0x00010004
139
140 struct obd_ioctl_data {
141         __u32 ioc_len;
142         __u32 ioc_version;
143
144         __u64 ioc_cookie;
145         __u32 ioc_conn1;
146         __u32 ioc_conn2;
147
148         struct obdo ioc_obdo1;
149         struct obdo ioc_obdo2;
150
151         obd_size ioc_count;
152         obd_off  ioc_offset;
153         __u32    ioc_dev;
154         __u32    ioc_command;
155
156         __u64 ioc_nid;
157         __u32 ioc_nal;
158         __u32 ioc_type;
159
160         /* buffers the kernel will treat as user pointers */
161         __u32  ioc_plen1;
162         char  *ioc_pbuf1;
163         __u32  ioc_plen2;
164         char  *ioc_pbuf2;
165
166         /* inline buffers for various arguments */
167         __u32  ioc_inllen1;
168         char  *ioc_inlbuf1;
169         __u32  ioc_inllen2;
170         char  *ioc_inlbuf2;
171         __u32  ioc_inllen3;
172         char  *ioc_inlbuf3;
173         __u32  ioc_inllen4;
174         char  *ioc_inlbuf4;
175
176         char    ioc_bulk[0];
177 };
178
179 struct obd_ioctl_hdr {
180         __u32 ioc_len;
181         __u32 ioc_version;
182 };
183
184 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
185 {
186         int len = size_round(sizeof(struct obd_ioctl_data));
187         len += size_round(data->ioc_inllen1);
188         len += size_round(data->ioc_inllen2);
189         len += size_round(data->ioc_inllen3);
190         len += size_round(data->ioc_inllen4);
191         return len;
192 }
193
194
195 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
196 {
197         if (data->ioc_len > (1<<30)) {
198                 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
199                 return 1;
200         }
201         if (data->ioc_inllen1 > (1<<30)) {
202                 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
203                 return 1;
204         }
205         if (data->ioc_inllen2 > (1<<30)) {
206                 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
207                 return 1;
208         }
209         if (data->ioc_inllen3 > (1<<30)) {
210                 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
211                 return 1;
212         }
213         if (data->ioc_inllen4 > (1<<30)) {
214                 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
215                 return 1;
216         }
217         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
218                 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
219                 return 1;
220         }
221         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
222                 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
223                 return 1;
224         }
225         if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
226                 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
227                 return 1;
228         }
229         if (data->ioc_inlbuf4 && !data->ioc_inllen4) {
230                 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
231                 return 1;
232         }
233         if (data->ioc_pbuf1 && !data->ioc_plen1) {
234                 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
235                 return 1;
236         }
237         if (data->ioc_pbuf2 && !data->ioc_plen2) {
238                 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
239                 return 1;
240         }
241         if (data->ioc_plen1 && !data->ioc_pbuf1) {
242                 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
243                 return 1;
244         }
245         if (data->ioc_plen2 && !data->ioc_pbuf2) {
246                 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
247                 return 1;
248         }
249         if (obd_ioctl_packlen(data) > data->ioc_len) {
250                 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
251                        obd_ioctl_packlen(data), data->ioc_len);
252                 return 1;
253         }
254         return 0;
255 }
256
257 #ifndef __KERNEL__
258 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
259                                  int max)
260 {
261         char *ptr;
262         struct obd_ioctl_data *overlay;
263         data->ioc_len = obd_ioctl_packlen(data);
264         data->ioc_version = OBD_IOCTL_VERSION;
265
266         if (*pbuf && data->ioc_len > max)
267                 return -EINVAL;
268         if (*pbuf == NULL) {
269                 *pbuf = malloc(data->ioc_len);
270         }
271         if (!*pbuf)
272                 return -ENOMEM;
273         overlay = (struct obd_ioctl_data *)*pbuf;
274         memcpy(*pbuf, data, sizeof(*data));
275
276         ptr = overlay->ioc_bulk;
277         if (data->ioc_inlbuf1)
278                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
279         if (data->ioc_inlbuf2)
280                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
281         if (data->ioc_inlbuf3)
282                 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
283         if (data->ioc_inlbuf4)
284                 LOGL(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
285         if (obd_ioctl_is_invalid(overlay))
286                 return -EINVAL;
287
288         return 0;
289 }
290
291 static inline int obd_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf,
292                                    int max)
293 {
294         char *ptr;
295         struct obd_ioctl_data *overlay;
296
297         if (!pbuf)
298                 return 1;
299         overlay = (struct obd_ioctl_data *)pbuf;
300
301         /* Preserve the caller's buffer pointers */
302         overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
303         overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
304         overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
305         overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
306
307         memcpy(data, pbuf, sizeof(*data));
308
309         ptr = overlay->ioc_bulk;
310         if (data->ioc_inlbuf1)
311                 LOGU(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
312         if (data->ioc_inlbuf2)
313                 LOGU(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
314         if (data->ioc_inlbuf3)
315                 LOGU(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
316         if (data->ioc_inlbuf4)
317                 LOGU(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
318
319         return 0;
320 }
321 #endif
322
323 #include <obd_support.h>
324
325 #ifdef __KERNEL__
326 /* function defined in lustre/obdclass/<platform>/<platform>-module.c */
327 int obd_ioctl_getdata(char **buf, int *len, void *arg);
328 int obd_ioctl_popdata(void *arg, void *data, int len);
329 #else
330 /* buffer MUST be at least the size of obd_ioctl_hdr */
331 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
332 {
333         struct obd_ioctl_hdr hdr;
334         struct obd_ioctl_data *data;
335         int err;
336         int offset = 0;
337         ENTRY;
338
339         err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
340         if (err)
341                 RETURN(err);
342
343         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
344                 CERROR("Version mismatch kernel vs application\n");
345                 RETURN(-EINVAL);
346         }
347
348         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
349                 CERROR("User buffer len %d exceeds %d max buffer\n",
350                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
351                 RETURN(-EINVAL);
352         }
353
354         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
355                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
356                 RETURN(-EINVAL);
357         }
358
359         /* XXX allocate this more intelligently, using kmalloc when
360          * appropriate */
361         OBD_VMALLOC(*buf, hdr.ioc_len);
362         if (*buf == NULL) {
363                 CERROR("Cannot allocate control buffer of len %d\n",
364                        hdr.ioc_len);
365                 RETURN(-EINVAL);
366         }
367         *len = hdr.ioc_len;
368         data = (struct obd_ioctl_data *)*buf;
369
370         err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
371         if (err) {
372                 OBD_VFREE(*buf, hdr.ioc_len);
373                 RETURN(err);
374         }
375
376         if (obd_ioctl_is_invalid(data)) {
377                 CERROR("ioctl not correctly formatted\n");
378                 OBD_VFREE(*buf, hdr.ioc_len);
379                 RETURN(-EINVAL);
380         }
381
382         if (data->ioc_inllen1) {
383                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
384                 offset += size_round(data->ioc_inllen1);
385         }
386
387         if (data->ioc_inllen2) {
388                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
389                 offset += size_round(data->ioc_inllen2);
390         }
391
392         if (data->ioc_inllen3) {
393                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
394                 offset += size_round(data->ioc_inllen3);
395         }
396
397         if (data->ioc_inllen4) {
398                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
399         }
400
401         RETURN(0);
402 }
403
404 static inline int obd_ioctl_popdata(void *arg, void *data, int len)
405 {
406         int err = copy_to_user(arg, data, len);
407         if (err)
408                 err = -EFAULT;
409         return err;
410 }
411 #endif
412
413 static inline void obd_ioctl_freedata(char *buf, int len)
414 {
415         ENTRY;
416
417         OBD_VFREE(buf, len);
418         EXIT;
419         return;
420 }
421
422 /*
423  * BSD ioctl description:
424  * #define IOC_V1       _IOR(g, n1, long)
425  * #define IOC_V2       _IOW(g, n2, long)
426  *
427  * ioctl(f, IOC_V1, arg);
428  * arg will be treated as a long value,
429  *
430  * ioctl(f, IOC_V2, arg)
431  * arg will be treated as a pointer, bsd will call
432  * copyin(buf, arg, sizeof(long))
433  *
434  * To make BSD ioctl handles argument correctly and simplely,
435  * we change _IOR to _IOWR so BSD will copyin obd_ioctl_data
436  * for us. Does this change affect Linux?  (XXX Liang)
437  */
438 #define OBD_IOC_CREATE                 _IOWR('f', 101, OBD_IOC_DATA_TYPE)
439 #define OBD_IOC_DESTROY                _IOW ('f', 104, OBD_IOC_DATA_TYPE)
440 #define OBD_IOC_PREALLOCATE            _IOWR('f', 105, OBD_IOC_DATA_TYPE)
441
442 #define OBD_IOC_SETATTR                _IOW ('f', 107, OBD_IOC_DATA_TYPE)
443 #define OBD_IOC_GETATTR                _IOWR ('f', 108, OBD_IOC_DATA_TYPE)
444 #define OBD_IOC_READ                   _IOWR('f', 109, OBD_IOC_DATA_TYPE)
445 #define OBD_IOC_WRITE                  _IOWR('f', 110, OBD_IOC_DATA_TYPE)
446
447
448 #define OBD_IOC_STATFS                 _IOWR('f', 113, OBD_IOC_DATA_TYPE)
449 #define OBD_IOC_SYNC                   _IOW ('f', 114, OBD_IOC_DATA_TYPE)
450 #define OBD_IOC_READ2                  _IOWR('f', 115, OBD_IOC_DATA_TYPE)
451 #define OBD_IOC_FORMAT                 _IOWR('f', 116, OBD_IOC_DATA_TYPE)
452 #define OBD_IOC_PARTITION              _IOWR('f', 117, OBD_IOC_DATA_TYPE)
453 #define OBD_IOC_COPY                   _IOWR('f', 120, OBD_IOC_DATA_TYPE)
454 #define OBD_IOC_MIGR                   _IOWR('f', 121, OBD_IOC_DATA_TYPE)
455 #define OBD_IOC_PUNCH                  _IOWR('f', 122, OBD_IOC_DATA_TYPE)
456
457 #define OBD_IOC_MODULE_DEBUG           _IOWR('f', 124, OBD_IOC_DATA_TYPE)
458 #define OBD_IOC_BRW_READ               _IOWR('f', 125, OBD_IOC_DATA_TYPE)
459 #define OBD_IOC_BRW_WRITE              _IOWR('f', 126, OBD_IOC_DATA_TYPE)
460 #define OBD_IOC_NAME2DEV               _IOWR('f', 127, OBD_IOC_DATA_TYPE)
461 #define OBD_IOC_UUID2DEV               _IOWR('f', 130, OBD_IOC_DATA_TYPE)
462 #define OBD_IOC_GETNAME                _IOWR('f', 131, OBD_IOC_DATA_TYPE)
463
464 #define OBD_IOC_LOV_GET_CONFIG         _IOWR('f', 132, OBD_IOC_DATA_TYPE)
465 #define OBD_IOC_CLIENT_RECOVER         _IOW ('f', 133, OBD_IOC_DATA_TYPE)
466 #define OBD_IOC_PING_TARGET            _IOW ('f', 136, OBD_IOC_DATA_TYPE)
467
468 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 139      )
469 #define OBD_IOC_NO_TRANSNO             _IOW ('f', 140, OBD_IOC_DATA_TYPE)
470 #define OBD_IOC_SET_READONLY           _IOW ('f', 141, OBD_IOC_DATA_TYPE)
471 #define OBD_IOC_ABORT_RECOVERY         _IOR ('f', 142, OBD_IOC_DATA_TYPE)
472
473 #define OBD_IOC_ROOT_SQUASH            _IOWR('f', 143, OBD_IOC_DATA_TYPE)
474
475 #define OBD_GET_VERSION                _IOWR ('f', 144, OBD_IOC_DATA_TYPE)
476
477 #define OBD_IOC_GSS_SUPPORT            _IOWR('f', 145, OBD_IOC_DATA_TYPE)
478
479 #define OBD_IOC_CLOSE_UUID             _IOWR ('f', 147, OBD_IOC_DATA_TYPE)
480
481 #define OBD_IOC_GETDEVICE              _IOWR ('f', 149, OBD_IOC_DATA_TYPE)
482 #define OBD_IOC_FID2PATH               _IOWR ('f', 150, OBD_IOC_DATA_TYPE)
483
484 #define OBD_IOC_LOV_SETSTRIPE          _IOW ('f', 154, OBD_IOC_DATA_TYPE)
485 #define OBD_IOC_LOV_GETSTRIPE          _IOW ('f', 155, OBD_IOC_DATA_TYPE)
486 #define OBD_IOC_LOV_SETEA              _IOW ('f', 156, OBD_IOC_DATA_TYPE)
487
488 #define OBD_IOC_QUOTACHECK             _IOW ('f', 160, int)
489 #define OBD_IOC_POLL_QUOTACHECK        _IOR ('f', 161, struct if_quotacheck *)
490 #define OBD_IOC_QUOTACTL               _IOWR('f', 162, struct if_quotactl *)
491
492 #define OBD_IOC_MOUNTOPT               _IOWR('f', 170, OBD_IOC_DATA_TYPE)
493
494 #define OBD_IOC_RECORD                 _IOWR('f', 180, OBD_IOC_DATA_TYPE)
495 #define OBD_IOC_ENDRECORD              _IOWR('f', 181, OBD_IOC_DATA_TYPE)
496 #define OBD_IOC_PARSE                  _IOWR('f', 182, OBD_IOC_DATA_TYPE)
497 #define OBD_IOC_DORECORD               _IOWR('f', 183, OBD_IOC_DATA_TYPE)
498 #define OBD_IOC_PROCESS_CFG            _IOWR('f', 184, OBD_IOC_DATA_TYPE)
499 #define OBD_IOC_DUMP_LOG               _IOWR('f', 185, OBD_IOC_DATA_TYPE)
500 #define OBD_IOC_CLEAR_LOG              _IOWR('f', 186, OBD_IOC_DATA_TYPE)
501 #define OBD_IOC_PARAM                  _IOW ('f', 187, OBD_IOC_DATA_TYPE)
502 #define OBD_IOC_POOL                   _IOWR('f', 188, OBD_IOC_DATA_TYPE)
503
504 #define OBD_IOC_CATLOGLIST             _IOWR('f', 190, OBD_IOC_DATA_TYPE)
505 #define OBD_IOC_LLOG_INFO              _IOWR('f', 191, OBD_IOC_DATA_TYPE)
506 #define OBD_IOC_LLOG_PRINT             _IOWR('f', 192, OBD_IOC_DATA_TYPE)
507 #define OBD_IOC_LLOG_CANCEL            _IOWR('f', 193, OBD_IOC_DATA_TYPE)
508 #define OBD_IOC_LLOG_REMOVE            _IOWR('f', 194, OBD_IOC_DATA_TYPE)
509 #define OBD_IOC_LLOG_CHECK             _IOWR('f', 195, OBD_IOC_DATA_TYPE)
510 #define OBD_IOC_LLOG_CATINFO           _IOWR('f', 196, OBD_IOC_DATA_TYPE)
511
512 #define ECHO_IOC_GET_STRIPE            _IOWR('f', 200, OBD_IOC_DATA_TYPE)
513 #define ECHO_IOC_SET_STRIPE            _IOWR('f', 201, OBD_IOC_DATA_TYPE)
514 #define ECHO_IOC_ENQUEUE               _IOWR('f', 202, OBD_IOC_DATA_TYPE)
515 #define ECHO_IOC_CANCEL                _IOWR('f', 203, OBD_IOC_DATA_TYPE)
516
517 /* XXX _IOWR('f', 250, long) has been defined in
518  * libcfs/include/libcfs/libcfs_private.h for debug, don't use it
519  */
520
521 /* Until such time as we get_info the per-stripe maximum from the OST,
522  * we define this to be 2T - 4k, which is the ext3 maxbytes. */
523 #define LUSTRE_STRIPE_MAXBYTES 0x1fffffff000ULL
524
525 /* #define POISON_BULK 0 */
526
527 /*
528  * l_wait_event is a flexible sleeping function, permitting simple caller
529  * configuration of interrupt and timeout sensitivity along with actions to
530  * be performed in the event of either exception.
531  *
532  * The first form of usage looks like this:
533  *
534  * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
535  *                                           intr_handler, callback_data);
536  * rc = l_wait_event(waitq, condition, &lwi);
537  *
538  * l_wait_event() makes the current process wait on 'waitq' until 'condition'
539  * is TRUE or a "killable" signal (SIGTERM, SIKGILL, SIGINT) is pending.  It
540  * returns 0 to signify 'condition' is TRUE, but if a signal wakes it before
541  * 'condition' becomes true, it optionally calls the specified 'intr_handler'
542  * if not NULL, and returns -EINTR.
543  *
544  * If a non-zero timeout is specified, signals are ignored until the timeout
545  * has expired.  At this time, if 'timeout_handler' is not NULL it is called.
546  * If it returns FALSE l_wait_event() continues to wait as described above with
547  * signals enabled.  Otherwise it returns -ETIMEDOUT.
548  *
549  * LWI_INTR(intr_handler, callback_data) is shorthand for
550  * LWI_TIMEOUT_INTR(0, NULL, intr_handler, callback_data)
551  *
552  * The second form of usage looks like this:
553  *
554  * struct l_wait_info lwi = LWI_TIMEOUT(timeout, timeout_handler);
555  * rc = l_wait_event(waitq, condition, &lwi);
556  *
557  * This form is the same as the first except that it COMPLETELY IGNORES
558  * SIGNALS.  The caller must therefore beware that if 'timeout' is zero, or if
559  * 'timeout_handler' is not NULL and returns FALSE, then the ONLY thing that
560  * can unblock the current process is 'condition' becoming TRUE.
561  *
562  * Another form of usage is:
563  * struct l_wait_info lwi = LWI_TIMEOUT_INTERVAL(timeout, interval,
564  *                                               timeout_handler);
565  * rc = l_wait_event(waitq, condition, &lwi);
566  * This is the same as previous case, but condition is checked once every
567  * 'interval' jiffies (if non-zero).
568  *
569  * Subtle synchronization point: this macro does *not* necessary takes
570  * wait-queue spin-lock before returning, and, hence, following idiom is safe
571  * ONLY when caller provides some external locking:
572  *
573  *             Thread1                            Thread2
574  *
575  *   l_wait_event(&obj->wq, ....);                                       (1)
576  *
577  *                                    wake_up(&obj->wq):                 (2)
578  *                                         spin_lock(&q->lock);          (2.1)
579  *                                         __wake_up_common(q, ...);     (2.2)
580  *                                         spin_unlock(&q->lock, flags); (2.3)
581  *
582  *   OBD_FREE_PTR(obj);                                                  (3)
583  *
584  * As l_wait_event() may "short-cut" execution and return without taking
585  * wait-queue spin-lock, some additional synchronization is necessary to
586  * guarantee that step (3) can begin only after (2.3) finishes.
587  *
588  * XXX nikita: some ptlrpc daemon threads have races of that sort.
589  *
590  */
591
592 #define LWI_ON_SIGNAL_NOOP ((void (*)(void *))(-1))
593
594 struct l_wait_info {
595         cfs_duration_t lwi_timeout;
596         cfs_duration_t lwi_interval;
597         int  (*lwi_on_timeout)(void *);
598         void (*lwi_on_signal)(void *);
599         void  *lwi_cb_data;
600 };
601
602 /* NB: LWI_TIMEOUT ignores signals completely */
603 #define LWI_TIMEOUT(time, cb, data)             \
604 ((struct l_wait_info) {                         \
605         .lwi_timeout    = time,                 \
606         .lwi_on_timeout = cb,                   \
607         .lwi_cb_data    = data,                 \
608         .lwi_interval   = 0                     \
609 })
610
611 #define LWI_TIMEOUT_INTERVAL(time, interval, cb, data)  \
612 ((struct l_wait_info) {                                 \
613         .lwi_timeout    = time,                         \
614         .lwi_on_timeout = cb,                           \
615         .lwi_cb_data    = data,                         \
616         .lwi_interval   = interval                      \
617 })
618
619 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)   \
620 ((struct l_wait_info) {                                 \
621         .lwi_timeout    = time,                         \
622         .lwi_on_timeout = time_cb,                      \
623         .lwi_on_signal = sig_cb,                        \
624         .lwi_cb_data    = data,                         \
625         .lwi_interval    = 0                            \
626 })
627
628 #define LWI_INTR(cb, data)  LWI_TIMEOUT_INTR(0, NULL, cb, data)
629
630 #ifdef __KERNEL__
631
632 /*
633  * wait for @condition to become true, but no longer than timeout, specified
634  * by @info.
635  */
636 #define __l_wait_event(wq, condition, info, ret, excl)                         \
637 do {                                                                           \
638         cfs_waitlink_t __wait;                                                 \
639         cfs_duration_t __timeout = info->lwi_timeout;                          \
640         cfs_sigset_t   __blocked;                                              \
641                                                                                \
642         ret = 0;                                                               \
643         if (condition)                                                         \
644                 break;                                                         \
645                                                                                \
646         cfs_waitlink_init(&__wait);                                            \
647         if (excl)                                                              \
648                 cfs_waitq_add_exclusive(&wq, &__wait);                         \
649         else                                                                   \
650                 cfs_waitq_add(&wq, &__wait);                                   \
651                                                                                \
652         /* Block all signals (just the non-fatal ones if no timeout). */       \
653         if (info->lwi_on_signal != NULL && __timeout == 0)                     \
654                 __blocked = l_w_e_set_sigs(LUSTRE_FATAL_SIGS);                 \
655         else                                                                   \
656                 __blocked = l_w_e_set_sigs(0);                                 \
657                                                                                \
658         for (;;) {                                                             \
659                 set_current_state(TASK_INTERRUPTIBLE);                         \
660                                                                                \
661                 if (condition)                                                 \
662                         break;                                                 \
663                                                                                \
664                 if (__timeout == 0) {                                          \
665                         cfs_waitq_wait(&__wait, CFS_TASK_INTERRUPTIBLE);       \
666                 } else {                                                       \
667                         cfs_duration_t interval = info->lwi_interval?          \
668                                              min_t(cfs_duration_t,             \
669                                                  info->lwi_interval,__timeout):\
670                                              __timeout;                        \
671                         cfs_duration_t remaining = cfs_waitq_timedwait(&__wait,\
672                                                    CFS_TASK_INTERRUPTIBLE,     \
673                                                    interval);                  \
674                         __timeout = cfs_time_sub(__timeout,                    \
675                                             cfs_time_sub(interval, remaining));\
676                         if (__timeout == 0) {                                  \
677                                 if (info->lwi_on_timeout == NULL ||            \
678                                     info->lwi_on_timeout(info->lwi_cb_data)) { \
679                                         ret = -ETIMEDOUT;                      \
680                                         break;                                 \
681                                 }                                              \
682                                 /* Take signals after the timeout expires. */  \
683                                 if (info->lwi_on_signal != NULL)               \
684                                     (void)l_w_e_set_sigs(LUSTRE_FATAL_SIGS);   \
685                         }                                                      \
686                 }                                                              \
687                                                                                \
688                 if (condition)                                                 \
689                         break;                                                 \
690                 if (cfs_signal_pending()) {                                    \
691                         if (info->lwi_on_signal != NULL && __timeout == 0) {   \
692                                 if (info->lwi_on_signal != LWI_ON_SIGNAL_NOOP) \
693                                         info->lwi_on_signal(info->lwi_cb_data);\
694                                 ret = -EINTR;                                  \
695                                 break;                                         \
696                         }                                                      \
697                         /* We have to do this here because some signals */     \
698                         /* are not blockable - ie from strace(1).       */     \
699                         /* In these cases we want to schedule_timeout() */     \
700                         /* again, because we don't want that to return  */     \
701                         /* -EINTR when the RPC actually succeeded.      */     \
702                         /* the RECALC_SIGPENDING below will deliver the */     \
703                         /* signal properly.                             */     \
704                         cfs_clear_sigpending();                                \
705                 }                                                              \
706         }                                                                      \
707                                                                                \
708         cfs_block_sigs(__blocked);                                             \
709                                                                                \
710         set_current_state(TASK_RUNNING);                                       \
711         cfs_waitq_del(&wq, &__wait);                                           \
712 } while (0)
713
714 #else /* !__KERNEL__ */
715 #define __l_wait_event(wq, condition, info, ret, excl)                  \
716 do {                                                                    \
717         long __timeout = info->lwi_timeout;                             \
718         long __now;                                                     \
719         long __then = 0;                                                \
720         int  __timed_out = 0;                                           \
721         int  __interval = obd_timeout;                                  \
722                                                                         \
723         ret = 0;                                                        \
724         if (condition)                                                  \
725                 break;                                                  \
726                                                                         \
727         if (__timeout != 0)                                             \
728                 __then = time(NULL);                                    \
729                                                                         \
730         if (__timeout && __timeout < __interval)                        \
731                 __interval = __timeout;                                 \
732         if (info->lwi_interval && info->lwi_interval < __interval)      \
733                 __interval = info->lwi_interval;                        \
734                                                                         \
735         while (!(condition)) {                                          \
736                 liblustre_wait_event(__interval);                       \
737                 if (condition)                                          \
738                         break;                                          \
739                                                                         \
740                 if (!__timed_out && info->lwi_timeout != 0) {           \
741                         __now = time(NULL);                             \
742                         __timeout -= __now - __then;                    \
743                         __then = __now;                                 \
744                                                                         \
745                         if (__timeout > 0)                              \
746                                 continue;                               \
747                                                                         \
748                         __timeout = 0;                                  \
749                         __timed_out = 1;                                \
750                         if (info->lwi_on_timeout == NULL ||             \
751                             info->lwi_on_timeout(info->lwi_cb_data)) {  \
752                                 ret = -ETIMEDOUT;                       \
753                                 break;                                  \
754                         }                                               \
755                 }                                                       \
756         }                                                               \
757 } while (0)
758
759 #endif /* __KERNEL__ */
760
761
762 #define l_wait_event(wq, condition, info)                       \
763 ({                                                              \
764         int                 __ret;                              \
765         struct l_wait_info *__info = (info);                    \
766                                                                 \
767         __l_wait_event(wq, condition, __info, __ret, 0);        \
768         __ret;                                                  \
769 })
770
771 #define l_wait_event_exclusive(wq, condition, info)             \
772 ({                                                              \
773         int                 __ret;                              \
774         struct l_wait_info *__info = (info);                    \
775                                                                 \
776         __l_wait_event(wq, condition, __info, __ret, 1);        \
777         __ret;                                                  \
778 })
779
780 #define cfs_wait_event(wq, condition)                          \
781 ({                                                              \
782         struct l_wait_info lwi = { 0 };                         \
783         l_wait_event(wq, condition, &lwi);                      \
784 })
785
786 #ifdef __KERNEL__
787 #define LIBLUSTRE_CLIENT (0)
788 #else
789 #define LIBLUSTRE_CLIENT (1)
790 #endif
791
792 #endif /* _LUSTRE_LIB_H */