xfs: Check for extent overflow when trivally adding a new extent
[xfstests-dev.git] / tests / btrfs / 056
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2014 Filipe Manana.  All Rights Reserved.
4 #
5 # FS QA Test No. btrfs/056
6 #
7 # Regression test for btrfs ioctl clone operation + fsync + log recovery.
8 # The issue was that doing an fsync after cloning into a file didn't gave any
9 # persistence guarantees as it should. What happened was that the in memory
10 # metadata (extent maps) weren't updated, which made the fsync code not able
11 # to detect that file data has been changed.
12 #
13 # This issue is fixed by the following linux kernel btrfs patch:
14 #
15 #    Btrfs: make fsync work after cloning into a file
16 #
17 seq=`basename $0`
18 seqres=$RESULT_DIR/$seq
19 echo "QA output created by $seq"
20
21 tmp=/tmp/$$
22 status=1        # failure is the default!
23 trap "_cleanup; exit \$status" 0 1 2 3 15
24
25 _cleanup()
26 {
27         _cleanup_flakey
28         rm -fr $tmp
29 }
30
31 # get standard environment, filters and checks
32 . ./common/rc
33 . ./common/filter
34 . ./common/dmflakey
35
36 # real QA test starts here
37 _supported_fs btrfs
38 _require_scratch
39 _require_cloner
40 _require_btrfs_fs_feature "no_holes"
41 _require_btrfs_mkfs_feature "no-holes"
42 _require_dm_target flakey
43
44 rm -f $seqres.full
45
46 test_btrfs_clone_fsync_log_recover()
47 {
48         _scratch_mkfs "$1" >/dev/null 2>&1
49         _init_flakey
50         SAVE_MOUNT_OPTIONS="$MOUNT_OPTIONS"
51         MOUNT_OPTIONS="$MOUNT_OPTIONS $2"
52         _mount_flakey
53
54         BLOCK_SIZE=$(_get_block_size $SCRATCH_MNT)
55
56         EXTENT_SIZE=$((2 * $BLOCK_SIZE))
57
58         # Create a file with 4 extents and 1 hole, all with a size of
59         # 2 blocks each.
60         # The hole is in the block range [4, 5].
61         $XFS_IO_PROG -s -f -c "pwrite -S 0x01 -b $EXTENT_SIZE 0 $EXTENT_SIZE" \
62                         -c "pwrite -S 0x02 -b $EXTENT_SIZE $((2 * $BLOCK_SIZE)) $EXTENT_SIZE" \
63                         -c "pwrite -S 0x04 -b $EXTENT_SIZE $((6 * $BLOCK_SIZE)) $EXTENT_SIZE" \
64                         -c "pwrite -S 0x05 -b $EXTENT_SIZE $((8 * $BLOCK_SIZE)) $EXTENT_SIZE" \
65                 $SCRATCH_MNT/foo | _filter_xfs_io_blocks_modified
66
67         # Clone destination file, 1 extent of 24 blocks.
68         $XFS_IO_PROG -f -c "pwrite -S 0xff -b $((24 * $BLOCK_SIZE)) 0 $((24 * $BLOCK_SIZE))" \
69                      -c "fsync" $SCRATCH_MNT/bar | _filter_xfs_io_blocks_modified
70
71         # Clone second half of the 2nd extent, the 2 block hole, the 3rd extent
72         # and the first half of the 4th extent into file bar.
73         $CLONER_PROG -s $((3 * $BLOCK_SIZE)) -d 0 -l $((6 * $BLOCK_SIZE)) \
74                      $SCRATCH_MNT/foo $SCRATCH_MNT/bar
75         $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/bar
76
77         # Test small files too consisting of 1 inline extent
78         EXTENT_SIZE=$(($BLOCK_SIZE - 48))
79         $XFS_IO_PROG -f -c "pwrite -S 0x00 -b $EXTENT_SIZE 0 $EXTENT_SIZE" -c "fsync" \
80                 $SCRATCH_MNT/foo2 | _filter_xfs_io_blocks_modified
81
82         EXTENT_SIZE=$(($BLOCK_SIZE - 1048))
83         $XFS_IO_PROG -f -c "pwrite -S 0xcc -b $EXTENT_SIZE 0 $EXTENT_SIZE" -c "fsync" \
84                 $SCRATCH_MNT/bar2 | _filter_xfs_io_blocks_modified
85
86         # Clone the entire foo2 file into bar2, overwriting all data in bar2
87         # and increasing its size.
88         EXTENT_SIZE=$(($BLOCK_SIZE - 48))
89         $CLONER_PROG -s 0 -d 0 -l $EXTENT_SIZE $SCRATCH_MNT/foo2 $SCRATCH_MNT/bar2
90         $XFS_IO_PROG -c "fsync" $SCRATCH_MNT/bar2
91
92         _flakey_drop_and_remount yes
93
94         # Verify the cloned range was persisted by fsync and the log recovery
95         # code did its work well.
96         echo "Verifying file bar content"
97         od -t x1 $SCRATCH_MNT/bar | _filter_od
98
99         echo "Verifying file bar2 content"
100         od -t x1 $SCRATCH_MNT/bar2 | _filter_od
101
102         _unmount_flakey
103
104         # Verify that there are no consistency errors.
105         _check_scratch_fs $FLAKEY_DEV
106
107         _cleanup_flakey
108         MOUNT_OPTIONS="$SAVE_MOUNT_OPTIONS"
109 }
110
111 # Regardless of the NO_HOLES feature being enabled or not, the test results
112 # should be exactly the same for both cases.
113
114 echo "Testing without the NO_HOLES feature"
115 # As of btrfs-progs 3.14.x, the no-holes feature isn't enabled by default.
116 # But explicitly disable it at mkfs time as it might be enabled by default
117 # in future versions.
118 test_btrfs_clone_fsync_log_recover "-O ^no-holes"
119
120 echo "Testing without the NO_HOLES feature and compression (lzo)"
121 test_btrfs_clone_fsync_log_recover "-O ^no-holes" "-o compress-force=lzo"
122
123 echo "Testing with the NO_HOLES feature enabled"
124 test_btrfs_clone_fsync_log_recover "-O no-holes"
125
126 echo "Testing with the NO_HOLES feature enabled and compression (lzo)"
127 test_btrfs_clone_fsync_log_recover "-O no-holes" "-o compress-force=lzo"
128
129 status=0
130 exit