[JFFS2] Use MTD_OOB_AUTO to automatically place cleanmarker on NAND

Nowadays MTD supports an MTD_OOB_AUTO option which allows users
to access free bytes in NAND's OOB as a contiguous buffer, although
it may be highly discontinuous.

This patch teaches JFFS2 to use this nice feature instead of the
old MTD_OOB_PLACE option. This for example caused problems with
OneNAND. Now JFFS2 does not care how are the free bytes situated.

This may change position of the clean marker on some flashes,
but this is not a problem. JFFS2 will just re-erase the empty
eraseblocks and write the new (correct) clean marker.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
This commit is contained in:
Artem Bityutskiy 2007-01-31 11:38:53 +02:00 committed by David Woodhouse
parent 73a4421c5a
commit a7a6ace140
3 changed files with 77 additions and 148 deletions

View File

@ -98,20 +98,14 @@ struct jffs2_sb_info {
uint32_t wbuf_pagesize; /* 0 for NOR and other flashes with no wbuf */ uint32_t wbuf_pagesize; /* 0 for NOR and other flashes with no wbuf */
#ifdef CONFIG_JFFS2_FS_WRITEBUFFER #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
/* Write-behind buffer for NAND flash */ unsigned char *wbuf; /* Write-behind buffer for NAND flash */
unsigned char *wbuf;
unsigned char *oobbuf;
uint32_t wbuf_ofs; uint32_t wbuf_ofs;
uint32_t wbuf_len; uint32_t wbuf_len;
struct jffs2_inodirty *wbuf_inodes; struct jffs2_inodirty *wbuf_inodes;
struct rw_semaphore wbuf_sem; /* Protects the write buffer */ struct rw_semaphore wbuf_sem; /* Protects the write buffer */
/* Information about out-of-band area usage... */ unsigned char *oobbuf;
struct nand_ecclayout *ecclayout; int oobavail; /* How many bytes are available for JFFS2 in OOB */
uint32_t badblock_pos;
uint32_t fsdata_pos;
uint32_t fsdata_len;
#endif #endif
struct jffs2_summary *summary; /* Summary information */ struct jffs2_summary *summary; /* Summary information */

View File

@ -450,16 +450,20 @@ static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblo
#ifdef CONFIG_JFFS2_FS_WRITEBUFFER #ifdef CONFIG_JFFS2_FS_WRITEBUFFER
if (jffs2_cleanmarker_oob(c)) { if (jffs2_cleanmarker_oob(c)) {
int ret = jffs2_check_nand_cleanmarker(c, jeb); int ret;
if (c->mtd->block_isbad(c->mtd, jeb->offset))
return BLK_STATE_BADBLOCK;
ret = jffs2_check_nand_cleanmarker(c, jeb);
D2(printk(KERN_NOTICE "jffs_check_nand_cleanmarker returned %d\n",ret)); D2(printk(KERN_NOTICE "jffs_check_nand_cleanmarker returned %d\n",ret));
/* Even if it's not found, we still scan to see /* Even if it's not found, we still scan to see
if the block is empty. We use this information if the block is empty. We use this information
to decide whether to erase it or not. */ to decide whether to erase it or not. */
switch (ret) { switch (ret) {
case 0: cleanmarkerfound = 1; break; case 0: cleanmarkerfound = 1; break;
case 1: break; case 1: break;
case 2: return BLK_STATE_BADBLOCK;
case 3: return BLK_STATE_ALLDIRTY; /* Block has failed to erase min. once */
default: return ret; default: return ret;
} }
} }

View File

@ -957,43 +957,48 @@ exit:
return ret; return ret;
} }
#define NR_OOB_SCAN_PAGES 4 #define NR_OOB_SCAN_PAGES 4
/* For historical reasons we use only 12 bytes for OOB clean marker */
#define OOB_CM_SIZE 12
static const struct jffs2_unknown_node oob_cleanmarker =
{
.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK),
.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER),
.totlen = cpu_to_je32(8)
};
/* /*
* Check, if the out of band area is empty * Check, if the out of band area is empty. This function knows about the clean
* marker and if it is present in OOB, treats the OOB as empty anyway.
*/ */
int jffs2_check_oob_empty(struct jffs2_sb_info *c, int jffs2_check_oob_empty(struct jffs2_sb_info *c,
struct jffs2_eraseblock *jeb, int mode) struct jffs2_eraseblock *jeb, int mode)
{ {
int i, page, ret; int i, ret;
int oobsize = c->mtd->oobsize; int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
struct mtd_oob_ops ops; struct mtd_oob_ops ops;
ops.ooblen = NR_OOB_SCAN_PAGES * oobsize; ops.mode = MTD_OOB_AUTO;
ops.ooblen = NR_OOB_SCAN_PAGES * c->oobavail;
ops.oobbuf = c->oobbuf; ops.oobbuf = c->oobbuf;
ops.ooboffs = 0; ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
ops.datbuf = NULL; ops.datbuf = NULL;
ops.mode = MTD_OOB_PLACE;
ret = c->mtd->read_oob(c->mtd, jeb->offset, &ops); ret = c->mtd->read_oob(c->mtd, jeb->offset, &ops);
if (ret) { if (ret || ops.oobretlen != ops.ooblen) {
D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB " printk(KERN_ERR "cannot read OOB for EB at %08x, requested %d "
"failed %d for block at %08x\n", ret, jeb->offset)); "bytes, read %d bytes, error %d\n", jeb->offset,
ops.ooblen, ops.oobretlen, ret);
if (!ret)
ret = -EIO;
return ret; return ret;
} }
if (ops.oobretlen < ops.ooblen) { for(i = 0; i < ops.ooblen; i++) {
D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Read OOB " if (mode && i < cmlen)
"returned short read (%zd bytes not %d) for block " /* Yeah, we know about the cleanmarker */
"at %08x\n", ops.oobretlen, ops.ooblen, jeb->offset));
return -EIO;
}
/* Special check for first page */
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; continue;
if (ops.oobbuf[i] != 0xFF) { if (ops.oobbuf[i] != 0xFF) {
@ -1003,111 +1008,63 @@ int jffs2_check_oob_empty(struct jffs2_sb_info *c,
} }
} }
/* we know, we are aligned :) */
for (page = oobsize; page < ops.ooblen; page += sizeof(long)) {
long dat = *(long *)(&ops.oobbuf[page]);
if(dat != -1)
return 1;
}
return 0; return 0;
} }
/* /*
* Scan for a valid cleanmarker and for bad blocks * Check for a valid cleanmarker.
* Returns: 0 if a valid cleanmarker was found
* 1 if no cleanmarker was found
* negative error code if an error occurred
*/ */
int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c, int jffs2_check_nand_cleanmarker(struct jffs2_sb_info *c,
struct jffs2_eraseblock *jeb) struct jffs2_eraseblock *jeb)
{ {
struct jffs2_unknown_node n;
struct mtd_oob_ops ops; struct mtd_oob_ops ops;
int oobsize = c->mtd->oobsize; int ret, cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
unsigned char *p,*b;
int i, ret;
size_t offset = jeb->offset;
/* Check first if the block is bad. */ ops.mode = MTD_OOB_AUTO;
if (c->mtd->block_isbad(c->mtd, offset)) { ops.ooblen = cmlen;
D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker()"
": Bad block at %08x\n", jeb->offset));
return 2;
}
ops.ooblen = oobsize;
ops.oobbuf = c->oobbuf; ops.oobbuf = c->oobbuf;
ops.ooboffs = 0; ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
ops.datbuf = NULL; ops.datbuf = NULL;
ops.mode = MTD_OOB_PLACE;
ret = c->mtd->read_oob(c->mtd, offset, &ops); ret = c->mtd->read_oob(c->mtd, jeb->offset, &ops);
if (ret) { if (ret || ops.oobretlen != ops.ooblen) {
D1 (printk(KERN_WARNING "jffs2_check_nand_cleanmarker(): " printk(KERN_ERR "cannot read OOB for EB at %08x, requested %d "
"Read OOB failed %d for block at %08x\n", "bytes, read %d bytes, error %d\n", jeb->offset,
ret, jeb->offset)); ops.ooblen, ops.oobretlen, ret);
if (!ret)
ret = -EIO;
return ret; return ret;
} }
if (ops.oobretlen < ops.ooblen) { return !!memcmp(&oob_cleanmarker, c->oobbuf, cmlen);
D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): "
"Read OOB return short read (%zd bytes not %d) "
"for block at %08x\n", ops.oobretlen, ops.ooblen,
jeb->offset));
return -EIO;
}
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, int jffs2_write_nand_cleanmarker(struct jffs2_sb_info *c,
struct jffs2_eraseblock *jeb) struct jffs2_eraseblock *jeb)
{ {
struct jffs2_unknown_node n; int ret;
int ret;
struct mtd_oob_ops ops; struct mtd_oob_ops ops;
int cmlen = min_t(int, c->oobavail, OOB_CM_SIZE);
n.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK); ops.mode = MTD_OOB_AUTO;
n.nodetype = cpu_to_je16(JFFS2_NODETYPE_CLEANMARKER); ops.ooblen = cmlen;
n.totlen = cpu_to_je32(8); ops.oobbuf = (uint8_t *)&oob_cleanmarker;
ops.len = ops.ooboffs = ops.retlen = ops.oobretlen = 0;
ops.ooblen = c->fsdata_len;
ops.oobbuf = (uint8_t *)&n;
ops.ooboffs = c->fsdata_pos;
ops.datbuf = NULL; ops.datbuf = NULL;
ops.mode = MTD_OOB_PLACE;
ret = c->mtd->write_oob(c->mtd, jeb->offset, &ops); ret = c->mtd->write_oob(c->mtd, jeb->offset, &ops);
if (ret || ops.oobretlen != ops.ooblen) {
if (ret) { printk(KERN_ERR "cannot write OOB for EB at %08x, requested %d "
D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): " "bytes, read %d bytes, error %d\n", jeb->offset,
"Write failed for block at %08x: error %d\n", ops.ooblen, ops.oobretlen, ret);
jeb->offset, ret)); if (!ret)
ret = -EIO;
return ret; return ret;
} }
if (ops.oobretlen != ops.ooblen) {
D1(printk(KERN_WARNING "jffs2_write_nand_cleanmarker(): "
"Short write for block at %08x: %zd not %d\n",
jeb->offset, ops.oobretlen, ops.ooblen));
return -EIO;
}
return 0; return 0;
} }
@ -1140,41 +1097,24 @@ int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *
return 1; return 1;
} }
static int jffs2_nand_set_oobinfo(struct jffs2_sb_info *c) int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
{ {
struct nand_ecclayout *oinfo = c->mtd->ecclayout; struct nand_ecclayout *oinfo = c->mtd->ecclayout;
/* Do this only, if we have an oob buffer */
if (!c->mtd->oobsize) if (!c->mtd->oobsize)
return 0; return 0;
/* Cleanmarker is out-of-band, so inline size zero */ /* Cleanmarker is out-of-band, so inline size zero */
c->cleanmarker_size = 0; c->cleanmarker_size = 0;
/* Should we use autoplacement ? */ if (!oinfo || oinfo->oobavail == 0) {
if (!oinfo) { printk(KERN_ERR "inconsistent device description\n");
D1(printk(KERN_DEBUG "JFFS2 on NAND. No autoplacment info found\n"));
return -EINVAL; return -EINVAL;
} }
D1(printk(KERN_DEBUG "JFFS2 using autoplace on NAND\n")); D1(printk(KERN_DEBUG "JFFS2 using OOB 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; c->oobavail = oinfo->oobavail;
}
int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
{
int res;
/* Initialise write buffer */ /* Initialise write buffer */
init_rwsem(&c->wbuf_sem); init_rwsem(&c->wbuf_sem);
@ -1185,22 +1125,13 @@ int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
if (!c->wbuf) if (!c->wbuf)
return -ENOMEM; return -ENOMEM;
c->oobbuf = kmalloc(NR_OOB_SCAN_PAGES * c->mtd->oobsize, GFP_KERNEL); c->oobbuf = kmalloc(NR_OOB_SCAN_PAGES * c->oobavail, GFP_KERNEL);
if (!c->oobbuf) if (!c->oobbuf) {
return -ENOMEM;
res = jffs2_nand_set_oobinfo(c);
#ifdef BREAKME
if (!brokenbuf)
brokenbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
if (!brokenbuf) {
kfree(c->wbuf); kfree(c->wbuf);
return -ENOMEM; return -ENOMEM;
} }
memset(brokenbuf, 0xdb, c->wbuf_pagesize);
#endif return 0;
return res;
} }
void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c) void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)