Skip to contentSkip to conversation

resize.f2fs: fix corruption when expanding after a safe shrink

PATCH New ChromMob 1 participant 1 message Oct 19

lore
Share & tools
lore

ChromMob

author
#0 lore raw

PATCH 1/1 resize.f2fs: fix corruption when expanding after a safe shrink

From: ChromMob <[email protected]>

w/ below testcase, expanding an image that was shrunk with -s to a size below its original size corrupts it:

truncate -s 4G img
mkfs.f2fs -f img
sload.f2fs -f <some dir> img
resize.f2fs -s -t $((2*1024*1024*1024/512)) img
truncate -s 2G img
truncate -s 3G img
resize.f2fs img
fsck.f2fs --dry-run img

Depending on the image, resize.f2fs hits an ASSERT in update_data_blkaddr() or BLKOFF_FROM_MAIN(), or segfaults, after it has already modified the image, and fsck.f2fs then reports unreachable NAT entries and SIT/node count mismatches.

The root cause is that safe resize keeps the SIT, NAT and SSA areas sized for the original volume, while expanding lays them out again for the target size. When the target is below the original size, the new areas are smaller and main_blkaddr moves backward, but f2fs_resize_grow() assumes the main area only moves forward: offset = new_main_blkaddr - old_main_blkaddr underflows, so f2fs_defragment() walks far past the end of the main area and migrate_main() writes blocks to wrapped-around addresses.

When the new layout would move main_blkaddr backward, keep the current metadata areas instead, as long as SIT and SSA still cover the new main area, i.e. the newly computed SIT and SSA are not larger. Not having enough SIT for the new size is what commit f614bccc18cf ("resize.f2fs:
fix to always change metadata for expand resize") found for -s expansion; here the areas were sized for a larger volume, so the SIT bitmap still addresses every main segment. NAT is not compared: it is capped by the checkpoint bitmap and can come out larger for a smaller volume (e.g. 86 segments for 900GiB vs 88 for 850GiB), and the current NAT already served the original size. The resulting layout is the same as when expanding back to the original size, which already works. Otherwise, fail instead of corrupting the image.

Tested on image files populated with sload.f2fs: shrink with -s, then expand below, to and above the original size, including several expansions in a row, for 4GiB, 64GiB and 880.5GiB images. After each step fsck.f2fs --dry-run is clean, and dump.f2fs -r output matches the contents before resizing.

---
 fsck/resize.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)
fsck/resize.c +26 -4
index beb7ce0..4b71a35 100644
@@ -9,7 +9,7 @@
  */
 #include "fsck.h"
 
-static int get_new_sb(struct f2fs_super_block *sb)
+static int get_new_sb(struct f2fs_super_block *sb, bool keep_meta)
 {
 	uint32_t zone_size_bytes;
 	uint64_t zone_align_start_offset;
@@ -37,7 +37,7 @@ static int get_new_sb(struct f2fs_super_block *sb)
 				zone_align_start_offset) / segment_size_bytes /
 				c.segs_per_sec * c.segs_per_sec);
 
-	if (c.safe_resize)
+	if (c.safe_resize || keep_meta)
 		goto safe_resize;
 
 	blocks_for_sit = SIZE_ALIGN(get_sb(segment_count), SIT_ENTRY_PER_BLOCK);
@@ -675,15 +675,37 @@ static int f2fs_resize_grow(struct f2fs_sb_info *sbi)
 	block_t end_blkaddr, old_main_blkaddr, new_main_blkaddr;
 	unsigned int offset;
 	unsigned int offset_seg = 0;
+	double overprovision = c.new_overprovision;
 	int err = -1;
 
 	/* flush NAT/SIT journal entries */
 	flush_journal_entries(sbi);
 
 	memcpy(new_sb, F2FS_RAW_SUPER(sbi), sizeof(*new_sb));
-	if (get_new_sb(new_sb))
+	if (get_new_sb(new_sb, false))
 		return -1;
 
+	/*
+	 * A safe resize shrink keeps the metadata areas sized for the old,
+	 * larger volume. Laying them out again for a smaller target would
+	 * move the main area backward, which the migration below cannot do,
+	 * so keep the current areas when SIT and SSA still cover the new
+	 * main area. NAT is not compared: it is capped by the checkpoint
+	 * bitmap, so it can come out larger for a smaller volume, and the
+	 * current one already served the old size.
+	 */
+	if (get_newsb(main_blkaddr) < get_sb(main_blkaddr)) {
+		if (get_newsb(segment_count_sit) > get_sb(segment_count_sit) ||
+		    get_newsb(segment_count_ssa) > get_sb(segment_count_ssa)) {
+			MSG(0, "\tError: Cannot move main area backward\n");
+			return -1;
+		}
+		memcpy(new_sb, F2FS_RAW_SUPER(sbi), sizeof(*new_sb));
+		c.new_overprovision = overprovision;
+		if (get_new_sb(new_sb, true))
+			return -1;
+	}
+
 	if (f2fs_resize_check(sbi, new_sb) < 0)
 		return -1;
 
@@ -739,7 +761,7 @@ static int f2fs_resize_shrink(struct f2fs_sb_info *sbi)
 	flush_journal_entries(sbi);
 
 	memcpy(new_sb, F2FS_RAW_SUPER(sbi), sizeof(*new_sb));
-	if (get_new_sb(new_sb))
+	if (get_new_sb(new_sb, false))
 		return -1;
 
 	if (f2fs_resize_check(sbi, new_sb) < 0)
Signature · 8 lines
2.56.0



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