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