Whamcloud - gitweb
libcfs: posix: 0. include stddef to get offsetoff() and other standard stuff; 1....
[fs/lustre-release.git] / libcfs / include / libcfs / posix / libcfs.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 <sys/errno.h>
47 #include <string.h>
48 #include <stdarg.h>
49 #include <stddef.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <sys/mman.h>
53 #include <sys/stat.h>
54 #include <unistd.h>
55 #include <fcntl.h>
56 #include <limits.h>
57 #include <assert.h>
58 #include <sys/signal.h>
59 #include <signal.h>
60 #include <sys/time.h>
61 #include <time.h>
62
63 #ifdef HAVE_LIBPTHREAD
64 #include <pthread.h>
65 #endif
66
67 #if defined(HAVE_SYS_TYPES_H)
68 #include <sys/types.h>
69 #endif
70
71 #ifdef HAVE_SYS_USER_H
72 # include <sys/user.h>
73 #endif
74
75 #ifdef HAVE_SYS_VFS_H
76 # include <sys/vfs.h>
77 #endif
78
79 #ifdef HAVE_STDINT_H
80 # include <stdint.h>
81 #endif
82
83 #include <libcfs/list.h>
84 #include <libcfs/posix/posix-types.h>
85 #include <libcfs/user-time.h>
86 #include <libcfs/user-prim.h>
87 #include <libcfs/user-mem.h>
88 #include <libcfs/user-lock.h>
89 #include <libcfs/user-tcpip.h>
90 #include <libcfs/posix/posix-wordsize.h>
91 #include <libcfs/user-bitops.h>
92
93 # define do_gettimeofday(tv) gettimeofday(tv, NULL);
94 typedef unsigned long long cycles_t;
95
96 #define IS_ERR(a) ((unsigned long)(a) > (unsigned long)-1000L)
97 #define PTR_ERR(a) ((long)(a))
98 #define ERR_PTR(a) ((void*)((long)(a)))
99
100 /* this goes in posix-fs.h */
101 #include <sys/mount.h>
102
103 #ifdef __linux__
104 #include <mntent.h>
105 #endif
106
107 typedef struct file cfs_file_t;
108 typedef struct dentry cfs_dentry_t;
109 #ifdef __linux__
110 typedef struct dirent64 cfs_dirent_t;
111 #endif
112
113 #ifdef __linux__
114 /* Userpace byte flipping */
115 # include <endian.h>
116 # include <byteswap.h>
117 # define __swab16(x) bswap_16(x)
118 # define __swab32(x) bswap_32(x)
119 # define __swab64(x) bswap_64(x)
120 # define __swab16s(x) do {*(x) = bswap_16(*(x));} while (0)
121 # define __swab32s(x) do {*(x) = bswap_32(*(x));} while (0)
122 # define __swab64s(x) do {*(x) = bswap_64(*(x));} while (0)
123 # if __BYTE_ORDER == __LITTLE_ENDIAN
124 #  define le16_to_cpu(x) (x)
125 #  define cpu_to_le16(x) (x)
126 #  define le32_to_cpu(x) (x)
127 #  define cpu_to_le32(x) (x)
128 #  define le64_to_cpu(x) (x)
129 #  define cpu_to_le64(x) (x)
130
131 #  define be16_to_cpu(x) bswap_16(x)
132 #  define cpu_to_be16(x) bswap_16(x)
133 #  define be32_to_cpu(x) bswap_32(x)
134 #  define cpu_to_be32(x) bswap_32(x)
135 #  define be64_to_cpu(x) bswap_64(x)
136 #  define cpu_to_be64(x) bswap_64(x)
137 # else
138 #  if __BYTE_ORDER == __BIG_ENDIAN
139 #   define le16_to_cpu(x) bswap_16(x)
140 #   define cpu_to_le16(x) bswap_16(x)
141 #   define le32_to_cpu(x) bswap_32(x)
142 #   define cpu_to_le32(x) bswap_32(x)
143 #   define le64_to_cpu(x) bswap_64(x)
144 #   define cpu_to_le64(x) bswap_64(x)
145
146 #   define be16_to_cpu(x) (x)
147 #   define cpu_to_be16(x) (x)
148 #   define be32_to_cpu(x) (x)
149 #   define cpu_to_be32(x) (x)
150 #   define be64_to_cpu(x) (x)
151 #   define cpu_to_be64(x) (x)
152
153 #  else
154 #   error "Unknown byte order"
155 #  endif /* __BIG_ENDIAN */
156 # endif /* __LITTLE_ENDIAN */
157 #elif __APPLE__
158 #define __cpu_to_le64(x)                        OSSwapHostToLittleInt64(x)
159 #define __cpu_to_le32(x)                        OSSwapHostToLittleInt32(x)
160 #define __cpu_to_le16(x)                        OSSwapHostToLittleInt16(x)
161
162 #define __le16_to_cpu(x)                        OSSwapLittleToHostInt16(x)
163 #define __le32_to_cpu(x)                        OSSwapLittleToHostInt32(x)
164 #define __le64_to_cpu(x)                        OSSwapLittleToHostInt64(x)
165
166 #define cpu_to_le64(x)                          __cpu_to_le64(x)
167 #define cpu_to_le32(x)                          __cpu_to_le32(x)
168 #define cpu_to_le16(x)                          __cpu_to_le16(x)
169
170 #define le64_to_cpu(x)                          __le64_to_cpu(x)
171 #define le32_to_cpu(x)                          __le32_to_cpu(x)
172 #define le16_to_cpu(x)                          __le16_to_cpu(x)
173
174 #define __swab16(x)                             OSSwapInt16(x)
175 #define __swab32(x)                             OSSwapInt32(x)
176 #define __swab64(x)                             OSSwapInt64(x)
177 #define __swab16s(x)                            do { *(x) = __swab16(*(x)); } while (0)
178 #define __swab32s(x)                            do { *(x) = __swab32(*(x)); } while (0)
179 #define __swab64s(x)                            do { *(x) = __swab64(*(x)); } while (0)
180 #endif
181
182
183 # ifndef THREAD_SIZE /* x86_64 linux has THREAD_SIZE in userspace */
184 #  define THREAD_SIZE 8192
185 # endif
186
187 #define LUSTRE_TRACE_SIZE (THREAD_SIZE >> 5)
188
189 #define CHECK_STACK() do { } while(0)
190 #define CDEBUG_STACK() (0L)
191
192 /* initial pid  */
193 #define LUSTRE_LNET_PID          12345
194
195 #define ENTRY_NESTING_SUPPORT (1)
196 #define ENTRY_NESTING   do {;} while (0)
197 #define EXIT_NESTING   do {;} while (0)
198 #define __current_nesting_level() (0)
199
200 /**
201  * Platform specific declarations for cfs_curproc API (libcfs/curproc.h)
202  *
203  * Implementation is in linux-curproc.c
204  */
205 #define CFS_CURPROC_COMM_MAX (sizeof ((struct task_struct *)0)->comm)
206
207 typedef __u32 cfs_kernel_cap_t;
208
209 /**
210  * Module support (probably shouldn't be used in generic code?)
211  */
212 struct module {
213         int count;
214         char *name;
215 };
216
217 static inline void MODULE_AUTHOR(char *name)
218 {
219         printf("%s\n", name);
220 }
221 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
222 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
223
224 #define THIS_MODULE NULL
225 #define __init
226 #define __exit
227
228 static inline int request_module(char *name)
229 {
230         return (-EINVAL);
231 }
232
233 static inline void __module_get(struct module *module)
234 {
235 }
236
237 static inline int try_module_get(struct module *module)
238 {
239         return 1;
240 }
241
242 static inline void module_put(struct module *module)
243 {
244 }
245
246
247 static inline int module_refcount(struct module *m)
248 {
249         return 1;
250 }
251
252 /***************************************************************************
253  *
254  * Linux kernel slab shrinker emulation. Currently used only in lu_object.c
255  *
256  ***************************************************************************/
257
258 struct shrinker {
259         ;
260 };
261
262 #define DEFAULT_SEEKS (0)
263
264 typedef int (*shrinker_t)(int, unsigned int);
265
266 static inline struct shrinker *set_shrinker(int seeks, shrinker_t shrinkert)
267 {
268         return NULL;
269 }
270
271 static inline void remove_shrinker(struct shrinker *shrinker)
272 {
273 }
274
275 /***************************************************************************
276  *
277  * Linux kernel radix tree emulation.
278  *
279  * XXX this stub-implementation assumes that elements stored in a radix tree
280  *     are struct page's and nothing else. Proper implementation will be
281  *     committed soon.
282  *
283  ***************************************************************************/
284
285 struct radix_tree_root {
286         struct list_head        *rnode;
287 };
288
289 #define RADIX_TREE_INIT(mask)   {               \
290         .rnode = NULL,                          \
291 }
292
293 #define RADIX_TREE(name, mask) \
294         struct radix_tree_root name = RADIX_TREE_INIT(mask)
295
296 #define INIT_RADIX_TREE(root, mask)             \
297 do {                                            \
298         (root)->rnode = NULL;                   \
299 } while (0)
300
301 static inline int radix_tree_insert(struct radix_tree_root *root,
302                                     unsigned long idx, struct page *page)
303 {
304         if (root->rnode == NULL)
305                 root->rnode = &page->_node;
306         else
307                 list_add_tail(&page->_node, root->rnode);
308         return 0;
309 }
310
311 static inline void *radix_tree_lookup(struct radix_tree_root *root,
312                                       unsigned long idx)
313 {
314         struct page *p;
315
316         if (root->rnode == NULL)
317                 return NULL;
318
319         p = list_entry(root->rnode, struct page, _node);
320         if (p->index == idx)
321                 return p;
322
323         list_for_each_entry(p, root->rnode, _node)
324                 if (p->index == idx)
325                         return p;
326
327         return NULL;
328 }
329
330 static inline void *radix_tree_delete(struct radix_tree_root *root,
331                                       unsigned long idx)
332 {
333         struct page *p = radix_tree_lookup(root, idx);
334
335         if (p == NULL)
336                 return NULL;
337         if (list_empty(root->rnode))
338                 root->rnode = NULL;
339         else if (root->rnode == &p->_node)
340                 root->rnode = p->_node.next;
341         list_del_init(&p->_node);
342         return p;
343 }
344
345 static inline unsigned int
346 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
347                        unsigned long first_index, unsigned int max_items)
348 {
349         int i;
350         int j = 0;
351
352         for (i = 0; i < max_items; i++, first_index++) {
353                 results[j++] = radix_tree_lookup(root, first_index);
354                 if (results[j - 1] == NULL)
355                         --j;
356         }
357
358         return j;
359 }
360
361 static inline int radix_tree_preload(int gfp_mask)
362 {
363         return 0;
364 }
365
366 void radix_tree_init(void);
367
368 static inline void radix_tree_preload_end(void)
369 {
370 }
371
372 typedef ssize_t (*read_actor_t)();
373
374 #endif