]> err.no Git - linux-2.6/blobdiff - fs/jffs2/wbuf.c
[PATCH] VFS: Permit filesystem to perform statfs with a known root dentry
[linux-2.6] / fs / jffs2 / wbuf.c
index dc275cedfe4a6d5b92c2ffb855ef1ed4778c39d7..a7f153f79ecb2436be0ef07dfc54cd7b8fcbc160 100644 (file)
@@ -296,10 +296,11 @@ static void jffs2_wbuf_recover(struct jffs2_sb_info *c)
                /* Do the read... */
                ret = c->mtd->read(c->mtd, start, c->wbuf_ofs - start, &retlen, buf);
 
-               if (ret == -EBADMSG && retlen == c->wbuf_ofs - start) {
-                       /* ECC recovered */
+               /* ECC recovered ? */
+               if ((ret == -EUCLEAN || ret == -EBADMSG) &&
+                   (retlen == c->wbuf_ofs - start))
                        ret = 0;
-               }
+
                if (ret || retlen != c->wbuf_ofs - start) {
                        printk(KERN_CRIT "Old data are already lost in wbuf recovery. Data loss ensues.\n");
 
@@ -908,20 +909,21 @@ int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *re
        down_read(&c->wbuf_sem);
        ret = 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);
+       if ( (ret == -EBADMSG || ret == -EUCLEAN) && (*retlen == len) ) {
+               if (ret == -EBADMSG)
+                       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.
+                * 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;
+               ret = 0;
        }
 
        /* if no writebuffer available or write buffer empty, return */
@@ -943,7 +945,7 @@ int jffs2_flash_read(struct jffs2_sb_info *c, loff_t ofs, size_t len, size_t *re
                orbf = (c->wbuf_ofs - ofs);     /* offset in read buffer */
                if (orbf > len)                 /* is write beyond write buffer ? */
                        goto exit;
-               lwbf = len - orbf;              /* number of bytes to copy */
+               lwbf = len - orbf;              /* number of bytes to copy */
                if (lwbf > c->wbuf_len)
                        lwbf = c->wbuf_len;
        }
@@ -955,158 +957,159 @@ exit:
        return ret;
 }
 
+#define NR_OOB_SCAN_PAGES      4
+
 /*
- *     Check, if the out of band area is empty
+ * Check, if the out of band area is empty
  */
-int jffs2_check_oob_empty( struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, int mode)
+int jffs2_check_oob_empty(struct jffs2_sb_info *c,
+                         struct jffs2_eraseblock *jeb, int mode)
 {
-       unsigned char *buf;
-       int     ret = 0;
-       int     i,len,page;
-       size_t  retlen;
-       int     oob_size;
-
-       /* allocate a buffer for all oob data in this sector */
-       oob_size = c->mtd->oobsize;
-       len = 4 * oob_size;
-       buf = kmalloc(len, GFP_KERNEL);
-       if (!buf) {
-               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
-       */
-       ret = jffs2_flash_read_oob(c, jeb->offset, len , &retlen, buf);
+       int i, page, ret;
+       int oobsize = c->mtd->oobsize;
+       struct mtd_oob_ops ops;
+
+       ops.len = NR_OOB_SCAN_PAGES * oobsize;
+       ops.ooblen = oobsize;
+       ops.oobbuf = c->oobbuf;
+       ops.ooboffs = 0;
+       ops.datbuf = NULL;
+       ops.mode = MTD_OOB_PLACE;
+
+       ret = c->mtd->read_oob(c->mtd, jeb->offset, &ops);
        if (ret) {
-               D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB failed %d for block at %08x\n", ret, jeb->offset));
-               goto out;
+               D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB "
+                         "failed %d for block at %08x\n", ret, jeb->offset));
+               return ret;
        }
 
-       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;
+       if (ops.retlen < ops.len) {
+               D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB "
+                         "returned short read (%zd bytes not %d) for block "
+                         "at %08x\n", ops.retlen, ops.len, jeb->offset));
+               return -EIO;
        }
 
        /* Special check for first page */
-       for(i = 0; i < oob_size ; i++) {
+       for(i = 0; i < oobsize ; i++) {
                /* Yeah, we know about the cleanmarker. */
                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[i], i, jeb->offset));
-                       ret = 1;
-                       goto out;
+               if (ops.oobbuf[i] != 0xFF) {
+                       D2(printk(KERN_DEBUG "Found %02x at %x in OOB for "
+                                 "%08x\n", ops.oobbuf[i], i, jeb->offset));
+                       return 1;
                }
        }
 
        /* 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;
-                       goto out;
-               }
+       for (page = oobsize; page < ops.len; page += sizeof(long)) {
+               long dat = *(long *)(&ops.oobbuf[page]);
+               if(dat != -1)
+                       return 1;
        }
-
-out:
-       kfree(buf);
-
-       return ret;
+       return 0;
 }
 
 /*
-*      Scan for a valid cleanmarker and for bad blocks
-*      For virtual blocks (concatenated physical blocks) check the cleanmarker
-*      only in the first page of the first physical block, but scan for bad blocks in all
-*      physical blocks
-*/
-int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
+ * Scan for a valid cleanmarker and for bad blocks
+ */
+int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c,
+                                 struct jffs2_eraseblock *jeb)
 {
        struct jffs2_unknown_node n;
-       unsigned char buf[2 * NAND_MAX_OOBSIZE];
-       unsigned char *p;
-       int ret, i, cnt, retval = 0;
-       size_t retlen, offset;
-       int oob_size;
-
-       offset = jeb->offset;
-       oob_size = c->mtd->oobsize;
-
-       /* Loop through the physical blocks */
-       for (cnt = 0; cnt < (c->sector_size / c->mtd->erasesize); cnt++) {
-               /* Check first if the block is bad. */
-               if (c->mtd->block_isbad (c->mtd, offset)) {
-                       D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Bad block at %08x\n", jeb->offset));
-                       return 2;
-               }
-               /*
-                  *    We read oob data from page 0 and 1 of the block.
-                  *    page 0 contains cleanmarker and badblock info
-                  *    page 1 contains failure count of this block
-                */
-               ret = c->mtd->read_oob (c->mtd, offset, oob_size << 1, &retlen, buf);
+       struct mtd_oob_ops ops;
+       int oobsize = c->mtd->oobsize;
+       unsigned char *p,*b;
+       int i, ret;
+       size_t offset = jeb->offset;
+
+       /* Check first if the block is bad. */
+       if (c->mtd->block_isbad(c->mtd, offset)) {
+               D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker()"
+                          ": Bad block at %08x\n", jeb->offset));
+               return 2;
+       }
 
-               if (ret) {
-                       D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Read OOB failed %d for block at %08x\n", ret, jeb->offset));
-                       return ret;
-               }
-               if (retlen < (oob_size << 1)) {
-                       D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Read OOB return short read (%zd bytes not %d) for block at %08x\n", retlen, oob_size << 1, jeb->offset));
-                       return -EIO;
-               }
+       ops.len = oobsize;
+       ops.ooblen = oobsize;
+       ops.oobbuf = c->oobbuf;
+       ops.ooboffs = 0;
+       ops.datbuf = NULL;
+       ops.mode = MTD_OOB_PLACE;
 
-               /* Check cleanmarker only on the first physical block */
-               if (!cnt) {
-                       n.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
-                       n.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
-                       n.totlen = cpu_to_je32 (8);
-                       p = (unsigned char *) &n;
+       ret = c->mtd->read_oob(c->mtd, offset, &ops);
+       if (ret) {
+               D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): "
+                          "Read OOB failed %d for block at %08x\n",
+                          ret, jeb->offset));
+               return ret;
+       }
 
-                       for (i = 0; i < c->fsdata_len; i++) {
-                               if (buf[c->fsdata_pos + i] != p[i]) {
-                                       retval = 1;
-                               }
-                       }
-                       D1(if (retval == 1) {
-                               printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): Cleanmarker node not detected in block at %08x\n", jeb->offset);
-                               printk(KERN_WARNING "OOB at %08zx was ", offset);
-                               for (i=0; i < oob_size; i++) {
-                                       printk("%02x ", buf[i]);
-                               }
-                               printk("\n");
-                       })
-               }
-               offset += c->mtd->erasesize;
+       if (ops.retlen < ops.len) {
+               D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): "
+                           "Read OOB return short read (%zd bytes not %d) "
+                           "for block at %08x\n", ops.retlen, ops.len,
+                           jeb->offset));
+               return -EIO;
        }
-       return retval;
+
+       n.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
+       n.nodetype = cpu_to_je16 (JFFS2_NODETYPE_CLEANMARKER);
+       n.totlen = cpu_to_je32 (8);
+       p = (unsigned char *) &n;
+       b = c->oobbuf + c->fsdata_pos;
+
+       for (i = c->fsdata_len; i; i--) {
+               if (*b++ != *p++)
+                       ret = 1;
+       }
+
+       D1(if (ret == 1) {
+               printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): "
+                      "Cleanmarker node not detected in block at %08x\n",
+                      offset);
+               printk(KERN_WARNING "OOB at %08zx was ", offset);
+               for (i=0; i < oobsize; i++)
+                       printk("%02x ", c->oobbuf[i]);
+               printk("\n");
+       });
+       return ret;
 }
 
-int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
+int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c,
+                                struct jffs2_eraseblock *jeb)
 {
-       struct  jffs2_unknown_node n;
-       int     ret;
-       size_t  retlen;
+       struct jffs2_unknown_node n;
+       int     ret;
+       struct mtd_oob_ops ops;
 
        n.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
        n.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER);
        n.totlen = cpu_to_je32(8);
 
-       ret = jffs2_flash_write_oob(c, jeb->offset + c->fsdata_pos, c->fsdata_len, &retlen, (unsigned char *)&n);
+       ops.len = c->fsdata_len;
+       ops.ooblen = c->fsdata_len;;
+       ops.oobbuf = (uint8_t *)&n;
+       ops.ooboffs = c->fsdata_pos;
+       ops.datbuf = NULL;
+       ops.mode = MTD_OOB_PLACE;
+
+       ret = c->mtd->write_oob(c->mtd, jeb->offset, &ops);
 
        if (ret) {
-               D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): Write failed for block at %08x: error %d\n", jeb->offset, ret));
+               D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): "
+                         "Write failed for block at %08x: error %d\n",
+                         jeb->offset, ret));
                return ret;
        }
-       if (retlen != c->fsdata_len) {
-               D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): Short write for block at %08x: %zd not %d\n", jeb->offset, retlen, c->fsdata_len));
-               return ret;
+       if (ops.retlen != ops.len) {
+               D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): "
+                         "Short write for block at %08x: %zd not %d\n",
+                         jeb->offset, ops.retlen, ops.len));
+               return -EIO;
        }
        return 0;
 }
@@ -1140,18 +1143,9 @@ int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *
        return 1;
 }
 
-#define NAND_JFFS2_OOB16_FSDALEN       8
-
-static struct nand_oobinfo jffs2_oobinfo_docecc = {
-       .useecc = MTD_NANDECC_PLACE,
-       .eccbytes = 6,
-       .eccpos = {0,1,2,3,4,5}
-};
-
-
 static int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c)
 {
-       struct nand_oobinfo *oinfo = c->mtd->oobinfo;
+       struct nand_ecclayout *oinfo = c->mtd->ecclayout;
 
        /* Do this only, if we have an oob buffer */
        if (!c->mtd->oobsize)
@@ -1161,33 +1155,23 @@ static int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c)
        c->cleanmarker_size = 0;
 
        /* Should we use autoplacement ? */
-       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][1]) {
-                       printk (KERN_WARNING "jffs2_nand_set_oobinfo(): Eeep. Autoplacement selected and no empty space in oob\n");
-                       return -ENOSPC;
-               }
-               c->fsdata_pos = oinfo->oobfree[0][0];
-               c->fsdata_len = oinfo->oobfree[0][1];
-               if (c->fsdata_len > 8)
-                       c->fsdata_len = 8;
-       } else {
-               /* This is just a legacy fallback and should go away soon */
-               switch(c->mtd->ecctype) {
-               case MTD_ECC_RS_DiskOnChip:
-                       printk(KERN_WARNING "JFFS2 using DiskOnChip hardware ECC without autoplacement. Fix it!\n");
-                       c->oobinfo = &jffs2_oobinfo_docecc;
-                       c->fsdata_pos = 6;
-                       c->fsdata_len = NAND_JFFS2_OOB16_FSDALEN;
-                       c->badblock_pos = 15;
-                       break;
+       if (!oinfo) {
+               D1(printk(KERN_DEBUG "JFFS2 on NAND. No autoplacment info found\n"));
+               return -EINVAL;
+       }
 
-               default:
-                       D1(printk(KERN_DEBUG "JFFS2 on NAND. No autoplacment info found\n"));
-                       return -EINVAL;
-               }
+       D1(printk(KERN_DEBUG "JFFS2 using autoplace on NAND\n"));
+       /* Get the position of the free bytes */
+       if (!oinfo->oobfree[0].length) {
+               printk (KERN_WARNING "jffs2_nand_set_oobinfo(): Eeep."
+                       " Autoplacement selected and no empty space in oob\n");
+               return -ENOSPC;
        }
+       c->fsdata_pos = oinfo->oobfree[0].offset;
+       c->fsdata_len = oinfo->oobfree[0].length;
+       if (c->fsdata_len > 8)
+               c->fsdata_len = 8;
+
        return 0;
 }
 
@@ -1204,6 +1188,10 @@ int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
        if (!c->wbuf)
                return -ENOMEM;
 
+       c->oobbuf = kmalloc(NR_OOB_SCAN_PAGES * c->mtd->oobsize, GFP_KERNEL);
+       if (!c->oobbuf)
+               return -ENOMEM;
+
        res = jffs2_nand_set_oobinfo(c);
 
 #ifdef BREAKME
@@ -1221,6 +1209,7 @@ int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
 void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)
 {
        kfree(c->wbuf);
+       kfree(c->oobbuf);
 }
 
 int jffs2_dataflash_setup(struct jffs2_sb_info *c) {