Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / fs / jffs2 / wbuf.c
index 2470eef..4cebf0e 100644 (file)
@@ -4,12 +4,12 @@
  * Copyright (C) 2001-2003 Red Hat, Inc.
  * Copyright (C) 2004 Thomas Gleixner <tglx@linutronix.de>
  *
- * Created by David Woodhouse <dwmw2@redhat.com>
+ * Created by David Woodhouse <dwmw2@infradead.org>
  * Modified debugged and enhanced by Thomas Gleixner <tglx@linutronix.de>
  *
  * For licensing information, see the file 'LICENCE' in this directory.
  *
- * $Id: wbuf.c,v 1.70 2004/07/13 08:58:25 dwmw2 Exp $
+ * $Id: wbuf.c,v 1.100 2005/09/30 13:59:13 dedekind Exp $
  *
  */
 
@@ -18,6 +18,8 @@
 #include <linux/mtd/mtd.h>
 #include <linux/crc32.h>
 #include <linux/mtd/nand.h>
+#include <linux/jiffies.h>
+
 #include "nodelist.h"
 
 /* For testing write failures */
 static unsigned char *brokenbuf;
 #endif
 
+#define PAGE_DIV(x) ( ((unsigned long)(x) / (unsigned long)(c->wbuf_pagesize)) * (unsigned long)(c->wbuf_pagesize) )
+#define PAGE_MOD(x) ( (unsigned long)(x) % (unsigned long)(c->wbuf_pagesize) )
+
 /* max. erase failures before we mark a block bad */
 #define MAX_ERASE_FAILURES     2
 
-/* two seconds timeout for timed wbuf-flushing */
-#define WBUF_FLUSH_TIMEOUT     2 * HZ
-
 struct jffs2_inodirty {
        uint32_t ino;
        struct jffs2_inodirty *next;
@@ -83,7 +85,7 @@ static void jffs2_wbuf_dirties_inode(struct jffs2_sb_info *c, uint32_t ino)
        struct jffs2_inodirty *new;
 
        /* Mark the superblock dirty so that kupdated will flush... */
-       OFNI_BS_2SFFJ(c)->s_dirt = 1;
+       jffs2_erase_pending_trigger(c);
 
        if (jffs2_wbuf_pending_for_ino(c, ino))
                return;
@@ -130,25 +132,13 @@ static inline void jffs2_refile_wbuf_blocks(struct jffs2_sb_info *c)
        }
 }
 
-/* Recover from failure to write wbuf. Recover the nodes up to the
- * wbuf, not the one which we were starting to try to write. */
+#define REFILE_NOTEMPTY 0
+#define REFILE_ANYWAY   1
 
-static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
+static void jffs2_block_refile(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int allow_empty)
 {
-       struct jffs2_eraseblock *jeb, *new_jeb;
-       struct jffs2_raw_node_ref **first_raw, **raw;
-       size_t retlen;
-       int ret;
-       unsigned char *buf;
-       uint32_t start, end, ofs, len;
-
-       spin_lock(&c->erase_completion_lock);
-
-       jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
-
        D1(printk("About to refile bad block at %08x\n", jeb->offset));
 
-       D2(jffs2_dump_block_lists(c));
        /* File the existing block on the bad_used_list.... */
        if (c->nextblock == jeb)
                c->nextblock = NULL;
@@ -158,14 +148,13 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
                D1(printk("Refiling block at %08x to bad_used_list\n", jeb->offset));
                list_add(&jeb->list, &c->bad_used_list);
        } else {
-               BUG();
+               BUG_ON(allow_empty == REFILE_NOTEMPTY);
                /* It has to have had some nodes or we couldn't be here */
                D1(printk("Refiling block at %08x to erase_pending_list\n", jeb->offset));
                list_add(&jeb->list, &c->erase_pending_list);
                c->nr_erasing_blocks++;
                jffs2_erase_pending_trigger(c);
        }
-       D2(jffs2_dump_block_lists(c));
 
        /* Adjust its size counts accordingly */
        c->wasted_size += jeb->free_size;
@@ -173,13 +162,33 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
        jeb->wasted_size += jeb->free_size;
        jeb->free_size = 0;
 
-       ACCT_SANITY_CHECK(c,jeb);
-       D1(ACCT_PARANOIA_CHECK(jeb));
+       jffs2_dbg_dump_block_lists_nolock(c);
+       jffs2_dbg_acct_sanity_check_nolock(c,jeb);
+       jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
+}
+
+/* Recover from failure to write wbuf. Recover the nodes up to the
+ * wbuf, not the one which we were starting to try to write. */
+
+static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
+{
+       struct jffs2_eraseblock *jeb, *new_jeb;
+       struct jffs2_raw_node_ref **first_raw, **raw;
+       size_t retlen;
+       int ret;
+       unsigned char *buf;
+       uint32_t start, end, ofs, len;
+
+       spin_lock(&c->erase_completion_lock);
+
+       jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
+
+       jffs2_block_refile(c, jeb, REFILE_NOTEMPTY);
 
        /* Find the first node to be recovered, by skipping over every
           node which ends before the wbuf starts, or which is obsolete. */
        first_raw = &jeb->first_node;
-       while (*first_raw && 
+       while (*first_raw &&
               (ref_obsolete(*first_raw) ||
                (ref_offset(*first_raw)+ref_totlen(c, jeb, *first_raw)) < c->wbuf_ofs)) {
                D1(printk(KERN_DEBUG "Skipping node at 0x%08x(%d)-0x%08x which is either before 0x%08x or obsolete\n",
@@ -224,8 +233,12 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
                }
 
                /* Do the read... */
-               ret = c->mtd->read_ecc(c->mtd, start, c->wbuf_ofs - start, &retlen, buf, NULL, c->oobinfo);
-               if (ret == -EIO && retlen == c->wbuf_ofs - start) {
+               if (jffs2_cleanmarker_oob(c))
+                       ret = c->mtd->read_ecc(c->mtd, start, c->wbuf_ofs - start, &retlen, buf, NULL, c->oobinfo);
+               else
+                       ret = c->mtd->read(c->mtd, start, c->wbuf_ofs - start, &retlen, buf);
+
+               if (ret == -EBADMSG && retlen == c->wbuf_ofs - start) {
                        /* ECC recovered */
                        ret = 0;
                }
@@ -252,25 +265,24 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
 
 
        /* ... and get an allocation of space from a shiny new block instead */
-       ret = jffs2_reserve_space_gc(c, end-start, &ofs, &len);
+       ret = jffs2_reserve_space_gc(c, end-start, &ofs, &len, JFFS2_SUMMARY_NOSUM_SIZE);
        if (ret) {
                printk(KERN_WARNING "Failed to allocate space for wbuf recovery. Data loss ensues.\n");
-               if (buf)
-                       kfree(buf);
+               kfree(buf);
                return;
        }
        if (end-start >= c->wbuf_pagesize) {
-               /* Need to do another write immediately. This, btw,
-                means that we'll be writing from 'buf' and not from
-                the wbuf. Since if we're writing from the wbuf there
-                won't be more than a wbuf full of data, now will
-                there? :) */
-
+               /* Need to do another write immediately, but it's possible
+                  that this is just because the wbuf itself is completely
+                  full, and there's nothing earlier read back from the
+                  flash. Hence 'buf' isn't necessarily what we're writing
+                  from. */
+               unsigned char *rewrite_buf = buf?:c->wbuf;
                uint32_t towrite = (end-start) - ((end-start)%c->wbuf_pagesize);
 
                D1(printk(KERN_DEBUG "Write 0x%x bytes at 0x%08x in wbuf recover\n",
                          towrite, ofs));
-         
+
 #ifdef BREAKMEHEADER
                static int breakme;
                if (breakme++ == 20) {
@@ -281,8 +293,11 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
                        ret = -EIO;
                } else
 #endif
+               if (jffs2_cleanmarker_oob(c))
                        ret = c->mtd->write_ecc(c->mtd, ofs, towrite, &retlen,
-                                               buf, NULL, c->oobinfo);
+                                               rewrite_buf, NULL, c->oobinfo);
+               else
+                       ret = c->mtd->write(c->mtd, ofs, towrite, &retlen, rewrite_buf);
 
                if (ret || retlen != towrite) {
                        /* Argh. We tried. Really we did. */
@@ -309,9 +324,8 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
 
                c->wbuf_len = (end - start) - towrite;
                c->wbuf_ofs = ofs + towrite;
-               memcpy(c->wbuf, buf + towrite, c->wbuf_len);
+               memmove(c->wbuf, rewrite_buf + towrite, c->wbuf_len);
                /* Don't muck about with c->wbuf_inodes. False positives are harmless. */
-
                kfree(buf);
        } else {
                /* OK, now we're left with the dregs in whichever buffer we're using */
@@ -376,11 +390,11 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
        else
                jeb->last_node = container_of(first_raw, struct jffs2_raw_node_ref, next_phys);
 
-       ACCT_SANITY_CHECK(c,jeb);
-        D1(ACCT_PARANOIA_CHECK(jeb));
+       jffs2_dbg_acct_sanity_check_nolock(c, jeb);
+        jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
 
-       ACCT_SANITY_CHECK(c,new_jeb);
-        D1(ACCT_PARANOIA_CHECK(new_jeb));
+       jffs2_dbg_acct_sanity_check_nolock(c, new_jeb);
+        jffs2_dbg_acct_paranoia_check_nolock(c, new_jeb);
 
        spin_unlock(&c->erase_completion_lock);
 
@@ -392,14 +406,18 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
    1: Pad, do not adjust nextblock free_size
    2: Pad, adjust nextblock free_size
 */
+#define NOPAD          0
+#define PAD_NOACCOUNT  1
+#define PAD_ACCOUNTING 2
+
 static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
 {
        int ret;
        size_t retlen;
 
-       /* Nothing to do if not NAND flash. In particular, we shouldn't
+       /* Nothing to do if not write-buffering the flash. In particular, we shouldn't
           del_timer() the timer we never initialised. */
-       if (jffs2_can_mark_obsolete(c))
+       if (!jffs2_is_writebuffered(c))
                return 0;
 
        if (!down_trylock(&c->alloc_sem)) {
@@ -408,32 +426,33 @@ static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
                BUG();
        }
 
-       if(!c->wbuf || !c->wbuf_len)
+       if (!c->wbuf_len)       /* already checked c->wbuf above */
                return 0;
 
        /* claim remaining space on the page
           this happens, if we have a change to a new block,
           or if fsync forces us to flush the writebuffer.
           if we have a switch to next page, we will not have
-          enough remaining space for this. 
+          enough remaining space for this.
        */
-       if (pad) {
+       if (pad ) {
                c->wbuf_len = PAD(c->wbuf_len);
-               
+
+               /* Pad with JFFS2_DIRTY_BITMASK initially.  this helps out ECC'd NOR
+                  with 8 byte page size */
+               memset(c->wbuf + c->wbuf_len, 0, c->wbuf_pagesize - c->wbuf_len);
+
                if ( c->wbuf_len + sizeof(struct jffs2_unknown_node) < c->wbuf_pagesize) {
                        struct jffs2_unknown_node *padnode = (void *)(c->wbuf + c->wbuf_len);
                        padnode->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
                        padnode->nodetype = cpu_to_je16(JFFS2_NODETYPE_PADDING);
                        padnode->totlen = cpu_to_je32(c->wbuf_pagesize - c->wbuf_len);
                        padnode->hdr_crc = cpu_to_je32(crc32(0, padnode, sizeof(*padnode)-4));
-               } else {
-                       /* Pad with JFFS2_DIRTY_BITMASK */
-                       memset(c->wbuf + c->wbuf_len, 0, c->wbuf_pagesize - c->wbuf_len);
                }
        }
        /* else jffs2_flash_writev has actually filled in the rest of the
           buffer for us, and will deal with the node refs etc. later. */
-       
+
 #ifdef BREAKME
        static int breakme;
        if (breakme++ == 20) {
@@ -442,10 +461,13 @@ static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
                c->mtd->write_ecc(c->mtd, c->wbuf_ofs, c->wbuf_pagesize,
                                        &retlen, brokenbuf, NULL, c->oobinfo);
                ret = -EIO;
-       } else 
+       } else
 #endif
-       ret = c->mtd->write_ecc(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen, c->wbuf, NULL, c->oobinfo);
 
+       if (jffs2_cleanmarker_oob(c))
+               ret = c->mtd->write_ecc(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen, c->wbuf, NULL, c->oobinfo);
+       else
+               ret = c->mtd->write(c->mtd, c->wbuf_ofs, c->wbuf_pagesize, &retlen, c->wbuf);
 
        if (ret || retlen != c->wbuf_pagesize) {
                if (ret)
@@ -458,7 +480,7 @@ static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
 
                jffs2_wbuf_recover(c);
 
-               return ret; 
+               return ret;
        }
 
        spin_lock(&c->erase_completion_lock);
@@ -472,7 +494,7 @@ static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
                D1(printk(KERN_DEBUG "jffs2_flush_wbuf() adjusting free_size of %sblock at %08x\n",
                          (jeb==c->nextblock)?"next":"", jeb->offset));
 
-               /* wbuf_pagesize - wbuf_len is the amount of space that's to be 
+               /* wbuf_pagesize - wbuf_len is the amount of space that's to be
                   padded. If there is less free space in the block than that,
                   something screwed up */
                if (jeb->free_size < (c->wbuf_pagesize - c->wbuf_len)) {
@@ -500,9 +522,9 @@ static int __jffs2_flush_wbuf(struct jffs2_sb_info *c, int pad)
        return 0;
 }
 
-/* Trigger garbage collection to flush the write-buffer. 
+/* Trigger garbage collection to flush the write-buffer.
    If ino arg is zero, do it if _any_ real (i.e. not GC) writes are
-   outstanding. If ino arg non-zero, do it only if a write for the 
+   outstanding. If ino arg non-zero, do it only if a write for the
    given inode is outstanding. */
 int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino)
 {
@@ -512,6 +534,9 @@ int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino)
 
        D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() called for ino #%u...\n", ino));
 
+       if (!c->wbuf)
+               return 0;
+
        down(&c->alloc_sem);
        if (!jffs2_wbuf_pending_for_ino(c, ino)) {
                D1(printk(KERN_DEBUG "Ino #%d not pending in wbuf. Returning\n", ino));
@@ -525,7 +550,13 @@ int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino)
        if (c->unchecked_size) {
                /* GC won't make any progress for a while */
                D1(printk(KERN_DEBUG "jffs2_flush_wbuf_gc() padding. Not finished checking\n"));
-               ret = __jffs2_flush_wbuf(c, 2);
+               down_write(&c->wbuf_sem);
+               ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
+               /* retry flushing wbuf in case jffs2_wbuf_recover
+                  left some data in the wbuf */
+               if (ret)
+                       ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
+               up_write(&c->wbuf_sem);
        } else while (old_wbuf_len &&
                      old_wbuf_ofs == c->wbuf_ofs) {
 
@@ -537,7 +568,13 @@ int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino)
                if (ret) {
                        /* GC failed. Flush it with padding instead */
                        down(&c->alloc_sem);
-                       ret = __jffs2_flush_wbuf(c, 2);
+                       down_write(&c->wbuf_sem);
+                       ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
+                       /* retry flushing wbuf in case jffs2_wbuf_recover
+                          left some data in the wbuf */
+                       if (ret)
+                               ret = __jffs2_flush_wbuf(c, PAD_ACCOUNTING);
+                       up_write(&c->wbuf_sem);
                        break;
                }
                down(&c->alloc_sem);
@@ -552,12 +589,20 @@ int jffs2_flush_wbuf_gc(struct jffs2_sb_info *c, uint32_t ino)
 /* Pad write-buffer to end and write it, wasting space. */
 int jffs2_flush_wbuf_pad(struct jffs2_sb_info *c)
 {
-       return __jffs2_flush_wbuf(c, 1);
-}
+       int ret;
+
+       if (!c->wbuf)
+               return 0;
 
+       down_write(&c->wbuf_sem);
+       ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
+       /* retry - maybe wbuf recover left some data in wbuf. */
+       if (ret)
+               ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
+       up_write(&c->wbuf_sem);
 
-#define PAGE_DIV(x) ( (x) & (~(c->wbuf_pagesize - 1)) )
-#define PAGE_MOD(x) ( (x) & (c->wbuf_pagesize - 1) )
+       return ret;
+}
 int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs, unsigned long count, loff_t to, size_t *retlen, uint32_t ino)
 {
        struct kvec outvecs[3];
@@ -572,38 +617,51 @@ int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs, unsig
        uint32_t outvec_to = to;
 
        /* If not NAND flash, don't bother */
-       if (!c->wbuf)
+       if (!jffs2_is_writebuffered(c))
                return jffs2_flash_direct_writev(c, invecs, count, to, retlen);
-       
+
+       down_write(&c->wbuf_sem);
+
        /* If wbuf_ofs is not initialized, set it to target address */
        if (c->wbuf_ofs == 0xFFFFFFFF) {
                c->wbuf_ofs = PAGE_DIV(to);
-               c->wbuf_len = PAGE_MOD(to);                     
+               c->wbuf_len = PAGE_MOD(to);
                memset(c->wbuf,0xff,c->wbuf_pagesize);
        }
 
-       /* Sanity checks on target address. 
-          It's permitted to write at PAD(c->wbuf_len+c->wbuf_ofs), 
-          and it's permitted to write at the beginning of a new 
+       /* Fixup the wbuf if we are moving to a new eraseblock.  The checks below
+          fail for ECC'd NOR because cleanmarker == 16, so a block starts at
+          xxx0010.  */
+       if (jffs2_nor_ecc(c)) {
+               if (((c->wbuf_ofs % c->sector_size) == 0) && !c->wbuf_len) {
+                       c->wbuf_ofs = PAGE_DIV(to);
+                       c->wbuf_len = PAGE_MOD(to);
+                       memset(c->wbuf,0xff,c->wbuf_pagesize);
+               }
+       }
+
+       /* Sanity checks on target address.
+          It's permitted to write at PAD(c->wbuf_len+c->wbuf_ofs),
+          and it's permitted to write at the beginning of a new
           erase block. Anything else, and you die.
           New block starts at xxx000c (0-b = block header)
        */
-       if ( (to & ~(c->sector_size-1)) != (c->wbuf_ofs & ~(c->sector_size-1)) ) {
+       if (SECTOR_ADDR(to) != SECTOR_ADDR(c->wbuf_ofs)) {
                /* It's a write to a new block */
                if (c->wbuf_len) {
                        D1(printk(KERN_DEBUG "jffs2_flash_writev() to 0x%lx causes flush of wbuf at 0x%08x\n", (unsigned long)to, c->wbuf_ofs));
-                       ret = jffs2_flush_wbuf_pad(c);
+                       ret = __jffs2_flush_wbuf(c, PAD_NOACCOUNT);
                        if (ret) {
                                /* the underlying layer has to check wbuf_len to do the cleanup */
                                D1(printk(KERN_WARNING "jffs2_flush_wbuf() called from jffs2_flash_writev() failed %d\n", ret));
                                *retlen = 0;
-                               return ret;
+                               goto exit;
                        }
                }
                /* set pointer to new block */
                c->wbuf_ofs = PAGE_DIV(to);
-               c->wbuf_len = PAGE_MOD(to);                     
-       } 
+               c->wbuf_len = PAGE_MOD(to);
+       }
 
        if (to != PAD(c->wbuf_ofs + c->wbuf_len)) {
                /* We're not writing immediately after the writebuffer. Bad. */
@@ -623,22 +681,21 @@ int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs, unsig
        invec = 0;
        outvec = 0;
 
-
-       /* Fill writebuffer first, if already in use */ 
+       /* Fill writebuffer first, if already in use */
        if (c->wbuf_len) {
                uint32_t invec_ofs = 0;
 
-               /* adjust alignment offset */ 
+               /* adjust alignment offset */
                if (c->wbuf_len != PAGE_MOD(to)) {
                        c->wbuf_len = PAGE_MOD(to);
                        /* take care of alignment to next page */
                        if (!c->wbuf_len)
                                c->wbuf_len = c->wbuf_pagesize;
                }
-               
+
                while(c->wbuf_len < c->wbuf_pagesize) {
                        uint32_t thislen;
-                       
+
                        if (invec == count)
                                goto alldone;
 
@@ -646,26 +703,26 @@ int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs, unsig
 
                        if (thislen >= invecs[invec].iov_len)
                                thislen = invecs[invec].iov_len;
-       
+
                        invec_ofs = thislen;
 
                        memcpy(c->wbuf + c->wbuf_len, invecs[invec].iov_base, thislen);
                        c->wbuf_len += thislen;
                        donelen += thislen;
                        /* Get next invec, if actual did not fill the buffer */
-                       if (c->wbuf_len < c->wbuf_pagesize) 
+                       if (c->wbuf_len < c->wbuf_pagesize)
                                invec++;
-               }                       
-               
+               }
+
                /* write buffer is full, flush buffer */
-               ret = __jffs2_flush_wbuf(c, 0);
+               ret = __jffs2_flush_wbuf(c, NOPAD);
                if (ret) {
                        /* the underlying layer has to check wbuf_len to do the cleanup */
                        D1(printk(KERN_WARNING "jffs2_flush_wbuf() called from jffs2_flash_writev() failed %d\n", ret));
                        /* Retlen zero to make sure our caller doesn't mark the space dirty.
                           We've already done everything that's necessary */
                        *retlen = 0;
-                       return ret;
+                       goto exit;
                }
                outvec_to += donelen;
                c->wbuf_ofs = outvec_to;
@@ -709,21 +766,33 @@ int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs, unsig
 
        if (splitvec != -1) {
                uint32_t remainder;
-               int ret;
 
                remainder = outvecs[splitvec].iov_len - split_ofs;
                outvecs[splitvec].iov_len = split_ofs;
 
                /* We did cross a page boundary, so we write some now */
-               ret = c->mtd->writev_ecc(c->mtd, outvecs, splitvec+1, outvec_to, &wbuf_retlen, NULL, c->oobinfo); 
+               if (jffs2_cleanmarker_oob(c))
+                       ret = c->mtd->writev_ecc(c->mtd, outvecs, splitvec+1, outvec_to, &wbuf_retlen, NULL, c->oobinfo);
+               else
+                       ret = jffs2_flash_direct_writev(c, outvecs, splitvec+1, outvec_to, &wbuf_retlen);
+
                if (ret < 0 || wbuf_retlen != PAGE_DIV(totlen)) {
                        /* At this point we have no problem,
-                          c->wbuf is empty. 
+                          c->wbuf is empty. However refile nextblock to avoid
+                          writing again to same address.
                        */
-                       *retlen = donelen;
-                       return ret;
+                       struct jffs2_eraseblock *jeb;
+
+                       spin_lock(&c->erase_completion_lock);
+
+                       jeb = &c->blocks[outvec_to / c->sector_size];
+                       jffs2_block_refile(c, jeb, REFILE_ANYWAY);
+
+                       *retlen = 0;
+                       spin_unlock(&c->erase_completion_lock);
+                       goto exit;
                }
-               
+
                donelen += wbuf_retlen;
                c->wbuf_ofs = PAGE_DIV(outvec_to) + PAGE_DIV(totlen);
 
@@ -757,10 +826,20 @@ int jffs2_flash_writev(struct jffs2_sb_info *c, const struct kvec *invecs, unsig
 alldone:
        *retlen = donelen;
 
+       if (jffs2_sum_active()) {
+               int res = jffs2_sum_add_kvec(c, invecs, count, (uint32_t) to);
+               if (res)
+                       return res;
+       }
+
        if (c->wbuf_len && ino)
                jffs2_wbuf_dirties_inode(c, ino);
 
-       return 0;
+       ret = 0;
+
+exit:
+       up_write(&c->wbuf_sem);
+       return ret;
 }
 
 /*
@@ -771,8 +850,8 @@ int jffs2_flash_write(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *r
 {
        struct kvec vecs[1];
 
-       if (jffs2_can_mark_obsolete(c))
-               return c->mtd->write(c->mtd, ofs, len, retlen, buf);
+       if (!jffs2_is_writebuffered(c))
+               return jffs2_flash_direct_write(c, ofs, len, retlen, buf);
 
        vecs[0].iov_base = (unsigned char *) buf;
        vecs[0].iov_len = len;
@@ -787,54 +866,60 @@ int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *re
        loff_t  orbf = 0, owbf = 0, lwbf = 0;
        int     ret;
 
+       if (!jffs2_is_writebuffered(c))
+               return c->mtd->read(c->mtd, ofs, len, retlen, buf);
+
        /* Read flash */
-       if (!jffs2_can_mark_obsolete(c)) {
+       down_read(&c->wbuf_sem);
+       if (jffs2_cleanmarker_oob(c))
                ret = c->mtd->read_ecc(c->mtd, ofs, len, retlen, buf, NULL, c->oobinfo);
+       else
+               ret = c->mtd->read(c->mtd, ofs, len, retlen, buf);
 
-               if ( (ret == -EIO) && (*retlen == len) ) {
-                       printk(KERN_WARNING "mtd->read(0x%zx bytes from 0x%llx) returned ECC error\n",
-                              len, ofs);
-                       /* 
-                        * We have the raw data without ECC correction in the buffer, maybe 
-                        * we are lucky and all data or parts are correct. We check the node.
-                        * If data are corrupted node check will sort it out.
-                        * We keep this block, it will fail on write or erase and the we
-                        * mark it bad. Or should we do that now? But we should give him a chance.
-                        * Maybe we had a system crash or power loss before the ecc write or  
-                        * a erase was completed.
-                        * So we return success. :)
-                        */
-                       ret = 0;
-                }      
-       } else
-               return c->mtd->read(c->mtd, ofs, len, retlen, buf);
+       if ( (ret == -EBADMSG) && (*retlen == len) ) {
+               printk(KERN_WARNING "mtd->read(0x%zx bytes from 0x%llx) returned ECC error\n",
+                      len, ofs);
+               /*
+                * We have the raw data without ECC correction in the buffer, maybe
+                * we are lucky and all data or parts are correct. We check the node.
+                * If data are corrupted node check will sort it out.
+                * We keep this block, it will fail on write or erase and the we
+                * mark it bad. Or should we do that now? But we should give him a chance.
+                * Maybe we had a system crash or power loss before the ecc write or
+                * a erase was completed.
+                * So we return success. :)
+                */
+               ret = 0;
+       }
 
        /* if no writebuffer available or write buffer empty, return */
        if (!c->wbuf_pagesize || !c->wbuf_len)
-               return ret;
+               goto exit;
 
        /* if we read in a different block, return */
-       if ( (ofs & ~(c->sector_size-1)) != (c->wbuf_ofs & ~(c->sector_size-1)) ) 
-               return ret;     
+       if (SECTOR_ADDR(ofs) != SECTOR_ADDR(c->wbuf_ofs))
+               goto exit;
 
        if (ofs >= c->wbuf_ofs) {
                owbf = (ofs - c->wbuf_ofs);     /* offset in write buffer */
                if (owbf > c->wbuf_len)         /* is read beyond write buffer ? */
-                       return ret;
+                       goto exit;
                lwbf = c->wbuf_len - owbf;      /* number of bytes to copy */
-               if (lwbf > len) 
+               if (lwbf > len)
                        lwbf = len;
-       } else {        
+       } else {
                orbf = (c->wbuf_ofs - ofs);     /* offset in read buffer */
                if (orbf > len)                 /* is write beyond write buffer ? */
-                       return ret;
+                       goto exit;
                lwbf = len - orbf;              /* number of bytes to copy */
-               if (lwbf > c->wbuf_len) 
+               if (lwbf > c->wbuf_len)
                        lwbf = c->wbuf_len;
-       }       
+       }
        if (lwbf > 0)
                memcpy(buf+orbf,c->wbuf+owbf,lwbf);
 
+exit:
+       up_read(&c->wbuf_sem);
        return ret;
 }
 
@@ -857,7 +942,7 @@ int jffs2_check_oob_empty( struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb
                printk(KERN_NOTICE "jffs2_check_oob_empty(): allocation of temporary data buffer for oob check failed\n");
                return -ENOMEM;
        }
-       /* 
+       /*
         * if mode = 0, we scan for a total empty oob area, else we have
         * to take care of the cleanmarker in the first page of the block
        */
@@ -866,41 +951,41 @@ int jffs2_check_oob_empty( struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb
                D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB failed %d for block at %08x\n", ret, jeb->offset));
                goto out;
        }
-       
+
        if (retlen < len) {
                D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB return short read "
                          "(%zd bytes not %d) for block at %08x\n", retlen, len, jeb->offset));
                ret = -EIO;
                goto out;
        }
-       
+
        /* Special check for first page */
        for(i = 0; i < oob_size ; i++) {
                /* Yeah, we know about the cleanmarker. */
-               if (mode && i >= c->fsdata_pos && 
+               if (mode && i >= c->fsdata_pos &&
                    i < c->fsdata_pos + c->fsdata_len)
                        continue;
 
                if (buf[i] != 0xFF) {
                        D2(printk(KERN_DEBUG "Found %02x at %x in OOB for %08x\n",
-                                 buf[page+i], page+i, jeb->offset));
-                       ret = 1; 
+                                 buf[i], i, jeb->offset));
+                       ret = 1;
                        goto out;
                }
        }
 
-       /* we know, we are aligned :) */        
+       /* we know, we are aligned :) */
        for (page = oob_size; page < len; page += sizeof(long)) {
                unsigned long dat = *(unsigned long *)(&buf[page]);
                if(dat != -1) {
-                       ret = 1; 
+                       ret = 1;
                        goto out;
                }
        }
 
 out:
-       kfree(buf);     
-       
+       kfree(buf);
+
        return ret;
 }
 
@@ -982,7 +1067,7 @@ int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c, struct jffs2_erasebloc
        n.totlen = cpu_to_je32(8);
 
        ret = jffs2_flash_write_oob(c, jeb->offset + c->fsdata_pos, c->fsdata_len, &retlen, (unsigned char *)&n);
-       
+
        if (ret) {
                D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): Write failed for block at %08x: error %d\n", jeb->offset, ret));
                return ret;
@@ -994,7 +1079,7 @@ int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c, struct jffs2_erasebloc
        return 0;
 }
 
-/* 
+/*
  * On NAND we try to mark this block bad. If the block was erased more
  * than MAX_ERASE_FAILURES we mark it finaly bad.
  * Don't care about failures. This block remains on the erase-pending
@@ -1015,7 +1100,7 @@ int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *
 
        D1(printk(KERN_WARNING "jffs2_write_nand_badblock(): Marking bad block at %08x\n", bad_offset));
        ret = c->mtd->block_markbad(c->mtd, bad_offset);
-       
+
        if (ret) {
                D1(printk(KERN_WARNING "jffs2_write_nand_badblock(): Write failed for block at %08x: error %d\n", jeb->offset, ret));
                return ret;
@@ -1032,14 +1117,14 @@ static struct nand_oobinfo jffs2_oobinfo_docecc = {
 };
 
 
-int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c)
+static int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c)
 {
        struct nand_oobinfo *oinfo = &c->mtd->oobinfo;
 
        /* Do this only, if we have an oob buffer */
        if (!c->mtd->oobsize)
                return 0;
-       
+
        /* Cleanmarker is out-of-band, so inline size zero */
        c->cleanmarker_size = 0;
 
@@ -1047,7 +1132,7 @@ int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c)
        if (oinfo && oinfo->useecc == MTD_NANDECC_AUTOPLACE) {
                D1(printk(KERN_DEBUG "JFFS2 using autoplace on NAND\n"));
                /* Get the position of the free bytes */
-               if (!oinfo->oobfree[0][0]) {
+               if (!oinfo->oobfree[0][1]) {
                        printk (KERN_WARNING "jffs2_nand_set_oobinfo(): Eeep. Autoplacement selected and no empty space in oob\n");
                        return -ENOSPC;
                }
@@ -1065,7 +1150,7 @@ int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c)
                        c->fsdata_len = NAND_JFFS2_OOB16_FSDALEN;
                        c->badblock_pos = 15;
                        break;
-       
+
                default:
                        D1(printk(KERN_DEBUG "JFFS2 on NAND. No autoplacment info found\n"));
                        return -EINVAL;
@@ -1079,10 +1164,10 @@ int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
        int res;
 
        /* Initialise write buffer */
+       init_rwsem(&c->wbuf_sem);
        c->wbuf_pagesize = c->mtd->oobblock;
        c->wbuf_ofs = 0xFFFFFFFF;
 
-       
        c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
        if (!c->wbuf)
                return -ENOMEM;
@@ -1105,3 +1190,88 @@ void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)
 {
        kfree(c->wbuf);
 }
+
+int jffs2_dataflash_setup(struct jffs2_sb_info *c) {
+       c->cleanmarker_size = 0;                /* No cleanmarkers needed */
+
+       /* Initialize write buffer */
+       init_rwsem(&c->wbuf_sem);
+
+
+       c->wbuf_pagesize =  c->mtd->erasesize;
+
+       /* Find a suitable c->sector_size
+        * - Not too much sectors
+        * - Sectors have to be at least 4 K + some bytes
+        * - All known dataflashes have erase sizes of 528 or 1056
+        * - we take at least 8 eraseblocks and want to have at least 8K size
+        * - The concatenation should be a power of 2
+       */
+
+       c->sector_size = 8 * c->mtd->erasesize;
+
+       while (c->sector_size < 8192) {
+               c->sector_size *= 2;
+       }
+
+       /* It may be necessary to adjust the flash size */
+       c->flash_size = c->mtd->size;
+
+       if ((c->flash_size % c->sector_size) != 0) {
+               c->flash_size = (c->flash_size / c->sector_size) * c->sector_size;
+               printk(KERN_WARNING "JFFS2 flash size adjusted to %dKiB\n", c->flash_size);
+       };
+
+       c->wbuf_ofs = 0xFFFFFFFF;
+       c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
+       if (!c->wbuf)
+               return -ENOMEM;
+
+       printk(KERN_INFO "JFFS2 write-buffering enabled buffer (%d) erasesize (%d)\n", c->wbuf_pagesize, c->sector_size);
+
+       return 0;
+}
+
+void jffs2_dataflash_cleanup(struct jffs2_sb_info *c) {
+       kfree(c->wbuf);
+}
+
+int jffs2_nor_ecc_flash_setup(struct jffs2_sb_info *c) {
+       /* Cleanmarker is actually larger on the flashes */
+       c->cleanmarker_size = 16;
+
+       /* Initialize write buffer */
+       init_rwsem(&c->wbuf_sem);
+       c->wbuf_pagesize = c->mtd->eccsize;
+       c->wbuf_ofs = 0xFFFFFFFF;
+
+       c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
+       if (!c->wbuf)
+               return -ENOMEM;
+
+       return 0;
+}
+
+void jffs2_nor_ecc_flash_cleanup(struct jffs2_sb_info *c) {
+       kfree(c->wbuf);
+}
+
+int jffs2_nor_wbuf_flash_setup(struct jffs2_sb_info *c) {
+       /* Cleanmarker currently occupies a whole programming region */
+       c->cleanmarker_size = MTD_PROGREGION_SIZE(c->mtd);
+
+       /* Initialize write buffer */
+       init_rwsem(&c->wbuf_sem);
+       c->wbuf_pagesize = MTD_PROGREGION_SIZE(c->mtd);
+       c->wbuf_ofs = 0xFFFFFFFF;
+
+       c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
+       if (!c->wbuf)
+               return -ENOMEM;
+
+       return 0;
+}
+
+void jffs2_nor_wbuf_flash_cleanup(struct jffs2_sb_info *c) {
+       kfree(c->wbuf);
+}