Whamcloud - gitweb
ea3097a1eec05be1b6d8a32d53a17035255ed3d4
[fs/lustre-release.git] / libcfs / 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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/linux/linux-prim.h
37  *
38  * Basic library routines.
39  */
40
41 #ifndef __LIBCFS_LINUX_CFS_PRIM_H__
42 #define __LIBCFS_LINUX_CFS_PRIM_H__
43
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
46 #endif
47
48 #ifdef __KERNEL__
49 #ifndef AUTOCONF_INCLUDED
50 #include <linux/config.h>
51 #endif
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/version.h>
55 #include <linux/proc_fs.h>
56 #include <linux/mm.h>
57 #include <linux/timer.h>
58 #include <linux/signal.h>
59 #include <linux/sched.h>
60
61 #include <linux/miscdevice.h>
62 #include <libcfs/linux/portals_compat25.h>
63 #include <asm/div64.h>
64
65 #include <libcfs/linux/linux-time.h>
66
67 /*
68  * Pseudo device register
69  */
70 typedef struct miscdevice               cfs_psdev_t;
71 #define cfs_psdev_register(dev)         misc_register(dev)
72 #define cfs_psdev_deregister(dev)       misc_deregister(dev)
73
74 /*
75  * Sysctl register
76  */
77 typedef struct ctl_table                cfs_sysctl_table_t;
78 typedef struct ctl_table_header         cfs_sysctl_table_header_t;
79
80 #ifdef HAVE_2ARGS_REGISTER_SYSCTL
81 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t, a)
82 #else
83 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t)
84 #endif
85 #define cfs_unregister_sysctl_table(t)  unregister_sysctl_table(t)
86
87 /*
88  * Symbol register
89  */
90 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
91 #define cfs_symbol_register(s, p)       inter_module_register(s, THIS_MODULE, p)
92 #define cfs_symbol_unregister(s)        inter_module_unregister(s)
93 #define cfs_symbol_get(s)               inter_module_get(s)
94 #define cfs_symbol_put(s)               inter_module_put(s)
95 #define cfs_module_get()                MOD_INC_USE_COUNT
96 #define cfs_module_put()                MOD_DEC_USE_COUNT
97 #else
98 #define cfs_symbol_register(s, p)       do {} while(0)
99 #define cfs_symbol_unregister(s)        do {} while(0)
100 #define cfs_symbol_get(s)               symbol_get(s)
101 #define cfs_symbol_put(s)               symbol_put(s)
102 #define cfs_module_get()                try_module_get(THIS_MODULE)
103 #define cfs_module_put()                module_put(THIS_MODULE)
104 #endif
105
106 /*
107  * Proc file system APIs
108  */
109 typedef read_proc_t                     cfs_read_proc_t;
110 typedef write_proc_t                    cfs_write_proc_t;
111 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
112 #define cfs_create_proc_entry(n, m, p)  create_proc_entry(n, m, p)
113 #define cfs_free_proc_entry(e)          free_proc_entry(e)
114 #define cfs_remove_proc_entry(n, e)     remove_proc_entry(n, e)
115
116 /*
117  * Wait Queue
118  */
119 #define CFS_TASK_INTERRUPTIBLE          TASK_INTERRUPTIBLE
120 #define CFS_TASK_UNINT                  TASK_UNINTERRUPTIBLE
121
122 typedef wait_queue_t                    cfs_waitlink_t;
123 typedef wait_queue_head_t               cfs_waitq_t;
124
125 typedef long                            cfs_task_state_t;
126
127 #define cfs_waitq_init(w)               init_waitqueue_head(w)
128 #define cfs_waitlink_init(l)            init_waitqueue_entry(l, current)
129 #define cfs_waitq_add(w, l)             add_wait_queue(w, l)
130 #define cfs_waitq_add_exclusive(w, l)   add_wait_queue_exclusive(w, l)
131 #define cfs_waitq_forward(l, w)         do {} while(0)
132 #define cfs_waitq_del(w, l)             remove_wait_queue(w, l)
133 #define cfs_waitq_active(w)             waitqueue_active(w)
134 #define cfs_waitq_signal(w)             wake_up(w)
135 #define cfs_waitq_signal_nr(w,n)        wake_up_nr(w, n)
136 #define cfs_waitq_broadcast(w)          wake_up_all(w)
137 #define cfs_waitq_wait(l, s)            schedule()
138 #define cfs_waitq_timedwait(l, s, t)    schedule_timeout(t)
139 #define cfs_schedule_timeout(s, t)      schedule_timeout(t)
140 #define cfs_schedule()                  schedule()
141
142 /* Kernel thread */
143 typedef int (*cfs_thread_t)(void *);
144
145 static inline int cfs_kernel_thread(int (*fn)(void *),
146                                     void *arg, unsigned long flags)
147 {
148         void *orig_info = current->journal_info;
149         int rc;
150
151         current->journal_info = NULL;
152         rc = kernel_thread(fn, arg, flags);
153         current->journal_info = orig_info;
154         return rc;
155 }
156
157
158 /*
159  * Task struct
160  */
161 typedef struct task_struct              cfs_task_t;
162 #define cfs_current()                   current
163 #define cfs_task_lock(t)                task_lock(t)
164 #define cfs_task_unlock(t)              task_unlock(t)
165 #define CFS_DECL_JOURNAL_DATA           void *journal_info
166 #define CFS_PUSH_JOURNAL                do {    \
167         journal_info = current->journal_info;   \
168         current->journal_info = NULL;           \
169         } while(0)
170 #define CFS_POP_JOURNAL                 do {    \
171         current->journal_info = journal_info;   \
172         } while(0)
173
174 /* Module interfaces */
175 #define cfs_module(name, version, init, fini) \
176 module_init(init);                            \
177 module_exit(fini)
178
179 /*
180  * Signal
181  */
182 typedef sigset_t                        cfs_sigset_t;
183
184 /*
185  * Timer
186  */
187 typedef struct timer_list cfs_timer_t;
188 typedef  void (*timer_func_t)(unsigned long);
189
190 #define cfs_init_timer(t)       init_timer(t)
191
192 static inline void cfs_timer_init(cfs_timer_t *t, void (*func)(unsigned long), void *arg)
193 {
194         init_timer(t);
195         t->function = (timer_func_t)func;
196         t->data = (unsigned long)arg;
197 }
198
199 static inline void cfs_timer_done(cfs_timer_t *t)
200 {
201         return;
202 }
203
204 static inline void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
205 {
206         mod_timer(t, deadline);
207 }
208
209 static inline void cfs_timer_disarm(cfs_timer_t *t)
210 {
211         del_timer(t);
212 }
213
214 static inline int  cfs_timer_is_armed(cfs_timer_t *t)
215 {
216         return timer_pending(t);
217 }
218
219 static inline cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
220 {
221         return t->expires;
222 }
223
224
225 /* deschedule for a bit... */
226 static inline void cfs_pause(cfs_duration_t ticks)
227 {
228         set_current_state(TASK_UNINTERRUPTIBLE);
229         schedule_timeout(ticks);
230 }
231
232 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
233 #define __wait_event_timeout(wq, condition, timeout, ret)        \
234 do {                                                             \
235         int __ret = 0;                                           \
236         if (!(condition)) {                                      \
237                 wait_queue_t __wait;                             \
238                 unsigned long expire;                            \
239                                                                  \
240                 init_waitqueue_entry(&__wait, current);          \
241                 expire = timeout + jiffies;                      \
242                 add_wait_queue(&wq, &__wait);                    \
243                 for (;;) {                                       \
244                         set_current_state(TASK_UNINTERRUPTIBLE); \
245                         if (condition)                           \
246                                 break;                           \
247                         if (jiffies > expire) {                  \
248                                 ret = jiffies - expire;          \
249                                 break;                           \
250                         }                                        \
251                         schedule_timeout(timeout);               \
252                 }                                                \
253                 current->state = TASK_RUNNING;                   \
254                 remove_wait_queue(&wq, &__wait);                 \
255         }                                                        \
256 } while (0)
257 /*
258    retval == 0; condition met; we're good.
259    retval > 0; timed out.
260 */
261 #define cfs_waitq_wait_event_timeout(wq, condition, timeout)         \
262 ({                                                                   \
263         int __ret = 0;                                               \
264         if (!(condition))                                            \
265                 __wait_event_timeout(wq, condition, timeout, __ret); \
266         __ret;                                                       \
267 })
268 #else
269 #define cfs_waitq_wait_event_timeout  wait_event_timeout
270 #endif
271
272 #ifndef wait_event_interruptible_timeout /* Only for RHEL3 2.4.21 kernel */
273 #define __wait_event_interruptible_timeout(wq, condition, timeout, ret)   \
274 do {                                                           \
275         int __ret = 0;                                         \
276         if (!(condition)) {                                    \
277                 wait_queue_t __wait;                           \
278                 unsigned long expire;                          \
279                                                                \
280                 init_waitqueue_entry(&__wait, current);        \
281                 expire = timeout + jiffies;                    \
282                 add_wait_queue(&wq, &__wait);                  \
283                 for (;;) {                                     \
284                         set_current_state(TASK_INTERRUPTIBLE); \
285                         if (condition)                         \
286                                 break;                         \
287                         if (jiffies > expire) {                \
288                                 ret = jiffies - expire;        \
289                                 break;                         \
290                         }                                      \
291                         if (!signal_pending(current)) {        \
292                                 schedule_timeout(timeout);     \
293                                 continue;                      \
294                         }                                      \
295                         ret = -ERESTARTSYS;                    \
296                         break;                                 \
297                 }                                              \
298                 current->state = TASK_RUNNING;                 \
299                 remove_wait_queue(&wq, &__wait);               \
300         }                                                      \
301 } while (0)
302
303 /*
304    retval == 0; condition met; we're good.
305    retval < 0; interrupted by signal.
306    retval > 0; timed out.
307 */
308 #define cfs_waitq_wait_event_interruptible_timeout(wq, condition, timeout) \
309 ({                                                                \
310         int __ret = 0;                                            \
311         if (!(condition))                                         \
312                 __wait_event_interruptible_timeout(wq, condition, \
313                                                 timeout, __ret);  \
314         __ret;                                                    \
315 })
316 #else
317 #define cfs_waitq_wait_event_interruptible_timeout wait_event_interruptible_timeout
318 #endif
319
320 #else   /* !__KERNEL__ */
321
322 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
323 #include "../user-prim.h"
324
325 #endif /* __KERNEL__ */
326
327 #endif