btrfs: check qgroup doesn't crash when beyond limit
[xfstests-dev.git] / tests / btrfs / 094
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/094
6 #
7 # Test that an incremental send issues valid clone operations for compressed
8 # file extents.
9 #
10 # For some compressed extents, namely those referred by a file extent item with
11 # a non-zero data offset, btrfs could issue a clone operation in the send stream
12 # with an offset and length pair that were not entirely contained in the source
13 # file's range, causing the receiving side to get -EINVAL errors from the clone
14 # ioctl when attempting to perform the clone operations.
15 #
16 # This issue was fixed by the following linux kernel btrfs patch:
17 #
18 #   Btrfs: incremental send, fix clone operations for compressed extents
19 #
20 seq=`basename $0`
21 seqres=$RESULT_DIR/$seq
22 echo "QA output created by $seq"
23
24 tmp=/tmp/$$
25 status=1        # failure is the default!
26 trap "_cleanup; exit \$status" 0 1 2 3 15
27
28 _cleanup()
29 {
30         rm -fr $send_files_dir
31         rm -f $tmp.*
32 }
33
34 # get standard environment, filters and checks
35 . ./common/rc
36 . ./common/filter
37
38 # real QA test starts here
39 _supported_fs btrfs
40 _require_scratch
41 _require_cloner
42
43 send_files_dir=$TEST_DIR/btrfs-test-$seq
44
45 rm -f $seqres.full
46 rm -fr $send_files_dir
47 mkdir $send_files_dir
48
49 _scratch_mkfs >>$seqres.full 2>&1
50 _scratch_mount "-o compress"
51
52 BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
53
54 # Create the file with a single extent of 32 blocks. This creates a metadata
55 # file extent item with a data start offset of 0 and a logical length of
56 # 32 blocks.
57 $XFS_IO_PROG -f -c "pwrite -S 0xaa $((16 * $BLOCK_SIZE)) $((32 * $BLOCK_SIZE))" \
58              -c "fsync" $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
59
60 # Now rewrite the block range [16, 28[ of our file. This will make
61 # the inode's metadata continue to point to the single 32 block extent
62 # we created before, but now with an extent item that points to the
63 # extent with a data start offset referring to the 28th block and a
64 # logical length of 4 blocks.
65 # That metadata file extent item is associated with the block range
66 # [44, 48[.
67 $XFS_IO_PROG -c "pwrite -S 0xbb $((16 * $BLOCK_SIZE)) $((28 * $BLOCK_SIZE))" \
68              -c "fsync" $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
69
70
71 # Now rewrite the block range [45, 48[. This will make the inode's
72 # metadata continue to point the 32 block extent we created earlier,
73 # with a single extent item that points to it with a start offset
74 # referring to the 28th block and a logical length of 1 block.
75 # That metadata file extent item is associated with the block range
76 # [44, 45[.
77 $XFS_IO_PROG -c "pwrite -S 0xcc $((45 * $BLOCK_SIZE)) $((3 * $BLOCK_SIZE))" \
78              -c "fsync" $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
79
80 _run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/mysnap1
81
82 # Now clone that same region of the 32 block extent into a new file, so that it
83 # gets referenced twice and the incremental send operation below decides to
84 # issue a clone operation instead of copying the data.
85 touch $SCRATCH_MNT/bar
86 $CLONER_PROG -s $((44 * $BLOCK_SIZE)) -d $((44 * $BLOCK_SIZE)) -l $BLOCK_SIZE \
87         $SCRATCH_MNT/foo $SCRATCH_MNT/bar
88
89 _run_btrfs_util_prog subvolume snapshot -r $SCRATCH_MNT $SCRATCH_MNT/mysnap2
90
91 _run_btrfs_util_prog send -f $send_files_dir/1.snap $SCRATCH_MNT/mysnap1
92 _run_btrfs_util_prog send -p $SCRATCH_MNT/mysnap1 -f $send_files_dir/2.snap \
93         $SCRATCH_MNT/mysnap2
94
95 echo "File contents in the original filesystem:"
96 echo "mysnap1/foo"
97 od -t x1 $SCRATCH_MNT/mysnap1/foo | _filter_od
98 echo "mysnap2/foo"
99 od -t x1 $SCRATCH_MNT/mysnap2/foo | _filter_od
100 echo "mysnap2/bar"
101 od -t x1 $SCRATCH_MNT/mysnap2/bar | _filter_od
102
103 # Now recreate the filesystem by receiving both send streams and verify we get
104 # the same file contents that the original filesystem had.
105 _scratch_unmount
106 _scratch_mkfs >>$seqres.full 2>&1
107 _scratch_mount
108
109 _run_btrfs_util_prog receive -f $send_files_dir/1.snap $SCRATCH_MNT
110 _run_btrfs_util_prog receive -f $send_files_dir/2.snap $SCRATCH_MNT
111
112 echo "File contents in the new 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 status=0
121 exit