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