Merge branch 'bugfixes' of git://git.linux-nfs.org/projects/trondmy/nfs-2.6
[safe/jmp/linux-2.6] / fs / jffs2 / malloc.c
index 3df3250..c082868 100644 (file)
@@ -1,14 +1,12 @@
 /*
  * JFFS2 -- Journalling Flash File System, Version 2.
  *
- * Copyright (C) 2001-2003 Red Hat, Inc.
+ * Copyright © 2001-2007 Red Hat, Inc.
  *
  * Created by David Woodhouse <dwmw2@infradead.org>
  *
  * For licensing information, see the file 'LICENCE' in this directory.
  *
- * $Id: malloc.c,v 1.31 2005/11/07 11:14:40 gleixner Exp $
- *
  */
 
 #include <linux/kernel.h>
 
 /* These are initialised to NULL in the kernel startup code.
    If you're porting to other operating systems, beware */
-static kmem_cache_t *full_dnode_slab;
-static kmem_cache_t *raw_dirent_slab;
-static kmem_cache_t *raw_inode_slab;
-static kmem_cache_t *tmp_dnode_info_slab;
-static kmem_cache_t *raw_node_ref_slab;
-static kmem_cache_t *node_frag_slab;
-static kmem_cache_t *inode_cache_slab;
+static struct kmem_cache *full_dnode_slab;
+static struct kmem_cache *raw_dirent_slab;
+static struct kmem_cache *raw_inode_slab;
+static struct kmem_cache *tmp_dnode_info_slab;
+static struct kmem_cache *raw_node_ref_slab;
+static struct kmem_cache *node_frag_slab;
+static struct kmem_cache *inode_cache_slab;
 #ifdef CONFIG_JFFS2_FS_XATTR
-static kmem_cache_t *xattr_datum_cache;
-static kmem_cache_t *xattr_ref_cache;
+static struct kmem_cache *xattr_datum_cache;
+static struct kmem_cache *xattr_ref_cache;
 #endif
 
 int __init jffs2_create_slab_caches(void)
 {
        full_dnode_slab = kmem_cache_create("jffs2_full_dnode",
                                            sizeof(struct jffs2_full_dnode),
-                                           0, 0, NULL, NULL);
+                                           0, 0, NULL);
        if (!full_dnode_slab)
                goto err;
 
        raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent",
                                            sizeof(struct jffs2_raw_dirent),
-                                           0, 0, NULL, NULL);
+                                           0, SLAB_HWCACHE_ALIGN, NULL);
        if (!raw_dirent_slab)
                goto err;
 
        raw_inode_slab = kmem_cache_create("jffs2_raw_inode",
                                           sizeof(struct jffs2_raw_inode),
-                                          0, 0, NULL, NULL);
+                                          0, SLAB_HWCACHE_ALIGN, NULL);
        if (!raw_inode_slab)
                goto err;
 
        tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode",
                                                sizeof(struct jffs2_tmp_dnode_info),
-                                               0, 0, NULL, NULL);
+                                               0, 0, NULL);
        if (!tmp_dnode_info_slab)
                goto err;
 
-       raw_node_ref_slab = kmem_cache_create("jffs2_raw_node_ref",
-                                             sizeof(struct jffs2_raw_node_ref),
-                                             0, 0, NULL, NULL);
+       raw_node_ref_slab = kmem_cache_create("jffs2_refblock",
+                                             sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1),
+                                             0, 0, NULL);
        if (!raw_node_ref_slab)
                goto err;
 
        node_frag_slab = kmem_cache_create("jffs2_node_frag",
                                           sizeof(struct jffs2_node_frag),
-                                          0, 0, NULL, NULL);
+                                          0, 0, NULL);
        if (!node_frag_slab)
                goto err;
 
        inode_cache_slab = kmem_cache_create("jffs2_inode_cache",
                                             sizeof(struct jffs2_inode_cache),
-                                            0, 0, NULL, NULL);
+                                            0, 0, NULL);
        if (!inode_cache_slab)
                goto err;
 
 #ifdef CONFIG_JFFS2_FS_XATTR
        xattr_datum_cache = kmem_cache_create("jffs2_xattr_datum",
                                             sizeof(struct jffs2_xattr_datum),
-                                            0, 0, NULL, NULL);
+                                            0, 0, NULL);
        if (!xattr_datum_cache)
                goto err;
 
        xattr_ref_cache = kmem_cache_create("jffs2_xattr_ref",
                                           sizeof(struct jffs2_xattr_ref),
-                                          0, 0, NULL, NULL);
+                                          0, 0, NULL);
        if (!xattr_ref_cache)
                goto err;
 #endif
@@ -190,37 +188,65 @@ void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
        kmem_cache_free(tmp_dnode_info_slab, x);
 }
 
-int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c, int nr)
+static struct jffs2_raw_node_ref *jffs2_alloc_refblock(void)
 {
-       struct jffs2_raw_node_ref *p = c->refs;
-
-       dbg_memalloc("%d\n", nr);
+       struct jffs2_raw_node_ref *ret;
 
-       while (nr && p) {
-               p = p->next_in_ino;
-               nr--;
-       }
-       while (nr) {
-               p = __jffs2_alloc_raw_node_ref();
-               if (!p)
-                       return -ENOMEM;
-               p->next_in_ino = c->refs;
-               c->refs = p;
-               nr--;
+       ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
+       if (ret) {
+               int i = 0;
+               for (i=0; i < REFS_PER_BLOCK; i++) {
+                       ret[i].flash_offset = REF_EMPTY_NODE;
+                       ret[i].next_in_ino = NULL;
+               }
+               ret[i].flash_offset = REF_LINK_NODE;
+               ret[i].next_in_ino = NULL;
        }
-       c->reserved_refs = nr;
-       return 0;
+       return ret;
 }
 
-struct jffs2_raw_node_ref *__jffs2_alloc_raw_node_ref(void)
+int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c,
+                                struct jffs2_eraseblock *jeb, int nr)
 {
-       struct jffs2_raw_node_ref *ret;
-       ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
-       dbg_memalloc("%p\n", ret);
-       return ret;
+       struct jffs2_raw_node_ref **p, *ref;
+       int i = nr;
+
+       dbg_memalloc("%d\n", nr);
+
+       p = &jeb->last_node;
+       ref = *p;
+
+       dbg_memalloc("Reserving %d refs for block @0x%08x\n", nr, jeb->offset);
+
+       /* If jeb->last_node is really a valid node then skip over it */
+       if (ref && ref->flash_offset != REF_EMPTY_NODE)
+               ref++;
+
+       while (i) {
+               if (!ref) {
+                       dbg_memalloc("Allocating new refblock linked from %p\n", p);
+                       ref = *p = jffs2_alloc_refblock();
+                       if (!ref)
+                               return -ENOMEM;
+               }
+               if (ref->flash_offset == REF_LINK_NODE) {
+                       p = &ref->next_in_ino;
+                       ref = *p;
+                       continue;
+               }
+               i--;
+               ref++;
+       }
+       jeb->allocated_refs = nr;
+
+       dbg_memalloc("Reserved %d refs for block @0x%08x, last_node is %p (%08x,%p)\n",
+                 nr, jeb->offset, jeb->last_node, jeb->last_node->flash_offset,
+                 jeb->last_node->next_in_ino);
+
+       return 0;
 }
 
-void __jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *x)
+void jffs2_free_refblock(struct jffs2_raw_node_ref *x)
 {
        dbg_memalloc("%p\n", x);
        kmem_cache_free(raw_node_ref_slab, x);
@@ -258,11 +284,11 @@ void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
 struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void)
 {
        struct jffs2_xattr_datum *xd;
-       xd = kmem_cache_alloc(xattr_datum_cache, GFP_KERNEL);
+       xd = kmem_cache_zalloc(xattr_datum_cache, GFP_KERNEL);
        dbg_memalloc("%p\n", xd);
 
-       memset(xd, 0, sizeof(struct jffs2_xattr_datum));
        xd->class = RAWNODE_CLASS_XATTR_DATUM;
+       xd->node = (void *)xd;
        INIT_LIST_HEAD(&xd->xindex);
        return xd;
 }
@@ -276,11 +302,11 @@ void jffs2_free_xattr_datum(struct jffs2_xattr_datum *xd)
 struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void)
 {
        struct jffs2_xattr_ref *ref;
-       ref = kmem_cache_alloc(xattr_ref_cache, GFP_KERNEL);
+       ref = kmem_cache_zalloc(xattr_ref_cache, GFP_KERNEL);
        dbg_memalloc("%p\n", ref);
 
-       memset(ref, 0, sizeof(struct jffs2_xattr_ref));
        ref->class = RAWNODE_CLASS_XATTR_REF;
+       ref->node = (void *)ref;
        return ref;
 }