Skip to contentSkip to conversation

btrfs-progs: check: add repair functionality for

PATCH New Qu Wenruo 1 participant 3 messages Oct 6, 2025 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs-progs: check: add repair functionality for

Currently any orphan device extents will cause kernels to reject the mount.

But on the other hand, btrfs check is unable to repair such problem, so end users may fall into a situation where they are unable to mount the fs.

Add the repair functionality to btrfs check, for both original lowmem modes, and add a new test case for it.

Qu Wenruo (2):
btrfs-progs: check: add repair functionality for orphan dev extents
btrfs-progs: fsck-tests: a new test case with orphan dev extent

 check/main.c                                  |   6 +-
 check/mode-lowmem.c                           |   7 +-
 check/repair.c                                |  78 ++++++++++++++++++
 check/repair.h                                |   1 +
 .../068-orphan-dev-extent/.lowmem_repairable  |   0
 .../068-orphan-dev-extent/default.img.xz      | Bin 0 -> 1784 bytes
 6 files changed, 90 insertions(+), 2 deletions(-)
 create mode 100644 tests/fsck-tests/068-orphan-dev-extent/.lowmem_repairable
 create mode 100644 tests/fsck-tests/068-orphan-dev-extent/default.img.xz

--
2.50.1

author
#1 lore raw

PATCH 1/2 btrfs-progs: check: add repair functionality for orphan dev extents

There is a bug report that btrfs-check --repair didn't repair an orphan dev extent, which makes kernel to reject the fs due to the overly strict chunk related checks.

However for btrfs-check, it's pretty easy to remove orphan device extents (which do not have corresponding chunks), and update the device item to reflect the used_bytes changes.

Add a repair function, btrfs_remove_dev_extent() which does the repair, and call that function for both original and lowmem modes.

---
 check/main.c        |  6 +++-
 check/mode-lowmem.c |  7 +++-
 check/repair.c      | 78 +++++++++++++++++++++++++++++++++++++++++++++
 check/repair.h      |  1 +
 4 files changed, 90 insertions(+), 2 deletions(-)
check/main.c +5 -1
index 91ce6d7401a1..e457c9073bfb 100644
@@ -8601,7 +8601,11 @@ int check_chunks(struct cache_tree *chunk_cache,
 				dext_rec->objectid,
 				dext_rec->offset,
 				dext_rec->length);
-		if (!ret)
+		err = -ENOENT;
+		if (opt_check_repair)
+			err = btrfs_remove_dev_extent(gfs_info, dext_rec->objectid,
+						      dext_rec->offset);
+		if (err && !ret)
 			ret = 1;
 	}
 	return ret;
check/mode-lowmem.c +6 -1
index 774a57c861a4..363dc4ae1904 100644
@@ -4749,7 +4749,12 @@ out:
 		error(
 		"device extent[%llu, %llu, %llu] did not find the related chunk",
 			devext_key.objectid, devext_key.offset, length);
-		return REFERENCER_MISSING;
+		ret = -ENOENT;
+		if (opt_check_repair)
+			ret = btrfs_remove_dev_extent(gfs_info, devext_key.objectid,
+						      devext_key.offset);
+		if (ret < 0)
+			return REFERENCER_MISSING;
 	}
 	return 0;
 }
check/repair.c +78 -0

91 lines not on this page · raw message

check/repair.h +1 -0
index 4d59ca1dd115..d8a191b89fc8 100644
@@ -48,6 +48,7 @@ int btrfs_mark_used_tree_blocks(struct btrfs_fs_info *fs_info,
 				struct extent_io_tree *tree);
 int btrfs_mark_used_blocks(struct btrfs_fs_info *fs_info,
 			   struct extent_io_tree *tree);
+int btrfs_remove_dev_extent(struct btrfs_fs_info *fs_info, u64 devid, u64 physical);
 enum btrfs_tree_block_status btrfs_check_block_for_repair(struct extent_buffer *eb,
 							  struct btrfs_key *first_key);
 void btrfs_set_item_key_unsafe(struct btrfs_root *root, struct btrfs_path *path,
Signature · 1 line
2.50.1
author
#2 lore raw

PATCH 2/2 btrfs-progs: fsck-tests: a new test case with orphan dev extent

In reply to Qu Wenruo

The fs has the following chunk tree (minor members removed):

	item 0 key (DEV_ITEMS DEV_ITEM 1) itemoff 16185 itemsize 98
		devid 1 total_bytes 536870912 bytes_used 159383552
		io_align 4096 io_width 4096 sector_size 4096 type 0
	item 1 key (FIRST_CHUNK_TREE CHUNK_ITEM 13631488) itemoff 16105 itemsize 80
		length 8388608 owner 2 stripe_len 65536 type DATA|single
		io_align 65536 io_width 65536 sector_size 4096
		num_stripes 1 sub_stripes 1
	item 2 key (FIRST_CHUNK_TREE CHUNK_ITEM 22020096) itemoff 15993 itemsize 112
		length 8388608 owner 2 stripe_len 65536 type SYSTEM|DUP
		io_align 65536 io_width 65536 sector_size 4096
	item 3 key (FIRST_CHUNK_TREE CHUNK_ITEM 30408704) itemoff 15881 itemsize 112
		length 33554432 owner 2 stripe_len 65536 type METADATA|DUP
		io_align 65536 io_width 65536 sector_size 4096

And the following dev tree:

	item 0 key (DEV_STATS PERSISTENT_ITEM 1) itemoff 16243 itemsize 40
		persistent item objectid DEV_STATS offset 1
		device stats
		write_errs 0 read_errs 0 flush_errs 0 corruption_errs 0 generation 0
	item 1 key (1 DEV_EXTENT 13631488) itemoff 16195 itemsize 48
	item 2 key (1 DEV_EXTENT 22020096) itemoff 16147 itemsize 48
	item 3 key (1 DEV_EXTENT 30408704) itemoff 16099 itemsize 48
	item 4 key (1 DEV_EXTENT 38797312) itemoff 16051 itemsize 48
	item 5 key (1 DEV_EXTENT 72351744) itemoff 16003 itemsize 48
	item 6 key (1 DEV_EXTENT 105906176) itemoff 15955 itemsize 48
		dev extent chunk_tree 3
		chunk_objectid 256 chunk_offset 63963136 length 67108864
		chunk_tree_uuid b2408431-a79a-42b6-aadc-4e853d4caa82

Notice the last dev_extent item, which has no corresponding chunk.

The image is created by calling btrfs_insert_dev_extent(), then btrfs_update_device() to update dev->bytes_used.

This image can be repaired by both original and lowmem modes.

---
 .../068-orphan-dev-extent/.lowmem_repairable     |   0
 .../068-orphan-dev-extent/default.img.xz         | Bin 0 -> 1784 bytes
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 tests/fsck-tests/068-orphan-dev-extent/.lowmem_repairable
 create mode 100644 tests/fsck-tests/068-orphan-dev-extent/default.img.xz
tests/fsck-tests/068-orphan-dev-extent/.lowmem_repairable new file +0 -0
new file mode 100644
index 000000000000..e69de29bb2d1
tests/fsck-tests/068-orphan-dev-extent/default.img.xz new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..6ca960a73a4ded4d7ffe57cb234c9579f8c0f584
GIT binary patch
Signature · 1 line
2.50.1