Clean-up a little bit the RW related bits of BDRV_O_FLAGS. BDRV_O_RDONLY gone (and so is BDRV_O_ACCESS). Default value for bdrv_flags (0/zero) is READ-ONLY. Need to explicitly request READ-WRITE.
Instead of using the field 'readonly' of the BlockDriverState struct for passing the request, pass the request in the flags parameter to the function. Signed-off-by: Naphtali Sprei <nsprei@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
parent
b196b1532f
commit
f5edb014ed
31
block.c
31
block.c
@ -310,7 +310,7 @@ static BlockDriver *find_image_format(const char *filename)
|
|||||||
if (drv && strcmp(drv->format_name, "vvfat") == 0)
|
if (drv && strcmp(drv->format_name, "vvfat") == 0)
|
||||||
return drv;
|
return drv;
|
||||||
|
|
||||||
ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
|
ret = bdrv_file_open(&bs, filename, 0);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
ret = bdrv_pread(bs, 0, buf, sizeof(buf));
|
ret = bdrv_pread(bs, 0, buf, sizeof(buf));
|
||||||
@ -356,7 +356,7 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
|
|||||||
int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
|
int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
|
||||||
BlockDriver *drv)
|
BlockDriver *drv)
|
||||||
{
|
{
|
||||||
int ret, open_flags, try_rw;
|
int ret, open_flags;
|
||||||
char tmp_filename[PATH_MAX];
|
char tmp_filename[PATH_MAX];
|
||||||
char backing_filename[PATH_MAX];
|
char backing_filename[PATH_MAX];
|
||||||
|
|
||||||
@ -446,19 +446,23 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
|
|||||||
|
|
||||||
/* Note: for compatibility, we open disk image files as RDWR, and
|
/* Note: for compatibility, we open disk image files as RDWR, and
|
||||||
RDONLY as fallback */
|
RDONLY as fallback */
|
||||||
try_rw = !bs->read_only || bs->is_temporary;
|
bs->read_only = (flags & BDRV_O_RDWR) == 0;
|
||||||
if (!(flags & BDRV_O_FILE))
|
if (!(flags & BDRV_O_FILE)) {
|
||||||
open_flags = (try_rw ? BDRV_O_RDWR : 0) |
|
open_flags = (flags & (BDRV_O_RDWR | BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO));
|
||||||
(flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO));
|
if (bs->is_temporary) { /* snapshot should be writeable */
|
||||||
else
|
open_flags |= BDRV_O_RDWR;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
|
open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
|
||||||
if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv))
|
}
|
||||||
|
if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
|
||||||
ret = -ENOTSUP;
|
ret = -ENOTSUP;
|
||||||
else
|
} else {
|
||||||
ret = drv->bdrv_open(bs, filename, open_flags);
|
ret = drv->bdrv_open(bs, filename, open_flags);
|
||||||
if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
|
if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
|
||||||
ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
|
ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
|
||||||
bs->read_only = 1;
|
bs->read_only = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
qemu_free(bs->opaque);
|
qemu_free(bs->opaque);
|
||||||
@ -481,14 +485,13 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
|
|||||||
/* if there is a backing file, use it */
|
/* if there is a backing file, use it */
|
||||||
BlockDriver *back_drv = NULL;
|
BlockDriver *back_drv = NULL;
|
||||||
bs->backing_hd = bdrv_new("");
|
bs->backing_hd = bdrv_new("");
|
||||||
/* pass on read_only property to the backing_hd */
|
|
||||||
bs->backing_hd->read_only = bs->read_only;
|
|
||||||
path_combine(backing_filename, sizeof(backing_filename),
|
path_combine(backing_filename, sizeof(backing_filename),
|
||||||
filename, bs->backing_file);
|
filename, bs->backing_file);
|
||||||
if (bs->backing_format[0] != '\0')
|
if (bs->backing_format[0] != '\0')
|
||||||
back_drv = bdrv_find_format(bs->backing_format);
|
back_drv = bdrv_find_format(bs->backing_format);
|
||||||
ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
|
ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
|
||||||
back_drv);
|
back_drv);
|
||||||
|
bs->backing_hd->read_only = (open_flags & BDRV_O_RDWR) == 0;
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
bdrv_close(bs);
|
bdrv_close(bs);
|
||||||
return ret;
|
return ret;
|
||||||
|
2
block.h
2
block.h
@ -27,9 +27,7 @@ typedef struct QEMUSnapshotInfo {
|
|||||||
uint64_t vm_clock_nsec; /* VM clock relative to boot */
|
uint64_t vm_clock_nsec; /* VM clock relative to boot */
|
||||||
} QEMUSnapshotInfo;
|
} QEMUSnapshotInfo;
|
||||||
|
|
||||||
#define BDRV_O_RDONLY 0x0000
|
|
||||||
#define BDRV_O_RDWR 0x0002
|
#define BDRV_O_RDWR 0x0002
|
||||||
#define BDRV_O_ACCESS 0x0003
|
|
||||||
#define BDRV_O_CREAT 0x0004 /* create an empty file */
|
#define BDRV_O_CREAT 0x0004 /* create an empty file */
|
||||||
#define BDRV_O_SNAPSHOT 0x0008 /* open the file read only and save writes in a snapshot */
|
#define BDRV_O_SNAPSHOT 0x0008 /* open the file read only and save writes in a snapshot */
|
||||||
#define BDRV_O_FILE 0x0010 /* open as a raw file (do not try to
|
#define BDRV_O_FILE 0x0010 /* open as a raw file (do not try to
|
||||||
|
@ -138,7 +138,7 @@ static int raw_open_common(BlockDriverState *bs, const char *filename,
|
|||||||
|
|
||||||
s->open_flags = open_flags | O_BINARY;
|
s->open_flags = open_flags | O_BINARY;
|
||||||
s->open_flags &= ~O_ACCMODE;
|
s->open_flags &= ~O_ACCMODE;
|
||||||
if ((bdrv_flags & BDRV_O_ACCESS) == BDRV_O_RDWR) {
|
if (bdrv_flags & BDRV_O_RDWR) {
|
||||||
s->open_flags |= O_RDWR;
|
s->open_flags |= O_RDWR;
|
||||||
} else {
|
} else {
|
||||||
s->open_flags |= O_RDONLY;
|
s->open_flags |= O_RDONLY;
|
||||||
|
@ -81,7 +81,7 @@ static int raw_open(BlockDriverState *bs, const char *filename, int flags)
|
|||||||
|
|
||||||
s->type = FTYPE_FILE;
|
s->type = FTYPE_FILE;
|
||||||
|
|
||||||
if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
if (flags & BDRV_O_RDWR) {
|
||||||
access_flags = GENERIC_READ | GENERIC_WRITE;
|
access_flags = GENERIC_READ | GENERIC_WRITE;
|
||||||
} else {
|
} else {
|
||||||
access_flags = GENERIC_READ;
|
access_flags = GENERIC_READ;
|
||||||
@ -337,7 +337,7 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
|
|||||||
}
|
}
|
||||||
s->type = find_device_type(bs, filename);
|
s->type = find_device_type(bs, filename);
|
||||||
|
|
||||||
if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
if (flags & BDRV_O_RDWR) {
|
||||||
access_flags = GENERIC_READ | GENERIC_WRITE;
|
access_flags = GENERIC_READ | GENERIC_WRITE;
|
||||||
} else {
|
} else {
|
||||||
access_flags = GENERIC_READ;
|
access_flags = GENERIC_READ;
|
||||||
|
@ -357,7 +357,7 @@ static int vmdk_parent_open(BlockDriverState *bs, const char * filename)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
parent_open = 1;
|
parent_open = 1;
|
||||||
if (bdrv_open(bs->backing_hd, parent_img_name, BDRV_O_RDONLY) < 0)
|
if (bdrv_open(bs->backing_hd, parent_img_name, 0) < 0)
|
||||||
goto failure;
|
goto failure;
|
||||||
parent_open = 0;
|
parent_open = 0;
|
||||||
}
|
}
|
||||||
@ -371,9 +371,10 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
|
|||||||
uint32_t magic;
|
uint32_t magic;
|
||||||
int l1_size, i, ret;
|
int l1_size, i, ret;
|
||||||
|
|
||||||
if (parent_open)
|
if (parent_open) {
|
||||||
// Parent must be opened as RO.
|
/* Parent must be opened as RO, no RDWR. */
|
||||||
flags = BDRV_O_RDONLY;
|
flags = 0;
|
||||||
|
}
|
||||||
|
|
||||||
ret = bdrv_file_open(&s->hd, filename, flags);
|
ret = bdrv_file_open(&s->hd, filename, flags);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
|
@ -613,7 +613,7 @@ static int blk_init(struct XenDevice *xendev)
|
|||||||
qflags = BDRV_O_RDWR;
|
qflags = BDRV_O_RDWR;
|
||||||
} else {
|
} else {
|
||||||
mode = O_RDONLY;
|
mode = O_RDONLY;
|
||||||
qflags = BDRV_O_RDONLY;
|
qflags = 0;
|
||||||
info |= VDISK_READONLY;
|
info |= VDISK_READONLY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -927,7 +927,7 @@ static void do_change_block(Monitor *mon, const char *device,
|
|||||||
}
|
}
|
||||||
if (eject_device(mon, bs, 0) < 0)
|
if (eject_device(mon, bs, 0) < 0)
|
||||||
return;
|
return;
|
||||||
bdrv_open2(bs, filename, 0, drv);
|
bdrv_open2(bs, filename, BDRV_O_RDWR, drv);
|
||||||
monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
|
monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
qemu-img.c
10
qemu-img.c
@ -204,7 +204,7 @@ static BlockDriverState *bdrv_new_open(const char *filename,
|
|||||||
} else {
|
} else {
|
||||||
drv = NULL;
|
drv = NULL;
|
||||||
}
|
}
|
||||||
if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
|
if (bdrv_open2(bs, filename, BRDV_O_FLAGS | BDRV_O_RDWR, drv) < 0) {
|
||||||
error("Could not open '%s'", filename);
|
error("Could not open '%s'", filename);
|
||||||
}
|
}
|
||||||
if (bdrv_is_encrypted(bs)) {
|
if (bdrv_is_encrypted(bs)) {
|
||||||
@ -468,7 +468,7 @@ static int img_commit(int argc, char **argv)
|
|||||||
} else {
|
} else {
|
||||||
drv = NULL;
|
drv = NULL;
|
||||||
}
|
}
|
||||||
if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
|
if (bdrv_open2(bs, filename, BRDV_O_FLAGS | BDRV_O_RDWR, drv) < 0) {
|
||||||
error("Could not open '%s'", filename);
|
error("Could not open '%s'", filename);
|
||||||
}
|
}
|
||||||
ret = bdrv_commit(bs);
|
ret = bdrv_commit(bs);
|
||||||
@ -966,10 +966,11 @@ static int img_snapshot(int argc, char **argv)
|
|||||||
BlockDriverState *bs;
|
BlockDriverState *bs;
|
||||||
QEMUSnapshotInfo sn;
|
QEMUSnapshotInfo sn;
|
||||||
char *filename, *snapshot_name = NULL;
|
char *filename, *snapshot_name = NULL;
|
||||||
int c, ret;
|
int c, ret, bdrv_oflags;
|
||||||
int action = 0;
|
int action = 0;
|
||||||
qemu_timeval tv;
|
qemu_timeval tv;
|
||||||
|
|
||||||
|
bdrv_oflags = BDRV_O_RDWR;
|
||||||
/* Parse commandline parameters */
|
/* Parse commandline parameters */
|
||||||
for(;;) {
|
for(;;) {
|
||||||
c = getopt(argc, argv, "la:c:d:h");
|
c = getopt(argc, argv, "la:c:d:h");
|
||||||
@ -985,6 +986,7 @@ static int img_snapshot(int argc, char **argv)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
action = SNAPSHOT_LIST;
|
action = SNAPSHOT_LIST;
|
||||||
|
bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
|
||||||
break;
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
if (action) {
|
if (action) {
|
||||||
@ -1022,7 +1024,7 @@ static int img_snapshot(int argc, char **argv)
|
|||||||
if (!bs)
|
if (!bs)
|
||||||
error("Not enough memory");
|
error("Not enough memory");
|
||||||
|
|
||||||
if (bdrv_open2(bs, filename, 0, NULL) < 0) {
|
if (bdrv_open2(bs, filename, bdrv_oflags, NULL) < 0) {
|
||||||
error("Could not open '%s'", filename);
|
error("Could not open '%s'", filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
qemu-io.c
14
qemu-io.c
@ -1359,10 +1359,9 @@ open_f(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (readonly)
|
if (!readonly) {
|
||||||
flags |= BDRV_O_RDONLY;
|
flags |= BDRV_O_RDWR;
|
||||||
else
|
}
|
||||||
flags |= BDRV_O_RDWR;
|
|
||||||
|
|
||||||
if (optind != argc - 1)
|
if (optind != argc - 1)
|
||||||
return command_usage(&open_cmd);
|
return command_usage(&open_cmd);
|
||||||
@ -1506,10 +1505,9 @@ int main(int argc, char **argv)
|
|||||||
add_check_command(init_check_command);
|
add_check_command(init_check_command);
|
||||||
|
|
||||||
/* open the device */
|
/* open the device */
|
||||||
if (readonly)
|
if (!readonly) {
|
||||||
flags |= BDRV_O_RDONLY;
|
flags |= BDRV_O_RDWR;
|
||||||
else
|
}
|
||||||
flags |= BDRV_O_RDWR;
|
|
||||||
|
|
||||||
if ((argc - optind) == 1)
|
if ((argc - optind) == 1)
|
||||||
openfile(argv[optind], flags, growable);
|
openfile(argv[optind], flags, growable);
|
||||||
|
@ -213,7 +213,7 @@ int main(int argc, char **argv)
|
|||||||
int opt_ind = 0;
|
int opt_ind = 0;
|
||||||
int li;
|
int li;
|
||||||
char *end;
|
char *end;
|
||||||
int flags = 0;
|
int flags = BDRV_O_RDWR;
|
||||||
int partition = -1;
|
int partition = -1;
|
||||||
int ret;
|
int ret;
|
||||||
int shared = 1;
|
int shared = 1;
|
||||||
|
8
vl.c
8
vl.c
@ -2227,19 +2227,19 @@ DriveInfo *drive_init(QemuOpts *opts, void *opaque,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ro == 1) {
|
if (ro == 1) {
|
||||||
if (type == IF_IDE) {
|
if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY) {
|
||||||
fprintf(stderr, "qemu: readonly flag not supported for drive with ide interface\n");
|
fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
(void)bdrv_set_read_only(dinfo->bdrv, 1);
|
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* cdrom is read-only. Set it now, after above interface checking
|
* cdrom is read-only. Set it now, after above interface checking
|
||||||
* since readonly attribute not explicitly required, so no error.
|
* since readonly attribute not explicitly required, so no error.
|
||||||
*/
|
*/
|
||||||
if (media == MEDIA_CDROM) {
|
if (media == MEDIA_CDROM) {
|
||||||
(void)bdrv_set_read_only(dinfo->bdrv, 1);
|
ro = 1;
|
||||||
}
|
}
|
||||||
|
bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
|
||||||
|
|
||||||
if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
|
if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
|
||||||
fprintf(stderr, "qemu: could not open disk image %s: %s\n",
|
fprintf(stderr, "qemu: could not open disk image %s: %s\n",
|
||||||
|
Loading…
Reference in New Issue
Block a user