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