Whamcloud - gitweb
41eeb8adeb068c113db058cf76c8e54866a8d36b
[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  *  Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  *
21  * Basic library routines.
22  *
23  */
24
25 #ifndef __LIBCFS_LINUX_CFS_PRIM_H__
26 #define __LIBCFS_LINUX_CFS_PRIM_H__
27
28 #ifndef __LIBCFS_LIBCFS_H__
29 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
30 #endif
31
32 #ifdef __KERNEL__
33 #ifdef HAVE_KERNEL_CONFIG_H
34 #include <linux/config.h>
35 #endif
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/version.h>
39 #include <linux/proc_fs.h>
40 #include <linux/mm.h>
41 #include <linux/timer.h>
42
43 #include <linux/miscdevice.h>
44 #include <libcfs/linux/portals_compat25.h>
45 #include <asm/div64.h>
46
47 #include <libcfs/linux/linux-time.h>
48
49 /*
50  * Pseudo device register
51  */
52 typedef struct miscdevice               cfs_psdev_t;
53 #define cfs_psdev_register(dev)         misc_register(dev)
54 #define cfs_psdev_deregister(dev)       misc_deregister(dev)
55
56 /*
57  * Sysctl register
58  */
59 typedef struct ctl_table                cfs_sysctl_table_t;
60 typedef struct ctl_table_header         cfs_sysctl_table_header_t;
61
62 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t, a)
63 #define cfs_unregister_sysctl_table(t)  unregister_sysctl_table(t, a)
64
65 /*
66  * Symbol register
67  */
68 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
69 #define cfs_symbol_register(s, p)       inter_module_register(s, THIS_MODULE, p)
70 #define cfs_symbol_unregister(s)        inter_module_unregister(s)
71 #define cfs_symbol_get(s)               inter_module_get(s)
72 #define cfs_symbol_put(s)               inter_module_put(s)
73 #define cfs_module_get()                MOD_INC_USE_COUNT
74 #define cfs_module_put()                MOD_DEC_USE_COUNT
75 #else
76 #define cfs_symbol_register(s, p)       do {} while(0)
77 #define cfs_symbol_unregister(s)        do {} while(0)
78 #define cfs_symbol_get(s)               symbol_get(s)
79 #define cfs_symbol_put(s)               symbol_put(s)
80 #define cfs_module_get()                try_module_get(THIS_MODULE)
81 #define cfs_module_put()                module_put(THIS_MODULE)
82 #endif
83
84 /*
85  * Proc file system APIs
86  */
87 typedef read_proc_t                     cfs_read_proc_t;
88 typedef write_proc_t                    cfs_write_proc_t;
89 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
90 #define cfs_create_proc_entry(n, m, p)  create_proc_entry(n, m, p)
91 #define cfs_free_proc_entry(e)          free_proc_entry(e)
92 #define cfs_remove_proc_entry(n, e)     remove_proc_entry(n, e)
93
94 /*
95  * Wait Queue
96  */
97 #define CFS_TASK_INTERRUPTIBLE          TASK_INTERRUPTIBLE
98 #define CFS_TASK_UNINT                  TASK_UNINTERRUPTIBLE
99
100 typedef wait_queue_t                    cfs_waitlink_t;
101 typedef wait_queue_head_t               cfs_waitq_t;
102
103 typedef long                            cfs_task_state_t;
104
105 #define cfs_waitq_init(w)               init_waitqueue_head(w)
106 #define cfs_waitlink_init(l)            init_waitqueue_entry(l, current)
107 #define cfs_waitq_add(w, l)             add_wait_queue(w, l)
108 #define cfs_waitq_add_exclusive(w, l)   add_wait_queue_exclusive(w, l)
109 #define cfs_waitq_forward(l, w)         do {} while(0)
110 #define cfs_waitq_del(w, l)             remove_wait_queue(w, l)
111 #define cfs_waitq_active(w)             waitqueue_active(w)
112 #define cfs_waitq_signal(w)             wake_up(w)
113 #define cfs_waitq_signal_nr(w,n)        wake_up_nr(w, n)
114 #define cfs_waitq_broadcast(w)          wake_up_all(w)
115 #define cfs_waitq_wait(l, s)            schedule()
116 #define cfs_waitq_timedwait(l, s, t)    schedule_timeout(t)
117 #define cfs_schedule_timeout(s, t)      schedule_timeout(t)
118 #define cfs_schedule()                  schedule()
119
120 /* Kernel thread */
121 typedef int (*cfs_thread_t)(void *);
122 #define cfs_kernel_thread(func, a, f)   kernel_thread(func, a, f)
123
124 /*
125  * Task struct
126  */
127 typedef struct task_struct              cfs_task_t;
128 #define cfs_current()                   current
129 #define cfs_task_lock(t)                task_lock(t)
130 #define cfs_task_unlock(t)              task_unlock(t)
131 #define CFS_DECL_JOURNAL_DATA           void *journal_info
132 #define CFS_PUSH_JOURNAL                do {    \
133         journal_info = current->journal_info;   \
134         current->journal_info = NULL;           \
135         } while(0)
136 #define CFS_POP_JOURNAL                 do {    \
137         current->journal_info = journal_info;   \
138         } while(0)
139
140 /* Module interfaces */
141 #define cfs_module(name, version, init, fini) \
142 module_init(init);                            \
143 module_exit(fini)
144
145 /*
146  * Signal
147  */
148 typedef sigset_t                        cfs_sigset_t;
149
150 /*
151  * Timer
152  */
153 typedef struct timer_list cfs_timer_t;
154 typedef  void (*timer_func_t)(unsigned long);
155
156 #define cfs_init_timer(t)       init_timer(t)
157
158 static inline void cfs_timer_init(cfs_timer_t *t, void (*func)(unsigned long), void *arg)
159 {
160         init_timer(t);
161         t->function = (timer_func_t)func;
162         t->data = (unsigned long)arg;
163 }
164
165 static inline void cfs_timer_done(cfs_timer_t *t)
166 {
167         return;
168 }
169
170 static inline void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
171 {
172         mod_timer(t, deadline);
173 }
174
175 static inline void cfs_timer_disarm(cfs_timer_t *t)
176 {
177         del_timer(t);
178 }
179
180 static inline int  cfs_timer_is_armed(cfs_timer_t *t)
181 {
182         return timer_pending(t);
183 }
184
185 static inline cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
186 {
187         return t->expires;
188 }
189
190
191 /* deschedule for a bit... */
192 static inline void cfs_pause(cfs_duration_t ticks)
193 {
194         set_current_state(TASK_UNINTERRUPTIBLE);
195         schedule_timeout(ticks);
196 }
197
198 #else   /* !__KERNEL__ */
199
200 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
201 #include "../user-prim.h"
202
203 #endif /* __KERNEL__ */
204
205 #endif