All of lore.kernel.org
 help / color / mirror / Atom feed
From: Boris Burkov <boris@bur.io>
To: Goffredo Baroncelli <kreijack@libero.it>
Cc: linux-btrfs@vger.kernel.org, Goffredo Baroncelli <kreijack@inwind.it>
Subject: Re: [PATCH][TRIVIAL] btrfs-progs: Allow autodetect_object_types() to handle the link.
Date: Wed, 5 Jan 2022 09:50:28 -0800	[thread overview]
Message-ID: <YdXaZP27ALM6KJ9G@zen> (raw)
In-Reply-To: <f4345fcac83ba226efdadcd4610861e434f8a73e.1641389199.git.kreijack@inwind.it>

On Wed, Jan 05, 2022 at 02:32:58PM +0100, Goffredo Baroncelli wrote:
> 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.

Thank you very much for investigating and fixing this. I tested this
patch an it works as advertised.

> 
> 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
> +	 */

I took a look at the original lstat and it doesn't seem super strongly
motivated. It is used to detect a subvolume object (ino==256) so I don't
see why we would hate to have property get/set work on a symlink to a
subvol.

I tested a patch that just changes lstat to stat instead of adding the
second stat, and it handled the subvol case nicely too.

e.g.
ln -s /mnt/real /mnt/lnk
./btrfs property get /mnt/real ro
ro=false
./btrfs property get /mnt/lnk ro
ro=false

> +	ret = stat(object, &st);
> +	if (ret < 0) {
> +		ret = -errno;
> +		goto out;
> +	}
>  	if (S_ISBLK(st.st_mode))
>  		types |= prop_object_dev;
>  
> -- 
> 2.34.1
> 

  reply	other threads:[~2022-01-05 17:50 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YdXaZP27ALM6KJ9G@zen \
    --to=boris@bur.io \
    --cc=kreijack@inwind.it \
    --cc=kreijack@libero.it \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.