From cb740b3dfb97f09e458c4e0bcfd87a6f5d1e7a33 Mon Sep 17 00:00:00 2001
From: David Sterba <dsterba@suse.com>
Date: Wed, 31 Jan 2024 10:58:17 +0100
Subject: [PATCH] btrfs-progs: fi show: canonicalize path when using blkid and
 -d

There's a report that passing raw device mapper path and -d don't work
together:

  yyy@xxx ~ $ sudo btrfs filesystem show /dev/dm-0
  Label: none  uuid: a7fbb8d6-ec5d-4e88-bd8b-c686553e0dc7
	  Total devices 1 FS bytes used 144.00KiB
	  devid    1 size 256.00MiB used 88.00MiB path /dev/mapper/da0972636816-LogVol00

  With --all-devices

  yyy@xxx ~ $ sudo btrfs filesystem show --all-devices /dev/dm-0
  ERROR: not a valid btrfs filesystem: /dev/dm-0

Where dm-0 corresponds to the LogVol00 device from above.

Passing the option -d skips some steps but still uses the real path of
the device that is required for scanning and identification, while
blkid uses the canonicalized path.

The combination of raw device name and -d was not handled as the raw
path is not in cache and thus not recognized. Canonicalization fixes
that although this changes the device name in the output.

Issue: #732
Signed-off-by: David Sterba <dsterba@suse.com>
---
 cmds/filesystem.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/cmds/filesystem.c b/cmds/filesystem.c
index 1b444b8..4967bba 100644
--- a/cmds/filesystem.c
+++ b/cmds/filesystem.c
@@ -708,6 +708,7 @@ static int cmd_filesystem_show(const struct cmd_struct *cmd,
 	struct btrfs_fs_devices *fs_devices;
 	struct btrfs_root *root = NULL;
 	char *search = NULL;
+	char *canon_path = NULL;
 	int ret;
 	/* default, search both kernel and udev */
 	int where = -1;
@@ -789,8 +790,15 @@ static int cmd_filesystem_show(const struct cmd_struct *cmd,
 		}
 	}
 
-	if (where == BTRFS_SCAN_LBLKID)
+	if (where == BTRFS_SCAN_LBLKID) {
+		/*
+		 * Blkid needs canonicalized paths, eg. when the /dev/dm-0 is
+		 * passed on command line.
+		 */
+		canon_path = path_canonicalize(search);
+		search = canon_path;
 		goto devs_only;
+	}
 
 	/* show mounted btrfs */
 	ret = btrfs_scan_kernel(search, unit_mode);
@@ -849,6 +857,7 @@ devs_only:
 		free_fs_devices(fs_devices);
 	}
 out:
+	free(canon_path);
 	if (root)
 		close_ctree(root);
 	free_seen_fsid(seen_fsid_hash);
-- 
2.43.0

