4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
38 #ifndef AUTOCONF_INCLUDED
39 #include <linux/config.h>
41 #include <linux/module.h>
42 #include <linux/kernel.h>
43 #include <linux/fs_struct.h>
44 #include <linux/sched.h>
46 #include <libcfs/libcfs.h>
48 #if defined(CONFIG_KGDB)
52 #define LINUX_WAITQ(w) ((wait_queue_t *) w)
53 #define LINUX_WAITQ_HEAD(w) ((wait_queue_head_t *) w)
56 cfs_waitq_init(cfs_waitq_t *waitq)
58 init_waitqueue_head(LINUX_WAITQ_HEAD(waitq));
60 EXPORT_SYMBOL(cfs_waitq_init);
63 cfs_waitlink_init(cfs_waitlink_t *link)
65 init_waitqueue_entry(LINUX_WAITQ(link), current);
67 EXPORT_SYMBOL(cfs_waitlink_init);
70 cfs_waitq_add(cfs_waitq_t *waitq, cfs_waitlink_t *link)
72 add_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
74 EXPORT_SYMBOL(cfs_waitq_add);
76 #ifndef HAVE___ADD_WAIT_QUEUE_EXCLUSIVE
78 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
81 wait->flags |= WQ_FLAG_EXCLUSIVE;
82 __add_wait_queue(q, wait);
85 #endif /* HAVE___ADD_WAIT_QUEUE_EXCLUSIVE */
88 cfs_waitq_add_exclusive(cfs_waitq_t *waitq,
91 add_wait_queue_exclusive(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
93 EXPORT_SYMBOL(cfs_waitq_add_exclusive);
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.
102 * LIFO list can resolve this problem because we always wakeup the most
103 * recent active thread by default.
105 * NB: please don't call non-exclusive & exclusive wait on the same
106 * waitq if cfs_waitq_add_exclusive_head is used.
109 cfs_waitq_add_exclusive_head(cfs_waitq_t *waitq, cfs_waitlink_t *link)
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);
117 EXPORT_SYMBOL(cfs_waitq_add_exclusive_head);
120 cfs_waitq_del(cfs_waitq_t *waitq, cfs_waitlink_t *link)
122 remove_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
124 EXPORT_SYMBOL(cfs_waitq_del);
127 cfs_waitq_active(cfs_waitq_t *waitq)
129 return waitqueue_active(LINUX_WAITQ_HEAD(waitq));
131 EXPORT_SYMBOL(cfs_waitq_active);
134 cfs_waitq_signal(cfs_waitq_t *waitq)
136 wake_up(LINUX_WAITQ_HEAD(waitq));
138 EXPORT_SYMBOL(cfs_waitq_signal);
141 cfs_waitq_signal_nr(cfs_waitq_t *waitq, int nr)
143 wake_up_nr(LINUX_WAITQ_HEAD(waitq), nr);
145 EXPORT_SYMBOL(cfs_waitq_signal_nr);
148 cfs_waitq_broadcast(cfs_waitq_t *waitq)
150 wake_up_all(LINUX_WAITQ_HEAD(waitq));
152 EXPORT_SYMBOL(cfs_waitq_broadcast);
155 cfs_waitq_wait(cfs_waitlink_t *link, cfs_task_state_t state)
159 EXPORT_SYMBOL(cfs_waitq_wait);
162 cfs_waitq_timedwait(cfs_waitlink_t *link, cfs_task_state_t state,
165 return schedule_timeout(timeout);
167 EXPORT_SYMBOL(cfs_waitq_timedwait);
170 cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t timeout)
172 set_current_state(state);
173 schedule_timeout(timeout);
175 EXPORT_SYMBOL(cfs_schedule_timeout_and_set_state);
178 cfs_schedule_timeout(int64_t timeout)
180 schedule_timeout(timeout);
182 EXPORT_SYMBOL(cfs_schedule_timeout);
189 EXPORT_SYMBOL(cfs_schedule);
191 /* deschedule for a bit... */
193 cfs_pause(cfs_duration_t ticks)
195 set_current_state(TASK_UNINTERRUPTIBLE);
196 schedule_timeout(ticks);
198 EXPORT_SYMBOL(cfs_pause);
200 int cfs_need_resched(void)
202 return need_resched();
204 EXPORT_SYMBOL(cfs_need_resched);
206 void cfs_cond_resched(void)
210 EXPORT_SYMBOL(cfs_cond_resched);
212 void cfs_init_timer(cfs_timer_t *t)
216 EXPORT_SYMBOL(cfs_init_timer);
218 void cfs_timer_init(cfs_timer_t *t, cfs_timer_func_t *func, void *arg)
222 t->data = (unsigned long)arg;
224 EXPORT_SYMBOL(cfs_timer_init);
226 void cfs_timer_done(cfs_timer_t *t)
230 EXPORT_SYMBOL(cfs_timer_done);
232 void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
234 mod_timer(t, deadline);
236 EXPORT_SYMBOL(cfs_timer_arm);
238 void cfs_timer_disarm(cfs_timer_t *t)
242 EXPORT_SYMBOL(cfs_timer_disarm);
244 int cfs_timer_is_armed(cfs_timer_t *t)
246 return timer_pending(t);
248 EXPORT_SYMBOL(cfs_timer_is_armed);
250 cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
254 EXPORT_SYMBOL(cfs_timer_deadline);
256 void cfs_enter_debugger(void)
258 #if defined(CONFIG_KGDB)
260 #elif defined(__arch_um__)
267 void cfs_daemonize(char *str) {
271 SIGNAL_MASK_LOCK(current, flags);
272 sigfillset(¤t->blocked);
274 SIGNAL_MASK_UNLOCK(current, flags);
277 int cfs_daemonize_ctxt(char *str) {
280 #ifndef HAVE_UNSHARE_FS_STRUCT
282 struct task_struct *tsk = current;
283 struct fs_struct *fs = NULL;
284 fs = copy_fs_struct(tsk->fs);
297 cfs_block_allsigs(void)
302 SIGNAL_MASK_LOCK(current, flags);
303 old = current->blocked;
304 sigfillset(¤t->blocked);
306 SIGNAL_MASK_UNLOCK(current, flags);
311 sigset_t cfs_block_sigs(unsigned long sigs)
316 SIGNAL_MASK_LOCK(current, flags);
317 old = current->blocked;
318 sigaddsetmask(¤t->blocked, sigs);
320 SIGNAL_MASK_UNLOCK(current, flags);
324 /* Block all signals except for the @sigs */
325 sigset_t cfs_block_sigsinv(unsigned long sigs)
330 SIGNAL_MASK_LOCK(current, flags);
331 old = current->blocked;
332 sigaddsetmask(¤t->blocked, ~sigs);
334 SIGNAL_MASK_UNLOCK(current, flags);
340 cfs_restore_sigs (cfs_sigset_t old)
344 SIGNAL_MASK_LOCK(current, flags);
345 current->blocked = old;
347 SIGNAL_MASK_UNLOCK(current, flags);
351 cfs_signal_pending(void)
353 return signal_pending(current);
357 cfs_clear_sigpending(void)
361 SIGNAL_MASK_LOCK(current, flags);
363 SIGNAL_MASK_UNLOCK(current, flags);
367 libcfs_arch_init(void)
373 libcfs_arch_cleanup(void)
378 EXPORT_SYMBOL(libcfs_arch_init);
379 EXPORT_SYMBOL(libcfs_arch_cleanup);
380 EXPORT_SYMBOL(cfs_enter_debugger);
381 EXPORT_SYMBOL(cfs_daemonize);
382 EXPORT_SYMBOL(cfs_daemonize_ctxt);
383 EXPORT_SYMBOL(cfs_block_allsigs);
384 EXPORT_SYMBOL(cfs_block_sigs);
385 EXPORT_SYMBOL(cfs_block_sigsinv);
386 EXPORT_SYMBOL(cfs_restore_sigs);
387 EXPORT_SYMBOL(cfs_signal_pending);
388 EXPORT_SYMBOL(cfs_clear_sigpending);