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