Whamcloud - gitweb
b=19720
[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 <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
70 #ifdef HAVE_NETDB_H
71 #include <netdb.h>
72 #endif
73
74 #ifdef HAVE_UNISTD_H
75 #include <unistd.h>
76 #endif
77
78 #ifdef HAVE_LIBPTHREAD
79 #include <pthread.h>
80 #endif
81
82 #if defined(HAVE_SYS_TYPES_H)
83 #include <sys/types.h>
84 #endif
85
86 #ifdef HAVE_SYS_USER_H
87 # include <sys/user.h>
88 #endif
89
90 #ifdef HAVE_SYS_VFS_H
91 # include <sys/vfs.h>
92 #endif
93
94 #ifdef HAVE_STDINT_H
95 # include <stdint.h>
96 #endif
97
98 #include <libcfs/list.h>
99 #include <libcfs/posix/posix-types.h>
100 #include <libcfs/user-time.h>
101 #include <libcfs/user-prim.h>
102 #include <libcfs/user-mem.h>
103 #include <libcfs/user-lock.h>
104 #include <libcfs/user-tcpip.h>
105 #include <libcfs/posix/posix-wordsize.h>
106 #include <libcfs/user-bitops.h>
107 #include <libcfs/posix/posix-kernelcomm.h>
108
109 # define do_gettimeofday(tv) gettimeofday(tv, NULL);
110 typedef unsigned long long cycles_t;
111
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)))
115
116 /* this goes in posix-fs.h */
117 #include <sys/mount.h>
118
119 #ifdef __linux__
120 #include <mntent.h>
121 #endif
122
123 typedef struct file cfs_file_t;
124 typedef struct dentry cfs_dentry_t;
125 #ifdef __linux__
126 typedef struct dirent64 cfs_dirent_t;
127 #endif
128
129 #ifdef __linux__
130 /* Userpace byte flipping */
131 # include <endian.h>
132 # include <byteswap.h>
133 # define __swab16(x) bswap_16(x)
134 # define __swab32(x) bswap_32(x)
135 # define __swab64(x) bswap_64(x)
136 # define __swab16s(x) do {*(x) = bswap_16(*(x));} while (0)
137 # define __swab32s(x) do {*(x) = bswap_32(*(x));} while (0)
138 # define __swab64s(x) do {*(x) = bswap_64(*(x));} while (0)
139 # if __BYTE_ORDER == __LITTLE_ENDIAN
140 #  define le16_to_cpu(x) (x)
141 #  define cpu_to_le16(x) (x)
142 #  define le32_to_cpu(x) (x)
143 #  define cpu_to_le32(x) (x)
144 #  define le64_to_cpu(x) (x)
145 #  define cpu_to_le64(x) (x)
146
147 #  define be16_to_cpu(x) bswap_16(x)
148 #  define cpu_to_be16(x) bswap_16(x)
149 #  define be32_to_cpu(x) bswap_32(x)
150 #  define cpu_to_be32(x) bswap_32(x)
151 #  define be64_to_cpu(x) bswap_64(x)
152 #  define cpu_to_be64(x) bswap_64(x)
153 # else
154 #  if __BYTE_ORDER == __BIG_ENDIAN
155 #   define le16_to_cpu(x) bswap_16(x)
156 #   define cpu_to_le16(x) bswap_16(x)
157 #   define le32_to_cpu(x) bswap_32(x)
158 #   define cpu_to_le32(x) bswap_32(x)
159 #   define le64_to_cpu(x) bswap_64(x)
160 #   define cpu_to_le64(x) bswap_64(x)
161
162 #   define be16_to_cpu(x) (x)
163 #   define cpu_to_be16(x) (x)
164 #   define be32_to_cpu(x) (x)
165 #   define cpu_to_be32(x) (x)
166 #   define be64_to_cpu(x) (x)
167 #   define cpu_to_be64(x) (x)
168
169 #  else
170 #   error "Unknown byte order"
171 #  endif /* __BIG_ENDIAN */
172 # endif /* __LITTLE_ENDIAN */
173 #elif __APPLE__
174 #define __cpu_to_le64(x)                        OSSwapHostToLittleInt64(x)
175 #define __cpu_to_le32(x)                        OSSwapHostToLittleInt32(x)
176 #define __cpu_to_le16(x)                        OSSwapHostToLittleInt16(x)
177
178 #define __le16_to_cpu(x)                        OSSwapLittleToHostInt16(x)
179 #define __le32_to_cpu(x)                        OSSwapLittleToHostInt32(x)
180 #define __le64_to_cpu(x)                        OSSwapLittleToHostInt64(x)
181
182 #define cpu_to_le64(x)                          __cpu_to_le64(x)
183 #define cpu_to_le32(x)                          __cpu_to_le32(x)
184 #define cpu_to_le16(x)                          __cpu_to_le16(x)
185
186 #define le64_to_cpu(x)                          __le64_to_cpu(x)
187 #define le32_to_cpu(x)                          __le32_to_cpu(x)
188 #define le16_to_cpu(x)                          __le16_to_cpu(x)
189
190 #define __swab16(x)                             OSSwapInt16(x)
191 #define __swab32(x)                             OSSwapInt32(x)
192 #define __swab64(x)                             OSSwapInt64(x)
193 #define __swab16s(x)                            do { *(x) = __swab16(*(x)); } while (0)
194 #define __swab32s(x)                            do { *(x) = __swab32(*(x)); } while (0)
195 #define __swab64s(x)                            do { *(x) = __swab64(*(x)); } while (0)
196 #endif
197
198
199 # ifndef THREAD_SIZE /* x86_64 linux has THREAD_SIZE in userspace */
200 #  define THREAD_SIZE 8192
201 # endif
202
203 #define LUSTRE_TRACE_SIZE (THREAD_SIZE >> 5)
204
205 #define CHECK_STACK() do { } while(0)
206 #define CDEBUG_STACK() (0L)
207
208 /* initial pid  */
209 #define LUSTRE_LNET_PID          12345
210
211 #define ENTRY_NESTING_SUPPORT (1)
212 #define ENTRY_NESTING   do {;} while (0)
213 #define EXIT_NESTING   do {;} while (0)
214 #define __current_nesting_level() (0)
215
216 /**
217  * Platform specific declarations for cfs_curproc API (libcfs/curproc.h)
218  *
219  * Implementation is in linux-curproc.c
220  */
221 #define CFS_CURPROC_COMM_MAX (sizeof ((struct task_struct *)0)->comm)
222
223 typedef __u32 cfs_kernel_cap_t;
224
225 /**
226  * Module support (probably shouldn't be used in generic code?)
227  */
228 struct module {
229         int count;
230         char *name;
231 };
232
233 static inline void MODULE_AUTHOR(char *name)
234 {
235         printf("%s\n", name);
236 }
237 #define MODULE_DESCRIPTION(name) MODULE_AUTHOR(name)
238 #define MODULE_LICENSE(name) MODULE_AUTHOR(name)
239
240 #define THIS_MODULE (void *)0x11111
241 #define __init
242 #define __exit
243
244 static inline int request_module(const char *name, ...)
245 {
246         return (-EINVAL);
247 }
248
249 static inline void __module_get(struct module *module)
250 {
251 }
252
253 static inline int try_module_get(struct module *module)
254 {
255         return 1;
256 }
257
258 static inline void module_put(struct module *module)
259 {
260 }
261
262
263 static inline int module_refcount(struct module *m)
264 {
265         return 1;
266 }
267
268 /***************************************************************************
269  *
270  * Linux kernel slab shrinker emulation. Currently used only in lu_object.c
271  *
272  ***************************************************************************/
273
274 struct shrinker {
275         ;
276 };
277
278 #define DEFAULT_SEEKS (0)
279
280 typedef int (*shrinker_t)(int, unsigned int);
281
282 static inline struct shrinker *set_shrinker(int seeks, shrinker_t shrinkert)
283 {
284         return (struct shrinker *)0xdeadbea1; // Cannot return NULL here
285 }
286
287 static inline void remove_shrinker(struct shrinker *shrinker)
288 {
289 }
290
291 /***************************************************************************
292  *
293  * Linux kernel radix tree emulation.
294  *
295  * XXX this stub-implementation assumes that elements stored in a radix tree
296  *     are struct page's and nothing else. Proper implementation will be
297  *     committed soon.
298  *
299  ***************************************************************************/
300
301 struct radix_tree_root {
302         struct list_head list;
303         void *rnode;
304 };
305
306 struct radix_tree_node {
307         struct list_head _node;
308         unsigned long index;
309         void *item;
310 };
311
312 #define RADIX_TREE_INIT(mask)   {               \
313                 NOT_IMPLEMENTED                 \
314 }
315
316 #define RADIX_TREE(name, mask) \
317         struct radix_tree_root name = RADIX_TREE_INIT(mask)
318
319
320 #define INIT_RADIX_TREE(root, mask)                                     \
321 do {                                                                    \
322         CFS_INIT_LIST_HEAD(&((struct radix_tree_root *)root)->list);    \
323         ((struct radix_tree_root *)root)->rnode = NULL;                 \
324 } while (0)
325
326 static inline int radix_tree_insert(struct radix_tree_root *root,
327                         unsigned long idx, void *item)
328 {
329         struct radix_tree_node *node;
330         node = malloc(sizeof(*node));
331         if (!node)
332                 return -ENOMEM;
333
334         CFS_INIT_LIST_HEAD(&node->_node);
335         node->index = idx;
336         node->item = item;
337         list_add_tail(&node->_node, &root->list);
338         root->rnode = (void *)1001;
339         return 0;
340 }
341
342 static inline struct radix_tree_node *radix_tree_lookup0(struct radix_tree_root *root,
343                                       unsigned long idx)
344 {
345         struct radix_tree_node *node;
346
347         if (list_empty(&root->list))
348                 return NULL;
349
350         cfs_list_for_each_entry_typed(node, &root->list,
351                                       struct radix_tree_node, _node)
352                 if (node->index == idx)
353                         return node;
354
355         return NULL;
356 }
357
358 static inline void *radix_tree_lookup(struct radix_tree_root *root,
359                                       unsigned long idx)
360 {
361         struct radix_tree_node *node = radix_tree_lookup0(root, idx);
362
363         if (node)
364                 return node->item;
365         return node;
366 }
367
368 static inline void *radix_tree_delete(struct radix_tree_root *root,
369                                       unsigned long idx)
370 {
371         struct radix_tree_node *p = radix_tree_lookup0(root, idx);
372         void *item;
373
374         if (p == NULL)
375                 return NULL;
376
377         list_del_init(&p->_node);
378         item = p->item;
379         free(p);
380         if (list_empty(&root->list))
381                 root->rnode = NULL;
382
383         return item;
384 }
385
386 static inline unsigned int
387 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
388                        unsigned long first_index, unsigned int max_items)
389 {
390         int i;
391         int j = 0;
392
393         for (i = 0; i < max_items; i++, first_index++) {
394                 results[j++] = radix_tree_lookup(root, first_index);
395                 if (results[j - 1] == NULL)
396                         --j;
397         }
398
399         return j;
400 }
401
402 static inline int radix_tree_preload(int gfp_mask)
403 {
404         return 0;
405 }
406
407 void radix_tree_init(void);
408
409 static inline void radix_tree_preload_end(void)
410 {
411 }
412
413 typedef ssize_t (*read_actor_t)();
414
415 #define CFS_IFSHIFT 12
416
417 #define CFS_IFTODT(type)           (((type) & S_IFMT) >> CFS_IFSHIFT)
418 #define CFS_DTTOIF(dirtype)        ((dirtype) << CFS_IFSHIFT)
419
420 #endif