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