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