Whamcloud - gitweb
3bb73a0659ced5d172024bd2ea7d3e1287120315
[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
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 int target_handle_connect(struct ptlrpc_request *req);
41 int target_handle_disconnect(struct ptlrpc_request *req);
42
43 /* l_lock.c */
44 struct lustre_lock { 
45         int l_depth;
46         struct task_struct *l_owner;
47         struct semaphore l_sem;
48         spinlock_t l_spin;
49 };
50
51 void l_lock_init(struct lustre_lock *);
52 void l_lock(struct lustre_lock *);
53 void l_unlock(struct lustre_lock *);
54
55
56 /* page.c */
57 inline void lustre_put_page(struct page *page);
58 struct page *lustre_get_page_read(struct inode *dir, unsigned long index);
59 struct page *lustre_get_page_write(struct inode *dir, unsigned long index);
60 int lustre_commit_write(struct page *page, unsigned from, unsigned to);
61 void set_page_clean(struct page *page);
62 void set_page_dirty(struct page *page);
63
64 /* simple.c */
65 struct obd_run_ctxt;
66 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new);
67 void pop_ctxt(struct obd_run_ctxt *saved);
68 #ifdef OBD_CTXT_DEBUG
69 #define OBD_SET_CTXT_MAGIC(ctxt) (ctxt)->magic = OBD_RUN_CTXT_MAGIC
70 #else
71 #define OBD_SET_CTXT_MAGIC(ctxt) do {} while(0)
72 #endif
73 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode);
74 int lustre_fread(struct file *file, char *str, int len, loff_t *off);
75 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off);
76 int lustre_fsync(struct file *file);
77
78 static inline void l_dput(struct dentry *de)
79 {
80         if (!de || IS_ERR(de))
81                 return;
82         shrink_dcache_parent(de);
83         dput(de);
84 }
85
86 static inline void ll_sleep(int t)
87 {
88         set_current_state(TASK_INTERRUPTIBLE);
89         schedule_timeout(t * HZ);
90         set_current_state(TASK_RUNNING);
91 }
92 #endif
93
94 /* FIXME: This needs to validate pointers and cookies */
95 static inline void *lustre_handle2object(struct lustre_handle *handle)
96 {
97         if (handle) 
98                 return (void *)(unsigned long)(handle->addr);
99         return NULL; 
100 }
101
102 static inline void ldlm_object2handle(void *object, struct lustre_handle *handle)
103 {
104         handle->addr = (__u64)(unsigned long)object;
105 }
106
107 struct obd_statfs;
108 struct statfs;
109 void obd_statfs_pack(struct obd_statfs *osfs, struct statfs *sfs);
110 void obd_statfs_unpack(struct obd_statfs *osfs, struct statfs *sfs);
111
112 #include <linux/portals_lib.h>
113
114 /*
115  *   OBD IOCTLS
116  */
117 #define OBD_IOCTL_VERSION 0x00010001
118
119 struct obd_ioctl_data {
120         uint32_t ioc_len;
121         uint32_t ioc_version;
122
123         uint64_t ioc_addr;
124         uint64_t ioc_cookie;
125         uint32_t ioc_conn1;
126         uint32_t ioc_conn2;
127
128         struct obdo ioc_obdo1;
129         struct obdo ioc_obdo2;
130
131         obd_size         ioc_count;
132         obd_off          ioc_offset;
133         uint32_t         ioc_dev;
134         uint32_t         ____padding;
135
136         /* buffers the kernel will treat as user pointers */
137         uint32_t ioc_plen1;
138         char    *ioc_pbuf1;
139         uint32_t ioc_plen2;
140         char    *ioc_pbuf2;
141
142         /* two inline buffers */
143         uint32_t ioc_inllen1;
144         char    *ioc_inlbuf1;
145         uint32_t ioc_inllen2;
146         char    *ioc_inlbuf2;
147         uint32_t ioc_inllen3;
148         char    *ioc_inlbuf3;
149
150         char    ioc_bulk[0];
151 };
152
153 struct obd_ioctl_hdr {
154         uint32_t ioc_len;
155         uint32_t ioc_version;
156 };
157
158 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
159 {
160         int len = size_round(sizeof(struct obd_ioctl_data));
161         len += size_round(data->ioc_inllen1);
162         len += size_round(data->ioc_inllen2);
163         len += size_round(data->ioc_inllen3);
164         return len;
165 }
166
167
168 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
169 {
170         if (data->ioc_len > (1<<30)) {
171                 printk("OBD ioctl: ioc_len larger than 1<<30\n");
172                 return 1;
173         }
174         if (data->ioc_inllen1 > (1<<30)) {
175                 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
176                 return 1;
177         }
178         if (data->ioc_inllen2 > (1<<30)) {
179                 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
180                 return 1;
181         }
182
183         if (data->ioc_inllen3 > (1<<30)) {
184                 printk("OBD ioctl: ioc_inllen3 larger than 1<<30\n");
185                 return 1;
186         }
187         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
188                 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
189                 return 1;
190         }
191         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
192                 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
193                 return 1;
194         }
195         if (data->ioc_inlbuf3 && !data->ioc_inllen3) {
196                 printk("OBD ioctl: inlbuf3 pointer but 0 length\n");
197                 return 1;
198         }
199         if (data->ioc_pbuf1 && !data->ioc_plen1) {
200                 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
201                 return 1;
202         }
203         if (data->ioc_pbuf2 && !data->ioc_plen2) {
204                 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
205                 return 1;
206         }
207         /*
208         if (data->ioc_inllen1 && !data->ioc_inlbuf1) {
209                 printk("OBD ioctl: inllen1 set but NULL pointer\n");
210                 return 1;
211         }
212         if (data->ioc_inllen2 && !data->ioc_inlbuf2) {
213                 printk("OBD ioctl: inllen2 set but NULL pointer\n");
214                 return 1;
215         }
216         if (data->ioc_inllen3 && !data->ioc_inlbuf3) {
217                 printk("OBD ioctl: inllen3 set but NULL pointer\n");
218                 return 1;
219         }
220         */
221         if (data->ioc_plen1 && !data->ioc_pbuf1) {
222                 printk("OBD ioctl: plen1 set but NULL pointer\n");
223                 return 1;
224         }
225         if (data->ioc_plen2 && !data->ioc_pbuf2) {
226                 printk("OBD ioctl: plen2 set but NULL pointer\n");
227                 return 1;
228         }
229         if (obd_ioctl_packlen(data) != data->ioc_len ) {
230                 printk("OBD ioctl: packlen exceeds ioc_len\n");
231                 return 1;
232         }
233 #if 0
234         if (data->ioc_inllen1 &&
235             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
236                 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
237                 return 1;
238         }
239         if (data->ioc_inllen2 &&
240             data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') {
241                 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
242                 return 1;
243         }
244         if (data->ioc_inllen3 &&
245             data->ioc_bulk[size_round(data->ioc_inllen1) + size_round(data->ioc_inllen2)
246                            + data->ioc_inllen3 - 1] != '\0') {
247                 printk("OBD ioctl: inlbuf3 not 0 terminated\n");
248                 return 1;
249         }
250 #endif 
251         return 0;
252 }
253
254 #ifndef __KERNEL__
255 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf,
256                                  int max)
257 {
258         char *ptr;
259         struct obd_ioctl_data *overlay;
260         data->ioc_len = obd_ioctl_packlen(data);
261         data->ioc_version = OBD_IOCTL_VERSION;
262
263         if (*pbuf && obd_ioctl_packlen(data) > max)
264                 return 1;
265         if (*pbuf == NULL) {
266                 *pbuf = malloc(data->ioc_len);
267         }
268         if (!*pbuf)
269                 return 1;
270         overlay = (struct obd_ioctl_data *)*pbuf;
271         memcpy(*pbuf, data, sizeof(*data));
272
273         ptr = overlay->ioc_bulk;
274         if (data->ioc_inlbuf1)
275                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
276         if (data->ioc_inlbuf2)
277                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
278         if (data->ioc_inlbuf3)
279                 LOGL(data->ioc_inlbuf3, data->ioc_inllen3, ptr);
280         if (obd_ioctl_is_invalid(overlay))
281                 return 1;
282
283         return 0;
284 }
285
286 #else
287
288 /* buffer MUST be at least the size of obd_ioctl_hdr */
289 static inline int obd_ioctl_getdata(char **buf, int *len, void *arg)
290 {
291         struct obd_ioctl_hdr hdr;
292         struct obd_ioctl_data *data;
293         int err;
294         ENTRY;
295
296
297         err = copy_from_user(&hdr, (void *)arg, sizeof(hdr));
298         if ( err ) {
299                 EXIT;
300                 return err;
301         }
302
303         if (hdr.ioc_version != OBD_IOCTL_VERSION) {
304                 printk("OBD: version mismatch kernel vs application\n");
305                 return -EINVAL;
306         }
307
308         if (hdr.ioc_len > 8192) {
309                 printk("OBD: user buffer exceeds 8192 max buffer\n");
310                 return -EINVAL;
311         }
312
313         if (hdr.ioc_len < sizeof(struct obd_ioctl_data)) {
314                 printk("OBD: user buffer too small for ioctl\n");
315                 return -EINVAL;
316         }
317
318         OBD_ALLOC(*buf, hdr.ioc_len);
319         if (!*buf) {
320                 CERROR("Cannot allocate control buffer of len %d\n",
321                        hdr.ioc_len);
322                 RETURN(-EINVAL);
323         }
324         *len = hdr.ioc_len;
325         data = (struct obd_ioctl_data *)*buf;
326
327         err = copy_from_user(*buf, (void *)arg, hdr.ioc_len);
328         if ( err ) {
329                 EXIT;
330                 return err;
331         }
332
333         if (obd_ioctl_is_invalid(data)) {
334                 printk("OBD: ioctl not correctly formatted\n");
335                 return -EINVAL;
336         }
337
338         if (data->ioc_inllen1) {
339                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
340         }
341
342         if (data->ioc_inllen2) {
343                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1);
344         }
345
346         if (data->ioc_inllen3) {
347                 data->ioc_inlbuf3 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1) + 
348                         size_round(data->ioc_inllen2);
349         }
350
351         EXIT;
352         return 0;
353 }
354 #endif
355
356 #define OBD_IOC_CREATE                 _IOR ('f', 101, long)
357 #define OBD_IOC_SETUP                  _IOW ('f', 102, long)
358 #define OBD_IOC_CLEANUP                _IO  ('f', 103      )
359 #define OBD_IOC_DESTROY                _IOW ('f', 104, long)
360 #define OBD_IOC_PREALLOCATE            _IOWR('f', 105, long)
361 #define OBD_IOC_DEC_USE_COUNT          _IO  ('f', 106      )
362 #define OBD_IOC_SETATTR                _IOW ('f', 107, long)
363 #define OBD_IOC_GETATTR                _IOR ('f', 108, long)
364 #define OBD_IOC_READ                   _IOWR('f', 109, long)
365 #define OBD_IOC_WRITE                  _IOWR('f', 110, long)
366 #define OBD_IOC_CONNECT                _IOR ('f', 111, long)
367 #define OBD_IOC_DISCONNECT             _IOW ('f', 112, long)
368 #define OBD_IOC_STATFS                 _IOWR('f', 113, long)
369 #define OBD_IOC_SYNC                   _IOR ('f', 114, long)
370 #define OBD_IOC_READ2                  _IOWR('f', 115, long)
371 #define OBD_IOC_FORMAT                 _IOWR('f', 116, long)
372 #define OBD_IOC_PARTITION              _IOWR('f', 117, long)
373 #define OBD_IOC_ATTACH                 _IOWR('f', 118, long)
374 #define OBD_IOC_DETACH                 _IOWR('f', 119, long)
375 #define OBD_IOC_COPY                   _IOWR('f', 120, long)
376 #define OBD_IOC_MIGR                   _IOWR('f', 121, long)
377 #define OBD_IOC_PUNCH                  _IOWR('f', 122, long)
378 #define OBD_IOC_DEVICE                 _IOWR('f', 123, long)
379 #define OBD_IOC_MODULE_DEBUG           _IOWR('f', 124, long)
380 #define OBD_IOC_BRW_READ               _IOWR('f', 125, long)
381 #define OBD_IOC_BRW_WRITE              _IOWR('f', 126, long)
382 #define OBD_IOC_NAME2DEV               _IOWR('f', 127, long)
383 #define OBD_IOC_NEWDEV                 _IOWR('f', 128, long)
384 #define OBD_IOC_LIST                   _IOWR('f', 129, long)
385 #define OBD_IOC_UUID2DEV               _IOWR('f', 130, long)
386
387 #define OBD_IOC_RECOVD_NEWCONN         _IOWR('f', 131, long)
388 #define OBD_IOC_LOV_CONFIG             _IOWR('f', 132, long)
389
390 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 133      )
391
392
393
394
395
396 /* XXX this should be one mask-check */
397 #define l_killable_pending(task)                                               \
398 (sigismember(&(task->pending.signal), SIGKILL) ||                              \
399  sigismember(&(task->pending.signal), SIGINT) ||                               \
400  sigismember(&(task->pending.signal), SIGTERM))
401
402 /*
403  * Like wait_event_interruptible, but we're only interruptible by KILL, INT, or
404  * TERM.
405  *
406  * XXXshaver These are going away soon, I hope.
407  */
408 #define __l_wait_event_killable(wq, condition, ret)                          \
409 do {                                                                         \
410         wait_queue_t __wait;                                                 \
411         init_waitqueue_entry(&__wait, current);                              \
412                                                                              \
413         add_wait_queue(&wq, &__wait);                                        \
414         for (;;) {                                                           \
415                 set_current_state(TASK_INTERRUPTIBLE);                       \
416                 if (condition)                                               \
417                         break;                                               \
418                 if (!signal_pending(current) ||                              \
419                     !l_killable_pending(current)) {                          \
420                         schedule();                                          \
421                         continue;                                            \
422                 }                                                            \
423                 ret = -ERESTARTSYS;                                          \
424                 break;                                                       \
425         }                                                                    \
426         current->state = TASK_RUNNING;                                       \
427         remove_wait_queue(&wq, &__wait);                                     \
428 } while(0)
429
430 #define l_wait_event_killable(wq, condition)                            \
431 ({                                                                      \
432         int __ret = 0;                                                  \
433         if (!(condition))                                               \
434                 __l_wait_event_killable(wq, condition, __ret);          \
435         __ret;                                                          \
436 })
437
438 #define __l_wait_event_timeout(wq, condition, timeout, ret)                   \
439 do {                                                                          \
440         wait_queue_t __wait;                                                  \
441         init_waitqueue_entry(&__wait, current);                               \
442                                                                               \
443         add_wait_queue(&wq, &__wait);                                         \
444         for (;;) {                                                            \
445                 set_current_state(TASK_INTERRUPTIBLE);                        \
446                 if (condition)                                                \
447                         break;                                                \
448                 if (timeout)                                                  \
449                         schedule_timeout(timeout);                            \
450                 else                                                          \
451                         schedule();                                           \
452         }                                                                     \
453         current->state = TASK_RUNNING;                                        \
454         remove_wait_queue(&wq, &__wait);                                      \
455 } while(0)
456
457 #define l_wait_event_timeout(wq, condition, timeout)                          \
458 ({                                                                            \
459         int __ret = 0;                                                        \
460         if (!(condition))                                                     \
461                 __l_wait_event_timeout(wq, condition, timeout, __ret);        \
462         __ret;                                                                \
463 })
464
465 #endif /* _LUSTRE_LIB_H */