Whamcloud - gitweb
Branch b1_8
[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  * 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  * lnet/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 #include <linux/kthread.h>
61
62 #include <linux/miscdevice.h>
63 #include <libcfs/linux/portals_compat25.h>
64 #include <asm/div64.h>
65
66 #include <libcfs/linux/linux-time.h>
67
68 /*
69  * Pseudo device register
70  */
71 typedef struct miscdevice               cfs_psdev_t;
72 #define cfs_psdev_register(dev)         misc_register(dev)
73 #define cfs_psdev_deregister(dev)       misc_deregister(dev)
74
75 /*
76  * Sysctl register
77  */
78 typedef struct ctl_table                cfs_sysctl_table_t;
79 typedef struct ctl_table_header         cfs_sysctl_table_header_t;
80
81 #ifdef HAVE_2ARGS_REGISTER_SYSCTL
82 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t, a)
83 #else
84 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t)
85 #endif
86 #define cfs_unregister_sysctl_table(t)  unregister_sysctl_table(t)
87
88 #define DECLARE_PROC_HANDLER(name)                      \
89 static int                                              \
90 LL_PROC_PROTO(name)                                     \
91 {                                                       \
92         DECLARE_LL_PROC_PPOS_DECL;                      \
93                                                         \
94         return proc_call_handler(table->data, write,    \
95                                  ppos, buffer, lenp,    \
96                                  __##name);             \
97 }
98
99 /*
100  * Symbol register
101  */
102 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
103 #define cfs_symbol_register(s, p)       inter_module_register(s, THIS_MODULE, p)
104 #define cfs_symbol_unregister(s)        inter_module_unregister(s)
105 #define cfs_symbol_get(s)               inter_module_get(s)
106 #define cfs_symbol_put(s)               inter_module_put(s)
107 #define cfs_module_get()                MOD_INC_USE_COUNT
108 #define cfs_module_put()                MOD_DEC_USE_COUNT
109 #else
110 #define cfs_symbol_register(s, p)       do {} while(0)
111 #define cfs_symbol_unregister(s)        do {} while(0)
112 #define cfs_symbol_get(s)               symbol_get(s)
113 #define cfs_symbol_put(s)               symbol_put(s)
114 #define cfs_module_get()                try_module_get(THIS_MODULE)
115 #define cfs_module_put()                module_put(THIS_MODULE)
116 #endif
117
118 /*
119  * Proc file system APIs
120  */
121 typedef read_proc_t                     cfs_read_proc_t;
122 typedef write_proc_t                    cfs_write_proc_t;
123 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
124 #define cfs_create_proc_entry(n, m, p)  create_proc_entry(n, m, p)
125 #define cfs_free_proc_entry(e)          free_proc_entry(e)
126 #define cfs_remove_proc_entry(n, e)     remove_proc_entry(n, e)
127
128 /*
129  * Wait Queue
130  */
131 #define CFS_TASK_INTERRUPTIBLE          TASK_INTERRUPTIBLE
132 #define CFS_TASK_UNINT                  TASK_UNINTERRUPTIBLE
133 #define CFS_TASK_RUNNING                TASK_RUNNING
134
135 #define cfs_set_current_state(state) set_current_state(state)
136
137 typedef wait_queue_t                    cfs_waitlink_t;
138 typedef wait_queue_head_t               cfs_waitq_t;
139
140 typedef long                            cfs_task_state_t;
141
142 #define cfs_waitq_init(w)               init_waitqueue_head(w)
143 #define cfs_waitlink_init(l)            init_waitqueue_entry(l, current)
144 #define cfs_waitq_add(w, l)             add_wait_queue(w, l)
145 #define cfs_waitq_add_exclusive(w, l)   add_wait_queue_exclusive(w, l)
146 #define cfs_waitq_forward(l, w)         do {} while(0)
147 #define cfs_waitq_del(w, l)             remove_wait_queue(w, l)
148 #define cfs_waitq_active(w)             waitqueue_active(w)
149 #define cfs_waitq_signal(w)             wake_up(w)
150 #define cfs_waitq_signal_nr(w,n)        wake_up_nr(w, n)
151 #define cfs_waitq_broadcast(w)          wake_up_all(w)
152 #define cfs_waitq_wait(l, s)            schedule()
153 #define cfs_waitq_timedwait(l, s, t)    schedule_timeout(t)
154 #define cfs_schedule_timeout(s, t)      schedule_timeout(t)
155 #define cfs_schedule()                  schedule()
156 #define cfs_kthread_run(fn, data, fmt, arg...) kthread_run(fn, data, fmt, ##arg)
157
158 /* Kernel thread */
159 typedef int (*cfs_thread_t)(void *);
160
161 static inline int cfs_kernel_thread(int (*fn)(void *),
162                                     void *arg, unsigned long flags)
163 {
164         void *orig_info = current->journal_info;
165         int rc;
166
167         current->journal_info = NULL;
168         rc = kernel_thread(fn, arg, flags);
169         current->journal_info = orig_info;
170         return rc;
171 }
172
173 #define CFS_MAX_SCHEDULE_TIMEOUT MAX_SCHEDULE_TIMEOUT
174
175 /*
176  * Task struct
177  */
178 typedef struct task_struct              cfs_task_t;
179 #define cfs_current()                   current
180 #define cfs_task_lock(t)                task_lock(t)
181 #define cfs_task_unlock(t)              task_unlock(t)
182 #define CFS_DECL_JOURNAL_DATA           void *journal_info
183 #define CFS_PUSH_JOURNAL                do {    \
184         journal_info = current->journal_info;   \
185         current->journal_info = NULL;           \
186         } while(0)
187 #define CFS_POP_JOURNAL                 do {    \
188         current->journal_info = journal_info;   \
189         } while(0)
190
191 /* Module interfaces */
192 #define cfs_module(name, version, init, fini) \
193 module_init(init);                            \
194 module_exit(fini)
195
196 /*
197  * Signal
198  */
199 typedef sigset_t                        cfs_sigset_t;
200
201 /*
202  * Timer
203  */
204 typedef struct timer_list cfs_timer_t;
205 typedef  void (*timer_func_t)(unsigned long);
206
207 #define cfs_init_timer(t)       init_timer(t)
208
209 static inline void cfs_timer_init(cfs_timer_t *t, void (*func)(unsigned long), void *arg)
210 {
211         init_timer(t);
212         t->function = (timer_func_t)func;
213         t->data = (unsigned long)arg;
214 }
215
216 static inline void cfs_timer_done(cfs_timer_t *t)
217 {
218         return;
219 }
220
221 static inline void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
222 {
223         mod_timer(t, deadline);
224 }
225
226 static inline void cfs_timer_disarm(cfs_timer_t *t)
227 {
228         del_timer(t);
229 }
230
231 static inline int  cfs_timer_is_armed(cfs_timer_t *t)
232 {
233         return timer_pending(t);
234 }
235
236 static inline cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
237 {
238         return t->expires;
239 }
240
241 #define CFS_MAX_SCHEDULE_TIMEOUT MAX_SCHEDULE_TIMEOUT
242
243 /* deschedule for a bit... */
244 static inline void cfs_pause(cfs_duration_t ticks)
245 {
246         set_current_state(TASK_UNINTERRUPTIBLE);
247         schedule_timeout(ticks);
248 }
249
250 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
251 #define __wait_event_timeout(wq, condition, timeout, ret)        \
252 do {                                                             \
253         int __ret = 0;                                           \
254         if (!(condition)) {                                      \
255                 wait_queue_t __wait;                             \
256                 unsigned long expire;                            \
257                                                                  \
258                 init_waitqueue_entry(&__wait, current);          \
259                 expire = timeout + jiffies;                      \
260                 add_wait_queue(&wq, &__wait);                    \
261                 for (;;) {                                       \
262                         set_current_state(TASK_UNINTERRUPTIBLE); \
263                         if (condition)                           \
264                                 break;                           \
265                         if (jiffies > expire) {                  \
266                                 ret = jiffies - expire;          \
267                                 break;                           \
268                         }                                        \
269                         schedule_timeout(timeout);               \
270                 }                                                \
271                 current->state = TASK_RUNNING;                   \
272                 remove_wait_queue(&wq, &__wait);                 \
273         }                                                        \
274 } while (0)
275 /*
276    retval == 0; condition met; we're good.
277    retval > 0; timed out.
278 */
279 #define cfs_waitq_wait_event_timeout(wq, condition, timeout)         \
280 ({                                                                   \
281         int __ret = 0;                                               \
282         if (!(condition))                                            \
283                 __wait_event_timeout(wq, condition, timeout, __ret); \
284         __ret;                                                       \
285 })
286 #else
287 #define cfs_waitq_wait_event_timeout  wait_event_timeout
288 #endif
289
290 #ifndef wait_event_interruptible_timeout /* Only for RHEL3 2.4.21 kernel */
291 #define __wait_event_interruptible_timeout(wq, condition, timeout, ret)   \
292 do {                                                           \
293         int __ret = 0;                                         \
294         if (!(condition)) {                                    \
295                 wait_queue_t __wait;                           \
296                 unsigned long expire;                          \
297                                                                \
298                 init_waitqueue_entry(&__wait, current);        \
299                 expire = timeout + jiffies;                    \
300                 add_wait_queue(&wq, &__wait);                  \
301                 for (;;) {                                     \
302                         set_current_state(TASK_INTERRUPTIBLE); \
303                         if (condition)                         \
304                                 break;                         \
305                         if (jiffies > expire) {                \
306                                 ret = jiffies - expire;        \
307                                 break;                         \
308                         }                                      \
309                         if (!signal_pending(current)) {        \
310                                 schedule_timeout(timeout);     \
311                                 continue;                      \
312                         }                                      \
313                         ret = -ERESTARTSYS;                    \
314                         break;                                 \
315                 }                                              \
316                 current->state = TASK_RUNNING;                 \
317                 remove_wait_queue(&wq, &__wait);               \
318         }                                                      \
319 } while (0)
320
321 /*
322    retval == 0; condition met; we're good.
323    retval < 0; interrupted by signal.
324    retval > 0; timed out.
325 */
326 #define cfs_waitq_wait_event_interruptible_timeout(wq, condition, timeout) \
327 ({                                                                \
328         int __ret = 0;                                            \
329         if (!(condition))                                         \
330                 __wait_event_interruptible_timeout(wq, condition, \
331                                                 timeout, __ret);  \
332         __ret;                                                    \
333 })
334 #else
335 #define cfs_waitq_wait_event_interruptible_timeout wait_event_interruptible_timeout
336 #endif
337
338 #define cfs_wait_event_interruptible_exclusive(wq, condition, rc)       \
339 ({                                                                      \
340         rc = wait_event_interruptible_exclusive(wq, condition);         \
341 })
342
343 /*
344  * atomic
345  */
346
347 typedef atomic_t cfs_atomic_t;
348
349 #define cfs_atomic_read(atom)         atomic_read(atom)
350 #define cfs_atomic_inc(atom)          atomic_inc(atom)
351 #define cfs_atomic_dec(atom)          atomic_dec(atom)
352 #define cfs_atomic_dec_and_test(atom) atomic_dec_and_test(atom)
353 #define cfs_atomic_set(atom, value)   atomic_set(atom, value)
354 #define cfs_atomic_add(value, atom)   atomic_add(value, atom)
355 #define cfs_atomic_sub(value, atom)   atomic_sub(value, atom)
356
357 /*
358  * membar
359  */
360
361 #define cfs_mb() mb()
362
363 /*
364  * interrupt
365  */
366
367 #define cfs_in_interrupt() in_interrupt()
368
369 #else   /* !__KERNEL__ */
370
371 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
372 #include "../user-prim.h"
373
374 #endif /* __KERNEL__ */
375
376 #endif