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, 2012, Intel Corporation.
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 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/fs_struct.h>
41 #include <linux/sched.h>
43 #include <libcfs/libcfs.h>
45 #if defined(CONFIG_KGDB)
49 #define LINUX_WAITQ(w) ((wait_queue_t *) w)
50 #define LINUX_WAITQ_HEAD(w) ((wait_queue_head_t *) w)
53 cfs_waitq_init(cfs_waitq_t *waitq)
55 init_waitqueue_head(LINUX_WAITQ_HEAD(waitq));
57 EXPORT_SYMBOL(cfs_waitq_init);
60 cfs_waitlink_init(cfs_waitlink_t *link)
62 init_waitqueue_entry(LINUX_WAITQ(link), current);
64 EXPORT_SYMBOL(cfs_waitlink_init);
67 cfs_waitq_add(cfs_waitq_t *waitq, cfs_waitlink_t *link)
69 add_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
71 EXPORT_SYMBOL(cfs_waitq_add);
73 #ifndef HAVE___ADD_WAIT_QUEUE_EXCLUSIVE
75 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
78 wait->flags |= WQ_FLAG_EXCLUSIVE;
79 __add_wait_queue(q, wait);
82 #endif /* HAVE___ADD_WAIT_QUEUE_EXCLUSIVE */
85 cfs_waitq_add_exclusive(cfs_waitq_t *waitq,
88 add_wait_queue_exclusive(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
90 EXPORT_SYMBOL(cfs_waitq_add_exclusive);
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.
99 * LIFO list can resolve this problem because we always wakeup the most
100 * recent active thread by default.
102 * NB: please don't call non-exclusive & exclusive wait on the same
103 * waitq if cfs_waitq_add_exclusive_head is used.
106 cfs_waitq_add_exclusive_head(cfs_waitq_t *waitq, cfs_waitlink_t *link)
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);
114 EXPORT_SYMBOL(cfs_waitq_add_exclusive_head);
117 cfs_waitq_del(cfs_waitq_t *waitq, cfs_waitlink_t *link)
119 remove_wait_queue(LINUX_WAITQ_HEAD(waitq), LINUX_WAITQ(link));
121 EXPORT_SYMBOL(cfs_waitq_del);
124 cfs_waitq_active(cfs_waitq_t *waitq)
126 return waitqueue_active(LINUX_WAITQ_HEAD(waitq));
128 EXPORT_SYMBOL(cfs_waitq_active);
131 cfs_waitq_signal(cfs_waitq_t *waitq)
133 wake_up(LINUX_WAITQ_HEAD(waitq));
135 EXPORT_SYMBOL(cfs_waitq_signal);
138 cfs_waitq_signal_nr(cfs_waitq_t *waitq, int nr)
140 wake_up_nr(LINUX_WAITQ_HEAD(waitq), nr);
142 EXPORT_SYMBOL(cfs_waitq_signal_nr);
145 cfs_waitq_broadcast(cfs_waitq_t *waitq)
147 wake_up_all(LINUX_WAITQ_HEAD(waitq));
149 EXPORT_SYMBOL(cfs_waitq_broadcast);
152 cfs_waitq_wait(cfs_waitlink_t *link, cfs_task_state_t state)
156 EXPORT_SYMBOL(cfs_waitq_wait);
159 cfs_waitq_timedwait(cfs_waitlink_t *link, cfs_task_state_t state,
162 return schedule_timeout(timeout);
164 EXPORT_SYMBOL(cfs_waitq_timedwait);
167 cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t timeout)
169 set_current_state(state);
170 schedule_timeout(timeout);
172 EXPORT_SYMBOL(cfs_schedule_timeout_and_set_state);
175 cfs_schedule_timeout(int64_t timeout)
177 schedule_timeout(timeout);
179 EXPORT_SYMBOL(cfs_schedule_timeout);
186 EXPORT_SYMBOL(cfs_schedule);
188 /* deschedule for a bit... */
190 cfs_pause(cfs_duration_t ticks)
192 set_current_state(TASK_UNINTERRUPTIBLE);
193 schedule_timeout(ticks);
195 EXPORT_SYMBOL(cfs_pause);
197 int cfs_need_resched(void)
199 return need_resched();
201 EXPORT_SYMBOL(cfs_need_resched);
203 void cfs_cond_resched(void)
207 EXPORT_SYMBOL(cfs_cond_resched);
209 void cfs_init_timer(cfs_timer_t *t)
213 EXPORT_SYMBOL(cfs_init_timer);
215 void cfs_timer_init(cfs_timer_t *t, cfs_timer_func_t *func, void *arg)
219 t->data = (unsigned long)arg;
221 EXPORT_SYMBOL(cfs_timer_init);
223 void cfs_timer_done(cfs_timer_t *t)
227 EXPORT_SYMBOL(cfs_timer_done);
229 void cfs_timer_arm(cfs_timer_t *t, cfs_time_t deadline)
231 mod_timer(t, deadline);
233 EXPORT_SYMBOL(cfs_timer_arm);
235 void cfs_timer_disarm(cfs_timer_t *t)
239 EXPORT_SYMBOL(cfs_timer_disarm);
241 int cfs_timer_is_armed(cfs_timer_t *t)
243 return timer_pending(t);
245 EXPORT_SYMBOL(cfs_timer_is_armed);
247 cfs_time_t cfs_timer_deadline(cfs_timer_t *t)
251 EXPORT_SYMBOL(cfs_timer_deadline);
253 void cfs_enter_debugger(void)
255 #if defined(CONFIG_KGDB)
262 void cfs_daemonize(char *str) {
266 SIGNAL_MASK_LOCK(current, flags);
267 sigfillset(¤t->blocked);
269 SIGNAL_MASK_UNLOCK(current, flags);
272 int cfs_daemonize_ctxt(char *str) {
275 #ifndef HAVE_UNSHARE_FS_STRUCT
277 struct task_struct *tsk = current;
278 struct fs_struct *fs = NULL;
279 fs = copy_fs_struct(tsk->fs);
292 cfs_block_allsigs(void)
297 SIGNAL_MASK_LOCK(current, flags);
298 old = current->blocked;
299 sigfillset(¤t->blocked);
301 SIGNAL_MASK_UNLOCK(current, flags);
306 sigset_t cfs_block_sigs(unsigned long sigs)
311 SIGNAL_MASK_LOCK(current, flags);
312 old = current->blocked;
313 sigaddsetmask(¤t->blocked, sigs);
315 SIGNAL_MASK_UNLOCK(current, flags);
319 /* Block all signals except for the @sigs */
320 sigset_t cfs_block_sigsinv(unsigned long sigs)
325 SIGNAL_MASK_LOCK(current, flags);
326 old = current->blocked;
327 sigaddsetmask(¤t->blocked, ~sigs);
329 SIGNAL_MASK_UNLOCK(current, flags);
335 cfs_restore_sigs (cfs_sigset_t old)
339 SIGNAL_MASK_LOCK(current, flags);
340 current->blocked = old;
342 SIGNAL_MASK_UNLOCK(current, flags);
346 cfs_signal_pending(void)
348 return signal_pending(current);
352 cfs_clear_sigpending(void)
356 SIGNAL_MASK_LOCK(current, flags);
358 SIGNAL_MASK_UNLOCK(current, flags);
362 libcfs_arch_init(void)
368 libcfs_arch_cleanup(void)
373 EXPORT_SYMBOL(libcfs_arch_init);
374 EXPORT_SYMBOL(libcfs_arch_cleanup);
375 EXPORT_SYMBOL(cfs_enter_debugger);
376 EXPORT_SYMBOL(cfs_daemonize);
377 EXPORT_SYMBOL(cfs_daemonize_ctxt);
378 EXPORT_SYMBOL(cfs_block_allsigs);
379 EXPORT_SYMBOL(cfs_block_sigs);
380 EXPORT_SYMBOL(cfs_block_sigsinv);
381 EXPORT_SYMBOL(cfs_restore_sigs);
382 EXPORT_SYMBOL(cfs_signal_pending);
383 EXPORT_SYMBOL(cfs_clear_sigpending);