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