blob: 35c1a5e30ba1b279d4e596531cdfe2d6ceea2021 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
David Woodhousec00c3102007-04-25 14:16:47 +01004 * Copyright © 2001-2007 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Created by David Woodhouse <dwmw2@infradead.org>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
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 Torvalds1da177e2005-04-16 15:20:36 -070018/* These are initialised to NULL in the kernel startup code.
19 If you're porting to other operating systems, beware */
Christoph Lametere18b8902006-12-06 20:33:20 -080020static struct kmem_cache *full_dnode_slab;
21static struct kmem_cache *raw_dirent_slab;
22static struct kmem_cache *raw_inode_slab;
23static struct kmem_cache *tmp_dnode_info_slab;
24static struct kmem_cache *raw_node_ref_slab;
25static struct kmem_cache *node_frag_slab;
26static struct kmem_cache *inode_cache_slab;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090027#ifdef CONFIG_JFFS2_FS_XATTR
Christoph Lametere18b8902006-12-06 20:33:20 -080028static struct kmem_cache *xattr_datum_cache;
29static struct kmem_cache *xattr_ref_cache;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090030#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32int __init jffs2_create_slab_caches(void)
33{
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000034 full_dnode_slab = kmem_cache_create("jffs2_full_dnode",
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 sizeof(struct jffs2_full_dnode),
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +010036 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 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. Bityutskiyf538c962005-07-27 15:16:57 +010042 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 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. Bityutskiyf538c962005-07-27 15:16:57 +010048 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 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. Bityutskiyf538c962005-07-27 15:16:57 +010054 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (!tmp_dnode_info_slab)
56 goto err;
57
David Woodhouse9bfeb692006-05-26 21:19:05 +010058 raw_node_ref_slab = kmem_cache_create("jffs2_refblock",
59 sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1),
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +010060 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 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. Bityutskiyf538c962005-07-27 15:16:57 +010066 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 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. Bityutskiyf538c962005-07-27 15:16:57 +010072 0, 0, NULL, NULL);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090073 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 Torvalds1da177e2005-04-16 15:20:36 -070091 err:
92 jffs2_destroy_slab_caches();
93 return -ENOMEM;
94}
95
96void 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 Koheiaa98d7c2006-05-13 15:09:47 +0900112#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 Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
121{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100122 struct jffs2_full_dirent *ret;
123 ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100124 dbg_memalloc("%p\n", ret);
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100125 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128void jffs2_free_full_dirent(struct jffs2_full_dirent *x)
129{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100130 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 kfree(x);
132}
133
134struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
135{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100136 struct jffs2_full_dnode *ret;
137 ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100138 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return ret;
140}
141
142void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
143{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100144 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 kmem_cache_free(full_dnode_slab, x);
146}
147
148struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
149{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100150 struct jffs2_raw_dirent *ret;
151 ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100152 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return ret;
154}
155
156void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
157{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100158 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 kmem_cache_free(raw_dirent_slab, x);
160}
161
162struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
163{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100164 struct jffs2_raw_inode *ret;
165 ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100166 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return ret;
168}
169
170void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
171{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100172 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 kmem_cache_free(raw_inode_slab, x);
174}
175
176struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
177{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100178 struct jffs2_tmp_dnode_info *ret;
179 ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100180 dbg_memalloc("%p\n",
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100181 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return ret;
183}
184
185void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
186{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100187 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 kmem_cache_free(tmp_dnode_info_slab, x);
189}
190
Adrian Bunkc05d52c2006-06-22 12:03:35 +0200191static struct jffs2_raw_node_ref *jffs2_alloc_refblock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100193 struct jffs2_raw_node_ref *ret;
David Woodhouse9bfeb692006-05-26 21:19:05 +0100194
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100195 ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
David Woodhouse9bfeb692006-05-26 21:19:05 +0100196 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 Torvalds1da177e2005-04-16 15:20:36 -0700205 return ret;
206}
207
David Woodhouse9bfeb692006-05-26 21:19:05 +0100208int 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
249void jffs2_free_refblock(struct jffs2_raw_node_ref *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100251 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 kmem_cache_free(raw_node_ref_slab, x);
253}
254
255struct jffs2_node_frag *jffs2_alloc_node_frag(void)
256{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100257 struct jffs2_node_frag *ret;
258 ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100259 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return ret;
261}
262
263void jffs2_free_node_frag(struct jffs2_node_frag *x)
264{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100265 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 kmem_cache_free(node_frag_slab, x);
267}
268
269struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
270{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100271 struct jffs2_inode_cache *ret;
272 ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100273 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return ret;
275}
276
277void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
278{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100279 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 kmem_cache_free(inode_cache_slab, x);
281}
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900282
283#ifdef CONFIG_JFFS2_FS_XATTR
284struct 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 Koheic9f700f2006-06-11 10:35:15 +0900292 xd->node = (void *)xd;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900293 INIT_LIST_HEAD(&xd->xindex);
294 return xd;
295}
296
297void 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
303struct 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 Koheic9f700f2006-06-11 10:35:15 +0900311 ref->node = (void *)ref;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900312 return ref;
313}
314
315void 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