Whamcloud - gitweb
d182041995220c8377c5c3813e9fe6e0befb8bcc
[fs/lustre-release.git] / lnet / include / libcfs / user-prim.h
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 time API for user-level.
23  *
24  */
25
26 #ifndef __LIBCFS_USER_PRIM_H__
27 #define __LIBCFS_USER_PRIM_H__
28
29 #ifndef __LIBCFS_LIBCFS_H__
30 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
31 #endif
32
33 /* Implementations of portable APIs for liblustre */
34
35 /*
36  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
37  */
38
39 #ifndef __KERNEL__
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/signal.h>
44 #include <sys/mman.h>
45 #include <libcfs/list.h>
46 #include <libcfs/user-time.h>
47 #include <signal.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50
51 #ifndef PAGE_SIZE
52
53 #define PAGE_SIZE (getpagesize())
54 static __inline__ int getpageshift()
55 {
56         int pagesize = getpagesize();
57 #if (__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4))
58         /* unsigned int is 32 bits on all our architectures */
59         return (__builtin_clz(pagesize) ^ 31);
60 #else
61         register int pageshift = -1;
62         while (pagesize) { pagesize >>= 1; pageshift++; }
63         return pageshift;
64 #endif
65 }
66
67 #undef  PAGE_MASK
68 #define PAGE_MASK  (~(PAGE_SIZE-1))
69 #undef  PAGE_SHIFT
70 #define PAGE_SHIFT (getpageshift())
71
72 #endif
73
74 /*
75  * Wait Queue. No-op implementation.
76  */
77
78 typedef struct cfs_waitlink {
79         struct list_head sleeping;
80         void *process;
81 } cfs_waitlink_t;
82
83 typedef struct cfs_waitq {
84         struct list_head sleepers;
85 } cfs_waitq_t;
86
87 void cfs_waitq_init(struct cfs_waitq *waitq);
88 void cfs_waitlink_init(struct cfs_waitlink *link);
89 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link);
90 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, 
91                              struct cfs_waitlink *link);
92 void cfs_waitq_forward(struct cfs_waitlink *link, struct cfs_waitq *waitq);
93 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link);
94 int  cfs_waitq_active(struct cfs_waitq *waitq);
95 void cfs_waitq_signal(struct cfs_waitq *waitq);
96 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr);
97 void cfs_waitq_broadcast(struct cfs_waitq *waitq, int state);
98 void cfs_waitq_wait(struct cfs_waitlink *link);
99 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, int state, int64_t timeout);
100 #define cfs_schedule_timeout(s, t)              \
101         do {                                    \
102                 cfs_waitlink_t    l;            \
103                 cfs_waitq_timedwait(&l, s, t);  \
104         } while (0)
105
106 #define CFS_TASK_INTERRUPTIBLE  (0)
107 #define CFS_TASK_UNINT          (0)
108
109 /* 2.4 defines */
110
111 /* XXX
112  * for this moment, liblusre will not rely OST for non-page-aligned write
113  */
114 #define LIBLUSTRE_HANDLE_UNALIGNED_PAGE
115
116 struct page {
117         void   *addr;
118         unsigned long index;
119         struct list_head list;
120         unsigned long private;
121
122         /* internally used by liblustre file i/o */
123         int     _offset;
124         int     _count;
125 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
126         int     _managed;
127 #endif
128 };
129
130 typedef struct page cfs_page_t;
131
132 #define CFS_PAGE_SIZE                   PAGE_SIZE
133 #define CFS_PAGE_SHIFT                  PAGE_SHIFT
134 #define CFS_PAGE_MASK                   (~((__u64)CFS_PAGE_SIZE-1))
135
136 cfs_page_t *cfs_alloc_page(unsigned int flags);
137 void cfs_free_page(cfs_page_t *pg);
138 void *cfs_page_address(cfs_page_t *pg);
139 void *cfs_kmap(cfs_page_t *pg);
140 void cfs_kunmap(cfs_page_t *pg);
141
142 #define cfs_get_page(p)                 __I_should_not_be_called__(at_all)
143 #define cfs_page_count(p)               __I_should_not_be_called__(at_all)
144 #define cfs_page_index(p)               ((p)->index)
145
146 /*
147  * Memory allocator
148  * Inline function, so utils can use them without linking of libcfs
149  */
150 #define __ALLOC_ZERO    (1 << 2)
151 static inline void *cfs_alloc(size_t nr_bytes, u_int32_t flags)
152 {
153         void *result;
154
155         result = malloc(nr_bytes);
156         if (result != NULL && (flags & __ALLOC_ZERO))
157                 memset(result, 0, nr_bytes);
158         return result;
159 }
160
161 #define cfs_free(addr)  free(addr)
162 #define cfs_alloc_large(nr_bytes) cfs_alloc(nr_bytes, 0)
163 #define cfs_free_large(addr) cfs_free(addr)
164
165 #define CFS_ALLOC_ATOMIC_TRY   (0)
166 /*
167  * SLAB allocator
168  */
169 typedef struct {
170          int size;
171 } cfs_mem_cache_t;
172
173 #define SLAB_HWCACHE_ALIGN 0
174 #define SLAB_KERNEL 0
175 #define SLAB_NOFS 0
176
177 cfs_mem_cache_t *
178 cfs_mem_cache_create(const char *, size_t, size_t, unsigned long);
179 int cfs_mem_cache_destroy(cfs_mem_cache_t *c);
180 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp);
181 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr);
182
183 typedef int (cfs_read_proc_t)(char *page, char **start, off_t off,
184                           int count, int *eof, void *data);
185
186 struct file; /* forward ref */
187 typedef int (cfs_write_proc_t)(struct file *file, const char *buffer,
188                                unsigned long count, void *data);
189
190 /*
191  * Signal
192  */
193 typedef sigset_t                        cfs_sigset_t;
194
195 /*
196  * Timer
197  */
198 #include <sys/time.h>
199
200 typedef struct {
201         struct list_head tl_list;
202         void (*function)(unsigned long unused);
203         unsigned long data;
204         long expires;
205 } cfs_timer_t;
206
207 #define cfs_init_timer(t)       do {} while(0)
208 #define cfs_jiffies                             \
209 ({                                              \
210         unsigned long _ret = 0;                 \
211         struct timeval tv;                      \
212         if (gettimeofday(&tv, NULL) == 0)       \
213                 _ret = tv.tv_sec;               \
214         _ret;                                   \
215 })
216
217 static inline int cfs_timer_init(cfs_timer_t *l, void (* func)(unsigned long), void *arg)
218 {
219         CFS_INIT_LIST_HEAD(&l->tl_list);
220         l->function = func;
221         l->data = (unsigned long)arg;
222         return 0;
223 }
224
225 static inline int cfs_timer_is_armed(cfs_timer_t *l)
226 {
227         if (cfs_time_before(cfs_jiffies, l->expires))
228                 return 1;
229         else
230                 return 0;
231 }
232
233 static inline void cfs_timer_arm(cfs_timer_t *l, int thetime)
234 {
235         l->expires = thetime;
236 }
237
238 static inline void cfs_timer_disarm(cfs_timer_t *l)
239 {
240 }
241
242 static inline long cfs_timer_deadline(cfs_timer_t *l)
243 {
244         return l->expires;
245 }
246
247 #if 0
248 #define cfs_init_timer(t)       do {} while(0)
249 void cfs_timer_init(struct cfs_timer *t, void (*func)(unsigned long), void *arg);
250 void cfs_timer_done(struct cfs_timer *t);
251 void cfs_timer_arm(struct cfs_timer *t, cfs_time_t deadline);
252 void cfs_timer_disarm(struct cfs_timer *t);
253 int  cfs_timer_is_armed(struct cfs_timer *t);
254
255 cfs_time_t cfs_timer_deadline(struct cfs_timer *t);
256 #endif
257
258 #define in_interrupt()    (0)
259
260 static inline void cfs_pause(cfs_duration_t d)
261 {
262         struct timespec s;
263         
264         cfs_duration_nsec(d, &s);
265         nanosleep(&s, NULL);
266 }
267
268 typedef void cfs_psdev_t;
269
270 static inline int cfs_psdev_register(cfs_psdev_t *foo)
271 {
272         return 0;
273 }
274
275 static inline int cfs_psdev_deregister(cfs_psdev_t *foo)
276 {
277         return 0;
278 }
279
280 /*
281  * portable UNIX device file identification.
282  */
283
284 typedef unsigned int cfs_rdev_t;
285 // typedef unsigned long long kdev_t;
286 /*
287  */
288 #define cfs_lock_kernel()               do {} while (0)
289 #define cfs_sigfillset(l) do {}         while (0)
290 #define cfs_recalc_sigpending(l)        do {} while (0)
291 #define cfs_kernel_thread(l,m,n)        LBUG()
292
293 // static inline void local_irq_save(unsigned long flag) {return;}
294 // static inline void local_irq_restore(unsigned long flag) {return;}
295
296 enum {
297         CFS_STACK_TRACE_DEPTH = 16
298 };
299
300 struct cfs_stack_trace {
301         void *frame[CFS_STACK_TRACE_DEPTH];
302 };
303
304 /*
305  * arithmetic
306  */
307 #define do_div(a,b)                     \
308         ({                              \
309                 unsigned long remainder;\
310                 remainder = (a) % (b);  \
311                 (a) = (a) / (b);        \
312                 (remainder);            \
313         })
314
315
316 /* !__KERNEL__ */
317 #endif
318
319 /* __LIBCFS_USER_PRIM_H__ */
320 #endif
321 /*
322  * Local variables:
323  * c-indentation-style: "K&R"
324  * c-basic-offset: 8
325  * tab-width: 8
326  * fill-column: 80
327  * scroll-step: 1
328  * End:
329  */