Whamcloud - gitweb
Prevent C-c and C-z from locking us up, and make most of our waits
[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 #endif
33
34 #include <linux/portals_lib.h>
35 #include <asm/semaphore.h>
36 #include <linux/lustre_idl.h>
37
38 #ifdef __KERNEL__
39 /* l_net.c */
40 struct ptlrpc_request;
41 int target_handle_connect(struct ptlrpc_request *req);
42 int target_handle_disconnect(struct ptlrpc_request *req);
43
44 /* l_lock.c */
45 struct lustre_lock { 
46         int l_depth;
47         struct task_struct *l_owner;
48         struct semaphore l_sem;
49         spinlock_t l_spin;
50 };
51
52 void l_lock_init(struct lustre_lock *);
53 void l_lock(struct lustre_lock *);
54 void l_unlock(struct lustre_lock *);
55
56
57 /* page.c */
58 inline void lustre_put_page(struct page *page);
59 struct page *lustre_get_page_read(struct inode *dir, unsigned long index);
60 struct page *lustre_get_page_write(struct inode *dir, unsigned long index);
61 int lustre_commit_write(struct page *page, unsigned from, unsigned to);
62 void set_page_clean(struct page *page);
63 void set_page_dirty(struct page *page);
64
65 /* simple.c */
66 struct obd_run_ctxt;
67 void push_ctxt(struct obd_run_ctxt *save, struct obd_run_ctxt *new);
68 void pop_ctxt(struct obd_run_ctxt *saved);
69 #ifdef OBD_CTXT_DEBUG
70 #define OBD_SET_CTXT_MAGIC(ctxt) (ctxt)->magic = OBD_RUN_CTXT_MAGIC
71 #else
72 #define OBD_SET_CTXT_MAGIC(ctxt) do {} while(0)
73 #endif
74 struct dentry *simple_mkdir(struct dentry *dir, char *name, int mode);
75 int lustre_fread(struct file *file, char *str, int len, loff_t *off);
76 int lustre_fwrite(struct file *file, const char *str, int len, loff_t *off);
77 int lustre_fsync(struct file *file);
78
79 static inline void l_dput(struct dentry *de)
80 {
81         if (!de || IS_ERR(de))
82                 return;
83         shrink_dcache_parent(de);
84         dput(de);
85 }
86
87 static inline void ll_sleep(int t)
88 {
89         set_current_state(TASK_INTERRUPTIBLE);
90         schedule_timeout(t * HZ);
91         set_current_state(TASK_RUNNING);
92 }
93 #endif
94
95 /* FIXME: This needs to validate pointers and cookies */
96 static inline void *lustre_handle2object(struct lustre_handle *handle)
97 {
98         if (handle) 
99                 return (void *)(unsigned long)(handle->addr);
100         return NULL; 
101 }
102
103 static inline void ldlm_object2handle(void *object, struct lustre_handle *handle)
104 {
105         handle->addr = (__u64)(unsigned long)object;
106 }
107
108 struct obd_statfs;
109 struct statfs;
110 void obd_statfs_pack(struct obd_statfs *osfs, struct statfs *sfs);
111 void obd_statfs_unpack(struct obd_statfs *osfs, struct statfs *sfs);
112
113 #include <linux/portals_lib.h>
114
115 /* XXX this should be one mask-check */
116 #define l_killable_pending(task)                                               \
117 (sigismember(&(task->pending.signal), SIGKILL) ||                              \
118  sigismember(&(task->pending.signal), SIGINT) ||                               \
119  sigismember(&(task->pending.signal), SIGTERM))
120
121 /*
122  * Like wait_event_interruptible, but we're only interruptible by KILL, INT, or
123  * TERM.
124  */
125 #define __l_wait_event_killable(wq, condition, ret)                          \
126 do {                                                                         \
127         wait_queue_t __wait;                                                 \
128         init_waitqueue_entry(&__wait, current);                              \
129                                                                              \
130         add_wait_queue(&wq, &__wait);                                        \
131         for (;;) {                                                           \
132                 set_current_state(TASK_INTERRUPTIBLE);                       \
133                 if (condition)                                               \
134                         break;                                               \
135                 if (!signal_pending(current) ||                              \
136                     !l_killable_pending(current)) {                          \
137                         schedule();                                          \
138                         continue;                                            \
139                 }                                                            \
140                 ret = -ERESTARTSYS;                                          \
141                 break;                                                       \
142         }                                                                    \
143         current->state = TASK_RUNNING;                                       \
144         remove_wait_queue(&wq, &__wait);                                     \
145 } while(0)
146
147 #define l_wait_event_killable(wq, condition)                            \
148 ({                                                                      \
149         int __ret = 0;                                                  \
150         if (!(condition))                                               \
151                 __l_wait_event_killable(wq, condition, __ret);          \
152         __ret;                                                          \
153 })
154
155 #endif /* _LUSTRE_LIB_H */