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