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