Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * JFFS2 -- Journalling Flash File System, Version 2. |
| 3 | * |
| 4 | * Copyright (C) 2001-2003 Red Hat, Inc. |
| 5 | * |
| 6 | * Created by David Woodhouse <dwmw2@infradead.org> |
| 7 | * |
| 8 | * For licensing information, see the file 'LICENCE' in this directory. |
| 9 | * |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 10 | * $Id: malloc.c,v 1.31 2005/11/07 11:14:40 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/jffs2.h> |
| 18 | #include "nodelist.h" |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | /* These are initialised to NULL in the kernel startup code. |
| 21 | If you're porting to other operating systems, beware */ |
| 22 | static kmem_cache_t *full_dnode_slab; |
| 23 | static kmem_cache_t *raw_dirent_slab; |
| 24 | static kmem_cache_t *raw_inode_slab; |
| 25 | static kmem_cache_t *tmp_dnode_info_slab; |
| 26 | static kmem_cache_t *raw_node_ref_slab; |
| 27 | static kmem_cache_t *node_frag_slab; |
| 28 | static kmem_cache_t *inode_cache_slab; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 29 | #ifdef CONFIG_JFFS2_FS_XATTR |
| 30 | static kmem_cache_t *xattr_datum_cache; |
| 31 | static kmem_cache_t *xattr_ref_cache; |
| 32 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | int __init jffs2_create_slab_caches(void) |
| 35 | { |
Thomas Gleixner | 182ec4e | 2005-11-07 11:16:07 +0000 | [diff] [blame] | 36 | full_dnode_slab = kmem_cache_create("jffs2_full_dnode", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | sizeof(struct jffs2_full_dnode), |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 38 | 0, 0, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | if (!full_dnode_slab) |
| 40 | goto err; |
| 41 | |
| 42 | raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent", |
| 43 | sizeof(struct jffs2_raw_dirent), |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 44 | 0, 0, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | if (!raw_dirent_slab) |
| 46 | goto err; |
| 47 | |
| 48 | raw_inode_slab = kmem_cache_create("jffs2_raw_inode", |
| 49 | sizeof(struct jffs2_raw_inode), |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 50 | 0, 0, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | if (!raw_inode_slab) |
| 52 | goto err; |
| 53 | |
| 54 | tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode", |
| 55 | sizeof(struct jffs2_tmp_dnode_info), |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 56 | 0, 0, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | if (!tmp_dnode_info_slab) |
| 58 | goto err; |
| 59 | |
| 60 | raw_node_ref_slab = kmem_cache_create("jffs2_raw_node_ref", |
| 61 | sizeof(struct jffs2_raw_node_ref), |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 62 | 0, 0, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | if (!raw_node_ref_slab) |
| 64 | goto err; |
| 65 | |
| 66 | node_frag_slab = kmem_cache_create("jffs2_node_frag", |
| 67 | sizeof(struct jffs2_node_frag), |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 68 | 0, 0, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | if (!node_frag_slab) |
| 70 | goto err; |
| 71 | |
| 72 | inode_cache_slab = kmem_cache_create("jffs2_inode_cache", |
| 73 | sizeof(struct jffs2_inode_cache), |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 74 | 0, 0, NULL, NULL); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 75 | if (!inode_cache_slab) |
| 76 | goto err; |
| 77 | |
| 78 | #ifdef CONFIG_JFFS2_FS_XATTR |
| 79 | xattr_datum_cache = kmem_cache_create("jffs2_xattr_datum", |
| 80 | sizeof(struct jffs2_xattr_datum), |
| 81 | 0, 0, NULL, NULL); |
| 82 | if (!xattr_datum_cache) |
| 83 | goto err; |
| 84 | |
| 85 | xattr_ref_cache = kmem_cache_create("jffs2_xattr_ref", |
| 86 | sizeof(struct jffs2_xattr_ref), |
| 87 | 0, 0, NULL, NULL); |
| 88 | if (!xattr_ref_cache) |
| 89 | goto err; |
| 90 | #endif |
| 91 | |
| 92 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | err: |
| 94 | jffs2_destroy_slab_caches(); |
| 95 | return -ENOMEM; |
| 96 | } |
| 97 | |
| 98 | void jffs2_destroy_slab_caches(void) |
| 99 | { |
| 100 | if(full_dnode_slab) |
| 101 | kmem_cache_destroy(full_dnode_slab); |
| 102 | if(raw_dirent_slab) |
| 103 | kmem_cache_destroy(raw_dirent_slab); |
| 104 | if(raw_inode_slab) |
| 105 | kmem_cache_destroy(raw_inode_slab); |
| 106 | if(tmp_dnode_info_slab) |
| 107 | kmem_cache_destroy(tmp_dnode_info_slab); |
| 108 | if(raw_node_ref_slab) |
| 109 | kmem_cache_destroy(raw_node_ref_slab); |
| 110 | if(node_frag_slab) |
| 111 | kmem_cache_destroy(node_frag_slab); |
| 112 | if(inode_cache_slab) |
| 113 | kmem_cache_destroy(inode_cache_slab); |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 114 | #ifdef CONFIG_JFFS2_FS_XATTR |
| 115 | if (xattr_datum_cache) |
| 116 | kmem_cache_destroy(xattr_datum_cache); |
| 117 | if (xattr_ref_cache) |
| 118 | kmem_cache_destroy(xattr_ref_cache); |
| 119 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize) |
| 123 | { |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 124 | struct jffs2_full_dirent *ret; |
| 125 | ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL); |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 126 | dbg_memalloc("%p\n", ret); |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 127 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void jffs2_free_full_dirent(struct jffs2_full_dirent *x) |
| 131 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 132 | dbg_memalloc("%p\n", x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | kfree(x); |
| 134 | } |
| 135 | |
| 136 | struct jffs2_full_dnode *jffs2_alloc_full_dnode(void) |
| 137 | { |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 138 | struct jffs2_full_dnode *ret; |
| 139 | ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL); |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 140 | dbg_memalloc("%p\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | void jffs2_free_full_dnode(struct jffs2_full_dnode *x) |
| 145 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 146 | dbg_memalloc("%p\n", x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | kmem_cache_free(full_dnode_slab, x); |
| 148 | } |
| 149 | |
| 150 | struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void) |
| 151 | { |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 152 | struct jffs2_raw_dirent *ret; |
| 153 | ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL); |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 154 | dbg_memalloc("%p\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x) |
| 159 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 160 | dbg_memalloc("%p\n", x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | kmem_cache_free(raw_dirent_slab, x); |
| 162 | } |
| 163 | |
| 164 | struct jffs2_raw_inode *jffs2_alloc_raw_inode(void) |
| 165 | { |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 166 | struct jffs2_raw_inode *ret; |
| 167 | ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL); |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 168 | dbg_memalloc("%p\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | void jffs2_free_raw_inode(struct jffs2_raw_inode *x) |
| 173 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 174 | dbg_memalloc("%p\n", x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | kmem_cache_free(raw_inode_slab, x); |
| 176 | } |
| 177 | |
| 178 | struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void) |
| 179 | { |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 180 | struct jffs2_tmp_dnode_info *ret; |
| 181 | ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL); |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 182 | dbg_memalloc("%p\n", |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 183 | ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x) |
| 188 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 189 | dbg_memalloc("%p\n", x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | kmem_cache_free(tmp_dnode_info_slab, x); |
| 191 | } |
| 192 | |
David Woodhouse | 046b8b9 | 2006-05-25 01:50:35 +0100 | [diff] [blame] | 193 | int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c, |
| 194 | struct jffs2_eraseblock *jeb, int nr) |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 195 | { |
| 196 | struct jffs2_raw_node_ref *p = c->refs; |
| 197 | |
| 198 | dbg_memalloc("%d\n", nr); |
| 199 | |
| 200 | while (nr && p) { |
| 201 | p = p->next_in_ino; |
| 202 | nr--; |
| 203 | } |
| 204 | while (nr) { |
| 205 | p = __jffs2_alloc_raw_node_ref(); |
| 206 | if (!p) |
| 207 | return -ENOMEM; |
| 208 | p->next_in_ino = c->refs; |
| 209 | c->refs = p; |
| 210 | nr--; |
| 211 | } |
| 212 | c->reserved_refs = nr; |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | struct jffs2_raw_node_ref *__jffs2_alloc_raw_node_ref(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 218 | struct jffs2_raw_node_ref *ret; |
| 219 | ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL); |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 220 | dbg_memalloc("%p\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return ret; |
| 222 | } |
| 223 | |
David Woodhouse | 2f78540 | 2006-05-24 02:04:45 +0100 | [diff] [blame] | 224 | void __jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *x) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 226 | dbg_memalloc("%p\n", x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | kmem_cache_free(raw_node_ref_slab, x); |
| 228 | } |
| 229 | |
| 230 | struct jffs2_node_frag *jffs2_alloc_node_frag(void) |
| 231 | { |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 232 | struct jffs2_node_frag *ret; |
| 233 | ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL); |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 234 | dbg_memalloc("%p\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | void jffs2_free_node_frag(struct jffs2_node_frag *x) |
| 239 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 240 | dbg_memalloc("%p\n", x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | kmem_cache_free(node_frag_slab, x); |
| 242 | } |
| 243 | |
| 244 | struct jffs2_inode_cache *jffs2_alloc_inode_cache(void) |
| 245 | { |
Artem B. Bityutskiy | f538c96 | 2005-07-27 15:16:57 +0100 | [diff] [blame] | 246 | struct jffs2_inode_cache *ret; |
| 247 | ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL); |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 248 | dbg_memalloc("%p\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | void jffs2_free_inode_cache(struct jffs2_inode_cache *x) |
| 253 | { |
Artem B. Bityutskiy | 733802d | 2005-09-22 12:25:00 +0100 | [diff] [blame] | 254 | dbg_memalloc("%p\n", x); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | kmem_cache_free(inode_cache_slab, x); |
| 256 | } |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 257 | |
| 258 | #ifdef CONFIG_JFFS2_FS_XATTR |
| 259 | struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void) |
| 260 | { |
| 261 | struct jffs2_xattr_datum *xd; |
| 262 | xd = kmem_cache_alloc(xattr_datum_cache, GFP_KERNEL); |
| 263 | dbg_memalloc("%p\n", xd); |
| 264 | |
| 265 | memset(xd, 0, sizeof(struct jffs2_xattr_datum)); |
| 266 | xd->class = RAWNODE_CLASS_XATTR_DATUM; |
| 267 | INIT_LIST_HEAD(&xd->xindex); |
| 268 | return xd; |
| 269 | } |
| 270 | |
| 271 | void jffs2_free_xattr_datum(struct jffs2_xattr_datum *xd) |
| 272 | { |
| 273 | dbg_memalloc("%p\n", xd); |
| 274 | kmem_cache_free(xattr_datum_cache, xd); |
| 275 | } |
| 276 | |
| 277 | struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void) |
| 278 | { |
| 279 | struct jffs2_xattr_ref *ref; |
| 280 | ref = kmem_cache_alloc(xattr_ref_cache, GFP_KERNEL); |
| 281 | dbg_memalloc("%p\n", ref); |
| 282 | |
| 283 | memset(ref, 0, sizeof(struct jffs2_xattr_ref)); |
| 284 | ref->class = RAWNODE_CLASS_XATTR_REF; |
KaiGai Kohei | aa98d7c | 2006-05-13 15:09:47 +0900 | [diff] [blame] | 285 | return ref; |
| 286 | } |
| 287 | |
| 288 | void jffs2_free_xattr_ref(struct jffs2_xattr_ref *ref) |
| 289 | { |
| 290 | dbg_memalloc("%p\n", ref); |
| 291 | kmem_cache_free(xattr_ref_cache, ref); |
| 292 | } |
| 293 | #endif |