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