Whamcloud - gitweb
LU-6245 libcfs: remove libcfsutil.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  * Copyright (c) 2012, 2013, Intel Corporation.
24  */
25
26 #ifndef __LIBCFS_USER_MEM_H__
27 #define __LIBCFS_USER_MEM_H__
28
29 #ifndef __LIBCFS_LIBCFS_H__
30 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
31 #endif
32
33 #ifdef __KERNEL__
34 #error "This is only for user space."
35 #endif
36
37
38 /* XXX
39  * for this moment, liblusre will not rely OST for non-page-aligned write
40  */
41 #define LIBLUSTRE_HANDLE_UNALIGNED_PAGE
42
43 struct page {
44         void                    *addr;
45         unsigned long           index;
46         struct list_head        list;
47         unsigned long           private;
48
49         /* internally used by liblustre file i/o */
50         int     _offset;
51         int     _count;
52 #ifdef LIBLUSTRE_HANDLE_UNALIGNED_PAGE
53         int     _managed;
54 #endif
55         struct list_head _node;
56 };
57
58
59 /* 4K */
60 #define PAGE_CACHE_SHIFT 12
61 #define PAGE_CACHE_SIZE (1UL << PAGE_CACHE_SHIFT)
62 #define CFS_PAGE_MASK (~((__u64)PAGE_CACHE_SIZE-1))
63
64 struct page *alloc_page(unsigned int flags);
65 void __free_page(struct page *pg);
66 void *page_address(struct page *pg);
67 void *kmap(struct page *pg);
68 void kunmap(struct page *pg);
69
70 #define get_page(p)                     __I_should_not_be_called__(at_all)
71 #define page_count(p)           __I_should_not_be_called__(at_all)
72 #define page_index(p)               ((p)->index)
73 #define page_cache_get(page) do { } while (0)
74 #define page_cache_release(page) do { } while (0)
75
76 #define inc_zone_page_state(page, state) do {} while (0)
77 #define dec_zone_page_state(page, state) do {} while (0)
78
79 /*
80  * Memory allocator
81  * Inline function, so utils can use them without linking of libcfs
82  */
83
84 /*
85  * Universal memory allocator API
86  */
87 enum cfs_alloc_flags {
88         /* allocation is not allowed to block */
89         GFP_ATOMIC = 0x1,
90         /* allocation is allowed to block */
91         __GFP_WAIT   = 0x2,
92         /* allocation should return zeroed memory */
93         __GFP_ZERO   = 0x4,
94         /* allocation is allowed to call file-system code to free/clean
95          * memory */
96         __GFP_FS     = 0x8,
97         /* allocation is allowed to do io to free/clean memory */
98         __GFP_IO     = 0x10,
99         /* don't report allocation failure to the console */
100         __GFP_NOWARN = 0x20,
101         /* standard allocator flag combination */
102         GFP_IOFS    = __GFP_FS | __GFP_IO,
103         GFP_USER   = __GFP_WAIT | __GFP_FS | __GFP_IO,
104         GFP_NOFS   = __GFP_WAIT | __GFP_IO,
105         GFP_KERNEL = __GFP_WAIT | __GFP_IO | __GFP_FS,
106 };
107
108 /* flags for cfs_page_alloc() in addition to enum cfs_alloc_flags */
109 enum cfs_alloc_page_flags {
110         /* allow to return page beyond KVM. It has to be mapped into KVM by
111          * kmap() and unmapped with kunmap(). */
112         __GFP_HIGHMEM  = 0x40,
113         GFP_HIGHUSER = __GFP_WAIT | __GFP_FS | __GFP_IO |
114                              __GFP_HIGHMEM,
115 };
116
117 static inline void *kmalloc(size_t nr_bytes, u_int32_t flags)
118 {
119         void *result;
120
121         result = malloc(nr_bytes);
122         if (result != NULL && (flags & __GFP_ZERO))
123                 memset(result, 0, nr_bytes);
124         return result;
125 }
126
127 #define kfree(addr)  free(addr)
128 #define vmalloc(nr_bytes) kmalloc(nr_bytes, 0)
129 #define vfree(addr) free(addr)
130
131 #define ALLOC_ATOMIC_TRY   (0)
132 /*
133  * SLAB allocator
134  */
135 struct kmem_cache {
136          int size;
137 };
138
139 #define SLAB_HWCACHE_ALIGN 0
140 #define SLAB_DESTROY_BY_RCU 0
141 #define SLAB_KERNEL 0
142 #define SLAB_NOFS 0
143
144 #define memory_pressure_get() (0)
145 #define memory_pressure_set() do {} while (0)
146 #define memory_pressure_clr() do {} while (0)
147
148 struct kmem_cache *kmem_cache_create(const char *, size_t, size_t,
149                                      unsigned long, void *);
150 void kmem_cache_destroy(struct kmem_cache *c);
151 void *kmem_cache_alloc(struct kmem_cache *c, int gfp);
152 void kmem_cache_free(struct kmem_cache *c, void *addr);
153 int kmem_is_in_cache(const void *addr, const struct kmem_cache *kmem);
154
155 /*
156  * NUMA allocators
157  */
158 #define cfs_cpt_malloc(cptab, cpt, bytes, flags)        \
159         kmalloc(bytes, flags)
160 #define cfs_cpt_vmalloc(cptab, cpt, bytes)              \
161         kmalloc(bytes)
162 #define cfs_page_cpt_alloc(cptab, cpt, mask)            \
163         alloc_page(mask)
164 #define cfs_mem_cache_cpt_alloc(cache, cptab, cpt, gfp) \
165         kmem_cache_alloc(cache, gfp)
166
167 #define smp_rmb()       do {} while (0)
168
169 /*
170  * Copy to/from user
171  */
172 static inline int copy_from_user(void *a, void *b, int c)
173 {
174         memcpy(a, b, c);
175         return 0;
176 }
177
178 static inline int copy_to_user(void *a, void *b, int c)
179 {
180         memcpy(a,b,c);
181         return 0;
182 }
183
184 #endif