1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/libcfs/user-prim.c
38 * Implementations of portable APIs for liblustre
40 * Author: Nikita Danilov <nikita@clusterfs.com>
45 * liblustre is single-threaded, so most "synchronization" APIs are trivial.
50 #include <libcfs/libcfs.h>
51 #include <libcfs/kp30.h>
56 #ifdef HAVE_ASM_PAGE_H
59 #ifdef HAVE_SYS_USER_H
63 #include <sys/types.h>
75 * Sleep channel. No-op implementation.
78 void cfs_waitq_init(struct cfs_waitq *waitq)
80 LASSERT(waitq != NULL);
84 void cfs_waitlink_init(struct cfs_waitlink *link)
86 LASSERT(link != NULL);
90 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
92 LASSERT(waitq != NULL);
93 LASSERT(link != NULL);
98 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
100 LASSERT(waitq != NULL);
101 LASSERT(link != NULL);
106 void cfs_waitq_forward(struct cfs_waitlink *link, struct cfs_waitq *waitq)
108 LASSERT(waitq != NULL);
109 LASSERT(link != NULL);
114 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
116 LASSERT(waitq != NULL);
117 LASSERT(link != NULL);
122 int cfs_waitq_active(struct cfs_waitq *waitq)
124 LASSERT(waitq != NULL);
129 void cfs_waitq_signal(struct cfs_waitq *waitq)
131 LASSERT(waitq != NULL);
135 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
137 LASSERT(waitq != NULL);
141 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
143 LASSERT(waitq != NULL);
147 void cfs_waitq_wait(struct cfs_waitlink *link, int state)
149 LASSERT(link != NULL);
153 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, int state, int64_t timeout)
155 LASSERT(link != NULL);
160 #ifdef HAVE_LIBPTHREAD
166 struct lustre_thread_arg {
170 static void *cfs_thread_helper(void *data)
172 struct lustre_thread_arg *targ = data;
173 cfs_thread_t f = targ->f;
174 void *arg = targ->arg;
181 int cfs_create_thread(cfs_thread_t func, void *arg)
184 pthread_attr_t tattr;
186 struct lustre_thread_arg *targ_p = malloc(sizeof(struct lustre_thread_arg));
188 if ( targ_p == NULL )
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);
202 uid_t cfs_curproc_uid(void)
207 int cfs_parse_int_tunable(int *value, char *name)
209 char *env = getenv(name);
215 *value = strtoull(env, &end, 0);
219 CERROR("Can't parse tunable %s=%s\n", name, env);
227 cfs_page_t *cfs_alloc_page(unsigned int flags)
229 cfs_page_t *pg = malloc(sizeof(*pg));
233 pg->addr = malloc(CFS_PAGE_SIZE);
242 void cfs_free_page(cfs_page_t *pg)
248 void *cfs_page_address(cfs_page_t *pg)
253 void *cfs_kmap(cfs_page_t *pg)
258 void cfs_kunmap(cfs_page_t *pg)
267 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
271 c = malloc(sizeof(*c));
275 CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
276 name, c, (int)objsize);
280 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
282 CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
287 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
289 return cfs_alloc(c->size, gfp);
292 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
297 void cfs_enter_debugger(void)
304 void cfs_daemonize(char *str)
309 int cfs_daemonize_ctxt(char *str)
314 cfs_sigset_t cfs_block_allsigs(void)
321 rc = sigprocmask(SIG_SETMASK, &all, &old);
327 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
332 rc = sigprocmask(SIG_SETMASK, &blocks, &old);
338 void cfs_restore_sigs(cfs_sigset_t old)
340 int rc = sigprocmask(SIG_SETMASK, &old, NULL);
345 int cfs_signal_pending(void)
351 rc = sigpending(&set);
356 return !memcmp(&empty, &set, sizeof(set));
359 void cfs_clear_sigpending(void)
367 * In glibc (NOT in Linux, so check above is not right), implement
368 * stack-back-tracing through backtrace() function.
370 #include <execinfo.h>
372 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
374 backtrace(trace->frame, sizeof_array(trace->frame));
377 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
379 if (0 <= frame_no && frame_no < sizeof_array(trace->frame))
380 return trace->frame[frame_no];
387 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
389 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
397 void lbug_with_loc(const char *file, const char *func, const int line)
399 /* No libcfs_catastrophe in userspace! */
400 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
409 * c-indentation-style: "K&R"