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