Whamcloud - gitweb
LU-56 libcfs: move range expression parser to libcfs
[fs/lustre-release.git] / libcfs / include / libcfs / posix / libcfs.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/include/libcfs/posix/libcfs.h
35  *
36  * Defines for posix userspace.
37  *
38  * Author: Robert Read <rread@sun.com>
39  */
40
41 #ifndef __LIBCFS_POSIX_LIBCFS_H__
42 #define __LIBCFS_POSIX_LIBCFS_H__
43
44 #include <errno.h>
45 #include <sys/errno.h>
46 #include <string.h>
47 #include <stdarg.h>
48 #include <stddef.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <sys/mman.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #include <limits.h>
55 #include <assert.h>
56 #include <sys/ioctl.h>
57 #include <sys/signal.h>
58 #include <signal.h>
59 #include <sys/time.h>
60 #include <time.h>
61 #include <getopt.h>
62 #include <signal.h>
63 #include <pwd.h>
64 #include <sys/socket.h>
65 #include <sys/utsname.h>
66 #include <ctype.h>
67
68 #ifdef HAVE_NETDB_H
69 #include <netdb.h>
70 #endif
71
72 #ifdef HAVE_UNISTD_H
73 #include <unistd.h>
74 #endif
75
76 #ifdef HAVE_LIBPTHREAD
77 #include <pthread.h>
78 #endif
79
80 #if defined(HAVE_SYS_TYPES_H)
81 #include <sys/types.h>
82 #endif
83
84 #ifdef HAVE_SYS_USER_H
85 # include <sys/user.h>
86 #endif
87
88 #ifdef HAVE_SYS_VFS_H
89 # include <sys/vfs.h>
90 #endif
91
92 #ifdef HAVE_STDINT_H
93 # include <stdint.h>
94 #endif
95
96 #include <libcfs/list.h>
97 #include <libcfs/posix/posix-types.h>
98 #include <libcfs/user-time.h>
99 #include <libcfs/user-prim.h>
100 #include <libcfs/user-mem.h>
101 #include <libcfs/user-lock.h>
102 #include <libcfs/user-tcpip.h>
103 #include <libcfs/posix/posix-wordsize.h>
104 #include <libcfs/user-bitops.h>
105
106 # define cfs_gettimeofday(tv) gettimeofday(tv, NULL);
107 typedef unsigned long long cfs_cycles_t;
108
109 #define IS_ERR(a) ((unsigned long)(a) > (unsigned long)-1000L)
110 #define PTR_ERR(a) ((long)(a))
111 #define ERR_PTR(a) ((void*)((long)(a)))
112
113 /* this goes in posix-fs.h */
114 #include <sys/mount.h>
115
116 #ifdef __linux__
117 #include <mntent.h>
118 #endif
119
120 typedef struct file cfs_file_t;
121 typedef struct dentry cfs_dentry_t;
122 #ifdef __linux__
123 typedef struct dirent64 cfs_dirent_t;
124 #endif
125
126 #define cfs_get_fd(x)   NULL
127 #define cfs_put_file(f) do {} while (0)
128
129 #ifdef __linux__
130 /* Userpace byte flipping */
131 # include <endian.h>
132 # include <byteswap.h>
133 # define __swab16(x) bswap_16(x)
134 # define __swab32(x) bswap_32(x)
135 # define __swab64(x) bswap_64(x)
136 # define __swab16s(x) do {*(x) = bswap_16(*(x));} while (0)
137 # define __swab32s(x) do {*(x) = bswap_32(*(x));} while (0)
138 # define __swab64s(x) do {*(x) = bswap_64(*(x));} while (0)
139 # if __BYTE_ORDER == __LITTLE_ENDIAN
140 #  define le16_to_cpu(x) (x)
141 #  define cpu_to_le16(x) (x)
142 #  define le32_to_cpu(x) (x)
143 #  define cpu_to_le32(x) (x)
144 #  define le64_to_cpu(x) (x)
145 #  define cpu_to_le64(x) (x)
146
147 #  define be16_to_cpu(x) bswap_16(x)
148 #  define cpu_to_be16(x) bswap_16(x)
149 #  define be32_to_cpu(x) bswap_32(x)
150 #  define cpu_to_be32(x) bswap_32(x)
151 #  define be64_to_cpu(x) (__u64)bswap_64(x)
152 #  define cpu_to_be64(x) (__u64)bswap_64(x)
153 # else
154 #  if __BYTE_ORDER == __BIG_ENDIAN
155 #   define le16_to_cpu(x) bswap_16(x)
156 #   define cpu_to_le16(x) bswap_16(x)
157 #   define le32_to_cpu(x) bswap_32(x)
158 #   define cpu_to_le32(x) bswap_32(x)
159 #   define le64_to_cpu(x) (__u64)bswap_64(x)
160 #   define cpu_to_le64(x) (__u64)bswap_64(x)
161
162 #   define be16_to_cpu(x) (x)
163 #   define cpu_to_be16(x) (x)
164 #   define be32_to_cpu(x) (x)
165 #   define cpu_to_be32(x) (x)
166 #   define be64_to_cpu(x) (x)
167 #   define cpu_to_be64(x) (x)
168
169 #  else
170 #   error "Unknown byte order"
171 #  endif /* __BIG_ENDIAN */
172 # endif /* __LITTLE_ENDIAN */
173 #elif __APPLE__
174 #define __cpu_to_le64(x)                        OSSwapHostToLittleInt64(x)
175 #define __cpu_to_le32(x)                        OSSwapHostToLittleInt32(x)
176 #define __cpu_to_le16(x)                        OSSwapHostToLittleInt16(x)
177
178 #define __le16_to_cpu(x)                        OSSwapLittleToHostInt16(x)
179 #define __le32_to_cpu(x)                        OSSwapLittleToHostInt32(x)
180 #define __le64_to_cpu(x)                        OSSwapLittleToHostInt64(x)
181
182 #define cpu_to_le64(x)                          __cpu_to_le64(x)
183 #define cpu_to_le32(x)                          __cpu_to_le32(x)
184 #define cpu_to_le16(x)                          __cpu_to_le16(x)
185
186 #define le64_to_cpu(x)                          __le64_to_cpu(x)
187 #define le32_to_cpu(x)                          __le32_to_cpu(x)
188 #define le16_to_cpu(x)                          __le16_to_cpu(x)
189
190 #define __swab16(x)                             OSSwapInt16(x)
191 #define __swab32(x)                             OSSwapInt32(x)
192 #define __swab64(x)                             OSSwapInt64(x)
193 #define __swab16s(x)                            do { *(x) = __swab16(*(x)); } while (0)
194 #define __swab32s(x)                            do { *(x) = __swab32(*(x)); } while (0)
195 #define __swab64s(x)                            do { *(x) = __swab64(*(x)); } while (0)
196 #endif
197
198
199 # ifndef THREAD_SIZE /* x86_64 linux has THREAD_SIZE in userspace */
200 #  define CFS_THREAD_SIZE 8192
201 # else
202 #  define CFS_THREAD_SIZE THREAD_SIZE
203 # endif
204
205 #define LUSTRE_TRACE_SIZE (CFS_THREAD_SIZE >> 5)
206
207 #define CFS_CHECK_STACK(msgdata, mask, cdls) do {} while(0)
208 #define CDEBUG_STACK() (0L)
209
210 /* initial pid  */
211 #define LUSTRE_LNET_PID          12345
212
213 #define ENTRY_NESTING_SUPPORT (1)
214 #define ENTRY_NESTING   do {;} while (0)
215 #define EXIT_NESTING   do {;} while (0)
216 #define __current_nesting_level() (0)
217
218 /**
219  * Platform specific declarations for cfs_curproc API (libcfs/curproc.h)
220  *
221  * Implementation is in linux-curproc.c
222  */
223 #define CFS_CURPROC_COMM_MAX (sizeof ((struct task_struct *)0)->comm)
224
225 typedef __u32 cfs_kernel_cap_t;
226
227 /**
228  * Module support (probably shouldn't be used in generic code?)
229  */
230 typedef struct cfs_module {
231         int count;
232         char *name;
233 } cfs_module_t;
234
235 static inline void MODULE_AUTHOR(char *name)
236 {
237         printf("%s\n", name);
238 }
239 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
240 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
241
242 #define THIS_MODULE (void *)0x11111
243 #define __init
244 #define __exit
245
246 #define EXPORT_SYMBOL(symbol)
247
248 static inline int cfs_request_module(const char *name, ...)
249 {
250         return (-EINVAL);
251 }
252
253 static inline void __cfs_module_get(cfs_module_t *module)
254 {
255 }
256
257 static inline int cfs_try_module_get(cfs_module_t *module)
258 {
259         return 1;
260 }
261
262 static inline void cfs_module_put(cfs_module_t *module)
263 {
264 }
265
266
267 static inline int cfs_module_refcount(cfs_module_t *m)
268 {
269         return 1;
270 }
271
272 /***************************************************************************
273  *
274  * Linux kernel slab shrinker emulation. Currently used only in lu_object.c
275  *
276  ***************************************************************************/
277
278 struct cfs_shrinker {
279         ;
280 };
281
282 #define CFS_DEFAULT_SEEKS (0)
283
284 typedef int (*cfs_shrinker_t)(int, unsigned int);
285
286 static inline
287 struct cfs_shrinker *cfs_set_shrinker(int seeks, cfs_shrinker_t shrink)
288 {
289         return (struct cfs_shrinker *)0xdeadbea1; // Cannot return NULL here
290 }
291
292 static inline void cfs_remove_shrinker(struct cfs_shrinker *shrinker)
293 {
294 }
295
296 /***************************************************************************
297  *
298  * Linux kernel radix tree emulation.
299  *
300  * XXX this stub-implementation assumes that elements stored in a radix tree
301  *     are struct page's and nothing else. Proper implementation will be
302  *     committed soon.
303  *
304  ***************************************************************************/
305
306 struct radix_tree_root {
307         cfs_list_t list;
308         void *rnode;
309 };
310
311 struct radix_tree_node {
312         cfs_list_t _node;
313         unsigned long index;
314         void *item;
315 };
316
317 #define RADIX_TREE_INIT(mask)   {               \
318                 NOT_IMPLEMENTED                 \
319 }
320
321 #define RADIX_TREE(name, mask) \
322         struct radix_tree_root name = RADIX_TREE_INIT(mask)
323
324
325 #define INIT_RADIX_TREE(root, mask)                                     \
326 do {                                                                    \
327         CFS_INIT_LIST_HEAD(&((struct radix_tree_root *)root)->list);    \
328         ((struct radix_tree_root *)root)->rnode = NULL;                 \
329 } while (0)
330
331 static inline int radix_tree_insert(struct radix_tree_root *root,
332                         unsigned long idx, void *item)
333 {
334         struct radix_tree_node *node;
335         node = malloc(sizeof(*node));
336         if (!node)
337                 return -ENOMEM;
338
339         CFS_INIT_LIST_HEAD(&node->_node);
340         node->index = idx;
341         node->item = item;
342         cfs_list_add_tail(&node->_node, &root->list);
343         root->rnode = (void *)1001;
344         return 0;
345 }
346
347 static inline struct radix_tree_node *radix_tree_lookup0(struct radix_tree_root *root,
348                                       unsigned long idx)
349 {
350         struct radix_tree_node *node;
351
352         if (cfs_list_empty(&root->list))
353                 return NULL;
354
355         cfs_list_for_each_entry_typed(node, &root->list,
356                                       struct radix_tree_node, _node)
357                 if (node->index == idx)
358                         return node;
359
360         return NULL;
361 }
362
363 static inline void *radix_tree_lookup(struct radix_tree_root *root,
364                                       unsigned long idx)
365 {
366         struct radix_tree_node *node = radix_tree_lookup0(root, idx);
367
368         if (node)
369                 return node->item;
370         return node;
371 }
372
373 static inline void *radix_tree_delete(struct radix_tree_root *root,
374                                       unsigned long idx)
375 {
376         struct radix_tree_node *p = radix_tree_lookup0(root, idx);
377         void *item;
378
379         if (p == NULL)
380                 return NULL;
381
382         cfs_list_del_init(&p->_node);
383         item = p->item;
384         free(p);
385         if (cfs_list_empty(&root->list))
386                 root->rnode = NULL;
387
388         return item;
389 }
390
391 static inline unsigned int
392 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
393                        unsigned long first_index, unsigned int max_items)
394 {
395         int i;
396         int j = 0;
397
398         for (i = 0; i < max_items; i++, first_index++) {
399                 results[j++] = radix_tree_lookup(root, first_index);
400                 if (results[j - 1] == NULL)
401                         --j;
402         }
403
404         return j;
405 }
406
407 static inline int radix_tree_preload(int gfp_mask)
408 {
409         return 0;
410 }
411
412 void radix_tree_init(void);
413
414 static inline void radix_tree_preload_end(void)
415 {
416 }
417
418 typedef ssize_t (*read_actor_t)();
419
420 #define CFS_IFSHIFT 12
421
422 #define CFS_IFTODT(type)           (((type) & S_IFMT) >> CFS_IFSHIFT)
423 #define CFS_DTTOIF(dirtype)        ((dirtype) << CFS_IFSHIFT)
424
425 #endif