File 0003-btrfs-progs-inspect-use-find_mount_fsroot-in-logical.patch of Package btrfsprogs.17950

From 4229af99a351c5fa0fe7ed4170258c6ac9db0562 Mon Sep 17 00:00:00 2001
From: Marcos Paulo de Souza <mpdesouza@suse.com>
Date: Wed, 13 Jan 2021 13:55:31 -0300
Subject: [PATCH 3/4] btrfs-progs: inspect: use find_mount_fsroot in
 logical-resolve

By using find_mount_fsroot we ensure that we return a valid path to the
final user, by ensuring that even if we return a bind mount, the
pathname of btrfs used was the same from the original mount.

This for a case when bind mounts and normal mount -o subvol=/path are
mixed.

Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
---
 cmds-inspect.c |   45 ++++++++++++++++++++++++++++++++++++---------
 1 file changed, 36 insertions(+), 9 deletions(-)

--- a/cmds-inspect.c
+++ b/cmds-inspect.c
@@ -234,6 +234,7 @@ static int cmd_inspect_logical_resolve(i
 		DIR *dirs = NULL;
 
 		if (getpath) {
+			char mount_path[PATH_MAX];
 			name = btrfs_list_path_for_root(fd, root);
 			if (IS_ERR(name)) {
 				ret = PTR_ERR(name);
@@ -242,24 +243,50 @@ static int cmd_inspect_logical_resolve(i
 			if (!name) {
 				path_ptr[-1] = '\0';
 				path_fd = fd;
+				strncpy(mount_path, full_path, PATH_MAX);
 			} else {
-				path_ptr[-1] = '/';
-				ret = snprintf(path_ptr, bytes_left, "%s",
-						name);
-				free(name);
-				if (ret >= bytes_left) {
-					error("path buffer too small: %d bytes",
-							bytes_left - ret);
+				char *mounted = NULL;
+				char subvol[PATH_MAX];
+				char subvolid[PATH_MAX];
+
+				/*
+				 * btrfs_list_path_for_root returns the full
+				 * path to the subvolume pointed by root, but the
+				 * subvolume can be mounted in a directory name
+				 * different from the subvolume name. In this
+				 * case we need to find the correct mount point
+				 * using same subvolume path and subvol id found
+				 * before.
+				 */
+
+				snprintf(subvol, PATH_MAX, "/%s", name);
+				snprintf(subvolid, PATH_MAX, "%llu", root);
+
+				ret = find_mount_fsroot(subvol, subvolid, &mounted);
+
+				if (ret) {
+					error("failed to parse mountinfo");
 					goto out;
 				}
-				path_fd = btrfs_open_dir(full_path, &dirs, 1);
+
+				if (!mounted) {
+					printf(
+			"inode %llu subvol %s could not be accessed: not mounted\n",
+						inum, name);
+					continue;
+				}
+
+				strncpy(mount_path, mounted, PATH_MAX);
+				free(mounted);
+
+				path_fd = btrfs_open_dir(mount_path, &dirs, 1);
 				if (path_fd < 0) {
 					ret = -ENOENT;
 					goto out;
 				}
 			}
 			ret = __ino_to_path_fd(inum, path_fd, verbose,
-						full_path);
+						mount_path);
 			if (path_fd != fd)
 				close_file_or_dir(path_fd, dirs);
 		} else {
openSUSE Build Service is sponsored by