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