]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 LT |
17 | */ |
18 | #ifndef __XFS_SUPPORT_KMEM_H__ | |
19 | #define __XFS_SUPPORT_KMEM_H__ | |
20 | ||
21 | #include <linux/slab.h> | |
22 | #include <linux/sched.h> | |
23 | #include <linux/mm.h> | |
24 | ||
25 | /* | |
26 | * memory management routines | |
27 | */ | |
760dea67 CH |
28 | #define KM_SLEEP 0x0001u |
29 | #define KM_NOSLEEP 0x0002u | |
30 | #define KM_NOFS 0x0004u | |
31 | #define KM_MAYFAIL 0x0008u | |
1da177e4 LT |
32 | |
33 | #define kmem_zone kmem_cache_s | |
34 | #define kmem_zone_t kmem_cache_t | |
35 | ||
36 | typedef unsigned long xfs_pflags_t; | |
37 | ||
38 | #define PFLAGS_TEST_NOIO() (current->flags & PF_NOIO) | |
39 | #define PFLAGS_TEST_FSTRANS() (current->flags & PF_FSTRANS) | |
40 | ||
41 | #define PFLAGS_SET_NOIO() do { \ | |
42 | current->flags |= PF_NOIO; \ | |
43 | } while (0) | |
44 | ||
45 | #define PFLAGS_CLEAR_NOIO() do { \ | |
46 | current->flags &= ~PF_NOIO; \ | |
47 | } while (0) | |
48 | ||
49 | /* these could be nested, so we save state */ | |
50 | #define PFLAGS_SET_FSTRANS(STATEP) do { \ | |
51 | *(STATEP) = current->flags; \ | |
52 | current->flags |= PF_FSTRANS; \ | |
53 | } while (0) | |
54 | ||
55 | #define PFLAGS_CLEAR_FSTRANS(STATEP) do { \ | |
56 | *(STATEP) = current->flags; \ | |
57 | current->flags &= ~PF_FSTRANS; \ | |
58 | } while (0) | |
59 | ||
60 | /* Restore the PF_FSTRANS state to what was saved in STATEP */ | |
61 | #define PFLAGS_RESTORE_FSTRANS(STATEP) do { \ | |
62 | current->flags = ((current->flags & ~PF_FSTRANS) | \ | |
63 | (*(STATEP) & PF_FSTRANS)); \ | |
64 | } while (0) | |
65 | ||
66 | #define PFLAGS_DUP(OSTATEP, NSTATEP) do { \ | |
67 | *(NSTATEP) = *(OSTATEP); \ | |
68 | } while (0) | |
69 | ||
27496a8c | 70 | static __inline gfp_t kmem_flags_convert(unsigned int __nocast flags) |
1da177e4 | 71 | { |
27496a8c | 72 | gfp_t lflags = __GFP_NOWARN; /* we'll report problems, if need be */ |
1da177e4 LT |
73 | |
74 | #ifdef DEBUG | |
75 | if (unlikely(flags & ~(KM_SLEEP|KM_NOSLEEP|KM_NOFS|KM_MAYFAIL))) { | |
76 | printk(KERN_WARNING | |
77 | "XFS: memory allocation with wrong flags (%x)\n", flags); | |
78 | BUG(); | |
79 | } | |
80 | #endif | |
81 | ||
82 | if (flags & KM_NOSLEEP) { | |
83 | lflags |= GFP_ATOMIC; | |
84 | } else { | |
85 | lflags |= GFP_KERNEL; | |
86 | ||
87 | /* avoid recusive callbacks to filesystem during transactions */ | |
88 | if (PFLAGS_TEST_FSTRANS() || (flags & KM_NOFS)) | |
89 | lflags &= ~__GFP_FS; | |
90 | } | |
91 | ||
92 | return lflags; | |
93 | } | |
94 | ||
95 | static __inline kmem_zone_t * | |
96 | kmem_zone_init(int size, char *zone_name) | |
97 | { | |
98 | return kmem_cache_create(zone_name, size, 0, 0, NULL, NULL); | |
99 | } | |
100 | ||
101 | static __inline void | |
102 | kmem_zone_free(kmem_zone_t *zone, void *ptr) | |
103 | { | |
104 | kmem_cache_free(zone, ptr); | |
105 | } | |
106 | ||
107 | static __inline void | |
108 | kmem_zone_destroy(kmem_zone_t *zone) | |
109 | { | |
110 | if (zone && kmem_cache_destroy(zone)) | |
111 | BUG(); | |
112 | } | |
113 | ||
27496a8c AV |
114 | extern void *kmem_zone_zalloc(kmem_zone_t *, unsigned int __nocast); |
115 | extern void *kmem_zone_alloc(kmem_zone_t *, unsigned int __nocast); | |
1da177e4 | 116 | |
27496a8c AV |
117 | extern void *kmem_alloc(size_t, unsigned int __nocast); |
118 | extern void *kmem_realloc(void *, size_t, size_t, unsigned int __nocast); | |
119 | extern void *kmem_zalloc(size_t, unsigned int __nocast); | |
1da177e4 LT |
120 | extern void kmem_free(void *, size_t); |
121 | ||
122 | typedef struct shrinker *kmem_shaker_t; | |
27496a8c | 123 | typedef int (*kmem_shake_func_t)(int, gfp_t); |
1da177e4 LT |
124 | |
125 | static __inline kmem_shaker_t | |
126 | kmem_shake_register(kmem_shake_func_t sfunc) | |
127 | { | |
128 | return set_shrinker(DEFAULT_SEEKS, sfunc); | |
129 | } | |
130 | ||
131 | static __inline void | |
132 | kmem_shake_deregister(kmem_shaker_t shrinker) | |
133 | { | |
134 | remove_shrinker(shrinker); | |
135 | } | |
136 | ||
137 | static __inline int | |
27496a8c | 138 | kmem_shake_allow(gfp_t gfp_mask) |
1da177e4 LT |
139 | { |
140 | return (gfp_mask & __GFP_WAIT); | |
141 | } | |
142 | ||
143 | #endif /* __XFS_SUPPORT_KMEM_H__ */ |