Skip to contentSkip to conversation

mtd: nand: fix the written length when nand_write_skip_bad failed

PATCH New Tao Hou 2 participants 8 messages Mar 2, 2013 – Mar 11, 2013

Subsystems (2)
lore
Share & tools
lore

Tao Hou

author
#0 lore raw

PATCH 1/1 mtd: nand: fix the written length when nand_write_skip_bad failed

When the data has been partially written into the NAND Flash, returning the written length instead of 0. The written length may be useful when the upper level decides to continue the writing after skipping the block causing the write failure.

Cc Scott Wood <[email protected]>
Cc Ben Gardiner <[email protected]>
Cc Lei Wen <[email protected]>
---
 drivers/mtd/nand/nand_util.c |   22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
drivers/mtd/nand/nand_util.c +15 -7
index de1d13e..f57d723 100644
@@ -496,8 +496,10 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
 
 #ifdef CONFIG_CMD_NAND_YAFFS
 	if (flags & WITH_YAFFS_OOB) {
-		if (flags & ~WITH_YAFFS_OOB)
+		if (flags & ~WITH_YAFFS_OOB) {
+			*length = 0;
 			return -EINVAL;
+		}
 
 		int pages;
 		pages = nand->erasesize / nand->writesize;
@@ -505,6 +507,7 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
 		if (*length % (nand->writesize + nand->oobsize)) {
 			printf("Attempt to write incomplete page"
 				" in yaffs mode\n");
+			*length = 0;
 			return -EINVAL;
 		}
 	} else
@@ -542,7 +545,6 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
 		if (rval == 0)
 			return 0;
 
-		*length = 0;
 		printf("NAND write to offset %llx failed %d\n",
 			offset, rval);
 		return rval;
@@ -550,7 +552,7 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
 
 	while (left_to_write > 0) {
 		size_t block_offset = offset & (nand->erasesize - 1);
-		size_t write_size, truncated_write_size;
+		size_t write_size, truncated_write_size, written_size;
 
 		WATCHDOG_RESET();
 
@@ -586,8 +588,10 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
 				ops.oobbuf = ops.datbuf + pagesize;
 
 				rval = nand->write_oob(nand, offset, &ops);
-				if (rval != 0)
+				if (rval != 0) {
+					written_size = pagesize_oob * page;
 					break;
+				}
 
 				offset += pagesize;
 				p_buffer += pagesize_oob;
@@ -605,14 +609,18 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length,
 
 			rval = nand_write(nand, offset, &truncated_write_size,
 					p_buffer);
-			offset += write_size;
-			p_buffer += write_size;
+			if (rval == 0) {
+				offset += write_size;
+				p_buffer += write_size;
+			} else {
+				written_size = truncated_write_size;
+			}
 		}
 
 		if (rval != 0) {
 			printf("NAND write to offset %llx failed %d\n",
 				offset, rval);
-			*length -= left_to_write;
+			*length -= left_to_write - written_size;
 			return rval;
 		}
 
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH 1/1 mtd: nand: fix the written length when nand_write_skip_bad failed

Quoted 4 lines · Tao Hou

When the data has been partially written into the NAND Flash, returning the written length instead of 0. The written length may be useful when the upper level decides to continue the writing after skipping the block causing the write failure.

We already do that in some code paths.

Quoted 7 lines
Cc Scott Wood <[email protected]>
Cc Ben Gardiner <[email protected]>
Cc Lei Wen <[email protected]>
---
 drivers/mtd/nand/nand_util.c |   22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

Could you rebase this on top of this patch:
http://patchwork.ozlabs.org/patch/224842/

BTW, are you actually using WITH_YAFFS_OOB? I think there are some other things wrong with it at the moment, as mentioned here:
http://lists.denx.de/pipermail/u-boot/2013-March/148378.html

Quoted 35 lines
drivers/mtd/nand/nand_util.c +3 -1
index de1d13e..f57d723 100644
@@ -496,8 +496,10 @@ int nand_write_skip_bad(nand_info_t *nand,  
 loff_t offset, size_t *length,
 
 #ifdef CONFIG_CMD_NAND_YAFFS
 	if (flags & WITH_YAFFS_OOB) {
-		if (flags & ~WITH_YAFFS_OOB)
+		if (flags & ~WITH_YAFFS_OOB) {
+			*length = 0;
 			return -EINVAL;
+		}
 
 		int pages;
drivers/mtd/nand/nand_util.c +1 -0
 		pages = nand->erasesize / nand->writesize;
@@ -505,6 +507,7 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t  
 offset, size_t *length,
 		if (*length % (nand->writesize + nand->oobsize)) {
 			printf("Attempt to write incomplete page"
 				" in yaffs mode\n");
+			*length = 0;
 			return -EINVAL;
 		}
drivers/mtd/nand/nand_util.c +0 -1
 	} else
@@ -542,7 +545,6 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t  
 offset, size_t *length,
 		if (rval == 0)
 			return 0;
 
-		*length = 0;
 		printf("NAND write to offset %llx failed %d\n",
 			offset, rval);
Quoted 1 line

return rval;

OK so far...

Quoted 47 lines
drivers/mtd/nand/nand_util.c +4 -2
@@ -550,7 +552,7 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t  
 offset, size_t *length,
 
 	while (left_to_write > 0) {
 		size_t block_offset = offset & (nand->erasesize - 1);
-		size_t write_size, truncated_write_size;
+		size_t write_size, truncated_write_size, written_size;
 
 		WATCHDOG_RESET();
@@ -586,8 +588,10 @@ int nand_write_skip_bad(nand_info_t *nand,  
 loff_t offset, size_t *length,
 				ops.oobbuf = ops.datbuf + pagesize;
 
 				rval = nand->write_oob(nand, offset,  
 &ops);
-				if (rval != 0)
+				if (rval != 0) {
+					written_size = pagesize_oob *  
 page;
 					break;
+				}
drivers/mtd/nand/nand_util.c +7 -3
 				offset += pagesize;
 				p_buffer += pagesize_oob;
@@ -605,14 +609,18 @@ int nand_write_skip_bad(nand_info_t *nand,  
 loff_t offset, size_t *length,
 
 			rval = nand_write(nand, offset,  
 &truncated_write_size,
 					p_buffer);
-			offset += write_size;
-			p_buffer += write_size;
+			if (rval == 0) {
+				offset += write_size;
+				p_buffer += write_size;
+			} else {
+				written_size = truncated_write_size;
+			}
 		}
 
 		if (rval != 0) {
 			printf("NAND write to offset %llx failed %d\n",
 				offset, rval);
-			*length -= left_to_write;
+			*length -= left_to_write - written_size;
 			return rval;
Quoted 1 line

}

...but I don't see why this part is needed (or correct). Why doesn't "*length -= left_to_write" already get you what you want?

-Scott

author
#2 lore raw

PATCH 1/1 mtd: nand: fix the written length when nand_write_skip_bad failed

Hi, Scott

Thanks for your review.

Quoted 20 lines · Scott Wood
Quoted 5 lines · Tao Hou

When the data has been partially written into the NAND Flash, returning the written length instead of 0. The written length may be useful when the upper level decides to continue the writing after skipping the block causing the write failure.

We already do that in some code paths.

Quoted 7 lines
Cc Scott Wood <[email protected]>
Cc Ben Gardiner <[email protected]>
Cc Lei Wen <[email protected]>
---
 drivers/mtd/nand/nand_util.c |   22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
Quoted 2 lines

Could you rebase this on top of this patch:
http://patchwork.ozlabs.org/patch/224842/

Do you mean a V2 patch ?

Quoted 3 lines

BTW, are you actually using WITH_YAFFS_OOB? I think there are some other things wrong with it at the moment, as mentioned here:
http://lists.denx.de/pipermail/u-boot/2013-March/148378.html

No, I don't use it.

Quoted 95 lines
Quoted 36 lines
drivers/mtd/nand/nand_util.c +3 -1
index de1d13e..f57d723 100644
@@ -496,8 +496,10 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t
 offset, size_t *length,
 
 #ifdef CONFIG_CMD_NAND_YAFFS
        if (flags & WITH_YAFFS_OOB) {
-               if (flags & ~WITH_YAFFS_OOB)
+               if (flags & ~WITH_YAFFS_OOB) {
+                       *length = 0;
                        return -EINVAL;
+               }
 
                int pages;
                pages = nand->erasesize / nand->writesize;
drivers/mtd/nand/nand_util.c +1 -0
@@ -505,6 +507,7 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t
 offset, size_t *length,
                if (*length % (nand->writesize + nand->oobsize)) {
                        printf("Attempt to write incomplete page"
                                " in yaffs mode\n");
+                       *length = 0;
                        return -EINVAL;
                }
        } else
drivers/mtd/nand/nand_util.c +0 -1
@@ -542,7 +545,6 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t
 offset, size_t *length,
                if (rval == 0)
                        return 0;
 
-               *length = 0;
                printf("NAND write to offset %llx failed %d\n",
                        offset, rval);
Quoted 1 line
                return rval;

OK so far...

Quoted 47 lines
drivers/mtd/nand/nand_util.c +4 -2
@@ -550,7 +552,7 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t
 offset, size_t *length,
 
        while (left_to_write > 0) {
                size_t block_offset = offset & (nand->erasesize - 1);
-               size_t write_size, truncated_write_size;
+               size_t write_size, truncated_write_size, written_size;
 
                WATCHDOG_RESET();
@@ -586,8 +588,10 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t
 offset, size_t *length,
                                ops.oobbuf = ops.datbuf + pagesize;
 
                                rval = nand->write_oob(nand, offset,
 &ops);
-                               if (rval != 0)
+                               if (rval != 0) {
+                                       written_size = pagesize_oob *
 page;
                                        break;
+                               }
                                offset += pagesize;
                                p_buffer += pagesize_oob;
drivers/mtd/nand/nand_util.c +7 -3
@@ -605,14 +609,18 @@ int nand_write_skip_bad(nand_info_t *nand, loff_t
 offset, size_t *length,
 
                        rval = nand_write(nand, offset,
 &truncated_write_size,
                                        p_buffer);
-                       offset += write_size;
-                       p_buffer += write_size;
+                       if (rval == 0) {
+                               offset += write_size;
+                               p_buffer += write_size;
+                       } else {
+                               written_size = truncated_write_size;
+                       }
                }
 
                if (rval != 0) {
                        printf("NAND write to offset %llx failed %d\n",
                                offset, rval);
-                       *length -= left_to_write;
+                       *length -= left_to_write - written_size;
                        return rval;
Quoted 1 line
                }

...but I don't see why this part is needed (or correct). Why doesn't "*length -= left_to_write" already get you what you want?

Quoted 1 line

-Scott

I just use "*length -= left_to_write - written_size" to tell the upper level that what
had been actually happened. For the current block, "written_size" has been written to the NAND Flash yet, so left_to_write should be subtracted by "written_size".

Cheers,
Hou.

#3 lore raw

PATCH 1/1 mtd: nand: fix the written length when nand_write_skip_bad failed

Quoted 26 lines · htbegin

Hi, Scott

Thanks for your review.

Quoted 20 lines · Scott Wood
Quoted 5 lines · Tao Hou

When the data has been partially written into the NAND Flash, returning the written length instead of 0. The written length may be useful when the upper level decides to continue the writing after skipping the block causing the write failure.

We already do that in some code paths.

Quoted 7 lines
Cc Scott Wood <[email protected]>
Cc Ben Gardiner <[email protected]>
Cc Lei Wen <[email protected]>
---
 drivers/mtd/nand/nand_util.c |   22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
Quoted 3 lines
Quoted 2 lines

Could you rebase this on top of this patch:
http://patchwork.ozlabs.org/patch/224842/

Do you mean a V2 patch ?

Yes.

Quoted 5 lines
Quoted 1 line

BTW, are you actually using WITH_YAFFS_OOB? I think there are some

other

Quoted 2 lines

things wrong with it at the moment, as mentioned here:
http://lists.denx.de/pipermail/u-boot/2013-March/148378.html

No, I don't use it.

Changes to that code should be tested by someone...

Quoted 12 lines
Quoted 2 lines
Quoted 2 lines

if (rval != 0) {
printf("NAND write to offset %llx failed

%d\n",

Quoted 8 lines

drivers/mtd/nand/nand_util.c int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length, · line 625

Quoted 5 lines
                                offset, rval);
drivers/mtd/nand/nand_util.c +1 -1
-                       *length -= left_to_write;
+                       *length -= left_to_write - written_size;
                        return rval;
                }

...but I don't see why this part is needed (or correct). Why

doesn't

Quoted 8 lines
Quoted 3 lines

"*length -= left_to_write" already get you what you want?

-Scott

I just use "*length -= left_to_write - written_size" to tell the upper level that what
had been actually happened. For the current block, "written_size" has been written to the NAND Flash yet, so left_to_write should be subtracted by "written_size".

But left_to_write isn't decreased until after this error return, so that's already the case. Subtracting written_size from left_to_write has the effect of increasing length by written_size, so the return value will now look like the error page has been written.

-Scott

author
#4 lore raw

PATCH 1/1 mtd: nand: fix the written length when nand_write_skip_bad failed

Hi, Scott

Quoted 28 lines · Scott Wood
Quoted 27 lines · htbegin

Hi, Scott

Thanks for your review.

Quoted 20 lines · Scott Wood
Quoted 5 lines · Tao Hou

When the data has been partially written into the NAND Flash, returning the written length instead of 0. The written length may be useful when the upper level decides to continue the writing after skipping the block causing the write failure.

We already do that in some code paths.

Quoted 7 lines
Cc Scott Wood <[email protected]>
Cc Ben Gardiner <[email protected]>
Cc Lei Wen <[email protected]>
---
 drivers/mtd/nand/nand_util.c |   22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)
Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

Could you rebase this on top of this patch:
http://patchwork.ozlabs.org/patch/224842/

Do you mean a V2 patch ?

Yes.

I will send a V2 patch once we reach an agreement on the "written_length" problem.

Quoted 10 lines
Quoted 7 lines
Quoted 6 lines

BTW, are you actually using WITH_YAFFS_OOB? I think there are some other
things wrong with it at the moment, as mentioned here:
http://lists.denx.de/pipermail/u-boot/2013-March/148378.html

No, I don't use it.

Changes to that code should be tested by someone...

Sorry, I can't help.

Quoted 22 lines
Quoted 14 lines
Quoted 14 lines

drivers/mtd/nand/nand_util.c int nand_write_skip_bad(nand_info_t *nand, loff_t offset, size_t *length, · line 625

Quoted 9 lines

                if (rval != 0) {
                        printf("NAND write to offset %llx failed %d\n",
                                offset, rval);
drivers/mtd/nand/nand_util.c +1 -1
-                       *length -= left_to_write;
+                       *length -= left_to_write - written_size;
                        return rval;
                }

...but I don't see why this part is needed (or correct). Why doesn't "*length -= left_to_write" already get you what you want?

Quoted 6 lines
Quoted 1 line

-Scott

I just use "*length -= left_to_write - written_size" to tell the upper level that what
had been actually happened. For the current block, "written_size" has been written to the NAND Flash yet, so left_to_write should be subtracted by "written_size".

Quoted 6 lines

But left_to_write isn't decreased until after this error return, so that's already the case. Subtracting written_size from left_to_write has the effect of increasing length by written_size, so the return value will now look like the error page has been written.

-Scott

No, the returned value doesn't include the length of the error page. In no-WITH_YAFFS_OOB case, when nand_write failed, truncated_write_size has been
updated by nand_write to the length which has been successfully written , so it's OK to subtract written_size from left_to_write. In WITH_YAFFS_OOB case, when nand->write_oob failed, written_size is also the length which has been successfully written.

Cheers,
Hou

#5 lore raw

PATCH 1/1 mtd: nand: fix the written length when nand_write_skip_bad failed

Quoted 7 lines · htbegin

Hi, Scott

Quoted 2 lines · Scott Wood
Quoted 1 line · htbegin
Quoted 1 line

BTW, are you actually using WITH_YAFFS_OOB? I think there are

some

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

other
things wrong with it at the moment, as mentioned here:
http://lists.denx.de/pipermail/u-boot/2013-March/148378.html

No, I don't use it.

Changes to that code should be tested by someone...

Sorry, I can't help.

It's moot because I don't think this change should be made, but this is a case where you could enable it temporarily in your board config for some basic testing.

Quoted 15 lines
Quoted 1 line
Quoted 1 line

I just use "*length -= left_to_write - written_size" to tell the

upper

Quoted 2 lines
Quoted 2 lines

level that what
had been actually happened. For the current block, "written_size"

has

Quoted 5 lines
Quoted 2 lines

been written to the NAND Flash yet, so left_to_write should be subtracted by "written_size".

But left_to_write isn't decreased until after this error return, so

that's

Quoted 1 line

already the case. Subtracting written_size from left_to_write has

the

Quoted 1 line

effect of increasing length by written_size, so the return value

will now

Quoted 8 lines
Quoted 3 lines

look like the error page has been written.

-Scott

No, the returned value doesn't include the length of the error page. In no-WITH_YAFFS_OOB case, when nand_write failed, truncated_write_size has been
updated by nand_write to the length which has been successfully written , so it's OK to subtract written_size from left_to_write.

OK, but that doesn't explain why the change is needed. You said you wanted to find the block with the error. We only write one block at a time in the loop. Why do you need the specific page within the block that failed?

-Scott

author
#6 lore raw

PATCH 1/1 mtd: nand: fix the written length when nand_write_skip_bad failed

Hi, Scott

Quoted 22 lines · Scott Wood
Quoted 14 lines
Quoted 14 lines
Quoted 5 lines

I just use "*length -= left_to_write - written_size" to tell the upper level that what
had been actually happened. For the current block, "written_size" has been written to the NAND Flash yet, so left_to_write should be subtracted by "written_size".

But left_to_write isn't decreased until after this error return, so that's
already the case. Subtracting written_size from left_to_write has the effect of increasing length by written_size, so the return value will now
look like the error page has been written.

Quoted 6 lines
Quoted 1 line

-Scott

No, the returned value doesn't include the length of the error page. In no-WITH_YAFFS_OOB case, when nand_write failed, truncated_write_size has been
updated by nand_write to the length which has been successfully written , so it's OK to subtract written_size from left_to_write.

Quoted 5 lines

OK, but that doesn't explain why the change is needed. You said you wanted to find the block with the error. We only write one block at a time in the loop. Why do you need the specific page within the block that failed?

-Scott

Yes, you are right it's OK to ignore the written length of the write-failed block, but as I said before I just wanted to tell the upper level what had been actually written. So if you insist the subtraction of written_len is unnecessary, it's alright with me.

Thanks.
Hou

#7 lore raw

PATCH 1/1 mtd: nand: fix the written length when nand_write_skip_bad failed

Quoted 38 lines · htbegin

Hi, Scott

Quoted 1 line · Scott Wood
Quoted 1 line
Quoted 1 line
Quoted 1 line

I just use "*length -= left_to_write - written_size" to tell

the upper

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

level that what
had been actually happened. For the current block,

"written_size" has

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

been written to the NAND Flash yet, so left_to_write should be subtracted by "written_size".

But left_to_write isn't decreased until after this error return,

so

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

that's
already the case. Subtracting written_size from left_to_write

has the

Quoted 1 line
Quoted 1 line
Quoted 1 line

effect of increasing length by written_size, so the return value

will

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

now
look like the error page has been written.

-Scott

No, the returned value doesn't include the length of the error

page.

Quoted 7 lines
Quoted 4 lines

In no-WITH_YAFFS_OOB case, when nand_write failed, truncated_write_size has been
updated by nand_write to the length which has been successfully written , so it's OK to subtract written_size from left_to_write.

OK, but that doesn't explain why the change is needed. You said

you wanted

Quoted 1 line

to find the block with the error. We only write one block at a

time in the

Quoted 1 line

loop. Why do you need the specific page within the block that

Quoted 8 lines

failed?

Quoted 2 lines

-Scott

Yes, you are right it's OK to ignore the written length of the write-failed block, but as I said before I just wanted to tell the upper level what had been actually written. So if you insist the subtraction of written_len is unnecessary, it's alright with me.

Thanks. I do insist -- it adds complexity.

-Scott