[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 21/22] qcow: Use bdrv functions to replace file oper
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PATCH 21/22] qcow: Use bdrv functions to replace file operation |
Date: |
Thu, 26 Jan 2012 17:38:15 +0100 |
From: Li Zhi Hui <address@hidden>
Since common file operation functions lack of error detection and use
much more I/O syscalls, so change them to bdrv series functions and
reduce I/O request.
Signed-off-by: Li Zhi Hui <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
block/qcow.c | 48 +++++++++++++++++++++++++++++++-----------------
1 files changed, 31 insertions(+), 17 deletions(-)
diff --git a/block/qcow.c b/block/qcow.c
index e0d0b88..b1cfe1f 100644
--- a/block/qcow.c
+++ b/block/qcow.c
@@ -644,13 +644,14 @@ static void qcow_close(BlockDriverState *bs)
static int qcow_create(const char *filename, QEMUOptionParameter *options)
{
- int fd, header_size, backing_filename_len, l1_size, i, shift;
+ int header_size, backing_filename_len, l1_size, shift, i;
QCowHeader header;
- uint64_t tmp;
+ uint8_t *tmp;
int64_t total_size = 0;
const char *backing_file = NULL;
int flags = 0;
int ret;
+ BlockDriverState *qcow_bs;
/* Read out options */
while (options && options->name) {
@@ -664,9 +665,21 @@ static int qcow_create(const char *filename,
QEMUOptionParameter *options)
options++;
}
- fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
- if (fd < 0)
- return -errno;
+ ret = bdrv_create_file(filename, options);
+ if (ret < 0) {
+ return ret;
+ }
+
+ ret = bdrv_file_open(&qcow_bs, filename, BDRV_O_RDWR);
+ if (ret < 0) {
+ return ret;
+ }
+
+ ret = bdrv_truncate(qcow_bs, 0);
+ if (ret < 0) {
+ goto exit;
+ }
+
memset(&header, 0, sizeof(header));
header.magic = cpu_to_be32(QCOW_MAGIC);
header.version = cpu_to_be32(QCOW_VERSION);
@@ -702,33 +715,34 @@ static int qcow_create(const char *filename,
QEMUOptionParameter *options)
}
/* write all the data */
- ret = qemu_write_full(fd, &header, sizeof(header));
+ ret = bdrv_pwrite(qcow_bs, 0, &header, sizeof(header));
if (ret != sizeof(header)) {
- ret = -errno;
goto exit;
}
if (backing_file) {
- ret = qemu_write_full(fd, backing_file, backing_filename_len);
+ ret = bdrv_pwrite(qcow_bs, sizeof(header),
+ backing_file, backing_filename_len);
if (ret != backing_filename_len) {
- ret = -errno;
goto exit;
}
-
}
- lseek(fd, header_size, SEEK_SET);
- tmp = 0;
- for(i = 0;i < l1_size; i++) {
- ret = qemu_write_full(fd, &tmp, sizeof(tmp));
- if (ret != sizeof(tmp)) {
- ret = -errno;
+
+ tmp = g_malloc0(BDRV_SECTOR_SIZE);
+ for (i = 0; i < ((sizeof(uint64_t)*l1_size + BDRV_SECTOR_SIZE - 1)/
+ BDRV_SECTOR_SIZE); i++) {
+ ret = bdrv_pwrite(qcow_bs, header_size +
+ BDRV_SECTOR_SIZE*i, tmp, BDRV_SECTOR_SIZE);
+ if (ret != BDRV_SECTOR_SIZE) {
+ g_free(tmp);
goto exit;
}
}
+ g_free(tmp);
ret = 0;
exit:
- close(fd);
+ bdrv_delete(qcow_bs);
return ret;
}
--
1.7.6.5
- [Qemu-devel] [PATCH 06/22] block: add BlockJob interface for long-running operations, (continued)
- [Qemu-devel] [PATCH 06/22] block: add BlockJob interface for long-running operations, Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 12/22] qmp: add query-block-jobs, Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 07/22] block: add image streaming block job, Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 15/22] add QERR_BASE_NOT_FOUND, Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 11/22] qmp: add block_job_cancel command, Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 19/22] block/vdi: Zero unused parts when allocating a new block (fix #919242), Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 13/22] blockdev: make image streaming safe across hotplug, Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 18/22] virtio-blk: add virtio_blk_handle_read trace event, Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 17/22] docs: describe live block operations, Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 14/22] block: add bdrv_find_backing_image, Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 21/22] qcow: Use bdrv functions to replace file operation,
Kevin Wolf <=
- [Qemu-devel] [PATCH 20/22] qcow: Return real error code in qcow_open, Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 16/22] block: add support for partial streaming, Kevin Wolf, 2012/01/26
- [Qemu-devel] [PATCH 22/22] scsi: Guard against buflen exceeding req->cmd.xfer in scsi_disk_emulate_command, Kevin Wolf, 2012/01/26
- Re: [Qemu-devel] [PULL 00/22] Block patches, Anthony Liguori, 2012/01/27