Whamcloud - gitweb
1dca9ac42321efc5884f7127f3e2faa2000162bf
[fs/lustre-release.git] / libcfs / 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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 #ifndef AUTOCONF_INCLUDED
53 #include <linux/config.h>
54 #endif
55 #include <linux/module.h>
56 #include <linux/init.h>
57 #include <linux/kernel.h>
58 #include <linux/version.h>
59 #include <linux/proc_fs.h>
60 #include <linux/mm.h>
61 #include <linux/timer.h>
62 #include <linux/signal.h>
63 #include <linux/sched.h>
64 #include <linux/kthread.h>
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 #define cfs_set_cpus_allowed(t, mask)  set_cpus_allowed(t, mask)
97 /*
98  * cache
99  */
100 #define CFS_L1_CACHE_ALIGN(x)           L1_CACHE_ALIGN(x)
101
102 /*
103  * IRQs
104  */
105 #define CFS_NR_IRQS                     NR_IRQS
106
107 #define CFS_EXPORT_SYMBOL(s)            EXPORT_SYMBOL(s)
108
109 /*
110  * Pseudo device register
111  */
112 typedef struct miscdevice               cfs_psdev_t;
113 #define cfs_psdev_register(dev)         misc_register(dev)
114 #define cfs_psdev_deregister(dev)       misc_deregister(dev)
115
116 /*
117  * Sysctl register
118  */
119 typedef struct ctl_table                cfs_sysctl_table_t;
120 typedef struct ctl_table_header         cfs_sysctl_table_header_t;
121
122 #ifdef HAVE_2ARGS_REGISTER_SYSCTL
123 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t, a)
124 #else
125 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t)
126 #endif
127 #define cfs_unregister_sysctl_table(t)  unregister_sysctl_table(t)
128
129 #define DECLARE_PROC_HANDLER(name)                      \
130 static int                                              \
131 LL_PROC_PROTO(name)                                     \
132 {                                                       \
133         DECLARE_LL_PROC_PPOS_DECL;                      \
134                                                         \
135         return proc_call_handler(table->data, write,    \
136                                  ppos, buffer, lenp,    \
137                                  __##name);             \
138 }
139
140 /*
141  * Symbol register
142  */
143 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
144 #define cfs_symbol_register(s, p)       inter_module_register(s, THIS_MODULE, p)
145 #define cfs_symbol_unregister(s)        inter_module_unregister(s)
146 #define cfs_symbol_get(s)               inter_module_get(s)
147 #define cfs_symbol_put(s)               inter_module_put(s)
148 #define cfs_module_get()                MOD_INC_USE_COUNT
149 #else
150 #define cfs_symbol_register(s, p)       do {} while(0)
151 #define cfs_symbol_unregister(s)        do {} while(0)
152 #define cfs_symbol_get(s)               symbol_get(s)
153 #define cfs_symbol_put(s)               symbol_put(s)
154 #define cfs_module_get()                try_module_get(THIS_MODULE)
155 #define cfs_try_module_get(m)           try_module_get(m)
156 #define __cfs_module_get(m)             __module_get(m)
157 #define cfs_module_put(m)               module_put(m)
158 #define cfs_module_refcount(m)          module_refcount(m)
159 #endif
160
161 typedef struct module cfs_module_t;
162
163 /*
164  * Proc file system APIs
165  */
166 typedef read_proc_t                     cfs_read_proc_t;
167 typedef write_proc_t                    cfs_write_proc_t;
168 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
169 #define cfs_create_proc_entry(n, m, p)  create_proc_entry(n, m, p)
170 #define cfs_free_proc_entry(e)          free_proc_entry(e)
171 #define cfs_remove_proc_entry(n, e)     remove_proc_entry(n, e)
172
173 /*
174  * Wait Queue
175  */
176 #define CFS_TASK_INTERRUPTIBLE          TASK_INTERRUPTIBLE
177 #define CFS_TASK_UNINT                  TASK_UNINTERRUPTIBLE
178 #define CFS_TASK_RUNNING                TASK_RUNNING
179
180 #define cfs_set_current_state(state)    set_current_state(state)
181 #define cfs_wait_event(wq, cond)        wait_event(wq, cond)
182
183 typedef wait_queue_t                    cfs_waitlink_t;
184 typedef wait_queue_head_t               cfs_waitq_t;
185 typedef long                            cfs_task_state_t;
186
187 #define cfs_kthread_run(fn, data, fmt, arg...) kthread_run(fn, data, fmt, ##arg)
188
189 /* Kernel thread */
190 typedef int (*cfs_thread_t)(void *);
191
192 static inline int cfs_kernel_thread(int (*fn)(void *),
193                                     void *arg, unsigned long flags)
194 {
195         void *orig_info = current->journal_info;
196         int rc;
197
198         current->journal_info = NULL;
199         rc = kernel_thread(fn, arg, flags);
200         current->journal_info = orig_info;
201         return rc;
202 }
203
204
205 /*
206  * Task struct
207  */
208 typedef struct task_struct              cfs_task_t;
209 #define cfs_current()                   current
210 #define cfs_task_lock(t)                task_lock(t)
211 #define cfs_task_unlock(t)              task_unlock(t)
212 #define CFS_DECL_JOURNAL_DATA           void *journal_info
213 #define CFS_PUSH_JOURNAL                do {    \
214         journal_info = current->journal_info;   \
215         current->journal_info = NULL;           \
216         } while(0)
217 #define CFS_POP_JOURNAL                 do {    \
218         current->journal_info = journal_info;   \
219         } while(0)
220
221 /* Module interfaces */
222 #define cfs_module(name, version, init, fini) \
223         module_init(init);                    \
224         module_exit(fini)
225 #define cfs_request_module              request_module
226
227 /*
228  * Signal
229  */
230 typedef sigset_t                        cfs_sigset_t;
231
232 /*
233  * Timer
234  */
235 typedef struct timer_list cfs_timer_t;
236
237 #define CFS_MAX_SCHEDULE_TIMEOUT MAX_SCHEDULE_TIMEOUT
238
239 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
240 #define __wait_event_timeout(wq, condition, timeout, ret)        \
241 do {                                                             \
242         int __ret = 0;                                           \
243         if (!(condition)) {                                      \
244                 wait_queue_t __wait;                             \
245                 unsigned long expire;                            \
246                                                                  \
247                 init_waitqueue_entry(&__wait, current);          \
248                 expire = timeout + jiffies;                      \
249                 add_wait_queue(&wq, &__wait);                    \
250                 for (;;) {                                       \
251                         set_current_state(TASK_UNINTERRUPTIBLE); \
252                         if (condition)                           \
253                                 break;                           \
254                         if (jiffies > expire) {                  \
255                                 ret = jiffies - expire;          \
256                                 break;                           \
257                         }                                        \
258                         schedule_timeout(timeout);               \
259                 }                                                \
260                 current->state = TASK_RUNNING;                   \
261                 remove_wait_queue(&wq, &__wait);                 \
262         }                                                        \
263 } while (0)
264 /*
265    retval == 0; condition met; we're good.
266    retval > 0; timed out.
267 */
268 #define cfs_waitq_wait_event_timeout(wq, condition, timeout, ret)    \
269 do {                                                                 \
270         ret = 0;                                                     \
271         if (!(condition))                                            \
272                 __wait_event_timeout(wq, condition, timeout, ret);   \
273 } while (0)
274 #else
275 #define cfs_waitq_wait_event_timeout(wq, condition, timeout, ret)    \
276         ret = wait_event_timeout(wq, condition, timeout)
277 #endif
278
279 #ifndef wait_event_interruptible_timeout /* Only for RHEL3 2.4.21 kernel */
280 #define __wait_event_interruptible_timeout(wq, condition, timeout, ret)  \
281 do {                                                           \
282         int __ret = 0;                                         \
283         if (!(condition)) {                                    \
284                 wait_queue_t __wait;                           \
285                 unsigned long expire;                          \
286                                                                \
287                 init_waitqueue_entry(&__wait, current);        \
288                 expire = timeout + jiffies;                    \
289                 add_wait_queue(&wq, &__wait);                  \
290                 for (;;) {                                     \
291                         set_current_state(TASK_INTERRUPTIBLE); \
292                         if (condition)                         \
293                                 break;                         \
294                         if (jiffies > expire) {                \
295                                 ret = jiffies - expire;        \
296                                 break;                         \
297                         }                                      \
298                         if (!signal_pending(current)) {        \
299                                 schedule_timeout(timeout);     \
300                                 continue;                      \
301                         }                                      \
302                         ret = -ERESTARTSYS;                    \
303                         break;                                 \
304                 }                                              \
305                 current->state = TASK_RUNNING;                 \
306                 remove_wait_queue(&wq, &__wait);               \
307         }                                                      \
308 } while (0)
309
310 /*
311    retval == 0; condition met; we're good.
312    retval < 0; interrupted by signal.
313    retval > 0; timed out.
314 */
315 #define cfs_waitq_wait_event_interruptible_timeout(wq, condition, timeout, ret)\
316 do {                                                              \
317         ret = 0;                                                  \
318         if (!(condition))                                         \
319                 __wait_event_interruptible_timeout(wq, condition, \
320                                                    timeout, ret); \
321 } while (0)
322 #else
323 #define cfs_waitq_wait_event_interruptible_timeout(wq, c, timeout, ret) \
324         ret = wait_event_interruptible_timeout(wq, c, timeout)
325 #endif
326
327 /*
328  * atomic
329  */
330
331 typedef atomic_t cfs_atomic_t;
332
333 #define cfs_atomic_read(atom)                atomic_read(atom)
334 #define cfs_atomic_inc(atom)                 atomic_inc(atom)
335 #define cfs_atomic_inc_and_test(atom)        atomic_inc_and_test(atom)
336 #define cfs_atomic_inc_return(atom)          atomic_inc_return(atom)
337 #define cfs_atomic_inc_not_zero(atom)        atomic_inc_not_zero(atom)
338 #define cfs_atomic_dec(atom)                 atomic_dec(atom)
339 #define cfs_atomic_dec_and_test(atom)        atomic_dec_and_test(atom)
340 #define cfs_atomic_dec_and_lock(atom, lock)  atomic_dec_and_lock(atom, lock)
341 #define cfs_atomic_dec_return(atom)          atomic_dec_return(atom)
342 #define cfs_atomic_set(atom, value)          atomic_set(atom, value)
343 #define cfs_atomic_add(value, atom)          atomic_add(value, atom)
344 #define cfs_atomic_add_return(value, atom)   atomic_add_return(value, atom)
345 #define cfs_atomic_sub(value, atom)          atomic_sub(value, atom)
346 #define cfs_atomic_sub_and_test(value, atom) atomic_sub_and_test(value, atom)
347 #define cfs_atomic_sub_return(value, atom)   atomic_sub_return(value, atom)
348 #define CFS_ATOMIC_INIT(i)                   ATOMIC_INIT(i)
349
350 /*
351  * membar
352  */
353
354 #define cfs_mb() mb()
355
356 /*
357  * interrupt
358  */
359
360 #define cfs_in_interrupt() in_interrupt()
361
362 /*
363  * might_sleep
364  */
365 #define cfs_might_sleep() might_sleep()
366
367 /*
368  * group_info
369  */
370 typedef struct group_info cfs_group_info_t;
371
372 #define cfs_get_group_info(group_info)     get_group_info(group_info)
373 #define cfs_put_group_info(group_info)     put_group_info(group_info)
374 #define cfs_set_current_groups(group_info) set_current_groups(group_info)
375 #define cfs_groups_free(group_info)        groups_free(group_info)
376 #define cfs_groups_alloc(gidsetsize)       groups_alloc(gidsetsize)
377
378 /*
379  * Random bytes
380  */
381 #define cfs_get_random_bytes(buf, nbytes)  get_random_bytes(buf, nbytes)
382 #endif