Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * JFFS2 -- Journalling Flash File System, Version 2.
0003  *
0004  * Copyright © 2001-2007 Red Hat, Inc.
0005  *
0006  * Created by David Woodhouse <dwmw2@infradead.org>
0007  *
0008  * For licensing information, see the file 'LICENCE' in this directory.
0009  *
0010  */
0011 
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 
0014 #include <linux/kernel.h>
0015 #include <linux/slab.h>
0016 #include <linux/init.h>
0017 #include <linux/jffs2.h>
0018 #include "nodelist.h"
0019 
0020 /* These are initialised to NULL in the kernel startup code.
0021    If you're porting to other operating systems, beware */
0022 static struct kmem_cache *full_dnode_slab;
0023 static struct kmem_cache *raw_dirent_slab;
0024 static struct kmem_cache *raw_inode_slab;
0025 static struct kmem_cache *tmp_dnode_info_slab;
0026 static struct kmem_cache *raw_node_ref_slab;
0027 static struct kmem_cache *node_frag_slab;
0028 static struct kmem_cache *inode_cache_slab;
0029 #ifdef CONFIG_JFFS2_FS_XATTR
0030 static struct kmem_cache *xattr_datum_cache;
0031 static struct kmem_cache *xattr_ref_cache;
0032 #endif
0033 
0034 int __init jffs2_create_slab_caches(void)
0035 {
0036     full_dnode_slab = kmem_cache_create("jffs2_full_dnode",
0037                         sizeof(struct jffs2_full_dnode),
0038                         0, 0, NULL);
0039     if (!full_dnode_slab)
0040         goto err;
0041 
0042     raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent",
0043                         sizeof(struct jffs2_raw_dirent),
0044                         0, SLAB_HWCACHE_ALIGN, NULL);
0045     if (!raw_dirent_slab)
0046         goto err;
0047 
0048     raw_inode_slab = kmem_cache_create("jffs2_raw_inode",
0049                        sizeof(struct jffs2_raw_inode),
0050                        0, SLAB_HWCACHE_ALIGN, NULL);
0051     if (!raw_inode_slab)
0052         goto err;
0053 
0054     tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode",
0055                         sizeof(struct jffs2_tmp_dnode_info),
0056                         0, 0, NULL);
0057     if (!tmp_dnode_info_slab)
0058         goto err;
0059 
0060     raw_node_ref_slab = kmem_cache_create("jffs2_refblock",
0061                           sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1),
0062                           0, 0, NULL);
0063     if (!raw_node_ref_slab)
0064         goto err;
0065 
0066     node_frag_slab = kmem_cache_create("jffs2_node_frag",
0067                        sizeof(struct jffs2_node_frag),
0068                        0, 0, NULL);
0069     if (!node_frag_slab)
0070         goto err;
0071 
0072     inode_cache_slab = kmem_cache_create("jffs2_inode_cache",
0073                          sizeof(struct jffs2_inode_cache),
0074                          0, 0, NULL);
0075     if (!inode_cache_slab)
0076         goto err;
0077 
0078 #ifdef CONFIG_JFFS2_FS_XATTR
0079     xattr_datum_cache = kmem_cache_create("jffs2_xattr_datum",
0080                          sizeof(struct jffs2_xattr_datum),
0081                          0, 0, NULL);
0082     if (!xattr_datum_cache)
0083         goto err;
0084 
0085     xattr_ref_cache = kmem_cache_create("jffs2_xattr_ref",
0086                        sizeof(struct jffs2_xattr_ref),
0087                        0, 0, NULL);
0088     if (!xattr_ref_cache)
0089         goto err;
0090 #endif
0091 
0092     return 0;
0093  err:
0094     jffs2_destroy_slab_caches();
0095     return -ENOMEM;
0096 }
0097 
0098 void jffs2_destroy_slab_caches(void)
0099 {
0100     kmem_cache_destroy(full_dnode_slab);
0101     kmem_cache_destroy(raw_dirent_slab);
0102     kmem_cache_destroy(raw_inode_slab);
0103     kmem_cache_destroy(tmp_dnode_info_slab);
0104     kmem_cache_destroy(raw_node_ref_slab);
0105     kmem_cache_destroy(node_frag_slab);
0106     kmem_cache_destroy(inode_cache_slab);
0107 #ifdef CONFIG_JFFS2_FS_XATTR
0108     kmem_cache_destroy(xattr_datum_cache);
0109     kmem_cache_destroy(xattr_ref_cache);
0110 #endif
0111 }
0112 
0113 struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
0114 {
0115     struct jffs2_full_dirent *ret;
0116     ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
0117     dbg_memalloc("%p\n", ret);
0118     return ret;
0119 }
0120 
0121 void jffs2_free_full_dirent(struct jffs2_full_dirent *x)
0122 {
0123     dbg_memalloc("%p\n", x);
0124     kfree(x);
0125 }
0126 
0127 struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
0128 {
0129     struct jffs2_full_dnode *ret;
0130     ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
0131     dbg_memalloc("%p\n", ret);
0132     return ret;
0133 }
0134 
0135 void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
0136 {
0137     dbg_memalloc("%p\n", x);
0138     kmem_cache_free(full_dnode_slab, x);
0139 }
0140 
0141 struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
0142 {
0143     struct jffs2_raw_dirent *ret;
0144     ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
0145     dbg_memalloc("%p\n", ret);
0146     return ret;
0147 }
0148 
0149 void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
0150 {
0151     dbg_memalloc("%p\n", x);
0152     kmem_cache_free(raw_dirent_slab, x);
0153 }
0154 
0155 struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
0156 {
0157     struct jffs2_raw_inode *ret;
0158     ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
0159     dbg_memalloc("%p\n", ret);
0160     return ret;
0161 }
0162 
0163 void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
0164 {
0165     dbg_memalloc("%p\n", x);
0166     kmem_cache_free(raw_inode_slab, x);
0167 }
0168 
0169 struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
0170 {
0171     struct jffs2_tmp_dnode_info *ret;
0172     ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
0173     dbg_memalloc("%p\n",
0174         ret);
0175     return ret;
0176 }
0177 
0178 void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
0179 {
0180     dbg_memalloc("%p\n", x);
0181     kmem_cache_free(tmp_dnode_info_slab, x);
0182 }
0183 
0184 static struct jffs2_raw_node_ref *jffs2_alloc_refblock(void)
0185 {
0186     struct jffs2_raw_node_ref *ret;
0187 
0188     ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
0189     if (ret) {
0190         int i = 0;
0191         for (i=0; i < REFS_PER_BLOCK; i++) {
0192             ret[i].flash_offset = REF_EMPTY_NODE;
0193             ret[i].next_in_ino = NULL;
0194         }
0195         ret[i].flash_offset = REF_LINK_NODE;
0196         ret[i].next_in_ino = NULL;
0197     }
0198     return ret;
0199 }
0200 
0201 int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c,
0202                  struct jffs2_eraseblock *jeb, int nr)
0203 {
0204     struct jffs2_raw_node_ref **p, *ref;
0205     int i = nr;
0206 
0207     dbg_memalloc("%d\n", nr);
0208 
0209     p = &jeb->last_node;
0210     ref = *p;
0211 
0212     dbg_memalloc("Reserving %d refs for block @0x%08x\n", nr, jeb->offset);
0213 
0214     /* If jeb->last_node is really a valid node then skip over it */
0215     if (ref && ref->flash_offset != REF_EMPTY_NODE)
0216         ref++;
0217 
0218     while (i) {
0219         if (!ref) {
0220             dbg_memalloc("Allocating new refblock linked from %p\n", p);
0221             ref = *p = jffs2_alloc_refblock();
0222             if (!ref)
0223                 return -ENOMEM;
0224         }
0225         if (ref->flash_offset == REF_LINK_NODE) {
0226             p = &ref->next_in_ino;
0227             ref = *p;
0228             continue;
0229         }
0230         i--;
0231         ref++;
0232     }
0233     jeb->allocated_refs = nr;
0234 
0235     dbg_memalloc("Reserved %d refs for block @0x%08x, last_node is %p (%08x,%p)\n",
0236           nr, jeb->offset, jeb->last_node, jeb->last_node->flash_offset,
0237           jeb->last_node->next_in_ino);
0238 
0239     return 0;
0240 }
0241 
0242 void jffs2_free_refblock(struct jffs2_raw_node_ref *x)
0243 {
0244     dbg_memalloc("%p\n", x);
0245     kmem_cache_free(raw_node_ref_slab, x);
0246 }
0247 
0248 struct jffs2_node_frag *jffs2_alloc_node_frag(void)
0249 {
0250     struct jffs2_node_frag *ret;
0251     ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
0252     dbg_memalloc("%p\n", ret);
0253     return ret;
0254 }
0255 
0256 void jffs2_free_node_frag(struct jffs2_node_frag *x)
0257 {
0258     dbg_memalloc("%p\n", x);
0259     kmem_cache_free(node_frag_slab, x);
0260 }
0261 
0262 struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
0263 {
0264     struct jffs2_inode_cache *ret;
0265     ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
0266     dbg_memalloc("%p\n", ret);
0267     return ret;
0268 }
0269 
0270 void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
0271 {
0272     dbg_memalloc("%p\n", x);
0273     kmem_cache_free(inode_cache_slab, x);
0274 }
0275 
0276 #ifdef CONFIG_JFFS2_FS_XATTR
0277 struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void)
0278 {
0279     struct jffs2_xattr_datum *xd;
0280     xd = kmem_cache_zalloc(xattr_datum_cache, GFP_KERNEL);
0281     dbg_memalloc("%p\n", xd);
0282     if (!xd)
0283         return NULL;
0284 
0285     xd->class = RAWNODE_CLASS_XATTR_DATUM;
0286     xd->node = (void *)xd;
0287     INIT_LIST_HEAD(&xd->xindex);
0288     return xd;
0289 }
0290 
0291 void jffs2_free_xattr_datum(struct jffs2_xattr_datum *xd)
0292 {
0293     dbg_memalloc("%p\n", xd);
0294     kmem_cache_free(xattr_datum_cache, xd);
0295 }
0296 
0297 struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void)
0298 {
0299     struct jffs2_xattr_ref *ref;
0300     ref = kmem_cache_zalloc(xattr_ref_cache, GFP_KERNEL);
0301     dbg_memalloc("%p\n", ref);
0302     if (!ref)
0303         return NULL;
0304 
0305     ref->class = RAWNODE_CLASS_XATTR_REF;
0306     ref->node = (void *)ref;
0307     return ref;
0308 }
0309 
0310 void jffs2_free_xattr_ref(struct jffs2_xattr_ref *ref)
0311 {
0312     dbg_memalloc("%p\n", ref);
0313     kmem_cache_free(xattr_ref_cache, ref);
0314 }
0315 #endif