generic/563: use a loop device to avoid partition incompatibility
[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 _require_scratch
56 _require_btrfs_fs_feature "rmdir_subvol"
57
58 _scratch_mkfs > /dev/null 2>&1 || _fail "mkfs failed"
59 _scratch_mount
60
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"
65
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"
74
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"
83
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 \
88         >> $seqres.full 2>&1
89 rmdir_subvol $SCRATCH_MNT/sub4 && \
90         echo "rmdir should fail for the default subvolume"
91
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)"
99
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
105
106 create_snapshot $SCRATCH_MNT/sub7 $SCRATCH_MNT/snap3
107 create_snapshot -r $SCRATCH_MNT/sub7 $SCRATCH_MNT/snap4
108
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"
115
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"
119
120 # success, all done
121 echo "Silence is golden"
122 status=0
123 exit