]> Git Repo - linux.git/blame - fs/jffs2/malloc.c
Merge tag 'drm-misc-next-fixes-2024-06-07' of https://gitlab.freedesktop.org/drm...
[linux.git] / fs / jffs2 / malloc.c
CommitLineData
1da177e4
LT
1/*
2 * JFFS2 -- Journalling Flash File System, Version 2.
3 *
c00c310e 4 * Copyright © 2001-2007 Red Hat, Inc.
1da177e4
LT
5 *
6 * Created by David Woodhouse <[email protected]>
7 *
8 * For licensing information, see the file 'LICENCE' in this directory.
9 *
1da177e4
LT
10 */
11
5a528957
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
1da177e4
LT
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
1da177e4
LT
20/* These are initialised to NULL in the kernel startup code.
21 If you're porting to other operating systems, beware */
e18b890b
CL
22static 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;
aa98d7cf 29#ifdef CONFIG_JFFS2_FS_XATTR
e18b890b
CL
30static struct kmem_cache *xattr_datum_cache;
31static struct kmem_cache *xattr_ref_cache;
aa98d7cf 32#endif
1da177e4
LT
33
34int __init jffs2_create_slab_caches(void)
35{
7096fae5 36 full_dnode_slab = KMEM_CACHE(jffs2_full_dnode, 0);
1da177e4
LT
37 if (!full_dnode_slab)
38 goto err;
39
7096fae5 40 raw_dirent_slab = KMEM_CACHE(jffs2_raw_dirent, SLAB_HWCACHE_ALIGN);
1da177e4
LT
41 if (!raw_dirent_slab)
42 goto err;
43
7096fae5 44 raw_inode_slab = KMEM_CACHE(jffs2_raw_inode, SLAB_HWCACHE_ALIGN);
1da177e4
LT
45 if (!raw_inode_slab)
46 goto err;
47
7096fae5 48 tmp_dnode_info_slab = KMEM_CACHE(jffs2_tmp_dnode_info, 0);
1da177e4
LT
49 if (!tmp_dnode_info_slab)
50 goto err;
51
9bfeb691
DW
52 raw_node_ref_slab = kmem_cache_create("jffs2_refblock",
53 sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1),
20c2df83 54 0, 0, NULL);
1da177e4
LT
55 if (!raw_node_ref_slab)
56 goto err;
57
7096fae5 58 node_frag_slab = KMEM_CACHE(jffs2_node_frag, 0);
1da177e4
LT
59 if (!node_frag_slab)
60 goto err;
61
7096fae5 62 inode_cache_slab = KMEM_CACHE(jffs2_inode_cache, 0);
aa98d7cf
KK
63 if (!inode_cache_slab)
64 goto err;
65
66#ifdef CONFIG_JFFS2_FS_XATTR
7096fae5 67 xattr_datum_cache = KMEM_CACHE(jffs2_xattr_datum, 0);
aa98d7cf
KK
68 if (!xattr_datum_cache)
69 goto err;
70
7096fae5 71 xattr_ref_cache = KMEM_CACHE(jffs2_xattr_ref, 0);
aa98d7cf
KK
72 if (!xattr_ref_cache)
73 goto err;
74#endif
75
76 return 0;
1da177e4
LT
77 err:
78 jffs2_destroy_slab_caches();
79 return -ENOMEM;
80}
81
82void jffs2_destroy_slab_caches(void)
83{
e1305df1
JL
84 kmem_cache_destroy(full_dnode_slab);
85 kmem_cache_destroy(raw_dirent_slab);
86 kmem_cache_destroy(raw_inode_slab);
87 kmem_cache_destroy(tmp_dnode_info_slab);
88 kmem_cache_destroy(raw_node_ref_slab);
89 kmem_cache_destroy(node_frag_slab);
90 kmem_cache_destroy(inode_cache_slab);
aa98d7cf 91#ifdef CONFIG_JFFS2_FS_XATTR
e1305df1
JL
92 kmem_cache_destroy(xattr_datum_cache);
93 kmem_cache_destroy(xattr_ref_cache);
aa98d7cf 94#endif
1da177e4
LT
95}
96
97struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
98{
f538c96b
AB
99 struct jffs2_full_dirent *ret;
100 ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
733802d9 101 dbg_memalloc("%p\n", ret);
f538c96b 102 return ret;
1da177e4
LT
103}
104
105void jffs2_free_full_dirent(struct jffs2_full_dirent *x)
106{
733802d9 107 dbg_memalloc("%p\n", x);
1da177e4
LT
108 kfree(x);
109}
110
111struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
112{
f538c96b
AB
113 struct jffs2_full_dnode *ret;
114 ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
733802d9 115 dbg_memalloc("%p\n", ret);
1da177e4
LT
116 return ret;
117}
118
119void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
120{
733802d9 121 dbg_memalloc("%p\n", x);
1da177e4
LT
122 kmem_cache_free(full_dnode_slab, x);
123}
124
125struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
126{
f538c96b
AB
127 struct jffs2_raw_dirent *ret;
128 ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
733802d9 129 dbg_memalloc("%p\n", ret);
1da177e4
LT
130 return ret;
131}
132
133void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
134{
733802d9 135 dbg_memalloc("%p\n", x);
1da177e4
LT
136 kmem_cache_free(raw_dirent_slab, x);
137}
138
139struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
140{
f538c96b
AB
141 struct jffs2_raw_inode *ret;
142 ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
733802d9 143 dbg_memalloc("%p\n", ret);
1da177e4
LT
144 return ret;
145}
146
147void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
148{
733802d9 149 dbg_memalloc("%p\n", x);
1da177e4
LT
150 kmem_cache_free(raw_inode_slab, x);
151}
152
153struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
154{
f538c96b
AB
155 struct jffs2_tmp_dnode_info *ret;
156 ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
733802d9 157 dbg_memalloc("%p\n",
f538c96b 158 ret);
1da177e4
LT
159 return ret;
160}
161
162void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
163{
733802d9 164 dbg_memalloc("%p\n", x);
1da177e4
LT
165 kmem_cache_free(tmp_dnode_info_slab, x);
166}
167
c05d52c7 168static struct jffs2_raw_node_ref *jffs2_alloc_refblock(void)
9bfeb691
DW
169{
170 struct jffs2_raw_node_ref *ret;
171
172 ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
173 if (ret) {
174 int i = 0;
175 for (i=0; i < REFS_PER_BLOCK; i++) {
176 ret[i].flash_offset = REF_EMPTY_NODE;
177 ret[i].next_in_ino = NULL;
178 }
179 ret[i].flash_offset = REF_LINK_NODE;
180 ret[i].next_in_ino = NULL;
181 }
182 return ret;
183}
184
046b8b98
DW
185int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c,
186 struct jffs2_eraseblock *jeb, int nr)
2f785402 187{
9bfeb691
DW
188 struct jffs2_raw_node_ref **p, *ref;
189 int i = nr;
2f785402
DW
190
191 dbg_memalloc("%d\n", nr);
192
9bfeb691
DW
193 p = &jeb->last_node;
194 ref = *p;
195
196 dbg_memalloc("Reserving %d refs for block @0x%08x\n", nr, jeb->offset);
197
198 /* If jeb->last_node is really a valid node then skip over it */
199 if (ref && ref->flash_offset != REF_EMPTY_NODE)
200 ref++;
201
202 while (i) {
203 if (!ref) {
204 dbg_memalloc("Allocating new refblock linked from %p\n", p);
205 ref = *p = jffs2_alloc_refblock();
206 if (!ref)
207 return -ENOMEM;
208 }
209 if (ref->flash_offset == REF_LINK_NODE) {
210 p = &ref->next_in_ino;
211 ref = *p;
212 continue;
213 }
214 i--;
215 ref++;
2f785402 216 }
9bfeb691 217 jeb->allocated_refs = nr;
2f785402 218
9bfeb691
DW
219 dbg_memalloc("Reserved %d refs for block @0x%08x, last_node is %p (%08x,%p)\n",
220 nr, jeb->offset, jeb->last_node, jeb->last_node->flash_offset,
221 jeb->last_node->next_in_ino);
222
223 return 0;
1da177e4
LT
224}
225
9bfeb691 226void jffs2_free_refblock(struct jffs2_raw_node_ref *x)
1da177e4 227{
733802d9 228 dbg_memalloc("%p\n", x);
1da177e4
LT
229 kmem_cache_free(raw_node_ref_slab, x);
230}
231
232struct jffs2_node_frag *jffs2_alloc_node_frag(void)
233{
f538c96b
AB
234 struct jffs2_node_frag *ret;
235 ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
733802d9 236 dbg_memalloc("%p\n", ret);
1da177e4
LT
237 return ret;
238}
239
240void jffs2_free_node_frag(struct jffs2_node_frag *x)
241{
733802d9 242 dbg_memalloc("%p\n", x);
1da177e4
LT
243 kmem_cache_free(node_frag_slab, x);
244}
245
246struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
247{
f538c96b
AB
248 struct jffs2_inode_cache *ret;
249 ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
733802d9 250 dbg_memalloc("%p\n", ret);
1da177e4
LT
251 return ret;
252}
253
254void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
255{
733802d9 256 dbg_memalloc("%p\n", x);
1da177e4
LT
257 kmem_cache_free(inode_cache_slab, x);
258}
aa98d7cf
KK
259
260#ifdef CONFIG_JFFS2_FS_XATTR
261struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void)
262{
263 struct jffs2_xattr_datum *xd;
c6d59cdd 264 xd = kmem_cache_zalloc(xattr_datum_cache, GFP_KERNEL);
aa98d7cf 265 dbg_memalloc("%p\n", xd);
2072552c
ZZ
266 if (!xd)
267 return NULL;
aa98d7cf 268
aa98d7cf 269 xd->class = RAWNODE_CLASS_XATTR_DATUM;
c9f700f8 270 xd->node = (void *)xd;
aa98d7cf
KK
271 INIT_LIST_HEAD(&xd->xindex);
272 return xd;
273}
274
275void jffs2_free_xattr_datum(struct jffs2_xattr_datum *xd)
276{
277 dbg_memalloc("%p\n", xd);
278 kmem_cache_free(xattr_datum_cache, xd);
279}
280
281struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void)
282{
283 struct jffs2_xattr_ref *ref;
c6d59cdd 284 ref = kmem_cache_zalloc(xattr_ref_cache, GFP_KERNEL);
aa98d7cf 285 dbg_memalloc("%p\n", ref);
2072552c
ZZ
286 if (!ref)
287 return NULL;
aa98d7cf 288
aa98d7cf 289 ref->class = RAWNODE_CLASS_XATTR_REF;
c9f700f8 290 ref->node = (void *)ref;
aa98d7cf
KK
291 return ref;
292}
293
294void jffs2_free_xattr_ref(struct jffs2_xattr_ref *ref)
295{
296 dbg_memalloc("%p\n", ref);
297 kmem_cache_free(xattr_ref_cache, ref);
298}
299#endif
This page took 1.183829 seconds and 4 git commands to generate.