Whamcloud - gitweb
merge b_devel into HEAD. Includes:
[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 #ifndef __KERNEL__
29 # include <string.h>
30 #else
31 # include <asm/semaphore.h>
32 # include <linux/sched.h>
33 # include <linux/signal.h>
34 #endif
35 #include <linux/types.h>
36 #include <linux/portals_lib.h>
37 #include <linux/kp30.h> /* XXX just for LASSERT! */
38 #include <linux/lustre_idl.h>
39
40 #ifndef LPU64
41 #if BITS_PER_LONG > 32
42 #define LPU64 "%lu"
43 #define LPD64 "%ld"
44 #define LPX64 "%#lx"
45 #else
46 #define LPU64 "%Lu"
47 #define LPD64 "%Ld"
48 #define LPX64 "%#Lx"
49 #endif
50 #endif
51
52 /* target.c */
53 struct ptlrpc_request;
54 struct obd_device;
55 struct recovd_data;
56 struct recovd_obd;
57 struct obd_export;
58 #include <linux/lustre_ha.h>
59 #include <linux/lustre_net.h>
60
61
62 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler);
63 int target_handle_disconnect(struct ptlrpc_request *req);
64 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
65                             struct obd_uuid *cluuid);
66 int target_revoke_connection(struct recovd_data *rd, int phase);
67
68 #define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 * HZ / 2) /* *waves hands* */
69 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler);
70 void target_abort_recovery(void *data);
71 int target_queue_recovery_request(struct ptlrpc_request *req,
72                                   struct obd_device *obd);
73 int target_queue_final_reply(struct ptlrpc_request *req, int rc);
74
75 /* client.c */
76 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
77                        struct obd_uuid *cluuid, struct recovd_obd *recovd,
78                        ptlrpc_recovery_cb_t recover);
79 int client_obd_disconnect(struct lustre_handle *conn);
80 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
81 int client_sanobd_setup(struct obd_device *obddev, obd_count len, void *buf);
82 int client_obd_cleanup(struct obd_device * obddev);
83 struct client_obd *client_conn2cli(struct lustre_handle *conn);
84 struct obd_device *client_tgtuuid2obd(struct obd_uuid *tgtuuid);
85
86 /* statfs_pack.c */
87 int obd_self_statfs(struct obd_device *dev, struct statfs *sfs);
88
89 /* l_lock.c */
90 struct lustre_lock {
91         int l_depth;
92         struct task_struct *l_owner;
93         struct semaphore l_sem;
94         spinlock_t l_spin;
95 };
96
97 void l_lock_init(struct lustre_lock *);
98 void l_lock(struct lustre_lock *);
99 void l_unlock(struct lustre_lock *);
100 int l_has_lock(struct lustre_lock *);
101
102 #define CB_PHASE_START   12
103 #define CB_PHASE_FINISH  13
104
105 /* This list head doesn't need to be locked, because it's only manipulated by
106  * one thread at a time. */
107 struct obd_brw_set {
108         struct list_head brw_desc_head; /* list of ptlrpc_bulk_desc */
109         wait_queue_head_t brw_waitq;
110         atomic_t brw_refcount;
111         atomic_t brw_desc_count;
112         int brw_flags;
113
114         int (*brw_callback)(struct obd_brw_set *, int phase);
115 };
116
117 /* simple.c */
118 struct obd_run_ctxt;
119 struct obd_ucred;
120 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
121                struct obd_ucred *cred);
122 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
123               struct obd_ucred *cred);
124 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode);
125 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode);
126 int lustre_fread(struct file *file, char *str, int len, loff_t *off);
127 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off);
128 int lustre_fsync(struct file *file);
129
130 #ifdef __KERNEL__
131
132 static inline void l_dput(struct dentry *de)
133 {
134         if (!de || IS_ERR(de))
135                 return;
136         shrink_dcache_parent(de);
137         LASSERT(atomic_read(&de->d_count) > 0);
138         dput(de);
139 }
140
141 static inline void ll_sleep(int t)
142 {
143         set_current_state(TASK_INTERRUPTIBLE);
144         schedule_timeout(t * HZ);
145         set_current_state(TASK_RUNNING);
146 }
147 #endif
148
149 /* FIXME: This needs to validate pointers and cookies */
150 static inline void *lustre_handle2object(struct lustre_handle *handle)
151 {
152         if (handle)
153                 return (void *)(unsigned long)(handle->addr);
154         return NULL;
155 }
156
157 static inline void ldlm_object2handle(void *object, struct lustre_handle *handle)
158 {
159         handle->addr = (__u64)(unsigned long)object;
160 }
161
162 #include <linux/portals_lib.h>
163
164 /*
165  *   OBD IOCTLS
166  */
167 #define OBD_IOCTL_VERSION 0x00010002
168
169 struct obd_ioctl_data {
170         uint32_t ioc_len;
171         uint32_t ioc_version;
172
173         uint64_t ioc_addr;
174         uint64_t ioc_cookie;
175         uint32_t ioc_conn1;
176         uint32_t ioc_conn2;
177
178         struct obdo ioc_obdo1;
179         struct obdo ioc_obdo2;
180
181         obd_size         ioc_count;
182         obd_off          ioc_offset;
183         uint32_t         ioc_dev;
184         uint32_t         ioc_command;
185
186         uint64_t ioc_nid;
187         uint32_t ioc_nal;
188
189         /* buffers the kernel will treat as user pointers */
190         uint32_t ioc_plen1;
191         char    *ioc_pbuf1;
192         uint32_t ioc_plen2;
193         char    *ioc_pbuf2;
194
195         /* two inline buffers */
196         uint32_t ioc_inllen1;
197         char    *ioc_inlbuf1;
198         uint32_t ioc_inllen2;
199         char    *ioc_inlbuf2;
200         uint32_t ioc_inllen3;
201         char    *ioc_inlbuf3;
202
203         char    ioc_bulk[0];
204 };
205
206 struct obd_ioctl_hdr {
207         uint32_t ioc_len;
208         uint32_t ioc_version;
209 };
210
211 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
212 {
213         int len = size_round(sizeof(struct obd_ioctl_data));
214         len += size_round(data->ioc_inllen1);
215         len += size_round(data->ioc_inllen2);
216         len += size_round(data->ioc_inllen3);
217         return len;
218 }
219
220
221 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
222 {
223         if (data->ioc_len > (1<<30)) {
224                 printk("OBD ioctl: ioc_len larger than 1<<30\n");
225                 return 1;
226         }
227         if (data->ioc_inllen1 > (1<<30)) {
228                 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
229                 return 1;
230         }
231         if (data->ioc_inllen2 > (1<<30)) {
232                 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
233                 return 1;
234         }
235
236         if (data->ioc_inllen3 > (1<<30)) {
237                 printk("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
238                 return 1;
239         }
240         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
241                 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
242                 return 1;
243         }
244         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
245                 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
246                 return 1;
247         }
248         if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
249                 printk("OBD ioctl: inlbuf3 pointer but 0 length\n");
250                 return 1;
251         }
252         if (data->ioc_pbuf1 && !data->ioc_plen1) {
253                 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
254                 return 1;
255         }
256         if (data->ioc_pbuf2 && !data->ioc_plen2) {
257                 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
258                 return 1;
259         }
260         /*
261         if (data->ioc_inllen1 && !data->ioc_inlbuf1) {
262                 printk("OBD ioctl: inllen1 set but NULL pointer\n");
263                 return 1;
264         }
265         if (data->ioc_inllen2 && !data->ioc_inlbuf2) {
266                 printk("OBD ioctl: inllen2 set but NULL pointer\n");
267                 return 1;
268         }
269         if (data->ioc_inllen3 && !data->ioc_inlbuf3) {
270                 printk("OBD ioctl: inllen3 set but NULL pointer\n");
271                 return 1;
272         }
273         */
274         if (data->ioc_plen1 && !data->ioc_pbuf1) {
275                 printk("OBD ioctl: plen1 set but NULL pointer\n");
276                 return 1;
277         }
278         if (data->ioc_plen2 && !data->ioc_pbuf2) {
279                 printk("OBD ioctl: plen2 set but NULL pointer\n");
280                 return 1;
281         }
282         if (obd_ioctl_packlen(data) != data->ioc_len) {
283                 printk("OBD ioctl: packlen exceeds ioc_len (%d != %d)\n",
284                        obd_ioctl_packlen(data), data->ioc_len);
285                 return 1;
286         }
287 #if 0
288         if (data->ioc_inllen1 &&
289             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
290                 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
291                 return 1;
292         }
293         if (data->ioc_inllen2 &&
294             data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') {
295                 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
296                 return 1;
297         }
298         if (data->ioc_inllen3 &&
299             data->ioc_bulk[size_round(data->ioc_inllen1) + size_round(data->ioc_inllen2)
300                            + data->ioc_inllen3 - 1] != '\0') {
301                 printk("OBD ioctl: inlbuf3 not 0 terminated\n");
302                 return 1;
303         }
304 #endif
305         return 0;
306 }
307
308 #ifndef __KERNEL__
309 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
310                                  int max)
311 {
312         char *ptr;
313         struct obd_ioctl_data *overlay;
314         data->ioc_len = obd_ioctl_packlen(data);
315         data->ioc_version = OBD_IOCTL_VERSION;
316
317         if (*pbuf && data->ioc_len > max)
318                 return 1;
319         if (*pbuf == NULL) {
320                 *pbuf = malloc(data->ioc_len);
321         }
322         if (!*pbuf)
323                 return 1;
324         overlay = (struct obd_ioctl_data *)*pbuf;
325         memcpy(*pbuf, data, sizeof(*data));
326
327         ptr = overlay->ioc_bulk;
328         if (data->ioc_inlbuf1)
329                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
330         if (data->ioc_inlbuf2)
331                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
332         if (data->ioc_inlbuf3)
333                 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
334         if (obd_ioctl_is_invalid(overlay))
335                 return 1;
336
337         return 0;
338 }
339
340 static inline int obd_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf,
341                                    int max)
342 {
343         char *ptr;
344         struct obd_ioctl_data *overlay;
345
346         if (!pbuf)
347                 return 1;
348         overlay = (struct obd_ioctl_data *)pbuf;
349
350         /* Preserve the caller's buffer pointers */
351         overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
352         overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
353         overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
354
355         memcpy(data, pbuf, sizeof(*data));
356
357         ptr = overlay->ioc_bulk;
358         if (data->ioc_inlbuf1)
359                 LOGU(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
360         if (data->ioc_inlbuf2)
361                 LOGU(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
362         if (data->ioc_inlbuf3)
363                 LOGU(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
364
365         return 0;
366 }
367 #endif
368
369 #include <linux/obd_support.h>
370
371 /* buffer MUST be at least the size of obd_ioctl_hdr */
372 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
373 {
374         struct obd_ioctl_hdr hdr;
375         struct obd_ioctl_data *data;
376         int err;
377         ENTRY;
378
379         err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
380         if ( err ) {
381                 EXIT;
382                 return err;
383         }
384
385         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
386                 printk("OBD: version mismatch kernel vs application\n");
387                 return -EINVAL;
388         }
389
390         if (hdr.ioc_len > 8192) {
391                 printk("OBD: user buffer exceeds 8192 max buffer\n");
392                 return -EINVAL;
393         }
394
395         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
396                 printk("OBD: user buffer too small for ioctl\n");
397                 return -EINVAL;
398         }
399
400         OBD_ALLOC(*buf, hdr.ioc_len);
401         if (!*buf) {
402                 CERROR("Cannot allocate control buffer of len %d\n",
403                        hdr.ioc_len);
404                 RETURN(-EINVAL);
405         }
406         *len = hdr.ioc_len;
407         data = (struct obd_ioctl_data *)*buf;
408
409         err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
410         if ( err ) {
411                 EXIT;
412                 return err;
413         }
414
415         if (obd_ioctl_is_invalid(data)) {
416                 printk("OBD: ioctl not correctly formatted\n");
417                 return -EINVAL;
418         }
419
420         if (data->ioc_inllen1) {
421                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
422         }
423
424         if (data->ioc_inllen2) {
425                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
426                         size_round(data->ioc_inllen1);
427         }
428
429         if (data->ioc_inllen3) {
430                 data->ioc_inlbuf3 = &data->ioc_bulk[0] +
431                         size_round(data->ioc_inllen1) +
432                         size_round(data->ioc_inllen2);
433         }
434
435         EXIT;
436         return 0;
437 }
438
439 #define OBD_IOC_CREATE                 _IOR ('f', 101, long)
440 #define OBD_IOC_SETUP                  _IOW ('f', 102, long)
441 #define OBD_IOC_CLEANUP                _IO  ('f', 103      )
442 #define OBD_IOC_DESTROY                _IOW ('f', 104, long)
443 #define OBD_IOC_PREALLOCATE            _IOWR('f', 105, long)
444
445 #define OBD_IOC_SETATTR                _IOW ('f', 107, long)
446 #define OBD_IOC_GETATTR                _IOR ('f', 108, long)
447 #define OBD_IOC_READ                   _IOWR('f', 109, long)
448 #define OBD_IOC_WRITE                  _IOWR('f', 110, long)
449 #define OBD_IOC_CONNECT                _IOR ('f', 111, long)
450 #define OBD_IOC_DISCONNECT             _IOW ('f', 112, long)
451 #define OBD_IOC_STATFS                 _IOWR('f', 113, long)
452 #define OBD_IOC_SYNC                   _IOR ('f', 114, long)
453 #define OBD_IOC_READ2                  _IOWR('f', 115, long)
454 #define OBD_IOC_FORMAT                 _IOWR('f', 116, long)
455 #define OBD_IOC_PARTITION              _IOWR('f', 117, long)
456 #define OBD_IOC_ATTACH                 _IOWR('f', 118, long)
457 #define OBD_IOC_DETACH                 _IOWR('f', 119, long)
458 #define OBD_IOC_COPY                   _IOWR('f', 120, long)
459 #define OBD_IOC_MIGR                   _IOWR('f', 121, long)
460 #define OBD_IOC_PUNCH                  _IOWR('f', 122, long)
461 #define OBD_IOC_DEVICE                 _IOWR('f', 123, long)
462 #define OBD_IOC_MODULE_DEBUG           _IOWR('f', 124, long)
463 #define OBD_IOC_BRW_READ               _IOWR('f', 125, long)
464 #define OBD_IOC_BRW_WRITE              _IOWR('f', 126, long)
465 #define OBD_IOC_NAME2DEV               _IOWR('f', 127, long)
466 #define OBD_IOC_NEWDEV                 _IOWR('f', 128, long)
467 #define OBD_IOC_LIST                   _IOWR('f', 129, long)
468 #define OBD_IOC_UUID2DEV               _IOWR('f', 130, long)
469
470 #define OBD_IOC_RECOVD_NEWCONN         _IOWR('f', 131, long)
471 #define OBD_IOC_LOV_SET_CONFIG         _IOWR('f', 132, long)
472 #define OBD_IOC_LOV_GET_CONFIG         _IOWR('f', 133, long)
473 #define OBD_IOC_LOV_CONFIG             OBD_IOC_LOV_SET_CONFIG
474
475 #define OBD_IOC_OPEN                   _IOWR('f', 134, long)
476 #define OBD_IOC_CLOSE                  _IOWR('f', 135, long)
477
478 #define OBD_IOC_RECOVD_FAILCONN        _IOWR('f', 136, long)
479
480 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 139      )
481 #define OBD_IOC_NO_TRANSNO             _IOW ('f', 140, long)
482 #define OBD_IOC_SET_READONLY           _IOW ('f', 141, long)
483
484 #define OBD_GET_VERSION                _IOWR ('f', 144, long)
485
486 #define OBD_IOC_ADD_UUID               _IOWR ('f', 145, long)
487 #define OBD_IOC_DEL_UUID               _IOWR ('f', 146, long)
488 #define OBD_IOC_CLOSE_UUID             _IOWR ('f', 147, long)
489
490 #define ECHO_IOC_GET_STRIPE            _IOWR('f', 200, long)
491 #define ECHO_IOC_SET_STRIPE            _IOWR('f', 201, long)
492 #define ECHO_IOC_ENQUEUE               _IOWR('f', 202, long)
493 #define ECHO_IOC_CANCEL                _IOWR('f', 203, long)
494
495
496 #define CHECKSUM_BULK 0
497
498 #if CHECKSUM_BULK
499 static inline void ost_checksum(__u64 *cksum, void *addr, int len)
500 {
501         unsigned char *ptr = (unsigned char *)addr;
502         __u64          sum = 0;
503
504         /* very stupid, but means I don't have to think about byte order */
505         while (len-- > 0)
506                 sum += *ptr++;
507
508         *cksum = (*cksum << 2) + sum;
509 }
510 #else
511 #define ost_checksum(cksum, addr, len) do {} while (0)
512 #endif
513
514 /*
515  * l_wait_event is a flexible sleeping function, permitting simple caller
516  * configuration of interrupt and timeout sensitivity along with actions to
517  * be performed in the event of either exception.
518  *
519  * Common usage looks like this:
520  *
521  * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
522  *                                           intr_handler, callback_data);
523  * rc = l_wait_event(waitq, condition, &lwi);
524  *
525  * (LWI_TIMEOUT and LWI_INTR macros are available for timeout- and
526  * interrupt-only variants, respectively.)
527  *
528  * If a timeout is specified, the timeout_handler will be invoked in the event
529  * that the timeout expires before the process is awakened.  (Note that any
530  * waking of the process will restart the timeout, even if the condition is
531  * not satisfied and the process immediately returns to sleep.  This might be
532  * considered a bug.)  If the timeout_handler returns non-zero, l_wait_event
533  * will return -ETIMEDOUT and the caller will continue.  If the handler returns
534  * zero instead, the process will go back to sleep until it is awakened by the
535  * waitq or some similar mechanism, or an interrupt occurs (if the caller has
536  * asked for interrupts to be detected).  The timeout will only fire once, so
537  * callers should take care that a timeout_handler which returns zero will take
538  * future steps to awaken the process.  N.B. that these steps must include
539  * making the provided condition become true.
540  *
541  * If the interrupt flag (lwi_signals) is non-zero, then the process will be
542  * interruptible, and will be awakened by any "killable" signal (SIGTERM,
543  * SIGKILL or SIGINT).  If a timeout is also specified, then the process will
544  * only become interruptible _after_ the timeout has expired, though it can be
545  * awakened by a signal that was delivered before the timeout and is still
546  * pending when the timeout expires.  If a timeout is not specified, the process
547  * will be interruptible at all times during l_wait_event.
548  */
549
550 struct l_wait_info {
551         long   lwi_timeout;
552         int  (*lwi_on_timeout)(void *);
553         long   lwi_signals;
554         int  (*lwi_on_signal)(void *); /* XXX return is ignored for now */
555         void  *lwi_cb_data;
556 };
557
558 #define LWI_TIMEOUT(time, cb, data)                                            \
559 ((struct l_wait_info) {                                                        \
560         lwi_timeout:    time,                                                  \
561         lwi_on_timeout: cb,                                                    \
562         lwi_cb_data:    data                                                   \
563 })
564
565 #define LWI_INTR(cb, data)                                                     \
566 ((struct l_wait_info) {                                                        \
567         lwi_signals:   1,                                                      \
568         lwi_on_signal: cb,                                                     \
569         lwi_cb_data:   data                                                    \
570 })
571
572 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)                          \
573 ((struct l_wait_info) {                                                        \
574         lwi_timeout:    time,                                                  \
575         lwi_on_timeout: time_cb,                                               \
576         lwi_signals:    1,                                                     \
577         lwi_on_signal:  sig_cb,                                                \
578         lwi_cb_data:    data                                                   \
579 })
580
581 #define LUSTRE_FATAL_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) |                \
582                            sigmask(SIGTERM) | sigmask(SIGQUIT))
583
584 #ifdef __KERNEL__
585 static inline sigset_t l_w_e_set_sigs(int sigs)
586 {
587         sigset_t old;
588         unsigned long irqflags;
589
590         spin_lock_irqsave(&current->sigmask_lock, irqflags);
591         old = current->blocked;
592         siginitsetinv(&current->blocked, sigs);
593         recalc_sigpending(current);
594         spin_unlock_irqrestore(&current->sigmask_lock, irqflags);
595
596         return old;
597 }
598
599 #define __l_wait_event(wq, condition, info, ret)                               \
600 do {                                                                           \
601         wait_queue_t __wait;                                                   \
602         int __timed_out = 0;                                                   \
603         unsigned long irqflags;                                                \
604         sigset_t blocked;                                                      \
605                                                                                \
606         init_waitqueue_entry(&__wait, current);                                \
607         add_wait_queue(&wq, &__wait);                                          \
608                                                                                \
609         /* Block all signals (just the non-fatal ones if no timeout). */       \
610         if (info->lwi_signals && !info->lwi_timeout)                           \
611             blocked = l_w_e_set_sigs(LUSTRE_FATAL_SIGS);                       \
612         else                                                                   \
613             blocked = l_w_e_set_sigs(0);                                       \
614                                                                                \
615         for (;;) {                                                             \
616             set_current_state(TASK_INTERRUPTIBLE);                             \
617             if (condition)                                                     \
618                     break;                                                     \
619             if (signal_pending(current)) {                                     \
620                 if (info->lwi_on_signal)                                       \
621                         info->lwi_on_signal(info->lwi_cb_data);                \
622                 ret = -EINTR;                                                  \
623                 break;                                                         \
624             }                                                                  \
625             if (info->lwi_timeout && !__timed_out) {                           \
626                 if (schedule_timeout(info->lwi_timeout) == 0) {                \
627                     __timed_out = 1;                                           \
628                     if (!info->lwi_on_timeout ||                               \
629                         info->lwi_on_timeout(info->lwi_cb_data)) {             \
630                         ret = -ETIMEDOUT;                                      \
631                         break;                                                 \
632                     }                                                          \
633                     /* We'll take signals after a timeout. */                  \
634                     if (info->lwi_signals)                                     \
635                         (void)l_w_e_set_sigs(LUSTRE_FATAL_SIGS);               \
636                 }                                                              \
637             } else {                                                           \
638                 schedule();                                                    \
639             }                                                                  \
640         }                                                                      \
641                                                                                \
642         spin_lock_irqsave(&current->sigmask_lock, irqflags);                   \
643         current->blocked = blocked;                                            \
644         recalc_sigpending(current);                                            \
645         spin_unlock_irqrestore(&current->sigmask_lock, irqflags);              \
646                                                                                \
647         current->state = TASK_RUNNING;                                         \
648         remove_wait_queue(&wq, &__wait);                                       \
649 } while(0)
650
651 #define l_wait_event(wq, condition, info)                                      \
652 ({                                                                             \
653         int __ret = 0;                                                         \
654         struct l_wait_info *__info = (info);                                   \
655         if (!(condition))                                                      \
656                 __l_wait_event(wq, condition, __info, __ret);                  \
657         __ret;                                                                 \
658 })
659 #endif /* __KERNEL__ */
660
661 #endif /* _LUSTRE_LIB_H */