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