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