Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Christoph Hellwig | 760dea6 | 2005-09-02 16:56:02 +1000 | [diff] [blame] | 28 | #define KM_SLEEP 0x0001u |
| 29 | #define KM_NOSLEEP 0x0002u |
| 30 | #define KM_NOFS 0x0004u |
| 31 | #define KM_MAYFAIL 0x0008u |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Pekka J Enberg | 2109a2d | 2005-11-07 00:58:01 -0800 | [diff] [blame] | 33 | #define kmem_zone kmem_cache |
| 34 | #define kmem_zone_t struct kmem_cache |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 70 | static __inline gfp_t kmem_flags_convert(unsigned int __nocast flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Nathan Scott | 7f248a8 | 2005-11-03 16:14:31 +1100 | [diff] [blame] | 72 | gfp_t lflags = __GFP_NOWARN; /* we'll report problems, if need be */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Nathan Scott | 7f248a8 | 2005-11-03 16:14:31 +1100 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Nathan Scott | 7f248a8 | 2005-11-03 16:14:31 +1100 | [diff] [blame] | 114 | extern void *kmem_zone_zalloc(kmem_zone_t *, unsigned int __nocast); |
| 115 | extern void *kmem_zone_alloc(kmem_zone_t *, unsigned int __nocast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
Nathan Scott | 7f248a8 | 2005-11-03 16:14:31 +1100 | [diff] [blame] | 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); |
| 120 | extern void kmem_free(void *, size_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | typedef struct shrinker *kmem_shaker_t; |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 123 | typedef int (*kmem_shake_func_t)(int, gfp_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 138 | kmem_shake_allow(gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
| 140 | return (gfp_mask & __GFP_WAIT); |
| 141 | } |
| 142 | |
| 143 | #endif /* __XFS_SUPPORT_KMEM_H__ */ |