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