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