1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/include/libcfs/linux/linux-prim.h
38 * Basic library routines.
41 #ifndef __LIBCFS_LINUX_CFS_PRIM_H__
42 #define __LIBCFS_LINUX_CFS_PRIM_H__
44 #ifndef __LIBCFS_LIBCFS_H__
45 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
49 #ifndef AUTOCONF_INCLUDED
50 #include <linux/config.h>
52 #include <linux/module.h>
53 #include <linux/kernel.h>
54 #include <linux/version.h>
55 #include <linux/proc_fs.h>
57 #include <linux/timer.h>
58 #include <linux/signal.h>
59 #include <linux/sched.h>
61 #include <linux/miscdevice.h>
62 #include <libcfs/linux/portals_compat25.h>
63 #include <asm/div64.h>
65 #include <libcfs/linux/linux-time.h>
68 * Pseudo device register
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)
77 typedef struct ctl_table cfs_sysctl_table_t;
78 typedef struct ctl_table_header cfs_sysctl_table_header_t;
80 #ifdef HAVE_2ARGS_REGISTER_SYSCTL
81 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t, a)
83 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t)
85 #define cfs_unregister_sysctl_table(t) unregister_sysctl_table(t)
87 #define DECLARE_PROC_HANDLER(name) \
91 DECLARE_LL_PROC_PPOS_DECL; \
93 return proc_call_handler(table->data, write, \
101 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
102 #define cfs_symbol_register(s, p) inter_module_register(s, THIS_MODULE, p)
103 #define cfs_symbol_unregister(s) inter_module_unregister(s)
104 #define cfs_symbol_get(s) inter_module_get(s)
105 #define cfs_symbol_put(s) inter_module_put(s)
106 #define cfs_module_get() MOD_INC_USE_COUNT
107 #define cfs_module_put() MOD_DEC_USE_COUNT
109 #define cfs_symbol_register(s, p) do {} while(0)
110 #define cfs_symbol_unregister(s) do {} while(0)
111 #define cfs_symbol_get(s) symbol_get(s)
112 #define cfs_symbol_put(s) symbol_put(s)
113 #define cfs_module_get() try_module_get(THIS_MODULE)
114 #define cfs_module_put() module_put(THIS_MODULE)
118 * Proc file system APIs
120 typedef read_proc_t cfs_read_proc_t;
121 typedef write_proc_t cfs_write_proc_t;
122 typedef struct proc_dir_entry cfs_proc_dir_entry_t;
123 #define cfs_create_proc_entry(n, m, p) create_proc_entry(n, m, p)
124 #define cfs_free_proc_entry(e) free_proc_entry(e)
125 #define cfs_remove_proc_entry(n, e) remove_proc_entry(n, e)
130 #define CFS_TASK_INTERRUPTIBLE TASK_INTERRUPTIBLE
131 #define CFS_TASK_UNINT TASK_UNINTERRUPTIBLE
132 #define CFS_TASK_RUNNING TASK_RUNNING
134 #define cfs_set_current_state(state) set_current_state(state)
136 typedef wait_queue_t cfs_waitlink_t;
137 typedef wait_queue_head_t cfs_waitq_t;
139 typedef long cfs_task_state_t;
141 #define cfs_waitq_init(w) init_waitqueue_head(w)
142 #define cfs_waitlink_init(l) init_waitqueue_entry(l, current)
143 #define cfs_waitq_add(w, l) add_wait_queue(w, l)
144 #define cfs_waitq_add_exclusive(w, l) add_wait_queue_exclusive(w, l)
145 #define cfs_waitq_forward(l, w) do {} while(0)
146 #define cfs_waitq_del(w, l) remove_wait_queue(w, l)
147 #define cfs_waitq_active(w) waitqueue_active(w)
148 #define cfs_waitq_signal(w) wake_up(w)
149 #define cfs_waitq_signal_nr(w,n) wake_up_nr(w, n)
150 #define cfs_waitq_broadcast(w) wake_up_all(w)
151 #define cfs_waitq_wait(l, s) schedule()
152 #define cfs_waitq_timedwait(l, s, t) schedule_timeout(t)
153 #define cfs_schedule_timeout(s, t) schedule_timeout(t)
154 #define cfs_schedule() schedule()
157 typedef int (*cfs_thread_t)(void *);
159 static inline int cfs_kernel_thread(int (*fn)(void *),
160 void *arg, unsigned long flags)
162 void *orig_info = current->journal_info;
165 current->journal_info = NULL;
166 rc = kernel_thread(fn, arg, flags);
167 current->journal_info = orig_info;
175 typedef struct task_struct cfs_task_t;
176 #define cfs_current() current
177 #define cfs_task_lock(t) task_lock(t)
178 #define cfs_task_unlock(t) task_unlock(t)
179 #define CFS_DECL_JOURNAL_DATA void *journal_info
180 #define CFS_PUSH_JOURNAL do { \
181 journal_info = current->journal_info; \
182 current->journal_info = NULL; \
184 #define CFS_POP_JOURNAL do { \
185 current->journal_info = journal_info; \
188 /* Module interfaces */
189 #define cfs_module(name, version, init, fini) \
196 typedef sigset_t cfs_sigset_t;
201 typedef struct timer_list cfs_timer_t;
202 typedef void (*timer_func_t)(unsigned long);
204 #define cfs_init_timer(t) init_timer(t)
206 static inline void cfs_timer_init(cfs_timer_t *t, void (*func)(unsigned long), void *arg)
209 t->function = (timer_func_t)func;
210 t->data = (unsigned long)arg;
213 static inline void cfs_timer_done(cfs_timer_t *t)
218 static inline void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
220 mod_timer(t, deadline);
223 static inline void cfs_timer_disarm(cfs_timer_t *t)
228 static inline int cfs_timer_is_armed(cfs_timer_t *t)
230 return timer_pending(t);
233 static inline cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
239 /* deschedule for a bit... */
240 static inline void cfs_pause(cfs_duration_t ticks)
242 set_current_state(TASK_UNINTERRUPTIBLE);
243 schedule_timeout(ticks);
246 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
247 #define __wait_event_timeout(wq, condition, timeout, ret) \
250 if (!(condition)) { \
251 wait_queue_t __wait; \
252 unsigned long expire; \
254 init_waitqueue_entry(&__wait, current); \
255 expire = timeout + jiffies; \
256 add_wait_queue(&wq, &__wait); \
258 set_current_state(TASK_UNINTERRUPTIBLE); \
261 if (jiffies > expire) { \
262 ret = jiffies - expire; \
265 schedule_timeout(timeout); \
267 current->state = TASK_RUNNING; \
268 remove_wait_queue(&wq, &__wait); \
272 retval == 0; condition met; we're good.
273 retval > 0; timed out.
275 #define cfs_waitq_wait_event_timeout(wq, condition, timeout) \
279 __wait_event_timeout(wq, condition, timeout, __ret); \
283 #define cfs_waitq_wait_event_timeout wait_event_timeout
286 #ifndef wait_event_interruptible_timeout /* Only for RHEL3 2.4.21 kernel */
287 #define __wait_event_interruptible_timeout(wq, condition, timeout, ret) \
290 if (!(condition)) { \
291 wait_queue_t __wait; \
292 unsigned long expire; \
294 init_waitqueue_entry(&__wait, current); \
295 expire = timeout + jiffies; \
296 add_wait_queue(&wq, &__wait); \
298 set_current_state(TASK_INTERRUPTIBLE); \
301 if (jiffies > expire) { \
302 ret = jiffies - expire; \
305 if (!signal_pending(current)) { \
306 schedule_timeout(timeout); \
309 ret = -ERESTARTSYS; \
312 current->state = TASK_RUNNING; \
313 remove_wait_queue(&wq, &__wait); \
318 retval == 0; condition met; we're good.
319 retval < 0; interrupted by signal.
320 retval > 0; timed out.
322 #define cfs_waitq_wait_event_interruptible_timeout(wq, condition, timeout) \
326 __wait_event_interruptible_timeout(wq, condition, \
331 #define cfs_waitq_wait_event_interruptible_timeout wait_event_interruptible_timeout
334 #define cfs_wait_event_interruptible_exclusive(wq, condition, rc) \
336 rc = wait_event_interruptible_exclusive(wq, condition); \
343 typedef atomic_t cfs_atomic_t;
345 #define cfs_atomic_read(atom) atomic_read(atom)
346 #define cfs_atomic_inc(atom) atomic_inc(atom)
347 #define cfs_atomic_dec(atom) atomic_dec(atom)
348 #define cfs_atomic_dec_and_test(atom) atomic_dec_and_test(atom)
349 #define cfs_atomic_set(atom, value) atomic_set(atom, value)
350 #define cfs_atomic_add(value, atom) atomic_add(value, atom)
351 #define cfs_atomic_sub(value, atom) atomic_sub(value, atom)
357 #define cfs_mb() mb()
363 #define cfs_in_interrupt() in_interrupt()
365 #else /* !__KERNEL__ */
367 typedef struct proc_dir_entry cfs_proc_dir_entry_t;
368 #include "../user-prim.h"
370 #endif /* __KERNEL__ */