Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / fs / jffs2 / gc.c
index 87ec74f..daff334 100644 (file)
@@ -7,7 +7,7 @@
  *
  * For licensing information, see the file 'LICENCE' in this directory.
  *
- * $Id: gc.c,v 1.144 2004/12/21 11:18:50 dwmw2 Exp $
+ * $Id: gc.c,v 1.155 2005/11/07 11:14:39 gleixner Exp $
  *
  */
 
 #include "nodelist.h"
 #include "compr.h"
 
-static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c, 
+static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
                                          struct jffs2_inode_cache *ic,
                                          struct jffs2_raw_node_ref *raw);
-static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
                                        struct jffs2_inode_info *f, struct jffs2_full_dnode *fd);
-static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
                                        struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
-static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
                                        struct jffs2_inode_info *f, struct jffs2_full_dirent *fd);
 static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
                                      struct jffs2_inode_info *f, struct jffs2_full_dnode *fn,
@@ -50,11 +50,12 @@ static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
           put the clever wear-levelling algorithms. Eventually.  */
        /* We possibly want to favour the dirtier blocks more when the
           number of free blocks is low. */
+again:
        if (!list_empty(&c->bad_used_list) && c->nr_free_blocks > c->resv_blocks_gcbad) {
                D1(printk(KERN_DEBUG "Picking block from bad_used_list to GC next\n"));
                nextlist = &c->bad_used_list;
        } else if (n < 50 && !list_empty(&c->erasable_list)) {
-               /* Note that most of them will have gone directly to be erased. 
+               /* Note that most of them will have gone directly to be erased.
                   So don't favour the erasable_list _too_ much. */
                D1(printk(KERN_DEBUG "Picking block from erasable_list to GC next\n"));
                nextlist = &c->erasable_list;
@@ -79,6 +80,13 @@ static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
                D1(printk(KERN_DEBUG "Picking block from erasable_list to GC next (clean_list and {very_,}dirty_list were empty)\n"));
 
                nextlist = &c->erasable_list;
+       } else if (!list_empty(&c->erasable_pending_wbuf_list)) {
+               /* There are blocks are wating for the wbuf sync */
+               D1(printk(KERN_DEBUG "Synching wbuf in order to reuse erasable_pending_wbuf_list blocks\n"));
+               spin_unlock(&c->erase_completion_lock);
+               jffs2_flush_wbuf_pad(c);
+               spin_lock(&c->erase_completion_lock);
+               goto again;
        } else {
                /* Eep. All were empty */
                D1(printk(KERN_NOTICE "jffs2: No clean, dirty _or_ erasable blocks to GC from! Where are they all?\n"));
@@ -93,7 +101,7 @@ static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
                printk(KERN_WARNING "Eep. ret->gc_node for block at 0x%08x is NULL\n", ret->offset);
                BUG();
        }
-       
+
        /* Have we accidentally picked a clean block with wasted space ? */
        if (ret->wasted_size) {
                D1(printk(KERN_DEBUG "Converting wasted_size %08x to dirty_size\n", ret->wasted_size));
@@ -103,7 +111,6 @@ static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
                ret->wasted_size = 0;
        }
 
-       D2(jffs2_dump_block_lists(c));
        return ret;
 }
 
@@ -118,6 +125,7 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
        struct jffs2_eraseblock *jeb;
        struct jffs2_raw_node_ref *raw;
        int ret = 0, inum, nlink;
+       int xattr = 0;
 
        if (down_interruptible(&c->alloc_sem))
                return -EINTR;
@@ -129,18 +137,21 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
 
                /* We can't start doing GC yet. We haven't finished checking
                   the node CRCs etc. Do it now. */
-               
+
                /* checked_ino is protected by the alloc_sem */
-               if (c->checked_ino > c->highest_ino) {
+               if (c->checked_ino > c->highest_ino && xattr) {
                        printk(KERN_CRIT "Checked all inodes but still 0x%x bytes of unchecked space?\n",
                               c->unchecked_size);
-                       D2(jffs2_dump_block_lists(c));
+                       jffs2_dbg_dump_block_lists_nolock(c);
                        spin_unlock(&c->erase_completion_lock);
                        BUG();
                }
 
                spin_unlock(&c->erase_completion_lock);
 
+               if (!xattr)
+                       xattr = jffs2_verify_xattr(c);
+
                spin_lock(&c->inocache_lock);
 
                ic = jffs2_get_ino_cache(c, c->checked_ino++);
@@ -154,6 +165,7 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
                        D1(printk(KERN_DEBUG "Skipping check of ino #%d with nlink zero\n",
                                  ic->ino));
                        spin_unlock(&c->inocache_lock);
+                       jffs2_xattr_delete_inode(c, ic);
                        continue;
                }
                switch(ic->state) {
@@ -171,9 +183,13 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
 
                case INO_STATE_READING:
                        /* We need to wait for it to finish, lest we move on
-                          and trigger the BUG() above while we haven't yet 
+                          and trigger the BUG() above while we haven't yet
                           finished checking all its nodes */
                        D1(printk(KERN_DEBUG "Waiting for ino #%u to finish reading\n", ic->ino));
+                       /* We need to come back again for the _same_ inode. We've
+                        made no progress in this case, but that should be OK */
+                       c->checked_ino--;
+
                        up(&c->alloc_sem);
                        sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
                        return 0;
@@ -221,13 +237,13 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
        }
 
        raw = jeb->gc_node;
-                       
+
        while(ref_obsolete(raw)) {
                D1(printk(KERN_DEBUG "Node at 0x%08x is obsolete... skipping\n", ref_offset(raw)));
-               raw = raw->next_phys;
+               raw = ref_next(raw);
                if (unlikely(!raw)) {
                        printk(KERN_WARNING "eep. End of raw list while still supposedly nodes to GC\n");
-                       printk(KERN_WARNING "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n", 
+                       printk(KERN_WARNING "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n",
                               jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size);
                        jeb->gc_node = raw;
                        spin_unlock(&c->erase_completion_lock);
@@ -241,18 +257,38 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
 
        if (!raw->next_in_ino) {
                /* Inode-less node. Clean marker, snapshot or something like that */
-               /* FIXME: If it's something that needs to be copied, including something
-                  we don't grok that has JFFS2_NODETYPE_RWCOMPAT_COPY, we should do so */
                spin_unlock(&c->erase_completion_lock);
-               jffs2_mark_node_obsolete(c, raw);
+               if (ref_flags(raw) == REF_PRISTINE) {
+                       /* It's an unknown node with JFFS2_FEATURE_RWCOMPAT_COPY */
+                       jffs2_garbage_collect_pristine(c, NULL, raw);
+               } else {
+                       /* Just mark it obsolete */
+                       jffs2_mark_node_obsolete(c, raw);
+               }
                up(&c->alloc_sem);
                goto eraseit_lock;
        }
 
        ic = jffs2_raw_ref_to_ic(raw);
 
+#ifdef CONFIG_JFFS2_FS_XATTR
+       /* When 'ic' refers xattr_datum/xattr_ref, this node is GCed as xattr.
+        * We can decide whether this node is inode or xattr by ic->class.     */
+       if (ic->class == RAWNODE_CLASS_XATTR_DATUM
+           || ic->class == RAWNODE_CLASS_XATTR_REF) {
+               spin_unlock(&c->erase_completion_lock);
+
+               if (ic->class == RAWNODE_CLASS_XATTR_DATUM) {
+                       ret = jffs2_garbage_collect_xattr_datum(c, (struct jffs2_xattr_datum *)ic, raw);
+               } else {
+                       ret = jffs2_garbage_collect_xattr_ref(c, (struct jffs2_xattr_ref *)ic, raw);
+               }
+               goto release_sem;
+       }
+#endif
+
        /* We need to hold the inocache. Either the erase_completion_lock or
-          the inocache_lock are sufficient; we trade down since the inocache_lock 
+          the inocache_lock are sufficient; we trade down since the inocache_lock
           causes less contention. */
        spin_lock(&c->inocache_lock);
 
@@ -271,14 +307,14 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
 
        switch(ic->state) {
        case INO_STATE_CHECKEDABSENT:
-               /* It's been checked, but it's not currently in-core. 
+               /* It's been checked, but it's not currently in-core.
                   We can just copy any pristine nodes, but have
                   to prevent anyone else from doing read_inode() while
                   we're at it, so we set the state accordingly */
                if (ref_flags(raw) == REF_PRISTINE)
                        ic->state = INO_STATE_GC;
                else {
-                       D1(printk(KERN_DEBUG "Ino #%u is absent but node not REF_PRISTINE. Reading.\n", 
+                       D1(printk(KERN_DEBUG "Ino #%u is absent but node not REF_PRISTINE. Reading.\n",
                                  ic->ino));
                }
                break;
@@ -291,8 +327,8 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
        case INO_STATE_CHECKING:
        case INO_STATE_GC:
                /* Should never happen. We should have finished checking
-                  by the time we actually start doing any GC, and since 
-                  we're holding the alloc_sem, no other garbage collection 
+                  by the time we actually start doing any GC, and since
+                  we're holding the alloc_sem, no other garbage collection
                   can happen.
                */
                printk(KERN_CRIT "Inode #%u already in state %d in jffs2_garbage_collect_pass()!\n",
@@ -312,21 +348,21 @@ int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
                D1(printk(KERN_DEBUG "jffs2_garbage_collect_pass() waiting for ino #%u in state %d\n",
                          ic->ino, ic->state));
                sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
-               /* And because we dropped the alloc_sem we must start again from the 
+               /* And because we dropped the alloc_sem we must start again from the
                   beginning. Ponder chance of livelock here -- we're returning success
                   without actually making any progress.
 
-                  Q: What are the chances that the inode is back in INO_STATE_READING 
+                  Q: What are the chances that the inode is back in INO_STATE_READING
                   again by the time we next enter this function? And that this happens
                   enough times to cause a real delay?
 
-                  A: Small enough that I don't care :) 
+                  A: Small enough that I don't care :)
                */
                return 0;
        }
 
        /* OK. Now if the inode is in state INO_STATE_GC, we are going to copy the
-          node intact, and we don't have to muck about with the fragtree etc. 
+          node intact, and we don't have to muck about with the fragtree etc.
           because we know it's not in-core. If it _was_ in-core, we go through
           all the iget() crap anyway */
 
@@ -446,7 +482,7 @@ static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_era
                        if (!ret) {
                                /* Urgh. Return it sensibly. */
                                frag->node->raw = f->inocache->nodes;
-                       }       
+                       }
                        if (ret != -EBADFD)
                                goto upnout;
                }
@@ -460,7 +496,7 @@ static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_era
                }
                goto upnout;
        }
-       
+
        /* Wasn't a dnode. Try dirent */
        for (fd = f->dents; fd; fd=fd->next) {
                if (fd->raw == raw)
@@ -477,7 +513,8 @@ static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_era
                if (ref_obsolete(raw)) {
                        printk(KERN_WARNING "But it's obsolete so we don't mind too much\n");
                } else {
-                       ret = -EIO;
+                       jffs2_dbg_dump_node(c, ref_offset(raw));
+                       BUG();
                }
        }
  upnout:
@@ -486,12 +523,11 @@ static int jffs2_garbage_collect_live(struct jffs2_sb_info *c,  struct jffs2_era
        return ret;
 }
 
-static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c, 
+static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
                                          struct jffs2_inode_cache *ic,
                                          struct jffs2_raw_node_ref *raw)
 {
        union jffs2_node_union *node;
-       struct jffs2_raw_node_ref *nraw;
        size_t retlen;
        int ret;
        uint32_t phys_ofs, alloclen;
@@ -500,13 +536,17 @@ static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
 
        D1(printk(KERN_DEBUG "Going to GC REF_PRISTINE node at 0x%08x\n", ref_offset(raw)));
 
-       rawlen = ref_totlen(c, c->gcblock, raw);
+       alloclen = rawlen = ref_totlen(c, c->gcblock, raw);
 
        /* Ask for a small amount of space (or the totlen if smaller) because we
           don't want to force wastage of the end of a block if splitting would
           work. */
-       ret = jffs2_reserve_space_gc(c, min_t(uint32_t, sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN, 
-                                             rawlen), &phys_ofs, &alloclen);
+       if (ic && alloclen > sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN)
+               alloclen = sizeof(struct jffs2_raw_inode) + JFFS2_MIN_DATA_LEN;
+
+       ret = jffs2_reserve_space_gc(c, alloclen, &alloclen, rawlen);
+       /* 'rawlen' is not the exact summary size; it is only an upper estimation */
+
        if (ret)
                return ret;
 
@@ -569,22 +609,17 @@ static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
                }
                break;
        default:
-               printk(KERN_WARNING "Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n", 
-                      ref_offset(raw), je16_to_cpu(node->u.nodetype));
-               goto bail;
-       }
-
-       nraw = jffs2_alloc_raw_node_ref();
-       if (!nraw) {
-               ret = -ENOMEM;
-               goto out_node;
+               /* If it's inode-less, we don't _know_ what it is. Just copy it intact */
+               if (ic) {
+                       printk(KERN_WARNING "Unknown node type for REF_PRISTINE node at 0x%08x: 0x%04x\n",
+                              ref_offset(raw), je16_to_cpu(node->u.nodetype));
+                       goto bail;
+               }
        }
 
        /* OK, all the CRCs are good; this node can just be copied as-is. */
  retry:
-       nraw->flash_offset = phys_ofs;
-       nraw->__totlen = rawlen;
-       nraw->next_phys = NULL;
+       phys_ofs = write_ofs(c);
 
        ret = jffs2_flash_write(c, phys_ofs, rawlen, &retlen, (char *)node);
 
@@ -592,17 +627,11 @@ static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
                printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
                        rawlen, phys_ofs, ret, retlen);
                if (retlen) {
-                        /* Doesn't belong to any inode */
-                       nraw->next_in_ino = NULL;
-
-                       nraw->flash_offset |= REF_OBSOLETE;
-                       jffs2_add_physical_node_ref(c, nraw);
-                       jffs2_mark_node_obsolete(c, nraw);
+                       jffs2_add_physical_node_ref(c, phys_ofs | REF_OBSOLETE, rawlen, NULL);
                } else {
-                       printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", nraw->flash_offset);
-                        jffs2_free_raw_node_ref(nraw);
+                       printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", phys_ofs);
                }
-               if (!retried && (nraw = jffs2_alloc_raw_node_ref())) {
+               if (!retried) {
                        /* Try to reallocate space and retry */
                        uint32_t dummy;
                        struct jffs2_eraseblock *jeb = &c->blocks[phys_ofs / c->sector_size];
@@ -610,40 +639,30 @@ static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
                        retried = 1;
 
                        D1(printk(KERN_DEBUG "Retrying failed write of REF_PRISTINE node.\n"));
-                       
-                       ACCT_SANITY_CHECK(c,jeb);
-                       D1(ACCT_PARANOIA_CHECK(jeb));
 
-                       ret = jffs2_reserve_space_gc(c, rawlen, &phys_ofs, &dummy);
+                       jffs2_dbg_acct_sanity_check(c,jeb);
+                       jffs2_dbg_acct_paranoia_check(c, jeb);
+
+                       ret = jffs2_reserve_space_gc(c, rawlen, &dummy, rawlen);
+                                               /* this is not the exact summary size of it,
+                                                       it is only an upper estimation */
 
                        if (!ret) {
                                D1(printk(KERN_DEBUG "Allocated space at 0x%08x to retry failed write.\n", phys_ofs));
 
-                               ACCT_SANITY_CHECK(c,jeb);
-                               D1(ACCT_PARANOIA_CHECK(jeb));
+                               jffs2_dbg_acct_sanity_check(c,jeb);
+                               jffs2_dbg_acct_paranoia_check(c, jeb);
 
                                goto retry;
                        }
                        D1(printk(KERN_DEBUG "Failed to allocate space to retry failed write: %d!\n", ret));
-                       jffs2_free_raw_node_ref(nraw);
                }
 
-               jffs2_free_raw_node_ref(nraw);
                if (!ret)
                        ret = -EIO;
                goto out_node;
        }
-       nraw->flash_offset |= REF_PRISTINE;
-       jffs2_add_physical_node_ref(c, nraw);
-
-       /* Link into per-inode list. This is safe because of the ic
-          state being INO_STATE_GC. Note that if we're doing this
-          for an inode which is in-core, the 'nraw' pointer is then
-          going to be fetched from ic->nodes by our caller. */
-       spin_lock(&c->erase_completion_lock);
-        nraw->next_in_ino = ic->nodes;
-        ic->nodes = nraw;
-       spin_unlock(&c->erase_completion_lock);
+       jffs2_add_physical_node_ref(c, phys_ofs | REF_PRISTINE, rawlen, ic);
 
        jffs2_mark_node_obsolete(c, raw);
        D1(printk(KERN_DEBUG "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n", ref_offset(raw)));
@@ -656,24 +675,22 @@ static int jffs2_garbage_collect_pristine(struct jffs2_sb_info *c,
        goto out_node;
 }
 
-static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
                                        struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
 {
        struct jffs2_full_dnode *new_fn;
        struct jffs2_raw_inode ri;
-       jint16_t dev;
+       struct jffs2_node_frag *last_frag;
+       union jffs2_device_node dev;
        char *mdata = NULL, mdatalen = 0;
-       uint32_t alloclen, phys_ofs;
+       uint32_t alloclen, ilen;
        int ret;
 
        if (S_ISBLK(JFFS2_F_I_MODE(f)) ||
            S_ISCHR(JFFS2_F_I_MODE(f)) ) {
                /* For these, we don't actually need to read the old node */
-               /* FIXME: for minor or major > 255. */
-               dev = cpu_to_je16(((JFFS2_F_I_RDEV_MAJ(f) << 8) | 
-                       JFFS2_F_I_RDEV_MIN(f)));
+               mdatalen = jffs2_encode_dev(&dev, JFFS2_F_I_RDEV(f));
                mdata = (char *)&dev;
-               mdatalen = sizeof(dev);
                D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bytes of kdev_t\n", mdatalen));
        } else if (S_ISLNK(JFFS2_F_I_MODE(f))) {
                mdatalen = fn->size;
@@ -691,14 +708,23 @@ static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_
                D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bites of symlink target\n", mdatalen));
 
        }
-       
-       ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &phys_ofs, &alloclen);
+
+       ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &alloclen,
+                               JFFS2_SUMMARY_INODE_SIZE);
        if (ret) {
                printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_metadata failed: %d\n",
                       sizeof(ri)+ mdatalen, ret);
                goto out;
        }
-       
+
+       last_frag = frag_last(&f->fragtree);
+       if (last_frag)
+               /* Fetch the inode length from the fragtree rather then
+                * from i_size since i_size may have not been updated yet */
+               ilen = last_frag->ofs + last_frag->size;
+       else
+               ilen = JFFS2_F_I_SIZE(f);
+
        memset(&ri, 0, sizeof(ri));
        ri.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
        ri.nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
@@ -710,7 +736,7 @@ static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_
        ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
        ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
        ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
-       ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
+       ri.isize = cpu_to_je32(ilen);
        ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
        ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
        ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
@@ -721,7 +747,7 @@ static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_
        ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
        ri.data_crc = cpu_to_je32(crc32(0, mdata, mdatalen));
 
-       new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, phys_ofs, ALLOC_GC);
+       new_fn = jffs2_write_dnode(c, f, &ri, mdata, mdatalen, ALLOC_GC);
 
        if (IS_ERR(new_fn)) {
                printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn));
@@ -737,12 +763,12 @@ static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_
        return ret;
 }
 
-static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
                                        struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
 {
        struct jffs2_full_dirent *new_fd;
        struct jffs2_raw_dirent rd;
-       uint32_t alloclen, phys_ofs;
+       uint32_t alloclen;
        int ret;
 
        rd.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
@@ -754,18 +780,24 @@ static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_er
        rd.pino = cpu_to_je32(f->inocache->ino);
        rd.version = cpu_to_je32(++f->highest_version);
        rd.ino = cpu_to_je32(fd->ino);
-       rd.mctime = cpu_to_je32(max(JFFS2_F_I_MTIME(f), JFFS2_F_I_CTIME(f)));
+       /* If the times on this inode were set by explicit utime() they can be different,
+          so refrain from splatting them. */
+       if (JFFS2_F_I_MTIME(f) == JFFS2_F_I_CTIME(f))
+               rd.mctime = cpu_to_je32(JFFS2_F_I_MTIME(f));
+       else
+               rd.mctime = cpu_to_je32(0);
        rd.type = fd->type;
        rd.node_crc = cpu_to_je32(crc32(0, &rd, sizeof(rd)-8));
        rd.name_crc = cpu_to_je32(crc32(0, fd->name, rd.nsize));
-       
-       ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &phys_ofs, &alloclen);
+
+       ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &alloclen,
+                               JFFS2_SUMMARY_DIRENT_SIZE(rd.nsize));
        if (ret) {
                printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dirent failed: %d\n",
                       sizeof(rd)+rd.nsize, ret);
                return ret;
        }
-       new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, phys_ofs, ALLOC_GC);
+       new_fd = jffs2_write_dirent(c, f, &rd, fd->name, rd.nsize, ALLOC_GC);
 
        if (IS_ERR(new_fd)) {
                printk(KERN_WARNING "jffs2_write_dirent in garbage_collect_dirent failed: %ld\n", PTR_ERR(new_fd));
@@ -775,7 +807,7 @@ static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_er
        return 0;
 }
 
-static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
                                        struct jffs2_inode_info *f, struct jffs2_full_dirent *fd)
 {
        struct jffs2_full_dirent **fdp = &f->dents;
@@ -814,10 +846,9 @@ static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct
                        if (ref_totlen(c, NULL, raw) != rawlen)
                                continue;
 
-                       /* Doesn't matter if there's one in the same erase block. We're going to 
+                       /* Doesn't matter if there's one in the same erase block. We're going to
                           delete it too at the same time. */
-                       if ((raw->flash_offset & ~(c->sector_size-1)) ==
-                           (fd->raw->flash_offset & ~(c->sector_size-1)))
+                       if (SECTOR_ADDR(raw->flash_offset) == SECTOR_ADDR(fd->raw->flash_offset))
                                continue;
 
                        D1(printk(KERN_DEBUG "Check potential deletion dirent at %08x\n", ref_offset(raw)));
@@ -867,6 +898,9 @@ static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct
                kfree(rd);
        }
 
+       /* FIXME: If we're deleting a dirent which contains the current mtime and ctime,
+          we should update the metadata node with those times accordingly */
+
        /* No need for it any more. Just mark it obsolete and remove it from the list */
        while (*fdp) {
                if ((*fdp) == fd) {
@@ -891,18 +925,18 @@ static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eras
        struct jffs2_raw_inode ri;
        struct jffs2_node_frag *frag;
        struct jffs2_full_dnode *new_fn;
-       uint32_t alloclen, phys_ofs;
+       uint32_t alloclen, ilen;
        int ret;
 
        D1(printk(KERN_DEBUG "Writing replacement hole node for ino #%u from offset 0x%x to 0x%x\n",
                  f->inocache->ino, start, end));
-       
+
        memset(&ri, 0, sizeof(ri));
 
        if(fn->frags > 1) {
                size_t readlen;
                uint32_t crc;
-               /* It's partially obsoleted by a later write. So we have to 
+               /* It's partially obsoleted by a later write. So we have to
                   write it out again with the _same_ version as before */
                ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(ri), &readlen, (char *)&ri);
                if (readlen != sizeof(ri) || ret) {
@@ -924,16 +958,16 @@ static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eras
                crc = crc32(0, &ri, sizeof(ri)-8);
                if (crc != je32_to_cpu(ri.node_crc)) {
                        printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
-                              ref_offset(fn->raw), 
+                              ref_offset(fn->raw),
                               je32_to_cpu(ri.node_crc), crc);
                        /* FIXME: We could possibly deal with this by writing new holes for each frag */
-                       printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n", 
+                       printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
                               start, end, f->inocache->ino);
                        goto fill;
                }
                if (ri.compr != JFFS2_COMPR_ZERO) {
                        printk(KERN_WARNING "jffs2_garbage_collect_hole: Node 0x%08x wasn't a hole node!\n", ref_offset(fn->raw));
-                       printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n", 
+                       printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%u will be lost\n",
                               start, end, f->inocache->ino);
                        goto fill;
                }
@@ -951,23 +985,33 @@ static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eras
                ri.csize = cpu_to_je32(0);
                ri.compr = JFFS2_COMPR_ZERO;
        }
+
+       frag = frag_last(&f->fragtree);
+       if (frag)
+               /* Fetch the inode length from the fragtree rather then
+                * from i_size since i_size may have not been updated yet */
+               ilen = frag->ofs + frag->size;
+       else
+               ilen = JFFS2_F_I_SIZE(f);
+
        ri.mode = cpu_to_jemode(JFFS2_F_I_MODE(f));
        ri.uid = cpu_to_je16(JFFS2_F_I_UID(f));
        ri.gid = cpu_to_je16(JFFS2_F_I_GID(f));
-       ri.isize = cpu_to_je32(JFFS2_F_I_SIZE(f));
+       ri.isize = cpu_to_je32(ilen);
        ri.atime = cpu_to_je32(JFFS2_F_I_ATIME(f));
        ri.ctime = cpu_to_je32(JFFS2_F_I_CTIME(f));
        ri.mtime = cpu_to_je32(JFFS2_F_I_MTIME(f));
        ri.data_crc = cpu_to_je32(0);
        ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
 
-       ret = jffs2_reserve_space_gc(c, sizeof(ri), &phys_ofs, &alloclen);
+       ret = jffs2_reserve_space_gc(c, sizeof(ri), &alloclen,
+                                    JFFS2_SUMMARY_INODE_SIZE);
        if (ret) {
                printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_hole failed: %d\n",
                       sizeof(ri), ret);
                return ret;
        }
-       new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, phys_ofs, ALLOC_GC);
+       new_fn = jffs2_write_dnode(c, f, &ri, NULL, 0, ALLOC_GC);
 
        if (IS_ERR(new_fn)) {
                printk(KERN_WARNING "Error writing new hole node: %ld\n", PTR_ERR(new_fn));
@@ -983,10 +1027,10 @@ static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eras
                return 0;
        }
 
-       /* 
+       /*
         * We should only get here in the case where the node we are
         * replacing had more than one frag, so we kept the same version
-        * number as before. (Except in case of error -- see 'goto fill;' 
+        * number as before. (Except in case of error -- see 'goto fill;'
         * above.)
         */
        D1(if(unlikely(fn->frags <= 1)) {
@@ -998,7 +1042,7 @@ static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eras
        /* This is a partially-overlapped hole node. Mark it REF_NORMAL not REF_PRISTINE */
        mark_ref_normal(new_fn->raw);
 
-       for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs); 
+       for (frag = jffs2_lookup_node_frag(&f->fragtree, fn->ofs);
             frag; frag = frag_next(frag)) {
                if (frag->ofs > fn->size + fn->ofs)
                        break;
@@ -1016,10 +1060,10 @@ static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eras
                printk(KERN_WARNING "jffs2_garbage_collect_hole: New node has no frags!\n");
                BUG();
        }
-               
+
        jffs2_mark_node_obsolete(c, fn->raw);
        jffs2_free_full_dnode(fn);
-       
+
        return 0;
 }
 
@@ -1029,12 +1073,12 @@ static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_era
 {
        struct jffs2_full_dnode *new_fn;
        struct jffs2_raw_inode ri;
-       uint32_t alloclen, phys_ofs, offset, orig_end, orig_start;      
+       uint32_t alloclen, offset, orig_end, orig_start;
        int ret = 0;
        unsigned char *comprbuf = NULL, *writebuf;
        unsigned long pg;
        unsigned char *pg_ptr;
+
        memset(&ri, 0, sizeof(ri));
 
        D1(printk(KERN_DEBUG "Writing replacement dnode for ino #%u from offset 0x%x to 0x%x\n",
@@ -1046,8 +1090,8 @@ static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_era
        if (c->nr_free_blocks + c->nr_erasing_blocks > c->resv_blocks_gcmerge) {
                /* Attempt to do some merging. But only expand to cover logically
                   adjacent frags if the block containing them is already considered
-                  to be dirty. Otherwise we end up with GC just going round in 
-                  circles dirtying the nodes it already wrote out, especially 
+                  to be dirty. Otherwise we end up with GC just going round in
+                  circles dirtying the nodes it already wrote out, especially
                   on NAND where we have small eraseblocks and hence a much higher
                   chance of nodes having to be split to cross boundaries. */
 
@@ -1081,7 +1125,7 @@ static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_era
                                break;
                        } else {
 
-                               /* OK, it's a frag which extends to the beginning of the page. Does it live 
+                               /* OK, it's a frag which extends to the beginning of the page. Does it live
                                   in a block which is still considered clean? If so, don't obsolete it.
                                   If not, cover it anyway. */
 
@@ -1131,7 +1175,7 @@ static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_era
                                break;
                        } else {
 
-                               /* OK, it's a frag which extends to the beginning of the page. Does it live 
+                               /* OK, it's a frag which extends to the beginning of the page. Does it live
                                   in a block which is still considered clean? If so, don't obsolete it.
                                   If not, cover it anyway. */
 
@@ -1158,14 +1202,14 @@ static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_era
                                break;
                        }
                }
-               D1(printk(KERN_DEBUG "Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)\n", 
+               D1(printk(KERN_DEBUG "Expanded dnode to write from (0x%x-0x%x) to (0x%x-0x%x)\n",
                          orig_start, orig_end, start, end));
 
-               BUG_ON(end > JFFS2_F_I_SIZE(f));
+               D1(BUG_ON(end > frag_last(&f->fragtree)->ofs + frag_last(&f->fragtree)->size));
                BUG_ON(end < orig_end);
                BUG_ON(start > orig_start);
        }
-       
+
        /* First, use readpage() to read the appropriate page into the page cache */
        /* Q: What happens if we actually try to GC the _same_ page for which commit_write()
         *    triggered garbage collection in the first place?
@@ -1186,7 +1230,8 @@ static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_era
                uint32_t cdatalen;
                uint16_t comprtype = JFFS2_COMPR_NONE;
 
-               ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, &alloclen);
+               ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN,
+                                       &alloclen, JFFS2_SUMMARY_INODE_SIZE);
 
                if (ret) {
                        printk(KERN_WARNING "jffs2_reserve_space_gc of %zd bytes for garbage_collect_dnode failed: %d\n",
@@ -1221,8 +1266,8 @@ static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_era
                ri.usercompr = (comprtype >> 8) & 0xff;
                ri.node_crc = cpu_to_je32(crc32(0, &ri, sizeof(ri)-8));
                ri.data_crc = cpu_to_je32(crc32(0, comprbuf, cdatalen));
-       
-               new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, phys_ofs, ALLOC_GC);
+
+               new_fn = jffs2_write_dnode(c, f, &ri, comprbuf, cdatalen, ALLOC_GC);
 
                jffs2_free_comprbuf(comprbuf, writebuf);
 
@@ -1243,4 +1288,3 @@ static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_era
        jffs2_gc_release_page(c, pg_ptr, &pg);
        return ret;
 }
-