generic/563: use a loop device to avoid partition incompatibility
[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 _require_scratch
40 _require_dm_target flakey
41 _require_btrfs_command inspect-internal dump-tree
42
43 rm -f $seqres.full
44
45 has_orphan_item()
46 {
47         INO=$1
48         if $BTRFS_UTIL_PROG inspect-internal dump-tree $FLAKEY_DEV | \
49                 grep -q "key (ORPHAN ORPHAN_ITEM $INO)"; then
50                 return 0
51         fi
52         return 1
53 }
54
55 test_orphan()
56 {
57         PRECMD=$1
58         SUB=0
59
60         _scratch_mkfs >> $seqres.full 2>&1
61         _init_flakey
62
63         _mount_flakey
64
65         $PRECMD
66
67         TESTPATH=$SCRATCH_MNT/testdir/testfile
68         DIR=$(dirname $TESTPATH)
69
70         [ -d "$DIR" ] || mkdir -p $DIR
71
72         SIZE=$(( 1024 * 1024 ))
73         run_check dd if=/dev/zero of=$TESTPATH bs=$SIZE count=1
74
75         INO=$(stat -c %i $TESTPATH)
76
77         # Orphan item won't be created if the file doesn't make it to disk
78         sync
79
80         # Open and delete the file
81         exec 27<$TESTPATH
82         rm -f $TESTPATH
83
84         # Ensure the unlink (and orphan item creation) hits the disk
85         sync
86
87         # Turn off writes before closing so the orphan item will be left behind
88         _load_flakey_table $FLAKEY_DROP_WRITES
89
90         # Close the file so we can umount
91         exec 27>&-
92
93         # Orphan item should be on disk if operating correctly
94         _unmount_flakey
95         _load_flakey_table $FLAKEY_ALLOW_WRITES
96         if ! has_orphan_item $INO; then
97                 echo "ERROR: No orphan item found after umount."
98                 return
99         fi
100         _mount_flakey
101
102         # If $DIR is a subvolume, this will cause a lookup and orphan cleanup
103         (cd $DIR; true)
104
105         # Orphan item will be cleaned up during mount but won't be on
106         # disk until there's a sync.
107         sync
108
109         _unmount_flakey
110         if has_orphan_item $INO; then
111                 echo "ERROR: Orphan item found after successful mount/sync."
112         fi
113         _cleanup_flakey
114 }
115
116 new_subvolume()
117 {
118         _run_btrfs_util_prog subvolume create $SCRATCH_MNT/testdir
119 }
120
121 new_default()
122 {
123         new_subvolume
124         SUB=$($BTRFS_UTIL_PROG subvolume list $SCRATCH_MNT |awk '{print $2}')
125         _run_btrfs_util_prog subvolume set-default $SUB $SCRATCH_MNT
126
127         _unmount_flakey
128         _mount_flakey
129 }
130
131 echo "Testing with fs root as default subvolume"
132 test_orphan true
133
134 echo "Testing with explicit default subvolume"
135 test_orphan new_default
136
137 echo "Testing with orphan on non-default subvolume"
138 test_orphan new_subvolume
139
140 status=0
141 exit