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