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