Whamcloud - gitweb
LU-812 kernel: AUTOCONF_INCLUDED removed
[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 #include <linux/module.h>
51 #include <linux/init.h>
52 #include <linux/kernel.h>
53 #include <linux/version.h>
54 #include <linux/proc_fs.h>
55 #include <linux/mm.h>
56 #include <linux/timer.h>
57 #include <linux/signal.h>
58 #include <linux/sched.h>
59 #include <linux/kthread.h>
60 #ifdef HAVE_LINUX_RANDOM_H
61 #include <linux/random.h>
62 #endif
63
64 #include <linux/miscdevice.h>
65 #include <libcfs/linux/portals_compat25.h>
66 #include <asm/div64.h>
67
68 #include <libcfs/linux/linux-time.h>
69
70 #define CFS_KERN_EMERG   KERN_EMERG
71 #define CFS_KERN_ALERT   KERN_ALERT
72 #define CFS_KERN_CRIT    KERN_CRIT
73 #define CFS_KERN_ERR     KERN_ERR
74 #define CFS_KERN_WARNING KERN_WARNING
75 #define CFS_KERN_NOTICE  KERN_NOTICE
76 #define CFS_KERN_INFO    KERN_INFO
77 #define CFS_KERN_DEBUG   KERN_DEBUG
78
79 /*
80  * CPU
81  */
82 #ifdef for_each_possible_cpu
83 #define cfs_for_each_possible_cpu(cpu) for_each_possible_cpu(cpu)
84 #elif defined(for_each_cpu)
85 #define cfs_for_each_possible_cpu(cpu) for_each_cpu(cpu)
86 #endif
87
88 #ifdef NR_CPUS
89 #define CFS_NR_CPUS     NR_CPUS
90 #else
91 #define CFS_NR_CPUS     1
92 #endif
93
94 #ifdef HAVE_SET_CPUS_ALLOWED
95 #define cfs_set_cpus_allowed(t, mask)  set_cpus_allowed(t, mask)
96 #else
97 #define cfs_set_cpus_allowed(t, mask)  set_cpus_allowed_ptr(t, &(mask))
98 #endif
99
100 /*
101  * cache
102  */
103 #define CFS_L1_CACHE_ALIGN(x)           L1_CACHE_ALIGN(x)
104
105 /*
106  * IRQs
107  */
108 #define CFS_NR_IRQS                     NR_IRQS
109
110 #define CFS_EXPORT_SYMBOL(s)            EXPORT_SYMBOL(s)
111
112 /*
113  * Pseudo device register
114  */
115 typedef struct miscdevice               cfs_psdev_t;
116 #define cfs_psdev_register(dev)         misc_register(dev)
117 #define cfs_psdev_deregister(dev)       misc_deregister(dev)
118
119 /*
120  * Sysctl register
121  */
122 typedef struct ctl_table                cfs_sysctl_table_t;
123 typedef struct ctl_table_header         cfs_sysctl_table_header_t;
124
125 #ifdef HAVE_2ARGS_REGISTER_SYSCTL
126 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t, a)
127 #else
128 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t)
129 #endif
130 #define cfs_unregister_sysctl_table(t)  unregister_sysctl_table(t)
131
132 #define DECLARE_PROC_HANDLER(name)                      \
133 static int                                              \
134 LL_PROC_PROTO(name)                                     \
135 {                                                       \
136         DECLARE_LL_PROC_PPOS_DECL;                      \
137                                                         \
138         return proc_call_handler(table->data, write,    \
139                                  ppos, buffer, lenp,    \
140                                  __##name);             \
141 }
142
143 /*
144  * Symbol register
145  */
146 #define cfs_symbol_register(s, p)       do {} while(0)
147 #define cfs_symbol_unregister(s)        do {} while(0)
148 #define cfs_symbol_get(s)               symbol_get(s)
149 #define cfs_symbol_put(s)               symbol_put(s)
150 #define cfs_module_get()                try_module_get(THIS_MODULE)
151 #define cfs_try_module_get(m)           try_module_get(m)
152 #define __cfs_module_get(m)             __module_get(m)
153 #define cfs_module_put(m)               module_put(m)
154 #define cfs_module_refcount(m)          module_refcount(m)
155
156 typedef struct module cfs_module_t;
157
158 /*
159  * Proc file system APIs
160  */
161 typedef read_proc_t                     cfs_read_proc_t;
162 typedef write_proc_t                    cfs_write_proc_t;
163 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
164 #define cfs_create_proc_entry(n, m, p)  create_proc_entry(n, m, p)
165 #define cfs_free_proc_entry(e)          free_proc_entry(e)
166 #define cfs_remove_proc_entry(n, e)     remove_proc_entry(n, e)
167
168 /*
169  * Wait Queue
170  */
171 #define CFS_TASK_INTERRUPTIBLE          TASK_INTERRUPTIBLE
172 #define CFS_TASK_UNINT                  TASK_UNINTERRUPTIBLE
173 #define CFS_TASK_RUNNING                TASK_RUNNING
174
175 #define cfs_set_current_state(state)    set_current_state(state)
176 #define cfs_wait_event(wq, cond)        wait_event(wq, cond)
177
178 typedef wait_queue_t                    cfs_waitlink_t;
179 typedef wait_queue_head_t               cfs_waitq_t;
180 typedef long                            cfs_task_state_t;
181
182 #define cfs_kthread_run(fn, data, fmt, arg...) kthread_run(fn, data, fmt, ##arg)
183
184 /* Kernel thread */
185 typedef int (*cfs_thread_t)(void *);
186
187 #define CFS_DAEMON_FLAGS (CLONE_VM | CLONE_FILES)
188 extern int cfs_create_thread(int (*fn)(void *),
189                              void *arg, unsigned long flags);
190
191 /*
192  * Task struct
193  */
194 typedef struct task_struct              cfs_task_t;
195 #define cfs_current()                   current
196 #define cfs_task_lock(t)                task_lock(t)
197 #define cfs_task_unlock(t)              task_unlock(t)
198 #define CFS_DECL_JOURNAL_DATA           void *journal_info
199 #define CFS_PUSH_JOURNAL                do {    \
200         journal_info = current->journal_info;   \
201         current->journal_info = NULL;           \
202         } while(0)
203 #define CFS_POP_JOURNAL                 do {    \
204         current->journal_info = journal_info;   \
205         } while(0)
206
207 /* Module interfaces */
208 #define cfs_module(name, version, init, fini) \
209         module_init(init);                    \
210         module_exit(fini)
211 #define cfs_request_module              request_module
212
213 /*
214  * Signal
215  */
216 typedef sigset_t                        cfs_sigset_t;
217
218 /*
219  * Timer
220  */
221 typedef struct timer_list cfs_timer_t;
222
223 #define CFS_MAX_SCHEDULE_TIMEOUT MAX_SCHEDULE_TIMEOUT
224
225 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
226 #define __wait_event_timeout(wq, condition, timeout, ret)        \
227 do {                                                             \
228         int __ret = 0;                                           \
229         if (!(condition)) {                                      \
230                 wait_queue_t __wait;                             \
231                 unsigned long expire;                            \
232                                                                  \
233                 init_waitqueue_entry(&__wait, current);          \
234                 expire = timeout + jiffies;                      \
235                 add_wait_queue(&wq, &__wait);                    \
236                 for (;;) {                                       \
237                         set_current_state(TASK_UNINTERRUPTIBLE); \
238                         if (condition)                           \
239                                 break;                           \
240                         if (jiffies > expire) {                  \
241                                 ret = jiffies - expire;          \
242                                 break;                           \
243                         }                                        \
244                         schedule_timeout(timeout);               \
245                 }                                                \
246                 current->state = TASK_RUNNING;                   \
247                 remove_wait_queue(&wq, &__wait);                 \
248         }                                                        \
249 } while (0)
250 /*
251    retval == 0; condition met; we're good.
252    retval > 0; timed out.
253 */
254 #define cfs_waitq_wait_event_timeout(wq, condition, timeout, ret)    \
255 do {                                                                 \
256         ret = 0;                                                     \
257         if (!(condition))                                            \
258                 __wait_event_timeout(wq, condition, timeout, ret);   \
259 } while (0)
260 #else
261 #define cfs_waitq_wait_event_timeout(wq, condition, timeout, ret)    \
262         ret = wait_event_timeout(wq, condition, timeout)
263 #endif
264
265 #define cfs_waitq_wait_event_interruptible_timeout(wq, c, timeout, ret) \
266         ret = wait_event_interruptible_timeout(wq, c, timeout)
267
268 /*
269  * atomic
270  */
271
272 typedef atomic_t cfs_atomic_t;
273
274 #define cfs_atomic_read(atom)                atomic_read(atom)
275 #define cfs_atomic_inc(atom)                 atomic_inc(atom)
276 #define cfs_atomic_inc_and_test(atom)        atomic_inc_and_test(atom)
277 #define cfs_atomic_inc_return(atom)          atomic_inc_return(atom)
278 #define cfs_atomic_inc_not_zero(atom)        atomic_inc_not_zero(atom)
279 #define cfs_atomic_dec(atom)                 atomic_dec(atom)
280 #define cfs_atomic_dec_and_test(atom)        atomic_dec_and_test(atom)
281 #define cfs_atomic_dec_and_lock(atom, lock)  atomic_dec_and_lock(atom, lock)
282 #define cfs_atomic_dec_return(atom)          atomic_dec_return(atom)
283 #define cfs_atomic_set(atom, value)          atomic_set(atom, value)
284 #define cfs_atomic_add(value, atom)          atomic_add(value, atom)
285 #define cfs_atomic_add_return(value, atom)   atomic_add_return(value, atom)
286 #define cfs_atomic_sub(value, atom)          atomic_sub(value, atom)
287 #define cfs_atomic_sub_and_test(value, atom) atomic_sub_and_test(value, atom)
288 #define cfs_atomic_sub_return(value, atom)   atomic_sub_return(value, atom)
289 #define CFS_ATOMIC_INIT(i)                   ATOMIC_INIT(i)
290
291 /*
292  * membar
293  */
294
295 #define cfs_mb() mb()
296
297 /*
298  * interrupt
299  */
300
301 #define cfs_in_interrupt() in_interrupt()
302
303 /*
304  * might_sleep
305  */
306 #define cfs_might_sleep() might_sleep()
307
308 /*
309  * group_info
310  */
311 typedef struct group_info cfs_group_info_t;
312
313 #define cfs_get_group_info(group_info)     get_group_info(group_info)
314 #define cfs_put_group_info(group_info)     put_group_info(group_info)
315 #define cfs_set_current_groups(group_info) set_current_groups(group_info)
316 #define cfs_groups_free(group_info)        groups_free(group_info)
317 #define cfs_groups_alloc(gidsetsize)       groups_alloc(gidsetsize)
318
319 /*
320  * Random bytes
321  */
322 #define cfs_get_random_bytes_prim(buf, nbytes)  get_random_bytes(buf, nbytes)
323 #endif