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