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