Whamcloud - gitweb
534a7dfa75af465998988574803e4656a9ac603a
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-prim.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/include/libcfs/linux/linux-prim.h
35  *
36  * Basic library routines.
37  */
38
39 #ifndef __LIBCFS_LINUX_CFS_PRIM_H__
40 #define __LIBCFS_LINUX_CFS_PRIM_H__
41
42 #ifndef __LIBCFS_LIBCFS_H__
43 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
44 #endif
45
46 #ifndef __KERNEL__
47 #error This include is only for kernel use.
48 #endif
49
50 #ifndef AUTOCONF_INCLUDED
51 #include <linux/config.h>
52 #endif
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/kernel.h>
56 #include <linux/version.h>
57 #include <linux/proc_fs.h>
58 #include <linux/mm.h>
59 #include <linux/timer.h>
60 #include <linux/signal.h>
61 #include <linux/sched.h>
62 #include <linux/kthread.h>
63 #ifdef HAVE_LINUX_RANDOM_H
64 #include <linux/random.h>
65 #endif
66
67 #include <linux/miscdevice.h>
68 #include <libcfs/linux/portals_compat25.h>
69 #include <asm/div64.h>
70
71 #include <libcfs/linux/linux-time.h>
72
73 #define CFS_KERN_EMERG   KERN_EMERG
74 #define CFS_KERN_ALERT   KERN_ALERT
75 #define CFS_KERN_CRIT    KERN_CRIT
76 #define CFS_KERN_ERR     KERN_ERR
77 #define CFS_KERN_WARNING KERN_WARNING
78 #define CFS_KERN_NOTICE  KERN_NOTICE
79 #define CFS_KERN_INFO    KERN_INFO
80 #define CFS_KERN_DEBUG   KERN_DEBUG
81
82 /*
83  * CPU
84  */
85 #ifdef for_each_possible_cpu
86 #define cfs_for_each_possible_cpu(cpu) for_each_possible_cpu(cpu)
87 #elif defined(for_each_cpu)
88 #define cfs_for_each_possible_cpu(cpu) for_each_cpu(cpu)
89 #endif
90
91 #ifdef NR_CPUS
92 #define CFS_NR_CPUS     NR_CPUS
93 #else
94 #define CFS_NR_CPUS     1
95 #endif
96
97 #ifdef HAVE_SET_CPUS_ALLOWED
98 #define cfs_set_cpus_allowed(t, mask)  set_cpus_allowed(t, mask)
99 #else
100 #define cfs_set_cpus_allowed(t, mask)  set_cpus_allowed_ptr(t, &(mask))
101 #endif
102
103 /*
104  * cache
105  */
106 #define CFS_L1_CACHE_ALIGN(x)           L1_CACHE_ALIGN(x)
107
108 /*
109  * IRQs
110  */
111 #define CFS_NR_IRQS                     NR_IRQS
112
113 #define CFS_EXPORT_SYMBOL(s)            EXPORT_SYMBOL(s)
114
115 /*
116  * Pseudo device register
117  */
118 typedef struct miscdevice               cfs_psdev_t;
119 #define cfs_psdev_register(dev)         misc_register(dev)
120 #define cfs_psdev_deregister(dev)       misc_deregister(dev)
121
122 /*
123  * Sysctl register
124  */
125 typedef struct ctl_table                cfs_sysctl_table_t;
126 typedef struct ctl_table_header         cfs_sysctl_table_header_t;
127
128 #ifdef HAVE_2ARGS_REGISTER_SYSCTL
129 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t, a)
130 #else
131 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t)
132 #endif
133 #define cfs_unregister_sysctl_table(t)  unregister_sysctl_table(t)
134
135 #define DECLARE_PROC_HANDLER(name)                      \
136 static int                                              \
137 LL_PROC_PROTO(name)                                     \
138 {                                                       \
139         DECLARE_LL_PROC_PPOS_DECL;                      \
140                                                         \
141         return proc_call_handler(table->data, write,    \
142                                  ppos, buffer, lenp,    \
143                                  __##name);             \
144 }
145
146 /*
147  * Symbol register
148  */
149 #define cfs_symbol_register(s, p)       do {} while(0)
150 #define cfs_symbol_unregister(s)        do {} while(0)
151 #define cfs_symbol_get(s)               symbol_get(s)
152 #define cfs_symbol_put(s)               symbol_put(s)
153 #define cfs_module_get()                try_module_get(THIS_MODULE)
154 #define cfs_try_module_get(m)           try_module_get(m)
155 #define __cfs_module_get(m)             __module_get(m)
156 #define cfs_module_put(m)               module_put(m)
157 #define cfs_module_refcount(m)          module_refcount(m)
158
159 typedef struct module cfs_module_t;
160
161 /*
162  * Proc file system APIs
163  */
164 typedef read_proc_t                     cfs_read_proc_t;
165 typedef write_proc_t                    cfs_write_proc_t;
166 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
167 #define cfs_create_proc_entry(n, m, p)  create_proc_entry(n, m, p)
168 #define cfs_free_proc_entry(e)          free_proc_entry(e)
169 #define cfs_remove_proc_entry(n, e)     remove_proc_entry(n, e)
170
171 /*
172  * Wait Queue
173  */
174 #define CFS_TASK_INTERRUPTIBLE          TASK_INTERRUPTIBLE
175 #define CFS_TASK_UNINT                  TASK_UNINTERRUPTIBLE
176 #define CFS_TASK_RUNNING                TASK_RUNNING
177
178 #define cfs_set_current_state(state)    set_current_state(state)
179 #define cfs_wait_event(wq, cond)        wait_event(wq, cond)
180
181 typedef wait_queue_t                    cfs_waitlink_t;
182 typedef wait_queue_head_t               cfs_waitq_t;
183 typedef long                            cfs_task_state_t;
184
185 #define cfs_kthread_run(fn, data, fmt, arg...) kthread_run(fn, data, fmt, ##arg)
186
187 /* Kernel thread */
188 typedef int (*cfs_thread_t)(void *);
189
190 #define CFS_DAEMON_FLAGS (CLONE_VM | CLONE_FILES)
191 extern int cfs_create_thread(int (*fn)(void *),
192                              void *arg, unsigned long flags);
193
194 /*
195  * Task struct
196  */
197 typedef struct task_struct              cfs_task_t;
198 #define cfs_current()                   current
199 #define cfs_task_lock(t)                task_lock(t)
200 #define cfs_task_unlock(t)              task_unlock(t)
201 #define CFS_DECL_JOURNAL_DATA           void *journal_info
202 #define CFS_PUSH_JOURNAL                do {    \
203         journal_info = current->journal_info;   \
204         current->journal_info = NULL;           \
205         } while(0)
206 #define CFS_POP_JOURNAL                 do {    \
207         current->journal_info = journal_info;   \
208         } while(0)
209
210 /* Module interfaces */
211 #define cfs_module(name, version, init, fini) \
212         module_init(init);                    \
213         module_exit(fini)
214 #define cfs_request_module              request_module
215
216 /*
217  * Signal
218  */
219 typedef sigset_t                        cfs_sigset_t;
220
221 /*
222  * Timer
223  */
224 typedef struct timer_list cfs_timer_t;
225
226 #define CFS_MAX_SCHEDULE_TIMEOUT MAX_SCHEDULE_TIMEOUT
227
228 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
229 #define __wait_event_timeout(wq, condition, timeout, ret)        \
230 do {                                                             \
231         int __ret = 0;                                           \
232         if (!(condition)) {                                      \
233                 wait_queue_t __wait;                             \
234                 unsigned long expire;                            \
235                                                                  \
236                 init_waitqueue_entry(&__wait, current);          \
237                 expire = timeout + jiffies;                      \
238                 add_wait_queue(&wq, &__wait);                    \
239                 for (;;) {                                       \
240                         set_current_state(TASK_UNINTERRUPTIBLE); \
241                         if (condition)                           \
242                                 break;                           \
243                         if (jiffies > expire) {                  \
244                                 ret = jiffies - expire;          \
245                                 break;                           \
246                         }                                        \
247                         schedule_timeout(timeout);               \
248                 }                                                \
249                 current->state = TASK_RUNNING;                   \
250                 remove_wait_queue(&wq, &__wait);                 \
251         }                                                        \
252 } while (0)
253 /*
254    retval == 0; condition met; we're good.
255    retval > 0; timed out.
256 */
257 #define cfs_waitq_wait_event_timeout(wq, condition, timeout, ret)    \
258 do {                                                                 \
259         ret = 0;                                                     \
260         if (!(condition))                                            \
261                 __wait_event_timeout(wq, condition, timeout, ret);   \
262 } while (0)
263 #else
264 #define cfs_waitq_wait_event_timeout(wq, condition, timeout, ret)    \
265         ret = wait_event_timeout(wq, condition, timeout)
266 #endif
267
268 #define cfs_waitq_wait_event_interruptible_timeout(wq, c, timeout, ret) \
269         ret = wait_event_interruptible_timeout(wq, c, timeout)
270
271 /*
272  * atomic
273  */
274
275 typedef atomic_t cfs_atomic_t;
276
277 #define cfs_atomic_read(atom)                atomic_read(atom)
278 #define cfs_atomic_inc(atom)                 atomic_inc(atom)
279 #define cfs_atomic_inc_and_test(atom)        atomic_inc_and_test(atom)
280 #define cfs_atomic_inc_return(atom)          atomic_inc_return(atom)
281 #define cfs_atomic_inc_not_zero(atom)        atomic_inc_not_zero(atom)
282 #define cfs_atomic_dec(atom)                 atomic_dec(atom)
283 #define cfs_atomic_dec_and_test(atom)        atomic_dec_and_test(atom)
284 #define cfs_atomic_dec_and_lock(atom, lock)  atomic_dec_and_lock(atom, lock)
285 #define cfs_atomic_dec_return(atom)          atomic_dec_return(atom)
286 #define cfs_atomic_set(atom, value)          atomic_set(atom, value)
287 #define cfs_atomic_add(value, atom)          atomic_add(value, atom)
288 #define cfs_atomic_add_return(value, atom)   atomic_add_return(value, atom)
289 #define cfs_atomic_sub(value, atom)          atomic_sub(value, atom)
290 #define cfs_atomic_sub_and_test(value, atom) atomic_sub_and_test(value, atom)
291 #define cfs_atomic_sub_return(value, atom)   atomic_sub_return(value, atom)
292 #define CFS_ATOMIC_INIT(i)                   ATOMIC_INIT(i)
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 /*
307  * might_sleep
308  */
309 #define cfs_might_sleep() might_sleep()
310
311 /*
312  * group_info
313  */
314 typedef struct group_info cfs_group_info_t;
315
316 #define cfs_get_group_info(group_info)     get_group_info(group_info)
317 #define cfs_put_group_info(group_info)     put_group_info(group_info)
318 #define cfs_set_current_groups(group_info) set_current_groups(group_info)
319 #define cfs_groups_free(group_info)        groups_free(group_info)
320 #define cfs_groups_alloc(gidsetsize)       groups_alloc(gidsetsize)
321
322 /*
323  * Random bytes
324  */
325 #define cfs_get_random_bytes_prim(buf, nbytes)  get_random_bytes(buf, nbytes)
326 #endif