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