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