Whamcloud - gitweb
LU-1346 libcfs: cleanup libcfs primitive (linux-prim.h)
[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
73 /*
74  * CPU
75  */
76 #ifdef for_each_possible_cpu
77 #define cfs_for_each_possible_cpu(cpu) for_each_possible_cpu(cpu)
78 #elif defined(for_each_cpu)
79 #define cfs_for_each_possible_cpu(cpu) for_each_cpu(cpu)
80 #endif
81
82 #ifndef NR_CPUS
83 #define NR_CPUS                         1
84 #endif
85
86 #define DECLARE_PROC_HANDLER(name)                      \
87 static int                                              \
88 LL_PROC_PROTO(name)                                     \
89 {                                                       \
90         DECLARE_LL_PROC_PPOS_DECL;                      \
91                                                         \
92         return proc_call_handler(table->data, write,    \
93                                  ppos, buffer, lenp,    \
94                                  __##name);             \
95 }
96
97 /*
98  * Proc file system APIs
99  */
100 typedef read_proc_t                     cfs_read_proc_t;
101 typedef write_proc_t                    cfs_write_proc_t;
102 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
103 #define cfs_create_proc_entry(n, m, p)  create_proc_entry(n, m, p)
104 #define cfs_free_proc_entry(e)          free_proc_entry(e)
105 #define cfs_remove_proc_entry(n, e)     remove_proc_entry(n, e)
106
107 /*
108  * Wait Queue
109  */
110 #define CFS_TASK_INTERRUPTIBLE          TASK_INTERRUPTIBLE
111 #define CFS_TASK_UNINT                  TASK_UNINTERRUPTIBLE
112 #define CFS_TASK_RUNNING                TASK_RUNNING
113
114 #define cfs_set_current_state(state)    set_current_state(state)
115 #define cfs_wait_event(wq, cond)        wait_event(wq, cond)
116
117 typedef wait_queue_t                    cfs_waitlink_t;
118 typedef wait_queue_head_t               cfs_waitq_t;
119 typedef long                            cfs_task_state_t;
120
121 #define CFS_DECL_WAITQ(wq)              DECLARE_WAIT_QUEUE_HEAD(wq)
122
123 /*
124  * Task struct
125  */
126 typedef struct task_struct              cfs_task_t;
127 #define cfs_current()                   current
128 #define cfs_task_lock(t)                task_lock(t)
129 #define cfs_task_unlock(t)              task_unlock(t)
130 #define CFS_DECL_JOURNAL_DATA           void *journal_info
131 #define CFS_PUSH_JOURNAL                do {    \
132         journal_info = current->journal_info;   \
133         current->journal_info = NULL;           \
134         } while(0)
135 #define CFS_POP_JOURNAL                 do {    \
136         current->journal_info = journal_info;   \
137         } while(0)
138
139 /* Module interfaces */
140 #define cfs_module(name, version, init, fini) \
141         module_init(init);                    \
142         module_exit(fini)
143
144 /*
145  * Signal
146  */
147 typedef sigset_t                        cfs_sigset_t;
148
149 /*
150  * Timer
151  */
152 typedef struct timer_list cfs_timer_t;
153
154 #define CFS_MAX_SCHEDULE_TIMEOUT MAX_SCHEDULE_TIMEOUT
155
156 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
157 #define __wait_event_timeout(wq, condition, timeout, ret)        \
158 do {                                                             \
159         int __ret = 0;                                           \
160         if (!(condition)) {                                      \
161                 wait_queue_t __wait;                             \
162                 unsigned long expire;                            \
163                                                                  \
164                 init_waitqueue_entry(&__wait, current);          \
165                 expire = timeout + jiffies;                      \
166                 add_wait_queue(&wq, &__wait);                    \
167                 for (;;) {                                       \
168                         set_current_state(TASK_UNINTERRUPTIBLE); \
169                         if (condition)                           \
170                                 break;                           \
171                         if (jiffies > expire) {                  \
172                                 ret = jiffies - expire;          \
173                                 break;                           \
174                         }                                        \
175                         schedule_timeout(timeout);               \
176                 }                                                \
177                 current->state = TASK_RUNNING;                   \
178                 remove_wait_queue(&wq, &__wait);                 \
179         }                                                        \
180 } while (0)
181 /*
182    retval == 0; condition met; we're good.
183    retval > 0; timed out.
184 */
185 #define cfs_waitq_wait_event_timeout(wq, condition, timeout, ret)    \
186 do {                                                                 \
187         ret = 0;                                                     \
188         if (!(condition))                                            \
189                 __wait_event_timeout(wq, condition, timeout, ret);   \
190 } while (0)
191 #else
192 #define cfs_waitq_wait_event_timeout(wq, condition, timeout, ret)    \
193         ret = wait_event_timeout(wq, condition, timeout)
194 #endif
195
196 #define cfs_waitq_wait_event_interruptible_timeout(wq, c, timeout, ret) \
197         ret = wait_event_interruptible_timeout(wq, c, timeout)
198
199 /*
200  * atomic
201  */
202
203 typedef atomic_t cfs_atomic_t;
204
205 #define cfs_atomic_read(atom)                atomic_read(atom)
206 #define cfs_atomic_inc(atom)                 atomic_inc(atom)
207 #define cfs_atomic_inc_and_test(atom)        atomic_inc_and_test(atom)
208 #define cfs_atomic_inc_return(atom)          atomic_inc_return(atom)
209 #define cfs_atomic_inc_not_zero(atom)        atomic_inc_not_zero(atom)
210 #define cfs_atomic_add_unless(atom, a, u)    atomic_add_unless(atom, a, u)
211 #define cfs_atomic_dec(atom)                 atomic_dec(atom)
212 #define cfs_atomic_dec_and_test(atom)        atomic_dec_and_test(atom)
213 #define cfs_atomic_dec_and_lock(atom, lock)  atomic_dec_and_lock(atom, lock)
214 #define cfs_atomic_dec_return(atom)          atomic_dec_return(atom)
215 #define cfs_atomic_set(atom, value)          atomic_set(atom, value)
216 #define cfs_atomic_add(value, atom)          atomic_add(value, atom)
217 #define cfs_atomic_add_return(value, atom)   atomic_add_return(value, atom)
218 #define cfs_atomic_sub(value, atom)          atomic_sub(value, atom)
219 #define cfs_atomic_sub_and_test(value, atom) atomic_sub_and_test(value, atom)
220 #define cfs_atomic_sub_return(value, atom)   atomic_sub_return(value, atom)
221 #define cfs_atomic_cmpxchg(atom, old, nv)    atomic_cmpxchg(atom, old, nv)
222 #define CFS_ATOMIC_INIT(i)                   ATOMIC_INIT(i)
223
224 /*
225  * membar
226  */
227
228 #define cfs_mb() mb()
229
230 /*
231  * interrupt
232  */
233
234 #define cfs_in_interrupt() in_interrupt()
235
236 /*
237  * might_sleep
238  */
239 #define cfs_might_sleep() might_sleep()
240
241 /*
242  * group_info
243  */
244 typedef struct group_info cfs_group_info_t;
245
246 #define cfs_get_group_info(group_info)     get_group_info(group_info)
247 #define cfs_put_group_info(group_info)     put_group_info(group_info)
248 #define cfs_set_current_groups(group_info) set_current_groups(group_info)
249 #define cfs_groups_free(group_info)        groups_free(group_info)
250 #define cfs_groups_alloc(gidsetsize)       groups_alloc(gidsetsize)
251
252 /*
253  * Random bytes
254  */
255 #define cfs_get_random_bytes_prim(buf, nbytes)  get_random_bytes(buf, nbytes)
256 #endif