Whamcloud - gitweb
9ebfe703756ccd07d8c8efc40d82f7ee99e285c3
[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 #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 #ifndef HAVE___ADD_WAIT_QUEUE_EXCLUSIVE
77
78 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
79                                               wait_queue_t *wait)
80 {
81         wait->flags |= WQ_FLAG_EXCLUSIVE;
82         __add_wait_queue(q, wait);
83 }
84
85 #endif /* HAVE___ADD_WAIT_QUEUE_EXCLUSIVE */
86
87 void
88 cfs_waitq_add_exclusive(cfs_waitq_t *waitq,
89                         cfs_waitlink_t *link)
90 {
91         add_wait_queue_exclusive(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
92 }
93 EXPORT_SYMBOL(cfs_waitq_add_exclusive);
94
95 /**
96  * wait_queue_t of Linux (version < 2.6.34) is a FIFO list for exclusively
97  * waiting threads, which is not always desirable because all threads will
98  * be waken up again and again, even user only needs a few of them to be
99  * active most time. This is not good for performance because cache can
100  * be polluted by different threads.
101  *
102  * LIFO list can resolve this problem because we always wakeup the most
103  * recent active thread by default.
104  *
105  * NB: please don't call non-exclusive & exclusive wait on the same
106  * waitq if cfs_waitq_add_exclusive_head is used.
107  */
108 void
109 cfs_waitq_add_exclusive_head(cfs_waitq_t *waitq, cfs_waitlink_t *link)
110 {
111         unsigned long flags;
112
113         spin_lock_irqsave(&LINUX_WAITQ_HEAD(waitq)->lock, flags);
114         __add_wait_queue_exclusive(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
115         spin_unlock_irqrestore(&LINUX_WAITQ_HEAD(waitq)->lock, flags);
116 }
117 EXPORT_SYMBOL(cfs_waitq_add_exclusive_head);
118
119 void
120 cfs_waitq_del(cfs_waitq_t *waitq, cfs_waitlink_t *link)
121 {
122         remove_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
123 }
124 EXPORT_SYMBOL(cfs_waitq_del);
125
126 int
127 cfs_waitq_active(cfs_waitq_t *waitq)
128 {
129         return waitqueue_active(LINUX_WAITQ_HEAD(waitq));
130 }
131 EXPORT_SYMBOL(cfs_waitq_active);
132
133 void
134 cfs_waitq_signal(cfs_waitq_t *waitq)
135 {
136         wake_up(LINUX_WAITQ_HEAD(waitq));
137 }
138 EXPORT_SYMBOL(cfs_waitq_signal);
139
140 void
141 cfs_waitq_signal_nr(cfs_waitq_t *waitq, int nr)
142 {
143         wake_up_nr(LINUX_WAITQ_HEAD(waitq), nr);
144 }
145 EXPORT_SYMBOL(cfs_waitq_signal_nr);
146
147 void
148 cfs_waitq_broadcast(cfs_waitq_t *waitq)
149 {
150         wake_up_all(LINUX_WAITQ_HEAD(waitq));
151 }
152 EXPORT_SYMBOL(cfs_waitq_broadcast);
153
154 void
155 cfs_waitq_wait(cfs_waitlink_t *link, cfs_task_state_t state)
156 {
157         schedule();
158 }
159 EXPORT_SYMBOL(cfs_waitq_wait);
160
161 int64_t
162 cfs_waitq_timedwait(cfs_waitlink_t *link, cfs_task_state_t state,
163                     int64_t timeout)
164 {
165         return schedule_timeout(timeout);
166 }
167 EXPORT_SYMBOL(cfs_waitq_timedwait);
168
169 void
170 cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t timeout)
171 {
172         set_current_state(state);
173         schedule_timeout(timeout);
174 }
175 EXPORT_SYMBOL(cfs_schedule_timeout_and_set_state);
176
177 void
178 cfs_schedule_timeout(int64_t timeout)
179 {
180         schedule_timeout(timeout);
181 }
182 EXPORT_SYMBOL(cfs_schedule_timeout);
183
184 void
185 cfs_schedule(void)
186 {
187         schedule();
188 }
189 EXPORT_SYMBOL(cfs_schedule);
190
191 /* deschedule for a bit... */
192 void
193 cfs_pause(cfs_duration_t ticks)
194 {
195         set_current_state(TASK_UNINTERRUPTIBLE);
196         schedule_timeout(ticks);
197 }
198 EXPORT_SYMBOL(cfs_pause);
199
200 int cfs_need_resched(void)
201 {
202         return need_resched();
203 }
204 EXPORT_SYMBOL(cfs_need_resched);
205
206 void cfs_cond_resched(void)
207 {
208         cond_resched();
209 }
210 EXPORT_SYMBOL(cfs_cond_resched);
211
212 void cfs_init_timer(cfs_timer_t *t)
213 {
214         init_timer(t);
215 }
216 EXPORT_SYMBOL(cfs_init_timer);
217
218 void cfs_timer_init(cfs_timer_t *t, cfs_timer_func_t *func, void *arg)
219 {
220         init_timer(t);
221         t->function = func;
222         t->data = (unsigned long)arg;
223 }
224 EXPORT_SYMBOL(cfs_timer_init);
225
226 void cfs_timer_done(cfs_timer_t *t)
227 {
228         return;
229 }
230 EXPORT_SYMBOL(cfs_timer_done);
231
232 void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
233 {
234         mod_timer(t, deadline);
235 }
236 EXPORT_SYMBOL(cfs_timer_arm);
237
238 void cfs_timer_disarm(cfs_timer_t *t)
239 {
240         del_timer(t);
241 }
242 EXPORT_SYMBOL(cfs_timer_disarm);
243
244 int  cfs_timer_is_armed(cfs_timer_t *t)
245 {
246         return timer_pending(t);
247 }
248 EXPORT_SYMBOL(cfs_timer_is_armed);
249
250 cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
251 {
252         return t->expires;
253 }
254 EXPORT_SYMBOL(cfs_timer_deadline);
255
256 void cfs_enter_debugger(void)
257 {
258 #if defined(CONFIG_KGDB)
259 //        BREAKPOINT();
260 #elif defined(__arch_um__)
261         asm("int $3");
262 #else
263         /* nothing */
264 #endif
265 }
266
267 void cfs_daemonize(char *str) {
268         unsigned long flags;
269
270         lock_kernel();
271         daemonize(str);
272         SIGNAL_MASK_LOCK(current, flags);
273         sigfillset(&current->blocked);
274         RECALC_SIGPENDING;
275         SIGNAL_MASK_UNLOCK(current, flags);
276         unlock_kernel();
277 }
278
279 int cfs_daemonize_ctxt(char *str) {
280
281         cfs_daemonize(str);
282 #ifndef HAVE_UNSHARE_FS_STRUCT
283         {
284         struct task_struct *tsk = current;
285         struct fs_struct *fs = NULL;
286         fs = copy_fs_struct(tsk->fs);
287         if (fs == NULL)
288                 return -ENOMEM;
289         exit_fs(tsk);
290         tsk->fs = fs;
291         }
292 #else
293         unshare_fs_struct();
294 #endif
295         return 0;
296 }
297
298 sigset_t
299 cfs_block_allsigs(void)
300 {
301         unsigned long          flags;
302         sigset_t        old;
303
304         SIGNAL_MASK_LOCK(current, flags);
305         old = current->blocked;
306         sigfillset(&current->blocked);
307         RECALC_SIGPENDING;
308         SIGNAL_MASK_UNLOCK(current, flags);
309
310         return old;
311 }
312
313 sigset_t
314 cfs_block_sigs(sigset_t bits)
315 {
316         unsigned long  flags;
317         sigset_t        old;
318
319         SIGNAL_MASK_LOCK(current, flags);
320         old = current->blocked;
321         current->blocked = bits;
322         RECALC_SIGPENDING;
323         SIGNAL_MASK_UNLOCK(current, flags);
324         return old;
325 }
326
327 /* Block all signals except for the @sigs */
328 cfs_sigset_t
329 cfs_block_sigsinv(unsigned long sigs)
330 {
331         unsigned long flags;
332         cfs_sigset_t old;
333
334         SIGNAL_MASK_LOCK(current, flags);
335         old = current->blocked;
336         siginitsetinv(&current->blocked, sigs);
337         RECALC_SIGPENDING;
338         SIGNAL_MASK_UNLOCK(current, flags);
339
340         return old;
341 }
342
343 void
344 cfs_restore_sigs (cfs_sigset_t old)
345 {
346         unsigned long  flags;
347
348         SIGNAL_MASK_LOCK(current, flags);
349         current->blocked = old;
350         RECALC_SIGPENDING;
351         SIGNAL_MASK_UNLOCK(current, flags);
352 }
353
354 int
355 cfs_signal_pending(void)
356 {
357         return signal_pending(current);
358 }
359
360 void
361 cfs_clear_sigpending(void)
362 {
363         unsigned long flags;
364
365         SIGNAL_MASK_LOCK(current, flags);
366         CLEAR_SIGPENDING;
367         SIGNAL_MASK_UNLOCK(current, flags);
368 }
369
370 int
371 libcfs_arch_init(void)
372 {
373         return 0;
374 }
375
376 void
377 libcfs_arch_cleanup(void)
378 {
379         return;
380 }
381
382 EXPORT_SYMBOL(libcfs_arch_init);
383 EXPORT_SYMBOL(libcfs_arch_cleanup);
384 EXPORT_SYMBOL(cfs_enter_debugger);
385 EXPORT_SYMBOL(cfs_daemonize);
386 EXPORT_SYMBOL(cfs_daemonize_ctxt);
387 EXPORT_SYMBOL(cfs_block_allsigs);
388 EXPORT_SYMBOL(cfs_block_sigs);
389 EXPORT_SYMBOL(cfs_block_sigsinv);
390 EXPORT_SYMBOL(cfs_restore_sigs);
391 EXPORT_SYMBOL(cfs_signal_pending);
392 EXPORT_SYMBOL(cfs_clear_sigpending);