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