1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
21 * Basic library routines.
25 #ifndef __LIBCFS_LINUX_CFS_PRIM_H__
26 #define __LIBCFS_LINUX_CFS_PRIM_H__
28 #ifndef __LIBCFS_LIBCFS_H__
29 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
33 #ifndef AUTOCONF_INCLUDED
34 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/version.h>
39 #include <linux/proc_fs.h>
41 #include <linux/timer.h>
42 #include <linux/signal.h>
43 #include <linux/sched.h>
45 #include <linux/miscdevice.h>
46 #include <libcfs/linux/portals_compat25.h>
47 #include <asm/div64.h>
49 #include <libcfs/linux/linux-time.h>
52 * Pseudo device register
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)
61 typedef struct ctl_table cfs_sysctl_table_t;
62 typedef struct ctl_table_header cfs_sysctl_table_header_t;
64 #ifdef HAVE_2ARGS_REGISTER_SYSCTL
65 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t, a)
67 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t)
69 #define cfs_unregister_sysctl_table(t) unregister_sysctl_table(t)
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
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)
91 * Proc file system APIs
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)
103 #define CFS_TASK_INTERRUPTIBLE TASK_INTERRUPTIBLE
104 #define CFS_TASK_UNINT TASK_UNINTERRUPTIBLE
106 typedef wait_queue_t cfs_waitlink_t;
107 typedef wait_queue_head_t cfs_waitq_t;
109 typedef long cfs_task_state_t;
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()
127 typedef int (*cfs_thread_t)(void *);
129 static inline int cfs_kernel_thread(int (*fn)(void *),
130 void *arg, unsigned long flags)
132 void *orig_info = current->journal_info;
135 current->journal_info = NULL;
136 rc = kernel_thread(fn, arg, flags);
137 current->journal_info = orig_info;
145 typedef struct task_struct cfs_task_t;
146 #define cfs_current() current
147 #define cfs_task_lock(t) task_lock(t)
148 #define cfs_task_unlock(t) task_unlock(t)
149 #define CFS_DECL_JOURNAL_DATA void *journal_info
150 #define CFS_PUSH_JOURNAL do { \
151 journal_info = current->journal_info; \
152 current->journal_info = NULL; \
154 #define CFS_POP_JOURNAL do { \
155 current->journal_info = journal_info; \
158 /* Module interfaces */
159 #define cfs_module(name, version, init, fini) \
166 typedef sigset_t cfs_sigset_t;
171 typedef struct timer_list cfs_timer_t;
172 typedef void (*timer_func_t)(unsigned long);
174 #define cfs_init_timer(t) init_timer(t)
176 static inline void cfs_timer_init(cfs_timer_t *t, void (*func)(unsigned long), void *arg)
179 t->function = (timer_func_t)func;
180 t->data = (unsigned long)arg;
183 static inline void cfs_timer_done(cfs_timer_t *t)
188 static inline void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
190 mod_timer(t, deadline);
193 static inline void cfs_timer_disarm(cfs_timer_t *t)
198 static inline int cfs_timer_is_armed(cfs_timer_t *t)
200 return timer_pending(t);
203 static inline cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
209 /* deschedule for a bit... */
210 static inline void cfs_pause(cfs_duration_t ticks)
212 set_current_state(TASK_UNINTERRUPTIBLE);
213 schedule_timeout(ticks);
216 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
217 #define __wait_event_timeout(wq, condition, timeout, ret) \
220 if (!(condition)) { \
221 wait_queue_t __wait; \
222 unsigned long expire; \
224 init_waitqueue_entry(&__wait, current); \
225 expire = timeout + jiffies; \
226 add_wait_queue(&wq, &__wait); \
228 set_current_state(TASK_UNINTERRUPTIBLE); \
231 if (jiffies > expire) { \
232 ret = jiffies - expire; \
235 schedule_timeout(timeout); \
237 current->state = TASK_RUNNING; \
238 remove_wait_queue(&wq, &__wait); \
242 retval == 0; condition met; we're good.
243 retval > 0; timed out.
245 #define cfs_waitq_wait_event_timeout(wq, condition, timeout) \
249 __wait_event_timeout(wq, condition, timeout, __ret); \
253 #define cfs_waitq_wait_event_timeout wait_event_timeout
256 #ifndef wait_event_interruptible_timeout /* Only for RHEL3 2.4.21 kernel */
257 #define __wait_event_interruptible_timeout(wq, condition, timeout, ret) \
260 if (!(condition)) { \
261 wait_queue_t __wait; \
262 unsigned long expire; \
264 init_waitqueue_entry(&__wait, current); \
265 expire = timeout + jiffies; \
266 add_wait_queue(&wq, &__wait); \
268 set_current_state(TASK_INTERRUPTIBLE); \
271 if (jiffies > expire) { \
272 ret = jiffies - expire; \
275 if (!signal_pending(current)) { \
276 schedule_timeout(timeout); \
279 ret = -ERESTARTSYS; \
282 current->state = TASK_RUNNING; \
283 remove_wait_queue(&wq, &__wait); \
288 retval == 0; condition met; we're good.
289 retval < 0; interrupted by signal.
290 retval > 0; timed out.
292 #define cfs_waitq_wait_event_interruptible_timeout(wq, condition, timeout) \
296 __wait_event_interruptible_timeout(wq, condition, \
301 #define cfs_waitq_wait_event_interruptible_timeout wait_event_interruptible_timeout
304 #else /* !__KERNEL__ */
306 typedef struct proc_dir_entry cfs_proc_dir_entry_t;
307 #include "../user-prim.h"
309 #endif /* __KERNEL__ */