2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2018 Fujitsu. All Rights Reserved.
7 # QA test that checks rmdir(2) works for subvolumes like ordinary directories.
9 # This behavior has been restricted long time but becomes allowed by kernel
10 # commit a79a464d5675 ("btrfs: Allow rmdir(2) to delete an empty subvolume")
13 seqres=$RESULT_DIR/$seq
14 echo "QA output created by $seq"
18 status=1 # failure is the default!
19 trap "_cleanup; exit \$status" 0 1 2 3 15
29 $BTRFS_UTIL_PROG subvolume create $1 >> $seqres.full 2>&1
34 $BTRFS_UTIL_PROG subvolume snapshot $@ >> $seqres.full 2>&1
39 rmdir $1 >> $seqres.full 2>&1
43 rm -r $1 >> $seqres.full 2>&1
46 # get standard environment, filters and checks
50 # remove previous $seqres.full before test
53 # real QA test starts here
56 _require_btrfs_fs_feature "rmdir_subvol"
58 _scratch_mkfs > /dev/null 2>&1 || _fail "mkfs failed"
61 # Check that an empty subvolume can be deleted by rmdir
62 create_subvol $SCRATCH_MNT/sub1
63 rmdir_subvol $SCRATCH_MNT/sub1 || \
64 echo "rmdir should delete an empty subvolume"
66 # Check that non-empty subvolume cannot be deleted by rmdir
67 create_subvol $SCRATCH_MNT/sub2
68 touch $SCRATCH_MNT/sub2/file
69 rmdir_subvol $SCRATCH_MNT/sub2 && \
70 echo "rmdir should fail for non-empty subvolume"
71 rm $SCRATCH_MNT/sub2/file
72 rmdir_subvol $SCRATCH_MNT/sub2 || \
73 echo "rmdir should delete an empty subvolume"
75 # Check that read-only empty subvolume can be deleted by rmdir
76 create_subvol $SCRATCH_MNT/sub3
77 create_snapshot -r $SCRATCH_MNT/sub3 $SCRATCH_MNT/snap
78 $BTRFS_UTIL_PROG property set $SCRATCH_MNT/sub3 ro true >> $seqres.full 2>&1
79 rmdir_subvol $SCRATCH_MNT/sub3 || \
80 echo "rmdir should delete an empty subvolume"
81 rmdir_subvol $SCRATCH_MNT/snap || \
82 echo "rmdir should delete a readonly empty subvolume"
84 # Check that the default subvolume cannot be deleted by rmdir
85 create_subvol $SCRATCH_MNT/sub4
86 subvolid=$(_btrfs_get_subvolid $SCRATCH_MNT sub4)
87 $BTRFS_UTIL_PROG subvolume set-default $subvolid $SCRATCH_MNT \
89 rmdir_subvol $SCRATCH_MNT/sub4 && \
90 echo "rmdir should fail for the default subvolume"
92 # Check that subvolume stub (created by snapshot) can be deleted by rmdir
93 # (Note: this has been always allowed)
94 create_subvol $SCRATCH_MNT/sub5
95 create_subvol $SCRATCH_MNT/sub5/sub6
96 create_snapshot $SCRATCH_MNT/sub5 $SCRATCH_MNT/snap2
97 rmdir $SCRATCH_MNT/snap2/sub6 || \
98 echo "rmdir should delete a subvolume stub (ino number == 2)"
100 # Check that rm -r works for both non-snapshot subvolume and snapshot
101 create_subvol $SCRATCH_MNT/sub7
102 mkdir $SCRATCH_MNT/sub7/dir
103 create_subvol $SCRATCH_MNT/sub7/dir/sub8
104 touch $SCRATCH_MNT/sub7/dir/sub8/file
106 create_snapshot $SCRATCH_MNT/sub7 $SCRATCH_MNT/snap3
107 create_snapshot -r $SCRATCH_MNT/sub7 $SCRATCH_MNT/snap4
109 rm_r_subvol $SCRATCH_MNT/sub7 || \
110 echo "rm -r should delete subvolumes recursively"
111 rm_r_subvol $SCRATCH_MNT/snap3 || \
112 echo "rm -r should delete subvolumes recursively"
113 rm_r_subvol $SCRATCH_MNT/snap4 && \
114 echo "rm -r should fail for non-empty readonly subvolume"
116 $BTRFS_UTIL_PROG property set $SCRATCH_MNT/snap4 ro false >> $seqres.full 2>&1
117 rm_r_subvol $SCRATCH_MNT/snap4 || \
118 echo "rm -r should delete subvolumes recursively"
121 echo "Silence is golden"