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