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