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