Whamcloud - gitweb
Land b_head_libcfs onto HEAD (20080805_1722)
[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 [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
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 void cfs_init_timer(cfs_timer_t *t)
157 {
158         init_timer(t);
159 }
160 EXPORT_SYMBOL(cfs_init_timer);
161
162 void cfs_timer_init(cfs_timer_t *t, cfs_timer_func_t *func, void *arg)
163 {
164         init_timer(t);
165         t->function = func;
166         t->data = (unsigned long)arg;
167 }
168 EXPORT_SYMBOL(cfs_timer_init);
169
170 void cfs_timer_done(cfs_timer_t *t)
171 {
172         return;
173 }
174 EXPORT_SYMBOL(cfs_timer_done);
175
176 void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
177 {
178         mod_timer(t, deadline);
179 }
180 EXPORT_SYMBOL(cfs_timer_arm);
181
182 void cfs_timer_disarm(cfs_timer_t *t)
183 {
184         del_timer(t);
185 }
186 EXPORT_SYMBOL(cfs_timer_disarm);
187
188 int  cfs_timer_is_armed(cfs_timer_t *t)
189 {
190         return timer_pending(t);
191 }
192 EXPORT_SYMBOL(cfs_timer_is_armed);
193
194 cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
195 {
196         return t->expires;
197 }
198 EXPORT_SYMBOL(cfs_timer_deadline);
199
200 void cfs_enter_debugger(void)
201 {
202 #if defined(CONFIG_KGDB)
203         BREAKPOINT();
204 #elif defined(__arch_um__)
205         asm("int $3");
206 #else
207         /* nothing */
208 #endif
209 }
210
211 void cfs_daemonize(char *str) {
212         unsigned long flags;
213
214         lock_kernel();
215 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,63))
216         daemonize(str);
217 #else
218         daemonize();
219         exit_files(current);
220         reparent_to_init();
221         snprintf (current->comm, sizeof (current->comm), "%s", str);
222 #endif
223         SIGNAL_MASK_LOCK(current, flags);
224         sigfillset(&current->blocked);
225         RECALC_SIGPENDING;
226         SIGNAL_MASK_UNLOCK(current, flags);
227         unlock_kernel();
228 }
229
230 int cfs_daemonize_ctxt(char *str) {
231         struct task_struct *tsk = current;
232         struct fs_struct *fs = NULL;
233
234         cfs_daemonize(str);
235         fs = copy_fs_struct(tsk->fs);
236         if (fs == NULL)
237                 return -ENOMEM;
238         exit_fs(tsk);
239         tsk->fs = fs;
240         return 0;
241 }
242
243
244 sigset_t
245 cfs_get_blockedsigs(void)
246 {
247         unsigned long          flags;
248         sigset_t        old;
249
250         SIGNAL_MASK_LOCK(current, flags);
251         old = current->blocked;
252         SIGNAL_MASK_UNLOCK(current, flags);
253         return old;
254 }
255
256 sigset_t
257 cfs_block_allsigs(void)
258 {
259         unsigned long          flags;
260         sigset_t        old;
261
262         SIGNAL_MASK_LOCK(current, flags);
263         old = current->blocked;
264         sigfillset(&current->blocked);
265         RECALC_SIGPENDING;
266         SIGNAL_MASK_UNLOCK(current, flags);
267
268         return old;
269 }
270
271 sigset_t
272 cfs_block_sigs(sigset_t bits)
273 {
274         unsigned long  flags;
275         sigset_t        old;
276
277         SIGNAL_MASK_LOCK(current, flags);
278         old = current->blocked;
279         current->blocked = bits;
280         RECALC_SIGPENDING;
281         SIGNAL_MASK_UNLOCK(current, flags);
282         return old;
283 }
284
285 void
286 cfs_restore_sigs (cfs_sigset_t old)
287 {
288         unsigned long  flags;
289
290         SIGNAL_MASK_LOCK(current, flags);
291         current->blocked = old;
292         RECALC_SIGPENDING;
293         SIGNAL_MASK_UNLOCK(current, flags);
294 }
295
296 int
297 cfs_signal_pending(void)
298 {
299         return signal_pending(current);
300 }
301
302 void
303 cfs_clear_sigpending(void)
304 {
305         unsigned long flags;
306
307         SIGNAL_MASK_LOCK(current, flags);
308         CLEAR_SIGPENDING;
309         SIGNAL_MASK_UNLOCK(current, flags);
310 }
311
312 int
313 libcfs_arch_init(void)
314 {
315         return 0;
316 }
317
318 void
319 libcfs_arch_cleanup(void)
320 {
321         return;
322 }
323
324 EXPORT_SYMBOL(libcfs_arch_init);
325 EXPORT_SYMBOL(libcfs_arch_cleanup);
326 EXPORT_SYMBOL(cfs_daemonize);
327 EXPORT_SYMBOL(cfs_daemonize_ctxt);
328 EXPORT_SYMBOL(cfs_block_allsigs);
329 EXPORT_SYMBOL(cfs_block_sigs);
330 EXPORT_SYMBOL(cfs_get_blockedsigs);
331 EXPORT_SYMBOL(cfs_restore_sigs);
332 EXPORT_SYMBOL(cfs_signal_pending);
333 EXPORT_SYMBOL(cfs_clear_sigpending);