* JFFS2 -- Journalling Flash File System, Version 2.
*
* Copyright (C) 2001-2003 Red Hat, Inc.
+ * Copyright (C) 2004 Thomas Gleixner <tglx@linutronix.de>
*
* Created by David Woodhouse <dwmw2@redhat.com>
+ * 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.53 2003/10/11 11:46:09 dwmw2 Exp $
+ * $Id: wbuf.c,v 1.70 2004/07/13 08:58:25 dwmw2 Exp $
*
*/
#endif
/* max. erase failures before we mark a block bad */
-#define MAX_ERASE_FAILURES 5
+#define MAX_ERASE_FAILURES 2
/* two seconds timeout for timed wbuf-flushing */
#define WBUF_FLUSH_TIMEOUT 2 * HZ
first_raw = &jeb->first_node;
while (*first_raw &&
(ref_obsolete(*first_raw) ||
- (ref_offset(*first_raw) + (*first_raw)->totlen) < c->wbuf_ofs)) {
+ (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",
ref_offset(*first_raw), ref_flags(*first_raw),
- (ref_offset(*first_raw) + (*first_raw)->totlen),
+ (ref_offset(*first_raw) + ref_totlen(c, jeb, *first_raw)),
c->wbuf_ofs));
first_raw = &(*first_raw)->next_phys;
}
}
start = ref_offset(*first_raw);
- end = ref_offset(*first_raw) + (*first_raw)->totlen;
+ end = ref_offset(*first_raw) + ref_totlen(c, jeb, *first_raw);
/* Find the last node to be recovered */
raw = first_raw;
while ((*raw)) {
if (!ref_obsolete(*raw))
- end = ref_offset(*raw) + (*raw)->totlen;
+ end = ref_offset(*raw) + ref_totlen(c, jeb, *raw);
raw = &(*raw)->next_phys;
}
return;
raw2->flash_offset = ofs | REF_OBSOLETE;
- raw2->totlen = (*first_raw)->totlen;
+ raw2->__totlen = ref_totlen(c, jeb, *first_raw);
raw2->next_phys = NULL;
raw2->next_in_ino = NULL;
raw = first_raw;
while (*raw) {
+ uint32_t rawlen = ref_totlen(c, jeb, *raw);
+
D1(printk(KERN_DEBUG "Refiling block of %08x at %08x(%d) to %08x\n",
- (*raw)->totlen, ref_offset(*raw), ref_flags(*raw), ofs));
+ rawlen, ref_offset(*raw), ref_flags(*raw), ofs));
if (ref_obsolete(*raw)) {
/* Shouldn't really happen much */
- new_jeb->dirty_size += (*raw)->totlen;
- new_jeb->free_size -= (*raw)->totlen;
- c->dirty_size += (*raw)->totlen;
+ new_jeb->dirty_size += rawlen;
+ new_jeb->free_size -= rawlen;
+ c->dirty_size += rawlen;
} else {
- new_jeb->used_size += (*raw)->totlen;
- new_jeb->free_size -= (*raw)->totlen;
- jeb->dirty_size += (*raw)->totlen;
- jeb->used_size -= (*raw)->totlen;
- c->dirty_size += (*raw)->totlen;
+ new_jeb->used_size += rawlen;
+ new_jeb->free_size -= rawlen;
+ jeb->dirty_size += rawlen;
+ jeb->used_size -= rawlen;
+ c->dirty_size += rawlen;
}
- c->free_size -= (*raw)->totlen;
+ c->free_size -= rawlen;
(*raw)->flash_offset = ofs | ref_flags(*raw);
- ofs += (*raw)->totlen;
+ ofs += rawlen;
new_jeb->last_node = *raw;
raw = &(*raw)->next_phys;
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
return ret;
}
- /* Adjusting free size of next block only, if it's called from fsync ! */
- if (pad == 2) {
- D1(printk(KERN_DEBUG "jffs2_flush_wbuf() adjusting free_size of c->nextblock\n"));
- spin_lock(&c->erase_completion_lock);
- if (!c->nextblock)
- BUG();
+ spin_lock(&c->erase_completion_lock);
+
+ /* Adjust free size of the block if we padded. */
+ if (pad) {
+ struct jffs2_eraseblock *jeb;
+
+ jeb = &c->blocks[c->wbuf_ofs / c->sector_size];
+
+ 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
padded. If there is less free space in the block than that,
something screwed up */
- if (c->nextblock->free_size < (c->wbuf_pagesize - c->wbuf_len)) {
+ if (jeb->free_size < (c->wbuf_pagesize - c->wbuf_len)) {
printk(KERN_CRIT "jffs2_flush_wbuf(): Accounting error. wbuf at 0x%08x has 0x%03x bytes, 0x%03x left.\n",
c->wbuf_ofs, c->wbuf_len, c->wbuf_pagesize-c->wbuf_len);
printk(KERN_CRIT "jffs2_flush_wbuf(): But free_size for block at 0x%08x is only 0x%08x\n",
- c->nextblock->offset, c->nextblock->free_size);
+ jeb->offset, jeb->free_size);
BUG();
}
- c->nextblock->free_size -= (c->wbuf_pagesize - c->wbuf_len);
+ jeb->free_size -= (c->wbuf_pagesize - c->wbuf_len);
c->free_size -= (c->wbuf_pagesize - c->wbuf_len);
- c->nextblock->wasted_size += (c->wbuf_pagesize - c->wbuf_len);
+ jeb->wasted_size += (c->wbuf_pagesize - c->wbuf_len);
c->wasted_size += (c->wbuf_pagesize - c->wbuf_len);
- spin_unlock(&c->erase_completion_lock);
}
/* Stick any now-obsoleted blocks on the erase_pending_list */
- spin_lock(&c->erase_completion_lock);
jffs2_refile_wbuf_blocks(c);
jffs2_clear_wbuf_ino_list(c);
spin_unlock(&c->erase_completion_lock);
old_wbuf_ofs = c->wbuf_ofs;
old_wbuf_len = c->wbuf_len;
- while (old_wbuf_len &&
- old_wbuf_ofs == c->wbuf_ofs) {
+ 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);
+ } else while (old_wbuf_len &&
+ old_wbuf_ofs == c->wbuf_ofs) {
up(&c->alloc_sem);
size_t retlen;
int oob_size;
- oob_size = c->mtd->oobsize;
-
/* 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) {
goto out;
}
- /* Special check for first two pages */
- for (page = 0; page < 2 * oob_size; page += oob_size) {
- /* Check for bad block marker */
- if (buf[page+c->badblock_pos] != 0xff) {
- D1(printk(KERN_WARNING "jffs2_check_oob_empty(): Bad or failed block at %08x\n",jeb->offset));
- /* Return 2 for bad and 3 for failed block
- bad goes to list_bad and failed to list_erase */
- ret = (!page) ? 2 : 3;
+ /* Special check for first page */
+ for(i = 0; i < oob_size ; 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[page+i], page+i, jeb->offset));
+ ret = 1;
goto out;
}
- for(i = 0; i < oob_size ; i++) {
- /* Yeah, we know about the cleanmarker. */
- if (mode && i >= c->fsdata_pos &&
- i < c->fsdata_pos+c->fsdata_len)
- continue;
-
- if (buf[page+i] != 0xFF) {
- D2(printk(KERN_DEBUG "Found %02x at %x in OOB for %08x\n",
- buf[page+i], page+i, jeb->offset));
- ret = 1;
- goto out;
- }
- }
- /* only the first page can contain a cleanmarker !*/
- mode = 0;
- }
+ }
/* we know, we are aligned :) */
- for (; page < len; page += sizeof(long)) {
+ for (page = oob_size; page < len; page += sizeof(long)) {
unsigned long dat = *(unsigned long *)(&buf[page]);
if(dat != -1) {
ret = 1;
int jffs2_check_nand_cleanmarker (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
{
struct jffs2_unknown_node n;
- unsigned char buf[32];
+ unsigned char buf[2 * NAND_MAX_OOBSIZE];
unsigned char *p;
int ret, i, cnt, retval = 0;
size_t retlen, offset;
/* 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
return -EIO;
}
- /* Check for bad block marker */
- if (buf[c->badblock_pos] != 0xff) {
- D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Bad block at %08x (has %02x %02x in badblock_pos %d\n",
- jeb->offset, buf[c->badblock_pos], buf[c->badblock_pos + oob_size], c->badblock_pos));
- return 2;
- }
-
- /* Check for failure counter in the second page */
- if (buf[c->badblock_pos + oob_size] != 0xff) {
- D1 (printk (KERN_WARNING "jffs2_check_nand_cleanmarker(): Block marked as failed at %08x, fail count:%d\n", jeb->offset, buf[c->badblock_pos + oob_size]));
- return 3;
- }
-
/* Check cleanmarker only on the first physical block */
if (!cnt) {
n.magic = cpu_to_je16 (JFFS2_MAGIC_BITMASK);
}
/*
- * We try to get the failure count of this block.
- */
-int jffs2_nand_read_failcnt(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) {
-
- unsigned char buf[16];
- int ret;
- size_t retlen;
- int oob_size;
-
- oob_size = c->mtd->oobsize;
-
- ret = c->mtd->read_oob(c->mtd, jeb->offset + c->mtd->oobblock, oob_size , &retlen, buf);
-
- if (ret) {
- D1(printk(KERN_WARNING "jffs2_nand_read_failcnt(): Read OOB failed %d for block at %08x\n", ret, jeb->offset));
- return ret;
- }
-
- if (retlen < oob_size) {
- D1(printk(KERN_WARNING "jffs2_nand_read_failcnt(): Read OOB return short read (%zd bytes not %d) for block at %08x\n", retlen, oob_size, jeb->offset));
- return -EIO;
- }
-
- jeb->bad_count = buf[c->badblock_pos];
- return 0;
-}
-
-/*
- * On NAND we try to mark this block bad. We try to write how often
- * the block was erased and mark it finaly bad, if the count
- * is > MAX_ERASE_FAILURES. We read this information on mount !
- * jeb->bad_count contains the count before this erase.
+ * 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
* or badblock list as long as nobody manipulates the flash with
* a bootloader or something like that.
*/
-int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
+int jffs2_write_nand_badblock(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, uint32_t bad_offset)
{
- unsigned char buf = 0x0;
int ret;
- size_t retlen;
/* if the count is < max, we try to write the counter to the 2nd page oob area */
- if( ++jeb->bad_count < MAX_ERASE_FAILURES) {
- buf = (unsigned char)jeb->bad_count;
- c->badblock_pos += c->mtd->oobblock;
- }
-
- ret = jffs2_flash_write_oob(c, jeb->offset + c->badblock_pos, 1, &retlen, &buf);
+ if( ++jeb->bad_count < MAX_ERASE_FAILURES)
+ return 0;
+
+ if (!c->mtd->block_markbad)
+ return 1; // What else can we do?
+
+ 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;
}
- if (retlen != 1) {
- D1(printk(KERN_WARNING "jffs2_write_nand_badblock(): Short write for block at %08x: %zd not 1\n", jeb->offset, retlen));
- return ret;
- }
- return 0;
+ return 1;
}
-#define JFFS2_OOB_ECCPOS0 0
-#define JFFS2_OOB_ECCPOS1 1
-#define JFFS2_OOB_ECCPOS2 2
-#define JFFS2_OOB_ECCPOS3 3
-#define JFFS2_OOB_ECCPOS4 6
-#define JFFS2_OOB_ECCPOS5 7
-
-#define NAND_JFFS2_OOB8_FSDAPOS 6
-#define NAND_JFFS2_OOB16_FSDAPOS 8
-#define NAND_JFFS2_OOB8_FSDALEN 2
#define NAND_JFFS2_OOB16_FSDALEN 8
-static struct nand_oobinfo jffs2_oobinfo_swecc = {
- .useecc = 1,
- .eccpos = {JFFS2_OOB_ECCPOS0, JFFS2_OOB_ECCPOS1, JFFS2_OOB_ECCPOS2,
- JFFS2_OOB_ECCPOS3, JFFS2_OOB_ECCPOS4, JFFS2_OOB_ECCPOS5}
-};
-
static struct nand_oobinfo jffs2_oobinfo_docecc = {
- .useecc = 1,
+ .useecc = MTD_NANDECC_PLACE,
+ .eccbytes = 6,
.eccpos = {0,1,2,3,4,5}
};
-
-int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
+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;
- /* Initialise write buffer */
- c->wbuf_pagesize = c->mtd->oobblock;
- c->wbuf_ofs = 0xFFFFFFFF;
-
- /* FIXME: If we had a generic way of describing the hardware's
- use of OOB area, we could perhaps make this generic too. */
- switch(c->mtd->ecctype) {
- case MTD_ECC_SW:
- D1(printk(KERN_DEBUG "JFFS2 using software ECC\n"));
- c->oobinfo = &jffs2_oobinfo_swecc;
- if (c->mtd->oobsize == 8) {
- c->fsdata_pos = NAND_JFFS2_OOB8_FSDAPOS;
- c->fsdata_len = NAND_JFFS2_OOB8_FSDALEN;
- } else {
- c->fsdata_pos = NAND_JFFS2_OOB16_FSDAPOS;
+ /* 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][0]) {
+ 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;
+
+ default:
+ D1(printk(KERN_DEBUG "JFFS2 on NAND. No autoplacment info found\n"));
+ return -EINVAL;
}
- c->badblock_pos = NAND_BADBLOCK_POS;
- break;
-
- case MTD_ECC_RS_DiskOnChip:
- D1(printk(KERN_DEBUG "JFFS2 using DiskOnChip hardware ECC\n"));
- c->oobinfo = &jffs2_oobinfo_docecc;
- c->fsdata_pos = 6;
- c->fsdata_len = NAND_JFFS2_OOB16_FSDALEN;
- c->badblock_pos = 15;
- break;
-
- default:
- printk("JFFS2 doesn't yet know how to handle ECC type %d\n",
- c->mtd->ecctype);
- return -EINVAL;
}
+ return 0;
+}
+int jffs2_nand_flash_setup(struct jffs2_sb_info *c)
+{
+ int res;
+
+ /* Initialise write buffer */
+ c->wbuf_pagesize = c->mtd->oobblock;
+ c->wbuf_ofs = 0xFFFFFFFF;
+
+
c->wbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
if (!c->wbuf)
return -ENOMEM;
+ res = jffs2_nand_set_oobinfo(c);
+
#ifdef BREAKME
if (!brokenbuf)
brokenbuf = kmalloc(c->wbuf_pagesize, GFP_KERNEL);
}
memset(brokenbuf, 0xdb, c->wbuf_pagesize);
#endif
- return 0;
+ return res;
}
void jffs2_nand_flash_cleanup(struct jffs2_sb_info *c)