Whamcloud - gitweb
LU-3030 build: Update Master Copyrights pre 2.4 split
[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, 2013, Intel Corporation.
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 #else
258         /* nothing */
259 #endif
260 }
261
262 void cfs_daemonize(char *str) {
263         unsigned long flags;
264
265         daemonize(str);
266         SIGNAL_MASK_LOCK(current, flags);
267         sigfillset(&current->blocked);
268         RECALC_SIGPENDING;
269         SIGNAL_MASK_UNLOCK(current, flags);
270 }
271
272 int cfs_daemonize_ctxt(char *str) {
273
274         cfs_daemonize(str);
275 #ifndef HAVE_UNSHARE_FS_STRUCT
276         {
277         struct task_struct *tsk = current;
278         struct fs_struct *fs = NULL;
279         fs = copy_fs_struct(tsk->fs);
280         if (fs == NULL)
281                 return -ENOMEM;
282         exit_fs(tsk);
283         tsk->fs = fs;
284         }
285 #else
286         unshare_fs_struct();
287 #endif
288         return 0;
289 }
290
291 sigset_t
292 cfs_block_allsigs(void)
293 {
294         unsigned long          flags;
295         sigset_t        old;
296
297         SIGNAL_MASK_LOCK(current, flags);
298         old = current->blocked;
299         sigfillset(&current->blocked);
300         RECALC_SIGPENDING;
301         SIGNAL_MASK_UNLOCK(current, flags);
302
303         return old;
304 }
305
306 sigset_t cfs_block_sigs(unsigned long sigs)
307 {
308         unsigned long  flags;
309         sigset_t        old;
310
311         SIGNAL_MASK_LOCK(current, flags);
312         old = current->blocked;
313         sigaddsetmask(&current->blocked, sigs);
314         RECALC_SIGPENDING;
315         SIGNAL_MASK_UNLOCK(current, flags);
316         return old;
317 }
318
319 /* Block all signals except for the @sigs */
320 sigset_t cfs_block_sigsinv(unsigned long sigs)
321 {
322         unsigned long flags;
323         sigset_t old;
324
325         SIGNAL_MASK_LOCK(current, flags);
326         old = current->blocked;
327         sigaddsetmask(&current->blocked, ~sigs);
328         RECALC_SIGPENDING;
329         SIGNAL_MASK_UNLOCK(current, flags);
330
331         return old;
332 }
333
334 void
335 cfs_restore_sigs (cfs_sigset_t old)
336 {
337         unsigned long  flags;
338
339         SIGNAL_MASK_LOCK(current, flags);
340         current->blocked = old;
341         RECALC_SIGPENDING;
342         SIGNAL_MASK_UNLOCK(current, flags);
343 }
344
345 int
346 cfs_signal_pending(void)
347 {
348         return signal_pending(current);
349 }
350
351 void
352 cfs_clear_sigpending(void)
353 {
354         unsigned long flags;
355
356         SIGNAL_MASK_LOCK(current, flags);
357         CLEAR_SIGPENDING;
358         SIGNAL_MASK_UNLOCK(current, flags);
359 }
360
361 int
362 libcfs_arch_init(void)
363 {
364         return 0;
365 }
366
367 void
368 libcfs_arch_cleanup(void)
369 {
370         return;
371 }
372
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);