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