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