generic: _require_dm_target() helper
[xfstests-dev.git] / tests / btrfs / 085
1 #!/bin/bash
2 # FS QA Test No. btrfs/085
3 #
4 # Tests to ensure that orphan items are properly created and cleaned up
5 # on next mount.
6 #
7 # There are three cases where orphan items may be cleaned up:
8 # 1) Default subvolume is fs tree root (mkfs default)
9 # 2) Default subvolume is explicitly created subvolume
10 #    (i.e. btrfs subvol set-default)
11 # 3) Non-default subvolume lookup
12 #
13 #-----------------------------------------------------------------------
14 # Copyright (c) 2015 SUSE.  All Rights Reserved.
15 #
16 # This program is free software; you can redistribute it and/or
17 # modify it under the terms of the GNU General Public License as
18 # published by the Free Software Foundation.
19 #
20 # This program is distributed in the hope that it would be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write the Free Software Foundation,
27 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28 #
29 #-----------------------------------------------------------------------
30 #
31
32 seq=`basename $0`
33 seqres=$RESULT_DIR/$seq
34 echo "QA output created by $seq"
35
36 here=`pwd`
37 tmp=/tmp/$$
38 status=1        # failure is the default!
39
40 _cleanup()
41 {
42     _cleanup_flakey
43     rm -f $tmp.*
44 }
45
46 trap "_cleanup ; exit \$status" 0 1 2 3 15
47
48 # get standard environment, filters and checks
49 . ./common/rc
50 . ./common/filter
51 . ./common/dmflakey
52
53 # real QA test starts here
54 _supported_fs btrfs
55 _supported_os Linux
56 _require_scratch
57 _require_dm_target flakey
58 _need_to_be_root
59
60 BTRFS_DEBUG_TREE_PROG="`set_prog_path btrfs-debug-tree`"
61 _require_command "$BTRFS_DEBUG_TREE_PROG" btrfs-debug-tree
62
63 rm -f $seqres.full
64
65 has_orphan_item()
66 {
67         INO=$1
68         if $BTRFS_DEBUG_TREE_PROG $SCRATCH_DEV | \
69                 grep -q "key (ORPHAN ORPHAN_ITEM $INO)"; then
70                 return 0
71         fi
72         return 1
73 }
74
75 test_orphan()
76 {
77         PRECMD=$1
78         SUB=0
79
80         _scratch_mkfs >> $seqres.full 2>&1
81         _init_flakey
82
83         _mount_flakey
84
85         $PRECMD
86
87         TESTPATH=$SCRATCH_MNT/testdir/testfile
88         DIR=$(dirname $TESTPATH)
89
90         [ -d "$DIR" ] || mkdir -p $DIR
91
92         SIZE=$(( 1024 * 1024 ))
93         run_check dd if=/dev/zero of=$TESTPATH bs=$SIZE count=1
94
95         INO=$(stat -c %i $TESTPATH)
96
97         # Orphan item won't be created if the file doesn't make it to disk
98         sync
99
100         # Open and delete the file
101         exec 27<$TESTPATH
102         rm -f $TESTPATH
103
104         # Ensure the unlink (and orphan item creation) hits the disk
105         sync
106
107         # Turn off writes before closing so the orphan item will be left behind
108         _load_flakey_table $FLAKEY_DROP_WRITES
109
110         # Close the file so we can umount
111         exec 27>&-
112
113         # Orphan item should be on disk if operating correctly
114         _unmount_flakey
115         _load_flakey_table $FLAKEY_ALLOW_WRITES
116         if ! has_orphan_item $INO; then
117                 echo "ERROR: No orphan item found after umount."
118                 return
119         fi
120         _mount_flakey
121
122         # If $DIR is a subvolume, this will cause a lookup and orphan cleanup
123         (cd $DIR; true)
124
125         # Orphan item will be cleaned up during mount but won't be on
126         # disk until there's a sync.
127         sync
128
129         _unmount_flakey
130         if has_orphan_item $INO; then
131                 echo "ERROR: Orphan item found after successful mount/sync."
132         fi
133         _cleanup_flakey
134 }
135
136 new_subvolume()
137 {
138         _run_btrfs_util_prog subvol create $SCRATCH_MNT/testdir
139 }
140
141 new_default()
142 {
143         new_subvolume
144         SUB=$($BTRFS_UTIL_PROG subvol list $SCRATCH_MNT |awk '{print $2}')
145         _run_btrfs_util_prog subvol set-default $SUB $SCRATCH_MNT
146
147         _unmount_flakey
148         _mount_flakey
149 }
150
151 echo "Testing with fs root as default subvolume"
152 test_orphan true
153
154 echo "Testing with explicit default subvolume"
155 test_orphan new_default
156
157 echo "Testing with orphan on non-default subvolume"
158 test_orphan new_subvolume
159
160 status=0
161 exit