Whamcloud - gitweb
Merge b_md to HEAD for 0.5.19 release.
[fs/lustre-release.git] / lustre / include / linux / lustre_lib.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * Basic Lustre library routines. 
22  *
23  */
24
25 #ifndef _LUSTRE_LIB_H
26 #define _LUSTRE_LIB_H
27
28 #ifndef __KERNEL__
29 # include <string.h>
30 #else
31 # include <asm/semaphore.h>
32 #endif
33 #include <linux/types.h>
34 #include <linux/portals_lib.h>
35 #include <linux/kp30.h> /* XXX just for LASSERT! */
36 #include <linux/lustre_idl.h>
37
38 #ifndef LPU64
39 #if BITS_PER_LONG > 32
40 #define LPU64 "%lu"
41 #define LPD64 "%ld"
42 #define LPX64 "%#lx"
43 #else
44 #define LPU64 "%Lu"
45 #define LPD64 "%Ld"
46 #define LPX64 "%#Lx"
47 #endif
48 #endif
49
50 #ifdef __KERNEL__
51 /* l_net.c */
52 struct ptlrpc_request;
53 struct obd_device;
54 struct recovd_data;
55 struct recovd_obd;
56 struct obd_export;
57 #include <linux/lustre_ha.h>
58
59 int target_handle_connect(struct ptlrpc_request *req);
60 int target_handle_disconnect(struct ptlrpc_request *req);
61 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
62                             char *cluuid);
63 int client_obd_connect(struct lustre_handle *conn, struct obd_device *obd,
64                        obd_uuid_t cluuid, struct recovd_obd *recovd,
65                        ptlrpc_recovery_cb_t recover);
66 int client_obd_disconnect(struct lustre_handle *conn);
67 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf);
68 int client_obd_cleanup(struct obd_device * obddev);
69 struct client_obd *client_conn2cli(struct lustre_handle *conn); 
70 struct obd_device *client_tgtuuid2obd(char *tgtuuid);
71
72 int target_revoke_connection(struct recovd_data *rd, int phase);
73
74 /* l_lock.c */
75 struct lustre_lock { 
76         int l_depth;
77         struct task_struct *l_owner;
78         struct semaphore l_sem;
79         spinlock_t l_spin;
80 };
81
82 void l_lock_init(struct lustre_lock *);
83 void l_lock(struct lustre_lock *);
84 void l_unlock(struct lustre_lock *);
85 int l_has_lock(struct lustre_lock *);
86
87 #define CB_PHASE_START   12
88 #define CB_PHASE_FINISH  13
89
90 /* This list head doesn't need to be locked, because it's only manipulated by
91  * one thread at a time. */
92 struct obd_brw_set {
93         struct list_head brw_desc_head; /* list of ptlrpc_bulk_desc */
94         wait_queue_head_t brw_waitq;
95         atomic_t brw_refcount;
96         int brw_flags;
97
98         int (*brw_callback)(struct obd_brw_set *, int phase);
99 };
100
101 /* simple.c */
102 struct obd_run_ctxt;
103 struct obd_ucred;
104 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new_ctx,
105                struct obd_ucred *cred);
106 void pop_ctxt(struct obd_run_ctxt *saved, struct obd_run_ctxt *new_ctx,
107               struct obd_ucred *cred);
108 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode);
109 struct dentry *simple_mknod(struct dentry *dir, char *name, int mode);
110 int lustre_fread(struct file *file, char *str, int len, loff_t *off);
111 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off);
112 int lustre_fsync(struct file *file);
113
114 static inline void l_dput(struct dentry *de)
115 {
116         if (!de || IS_ERR(de))
117                 return;
118         shrink_dcache_parent(de);
119         LASSERT(atomic_read(&de->d_count) > 0);
120         dput(de);
121 }
122
123 static inline void ll_sleep(int t)
124 {
125         set_current_state(TASK_INTERRUPTIBLE);
126         schedule_timeout(t * HZ);
127         set_current_state(TASK_RUNNING);
128 }
129 #endif
130
131 /* FIXME: This needs to validate pointers and cookies */
132 static inline void *lustre_handle2object(struct lustre_handle *handle)
133 {
134         if (handle) 
135                 return (void *)(unsigned long)(handle->addr);
136         return NULL; 
137 }
138
139 static inline void ldlm_object2handle(void *object, struct lustre_handle *handle)
140 {
141         handle->addr = (__u64)(unsigned long)object;
142 }
143
144 #include <linux/portals_lib.h>
145
146 /*
147  *   OBD IOCTLS
148  */
149 #define OBD_IOCTL_VERSION 0x00010001
150
151 struct obd_ioctl_data {
152         uint32_t ioc_len;
153         uint32_t ioc_version;
154
155         uint64_t ioc_addr;
156         uint64_t ioc_cookie;
157         uint32_t ioc_conn1;
158         uint32_t ioc_conn2;
159
160         struct obdo ioc_obdo1;
161         struct obdo ioc_obdo2;
162
163         obd_size         ioc_count;
164         obd_off          ioc_offset;
165         uint32_t         ioc_dev;
166         uint32_t         ____padding;
167
168         /* buffers the kernel will treat as user pointers */
169         uint32_t ioc_plen1;
170         char    *ioc_pbuf1;
171         uint32_t ioc_plen2;
172         char    *ioc_pbuf2;
173
174         /* two inline buffers */
175         uint32_t ioc_inllen1;
176         char    *ioc_inlbuf1;
177         uint32_t ioc_inllen2;
178         char    *ioc_inlbuf2;
179         uint32_t ioc_inllen3;
180         char    *ioc_inlbuf3;
181
182         char    ioc_bulk[0];
183 };
184
185 struct obd_ioctl_hdr {
186         uint32_t ioc_len;
187         uint32_t ioc_version;
188 };
189
190 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
191 {
192         int len = size_round(sizeof(struct obd_ioctl_data));
193         len += size_round(data->ioc_inllen1);
194         len += size_round(data->ioc_inllen2);
195         len += size_round(data->ioc_inllen3);
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                 printk("OBD ioctl: ioc_len larger than 1<<30\n");
204                 return 1;
205         }
206         if (data->ioc_inllen1 > (1<<30)) {
207                 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
208                 return 1;
209         }
210         if (data->ioc_inllen2 > (1<<30)) {
211                 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
212                 return 1;
213         }
214
215         if (data->ioc_inllen3 > (1<<30)) {
216                 printk("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
217                 return 1;
218         }
219         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
220                 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
221                 return 1;
222         }
223         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
224                 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
225                 return 1;
226         }
227         if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
228                 printk("OBD ioctl: inlbuf3 pointer but 0 length\n");
229                 return 1;
230         }
231         if (data->ioc_pbuf1 && !data->ioc_plen1) {
232                 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
233                 return 1;
234         }
235         if (data->ioc_pbuf2 && !data->ioc_plen2) {
236                 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
237                 return 1;
238         }
239         /*
240         if (data->ioc_inllen1 && !data->ioc_inlbuf1) {
241                 printk("OBD ioctl: inllen1 set but NULL pointer\n");
242                 return 1;
243         }
244         if (data->ioc_inllen2 && !data->ioc_inlbuf2) {
245                 printk("OBD ioctl: inllen2 set but NULL pointer\n");
246                 return 1;
247         }
248         if (data->ioc_inllen3 && !data->ioc_inlbuf3) {
249                 printk("OBD ioctl: inllen3 set but NULL pointer\n");
250                 return 1;
251         }
252         */
253         if (data->ioc_plen1 && !data->ioc_pbuf1) {
254                 printk("OBD ioctl: plen1 set but NULL pointer\n");
255                 return 1;
256         }
257         if (data->ioc_plen2 && !data->ioc_pbuf2) {
258                 printk("OBD ioctl: plen2 set but NULL pointer\n");
259                 return 1;
260         }
261         if (obd_ioctl_packlen(data) != data->ioc_len ) {
262                 printk("OBD ioctl: packlen exceeds ioc_len\n");
263                 return 1;
264         }
265 #if 0
266         if (data->ioc_inllen1 &&
267             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
268                 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
269                 return 1;
270         }
271         if (data->ioc_inllen2 &&
272             data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') {
273                 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
274                 return 1;
275         }
276         if (data->ioc_inllen3 &&
277             data->ioc_bulk[size_round(data->ioc_inllen1) + size_round(data->ioc_inllen2)
278                            + data->ioc_inllen3 - 1] != '\0') {
279                 printk("OBD ioctl: inlbuf3 not 0 terminated\n");
280                 return 1;
281         }
282 #endif 
283         return 0;
284 }
285
286 #ifndef __KERNEL__
287 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
288                                  int max)
289 {
290         char *ptr;
291         struct obd_ioctl_data *overlay;
292         data->ioc_len = obd_ioctl_packlen(data);
293         data->ioc_version = OBD_IOCTL_VERSION;
294
295         if (*pbuf && data->ioc_len > max)
296                 return 1;
297         if (*pbuf == NULL) {
298                 *pbuf = malloc(data->ioc_len);
299         }
300         if (!*pbuf)
301                 return 1;
302         overlay = (struct obd_ioctl_data *)*pbuf;
303         memcpy(*pbuf, data, sizeof(*data));
304
305         ptr = overlay->ioc_bulk;
306         if (data->ioc_inlbuf1)
307                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
308         if (data->ioc_inlbuf2)
309                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
310         if (data->ioc_inlbuf3)
311                 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
312         if (obd_ioctl_is_invalid(overlay))
313                 return 1;
314
315         return 0;
316 }
317
318 static inline int obd_ioctl_unpack(struct obd_ioctl_data *data, char *pbuf,
319                                    int max)
320 {
321         char *ptr;
322         struct obd_ioctl_data *overlay;
323
324         if (!pbuf)
325                 return 1;
326         overlay = (struct obd_ioctl_data *)pbuf;
327
328         /* Preserve the caller's buffer pointers */
329         overlay->ioc_inlbuf1 = data->ioc_inlbuf1;
330         overlay->ioc_inlbuf2 = data->ioc_inlbuf2;
331         overlay->ioc_inlbuf3 = data->ioc_inlbuf3;
332
333         memcpy(data, pbuf, sizeof(*data));
334
335         ptr = overlay->ioc_bulk;
336         if (data->ioc_inlbuf1)
337                 LOGU(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
338         if (data->ioc_inlbuf2)
339                 LOGU(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
340         if (data->ioc_inlbuf3)
341                 LOGU(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
342
343         return 0;
344 }
345 #else
346
347 #include <linux/obd_support.h>
348
349 /* buffer MUST be at least the size of obd_ioctl_hdr */
350 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
351 {
352         struct obd_ioctl_hdr hdr;
353         struct obd_ioctl_data *data;
354         int err;
355         ENTRY;
356
357         err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
358         if ( err ) {
359                 EXIT;
360                 return err;
361         }
362
363         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
364                 printk("OBD: version mismatch kernel vs application\n");
365                 return -EINVAL;
366         }
367
368         if (hdr.ioc_len > 8192) {
369                 printk("OBD: user buffer exceeds 8192 max buffer\n");
370                 return -EINVAL;
371         }
372
373         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
374                 printk("OBD: user buffer too small for ioctl\n");
375                 return -EINVAL;
376         }
377
378         OBD_ALLOC(*buf, hdr.ioc_len);
379         if (!*buf) {
380                 CERROR("Cannot allocate control buffer of len %d\n",
381                        hdr.ioc_len);
382                 RETURN(-EINVAL);
383         }
384         *len = hdr.ioc_len;
385         data = (struct obd_ioctl_data *)*buf;
386
387         err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
388         if ( err ) {
389                 EXIT;
390                 return err;
391         }
392
393         if (obd_ioctl_is_invalid(data)) {
394                 printk("OBD: ioctl not correctly formatted\n");
395                 return -EINVAL;
396         }
397
398         if (data->ioc_inllen1) {
399                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
400         }
401
402         if (data->ioc_inllen2) {
403                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
404                         size_round(data->ioc_inllen1);
405         }
406
407         if (data->ioc_inllen3) {
408                 data->ioc_inlbuf3 = &data->ioc_bulk[0] +
409                         size_round(data->ioc_inllen1) +
410                         size_round(data->ioc_inllen2);
411         }
412
413         EXIT;
414         return 0;
415 }
416 #endif
417
418 #define OBD_IOC_CREATE                 _IOR ('f', 101, long)
419 #define OBD_IOC_SETUP                  _IOW ('f', 102, long)
420 #define OBD_IOC_CLEANUP                _IO  ('f', 103      )
421 #define OBD_IOC_DESTROY                _IOW ('f', 104, long)
422 #define OBD_IOC_PREALLOCATE            _IOWR('f', 105, long)
423
424 #define OBD_IOC_SETATTR                _IOW ('f', 107, long)
425 #define OBD_IOC_GETATTR                _IOR ('f', 108, long)
426 #define OBD_IOC_READ                   _IOWR('f', 109, long)
427 #define OBD_IOC_WRITE                  _IOWR('f', 110, long)
428 #define OBD_IOC_CONNECT                _IOR ('f', 111, long)
429 #define OBD_IOC_DISCONNECT             _IOW ('f', 112, long)
430 #define OBD_IOC_STATFS                 _IOWR('f', 113, long)
431 #define OBD_IOC_SYNC                   _IOR ('f', 114, long)
432 #define OBD_IOC_READ2                  _IOWR('f', 115, long)
433 #define OBD_IOC_FORMAT                 _IOWR('f', 116, long)
434 #define OBD_IOC_PARTITION              _IOWR('f', 117, long)
435 #define OBD_IOC_ATTACH                 _IOWR('f', 118, long)
436 #define OBD_IOC_DETACH                 _IOWR('f', 119, long)
437 #define OBD_IOC_COPY                   _IOWR('f', 120, long)
438 #define OBD_IOC_MIGR                   _IOWR('f', 121, long)
439 #define OBD_IOC_PUNCH                  _IOWR('f', 122, long)
440 #define OBD_IOC_DEVICE                 _IOWR('f', 123, long)
441 #define OBD_IOC_MODULE_DEBUG           _IOWR('f', 124, long)
442 #define OBD_IOC_BRW_READ               _IOWR('f', 125, long)
443 #define OBD_IOC_BRW_WRITE              _IOWR('f', 126, long)
444 #define OBD_IOC_NAME2DEV               _IOWR('f', 127, long)
445 #define OBD_IOC_NEWDEV                 _IOWR('f', 128, long)
446 #define OBD_IOC_LIST                   _IOWR('f', 129, long)
447 #define OBD_IOC_UUID2DEV               _IOWR('f', 130, long)
448
449 #define OBD_IOC_RECOVD_NEWCONN         _IOWR('f', 131, long)
450 #define OBD_IOC_LOV_SET_CONFIG         _IOWR('f', 132, long)
451 #define OBD_IOC_LOV_GET_CONFIG         _IOWR('f', 133, long)
452 #define OBD_IOC_LOV_CONFIG             OBD_IOC_LOV_SET_CONFIG
453
454 #define OBD_IOC_OPEN                   _IOWR('f', 134, long)
455 #define OBD_IOC_CLOSE                  _IOWR('f', 135, long)
456
457 #define OBD_IOC_RECOVD_FAILCONN        _IOWR('f', 136, long)
458
459 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 139      )
460
461 #define OBD_GET_VERSION                _IOWR ('f', 144, long)
462
463 /*
464  * l_wait_event is a flexible sleeping function, permitting simple caller
465  * configuration of interrupt and timeout sensitivity along with actions to
466  * be performed in the event of either exception.
467  *
468  * Common usage looks like this:
469  * 
470  * struct l_wait_info lwi = LWI_TIMEOUT_INTR(timeout, timeout_handler,
471  *                                           intr_handler, callback_data);
472  * rc = l_wait_event(waitq, condition, &lwi);
473  *
474  * (LWI_TIMEOUT and LWI_INTR macros are available for timeout- and
475  * interrupt-only variants, respectively.)
476  *
477  * If a timeout is specified, the timeout_handler will be invoked in the event
478  * that the timeout expires before the process is awakened.  (Note that any
479  * waking of the process will restart the timeout, even if the condition is
480  * not satisfied and the process immediately returns to sleep.  This might be
481  * considered a bug.)  If the timeout_handler returns non-zero, l_wait_event
482  * will return -ETIMEDOUT and the caller will continue.  If the handler returns
483  * zero instead, the process will go back to sleep until it is awakened by the
484  * waitq or some similar mechanism, or an interrupt occurs (if the caller has
485  * asked for interrupts to be detected).  The timeout will only fire once, so
486  * callers should take care that a timeout_handler which returns zero will take
487  * future steps to awaken the process.  N.B. that these steps must include
488  * making the provided condition become true.
489  *
490  * If the interrupt flag (lwi_signals) is non-zero, then the process will be
491  * interruptible, and will be awakened by any "killable" signal (SIGTERM,
492  * SIGKILL or SIGINT).  If a timeout is also specified, then the process will
493  * only become interruptible _after_ the timeout has expired, though it can be
494  * awakened by a signal that was delivered before the timeout and is still
495  * pending when the timeout expires.  If a timeout is not specified, the process
496  * will be interruptible at all times during l_wait_event.
497  */
498
499 struct l_wait_info {
500         long   lwi_timeout;
501         int  (*lwi_on_timeout)(void *);
502         long   lwi_signals;
503         int  (*lwi_on_signal)(void *); /* XXX return is ignored for now */
504         void  *lwi_cb_data;
505 };
506
507 #define LWI_TIMEOUT(time, cb, data)                                            \
508 ((struct l_wait_info) {                                                        \
509         lwi_timeout:    time,                                                  \
510         lwi_on_timeout: cb,                                                    \
511         lwi_cb_data:    data                                                   \
512 })
513
514 #define LWI_INTR(cb, data)                                                     \
515 ((struct l_wait_info) {                                                        \
516         lwi_signals:   1,                                                      \
517         lwi_on_signal: cb,                                                     \
518         lwi_cb_data:   data                                                    \
519 })
520
521 #define LWI_TIMEOUT_INTR(time, time_cb, sig_cb, data)                          \
522 ((struct l_wait_info) {                                                        \
523         lwi_timeout:    time,                                                  \
524         lwi_on_timeout: time_cb,                                               \
525         lwi_signals:    1,                                                     \
526         lwi_on_signal:  sig_cb,                                                \
527         lwi_cb_data:    data                                                   \
528 })
529
530 /* XXX this should be one mask-check */
531 #define l_killable_pending(task)                                               \
532 (sigismember(&(task->pending.signal), SIGKILL) ||                              \
533  sigismember(&(task->pending.signal), SIGINT) ||                               \
534  sigismember(&(task->pending.signal), SIGTERM))
535
536 #define __l_wait_event(wq, condition, info, ret)                               \
537 do {                                                                           \
538         wait_queue_t __wait;                                                   \
539         long __state;                                                          \
540         int __timed_out = 0;                                                   \
541         init_waitqueue_entry(&__wait, current);                                \
542                                                                                \
543         add_wait_queue(&wq, &__wait);                                          \
544         if (info->lwi_signals && !info->lwi_timeout)                           \
545             __state = TASK_INTERRUPTIBLE;                                      \
546         else                                                                   \
547             __state = TASK_UNINTERRUPTIBLE;                                    \
548         for (;;) {                                                             \
549             set_current_state(__state);                                        \
550             if (condition)                                                     \
551                     break;                                                     \
552             if (__state == TASK_INTERRUPTIBLE && l_killable_pending(current)) {\
553                 if (info->lwi_on_signal)                                       \
554                         info->lwi_on_signal(info->lwi_cb_data);                \
555                 ret = -EINTR;                                                  \
556                 break;                                                         \
557             }                                                                  \
558             if (info->lwi_timeout && !__timed_out) {                           \
559                 if (schedule_timeout(info->lwi_timeout) == 0) {                \
560                     __timed_out = 1;                                           \
561                     if (!info->lwi_on_timeout ||                               \
562                         info->lwi_on_timeout(info->lwi_cb_data)) {             \
563                         ret = -ETIMEDOUT;                                      \
564                         break;                                                 \
565                     }                                                          \
566                     /* We'll take signals after a timeout. */                  \
567                     if (info->lwi_signals) {                                   \
568                         __state = TASK_INTERRUPTIBLE;                          \
569                         /* Check for a pending interrupt. */                   \
570                         if (info->lwi_signals && l_killable_pending(current)) {\
571                             if (info->lwi_on_signal)                           \
572                                 info->lwi_on_signal(info->lwi_cb_data);        \
573                             ret = -EINTR;                                      \
574                             break;                                             \
575                         }                                                      \
576                     }                                                          \
577                 }                                                              \
578             } else {                                                           \
579                 schedule();                                                    \
580             }                                                                  \
581         }                                                                      \
582         current->state = TASK_RUNNING;                                         \
583         remove_wait_queue(&wq, &__wait);                                       \
584 } while(0)
585
586 #define l_wait_event(wq, condition, info)                                      \
587 ({                                                                             \
588         int __ret = 0;                                                         \
589         struct l_wait_info *__info = (info);                                   \
590         if (!(condition))                                                      \
591                 __l_wait_event(wq, condition, __info, __ret);                  \
592         __ret;                                                                 \
593 })
594
595 #endif /* _LUSTRE_LIB_H */