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