This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / xfs / linux-2.6 / kmem.h
1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32 #ifndef __XFS_SUPPORT_KMEM_H__
33 #define __XFS_SUPPORT_KMEM_H__
34
35 #include <linux/mm.h>
36 #include <linux/highmem.h>
37 #include <linux/slab.h>
38 #include <linux/vmalloc.h>
39
40 /*
41  * Cutoff point to use vmalloc instead of kmalloc.
42  */
43 #define MAX_SLAB_SIZE   0x20000
44
45 /*
46  * XFS uses slightly different names for these due to the
47  * IRIX heritage.
48  */
49 #define kmem_zone       kmem_cache_s
50 #define kmem_zone_t     kmem_cache_t
51
52 #define KM_SLEEP        0x0001
53 #define KM_NOSLEEP      0x0002
54 #define KM_NOFS         0x0004
55 #define KM_MAYFAIL      0x0005
56
57 typedef unsigned long xfs_pflags_t;
58
59 #define PFLAGS_TEST_FSTRANS()           (current->flags & PF_FSTRANS)
60
61 /* these could be nested, so we save state */
62 #define PFLAGS_SET_FSTRANS(STATEP) do { \
63         *(STATEP) = current->flags;     \
64         current->flags |= PF_FSTRANS;   \
65 } while (0)
66
67 #define PFLAGS_CLEAR_FSTRANS(STATEP) do { \
68         *(STATEP) = current->flags;     \
69         current->flags &= ~PF_FSTRANS;  \
70 } while (0)
71
72 /* Restore the PF_FSTRANS state to what was saved in STATEP */
73 #define PFLAGS_RESTORE_FSTRANS(STATEP) do {                     \
74         current->flags = ((current->flags & ~PF_FSTRANS) |      \
75                           (*(STATEP) & PF_FSTRANS));            \
76 } while (0)
77
78 #define PFLAGS_DUP(OSTATEP, NSTATEP) do { \
79         *(NSTATEP) = *(OSTATEP);        \
80 } while (0)
81
82 static __inline unsigned int
83 kmem_flags_convert(int flags)
84 {
85         int lflags;
86
87 #if DEBUG
88         if (unlikely(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL))) {
89                 printk(KERN_WARNING
90                     "XFS: memory allocation with wrong flags (%x)\n", flags);
91                 BUG();
92         }
93 #endif
94
95         if (flags & KM_NOSLEEP) {
96                 lflags = GFP_ATOMIC;
97         } else {
98                 lflags = GFP_KERNEL;
99
100                 /* avoid recusive callbacks to filesystem during transactions */
101                 if (PFLAGS_TEST_FSTRANS() || (flags & KM_NOFS))
102                         lflags &= ~__GFP_FS;
103
104                 if (!(flags & KM_MAYFAIL))
105                         lflags |= __GFP_NOFAIL;
106         }
107
108         return lflags;
109 }
110
111 static __inline void *
112 kmem_alloc(size_t size, int flags)
113 {
114         if (unlikely(MAX_SLAB_SIZE < size))
115                 /* Avoid doing filesystem sensitive stuff to get this */
116                 return __vmalloc(size, kmem_flags_convert(flags), PAGE_KERNEL);
117         return kmalloc(size, kmem_flags_convert(flags));
118 }
119
120 static __inline void *
121 kmem_zalloc(size_t size, int flags)
122 {
123         void *ptr = kmem_alloc(size, flags);
124         if (likely(ptr != NULL))
125                 memset(ptr, 0, size);
126         return ptr;
127 }
128
129 static __inline void
130 kmem_free(void *ptr, size_t size)
131 {
132         if (unlikely((unsigned long)ptr < VMALLOC_START ||
133                      (unsigned long)ptr >= VMALLOC_END))
134                 kfree(ptr);
135         else
136                 vfree(ptr);
137 }
138
139 static __inline void *
140 kmem_realloc(void *ptr, size_t newsize, size_t oldsize, int flags)
141 {
142         void *new = kmem_alloc(newsize, flags);
143
144         if (likely(ptr != NULL)) {
145                 if (likely(new != NULL))
146                         memcpy(new, ptr, min(oldsize, newsize));
147                 kmem_free(ptr, oldsize);
148         }
149
150         return new;
151 }
152
153 static __inline kmem_zone_t *
154 kmem_zone_init(int size, char *zone_name)
155 {
156         return kmem_cache_create(zone_name, size, 0, 0, NULL, NULL);
157 }
158
159 static __inline void *
160 kmem_zone_alloc(kmem_zone_t *zone, int flags)
161 {
162         return kmem_cache_alloc(zone, kmem_flags_convert(flags));
163 }
164
165 static __inline void *
166 kmem_zone_zalloc(kmem_zone_t *zone, int flags)
167 {
168         void *ptr = kmem_zone_alloc(zone, flags);
169         if (likely(ptr != NULL))
170                 memset(ptr, 0, kmem_cache_size(zone));
171         return ptr;
172 }
173
174 static __inline void
175 kmem_zone_free(kmem_zone_t *zone, void *ptr)
176 {
177         kmem_cache_free(zone, ptr);
178 }
179
180 typedef struct shrinker *kmem_shaker_t;
181 typedef int (*kmem_shake_func_t)(int, unsigned int);
182
183 static __inline kmem_shaker_t
184 kmem_shake_register(kmem_shake_func_t sfunc)
185 {
186         return set_shrinker(DEFAULT_SEEKS, sfunc);
187 }
188
189 static __inline void
190 kmem_shake_deregister(kmem_shaker_t shrinker)
191 {
192         remove_shrinker(shrinker);
193 }
194
195 static __inline int
196 kmem_shake_allow(unsigned int gfp_mask)
197 {
198         return (gfp_mask & __GFP_WAIT);
199 }
200
201 #endif /* __XFS_SUPPORT_KMEM_H__ */