Whamcloud - gitweb
94f891179a8d4361c5c12600db33a9858d9ee276
[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 typedef struct page {
41         void   *addr;
42         unsigned long index;
43         cfs_list_t 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         cfs_list_t _node;
53 } cfs_page_t;
54
55
56 /* 4K */
57 #define CFS_PAGE_SHIFT 12
58 #define CFS_PAGE_SIZE (1UL << CFS_PAGE_SHIFT)
59 #define CFS_PAGE_MASK (~((__u64)CFS_PAGE_SIZE-1))
60
61 cfs_page_t *cfs_alloc_page(unsigned int flags);
62 void cfs_free_page(cfs_page_t *pg);
63 void *cfs_page_address(cfs_page_t *pg);
64 void *cfs_kmap(cfs_page_t *pg);
65 void cfs_kunmap(cfs_page_t *pg);
66
67 #define cfs_get_page(p)                 __I_should_not_be_called__(at_all)
68 #define cfs_page_count(p)               __I_should_not_be_called__(at_all)
69 #define cfs_page_index(p)               ((p)->index)
70 #define cfs_page_pin(page) do {} while (0)
71 #define cfs_page_unpin(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 #define __ALLOC_ZERO    (1 << 2)
81 static inline void *cfs_alloc(size_t nr_bytes, u_int32_t flags)
82 {
83         void *result;
84
85         result = malloc(nr_bytes);
86         if (result != NULL && (flags & __ALLOC_ZERO))
87                 memset(result, 0, nr_bytes);
88         return result;
89 }
90
91 #define cfs_free(addr)  free(addr)
92 #define cfs_alloc_large(nr_bytes) cfs_alloc(nr_bytes, 0)
93 #define cfs_free_large(addr) cfs_free(addr)
94
95 #define CFS_ALLOC_ATOMIC_TRY   (0)
96 /*
97  * SLAB allocator
98  */
99 typedef struct {
100          int size;
101 } cfs_mem_cache_t;
102
103 #define CFS_SLAB_HWCACHE_ALIGN 0
104 #define SLAB_DESTROY_BY_RCU 0
105 #define CFS_SLAB_KERNEL 0
106 #define CFS_SLAB_NOFS 0
107
108 cfs_mem_cache_t *
109 cfs_mem_cache_create(const char *, size_t, size_t, unsigned long);
110 int cfs_mem_cache_destroy(cfs_mem_cache_t *c);
111 void *cfs_mem_cache_alloc(cfs_mem_cache_t *c, int gfp);
112 void cfs_mem_cache_free(cfs_mem_cache_t *c, void *addr);
113 int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem);
114
115 /*
116  * NUMA allocators
117  */
118 #define cfs_cpt_malloc(cptab, cpt, bytes, flags)        \
119         cfs_alloc(bytes, flags)
120 #define cfs_cpt_vmalloc(cptab, cpt, bytes)              \
121         cfs_alloc(bytes)
122 #define cfs_page_cpt_alloc(cptab, cpt, mask)            \
123         cfs_alloc_page(mask)
124 #define cfs_mem_cache_cpt_alloc(cache, cptab, cpt, gfp) \
125         cfs_mem_cache_alloc(cache, gfp)
126
127 #define smp_rmb()       do {} while (0)
128
129 /*
130  * Copy to/from user
131  */
132 static inline int cfs_copy_from_user(void *a,void *b, int c)
133 {
134         memcpy(a,b,c);
135         return 0;
136 }
137
138 static inline int cfs_copy_to_user(void *a,void *b, int c)
139 {
140         memcpy(a,b,c);
141         return 0;
142 }
143
144 #endif