From 4bb0afef46409583c737044437565e6886017376 Mon Sep 17 00:00:00 2001 From: code Date: Thu, 1 Sep 2011 13:01:11 +0800 Subject: [PATCH] do not use fd --- block/add-cow.c | 74 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/block/add-cow.c b/block/add-cow.c index 06c22441b5..2c12e6c52c 100644 --- a/block/add-cow.c +++ b/block/add-cow.c @@ -15,7 +15,7 @@ struct add_cow_header { char image_file[1024]; uint64_t size; uint32_t sectorsize; -}; +}add_cow_header; typedef struct BDRVAddCowState { CoMutex lock; @@ -159,14 +159,13 @@ static void add_cow_close(BlockDriverState *bs) static int add_cow_create(const char *filename, QEMUOptionParameter *options) { - int fd, cow_fd; struct add_cow_header add_cow_header; int64_t image_sectors = 0; const char *backing_filename = NULL; const char *image_filename = NULL; int ret; + BlockDriverState* bs; - /* Read out options */ while (options && options->name) { if (!strcmp(options->name, BLOCK_OPT_SIZE)) { image_sectors = options->value.n / 512; @@ -178,54 +177,44 @@ static int add_cow_create(const char *filename, QEMUOptionParameter *options) options++; } - cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, - 0644); - if (cow_fd < 0) { - return -errno; + ret = bdrv_create_file(filename, NULL); + if (ret < 0) { + return ret; } - if (!backing_filename || !image_filename) { - fprintf(stderr, " backing_file and image_file can not be empty!\n"); - ret = -EINVAL; - goto exit; + ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR); + if (ret < 0) { + return ret; } - memset(&add_cow_header, 0, sizeof(add_cow_header)); add_cow_header.magic = cpu_to_be64(ADD_COW_MAGIC); add_cow_header.version = cpu_to_be32(ADD_COW_VERSION); - /* Make sure backing_file exists*/ - if (backing_filename) { - fd = open(backing_filename, O_RDONLY | O_BINARY); - if (fd < 0) { - close(cow_fd); - goto open_fail; - } - close(fd); -open_fail: - pstrcpy(add_cow_header.backing_file, \ + pstrcpy(add_cow_header.backing_file, \ sizeof(add_cow_header.backing_file), backing_filename); - pstrcpy(add_cow_header.image_file, sizeof(add_cow_header.image_file), + pstrcpy(add_cow_header.image_file, sizeof(add_cow_header.image_file), image_filename); - } - + add_cow_header.sectorsize = cpu_to_be32(512); add_cow_header.size = cpu_to_be64(image_sectors * 512); - ret = qemu_write_full(cow_fd, &add_cow_header, sizeof(add_cow_header)); - if (ret != sizeof(add_cow_header)) { - ret = -errno; - goto exit; + + ret = bdrv_pwrite(bs, 0, &add_cow_header, sizeof(add_cow_header)); + if (ret < 0) { + return ret; } + bdrv_close(bs); - ret = ftruncate(cow_fd, sizeof(add_cow_header) + \ - ((image_sectors + 7) >> 3)); - if (ret) { - ret = -errno; - goto exit; + BlockDriver* drv = bdrv_find_format("add-cow"); + assert(drv != NULL); + ret = bdrv_open(bs, filename, BDRV_O_RDWR | BDRV_O_NO_FLUSH, drv); + if (ret < 0) { + return ret; } -exit: - close(cow_fd); + ret = bdrv_truncate(bs, ((image_sectors + 7) >> 3)); + if (ret < 0) { + return ret; + } return ret; } @@ -359,7 +348,6 @@ static int add_cow_co_readv(BlockDriverState *bs, int64_t sector_num, int ret; acb = add_cow_aio_setup(bs, sector_num, qiov, nb_sectors, 0); - qemu_co_mutex_lock(&s->lock); do { ret = add_cow_aio_read_cb(acb); @@ -401,6 +389,17 @@ static int add_cow_co_writev(BlockDriverState *bs, int64_t sector_num, return ret; } +static int bdrv_add_cow_truncate(BlockDriverState *bs, int64_t offset) +{ + int ret; + ret = bdrv_truncate(bs->file,offset + sizeof(add_cow_header)); + if (ret < 0) { + return ret; + } + + return 0; +} + static QEMUOptionParameter add_cow_create_options[] = { { .name = BLOCK_OPT_SIZE, @@ -431,6 +430,7 @@ static BlockDriver bdrv_add_cow = { .bdrv_co_readv = add_cow_co_readv, .bdrv_co_writev = add_cow_co_writev, + .bdrv_truncate = bdrv_add_cow_truncate, .create_options = add_cow_create_options, }; -- 2.11.4.GIT