Whamcloud - gitweb
LU-2675 libcfs: add libcfs/byteorder.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 #include <mntent.h>
115
116 #define fget(x) NULL
117 #define fput(f) do {} while (0)
118
119 #ifndef THREAD_SIZE /* x86_64 linux has THREAD_SIZE in userspace */
120 # define THREAD_SIZE 8192
121 #endif /* THREAD_SIZE */
122
123 #define CFS_CHECK_STACK(msgdata, mask, cdls) do {} while(0)
124 #define CDEBUG_STACK() (0L)
125
126 /**
127  * Platform specific declarations for cfs_curproc API (libcfs/curproc.h)
128  *
129  * Implementation is in linux-curproc.c
130  */
131 #define CFS_CURPROC_COMM_MAX (sizeof ((struct task_struct *)0)->comm)
132
133 typedef __u32 kernel_cap_t;
134
135 /**
136  * Module support (probably shouldn't be used in generic code?)
137  */
138 struct module {
139         int count;
140         char *name;
141 };
142
143 static inline void MODULE_AUTHOR(char *name)
144 {
145         printf("%s\n", name);
146 }
147 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
148 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
149
150 #define THIS_MODULE (void *)0x11111
151 #define __init
152 #define __exit
153
154 static inline int request_module(const char *name, ...)
155 {
156         return (-EINVAL);
157 }
158
159 static inline void __module_get(struct module *module)
160 {
161 }
162
163 static inline int try_module_get(struct module *module)
164 {
165         return 1;
166 }
167
168 static inline void module_put(struct module *module)
169 {
170 }
171
172
173 static inline int module_refcount(struct module *m)
174 {
175         return 1;
176 }
177
178 /***************************************************************************
179  *
180  * Linux kernel slab shrinker emulation. Currently used only in lu_object.c
181  *
182  ***************************************************************************/
183
184 struct shrinker {
185 #ifndef __INTEL_COMPILER
186         ;
187 #endif
188 };
189
190 struct shrinker_var {
191 #ifndef __INTEL_COMPILER
192         ;
193 #endif
194 };
195
196 #define DEF_SHRINKER_VAR(name, shrink, count, scan) \
197                 struct shrinker_var name = {};
198
199 #define DEFAULT_SEEKS (0)
200
201 static inline
202 struct shrinker *set_shrinker(int seeks, struct shrinker_var *var)
203 {
204         return (struct shrinker *)0xdeadbea1; /* Cannot return NULL here */
205 }
206
207 static inline void remove_shrinker(struct shrinker *shrinker)
208 {
209 }
210
211 /***************************************************************************
212  *
213  * Linux kernel radix tree emulation.
214  *
215  * XXX this stub-implementation assumes that elements stored in a radix tree
216  *     are struct page's and nothing else. Proper implementation will be
217  *     committed soon.
218  *
219  ***************************************************************************/
220
221 struct radix_tree_root {
222         struct list_head list;
223         void *rnode;
224 };
225
226 struct radix_tree_node {
227         struct list_head _node;
228         unsigned long index;
229         void *item;
230 };
231
232 #define RADIX_TREE_INIT(mask)   {       \
233                 NOT_IMPLEMENTED         \
234 }
235
236 #define RADIX_TREE(name, mask) \
237         struct radix_tree_root name = RADIX_TREE_INIT(mask)
238
239
240 #define INIT_RADIX_TREE(root, mask)                                     \
241 do {                                                                    \
242         INIT_LIST_HEAD(&((struct radix_tree_root *)root)->list);        \
243         ((struct radix_tree_root *)root)->rnode = NULL;                 \
244 } while (0)
245
246 static inline int radix_tree_insert(struct radix_tree_root *root,
247                                         unsigned long idx, void *item)
248 {
249         struct radix_tree_node *node;
250         node = malloc(sizeof(*node));
251         if (!node)
252                 return -ENOMEM;
253
254         INIT_LIST_HEAD(&node->_node);
255         node->index = idx;
256         node->item = item;
257         list_add_tail(&node->_node, &root->list);
258         root->rnode = (void *)1001;
259         return 0;
260 }
261
262 static inline struct radix_tree_node *
263 radix_tree_lookup0(struct radix_tree_root *root, unsigned long idx)
264 {
265         struct radix_tree_node *node;
266
267         if (list_empty(&root->list))
268                 return NULL;
269
270         list_for_each_entry(node, &root->list, _node)
271                 if (node->index == idx)
272                         return node;
273
274         return NULL;
275 }
276
277 static inline void *radix_tree_lookup(struct radix_tree_root *root,
278                                         unsigned long idx)
279 {
280         struct radix_tree_node *node = radix_tree_lookup0(root, idx);
281
282         if (node)
283                 return node->item;
284         return node;
285 }
286
287 static inline void *radix_tree_delete(struct radix_tree_root *root,
288                                         unsigned long idx)
289 {
290         struct radix_tree_node *p = radix_tree_lookup0(root, idx);
291         void *item;
292
293         if (p == NULL)
294                 return NULL;
295
296         list_del_init(&p->_node);
297         item = p->item;
298         free(p);
299         if (list_empty(&root->list))
300                 root->rnode = NULL;
301
302         return item;
303 }
304
305 static inline unsigned int
306 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
307                        unsigned long first_index, unsigned int max_items)
308 {
309         int i;
310         int j = 0;
311
312         for (i = 0; i < max_items; i++, first_index++) {
313                 results[j++] = radix_tree_lookup(root, first_index);
314                 if (results[j - 1] == NULL)
315                         --j;
316         }
317
318         return j;
319 }
320
321 static inline int radix_tree_preload(int gfp_mask)
322 {
323         return 0;
324 }
325
326 void radix_tree_init(void);
327
328 static inline void radix_tree_preload_end(void)
329 {
330 }
331
332 /***************************************************************************
333  *
334  * Linux kernel red black tree emulation.
335  *
336  ***************************************************************************/
337 struct rb_node {
338         unsigned long  rb_parent_color;
339 #define RB_RED          0
340 #define RB_BLACK        1
341         struct rb_node *rb_right;
342         struct rb_node *rb_left;
343 };
344
345 struct rb_root {
346         struct rb_node *rb_node;
347 };
348
349
350 #define rb_parent(r)   ((struct rb_node *)((r)->rb_parent_color & ~3))
351 #define rb_color(r)   ((r)->rb_parent_color & 1)
352 #define rb_is_red(r)   (!rb_color(r))
353 #define rb_is_black(r) rb_color(r)
354 #define rb_set_red(r)  do { (r)->rb_parent_color &= ~1; } while (0)
355 #define rb_set_black(r)  do { (r)->rb_parent_color |= 1; } while (0)
356
357 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
358 {
359         rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
360 }
361 static inline void rb_set_color(struct rb_node *rb, int color)
362 {
363         rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
364 }
365
366 #define RB_ROOT ((struct rb_root) { NULL, })
367 #define rb_entry(ptr, type, member) container_of(ptr, type, member)
368
369 #define RB_EMPTY_ROOT(root)     ((root)->rb_node == NULL)
370 #define RB_EMPTY_NODE(node)     (rb_parent(node) == node)
371 #define RB_CLEAR_NODE(node)     (rb_set_parent(node, node))
372
373 static inline void rb_init_node(struct rb_node *rb)
374 {
375         rb->rb_parent_color = 0;
376         rb->rb_right = NULL;
377         rb->rb_left = NULL;
378         RB_CLEAR_NODE(rb);
379 }
380
381 extern void rb_insert_color(struct rb_node *, struct rb_root *);
382 extern void rb_erase(struct rb_node *, struct rb_root *);
383
384 /* Find logical next and previous nodes in a tree */
385 extern struct rb_node *rb_next(const struct rb_node *);
386 extern struct rb_node *rb_prev(const struct rb_node *);
387 extern struct rb_node *rb_first(const struct rb_root *);
388 extern struct rb_node *rb_last(const struct rb_root *);
389 static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
390                                 struct rb_node **rb_link)
391 {
392         node->rb_parent_color = (unsigned long)parent;
393         node->rb_left = node->rb_right = NULL;
394
395         *rb_link = node;
396 }
397
398 /***************************************************************************
399  *
400  * End of Linux kernel red black tree emulation.
401  *
402  ***************************************************************************/
403
404 typedef ssize_t (*read_actor_t)();
405
406 # ifndef IFTODT
407 #  define IFSHIFT               12
408 #  define IFTODT(type)          (((type) & S_IFMT) >> IFSHIFT)
409 #  define DTTOIF(dirtype)       ((dirtype) << IFSHIFT)
410 # endif
411
412 #ifndef ERESTARTSYS
413 #define ERESTARTSYS ERESTART
414 #endif
415
416 #endif