Skip to contentSkip to conversation

resize.f2fs: fix to avoid zeroing main area blocks in migrate_ssa()

PATCH In review Yangyang Zang 2 participants 8 messages Mar 11 – Mar 13

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/3 resize.f2fs: fix to avoid zeroing main area blocks in migrate_ssa()

ad1357c34023 ("f2fs-tools: revert summary entry count from 2048 to 512 in 16kb block support") changed the logic for migrating SSA blocks, regardless of whether the packed_ssa feature is enabled. During the reisze process, this patch may corrupt the file system root inode.

Reproducer:
dd if=/dev/zero of=/data/f2fs.img bs=4k count=525312 make_f2fs -g android /data/f2fs.img 131072 resize.f2fs -F /data/f2fs.img
mkdir /mnt/f2fs
mount /data/f2fs.img /mnt/f2fs

The root cause is that the number of SSA blocks after segment alignment is inconsistent with get_newsb(segment_count). This may result in some SSA blocks not being zeroed out, or the main area blocks being incorrectly zeroed out during SSA migration. Zeroing out the main area blocks may corrupt the root inode.

Fixes ad1357c34023 ("f2fs-tools: revert summary entry count from 2048 to 512 in 16kb block support")

---
Changes in v3:
- Split the fix and cleanup into two patches
- Add reproduction steps

Changes in v2:
- Modify the commit message

---
 fsck/resize.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fsck/resize.c +1 -1
index e6502837b209..b73a2529dc75 100644
@@ -258,7 +258,7 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
 	block_t old_sum_blkaddr = get_sb(ssa_blkaddr);
 	block_t new_sum_blkaddr = get_newsb(ssa_blkaddr);
 	unsigned int expand_segno = MAIN_SEGS(sbi) - offset;
-	unsigned int new_seg_total = get_newsb(segment_count);
+	unsigned int new_seg_total = get_newsb(main_blkaddr) - new_sum_blkaddr;
 	int new_segno;
 	int ret = 0;
 	void *zero_block = calloc(F2FS_SUM_BLKSIZE, 1);
Signature · 8 lines
2.43.2



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

PATCH v3 2/3 resize.f2fs: clean up the code in migrate_ssa()

Clean up the code to make its semantics more logical, no logic changes.

---
 fsck/resize.c | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)
fsck/resize.c +19 -19
index b73a2529dc75..79db7eede65c 100644
@@ -257,48 +257,48 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
 	struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
 	block_t old_sum_blkaddr = get_sb(ssa_blkaddr);
 	block_t new_sum_blkaddr = get_newsb(ssa_blkaddr);
-	unsigned int expand_segno = MAIN_SEGS(sbi) - offset;
-	unsigned int new_seg_total = get_newsb(main_blkaddr) - new_sum_blkaddr;
-	int new_segno;
+	unsigned int expand_sum_blocks = MAIN_SEGS(sbi) - offset;
+	unsigned int new_sum_blocks = get_newsb(main_blkaddr) - new_sum_blkaddr;
+	int new_sum_blkoff;
 	int ret = 0;
 	void *zero_block = calloc(F2FS_SUM_BLKSIZE, 1);
 	ASSERT(zero_block);
 
 	if (offset && new_sum_blkaddr <= (old_sum_blkaddr +
 				offset / SUMS_PER_BLOCK)) {
-		new_segno = 0;
-		while (new_segno < new_seg_total) {
-			if (new_segno < expand_segno)
-				move_ssa(sbi, new_sb, offset++, new_segno);
+		new_sum_blkoff = 0;
+		while (new_sum_blkoff < new_sum_blocks) {
+			if (new_sum_blkoff < expand_sum_blocks)
+				move_ssa(sbi, new_sb, offset++, new_sum_blkoff);
 			else if (c.feature & F2FS_FEATURE_PACKED_SSA)
 				ret = dev_write_4k_block(zero_block,
-					GET_SUM_NEW_BLKADDR(new_sb, new_segno),
-					GET_SUM_NEW_BLKOFF(new_sb, new_segno),
+					GET_SUM_NEW_BLKADDR(new_sb, new_sum_blkoff),
+					GET_SUM_NEW_BLKOFF(new_sb, new_sum_blkoff),
 					WRITE_LIFE_NONE);
 			else
 				ret = dev_write_block(zero_block,
-					GET_SUM_NEW_BLKADDR(new_sb, new_segno),
+					GET_SUM_NEW_BLKADDR(new_sb, new_sum_blkoff),
 					WRITE_LIFE_NONE);
 			ASSERT(ret >= 0);
-			new_segno++;
+			new_sum_blkoff++;
 		}
 	} else {
-		new_segno = new_seg_total - 1;
+		new_sum_blkoff = new_sum_blocks - 1;
 		offset = MAIN_SEGS(sbi) - 1;
-		while (new_segno >= 0) {
-			if (new_segno < expand_segno)
-				move_ssa(sbi, new_sb, offset--, new_segno);
+		while (new_sum_blkoff >= 0) {
+			if (new_sum_blkoff < expand_sum_blocks)
+				move_ssa(sbi, new_sb, offset--, new_sum_blkoff);
 			else if (c.feature & F2FS_FEATURE_PACKED_SSA)
 				ret = dev_write_4k_block(zero_block,
-					GET_SUM_NEW_BLKADDR(new_sb, new_segno),
-					GET_SUM_NEW_BLKOFF(new_sb, new_segno),
+					GET_SUM_NEW_BLKADDR(new_sb, new_sum_blkoff),
+					GET_SUM_NEW_BLKOFF(new_sb, new_sum_blkoff),
 					WRITE_LIFE_NONE);
 			else
 				ret = dev_write_block(zero_block,
-					GET_SUM_NEW_BLKADDR(new_sb, new_segno),
+					GET_SUM_NEW_BLKADDR(new_sb, new_sum_blkoff),
 					WRITE_LIFE_NONE);
 			ASSERT(ret >= 0);
-			new_segno--;
+			new_sum_blkoff--;
 		}
 	}
 
Signature · 8 lines
2.43.2



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Reviewed-by Chao Yu on v3 2/3
#2 lore raw

Re: [f2fs-dev] [PATCH v3 2/3] resize.f2fs: clean up the code in migrate_ssa()

Quoted 4 lines · Yangyang Zang

Clean up the code to make its semantics more logical, no logic changes.

Thanks,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#3 lore raw

PATCH v3 3/3 resize.f2fs: add more logs in migrate_ssa()

In reply to Yangyang Zang

Add logs for zeroing SSA blocks and add an ASSERT to prevent the issue of zeroing main area blocks from recurring in the future.

---
Changes in v2:
- Add commit message

---
 fsck/resize.c | 50 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 30 insertions(+), 20 deletions(-)
fsck/resize.c +30 -20
index 79db7eede65c..beb7ce047ec8 100644
@@ -259,6 +259,7 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
 	block_t new_sum_blkaddr = get_newsb(ssa_blkaddr);
 	unsigned int expand_sum_blocks = MAIN_SEGS(sbi) - offset;
 	unsigned int new_sum_blocks = get_newsb(main_blkaddr) - new_sum_blkaddr;
+	block_t sum_blkaddr;
 	int new_sum_blkoff;
 	int ret = 0;
 	void *zero_block = calloc(F2FS_SUM_BLKSIZE, 1);
@@ -268,17 +269,21 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
 				offset / SUMS_PER_BLOCK)) {
 		new_sum_blkoff = 0;
 		while (new_sum_blkoff < new_sum_blocks) {
-			if (new_sum_blkoff < expand_sum_blocks)
+			if (new_sum_blkoff < expand_sum_blocks) {
 				move_ssa(sbi, new_sb, offset++, new_sum_blkoff);
-			else if (c.feature & F2FS_FEATURE_PACKED_SSA)
-				ret = dev_write_4k_block(zero_block,
-					GET_SUM_NEW_BLKADDR(new_sb, new_sum_blkoff),
-					GET_SUM_NEW_BLKOFF(new_sb, new_sum_blkoff),
-					WRITE_LIFE_NONE);
-			else
-				ret = dev_write_block(zero_block,
-					GET_SUM_NEW_BLKADDR(new_sb, new_sum_blkoff),
-					WRITE_LIFE_NONE);
+			} else {
+				sum_blkaddr = GET_SUM_NEW_BLKADDR(new_sb, new_sum_blkoff);
+				if (c.feature & F2FS_FEATURE_PACKED_SSA)
+					ret = dev_write_4k_block(zero_block,
+						sum_blkaddr,
+						GET_SUM_NEW_BLKOFF(new_sb, new_sum_blkoff),
+						WRITE_LIFE_NONE);
+				else
+					ret = dev_write_block(zero_block,
+						sum_blkaddr,
+						WRITE_LIFE_NONE);
+				DBG(1, "Zero summary block: %x\n", sum_blkaddr);
+			}
 			ASSERT(ret >= 0);
 			new_sum_blkoff++;
 		}
@@ -286,17 +291,22 @@ static void migrate_ssa(struct f2fs_sb_info *sbi,
 		new_sum_blkoff = new_sum_blocks - 1;
 		offset = MAIN_SEGS(sbi) - 1;
 		while (new_sum_blkoff >= 0) {
-			if (new_sum_blkoff < expand_sum_blocks)
+			if (new_sum_blkoff < expand_sum_blocks) {
 				move_ssa(sbi, new_sb, offset--, new_sum_blkoff);
-			else if (c.feature & F2FS_FEATURE_PACKED_SSA)
-				ret = dev_write_4k_block(zero_block,
-					GET_SUM_NEW_BLKADDR(new_sb, new_sum_blkoff),
-					GET_SUM_NEW_BLKOFF(new_sb, new_sum_blkoff),
-					WRITE_LIFE_NONE);
-			else
-				ret = dev_write_block(zero_block,
-					GET_SUM_NEW_BLKADDR(new_sb, new_sum_blkoff),
-					WRITE_LIFE_NONE);
+			} else {
+				sum_blkaddr = GET_SUM_NEW_BLKADDR(new_sb, new_sum_blkoff);
+				ASSERT(sum_blkaddr < get_newsb(main_blkaddr));
+				if (c.feature & F2FS_FEATURE_PACKED_SSA)
+					ret = dev_write_4k_block(zero_block,
+						sum_blkaddr,
+						GET_SUM_NEW_BLKOFF(new_sb, new_sum_blkoff),
+						WRITE_LIFE_NONE);
+				else
+					ret = dev_write_block(zero_block,
+						sum_blkaddr,
+						WRITE_LIFE_NONE);
+				DBG(1, "Zero summary block: %x\n", sum_blkaddr);
+			}
 			ASSERT(ret >= 0);
 			new_sum_blkoff--;
 		}
Signature · 8 lines
2.43.2



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Reviewed-by Chao Yu on v3 3/3
#4 lore raw

Re: [f2fs-dev] [PATCH v3 3/3] resize.f2fs: add more logs in migrate_ssa()

Quoted 5 lines · Yangyang Zang

Add logs for zeroing SSA blocks and add an ASSERT to prevent the issue of zeroing main area blocks from recurring in the future.

Thanks,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Reviewed-by Chao Yu on v3 1/3
#5 lore raw
In reply to Yangyang Zang
Quoted 6 lines · Yangyang Zang

ad1357c34023 ("f2fs-tools: revert summary entry count from 2048 to 512 in 16kb block support") changed the logic for migrating SSA blocks, regardless of whether the packed_ssa feature is enabled. During the reisze process, this patch may corrupt the file system root inode.

Quoted 6 lines

Reproducer:
dd if=/dev/zero of=/data/f2fs.img bs=4k count=525312 make_f2fs -g android /data/f2fs.img 131072 resize.f2fs -F /data/f2fs.img
mkdir /mnt/f2fs
mount /data/f2fs.img /mnt/f2fs

Hi Yangyang,

Do you have any plan to wrap above testcase into xfsqa?

Quoted 10 lines

The root cause is that the number of SSA blocks after segment alignment is inconsistent with get_newsb(segment_count). This may result in some SSA blocks not being zeroed out, or the main area blocks being incorrectly zeroed out during SSA migration. Zeroing out the main area blocks may corrupt the root inode.

Fixes ad1357c34023 ("f2fs-tools: revert summary entry count from 2048 to 512 in 16kb block support")

Thanks,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#6 lore raw
Quoted 14 lines · Chao Yu
Quoted 6 lines · Yangyang Zang

ad1357c34023 ("f2fs-tools: revert summary entry count from 2048 to 512 in 16kb block support") changed the logic for migrating SSA blocks, regardless of whether the packed_ssa feature is enabled. During the reisze process, this patch may corrupt the file system root inode.

Quoted 6 lines

Reproducer:
dd if=/dev/zero of=/data/f2fs.img bs=4k count=525312 make_f2fs -g android /data/f2fs.img 131072 resize.f2fs -F /data/f2fs.img
mkdir /mnt/f2fs
mount /data/f2fs.img /mnt/f2fs

Quoted 3 lines

Hi Yangyang,

Do you have any plan to wrap above testcase into xfsqa?

I don't have any plans yet, but I can give it a try.

Quoted 8 lines
Quoted 8 lines

The root cause is that the number of SSA blocks after segment alignment is inconsistent with get_newsb(segment_count). This may result in some SSA blocks not being zeroed out, or the main area blocks being incorrectly zeroed out during SSA migration. Zeroing out the main area blocks may corrupt the root inode.

Quoted 6 lines
Quoted 2 lines
Fixes ad1357c34023 ("f2fs-tools: revert summary entry count from 2048 to 512 in 16kb block support")

Thanks,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#7 lore raw
Quoted 16 lines · Yangyang Zang
Quoted 14 lines · Chao Yu
Quoted 12 lines · Yangyang Zang

ad1357c34023 ("f2fs-tools: revert summary entry count from 2048 to 512 in 16kb block support") changed the logic for migrating SSA blocks, regardless of whether the packed_ssa feature is enabled. During the reisze process, this patch may corrupt the file system root inode.

Reproducer:
dd if=/dev/zero of=/data/f2fs.img bs=4k count=525312 make_f2fs -g android /data/f2fs.img 131072 resize.f2fs -F /data/f2fs.img
mkdir /mnt/f2fs
mount /data/f2fs.img /mnt/f2fs

Quoted 4 lines
Quoted 3 lines

Hi Yangyang,

Do you have any plan to wrap above testcase into xfsqa?

I don't have any plans yet, but I can give it a try.

Thank you! Yangyang.

Thanks,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

The root cause is that the number of SSA blocks after segment alignment is inconsistent with get_newsb(segment_count). This may result in some SSA blocks not being zeroed out, or the main area blocks being incorrectly zeroed out during SSA migration. Zeroing out the main area blocks may corrupt the root inode.

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Fixes ad1357c34023 ("f2fs-tools: revert summary entry count from 2048 to 512 in 16kb block support")

Thanks,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel