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