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