Whamcloud - gitweb
7ff3671b6c6e9de08b4132f0e7c992b96b2ee2f8
[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
71 #ifdef HAVE_SERVER_SUPPORT
72 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
73                           int error);
74 int target_handle_connect(struct ptlrpc_request *req);
75 int target_handle_disconnect(struct ptlrpc_request *req);
76 void target_destroy_export(struct obd_export *exp);
77 int target_handle_ping(struct ptlrpc_request *req);
78 void target_committed_to_req(struct ptlrpc_request *req);
79 void target_cancel_recovery_timer(struct obd_device *obd);
80 void target_stop_recovery_thread(struct obd_device *obd);
81 void target_cleanup_recovery(struct obd_device *obd);
82 int target_queue_recovery_request(struct ptlrpc_request *req,
83                                   struct obd_device *obd);
84 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc,
85                    struct l_wait_info *lwi);
86 #endif
87
88 int target_pack_pool_reply(struct ptlrpc_request *req);
89 int do_set_info_async(struct obd_import *imp,
90                       int opcode, int version,
91                       obd_count keylen, void *key,
92                       obd_count vallen, void *val,
93                       struct ptlrpc_request_set *set);
94
95 #define OBD_RECOVERY_MAX_TIME (obd_timeout * 18) /* b13079 */
96 #define OBD_MAX_IOCTL_BUFFER CONFIG_LUSTRE_OBD_MAX_IOCTL_BUFFER
97
98 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id);
99
100 /* client.c */
101
102 int client_sanobd_setup(struct obd_device *obddev, struct lustre_cfg* lcfg);
103 struct client_obd *client_conn2cli(struct lustre_handle *conn);
104
105 struct md_open_data;
106 struct obd_client_handle {
107         struct lustre_handle     och_fh;
108         struct lu_fid            och_fid;
109         struct md_open_data     *och_mod;
110         struct lustre_handle     och_lease_handle; /* open lock for lease */
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 /*
121  * For md echo client
122  */
123 enum md_echo_cmd {
124         ECHO_MD_CREATE       = 1, /* Open/Create file on MDT */
125         ECHO_MD_MKDIR        = 2, /* Mkdir on MDT */
126         ECHO_MD_DESTROY      = 3, /* Unlink file on MDT */
127         ECHO_MD_RMDIR        = 4, /* Rmdir on MDT */
128         ECHO_MD_LOOKUP       = 5, /* Lookup on MDT */
129         ECHO_MD_GETATTR      = 6, /* Getattr on MDT */
130         ECHO_MD_SETATTR      = 7, /* Setattr on MDT */
131         ECHO_MD_ALLOC_FID    = 8, /* Get FIDs from MDT */
132 };
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                 fprintf(stderr, "pbuf %p ioc_len %u max %d\n", *pbuf,
276                         data->ioc_len, max);
277                 return -EINVAL;
278         }
279         if (*pbuf == NULL) {
280                 *pbuf = malloc(data->ioc_len);
281         }
282         if (!*pbuf)
283                 return -ENOMEM;
284         overlay = (struct obd_ioctl_data *)*pbuf;
285         memcpy(*pbuf, data, sizeof(*data));
286
287         ptr = overlay->ioc_bulk;
288         if (data->ioc_inlbuf1)
289                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
290         if (data->ioc_inlbuf2)
291                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
292         if (data->ioc_inlbuf3)
293                 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
294         if (data->ioc_inlbuf4)
295                 LOGL(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
296         if (obd_ioctl_is_invalid(overlay)) {
297                 fprintf(stderr, "ioc_len %u max %d\n",
298                         data->ioc_len, max);
299                 return -EINVAL;
300         }
301
302         return 0;
303 }
304
305 static inline int obd_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf,
306                                    int max)
307 {
308         char *ptr;
309         struct obd_ioctl_data *overlay;
310
311         if (!pbuf)
312                 return 1;
313         overlay = (struct obd_ioctl_data *)pbuf;
314
315         /* Preserve the caller's buffer pointers */
316         overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
317         overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
318         overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
319         overlay->ioc_inlbuf4 = data->ioc_inlbuf4;
320
321         memcpy(data, pbuf, sizeof(*data));
322
323         ptr = overlay->ioc_bulk;
324         if (data->ioc_inlbuf1)
325                 LOGU(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
326         if (data->ioc_inlbuf2)
327                 LOGU(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
328         if (data->ioc_inlbuf3)
329                 LOGU(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
330         if (data->ioc_inlbuf4)
331                 LOGU(data->ioc_inlbuf4, data->ioc_inllen4, ptr);
332
333         return 0;
334 }
335 #endif
336
337 #include <obd_support.h>
338
339 #ifdef __KERNEL__
340 /* function defined in lustre/obdclass/<platform>/<platform>-module.c */
341 int obd_ioctl_getdata(char **buf, int *len, void *arg);
342 int obd_ioctl_popdata(void *arg, void *data, int len);
343 #else
344 /* buffer MUST be at least the size of obd_ioctl_hdr */
345 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
346 {
347         struct obd_ioctl_hdr hdr;
348         struct obd_ioctl_data *data;
349         int err;
350         int offset = 0;
351         ENTRY;
352
353         err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
354         if (err)
355                 RETURN(err);
356
357         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
358                 CERROR("Version mismatch kernel vs application\n");
359                 RETURN(-EINVAL);
360         }
361
362         if (hdr.ioc_len > OBD_MAX_IOCTL_BUFFER) {
363                 CERROR("User buffer len %d exceeds %d max buffer\n",
364                        hdr.ioc_len, OBD_MAX_IOCTL_BUFFER);
365                 RETURN(-EINVAL);
366         }
367
368         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
369                 CERROR("User buffer too small for ioctl (%d)\n", hdr.ioc_len);
370                 RETURN(-EINVAL);
371         }
372
373         OBD_ALLOC_LARGE(*buf, hdr.ioc_len);
374         if (*buf == NULL) {
375                 CERROR("Cannot allocate control buffer of len %d\n",
376                        hdr.ioc_len);
377                 RETURN(-EINVAL);
378         }
379         *len = hdr.ioc_len;
380         data = (struct obd_ioctl_data *)*buf;
381
382         err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
383         if (err) {
384                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
385                 RETURN(err);
386         }
387
388         if (obd_ioctl_is_invalid(data)) {
389                 CERROR("ioctl not correctly formatted\n");
390                 OBD_FREE_LARGE(*buf, hdr.ioc_len);
391                 RETURN(-EINVAL);
392         }
393
394         if (data->ioc_inllen1) {
395                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
396                 offset += cfs_size_round(data->ioc_inllen1);
397         }
398
399         if (data->ioc_inllen2) {
400                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + offset;
401                 offset += cfs_size_round(data->ioc_inllen2);
402         }
403
404         if (data->ioc_inllen3) {
405                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + offset;
406                 offset += cfs_size_round(data->ioc_inllen3);
407         }
408
409         if (data->ioc_inllen4) {
410                 data->ioc_inlbuf4 = &data->ioc_bulk[0] + offset;
411         }
412
413         RETURN(0);
414 }
415
416 static inline int obd_ioctl_popdata(void *arg, void *data, int len)
417 {
418         int err = copy_to_user(arg, data, len);
419         if (err)
420                 err = -EFAULT;
421         return err;
422 }
423 #endif
424
425 static inline void obd_ioctl_freedata(char *buf, int len)
426 {
427         ENTRY;
428
429         OBD_FREE_LARGE(buf, len);
430         EXIT;
431         return;
432 }
433
434 /*
435  * BSD ioctl description:
436  * #define IOC_V1       _IOR(g, n1, long)
437  * #define IOC_V2       _IOW(g, n2, long)
438  *
439  * ioctl(f, IOC_V1, arg);
440  * arg will be treated as a long value,
441  *
442  * ioctl(f, IOC_V2, arg)
443  * arg will be treated as a pointer, bsd will call
444  * copyin(buf, arg, sizeof(long))
445  *
446  * To make BSD ioctl handles argument correctly and simplely,
447  * we change _IOR to _IOWR so BSD will copyin obd_ioctl_data
448  * for us. Does this change affect Linux?  (XXX Liang)
449  */
450 #define OBD_IOC_CREATE                 _IOWR('f', 101, OBD_IOC_DATA_TYPE)
451 #define OBD_IOC_DESTROY                _IOW ('f', 104, OBD_IOC_DATA_TYPE)
452 #define OBD_IOC_PREALLOCATE            _IOWR('f', 105, OBD_IOC_DATA_TYPE)
453
454 #define OBD_IOC_SETATTR                _IOW ('f', 107, OBD_IOC_DATA_TYPE)
455 #define OBD_IOC_GETATTR                _IOWR ('f', 108, OBD_IOC_DATA_TYPE)
456 #define OBD_IOC_READ                   _IOWR('f', 109, OBD_IOC_DATA_TYPE)
457 #define OBD_IOC_WRITE                  _IOWR('f', 110, OBD_IOC_DATA_TYPE)
458
459
460 #define OBD_IOC_STATFS                 _IOWR('f', 113, OBD_IOC_DATA_TYPE)
461 #define OBD_IOC_SYNC                   _IOW ('f', 114, OBD_IOC_DATA_TYPE)
462 #define OBD_IOC_READ2                  _IOWR('f', 115, OBD_IOC_DATA_TYPE)
463 #define OBD_IOC_FORMAT                 _IOWR('f', 116, OBD_IOC_DATA_TYPE)
464 #define OBD_IOC_PARTITION              _IOWR('f', 117, OBD_IOC_DATA_TYPE)
465 #define OBD_IOC_COPY                   _IOWR('f', 120, OBD_IOC_DATA_TYPE)
466 #define OBD_IOC_MIGR                   _IOWR('f', 121, OBD_IOC_DATA_TYPE)
467 #define OBD_IOC_PUNCH                  _IOWR('f', 122, OBD_IOC_DATA_TYPE)
468
469 #define OBD_IOC_MODULE_DEBUG           _IOWR('f', 124, OBD_IOC_DATA_TYPE)
470 #define OBD_IOC_BRW_READ               _IOWR('f', 125, OBD_IOC_DATA_TYPE)
471 #define OBD_IOC_BRW_WRITE              _IOWR('f', 126, OBD_IOC_DATA_TYPE)
472 #define OBD_IOC_NAME2DEV               _IOWR('f', 127, OBD_IOC_DATA_TYPE)
473 #define OBD_IOC_UUID2DEV               _IOWR('f', 130, OBD_IOC_DATA_TYPE)
474
475 #define OBD_IOC_GETNAME                _IOWR('f', 131, OBD_IOC_DATA_TYPE)
476 #define OBD_IOC_GETMDNAME              _IOR('f', 131, char[MAX_OBD_NAME])
477 #define OBD_IOC_GETDTNAME               OBD_IOC_GETNAME
478
479 #define OBD_IOC_LOV_GET_CONFIG         _IOWR('f', 132, OBD_IOC_DATA_TYPE)
480 #define OBD_IOC_CLIENT_RECOVER         _IOW ('f', 133, OBD_IOC_DATA_TYPE)
481 #define OBD_IOC_PING_TARGET            _IOW ('f', 136, OBD_IOC_DATA_TYPE)
482
483 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 139      )
484 #define OBD_IOC_NO_TRANSNO             _IOW ('f', 140, OBD_IOC_DATA_TYPE)
485 #define OBD_IOC_SET_READONLY           _IOW ('f', 141, OBD_IOC_DATA_TYPE)
486 #define OBD_IOC_ABORT_RECOVERY         _IOR ('f', 142, OBD_IOC_DATA_TYPE)
487
488 #define OBD_IOC_ROOT_SQUASH            _IOWR('f', 143, OBD_IOC_DATA_TYPE)
489
490 #define OBD_GET_VERSION                _IOWR ('f', 144, OBD_IOC_DATA_TYPE)
491
492 #define OBD_IOC_GSS_SUPPORT            _IOWR('f', 145, OBD_IOC_DATA_TYPE)
493
494 #define OBD_IOC_CLOSE_UUID             _IOWR ('f', 147, OBD_IOC_DATA_TYPE)
495
496 #define OBD_IOC_CHANGELOG_SEND         _IOW ('f', 148, OBD_IOC_DATA_TYPE)
497 #define OBD_IOC_GETDEVICE              _IOWR ('f', 149, OBD_IOC_DATA_TYPE)
498 #define OBD_IOC_FID2PATH               _IOWR ('f', 150, OBD_IOC_DATA_TYPE)
499 /* see also <lustre/lustre_user.h> for ioctls 151-153 */
500 /* OBD_IOC_LOV_SETSTRIPE: See also LL_IOC_LOV_SETSTRIPE */
501 #define OBD_IOC_LOV_SETSTRIPE          _IOW ('f', 154, OBD_IOC_DATA_TYPE)
502 /* OBD_IOC_LOV_GETSTRIPE: See also LL_IOC_LOV_GETSTRIPE */
503 #define OBD_IOC_LOV_GETSTRIPE          _IOW ('f', 155, OBD_IOC_DATA_TYPE)
504 /* OBD_IOC_LOV_SETEA: See also LL_IOC_LOV_SETEA */
505 #define OBD_IOC_LOV_SETEA              _IOW ('f', 156, OBD_IOC_DATA_TYPE)
506 /* see <lustre/lustre_user.h> for ioctls 157-159 */
507 /* OBD_IOC_QUOTACHECK: See also LL_IOC_QUOTACHECK */
508 #define OBD_IOC_QUOTACHECK             _IOW ('f', 160, int)
509 /* OBD_IOC_POLL_QUOTACHECK: See also LL_IOC_POLL_QUOTACHECK */
510 #define OBD_IOC_POLL_QUOTACHECK        _IOR ('f', 161, struct if_quotacheck *)
511 /* OBD_IOC_QUOTACTL: See also LL_IOC_QUOTACTL */
512 #define OBD_IOC_QUOTACTL               _IOWR('f', 162, struct if_quotactl)
513 /* see  also <lustre/lustre_user.h> for ioctls 163-176 */
514 #define OBD_IOC_CHANGELOG_REG          _IOW ('f', 177, struct obd_ioctl_data)
515 #define OBD_IOC_CHANGELOG_DEREG        _IOW ('f', 178, struct obd_ioctl_data)
516 #define OBD_IOC_CHANGELOG_CLEAR        _IOW ('f', 179, struct obd_ioctl_data)
517 #define OBD_IOC_RECORD                 _IOWR('f', 180, OBD_IOC_DATA_TYPE)
518 #define OBD_IOC_ENDRECORD              _IOWR('f', 181, OBD_IOC_DATA_TYPE)
519 #define OBD_IOC_PARSE                  _IOWR('f', 182, OBD_IOC_DATA_TYPE)
520 #define OBD_IOC_DORECORD               _IOWR('f', 183, OBD_IOC_DATA_TYPE)
521 #define OBD_IOC_PROCESS_CFG            _IOWR('f', 184, OBD_IOC_DATA_TYPE)
522 #define OBD_IOC_DUMP_LOG               _IOWR('f', 185, OBD_IOC_DATA_TYPE)
523 #define OBD_IOC_CLEAR_LOG              _IOWR('f', 186, OBD_IOC_DATA_TYPE)
524 #define OBD_IOC_PARAM                  _IOW ('f', 187, OBD_IOC_DATA_TYPE)
525 #define OBD_IOC_POOL                   _IOWR('f', 188, OBD_IOC_DATA_TYPE)
526 #define OBD_IOC_REPLACE_NIDS           _IOWR('f', 189, OBD_IOC_DATA_TYPE)
527
528 #define OBD_IOC_CATLOGLIST             _IOWR('f', 190, OBD_IOC_DATA_TYPE)
529 #define OBD_IOC_LLOG_INFO              _IOWR('f', 191, OBD_IOC_DATA_TYPE)
530 #define OBD_IOC_LLOG_PRINT             _IOWR('f', 192, OBD_IOC_DATA_TYPE)
531 #define OBD_IOC_LLOG_CANCEL            _IOWR('f', 193, OBD_IOC_DATA_TYPE)
532 #define OBD_IOC_LLOG_REMOVE            _IOWR('f', 194, OBD_IOC_DATA_TYPE)
533 #define OBD_IOC_LLOG_CHECK             _IOWR('f', 195, OBD_IOC_DATA_TYPE)
534 /* OBD_IOC_LLOG_CATINFO is deprecated */
535 #define OBD_IOC_LLOG_CATINFO           _IOWR('f', 196, OBD_IOC_DATA_TYPE)
536 #define OBD_IOC_NODEMAP                 _IOWR('f', 197, OBD_IOC_DATA_TYPE)
537
538 #define ECHO_IOC_GET_STRIPE            _IOWR('f', 200, OBD_IOC_DATA_TYPE)
539 #define ECHO_IOC_SET_STRIPE            _IOWR('f', 201, OBD_IOC_DATA_TYPE)
540 #define ECHO_IOC_ENQUEUE               _IOWR('f', 202, OBD_IOC_DATA_TYPE)
541 #define ECHO_IOC_CANCEL                _IOWR('f', 203, OBD_IOC_DATA_TYPE)
542
543 #define OBD_IOC_GET_OBJ_VERSION        _IOR('f', 210, OBD_IOC_DATA_TYPE)
544
545 /* <lustre/lustre_user.h> defines ioctl number 218-219 */
546 #define OBD_IOC_GET_MNTOPT             _IOW('f', 220, mntopt_t)
547 #define OBD_IOC_ECHO_MD                _IOR('f', 221, struct obd_ioctl_data)
548 #define OBD_IOC_ECHO_ALLOC_SEQ         _IOWR('f', 222, struct obd_ioctl_data)
549 #define OBD_IOC_START_LFSCK            _IOWR('f', 230, OBD_IOC_DATA_TYPE)
550 #define OBD_IOC_STOP_LFSCK             _IOW('f', 231, OBD_IOC_DATA_TYPE)
551
552 /* XXX _IOWR('f', 250, long) has been defined in
553  * libcfs/include/libcfs/libcfs_private.h for debug, don't use it
554  */
555
556 /* Until such time as we get_info the per-stripe maximum from the OST,
557  * we define this to be 2T - 4k, which is the ext3 maxbytes. */
558 #define LUSTRE_STRIPE_MAXBYTES 0x1fffffff000ULL
559
560 /* Special values for remove LOV EA from disk */
561 #define LOVEA_DELETE_VALUES(size, count, offset) (size == 0 && count == 0 && \
562                                                  offset == (typeof(offset))(-1))
563
564 #define LMVEA_DELETE_VALUES(count, offset) ((count) == 0 && \
565                                             (offset) == (typeof(offset))(-1))
566 /* #define POISON_BULK 0 */
567
568 /*
569  * l_wait_event is a flexible sleeping function, permitting simple caller
570  * configuration of interrupt and timeout sensitivity along with actions to
571  * be performed in the event of either exception.
572  *
573  * The first form of usage looks like this:
574  *
575  * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
576  *                                           intr_handler, callback_data);
577  * rc = l_wait_event(waitq, condition, &lwi);
578  *
579  * l_wait_event() makes the current process wait on 'waitq' until 'condition'
580  * is TRUE or a "killable" signal (SIGTERM, SIKGILL, SIGINT) is pending.  It
581  * returns 0 to signify 'condition' is TRUE, but if a signal wakes it before
582  * 'condition' becomes true, it optionally calls the specified 'intr_handler'
583  * if not NULL, and returns -EINTR.
584  *
585  * If a non-zero timeout is specified, signals are ignored until the timeout
586  * has expired.  At this time, if 'timeout_handler' is not NULL it is called.
587  * If it returns FALSE l_wait_event() continues to wait as described above with
588  * signals enabled.  Otherwise it returns -ETIMEDOUT.
589  *
590  * LWI_INTR(intr_handler, callback_data) is shorthand for
591  * LWI_TIMEOUT_INTR(0, NULL, intr_handler, callback_data)
592  *
593  * The second form of usage looks like this:
594  *
595  * struct l_wait_info lwi = LWI_TIMEOUT(timeout, timeout_handler);
596  * rc = l_wait_event(waitq, condition, &lwi);
597  *
598  * This form is the same as the first except that it COMPLETELY IGNORES
599  * SIGNALS.  The caller must therefore beware that if 'timeout' is zero, or if
600  * 'timeout_handler' is not NULL and returns FALSE, then the ONLY thing that
601  * can unblock the current process is 'condition' becoming TRUE.
602  *
603  * Another form of usage is:
604  * struct l_wait_info lwi = LWI_TIMEOUT_INTERVAL(timeout, interval,
605  *                                               timeout_handler);
606  * rc = l_wait_event(waitq, condition, &lwi);
607  * This is the same as previous case, but condition is checked once every
608  * 'interval' jiffies (if non-zero).
609  *
610  * Subtle synchronization point: this macro does *not* necessary takes
611  * wait-queue spin-lock before returning, and, hence, following idiom is safe
612  * ONLY when caller provides some external locking:
613  *
614  *             Thread1                            Thread2
615  *
616  *   l_wait_event(&obj->wq, ....);                                       (1)
617  *
618  *                                    wake_up(&obj->wq):                 (2)
619  *                                         spin_lock(&q->lock);          (2.1)
620  *                                         __wake_up_common(q, ...);     (2.2)
621  *                                         spin_unlock(&q->lock, flags); (2.3)
622  *
623  *   OBD_FREE_PTR(obj);                                                  (3)
624  *
625  * As l_wait_event() may "short-cut" execution and return without taking
626  * wait-queue spin-lock, some additional synchronization is necessary to
627  * guarantee that step (3) can begin only after (2.3) finishes.
628  *
629  * XXX nikita: some ptlrpc daemon threads have races of that sort.
630  *
631  */
632 static inline int back_to_sleep(void *arg)
633 {
634         return 0;
635 }
636
637 #define LWI_ON_SIGNAL_NOOP ((void (*)(void *))(-1))
638
639 struct l_wait_info {
640         cfs_duration_t lwi_timeout;
641         cfs_duration_t lwi_interval;
642         int            lwi_allow_intr;
643         int  (*lwi_on_timeout)(void *);
644         void (*lwi_on_signal)(void *);
645         void  *lwi_cb_data;
646 };
647
648 /* NB: LWI_TIMEOUT ignores signals completely */
649 #define LWI_TIMEOUT(time, cb, data)             \
650 ((struct l_wait_info) {                         \
651         .lwi_timeout    = time,                 \
652         .lwi_on_timeout = cb,                   \
653         .lwi_cb_data    = data,                 \
654         .lwi_interval   = 0,                    \
655         .lwi_allow_intr = 0                     \
656 })
657
658 #define LWI_TIMEOUT_INTERVAL(time, interval, cb, data)  \
659 ((struct l_wait_info) {                                 \
660         .lwi_timeout    = time,                         \
661         .lwi_on_timeout = cb,                           \
662         .lwi_cb_data    = data,                         \
663         .lwi_interval   = interval,                     \
664         .lwi_allow_intr = 0                             \
665 })
666
667 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)   \
668 ((struct l_wait_info) {                                 \
669         .lwi_timeout    = time,                         \
670         .lwi_on_timeout = time_cb,                      \
671         .lwi_on_signal  = sig_cb,                       \
672         .lwi_cb_data    = data,                         \
673         .lwi_interval   = 0,                            \
674         .lwi_allow_intr = 0                             \
675 })
676
677 #define LWI_TIMEOUT_INTR_ALL(time, time_cb, sig_cb, data)       \
678 ((struct l_wait_info) {                                         \
679         .lwi_timeout    = time,                                 \
680         .lwi_on_timeout = time_cb,                              \
681         .lwi_on_signal  = sig_cb,                               \
682         .lwi_cb_data    = data,                                 \
683         .lwi_interval   = 0,                                    \
684         .lwi_allow_intr = 1                                     \
685 })
686
687 #define LWI_INTR(cb, data)  LWI_TIMEOUT_INTR(0, NULL, cb, data)
688
689 #ifdef __KERNEL__
690
691 /*
692  * wait for @condition to become true, but no longer than timeout, specified
693  * by @info.
694  */
695 #define __l_wait_event(wq, condition, info, ret, l_add_wait)                   \
696 do {                                                                           \
697         wait_queue_t __wait;                                                   \
698         cfs_duration_t __timeout = info->lwi_timeout;                          \
699         sigset_t   __blocked;                                              \
700         int   __allow_intr = info->lwi_allow_intr;                             \
701                                                                                \
702         ret = 0;                                                               \
703         if (condition)                                                         \
704                 break;                                                         \
705                                                                                \
706         init_waitqueue_entry_current(&__wait);                                 \
707         l_add_wait(&wq, &__wait);                                              \
708                                                                                \
709         /* Block all signals (just the non-fatal ones if no timeout). */       \
710         if (info->lwi_on_signal != NULL && (__timeout == 0 || __allow_intr))   \
711                 __blocked = cfs_block_sigsinv(LUSTRE_FATAL_SIGS);              \
712         else                                                                   \
713                 __blocked = cfs_block_sigsinv(0);                              \
714                                                                                \
715         for (;;) {                                                             \
716                 unsigned       __wstate;                                       \
717                                                                                \
718                 __wstate = info->lwi_on_signal != NULL &&                      \
719                            (__timeout == 0 || __allow_intr) ?                  \
720                         TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE;             \
721                                                                                \
722                 set_current_state(TASK_INTERRUPTIBLE);                         \
723                                                                                \
724                 if (condition)                                                 \
725                         break;                                                 \
726                                                                                \
727                 if (__timeout == 0) {                                          \
728                         waitq_wait(&__wait, __wstate);                         \
729                 } else {                                                       \
730                         cfs_duration_t interval = info->lwi_interval?          \
731                                              min_t(cfs_duration_t,             \
732                                                  info->lwi_interval,__timeout):\
733                                              __timeout;                        \
734                         cfs_duration_t remaining = waitq_timedwait(&__wait,    \
735                                                    __wstate,                   \
736                                                    interval);                  \
737                         __timeout = cfs_time_sub(__timeout,                    \
738                                             cfs_time_sub(interval, remaining));\
739                         if (__timeout == 0) {                                  \
740                                 if (info->lwi_on_timeout == NULL ||            \
741                                     info->lwi_on_timeout(info->lwi_cb_data)) { \
742                                         ret = -ETIMEDOUT;                      \
743                                         break;                                 \
744                                 }                                              \
745                                 /* Take signals after the timeout expires. */  \
746                                 if (info->lwi_on_signal != NULL)               \
747                                     (void)cfs_block_sigsinv(LUSTRE_FATAL_SIGS);\
748                         }                                                      \
749                 }                                                              \
750                                                                                \
751                 if (condition)                                                 \
752                         break;                                                 \
753                 if (cfs_signal_pending()) {                                    \
754                         if (info->lwi_on_signal != NULL &&                     \
755                             (__timeout == 0 || __allow_intr)) {                \
756                                 if (info->lwi_on_signal != LWI_ON_SIGNAL_NOOP) \
757                                         info->lwi_on_signal(info->lwi_cb_data);\
758                                 ret = -EINTR;                                  \
759                                 break;                                         \
760                         }                                                      \
761                         /* We have to do this here because some signals */     \
762                         /* are not blockable - ie from strace(1).       */     \
763                         /* In these cases we want to schedule_timeout() */     \
764                         /* again, because we don't want that to return  */     \
765                         /* -EINTR when the RPC actually succeeded.      */     \
766                         /* the recalc_sigpending() below will deliver the */   \
767                         /* signal properly.                             */     \
768                         cfs_clear_sigpending();                                \
769                 }                                                              \
770         }                                                                      \
771                                                                                \
772         cfs_restore_sigs(__blocked);                                           \
773                                                                                \
774         set_current_state(TASK_RUNNING);                                       \
775         remove_wait_queue(&wq, &__wait);                                       \
776 } while (0)
777
778 #else /* !__KERNEL__ */
779
780 #define __l_wait_event(wq, condition, info, ret, l_add_wait)            \
781 do {                                                                    \
782         long __timeout = info->lwi_timeout;                             \
783         long __now;                                                     \
784         long __then = 0;                                                \
785         int  __timed_out = 0;                                           \
786         int  __interval = obd_timeout;                                  \
787                                                                         \
788         ret = 0;                                                        \
789         if (condition)                                                  \
790                 break;                                                  \
791                                                                         \
792         if (__timeout != 0)                                             \
793                 __then = time(NULL);                                    \
794                                                                         \
795         if (__timeout && __timeout < __interval)                        \
796                 __interval = __timeout;                                 \
797         if (info->lwi_interval && info->lwi_interval < __interval)      \
798                 __interval = info->lwi_interval;                        \
799                                                                         \
800         while (!(condition)) {                                          \
801                 liblustre_wait_event(__interval);                       \
802                 if (condition)                                          \
803                         break;                                          \
804                                                                         \
805                 if (!__timed_out && info->lwi_timeout != 0) {           \
806                         __now = time(NULL);                             \
807                         __timeout -= __now - __then;                    \
808                         __then = __now;                                 \
809                                                                         \
810                         if (__timeout > 0)                              \
811                                 continue;                               \
812                                                                         \
813                         __timeout = 0;                                  \
814                         __timed_out = 1;                                \
815                         if (info->lwi_on_timeout == NULL ||             \
816                             info->lwi_on_timeout(info->lwi_cb_data)) {  \
817                                 ret = -ETIMEDOUT;                       \
818                                 break;                                  \
819                         }                                               \
820                 }                                                       \
821         }                                                               \
822 } while (0)
823
824 #endif /* __KERNEL__ */
825
826
827 #define l_wait_event(wq, condition, info)                       \
828 ({                                                              \
829         int                 __ret;                              \
830         struct l_wait_info *__info = (info);                    \
831                                                                 \
832         __l_wait_event(wq, condition, __info,                   \
833                        __ret, add_wait_queue);                  \
834         __ret;                                                  \
835 })
836
837 #define l_wait_event_exclusive(wq, condition, info)             \
838 ({                                                              \
839         int                 __ret;                              \
840         struct l_wait_info *__info = (info);                    \
841                                                                 \
842         __l_wait_event(wq, condition, __info,                   \
843                        __ret, add_wait_queue_exclusive);        \
844         __ret;                                                  \
845 })
846
847 #define l_wait_event_exclusive_head(wq, condition, info)        \
848 ({                                                              \
849         int                 __ret;                              \
850         struct l_wait_info *__info = (info);                    \
851                                                                 \
852         __l_wait_event(wq, condition, __info,                   \
853                        __ret, add_wait_queue_exclusive_head);   \
854         __ret;                                                  \
855 })
856
857 #define l_wait_condition(wq, condition)                         \
858 ({                                                              \
859         struct l_wait_info lwi = { 0 };                         \
860         l_wait_event(wq, condition, &lwi);                      \
861 })
862
863 #define l_wait_condition_exclusive(wq, condition)               \
864 ({                                                              \
865         struct l_wait_info lwi = { 0 };                         \
866         l_wait_event_exclusive(wq, condition, &lwi);            \
867 })
868
869 #define l_wait_condition_exclusive_head(wq, condition)          \
870 ({                                                              \
871         struct l_wait_info lwi = { 0 };                         \
872         l_wait_event_exclusive_head(wq, condition, &lwi);       \
873 })
874
875 #ifdef __KERNEL__
876 #define LIBLUSTRE_CLIENT (0)
877 #else
878 #define LIBLUSTRE_CLIENT (1)
879 #endif
880
881 /** @} lib */
882
883 #endif /* _LUSTRE_LIB_H */