Whamcloud - gitweb
2067c403af5e963df6190d6a2d7db66ed7c61ffa
[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 [sun.com URL with a
20  * copy of GPLv2].
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  2008 Sun Microsystems, Inc. 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 #include <sys/mman.h>
52 #ifndef  __CYGWIN__
53 #include <stdint.h>
54 #ifdef HAVE_ASM_PAGE_H
55 #include <asm/page.h>
56 #endif
57 #ifdef HAVE_SYS_USER_H
58 #include <sys/user.h>
59 #endif
60 #else
61 #include <sys/types.h>
62 #endif
63 #include <stdlib.h>
64 #include <string.h>
65 #include <signal.h>
66 #include <errno.h>
67 #include <sys/stat.h>
68 #ifdef  HAVE_SYS_VFS_H
69 #include <sys/vfs.h>
70 #endif
71
72 /*
73  * Sleep channel. No-op implementation.
74  */
75
76 void cfs_waitq_init(struct cfs_waitq *waitq)
77 {
78         LASSERT(waitq != NULL);
79         (void)waitq;
80 }
81
82 void cfs_waitlink_init(struct cfs_waitlink *link)
83 {
84         LASSERT(link != NULL);
85         (void)link;
86 }
87
88 void cfs_waitq_add(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 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
97 {
98         LASSERT(waitq != NULL);
99         LASSERT(link != NULL);
100         (void)waitq;
101         (void)link;
102 }
103
104 void cfs_waitq_forward(struct cfs_waitlink *link, struct cfs_waitq *waitq)
105 {
106         LASSERT(waitq != NULL);
107         LASSERT(link != NULL);
108         (void)waitq;
109         (void)link;
110 }
111
112 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
113 {
114         LASSERT(waitq != NULL);
115         LASSERT(link != NULL);
116         (void)waitq;
117         (void)link;
118 }
119
120 int cfs_waitq_active(struct cfs_waitq *waitq)
121 {
122         LASSERT(waitq != NULL);
123         (void)waitq;
124         return 0;
125 }
126
127 void cfs_waitq_signal(struct cfs_waitq *waitq)
128 {
129         LASSERT(waitq != NULL);
130         (void)waitq;
131 }
132
133 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
134 {
135         LASSERT(waitq != NULL);
136         (void)waitq;
137 }
138
139 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
140 {
141         LASSERT(waitq != NULL);
142         (void)waitq;
143 }
144
145 void cfs_waitq_wait(struct cfs_waitlink *link, int state)
146 {
147         LASSERT(link != NULL);
148         (void)link;
149 }
150
151 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, int state, int64_t timeout)
152 {
153         LASSERT(link != NULL);
154         (void)link;
155         return 0;
156 }
157
158 #ifdef HAVE_LIBPTHREAD
159
160 /*
161  * Threads
162  */
163
164 struct lustre_thread_arg {
165         cfs_thread_t f; 
166         void *arg;
167 };
168 static void *cfs_thread_helper(void *data)
169 {
170         struct lustre_thread_arg *targ = data;
171         cfs_thread_t f  = targ->f;
172         void *arg = targ->arg;
173
174         free(targ);
175         
176         (void)f(arg);
177         return NULL;
178 }
179 int cfs_create_thread(cfs_thread_t func, void *arg)
180 {
181         pthread_t tid;
182         pthread_attr_t tattr;
183         int rc;
184         struct lustre_thread_arg *targ_p = malloc(sizeof(struct lustre_thread_arg));
185
186         if ( targ_p == NULL )
187                 return -ENOMEM;
188         
189         targ_p->f = func;
190         targ_p->arg = arg;
191
192         pthread_attr_init(&tattr); 
193         pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
194         rc = pthread_create(&tid, &tattr, cfs_thread_helper, targ_p);
195         pthread_attr_destroy(&tattr);
196         return -rc;
197 }
198 #endif
199
200 uid_t cfs_curproc_uid(void)
201 {
202         return getuid();
203 }
204
205 int cfs_parse_int_tunable(int *value, char *name)
206 {
207         char    *env = getenv(name);
208         char    *end;
209
210         if (env == NULL)
211                 return 0;
212
213         *value = strtoull(env, &end, 0);
214         if (*end == 0)
215                 return 0;
216
217         CERROR("Can't parse tunable %s=%s\n", name, env);
218         return -EINVAL;
219 }
220
221 /*
222  * Allocator
223  */
224
225 cfs_page_t *cfs_alloc_page(unsigned int flags)
226 {
227         cfs_page_t *pg = malloc(sizeof(*pg));
228
229         if (!pg)
230                 return NULL;
231         pg->addr = malloc(CFS_PAGE_SIZE);
232
233         if (!pg->addr) {
234                 free(pg);
235                 return NULL;
236         }
237         return pg;
238 }
239
240 void cfs_free_page(cfs_page_t *pg)
241 {
242         free(pg->addr);
243         free(pg);
244 }
245
246 void *cfs_page_address(cfs_page_t *pg)
247 {
248         return pg->addr;
249 }
250
251 void *cfs_kmap(cfs_page_t *pg)
252 {
253         return pg->addr;
254 }
255
256 void cfs_kunmap(cfs_page_t *pg)
257 {
258 }
259
260 /*
261  * SLAB allocator
262  */
263
264 cfs_mem_cache_t *
265 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
266 {
267         cfs_mem_cache_t *c;
268
269         c = malloc(sizeof(*c));
270         if (!c)
271                 return NULL;
272         c->size = objsize;
273         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
274                name, c, (int)objsize);
275         return c;
276 }
277
278 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
279 {
280         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
281         free(c);
282         return 0;
283 }
284
285 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
286 {
287         return cfs_alloc(c->size, gfp);
288 }
289
290 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
291 {
292         cfs_free(addr);
293 }
294
295 void cfs_enter_debugger(void)
296 {
297         /*
298          * nothing for now.
299          */
300 }
301
302 void cfs_daemonize(char *str)
303 {
304         return;
305 }
306
307 int cfs_daemonize_ctxt(char *str)
308 {
309         return 0;
310 }
311
312 cfs_sigset_t cfs_block_allsigs(void)
313 {
314         cfs_sigset_t   all;
315         cfs_sigset_t   old;
316         int            rc;
317
318         sigfillset(&all);
319         rc = sigprocmask(SIG_SETMASK, &all, &old);
320         LASSERT(rc == 0);
321
322         return old;
323 }
324
325 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
326 {
327         cfs_sigset_t   old;
328         int   rc;
329         
330         rc = sigprocmask(SIG_SETMASK, &blocks, &old);
331         LASSERT (rc == 0);
332
333         return old;
334 }
335
336 void cfs_restore_sigs(cfs_sigset_t old)
337 {
338         int   rc = sigprocmask(SIG_SETMASK, &old, NULL);
339
340         LASSERT (rc == 0);
341 }
342
343 int cfs_signal_pending(void)
344 {
345         cfs_sigset_t    empty;
346         cfs_sigset_t    set;
347         int  rc;
348
349         rc = sigpending(&set);
350         LASSERT (rc == 0);
351
352         sigemptyset(&empty);
353
354         return !memcmp(&empty, &set, sizeof(set));
355 }
356
357 void cfs_clear_sigpending(void)
358 {
359         return;
360 }
361
362 #ifdef __linux__
363
364 /*
365  * In glibc (NOT in Linux, so check above is not right), implement
366  * stack-back-tracing through backtrace() function.
367  */
368 #include <execinfo.h>
369
370 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
371 {
372         backtrace(trace->frame, sizeof_array(trace->frame));
373 }
374
375 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
376 {
377         if (0 <= frame_no && frame_no < sizeof_array(trace->frame))
378                 return trace->frame[frame_no];
379         else
380                 return NULL;
381 }
382
383 #else
384
385 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
386 {}
387 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
388 {
389         return NULL;
390 }
391
392 /* __linux__ */
393 #endif
394
395 void lbug_with_loc(char *file, const char *func, const int line)
396 {
397         /* No libcfs_catastrophe in userspace! */
398         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
399         abort();
400 }
401
402 /* !__KERNEL__ */
403 #endif
404
405 /*
406  * Local variables:
407  * c-indentation-style: "K&R"
408  * c-basic-offset: 8
409  * tab-width: 8
410  * fill-column: 80
411  * scroll-step: 1
412  * End:
413  */