generic: convert tests to SPDX license tags
[xfstests-dev.git] / tests / generic / 485
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2018 Google, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 485
6 #
7 # Regression test for:
8 #    349fa7d6e193 ("ext4: prevent right-shifting extents beyond EXT_MAX_BLOCKS")
9 #    7d83fb14258b ("xfs: prevent creating negative-sized file via INSERT_RANGE")
10 #
11 seq=`basename $0`
12 seqres=$RESULT_DIR/$seq
13 echo "QA output created by $seq"
14
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 $testfile
22 }
23
24 # get standard environment, filters and checks
25 . ./common/rc
26 . ./common/filter
27
28 # remove previous $seqres.full before test
29 rm -f $seqres.full
30
31 # real QA test starts here
32 _supported_fs generic
33 _supported_os Linux
34 _require_test
35 _require_math
36 _require_xfs_io_command "falloc" "-k"
37 _require_xfs_io_command "finsert"
38 _require_xfs_io_command "truncate"
39
40 # Get the maximum size of a file in $TEST_DIR (s_maxbytes).  On ext4 this will
41 # be UINT32_MAX * block_size, but other filesystems may allow up to LLONG_MAX.
42 get_max_file_size()
43 {
44         local testfile=$TEST_DIR/maxfilesize.$seq
45         local l=0
46         local r=9223372036854775807 # LLONG_MAX
47
48         rm -f $testfile
49         while (( l < r )); do
50                 # Use _math() to avoid signed integer overflow.
51                 local m=$(_math "($l + $r + 1) / 2")
52                 if $XFS_IO_PROG -f -c "truncate $m" $testfile \
53                         |& grep -q 'File too large'
54                 then
55                         r=$(( m - 1 ))
56                 else
57                         l=$m
58                 fi
59         done
60         echo $l
61 }
62
63 block_size=$(_get_file_block_size $TEST_DIR)
64 max_file_size=$(get_max_file_size)
65 max_blocks=$((max_file_size / block_size))
66 testfile=$TEST_DIR/testfile.$seq
67
68 echo "# With KEEP_SIZE"
69 rm -f "$testfile"
70
71 # Add an extent at the beginning of the file.  With ext4, this is needed to
72 # reproduce the bug where the extents appear out of order later.
73 $XFS_IO_PROG -f -c "falloc 0 $((2 * block_size))" "$testfile"
74
75 # Add an extent just below s_maxbytes, without changing i_size (i.e. with -k).
76 # The out-of-order extents bug can't be reproduced if i_size is changed, because
77 # then the range insertion fails due to the new i_size being > s_maxbytes.
78 $XFS_IO_PROG -c "falloc -k $(( (max_blocks - 1) * $block_size )) $block_size" \
79         "$testfile"
80
81 # Insert an extent at the beginning of the file.  With the ext4 bug, this caused
82 # the logical block number of the extent just below s_maxbytes to wrap around,
83 # causing the extents to get out of order, causing corruption detected by
84 # e2fsck.  With the fix, the range insertion fails with EINVAL.  Though, with
85 # xfs and f2fs the insertion succeeds, resulting in extents beyond s_maxbytes,
86 # but there is no wraparound -- which is arguably okay.  So we allow either
87 # behavior and just rely on fsck detecting if something went wrong.
88 $XFS_IO_PROG -c "finsert 0 $((2 * block_size))" "$testfile" &>>$seqres.full
89
90 # Also do the same test, but with changing i_size (i.e. without -k).  The
91 # insertion should fail with EFBIG.  This exposed an XFS bug where i_size + len
92 # underwent signed overflow, resulting in a negative-sized file.
93 echo "# Without KEEP_SIZE"
94 rm -f "$testfile"
95 $XFS_IO_PROG -f -c "falloc 0 $((2 * block_size))" "$testfile"
96 $XFS_IO_PROG -c "falloc $(( (max_blocks - 1) * $block_size )) $block_size" \
97         "$testfile"
98 $XFS_IO_PROG -c "finsert 0 $((2 * block_size))" "$testfile"
99
100 status=0
101 exit