Whamcloud - gitweb
aa9c37753ccac5f76c4c7fd1c3eee1e8532a1a4b
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-prim.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
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 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/fs_struct.h>
41 #include <linux/sched.h>
42
43 #include <libcfs/libcfs.h>
44
45 #if defined(CONFIG_KGDB)
46 #include <asm/kgdb.h>
47 #endif
48
49 #define LINUX_WAITQ(w) ((wait_queue_t *) w)
50 #define LINUX_WAITQ_HEAD(w) ((wait_queue_head_t *) w)
51
52 void
53 cfs_waitq_init(cfs_waitq_t *waitq)
54 {
55         init_waitqueue_head(LINUX_WAITQ_HEAD(waitq));
56 }
57 EXPORT_SYMBOL(cfs_waitq_init);
58
59 void
60 cfs_waitlink_init(cfs_waitlink_t *link)
61 {
62         init_waitqueue_entry(LINUX_WAITQ(link), current);
63 }
64 EXPORT_SYMBOL(cfs_waitlink_init);
65
66 void
67 cfs_waitq_add(cfs_waitq_t *waitq, cfs_waitlink_t *link)
68 {
69         add_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
70 }
71 EXPORT_SYMBOL(cfs_waitq_add);
72
73 #ifndef HAVE___ADD_WAIT_QUEUE_EXCLUSIVE
74
75 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
76                                               wait_queue_t *wait)
77 {
78         wait->flags |= WQ_FLAG_EXCLUSIVE;
79         __add_wait_queue(q, wait);
80 }
81
82 #endif /* HAVE___ADD_WAIT_QUEUE_EXCLUSIVE */
83
84 void
85 cfs_waitq_add_exclusive(cfs_waitq_t *waitq,
86                         cfs_waitlink_t *link)
87 {
88         add_wait_queue_exclusive(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
89 }
90 EXPORT_SYMBOL(cfs_waitq_add_exclusive);
91
92 /**
93  * wait_queue_t of Linux (version < 2.6.34) is a FIFO list for exclusively
94  * waiting threads, which is not always desirable because all threads will
95  * be waken up again and again, even user only needs a few of them to be
96  * active most time. This is not good for performance because cache can
97  * be polluted by different threads.
98  *
99  * LIFO list can resolve this problem because we always wakeup the most
100  * recent active thread by default.
101  *
102  * NB: please don't call non-exclusive & exclusive wait on the same
103  * waitq if cfs_waitq_add_exclusive_head is used.
104  */
105 void
106 cfs_waitq_add_exclusive_head(cfs_waitq_t *waitq, cfs_waitlink_t *link)
107 {
108         unsigned long flags;
109
110         spin_lock_irqsave(&LINUX_WAITQ_HEAD(waitq)->lock, flags);
111         __add_wait_queue_exclusive(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
112         spin_unlock_irqrestore(&LINUX_WAITQ_HEAD(waitq)->lock, flags);
113 }
114 EXPORT_SYMBOL(cfs_waitq_add_exclusive_head);
115
116 void
117 cfs_waitq_del(cfs_waitq_t *waitq, cfs_waitlink_t *link)
118 {
119         remove_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
120 }
121 EXPORT_SYMBOL(cfs_waitq_del);
122
123 int
124 cfs_waitq_active(cfs_waitq_t *waitq)
125 {
126         return waitqueue_active(LINUX_WAITQ_HEAD(waitq));
127 }
128 EXPORT_SYMBOL(cfs_waitq_active);
129
130 void
131 cfs_waitq_signal(cfs_waitq_t *waitq)
132 {
133         wake_up(LINUX_WAITQ_HEAD(waitq));
134 }
135 EXPORT_SYMBOL(cfs_waitq_signal);
136
137 void
138 cfs_waitq_signal_nr(cfs_waitq_t *waitq, int nr)
139 {
140         wake_up_nr(LINUX_WAITQ_HEAD(waitq), nr);
141 }
142 EXPORT_SYMBOL(cfs_waitq_signal_nr);
143
144 void
145 cfs_waitq_broadcast(cfs_waitq_t *waitq)
146 {
147         wake_up_all(LINUX_WAITQ_HEAD(waitq));
148 }
149 EXPORT_SYMBOL(cfs_waitq_broadcast);
150
151 void
152 cfs_waitq_wait(cfs_waitlink_t *link, cfs_task_state_t state)
153 {
154         schedule();
155 }
156 EXPORT_SYMBOL(cfs_waitq_wait);
157
158 int64_t
159 cfs_waitq_timedwait(cfs_waitlink_t *link, cfs_task_state_t state,
160                     int64_t timeout)
161 {
162         return schedule_timeout(timeout);
163 }
164 EXPORT_SYMBOL(cfs_waitq_timedwait);
165
166 void
167 cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t timeout)
168 {
169         set_current_state(state);
170         schedule_timeout(timeout);
171 }
172 EXPORT_SYMBOL(cfs_schedule_timeout_and_set_state);
173
174 void
175 cfs_schedule_timeout(int64_t timeout)
176 {
177         schedule_timeout(timeout);
178 }
179 EXPORT_SYMBOL(cfs_schedule_timeout);
180
181 void
182 cfs_schedule(void)
183 {
184         schedule();
185 }
186 EXPORT_SYMBOL(cfs_schedule);
187
188 /* deschedule for a bit... */
189 void
190 cfs_pause(cfs_duration_t ticks)
191 {
192         set_current_state(TASK_UNINTERRUPTIBLE);
193         schedule_timeout(ticks);
194 }
195 EXPORT_SYMBOL(cfs_pause);
196
197 int cfs_need_resched(void)
198 {
199         return need_resched();
200 }
201 EXPORT_SYMBOL(cfs_need_resched);
202
203 void cfs_cond_resched(void)
204 {
205         cond_resched();
206 }
207 EXPORT_SYMBOL(cfs_cond_resched);
208
209 void cfs_init_timer(cfs_timer_t *t)
210 {
211         init_timer(t);
212 }
213 EXPORT_SYMBOL(cfs_init_timer);
214
215 void cfs_timer_init(cfs_timer_t *t, cfs_timer_func_t *func, void *arg)
216 {
217         init_timer(t);
218         t->function = func;
219         t->data = (unsigned long)arg;
220 }
221 EXPORT_SYMBOL(cfs_timer_init);
222
223 void cfs_timer_done(cfs_timer_t *t)
224 {
225         return;
226 }
227 EXPORT_SYMBOL(cfs_timer_done);
228
229 void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
230 {
231         mod_timer(t, deadline);
232 }
233 EXPORT_SYMBOL(cfs_timer_arm);
234
235 void cfs_timer_disarm(cfs_timer_t *t)
236 {
237         del_timer(t);
238 }
239 EXPORT_SYMBOL(cfs_timer_disarm);
240
241 int  cfs_timer_is_armed(cfs_timer_t *t)
242 {
243         return timer_pending(t);
244 }
245 EXPORT_SYMBOL(cfs_timer_is_armed);
246
247 cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
248 {
249         return t->expires;
250 }
251 EXPORT_SYMBOL(cfs_timer_deadline);
252
253 void cfs_enter_debugger(void)
254 {
255 #if defined(CONFIG_KGDB)
256 //        BREAKPOINT();
257 #elif defined(__arch_um__)
258         asm("int $3");
259 #else
260         /* nothing */
261 #endif
262 }
263
264 void cfs_daemonize(char *str) {
265         unsigned long flags;
266
267         daemonize(str);
268         SIGNAL_MASK_LOCK(current, flags);
269         sigfillset(&current->blocked);
270         RECALC_SIGPENDING;
271         SIGNAL_MASK_UNLOCK(current, flags);
272 }
273
274 int cfs_daemonize_ctxt(char *str) {
275
276         cfs_daemonize(str);
277 #ifndef HAVE_UNSHARE_FS_STRUCT
278         {
279         struct task_struct *tsk = current;
280         struct fs_struct *fs = NULL;
281         fs = copy_fs_struct(tsk->fs);
282         if (fs == NULL)
283                 return -ENOMEM;
284         exit_fs(tsk);
285         tsk->fs = fs;
286         }
287 #else
288         unshare_fs_struct();
289 #endif
290         return 0;
291 }
292
293 sigset_t
294 cfs_block_allsigs(void)
295 {
296         unsigned long          flags;
297         sigset_t        old;
298
299         SIGNAL_MASK_LOCK(current, flags);
300         old = current->blocked;
301         sigfillset(&current->blocked);
302         RECALC_SIGPENDING;
303         SIGNAL_MASK_UNLOCK(current, flags);
304
305         return old;
306 }
307
308 sigset_t cfs_block_sigs(unsigned long sigs)
309 {
310         unsigned long  flags;
311         sigset_t        old;
312
313         SIGNAL_MASK_LOCK(current, flags);
314         old = current->blocked;
315         sigaddsetmask(&current->blocked, sigs);
316         RECALC_SIGPENDING;
317         SIGNAL_MASK_UNLOCK(current, flags);
318         return old;
319 }
320
321 /* Block all signals except for the @sigs */
322 sigset_t cfs_block_sigsinv(unsigned long sigs)
323 {
324         unsigned long flags;
325         sigset_t old;
326
327         SIGNAL_MASK_LOCK(current, flags);
328         old = current->blocked;
329         sigaddsetmask(&current->blocked, ~sigs);
330         RECALC_SIGPENDING;
331         SIGNAL_MASK_UNLOCK(current, flags);
332
333         return old;
334 }
335
336 void
337 cfs_restore_sigs (cfs_sigset_t old)
338 {
339         unsigned long  flags;
340
341         SIGNAL_MASK_LOCK(current, flags);
342         current->blocked = old;
343         RECALC_SIGPENDING;
344         SIGNAL_MASK_UNLOCK(current, flags);
345 }
346
347 int
348 cfs_signal_pending(void)
349 {
350         return signal_pending(current);
351 }
352
353 void
354 cfs_clear_sigpending(void)
355 {
356         unsigned long flags;
357
358         SIGNAL_MASK_LOCK(current, flags);
359         CLEAR_SIGPENDING;
360         SIGNAL_MASK_UNLOCK(current, flags);
361 }
362
363 int
364 libcfs_arch_init(void)
365 {
366         return 0;
367 }
368
369 void
370 libcfs_arch_cleanup(void)
371 {
372         return;
373 }
374
375 EXPORT_SYMBOL(libcfs_arch_init);
376 EXPORT_SYMBOL(libcfs_arch_cleanup);
377 EXPORT_SYMBOL(cfs_enter_debugger);
378 EXPORT_SYMBOL(cfs_daemonize);
379 EXPORT_SYMBOL(cfs_daemonize_ctxt);
380 EXPORT_SYMBOL(cfs_block_allsigs);
381 EXPORT_SYMBOL(cfs_block_sigs);
382 EXPORT_SYMBOL(cfs_block_sigsinv);
383 EXPORT_SYMBOL(cfs_restore_sigs);
384 EXPORT_SYMBOL(cfs_signal_pending);
385 EXPORT_SYMBOL(cfs_clear_sigpending);