btrfs/085: check flakey device instead of backend device
[xfstests-dev.git] / tests / btrfs / 085
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2015 SUSE.  All Rights Reserved.
4 #
5 # FS QA Test No. btrfs/085
6 #
7 # Tests to ensure that orphan items are properly created and cleaned up
8 # on next mount.
9 #
10 # There are three cases where orphan items may be cleaned up:
11 # 1) Default subvolume is fs tree root (mkfs default)
12 # 2) Default subvolume is explicitly created subvolume
13 #    (i.e. btrfs subvol set-default)
14 # 3) Non-default subvolume lookup
15 #
16 seq=`basename $0`
17 seqres=$RESULT_DIR/$seq
18 echo "QA output created by $seq"
19
20 here=`pwd`
21 tmp=/tmp/$$
22 status=1        # failure is the default!
23
24 _cleanup()
25 {
26     _cleanup_flakey
27     rm -f $tmp.*
28 }
29
30 trap "_cleanup ; exit \$status" 0 1 2 3 15
31
32 # get standard environment, filters and checks
33 . ./common/rc
34 . ./common/filter
35 . ./common/dmflakey
36
37 # real QA test starts here
38 _supported_fs btrfs
39 _supported_os Linux
40 _require_scratch
41 _require_dm_target flakey
42 _require_btrfs_command inspect-internal dump-tree
43
44 rm -f $seqres.full
45
46 has_orphan_item()
47 {
48         INO=$1
49         if $BTRFS_UTIL_PROG inspect-internal dump-tree $FLAKEY_DEV | \
50                 grep -q "key (ORPHAN ORPHAN_ITEM $INO)"; then
51                 return 0
52         fi
53         return 1
54 }
55
56 test_orphan()
57 {
58         PRECMD=$1
59         SUB=0
60
61         _scratch_mkfs >> $seqres.full 2>&1
62         _init_flakey
63
64         _mount_flakey
65
66         $PRECMD
67
68         TESTPATH=$SCRATCH_MNT/testdir/testfile
69         DIR=$(dirname $TESTPATH)
70
71         [ -d "$DIR" ] || mkdir -p $DIR
72
73         SIZE=$(( 1024 * 1024 ))
74         run_check dd if=/dev/zero of=$TESTPATH bs=$SIZE count=1
75
76         INO=$(stat -c %i $TESTPATH)
77
78         # Orphan item won't be created if the file doesn't make it to disk
79         sync
80
81         # Open and delete the file
82         exec 27<$TESTPATH
83         rm -f $TESTPATH
84
85         # Ensure the unlink (and orphan item creation) hits the disk
86         sync
87
88         # Turn off writes before closing so the orphan item will be left behind
89         _load_flakey_table $FLAKEY_DROP_WRITES
90
91         # Close the file so we can umount
92         exec 27>&-
93
94         # Orphan item should be on disk if operating correctly
95         _unmount_flakey
96         _load_flakey_table $FLAKEY_ALLOW_WRITES
97         if ! has_orphan_item $INO; then
98                 echo "ERROR: No orphan item found after umount."
99                 return
100         fi
101         _mount_flakey
102
103         # If $DIR is a subvolume, this will cause a lookup and orphan cleanup
104         (cd $DIR; true)
105
106         # Orphan item will be cleaned up during mount but won't be on
107         # disk until there's a sync.
108         sync
109
110         _unmount_flakey
111         if has_orphan_item $INO; then
112                 echo "ERROR: Orphan item found after successful mount/sync."
113         fi
114         _cleanup_flakey
115 }
116
117 new_subvolume()
118 {
119         _run_btrfs_util_prog subvolume create $SCRATCH_MNT/testdir
120 }
121
122 new_default()
123 {
124         new_subvolume
125         SUB=$($BTRFS_UTIL_PROG subvolume list $SCRATCH_MNT |awk '{print $2}')
126         _run_btrfs_util_prog subvolume set-default $SUB $SCRATCH_MNT
127
128         _unmount_flakey
129         _mount_flakey
130 }
131
132 echo "Testing with fs root as default subvolume"
133 test_orphan true
134
135 echo "Testing with explicit default subvolume"
136 test_orphan new_default
137
138 echo "Testing with orphan on non-default subvolume"
139 test_orphan new_subvolume
140
141 status=0
142 exit