block: consistently use bdrv_is_read_only()
It's better to use accessor function instead of bs->read_only directly. In some places use bdrv_is_writable() instead of checking both BDRV_O_RDWR set and BDRV_O_INACTIVE not set. In bdrv_open_common() it's a bit strange to add one more variable, but we are going to drop bs->read_only in the next patch, so new ro local variable substitutes it here. Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> Message-Id: <20210527154056.70294-2-vsementsov@virtuozzo.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
39df2c6d57
commit
307261b243
11
block.c
11
block.c
@ -1720,6 +1720,7 @@ static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
|
||||
QemuOpts *opts;
|
||||
BlockDriver *drv;
|
||||
Error *local_err = NULL;
|
||||
bool ro;
|
||||
|
||||
assert(bs->file == NULL);
|
||||
assert(options != NULL && bs->options != options);
|
||||
@ -1772,15 +1773,17 @@ static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
|
||||
|
||||
bs->read_only = !(bs->open_flags & BDRV_O_RDWR);
|
||||
|
||||
if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
|
||||
if (!bs->read_only && bdrv_is_whitelisted(drv, true)) {
|
||||
ro = bdrv_is_read_only(bs);
|
||||
|
||||
if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, ro)) {
|
||||
if (!ro && bdrv_is_whitelisted(drv, true)) {
|
||||
ret = bdrv_apply_auto_read_only(bs, NULL, NULL);
|
||||
} else {
|
||||
ret = -ENOTSUP;
|
||||
}
|
||||
if (ret < 0) {
|
||||
error_setg(errp,
|
||||
!bs->read_only && bdrv_is_whitelisted(drv, true)
|
||||
!ro && bdrv_is_whitelisted(drv, true)
|
||||
? "Driver '%s' can only be used for read-only devices"
|
||||
: "Driver '%s' is not whitelisted",
|
||||
drv->format_name);
|
||||
@ -1792,7 +1795,7 @@ static int bdrv_open_common(BlockDriverState *bs, BlockBackend *file,
|
||||
assert(qatomic_read(&bs->copy_on_read) == 0);
|
||||
|
||||
if (bs->open_flags & BDRV_O_COPY_ON_READ) {
|
||||
if (!bs->read_only) {
|
||||
if (!ro) {
|
||||
bdrv_enable_copy_on_read(bs);
|
||||
} else {
|
||||
error_setg(errp, "Can't use copy-on-read on read-only device");
|
||||
|
@ -2269,7 +2269,7 @@ void blk_update_root_state(BlockBackend *blk)
|
||||
assert(blk->root);
|
||||
|
||||
blk->root_state.open_flags = blk->root->bs->open_flags;
|
||||
blk->root_state.read_only = blk->root->bs->read_only;
|
||||
blk->root_state.read_only = bdrv_is_read_only(blk->root->bs);
|
||||
blk->root_state.detect_zeroes = blk->root->bs->detect_zeroes;
|
||||
}
|
||||
|
||||
|
@ -453,7 +453,7 @@ int bdrv_commit(BlockDriverState *bs)
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
ro = backing_file_bs->read_only;
|
||||
ro = bdrv_is_read_only(backing_file_bs);
|
||||
|
||||
if (ro) {
|
||||
if (bdrv_reopen_set_read_only(backing_file_bs, false, NULL)) {
|
||||
|
@ -1973,7 +1973,7 @@ bdrv_co_write_req_prepare(BdrvChild *child, int64_t offset, int64_t bytes,
|
||||
|
||||
bdrv_check_request(offset, bytes, &error_abort);
|
||||
|
||||
if (bs->read_only) {
|
||||
if (bdrv_is_read_only(bs)) {
|
||||
return -EPERM;
|
||||
}
|
||||
|
||||
@ -3406,7 +3406,7 @@ int coroutine_fn bdrv_co_truncate(BdrvChild *child, int64_t offset, bool exact,
|
||||
if (new_bytes) {
|
||||
bdrv_make_request_serialising(&req, 1);
|
||||
}
|
||||
if (bs->read_only) {
|
||||
if (bdrv_is_read_only(bs)) {
|
||||
error_setg(errp, "Image is read-only");
|
||||
ret = -EACCES;
|
||||
goto out;
|
||||
|
@ -59,7 +59,7 @@ BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
|
||||
|
||||
info = g_malloc0(sizeof(*info));
|
||||
info->file = g_strdup(bs->filename);
|
||||
info->ro = bs->read_only;
|
||||
info->ro = bdrv_is_read_only(bs);
|
||||
info->drv = g_strdup(bs->drv->format_name);
|
||||
info->encrypted = bs->encrypted;
|
||||
|
||||
|
@ -1026,7 +1026,7 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs,
|
||||
int new_l1_bytes;
|
||||
int ret;
|
||||
|
||||
assert(bs->read_only);
|
||||
assert(bdrv_is_read_only(bs));
|
||||
|
||||
/* Search the snapshot */
|
||||
snapshot_index = find_snapshot_by_id_and_name(bs, snapshot_id, name);
|
||||
|
@ -1723,8 +1723,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
|
||||
|
||||
/* Clear unknown autoclear feature bits */
|
||||
update_header |= s->autoclear_features & ~QCOW2_AUTOCLEAR_MASK;
|
||||
update_header =
|
||||
update_header && !bs->read_only && !(flags & BDRV_O_INACTIVE);
|
||||
update_header = update_header && bdrv_is_writable(bs);
|
||||
if (update_header) {
|
||||
s->autoclear_features &= QCOW2_AUTOCLEAR_MASK;
|
||||
}
|
||||
@ -1811,7 +1810,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
|
||||
bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
|
||||
|
||||
/* Repair image if dirty */
|
||||
if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&
|
||||
if (!(flags & BDRV_O_CHECK) && bdrv_is_writable(bs) &&
|
||||
(s->incompatible_features & QCOW2_INCOMPAT_DIRTY)) {
|
||||
BdrvCheckResult result = {0};
|
||||
|
||||
|
@ -415,7 +415,7 @@ int bdrv_snapshot_load_tmp(BlockDriverState *bs,
|
||||
error_setg(errp, "snapshot_id and name are both NULL");
|
||||
return -EINVAL;
|
||||
}
|
||||
if (!bs->read_only) {
|
||||
if (!bdrv_is_read_only(bs)) {
|
||||
error_setg(errp, "Device is not readonly");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
@ -801,7 +801,7 @@ int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed,
|
||||
}
|
||||
|
||||
if (logs.valid) {
|
||||
if (bs->read_only) {
|
||||
if (bdrv_is_read_only(bs)) {
|
||||
bdrv_refresh_filename(bs);
|
||||
ret = -EPERM;
|
||||
error_setg(errp,
|
||||
|
Loading…
Reference in New Issue
Block a user