Whamcloud - gitweb
05dde1544b86bd6f47c1da29fbb599c9237be8e0
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-mem.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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * libcfs/include/libcfs/linux/linux-mem.h
39  *
40  * Basic library routines.
41  */
42
43 #ifndef __LIBCFS_LINUX_CFS_MEM_H__
44 #define __LIBCFS_LINUX_CFS_MEM_H__
45
46 #ifndef __LIBCFS_LIBCFS_H__
47 #error Do not #include this file directly. #include <libcfs/libcfs.h> instead
48 #endif
49
50 #ifndef __KERNEL__
51 #error This include is only for kernel use.
52 #endif
53
54 #include <linux/mm.h>
55 #include <linux/vmalloc.h>
56 #include <linux/pagemap.h>
57 #include <linux/slab.h>
58 #ifdef HAVE_MM_INLINE
59 # include <linux/mm_inline.h>
60 #endif
61
62 typedef struct page                     cfs_page_t;
63 #define CFS_PAGE_SIZE                   PAGE_CACHE_SIZE
64 #define CFS_PAGE_SHIFT                  PAGE_CACHE_SHIFT
65 #define CFS_PAGE_MASK                   (~((__u64)CFS_PAGE_SIZE-1))
66
67 #define cfs_num_physpages               num_physpages
68
69 #define cfs_copy_from_user(to, from, n) copy_from_user(to, from, n)
70 #define cfs_copy_to_user(to, from, n)   copy_to_user(to, from, n)
71
72 static inline void *cfs_page_address(cfs_page_t *page)
73 {
74         /*
75          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
76          * from here: this will lead to infinite recursion.
77          */
78         return page_address(page);
79 }
80
81 static inline void *cfs_kmap(cfs_page_t *page)
82 {
83         return kmap(page);
84 }
85
86 static inline void cfs_kunmap(cfs_page_t *page)
87 {
88         kunmap(page);
89 }
90
91 static inline void cfs_get_page(cfs_page_t *page)
92 {
93         get_page(page);
94 }
95
96 static inline int cfs_page_count(cfs_page_t *page)
97 {
98         return page_count(page);
99 }
100
101 #define cfs_page_index(p)       ((p)->index)
102
103 #define cfs_page_pin(page) page_cache_get(page)
104 #define cfs_page_unpin(page) page_cache_release(page)
105
106 /*
107  * Memory allocator
108  * XXX Liang: move these declare to public file
109  */
110 extern void *cfs_alloc(size_t nr_bytes, u_int32_t flags);
111 extern void  cfs_free(void *addr);
112
113 extern void *cfs_alloc_large(size_t nr_bytes);
114 extern void  cfs_free_large(void *addr);
115
116 extern cfs_page_t *cfs_alloc_page(unsigned int flags);
117 extern void cfs_free_page(cfs_page_t *page);
118
119 #define cfs_memory_pressure_get() (current->flags & PF_MEMALLOC)
120 #define cfs_memory_pressure_set() do { current->flags |= PF_MEMALLOC; } while (0)
121 #define cfs_memory_pressure_clr() do { current->flags &= ~PF_MEMALLOC; } while (0)
122
123 #if BITS_PER_LONG == 32
124 /* limit to lowmem on 32-bit systems */
125 #define CFS_NUM_CACHEPAGES \
126         min(cfs_num_physpages, 1UL << (30 - CFS_PAGE_SHIFT) * 3 / 4)
127 #else
128 #define CFS_NUM_CACHEPAGES cfs_num_physpages
129 #endif
130
131 /*
132  * In Linux there is no way to determine whether current execution context is
133  * blockable.
134  */
135 #define CFS_ALLOC_ATOMIC_TRY   CFS_ALLOC_ATOMIC
136
137 /*
138  * SLAB allocator
139  * XXX Liang: move these declare to public file
140  */
141 typedef struct kmem_cache cfs_mem_cache_t;
142 extern cfs_mem_cache_t * cfs_mem_cache_create (const char *, size_t, size_t, unsigned long);
143 extern int cfs_mem_cache_destroy ( cfs_mem_cache_t * );
144 extern void *cfs_mem_cache_alloc ( cfs_mem_cache_t *, int);
145 extern void cfs_mem_cache_free ( cfs_mem_cache_t *, void *);
146 extern int cfs_mem_is_in_cache(const void *addr, const cfs_mem_cache_t *kmem);
147
148 #define CFS_DECL_MMSPACE                mm_segment_t __oldfs
149 #define CFS_MMSPACE_OPEN \
150         do { __oldfs = get_fs(); set_fs(get_ds());} while(0)
151 #define CFS_MMSPACE_CLOSE               set_fs(__oldfs)
152
153 #define CFS_SLAB_HWCACHE_ALIGN          SLAB_HWCACHE_ALIGN
154 #define CFS_SLAB_KERNEL                 SLAB_KERNEL
155 #define CFS_SLAB_NOFS                   SLAB_NOFS
156
157 /*
158  * Shrinker
159  */
160 #define cfs_shrinker    shrinker
161
162 #ifdef HAVE_SHRINK_CONTROL
163 # define SHRINKER_ARGS(sc, nr_to_scan, gfp_mask)  \
164                        struct shrinker *shrinker, \
165                        struct shrink_control *sc
166 # define shrink_param(sc, var) ((sc)->var)
167 #else
168 # ifdef HAVE_SHRINKER_WANT_SHRINK_PTR
169 #  define SHRINKER_ARGS(sc, nr_to_scan, gfp_mask)  \
170                         struct shrinker *shrinker, \
171                         int nr_to_scan, gfp_t gfp_mask
172 # else
173 #  define SHRINKER_ARGS(sc, nr_to_scan, gfp_mask)  \
174                         int nr_to_scan, gfp_t gfp_mask
175 # endif
176 # define shrink_param(sc, var) (var)
177 #endif
178
179 #ifdef HAVE_REGISTER_SHRINKER
180 typedef int (*cfs_shrinker_t)(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask));
181
182 static inline
183 struct cfs_shrinker *cfs_set_shrinker(int seek, cfs_shrinker_t func)
184 {
185         struct shrinker *s;
186
187         s = kmalloc(sizeof(*s), GFP_KERNEL);
188         if (s == NULL)
189                 return (NULL);
190
191         s->shrink = func;
192         s->seeks = seek;
193
194         register_shrinker(s);
195
196         return s;
197 }
198
199 static inline
200 void cfs_remove_shrinker(struct cfs_shrinker *shrinker)
201 {
202         if (shrinker == NULL)
203                 return;
204
205         unregister_shrinker(shrinker);
206         kfree(shrinker);
207 }
208 #else
209 typedef shrinker_t              cfs_shrinker_t;
210 #define cfs_set_shrinker(s, f)  set_shrinker(s, f)
211 #define cfs_remove_shrinker(s)  remove_shrinker(s)
212 #endif
213
214 #define CFS_DEFAULT_SEEKS                 DEFAULT_SEEKS
215 #endif /* __LINUX_CFS_MEM_H__ */