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