fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / jffs / jffs_fm.c
index 71cda0c..5a95fbd 100644 (file)
  *
  */
 #include <linux/slab.h>
+#include <linux/err.h>
 #include <linux/blkdev.h>
 #include <linux/jffs.h>
 #include "jffs_fm.h"
+#include "intrep.h"
 
 #if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
 static int jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset);
 #endif
 
-extern kmem_cache_t     *fm_cache;
-extern kmem_cache_t     *node_cache;
+static struct jffs_fm *jffs_alloc_fm(void);
+static void jffs_free_fm(struct jffs_fm *n);
+
+extern struct kmem_cache     *fm_cache;
+extern struct kmem_cache     *node_cache;
+
+#if CONFIG_JFFS_FS_VERBOSE > 0
+void
+jffs_print_fmcontrol(struct jffs_fmcontrol *fmc)
+{
+       D(printk("struct jffs_fmcontrol: 0x%p\n", fmc));
+       D(printk("{\n"));
+       D(printk("        %u, /* flash_size  */\n", fmc->flash_size));
+       D(printk("        %u, /* used_size  */\n", fmc->used_size));
+       D(printk("        %u, /* dirty_size  */\n", fmc->dirty_size));
+       D(printk("        %u, /* free_size  */\n", fmc->free_size));
+       D(printk("        %u, /* sector_size  */\n", fmc->sector_size));
+       D(printk("        %u, /* min_free_size  */\n", fmc->min_free_size));
+       D(printk("        %u, /* max_chunk_size  */\n", fmc->max_chunk_size));
+       D(printk("        0x%p, /* mtd  */\n", fmc->mtd));
+       D(printk("        0x%p, /* head  */    "
+                "(head->offset = 0x%08x)\n",
+                fmc->head, (fmc->head ? fmc->head->offset : 0)));
+       D(printk("        0x%p, /* tail  */    "
+                "(tail->offset + tail->size = 0x%08x)\n",
+                fmc->tail,
+                (fmc->tail ? fmc->tail->offset + fmc->tail->size : 0)));
+       D(printk("        0x%p, /* head_extra  */\n", fmc->head_extra));
+       D(printk("        0x%p, /* tail_extra  */\n", fmc->tail_extra));
+       D(printk("}\n"));
+}
+#endif  /*  CONFIG_JFFS_FS_VERBOSE > 0  */
+
+#if CONFIG_JFFS_FS_VERBOSE > 2
+static void
+jffs_print_fm(struct jffs_fm *fm)
+{
+       D(printk("struct jffs_fm: 0x%p\n", fm));
+       D(printk("{\n"));
+       D(printk("       0x%08x, /* offset  */\n", fm->offset));
+       D(printk("       %u, /* size  */\n", fm->size));
+       D(printk("       0x%p, /* prev  */\n", fm->prev));
+       D(printk("       0x%p, /* next  */\n", fm->next));
+       D(printk("       0x%p, /* nodes  */\n", fm->nodes));
+       D(printk("}\n"));
+}
+#endif  /*  CONFIG_JFFS_FS_VERBOSE > 2  */
+
+#if 0
+void
+jffs_print_node_ref(struct jffs_node_ref *ref)
+{
+       D(printk("struct jffs_node_ref: 0x%p\n", ref));
+       D(printk("{\n"));
+       D(printk("       0x%p, /* node  */\n", ref->node));
+       D(printk("       0x%p, /* next  */\n", ref->next));
+       D(printk("}\n"));
+}
+#endif  /*  0  */
 
 /* This function creates a new shiny flash memory control structure.  */
 struct jffs_fmcontrol *
@@ -36,8 +95,7 @@ jffs_build_begin(struct jffs_control *c, int unit)
        struct mtd_info *mtd;
        
        D3(printk("jffs_build_begin()\n"));
-       fmc = (struct jffs_fmcontrol *)kmalloc(sizeof(struct jffs_fmcontrol),
-                                              GFP_KERNEL);
+       fmc = kmalloc(sizeof(*fmc), GFP_KERNEL);
        if (!fmc) {
                D(printk("jffs_build_begin(): Allocation of "
                         "struct jffs_fmcontrol failed!\n"));
@@ -47,7 +105,7 @@ jffs_build_begin(struct jffs_control *c, int unit)
 
        mtd = get_mtd_device(NULL, unit);
 
-       if (!mtd) {
+       if (IS_ERR(mtd)) {
                kfree(fmc);
                DJM(no_jffs_fmcontrol--);
                return NULL;
@@ -77,11 +135,11 @@ jffs_build_begin(struct jffs_control *c, int unit)
        fmc->min_free_size = fmc->sector_size << 2;
        fmc->mtd = mtd;
        fmc->c = c;
-       fmc->head = 0;
-       fmc->tail = 0;
-       fmc->head_extra = 0;
-       fmc->tail_extra = 0;
-       init_MUTEX(&fmc->biglock);
+       fmc->head = NULL;
+       fmc->tail = NULL;
+       fmc->head_extra = NULL;
+       fmc->tail_extra = NULL;
+       mutex_init(&fmc->biglock);
        return fmc;
 }
 
@@ -102,8 +160,8 @@ jffs_build_end(struct jffs_fmcontrol *fmc)
                fmc->head->prev = fmc->tail_extra;
                fmc->head = fmc->head_extra;
        }
-       fmc->head_extra = 0; /* These two instructions should be omitted.  */
-       fmc->tail_extra = 0;
+       fmc->head_extra = NULL; /* These two instructions should be omitted.  */
+       fmc->tail_extra = NULL;
        D3(jffs_print_fmcontrol(fmc));
 }
 
@@ -114,10 +172,9 @@ void
 jffs_cleanup_fmcontrol(struct jffs_fmcontrol *fmc)
 {
        if (fmc) {
-               struct jffs_fm *cur;
                struct jffs_fm *next = fmc->head;
-
-               while ((cur = next)) {
+               while (next) {
+                       struct jffs_fm *cur = next;
                        next = next->next;
                        jffs_free_fm(cur);
                }
@@ -205,7 +262,7 @@ jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size, struct jffs_node *node,
        D2(printk("jffs_fmalloc(): fmc = 0x%p, size = %d, "
                  "node = 0x%p\n", fmc, size, node));
 
-       *result = 0;
+       *result = NULL;
 
        if (!(fm = jffs_alloc_fm())) {
                D(printk("jffs_fmalloc(): kmalloc() failed! (fm)\n"));
@@ -234,7 +291,7 @@ jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size, struct jffs_node *node,
                }
                DJM(no_jffs_node_ref++);
                fm->nodes->node = node;
-               fm->nodes->next = 0;
+               fm->nodes->next = NULL;
                if (fmc->tail) {
                        fm->offset = fmc->tail->offset + fmc->tail->size;
                        if (fm->offset == fmc->flash_size) {
@@ -264,7 +321,7 @@ jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size, struct jffs_node *node,
        else {
                fm->offset = fmc->tail->offset + fmc->tail->size;
                fm->size = free_chunk_size1;
-               fm->nodes = 0;
+               fm->nodes = NULL;
                fmc->free_size -= fm->size;
                fmc->dirty_size += fm->size; /* Changed by simonk. This seemingly fixes a 
                                                bug that caused infinite garbage collection.
@@ -273,9 +330,9 @@ jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size, struct jffs_node *node,
                                             */
        }
 
-       fm->next = 0;
+       fm->next = NULL;
        if (!fmc->head) {
-               fm->prev = 0;
+               fm->prev = NULL;
                fmc->head = fm;
                fmc->tail = fm;
        }
@@ -309,13 +366,13 @@ jffs_fmfree(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, struct jffs_node *no
        ASSERT(if (!fmc || !fm || !fm->nodes) {
                printk(KERN_ERR "jffs_fmfree(): fmc: 0x%p, fm: 0x%p, "
                       "fm->nodes: 0x%p\n",
-                      fmc, fm, (fm ? fm->nodes : 0));
+                      fmc, fm, (fm ? fm->nodes : NULL));
                return -1;
        });
 
        /* Find the reference to the node that is going to be removed
           and remove it.  */
-       for (ref = fm->nodes, prev = 0; ref; ref = ref->next) {
+       for (ref = fm->nodes, prev = NULL; ref; ref = ref->next) {
                if (ref->node == node) {
                        if (prev) {
                                prev->next = ref->next;
@@ -368,13 +425,13 @@ jffs_fmalloced(struct jffs_fmcontrol *fmc, __u32 offset, __u32 size,
        if (!(fm = jffs_alloc_fm())) {
                D(printk("jffs_fmalloced(0x%p, %u, %u, 0x%p): failed!\n",
                         fmc, offset, size, node));
-               return 0;
+               return NULL;
        }
        fm->offset = offset;
        fm->size = size;
-       fm->prev = 0;
-       fm->next = 0;
-       fm->nodes = 0;
+       fm->prev = NULL;
+       fm->next = NULL;
+       fm->nodes = NULL;
        if (node) {
                /* `node' exists and it should be associated with the
                    jffs_fm structure `fm'.  */
@@ -383,11 +440,11 @@ jffs_fmalloced(struct jffs_fmcontrol *fmc, __u32 offset, __u32 size,
                                          GFP_KERNEL))) {
                        D(printk("jffs_fmalloced(): !fm->nodes\n"));
                        jffs_free_fm(fm);
-                       return 0;
+                       return NULL;
                }
                DJM(no_jffs_node_ref++);
                fm->nodes->node = node;
-               fm->nodes->next = 0;
+               fm->nodes->next = NULL;
                fmc->used_size += size;
                fmc->free_size -= size;
        }
@@ -429,8 +486,7 @@ jffs_add_node(struct jffs_node *node)
 
        D3(printk("jffs_add_node(): ino = %u\n", node->ino));
 
-       ref = (struct jffs_node_ref *)kmalloc(sizeof(struct jffs_node_ref),
-                                             GFP_KERNEL);
+       ref = kmalloc(sizeof(*ref), GFP_KERNEL);
        if (!ref)
                return -ENOMEM;
 
@@ -454,7 +510,7 @@ jffs_fmfree_partly(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, __u32 size)
        if (fm->nodes) {
                kfree(fm->nodes);
                DJM(no_jffs_node_ref--);
-               fm->nodes = 0;
+               fm->nodes = NULL;
        }
        fmc->used_size -= fm->size;
        if (fm == fmc->tail) {
@@ -476,12 +532,12 @@ jffs_cut_node(struct jffs_fmcontrol *fmc, __u32 size)
        __u32 pos = 0;
 
        if (size == 0) {
-               return 0;
+               return NULL;
        }
 
        ASSERT(if (!fmc) {
                printk(KERN_ERR "jffs_cut_node(): fmc == NULL\n");
-               return 0;
+               return NULL;
        });
 
        fm = fmc->head;
@@ -495,7 +551,7 @@ jffs_cut_node(struct jffs_fmcontrol *fmc, __u32 size)
                        break;
                }
                else {
-                       fm = 0;
+                       fm = NULL;
                        break;
                }
        }
@@ -524,7 +580,7 @@ jffs_sync_erase(struct jffs_fmcontrol *fmc, int erased_size)
                        erased_size -= fm->size;
                        del = fm;
                        fm = fm->next;
-                       fm->prev = 0;
+                       fm->prev = NULL;
                        fmc->head = fm;
                        jffs_free_fm(del);
                }
@@ -543,17 +599,17 @@ jffs_get_oldest_node(struct jffs_fmcontrol *fmc)
 {
        struct jffs_fm *fm;
        struct jffs_node_ref *nref;
-       struct jffs_node *node = 0;
+       struct jffs_node *node = NULL;
 
        ASSERT(if (!fmc) {
                printk(KERN_ERR "jffs_get_oldest_node(): fmc == NULL\n");
-               return 0;
+               return NULL;
        });
 
        for (fm = fmc->head; fm && !fm->nodes; fm = fm->next);
 
        if (!fm) {
-               return 0;
+               return NULL;
        }
 
        /* The oldest node is the last one in the reference list.  This list
@@ -603,7 +659,7 @@ jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset)
 /* check if it's possible to erase the wanted range, and if not, return
  * the range that IS erasable, or a negative error code.
  */
-long
+static long
 jffs_flash_erasable_size(struct mtd_info *mtd, __u32 offset, __u32 size)
 {
          u_long ssize;
@@ -701,7 +757,7 @@ jffs_erasable_size(struct jffs_fmcontrol *fmc)
        return (ret >= 0 ? ret : 0);
 }
 
-struct jffs_fm *jffs_alloc_fm(void)
+static struct jffs_fm *jffs_alloc_fm(void)
 {
        struct jffs_fm *fm;
 
@@ -711,7 +767,7 @@ struct jffs_fm *jffs_alloc_fm(void)
        return fm;
 }
 
-void jffs_free_fm(struct jffs_fm *n)
+static void jffs_free_fm(struct jffs_fm *n)
 {
        kmem_cache_free(fm_cache,n);
        DJM(no_jffs_fm--);
@@ -740,51 +796,3 @@ int jffs_get_node_inuse(void)
 {
        return no_jffs_node;
 }
-
-void
-jffs_print_fmcontrol(struct jffs_fmcontrol *fmc)
-{
-       D(printk("struct jffs_fmcontrol: 0x%p\n", fmc));
-       D(printk("{\n"));
-       D(printk("        %u, /* flash_size  */\n", fmc->flash_size));
-       D(printk("        %u, /* used_size  */\n", fmc->used_size));
-       D(printk("        %u, /* dirty_size  */\n", fmc->dirty_size));
-       D(printk("        %u, /* free_size  */\n", fmc->free_size));
-       D(printk("        %u, /* sector_size  */\n", fmc->sector_size));
-       D(printk("        %u, /* min_free_size  */\n", fmc->min_free_size));
-       D(printk("        %u, /* max_chunk_size  */\n", fmc->max_chunk_size));
-       D(printk("        0x%p, /* mtd  */\n", fmc->mtd));
-       D(printk("        0x%p, /* head  */    "
-                "(head->offset = 0x%08x)\n",
-                fmc->head, (fmc->head ? fmc->head->offset : 0)));
-       D(printk("        0x%p, /* tail  */    "
-                "(tail->offset + tail->size = 0x%08x)\n",
-                fmc->tail,
-                (fmc->tail ? fmc->tail->offset + fmc->tail->size : 0)));
-       D(printk("        0x%p, /* head_extra  */\n", fmc->head_extra));
-       D(printk("        0x%p, /* tail_extra  */\n", fmc->tail_extra));
-       D(printk("}\n"));
-}
-
-void
-jffs_print_fm(struct jffs_fm *fm)
-{
-       D(printk("struct jffs_fm: 0x%p\n", fm));
-       D(printk("{\n"));
-       D(printk("       0x%08x, /* offset  */\n", fm->offset));
-       D(printk("       %u, /* size  */\n", fm->size));
-       D(printk("       0x%p, /* prev  */\n", fm->prev));
-       D(printk("       0x%p, /* next  */\n", fm->next));
-       D(printk("       0x%p, /* nodes  */\n", fm->nodes));
-       D(printk("}\n"));
-}
-
-void
-jffs_print_node_ref(struct jffs_node_ref *ref)
-{
-       D(printk("struct jffs_node_ref: 0x%p\n", ref));
-       D(printk("{\n"));
-       D(printk("       0x%p, /* node  */\n", ref->node));
-       D(printk("       0x%p, /* next  */\n", ref->next));
-       D(printk("}\n"));
-}