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