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.
37 #ifdef HAVE_ASM_PAGE_H
40 #ifdef HAVE_SYS_USER_H
44 #include <sys/types.h>
53 #include <libcfs/libcfs.h>
54 #include <libcfs/kp30.h>
57 * Sleep channel. No-op implementation.
60 void cfs_waitq_init(struct cfs_waitq *waitq)
62 LASSERT(waitq != NULL);
66 void cfs_waitlink_init(struct cfs_waitlink *link)
68 LASSERT(link != NULL);
72 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
74 LASSERT(waitq != NULL);
75 LASSERT(link != NULL);
80 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
82 LASSERT(waitq != NULL);
83 LASSERT(link != NULL);
88 void cfs_waitq_forward(struct cfs_waitlink *link, struct cfs_waitq *waitq)
90 LASSERT(waitq != NULL);
91 LASSERT(link != NULL);
96 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
98 LASSERT(waitq != NULL);
99 LASSERT(link != NULL);
104 int cfs_waitq_active(struct cfs_waitq *waitq)
106 LASSERT(waitq != NULL);
111 void cfs_waitq_signal(struct cfs_waitq *waitq)
113 LASSERT(waitq != NULL);
117 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
119 LASSERT(waitq != NULL);
123 void cfs_waitq_broadcast(struct cfs_waitq *waitq, int state)
125 LASSERT(waitq != NULL);
129 void cfs_waitq_wait(struct cfs_waitlink *link)
131 LASSERT(link != NULL);
135 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, int state, int64_t timeout)
137 LASSERT(link != NULL);
146 cfs_page_t *cfs_alloc_page(unsigned int flags)
148 cfs_page_t *pg = malloc(sizeof(*pg));
152 pg->addr = malloc(CFS_PAGE_SIZE);
161 void cfs_free_page(cfs_page_t *pg)
167 void *cfs_page_address(cfs_page_t *pg)
172 void *cfs_kmap(cfs_page_t *pg)
177 void cfs_kunmap(cfs_page_t *pg)
186 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
190 c = malloc(sizeof(*c));
194 CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
195 name, c, (int)objsize);
199 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
201 CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
206 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
208 return cfs_alloc(c->size, gfp);
211 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
217 * This uses user-visible declarations from <linux/kdev_t.h>
220 #include <linux/kdev_t.h>
225 #define MAJOR(dev) ((dev)>>8)
226 #define MINOR(dev) ((dev) & 0xff)
227 #define MKDEV(ma,mi) ((ma)<<8 | (mi))
231 cfs_rdev_t cfs_rdev_build(cfs_major_nr_t major, cfs_minor_nr_t minor)
233 return MKDEV(major, minor);
236 cfs_major_nr_t cfs_rdev_major(cfs_rdev_t rdev)
241 cfs_minor_nr_t cfs_rdev_minor(cfs_rdev_t rdev)
246 void cfs_enter_debugger(void)
253 void cfs_daemonize(char *str)
258 cfs_sigset_t cfs_block_allsigs(void)
265 rc = sigprocmask(SIG_SETMASK, &all, &old);
271 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
276 rc = sigprocmask(SIG_SETMASK, &blocks, &old);
282 void cfs_restore_sigs(cfs_sigset_t old)
284 int rc = sigprocmask(SIG_SETMASK, &old, NULL);
289 int cfs_signal_pending(void)
295 rc = sigpending(&set);
300 return !memcmp(&empty, &set, sizeof(set));
303 void cfs_clear_sigpending(void)
311 * In glibc (NOT in Linux, so check above is not right), implement
312 * stack-back-tracing through backtrace() function.
314 #include <execinfo.h>
316 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
318 backtrace(trace->frame, sizeof_array(trace->frame));
321 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
323 if (0 <= frame_no && frame_no < sizeof_array(trace->frame))
324 return trace->frame[frame_no];
331 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
333 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
341 void lbug_with_loc(char *file, const char *func, const int line)
343 /* No libcfs_catastrophe in userspace! */
344 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
354 * c-indentation-style: "K&R"