All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH][TRIVIAL] btrfs-progs: Allow autodetect_object_types() to handle the link.
@ 2022-01-05 13:32 Goffredo Baroncelli
  2022-01-05 17:50 ` Boris Burkov
  0 siblings, 1 reply; 7+ messages in thread
From: Goffredo Baroncelli @ 2022-01-05 13:32 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Boris Burkov, Goffredo Baroncelli

From: Goffredo Baroncelli <kreijack@inwind.it>


Hi All,

Boris Burkov reported a problem when "btrfs prop get" is invoked on a link of a block
device. This happens when btrfs is invoked on a LVM device (which typically is
a link with an user friendly name to the real block device). In this case btrfs
reports 'ERROR: object is not a btrfs object'.

------------------
Steps to reproduce:

$ sudo losetup -f disk-1.img 
$ sudo losetup -f disk-2.img 
$ sudo losetup -O NAME,BACK-FILE
NAME       BACK-FILE
/dev/loop1 /home/ghigo/test-allocation-hint/disk-2.img
/dev/loop0 /home/ghigo/test-allocation-hint/disk-1.img
                                  
$ cd /dev/
$ mv loop1 loop1-tmp
$ ln -sf loop1-tmp loop1
$ ls -l /dev/loop[01]*
brw-rw---- 1 root disk 7, 0 Jan  5 13:42 /dev/loop0
lrwxrwxrwx 1 root root    9 Jan  5 13:41 /dev/loop1 -> loop1-tmp
brw-rw---- 1 root disk 7, 1 Jan  5 13:42 /dev/loop1-tmp

$ sudo mkfs.btrfs /dev/loop[0-1]
[....]
$ sudo mount /dev/loop1 mnt/

$ # this is the error
$ sudo btrfs prop get /dev/loop1
ERROR: object is not a btrfs object: /dev/loop1

$ # this is what should happen
$ sudo btrfs prop get /dev/loop0
label=

------------------

The cause is in the function autodetect_object_types() that detects the type of
btrfs object passed. If the object is an "inode" type (e.g. file, link...) it
returns the type of object. If the object is a block device (it doesn't matter
if it is in a btrfs filesystem), it returns it as block device.
However it doesn't handle well the case where the object passed is a link
to a block device (which could be a valid btrfs object). For example
LVM/DM creates link to block devices.

This patch adds a further call to stat() (instead of reusing the previous lstat()
returned value) when btrfs-progs checks if the object is a block device.

Reported-by: Boris Burkov <boris@bur.io>
Signed-off-by: Goffredo Baroncelli <kreijack@inwind.it>
---
 cmds/property.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/cmds/property.c b/cmds/property.c
index 59ef997c..97dc5ae1 100644
--- a/cmds/property.c
+++ b/cmds/property.c
@@ -391,6 +391,14 @@ static int autodetect_object_types(const char *object, int *types_out)
 			types |= prop_object_root;
 	}
 
+	/*
+	 * Do a new stat to follow a possible link
+	 */
+	ret = stat(object, &st);
+	if (ret < 0) {
+		ret = -errno;
+		goto out;
+	}
 	if (S_ISBLK(st.st_mode))
 		types |= prop_object_dev;
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-01-05 21:43 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-05 13:32 [PATCH][TRIVIAL] btrfs-progs: Allow autodetect_object_types() to handle the link Goffredo Baroncelli
2022-01-05 17:50 ` Boris Burkov
2022-01-05 18:23   ` Goffredo Baroncelli
2022-01-05 19:05     ` Boris Burkov
2022-01-05 19:14       ` Goffredo Baroncelli
2022-01-05 20:14         ` Goffredo Baroncelli
2022-01-05 21:43           ` Goffredo Baroncelli

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.