Whamcloud - gitweb
5d7dfd28aeda8f7330608525af671209befe9843
[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  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/include/libcfs/posix/libcfs.h
37  *
38  * Defines for posix userspace.
39  *
40  * Author: Robert Read <rread@sun.com>
41  */
42
43 #ifndef __LIBCFS_POSIX_LIBCFS_H__
44 #define __LIBCFS_POSIX_LIBCFS_H__
45
46 #include <errno.h>
47 #include <sys/errno.h>
48 #include <string.h>
49 #include <stdarg.h>
50 #include <stddef.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <sys/mman.h>
54 #include <sys/stat.h>
55 #include <fcntl.h>
56 #include <limits.h>
57 #include <assert.h>
58 #include <sys/ioctl.h>
59 #include <sys/signal.h>
60 #include <signal.h>
61 #include <sys/time.h>
62 #include <time.h>
63 #include <getopt.h>
64 #include <signal.h>
65 #include <pwd.h>
66 #include <sys/socket.h>
67 #include <sys/utsname.h>
68 #include <ctype.h>
69 #include <stdbool.h>
70
71 #ifdef HAVE_NETDB_H
72 #include <netdb.h>
73 #endif
74
75 #ifdef HAVE_UNISTD_H
76 #include <unistd.h>
77 #endif
78
79 #ifdef HAVE_LIBPTHREAD
80 #include <pthread.h>
81 #endif
82
83 #if defined(HAVE_SYS_TYPES_H)
84 #include <sys/types.h>
85 #endif
86
87 #ifdef HAVE_SYS_USER_H
88 # include <sys/user.h>
89 #endif
90
91 #ifdef HAVE_SYS_VFS_H
92 # include <sys/vfs.h>
93 #endif
94
95 #ifdef HAVE_STDINT_H
96 # include <stdint.h>
97 #endif
98
99 #include <libcfs/list.h>
100 #include <libcfs/posix/posix-types.h>
101 #include <libcfs/user-time.h>
102 #include <libcfs/user-prim.h>
103 #include <libcfs/user-mem.h>
104 #include <libcfs/user-lock.h>
105 #include <libcfs/user-tcpip.h>
106 #include <libcfs/posix/posix-wordsize.h>
107 #include <libcfs/user-bitops.h>
108
109 # define cfs_gettimeofday(tv) gettimeofday(tv, NULL);
110 typedef unsigned long long cfs_cycles_t;
111
112 #define IS_ERR(a) ((unsigned long)(a) > (unsigned long)-1000L)
113 #define PTR_ERR(a) ((long)(a))
114 #define ERR_PTR(a) ((void*)((long)(a)))
115
116 /* this goes in posix-fs.h */
117 #include <sys/mount.h>
118
119 #ifdef __linux__
120 #include <mntent.h>
121 #endif
122
123 typedef struct file cfs_file_t;
124 typedef struct dentry cfs_dentry_t;
125 #ifdef __linux__
126 typedef struct dirent64 cfs_dirent_t;
127 #endif
128
129 #define cfs_get_fd(x)   NULL
130 #define cfs_put_file(f) do {} while (0)
131
132 #ifdef __linux__
133 /* Userpace byte flipping */
134 # include <endian.h>
135 # include <byteswap.h>
136 # define __swab16(x) bswap_16(x)
137 # define __swab32(x) bswap_32(x)
138 # define __swab64(x) bswap_64(x)
139 # define __swab16s(x) do {*(x) = bswap_16(*(x));} while (0)
140 # define __swab32s(x) do {*(x) = bswap_32(*(x));} while (0)
141 # define __swab64s(x) do {*(x) = bswap_64(*(x));} while (0)
142 # if __BYTE_ORDER == __LITTLE_ENDIAN
143 #  define le16_to_cpu(x) (x)
144 #  define cpu_to_le16(x) (x)
145 #  define le32_to_cpu(x) (x)
146 #  define cpu_to_le32(x) (x)
147 #  define le64_to_cpu(x) (x)
148 #  define cpu_to_le64(x) (x)
149
150 #  define be16_to_cpu(x) bswap_16(x)
151 #  define cpu_to_be16(x) bswap_16(x)
152 #  define be32_to_cpu(x) bswap_32(x)
153 #  define cpu_to_be32(x) bswap_32(x)
154 #  define be64_to_cpu(x) (__u64)bswap_64(x)
155 #  define cpu_to_be64(x) (__u64)bswap_64(x)
156 # else
157 #  if __BYTE_ORDER == __BIG_ENDIAN
158 #   define le16_to_cpu(x) bswap_16(x)
159 #   define cpu_to_le16(x) bswap_16(x)
160 #   define le32_to_cpu(x) bswap_32(x)
161 #   define cpu_to_le32(x) bswap_32(x)
162 #   define le64_to_cpu(x) (__u64)bswap_64(x)
163 #   define cpu_to_le64(x) (__u64)bswap_64(x)
164
165 #   define be16_to_cpu(x) (x)
166 #   define cpu_to_be16(x) (x)
167 #   define be32_to_cpu(x) (x)
168 #   define cpu_to_be32(x) (x)
169 #   define be64_to_cpu(x) (x)
170 #   define cpu_to_be64(x) (x)
171
172 #  else
173 #   error "Unknown byte order"
174 #  endif /* __BIG_ENDIAN */
175 # endif /* __LITTLE_ENDIAN */
176 #elif __APPLE__
177 #define __cpu_to_le64(x)                        OSSwapHostToLittleInt64(x)
178 #define __cpu_to_le32(x)                        OSSwapHostToLittleInt32(x)
179 #define __cpu_to_le16(x)                        OSSwapHostToLittleInt16(x)
180
181 #define __le16_to_cpu(x)                        OSSwapLittleToHostInt16(x)
182 #define __le32_to_cpu(x)                        OSSwapLittleToHostInt32(x)
183 #define __le64_to_cpu(x)                        OSSwapLittleToHostInt64(x)
184
185 #define cpu_to_le64(x)                          __cpu_to_le64(x)
186 #define cpu_to_le32(x)                          __cpu_to_le32(x)
187 #define cpu_to_le16(x)                          __cpu_to_le16(x)
188
189 #define le64_to_cpu(x)                          __le64_to_cpu(x)
190 #define le32_to_cpu(x)                          __le32_to_cpu(x)
191 #define le16_to_cpu(x)                          __le16_to_cpu(x)
192
193 #define __swab16(x)                             OSSwapInt16(x)
194 #define __swab32(x)                             OSSwapInt32(x)
195 #define __swab64(x)                             OSSwapInt64(x)
196 #define __swab16s(x)                            do { *(x) = __swab16(*(x)); } while (0)
197 #define __swab32s(x)                            do { *(x) = __swab32(*(x)); } while (0)
198 #define __swab64s(x)                            do { *(x) = __swab64(*(x)); } while (0)
199 #endif
200
201
202 # ifndef THREAD_SIZE /* x86_64 linux has THREAD_SIZE in userspace */
203 #  define CFS_THREAD_SIZE 8192
204 # else
205 #  define CFS_THREAD_SIZE THREAD_SIZE
206 # endif
207
208 #define LUSTRE_TRACE_SIZE (CFS_THREAD_SIZE >> 5)
209
210 #define CFS_CHECK_STACK(msgdata, mask, cdls) do {} while(0)
211 #define CDEBUG_STACK() (0L)
212
213 /* initial pid  */
214 #define LUSTRE_LNET_PID          12345
215
216 #define ENTRY_NESTING_SUPPORT (1)
217 #define ENTRY_NESTING   do {;} while (0)
218 #define EXIT_NESTING   do {;} while (0)
219 #define __current_nesting_level() (0)
220
221 /**
222  * Platform specific declarations for cfs_curproc API (libcfs/curproc.h)
223  *
224  * Implementation is in linux-curproc.c
225  */
226 #define CFS_CURPROC_COMM_MAX (sizeof ((struct task_struct *)0)->comm)
227
228 typedef __u32 cfs_kernel_cap_t;
229
230 /**
231  * Module support (probably shouldn't be used in generic code?)
232  */
233 typedef struct cfs_module {
234         int count;
235         char *name;
236 } cfs_module_t;
237
238 static inline void MODULE_AUTHOR(char *name)
239 {
240         printf("%s\n", name);
241 }
242 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
243 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
244
245 #define THIS_MODULE (void *)0x11111
246 #define __init
247 #define __exit
248
249 #define EXPORT_SYMBOL(symbol)
250
251 static inline int cfs_request_module(const char *name, ...)
252 {
253         return (-EINVAL);
254 }
255
256 static inline void __cfs_module_get(cfs_module_t *module)
257 {
258 }
259
260 static inline int cfs_try_module_get(cfs_module_t *module)
261 {
262         return 1;
263 }
264
265 static inline void cfs_module_put(cfs_module_t *module)
266 {
267 }
268
269
270 static inline int cfs_module_refcount(cfs_module_t *m)
271 {
272         return 1;
273 }
274
275 /***************************************************************************
276  *
277  * Linux kernel slab shrinker emulation. Currently used only in lu_object.c
278  *
279  ***************************************************************************/
280
281 struct cfs_shrinker {
282         ;
283 };
284
285 #define CFS_DEFAULT_SEEKS (0)
286
287 typedef int (*cfs_shrinker_t)(int, unsigned int);
288
289 static inline
290 struct cfs_shrinker *cfs_set_shrinker(int seeks, cfs_shrinker_t shrink)
291 {
292         return (struct cfs_shrinker *)0xdeadbea1; // Cannot return NULL here
293 }
294
295 static inline void cfs_remove_shrinker(struct cfs_shrinker *shrinker)
296 {
297 }
298
299 /***************************************************************************
300  *
301  * Linux kernel radix tree emulation.
302  *
303  * XXX this stub-implementation assumes that elements stored in a radix tree
304  *     are struct page's and nothing else. Proper implementation will be
305  *     committed soon.
306  *
307  ***************************************************************************/
308
309 struct radix_tree_root {
310         cfs_list_t list;
311         void *rnode;
312 };
313
314 struct radix_tree_node {
315         cfs_list_t _node;
316         unsigned long index;
317         void *item;
318 };
319
320 #define RADIX_TREE_INIT(mask)   {               \
321                 NOT_IMPLEMENTED                 \
322 }
323
324 #define RADIX_TREE(name, mask) \
325         struct radix_tree_root name = RADIX_TREE_INIT(mask)
326
327
328 #define INIT_RADIX_TREE(root, mask)                                     \
329 do {                                                                    \
330         CFS_INIT_LIST_HEAD(&((struct radix_tree_root *)root)->list);    \
331         ((struct radix_tree_root *)root)->rnode = NULL;                 \
332 } while (0)
333
334 static inline int radix_tree_insert(struct radix_tree_root *root,
335                         unsigned long idx, void *item)
336 {
337         struct radix_tree_node *node;
338         node = malloc(sizeof(*node));
339         if (!node)
340                 return -ENOMEM;
341
342         CFS_INIT_LIST_HEAD(&node->_node);
343         node->index = idx;
344         node->item = item;
345         cfs_list_add_tail(&node->_node, &root->list);
346         root->rnode = (void *)1001;
347         return 0;
348 }
349
350 static inline struct radix_tree_node *radix_tree_lookup0(struct radix_tree_root *root,
351                                       unsigned long idx)
352 {
353         struct radix_tree_node *node;
354
355         if (cfs_list_empty(&root->list))
356                 return NULL;
357
358         cfs_list_for_each_entry_typed(node, &root->list,
359                                       struct radix_tree_node, _node)
360                 if (node->index == idx)
361                         return node;
362
363         return NULL;
364 }
365
366 static inline void *radix_tree_lookup(struct radix_tree_root *root,
367                                       unsigned long idx)
368 {
369         struct radix_tree_node *node = radix_tree_lookup0(root, idx);
370
371         if (node)
372                 return node->item;
373         return node;
374 }
375
376 static inline void *radix_tree_delete(struct radix_tree_root *root,
377                                       unsigned long idx)
378 {
379         struct radix_tree_node *p = radix_tree_lookup0(root, idx);
380         void *item;
381
382         if (p == NULL)
383                 return NULL;
384
385         cfs_list_del_init(&p->_node);
386         item = p->item;
387         free(p);
388         if (cfs_list_empty(&root->list))
389                 root->rnode = NULL;
390
391         return item;
392 }
393
394 static inline unsigned int
395 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
396                        unsigned long first_index, unsigned int max_items)
397 {
398         int i;
399         int j = 0;
400
401         for (i = 0; i < max_items; i++, first_index++) {
402                 results[j++] = radix_tree_lookup(root, first_index);
403                 if (results[j - 1] == NULL)
404                         --j;
405         }
406
407         return j;
408 }
409
410 static inline int radix_tree_preload(int gfp_mask)
411 {
412         return 0;
413 }
414
415 void radix_tree_init(void);
416
417 static inline void radix_tree_preload_end(void)
418 {
419 }
420
421 /***************************************************************************
422  *
423  * Linux kernel red black tree emulation.
424  *
425  ***************************************************************************/
426 struct rb_node {
427         unsigned long  rb_parent_color;
428 #define RB_RED          0
429 #define RB_BLACK        1
430         struct rb_node *rb_right;
431         struct rb_node *rb_left;
432 };
433
434 struct rb_root {
435         struct rb_node *rb_node;
436 };
437
438
439 #define rb_parent(r)   ((struct rb_node *)((r)->rb_parent_color & ~3))
440 #define rb_color(r)   ((r)->rb_parent_color & 1)
441 #define rb_is_red(r)   (!rb_color(r))
442 #define rb_is_black(r) rb_color(r)
443 #define rb_set_red(r)  do { (r)->rb_parent_color &= ~1; } while (0)
444 #define rb_set_black(r)  do { (r)->rb_parent_color |= 1; } while (0)
445
446 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
447 {
448         rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
449 }
450 static inline void rb_set_color(struct rb_node *rb, int color)
451 {
452         rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
453 }
454
455 #define RB_ROOT ((struct rb_root) { NULL, })
456 #define rb_entry(ptr, type, member) container_of(ptr, type, member)
457
458 #define RB_EMPTY_ROOT(root)     ((root)->rb_node == NULL)
459 #define RB_EMPTY_NODE(node)     (rb_parent(node) == node)
460 #define RB_CLEAR_NODE(node)     (rb_set_parent(node, node))
461
462 static inline void rb_init_node(struct rb_node *rb)
463 {
464         rb->rb_parent_color = 0;
465         rb->rb_right = NULL;
466         rb->rb_left = NULL;
467         RB_CLEAR_NODE(rb);
468 }
469
470 extern void rb_insert_color(struct rb_node *, struct rb_root *);
471 extern void rb_erase(struct rb_node *, struct rb_root *);
472
473 /* Find logical next and previous nodes in a tree */
474 extern struct rb_node *rb_next(const struct rb_node *);
475 extern struct rb_node *rb_prev(const struct rb_node *);
476 extern struct rb_node *rb_first(const struct rb_root *);
477 extern struct rb_node *rb_last(const struct rb_root *);
478 static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
479                                 struct rb_node **rb_link)
480 {
481         node->rb_parent_color = (unsigned long)parent;
482         node->rb_left = node->rb_right = NULL;
483
484         *rb_link = node;
485 }
486
487 /***************************************************************************
488  *
489  * End of Linux kernel red black tree emulation.
490  *
491  ***************************************************************************/
492
493 typedef ssize_t (*read_actor_t)();
494
495 #define CFS_IFSHIFT 12
496
497 #define CFS_IFTODT(type)           (((type) & S_IFMT) >> CFS_IFSHIFT)
498 #define CFS_DTTOIF(dirtype)        ((dirtype) << CFS_IFSHIFT)
499
500 #endif