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