Whamcloud - gitweb
Landing b_hd_newconfig on HEAD
[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  * Copyright (C) 2004 Cluster File Systems, Inc.
5  * Author: Nikita Danilov <nikita@clusterfs.com>
6  *
7  * This file is part of Lustre, http://www.lustre.org.
8  *
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.
12  *
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
16  * details.
17  *
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.
21  *
22  * Implementation of portable APIs for user-level.
23  *
24  */
25
26 /* Implementations of portable APIs for liblustre */
27
28 /*
29  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
30  */
31
32 #ifndef __KERNEL__
33
34 #include <sys/mman.h>
35 #ifndef  __CYGWIN__
36 #include <stdint.h>
37 #ifdef HAVE_ASM_PAGE_H
38 #include <asm/page.h>
39 #endif
40 #ifdef HAVE_SYS_USER_H
41 #include <sys/user.h>
42 #endif
43 #else
44 #include <sys/types.h>
45 #endif
46 #include <stdlib.h>
47 #include <string.h>
48 #include <signal.h>
49 #include <errno.h>
50 #include <sys/stat.h>
51 #include <sys/vfs.h>
52
53 #include <libcfs/libcfs.h>
54 #include <libcfs/kp30.h>
55
56 /*
57  * Sleep channel. No-op implementation.
58  */
59
60 void cfs_waitq_init(struct cfs_waitq *waitq)
61 {
62         LASSERT(waitq != NULL);
63         (void)waitq;
64 }
65
66 void cfs_waitlink_init(struct cfs_waitlink *link)
67 {
68         LASSERT(link != NULL);
69         (void)link;
70 }
71
72 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
73 {
74         LASSERT(waitq != NULL);
75         LASSERT(link != NULL);
76         (void)waitq;
77         (void)link;
78 }
79
80 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
81 {
82         LASSERT(waitq != NULL);
83         LASSERT(link != NULL);
84         (void)waitq;
85         (void)link;
86 }
87
88 void cfs_waitq_forward(struct cfs_waitlink *link, struct cfs_waitq *waitq)
89 {
90         LASSERT(waitq != NULL);
91         LASSERT(link != NULL);
92         (void)waitq;
93         (void)link;
94 }
95
96 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
97 {
98         LASSERT(waitq != NULL);
99         LASSERT(link != NULL);
100         (void)waitq;
101         (void)link;
102 }
103
104 int cfs_waitq_active(struct cfs_waitq *waitq)
105 {
106         LASSERT(waitq != NULL);
107         (void)waitq;
108         return 0;
109 }
110
111 void cfs_waitq_signal(struct cfs_waitq *waitq)
112 {
113         LASSERT(waitq != NULL);
114         (void)waitq;
115 }
116
117 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
118 {
119         LASSERT(waitq != NULL);
120         (void)waitq;
121 }
122
123 void cfs_waitq_broadcast(struct cfs_waitq *waitq, int state)
124 {
125         LASSERT(waitq != NULL);
126         (void)waitq;
127 }
128
129 void cfs_waitq_wait(struct cfs_waitlink *link)
130 {
131         LASSERT(link != NULL);
132         (void)link;
133 }
134
135 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, int state, int64_t timeout)
136 {
137         LASSERT(link != NULL);
138         (void)link;
139         return 0;
140 }
141
142 /*
143  * Allocator
144  */
145
146 cfs_page_t *cfs_alloc_page(unsigned int flags)
147 {
148         cfs_page_t *pg = malloc(sizeof(*pg));
149
150         if (!pg)
151                 return NULL;
152         pg->addr = malloc(CFS_PAGE_SIZE);
153
154         if (!pg->addr) {
155                 free(pg);
156                 return NULL;
157         }
158         return pg;
159 }
160
161 void cfs_free_page(cfs_page_t *pg)
162 {
163         free(pg->addr);
164         free(pg);
165 }
166
167 void *cfs_page_address(cfs_page_t *pg)
168 {
169         return pg->addr;
170 }
171
172 void *cfs_kmap(cfs_page_t *pg)
173 {
174         return pg->addr;
175 }
176
177 void cfs_kunmap(cfs_page_t *pg)
178 {
179 }
180
181 /*
182  * SLAB allocator
183  */
184
185 cfs_mem_cache_t *
186 cfs_mem_cache_create(const char *name, size_t objsize, size_t off, unsigned long flags)
187 {
188         cfs_mem_cache_t *c;
189
190         c = malloc(sizeof(*c));
191         if (!c)
192                 return NULL;
193         c->size = objsize;
194         CDEBUG(D_MALLOC, "alloc slab cache %s at %p, objsize %d\n",
195                name, c, (int)objsize);
196         return c;
197 }
198
199 int cfs_mem_cache_destroy(cfs_mem_cache_t *c)
200 {
201         CDEBUG(D_MALLOC, "destroy slab cache %p, objsize %u\n", c, c->size);
202         free(c);
203         return 0;
204 }
205
206 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp)
207 {
208         return cfs_alloc(c->size, gfp);
209 }
210
211 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr)
212 {
213         cfs_free(addr);
214 }
215
216 /*
217  * This uses user-visible declarations from <linux/kdev_t.h>
218  */
219 #ifdef __LINUX__
220 #include <linux/kdev_t.h>
221 #endif
222
223 #ifndef MKDEV
224
225 #define MAJOR(dev)      ((dev)>>8)
226 #define MINOR(dev)      ((dev) & 0xff)
227 #define MKDEV(ma,mi)    ((ma)<<8 | (mi))
228
229 #endif
230
231 cfs_rdev_t cfs_rdev_build(cfs_major_nr_t major, cfs_minor_nr_t minor)
232 {
233         return MKDEV(major, minor);
234 }
235
236 cfs_major_nr_t cfs_rdev_major(cfs_rdev_t rdev)
237 {
238         return MAJOR(rdev);
239 }
240
241 cfs_minor_nr_t cfs_rdev_minor(cfs_rdev_t rdev)
242 {
243         return MINOR(rdev);
244 }
245
246 void cfs_enter_debugger(void)
247 {
248         /*
249          * nothing for now.
250          */
251 }
252
253 void cfs_daemonize(char *str)
254 {
255         return;
256 }
257
258 cfs_sigset_t cfs_block_allsigs(void)
259 {
260         cfs_sigset_t   all;
261         cfs_sigset_t   old;
262         int            rc;
263
264         sigfillset(&all);
265         rc = sigprocmask(SIG_SETMASK, &all, &old);
266         LASSERT(rc == 0);
267
268         return old;
269 }
270
271 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
272 {
273         cfs_sigset_t   old;
274         int   rc;
275         
276         rc = sigprocmask(SIG_SETMASK, &blocks, &old);
277         LASSERT (rc == 0);
278
279         return old;
280 }
281
282 void cfs_restore_sigs(cfs_sigset_t old)
283 {
284         int   rc = sigprocmask(SIG_SETMASK, &old, NULL);
285
286         LASSERT (rc == 0);
287 }
288
289 int cfs_signal_pending(void)
290 {
291         cfs_sigset_t    empty;
292         cfs_sigset_t    set;
293         int  rc;
294
295         rc = sigpending(&set);
296         LASSERT (rc == 0);
297
298         sigemptyset(&empty);
299
300         return !memcmp(&empty, &set, sizeof(set));
301 }
302
303 void cfs_clear_sigpending(void)
304 {
305         return;
306 }
307
308 #ifdef __LINUX__
309
310 /*
311  * In glibc (NOT in Linux, so check above is not right), implement
312  * stack-back-tracing through backtrace() function.
313  */
314 #include <execinfo.h>
315
316 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
317 {
318         backtrace(trace->frame, sizeof_array(trace->frame));
319 }
320
321 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
322 {
323         if (0 <= frame_no && frame_no < sizeof_array(trace->frame))
324                 return trace->frame[frame_no];
325         else
326                 return NULL;
327 }
328
329 #else
330
331 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
332 {}
333 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
334 {
335         return NULL;
336 }
337
338 /* __LINUX__ */
339 #endif
340
341 void lbug_with_loc(char *file, const char *func, const int line)
342 {
343         /* No libcfs_catastrophe in userspace! */
344         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
345         abort();
346 }
347
348
349 /* !__KERNEL__ */
350 #endif
351
352 /*
353  * Local variables:
354  * c-indentation-style: "K&R"
355  * c-basic-offset: 8
356  * tab-width: 8
357  * fill-column: 80
358  * scroll-step: 1
359  * End:
360  */