1#!/usr/bin/env bash
2# group: rw
3#
4# Produce an I/O error in file-posix, and hope that it is not catastrophic.
5# Regression test for: https://bugzilla.redhat.com/show_bug.cgi?id=2234374
6#
7# Copyright (C) 2023 Red Hat, Inc.
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22
23seq=$(basename "$0")
24echo "QA output created by $seq"
25
26status=1	# failure is the default!
27
28_cleanup()
29{
30    _cleanup_qemu
31    rm -f "$TEST_DIR/fuse-export"
32}
33trap "_cleanup; exit \$status" 0 1 2 3 15
34
35# get standard environment, filters and checks
36. ../common.rc
37. ../common.filter
38. ../common.qemu
39
40# Format-agnostic (we do not use any), but we do test the file protocol
41_supported_proto file
42_require_drivers blkdebug null-co
43
44if [ "$IMGOPTSSYNTAX" = "true" ]; then
45    # We need `$QEMU_IO -f file` to work; IMGOPTSSYNTAX uses --image-opts,
46    # breaking -f.
47    _unsupported_fmt $IMGFMT
48fi
49
50# This is a regression test of a bug in which flie-posix would access zone
51# information in case of an I/O error even when there is no zone information,
52# resulting in a division by zero.
53# To reproduce the problem, we need to trigger an I/O error inside of
54# file-posix, which can be done (rootless) by providing a FUSE export that
55# presents only errors when accessed.
56
57_launch_qemu
58_send_qemu_cmd $QEMU_HANDLE \
59    "{'execute': 'qmp_capabilities'}" \
60    'return'
61
62_send_qemu_cmd $QEMU_HANDLE \
63    "{'execute': 'blockdev-add',
64      'arguments': {
65          'driver': 'blkdebug',
66          'node-name': 'node0',
67          'inject-error': [{'event': 'none'}],
68          'image': {
69              'driver': 'null-co'
70          }
71      }}" \
72    'return'
73
74# FUSE mountpoint must exist and be a regular file
75touch "$TEST_DIR/fuse-export"
76
77# The grep -v to filter fusermount's (benign) error when /etc/fuse.conf does
78# not contain user_allow_other and the subsequent check for missing FUSE support
79# have both been taken from iotest 308.
80output=$(_send_qemu_cmd $QEMU_HANDLE \
81    "{'execute': 'block-export-add',
82      'arguments': {
83          'id': 'exp0',
84          'type': 'fuse',
85          'node-name': 'node0',
86          'mountpoint': '$TEST_DIR/fuse-export',
87          'writable': true
88      }}" \
89    'return' \
90    | grep -v 'option allow_other only allowed if')
91
92if echo "$output" | grep -q "Parameter 'type' does not accept value 'fuse'"; then
93    _notrun 'No FUSE support'
94fi
95echo "$output"
96
97echo
98# This should fail, but gracefully, i.e. just print an I/O error, not crash.
99$QEMU_IO -f file -c 'write 0 64M' "$TEST_DIR/fuse-export" | _filter_qemu_io
100echo
101
102capture_events=BLOCK_EXPORT_DELETED _send_qemu_cmd $QEMU_HANDLE \
103    "{'execute': 'block-export-del',
104      'arguments': {'id': 'exp0'}}" \
105    'return'
106
107_wait_event $QEMU_HANDLE \
108    'BLOCK_EXPORT_DELETED'
109
110_send_qemu_cmd $QEMU_HANDLE \
111    "{'execute': 'blockdev-del',
112      'arguments': {'node-name': 'node0'}}" \
113    'return'
114
115# success, all done
116echo "*** done"
117rm -f $seq.full
118status=0
119