blob: 4f47aa24b5562001cf8983d6c7634c373d50206b [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
Joe Perches5a528952012-02-15 15:56:45 -080012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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 Torvalds1da177e2005-04-16 15:20:36 -070020/* These are initialised to NULL in the kernel startup code.
21 If you're porting to other operating systems, beware */
Christoph Lametere18b8902006-12-06 20:33:20 -080022static struct kmem_cache *full_dnode_slab;
23static struct kmem_cache *raw_dirent_slab;
24static struct kmem_cache *raw_inode_slab;
25static struct kmem_cache *tmp_dnode_info_slab;
26static struct kmem_cache *raw_node_ref_slab;
27static struct kmem_cache *node_frag_slab;
28static struct kmem_cache *inode_cache_slab;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090029#ifdef CONFIG_JFFS2_FS_XATTR
Christoph Lametere18b8902006-12-06 20:33:20 -080030static struct kmem_cache *xattr_datum_cache;
31static struct kmem_cache *xattr_ref_cache;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090032#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34int __init jffs2_create_slab_caches(void)
35{
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000036 full_dnode_slab = kmem_cache_create("jffs2_full_dnode",
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 sizeof(struct jffs2_full_dnode),
Paul Mundt20c2df82007-07-20 10:11:58 +090038 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (!full_dnode_slab)
40 goto err;
41
42 raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent",
43 sizeof(struct jffs2_raw_dirent),
David Woodhousedd799982009-09-19 16:14:01 -070044 0, SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 if (!raw_dirent_slab)
46 goto err;
47
48 raw_inode_slab = kmem_cache_create("jffs2_raw_inode",
49 sizeof(struct jffs2_raw_inode),
David Woodhousedd799982009-09-19 16:14:01 -070050 0, SLAB_HWCACHE_ALIGN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 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),
Paul Mundt20c2df82007-07-20 10:11:58 +090056 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 if (!tmp_dnode_info_slab)
58 goto err;
59
David Woodhouse9bfeb692006-05-26 21:19:05 +010060 raw_node_ref_slab = kmem_cache_create("jffs2_refblock",
61 sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1),
Paul Mundt20c2df82007-07-20 10:11:58 +090062 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 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),
Paul Mundt20c2df82007-07-20 10:11:58 +090068 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (!node_frag_slab)
70 goto err;
71
72 inode_cache_slab = kmem_cache_create("jffs2_inode_cache",
73 sizeof(struct jffs2_inode_cache),
Paul Mundt20c2df82007-07-20 10:11:58 +090074 0, 0, NULL);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090075 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),
Paul Mundt20c2df82007-07-20 10:11:58 +090081 0, 0, NULL);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090082 if (!xattr_datum_cache)
83 goto err;
84
85 xattr_ref_cache = kmem_cache_create("jffs2_xattr_ref",
86 sizeof(struct jffs2_xattr_ref),
Paul Mundt20c2df82007-07-20 10:11:58 +090087 0, 0, NULL);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +090088 if (!xattr_ref_cache)
89 goto err;
90#endif
91
92 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 err:
94 jffs2_destroy_slab_caches();
95 return -ENOMEM;
96}
97
98void 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 Koheiaa98d7c2006-05-13 15:09:47 +0900114#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 Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
123{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100124 struct jffs2_full_dirent *ret;
125 ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100126 dbg_memalloc("%p\n", ret);
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130void jffs2_free_full_dirent(struct jffs2_full_dirent *x)
131{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100132 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 kfree(x);
134}
135
136struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
137{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100138 struct jffs2_full_dnode *ret;
139 ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100140 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return ret;
142}
143
144void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
145{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100146 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 kmem_cache_free(full_dnode_slab, x);
148}
149
150struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
151{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100152 struct jffs2_raw_dirent *ret;
153 ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100154 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return ret;
156}
157
158void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
159{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100160 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 kmem_cache_free(raw_dirent_slab, x);
162}
163
164struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
165{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100166 struct jffs2_raw_inode *ret;
167 ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100168 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return ret;
170}
171
172void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
173{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100174 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 kmem_cache_free(raw_inode_slab, x);
176}
177
178struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
179{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100180 struct jffs2_tmp_dnode_info *ret;
181 ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100182 dbg_memalloc("%p\n",
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100183 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return ret;
185}
186
187void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
188{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100189 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 kmem_cache_free(tmp_dnode_info_slab, x);
191}
192
Adrian Bunkc05d52c2006-06-22 12:03:35 +0200193static struct jffs2_raw_node_ref *jffs2_alloc_refblock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100195 struct jffs2_raw_node_ref *ret;
David Woodhouse9bfeb692006-05-26 21:19:05 +0100196
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100197 ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
David Woodhouse9bfeb692006-05-26 21:19:05 +0100198 if (ret) {
199 int i = 0;
200 for (i=0; i < REFS_PER_BLOCK; i++) {
201 ret[i].flash_offset = REF_EMPTY_NODE;
202 ret[i].next_in_ino = NULL;
203 }
204 ret[i].flash_offset = REF_LINK_NODE;
205 ret[i].next_in_ino = NULL;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return ret;
208}
209
David Woodhouse9bfeb692006-05-26 21:19:05 +0100210int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c,
211 struct jffs2_eraseblock *jeb, int nr)
212{
213 struct jffs2_raw_node_ref **p, *ref;
214 int i = nr;
215
216 dbg_memalloc("%d\n", nr);
217
218 p = &jeb->last_node;
219 ref = *p;
220
221 dbg_memalloc("Reserving %d refs for block @0x%08x\n", nr, jeb->offset);
222
223 /* If jeb->last_node is really a valid node then skip over it */
224 if (ref && ref->flash_offset != REF_EMPTY_NODE)
225 ref++;
226
227 while (i) {
228 if (!ref) {
229 dbg_memalloc("Allocating new refblock linked from %p\n", p);
230 ref = *p = jffs2_alloc_refblock();
231 if (!ref)
232 return -ENOMEM;
233 }
234 if (ref->flash_offset == REF_LINK_NODE) {
235 p = &ref->next_in_ino;
236 ref = *p;
237 continue;
238 }
239 i--;
240 ref++;
241 }
242 jeb->allocated_refs = nr;
243
244 dbg_memalloc("Reserved %d refs for block @0x%08x, last_node is %p (%08x,%p)\n",
245 nr, jeb->offset, jeb->last_node, jeb->last_node->flash_offset,
246 jeb->last_node->next_in_ino);
247
248 return 0;
249}
250
251void jffs2_free_refblock(struct jffs2_raw_node_ref *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100253 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 kmem_cache_free(raw_node_ref_slab, x);
255}
256
257struct jffs2_node_frag *jffs2_alloc_node_frag(void)
258{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100259 struct jffs2_node_frag *ret;
260 ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100261 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return ret;
263}
264
265void jffs2_free_node_frag(struct jffs2_node_frag *x)
266{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100267 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 kmem_cache_free(node_frag_slab, x);
269}
270
271struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
272{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100273 struct jffs2_inode_cache *ret;
274 ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100275 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return ret;
277}
278
279void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
280{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100281 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 kmem_cache_free(inode_cache_slab, x);
283}
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900284
285#ifdef CONFIG_JFFS2_FS_XATTR
286struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void)
287{
288 struct jffs2_xattr_datum *xd;
Wei Yongjunc6d59cd2009-03-04 12:01:35 -0800289 xd = kmem_cache_zalloc(xattr_datum_cache, GFP_KERNEL);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900290 dbg_memalloc("%p\n", xd);
291
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900292 xd->class = RAWNODE_CLASS_XATTR_DATUM;
KaiGai Koheic9f700f2006-06-11 10:35:15 +0900293 xd->node = (void *)xd;
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900294 INIT_LIST_HEAD(&xd->xindex);
295 return xd;
296}
297
298void jffs2_free_xattr_datum(struct jffs2_xattr_datum *xd)
299{
300 dbg_memalloc("%p\n", xd);
301 kmem_cache_free(xattr_datum_cache, xd);
302}
303
304struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void)
305{
306 struct jffs2_xattr_ref *ref;
Wei Yongjunc6d59cd2009-03-04 12:01:35 -0800307 ref = kmem_cache_zalloc(xattr_ref_cache, GFP_KERNEL);
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900308 dbg_memalloc("%p\n", ref);
309
KaiGai Koheiaa98d7c2006-05-13 15:09:47 +0900310 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