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