Whamcloud - gitweb
Branch HEAD
[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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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 #ifndef __KERNEL__
49 #error This include is only for kernel use.
50 #endif
51
52 #ifndef AUTOCONF_INCLUDED
53 #include <linux/config.h>
54 #endif
55 #include <linux/module.h>
56 #include <linux/init.h>
57 #include <linux/kernel.h>
58 #include <linux/version.h>
59 #include <linux/proc_fs.h>
60 #include <linux/mm.h>
61 #include <linux/timer.h>
62 #include <linux/signal.h>
63 #include <linux/sched.h>
64 #include <linux/kthread.h>
65
66 #include <linux/miscdevice.h>
67 #include <libcfs/linux/portals_compat25.h>
68 #include <asm/div64.h>
69
70 #include <libcfs/linux/linux-time.h>
71
72 #define CFS_EXPORT_SYMBOL(s)            EXPORT_SYMBOL(s)
73
74 /*
75  * Pseudo device register
76  */
77 typedef struct miscdevice               cfs_psdev_t;
78 #define cfs_psdev_register(dev)         misc_register(dev)
79 #define cfs_psdev_deregister(dev)       misc_deregister(dev)
80
81 /*
82  * Sysctl register
83  */
84 typedef struct ctl_table                cfs_sysctl_table_t;
85 typedef struct ctl_table_header         cfs_sysctl_table_header_t;
86
87 #ifdef HAVE_2ARGS_REGISTER_SYSCTL
88 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t, a)
89 #else
90 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t)
91 #endif
92 #define cfs_unregister_sysctl_table(t)  unregister_sysctl_table(t)
93
94 #define DECLARE_PROC_HANDLER(name)                      \
95 static int                                              \
96 LL_PROC_PROTO(name)                                     \
97 {                                                       \
98         DECLARE_LL_PROC_PPOS_DECL;                      \
99                                                         \
100         return proc_call_handler(table->data, write,    \
101                                  ppos, buffer, lenp,    \
102                                  __##name);             \
103 }
104
105 /*
106  * Symbol register
107  */
108 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
109 #define cfs_symbol_register(s, p)       inter_module_register(s, THIS_MODULE, p)
110 #define cfs_symbol_unregister(s)        inter_module_unregister(s)
111 #define cfs_symbol_get(s)               inter_module_get(s)
112 #define cfs_symbol_put(s)               inter_module_put(s)
113 #define cfs_module_get()                MOD_INC_USE_COUNT
114 #define cfs_module_put()                MOD_DEC_USE_COUNT
115 #else
116 #define cfs_symbol_register(s, p)       do {} while(0)
117 #define cfs_symbol_unregister(s)        do {} while(0)
118 #define cfs_symbol_get(s)               symbol_get(s)
119 #define cfs_symbol_put(s)               symbol_put(s)
120 #define cfs_module_get()                try_module_get(THIS_MODULE)
121 #define cfs_module_put()                module_put(THIS_MODULE)
122 #endif
123
124 /*
125  * Proc file system APIs
126  */
127 typedef read_proc_t                     cfs_read_proc_t;
128 typedef write_proc_t                    cfs_write_proc_t;
129 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
130 #define cfs_create_proc_entry(n, m, p)  create_proc_entry(n, m, p)
131 #define cfs_free_proc_entry(e)          free_proc_entry(e)
132 #define cfs_remove_proc_entry(n, e)     remove_proc_entry(n, e)
133
134 /*
135  * Wait Queue
136  */
137 #define CFS_TASK_INTERRUPTIBLE          TASK_INTERRUPTIBLE
138 #define CFS_TASK_UNINT                  TASK_UNINTERRUPTIBLE
139 #define CFS_TASK_RUNNING                TASK_RUNNING
140
141 #define cfs_set_current_state(state) set_current_state(state)
142
143 typedef wait_queue_t                    cfs_waitlink_t;
144 typedef wait_queue_head_t               cfs_waitq_t;
145 typedef long                            cfs_task_state_t;
146
147 #define cfs_kthread_run(fn, data, fmt, arg...) kthread_run(fn, data, fmt, ##arg)
148
149 /* Kernel thread */
150 typedef int (*cfs_thread_t)(void *);
151
152 static inline int cfs_kernel_thread(int (*fn)(void *),
153                                     void *arg, unsigned long flags)
154 {
155         void *orig_info = current->journal_info;
156         int rc;
157
158         current->journal_info = NULL;
159         rc = kernel_thread(fn, arg, flags);
160         current->journal_info = orig_info;
161         return rc;
162 }
163
164
165 /*
166  * Task struct
167  */
168 typedef struct task_struct              cfs_task_t;
169 #define cfs_current()                   current
170 #define cfs_task_lock(t)                task_lock(t)
171 #define cfs_task_unlock(t)              task_unlock(t)
172 #define CFS_DECL_JOURNAL_DATA           void *journal_info
173 #define CFS_PUSH_JOURNAL                do {    \
174         journal_info = current->journal_info;   \
175         current->journal_info = NULL;           \
176         } while(0)
177 #define CFS_POP_JOURNAL                 do {    \
178         current->journal_info = journal_info;   \
179         } while(0)
180
181 /* Module interfaces */
182 #define cfs_module(name, version, init, fini) \
183 module_init(init);                            \
184 module_exit(fini)
185
186 /*
187  * Signal
188  */
189 typedef sigset_t                        cfs_sigset_t;
190
191 /*
192  * Timer
193  */
194 typedef struct timer_list cfs_timer_t;
195
196 #define CFS_MAX_SCHEDULE_TIMEOUT MAX_SCHEDULE_TIMEOUT
197
198 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
199 #define __wait_event_timeout(wq, condition, timeout, ret)        \
200 do {                                                             \
201         int __ret = 0;                                           \
202         if (!(condition)) {                                      \
203                 wait_queue_t __wait;                             \
204                 unsigned long expire;                            \
205                                                                  \
206                 init_waitqueue_entry(&__wait, current);          \
207                 expire = timeout + jiffies;                      \
208                 add_wait_queue(&wq, &__wait);                    \
209                 for (;;) {                                       \
210                         set_current_state(TASK_UNINTERRUPTIBLE); \
211                         if (condition)                           \
212                                 break;                           \
213                         if (jiffies > expire) {                  \
214                                 ret = jiffies - expire;          \
215                                 break;                           \
216                         }                                        \
217                         schedule_timeout(timeout);               \
218                 }                                                \
219                 current->state = TASK_RUNNING;                   \
220                 remove_wait_queue(&wq, &__wait);                 \
221         }                                                        \
222 } while (0)
223 /*
224    retval == 0; condition met; we're good.
225    retval > 0; timed out.
226 */
227 #define cfs_waitq_wait_event_timeout(wq, condition, timeout, ret)    \
228 do {                                                                 \
229         ret = 0;                                                     \
230         if (!(condition))                                            \
231                 __wait_event_timeout(wq, condition, timeout, ret);   \
232 } while (0)
233 #else
234 #define cfs_waitq_wait_event_timeout(wq, condition, timeout, ret)    \
235         ret = wait_event_timeout(wq, condition, timeout)
236 #endif
237
238 #ifndef wait_event_interruptible_timeout /* Only for RHEL3 2.4.21 kernel */
239 #define __wait_event_interruptible_timeout(wq, condition, timeout, ret)   \
240 do {                                                           \
241         int __ret = 0;                                         \
242         if (!(condition)) {                                    \
243                 wait_queue_t __wait;                           \
244                 unsigned long expire;                          \
245                                                                \
246                 init_waitqueue_entry(&__wait, current);        \
247                 expire = timeout + jiffies;                    \
248                 add_wait_queue(&wq, &__wait);                  \
249                 for (;;) {                                     \
250                         set_current_state(TASK_INTERRUPTIBLE); \
251                         if (condition)                         \
252                                 break;                         \
253                         if (jiffies > expire) {                \
254                                 ret = jiffies - expire;        \
255                                 break;                         \
256                         }                                      \
257                         if (!signal_pending(current)) {        \
258                                 schedule_timeout(timeout);     \
259                                 continue;                      \
260                         }                                      \
261                         ret = -ERESTARTSYS;                    \
262                         break;                                 \
263                 }                                              \
264                 current->state = TASK_RUNNING;                 \
265                 remove_wait_queue(&wq, &__wait);               \
266         }                                                      \
267 } while (0)
268
269 /*
270    retval == 0; condition met; we're good.
271    retval < 0; interrupted by signal.
272    retval > 0; timed out.
273 */
274 #define cfs_waitq_wait_event_interruptible_timeout(wq, condition, timeout, ret)\
275 do {                                                              \
276         ret = 0;                                                  \
277         if (!(condition))                                         \
278                 __wait_event_interruptible_timeout(wq, condition, \
279                                                 timeout, ret);     \
280 } while (0)
281 #else
282 #define cfs_waitq_wait_event_interruptible_timeout(wq, c, timeout, ret) \
283         ret = wait_event_interruptible_timeout(wq, c, timeout)
284 #endif
285
286 /*
287  * atomic
288  */
289
290 typedef atomic_t cfs_atomic_t;
291
292 #define cfs_atomic_read(atom)         atomic_read(atom)
293 #define cfs_atomic_inc(atom)          atomic_inc(atom)
294 #define cfs_atomic_dec(atom)          atomic_dec(atom)
295 #define cfs_atomic_dec_and_test(atom) atomic_dec_and_test(atom)
296 #define cfs_atomic_set(atom, value)   atomic_set(atom, value)
297 #define cfs_atomic_add(value, atom)   atomic_add(value, atom)
298 #define cfs_atomic_sub(value, atom)   atomic_sub(value, atom)
299
300 /*
301  * membar
302  */
303
304 #define cfs_mb() mb()
305
306 /*
307  * interrupt
308  */
309
310 #define cfs_in_interrupt() in_interrupt()
311
312 #endif