btrfs-progs: fix BUG_ON when all devices under seed fs are missing
diff mbox

Message ID
State Accepted
Headers show

Commit Message

Gui Hecheng Oct. 6, 2014, 10:17 a.m. UTC
Steps to reproduce:
	# mkfs.btrfs -f /dev/sda[1-2]
	# btrfstune -S 1 /dev/sda1
	# mount /dev/sda /mnt
	# btrfs dev add /dev/sda3 /mnt
	# umount /mnt
	# mkfs.ext4 /dev/sda1		// kill seed dev
	# mkfs.ext4 /dev/sda2		// kill seed dev
	# btrfs-debug-tree /dev/sda3	<== BUG_ON
Output msg:
	volumes.c:1824: btrfs_read_chunk_tree: Assertion `ret` failed.

This BUG_ON complains about a failed @read_one_dev() call when
@open_seed_devices() failed to find the seed @fs_devices object
for a dev_item in chunk tree.
In this case, just insert a "shadow" @fs_devices with the fsid in
dev_item shall make no harm since no other tools will try to
make use of the stuff that the "shadow" @fs_devices possesses
after its creation.

After apply this commit, btrfs-debug-tree will report unable
to open the device.

Signed-off-by: Gui Hecheng <>
This should be after patch:
	Btrfs-progs: fsck: disallow partial opening if critical \
						roots corrupted
 volumes.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff mbox

diff --git a/volumes.c b/volumes.c
index eaf3f9f..a9c7662 100644
--- a/volumes.c
+++ b/volumes.c
@@ -1671,8 +1671,15 @@  static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
 	fs_devices = find_fsid(fsid);
 	if (!fs_devices) {
-		ret = -ENOENT;
-		goto out;
+		/* missing all seed devices */
+		fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
+		if (!fs_devices) {
+			ret = -ENOMEM;
+			goto out;
+		}
+		INIT_LIST_HEAD(&fs_devices->devices);
+		list_add(&fs_devices->list, &fs_uuids);
+		memcpy(fs_devices->fsid, fsid, BTRFS_FSID_SIZE);
 	ret = btrfs_open_devices(fs_devices, O_RDONLY);