From: Filipe Manana <fdmanana@kernel.org>
To: dsterba@suse.cz, Kaiwen Hu <kevinhu@synology.com>,
quwenruo.btrfs@gmx.com, linux-btrfs@vger.kernel.org,
robbieko@synology.com, cccheng@synology.com,
seanding@synology.com
Subject: Re: [PATCH v2] btrfs: prevent subvol with swapfile from being deleted
Date: Wed, 23 Mar 2022 13:33:10 +0000 [thread overview]
Message-ID: <YjshlpsQxGnxZVwX@debian9.Home> (raw)
In-Reply-To: <20220323123429.GK12643@twin.jikos.cz>
On Wed, Mar 23, 2022 at 01:34:29PM +0100, David Sterba wrote:
> On Wed, Mar 23, 2022 at 03:10:32PM +0800, Kaiwen Hu wrote:
> > This patch prevent subvol being deleted when the subvol contains
> > any active swapfile.
> >
> > Since the subvolume is deleted, we cannot swapoff the swapfile in
> > this deleted subvolume. However, the swapfile is still active,
> > we unable to unmount this volume. Let it into some deadlock
> > situation.
> >
> > The test looks like this:
> > mkfs.btrfs -f $dev > /dev/null
> > mount $dev $mnt
> >
> > btrfs sub create $mnt/subvol
> > touch $mnt/subvol/swapfile
> > chmod 600 $mnt/subvol/swapfile
> > chattr +C $mnt/subvol/swapfile
> > dd if=/dev/zero of=$mnt/subvol/swapfile bs=1K count=4096
> > mkswap $mnt/subvol/swapfile
> > swapon $mnt/subvol/swapfile
> >
> > btrfs sub delete $mnt/subvol
> > swapoff $mnt/subvol/swapfile // failed: No such file or directory
> > swapoff --all
> >
> > unmount $mnt // target is busy.
> >
> > To prevent above issue, we simply check that whether the subvolume
> > contains any active swapfile, and stop the deleting process. This
> > behavior is like snapshot ioctl dealing with a swapfile.
> >
> > Reviewed-by: Robbie Ko <robbieko@synology.com>
> > Signed-off-by: Kaiwen Hu <kevinhu@synology.com>
> > ---
> >
> > Add comment on it.
> >
> > fs/btrfs/inode.c | 20 ++++++++++++++++++++
> > 1 file changed, 20 insertions(+)
> >
> > diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
> > index 5bbea5ec31fc..b32def311f44 100644
> > --- a/fs/btrfs/inode.c
> > +++ b/fs/btrfs/inode.c
> > @@ -4460,6 +4460,12 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
> > dest->root_key.objectid);
> > return -EPERM;
> > }
> > + if (atomic_read(&dest->nr_swapfiles)) {
>
> This is under the root_item_lock and the increment in
> btrfs_swap_activate as well, but reading and decrementing nr_swapfiles
> is not, so this is inconsistent. It may not be wrong although still
> racy. The case where I think it could be racing is:
>
> btrfs_swap_activate
> create_snapshot
> atomic_read(&root->nr_swapfiles)
> <continue snapshot>
> atomic_inc(&root->nr_swapfiles)
> <activate swapfile>
>
> and this patch would not prevent it.
That race can not happen due to the protection of the snapshot lock.
In fact that race used to exist, but it was fixed last year by
commit dd0734f2a866f9 ("btrfs: fix race between swap file activation and snapshot creation").
> If it turns out that the root_item_lock is necessary then it would also
> make sense to switch the type of nr_swapfiles to a plain int if we don't
> use the semantics of atomic_t.
next prev parent reply other threads:[~2022-03-23 13:33 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-22 10:27 [PATCH] btrfs: prevent subvol with swapfile from being deleted Kaiwen Hu
2022-03-22 10:47 ` Qu Wenruo
2022-03-22 19:39 ` David Sterba
2022-03-23 3:13 ` Kaiwen Hu
2022-03-23 4:40 ` Qu Wenruo
2022-03-23 7:10 ` [PATCH v2] " Kaiwen Hu
2022-03-23 7:59 ` Qu Wenruo
2022-03-23 12:34 ` David Sterba
2022-03-23 13:33 ` Filipe Manana [this message]
2022-03-23 13:37 ` Filipe Manana
2022-03-23 21:45 ` David Sterba
2022-03-24 4:59 ` Kaiwen Hu
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=YjshlpsQxGnxZVwX@debian9.Home \
--to=fdmanana@kernel.org \
--cc=cccheng@synology.com \
--cc=dsterba@suse.cz \
--cc=kevinhu@synology.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=quwenruo.btrfs@gmx.com \
--cc=robbieko@synology.com \
--cc=seanding@synology.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox