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