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