Skip to contentSkip to conversation

btrfs-progs: Correctly open filesystem on image file

PATCH Applied Nikolay Borisov 3 participants 9 messages May 16, 2019 – Jun 5, 2019 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 btrfs-progs: Correctly open filesystem on image file

When btrfs' 'filesystem' subcommand is passed path to an image file it currently fails since the code expects the image file is going to be recognised by libblkid (called from btrfs_scan_devices()). This is not the case since libblkid only scan well-known locations under /dev.

Fix this by explicitly calling open_ctree which will correctly open the image and add it to the correct btrfs_fs_devices struct. This allows subsequent cmd_filesystem_show logic to correctly show requested information.

---
 cmds-filesystem.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
cmds-filesystem.c +12 -1
index b8beec13f0e5..f55ce9b4ab85 100644
@@ -771,7 +771,18 @@ static int cmd_filesystem_show(int argc, char **argv)
 		goto out;
 
 devs_only:
-	ret = btrfs_scan_devices();
+	if (type == BTRFS_ARG_REG) {
+		/*
+		 * We don't close the fs_info because it will free the device,
+		 * this is not a long-running process so it's fine
+		 */
+		if (open_ctree(search, btrfs_sb_offset(0), 0))
+			ret = 0;
+		else
+			ret = 1;
+	} else {
+		ret = btrfs_scan_devices();
+	}
 
 	if (ret) {
 		error("blkid device scan returned %d", ret);
Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/2 btrfs-progs: tests: Test fs on image files is correctly recognised

This ensures that 'btrfs filesystem show' can correctly identify a filesystem on a newly created local file.

---
 tests/cli-tests/010-fi-show-on-new-file/test.sh | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
 create mode 100755 tests/cli-tests/010-fi-show-on-new-file/test.sh
tests/cli-tests/010-fi-show-on-new-file/test.sh new file +16 -0
new file mode 100755
index 000000000000..668f0f9b2cfe
@@ -0,0 +1,16 @@
+#!/bin/bash
+# test for 'filesystem show' on fresh local file
+
+source "$TEST_TOP/common"
+
+check_prereq mkfs.btrfs
+check_prereq btrfs
+
+IMAGE=$(mktemp -u btrfs-XXXXXX.img)
+
+truncate -s3g "$IMAGE"
+run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f "$IMAGE"
+run_check $SUDO_HELPER "$TOP/btrfs" filesystem show "$IMAGE"
+
+rm -f "$IMAGE"
+
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 2/2] btrfs-progs: tests: Test fs on image files is correctly recognised

Quoted 6 lines · Nikolay Borisov

This ensures that 'btrfs filesystem show' can correctly identify a filesystem on a newly created local file.

---
 tests/cli-tests/010-fi-show-on-new-file/test.sh | 16 ++++++++++++++++

Test is ok, the placment should be to misc-tests, cli-tests is really to verify that argumets get parsed and that some combinations are handled.

#3 lore raw
In reply to Nikolay Borisov
Quoted 20 lines · Nikolay Borisov

When btrfs' 'filesystem' subcommand is passed path to an image file it currently fails since the code expects the image file is going to be recognised by libblkid (called from btrfs_scan_devices()). This is not the case since libblkid only scan well-known locations under /dev.

Fix this by explicitly calling open_ctree which will correctly open the image and add it to the correct btrfs_fs_devices struct. This allows subsequent cmd_filesystem_show logic to correctly show requested information.

---
 cmds-filesystem.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
cmds-filesystem.c +0 -0
index b8beec13f0e5..f55ce9b4ab85 100644

cmds-filesystem.c static int cmd_filesystem_show(int argc, char **argv) · line 778

Quoted 9 lines
cmds-filesystem.c +5 -1
@@ -771,7 +771,18 @@ static int cmd_filesystem_show(int argc, char **argv)
 		goto out;
 
 devs_only:
-	ret = btrfs_scan_devices();
+	if (type == BTRFS_ARG_REG) {
+		/*
+		 * We don't close the fs_info because it will free the device,
+		 * this is not a long-running process so it's fine
+		 */

The comment makes sense, but I'm pretty sure we still prefer to clean it up.

Just something like:

	struct btrfs_root *root = NULL;

	root = open_ctree();
	if (root)
		ret = 0;
	else
		ret = 1;
	close_ctree(root);

Despite that, I think the patch looks good to me.

Thanks,
Qu

Quoted 11 lines
cmds-filesystem.c +7 -0
+		if (open_ctree(search, btrfs_sb_offset(0), 0))
+			ret = 0;
+		else
+			ret = 1;
+	} else {
+		ret = btrfs_scan_devices();
+	}
 
 	if (ret) {
 		error("blkid device scan returned %d", ret);
#4 lore raw
Quoted 37 lines · Qu Wenruo
Quoted 20 lines · Nikolay Borisov

When btrfs' 'filesystem' subcommand is passed path to an image file it currently fails since the code expects the image file is going to be recognised by libblkid (called from btrfs_scan_devices()). This is not the case since libblkid only scan well-known locations under /dev.

Fix this by explicitly calling open_ctree which will correctly open the image and add it to the correct btrfs_fs_devices struct. This allows subsequent cmd_filesystem_show logic to correctly show requested information.

---
 cmds-filesystem.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
cmds-filesystem.c +0 -0
index b8beec13f0e5..f55ce9b4ab85 100644

cmds-filesystem.c static int cmd_filesystem_show(int argc, char **argv) · line 778

Quoted 9 lines
cmds-filesystem.c +5 -1
@@ -771,7 +771,18 @@ static int cmd_filesystem_show(int argc, char **argv)
 		goto out;
 
 devs_only:
-	ret = btrfs_scan_devices();
+	if (type == BTRFS_ARG_REG) {
+		/*
+		 * We don't close the fs_info because it will free the device,
+		 * this is not a long-running process so it's fine
+		 */

The comment makes sense, but I'm pretty sure we still prefer to clean it up.

Just something like:

Quoted 8 lines
	struct btrfs_root *root = NULL;

	root = open_ctree();
	if (root)
		ret = 0;
	else
		ret = 1;
	close_ctree(root);

Tested that and it doesn't work, because close_ctree will call btrfs_close_devices which frees existing devices so the test fails.

Quoted 18 lines

Despite that, I think the patch looks good to me.

Thanks,
Qu

Quoted 11 lines
cmds-filesystem.c +7 -0
+		if (open_ctree(search, btrfs_sb_offset(0), 0))
+			ret = 0;
+		else
+			ret = 1;
+	} else {
+		ret = btrfs_scan_devices();
+	}
 
 	if (ret) {
 		error("blkid device scan returned %d", ret);
#5 lore raw
Quoted 49 lines · Nikolay Borisov
Quoted 37 lines · Qu Wenruo
Quoted 20 lines · Nikolay Borisov

When btrfs' 'filesystem' subcommand is passed path to an image file it currently fails since the code expects the image file is going to be recognised by libblkid (called from btrfs_scan_devices()). This is not the case since libblkid only scan well-known locations under /dev.

Fix this by explicitly calling open_ctree which will correctly open the image and add it to the correct btrfs_fs_devices struct. This allows subsequent cmd_filesystem_show logic to correctly show requested information.

---
 cmds-filesystem.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
cmds-filesystem.c +0 -0
index b8beec13f0e5..f55ce9b4ab85 100644

cmds-filesystem.c static int cmd_filesystem_show(int argc, char **argv) · line 778

Quoted 9 lines
cmds-filesystem.c +5 -1
@@ -771,7 +771,18 @@ static int cmd_filesystem_show(int argc, char **argv)
 		goto out;
 
 devs_only:
-	ret = btrfs_scan_devices();
+	if (type == BTRFS_ARG_REG) {
+		/*
+		 * We don't close the fs_info because it will free the device,
+		 * this is not a long-running process so it's fine
+		 */

The comment makes sense, but I'm pretty sure we still prefer to clean it up.

Just something like:

Quoted 8 lines
	struct btrfs_root *root = NULL;

	root = open_ctree();
	if (root)
		ret = 0;
	else
		ret = 1;
	close_ctree(root);
Quoted 2 lines

Tested that and it doesn't work, because close_ctree will call btrfs_close_devices which frees existing devices so the test fails.

Sorry for the confusion, I missed "..." line before close_ctree(root);

I mean something like:

cmds-filesystem.c +1 -0
index c4e43f8446dd..d20cbd49c201 100644
@@ -677,6 +677,7 @@ static int cmd_filesystem_show(int argc, char **argv)
 {
        LIST_HEAD(all_uuids);
        struct btrfs_fs_devices *fs_devices;
+       struct btrfs_root *root = NULL;
        char *search = NULL;
        int ret;
        /* default, search both kernel and udev */
@@ -779,7 +780,8 @@ devs_only:
                 * We don't close the fs_info because it will free the

device,
* this is not a long-running process so it's fine */

-               if (open_ctree(search, btrfs_sb_offset(0), 0))
+               root = open_ctree(search, 0, 0);
+               if (root)
                        ret = 0;
                else
                        ret = 1;
@@ -821,6 +823,7 @@ devs_only:
                free_fs_devices(fs_devices);
        }
 out:
+       close_ctree(root);
        free_seen_fsid(seen_fsid_hash);
        return ret;

}

Thanks,
Qu

Quoted 19 lines
Quoted 19 lines

Despite that, I think the patch looks good to me.

Thanks,
Qu

Quoted 11 lines
cmds-filesystem.c +7 -0
+		if (open_ctree(search, btrfs_sb_offset(0), 0))
+			ret = 0;
+		else
+			ret = 1;
+	} else {
+		ret = btrfs_scan_devices();
+	}
 
 	if (ret) {
 		error("blkid device scan returned %d", ret);
#6 lore raw
Quoted 52 lines · Qu Wenruo
Quoted 49 lines · Nikolay Borisov
Quoted 45 lines · Qu Wenruo
Quoted 20 lines · Nikolay Borisov

When btrfs' 'filesystem' subcommand is passed path to an image file it currently fails since the code expects the image file is going to be recognised by libblkid (called from btrfs_scan_devices()). This is not the case since libblkid only scan well-known locations under /dev.

Fix this by explicitly calling open_ctree which will correctly open the image and add it to the correct btrfs_fs_devices struct. This allows subsequent cmd_filesystem_show logic to correctly show requested information.

---
 cmds-filesystem.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
cmds-filesystem.c +0 -0
index b8beec13f0e5..f55ce9b4ab85 100644

cmds-filesystem.c static int cmd_filesystem_show(int argc, char **argv) · line 778

Quoted 9 lines
cmds-filesystem.c +5 -1
@@ -771,7 +771,18 @@ static int cmd_filesystem_show(int argc, char **argv)
 		goto out;
 
 devs_only:
-	ret = btrfs_scan_devices();
+	if (type == BTRFS_ARG_REG) {
+		/*
+		 * We don't close the fs_info because it will free the device,
+		 * this is not a long-running process so it's fine
+		 */

The comment makes sense, but I'm pretty sure we still prefer to clean it up.

Just something like:

	struct btrfs_root *root = NULL;

	root = open_ctree();
	if (root)
		ret = 0;
	else
		ret = 1;
	close_ctree(root);
Quoted 6 lines
Quoted 2 lines

Tested that and it doesn't work, because close_ctree will call btrfs_close_devices which frees existing devices so the test fails.

Sorry for the confusion, I missed "..." line before close_ctree(root);

I mean something like:

That would work but it quickly becomes ugly due to the other possible failures i.e :

 ret = map_seed_devices(&all_uuids);
        if (ret) {

                error("mapping seed devices returned error %d", ret);

                return 1;

        }

It's not critical since we return 1 and all allocated memory is free. So the options is to either change those 'return 1' statement to 'goto close_ctree' or just leave it as is currently. I opted for the the latter.

Quoted 55 lines
cmds-filesystem.c +3 -1
index c4e43f8446dd..d20cbd49c201 100644
@@ -677,6 +677,7 @@ static int cmd_filesystem_show(int argc, char **argv)
 {
        LIST_HEAD(all_uuids);
        struct btrfs_fs_devices *fs_devices;
+       struct btrfs_root *root = NULL;
        char *search = NULL;
        int ret;
        /* default, search both kernel and udev */
@@ -779,7 +780,8 @@ devs_only:
                 * We don't close the fs_info because it will free the
 device,
                 * this is not a long-running process so it's fine
                 */
-               if (open_ctree(search, btrfs_sb_offset(0), 0))
+               root = open_ctree(search, 0, 0);
+               if (root)
                        ret = 0;
                else
                        ret = 1;
cmds-filesystem.c +1 -0
@@ -821,6 +823,7 @@ devs_only:
                free_fs_devices(fs_devices);
        }
 out:
+       close_ctree(root);
        free_seen_fsid(seen_fsid_hash);
        return ret;
 }

Thanks,
Qu

Quoted 19 lines
Quoted 19 lines

Despite that, I think the patch looks good to me.

Thanks,
Qu

Quoted 11 lines
cmds-filesystem.c +7 -0
+		if (open_ctree(search, btrfs_sb_offset(0), 0))
+			ret = 0;
+		else
+			ret = 1;
+	} else {
+		ret = btrfs_scan_devices();
+	}
 
 	if (ret) {
 		error("blkid device scan returned %d", ret);
Reviewed-by
#7 lore raw
Quoted 74 lines · Nikolay Borisov
Quoted 52 lines · Qu Wenruo
Quoted 49 lines · Nikolay Borisov
Quoted 45 lines · Qu Wenruo
Quoted 20 lines · Nikolay Borisov

When btrfs' 'filesystem' subcommand is passed path to an image file it currently fails since the code expects the image file is going to be recognised by libblkid (called from btrfs_scan_devices()). This is not the case since libblkid only scan well-known locations under /dev.

Fix this by explicitly calling open_ctree which will correctly open the image and add it to the correct btrfs_fs_devices struct. This allows subsequent cmd_filesystem_show logic to correctly show requested information.

---
 cmds-filesystem.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
cmds-filesystem.c +0 -0
index b8beec13f0e5..f55ce9b4ab85 100644

cmds-filesystem.c static int cmd_filesystem_show(int argc, char **argv) · line 778

Quoted 9 lines
cmds-filesystem.c +5 -1
@@ -771,7 +771,18 @@ static int cmd_filesystem_show(int argc, char **argv)
 		goto out;
 
 devs_only:
-	ret = btrfs_scan_devices();
+	if (type == BTRFS_ARG_REG) {
+		/*
+		 * We don't close the fs_info because it will free the device,
+		 * this is not a long-running process so it's fine
+		 */

The comment makes sense, but I'm pretty sure we still prefer to clean it up.

Just something like:

	struct btrfs_root *root = NULL;

	root = open_ctree();
	if (root)
		ret = 0;
	else
		ret = 1;
	close_ctree(root);
Quoted 6 lines
Quoted 2 lines

Tested that and it doesn't work, because close_ctree will call btrfs_close_devices which frees existing devices so the test fails.

Sorry for the confusion, I missed "..." line before close_ctree(root);

I mean something like:

That would work but it quickly becomes ugly due to the other possible failures i.e :

 ret = map_seed_devices(&all_uuids);
        if (ret) {

                error("mapping seed devices returned error %d", ret);

                return 1;

        }
Quoted 3 lines

It's not critical since we return 1 and all allocated memory is free. So the options is to either change those 'return 1' statement to 'goto close_ctree' or just leave it as is currently. I opted for the the latter.

Fair enough.

The problem can be later solved by implementing refcount for each device, but so far we don't have so much desire for that feature, so it should be OK.

Thanks,
Qu

Quoted 56 lines
Quoted 56 lines
cmds-filesystem.c +3 -1
index c4e43f8446dd..d20cbd49c201 100644
@@ -677,6 +677,7 @@ static int cmd_filesystem_show(int argc, char **argv)
 {
        LIST_HEAD(all_uuids);
        struct btrfs_fs_devices *fs_devices;
+       struct btrfs_root *root = NULL;
        char *search = NULL;
        int ret;
        /* default, search both kernel and udev */
@@ -779,7 +780,8 @@ devs_only:
                 * We don't close the fs_info because it will free the
 device,
                 * this is not a long-running process so it's fine
                 */
-               if (open_ctree(search, btrfs_sb_offset(0), 0))
+               root = open_ctree(search, 0, 0);
+               if (root)
                        ret = 0;
                else
                        ret = 1;
cmds-filesystem.c +1 -0
@@ -821,6 +823,7 @@ devs_only:
                free_fs_devices(fs_devices);
        }
 out:
+       close_ctree(root);
        free_seen_fsid(seen_fsid_hash);
        return ret;
 }

Thanks,
Qu

Quoted 19 lines
Quoted 19 lines

Despite that, I think the patch looks good to me.

Thanks,
Qu

Quoted 11 lines
cmds-filesystem.c +7 -0
+		if (open_ctree(search, btrfs_sb_offset(0), 0))
+			ret = 0;
+		else
+			ret = 1;
+	} else {
+		ret = btrfs_scan_devices();
+	}
 
 	if (ret) {
 		error("blkid device scan returned %d", ret);
Applied
#8 lore raw
In reply to Nikolay Borisov
Quoted 11 lines · Nikolay Borisov

When btrfs' 'filesystem' subcommand is passed path to an image file it currently fails since the code expects the image file is going to be recognised by libblkid (called from btrfs_scan_devices()). This is not the case since libblkid only scan well-known locations under /dev.

Fix this by explicitly calling open_ctree which will correctly open the image and add it to the correct btrfs_fs_devices struct. This allows subsequent cmd_filesystem_show logic to correctly show requested information.

Applied, thanks.