diff mbox

[RESEND,v2] mtd: nand: nand_bbt: scan for next free bbt block if writing bbt fails

Message ID 1368714102-8399-1-git-send-email-jeff.westfahl@ni.com
State Superseded
Headers show

Commit Message

Jeff Westfahl May 16, 2013, 2:21 p.m. UTC
If erasing or writing the BBT fails, we should mark the current BBT block
as bad and use the BBT descriptor to scan for the next available unused
block in the BBT. We should only return a failure if there isn't any space
left.

Signed-off-by: Jeff Westfahl <jeff.westfahl@ni.com>
---
 drivers/mtd/nand/nand_bbt.c |   21 +++++++++++++++++----
 1 files changed, 17 insertions(+), 4 deletions(-)

Comments

Artem Bityutskiy July 1, 2013, 8:32 a.m. UTC | #1
On Thu, 2013-05-16 at 09:21 -0500, Jeff Westfahl wrote:
> If erasing or writing the BBT fails, we should mark the current BBT block
> as bad and use the BBT descriptor to scan for the next available unused
> block in the BBT. We should only return a failure if there isn't any space
> left.
> 
> Signed-off-by: Jeff Westfahl <jeff.westfahl@ni.com>

Hi Brian, I think this is another "Brien gook a glimpse" case :-) You
are our bad blocks expert nowadays, and if you could help reviewing this
patch, it would be cool.
diff mbox

Patch

diff --git a/drivers/mtd/nand/nand_bbt.c b/drivers/mtd/nand/nand_bbt.c
index 2672643..900bda1 100644
--- a/drivers/mtd/nand/nand_bbt.c
+++ b/drivers/mtd/nand/nand_bbt.c
@@ -697,6 +697,7 @@  static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
 			page = td->pages[chip];
 			goto write;
 		}
+	next:
 
 		/*
 		 * Automatic placement of the bad block table. Search direction
@@ -831,14 +832,26 @@  static int write_bbt(struct mtd_info *mtd, uint8_t *buf,
 		einfo.addr = to;
 		einfo.len = 1 << this->bbt_erase_shift;
 		res = nand_erase_nand(mtd, &einfo, 1);
-		if (res < 0)
-			goto outerr;
+		if (res < 0) {
+			/* This block is bad. Mark it as such and see if there's
+			   another available in the BBT area. */
+			int block = page >>
+				(this->bbt_erase_shift - this->page_shift);
+			this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
+			goto next;
+		}
 
 		res = scan_write_bbt(mtd, to, len, buf,
 				td->options & NAND_BBT_NO_OOB ? NULL :
 				&buf[len]);
-		if (res < 0)
-			goto outerr;
+		if (res < 0) {
+			/* This block is bad. Mark it as such and see if there's
+			   another available in the BBT area. */
+			int block = page >>
+				(this->bbt_erase_shift - this->page_shift);
+			this->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
+			goto next;
+		}
 
 		pr_info("Bad block table written to 0x%012llx, version 0x%02X\n",
 			 (unsigned long long)to, td->version[chip]);