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