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