aa1e9ab9a9fc6b59e92fdba26848da2a03cca922
[xfstests-dev.git] / tests / btrfs / 094
1 #! /bin/bash
2 # FS QA Test No. btrfs/094
3 #
4 # Test that an incremental send issues valid clone operations for compressed
5 # file extents.
6 #
7 # For some compressed extents, namely those referred by a file extent item with
8 # a non-zero data offset, btrfs could issue a clone operation in the send stream
9 # with an offset and length pair that were not entirely contained in the source
10 # file's range, causing the receiving side to get -EINVAL errors from the clone
11 # ioctl when attempting to perform the clone operations.
12 #
13 # This issue was fixed by the following linux kernel btrfs patch:
14 #
15 #   Btrfs: incremental send, fix clone operations for compressed extents
16 #
17 #-----------------------------------------------------------------------
18 # Copyright (C) 2015 SUSE Linux Products GmbH. All Rights Reserved.
19 # Author: Filipe Manana <fdmanana@suse.com>
20 #
21 # This program is free software; you can redistribute it and/or
22 # modify it under the terms of the GNU General Public License as
23 # published by the Free Software Foundation.
24 #
25 # This program is distributed in the hope that it would be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 # GNU General Public License for more details.
29 #
30 # You should have received a copy of the GNU General Public License
31 # along with this program; if not, write the Free Software Foundation,
32 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
33 #-----------------------------------------------------------------------
34 #
35
36 seq=`basename $0`
37 seqres=$RESULT_DIR/$seq
38 echo "QA output created by $seq"
39
40 tmp=/tmp/$$
41 status=1        # failure is the default!
42 trap "_cleanup; exit \$status" 0 1 2 3 15
43
44 _cleanup()
45 {
46         rm -fr $send_files_dir
47         rm -f $tmp.*
48 }
49
50 # get standard environment, filters and checks
51 . ./common/rc
52 . ./common/filter
53
54 # real QA test starts here
55 _supported_fs btrfs
56 _supported_os Linux
57 _require_scratch
58 _require_cloner
59
60 send_files_dir=$TEST_DIR/btrfs-test-$seq
61
62 rm -f $seqres.full
63 rm -fr $send_files_dir
64 mkdir $send_files_dir
65
66 _scratch_mkfs >>$seqres.full 2>&1
67 _scratch_mount "-o compress"
68
69 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
70
71 # Create the file with a single extent of 32 blocks. This creates a metadata
72 # file extent item with a data start offset of 0 and a logical length of
73 # 32 blocks.
74 $XFS_IO_PROG -f -c "pwrite -S 0xaa $((16 * $BLOCK_SIZE)) $((32 * $BLOCK_SIZE))" \
75              -c "fsync" $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
76
77 # Now rewrite the block range [16, 28[ of our file. This will make
78 # the inode's metadata continue to point to the single 32 block extent
79 # we created before, but now with an extent item that points to the
80 # extent with a data start offset referring to the 28th block and a
81 # logical length of 4 blocks.
82 # That metadata file extent item is associated with the block range
83 # [44, 48[.
84 $XFS_IO_PROG -c "pwrite -S 0xbb $((16 * $BLOCK_SIZE)) $((28 * $BLOCK_SIZE))" \
85              -c "fsync" $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
86
87
88 # Now rewrite the block range [45, 48[. This will make the inode's
89 # metadata continue to point the 32 block extent we created earlier,
90 # with a single extent item that points to it with a start offset
91 # referring to the 28th block and a logical length of 1 block.
92 # That metadata file extent item is associated with the block range
93 # [44, 45[.
94 $XFS_IO_PROG -c "pwrite -S 0xcc $((45 * $BLOCK_SIZE)) $((3 * $BLOCK_SIZE))" \
95              -c "fsync" $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
96
97 _run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/mysnap1
98
99 # Now clone that same region of the 32 block extent into a new file, so that it
100 # gets referenced twice and the incremental send operation below decides to
101 # issue a clone operation instead of copying the data.
102 touch $SCRATCH_MNT/bar
103 $CLONER_PROG -s $((44 * $BLOCK_SIZE)) -d $((44 * $BLOCK_SIZE)) -l $BLOCK_SIZE \
104         $SCRATCH_MNT/foo $SCRATCH_MNT/bar
105
106 _run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/mysnap2
107
108 _run_btrfs_util_prog send -f $send_files_dir/1.snap $SCRATCH_MNT/mysnap1
109 _run_btrfs_util_prog send -p $SCRATCH_MNT/mysnap1 -f $send_files_dir/2.snap \
110         $SCRATCH_MNT/mysnap2
111
112 echo "File contents in the original filesystem:"
113 echo "mysnap1/foo"
114 od -t x1 $SCRATCH_MNT/mysnap1/foo | _filter_od
115 echo "mysnap2/foo"
116 od -t x1 $SCRATCH_MNT/mysnap2/foo | _filter_od
117 echo "mysnap2/bar"
118 od -t x1 $SCRATCH_MNT/mysnap2/bar | _filter_od
119
120 # Now recreate the filesystem by receiving both send streams and verify we get
121 # the same file contents that the original filesystem had.
122 _scratch_unmount
123 _scratch_mkfs >>$seqres.full 2>&1
124 _scratch_mount
125
126 _run_btrfs_util_prog receive -f $send_files_dir/1.snap $SCRATCH_MNT
127 _run_btrfs_util_prog receive -f $send_files_dir/2.snap $SCRATCH_MNT
128
129 echo "File contents in the new filesystem:"
130 echo "mysnap1/foo"
131 od -t x1 $SCRATCH_MNT/mysnap1/foo | _filter_od
132 echo "mysnap2/foo"
133 od -t x1 $SCRATCH_MNT/mysnap2/foo | _filter_od
134 echo "mysnap2/bar"
135 od -t x1 $SCRATCH_MNT/mysnap2/bar | _filter_od
136
137 status=0
138 exit