Whamcloud - gitweb
LU-5577 libcfs: fix warnings in libcfs/curproc.h
[fs/lustre-release.git] / libcfs / include / libcfs / user-mem.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22
23 #ifndef __LIBCFS_USER_MEM_H__
24 #define __LIBCFS_USER_MEM_H__
25
26 #ifndef __LIBCFS_LIBCFS_H__
27 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
28 #endif
29
30 #ifdef __KERNEL__
31 #error "This is only for user space."
32 #endif
33
34
35 /* XXX
36  * for this moment, liblusre will not rely OST for non-page-aligned write
37  */
38 #define LIBLUSTRE_HANDLE_UNALIGNED_PAGE
39
40 struct page {
41         void                    *addr;
42         unsigned long           index;
43         struct list_head        list;
44         unsigned long           private;
45
46         /* internally used by liblustre file i/o */
47         int     _offset;
48         int     _count;
49 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
50         int     _managed;
51 #endif
52         struct list_head _node;
53 };
54
55
56 /* 4K */
57 #define PAGE_CACHE_SHIFT 12
58 #define PAGE_CACHE_SIZE (1UL << PAGE_CACHE_SHIFT)
59 #define CFS_PAGE_MASK (~((__u64)PAGE_CACHE_SIZE-1))
60
61 struct page *alloc_page(unsigned int flags);
62 void __free_page(struct page *pg);
63 void *page_address(struct page *pg);
64 void *kmap(struct page *pg);
65 void kunmap(struct page *pg);
66
67 #define get_page(p)                     __I_should_not_be_called__(at_all)
68 #define page_count(p)           __I_should_not_be_called__(at_all)
69 #define page_index(p)               ((p)->index)
70 #define page_cache_get(page) do { } while (0)
71 #define page_cache_release(page) do { } while (0)
72
73 #define inc_zone_page_state(page, state) do {} while (0)
74 #define dec_zone_page_state(page, state) do {} while (0)
75
76 /*
77  * Memory allocator
78  * Inline function, so utils can use them without linking of libcfs
79  */
80
81 /*
82  * Universal memory allocator API
83  */
84 enum cfs_alloc_flags {
85         /* allocation is not allowed to block */
86         GFP_ATOMIC = 0x1,
87         /* allocation is allowed to block */
88         __GFP_WAIT   = 0x2,
89         /* allocation should return zeroed memory */
90         __GFP_ZERO   = 0x4,
91         /* allocation is allowed to call file-system code to free/clean
92          * memory */
93         __GFP_FS     = 0x8,
94         /* allocation is allowed to do io to free/clean memory */
95         __GFP_IO     = 0x10,
96         /* don't report allocation failure to the console */
97         __GFP_NOWARN = 0x20,
98         /* standard allocator flag combination */
99         GFP_IOFS    = __GFP_FS | __GFP_IO,
100         GFP_USER   = __GFP_WAIT | __GFP_FS | __GFP_IO,
101         GFP_NOFS   = __GFP_WAIT | __GFP_IO,
102         GFP_KERNEL = __GFP_WAIT | __GFP_IO | __GFP_FS,
103 };
104
105 /* flags for cfs_page_alloc() in addition to enum cfs_alloc_flags */
106 enum cfs_alloc_page_flags {
107         /* allow to return page beyond KVM. It has to be mapped into KVM by
108          * kmap() and unmapped with kunmap(). */
109         __GFP_HIGHMEM  = 0x40,
110         GFP_HIGHUSER = __GFP_WAIT | __GFP_FS | __GFP_IO |
111                              __GFP_HIGHMEM,
112 };
113
114 static inline void *kmalloc(size_t nr_bytes, u_int32_t flags)
115 {
116         void *result;
117
118         result = malloc(nr_bytes);
119         if (result != NULL && (flags & __GFP_ZERO))
120                 memset(result, 0, nr_bytes);
121         return result;
122 }
123
124 #define kfree(addr)  free(addr)
125 #define vmalloc(nr_bytes) kmalloc(nr_bytes, 0)
126 #define vfree(addr) free(addr)
127
128 #define ALLOC_ATOMIC_TRY   (0)
129 /*
130  * SLAB allocator
131  */
132 struct kmem_cache {
133          int size;
134 };
135
136 #define SLAB_HWCACHE_ALIGN 0
137 #define SLAB_DESTROY_BY_RCU 0
138 #define SLAB_KERNEL 0
139 #define SLAB_NOFS 0
140
141 #define memory_pressure_get() (0)
142 #define memory_pressure_set() do {} while (0)
143 #define memory_pressure_clr() do {} while (0)
144
145 struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
146                                      unsigned long, void *);
147 void kmem_cache_destroy(struct kmem_cache *c);
148 void *kmem_cache_alloc(struct kmem_cache *c, int gfp);
149 void kmem_cache_free(struct kmem_cache *c, void *addr);
150 int kmem_is_in_cache(const void *addr, const struct kmem_cache *kmem);
151
152 /*
153  * NUMA allocators
154  */
155 #define cfs_cpt_malloc(cptab, cpt, bytes, flags)        \
156         kmalloc(bytes, flags)
157 #define cfs_cpt_vmalloc(cptab, cpt, bytes)              \
158         kmalloc(bytes)
159 #define cfs_page_cpt_alloc(cptab, cpt, mask)            \
160         alloc_page(mask)
161 #define cfs_mem_cache_cpt_alloc(cache, cptab, cpt, gfp) \
162         kmem_cache_alloc(cache, gfp)
163
164 #define smp_rmb()       do {} while (0)
165
166 /*
167  * Copy to/from user
168  */
169 static inline int copy_from_user(void *a, void *b, int c)
170 {
171         memcpy(a, b, c);
172         return 0;
173 }
174
175 static inline int copy_to_user(void *a, void *b, int c)
176 {
177         memcpy(a,b,c);
178         return 0;
179 }
180
181 #endif