4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/include/libcfs/posix/libcfs.h
38 * Defines for posix userspace.
40 * Author: Robert Read <rread@sun.com>
43 #ifndef __LIBCFS_POSIX_LIBCFS_H__
44 #define __LIBCFS_POSIX_LIBCFS_H__
47 #include <sys/errno.h>
58 #include <sys/ioctl.h>
59 #include <sys/signal.h>
66 #include <sys/socket.h>
67 #include <sys/utsname.h>
79 #ifdef HAVE_LIBPTHREAD
83 #if defined(HAVE_SYS_TYPES_H)
84 #include <sys/types.h>
87 #ifdef HAVE_SYS_USER_H
88 # include <sys/user.h>
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>
109 # define cfs_gettimeofday(tv) gettimeofday(tv, NULL);
110 typedef unsigned long long cfs_cycles_t;
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)))
116 /* this goes in posix-fs.h */
117 #include <sys/mount.h>
124 #define fput(f) do {} while (0)
127 /* Userpace byte flipping */
129 # include <byteswap.h>
130 # define __swab16(x) bswap_16(x)
131 # define __swab32(x) bswap_32(x)
132 # define __swab64(x) bswap_64(x)
133 # define __swab16s(x) do {*(x) = bswap_16(*(x));} while (0)
134 # define __swab32s(x) do {*(x) = bswap_32(*(x));} while (0)
135 # define __swab64s(x) do {*(x) = bswap_64(*(x));} while (0)
136 # if __BYTE_ORDER == __LITTLE_ENDIAN
137 # define le16_to_cpu(x) (x)
138 # define cpu_to_le16(x) (x)
139 # define le32_to_cpu(x) (x)
140 # define cpu_to_le32(x) (x)
141 # define le64_to_cpu(x) (x)
142 # define cpu_to_le64(x) (x)
144 # define be16_to_cpu(x) bswap_16(x)
145 # define cpu_to_be16(x) bswap_16(x)
146 # define be32_to_cpu(x) bswap_32(x)
147 # define cpu_to_be32(x) bswap_32(x)
148 # define be64_to_cpu(x) (__u64)bswap_64(x)
149 # define cpu_to_be64(x) (__u64)bswap_64(x)
151 # if __BYTE_ORDER == __BIG_ENDIAN
152 # define le16_to_cpu(x) bswap_16(x)
153 # define cpu_to_le16(x) bswap_16(x)
154 # define le32_to_cpu(x) bswap_32(x)
155 # define cpu_to_le32(x) bswap_32(x)
156 # define le64_to_cpu(x) (__u64)bswap_64(x)
157 # define cpu_to_le64(x) (__u64)bswap_64(x)
159 # define be16_to_cpu(x) (x)
160 # define cpu_to_be16(x) (x)
161 # define be32_to_cpu(x) (x)
162 # define cpu_to_be32(x) (x)
163 # define be64_to_cpu(x) (x)
164 # define cpu_to_be64(x) (x)
167 # error "Unknown byte order"
168 # endif /* __BIG_ENDIAN */
169 # endif /* __LITTLE_ENDIAN */
171 #define __cpu_to_le64(x) OSSwapHostToLittleInt64(x)
172 #define __cpu_to_le32(x) OSSwapHostToLittleInt32(x)
173 #define __cpu_to_le16(x) OSSwapHostToLittleInt16(x)
175 #define __le16_to_cpu(x) OSSwapLittleToHostInt16(x)
176 #define __le32_to_cpu(x) OSSwapLittleToHostInt32(x)
177 #define __le64_to_cpu(x) OSSwapLittleToHostInt64(x)
179 #define cpu_to_le64(x) __cpu_to_le64(x)
180 #define cpu_to_le32(x) __cpu_to_le32(x)
181 #define cpu_to_le16(x) __cpu_to_le16(x)
183 #define le64_to_cpu(x) __le64_to_cpu(x)
184 #define le32_to_cpu(x) __le32_to_cpu(x)
185 #define le16_to_cpu(x) __le16_to_cpu(x)
187 #define __swab16(x) OSSwapInt16(x)
188 #define __swab32(x) OSSwapInt32(x)
189 #define __swab64(x) OSSwapInt64(x)
190 #define __swab16s(x) do { *(x) = __swab16(*(x)); } while (0)
191 #define __swab32s(x) do { *(x) = __swab32(*(x)); } while (0)
192 #define __swab64s(x) do { *(x) = __swab64(*(x)); } while (0)
196 #define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
197 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
200 # ifndef THREAD_SIZE /* x86_64 linux has THREAD_SIZE in userspace */
201 # define CFS_THREAD_SIZE 8192
203 # define CFS_THREAD_SIZE THREAD_SIZE
206 #define LUSTRE_TRACE_SIZE (CFS_THREAD_SIZE >> 5)
208 #define CFS_CHECK_STACK(msgdata, mask, cdls) do {} while(0)
209 #define CDEBUG_STACK() (0L)
212 #define LUSTRE_LNET_PID 12345
214 #define ENTRY_NESTING_SUPPORT (1)
215 #define ENTRY_NESTING do {;} while (0)
216 #define EXIT_NESTING do {;} while (0)
217 #define __current_nesting_level() (0)
220 * Platform specific declarations for cfs_curproc API (libcfs/curproc.h)
222 * Implementation is in linux-curproc.c
224 #define CFS_CURPROC_COMM_MAX (sizeof ((struct task_struct *)0)->comm)
226 typedef __u32 cfs_kernel_cap_t;
229 * Module support (probably shouldn't be used in generic code?)
231 typedef struct cfs_module {
236 static inline void MODULE_AUTHOR(char *name)
238 printf("%s\n", name);
240 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
241 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
243 #define THIS_MODULE (void *)0x11111
247 #define EXPORT_SYMBOL(symbol)
249 static inline int cfs_request_module(const char *name, ...)
254 static inline void __cfs_module_get(cfs_module_t *module)
258 static inline int cfs_try_module_get(cfs_module_t *module)
263 static inline void cfs_module_put(cfs_module_t *module)
268 static inline int cfs_module_refcount(cfs_module_t *m)
273 /***************************************************************************
275 * Linux kernel slab shrinker emulation. Currently used only in lu_object.c
277 ***************************************************************************/
279 struct cfs_shrinker {
283 #define CFS_DEFAULT_SEEKS (0)
285 typedef int (*cfs_shrinker_t)(int, unsigned int);
288 struct cfs_shrinker *cfs_set_shrinker(int seeks, cfs_shrinker_t shrink)
290 return (struct cfs_shrinker *)0xdeadbea1; // Cannot return NULL here
293 static inline void cfs_remove_shrinker(struct cfs_shrinker *shrinker)
297 /***************************************************************************
299 * Linux kernel radix tree emulation.
301 * XXX this stub-implementation assumes that elements stored in a radix tree
302 * are struct page's and nothing else. Proper implementation will be
305 ***************************************************************************/
307 struct radix_tree_root {
312 struct radix_tree_node {
318 #define RADIX_TREE_INIT(mask) { \
322 #define RADIX_TREE(name, mask) \
323 struct radix_tree_root name = RADIX_TREE_INIT(mask)
326 #define INIT_RADIX_TREE(root, mask) \
328 CFS_INIT_LIST_HEAD(&((struct radix_tree_root *)root)->list); \
329 ((struct radix_tree_root *)root)->rnode = NULL; \
332 static inline int radix_tree_insert(struct radix_tree_root *root,
333 unsigned long idx, void *item)
335 struct radix_tree_node *node;
336 node = malloc(sizeof(*node));
340 CFS_INIT_LIST_HEAD(&node->_node);
343 cfs_list_add_tail(&node->_node, &root->list);
344 root->rnode = (void *)1001;
348 static inline struct radix_tree_node *radix_tree_lookup0(struct radix_tree_root *root,
351 struct radix_tree_node *node;
353 if (cfs_list_empty(&root->list))
356 cfs_list_for_each_entry_typed(node, &root->list,
357 struct radix_tree_node, _node)
358 if (node->index == idx)
364 static inline void *radix_tree_lookup(struct radix_tree_root *root,
367 struct radix_tree_node *node = radix_tree_lookup0(root, idx);
374 static inline void *radix_tree_delete(struct radix_tree_root *root,
377 struct radix_tree_node *p = radix_tree_lookup0(root, idx);
383 cfs_list_del_init(&p->_node);
386 if (cfs_list_empty(&root->list))
392 static inline unsigned int
393 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
394 unsigned long first_index, unsigned int max_items)
399 for (i = 0; i < max_items; i++, first_index++) {
400 results[j++] = radix_tree_lookup(root, first_index);
401 if (results[j - 1] == NULL)
408 static inline int radix_tree_preload(int gfp_mask)
413 void radix_tree_init(void);
415 static inline void radix_tree_preload_end(void)
419 /***************************************************************************
421 * Linux kernel red black tree emulation.
423 ***************************************************************************/
425 unsigned long rb_parent_color;
428 struct rb_node *rb_right;
429 struct rb_node *rb_left;
433 struct rb_node *rb_node;
437 #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
438 #define rb_color(r) ((r)->rb_parent_color & 1)
439 #define rb_is_red(r) (!rb_color(r))
440 #define rb_is_black(r) rb_color(r)
441 #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
442 #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
444 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
446 rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
448 static inline void rb_set_color(struct rb_node *rb, int color)
450 rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
453 #define RB_ROOT ((struct rb_root) { NULL, })
454 #define rb_entry(ptr, type, member) container_of(ptr, type, member)
456 #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
457 #define RB_EMPTY_NODE(node) (rb_parent(node) == node)
458 #define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
460 static inline void rb_init_node(struct rb_node *rb)
462 rb->rb_parent_color = 0;
468 extern void rb_insert_color(struct rb_node *, struct rb_root *);
469 extern void rb_erase(struct rb_node *, struct rb_root *);
471 /* Find logical next and previous nodes in a tree */
472 extern struct rb_node *rb_next(const struct rb_node *);
473 extern struct rb_node *rb_prev(const struct rb_node *);
474 extern struct rb_node *rb_first(const struct rb_root *);
475 extern struct rb_node *rb_last(const struct rb_root *);
476 static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
477 struct rb_node **rb_link)
479 node->rb_parent_color = (unsigned long)parent;
480 node->rb_left = node->rb_right = NULL;
485 /***************************************************************************
487 * End of Linux kernel red black tree emulation.
489 ***************************************************************************/
491 typedef ssize_t (*read_actor_t)();
495 # define IFTODT(type) (((type) & S_IFMT) >> IFSHIFT)
496 # define DTTOIF(dirtype) ((dirtype) << IFSHIFT)