Whamcloud - gitweb
Branch b1_4
[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 #define cfs_kernel_thread(func, a, f)   kernel_thread(func, a, f)
129
130 /*
131  * Task struct
132  */
133 typedef struct task_struct              cfs_task_t;
134 #define cfs_current()                   current
135 #define cfs_task_lock(t)                task_lock(t)
136 #define cfs_task_unlock(t)              task_unlock(t)
137 #define CFS_DECL_JOURNAL_DATA           void *journal_info
138 #define CFS_PUSH_JOURNAL                do {    \
139         journal_info = current->journal_info;   \
140         current->journal_info = NULL;           \
141         } while(0)
142 #define CFS_POP_JOURNAL                 do {    \
143         current->journal_info = journal_info;   \
144         } while(0)
145
146 /* Module interfaces */
147 #define cfs_module(name, version, init, fini) \
148 module_init(init);                            \
149 module_exit(fini)
150
151 /*
152  * Signal
153  */
154 typedef sigset_t                        cfs_sigset_t;
155
156 /*
157  * Timer
158  */
159 typedef struct timer_list cfs_timer_t;
160 typedef  void (*timer_func_t)(unsigned long);
161
162 #define cfs_init_timer(t)       init_timer(t)
163
164 static inline void cfs_timer_init(cfs_timer_t *t, void (*func)(unsigned long), void *arg)
165 {
166         init_timer(t);
167         t->function = (timer_func_t)func;
168         t->data = (unsigned long)arg;
169 }
170
171 static inline void cfs_timer_done(cfs_timer_t *t)
172 {
173         return;
174 }
175
176 static inline void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
177 {
178         mod_timer(t, deadline);
179 }
180
181 static inline void cfs_timer_disarm(cfs_timer_t *t)
182 {
183         del_timer(t);
184 }
185
186 static inline int  cfs_timer_is_armed(cfs_timer_t *t)
187 {
188         return timer_pending(t);
189 }
190
191 static inline cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
192 {
193         return t->expires;
194 }
195
196
197 /* deschedule for a bit... */
198 static inline void cfs_pause(cfs_duration_t ticks)
199 {
200         set_current_state(TASK_UNINTERRUPTIBLE);
201         schedule_timeout(ticks);
202 }
203
204 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
205 #define __wait_event_timeout(wq, condition, timeout, ret)        \
206 do {                                                             \
207         int __ret = 0;                                           \
208         if (!(condition)) {                                      \
209                 wait_queue_t __wait;                             \
210                 unsigned long expire;                            \
211                                                                  \
212                 init_waitqueue_entry(&__wait, current);          \
213                 expire = timeout + jiffies;                      \
214                 add_wait_queue(&wq, &__wait);                    \
215                 for (;;) {                                       \
216                         set_current_state(TASK_UNINTERRUPTIBLE); \
217                         if (condition)                           \
218                                 break;                           \
219                         if (jiffies > expire) {                  \
220                                 ret = jiffies - expire;          \
221                                 break;                           \
222                         }                                        \
223                         schedule_timeout(timeout);               \
224                 }                                                \
225                 current->state = TASK_RUNNING;                   \
226                 remove_wait_queue(&wq, &__wait);                 \
227         }                                                        \
228 } while (0)
229 /*
230    retval == 0; condition met; we're good.
231    retval > 0; timed out.
232 */
233 #define cfs_waitq_wait_event_timeout(wq, condition, timeout)         \
234 ({                                                                   \
235         int __ret = 0;                                               \
236         if (!(condition))                                            \
237                 __wait_event_timeout(wq, condition, timeout, __ret); \
238         __ret;                                                       \
239 })
240 #else
241 #define cfs_waitq_wait_event_timeout  wait_event_timeout
242 #endif
243
244 #ifndef wait_event_interruptible_timeout /* Only for RHEL3 2.4.21 kernel */
245 #define __wait_event_interruptible_timeout(wq, condition, timeout, ret)   \
246 do {                                                           \
247         int __ret = 0;                                         \
248         if (!(condition)) {                                    \
249                 wait_queue_t __wait;                           \
250                 unsigned long expire;                          \
251                                                                \
252                 init_waitqueue_entry(&__wait, current);        \
253                 expire = timeout + jiffies;                    \
254                 add_wait_queue(&wq, &__wait);                  \
255                 for (;;) {                                     \
256                         set_current_state(TASK_INTERRUPTIBLE); \
257                         if (condition)                         \
258                                 break;                         \
259                         if (jiffies > expire) {                \
260                                 ret = jiffies - expire;        \
261                                 break;                         \
262                         }                                      \
263                         if (!signal_pending(current)) {        \
264                                 schedule_timeout(timeout);     \
265                                 continue;                      \
266                         }                                      \
267                         ret = -ERESTARTSYS;                    \
268                         break;                                 \
269                 }                                              \
270                 current->state = TASK_RUNNING;                 \
271                 remove_wait_queue(&wq, &__wait);               \
272         }                                                      \
273 } while (0)
274
275 /*
276    retval == 0; condition met; we're good.
277    retval < 0; interrupted by signal.
278    retval > 0; timed out.
279 */
280 #define cfs_waitq_wait_event_interruptible_timeout(wq, condition, timeout) \
281 ({                                                                \
282         int __ret = 0;                                            \
283         if (!(condition))                                         \
284                 __wait_event_interruptible_timeout(wq, condition, \
285                                                 timeout, __ret);  \
286         __ret;                                                    \
287 })
288 #else
289 #define cfs_waitq_wait_event_interruptible_timeout wait_event_interruptible_timeout
290 #endif
291
292 #else   /* !__KERNEL__ */
293
294 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
295 #include "../user-prim.h"
296
297 #endif /* __KERNEL__ */
298
299 #endif