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