Whamcloud - gitweb
b=14230
[fs/lustre-release.git] / lustre / include / 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 <libcfs/kp30.h>
29 #include <lustre/lustre_idl.h>
30 #include <lustre_ver.h>
31 #include <lustre_cfg.h>
32 #if defined(__linux__)
33 #include <linux/lustre_lib.h>
34 #elif defined(__APPLE__)
35 #include <darwin/lustre_lib.h>
36 #elif defined(__WINNT__)
37 #include <winnt/lustre_lib.h>
38 #else
39 #error Unsupported operating system.
40 #endif
41
42 /* prng.c */
43 unsigned int ll_rand(void);        /* returns a random 32-bit integer */
44 void ll_srand(unsigned int, unsigned int);     /* seed the generator */
45 void ll_get_random_bytes(void *buf, int size);
46
47 /* target.c */
48 struct ptlrpc_request;
49 struct recovd_data;
50 struct recovd_obd;
51 struct obd_export;
52 #include <lustre_ha.h>
53 #include <lustre_net.h>
54 #include <lvfs.h>
55
56 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
57                           int error);
58 int target_handle_connect(struct ptlrpc_request *req);
59 int target_handle_disconnect(struct ptlrpc_request *req);
60 void target_destroy_export(struct obd_export *exp);
61 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
62                             struct obd_uuid *cluuid, int);
63 int target_pack_pool_reply(struct ptlrpc_request *req);
64 int target_handle_ping(struct ptlrpc_request *req);
65 void target_committed_to_req(struct ptlrpc_request *req);
66
67 #ifdef HAVE_QUOTA_SUPPORT
68 /* quotacheck callback, dqacq/dqrel callback handler */
69 int target_handle_qc_callback(struct ptlrpc_request *req);
70 int target_handle_dqacq_callback(struct ptlrpc_request *req);
71 #else
72 #define target_handle_dqacq_callback(req) ldlm_callback_reply(req, -ENOTSUPP)
73 #define target_handle_qc_callback(req) (0)
74 #endif
75
76 void target_cancel_recovery_timer(struct obd_device *obd);
77
78 #define OBD_RECOVERY_TIMEOUT (obd_timeout * 5 / 2) /* *waves hands* */
79 #define OBD_RECOVERY_MAX_TIME (obd_timeout * 18) /* b13079 */
80 void target_start_recovery_timer(struct obd_device *obd);
81 int target_start_recovery_thread(struct obd_device *obd, 
82                                   svc_handler_t handler);
83 void target_stop_recovery_thread(struct obd_device *obd);
84 void target_cleanup_recovery(struct obd_device *obd);
85 int target_queue_recovery_request(struct ptlrpc_request *req,
86                                   struct obd_device *obd);
87 int target_queue_final_reply(struct ptlrpc_request *req, int rc);
88 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
89
90 /* client.c */
91
92 int client_sanobd_setup(struct obd_device *obddev, struct lustre_cfg* lcfg);
93 struct client_obd *client_conn2cli(struct lustre_handle *conn);
94
95 struct md_open_data;
96 struct obd_client_handle {
97         struct lustre_handle  och_fh;
98         struct lu_fid         och_fid;
99         struct md_open_data *och_mod;
100         __u32 och_magic;
101         int och_flags;
102 };
103 #define OBD_CLIENT_HANDLE_MAGIC 0xd15ea5ed
104
105 /* statfs_pack.c */
106 void statfs_pack(struct obd_statfs *osfs, struct kstatfs *sfs);
107 void statfs_unpack(struct kstatfs *sfs, struct obd_statfs *osfs);
108
109 /* l_lock.c */
110 struct lustre_lock {
111         int l_depth;
112         cfs_task_t *l_owner;
113         struct semaphore l_sem;
114         spinlock_t l_spin;
115 };
116
117 void l_lock_init(struct lustre_lock *);
118 void l_lock(struct lustre_lock *);
119 void l_unlock(struct lustre_lock *);
120 int l_has_lock(struct lustre_lock *);
121
122
123 /*
124  *   OBD IOCTLS
125  */
126 #define OBD_IOCTL_VERSION 0x00010004
127
128 struct obd_ioctl_data {
129         __u32 ioc_len;
130         __u32 ioc_version;
131
132         __u64 ioc_cookie;
133         __u32 ioc_conn1;
134         __u32 ioc_conn2;
135
136         struct obdo ioc_obdo1;
137         struct obdo ioc_obdo2;
138
139         obd_size ioc_count;
140         obd_off  ioc_offset;
141         __u32    ioc_dev;
142         __u32    ioc_command;
143
144         __u64 ioc_nid;
145         __u32 ioc_nal;
146         __u32 ioc_type;
147
148         /* buffers the kernel will treat as user pointers */
149         __u32  ioc_plen1;
150         char  *ioc_pbuf1;
151         __u32  ioc_plen2;
152         char  *ioc_pbuf2;
153
154         /* inline buffers for various arguments */
155         __u32  ioc_inllen1;
156         char  *ioc_inlbuf1;
157         __u32  ioc_inllen2;
158         char  *ioc_inlbuf2;
159         __u32  ioc_inllen3;
160         char  *ioc_inlbuf3;
161         __u32  ioc_inllen4;
162         char  *ioc_inlbuf4;
163
164         char    ioc_bulk[0];
165 };
166
167 struct obd_ioctl_hdr {
168         __u32 ioc_len;
169         __u32 ioc_version;
170 };
171
172 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
173 {
174         int len = size_round(sizeof(struct obd_ioctl_data));
175         len += size_round(data->ioc_inllen1);
176         len += size_round(data->ioc_inllen2);
177         len += size_round(data->ioc_inllen3);
178         len += size_round(data->ioc_inllen4);
179         return len;
180 }
181
182
183 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
184 {
185         if (data->ioc_len > (1<<30)) {
186                 CERROR("OBD ioctl: ioc_len larger than 1<<30\n");
187                 return 1;
188         }
189         if (data->ioc_inllen1 > (1<<30)) {
190                 CERROR("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
191                 return 1;
192         }
193         if (data->ioc_inllen2 > (1<<30)) {
194                 CERROR("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
195                 return 1;
196         }
197         if (data->ioc_inllen3 > (1<<30)) {
198                 CERROR("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
199                 return 1;
200         }
201         if (data->ioc_inllen4 > (1<<30)) {
202                 CERROR("OBD ioctl: ioc_inllen4 larger than 1<<30\n");
203                 return 1;
204         }
205         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
206                 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
207                 return 1;
208         }
209         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
210                 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
211                 return 1;
212         }
213         if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
214                 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
215                 return 1;
216         }
217         if (data->ioc_inlbuf4 && !data->ioc_inllen4) {
218                 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
219                 return 1;
220         }
221         if (data->ioc_pbuf1 && !data->ioc_plen1) {
222                 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
223                 return 1;
224         }
225         if (data->ioc_pbuf2 && !data->ioc_plen2) {
226                 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
227                 return 1;
228         }
229         if (data->ioc_plen1 && !data->ioc_pbuf1) {
230                 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
231                 return 1;
232         }
233         if (data->ioc_plen2 && !data->ioc_pbuf2) {
234                 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
235                 return 1;
236         }
237         if (obd_ioctl_packlen(data) > data->ioc_len) {
238                 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
239                        obd_ioctl_packlen(data), data->ioc_len);
240                 return 1;
241         }
242         return 0;
243 }
244
245 #ifndef __KERNEL__
246 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
247                                  int max)
248 {
249         char *ptr;
250         struct obd_ioctl_data *overlay;
251         data->ioc_len = obd_ioctl_packlen(data);
252         data->ioc_version = OBD_IOCTL_VERSION;
253
254         if (*pbuf && data->ioc_len > max)
255                 return -EINVAL;
256         if (*pbuf == NULL) {
257                 *pbuf = malloc(data->ioc_len);
258         }
259         if (!*pbuf)
260                 return -ENOMEM;
261         overlay = (struct obd_ioctl_data *)*pbuf;
262         memcpy(*pbuf, data, sizeof(*data));
263
264         ptr = overlay->ioc_bulk;
265         if (data->ioc_inlbuf1)
266                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
267         if (data->ioc_inlbuf2)
268                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
269         if (data->ioc_inlbuf3)
270                 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
271         if (data->ioc_inlbuf4)
272                 LOGL(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
273         if (obd_ioctl_is_invalid(overlay))
274                 return -EINVAL;
275
276         return 0;
277 }
278
279 static inline int obd_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf,
280                                    int max)
281 {
282         char *ptr;
283         struct obd_ioctl_data *overlay;
284
285         if (!pbuf)
286                 return 1;
287         overlay = (struct obd_ioctl_data *)pbuf;
288
289         /* Preserve the caller's buffer pointers */
290         overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
291         overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
292         overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
293         overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
294
295         memcpy(data, pbuf, sizeof(*data));
296
297         ptr = overlay->ioc_bulk;
298         if (data->ioc_inlbuf1)
299                 LOGU(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
300         if (data->ioc_inlbuf2)
301                 LOGU(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
302         if (data->ioc_inlbuf3)
303                 LOGU(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
304         if (data->ioc_inlbuf4)
305                 LOGU(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
306
307         return 0;
308 }
309 #endif
310
311 #include <obd_support.h>
312
313 #ifdef __KERNEL__
314 /* function defined in lustre/obdclass/<platform>/<platform>-module.c */
315 int obd_ioctl_getdata(char **buf, int *len, void *arg);
316 int obd_ioctl_popdata(void *arg, void *data, int len);
317 #else
318 /* buffer MUST be at least the size of obd_ioctl_hdr */
319 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
320 {
321         struct obd_ioctl_hdr hdr;
322         struct obd_ioctl_data *data;
323         int err;
324         int offset = 0;
325         ENTRY;
326
327         err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
328         if (err) 
329                 RETURN(err);
330
331         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
332                 CERROR("Version mismatch kernel vs application\n");
333                 RETURN(-EINVAL);
334         }
335
336         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
337                 CERROR("User buffer len %d exceeds %d max buffer\n",
338                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
339                 RETURN(-EINVAL);
340         }
341
342         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
343                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
344                 RETURN(-EINVAL);
345         }
346
347         /* XXX allocate this more intelligently, using kmalloc when
348          * appropriate */
349         OBD_VMALLOC(*buf, hdr.ioc_len);
350         if (*buf == NULL) {
351                 CERROR("Cannot allocate control buffer of len %d\n",
352                        hdr.ioc_len);
353                 RETURN(-EINVAL);
354         }
355         *len = hdr.ioc_len;
356         data = (struct obd_ioctl_data *)*buf;
357
358         err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
359         if (err) {
360                 OBD_VFREE(*buf, hdr.ioc_len);
361                 RETURN(err);
362         }
363
364         if (obd_ioctl_is_invalid(data)) {
365                 CERROR("ioctl not correctly formatted\n");
366                 OBD_VFREE(*buf, hdr.ioc_len);
367                 RETURN(-EINVAL);
368         }
369
370         if (data->ioc_inllen1) {
371                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
372                 offset += size_round(data->ioc_inllen1);
373         }
374
375         if (data->ioc_inllen2) {
376                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
377                 offset += size_round(data->ioc_inllen2);
378         }
379
380         if (data->ioc_inllen3) {
381                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
382                 offset += size_round(data->ioc_inllen3);
383         }
384
385         if (data->ioc_inllen4) {
386                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
387         }
388
389         RETURN(0);
390 }
391
392 static inline int obd_ioctl_popdata(void *arg, void *data, int len)
393 {
394         int err = copy_to_user(arg, data, len);
395         if (err)
396                 err = -EFAULT;
397         return err;
398 }
399 #endif
400
401 static inline void obd_ioctl_freedata(char *buf, int len)
402 {
403         ENTRY;
404
405         OBD_VFREE(buf, len);
406         EXIT;
407         return;
408 }
409
410 /*
411  * BSD ioctl description:
412  * #define IOC_V1       _IOR(g, n1, long)
413  * #define IOC_V2       _IOW(g, n2, long)
414  *
415  * ioctl(f, IOC_V1, arg);
416  * arg will be treated as a long value,
417  *
418  * ioctl(f, IOC_V2, arg)
419  * arg will be treated as a pointer, bsd will call
420  * copyin(buf, arg, sizeof(long))
421  *
422  * To make BSD ioctl handles argument correctly and simplely, 
423  * we change _IOR to _IOWR so BSD will copyin obd_ioctl_data 
424  * for us. Does this change affect Linux?  (XXX Liang)
425  */
426 #define OBD_IOC_CREATE                 _IOWR('f', 101, OBD_IOC_DATA_TYPE)
427 #define OBD_IOC_DESTROY                _IOW ('f', 104, OBD_IOC_DATA_TYPE)
428 #define OBD_IOC_PREALLOCATE            _IOWR('f', 105, OBD_IOC_DATA_TYPE)
429
430 #define OBD_IOC_SETATTR                _IOW ('f', 107, OBD_IOC_DATA_TYPE)
431 #define OBD_IOC_GETATTR                _IOWR ('f', 108, OBD_IOC_DATA_TYPE)
432 #define OBD_IOC_READ                   _IOWR('f', 109, OBD_IOC_DATA_TYPE)
433 #define OBD_IOC_WRITE                  _IOWR('f', 110, OBD_IOC_DATA_TYPE)
434
435
436 #define OBD_IOC_STATFS                 _IOWR('f', 113, OBD_IOC_DATA_TYPE)
437 #define OBD_IOC_SYNC                   _IOW ('f', 114, OBD_IOC_DATA_TYPE)
438 #define OBD_IOC_READ2                  _IOWR('f', 115, OBD_IOC_DATA_TYPE)
439 #define OBD_IOC_FORMAT                 _IOWR('f', 116, OBD_IOC_DATA_TYPE)
440 #define OBD_IOC_PARTITION              _IOWR('f', 117, OBD_IOC_DATA_TYPE)
441 #define OBD_IOC_COPY                   _IOWR('f', 120, OBD_IOC_DATA_TYPE)
442 #define OBD_IOC_MIGR                   _IOWR('f', 121, OBD_IOC_DATA_TYPE)
443 #define OBD_IOC_PUNCH                  _IOWR('f', 122, OBD_IOC_DATA_TYPE)
444
445 #define OBD_IOC_MODULE_DEBUG           _IOWR('f', 124, OBD_IOC_DATA_TYPE)
446 #define OBD_IOC_BRW_READ               _IOWR('f', 125, OBD_IOC_DATA_TYPE)
447 #define OBD_IOC_BRW_WRITE              _IOWR('f', 126, OBD_IOC_DATA_TYPE)
448 #define OBD_IOC_NAME2DEV               _IOWR('f', 127, OBD_IOC_DATA_TYPE)
449 #define OBD_IOC_UUID2DEV               _IOWR('f', 130, OBD_IOC_DATA_TYPE)
450 #define OBD_IOC_GETNAME                _IOWR('f', 131, OBD_IOC_DATA_TYPE)
451
452 #define OBD_IOC_LOV_GET_CONFIG         _IOWR('f', 132, OBD_IOC_DATA_TYPE)
453 #define OBD_IOC_CLIENT_RECOVER         _IOW ('f', 133, OBD_IOC_DATA_TYPE)
454
455 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 139      )
456 #define OBD_IOC_NO_TRANSNO             _IOW ('f', 140, OBD_IOC_DATA_TYPE)
457 #define OBD_IOC_SET_READONLY           _IOW ('f', 141, OBD_IOC_DATA_TYPE)
458 #define OBD_IOC_ABORT_RECOVERY         _IOR ('f', 142, OBD_IOC_DATA_TYPE)
459
460 #define OBD_IOC_ROOT_SQUASH            _IOWR('f', 143, OBD_IOC_DATA_TYPE)
461
462 #define OBD_GET_VERSION                _IOWR ('f', 144, OBD_IOC_DATA_TYPE)
463
464 #define OBD_IOC_GSS_SUPPORT            _IOWR('f', 145, OBD_IOC_DATA_TYPE)
465
466 #define OBD_IOC_CLOSE_UUID             _IOWR ('f', 147, OBD_IOC_DATA_TYPE)
467
468 #define OBD_IOC_GETDEVICE              _IOWR ('f', 149, OBD_IOC_DATA_TYPE)
469
470 #define OBD_IOC_LOV_SETSTRIPE          _IOW ('f', 154, OBD_IOC_DATA_TYPE)
471 #define OBD_IOC_LOV_GETSTRIPE          _IOW ('f', 155, OBD_IOC_DATA_TYPE)
472 #define OBD_IOC_LOV_SETEA              _IOW ('f', 156, OBD_IOC_DATA_TYPE)
473
474 #define OBD_IOC_QUOTACHECK             _IOW ('f', 160, int)
475 #define OBD_IOC_POLL_QUOTACHECK        _IOR ('f', 161, struct if_quotacheck *)
476 #define OBD_IOC_QUOTACTL               _IOWR('f', 162, struct if_quotactl *)
477
478 #define OBD_IOC_MOUNTOPT               _IOWR('f', 170, OBD_IOC_DATA_TYPE)
479
480 #define OBD_IOC_RECORD                 _IOWR('f', 180, OBD_IOC_DATA_TYPE)
481 #define OBD_IOC_ENDRECORD              _IOWR('f', 181, OBD_IOC_DATA_TYPE)
482 #define OBD_IOC_PARSE                  _IOWR('f', 182, OBD_IOC_DATA_TYPE)
483 #define OBD_IOC_DORECORD               _IOWR('f', 183, OBD_IOC_DATA_TYPE)
484 #define OBD_IOC_PROCESS_CFG            _IOWR('f', 184, OBD_IOC_DATA_TYPE)
485 #define OBD_IOC_DUMP_LOG               _IOWR('f', 185, OBD_IOC_DATA_TYPE)
486 #define OBD_IOC_CLEAR_LOG              _IOWR('f', 186, OBD_IOC_DATA_TYPE)
487 #define OBD_IOC_PARAM                  _IOW ('f', 187, OBD_IOC_DATA_TYPE)
488
489 #define OBD_IOC_CATLOGLIST             _IOWR('f', 190, OBD_IOC_DATA_TYPE)
490 #define OBD_IOC_LLOG_INFO              _IOWR('f', 191, OBD_IOC_DATA_TYPE)
491 #define OBD_IOC_LLOG_PRINT             _IOWR('f', 192, OBD_IOC_DATA_TYPE)
492 #define OBD_IOC_LLOG_CANCEL            _IOWR('f', 193, OBD_IOC_DATA_TYPE)
493 #define OBD_IOC_LLOG_REMOVE            _IOWR('f', 194, OBD_IOC_DATA_TYPE)
494 #define OBD_IOC_LLOG_CHECK             _IOWR('f', 195, OBD_IOC_DATA_TYPE)
495 #define OBD_IOC_LLOG_CATINFO           _IOWR('f', 196, OBD_IOC_DATA_TYPE)
496
497 #define ECHO_IOC_GET_STRIPE            _IOWR('f', 200, OBD_IOC_DATA_TYPE)
498 #define ECHO_IOC_SET_STRIPE            _IOWR('f', 201, OBD_IOC_DATA_TYPE)
499 #define ECHO_IOC_ENQUEUE               _IOWR('f', 202, OBD_IOC_DATA_TYPE)
500 #define ECHO_IOC_CANCEL                _IOWR('f', 203, OBD_IOC_DATA_TYPE)
501
502 /* XXX _IOWR('f', 250, long) has been defined in
503  * lnet/include/libcfs/kp30.h for debug, don't use it
504  */
505
506 /* Until such time as we get_info the per-stripe maximum from the OST,
507  * we define this to be 2T - 4k, which is the ext3 maxbytes. */
508 #define LUSTRE_STRIPE_MAXBYTES 0x1fffffff000ULL
509
510 /* #define POISON_BULK 0 */
511
512 /*
513  * l_wait_event is a flexible sleeping function, permitting simple caller
514  * configuration of interrupt and timeout sensitivity along with actions to
515  * be performed in the event of either exception.
516  *
517  * The first form of usage looks like this:
518  *
519  * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
520  *                                           intr_handler, callback_data);
521  * rc = l_wait_event(waitq, condition, &lwi);
522  *
523  * l_wait_event() makes the current process wait on 'waitq' until 'condition'
524  * is TRUE or a "killable" signal (SIGTERM, SIKGILL, SIGINT) is pending.  It
525  * returns 0 to signify 'condition' is TRUE, but if a signal wakes it before
526  * 'condition' becomes true, it optionally calls the specified 'intr_handler'
527  * if not NULL, and returns -EINTR.
528  *
529  * If a non-zero timeout is specified, signals are ignored until the timeout
530  * has expired.  At this time, if 'timeout_handler' is not NULL it is called.
531  * If it returns FALSE l_wait_event() continues to wait as described above with
532  * signals enabled.  Otherwise it returns -ETIMEDOUT.
533  *
534  * LWI_INTR(intr_handler, callback_data) is shorthand for
535  * LWI_TIMEOUT_INTR(0, NULL, intr_handler, callback_data)
536  *
537  * The second form of usage looks like this:
538  *
539  * struct l_wait_info lwi = LWI_TIMEOUT(timeout, timeout_handler);
540  * rc = l_wait_event(waitq, condition, &lwi);
541  *
542  * This form is the same as the first except that it COMPLETELY IGNORES
543  * SIGNALS.  The caller must therefore beware that if 'timeout' is zero, or if
544  * 'timeout_handler' is not NULL and returns FALSE, then the ONLY thing that
545  * can unblock the current process is 'condition' becoming TRUE.
546  *
547  * Another form of usage is:
548  * struct l_wait_info lwi = LWI_TIMEOUT_INTERVAL(timeout, interval,
549  *                                               timeout_handler);
550  * rc = l_wait_event(waitq, condition, &lwi);
551  * This is the same as previous case, but condition is checked once every
552  * 'interval' jiffies (if non-zero).
553  *
554  * Subtle synchronization point: this macro does *not* necessary takes
555  * wait-queue spin-lock before returning, and, hence, following idiom is safe
556  * ONLY when caller provides some external locking:
557  *
558  *             Thread1                            Thread2
559  *
560  *   l_wait_event(&obj->wq, ....);                                       (1)
561  *
562  *                                    wake_up(&obj->wq):                 (2)
563  *                                         spin_lock(&q->lock);          (2.1)
564  *                                         __wake_up_common(q, ...);     (2.2)
565  *                                         spin_unlock(&q->lock, flags); (2.3)
566  *
567  *   OBD_FREE_PTR(obj);                                                  (3)
568  *
569  * As l_wait_event() may "short-cut" execution and return without taking
570  * wait-queue spin-lock, some additional synchronization is necessary to
571  * guarantee that step (3) can begin only after (2.3) finishes.
572  *
573  * XXX nikita: some ptlrpc daemon threads have races of that sort.
574  *
575  */
576
577 #define LWI_ON_SIGNAL_NOOP ((void (*)(void *))(-1))
578
579 struct l_wait_info {
580         cfs_duration_t lwi_timeout;
581         cfs_duration_t lwi_interval;
582         int  (*lwi_on_timeout)(void *);
583         void (*lwi_on_signal)(void *);
584         void  *lwi_cb_data;
585 };
586
587 /* NB: LWI_TIMEOUT ignores signals completely */
588 #define LWI_TIMEOUT(time, cb, data)             \
589 ((struct l_wait_info) {                         \
590         .lwi_timeout    = time,                 \
591         .lwi_on_timeout = cb,                   \
592         .lwi_cb_data    = data,                 \
593         .lwi_interval   = 0                     \
594 })
595
596 #define LWI_TIMEOUT_INTERVAL(time, interval, cb, data)  \
597 ((struct l_wait_info) {                                 \
598         .lwi_timeout    = time,                         \
599         .lwi_on_timeout = cb,                           \
600         .lwi_cb_data    = data,                         \
601         .lwi_interval   = interval                      \
602 })
603
604 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)   \
605 ((struct l_wait_info) {                                 \
606         .lwi_timeout    = time,                         \
607         .lwi_on_timeout = time_cb,                      \
608         .lwi_on_signal = sig_cb,                        \
609         .lwi_cb_data    = data,                         \
610         .lwi_interval    = 0                            \
611 })
612
613 #define LWI_INTR(cb, data)  LWI_TIMEOUT_INTR(0, NULL, cb, data)
614
615 #ifdef __KERNEL__
616
617 /*
618  * wait for @condition to become true, but no longer than timeout, specified
619  * by @info.
620  */
621 #define __l_wait_event(wq, condition, info, ret, excl)                         \
622 do {                                                                           \
623         cfs_waitlink_t __wait;                                                 \
624         cfs_duration_t __timeout = info->lwi_timeout;                          \
625         cfs_sigset_t   __blocked;                                              \
626                                                                                \
627         ret = 0;                                                               \
628         if (condition)                                                         \
629                 break;                                                         \
630                                                                                \
631         cfs_waitlink_init(&__wait);                                            \
632         if (excl)                                                              \
633                 cfs_waitq_add_exclusive(&wq, &__wait);                         \
634         else                                                                   \
635                 cfs_waitq_add(&wq, &__wait);                                   \
636                                                                                \
637         /* Block all signals (just the non-fatal ones if no timeout). */       \
638         if (info->lwi_on_signal != NULL && __timeout == 0)                     \
639                 __blocked = l_w_e_set_sigs(LUSTRE_FATAL_SIGS);                 \
640         else                                                                   \
641                 __blocked = l_w_e_set_sigs(0);                                 \
642                                                                                \
643         for (;;) {                                                             \
644                 set_current_state(TASK_INTERRUPTIBLE);                         \
645                                                                                \
646                 if (condition)                                                 \
647                         break;                                                 \
648                                                                                \
649                 if (__timeout == 0) {                                          \
650                         cfs_waitq_wait(&__wait, CFS_TASK_INTERRUPTIBLE);       \
651                 } else {                                                       \
652                         cfs_duration_t interval = info->lwi_interval?          \
653                                              min_t(cfs_duration_t,             \
654                                                  info->lwi_interval,__timeout):\
655                                              __timeout;                        \
656                         cfs_duration_t remaining = cfs_waitq_timedwait(&__wait,\
657                                                    CFS_TASK_INTERRUPTIBLE,     \
658                                                    interval);                  \
659                         __timeout = cfs_time_sub(__timeout,                    \
660                                             cfs_time_sub(interval, remaining));\
661                         if (__timeout == 0) {                                  \
662                                 if (info->lwi_on_timeout == NULL ||            \
663                                     info->lwi_on_timeout(info->lwi_cb_data)) { \
664                                         ret = -ETIMEDOUT;                      \
665                                         break;                                 \
666                                 }                                              \
667                                 /* Take signals after the timeout expires. */  \
668                                 if (info->lwi_on_signal != NULL)               \
669                                     (void)l_w_e_set_sigs(LUSTRE_FATAL_SIGS);   \
670                         }                                                      \
671                 }                                                              \
672                                                                                \
673                 if (condition)                                                 \
674                         break;                                                 \
675                 if (cfs_signal_pending()) {                                    \
676                         if (info->lwi_on_signal != NULL && __timeout == 0) {   \
677                                 if (info->lwi_on_signal != LWI_ON_SIGNAL_NOOP) \
678                                         info->lwi_on_signal(info->lwi_cb_data);\
679                                 ret = -EINTR;                                  \
680                                 break;                                         \
681                         }                                                      \
682                         /* We have to do this here because some signals */     \
683                         /* are not blockable - ie from strace(1).       */     \
684                         /* In these cases we want to schedule_timeout() */     \
685                         /* again, because we don't want that to return  */     \
686                         /* -EINTR when the RPC actually succeeded.      */     \
687                         /* the RECALC_SIGPENDING below will deliver the */     \
688                         /* signal properly.                             */     \
689                         cfs_clear_sigpending();                                \
690                 }                                                              \
691         }                                                                      \
692                                                                                \
693         cfs_block_sigs(__blocked);                                             \
694                                                                                \
695         set_current_state(TASK_RUNNING);                                       \
696         cfs_waitq_del(&wq, &__wait);                                           \
697 } while (0)
698
699 #else /* !__KERNEL__ */
700 #define __l_wait_event(wq, condition, info, ret, excl)                  \
701 do {                                                                    \
702         long __timeout = info->lwi_timeout;                             \
703         long __now;                                                     \
704         long __then = 0;                                                \
705         int  __timed_out = 0;                                           \
706         int  __interval = obd_timeout;                                  \
707                                                                         \
708         ret = 0;                                                        \
709         if (condition)                                                  \
710                 break;                                                  \
711                                                                         \
712         if (__timeout != 0)                                             \
713                 __then = time(NULL);                                    \
714                                                                         \
715         if (__timeout && __timeout < __interval)                        \
716                 __interval = __timeout;                                 \
717         if (info->lwi_interval && info->lwi_interval < __interval)      \
718                 __interval = info->lwi_interval;                        \
719                                                                         \
720         while (!(condition)) {                                          \
721                 liblustre_wait_event(__interval);                       \
722                 if (condition)                                          \
723                         break;                                          \
724                                                                         \
725                 if (!__timed_out && info->lwi_timeout != 0) {           \
726                         __now = time(NULL);                             \
727                         __timeout -= __now - __then;                    \
728                         __then = __now;                                 \
729                                                                         \
730                         if (__timeout > 0)                              \
731                                 continue;                               \
732                                                                         \
733                         __timeout = 0;                                  \
734                         __timed_out = 1;                                \
735                         if (info->lwi_on_timeout == NULL ||             \
736                             info->lwi_on_timeout(info->lwi_cb_data)) {  \
737                                 ret = -ETIMEDOUT;                       \
738                                 break;                                  \
739                         }                                               \
740                 }                                                       \
741         }                                                               \
742 } while (0)
743
744 #endif /* __KERNEL__ */
745
746
747 #define l_wait_event(wq, condition, info)                       \
748 ({                                                              \
749         int                 __ret;                              \
750         struct l_wait_info *__info = (info);                    \
751                                                                 \
752         __l_wait_event(wq, condition, __info, __ret, 0);        \
753         __ret;                                                  \
754 })
755
756 #define l_wait_event_exclusive(wq, condition, info)             \
757 ({                                                              \
758         int                 __ret;                              \
759         struct l_wait_info *__info = (info);                    \
760                                                                 \
761         __l_wait_event(wq, condition, __info, __ret, 1);        \
762         __ret;                                                  \
763 })
764
765 #define cfs_wait_event(wq, condition)                          \
766 ({                                                              \
767         int  __ret;                                                    \
768                                                                 \
769         struct l_wait_info lwi = { 0 };                         \
770         l_wait_event(wq, condition, &lwi);                      \
771         __ret;                                                  \
772 })
773
774 #ifdef __KERNEL__
775 #define LIBLUSTRE_CLIENT (0)
776 #else
777 #define LIBLUSTRE_CLIENT (1)
778 #endif
779
780 #endif /* _LUSTRE_LIB_H */
781