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