Whamcloud - gitweb
2ff5a9715d9d2e710fd609ea5cfb69d4199f31a6
[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 [sun.com URL with a
20  * copy of GPLv2].
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/kernel.h>
57 #include <linux/version.h>
58 #include <linux/proc_fs.h>
59 #include <linux/mm.h>
60 #include <linux/timer.h>
61 #include <linux/signal.h>
62 #include <linux/sched.h>
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 /*
71  * Pseudo device register
72  */
73 typedef struct miscdevice               cfs_psdev_t;
74 #define cfs_psdev_register(dev)         misc_register(dev)
75 #define cfs_psdev_deregister(dev)       misc_deregister(dev)
76
77 /*
78  * Sysctl register
79  */
80 typedef struct ctl_table                cfs_sysctl_table_t;
81 typedef struct ctl_table_header         cfs_sysctl_table_header_t;
82
83 #ifdef HAVE_2ARGS_REGISTER_SYSCTL
84 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t, a)
85 #else
86 #define cfs_register_sysctl_table(t, a) register_sysctl_table(t)
87 #endif
88 #define cfs_unregister_sysctl_table(t)  unregister_sysctl_table(t)
89
90 /*
91  * Symbol register
92  */
93 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
94 #define cfs_symbol_register(s, p)       inter_module_register(s, THIS_MODULE, p)
95 #define cfs_symbol_unregister(s)        inter_module_unregister(s)
96 #define cfs_symbol_get(s)               inter_module_get(s)
97 #define cfs_symbol_put(s)               inter_module_put(s)
98 #define cfs_module_get()                MOD_INC_USE_COUNT
99 #define cfs_module_put()                MOD_DEC_USE_COUNT
100 #else
101 #define cfs_symbol_register(s, p)       do {} while(0)
102 #define cfs_symbol_unregister(s)        do {} while(0)
103 #define cfs_symbol_get(s)               symbol_get(s)
104 #define cfs_symbol_put(s)               symbol_put(s)
105 #define cfs_module_get()                try_module_get(THIS_MODULE)
106 #define cfs_module_put()                module_put(THIS_MODULE)
107 #endif
108
109 /*
110  * Proc file system APIs
111  */
112 typedef read_proc_t                     cfs_read_proc_t;
113 typedef write_proc_t                    cfs_write_proc_t;
114 typedef struct proc_dir_entry           cfs_proc_dir_entry_t;
115 #define cfs_create_proc_entry(n, m, p)  create_proc_entry(n, m, p)
116 #define cfs_free_proc_entry(e)          free_proc_entry(e)
117 #define cfs_remove_proc_entry(n, e)     remove_proc_entry(n, e)
118
119 /*
120  * Wait Queue
121  */
122 #define CFS_TASK_INTERRUPTIBLE          TASK_INTERRUPTIBLE
123 #define CFS_TASK_UNINT                  TASK_UNINTERRUPTIBLE
124 #define CFS_TASK_RUNNING                TASK_RUNNING
125
126 typedef wait_queue_t                    cfs_waitlink_t;
127 typedef wait_queue_head_t               cfs_waitq_t;
128 typedef long                            cfs_task_state_t;
129
130 /* Kernel thread */
131 typedef int (*cfs_thread_t)(void *);
132
133 static inline int cfs_kernel_thread(int (*fn)(void *),
134                                     void *arg, unsigned long flags)
135 {
136         void *orig_info = current->journal_info;
137         int rc;
138
139         current->journal_info = NULL;
140         rc = kernel_thread(fn, arg, flags);
141         current->journal_info = orig_info;
142         return rc;
143 }
144
145
146 /*
147  * Task struct
148  */
149 typedef struct task_struct              cfs_task_t;
150 #define cfs_current()                   current
151 #define cfs_task_lock(t)                task_lock(t)
152 #define cfs_task_unlock(t)              task_unlock(t)
153 #define CFS_DECL_JOURNAL_DATA           void *journal_info
154 #define CFS_PUSH_JOURNAL                do {    \
155         journal_info = current->journal_info;   \
156         current->journal_info = NULL;           \
157         } while(0)
158 #define CFS_POP_JOURNAL                 do {    \
159         current->journal_info = journal_info;   \
160         } while(0)
161
162 /* Module interfaces */
163 #define cfs_module(name, version, init, fini) \
164 module_init(init);                            \
165 module_exit(fini)
166
167 /*
168  * Signal
169  */
170 typedef sigset_t                        cfs_sigset_t;
171
172 /*
173  * Timer
174  */
175 typedef struct timer_list cfs_timer_t;
176
177
178 #ifndef wait_event_timeout /* Only for RHEL3 2.4.21 kernel */
179 #define __wait_event_timeout(wq, condition, timeout, ret)        \
180 do {                                                             \
181         int __ret = 0;                                           \
182         if (!(condition)) {                                      \
183                 wait_queue_t __wait;                             \
184                 unsigned long expire;                            \
185                                                                  \
186                 init_waitqueue_entry(&__wait, current);          \
187                 expire = timeout + jiffies;                      \
188                 add_wait_queue(&wq, &__wait);                    \
189                 for (;;) {                                       \
190                         set_current_state(TASK_UNINTERRUPTIBLE); \
191                         if (condition)                           \
192                                 break;                           \
193                         if (jiffies > expire) {                  \
194                                 ret = jiffies - expire;          \
195                                 break;                           \
196                         }                                        \
197                         schedule_timeout(timeout);               \
198                 }                                                \
199                 current->state = TASK_RUNNING;                   \
200                 remove_wait_queue(&wq, &__wait);                 \
201         }                                                        \
202 } while (0)
203 /*
204    retval == 0; condition met; we're good.
205    retval > 0; timed out.
206 */
207 #define cfs_waitq_wait_event_timeout(wq, condition, timeout)         \
208 ({                                                                   \
209         int __ret = 0;                                               \
210         if (!(condition))                                            \
211                 __wait_event_timeout(wq, condition, timeout, __ret); \
212         __ret;                                                       \
213 })
214 #else
215 #define cfs_waitq_wait_event_timeout  wait_event_timeout
216 #endif
217
218 #ifndef wait_event_interruptible_timeout /* Only for RHEL3 2.4.21 kernel */
219 #define __wait_event_interruptible_timeout(wq, condition, timeout, ret)   \
220 do {                                                           \
221         int __ret = 0;                                         \
222         if (!(condition)) {                                    \
223                 wait_queue_t __wait;                           \
224                 unsigned long expire;                          \
225                                                                \
226                 init_waitqueue_entry(&__wait, current);        \
227                 expire = timeout + jiffies;                    \
228                 add_wait_queue(&wq, &__wait);                  \
229                 for (;;) {                                     \
230                         set_current_state(TASK_INTERRUPTIBLE); \
231                         if (condition)                         \
232                                 break;                         \
233                         if (jiffies > expire) {                \
234                                 ret = jiffies - expire;        \
235                                 break;                         \
236                         }                                      \
237                         if (!signal_pending(current)) {        \
238                                 schedule_timeout(timeout);     \
239                                 continue;                      \
240                         }                                      \
241                         ret = -ERESTARTSYS;                    \
242                         break;                                 \
243                 }                                              \
244                 current->state = TASK_RUNNING;                 \
245                 remove_wait_queue(&wq, &__wait);               \
246         }                                                      \
247 } while (0)
248
249 /*
250    retval == 0; condition met; we're good.
251    retval < 0; interrupted by signal.
252    retval > 0; timed out.
253 */
254 #define cfs_waitq_wait_event_interruptible_timeout(wq, condition, timeout) \
255 ({                                                                \
256         int __ret = 0;                                            \
257         if (!(condition))                                         \
258                 __wait_event_interruptible_timeout(wq, condition, \
259                                                 timeout, __ret);  \
260         __ret;                                                    \
261 })
262 #else
263 #define cfs_waitq_wait_event_interruptible_timeout wait_event_interruptible_timeout
264 #endif
265
266 #endif