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 [sun.com URL with a
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 * libcfs/libcfs/user-prim.c
38 * Implementations of portable APIs for liblustre
40 * Author: Nikita Danilov <nikita@clusterfs.com>
44 * liblustre is single-threaded, so most "synchronization" APIs are trivial.
49 #include <libcfs/libcfs.h>
54 #ifdef HAVE_ASM_PAGE_H
57 #ifdef HAVE_SYS_USER_H
61 #include <sys/types.h>
73 * Sleep channel. No-op implementation.
76 void cfs_waitq_init(struct cfs_waitq *waitq)
78 LASSERT(waitq != NULL);
82 void cfs_waitlink_init(struct cfs_waitlink *link)
84 LASSERT(link != NULL);
88 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
90 LASSERT(waitq != NULL);
91 LASSERT(link != NULL);
96 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
98 LASSERT(waitq != NULL);
99 LASSERT(link != NULL);
104 void cfs_waitq_forward(struct cfs_waitlink *link, struct cfs_waitq *waitq)
106 LASSERT(waitq != NULL);
107 LASSERT(link != NULL);
112 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
114 LASSERT(waitq != NULL);
115 LASSERT(link != NULL);
120 int cfs_waitq_active(struct cfs_waitq *waitq)
122 LASSERT(waitq != NULL);
127 void cfs_waitq_signal(struct cfs_waitq *waitq)
129 LASSERT(waitq != NULL);
133 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
135 LASSERT(waitq != NULL);
139 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
141 LASSERT(waitq != NULL);
145 void cfs_waitq_wait(struct cfs_waitlink *link, int state)
147 LASSERT(link != NULL);
151 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, int state, int64_t timeout)
153 LASSERT(link != NULL);
158 #ifdef HAVE_LIBPTHREAD
164 struct lustre_thread_arg {
168 static void *cfs_thread_helper(void *data)
170 struct lustre_thread_arg *targ = data;
171 cfs_thread_t f = targ->f;
172 void *arg = targ->arg;
179 int cfs_create_thread(cfs_thread_t func, void *arg)
182 pthread_attr_t tattr;
184 struct lustre_thread_arg *targ_p = malloc(sizeof(struct lustre_thread_arg));
186 if ( targ_p == NULL )
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);
200 uid_t cfs_curproc_uid(void)
205 int cfs_parse_int_tunable(int *value, char *name)
207 char *env = getenv(name);
213 *value = strtoull(env, &end, 0);
217 CERROR("Can't parse tunable %s=%s\n", name, env);
225 cfs_page_t *cfs_alloc_page(unsigned int flags)
227 cfs_page_t *pg = malloc(sizeof(*pg));
231 pg->addr = malloc(CFS_PAGE_SIZE);
240 void cfs_free_page(cfs_page_t *pg)
246 void *cfs_page_address(cfs_page_t *pg)
251 void *cfs_kmap(cfs_page_t *pg)
256 void cfs_kunmap(cfs_page_t *pg)
265 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
269 c = malloc(sizeof(*c));
273 CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
274 name, c, (int)objsize);
278 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
280 CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
285 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
287 return cfs_alloc(c->size, gfp);
290 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
295 void cfs_enter_debugger(void)
302 void cfs_daemonize(char *str)
307 int cfs_daemonize_ctxt(char *str)
312 cfs_sigset_t cfs_block_allsigs(void)
319 rc = sigprocmask(SIG_SETMASK, &all, &old);
325 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
330 rc = sigprocmask(SIG_SETMASK, &blocks, &old);
336 void cfs_restore_sigs(cfs_sigset_t old)
338 int rc = sigprocmask(SIG_SETMASK, &old, NULL);
343 int cfs_signal_pending(void)
349 rc = sigpending(&set);
354 return !memcmp(&empty, &set, sizeof(set));
357 void cfs_clear_sigpending(void)
365 * In glibc (NOT in Linux, so check above is not right), implement
366 * stack-back-tracing through backtrace() function.
368 #include <execinfo.h>
370 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
372 backtrace(trace->frame, sizeof_array(trace->frame));
375 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
377 if (0 <= frame_no && frame_no < sizeof_array(trace->frame))
378 return trace->frame[frame_no];
385 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
387 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
395 void lbug_with_loc(char *file, const char *func, const int line)
397 /* No libcfs_catastrophe in userspace! */
398 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
407 * c-indentation-style: "K&R"