generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / tests / generic / 590
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2019 Facebook.  All Rights Reserved.
4 #
5 # FS QA Test 590
6 #
7 # Test commit 0c4da70c83d4 ("xfs: fix realtime file data space leak") and
8 # 69ffe5960df1 ("xfs: don't check for AG deadlock for realtime files in
9 # bunmapi"). On XFS without the fixes, truncate will hang forever. On other
10 # filesystems, this just tests writing into big fallocates.
11 #
12 . ./common/preamble
13 _begin_fstest auto prealloc preallocrw
14
15 # Override the default cleanup function.
16 _cleanup()
17 {
18         cd /
19         rm -f $tmp.*
20         test -n "$loop" && _destroy_loop_device "$loop"
21         rm -f "$TEST_DIR/$seq"
22 }
23
24 . ./common/filter
25
26 _supported_fs generic
27 _require_scratch_nocheck
28 _require_xfs_io_command "falloc"
29
30 maxextlen=$((0x1fffff))
31 bs=4096
32 rextsize=4
33 filesz=$(((maxextlen + 1) * bs))
34
35 must_disable_feature() {
36         local feat="$1"
37
38         # If mkfs doesn't know about the feature, we don't need to disable it
39         $MKFS_XFS_PROG --help 2>&1 | grep -q "${feat}=0" || return 1
40
41         # If turning the feature on works, we don't need to disable it
42         _scratch_mkfs_xfs_supported -m "${feat}=1" "${disabled_features[@]}" \
43                 > /dev/null 2>&1 && return 1
44
45         # Otherwise mkfs knows of the feature and formatting with it failed,
46         # so we do need to mask it.
47         return 0
48 }
49
50 extra_options=""
51 # If we're testing XFS, set up the realtime device to reproduce the bug.
52 if [[ $FSTYP = xfs ]]; then
53         # If we don't have a realtime device, set up a loop device on the test
54         # filesystem.
55         if [[ $USE_EXTERNAL != yes || -z $SCRATCH_RTDEV ]]; then
56                 _require_test
57                 loopsz="$((filesz + (1 << 26)))"
58                 _require_fs_space "$TEST_DIR" $((loopsz / 1024))
59                 $XFS_IO_PROG -c "truncate $loopsz" -f "$TEST_DIR/$seq"
60                 loop="$(_create_loop_device "$TEST_DIR/$seq")"
61                 USE_EXTERNAL=yes
62                 SCRATCH_RTDEV="$loop"
63                 disabled_features=()
64
65                 # disable reflink if not supported by realtime devices
66                 must_disable_feature reflink &&
67                         disabled_features=(-m reflink=0)
68
69                 # disable rmap if not supported by realtime devices
70                 must_disable_feature rmapbt &&
71                         disabled_features+=(-m rmapbt=0)
72         fi
73         extra_options="$extra_options -r extsize=$((bs * rextsize))"
74         extra_options="$extra_options -d agsize=$(((maxextlen + 1) * bs / 2)),rtinherit=1"
75
76         _scratch_mkfs $extra_options "${disabled_features[@]}" >>$seqres.full 2>&1
77         _try_scratch_mount >>$seqres.full 2>&1 || \
78                 _notrun "mount failed, kernel doesn't support realtime?"
79         _scratch_unmount
80 else
81         _scratch_mkfs $extra_options >>$seqres.full 2>&1
82 fi
83 _scratch_mount
84 _require_fs_space "$SCRATCH_MNT" $((filesz / 1024))
85
86 # Allocate maxextlen + 1 blocks. As long as the allocator does something sane,
87 # we should end up with two extents that look something like:
88 #
89 # u3.bmx[0-1] = [startoff,startblock,blockcount,extentflag]
90 # 0:[0,0,2097148,1]
91 # 1:[2097148,2097148,4,1]
92 #
93 # Extent 0 has blockcount = ALIGN_DOWN(maxextlen, rextsize). Extent 1 is
94 # adjacent and has blockcount = rextsize. Both are unwritten.
95 $XFS_IO_PROG -c "falloc 0 $filesz" -c fsync -f "$SCRATCH_MNT/file"
96
97 # Write extent 0 + one block of extent 1. Our extents should end up like so:
98 #
99 # u3.bmx[0-1] = [startoff,startblock,blockcount,extentflag]
100 # 0:[0,0,2097149,0]
101 # 1:[2097149,2097149,3,1]
102 #
103 # Extent 0 is written and has blockcount = ALIGN_DOWN(maxextlen, rextsize) + 1,
104 # Extent 1 is adjacent, unwritten, and has blockcount = rextsize - 1 and
105 # startblock % rextsize = 1.
106 #
107 # The -b is just to speed things up (doing GBs of I/O in 4k chunks kind of
108 # sucks).
109 $XFS_IO_PROG -c "pwrite -b 1M -W 0 $(((maxextlen + 2 - rextsize) * bs))" \
110         "$SCRATCH_MNT/file" >> "$seqres.full"
111
112 # Truncate the extents.
113 $XFS_IO_PROG -c "truncate 0" -c fsync "$SCRATCH_MNT/file"
114
115 # We need to do this before the loop device gets torn down.
116 _scratch_unmount
117 _check_scratch_fs
118
119 echo "Silence is golden"
120 status=0
121 exit