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