Whamcloud - gitweb
3cb1a8ccd4649cb69f64e666ac320fbe1e1086b0
[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,
127                     int64_t timeout)
128 {
129         return schedule_timeout(timeout);
130 }
131 EXPORT_SYMBOL(cfs_waitq_timedwait);
132
133 void
134 cfs_schedule_timeout_and_set_state(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_and_set_state);
140
141 void
142 cfs_schedule_timeout(int64_t timeout)
143 {
144         schedule_timeout(timeout);
145 }
146 EXPORT_SYMBOL(cfs_schedule_timeout);
147
148 void
149 cfs_schedule(void)
150 {
151         schedule();
152 }
153 EXPORT_SYMBOL(cfs_schedule);
154
155 /* deschedule for a bit... */
156 void
157 cfs_pause(cfs_duration_t ticks)
158 {
159         set_current_state(TASK_UNINTERRUPTIBLE);
160         schedule_timeout(ticks);
161 }
162 EXPORT_SYMBOL(cfs_pause);
163
164 int cfs_need_resched(void)
165 {
166         return need_resched();
167 }
168 EXPORT_SYMBOL(cfs_need_resched);
169
170 void cfs_cond_resched(void)
171 {
172         cond_resched();
173 }
174 EXPORT_SYMBOL(cfs_cond_resched);
175
176 void cfs_init_timer(cfs_timer_t *t)
177 {
178         init_timer(t);
179 }
180 EXPORT_SYMBOL(cfs_init_timer);
181
182 void cfs_timer_init(cfs_timer_t *t, cfs_timer_func_t *func, void *arg)
183 {
184         init_timer(t);
185         t->function = func;
186         t->data = (unsigned long)arg;
187 }
188 EXPORT_SYMBOL(cfs_timer_init);
189
190 void cfs_timer_done(cfs_timer_t *t)
191 {
192         return;
193 }
194 EXPORT_SYMBOL(cfs_timer_done);
195
196 void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
197 {
198         mod_timer(t, deadline);
199 }
200 EXPORT_SYMBOL(cfs_timer_arm);
201
202 void cfs_timer_disarm(cfs_timer_t *t)
203 {
204         del_timer(t);
205 }
206 EXPORT_SYMBOL(cfs_timer_disarm);
207
208 int  cfs_timer_is_armed(cfs_timer_t *t)
209 {
210         return timer_pending(t);
211 }
212 EXPORT_SYMBOL(cfs_timer_is_armed);
213
214 cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
215 {
216         return t->expires;
217 }
218 EXPORT_SYMBOL(cfs_timer_deadline);
219
220 void cfs_enter_debugger(void)
221 {
222 #if defined(CONFIG_KGDB)
223 //        BREAKPOINT();
224 #elif defined(__arch_um__)
225         asm("int $3");
226 #else
227         /* nothing */
228 #endif
229 }
230
231 void cfs_daemonize(char *str) {
232         unsigned long flags;
233
234         lock_kernel();
235         daemonize(str);
236         SIGNAL_MASK_LOCK(current, flags);
237         sigfillset(&current->blocked);
238         RECALC_SIGPENDING;
239         SIGNAL_MASK_UNLOCK(current, flags);
240         unlock_kernel();
241 }
242
243 int cfs_daemonize_ctxt(char *str) {
244         struct task_struct *tsk = current;
245         struct fs_struct *fs = NULL;
246
247         cfs_daemonize(str);
248         fs = copy_fs_struct(tsk->fs);
249         if (fs == NULL)
250                 return -ENOMEM;
251         exit_fs(tsk);
252         tsk->fs = fs;
253         return 0;
254 }
255
256
257 sigset_t
258 cfs_get_blockedsigs(void)
259 {
260         unsigned long          flags;
261         sigset_t        old;
262
263         SIGNAL_MASK_LOCK(current, flags);
264         old = current->blocked;
265         SIGNAL_MASK_UNLOCK(current, flags);
266         return old;
267 }
268
269 sigset_t
270 cfs_block_allsigs(void)
271 {
272         unsigned long          flags;
273         sigset_t        old;
274
275         SIGNAL_MASK_LOCK(current, flags);
276         old = current->blocked;
277         sigfillset(&current->blocked);
278         RECALC_SIGPENDING;
279         SIGNAL_MASK_UNLOCK(current, flags);
280
281         return old;
282 }
283
284 sigset_t
285 cfs_block_sigs(sigset_t bits)
286 {
287         unsigned long  flags;
288         sigset_t        old;
289
290         SIGNAL_MASK_LOCK(current, flags);
291         old = current->blocked;
292         current->blocked = bits;
293         RECALC_SIGPENDING;
294         SIGNAL_MASK_UNLOCK(current, flags);
295         return old;
296 }
297
298 void
299 cfs_restore_sigs (cfs_sigset_t old)
300 {
301         unsigned long  flags;
302
303         SIGNAL_MASK_LOCK(current, flags);
304         current->blocked = old;
305         RECALC_SIGPENDING;
306         SIGNAL_MASK_UNLOCK(current, flags);
307 }
308
309 int
310 cfs_signal_pending(void)
311 {
312         return signal_pending(current);
313 }
314
315 void
316 cfs_clear_sigpending(void)
317 {
318         unsigned long flags;
319
320         SIGNAL_MASK_LOCK(current, flags);
321         CLEAR_SIGPENDING;
322         SIGNAL_MASK_UNLOCK(current, flags);
323 }
324
325 int
326 libcfs_arch_init(void)
327 {
328         return 0;
329 }
330
331 void
332 libcfs_arch_cleanup(void)
333 {
334         return;
335 }
336
337 EXPORT_SYMBOL(libcfs_arch_init);
338 EXPORT_SYMBOL(libcfs_arch_cleanup);
339 EXPORT_SYMBOL(cfs_enter_debugger);
340 EXPORT_SYMBOL(cfs_daemonize);
341 EXPORT_SYMBOL(cfs_daemonize_ctxt);
342 EXPORT_SYMBOL(cfs_block_allsigs);
343 EXPORT_SYMBOL(cfs_block_sigs);
344 EXPORT_SYMBOL(cfs_get_blockedsigs);
345 EXPORT_SYMBOL(cfs_restore_sigs);
346 EXPORT_SYMBOL(cfs_signal_pending);
347 EXPORT_SYMBOL(cfs_clear_sigpending);