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