ext4: verify extent header depth
[cascardo/linux.git] / drivers / staging / lustre / lustre / include / lustre_lib.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/include/lustre_lib.h
37  *
38  * Basic Lustre library routines.
39  */
40
41 #ifndef _LUSTRE_LIB_H
42 #define _LUSTRE_LIB_H
43
44 /** \defgroup lib lib
45  *
46  * @{
47  */
48
49 #include <linux/sched.h>
50 #include <linux/signal.h>
51 #include <linux/types.h>
52 #include "../../include/linux/libcfs/libcfs.h"
53 #include "lustre/lustre_idl.h"
54 #include "lustre_ver.h"
55 #include "lustre_cfg.h"
56
57 /* target.c */
58 struct kstatfs;
59 struct ptlrpc_request;
60 struct obd_export;
61 struct lu_target;
62 struct l_wait_info;
63 #include "lustre_ha.h"
64 #include "lustre_net.h"
65
66 #define LI_POISON 0x5a5a5a5a
67 #if BITS_PER_LONG > 32
68 # define LL_POISON 0x5a5a5a5a5a5a5a5aL
69 #else
70 # define LL_POISON 0x5a5a5a5aL
71 #endif
72 #define LP_POISON ((void *)LL_POISON)
73
74 int target_pack_pool_reply(struct ptlrpc_request *req);
75 int do_set_info_async(struct obd_import *imp,
76                       int opcode, int version,
77                       u32 keylen, void *key,
78                       u32 vallen, void *val,
79                       struct ptlrpc_request_set *set);
80
81 #define OBD_RECOVERY_MAX_TIME (obd_timeout * 18) /* b13079 */
82 #define OBD_MAX_IOCTL_BUFFER CONFIG_LUSTRE_OBD_MAX_IOCTL_BUFFER
83
84 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
85
86 /* client.c */
87
88 int client_sanobd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg);
89 struct client_obd *client_conn2cli(struct lustre_handle *conn);
90
91 struct md_open_data;
92 struct obd_client_handle {
93         struct lustre_handle     och_fh;
94         struct lu_fid            och_fid;
95         struct md_open_data     *och_mod;
96         struct lustre_handle     och_lease_handle; /* open lock for lease */
97         __u32                    och_magic;
98         fmode_t                  och_flags;
99 };
100
101 #define OBD_CLIENT_HANDLE_MAGIC 0xd15ea5ed
102
103 /* statfs_pack.c */
104 void statfs_unpack(struct kstatfs *sfs, struct obd_statfs *osfs);
105
106 /*
107  * For md echo client
108  */
109 enum md_echo_cmd {
110         ECHO_MD_CREATE       = 1, /* Open/Create file on MDT */
111         ECHO_MD_MKDIR   = 2, /* Mkdir on MDT */
112         ECHO_MD_DESTROY      = 3, /* Unlink file on MDT */
113         ECHO_MD_RMDIR   = 4, /* Rmdir on MDT */
114         ECHO_MD_LOOKUP       = 5, /* Lookup on MDT */
115         ECHO_MD_GETATTR      = 6, /* Getattr on MDT */
116         ECHO_MD_SETATTR      = 7, /* Setattr on MDT */
117         ECHO_MD_ALLOC_FID    = 8, /* Get FIDs from MDT */
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         union {
130                 __u64 ioc_cookie;
131                 __u64 ioc_u64_1;
132         };
133         union {
134                 __u32 ioc_conn1;
135                 __u32 ioc_u32_1;
136         };
137         union {
138                 __u32 ioc_conn2;
139                 __u32 ioc_u32_2;
140         };
141
142         struct obdo ioc_obdo1;
143         struct obdo ioc_obdo2;
144
145         u64      ioc_count;
146         u64      ioc_offset;
147         __u32    ioc_dev;
148         __u32    ioc_command;
149
150         __u64 ioc_nid;
151         __u32 ioc_nal;
152         __u32 ioc_type;
153
154         /* buffers the kernel will treat as user pointers */
155         __u32  ioc_plen1;
156         void __user *ioc_pbuf1;
157         __u32  ioc_plen2;
158         void __user *ioc_pbuf2;
159
160         /* inline buffers for various arguments */
161         __u32  ioc_inllen1;
162         char  *ioc_inlbuf1;
163         __u32  ioc_inllen2;
164         char  *ioc_inlbuf2;
165         __u32  ioc_inllen3;
166         char  *ioc_inlbuf3;
167         __u32  ioc_inllen4;
168         char  *ioc_inlbuf4;
169
170         char    ioc_bulk[0];
171 };
172
173 struct obd_ioctl_hdr {
174         __u32 ioc_len;
175         __u32 ioc_version;
176 };
177
178 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
179 {
180         int len = cfs_size_round(sizeof(struct obd_ioctl_data));
181
182         len += cfs_size_round(data->ioc_inllen1);
183         len += cfs_size_round(data->ioc_inllen2);
184         len += cfs_size_round(data->ioc_inllen3);
185         len += cfs_size_round(data->ioc_inllen4);
186         return len;
187 }
188
189 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
190 {
191         if (data->ioc_len > OBD_MAX_IOCTL_BUFFER) {
192                 CERROR("OBD ioctl: ioc_len larger than %d\n",
193                        OBD_MAX_IOCTL_BUFFER);
194                 return 1;
195         }
196         if (data->ioc_inllen1 > OBD_MAX_IOCTL_BUFFER) {
197                 CERROR("OBD ioctl: ioc_inllen1 larger than ioc_len\n");
198                 return 1;
199         }
200         if (data->ioc_inllen2 > OBD_MAX_IOCTL_BUFFER) {
201                 CERROR("OBD ioctl: ioc_inllen2 larger than ioc_len\n");
202                 return 1;
203         }
204         if (data->ioc_inllen3 > OBD_MAX_IOCTL_BUFFER) {
205                 CERROR("OBD ioctl: ioc_inllen3 larger than ioc_len\n");
206                 return 1;
207         }
208         if (data->ioc_inllen4 > OBD_MAX_IOCTL_BUFFER) {
209                 CERROR("OBD ioctl: ioc_inllen4 larger than ioc_len\n");
210                 return 1;
211         }
212         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
213                 CERROR("OBD ioctl: inlbuf1 pointer but 0 length\n");
214                 return 1;
215         }
216         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
217                 CERROR("OBD ioctl: inlbuf2 pointer but 0 length\n");
218                 return 1;
219         }
220         if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
221                 CERROR("OBD ioctl: inlbuf3 pointer but 0 length\n");
222                 return 1;
223         }
224         if (data->ioc_inlbuf4 && !data->ioc_inllen4) {
225                 CERROR("OBD ioctl: inlbuf4 pointer but 0 length\n");
226                 return 1;
227         }
228         if (data->ioc_pbuf1 && !data->ioc_plen1) {
229                 CERROR("OBD ioctl: pbuf1 pointer but 0 length\n");
230                 return 1;
231         }
232         if (data->ioc_pbuf2 && !data->ioc_plen2) {
233                 CERROR("OBD ioctl: pbuf2 pointer but 0 length\n");
234                 return 1;
235         }
236         if (data->ioc_plen1 && !data->ioc_pbuf1) {
237                 CERROR("OBD ioctl: plen1 set but NULL pointer\n");
238                 return 1;
239         }
240         if (data->ioc_plen2 && !data->ioc_pbuf2) {
241                 CERROR("OBD ioctl: plen2 set but NULL pointer\n");
242                 return 1;
243         }
244         if (obd_ioctl_packlen(data) > data->ioc_len) {
245                 CERROR("OBD ioctl: packlen exceeds ioc_len (%d > %d)\n",
246                        obd_ioctl_packlen(data), data->ioc_len);
247                 return 1;
248         }
249         return 0;
250 }
251
252 #include "obd_support.h"
253
254 /* function defined in lustre/obdclass/<platform>/<platform>-module.c */
255 int obd_ioctl_getdata(char **buf, int *len, void __user *arg);
256 int obd_ioctl_popdata(void __user *arg, void *data, int len);
257
258 static inline void obd_ioctl_freedata(char *buf, int len)
259 {
260         kvfree(buf);
261         return;
262 }
263
264 /*
265  * BSD ioctl description:
266  * #define IOC_V1       _IOR(g, n1, long)
267  * #define IOC_V2       _IOW(g, n2, long)
268  *
269  * ioctl(f, IOC_V1, arg);
270  * arg will be treated as a long value,
271  *
272  * ioctl(f, IOC_V2, arg)
273  * arg will be treated as a pointer, bsd will call
274  * copyin(buf, arg, sizeof(long))
275  *
276  * To make BSD ioctl handles argument correctly and simplely,
277  * we change _IOR to _IOWR so BSD will copyin obd_ioctl_data
278  * for us. Does this change affect Linux?  (XXX Liang)
279  */
280 #define OBD_IOC_DATA_TYPE long
281
282 #define OBD_IOC_CREATE           _IOWR('f', 101, OBD_IOC_DATA_TYPE)
283 #define OBD_IOC_DESTROY         _IOW('f', 104, OBD_IOC_DATA_TYPE)
284 #define OBD_IOC_PREALLOCATE         _IOWR('f', 105, OBD_IOC_DATA_TYPE)
285
286 #define OBD_IOC_SETATTR         _IOW('f', 107, OBD_IOC_DATA_TYPE)
287 #define OBD_IOC_GETATTR         _IOWR ('f', 108, OBD_IOC_DATA_TYPE)
288 #define OBD_IOC_READ               _IOWR('f', 109, OBD_IOC_DATA_TYPE)
289 #define OBD_IOC_WRITE             _IOWR('f', 110, OBD_IOC_DATA_TYPE)
290
291 #define OBD_IOC_STATFS           _IOWR('f', 113, OBD_IOC_DATA_TYPE)
292 #define OBD_IOC_SYNC               _IOW('f', 114, OBD_IOC_DATA_TYPE)
293 #define OBD_IOC_READ2             _IOWR('f', 115, OBD_IOC_DATA_TYPE)
294 #define OBD_IOC_FORMAT           _IOWR('f', 116, OBD_IOC_DATA_TYPE)
295 #define OBD_IOC_PARTITION             _IOWR('f', 117, OBD_IOC_DATA_TYPE)
296 #define OBD_IOC_COPY               _IOWR('f', 120, OBD_IOC_DATA_TYPE)
297 #define OBD_IOC_MIGR               _IOWR('f', 121, OBD_IOC_DATA_TYPE)
298 #define OBD_IOC_PUNCH             _IOWR('f', 122, OBD_IOC_DATA_TYPE)
299
300 #define OBD_IOC_MODULE_DEBUG       _IOWR('f', 124, OBD_IOC_DATA_TYPE)
301 #define OBD_IOC_BRW_READ               _IOWR('f', 125, OBD_IOC_DATA_TYPE)
302 #define OBD_IOC_BRW_WRITE             _IOWR('f', 126, OBD_IOC_DATA_TYPE)
303 #define OBD_IOC_NAME2DEV               _IOWR('f', 127, OBD_IOC_DATA_TYPE)
304 #define OBD_IOC_UUID2DEV               _IOWR('f', 130, OBD_IOC_DATA_TYPE)
305
306 #define OBD_IOC_GETNAME         _IOWR('f', 131, OBD_IOC_DATA_TYPE)
307 #define OBD_IOC_GETMDNAME             _IOR('f', 131, char[MAX_OBD_NAME])
308 #define OBD_IOC_GETDTNAME              OBD_IOC_GETNAME
309
310 #define OBD_IOC_LOV_GET_CONFIG   _IOWR('f', 132, OBD_IOC_DATA_TYPE)
311 #define OBD_IOC_CLIENT_RECOVER   _IOW('f', 133, OBD_IOC_DATA_TYPE)
312 #define OBD_IOC_PING_TARGET         _IOW('f', 136, OBD_IOC_DATA_TYPE)
313
314 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 139)
315 #define OBD_IOC_NO_TRANSNO           _IOW('f', 140, OBD_IOC_DATA_TYPE)
316 #define OBD_IOC_SET_READONLY       _IOW('f', 141, OBD_IOC_DATA_TYPE)
317 #define OBD_IOC_ABORT_RECOVERY   _IOR('f', 142, OBD_IOC_DATA_TYPE)
318
319 #define OBD_IOC_ROOT_SQUASH         _IOWR('f', 143, OBD_IOC_DATA_TYPE)
320
321 #define OBD_GET_VERSION         _IOWR ('f', 144, OBD_IOC_DATA_TYPE)
322
323 #define OBD_IOC_GSS_SUPPORT         _IOWR('f', 145, OBD_IOC_DATA_TYPE)
324
325 #define OBD_IOC_CLOSE_UUID           _IOWR ('f', 147, OBD_IOC_DATA_TYPE)
326
327 #define OBD_IOC_CHANGELOG_SEND   _IOW('f', 148, OBD_IOC_DATA_TYPE)
328 #define OBD_IOC_GETDEVICE             _IOWR ('f', 149, OBD_IOC_DATA_TYPE)
329 #define OBD_IOC_FID2PATH               _IOWR ('f', 150, OBD_IOC_DATA_TYPE)
330 /* see also <lustre/lustre_user.h> for ioctls 151-153 */
331 /* OBD_IOC_LOV_SETSTRIPE: See also LL_IOC_LOV_SETSTRIPE */
332 #define OBD_IOC_LOV_SETSTRIPE     _IOW('f', 154, OBD_IOC_DATA_TYPE)
333 /* OBD_IOC_LOV_GETSTRIPE: See also LL_IOC_LOV_GETSTRIPE */
334 #define OBD_IOC_LOV_GETSTRIPE     _IOW('f', 155, OBD_IOC_DATA_TYPE)
335 /* OBD_IOC_LOV_SETEA: See also LL_IOC_LOV_SETEA */
336 #define OBD_IOC_LOV_SETEA             _IOW('f', 156, OBD_IOC_DATA_TYPE)
337 /* see <lustre/lustre_user.h> for ioctls 157-159 */
338 /* OBD_IOC_QUOTACHECK: See also LL_IOC_QUOTACHECK */
339 #define OBD_IOC_QUOTACHECK           _IOW('f', 160, int)
340 /* OBD_IOC_POLL_QUOTACHECK: See also LL_IOC_POLL_QUOTACHECK */
341 #define OBD_IOC_POLL_QUOTACHECK _IOR('f', 161, struct if_quotacheck *)
342 /* OBD_IOC_QUOTACTL: See also LL_IOC_QUOTACTL */
343 #define OBD_IOC_QUOTACTL               _IOWR('f', 162, struct if_quotactl)
344 /* see  also <lustre/lustre_user.h> for ioctls 163-176 */
345 #define OBD_IOC_CHANGELOG_REG     _IOW('f', 177, struct obd_ioctl_data)
346 #define OBD_IOC_CHANGELOG_DEREG _IOW('f', 178, struct obd_ioctl_data)
347 #define OBD_IOC_CHANGELOG_CLEAR _IOW('f', 179, struct obd_ioctl_data)
348 #define OBD_IOC_RECORD           _IOWR('f', 180, OBD_IOC_DATA_TYPE)
349 #define OBD_IOC_ENDRECORD             _IOWR('f', 181, OBD_IOC_DATA_TYPE)
350 #define OBD_IOC_PARSE             _IOWR('f', 182, OBD_IOC_DATA_TYPE)
351 #define OBD_IOC_DORECORD               _IOWR('f', 183, OBD_IOC_DATA_TYPE)
352 #define OBD_IOC_PROCESS_CFG         _IOWR('f', 184, OBD_IOC_DATA_TYPE)
353 #define OBD_IOC_DUMP_LOG               _IOWR('f', 185, OBD_IOC_DATA_TYPE)
354 #define OBD_IOC_CLEAR_LOG             _IOWR('f', 186, OBD_IOC_DATA_TYPE)
355 #define OBD_IOC_PARAM             _IOW('f', 187, OBD_IOC_DATA_TYPE)
356 #define OBD_IOC_POOL               _IOWR('f', 188, OBD_IOC_DATA_TYPE)
357 #define OBD_IOC_REPLACE_NIDS       _IOWR('f', 189, OBD_IOC_DATA_TYPE)
358
359 #define OBD_IOC_CATLOGLIST           _IOWR('f', 190, OBD_IOC_DATA_TYPE)
360 #define OBD_IOC_LLOG_INFO             _IOWR('f', 191, OBD_IOC_DATA_TYPE)
361 #define OBD_IOC_LLOG_PRINT           _IOWR('f', 192, OBD_IOC_DATA_TYPE)
362 #define OBD_IOC_LLOG_CANCEL         _IOWR('f', 193, OBD_IOC_DATA_TYPE)
363 #define OBD_IOC_LLOG_REMOVE         _IOWR('f', 194, OBD_IOC_DATA_TYPE)
364 #define OBD_IOC_LLOG_CHECK           _IOWR('f', 195, OBD_IOC_DATA_TYPE)
365 /* OBD_IOC_LLOG_CATINFO is deprecated */
366 #define OBD_IOC_LLOG_CATINFO       _IOWR('f', 196, OBD_IOC_DATA_TYPE)
367
368 /*      #define ECHO_IOC_GET_STRIPE    _IOWR('f', 200, OBD_IOC_DATA_TYPE) */
369 /*      #define ECHO_IOC_SET_STRIPE    _IOWR('f', 201, OBD_IOC_DATA_TYPE) */
370 /*      #define ECHO_IOC_ENQUEUE       _IOWR('f', 202, OBD_IOC_DATA_TYPE) */
371 /*      #define ECHO_IOC_CANCEL        _IOWR('f', 203, OBD_IOC_DATA_TYPE) */
372
373 #define OBD_IOC_GET_OBJ_VERSION _IOR('f', 210, OBD_IOC_DATA_TYPE)
374
375 /* <lustre/lustre_user.h> defines ioctl number 218-219 */
376 #define OBD_IOC_GET_MNTOPT           _IOW('f', 220, mntopt_t)
377
378 #define OBD_IOC_ECHO_MD         _IOR('f', 221, struct obd_ioctl_data)
379 #define OBD_IOC_ECHO_ALLOC_SEQ   _IOWR('f', 222, struct obd_ioctl_data)
380
381 #define OBD_IOC_START_LFSCK            _IOWR('f', 230, OBD_IOC_DATA_TYPE)
382 #define OBD_IOC_STOP_LFSCK             _IOW('f', 231, OBD_IOC_DATA_TYPE)
383 #define OBD_IOC_PAUSE_LFSCK            _IOW('f', 232, OBD_IOC_DATA_TYPE)
384
385 /* XXX _IOWR('f', 250, long) has been defined in
386  * libcfs/include/libcfs/libcfs_private.h for debug, don't use it
387  */
388
389 /* Until such time as we get_info the per-stripe maximum from the OST,
390  * we define this to be 2T - 4k, which is the ext3 maxbytes.
391  */
392 #define LUSTRE_STRIPE_MAXBYTES 0x1fffffff000ULL
393
394 /* Special values for remove LOV EA from disk */
395 #define LOVEA_DELETE_VALUES(size, count, offset) (size == 0 && count == 0 && \
396                                                  offset == (typeof(offset))(-1))
397
398 /* #define POISON_BULK 0 */
399
400 /*
401  * l_wait_event is a flexible sleeping function, permitting simple caller
402  * configuration of interrupt and timeout sensitivity along with actions to
403  * be performed in the event of either exception.
404  *
405  * The first form of usage looks like this:
406  *
407  * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
408  *                                         intr_handler, callback_data);
409  * rc = l_wait_event(waitq, condition, &lwi);
410  *
411  * l_wait_event() makes the current process wait on 'waitq' until 'condition'
412  * is TRUE or a "killable" signal (SIGTERM, SIKGILL, SIGINT) is pending.  It
413  * returns 0 to signify 'condition' is TRUE, but if a signal wakes it before
414  * 'condition' becomes true, it optionally calls the specified 'intr_handler'
415  * if not NULL, and returns -EINTR.
416  *
417  * If a non-zero timeout is specified, signals are ignored until the timeout
418  * has expired.  At this time, if 'timeout_handler' is not NULL it is called.
419  * If it returns FALSE l_wait_event() continues to wait as described above with
420  * signals enabled.  Otherwise it returns -ETIMEDOUT.
421  *
422  * LWI_INTR(intr_handler, callback_data) is shorthand for
423  * LWI_TIMEOUT_INTR(0, NULL, intr_handler, callback_data)
424  *
425  * The second form of usage looks like this:
426  *
427  * struct l_wait_info lwi = LWI_TIMEOUT(timeout, timeout_handler);
428  * rc = l_wait_event(waitq, condition, &lwi);
429  *
430  * This form is the same as the first except that it COMPLETELY IGNORES
431  * SIGNALS.  The caller must therefore beware that if 'timeout' is zero, or if
432  * 'timeout_handler' is not NULL and returns FALSE, then the ONLY thing that
433  * can unblock the current process is 'condition' becoming TRUE.
434  *
435  * Another form of usage is:
436  * struct l_wait_info lwi = LWI_TIMEOUT_INTERVAL(timeout, interval,
437  *                                             timeout_handler);
438  * rc = l_wait_event(waitq, condition, &lwi);
439  * This is the same as previous case, but condition is checked once every
440  * 'interval' jiffies (if non-zero).
441  *
442  * Subtle synchronization point: this macro does *not* necessary takes
443  * wait-queue spin-lock before returning, and, hence, following idiom is safe
444  * ONLY when caller provides some external locking:
445  *
446  *           Thread1                        Thread2
447  *
448  *   l_wait_event(&obj->wq, ....);                                     (1)
449  *
450  *                                  wake_up(&obj->wq):           (2)
451  *                                       spin_lock(&q->lock);     (2.1)
452  *                                       __wake_up_common(q, ...);     (2.2)
453  *                                       spin_unlock(&q->lock, flags); (2.3)
454  *
455  *   kfree(obj);                                                  (3)
456  *
457  * As l_wait_event() may "short-cut" execution and return without taking
458  * wait-queue spin-lock, some additional synchronization is necessary to
459  * guarantee that step (3) can begin only after (2.3) finishes.
460  *
461  * XXX nikita: some ptlrpc daemon threads have races of that sort.
462  *
463  */
464 static inline int back_to_sleep(void *arg)
465 {
466         return 0;
467 }
468
469 #define LWI_ON_SIGNAL_NOOP ((void (*)(void *))(-1))
470
471 struct l_wait_info {
472         long lwi_timeout;
473         long lwi_interval;
474         int         lwi_allow_intr;
475         int  (*lwi_on_timeout)(void *);
476         void (*lwi_on_signal)(void *);
477         void  *lwi_cb_data;
478 };
479
480 /* NB: LWI_TIMEOUT ignores signals completely */
481 #define LWI_TIMEOUT(time, cb, data)          \
482 ((struct l_wait_info) {                  \
483         .lwi_timeout    = time,          \
484         .lwi_on_timeout = cb,              \
485         .lwi_cb_data    = data,          \
486         .lwi_interval   = 0,                \
487         .lwi_allow_intr = 0                  \
488 })
489
490 #define LWI_TIMEOUT_INTERVAL(time, interval, cb, data)  \
491 ((struct l_wait_info) {                          \
492         .lwi_timeout    = time,                  \
493         .lwi_on_timeout = cb,                      \
494         .lwi_cb_data    = data,                  \
495         .lwi_interval   = interval,                  \
496         .lwi_allow_intr = 0                          \
497 })
498
499 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)   \
500 ((struct l_wait_info) {                          \
501         .lwi_timeout    = time,                  \
502         .lwi_on_timeout = time_cb,                    \
503         .lwi_on_signal  = sig_cb,                      \
504         .lwi_cb_data    = data,                  \
505         .lwi_interval   = 0,                        \
506         .lwi_allow_intr = 0                          \
507 })
508
509 #define LWI_TIMEOUT_INTR_ALL(time, time_cb, sig_cb, data)       \
510 ((struct l_wait_info) {                                  \
511         .lwi_timeout    = time,                          \
512         .lwi_on_timeout = time_cb,                            \
513         .lwi_on_signal  = sig_cb,                              \
514         .lwi_cb_data    = data,                          \
515         .lwi_interval   = 0,                                \
516         .lwi_allow_intr = 1                                  \
517 })
518
519 #define LWI_INTR(cb, data)  LWI_TIMEOUT_INTR(0, NULL, cb, data)
520
521 #define LUSTRE_FATAL_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) |         \
522                            sigmask(SIGTERM) | sigmask(SIGQUIT) |        \
523                            sigmask(SIGALRM))
524
525 /**
526  * wait_queue_t of Linux (version < 2.6.34) is a FIFO list for exclusively
527  * waiting threads, which is not always desirable because all threads will
528  * be waken up again and again, even user only needs a few of them to be
529  * active most time. This is not good for performance because cache can
530  * be polluted by different threads.
531  *
532  * LIFO list can resolve this problem because we always wakeup the most
533  * recent active thread by default.
534  *
535  * NB: please don't call non-exclusive & exclusive wait on the same
536  * waitq if add_wait_queue_exclusive_head is used.
537  */
538 #define add_wait_queue_exclusive_head(waitq, link)              \
539 {                                                               \
540         unsigned long flags;                                    \
541                                                                 \
542         spin_lock_irqsave(&((waitq)->lock), flags);             \
543         __add_wait_queue_exclusive(waitq, link);                \
544         spin_unlock_irqrestore(&((waitq)->lock), flags);        \
545 }
546
547 /*
548  * wait for @condition to become true, but no longer than timeout, specified
549  * by @info.
550  */
551 #define __l_wait_event(wq, condition, info, ret, l_add_wait)               \
552 do {                                                                       \
553         wait_queue_t __wait;                                             \
554         long __timeout = info->lwi_timeout;                       \
555         sigset_t   __blocked;                                         \
556         int   __allow_intr = info->lwi_allow_intr;                           \
557                                                                                \
558         ret = 0;                                                               \
559         if (condition)                                                   \
560                 break;                                                   \
561                                                                                \
562         init_waitqueue_entry(&__wait, current);                                     \
563         l_add_wait(&wq, &__wait);                                             \
564                                                                                \
565         /* Block all signals (just the non-fatal ones if no timeout). */       \
566         if (info->lwi_on_signal && (__timeout == 0 || __allow_intr))   \
567                 __blocked = cfs_block_sigsinv(LUSTRE_FATAL_SIGS);             \
568         else                                                               \
569                 __blocked = cfs_block_sigsinv(0);                             \
570                                                                                \
571         for (;;) {                                                           \
572                 if (condition)                                           \
573                         break;                                           \
574                                                                                \
575                 set_current_state(TASK_INTERRUPTIBLE);                         \
576                                                                                \
577                 if (__timeout == 0) {                                     \
578                         schedule();                                            \
579                 } else {                                                       \
580                         long interval = info->lwi_interval ?      \
581                                              min_t(long,             \
582                                                  info->lwi_interval, __timeout) : \
583                                              __timeout;                 \
584                         long remaining = schedule_timeout(interval);\
585                         __timeout = cfs_time_sub(__timeout,                 \
586                                             cfs_time_sub(interval, remaining));\
587                         if (__timeout == 0) {                             \
588                                 if (!info->lwi_on_timeout ||                  \
589                                     info->lwi_on_timeout(info->lwi_cb_data)) { \
590                                         ret = -ETIMEDOUT;                     \
591                                         break;                           \
592                                 }                                             \
593                                 /* Take signals after the timeout expires. */  \
594                                 if (info->lwi_on_signal)                       \
595                                     (void)cfs_block_sigsinv(LUSTRE_FATAL_SIGS);\
596                         }                                                     \
597                 }                                                             \
598                                                                                \
599                 set_current_state(TASK_RUNNING);                               \
600                                                                                \
601                 if (condition)                                           \
602                         break;                                           \
603                 if (signal_pending(current)) {                              \
604                         if (info->lwi_on_signal &&                   \
605                             (__timeout == 0 || __allow_intr)) {         \
606                                 if (info->lwi_on_signal != LWI_ON_SIGNAL_NOOP) \
607                                         info->lwi_on_signal(info->lwi_cb_data);\
608                                 ret = -EINTR;                             \
609                                 break;                                   \
610                         }                                                     \
611                         /* We have to do this here because some signals */     \
612                         /* are not blockable - ie from strace(1).       */     \
613                         /* In these cases we want to schedule_timeout() */     \
614                         /* again, because we don't want that to return  */     \
615                         /* -EINTR when the RPC actually succeeded.      */     \
616                         /* the recalc_sigpending() below will deliver the */     \
617                         /* signal properly.                          */     \
618                         cfs_clear_sigpending();                         \
619                 }                                                             \
620         }                                                                     \
621                                                                                \
622         cfs_restore_sigs(__blocked);                                       \
623                                                                                \
624         remove_wait_queue(&wq, &__wait);                                           \
625 } while (0)
626
627 #define l_wait_event(wq, condition, info)                      \
628 ({                                                            \
629         int              __ret;                       \
630         struct l_wait_info *__info = (info);                \
631                                                                 \
632         __l_wait_event(wq, condition, __info,              \
633                        __ret, add_wait_queue);             \
634         __ret;                                            \
635 })
636
637 #define l_wait_event_exclusive(wq, condition, info)          \
638 ({                                                            \
639         int              __ret;                       \
640         struct l_wait_info *__info = (info);                \
641                                                                 \
642         __l_wait_event(wq, condition, __info,              \
643                        __ret, add_wait_queue_exclusive);         \
644         __ret;                                            \
645 })
646
647 #define l_wait_event_exclusive_head(wq, condition, info)        \
648 ({                                                            \
649         int              __ret;                       \
650         struct l_wait_info *__info = (info);                \
651                                                                 \
652         __l_wait_event(wq, condition, __info,              \
653                        __ret, add_wait_queue_exclusive_head);    \
654         __ret;                                            \
655 })
656
657 #define l_wait_condition(wq, condition)                  \
658 ({                                                            \
659         struct l_wait_info lwi = { 0 };                  \
660         l_wait_event(wq, condition, &lwi);                    \
661 })
662
663 #define l_wait_condition_exclusive(wq, condition)              \
664 ({                                                            \
665         struct l_wait_info lwi = { 0 };                  \
666         l_wait_event_exclusive(wq, condition, &lwi);        \
667 })
668
669 #define l_wait_condition_exclusive_head(wq, condition)    \
670 ({                                                            \
671         struct l_wait_info lwi = { 0 };                  \
672         l_wait_event_exclusive_head(wq, condition, &lwi);       \
673 })
674
675 #define LIBLUSTRE_CLIENT (0)
676
677 /** @} lib */
678
679 #endif /* _LUSTRE_LIB_H */