Whamcloud - gitweb
Don't crash in expired_lock_main() with racing client eviction/lock completion.
[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/rwsem.h>
36 # include <linux/sched.h>
37 # include <linux/signal.h>
38 # include <linux/types.h>
39 #endif
40 #include <linux/portals_lib.h>
41 #include <linux/kp30.h> /* XXX just for LASSERT! */
42 #include <linux/lustre_idl.h>
43 #include <linux/lustre_cfg.h>
44
45 #ifndef LPU64
46 /* x86_64 has 64bit longs and defines u64 as long long */
47 #if BITS_PER_LONG > 32 && !defined(__x86_64__)
48 #define LPU64 "%lu"
49 #define LPD64 "%ld"
50 #define LPX64 "%#lx"
51 #define LP_POISON ((void *)0x5a5a5a5a5a5a5a5a)
52 #else
53 #define LPU64 "%Lu"
54 #define LPD64 "%Ld"
55 #define LPX64 "%#Lx"
56 #define LP_POISON ((void *)0x5a5a5a5a)
57 #endif
58 #endif
59
60 /* target.c */
61 struct ptlrpc_request;
62 struct recovd_data;
63 struct recovd_obd;
64 struct obd_export;
65 #include <linux/lustre_ha.h>
66 #include <linux/lustre_net.h>
67 #include <linux/lustre_compat25.h>
68 #include <linux/lvfs.h>
69
70 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler);
71 int target_handle_disconnect(struct ptlrpc_request *req);
72 void target_destroy_export(struct obd_export *exp);
73 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
74                             struct obd_uuid *cluuid);
75 int target_handle_ping(struct ptlrpc_request *req);
76 void target_cancel_recovery_timer(struct obd_device *obd);
77
78 #define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 * HZ / 2) /* *waves hands* */
79 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler);
80 void target_abort_recovery(void *data);
81 int target_queue_recovery_request(struct ptlrpc_request *req,
82                                   struct obd_device *obd);
83 int target_queue_final_reply(struct ptlrpc_request *req, int rc);
84 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
85
86 /* client.c */
87
88 int client_sanobd_setup(struct obd_device *obddev, obd_count len, void *buf);
89 struct client_obd *client_conn2cli(struct lustre_handle *conn);
90
91 struct mdc_open_data;
92 struct obd_client_handle {
93         struct lustre_handle och_fh;
94         struct llog_cookie och_cookie;
95         struct mdc_open_data *och_mod;
96         __u32 och_magic;
97 };
98 #define OBD_CLIENT_HANDLE_MAGIC 0xd15ea5ed
99
100 /* statfs_pack.c */
101 void statfs_pack(struct obd_statfs *osfs, struct kstatfs *sfs);
102 void statfs_unpack(struct kstatfs *sfs, struct obd_statfs *osfs);
103
104 /* l_lock.c */
105 struct lustre_lock {
106         int l_depth;
107         struct task_struct *l_owner;
108         struct semaphore l_sem;
109         spinlock_t l_spin;
110 };
111
112 void l_lock_init(struct lustre_lock *);
113 void l_lock(struct lustre_lock *);
114 void l_unlock(struct lustre_lock *);
115 int l_has_lock(struct lustre_lock *);
116
117
118 #include <linux/portals_lib.h>
119
120 /*
121  *   OBD IOCTLS
122  */
123 #define OBD_IOCTL_VERSION 0x00010004
124
125 struct obd_ioctl_data {
126         uint32_t ioc_len;
127         uint32_t ioc_version;
128
129         uint64_t ioc_cookie;
130         uint32_t ioc_conn1;
131         uint32_t ioc_conn2;
132
133         struct obdo ioc_obdo1;
134         struct obdo ioc_obdo2;
135
136         obd_size         ioc_count;
137         obd_off          ioc_offset;
138         uint32_t         ioc_dev;
139         uint32_t         ioc_command;
140
141         uint64_t ioc_nid;
142         uint32_t ioc_nal;
143         uint32_t ioc_type;
144
145         /* buffers the kernel will treat as user pointers */
146         uint32_t ioc_plen1;
147         char    *ioc_pbuf1;
148         uint32_t ioc_plen2;
149         char    *ioc_pbuf2;
150
151         /* inline buffers for various arguments */
152         uint32_t ioc_inllen1;
153         char    *ioc_inlbuf1;
154         uint32_t ioc_inllen2;
155         char    *ioc_inlbuf2;
156         uint32_t ioc_inllen3;
157         char    *ioc_inlbuf3;
158         uint32_t ioc_inllen4;
159         char    *ioc_inlbuf4;
160
161         char    ioc_bulk[0];
162 };
163
164 struct obd_ioctl_hdr {
165         uint32_t ioc_len;
166         uint32_t ioc_version;
167 };
168
169 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
170 {
171         int len = size_round(sizeof(struct obd_ioctl_data));
172         len += size_round(data->ioc_inllen1);
173         len += size_round(data->ioc_inllen2);
174         len += size_round(data->ioc_inllen3);
175         len += size_round(data->ioc_inllen4);
176         return len;
177 }
178
179
180 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
181 {
182         if (data->ioc_len > (1<<30)) {
183                 printk("LustreError: OBD ioctl: ioc_len larger than 1<<30\n");
184                 return 1;
185         }
186         if (data->ioc_inllen1 > (1<<30)) {
187                 printk("LustreError: OBD ioctl: ioc_inllen1 larger than 1<<30\n");
188                 return 1;
189         }
190         if (data->ioc_inllen2 > (1<<30)) {
191                 printk("LustreError: OBD ioctl: ioc_inllen2 larger than 1<<30\n");
192                 return 1;
193         }
194         if (data->ioc_inllen3 > (1<<30)) {
195                 printk("LustreError: OBD ioctl: ioc_inllen3 larger than 1<<30\n");
196                 return 1;
197         }
198         if (data->ioc_inllen4 > (1<<30)) {
199                 printk("LustreError: OBD ioctl: ioc_inllen4 larger than 1<<30\n");
200                 return 1;
201         }
202         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
203                 printk("LustreError: OBD ioctl: inlbuf1 pointer but 0 length\n");
204                 return 1;
205         }
206         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
207                 printk("LustreError: OBD ioctl: inlbuf2 pointer but 0 length\n");
208                 return 1;
209         }
210         if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
211                 printk("LustreError: OBD ioctl: inlbuf3 pointer but 0 length\n");
212                 return 1;
213         }
214         if (data->ioc_inlbuf4 && !data->ioc_inllen4) {
215                 printk("LustreError: OBD ioctl: inlbuf4 pointer but 0 length\n");
216                 return 1;
217         }
218         if (data->ioc_pbuf1 && !data->ioc_plen1) {
219                 printk("LustreError: OBD ioctl: pbuf1 pointer but 0 length\n");
220                 return 1;
221         }
222         if (data->ioc_pbuf2 && !data->ioc_plen2) {
223                 printk("LustreError: OBD ioctl: pbuf2 pointer but 0 length\n");
224                 return 1;
225         }
226         if (data->ioc_plen1 && !data->ioc_pbuf1) {
227                 printk("LustreError: OBD ioctl: plen1 set but NULL pointer\n");
228                 return 1;
229         }
230         if (data->ioc_plen2 && !data->ioc_pbuf2) {
231                 printk("LustreError: OBD ioctl: plen2 set but NULL pointer\n");
232                 return 1;
233         }
234         if (obd_ioctl_packlen(data) != data->ioc_len) {
235                 printk("LustreError: OBD ioctl: packlen exceeds ioc_len (%d != %d)\n",
236                        obd_ioctl_packlen(data), data->ioc_len);
237                 return 1;
238         }
239         return 0;
240 }
241
242 #ifndef __KERNEL__
243 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
244                                  int max)
245 {
246         char *ptr;
247         struct obd_ioctl_data *overlay;
248         data->ioc_len = obd_ioctl_packlen(data);
249         data->ioc_version = OBD_IOCTL_VERSION;
250
251         if (*pbuf && data->ioc_len > max)
252                 return 1;
253         if (*pbuf == NULL) {
254                 *pbuf = malloc(data->ioc_len);
255         }
256         if (!*pbuf)
257                 return 1;
258         overlay = (struct obd_ioctl_data *)*pbuf;
259         memcpy(*pbuf, data, sizeof(*data));
260
261         ptr = overlay->ioc_bulk;
262         if (data->ioc_inlbuf1)
263                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
264         if (data->ioc_inlbuf2)
265                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
266         if (data->ioc_inlbuf3)
267                 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
268         if (data->ioc_inlbuf4)
269                 LOGL(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
270         if (obd_ioctl_is_invalid(overlay))
271                 return 1;
272
273         return 0;
274 }
275
276 static inline int obd_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf,
277                                    int max)
278 {
279         char *ptr;
280         struct obd_ioctl_data *overlay;
281
282         if (!pbuf)
283                 return 1;
284         overlay = (struct obd_ioctl_data *)pbuf;
285
286         /* Preserve the caller's buffer pointers */
287         overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
288         overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
289         overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
290         overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
291
292         memcpy(data, pbuf, sizeof(*data));
293
294         ptr = overlay->ioc_bulk;
295         if (data->ioc_inlbuf1)
296                 LOGU(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
297         if (data->ioc_inlbuf2)
298                 LOGU(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
299         if (data->ioc_inlbuf3)
300                 LOGU(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
301         if (data->ioc_inlbuf4)
302                 LOGU(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
303
304         return 0;
305 }
306 #endif
307
308 #include <linux/obd_support.h>
309
310 /* buffer MUST be at least the size of obd_ioctl_hdr */
311 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
312 {
313         struct obd_ioctl_hdr hdr;
314         struct obd_ioctl_data *data;
315         int err;
316         int offset = 0;
317         ENTRY;
318
319         err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
320         if ( err ) {
321                 EXIT;
322                 return err;
323         }
324
325         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
326                 CERROR("Version mismatch kernel vs application\n");
327                 return -EINVAL;
328         }
329
330         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
331                 CERROR("User buffer len %d exceeds %d max buffer\n",
332                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
333                 return -EINVAL;
334         }
335
336         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
337                 printk("LustreError: OBD: user buffer too small for ioctl\n");
338                 return -EINVAL;
339         }
340
341         /* XXX allocate this more intelligently, using kmalloc when
342          * appropriate */
343         OBD_VMALLOC(*buf, hdr.ioc_len);
344         if (*buf == NULL) {
345                 CERROR("Cannot allocate control buffer of len %d\n",
346                        hdr.ioc_len);
347                 RETURN(-EINVAL);
348         }
349         *len = hdr.ioc_len;
350         data = (struct obd_ioctl_data *)*buf;
351
352         err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
353         if ( err ) {
354                 EXIT;
355                 return err;
356         }
357
358         if (obd_ioctl_is_invalid(data)) {
359                 CERROR("ioctl not correctly formatted\n");
360                 return -EINVAL;
361         }
362
363         if (data->ioc_inllen1) {
364                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
365                 offset += size_round(data->ioc_inllen1);
366         }
367
368         if (data->ioc_inllen2) {
369                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
370                 offset += size_round(data->ioc_inllen2);
371         }
372
373         if (data->ioc_inllen3) {
374                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
375                 offset += size_round(data->ioc_inllen3);
376         }
377
378         if (data->ioc_inllen4) {
379                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
380         }
381
382         EXIT;
383         return 0;
384 }
385
386 static inline void obd_ioctl_freedata(char *buf, int len)
387 {
388         ENTRY;
389
390         OBD_VFREE(buf, len);
391         EXIT;
392         return;
393 }
394
395 #define OBD_IOC_CREATE                 _IOR ('f', 101, long)
396 #define OBD_IOC_DESTROY                _IOW ('f', 104, long)
397 #define OBD_IOC_PREALLOCATE            _IOWR('f', 105, long)
398
399 #define OBD_IOC_SETATTR                _IOW ('f', 107, long)
400 #define OBD_IOC_GETATTR                _IOR ('f', 108, long)
401 #define OBD_IOC_READ                   _IOWR('f', 109, long)
402 #define OBD_IOC_WRITE                  _IOWR('f', 110, long)
403
404
405 #define OBD_IOC_STATFS                 _IOWR('f', 113, long)
406 #define OBD_IOC_SYNC                   _IOR ('f', 114, long)
407 #define OBD_IOC_READ2                  _IOWR('f', 115, long)
408 #define OBD_IOC_FORMAT                 _IOWR('f', 116, long)
409 #define OBD_IOC_PARTITION              _IOWR('f', 117, long)
410 #define OBD_IOC_COPY                   _IOWR('f', 120, long)
411 #define OBD_IOC_MIGR                   _IOWR('f', 121, long)
412 #define OBD_IOC_PUNCH                  _IOWR('f', 122, long)
413
414 #define OBD_IOC_MODULE_DEBUG           _IOWR('f', 124, long)
415 #define OBD_IOC_BRW_READ               _IOWR('f', 125, long)
416 #define OBD_IOC_BRW_WRITE              _IOWR('f', 126, long)
417 #define OBD_IOC_NAME2DEV               _IOWR('f', 127, long)
418 #define OBD_IOC_UUID2DEV               _IOWR('f', 130, long)
419
420 #define OBD_IOC_LOV_GET_CONFIG         _IOWR('f', 132, long)
421 #define OBD_IOC_CLIENT_RECOVER         _IOW ('f', 133, long)
422
423 #define OBD_IOC_PING                   _IOWR('f', 135, long)
424
425 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 139      )
426 #define OBD_IOC_NO_TRANSNO             _IOW ('f', 140, long)
427 #define OBD_IOC_SET_READONLY           _IOW ('f', 141, long)
428 #define OBD_IOC_ABORT_RECOVERY         _IOR ('f', 142, long)
429
430 #define OBD_GET_VERSION                _IOWR ('f', 144, long)
431
432 #define OBD_IOC_CLOSE_UUID             _IOWR ('f', 147, long)
433
434 #define OBD_IOC_LOV_SETSTRIPE            _IOW ('f', 154, long)
435 #define OBD_IOC_LOV_GETSTRIPE            _IOW ('f', 155, long)
436 #define OBD_IOC_LOV_SETEA                _IOW ('f', 156, long)
437
438 #define OBD_IOC_MOUNTOPT               _IOWR('f', 170, long)
439
440 #define OBD_IOC_RECORD                 _IOWR('f', 180, long)
441 #define OBD_IOC_ENDRECORD              _IOWR('f', 181, long)
442 #define OBD_IOC_PARSE                  _IOWR('f', 182, long)
443 #define OBD_IOC_DORECORD               _IOWR('f', 183, long)
444 #define OBD_IOC_PROCESS_CFG            _IOWR('f', 184, long)
445 #define OBD_IOC_DUMP_LOG               _IOWR('f', 185, long)
446 #define OBD_IOC_CLEAR_LOG              _IOWR('f', 186, long)
447
448 #define OBD_IOC_CATLOGLIST             _IOWR('f', 190, long)
449 #define OBD_IOC_LLOG_INFO              _IOWR('f', 191, long)
450 #define OBD_IOC_LLOG_PRINT             _IOWR('f', 192, long)
451 #define OBD_IOC_LLOG_CANCEL            _IOWR('f', 193, long)
452 #define OBD_IOC_LLOG_REMOVE            _IOWR('f', 194, long)
453 #define OBD_IOC_LLOG_CHECK             _IOWR('f', 195, long)
454 #define OBD_IOC_LLOG_CATINFO           _IOWR('f', 196, long)
455
456 #define ECHO_IOC_GET_STRIPE            _IOWR('f', 200, long)
457 #define ECHO_IOC_SET_STRIPE            _IOWR('f', 201, long)
458 #define ECHO_IOC_ENQUEUE               _IOWR('f', 202, long)
459 #define ECHO_IOC_CANCEL                _IOWR('f', 203, long)
460
461 /* XXX _IOWR('f', 250, long) has been defined in
462  * portals/include/linux/kp30.h for debug, don't use it
463  */
464
465 /* Until such time as we get_info the per-stripe maximum from the OST,
466  * we define this to be 2T - 4k, which is the ext3 maxbytes. */
467 #define LUSTRE_STRIPE_MAXBYTES 0x1fffffff000ULL
468
469 #define CHECKSUM_BULK 0
470 #define POISON_BULK 0
471
472 #if CHECKSUM_BULK
473 static inline void ost_checksum(obd_count *cksum, void *addr, int len)
474 {
475         unsigned char *ptr = (unsigned char *)addr;
476         obd_count          sum = 0;
477
478         /* very stupid, but means I don't have to think about byte order */
479         while (len-- > 0)
480                 sum += *ptr++;
481
482         *cksum = (*cksum << 2) + sum;
483 }
484 #endif
485
486 static inline int ll_insecure_random_int(void)
487 {
488 #ifdef __arch_um__
489         struct timeval t;
490         do_gettimeofday(&t);
491         return (int)(t.tv_usec);
492 #else
493         return (int)(get_cycles() >> 2);
494 #endif
495 }
496
497 /*
498  * l_wait_event is a flexible sleeping function, permitting simple caller
499  * configuration of interrupt and timeout sensitivity along with actions to
500  * be performed in the event of either exception.
501  *
502  * Common usage looks like this:
503  *
504  * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
505  *                                           intr_handler, callback_data);
506  * rc = l_wait_event(waitq, condition, &lwi);
507  *
508  * (LWI_TIMEOUT and LWI_INTR macros are available for timeout- and
509  * interrupt-only variants, respectively.)
510  *
511  * If a timeout is specified, the timeout_handler will be invoked in the event
512  * that the timeout expires before the process is awakened.  (Note that any
513  * waking of the process will restart the timeout, even if the condition is
514  * not satisfied and the process immediately returns to sleep.  This might be
515  * considered a bug.)  If the timeout_handler returns non-zero, l_wait_event
516  * will return -ETIMEDOUT and the caller will continue.  If the handler returns
517  * zero instead, the process will go back to sleep until it is awakened by the
518  * waitq or some similar mechanism, or an interrupt occurs (if the caller has
519  * asked for interrupts to be detected).  The timeout will only fire once, so
520  * callers should take care that a timeout_handler which returns zero will take
521  * future steps to awaken the process.  N.B. that these steps must include
522  * making the provided condition become true.
523  *
524  * If the interrupt flag (lwi_signals) is non-zero, then the process will be
525  * interruptible, and will be awakened by any "killable" signal (SIGTERM,
526  * SIGKILL or SIGINT).  If a timeout is also specified, then the process will
527  * only become interruptible _after_ the timeout has expired, though it can be
528  * awakened by a signal that was delivered before the timeout and is still
529  * pending when the timeout expires.  If a timeout is not specified, the process
530  * will be interruptible at all times during l_wait_event.
531  */
532
533 struct l_wait_info {
534         long   lwi_timeout;
535         int  (*lwi_on_timeout)(void *);
536         long   lwi_signals;
537         void (*lwi_on_signal)(void *);
538         void  *lwi_cb_data;
539 };
540
541 #define LWI_TIMEOUT(time, cb, data)                                            \
542 ((struct l_wait_info) {                                                        \
543         lwi_timeout:    time,                                                  \
544         lwi_on_timeout: cb,                                                    \
545         lwi_cb_data:    data                                                   \
546 })
547
548 #define LWI_INTR(cb, data)                                                     \
549 ((struct l_wait_info) {                                                        \
550         lwi_signals:   1,                                                      \
551         lwi_on_signal: cb,                                                     \
552         lwi_cb_data:   data                                                    \
553 })
554
555 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)                          \
556 ((struct l_wait_info) {                                                        \
557         lwi_timeout:    time,                                                  \
558         lwi_on_timeout: time_cb,                                               \
559         lwi_signals:    1,                                                     \
560         lwi_on_signal:  sig_cb,                                                \
561         lwi_cb_data:    data                                                   \
562 })
563
564 #define LUSTRE_FATAL_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) |                \
565                            sigmask(SIGTERM) | sigmask(SIGQUIT) |               \
566                            sigmask(SIGALRM))
567
568 #ifdef __KERNEL__
569 static inline sigset_t l_w_e_set_sigs(int sigs)
570 {
571         sigset_t old;
572         unsigned long irqflags;
573
574         SIGNAL_MASK_LOCK(current, irqflags);
575         old = current->blocked;
576         siginitsetinv(&current->blocked, sigs);
577         RECALC_SIGPENDING;
578         SIGNAL_MASK_UNLOCK(current, irqflags);
579
580         return old;
581 }
582
583 #define __l_wait_event(wq, condition, info, ret, excl)                         \
584 do {                                                                           \
585         wait_queue_t __wait;                                                   \
586         int __timed_out = 0;                                                   \
587         unsigned long irqflags;                                                \
588         sigset_t blocked;                                                      \
589                                                                                \
590         init_waitqueue_entry(&__wait, current);                                \
591         if (excl)                                                              \
592             add_wait_queue_exclusive(&wq, &__wait);                            \
593         else                                                                   \
594             add_wait_queue(&wq, &__wait);                                      \
595                                                                                \
596         /* Block all signals (just the non-fatal ones if no timeout). */       \
597         if (info->lwi_signals && !info->lwi_timeout)                           \
598             blocked = l_w_e_set_sigs(LUSTRE_FATAL_SIGS);                       \
599         else                                                                   \
600             blocked = l_w_e_set_sigs(0);                                       \
601                                                                                \
602         for (;;) {                                                             \
603             set_current_state(TASK_INTERRUPTIBLE);                             \
604             if (condition)                                                     \
605                     break;                                                     \
606             if (signal_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                         (void)l_w_e_set_sigs(LUSTRE_FATAL_SIGS);               \
623                 }                                                              \
624             } else {                                                           \
625                 schedule();                                                    \
626             }                                                                  \
627         }                                                                      \
628                                                                                \
629         SIGNAL_MASK_LOCK(current, irqflags);                                   \
630         current->blocked = blocked;                                            \
631         RECALC_SIGPENDING;                                                     \
632         SIGNAL_MASK_UNLOCK(current, irqflags);                                 \
633                                                                                \
634         current->state = TASK_RUNNING;                                         \
635         remove_wait_queue(&wq, &__wait);                                       \
636 } while(0)
637
638 #else /* !__KERNEL__ */
639 #define __l_wait_event(wq, condition, info, ret, excl)                         \
640 do {                                                                           \
641         int __timed_out = 0;                                                   \
642                                                                                \
643         for (;;) {                                                             \
644             if (condition)                                                     \
645                 break;                                                         \
646             if (liblustre_wait_event(info->lwi_timeout))                       \
647                 continue;                                                      \
648             if (info->lwi_timeout && !__timed_out) {                           \
649                 __timed_out = 1;                                               \
650                 if (info->lwi_on_timeout == NULL ||                            \
651                     info->lwi_on_timeout(info->lwi_cb_data)) {                 \
652                     ret = -ETIMEDOUT;                                          \
653                     break;                                                     \
654                 }                                                              \
655             }                                                                  \
656         }                                                                      \
657 } while (0)
658
659 #endif /* __KERNEL__ */
660
661 #define l_wait_event(wq, condition, info)                                      \
662 ({                                                                             \
663         int __ret = 0;                                                         \
664         struct l_wait_info *__info = (info);                                   \
665         if (!(condition))                                                      \
666                 __l_wait_event(wq, condition, __info, __ret, 0);               \
667         __ret;                                                                 \
668 })
669
670 #define l_wait_event_exclusive(wq, condition, info)                            \
671 ({                                                                             \
672         int __ret = 0;                                                         \
673         struct l_wait_info *__info = (info);                                   \
674         if (!(condition))                                                      \
675                 __l_wait_event(wq, condition, __info, __ret, 1);               \
676         __ret;                                                                 \
677 })
678
679 #endif /* _LUSTRE_LIB_H */
680
681 #define LMD_MAGIC 0xbdacbdac
682
683 #define lmd_bad_magic(LMDP)                                             \
684 ({                                                                      \
685         struct lustre_mount_data *_lmd__ = (LMDP);                      \
686         int _ret__ = 0;                                                 \
687         if (!_lmd__) {                                                  \
688                 CERROR("Missing mount data: "                           \
689                        "check that /sbin/mount.lustre is installed.\n");\
690                 _ret__ = 1;                                             \
691         } else if (_lmd__->lmd_magic != LMD_MAGIC) {                    \
692                 CERROR("Invalid mount data (%#x != %#x): "              \
693                        "check that /sbin/mount.lustre is installed\n",  \
694                        _lmd__->lmd_magic, LMD_MAGIC);                   \
695                 _ret__ = 1;                                             \
696         }                                                               \
697         _ret__;                                                         \
698 })
699