Whamcloud - gitweb
LU-1756 kernel: cleanup lustre_compat25.h
[fs/lustre-release.git] / libcfs / include / libcfs / user-mem.h
1 #ifndef __LIBCFS_USER_MEM_H__
2 #define __LIBCFS_USER_MEM_H__
3
4 #ifndef __LIBCFS_LIBCFS_H__
5 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
6 #endif
7
8 #ifdef __KERNEL__
9 #error "This is only for user space."
10 #endif
11
12
13 /* XXX
14  * for this moment, liblusre will not rely OST for non-page-aligned write
15  */
16 #define LIBLUSTRE_HANDLE_UNALIGNED_PAGE
17
18 typedef struct page {
19         void   *addr;
20         unsigned long index;
21         cfs_list_t list;
22         unsigned long private;
23
24         /* internally used by liblustre file i/o */
25         int     _offset;
26         int     _count;
27 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
28         int     _managed;
29 #endif
30         cfs_list_t _node;
31 } cfs_page_t;
32
33
34 /* 4K */
35 #define CFS_PAGE_SHIFT 12
36 #define CFS_PAGE_SIZE (1UL << CFS_PAGE_SHIFT)
37 #define CFS_PAGE_MASK (~((__u64)CFS_PAGE_SIZE-1))
38
39 cfs_page_t *cfs_alloc_page(unsigned int flags);
40 void cfs_free_page(cfs_page_t *pg);
41 void *cfs_page_address(cfs_page_t *pg);
42 void *cfs_kmap(cfs_page_t *pg);
43 void cfs_kunmap(cfs_page_t *pg);
44
45 #define cfs_get_page(p)                 __I_should_not_be_called__(at_all)
46 #define cfs_page_count(p)               __I_should_not_be_called__(at_all)
47 #define cfs_page_index(p)               ((p)->index)
48 #define cfs_page_pin(page) do {} while (0)
49 #define cfs_page_unpin(page) do {} while (0)
50
51 /*
52  * Memory allocator
53  * Inline function, so utils can use them without linking of libcfs
54  */
55 #define __ALLOC_ZERO    (1 << 2)
56 static inline void *cfs_alloc(size_t nr_bytes, u_int32_t flags)
57 {
58         void *result;
59
60         result = malloc(nr_bytes);
61         if (result != NULL && (flags & __ALLOC_ZERO))
62                 memset(result, 0, nr_bytes);
63         return result;
64 }
65
66 #define cfs_free(addr)  free(addr)
67 #define cfs_alloc_large(nr_bytes) cfs_alloc(nr_bytes, 0)
68 #define cfs_free_large(addr) cfs_free(addr)
69
70 #define CFS_ALLOC_ATOMIC_TRY   (0)
71 /*
72  * SLAB allocator
73  */
74 typedef struct {
75          int size;
76 } cfs_mem_cache_t;
77
78 #define CFS_SLAB_HWCACHE_ALIGN 0
79 #define SLAB_DESTROY_BY_RCU 0
80 #define CFS_SLAB_KERNEL 0
81 #define CFS_SLAB_NOFS 0
82
83 cfs_mem_cache_t *
84 cfs_mem_cache_create(const char *, size_t, size_t, unsigned long);
85 int cfs_mem_cache_destroy(cfs_mem_cache_t *c);
86 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp);
87 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr);
88 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem);
89
90 /*
91  * NUMA allocators
92  */
93 #define cfs_cpt_malloc(cptab, cpt, bytes, flags)        \
94         cfs_alloc(bytes, flags)
95 #define cfs_cpt_vmalloc(cptab, cpt, bytes)              \
96         cfs_alloc(bytes)
97 #define cfs_page_cpt_alloc(cptab, cpt, mask)            \
98         cfs_alloc_page(mask)
99 #define cfs_mem_cache_cpt_alloc(cache, cptab, cpt, gfp) \
100         cfs_mem_cache_alloc(cache, gfp)
101
102 /*
103  * Copy to/from user
104  */
105 static inline int cfs_copy_from_user(void *a,void *b, int c)
106 {
107         memcpy(a,b,c);
108         return 0;
109 }
110
111 static inline int cfs_copy_to_user(void *a,void *b, int c)
112 {
113         memcpy(a,b,c);
114         return 0;
115 }
116
117 #endif