common/fuzzy: try to clear blocking flags first in _scratch_fuzz_modify
[xfstests-dev.git] / tests / btrfs / 165
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2018 Fujitsu. All Rights Reserved.
4 #
5 # FS QA Test No. 165
6 #
7 # QA test that checks rmdir(2) works for subvolumes like ordinary directories.
8 #
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")
11 #
12 seq=`basename $0`
13 seqres=$RESULT_DIR/$seq
14 echo "QA output created by $seq"
15
16 here=`pwd`
17 tmp=/tmp/$$
18 status=1        # failure is the default!
19 trap "_cleanup; exit \$status" 0 1 2 3 15
20
21 _cleanup()
22 {
23         cd /
24         rm -f $tmp.*
25 }
26
27 create_subvol()
28 {
29         $BTRFS_UTIL_PROG subvolume create $1 >> $seqres.full 2>&1
30 }
31
32 create_snapshot()
33 {
34         $BTRFS_UTIL_PROG subvolume snapshot $@ >> $seqres.full 2>&1
35 }
36
37 rmdir_subvol()
38 {
39         rmdir $1 >> $seqres.full 2>&1
40 }
41
42 rm_r_subvol() {
43         rm -r $1 >> $seqres.full 2>&1
44 }
45
46 # get standard environment, filters and checks
47 . ./common/rc
48 . ./common/filter
49
50 # remove previous $seqres.full before test
51 rm -f $seqres.full
52
53 # real QA test starts here
54 _supported_fs btrfs
55 _supported_os Linux
56 _require_scratch
57 _require_btrfs_fs_feature "rmdir_subvol"
58
59 _scratch_mkfs > /dev/null 2>&1 || _fail "mkfs failed"
60 _scratch_mount
61
62 # Check that an empty subvolume can be deleted by rmdir
63 create_subvol $SCRATCH_MNT/sub1
64 rmdir_subvol $SCRATCH_MNT/sub1 || \
65         echo "rmdir should delete an empty subvolume"
66
67 # Check that non-empty subvolume cannot be deleted by rmdir
68 create_subvol $SCRATCH_MNT/sub2
69 touch $SCRATCH_MNT/sub2/file
70 rmdir_subvol $SCRATCH_MNT/sub2 && \
71         echo "rmdir should fail for non-empty subvolume"
72 rm $SCRATCH_MNT/sub2/file
73 rmdir_subvol $SCRATCH_MNT/sub2 || \
74         echo "rmdir should delete an empty subvolume"
75
76 # Check that read-only empty subvolume can be deleted by rmdir
77 create_subvol $SCRATCH_MNT/sub3
78 create_snapshot -r $SCRATCH_MNT/sub3 $SCRATCH_MNT/snap
79 $BTRFS_UTIL_PROG property set $SCRATCH_MNT/sub3 ro true >> $seqres.full 2>&1
80 rmdir_subvol $SCRATCH_MNT/sub3 || \
81         echo "rmdir should delete an empty subvolume"
82 rmdir_subvol $SCRATCH_MNT/snap || \
83         echo "rmdir should delete a readonly empty subvolume"
84
85 # Check that the default subvolume cannot be deleted by rmdir
86 create_subvol $SCRATCH_MNT/sub4
87 subvolid=$(_btrfs_get_subvolid $SCRATCH_MNT sub4)
88 $BTRFS_UTIL_PROG subvolume set-default $subvolid $SCRATCH_MNT \
89         >> $seqres.full 2>&1
90 rmdir_subvol $SCRATCH_MNT/sub4 && \
91         echo "rmdir should fail for the default subvolume"
92
93 # Check that subvolume stub (created by snapshot) can be deleted by rmdir
94 # (Note: this has been always allowed)
95 create_subvol $SCRATCH_MNT/sub5
96 create_subvol $SCRATCH_MNT/sub5/sub6
97 create_snapshot $SCRATCH_MNT/sub5 $SCRATCH_MNT/snap2
98 rmdir $SCRATCH_MNT/snap2/sub6 || \
99         echo "rmdir should delete a subvolume stub (ino number == 2)"
100
101 # Check that rm -r works for both non-snapshot subvolume and snapshot
102 create_subvol $SCRATCH_MNT/sub7
103 mkdir $SCRATCH_MNT/sub7/dir
104 create_subvol $SCRATCH_MNT/sub7/dir/sub8
105 touch $SCRATCH_MNT/sub7/dir/sub8/file
106
107 create_snapshot $SCRATCH_MNT/sub7 $SCRATCH_MNT/snap3
108 create_snapshot -r $SCRATCH_MNT/sub7 $SCRATCH_MNT/snap4
109
110 rm_r_subvol $SCRATCH_MNT/sub7 || \
111         echo "rm -r should delete subvolumes recursively"
112 rm_r_subvol $SCRATCH_MNT/snap3 || \
113         echo "rm -r should delete subvolumes recursively"
114 rm_r_subvol $SCRATCH_MNT/snap4 && \
115         echo "rm -r should fail for non-empty readonly subvolume"
116
117 $BTRFS_UTIL_PROG property set $SCRATCH_MNT/snap4 ro false >> $seqres.full 2>&1
118 rm_r_subvol $SCRATCH_MNT/snap4 || \
119         echo "rm -r should delete subvolumes recursively"
120
121 # success, all done
122 echo "Silence is golden"
123 status=0
124 exit