Whamcloud - gitweb
merge b_devel into HEAD (20030626 merge tag) for 0.7.1
[fs/lustre-release.git] / lustre / include / linux / lustre_lib.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * Basic Lustre library routines.
22  *
23  */
24
25 #ifndef _LUSTRE_LIB_H
26 #define _LUSTRE_LIB_H
27
28 #include <config.h>
29
30 #ifndef __KERNEL__
31 # include <string.h>
32 # include <sys/types.h>
33 #else
34 # include <asm/semaphore.h>
35 # include <linux/sched.h>
36 # include <linux/signal.h>
37 # include <linux/types.h>
38 #endif
39 #include <linux/portals_lib.h>
40 #include <linux/kp30.h> /* XXX just for LASSERT! */
41 #include <linux/lustre_idl.h>
42
43 #ifndef LPU64
44 #if BITS_PER_LONG > 32
45 #define LPU64 "%lu"
46 #define LPD64 "%ld"
47 #define LPX64 "%#lx"
48 #else
49 #define LPU64 "%Lu"
50 #define LPD64 "%Ld"
51 #define LPX64 "%#Lx"
52 #endif
53 #endif
54
55 /* target.c */
56 struct ptlrpc_request;
57 struct recovd_data;
58 struct recovd_obd;
59 struct obd_export;
60 #include <linux/lustre_ha.h>
61 #include <linux/lustre_net.h>
62 #include <linux/lustre_compat25.h>
63
64 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler);
65 int target_handle_disconnect(struct ptlrpc_request *req);
66 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
67                             struct obd_uuid *cluuid);
68 int target_handle_ping(struct ptlrpc_request *req);
69 void target_cancel_recovery_timer(struct obd_device *obd);
70
71 #define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 * HZ / 2) /* *waves hands* */
72 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler);
73 void target_abort_recovery(void *data);
74 int target_queue_recovery_request(struct ptlrpc_request *req,
75                                   struct obd_device *obd);
76 int target_queue_final_reply(struct ptlrpc_request *req, int rc);
77 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
78
79 /* client.c */
80
81 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
82 int client_sanobd_setup(struct obd_device *obddev, obd_count len, void *buf);
83 int client_obd_cleanup(struct obd_device * obddev, int force, int failover);
84 struct client_obd *client_conn2cli(struct lustre_handle *conn);
85 struct obd_device *client_tgtuuid2obd(struct obd_uuid *tgtuuid);
86
87 /* It is important that och_fh remain the first item in this structure: that
88  * way, we don't have to re-pack the obdo's inline data before we send it to
89  * the server, we can just send the whole struct unaltered. */
90 struct obd_client_handle {
91         struct lustre_handle och_fh;
92         struct ptlrpc_request *och_req;
93         __u32 och_magic;
94 };
95 #define OBD_CLIENT_HANDLE_MAGIC 0xd15ea5ed
96
97 /* statfs_pack.c */
98 int obd_self_statfs(struct obd_device *dev, struct statfs *sfs);
99
100 /* l_lock.c */
101 struct lustre_lock {
102         int l_depth;
103         struct task_struct *l_owner;
104         struct semaphore l_sem;
105         spinlock_t l_spin;
106 };
107
108 void l_lock_init(struct lustre_lock *);
109 void l_lock(struct lustre_lock *);
110 void l_unlock(struct lustre_lock *);
111 int l_has_lock(struct lustre_lock *);
112
113 /* simple.c */
114 struct obd_ucred {
115         __u32 ouc_fsuid;
116         __u32 ouc_fsgid;
117         __u32 ouc_cap;
118         __u32 ouc_suppgid1;
119         __u32 ouc_suppgid2;
120 };
121
122 #define OBD_RUN_CTXT_MAGIC      0xC0FFEEAA
123 #define OBD_CTXT_DEBUG          /* development-only debugging */
124 struct obd_run_ctxt {
125         struct vfsmount *pwdmnt;
126         struct dentry   *pwd;
127         mm_segment_t     fs;
128         struct obd_ucred ouc;
129         int              ngroups;
130 #ifdef OBD_CTXT_DEBUG
131         __u32            magic;
132 #endif
133 };
134
135
136 #ifdef OBD_CTXT_DEBUG
137 #define OBD_SET_CTXT_MAGIC(ctxt) (ctxt)->magic = OBD_RUN_CTXT_MAGIC
138 #else
139 #define OBD_SET_CTXT_MAGIC(ctxt) do {} while(0)
140 #endif
141
142 #ifdef __KERNEL__
143
144 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
145                struct obd_ucred *cred);
146 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
147               struct obd_ucred *cred);
148 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode);
149 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode);
150 int lustre_fread(struct file *file, void *buf, int len, loff_t *off);
151 int lustre_fwrite(struct file *file, const void *buf, int len, loff_t *off);
152 int lustre_fsync(struct file *file);
153
154 static inline void l_dput(struct dentry *de)
155 {
156         if (!de || IS_ERR(de))
157                 return;
158         //shrink_dcache_parent(de);
159         LASSERT(atomic_read(&de->d_count) > 0);
160         dput(de);
161 }
162
163 /* We need to hold the inode semaphore over the dcache lookup itself, or we
164  * run the risk of entering the filesystem lookup path concurrently on SMP
165  * systems, and instantiating two inodes for the same entry.  We still
166  * protect against concurrent addition/removal races with the DLM locking.
167  */
168 static inline struct dentry *ll_lookup_one_len(char *fid_name,
169                                                struct dentry *dparent,
170                                                int fid_namelen)
171 {
172         struct dentry *dchild;
173
174         down(&dparent->d_inode->i_sem);
175         dchild = lookup_one_len(fid_name, dparent, fid_namelen);
176         up(&dparent->d_inode->i_sem);
177
178         return dchild;
179 }
180
181 static inline void ll_sleep(int t)
182 {
183         set_current_state(TASK_INTERRUPTIBLE);
184         schedule_timeout(t * HZ);
185         set_current_state(TASK_RUNNING);
186 }
187 #endif
188
189 #define LL_FID_NAMELEN         (16 + 1 + 8 + 1)
190 static inline int ll_fid2str(char *str, __u64 id, __u32 generation)
191 {
192         return sprintf(str, "%llx:%08x", (unsigned long long)id, generation);
193 }
194
195 #include <linux/portals_lib.h>
196
197 /*
198  *   OBD IOCTLS
199  */
200 #define OBD_IOCTL_VERSION 0x00010003
201
202 struct obd_ioctl_data {
203         uint32_t ioc_len;
204         uint32_t ioc_version;
205
206         uint64_t ioc_cookie;
207         uint32_t ioc_conn1;
208         uint32_t ioc_conn2;
209
210         struct obdo ioc_obdo1;
211         struct obdo ioc_obdo2;
212
213         obd_size         ioc_count;
214         obd_off          ioc_offset;
215         uint32_t         ioc_dev;
216         uint32_t         ioc_command;
217
218         uint64_t ioc_nid;
219         uint32_t ioc_nal;
220
221         /* buffers the kernel will treat as user pointers */
222         uint32_t ioc_plen1;
223         char    *ioc_pbuf1;
224         uint32_t ioc_plen2;
225         char    *ioc_pbuf2;
226
227         /* inline buffers for various arguments */
228         uint32_t ioc_inllen1;
229         char    *ioc_inlbuf1;
230         uint32_t ioc_inllen2;
231         char    *ioc_inlbuf2;
232         uint32_t ioc_inllen3;
233         char    *ioc_inlbuf3;
234         uint32_t ioc_inllen4;
235         char    *ioc_inlbuf4;
236
237         char    ioc_bulk[0];
238 };
239
240 struct obd_ioctl_hdr {
241         uint32_t ioc_len;
242         uint32_t ioc_version;
243 };
244
245 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
246 {
247         int len = size_round(sizeof(struct obd_ioctl_data));
248         len += size_round(data->ioc_inllen1);
249         len += size_round(data->ioc_inllen2);
250         len += size_round(data->ioc_inllen3);
251         len += size_round(data->ioc_inllen4);
252         return len;
253 }
254
255
256 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
257 {
258         if (data->ioc_len > (1<<30)) {
259                 printk("OBD ioctl: ioc_len larger than 1<<30\n");
260                 return 1;
261         }
262         if (data->ioc_inllen1 > (1<<30)) {
263                 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
264                 return 1;
265         }
266         if (data->ioc_inllen2 > (1<<30)) {
267                 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
268                 return 1;
269         }
270         if (data->ioc_inllen3 > (1<<30)) {
271                 printk("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
272                 return 1;
273         }
274         if (data->ioc_inllen4 > (1<<30)) {
275                 printk("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
276                 return 1;
277         }
278         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
279                 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
280                 return 1;
281         }
282         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
283                 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
284                 return 1;
285         }
286         if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
287                 printk("OBD ioctl: inlbuf3 pointer but 0 length\n");
288                 return 1;
289         }
290         if (data->ioc_inlbuf4 && !data->ioc_inllen4) {
291                 printk("OBD ioctl: inlbuf4 pointer but 0 length\n");
292                 return 1;
293         }
294         if (data->ioc_pbuf1 && !data->ioc_plen1) {
295                 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
296                 return 1;
297         }
298         if (data->ioc_pbuf2 && !data->ioc_plen2) {
299                 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
300                 return 1;
301         }
302         if (data->ioc_plen1 && !data->ioc_pbuf1) {
303                 printk("OBD ioctl: plen1 set but NULL pointer\n");
304                 return 1;
305         }
306         if (data->ioc_plen2 && !data->ioc_pbuf2) {
307                 printk("OBD ioctl: plen2 set but NULL pointer\n");
308                 return 1;
309         }
310         if (obd_ioctl_packlen(data) != data->ioc_len) {
311                 printk("OBD ioctl: packlen exceeds ioc_len (%d != %d)\n",
312                        obd_ioctl_packlen(data), data->ioc_len);
313                 return 1;
314         }
315         return 0;
316 }
317
318 #ifndef __KERNEL__
319 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
320                                  int max)
321 {
322         char *ptr;
323         struct obd_ioctl_data *overlay;
324         data->ioc_len = obd_ioctl_packlen(data);
325         data->ioc_version = OBD_IOCTL_VERSION;
326
327         if (*pbuf && data->ioc_len > max)
328                 return 1;
329         if (*pbuf == NULL) {
330                 *pbuf = malloc(data->ioc_len);
331         }
332         if (!*pbuf)
333                 return 1;
334         overlay = (struct obd_ioctl_data *)*pbuf;
335         memcpy(*pbuf, data, sizeof(*data));
336
337         ptr = overlay->ioc_bulk;
338         if (data->ioc_inlbuf1)
339                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
340         if (data->ioc_inlbuf2)
341                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
342         if (data->ioc_inlbuf3)
343                 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
344         if (data->ioc_inlbuf4)
345                 LOGL(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
346         if (obd_ioctl_is_invalid(overlay))
347                 return 1;
348
349         return 0;
350 }
351
352 static inline int obd_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf,
353                                    int max)
354 {
355         char *ptr;
356         struct obd_ioctl_data *overlay;
357
358         if (!pbuf)
359                 return 1;
360         overlay = (struct obd_ioctl_data *)pbuf;
361
362         /* Preserve the caller's buffer pointers */
363         overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
364         overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
365         overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
366         overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
367
368         memcpy(data, pbuf, sizeof(*data));
369
370         ptr = overlay->ioc_bulk;
371         if (data->ioc_inlbuf1)
372                 LOGU(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
373         if (data->ioc_inlbuf2)
374                 LOGU(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
375         if (data->ioc_inlbuf3)
376                 LOGU(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
377         if (data->ioc_inlbuf4)
378                 LOGU(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
379
380         return 0;
381 }
382 #endif
383
384 #include <linux/obd_support.h>
385
386 /* buffer MUST be at least the size of obd_ioctl_hdr */
387 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
388 {
389         struct obd_ioctl_hdr hdr;
390         struct obd_ioctl_data *data;
391         int err;
392         ENTRY;
393
394         err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
395         if ( err ) {
396                 EXIT;
397                 return err;
398         }
399
400         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
401                 CERROR("Version mismatch kernel vs application\n");
402                 return -EINVAL;
403         }
404
405         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
406                 CERROR("User buffer len %d exceeds %d max buffer\n",
407                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
408                 return -EINVAL;
409         }
410
411         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
412                 printk("OBD: user buffer too small for ioctl\n");
413                 return -EINVAL;
414         }
415
416         /* XXX allocate this more intelligently, using kmalloc when
417          * appropriate */
418         OBD_VMALLOC(*buf, hdr.ioc_len);
419         if (*buf == NULL) {
420                 CERROR("Cannot allocate control buffer of len %d\n",
421                        hdr.ioc_len);
422                 RETURN(-EINVAL);
423         }
424         *len = hdr.ioc_len;
425         data = (struct obd_ioctl_data *)*buf;
426
427         err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
428         if ( err ) {
429                 EXIT;
430                 return err;
431         }
432
433         if (obd_ioctl_is_invalid(data)) {
434                 CERROR("ioctl not correctly formatted\n");
435                 return -EINVAL;
436         }
437
438         if (data->ioc_inllen1) {
439                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
440         }
441
442         if (data->ioc_inllen2) {
443                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
444                         size_round(data->ioc_inllen1);
445         }
446
447         if (data->ioc_inllen3) {
448                 data->ioc_inlbuf3 = &data->ioc_bulk[0] +
449                         size_round(data->ioc_inllen1) +
450                         size_round(data->ioc_inllen2);
451         }
452
453         if (data->ioc_inllen4) {
454                 data->ioc_inlbuf4 = &data->ioc_bulk[0] +
455                         size_round(data->ioc_inllen1) +
456                         size_round(data->ioc_inllen2) + 
457                         size_round(data->ioc_inllen3) ;
458         }
459
460         EXIT;
461         return 0;
462 }
463
464 static inline void obd_ioctl_freedata(char *buf, int len)
465 {
466         ENTRY;
467
468         OBD_VFREE(buf, len);
469         EXIT;
470         return;
471 }
472
473 #define OBD_IOC_CREATE                 _IOR ('f', 101, long)
474 #define OBD_IOC_SETUP                  _IOW ('f', 102, long)
475 #define OBD_IOC_CLEANUP                _IO  ('f', 103      )
476 #define OBD_IOC_DESTROY                _IOW ('f', 104, long)
477 #define OBD_IOC_PREALLOCATE            _IOWR('f', 105, long)
478
479 #define OBD_IOC_SETATTR                _IOW ('f', 107, long)
480 #define OBD_IOC_GETATTR                _IOR ('f', 108, long)
481 #define OBD_IOC_READ                   _IOWR('f', 109, long)
482 #define OBD_IOC_WRITE                  _IOWR('f', 110, long)
483 #define OBD_IOC_CONNECT                _IOR ('f', 111, long)
484 #define OBD_IOC_DISCONNECT             _IOW ('f', 112, long)
485 #define OBD_IOC_STATFS                 _IOWR('f', 113, long)
486 #define OBD_IOC_SYNC                   _IOR ('f', 114, long)
487 #define OBD_IOC_READ2                  _IOWR('f', 115, long)
488 #define OBD_IOC_FORMAT                 _IOWR('f', 116, long)
489 #define OBD_IOC_PARTITION              _IOWR('f', 117, long)
490 #define OBD_IOC_ATTACH                 _IOWR('f', 118, long)
491 #define OBD_IOC_DETACH                 _IOWR('f', 119, long)
492 #define OBD_IOC_COPY                   _IOWR('f', 120, long)
493 #define OBD_IOC_MIGR                   _IOWR('f', 121, long)
494 #define OBD_IOC_PUNCH                  _IOWR('f', 122, long)
495 #define OBD_IOC_DEVICE                 _IOWR('f', 123, long)
496 #define OBD_IOC_MODULE_DEBUG           _IOWR('f', 124, long)
497 #define OBD_IOC_BRW_READ               _IOWR('f', 125, long)
498 #define OBD_IOC_BRW_WRITE              _IOWR('f', 126, long)
499 #define OBD_IOC_NAME2DEV               _IOWR('f', 127, long)
500 #define OBD_IOC_NEWDEV                 _IOWR('f', 128, long)
501 #define OBD_IOC_LIST                   _IOWR('f', 129, long)
502 #define OBD_IOC_UUID2DEV               _IOWR('f', 130, long)
503
504 #define OBD_IOC_LOV_SET_CONFIG         _IOWR('f', 131, long)
505 #define OBD_IOC_LOV_GET_CONFIG         _IOWR('f', 132, long)
506 #define OBD_IOC_LOV_CONFIG             OBD_IOC_LOV_SET_CONFIG
507 #define OBD_IOC_CLIENT_RECOVER         _IOW ('f', 133, long)
508
509 #define OBD_IOC_OPEN                   _IOWR('f', 134, long)
510 #define OBD_IOC_CLOSE                  _IOWR('f', 135, long)
511
512 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 139      )
513 #define OBD_IOC_NO_TRANSNO             _IOW ('f', 140, long)
514 #define OBD_IOC_SET_READONLY           _IOW ('f', 141, long)
515 #define OBD_IOC_ABORT_RECOVERY         _IOR ('f', 142, long)
516
517 #define OBD_GET_VERSION                _IOWR ('f', 144, long)
518
519 #define OBD_IOC_ADD_UUID               _IOWR ('f', 145, long)
520 #define OBD_IOC_DEL_UUID               _IOWR ('f', 146, long)
521 #define OBD_IOC_CLOSE_UUID             _IOWR ('f', 147, long)
522
523 #define OBD_IOC_MOUNTOPT               _IOWR('f', 170, long)
524
525 #define ECHO_IOC_GET_STRIPE            _IOWR('f', 200, long)
526 #define ECHO_IOC_SET_STRIPE            _IOWR('f', 201, long)
527 #define ECHO_IOC_ENQUEUE               _IOWR('f', 202, long)
528 #define ECHO_IOC_CANCEL                _IOWR('f', 203, long)
529
530 /* XXX _IOWR('f', 250, long) has been defined in
531  * portals/include/linux/kp30.h for debug, don't use it
532  */
533
534 /* Until such time as we get_info the per-stripe maximum from the OST,
535  * we define this to be 2T - 4k, which is the ext3 maxbytes. */
536 #define LUSTRE_STRIPE_MAXBYTES 0x1fffffff000ULL
537
538 #define CHECKSUM_BULK 0
539
540 #if CHECKSUM_BULK
541 static inline void ost_checksum(__u64 *cksum, void *addr, int len)
542 {
543         unsigned char *ptr = (unsigned char *)addr;
544         __u64          sum = 0;
545
546         /* very stupid, but means I don't have to think about byte order */
547         while (len-- > 0)
548                 sum += *ptr++;
549
550         *cksum = (*cksum << 2) + sum;
551 }
552 #endif
553
554 /*
555  * l_wait_event is a flexible sleeping function, permitting simple caller
556  * configuration of interrupt and timeout sensitivity along with actions to
557  * be performed in the event of either exception.
558  *
559  * Common usage looks like this:
560  *
561  * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
562  *                                           intr_handler, callback_data);
563  * rc = l_wait_event(waitq, condition, &lwi);
564  *
565  * (LWI_TIMEOUT and LWI_INTR macros are available for timeout- and
566  * interrupt-only variants, respectively.)
567  *
568  * If a timeout is specified, the timeout_handler will be invoked in the event
569  * that the timeout expires before the process is awakened.  (Note that any
570  * waking of the process will restart the timeout, even if the condition is
571  * not satisfied and the process immediately returns to sleep.  This might be
572  * considered a bug.)  If the timeout_handler returns non-zero, l_wait_event
573  * will return -ETIMEDOUT and the caller will continue.  If the handler returns
574  * zero instead, the process will go back to sleep until it is awakened by the
575  * waitq or some similar mechanism, or an interrupt occurs (if the caller has
576  * asked for interrupts to be detected).  The timeout will only fire once, so
577  * callers should take care that a timeout_handler which returns zero will take
578  * future steps to awaken the process.  N.B. that these steps must include
579  * making the provided condition become true.
580  *
581  * If the interrupt flag (lwi_signals) is non-zero, then the process will be
582  * interruptible, and will be awakened by any "killable" signal (SIGTERM,
583  * SIGKILL or SIGINT).  If a timeout is also specified, then the process will
584  * only become interruptible _after_ the timeout has expired, though it can be
585  * awakened by a signal that was delivered before the timeout and is still
586  * pending when the timeout expires.  If a timeout is not specified, the process
587  * will be interruptible at all times during l_wait_event.
588  */
589
590 struct l_wait_info {
591         long   lwi_timeout;
592         int  (*lwi_on_timeout)(void *);
593         long   lwi_signals;
594         void (*lwi_on_signal)(void *);
595         void  *lwi_cb_data;
596 };
597
598 #define LWI_TIMEOUT(time, cb, data)                                            \
599 ((struct l_wait_info) {                                                        \
600         lwi_timeout:    time,                                                  \
601         lwi_on_timeout: cb,                                                    \
602         lwi_cb_data:    data                                                   \
603 })
604
605 #define LWI_INTR(cb, data)                                                     \
606 ((struct l_wait_info) {                                                        \
607         lwi_signals:   1,                                                      \
608         lwi_on_signal: cb,                                                     \
609         lwi_cb_data:   data                                                    \
610 })
611
612 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)                          \
613 ((struct l_wait_info) {                                                        \
614         lwi_timeout:    time,                                                  \
615         lwi_on_timeout: time_cb,                                               \
616         lwi_signals:    1,                                                     \
617         lwi_on_signal:  sig_cb,                                                \
618         lwi_cb_data:    data                                                   \
619 })
620
621 #define LUSTRE_FATAL_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) |                \
622                            sigmask(SIGTERM) | sigmask(SIGQUIT))
623
624 #ifdef __KERNEL__
625 static inline sigset_t l_w_e_set_sigs(int sigs)
626 {
627         sigset_t old;
628         unsigned long irqflags;
629
630         SIGNAL_MASK_LOCK(current, irqflags);
631         old = current->blocked;
632         siginitsetinv(&current->blocked, sigs);
633         RECALC_SIGPENDING;
634         SIGNAL_MASK_UNLOCK(current, irqflags);
635
636         return old;
637 }
638
639 #define __l_wait_event(wq, condition, info, ret)                               \
640 do {                                                                           \
641         wait_queue_t __wait;                                                   \
642         int __timed_out = 0;                                                   \
643         unsigned long irqflags;                                                \
644         sigset_t blocked;                                                      \
645                                                                                \
646         init_waitqueue_entry(&__wait, current);                                \
647         add_wait_queue(&wq, &__wait);                                          \
648                                                                                \
649         /* Block all signals (just the non-fatal ones if no timeout). */       \
650         if (info->lwi_signals && !info->lwi_timeout)                           \
651             blocked = l_w_e_set_sigs(LUSTRE_FATAL_SIGS);                       \
652         else                                                                   \
653             blocked = l_w_e_set_sigs(0);                                       \
654                                                                                \
655         for (;;) {                                                             \
656             set_current_state(TASK_INTERRUPTIBLE);                             \
657             if (condition)                                                     \
658                     break;                                                     \
659             if (signal_pending(current)) {                                     \
660                 if (info->lwi_on_signal)                                       \
661                         info->lwi_on_signal(info->lwi_cb_data);                \
662                 ret = -EINTR;                                                  \
663                 break;                                                         \
664             }                                                                  \
665             if (info->lwi_timeout && !__timed_out) {                           \
666                 if (schedule_timeout(info->lwi_timeout) == 0) {                \
667                     __timed_out = 1;                                           \
668                     if (!info->lwi_on_timeout ||                               \
669                         info->lwi_on_timeout(info->lwi_cb_data)) {             \
670                         ret = -ETIMEDOUT;                                      \
671                         break;                                                 \
672                     }                                                          \
673                     /* We'll take signals after a timeout. */                  \
674                     if (info->lwi_signals)                                     \
675                         (void)l_w_e_set_sigs(LUSTRE_FATAL_SIGS);               \
676                 }                                                              \
677             } else {                                                           \
678                 schedule();                                                    \
679             }                                                                  \
680         }                                                                      \
681                                                                                \
682         SIGNAL_MASK_LOCK(current, irqflags);                                   \
683         current->blocked = blocked;                                            \
684         RECALC_SIGPENDING;                                                     \
685         SIGNAL_MASK_UNLOCK(current, irqflags);                                 \
686                                                                                \
687         current->state = TASK_RUNNING;                                         \
688         remove_wait_queue(&wq, &__wait);                                       \
689 } while(0)
690
691 #define l_wait_event(wq, condition, info)                                      \
692 ({                                                                             \
693         int __ret = 0;                                                         \
694         struct l_wait_info *__info = (info);                                   \
695         if (!(condition))                                                      \
696                 __l_wait_event(wq, condition, __info, __ret);                  \
697         __ret;                                                                 \
698 })
699 #else
700 #define l_wait_event(wq, condition, info)       \
701 ({                                              \
702         0;                                      \
703 })
704 #endif /* __KERNEL__ */
705
706 #endif /* _LUSTRE_LIB_H */