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