generic: test MADV_POPULATE_READ with IO errors
[xfstests-dev.git] / tests / btrfs / 141
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Liu Bo.  All Rights Reserved.
4 #
5 # FS QA Test 141
6 #
7 # Regression test for btrfs buffered read's repair during read.
8 #
9 # Commit 20a7db8ab3f2 ("btrfs: add dummy callback for readpage_io_failed
10 # and drop checks") introduced the regression.
11 #
12 # The upstream fix is
13 #       Commit 9d0d1c8b1c9d ("Btrfs: bring back repair during read")
14 #
15 . ./common/preamble
16 _begin_fstest auto quick read_repair
17
18 # Import common functions.
19 . ./common/filter
20
21 # real QA test starts here
22
23 # Modify as appropriate.
24 _supported_fs btrfs
25 _require_scratch_dev_pool 2
26
27 _require_btrfs_command inspect-internal dump-tree
28
29 get_physical()
30 {
31         local logical=$1
32         local stripe=$2
33         $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
34                 grep $logical -A 6 | \
35                 $AWK_PROG "(\$1 ~ /stripe/ && \$3 ~ /devid/ && \$2 ~ /$stripe/) { print \$6 }"
36 }
37
38 get_devid()
39 {
40         local logical=$1
41         local stripe=$2
42         $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
43                 grep $logical -A 6 | \
44                 $AWK_PROG "(\$1 ~ /stripe/ && \$3 ~ /devid/ && \$2 ~ /$stripe/) { print \$4 }"
45 }
46
47 get_device_path()
48 {
49         local devid=$1
50         echo "$SCRATCH_DEV_POOL" | $AWK_PROG "{print \$$devid}"
51 }
52
53 _scratch_dev_pool_get 2
54 # step 1, create a raid1 btrfs which contains one 128k file.
55 echo "step 1......mkfs.btrfs" >>$seqres.full
56
57 _check_minimal_fs_size $(( 1024 * 1024 * 1024 ))
58 mkfs_opts="-d raid1 -b 1G"
59 _scratch_pool_mkfs $mkfs_opts >>$seqres.full 2>&1
60
61 # make sure data is written to the start position of the data chunk
62 _scratch_mount $(_btrfs_no_v1_cache_opt)
63
64 $XFS_IO_PROG -f -d -c "pwrite -S 0xaa -b 128K 0 128K" "$SCRATCH_MNT/foobar" |\
65         _filter_xfs_io_offset
66
67 # step 2, corrupt the first 64k of one copy (on SCRATCH_DEV which is the first
68 # one in $SCRATCH_DEV_POOL
69 echo "step 2......corrupt file extent" >>$seqres.full
70
71 logical_in_btrfs=$(_btrfs_get_first_logical $SCRATCH_MNT/foobar)
72 physical=$(get_physical ${logical_in_btrfs} 1)
73 devid=$(get_devid ${logical_in_btrfs} 1)
74 devpath=$(get_device_path ${devid})
75
76 _scratch_unmount
77 echo " corrupt stripe #1, devid $devid devpath $devpath physical $physical" \
78         >> $seqres.full
79 $XFS_IO_PROG -d -c "pwrite -S 0xbb -b 64K $physical 64K" $devpath > /dev/null
80
81 _scratch_mount
82
83 # step 3, 128k buffered read (this read can repair bad copy)
84 echo "step 3......repair the bad copy" >>$seqres.full
85
86 _btrfs_buffered_read_on_mirror 1 2 "$SCRATCH_MNT/foobar" 0 128K
87
88 _scratch_unmount
89
90 # check if the repair works
91 $XFS_IO_PROG -c "pread -v -b 512 $physical 512" $devpath |\
92         _filter_xfs_io_offset
93
94 _scratch_dev_pool_put
95 # success, all done
96 status=0
97 exit