diff -Nru qemu.org/block/sheepdog.c qemu/block/sheepdog.c --- qemu.org/block/sheepdog.c 2011-05-16 20:31:15.000000000 +0900 +++ qemu/block/sheepdog.c 2011-05-16 20:39:46.000000000 +0900 @@ -1292,12 +1292,65 @@ return 0; } +static int sd_prealloc(uint32_t vid, int64_t vdi_size) +{ + int fd, ret; + SheepdogInode *inode; + char *buf; + unsigned long idx, max_idx; + + fd = connect_to_sdog(NULL, NULL); + if (fd < 0) { + return -EIO; + } + + inode = qemu_malloc(sizeof(*inode)); + buf = qemu_malloc(SD_DATA_OBJ_SIZE); + + ret = read_object(fd, (char *)inode, vid_to_vdi_oid(vid), + 0, sizeof(*inode), 0); + + max_idx = (vdi_size + SD_DATA_OBJ_SIZE - 1) / SD_DATA_OBJ_SIZE; + + for (idx = 0; idx < max_idx; idx++) { + uint64_t oid; + oid = vid_to_data_oid(vid, idx); + + if (inode->data_vdi_id[idx]) { + ret = read_object(fd, buf, vid_to_vdi_oid(inode->data_vdi_id[idx]), + 1, SD_DATA_OBJ_SIZE, 0); + if (ret) + goto out; + } else { + memset(buf, 0, SD_DATA_OBJ_SIZE); + } + + ret = write_object(fd, buf, oid, 1, SD_DATA_OBJ_SIZE, 0, 1); + if (ret) + goto out; + + inode->data_vdi_id[idx] = vid; + ret = write_object(fd, (char *)inode, vid_to_vdi_oid(vid), + 1, sizeof(*inode), 0, 0); + if (ret) + goto out; + } +out: + free(inode); + free(buf); + closesocket(fd); + + return ret; +} + + static int sd_create(const char *filename, QEMUOptionParameter *options) { int ret; uint32_t vid = 0, base_vid = 0; int64_t vdi_size = 0; char *backing_file = NULL; + int prealloc = 0; BDRVSheepdogState s; char vdi[SD_MAX_VDI_LEN], tag[SD_MAX_VDI_TAG_LEN]; uint32_t snapid; @@ -1317,7 +1370,18 @@ vdi_size = options->value.n; } else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) { backing_file = options->value.s; - } + } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) { + if (!options->value.s || !strcmp(options->value.s, "off")) { + prealloc = 0; + } else if (!strcmp(options->value.s, "data")) { + prealloc = 1; + } else { + error_report("Invalid preallocation mode: '%s'\n", + options->value.s); + return -EINVAL; + } + + } options++; } @@ -1353,8 +1417,12 @@ base_vid = s->inode.vdi_id; bdrv_delete(bs); } + ret = do_sd_create((char *)filename, vdi_size, base_vid, &vid, 0, NULL, NULL); + if (!prealloc || ret) + return ret; + + return sd_prealloc(vid, vdi_size); - return do_sd_create((char *)vdi, vdi_size, base_vid, &vid, 0, s.addr, s.port); } static void sd_close(BlockDriverState *bs) @@ -1990,6 +2058,11 @@ .type = OPT_STRING, .help = "File name of a base image" }, + { + .name = BLOCK_OPT_PREALLOC, + .type = OPT_STRING, + .help = "Preallocation mode (allowed values: off, data)" + }, { NULL } };