Skip to content

Commit 60021bd

Browse files
Kaiwen Hukdave
authored andcommitted
btrfs: prevent subvol with swapfile from being deleted
A subvolume with an active swapfile must not be deleted otherwise it would not be possible to deactivate it. After the subvolume is deleted, we cannot swapoff the swapfile in this deleted subvolume because the path is unreachable. The swapfile is still active and holding references, the filesystem cannot be unmounted. 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. CC: [email protected] # 5.4+ Reviewed-by: Robbie Ko <[email protected]> Reviewed-by: Qu Wenruo <[email protected]> Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Kaiwen Hu <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent a7d16d9 commit 60021bd

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

fs/btrfs/inode.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4487,6 +4487,13 @@ int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
44874487
dest->root_key.objectid);
44884488
return -EPERM;
44894489
}
4490+
if (atomic_read(&dest->nr_swapfiles)) {
4491+
spin_unlock(&dest->root_item_lock);
4492+
btrfs_warn(fs_info,
4493+
"attempt to delete subvolume %llu with active swapfile",
4494+
root->root_key.objectid);
4495+
return -EPERM;
4496+
}
44904497
root_flags = btrfs_root_flags(&dest->root_item);
44914498
btrfs_set_root_flags(&dest->root_item,
44924499
root_flags | BTRFS_ROOT_SUBVOL_DEAD);
@@ -11110,8 +11117,23 @@ static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
1111011117
* set. We use this counter to prevent snapshots. We must increment it
1111111118
* before walking the extents because we don't want a concurrent
1111211119
* snapshot to run after we've already checked the extents.
11120+
*
11121+
* It is possible that subvolume is marked for deletion but still not
11122+
* removed yet. To prevent this race, we check the root status before
11123+
* activating the swapfile.
1111311124
*/
11125+
spin_lock(&root->root_item_lock);
11126+
if (btrfs_root_dead(root)) {
11127+
spin_unlock(&root->root_item_lock);
11128+
11129+
btrfs_exclop_finish(fs_info);
11130+
btrfs_warn(fs_info,
11131+
"cannot activate swapfile because subvolume %llu is being deleted",
11132+
root->root_key.objectid);
11133+
return -EPERM;
11134+
}
1111411135
atomic_inc(&root->nr_swapfiles);
11136+
spin_unlock(&root->root_item_lock);
1111511137

1111611138
isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
1111711139

0 commit comments

Comments
 (0)