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>
80 #ifdef HAVE_LIBPTHREAD
84 #if defined(HAVE_SYS_TYPES_H)
85 #include <sys/types.h>
88 #ifdef HAVE_SYS_USER_H
89 # include <sys/user.h>
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>
108 #define do_gettimeofday(tv) gettimeofday(tv, NULL);
109 typedef unsigned long long cfs_cycles_t;
111 /* this goes in posix-fs.h */
112 #include <sys/mount.h>
117 #define fput(f) do {} while (0)
119 /* Userpace byte flipping */
121 #include <byteswap.h>
122 #define __swab16(x) bswap_16(x)
123 #define __swab32(x) bswap_32(x)
124 #define __swab64(x) bswap_64(x)
125 #define __swab16s(x) do {*(x) = bswap_16(*(x));} while (0)
126 #define __swab32s(x) do {*(x) = bswap_32(*(x));} while (0)
127 #define __swab64s(x) do {*(x) = bswap_64(*(x));} while (0)
128 #if __BYTE_ORDER == __LITTLE_ENDIAN
129 # define le16_to_cpu(x) (x)
130 # define cpu_to_le16(x) (x)
131 # define le32_to_cpu(x) (x)
132 # define cpu_to_le32(x) (x)
133 # define le64_to_cpu(x) (x)
134 # define cpu_to_le64(x) (x)
136 # define be16_to_cpu(x) bswap_16(x)
137 # define cpu_to_be16(x) bswap_16(x)
138 # define be32_to_cpu(x) bswap_32(x)
139 # define cpu_to_be32(x) bswap_32(x)
140 # define be64_to_cpu(x) ((__u64)bswap_64(x))
141 # define cpu_to_be64(x) ((__u64)bswap_64(x))
142 #elif __BYTE_ORDER == __BIG_ENDIAN
143 # define le16_to_cpu(x) bswap_16(x)
144 # define cpu_to_le16(x) bswap_16(x)
145 # define le32_to_cpu(x) bswap_32(x)
146 # define cpu_to_le32(x) bswap_32(x)
147 # define le64_to_cpu(x) ((__u64)bswap_64(x))
148 # define cpu_to_le64(x) ((__u64)bswap_64(x))
150 # define be16_to_cpu(x) (x)
151 # define cpu_to_be16(x) (x)
152 # define be32_to_cpu(x) (x)
153 # define cpu_to_be32(x) (x)
154 # define be64_to_cpu(x) (x)
155 # define cpu_to_be64(x) (x)
156 #else /* __BYTE_ORDER == __BIG_ENDIAN */
157 # error "Unknown byte order"
158 #endif /* __BYTE_ORDER != __BIG_ENDIAN */
160 #ifndef THREAD_SIZE /* x86_64 linux has THREAD_SIZE in userspace */
161 # define THREAD_SIZE 8192
162 #endif /* THREAD_SIZE */
164 #define CFS_CHECK_STACK(msgdata, mask, cdls) do {} while(0)
165 #define CDEBUG_STACK() (0L)
168 #define LUSTRE_LNET_PID 12345
170 #define ENTRY_NESTING_SUPPORT (1)
171 #define ENTRY_NESTING do {;} while (0)
172 #define EXIT_NESTING do {;} while (0)
173 #define __current_nesting_level() (0)
176 * Platform specific declarations for cfs_curproc API (libcfs/curproc.h)
178 * Implementation is in linux-curproc.c
180 #define CFS_CURPROC_COMM_MAX (sizeof ((struct task_struct *)0)->comm)
182 typedef __u32 kernel_cap_t;
185 * Module support (probably shouldn't be used in generic code?)
192 static inline void MODULE_AUTHOR(char *name)
194 printf("%s\n", name);
196 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
197 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
199 #define THIS_MODULE (void *)0x11111
203 static inline int request_module(const char *name, ...)
208 static inline void __module_get(struct module *module)
212 static inline int try_module_get(struct module *module)
217 static inline void module_put(struct module *module)
222 static inline int module_refcount(struct module *m)
227 /***************************************************************************
229 * Linux kernel slab shrinker emulation. Currently used only in lu_object.c
231 ***************************************************************************/
234 #ifndef __INTEL_COMPILER
239 struct shrinker_var {
240 #ifndef __INTEL_COMPILER
245 #define DEF_SHRINKER_VAR(name, shrink, count, scan) \
246 struct shrinker_var name = {};
248 #define DEFAULT_SEEKS (0)
251 struct shrinker *set_shrinker(int seeks, struct shrinker_var *var)
253 return (struct shrinker *)0xdeadbea1; /* Cannot return NULL here */
256 static inline void remove_shrinker(struct shrinker *shrinker)
260 /***************************************************************************
262 * Linux kernel radix tree emulation.
264 * XXX this stub-implementation assumes that elements stored in a radix tree
265 * are struct page's and nothing else. Proper implementation will be
268 ***************************************************************************/
270 struct radix_tree_root {
271 struct list_head list;
275 struct radix_tree_node {
276 struct list_head _node;
281 #define RADIX_TREE_INIT(mask) { \
285 #define RADIX_TREE(name, mask) \
286 struct radix_tree_root name = RADIX_TREE_INIT(mask)
289 #define INIT_RADIX_TREE(root, mask) \
291 INIT_LIST_HEAD(&((struct radix_tree_root *)root)->list); \
292 ((struct radix_tree_root *)root)->rnode = NULL; \
295 static inline int radix_tree_insert(struct radix_tree_root *root,
296 unsigned long idx, void *item)
298 struct radix_tree_node *node;
299 node = malloc(sizeof(*node));
303 INIT_LIST_HEAD(&node->_node);
306 list_add_tail(&node->_node, &root->list);
307 root->rnode = (void *)1001;
311 static inline struct radix_tree_node *
312 radix_tree_lookup0(struct radix_tree_root *root, unsigned long idx)
314 struct radix_tree_node *node;
316 if (list_empty(&root->list))
319 list_for_each_entry(node, &root->list, _node)
320 if (node->index == idx)
326 static inline void *radix_tree_lookup(struct radix_tree_root *root,
329 struct radix_tree_node *node = radix_tree_lookup0(root, idx);
336 static inline void *radix_tree_delete(struct radix_tree_root *root,
339 struct radix_tree_node *p = radix_tree_lookup0(root, idx);
345 list_del_init(&p->_node);
348 if (list_empty(&root->list))
354 static inline unsigned int
355 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
356 unsigned long first_index, unsigned int max_items)
361 for (i = 0; i < max_items; i++, first_index++) {
362 results[j++] = radix_tree_lookup(root, first_index);
363 if (results[j - 1] == NULL)
370 static inline int radix_tree_preload(int gfp_mask)
375 void radix_tree_init(void);
377 static inline void radix_tree_preload_end(void)
381 /***************************************************************************
383 * Linux kernel red black tree emulation.
385 ***************************************************************************/
387 unsigned long rb_parent_color;
390 struct rb_node *rb_right;
391 struct rb_node *rb_left;
395 struct rb_node *rb_node;
399 #define rb_parent(r) ((struct rb_node *)((r)->rb_parent_color & ~3))
400 #define rb_color(r) ((r)->rb_parent_color & 1)
401 #define rb_is_red(r) (!rb_color(r))
402 #define rb_is_black(r) rb_color(r)
403 #define rb_set_red(r) do { (r)->rb_parent_color &= ~1; } while (0)
404 #define rb_set_black(r) do { (r)->rb_parent_color |= 1; } while (0)
406 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
408 rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
410 static inline void rb_set_color(struct rb_node *rb, int color)
412 rb->rb_parent_color = (rb->rb_parent_color & ~1) | color;
415 #define RB_ROOT ((struct rb_root) { NULL, })
416 #define rb_entry(ptr, type, member) container_of(ptr, type, member)
418 #define RB_EMPTY_ROOT(root) ((root)->rb_node == NULL)
419 #define RB_EMPTY_NODE(node) (rb_parent(node) == node)
420 #define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
422 static inline void rb_init_node(struct rb_node *rb)
424 rb->rb_parent_color = 0;
430 extern void rb_insert_color(struct rb_node *, struct rb_root *);
431 extern void rb_erase(struct rb_node *, struct rb_root *);
433 /* Find logical next and previous nodes in a tree */
434 extern struct rb_node *rb_next(const struct rb_node *);
435 extern struct rb_node *rb_prev(const struct rb_node *);
436 extern struct rb_node *rb_first(const struct rb_root *);
437 extern struct rb_node *rb_last(const struct rb_root *);
438 static inline void rb_link_node(struct rb_node *node, struct rb_node *parent,
439 struct rb_node **rb_link)
441 node->rb_parent_color = (unsigned long)parent;
442 node->rb_left = node->rb_right = NULL;
447 /***************************************************************************
449 * End of Linux kernel red black tree emulation.
451 ***************************************************************************/
453 typedef ssize_t (*read_actor_t)();
457 # define IFTODT(type) (((type) & S_IFMT) >> IFSHIFT)
458 # define DTTOIF(dirtype) ((dirtype) << IFSHIFT)
462 #define ERESTARTSYS ERESTART