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