1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
5 * Author: Nikita Danilov <nikita@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
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.
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
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.
22 * Implementation of portable APIs for user-level.
26 /* Implementations of portable APIs for liblustre */
29 * liblustre is single-threaded, so most "synchronization" APIs are trivial.
34 #include <libcfs/libcfs.h>
39 #ifdef HAVE_ASM_PAGE_H
42 #ifdef HAVE_SYS_USER_H
46 #include <sys/types.h>
58 * Sleep channel. No-op implementation.
61 void cfs_waitq_init(struct cfs_waitq *waitq)
63 LASSERT(waitq != NULL);
67 void cfs_waitlink_init(struct cfs_waitlink *link)
69 LASSERT(link != NULL);
73 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
75 LASSERT(waitq != NULL);
76 LASSERT(link != NULL);
81 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
83 LASSERT(waitq != NULL);
84 LASSERT(link != NULL);
89 void cfs_waitq_forward(struct cfs_waitlink *link, struct cfs_waitq *waitq)
91 LASSERT(waitq != NULL);
92 LASSERT(link != NULL);
97 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
99 LASSERT(waitq != NULL);
100 LASSERT(link != NULL);
105 int cfs_waitq_active(struct cfs_waitq *waitq)
107 LASSERT(waitq != NULL);
112 void cfs_waitq_signal(struct cfs_waitq *waitq)
114 LASSERT(waitq != NULL);
118 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
120 LASSERT(waitq != NULL);
124 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
126 LASSERT(waitq != NULL);
130 void cfs_waitq_wait(struct cfs_waitlink *link, int state)
132 LASSERT(link != NULL);
136 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, int state, int64_t timeout)
138 LASSERT(link != NULL);
143 #ifdef HAVE_LIBPTHREAD
149 struct lustre_thread_arg {
153 static void *cfs_thread_helper(void *data)
155 struct lustre_thread_arg *targ = data;
156 cfs_thread_t f = targ->f;
157 void *arg = targ->arg;
164 int cfs_create_thread(cfs_thread_t func, void *arg)
167 pthread_attr_t tattr;
169 struct lustre_thread_arg *targ_p = malloc(sizeof(struct lustre_thread_arg));
171 if ( targ_p == NULL )
177 pthread_attr_init(&tattr);
178 pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
179 rc = pthread_create(&tid, &tattr, cfs_thread_helper, targ_p);
180 pthread_attr_destroy(&tattr);
185 uid_t cfs_curproc_uid(void)
190 int cfs_parse_int_tunable(int *value, char *name)
192 char *env = getenv(name);
198 *value = strtoull(env, &end, 0);
202 CERROR("Can't parse tunable %s=%s\n", name, env);
210 cfs_page_t *cfs_alloc_page(unsigned int flags)
212 cfs_page_t *pg = malloc(sizeof(*pg));
216 pg->addr = malloc(CFS_PAGE_SIZE);
225 void cfs_free_page(cfs_page_t *pg)
231 void *cfs_page_address(cfs_page_t *pg)
236 void *cfs_kmap(cfs_page_t *pg)
241 void cfs_kunmap(cfs_page_t *pg)
250 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
254 c = malloc(sizeof(*c));
258 CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
259 name, c, (int)objsize);
263 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
265 CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
270 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
272 return cfs_alloc(c->size, gfp);
275 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
280 void cfs_enter_debugger(void)
287 void cfs_daemonize(char *str)
292 int cfs_daemonize_ctxt(char *str)
297 cfs_sigset_t cfs_block_allsigs(void)
304 rc = sigprocmask(SIG_SETMASK, &all, &old);
310 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
315 rc = sigprocmask(SIG_SETMASK, &blocks, &old);
321 void cfs_restore_sigs(cfs_sigset_t old)
323 int rc = sigprocmask(SIG_SETMASK, &old, NULL);
328 int cfs_signal_pending(void)
334 rc = sigpending(&set);
339 return !memcmp(&empty, &set, sizeof(set));
342 void cfs_clear_sigpending(void)
350 * In glibc (NOT in Linux, so check above is not right), implement
351 * stack-back-tracing through backtrace() function.
353 #include <execinfo.h>
355 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
357 backtrace(trace->frame, sizeof_array(trace->frame));
360 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
362 if (0 <= frame_no && frame_no < sizeof_array(trace->frame))
363 return trace->frame[frame_no];
370 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
372 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
380 void lbug_with_loc(char *file, const char *func, const int line)
382 /* No libcfs_catastrophe in userspace! */
383 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
392 * c-indentation-style: "K&R"