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