Whamcloud - gitweb
705499e7ddf77761c647b60cd3d88f132b04c795
[fs/lustre-release.git] / lnet / include / libcfs / linux / linux-prim.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 library routines.
22  *
23  */
24
25 #ifndef __LIBCFS_LINUX_CFS_PRIM_H__
26 #define __LIBCFS_LINUX_CFS_PRIM_H__
27
28 #ifndef __LIBCFS_LIBCFS_H__
29 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
30 #endif
31
32 #ifdef __KERNEL__
33 #ifndef AUTOCONF_INCLUDED
34 #include <linux/config.h>
35 #endif
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/version.h>
39 #include <linux/proc_fs.h>
40 #include <linux/mm.h>
41 #include <linux/timer.h>
42 #include <linux/signal.h>
43 #include <linux/sched.h>
44
45 #include <linux/miscdevice.h>
46 #include <libcfs/linux/portals_compat25.h>
47 #include <asm/div64.h>
48
49 #include <libcfs/linux/linux-time.h>
50
51 /*
52  * Pseudo device register
53  */
54 typedef struct miscdevice               cfs_psdev_t;
55 #define cfs_psdev_register(dev)         misc_register(dev)
56 #define cfs_psdev_deregister(dev)       misc_deregister(dev)
57
58 /*
59  * Sysctl register
60  */
61 typedef struct ctl_table                cfs_sysctl_table_t;
62 typedef struct ctl_table_header         cfs_sysctl_table_header_t;
63
64 #ifdef HAVE_2ARGS_REGISTER_SYSCTL
65 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t, a)
66 #else
67 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t)
68 #endif
69 #define cfs_unregister_sysctl_table(t)  unregister_sysctl_table(t)
70
71 /*
72  * Symbol register
73  */
74 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
75 #define cfs_symbol_register(s, p)       inter_module_register(s, THIS_MODULE, p)
76 #define cfs_symbol_unregister(s)        inter_module_unregister(s)
77 #define cfs_symbol_get(s)               inter_module_get(s)
78 #define cfs_symbol_put(s)               inter_module_put(s)
79 #define cfs_module_get()                MOD_INC_USE_COUNT
80 #define cfs_module_put()                MOD_DEC_USE_COUNT
81 #else
82 #define cfs_symbol_register(s, p)       do {} while(0)
83 #define cfs_symbol_unregister(s)        do {} while(0)
84 #define cfs_symbol_get(s)               symbol_get(s)
85 #define cfs_symbol_put(s)               symbol_put(s)
86 #define cfs_module_get()                try_module_get(THIS_MODULE)
87 #define cfs_module_put()                module_put(THIS_MODULE)
88 #endif
89
90 /*
91  * Proc file system APIs
92  */
93 typedef read_proc_t                     cfs_read_proc_t;
94 typedef write_proc_t                    cfs_write_proc_t;
95 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
96 #define cfs_create_proc_entry(n, m, p)  create_proc_entry(n, m, p)
97 #define cfs_free_proc_entry(e)          free_proc_entry(e)
98 #define cfs_remove_proc_entry(n, e)     remove_proc_entry(n, e)
99
100 /*
101  * Wait Queue
102  */
103 #define CFS_TASK_INTERRUPTIBLE          TASK_INTERRUPTIBLE
104 #define CFS_TASK_UNINT                  TASK_UNINTERRUPTIBLE
105
106 typedef wait_queue_t                    cfs_waitlink_t;
107 typedef wait_queue_head_t               cfs_waitq_t;
108
109 typedef long                            cfs_task_state_t;
110
111 #define cfs_waitq_init(w)               init_waitqueue_head(w)
112 #define cfs_waitlink_init(l)            init_waitqueue_entry(l, current)
113 #define cfs_waitq_add(w, l)             add_wait_queue(w, l)
114 #define cfs_waitq_add_exclusive(w, l)   add_wait_queue_exclusive(w, l)
115 #define cfs_waitq_forward(l, w)         do {} while(0)
116 #define cfs_waitq_del(w, l)             remove_wait_queue(w, l)
117 #define cfs_waitq_active(w)             waitqueue_active(w)
118 #define cfs_waitq_signal(w)             wake_up(w)
119 #define cfs_waitq_signal_nr(w,n)        wake_up_nr(w, n)
120 #define cfs_waitq_broadcast(w)          wake_up_all(w)
121 #define cfs_waitq_wait(l, s)            schedule()
122 #define cfs_waitq_timedwait(l, s, t)    schedule_timeout(t)
123 #define cfs_schedule_timeout(s, t)      schedule_timeout(t)
124 #define cfs_schedule()                  schedule()
125
126 /* Kernel thread */
127 typedef int (*cfs_thread_t)(void *);
128
129 static inline int cfs_kernel_thread(int (*fn)(void *),
130                                     void *arg, unsigned long flags)
131 {
132         void *orig_info = current->journal_info;
133         int rc;
134
135         current->journal_info = NULL;
136         rc = kernel_thread(fn, arg, flags);
137         current->journal_info = orig_info;
138         return rc;
139 }
140
141
142 /*
143  * Task struct
144  */
145 typedef struct task_struct              cfs_task_t;
146 #define cfs_current()                   current
147 #define cfs_task_lock(t)                task_lock(t)
148 #define cfs_task_unlock(t)              task_unlock(t)
149 #define CFS_DECL_JOURNAL_DATA           void *journal_info
150 #define CFS_PUSH_JOURNAL                do {    \
151         journal_info = current->journal_info;   \
152         current->journal_info = NULL;           \
153         } while(0)
154 #define CFS_POP_JOURNAL                 do {    \
155         current->journal_info = journal_info;   \
156         } while(0)
157
158 /* Module interfaces */
159 #define cfs_module(name, version, init, fini) \
160 module_init(init);                            \
161 module_exit(fini)
162
163 /*
164  * Signal
165  */
166 typedef sigset_t                        cfs_sigset_t;
167
168 /*
169  * Timer
170  */
171 typedef struct timer_list cfs_timer_t;
172 typedef  void (*timer_func_t)(unsigned long);
173
174 #define cfs_init_timer(t)       init_timer(t)
175
176 static inline void cfs_timer_init(cfs_timer_t *t, void (*func)(unsigned long), void *arg)
177 {
178         init_timer(t);
179         t->function = (timer_func_t)func;
180         t->data = (unsigned long)arg;
181 }
182
183 static inline void cfs_timer_done(cfs_timer_t *t)
184 {
185         return;
186 }
187
188 static inline void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
189 {
190         mod_timer(t, deadline);
191 }
192
193 static inline void cfs_timer_disarm(cfs_timer_t *t)
194 {
195         del_timer(t);
196 }
197
198 static inline int  cfs_timer_is_armed(cfs_timer_t *t)
199 {
200         return timer_pending(t);
201 }
202
203 static inline cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
204 {
205         return t->expires;
206 }
207
208
209 /* deschedule for a bit... */
210 static inline void cfs_pause(cfs_duration_t ticks)
211 {
212         set_current_state(TASK_UNINTERRUPTIBLE);
213         schedule_timeout(ticks);
214 }
215
216 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
217 #define __wait_event_timeout(wq, condition, timeout, ret)        \
218 do {                                                             \
219         int __ret = 0;                                           \
220         if (!(condition)) {                                      \
221                 wait_queue_t __wait;                             \
222                 unsigned long expire;                            \
223                                                                  \
224                 init_waitqueue_entry(&__wait, current);          \
225                 expire = timeout + jiffies;                      \
226                 add_wait_queue(&wq, &__wait);                    \
227                 for (;;) {                                       \
228                         set_current_state(TASK_UNINTERRUPTIBLE); \
229                         if (condition)                           \
230                                 break;                           \
231                         if (jiffies > expire) {                  \
232                                 ret = jiffies - expire;          \
233                                 break;                           \
234                         }                                        \
235                         schedule_timeout(timeout);               \
236                 }                                                \
237                 current->state = TASK_RUNNING;                   \
238                 remove_wait_queue(&wq, &__wait);                 \
239         }                                                        \
240 } while (0)
241 /*
242    retval == 0; condition met; we're good.
243    retval > 0; timed out.
244 */
245 #define cfs_waitq_wait_event_timeout(wq, condition, timeout)         \
246 ({                                                                   \
247         int __ret = 0;                                               \
248         if (!(condition))                                            \
249                 __wait_event_timeout(wq, condition, timeout, __ret); \
250         __ret;                                                       \
251 })
252 #else
253 #define cfs_waitq_wait_event_timeout  wait_event_timeout
254 #endif
255
256 #ifndef wait_event_interruptible_timeout /* Only for RHEL3 2.4.21 kernel */
257 #define __wait_event_interruptible_timeout(wq, condition, timeout, ret)   \
258 do {                                                           \
259         int __ret = 0;                                         \
260         if (!(condition)) {                                    \
261                 wait_queue_t __wait;                           \
262                 unsigned long expire;                          \
263                                                                \
264                 init_waitqueue_entry(&__wait, current);        \
265                 expire = timeout + jiffies;                    \
266                 add_wait_queue(&wq, &__wait);                  \
267                 for (;;) {                                     \
268                         set_current_state(TASK_INTERRUPTIBLE); \
269                         if (condition)                         \
270                                 break;                         \
271                         if (jiffies > expire) {                \
272                                 ret = jiffies - expire;        \
273                                 break;                         \
274                         }                                      \
275                         if (!signal_pending(current)) {        \
276                                 schedule_timeout(timeout);     \
277                                 continue;                      \
278                         }                                      \
279                         ret = -ERESTARTSYS;                    \
280                         break;                                 \
281                 }                                              \
282                 current->state = TASK_RUNNING;                 \
283                 remove_wait_queue(&wq, &__wait);               \
284         }                                                      \
285 } while (0)
286
287 /*
288    retval == 0; condition met; we're good.
289    retval < 0; interrupted by signal.
290    retval > 0; timed out.
291 */
292 #define cfs_waitq_wait_event_interruptible_timeout(wq, condition, timeout) \
293 ({                                                                \
294         int __ret = 0;                                            \
295         if (!(condition))                                         \
296                 __wait_event_interruptible_timeout(wq, condition, \
297                                                 timeout, __ret);  \
298         __ret;                                                    \
299 })
300 #else
301 #define cfs_waitq_wait_event_interruptible_timeout wait_event_interruptible_timeout
302 #endif
303
304 #else   /* !__KERNEL__ */
305
306 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
307 #include "../user-prim.h"
308
309 #endif /* __KERNEL__ */
310
311 #endif