18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * JFFS2 -- Journalling Flash File System, Version 2. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright © 2001-2007 Red Hat, Inc. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Created by David Woodhouse <dwmw2@infradead.org> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * For licensing information, see the file 'LICENCE' in this directory. 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include <linux/kernel.h> 158c2ecf20Sopenharmony_ci#include <linux/slab.h> 168c2ecf20Sopenharmony_ci#include <linux/init.h> 178c2ecf20Sopenharmony_ci#include <linux/jffs2.h> 188c2ecf20Sopenharmony_ci#include "nodelist.h" 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci/* These are initialised to NULL in the kernel startup code. 218c2ecf20Sopenharmony_ci If you're porting to other operating systems, beware */ 228c2ecf20Sopenharmony_cistatic struct kmem_cache *full_dnode_slab; 238c2ecf20Sopenharmony_cistatic struct kmem_cache *raw_dirent_slab; 248c2ecf20Sopenharmony_cistatic struct kmem_cache *raw_inode_slab; 258c2ecf20Sopenharmony_cistatic struct kmem_cache *tmp_dnode_info_slab; 268c2ecf20Sopenharmony_cistatic struct kmem_cache *raw_node_ref_slab; 278c2ecf20Sopenharmony_cistatic struct kmem_cache *node_frag_slab; 288c2ecf20Sopenharmony_cistatic struct kmem_cache *inode_cache_slab; 298c2ecf20Sopenharmony_ci#ifdef CONFIG_JFFS2_FS_XATTR 308c2ecf20Sopenharmony_cistatic struct kmem_cache *xattr_datum_cache; 318c2ecf20Sopenharmony_cistatic struct kmem_cache *xattr_ref_cache; 328c2ecf20Sopenharmony_ci#endif 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ciint __init jffs2_create_slab_caches(void) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci full_dnode_slab = kmem_cache_create("jffs2_full_dnode", 378c2ecf20Sopenharmony_ci sizeof(struct jffs2_full_dnode), 388c2ecf20Sopenharmony_ci 0, 0, NULL); 398c2ecf20Sopenharmony_ci if (!full_dnode_slab) 408c2ecf20Sopenharmony_ci goto err; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent", 438c2ecf20Sopenharmony_ci sizeof(struct jffs2_raw_dirent), 448c2ecf20Sopenharmony_ci 0, SLAB_HWCACHE_ALIGN, NULL); 458c2ecf20Sopenharmony_ci if (!raw_dirent_slab) 468c2ecf20Sopenharmony_ci goto err; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci raw_inode_slab = kmem_cache_create("jffs2_raw_inode", 498c2ecf20Sopenharmony_ci sizeof(struct jffs2_raw_inode), 508c2ecf20Sopenharmony_ci 0, SLAB_HWCACHE_ALIGN, NULL); 518c2ecf20Sopenharmony_ci if (!raw_inode_slab) 528c2ecf20Sopenharmony_ci goto err; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode", 558c2ecf20Sopenharmony_ci sizeof(struct jffs2_tmp_dnode_info), 568c2ecf20Sopenharmony_ci 0, 0, NULL); 578c2ecf20Sopenharmony_ci if (!tmp_dnode_info_slab) 588c2ecf20Sopenharmony_ci goto err; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci raw_node_ref_slab = kmem_cache_create("jffs2_refblock", 618c2ecf20Sopenharmony_ci sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1), 628c2ecf20Sopenharmony_ci 0, 0, NULL); 638c2ecf20Sopenharmony_ci if (!raw_node_ref_slab) 648c2ecf20Sopenharmony_ci goto err; 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci node_frag_slab = kmem_cache_create("jffs2_node_frag", 678c2ecf20Sopenharmony_ci sizeof(struct jffs2_node_frag), 688c2ecf20Sopenharmony_ci 0, 0, NULL); 698c2ecf20Sopenharmony_ci if (!node_frag_slab) 708c2ecf20Sopenharmony_ci goto err; 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci inode_cache_slab = kmem_cache_create("jffs2_inode_cache", 738c2ecf20Sopenharmony_ci sizeof(struct jffs2_inode_cache), 748c2ecf20Sopenharmony_ci 0, 0, NULL); 758c2ecf20Sopenharmony_ci if (!inode_cache_slab) 768c2ecf20Sopenharmony_ci goto err; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci#ifdef CONFIG_JFFS2_FS_XATTR 798c2ecf20Sopenharmony_ci xattr_datum_cache = kmem_cache_create("jffs2_xattr_datum", 808c2ecf20Sopenharmony_ci sizeof(struct jffs2_xattr_datum), 818c2ecf20Sopenharmony_ci 0, 0, NULL); 828c2ecf20Sopenharmony_ci if (!xattr_datum_cache) 838c2ecf20Sopenharmony_ci goto err; 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci xattr_ref_cache = kmem_cache_create("jffs2_xattr_ref", 868c2ecf20Sopenharmony_ci sizeof(struct jffs2_xattr_ref), 878c2ecf20Sopenharmony_ci 0, 0, NULL); 888c2ecf20Sopenharmony_ci if (!xattr_ref_cache) 898c2ecf20Sopenharmony_ci goto err; 908c2ecf20Sopenharmony_ci#endif 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci return 0; 938c2ecf20Sopenharmony_ci err: 948c2ecf20Sopenharmony_ci jffs2_destroy_slab_caches(); 958c2ecf20Sopenharmony_ci return -ENOMEM; 968c2ecf20Sopenharmony_ci} 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_civoid jffs2_destroy_slab_caches(void) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci kmem_cache_destroy(full_dnode_slab); 1018c2ecf20Sopenharmony_ci kmem_cache_destroy(raw_dirent_slab); 1028c2ecf20Sopenharmony_ci kmem_cache_destroy(raw_inode_slab); 1038c2ecf20Sopenharmony_ci kmem_cache_destroy(tmp_dnode_info_slab); 1048c2ecf20Sopenharmony_ci kmem_cache_destroy(raw_node_ref_slab); 1058c2ecf20Sopenharmony_ci kmem_cache_destroy(node_frag_slab); 1068c2ecf20Sopenharmony_ci kmem_cache_destroy(inode_cache_slab); 1078c2ecf20Sopenharmony_ci#ifdef CONFIG_JFFS2_FS_XATTR 1088c2ecf20Sopenharmony_ci kmem_cache_destroy(xattr_datum_cache); 1098c2ecf20Sopenharmony_ci kmem_cache_destroy(xattr_ref_cache); 1108c2ecf20Sopenharmony_ci#endif 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cistruct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci struct jffs2_full_dirent *ret; 1168c2ecf20Sopenharmony_ci ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL); 1178c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", ret); 1188c2ecf20Sopenharmony_ci return ret; 1198c2ecf20Sopenharmony_ci} 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_civoid jffs2_free_full_dirent(struct jffs2_full_dirent *x) 1228c2ecf20Sopenharmony_ci{ 1238c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", x); 1248c2ecf20Sopenharmony_ci kfree(x); 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_cistruct jffs2_full_dnode *jffs2_alloc_full_dnode(void) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci struct jffs2_full_dnode *ret; 1308c2ecf20Sopenharmony_ci ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL); 1318c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", ret); 1328c2ecf20Sopenharmony_ci return ret; 1338c2ecf20Sopenharmony_ci} 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_civoid jffs2_free_full_dnode(struct jffs2_full_dnode *x) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", x); 1388c2ecf20Sopenharmony_ci kmem_cache_free(full_dnode_slab, x); 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistruct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void) 1428c2ecf20Sopenharmony_ci{ 1438c2ecf20Sopenharmony_ci struct jffs2_raw_dirent *ret; 1448c2ecf20Sopenharmony_ci ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL); 1458c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", ret); 1468c2ecf20Sopenharmony_ci return ret; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_civoid jffs2_free_raw_dirent(struct jffs2_raw_dirent *x) 1508c2ecf20Sopenharmony_ci{ 1518c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", x); 1528c2ecf20Sopenharmony_ci kmem_cache_free(raw_dirent_slab, x); 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cistruct jffs2_raw_inode *jffs2_alloc_raw_inode(void) 1568c2ecf20Sopenharmony_ci{ 1578c2ecf20Sopenharmony_ci struct jffs2_raw_inode *ret; 1588c2ecf20Sopenharmony_ci ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL); 1598c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", ret); 1608c2ecf20Sopenharmony_ci return ret; 1618c2ecf20Sopenharmony_ci} 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_civoid jffs2_free_raw_inode(struct jffs2_raw_inode *x) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", x); 1668c2ecf20Sopenharmony_ci kmem_cache_free(raw_inode_slab, x); 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_cistruct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci struct jffs2_tmp_dnode_info *ret; 1728c2ecf20Sopenharmony_ci ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL); 1738c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", 1748c2ecf20Sopenharmony_ci ret); 1758c2ecf20Sopenharmony_ci return ret; 1768c2ecf20Sopenharmony_ci} 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_civoid jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x) 1798c2ecf20Sopenharmony_ci{ 1808c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", x); 1818c2ecf20Sopenharmony_ci kmem_cache_free(tmp_dnode_info_slab, x); 1828c2ecf20Sopenharmony_ci} 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_cistatic struct jffs2_raw_node_ref *jffs2_alloc_refblock(void) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci struct jffs2_raw_node_ref *ret; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL); 1898c2ecf20Sopenharmony_ci if (ret) { 1908c2ecf20Sopenharmony_ci int i = 0; 1918c2ecf20Sopenharmony_ci for (i=0; i < REFS_PER_BLOCK; i++) { 1928c2ecf20Sopenharmony_ci ret[i].flash_offset = REF_EMPTY_NODE; 1938c2ecf20Sopenharmony_ci ret[i].next_in_ino = NULL; 1948c2ecf20Sopenharmony_ci } 1958c2ecf20Sopenharmony_ci ret[i].flash_offset = REF_LINK_NODE; 1968c2ecf20Sopenharmony_ci ret[i].next_in_ino = NULL; 1978c2ecf20Sopenharmony_ci } 1988c2ecf20Sopenharmony_ci return ret; 1998c2ecf20Sopenharmony_ci} 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ciint jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c, 2028c2ecf20Sopenharmony_ci struct jffs2_eraseblock *jeb, int nr) 2038c2ecf20Sopenharmony_ci{ 2048c2ecf20Sopenharmony_ci struct jffs2_raw_node_ref **p, *ref; 2058c2ecf20Sopenharmony_ci int i = nr; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci dbg_memalloc("%d\n", nr); 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci p = &jeb->last_node; 2108c2ecf20Sopenharmony_ci ref = *p; 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci dbg_memalloc("Reserving %d refs for block @0x%08x\n", nr, jeb->offset); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci /* If jeb->last_node is really a valid node then skip over it */ 2158c2ecf20Sopenharmony_ci if (ref && ref->flash_offset != REF_EMPTY_NODE) 2168c2ecf20Sopenharmony_ci ref++; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci while (i) { 2198c2ecf20Sopenharmony_ci if (!ref) { 2208c2ecf20Sopenharmony_ci dbg_memalloc("Allocating new refblock linked from %p\n", p); 2218c2ecf20Sopenharmony_ci ref = *p = jffs2_alloc_refblock(); 2228c2ecf20Sopenharmony_ci if (!ref) 2238c2ecf20Sopenharmony_ci return -ENOMEM; 2248c2ecf20Sopenharmony_ci } 2258c2ecf20Sopenharmony_ci if (ref->flash_offset == REF_LINK_NODE) { 2268c2ecf20Sopenharmony_ci p = &ref->next_in_ino; 2278c2ecf20Sopenharmony_ci ref = *p; 2288c2ecf20Sopenharmony_ci continue; 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci i--; 2318c2ecf20Sopenharmony_ci ref++; 2328c2ecf20Sopenharmony_ci } 2338c2ecf20Sopenharmony_ci jeb->allocated_refs = nr; 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci dbg_memalloc("Reserved %d refs for block @0x%08x, last_node is %p (%08x,%p)\n", 2368c2ecf20Sopenharmony_ci nr, jeb->offset, jeb->last_node, jeb->last_node->flash_offset, 2378c2ecf20Sopenharmony_ci jeb->last_node->next_in_ino); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci return 0; 2408c2ecf20Sopenharmony_ci} 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_civoid jffs2_free_refblock(struct jffs2_raw_node_ref *x) 2438c2ecf20Sopenharmony_ci{ 2448c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", x); 2458c2ecf20Sopenharmony_ci kmem_cache_free(raw_node_ref_slab, x); 2468c2ecf20Sopenharmony_ci} 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_cistruct jffs2_node_frag *jffs2_alloc_node_frag(void) 2498c2ecf20Sopenharmony_ci{ 2508c2ecf20Sopenharmony_ci struct jffs2_node_frag *ret; 2518c2ecf20Sopenharmony_ci ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL); 2528c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", ret); 2538c2ecf20Sopenharmony_ci return ret; 2548c2ecf20Sopenharmony_ci} 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_civoid jffs2_free_node_frag(struct jffs2_node_frag *x) 2578c2ecf20Sopenharmony_ci{ 2588c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", x); 2598c2ecf20Sopenharmony_ci kmem_cache_free(node_frag_slab, x); 2608c2ecf20Sopenharmony_ci} 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_cistruct jffs2_inode_cache *jffs2_alloc_inode_cache(void) 2638c2ecf20Sopenharmony_ci{ 2648c2ecf20Sopenharmony_ci struct jffs2_inode_cache *ret; 2658c2ecf20Sopenharmony_ci ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL); 2668c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", ret); 2678c2ecf20Sopenharmony_ci return ret; 2688c2ecf20Sopenharmony_ci} 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_civoid jffs2_free_inode_cache(struct jffs2_inode_cache *x) 2718c2ecf20Sopenharmony_ci{ 2728c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", x); 2738c2ecf20Sopenharmony_ci kmem_cache_free(inode_cache_slab, x); 2748c2ecf20Sopenharmony_ci} 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci#ifdef CONFIG_JFFS2_FS_XATTR 2778c2ecf20Sopenharmony_cistruct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void) 2788c2ecf20Sopenharmony_ci{ 2798c2ecf20Sopenharmony_ci struct jffs2_xattr_datum *xd; 2808c2ecf20Sopenharmony_ci xd = kmem_cache_zalloc(xattr_datum_cache, GFP_KERNEL); 2818c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", xd); 2828c2ecf20Sopenharmony_ci if (!xd) 2838c2ecf20Sopenharmony_ci return NULL; 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci xd->class = RAWNODE_CLASS_XATTR_DATUM; 2868c2ecf20Sopenharmony_ci xd->node = (void *)xd; 2878c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&xd->xindex); 2888c2ecf20Sopenharmony_ci return xd; 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_civoid jffs2_free_xattr_datum(struct jffs2_xattr_datum *xd) 2928c2ecf20Sopenharmony_ci{ 2938c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", xd); 2948c2ecf20Sopenharmony_ci kmem_cache_free(xattr_datum_cache, xd); 2958c2ecf20Sopenharmony_ci} 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_cistruct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void) 2988c2ecf20Sopenharmony_ci{ 2998c2ecf20Sopenharmony_ci struct jffs2_xattr_ref *ref; 3008c2ecf20Sopenharmony_ci ref = kmem_cache_zalloc(xattr_ref_cache, GFP_KERNEL); 3018c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", ref); 3028c2ecf20Sopenharmony_ci if (!ref) 3038c2ecf20Sopenharmony_ci return NULL; 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci ref->class = RAWNODE_CLASS_XATTR_REF; 3068c2ecf20Sopenharmony_ci ref->node = (void *)ref; 3078c2ecf20Sopenharmony_ci return ref; 3088c2ecf20Sopenharmony_ci} 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_civoid jffs2_free_xattr_ref(struct jffs2_xattr_ref *ref) 3118c2ecf20Sopenharmony_ci{ 3128c2ecf20Sopenharmony_ci dbg_memalloc("%p\n", ref); 3138c2ecf20Sopenharmony_ci kmem_cache_free(xattr_ref_cache, ref); 3148c2ecf20Sopenharmony_ci} 3158c2ecf20Sopenharmony_ci#endif 316