btrfs/079: fix failure to umount scratch fs due to running filefrag process
[xfstests-dev.git] / tests / btrfs / 097
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
4 #
5 # FS QA Test No. btrfs/097
6 #
7 # Test that an incremental send works after a file gets one of its extents
8 # cloned/deduplicated into lower file offsets.
9 #
10 seq=`basename $0`
11 seqres=$RESULT_DIR/$seq
12 echo "QA output created by $seq"
13
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         rm -fr $send_files_dir
21         rm -f $tmp.*
22 }
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27
28 # real QA test starts here
29 _supported_fs btrfs
30 _supported_os Linux
31 _require_scratch
32 _require_cloner
33
34 send_files_dir=$TEST_DIR/btrfs-test-$seq
35
36 rm -f $seqres.full
37 rm -fr $send_files_dir
38 mkdir $send_files_dir
39
40 _scratch_mkfs >>$seqres.full 2>&1
41 _scratch_mount
42
43 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
44
45 # Create our test file with a single extent of 16 blocks starting at a file
46 # offset mapped by 32nd block.
47 $XFS_IO_PROG -f -c "pwrite -S 0xaa $((32 * $BLOCK_SIZE)) $((16 * $BLOCK_SIZE))" \
48              $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
49
50 _run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/mysnap1
51
52 # Now clone parts of the original extent into lower offsets of the file.
53 #
54 # The first clone operation adds a file extent item to file offset 0 that points
55 # to our initial extent with a data offset of 4 blocks. The corresponding data back
56 # reference in the extent tree has a large value for the 'offset' field, which is
57 # the result of file_offset - data_offset = 0 - (file offset of 4th block).  For
58 # example in case of 4k block size, it will be 0 - 16k = 18446744073709535232.
59
60 # The second clone operation adds a file extent item to file offset mapped by
61 # 4th block that points to our initial extent with a data offset of 12
62 # blocks. The corresponding data back reference in the extent tree has a large
63 # value for the 'offset' field, which is the result of file_offset - data_offset
64 # = (file offset of 4th block) - (file offset of 12th block). For example in
65 # case of 4k block size, it will be 16K - 48K = 18446744073709518848.
66 #
67 # Those large back reference offsets (result of unsigned arithmetic underflow)
68 # confused the back reference walking code (used by an incremental send and
69 # the multiple inspect-internal ioctls) and made it miss the back references,
70 # which for the case of an incremental send it made it fail with -EIO and print
71 # a message like the following to dmesg:
72 #
73 # "BTRFS error (device sdc): did not find backref in send_root. inode=257, \
74 #  offset=0, disk_byte=12845056 found extent=12845056"
75 #
76 $CLONER_PROG -s $(((32 + 4) * $BLOCK_SIZE)) -d 0 -l $((4 * $BLOCK_SIZE)) \
77         $SCRATCH_MNT/foo $SCRATCH_MNT/foo
78 $CLONER_PROG -s $(((32 + 12) * $BLOCK_SIZE)) -d $((4 * $BLOCK_SIZE)) \
79              -l $((4 * $BLOCK_SIZE)) $SCRATCH_MNT/foo $SCRATCH_MNT/foo
80
81 _run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/mysnap2
82
83 _run_btrfs_util_prog send -f $send_files_dir/1.snap $SCRATCH_MNT/mysnap1
84 _run_btrfs_util_prog send -p $SCRATCH_MNT/mysnap1 -f $send_files_dir/2.snap \
85         $SCRATCH_MNT/mysnap2
86
87 echo "File contents in the original filesystem:"
88 od -t x1 $SCRATCH_MNT/mysnap2/foo | _filter_od
89
90 # Now recreate the filesystem by receiving both send streams and verify we get
91 # the same file contents that the original filesystem had.
92 _scratch_unmount
93 _scratch_mkfs >>$seqres.full 2>&1
94 _scratch_mount
95
96 _run_btrfs_util_prog receive -f $send_files_dir/1.snap $SCRATCH_MNT
97 _run_btrfs_util_prog receive -f $send_files_dir/2.snap $SCRATCH_MNT
98
99 echo "File contents in the new filesystem:"
100 od -t x1 $SCRATCH_MNT/mysnap2/foo | _filter_od
101
102 status=0
103 exit