btrfs: fsync after hole punching with no-holes mode
[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
59 BTRFS_DEBUG_TREE_PROG="`set_prog_path btrfs-debug-tree`"
60 _require_command "$BTRFS_DEBUG_TREE_PROG" btrfs-debug-tree
61
62 rm -f $seqres.full
63
64 has_orphan_item()
65 {
66         INO=$1
67         if $BTRFS_DEBUG_TREE_PROG $SCRATCH_DEV | \
68                 grep -q "key (ORPHAN ORPHAN_ITEM $INO)"; then
69                 return 0
70         fi
71         return 1
72 }
73
74 test_orphan()
75 {
76         PRECMD=$1
77         SUB=0
78
79         _scratch_mkfs >> $seqres.full 2>&1
80         _init_flakey
81
82         _mount_flakey
83
84         $PRECMD
85
86         TESTPATH=$SCRATCH_MNT/testdir/testfile
87         DIR=$(dirname $TESTPATH)
88
89         [ -d "$DIR" ] || mkdir -p $DIR
90
91         SIZE=$(( 1024 * 1024 ))
92         run_check dd if=/dev/zero of=$TESTPATH bs=$SIZE count=1
93
94         INO=$(stat -c %i $TESTPATH)
95
96         # Orphan item won't be created if the file doesn't make it to disk
97         sync
98
99         # Open and delete the file
100         exec 27<$TESTPATH
101         rm -f $TESTPATH
102
103         # Ensure the unlink (and orphan item creation) hits the disk
104         sync
105
106         # Turn off writes before closing so the orphan item will be left behind
107         _load_flakey_table $FLAKEY_DROP_WRITES
108
109         # Close the file so we can umount
110         exec 27>&-
111
112         # Orphan item should be on disk if operating correctly
113         _unmount_flakey
114         _load_flakey_table $FLAKEY_ALLOW_WRITES
115         if ! has_orphan_item $INO; then
116                 echo "ERROR: No orphan item found after umount."
117                 return
118         fi
119         _mount_flakey
120
121         # If $DIR is a subvolume, this will cause a lookup and orphan cleanup
122         (cd $DIR; true)
123
124         # Orphan item will be cleaned up during mount but won't be on
125         # disk until there's a sync.
126         sync
127
128         _unmount_flakey
129         if has_orphan_item $INO; then
130                 echo "ERROR: Orphan item found after successful mount/sync."
131         fi
132         _cleanup_flakey
133 }
134
135 new_subvolume()
136 {
137         _run_btrfs_util_prog subvolume create $SCRATCH_MNT/testdir
138 }
139
140 new_default()
141 {
142         new_subvolume
143         SUB=$($BTRFS_UTIL_PROG subvolume list $SCRATCH_MNT |awk '{print $2}')
144         _run_btrfs_util_prog subvolume set-default $SUB $SCRATCH_MNT
145
146         _unmount_flakey
147         _mount_flakey
148 }
149
150 echo "Testing with fs root as default subvolume"
151 test_orphan true
152
153 echo "Testing with explicit default subvolume"
154 test_orphan new_default
155
156 echo "Testing with orphan on non-default subvolume"
157 test_orphan new_subvolume
158
159 status=0
160 exit