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