2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2017 Liu Bo. All Rights Reserved.
7 # Regression test for btrfs buffered read's repair during read.
9 # Commit 20a7db8ab3f2 ("btrfs: add dummy callback for readpage_io_failed
10 # and drop checks") introduced the regression.
13 # Commit 9d0d1c8b1c9d ("Btrfs: bring back repair during read")
16 _begin_fstest auto quick
18 # Import common functions.
21 # real QA test starts here
23 # Modify as appropriate.
25 _require_scratch_dev_pool 2
27 _require_btrfs_command inspect-internal dump-tree
28 _require_command "$FILEFRAG_PROG" filefrag
34 $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
35 grep $logical -A 6 | \
36 $AWK_PROG "(\$1 ~ /stripe/ && \$3 ~ /devid/ && \$2 ~ /$stripe/) { print \$6 }"
43 $BTRFS_UTIL_PROG inspect-internal dump-tree -t 3 $SCRATCH_DEV | \
44 grep $logical -A 6 | \
45 $AWK_PROG "(\$1 ~ /stripe/ && \$3 ~ /devid/ && \$2 ~ /$stripe/) { print \$4 }"
51 echo "$SCRATCH_DEV_POOL" | $AWK_PROG "{print \$$devid}"
54 _scratch_dev_pool_get 2
55 # step 1, create a raid1 btrfs which contains one 128k file.
56 echo "step 1......mkfs.btrfs" >>$seqres.full
58 _check_minimal_fs_size $(( 1024 * 1024 * 1024 ))
59 mkfs_opts="-d raid1 -b 1G"
60 _scratch_pool_mkfs $mkfs_opts >>$seqres.full 2>&1
62 # make sure data is written to the start position of the data chunk
63 _scratch_mount $(_btrfs_no_v1_cache_opt)
65 $XFS_IO_PROG -f -d -c "pwrite -S 0xaa -b 128K 0 128K" "$SCRATCH_MNT/foobar" |\
68 # step 2, corrupt the first 64k of one copy (on SCRATCH_DEV which is the first
69 # one in $SCRATCH_DEV_POOL
70 echo "step 2......corrupt file extent" >>$seqres.full
72 ${FILEFRAG_PROG} -v $SCRATCH_MNT/foobar >> $seqres.full
73 logical_in_btrfs=`${FILEFRAG_PROG} -v $SCRATCH_MNT/foobar | _filter_filefrag | cut -d '#' -f 1`
74 physical=$(get_physical ${logical_in_btrfs} 1)
75 devid=$(get_devid ${logical_in_btrfs} 1)
76 devpath=$(get_device_path ${devid})
79 echo " corrupt stripe #1, devid $devid devpath $devpath physical $physical" \
81 $XFS_IO_PROG -d -c "pwrite -S 0xbb -b 64K $physical 64K" $devpath > /dev/null
85 # step 3, 128k buffered read (this read can repair bad copy)
86 echo "step 3......repair the bad copy" >>$seqres.full
88 # since raid1 consists of two copies, and the bad copy was put on stripe #1
89 # while the good copy lies on stripe #0, the bad copy only gets access when the
90 # reader's pid % 2 == 1 is true
92 echo 3 > /proc/sys/vm/drop_caches
93 $XFS_IO_PROG -c "pread -b 128K 0 128K" "$SCRATCH_MNT/foobar" > /dev/null &
96 [ $((pid % 2)) == 1 ] && break
101 # check if the repair works
102 $XFS_IO_PROG -c "pread -v -b 512 $physical 512" $devpath |\
103 _filter_xfs_io_offset
105 _scratch_dev_pool_put