Skip to content

Commit 398e6ad

Browse files
committed
block: Deprecate bdrv_set_read_only() and users
bdrv_set_read_only() is used by some block drivers to override the read-only option given by the user. This is not how read-only images generally work in QEMU: Instead of second guessing what the user really meant (which currently includes making an image read-only even if the user didn't only use the default, but explicitly said read-only=off), we should error out if we can't provide what the user requested. This adds deprecation warnings to all callers of bdrv_set_read_only() so that the behaviour can be corrected after the usual deprecation period. Signed-off-by: Kevin Wolf <[email protected]>
1 parent f66afbe commit 398e6ad

File tree

7 files changed

+54
-16
lines changed

7 files changed

+54
-16
lines changed

block.c

+5
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,11 @@ int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only,
261261
return 0;
262262
}
263263

264+
/* TODO Remove (deprecated since 2.11)
265+
* Block drivers are not supposed to automatically change bs->read_only.
266+
* Instead, they should just check whether they can provide what the user
267+
* explicitly requested and error out if read-write is requested, but they can
268+
* only provide read-only access. */
264269
int bdrv_set_read_only(BlockDriverState *bs, bool read_only, Error **errp)
265270
{
266271
int ret = 0;

block/bochs.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "block/block_int.h"
2929
#include "qemu/module.h"
3030
#include "qemu/bswap.h"
31+
#include "qemu/error-report.h"
3132

3233
/**************************************************************/
3334

@@ -110,9 +111,15 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
110111
return -EINVAL;
111112
}
112113

113-
ret = bdrv_set_read_only(bs, true, errp); /* no write support yet */
114-
if (ret < 0) {
115-
return ret;
114+
if (!bdrv_is_read_only(bs)) {
115+
error_report("Opening bochs images without an explicit read-only=on "
116+
"option is deprecated. Future versions will refuse to "
117+
"open the image instead of automatically marking the "
118+
"image read-only.");
119+
ret = bdrv_set_read_only(bs, true, errp); /* no write support yet */
120+
if (ret < 0) {
121+
return ret;
122+
}
116123
}
117124

118125
ret = bdrv_pread(bs->file, 0, &bochs, sizeof(bochs));

block/cloop.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
#include "qemu/osdep.h"
2525
#include "qapi/error.h"
26+
#include "qemu/error-report.h"
2627
#include "qemu-common.h"
2728
#include "block/block_int.h"
2829
#include "qemu/module.h"
@@ -72,9 +73,15 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
7273
return -EINVAL;
7374
}
7475

75-
ret = bdrv_set_read_only(bs, true, errp);
76-
if (ret < 0) {
77-
return ret;
76+
if (!bdrv_is_read_only(bs)) {
77+
error_report("Opening cloop images without an explicit read-only=on "
78+
"option is deprecated. Future versions will refuse to "
79+
"open the image instead of automatically marking the "
80+
"image read-only.");
81+
ret = bdrv_set_read_only(bs, true, errp);
82+
if (ret < 0) {
83+
return ret;
84+
}
7885
}
7986

8087
/* read header */

block/dmg.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,15 @@ static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
419419
return -EINVAL;
420420
}
421421

422-
ret = bdrv_set_read_only(bs, true, errp);
423-
if (ret < 0) {
424-
return ret;
422+
if (!bdrv_is_read_only(bs)) {
423+
error_report("Opening dmg images without an explicit read-only=on "
424+
"option is deprecated. Future versions will refuse to "
425+
"open the image instead of automatically marking the "
426+
"image read-only.");
427+
ret = bdrv_set_read_only(bs, true, errp);
428+
if (ret < 0) {
429+
return ret;
430+
}
425431
}
426432

427433
block_module_load_one("dmg-bz2");

block/rbd.c

+10-4
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,16 @@ static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
665665
/* If we are using an rbd snapshot, we must be r/o, otherwise
666666
* leave as-is */
667667
if (s->snap != NULL) {
668-
r = bdrv_set_read_only(bs, true, &local_err);
669-
if (r < 0) {
670-
error_propagate(errp, local_err);
671-
goto failed_open;
668+
if (!bdrv_is_read_only(bs)) {
669+
error_report("Opening rbd snapshots without an explicit "
670+
"read-only=on option is deprecated. Future versions "
671+
"will refuse to open the image instead of "
672+
"automatically marking the image read-only.");
673+
r = bdrv_set_read_only(bs, true, &local_err);
674+
if (r < 0) {
675+
error_propagate(errp, local_err);
676+
goto failed_open;
677+
}
672678
}
673679
}
674680

block/vvfat.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,11 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
12591259
"Unable to set VVFAT to 'rw' when drive is read-only");
12601260
goto fail;
12611261
}
1262-
} else {
1262+
} else if (!bdrv_is_read_only(bs)) {
1263+
error_report("Opening non-rw vvfat images without an explicit "
1264+
"read-only=on option is deprecated. Future versions "
1265+
"will refuse to open the image instead of "
1266+
"automatically marking the image read-only.");
12631267
/* read only is the default for safety */
12641268
ret = bdrv_set_read_only(bs, true, &local_err);
12651269
if (ret < 0) {

qapi/block-core.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -3134,8 +3134,11 @@
31343134
# This option is required on the top level of blockdev-add.
31353135
# @discard: discard-related options (default: ignore)
31363136
# @cache: cache-related options
3137-
# @read-only: whether the block device should be read-only
3138-
# (default: false)
3137+
# @read-only: whether the block device should be read-only (default: false).
3138+
# Note that some block drivers support only read-only access,
3139+
# either generally or in certain configurations. In this case,
3140+
# the default value does not work and the option must be
3141+
# specified explicitly.
31393142
# @detect-zeroes: detect and optimize zero writes (Since 2.1)
31403143
# (default: off)
31413144
# @force-share: force share all permission on added nodes.

0 commit comments

Comments
 (0)