Whamcloud - gitweb
f8e277562989fbdf00a55b2fc14aa0e401a9c4fc
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-prim.c
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
37 #define DEBUG_SUBSYSTEM S_LNET
38 #ifndef AUTOCONF_INCLUDED
39 #include <linux/config.h>
40 #endif
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44
45 #include <libcfs/libcfs.h>
46
47 #if defined(CONFIG_KGDB)
48 #include <asm/kgdb.h>
49 #endif
50
51 #define LINUX_WAITQ(w) ((wait_queue_t *) w)
52 #define LINUX_WAITQ_HEAD(w) ((wait_queue_head_t *) w)
53
54 void
55 cfs_waitq_init(cfs_waitq_t *waitq)
56 {
57         init_waitqueue_head(LINUX_WAITQ_HEAD(waitq));
58 }
59 EXPORT_SYMBOL(cfs_waitq_init);
60
61 void
62 cfs_waitlink_init(cfs_waitlink_t *link)
63 {
64         init_waitqueue_entry(LINUX_WAITQ(link), current);
65 }
66 EXPORT_SYMBOL(cfs_waitlink_init);
67
68 void
69 cfs_waitq_add(cfs_waitq_t *waitq, cfs_waitlink_t *link)
70 {
71         add_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
72 }
73 EXPORT_SYMBOL(cfs_waitq_add);
74
75 void
76 cfs_waitq_add_exclusive(cfs_waitq_t *waitq,
77                              cfs_waitlink_t *link)
78 {
79         add_wait_queue_exclusive(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
80 }
81 EXPORT_SYMBOL(cfs_waitq_add_exclusive);
82
83 void
84 cfs_waitq_del(cfs_waitq_t *waitq, cfs_waitlink_t *link)
85 {
86         remove_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
87 }
88 EXPORT_SYMBOL(cfs_waitq_del);
89
90 int
91 cfs_waitq_active(cfs_waitq_t *waitq)
92 {
93         return waitqueue_active(LINUX_WAITQ_HEAD(waitq));
94 }
95 EXPORT_SYMBOL(cfs_waitq_active);
96
97 void
98 cfs_waitq_signal(cfs_waitq_t *waitq)
99 {
100         wake_up(LINUX_WAITQ_HEAD(waitq));
101 }
102 EXPORT_SYMBOL(cfs_waitq_signal);
103
104 void
105 cfs_waitq_signal_nr(cfs_waitq_t *waitq, int nr)
106 {
107         wake_up_nr(LINUX_WAITQ_HEAD(waitq), nr);
108 }
109 EXPORT_SYMBOL(cfs_waitq_signal_nr);
110
111 void
112 cfs_waitq_broadcast(cfs_waitq_t *waitq)
113 {
114         wake_up_all(LINUX_WAITQ_HEAD(waitq));
115 }
116 EXPORT_SYMBOL(cfs_waitq_broadcast);
117
118 void
119 cfs_waitq_wait(cfs_waitlink_t *link, cfs_task_state_t state)
120 {
121         schedule();
122 }
123 EXPORT_SYMBOL(cfs_waitq_wait);
124
125 int64_t
126 cfs_waitq_timedwait(cfs_waitlink_t *link, cfs_task_state_t state, int64_t timeout)
127 {
128         return schedule_timeout(timeout);
129 }
130 EXPORT_SYMBOL(cfs_waitq_timedwait);
131
132 void
133 cfs_schedule_timeout(cfs_task_state_t state, int64_t timeout)
134 {
135         set_current_state(state);
136         schedule_timeout(timeout);
137 }
138 EXPORT_SYMBOL(cfs_schedule_timeout);
139
140 void
141 cfs_schedule(void)
142 {
143         schedule();
144 }
145 EXPORT_SYMBOL(cfs_schedule);
146
147 /* deschedule for a bit... */
148 void
149 cfs_pause(cfs_duration_t ticks)
150 {
151         set_current_state(TASK_UNINTERRUPTIBLE);
152         schedule_timeout(ticks);
153 }
154 EXPORT_SYMBOL(cfs_pause);
155
156 int cfs_need_resched(void)
157 {
158         return need_resched();
159 }
160 EXPORT_SYMBOL(cfs_need_resched);
161
162 void cfs_cond_resched(void)
163 {
164         cond_resched();
165 }
166 EXPORT_SYMBOL(cfs_cond_resched);
167
168 void cfs_init_timer(cfs_timer_t *t)
169 {
170         init_timer(t);
171 }
172 EXPORT_SYMBOL(cfs_init_timer);
173
174 void cfs_timer_init(cfs_timer_t *t, cfs_timer_func_t *func, void *arg)
175 {
176         init_timer(t);
177         t->function = func;
178         t->data = (unsigned long)arg;
179 }
180 EXPORT_SYMBOL(cfs_timer_init);
181
182 void cfs_timer_done(cfs_timer_t *t)
183 {
184         return;
185 }
186 EXPORT_SYMBOL(cfs_timer_done);
187
188 void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
189 {
190         mod_timer(t, deadline);
191 }
192 EXPORT_SYMBOL(cfs_timer_arm);
193
194 void cfs_timer_disarm(cfs_timer_t *t)
195 {
196         del_timer(t);
197 }
198 EXPORT_SYMBOL(cfs_timer_disarm);
199
200 int  cfs_timer_is_armed(cfs_timer_t *t)
201 {
202         return timer_pending(t);
203 }
204 EXPORT_SYMBOL(cfs_timer_is_armed);
205
206 cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
207 {
208         return t->expires;
209 }
210 EXPORT_SYMBOL(cfs_timer_deadline);
211
212 void cfs_enter_debugger(void)
213 {
214 #if defined(CONFIG_KGDB)
215         BREAKPOINT();
216 #elif defined(__arch_um__)
217         asm("int $3");
218 #else
219         /* nothing */
220 #endif
221 }
222
223 void cfs_daemonize(char *str) {
224         unsigned long flags;
225
226         lock_kernel();
227         daemonize(str);
228         SIGNAL_MASK_LOCK(current, flags);
229         sigfillset(&current->blocked);
230         RECALC_SIGPENDING;
231         SIGNAL_MASK_UNLOCK(current, flags);
232         unlock_kernel();
233 }
234
235 int cfs_daemonize_ctxt(char *str) {
236         struct task_struct *tsk = current;
237         struct fs_struct *fs = NULL;
238
239         cfs_daemonize(str);
240         fs = copy_fs_struct(tsk->fs);
241         if (fs == NULL)
242                 return -ENOMEM;
243         exit_fs(tsk);
244         tsk->fs = fs;
245         return 0;
246 }
247
248
249 sigset_t
250 cfs_get_blockedsigs(void)
251 {
252         unsigned long          flags;
253         sigset_t        old;
254
255         SIGNAL_MASK_LOCK(current, flags);
256         old = current->blocked;
257         SIGNAL_MASK_UNLOCK(current, flags);
258         return old;
259 }
260
261 sigset_t
262 cfs_block_allsigs(void)
263 {
264         unsigned long          flags;
265         sigset_t        old;
266
267         SIGNAL_MASK_LOCK(current, flags);
268         old = current->blocked;
269         sigfillset(&current->blocked);
270         RECALC_SIGPENDING;
271         SIGNAL_MASK_UNLOCK(current, flags);
272
273         return old;
274 }
275
276 sigset_t
277 cfs_block_sigs(sigset_t bits)
278 {
279         unsigned long  flags;
280         sigset_t        old;
281
282         SIGNAL_MASK_LOCK(current, flags);
283         old = current->blocked;
284         current->blocked = bits;
285         RECALC_SIGPENDING;
286         SIGNAL_MASK_UNLOCK(current, flags);
287         return old;
288 }
289
290 void
291 cfs_restore_sigs (cfs_sigset_t old)
292 {
293         unsigned long  flags;
294
295         SIGNAL_MASK_LOCK(current, flags);
296         current->blocked = old;
297         RECALC_SIGPENDING;
298         SIGNAL_MASK_UNLOCK(current, flags);
299 }
300
301 int
302 cfs_signal_pending(void)
303 {
304         return signal_pending(current);
305 }
306
307 void
308 cfs_clear_sigpending(void)
309 {
310         unsigned long flags;
311
312         SIGNAL_MASK_LOCK(current, flags);
313         CLEAR_SIGPENDING;
314         SIGNAL_MASK_UNLOCK(current, flags);
315 }
316
317 int
318 libcfs_arch_init(void)
319 {
320         return 0;
321 }
322
323 void
324 libcfs_arch_cleanup(void)
325 {
326         return;
327 }
328
329 EXPORT_SYMBOL(libcfs_arch_init);
330 EXPORT_SYMBOL(libcfs_arch_cleanup);
331 EXPORT_SYMBOL(cfs_enter_debugger);
332 EXPORT_SYMBOL(cfs_daemonize);
333 EXPORT_SYMBOL(cfs_daemonize_ctxt);
334 EXPORT_SYMBOL(cfs_block_allsigs);
335 EXPORT_SYMBOL(cfs_block_sigs);
336 EXPORT_SYMBOL(cfs_get_blockedsigs);
337 EXPORT_SYMBOL(cfs_restore_sigs);
338 EXPORT_SYMBOL(cfs_signal_pending);
339 EXPORT_SYMBOL(cfs_clear_sigpending);