Whamcloud - gitweb
LU-506 kernel: FC15 - small changes
[fs/lustre-release.git] / libcfs / libcfs / user-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 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/user-prim.c
37  *
38  * Implementations of portable APIs for liblustre
39  *
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  */
42
43 /*
44  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
45  */
46
47 #ifndef __KERNEL__
48
49 #include <libcfs/libcfs.h>
50
51 /*
52  * Wait queue. No-op implementation.
53  */
54
55 void cfs_waitq_init(struct cfs_waitq *waitq)
56 {
57         LASSERT(waitq != NULL);
58         (void)waitq;
59 }
60
61 void cfs_waitlink_init(struct cfs_waitlink *link)
62 {
63         LASSERT(link != NULL);
64         (void)link;
65 }
66
67 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
68 {
69         LASSERT(waitq != NULL);
70         LASSERT(link != NULL);
71         (void)waitq;
72         (void)link;
73 }
74
75 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
76 {
77         LASSERT(waitq != NULL);
78         LASSERT(link != NULL);
79         (void)waitq;
80         (void)link;
81 }
82
83 void cfs_waitq_add_exclusive_head(struct cfs_waitq *waitq, struct cfs_waitlink *link)
84 {
85         cfs_waitq_add_exclusive(waitq, link);
86 }
87
88 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
89 {
90         LASSERT(waitq != NULL);
91         LASSERT(link != NULL);
92         (void)waitq;
93         (void)link;
94 }
95
96 int cfs_waitq_active(struct cfs_waitq *waitq)
97 {
98         LASSERT(waitq != NULL);
99         (void)waitq;
100         return 0;
101 }
102
103 void cfs_waitq_signal(struct cfs_waitq *waitq)
104 {
105         LASSERT(waitq != NULL);
106         (void)waitq;
107 }
108
109 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
110 {
111         LASSERT(waitq != NULL);
112         (void)waitq;
113 }
114
115 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
116 {
117         LASSERT(waitq != NULL);
118         (void)waitq;
119 }
120
121 void cfs_waitq_wait(struct cfs_waitlink *link, cfs_task_state_t state)
122 {
123         LASSERT(link != NULL);
124         (void)link;
125
126         /* well, wait for something to happen */
127         cfs_call_wait_handler(0);
128 }
129
130 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, cfs_task_state_t state,
131                             int64_t timeout)
132 {
133         LASSERT(link != NULL);
134         (void)link;
135         cfs_call_wait_handler(timeout);
136         return 0;
137 }
138
139 void cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t timeout)
140 {
141         cfs_waitlink_t    l;
142         /* sleep(timeout) here instead? */
143         cfs_waitq_timedwait(&l, state, timeout);
144 }
145
146 void
147 cfs_pause(cfs_duration_t d)
148 {
149         struct timespec s;
150
151         cfs_duration_nsec(d, &s);
152         nanosleep(&s, NULL);
153 }
154
155 int cfs_need_resched(void)
156 {
157         return 0;
158 }
159
160 void cfs_cond_resched(void)
161 {
162 }
163
164 /*
165  * Timer
166  */
167
168 void cfs_init_timer(cfs_timer_t *t)
169 {
170         CFS_INIT_LIST_HEAD(&t->tl_list);
171 }
172
173 void cfs_timer_init(cfs_timer_t *l, cfs_timer_func_t *func, void *arg)
174 {
175         CFS_INIT_LIST_HEAD(&l->tl_list);
176         l->function = func;
177         l->data = (ulong_ptr_t)arg;
178         return;
179 }
180
181 int cfs_timer_is_armed(cfs_timer_t *l)
182 {
183         if (cfs_time_before(cfs_time_current(), l->expires))
184                 return 1;
185         else
186                 return 0;
187 }
188
189 void cfs_timer_arm(cfs_timer_t *l, cfs_time_t deadline)
190 {
191         l->expires = deadline;
192 }
193
194 void cfs_timer_disarm(cfs_timer_t *l)
195 {
196 }
197 cfs_time_t cfs_timer_deadline(cfs_timer_t *l)
198 {
199         return l->expires;
200 }
201
202
203 #ifdef HAVE_LIBPTHREAD
204
205 /*
206  * Threads
207  */
208
209 struct lustre_thread_arg {
210         cfs_thread_t f;
211         void *arg;
212 };
213 static void *cfs_thread_helper(void *data)
214 {
215         struct lustre_thread_arg *targ = data;
216         cfs_thread_t f  = targ->f;
217         void *arg = targ->arg;
218
219         free(targ);
220
221         (void)f(arg);
222         return NULL;
223 }
224
225 int cfs_create_thread(cfs_thread_t func, void *arg, unsigned long flags)
226 {
227         pthread_t tid;
228         pthread_attr_t tattr;
229         int rc;
230         struct lustre_thread_arg *targ_p = malloc(sizeof(struct lustre_thread_arg));
231
232         if ( targ_p == NULL )
233                 return -ENOMEM;
234
235         targ_p->f = func;
236         targ_p->arg = arg;
237
238         pthread_attr_init(&tattr);
239         pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
240         rc = pthread_create(&tid, &tattr, cfs_thread_helper, targ_p);
241         pthread_attr_destroy(&tattr);
242         return -rc;
243 }
244 #endif
245
246 uid_t cfs_curproc_uid(void)
247 {
248         return getuid();
249 }
250
251 gid_t cfs_curproc_gid(void)
252 {
253         return getgid();
254 }
255
256 uid_t cfs_curproc_fsuid(void)
257 {
258         return getuid();
259 }
260
261 gid_t cfs_curproc_fsgid(void)
262 {
263         return getgid();
264 }
265
266 void cfs_enter_debugger(void)
267 {
268         /*
269          * nothing for now.
270          */
271 }
272
273 void cfs_daemonize(char *str)
274 {
275         return;
276 }
277
278 int cfs_daemonize_ctxt(char *str)
279 {
280         return 0;
281 }
282
283 cfs_sigset_t cfs_block_allsigs(void)
284 {
285         cfs_sigset_t   all;
286         cfs_sigset_t   old;
287         int            rc;
288
289         sigfillset(&all);
290         rc = sigprocmask(SIG_SETMASK, &all, &old);
291         LASSERT(rc == 0);
292
293         return old;
294 }
295
296 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
297 {
298         cfs_sigset_t   old;
299         int   rc;
300
301         rc = sigprocmask(SIG_SETMASK, &blocks, &old);
302         LASSERT (rc == 0);
303
304         return old;
305 }
306
307 /* Block all signals except for the @sigs. It's only used in
308  * Linux kernel, just a dummy here. */
309 cfs_sigset_t cfs_block_sigsinv(unsigned long sigs)
310 {
311         cfs_sigset_t old;
312         int rc;
313
314         /* Return old blocked sigs */
315         rc = sigprocmask(SIG_SETMASK, NULL, &old);
316         LASSERT(rc == 0);
317
318         return old;
319 }
320
321 void cfs_restore_sigs(cfs_sigset_t old)
322 {
323         int   rc = sigprocmask(SIG_SETMASK, &old, NULL);
324
325         LASSERT (rc == 0);
326 }
327
328 int cfs_signal_pending(void)
329 {
330         cfs_sigset_t    empty;
331         cfs_sigset_t    set;
332         int  rc;
333
334         rc = sigpending(&set);
335         LASSERT (rc == 0);
336
337         sigemptyset(&empty);
338
339         return !memcmp(&empty, &set, sizeof(set));
340 }
341
342 void cfs_clear_sigpending(void)
343 {
344         return;
345 }
346
347 #ifdef __linux__
348
349 /*
350  * In glibc (NOT in Linux, so check above is not right), implement
351  * stack-back-tracing through backtrace() function.
352  */
353 #include <execinfo.h>
354
355 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
356 {
357         backtrace(trace->frame, ARRAY_SIZE(trace->frame));
358 }
359
360 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
361 {
362         if (0 <= frame_no && frame_no < ARRAY_SIZE(trace->frame))
363                 return trace->frame[frame_no];
364         else
365                 return NULL;
366 }
367
368 #else
369
370 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
371 {}
372 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
373 {
374         return NULL;
375 }
376
377 /* __linux__ */
378 #endif
379
380 void lbug_with_loc(const char *file, const char *func, const int line)
381 {
382         /* No libcfs_catastrophe in userspace! */
383         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
384         abort();
385 }
386
387 /* !__KERNEL__ */
388 #endif
389
390 /*
391  * Local variables:
392  * c-indentation-style: "K&R"
393  * c-basic-offset: 8
394  * tab-width: 8
395  * fill-column: 80
396  * scroll-step: 1
397  * End:
398  */