Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/block/monitor/qmp-cmds.c
Line
Count
Source
1
/* SPDX-License-Identifier: GPL-2.0-or-later */
2
#include "qemu/osdep.h"
3
4
#include "system/block-backend.h"
5
#include "block/block_int.h"
6
#include "qapi/qapi-commands-block.h"
7
#include "qapi/error.h"
8
#include "qemu-io.h"
9
10
void qmp_x_qemu_io(const char *device, const char *qdev,
11
                   const char *command, Error **errp)
12
0
{
13
0
    BlockBackend *blk = NULL;
14
0
    BlockBackend *local_blk = NULL;
15
0
    BlockDriverState *bs = NULL;
16
0
    int ret;
17
18
0
    if (!device && !qdev) {
19
0
        error_setg(errp, "Must specify either device or qdev");
20
0
        return;
21
0
    }
22
0
    if (qdev && device) {
23
0
        error_setg(errp, "Cannot specify both qdev and device");
24
0
        return;
25
0
    }
26
27
0
    if (qdev) {
28
0
        blk = blk_by_qdev_id(qdev, errp);
29
0
        if (!blk) {
30
0
            return;
31
0
        }
32
0
    } else {
33
0
        blk = blk_by_name(device);
34
0
        if (!blk) {
35
0
            bs = bdrv_lookup_bs(NULL, device, errp);
36
0
            if (!bs) {
37
0
                return;
38
0
            }
39
0
        }
40
0
    }
41
42
0
    if (bs) {
43
0
        blk = local_blk = blk_new(bdrv_get_aio_context(bs), 0, BLK_PERM_ALL);
44
0
        ret = blk_insert_bs(blk, bs, errp);
45
0
        if (ret < 0) {
46
0
            goto fail;
47
0
        }
48
0
    }
49
50
    /*
51
     * Notably absent: Proper permission management. This is sad, but it seems
52
     * almost impossible to achieve without changing the semantics and thereby
53
     * limiting the use cases of the qemu-io command.
54
     *
55
     * In an ideal world we would unconditionally create a new BlockBackend for
56
     * qemuio_command(), but we have commands like 'reopen' and want them to
57
     * take effect on the exact BlockBackend whose name the user passed instead
58
     * of just on a temporary copy of it.
59
     *
60
     * Another problem is that deleting the temporary BlockBackend involves
61
     * draining all requests on it first, but some qemu-iotests cases want to
62
     * issue multiple aio_read/write requests and expect them to complete in
63
     * the background while the monitor has already returned.
64
     *
65
     * This is also what prevents us from saving the original permissions and
66
     * restoring them later: We can't revoke permissions until all requests
67
     * have completed, and we don't know when that is nor can we really let
68
     * anything else run before we have revoken them to avoid race conditions.
69
     *
70
     * What happens now is that command() in qemu-io-cmds.c can extend the
71
     * permissions if necessary for the qemu-io command. And they simply stay
72
     * extended, possibly resulting in a read-only guest device keeping write
73
     * permissions. Ugly, but it appears to be the lesser evil.
74
     */
75
0
    qemuio_command(blk, command, errp);
76
77
0
fail:
78
0
    blk_unref(local_blk);
79
0
}