blob: 036cbd11c00449d6755481b8a51e53bbf0f7fb4f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Gleixner182ec4e2005-11-07 11:16:07 +000010 * $Id: malloc.c,v 1.31 2005/11/07 11:14:40 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
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 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 */
22static kmem_cache_t *full_dnode_slab;
23static kmem_cache_t *raw_dirent_slab;
24static kmem_cache_t *raw_inode_slab;
25static kmem_cache_t *tmp_dnode_info_slab;
26static kmem_cache_t *raw_node_ref_slab;
27static kmem_cache_t *node_frag_slab;
28static kmem_cache_t *inode_cache_slab;
29
30int __init jffs2_create_slab_caches(void)
31{
Thomas Gleixner182ec4e2005-11-07 11:16:07 +000032 full_dnode_slab = kmem_cache_create("jffs2_full_dnode",
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 sizeof(struct jffs2_full_dnode),
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +010034 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 if (!full_dnode_slab)
36 goto err;
37
38 raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent",
39 sizeof(struct jffs2_raw_dirent),
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +010040 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (!raw_dirent_slab)
42 goto err;
43
44 raw_inode_slab = kmem_cache_create("jffs2_raw_inode",
45 sizeof(struct jffs2_raw_inode),
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +010046 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if (!raw_inode_slab)
48 goto err;
49
50 tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode",
51 sizeof(struct jffs2_tmp_dnode_info),
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +010052 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 if (!tmp_dnode_info_slab)
54 goto err;
55
56 raw_node_ref_slab = kmem_cache_create("jffs2_raw_node_ref",
57 sizeof(struct jffs2_raw_node_ref),
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +010058 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (!raw_node_ref_slab)
60 goto err;
61
62 node_frag_slab = kmem_cache_create("jffs2_node_frag",
63 sizeof(struct jffs2_node_frag),
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +010064 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (!node_frag_slab)
66 goto err;
67
68 inode_cache_slab = kmem_cache_create("jffs2_inode_cache",
69 sizeof(struct jffs2_inode_cache),
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +010070 0, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (inode_cache_slab)
72 return 0;
73 err:
74 jffs2_destroy_slab_caches();
75 return -ENOMEM;
76}
77
78void jffs2_destroy_slab_caches(void)
79{
80 if(full_dnode_slab)
81 kmem_cache_destroy(full_dnode_slab);
82 if(raw_dirent_slab)
83 kmem_cache_destroy(raw_dirent_slab);
84 if(raw_inode_slab)
85 kmem_cache_destroy(raw_inode_slab);
86 if(tmp_dnode_info_slab)
87 kmem_cache_destroy(tmp_dnode_info_slab);
88 if(raw_node_ref_slab)
89 kmem_cache_destroy(raw_node_ref_slab);
90 if(node_frag_slab)
91 kmem_cache_destroy(node_frag_slab);
92 if(inode_cache_slab)
93 kmem_cache_destroy(inode_cache_slab);
94}
95
96struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
97{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +010098 struct jffs2_full_dirent *ret;
99 ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100100 dbg_memalloc("%p\n", ret);
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100101 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104void jffs2_free_full_dirent(struct jffs2_full_dirent *x)
105{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100106 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 kfree(x);
108}
109
110struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
111{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100112 struct jffs2_full_dnode *ret;
113 ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100114 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return ret;
116}
117
118void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
119{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100120 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 kmem_cache_free(full_dnode_slab, x);
122}
123
124struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
125{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100126 struct jffs2_raw_dirent *ret;
127 ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100128 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return ret;
130}
131
132void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
133{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100134 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 kmem_cache_free(raw_dirent_slab, x);
136}
137
138struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
139{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100140 struct jffs2_raw_inode *ret;
141 ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100142 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return ret;
144}
145
146void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
147{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100148 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 kmem_cache_free(raw_inode_slab, x);
150}
151
152struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
153{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100154 struct jffs2_tmp_dnode_info *ret;
155 ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100156 dbg_memalloc("%p\n",
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100157 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return ret;
159}
160
161void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
162{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100163 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 kmem_cache_free(tmp_dnode_info_slab, x);
165}
166
167struct jffs2_raw_node_ref *jffs2_alloc_raw_node_ref(void)
168{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100169 struct jffs2_raw_node_ref *ret;
170 ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100171 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return ret;
173}
174
175void jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *x)
176{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100177 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 kmem_cache_free(raw_node_ref_slab, x);
179}
180
181struct jffs2_node_frag *jffs2_alloc_node_frag(void)
182{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100183 struct jffs2_node_frag *ret;
184 ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100185 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return ret;
187}
188
189void jffs2_free_node_frag(struct jffs2_node_frag *x)
190{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100191 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 kmem_cache_free(node_frag_slab, x);
193}
194
195struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
196{
Artem B. Bityutskiyf538c962005-07-27 15:16:57 +0100197 struct jffs2_inode_cache *ret;
198 ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100199 dbg_memalloc("%p\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return ret;
201}
202
203void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
204{
Artem B. Bityutskiy733802d2005-09-22 12:25:00 +0100205 dbg_memalloc("%p\n", x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 kmem_cache_free(inode_cache_slab, x);
207}