Whamcloud - gitweb
Branch HEAD
[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  * Copyright (C) 2004 Cluster File Systems, Inc.
5  * Author: Nikita Danilov <nikita@clusterfs.com>
6  *
7  * This file is part of Lustre, http://www.lustre.org.
8  *
9  * Lustre is free software; you can redistribute it and/or modify it under the
10  * terms of version 2 of the GNU General Public License as published by the
11  * Free Software Foundation.
12  *
13  * Lustre is distributed in the hope that it will be useful, but WITHOUT ANY
14  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with Lustre; if not, write to the Free Software Foundation, Inc., 675 Mass
20  * Ave, Cambridge, MA 02139, USA.
21  *
22  * Implementation of portable APIs for user-level.
23  *
24  */
25
26 /* Implementations of portable APIs for liblustre */
27
28 /*
29  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
30  */
31
32 #ifndef __KERNEL__
33
34 #include <libcfs/libcfs.h>
35 #include <libcfs/kp30.h>
36
37 #include <sys/mman.h>
38 #ifndef  __CYGWIN__
39 #include <stdint.h>
40 #ifdef HAVE_ASM_PAGE_H
41 #include <asm/page.h>
42 #endif
43 #ifdef HAVE_SYS_USER_H
44 #include <sys/user.h>
45 #endif
46 #else
47 #include <sys/types.h>
48 #endif
49 #include <stdlib.h>
50 #include <string.h>
51 #include <signal.h>
52 #include <errno.h>
53 #include <sys/stat.h>
54 #ifdef  HAVE_SYS_VFS_H
55 #include <sys/vfs.h>
56 #endif
57
58 /*
59  * Sleep channel. No-op implementation.
60  */
61
62 void cfs_waitq_init(struct cfs_waitq *waitq)
63 {
64         LASSERT(waitq != NULL);
65         (void)waitq;
66 }
67
68 void cfs_waitlink_init(struct cfs_waitlink *link)
69 {
70         LASSERT(link != NULL);
71         (void)link;
72 }
73
74 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
75 {
76         LASSERT(waitq != NULL);
77         LASSERT(link != NULL);
78         (void)waitq;
79         (void)link;
80 }
81
82 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
83 {
84         LASSERT(waitq != NULL);
85         LASSERT(link != NULL);
86         (void)waitq;
87         (void)link;
88 }
89
90 void cfs_waitq_forward(struct cfs_waitlink *link, struct cfs_waitq *waitq)
91 {
92         LASSERT(waitq != NULL);
93         LASSERT(link != NULL);
94         (void)waitq;
95         (void)link;
96 }
97
98 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
99 {
100         LASSERT(waitq != NULL);
101         LASSERT(link != NULL);
102         (void)waitq;
103         (void)link;
104 }
105
106 int cfs_waitq_active(struct cfs_waitq *waitq)
107 {
108         LASSERT(waitq != NULL);
109         (void)waitq;
110         return 0;
111 }
112
113 void cfs_waitq_signal(struct cfs_waitq *waitq)
114 {
115         LASSERT(waitq != NULL);
116         (void)waitq;
117 }
118
119 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
120 {
121         LASSERT(waitq != NULL);
122         (void)waitq;
123 }
124
125 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
126 {
127         LASSERT(waitq != NULL);
128         (void)waitq;
129 }
130
131 void cfs_waitq_wait(struct cfs_waitlink *link, int state)
132 {
133         LASSERT(link != NULL);
134         (void)link;
135 }
136
137 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, int state, int64_t timeout)
138 {
139         LASSERT(link != NULL);
140         (void)link;
141         return 0;
142 }
143
144 #ifdef HAVE_LIBPTHREAD
145
146 /*
147  * Threads
148  */
149
150 struct lustre_thread_arg {
151         cfs_thread_t f; 
152         void *arg;
153 };
154 static void *cfs_thread_helper(void *data)
155 {
156         struct lustre_thread_arg *targ = data;
157         cfs_thread_t f  = targ->f;
158         void *arg = targ->arg;
159
160         free(targ);
161         
162         (void)f(arg);
163         return NULL;
164 }
165 int cfs_create_thread(cfs_thread_t func, void *arg)
166 {
167         pthread_t tid;
168         pthread_attr_t tattr;
169         int rc;
170         struct lustre_thread_arg *targ_p = malloc(sizeof(struct lustre_thread_arg));
171
172         if ( targ_p == NULL )
173                 return -ENOMEM;
174         
175         targ_p->f = func;
176         targ_p->arg = arg;
177
178         pthread_attr_init(&tattr); 
179         pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
180         rc = pthread_create(&tid, &tattr, cfs_thread_helper, targ_p);
181         pthread_attr_destroy(&tattr);
182         return -rc;
183 }
184 #endif
185
186 uid_t cfs_curproc_uid(void)
187 {
188         return getuid();
189 }
190
191 int cfs_parse_int_tunable(int *value, char *name)
192 {
193         char    *env = getenv(name);
194         char    *end;
195
196         if (env == NULL)
197                 return 0;
198
199         *value = strtoull(env, &end, 0);
200         if (*end == 0)
201                 return 0;
202
203         CERROR("Can't parse tunable %s=%s\n", name, env);
204         return -EINVAL;
205 }
206
207 /*
208  * Allocator
209  */
210
211 cfs_page_t *cfs_alloc_page(unsigned int flags)
212 {
213         cfs_page_t *pg = malloc(sizeof(*pg));
214
215         if (!pg)
216                 return NULL;
217         pg->addr = malloc(CFS_PAGE_SIZE);
218
219         if (!pg->addr) {
220                 free(pg);
221                 return NULL;
222         }
223         return pg;
224 }
225
226 void cfs_free_page(cfs_page_t *pg)
227 {
228         free(pg->addr);
229         free(pg);
230 }
231
232 void *cfs_page_address(cfs_page_t *pg)
233 {
234         return pg->addr;
235 }
236
237 void *cfs_kmap(cfs_page_t *pg)
238 {
239         return pg->addr;
240 }
241
242 void cfs_kunmap(cfs_page_t *pg)
243 {
244 }
245
246 /*
247  * SLAB allocator
248  */
249
250 cfs_mem_cache_t *
251 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
252 {
253         cfs_mem_cache_t *c;
254
255         c = malloc(sizeof(*c));
256         if (!c)
257                 return NULL;
258         c->size = objsize;
259         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
260                name, c, (int)objsize);
261         return c;
262 }
263
264 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
265 {
266         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
267         free(c);
268         return 0;
269 }
270
271 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
272 {
273         return cfs_alloc(c->size, gfp);
274 }
275
276 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
277 {
278         cfs_free(addr);
279 }
280
281 void cfs_enter_debugger(void)
282 {
283         /*
284          * nothing for now.
285          */
286 }
287
288 void cfs_daemonize(char *str)
289 {
290         return;
291 }
292
293 int cfs_daemonize_ctxt(char *str)
294 {
295         return 0;
296 }
297
298 cfs_sigset_t cfs_block_allsigs(void)
299 {
300         cfs_sigset_t   all;
301         cfs_sigset_t   old;
302         int            rc;
303
304         sigfillset(&all);
305         rc = sigprocmask(SIG_SETMASK, &all, &old);
306         LASSERT(rc == 0);
307
308         return old;
309 }
310
311 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
312 {
313         cfs_sigset_t   old;
314         int   rc;
315         
316         rc = sigprocmask(SIG_SETMASK, &blocks, &old);
317         LASSERT (rc == 0);
318
319         return old;
320 }
321
322 void cfs_restore_sigs(cfs_sigset_t old)
323 {
324         int   rc = sigprocmask(SIG_SETMASK, &old, NULL);
325
326         LASSERT (rc == 0);
327 }
328
329 int cfs_signal_pending(void)
330 {
331         cfs_sigset_t    empty;
332         cfs_sigset_t    set;
333         int  rc;
334
335         rc = sigpending(&set);
336         LASSERT (rc == 0);
337
338         sigemptyset(&empty);
339
340         return !memcmp(&empty, &set, sizeof(set));
341 }
342
343 void cfs_clear_sigpending(void)
344 {
345         return;
346 }
347
348 #ifdef __linux__
349
350 /*
351  * In glibc (NOT in Linux, so check above is not right), implement
352  * stack-back-tracing through backtrace() function.
353  */
354 #include <execinfo.h>
355
356 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
357 {
358         backtrace(trace->frame, sizeof_array(trace->frame));
359 }
360
361 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
362 {
363         if (0 <= frame_no && frame_no < sizeof_array(trace->frame))
364                 return trace->frame[frame_no];
365         else
366                 return NULL;
367 }
368
369 #else
370
371 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
372 {}
373 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
374 {
375         return NULL;
376 }
377
378 /* __linux__ */
379 #endif
380
381 void lbug_with_loc(char *file, const char *func, const int line)
382 {
383         /* No libcfs_catastrophe in userspace! */
384         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
385         abort();
386 }
387
388 /* !__KERNEL__ */
389 #endif
390
391 /*
392  * Local variables:
393  * c-indentation-style: "K&R"
394  * c-basic-offset: 8
395  * tab-width: 8
396  * fill-column: 80
397  * scroll-step: 1
398  * End:
399  */