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