btrfs/079: fix failure to umount scratch fs due to running filefrag process
[xfstests-dev.git] / tests / btrfs / 168
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2018 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test No. 168
6 #
7 # Test that we are able to do send operations when one of the source snapshots
8 # (or subvolume) has a file that is deleted while there is still a open file
9 # descriptor for that file.
10 #
11 seq=`basename $0`
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
14 tmp=/tmp/$$
15 status=1        # failure is the default!
16 trap "_cleanup; exit \$status" 0 1 2 3 15
17
18 _cleanup()
19 {
20         cd /
21         rm -f $tmp.*
22         rm -fr $send_files_dir
23 }
24
25 # get standard environment, filters and checks
26 . ./common/rc
27 . ./common/filter
28
29 # real QA test starts here
30 _supported_fs btrfs
31 _supported_os Linux
32 _require_test
33 _require_scratch
34 _require_btrfs_command "property"
35 _require_fssum
36
37 send_files_dir=$TEST_DIR/btrfs-test-$seq
38
39 rm -f $seqres.full
40 rm -fr $send_files_dir
41 mkdir $send_files_dir
42
43 _scratch_mkfs >>$seqres.full 2>&1
44 _scratch_mount
45
46 # Create a subvolume used for first full send test and used to create two
47 # snapshots for the incremental send test.
48 $BTRFS_UTIL_PROG subvolume create $SCRATCH_MNT/sv1 | _filter_scratch
49
50 # Create some test files.
51 $XFS_IO_PROG -f -c "pwrite -S 0xf1 0 64K" $SCRATCH_MNT/sv1/foo >>$seqres.full
52 $XFS_IO_PROG -f -c "pwrite -S 0x7b 0 90K" $SCRATCH_MNT/sv1/bar >>$seqres.full
53 $XFS_IO_PROG -f -c "pwrite -S 0xea 0 256K" $SCRATCH_MNT/sv1/baz >>$seqres.full
54
55 # Flush the previous buffered writes, since setting a subvolume to RO mode
56 # does not do it and we want to check if the data is correctly transmitted by
57 # the send operations.
58 sync
59
60 # Keep an open file descriptor on file bar.
61 exec 73<$SCRATCH_MNT/sv1/bar
62
63 # While the file descriptor is open, delete the file, set the subvolume to
64 # read-only mode and see if a full send operation succeeds.
65 unlink $SCRATCH_MNT/sv1/bar
66 $BTRFS_UTIL_PROG property set $SCRATCH_MNT/sv1 ro true
67 $FSSUM_PROG -A -f -w $send_files_dir/sv1.fssum $SCRATCH_MNT/sv1
68 $BTRFS_UTIL_PROG send -f $send_files_dir/sv1.send $SCRATCH_MNT/sv1 2>&1 \
69         | _filter_scratch
70
71 # Now that the we did the full send, close the file descriptor and set the
72 # subvolume back to read-write mode.
73 exec 73>&-
74 $BTRFS_UTIL_PROG property set $SCRATCH_MNT/sv1 ro false
75
76 # Now try an incremental send operation while there's an open file descriptor
77 # for a file that was deleted from the send snapshot (while it was in read-write
78 # mode).
79
80 # Create a snapshot of the subvolume, to be used later as the parent snapshot
81 # for an incremental send operation.
82 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT/sv1 $SCRATCH_MNT/snap1 \
83         | _filter_scratch
84
85 # First do a full send of this snapshot.
86 $FSSUM_PROG -A -f -w $send_files_dir/snap1.fssum $SCRATCH_MNT/snap1
87 $BTRFS_UTIL_PROG send -f $send_files_dir/snap1.send $SCRATCH_MNT/snap1 2>&1 \
88         | _filter_scratch
89
90 # Modify file baz, to check that the incremental send operation does not miss
91 # that this file has changed.
92 $XFS_IO_PROG -c "pwrite -S 0x19 4K 8K" $SCRATCH_MNT/sv1/baz >>$seqres.full
93
94 # Create a second snapshot of the subvolume, to be used later as the send
95 # snapshot of an incremental send operation.
96 $BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT/sv1 $SCRATCH_MNT/snap2 \
97         | _filter_scratch
98
99 # Temporarily turn the second snapshot to read-write mode and then open a file
100 # descriptor on its foo file.
101 $BTRFS_UTIL_PROG property set $SCRATCH_MNT/snap2 ro false
102 exec 73<$SCRATCH_MNT/snap2/foo
103
104 # Delete the foo file from the second snapshot while holding the file descriptor
105 # open.
106 unlink $SCRATCH_MNT/snap2/foo
107
108 # Set the second snapshot back to RO mode, so that we can use it for the
109 # incremental send operation.
110 $BTRFS_UTIL_PROG property set $SCRATCH_MNT/snap2 ro true
111
112 # Do the incremental send while there's an open file descriptor on file foo from
113 # the second snapshot.
114 $FSSUM_PROG -A -f -w $send_files_dir/snap2.fssum $SCRATCH_MNT/snap2
115 $BTRFS_UTIL_PROG send -f $send_files_dir/snap2.send -p $SCRATCH_MNT/snap1 \
116         $SCRATCH_MNT/snap2 2>&1 | _filter_scratch
117
118 # Now that the incremental send is done close the file descriptor on snap2/foo.
119 exec 73>&-
120
121 # Recreate the filesystem using the send streams and check all files exist,
122 # except for the ones we deleted while holding an open file descriptor on them,
123 # and with correct data and metadata (size, timestamps, ownership, permissions).
124 _scratch_unmount
125 _scratch_mkfs >>$seqres.full 2>&1
126 _scratch_mount
127
128 $BTRFS_UTIL_PROG receive -f $send_files_dir/sv1.send $SCRATCH_MNT
129 $FSSUM_PROG -r $send_files_dir/sv1.fssum $SCRATCH_MNT/sv1
130 $BTRFS_UTIL_PROG receive -f $send_files_dir/snap1.send $SCRATCH_MNT
131 $FSSUM_PROG -r $send_files_dir/snap1.fssum $SCRATCH_MNT/snap1
132 $BTRFS_UTIL_PROG receive -f $send_files_dir/snap2.send $SCRATCH_MNT
133 $FSSUM_PROG -r $send_files_dir/snap2.fssum $SCRATCH_MNT/snap2
134
135 status=0
136 exit