From patchwork Thu May 19 08:56:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: zhangzhiming X-Patchwork-Id: 9125071 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 351139F1C3 for ; Thu, 19 May 2016 08:58:02 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id DC37220219 for ; Thu, 19 May 2016 08:58:00 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 02CF820211 for ; Thu, 19 May 2016 08:57:56 +0000 (UTC) Received: from localhost ([::1]:48973 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3JmE-0000T2-2B for patchwork-qemu-devel@patchwork.kernel.org; Thu, 19 May 2016 04:57:54 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57982) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3Jlx-0000S1-Ki for qemu-devel@nongnu.org; Thu, 19 May 2016 04:57:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b3Jlq-0000AA-Eg for qemu-devel@nongnu.org; Thu, 19 May 2016 04:57:36 -0400 Received: from mx-fe5-210.meituan.com ([103.37.138.210]:56052 helo=mx02.meituan.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b3Jlh-0008Vy-06; Thu, 19 May 2016 04:57:22 -0400 Received: from localhost (localhost [127.0.0.1]) by it-mx02.lf.sankuai.com (Postfix) with ESMTP id C04EC2977F52; Thu, 19 May 2016 16:56:49 +0800 (CST) Authentication-Results: it-mx02.lf.sankuai.com (amavisd-new); dkim=pass (1024-bit key) header.d=meituan.com Received: from mx02.meituan.com ([127.0.0.1]) by localhost (it-mx02.lf.sankuai.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id G1S8eP8znQgk; Thu, 19 May 2016 16:56:49 +0800 (CST) Received: from localhost (localhost [127.0.0.1]) by it-mx02.lf.sankuai.com (Postfix) with ESMTP id 92E542977F6D; Thu, 19 May 2016 16:56:49 +0800 (CST) DKIM-Filter: OpenDKIM Filter v2.9.2 it-mx02.lf.sankuai.com 92E542977F6D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=meituan.com; s=20130113; t=1463648209; bh=DdL+89EdtSSJGuhpsKOziDeayt8luZ1XYQaaMxpMXPg=; h=Content-Type:Mime-Version:Subject:From:Date:Message-Id:To; b=eaYYDW0XO6BxuvOplNUGM2/IsmlPxu9UaV0p3vnzV6/aZfyGK0C6me1gfNTrgfwNj qjFdDOqKhJHzivQ2At6xuPLn0G/XbX3VdLUIAVrhC/fbEB5mEB2JSxnKEVkU5T7EBx VDsZcX4sUEdE4WChFZdD2JbTZThxnkF8VsG3ocaU= X-Virus-Scanned: amavisd-new at it-mx02.lf.sankuai.com Received: from mx02.meituan.com ([127.0.0.1]) by localhost (it-mx02.lf.sankuai.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id J3ALoyHeZllT; Thu, 19 May 2016 16:56:49 +0800 (CST) Received: from [172.18.35.223] (unknown [103.37.140.18]) by it-mx02.lf.sankuai.com (Postfix) with ESMTPSA id E48A22977F52; Thu, 19 May 2016 16:56:46 +0800 (CST) Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2070.6\)) From: zhangzhiming In-Reply-To: <1E3A05EF-C0F1-48FD-A967-657D9B61C5E2@meituan.com> Date: Thu, 19 May 2016 16:56:45 +0800 Message-Id: <2521E138-9D98-43C2-8E75-56E491CBFED1@meituan.com> References: <1E3A05EF-C0F1-48FD-A967-657D9B61C5E2@meituan.com> To: qemu-devel@nongnu.org X-Mailer: Apple Mail (2.2070.6) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [generic] X-Received-From: 103.37.138.210 X-Content-Filtered-By: Mailman/MimeDel 2.1.21 Subject: Re: [Qemu-devel] [Qemu-block] [PATCH v1 1/1] qcow2 resize with snapshots X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , lihuiba , qemu-block@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP hi, missed some code. zhangzhiming zhangzhiming02@meituan.com --- -- > On May 19, 2016, at 3:46 PM, zhangzhiming wrote: > > hi, i wrote some code for 'qcow2 resize' with snapshot with v3 image and 'qcow2 goto’ too, > different size of snapshots are supported. > and i have tested the function and it seems work well. > there are some code copied from snapshot_delete_blkdev_internal, and qmp_block_resize, > it feels not very good. > > please review it for me. thanks. > > zhangzhiming > zhangzhiming02@meituan.com > > -- > > diff --git a/block.c b/block.c > index 18a497f..047698a 100644 > --- a/block.c > +++ b/block.c > @@ -2632,6 +2632,24 @@ int bdrv_truncate(BlockDriverState *bs, int64_t offset) > } > > /** > + * goto a snapshot > + */ > +int bdrv_apply_snapshot(BlockDriverState *bs, const char *snapshot_id, uint64_t snapshot_size) > +{ > + int ret = bdrv_snapshot_goto(bs, snapshot_id); > + if(ret < 0){ > + return ret; > + } > + > + ret = refresh_total_sectors(bs, snapshot_size); > + bdrv_dirty_bitmap_truncate(bs); > + if (bs->blk) { > + blk_dev_resize_cb(bs->blk); > + } > + return ret; > +} > + > +/** > * Length of a allocated file in bytes. Sparse files are counted by actual > * allocated space. Return < 0 if error or unknown. > */ > diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c > index 5f4a17e..9bc987f 100644 > --- a/block/qcow2-snapshot.c > +++ b/block/qcow2-snapshot.c > @@ -477,13 +477,6 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) > } > sn = &s->snapshots[snapshot_index]; > > - if (sn->disk_size != bs->total_sectors * BDRV_SECTOR_SIZE) { > - error_report("qcow2: Loading snapshots with different disk " > - "size is not implemented"); > - ret = -ENOTSUP; > - goto fail; > - } > - > /* > * Make sure that the current L1 table is big enough to contain the whole > * L1 table of the snapshot. If the snapshot L1 table is smaller, the > @@ -675,6 +668,7 @@ int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) > sn_info->date_sec = sn->date_sec; > sn_info->date_nsec = sn->date_nsec; > sn_info->vm_clock_nsec = sn->vm_clock_nsec; > + sn_info->disk_size = sn->disk_size; > } > *psn_tab = sn_tab; > return s->nb_snapshots; > diff --git a/block/qcow2.c b/block/qcow2.c > index 62febfc..6535f92 100644 > --- a/block/qcow2.c > +++ b/block/qcow2.c > @@ -2501,15 +2501,17 @@ static int qcow2_truncate(BlockDriverState *bs, int64_t offset) > return -EINVAL; > } > > - /* cannot proceed if image has snapshots */ > - if (s->nb_snapshots) { > - error_report("Can't resize an image which has snapshots"); > + bool v3_truncate = (s->qcow_version == 3); > + > + /* cannot proceed if image has snapshots and qcow_version is not 3*/ > + if (!v3_truncate && s->nb_snapshots) { > + error_report("Can't resize an image which has snapshots and qcow_version is not 3"); > return -ENOTSUP; > } > > - /* shrinking is currently not supported */ > - if (offset < bs->total_sectors * 512) { > - error_report("qcow2 doesn't support shrinking images yet"); > + /* shrinking is supported from version 3*/ > + if (!v3_truncate && offset < bs->total_sectors * 512) { > + error_report("qcow2 doesn't support shrinking images yet while qcow_version is not 3"); > return -ENOTSUP; > } > > diff --git a/block/sheepdog.c b/block/sheepdog.c > index 23fbace..bc12f7b 100644 > --- a/block/sheepdog.c > +++ b/block/sheepdog.c > @@ -2693,6 +2693,7 @@ static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab) > sn_tab[found].date_nsec = inode.snap_ctime & 0xffffffff; > sn_tab[found].vm_state_size = inode.vm_state_size; > sn_tab[found].vm_clock_nsec = inode.vm_clock_nsec; > + sn_tab[found].disk_size = inode.vdi_size; > > snprintf(sn_tab[found].id_str, sizeof(sn_tab[found].id_str), > "%" PRIu32, inode.snap_id); > diff --git a/blockdev.c b/blockdev.c > index 1892b8e..36c66c1 100644 > --- a/blockdev.c > +++ b/blockdev.c > @@ -2961,6 +2961,120 @@ out: > aio_context_release(aio_context); > } > > +SnapshotInfo *qmp_blockdev_snapshot_goto_internal_sync(const char *device, > + bool has_id, > + const char *id, > + bool has_name, > + const char *name, > + Error **errp) > +{ > + BlockDriverState *bs; > + BlockBackend *blk; > + AioContext *aio_context; > + QEMUSnapshotInfo sn; > + Error *local_err = NULL; > + SnapshotInfo *info = NULL; > + int ret; > + > + blk = blk_by_name(device); > + if (!blk) { > + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, > + "Device '%s' not found", device); > + return NULL; > + } > + aio_context = blk_get_aio_context(blk); > + aio_context_acquire(aio_context); > + > + if(!has_id){ > + id = NULL; > + } > + > + if(!has_name){ > + name = NULL; > + } > + > + if(!id && !name){ > + error_setg(errp, "Name or id must be provided"); > + goto out_aio_context; > + } > + > + if(!blk_is_available(blk)){ > + error_setg(errp, "Device '%s' has no medium", device); > + goto out_aio_context; > + } > + > + bs = blk_bs(blk); > + > + if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_APPLY, errp)){ > + goto out_aio_context; > + } > + > + ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); > + if(local_err){ > + error_propagate(errp, local_err); > + goto out_aio_context; > + } > + if(!ret){ > + error_setg(errp, > + "Snapshot with id '%s' and name '%s' does not exist on " > + "device '%s'", > + STR_OR_NULL(id), STR_OR_NULL(name), device); > + goto out_aio_context; > + } > + if(!sn.disk_size){ > + error_setg(errp, > + "Snapshot with id '%s' and name '%s' does not has a disk size " > + "device '%s'", > + STR_OR_NULL(id), STR_OR_NULL(name), device); > + goto out_aio_context; > + } > + > + /* complete all in-flight operations before resizing the device */ > + bdrv_drain_all(); > + > + ret = bdrv_apply_snapshot(bs, sn.id_str, sn.disk_size); > + switch (ret) { > + case 0: > + break; > + case -ENOMEDIUM: > + error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); > + break; > + case -ENOTSUP: > + error_setg(errp, QERR_UNSUPPORTED); > + break; > + case -EACCES: > + error_setg(errp, "Device '%s' is read only", device); > + break; > + case -EBUSY: > + error_setg(errp, QERR_DEVICE_IN_USE, device); > + break; > + default: > + error_setg_errno(errp, -ret, "Could not resize"); > + break; > + } > + > + if(ret < 0){ > + goto out_aio_context; > + } > + > + aio_context_release(aio_context); > + > + info = g_new0(SnapshotInfo, 1); > + info->id = g_strdup(sn.id_str); > + info->name = g_strdup(sn.name); > + info->date_nsec = sn.date_nsec; > + info->date_sec = sn.date_sec; > + info->vm_state_size = sn.vm_state_size; > + info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; > + info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; > + > + return info; > + > +out_aio_context: > + aio_context_release(aio_context); > + return NULL; > +} > + > static void block_job_cb(void *opaque, int ret) > { > /* Note that this function may be executed from another AioContext besides > diff --git a/hmp-commands.hx b/hmp-commands.hx > index 4f4f60a..5848a57 100644 > --- a/hmp-commands.hx > +++ b/hmp-commands.hx > @@ -1159,6 +1159,24 @@ Delete an internal snapshot on device if it support > ETEXI > > { > + .name = "snapshot_goto_blkdev_internal", > + .args_type = "device:B,name:s,id:s?", > + .params = "device name [id]", > + .help = "apply an internal snapshot of device.\n\t\t\t" > + "If id is specified, qemu will try apply\n\t\t\t" > + "the snapshot matching both id and name.\n\t\t\t" > + "The format of the image used by device must\n\t\t\t" > + "support it, such as qcow2.\n\t\t\t", > + .mhandler.cmd = hmp_snapshot_goto_blkdev_internal, > + }, > + > +STEXI > +@item snapshot_goto_blkdev_internal > +@findex snapshot_goto_blkdev_internal > +Apply an internal snapshot on device if it support > +ETEXI > + > + { > .name = "drive_mirror", > .args_type = "reuse:-n,full:-f,device:B,target:s,format:s?", > .params = "[-n] [-f] device target [format]", > diff --git a/hmp.c b/hmp.c > index d510236..3f1d146 100644 > --- a/hmp.c > +++ b/hmp.c > @@ -1057,6 +1057,8 @@ void hmp_block_resize(Monitor *mon, const QDict *qdict) > hmp_handle_error(mon, &err); > } > > + > + > void hmp_drive_mirror(Monitor *mon, const QDict *qdict) > { > const char *device = qdict_get_str(qdict, "device"); > @@ -1163,6 +1165,17 @@ void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict) > hmp_handle_error(mon, &err); > } > > +void hmp_snapshot_goto_blkdev_internal(Monitor *mon, const QDict *qdict) > +{ > + const char *device = qdict_get_str(qdict, "device"); > + const char *name = qdict_get_str(qdict, "name"); > + const char *id = qdict_get_try_str(qdict, "id"); > + Error *err = NULL; > + > + qmp_blockdev_snapshot_goto_internal_sync(device, !!id, id, true, name, &err); > + hmp_handle_error(mon, &err); > +} > + > void hmp_migrate_cancel(Monitor *mon, const QDict *qdict) > { > qmp_migrate_cancel(NULL); > diff --git a/hmp.h b/hmp.h > index 093d65f..b1ad2f5 100644 > --- a/hmp.h > +++ b/hmp.h > @@ -59,6 +59,7 @@ void hmp_block_resize(Monitor *mon, const QDict *qdict); > void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict); > void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict); > void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict); > +void hmp_snapshot_goto_blkdev_internal(Monitor *mon, const QDict *qdict); > void hmp_drive_mirror(Monitor *mon, const QDict *qdict); > void hmp_drive_backup(Monitor *mon, const QDict *qdict); > void hmp_migrate_cancel(Monitor *mon, const QDict *qdict); > diff --git a/include/block/block.h b/include/block/block.h > index b210832..393ca6b 100644 > --- a/include/block/block.h > +++ b/include/block/block.h > @@ -173,6 +173,7 @@ typedef enum BlockOpType { > BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, > BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, > BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, > + BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_APPLY, > BLOCK_OP_TYPE_MIRROR_SOURCE, > BLOCK_OP_TYPE_MIRROR_TARGET, > BLOCK_OP_TYPE_RESIZE, > @@ -266,6 +267,7 @@ BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, > int bdrv_get_backing_file_depth(BlockDriverState *bs); > void bdrv_refresh_filename(BlockDriverState *bs); > int bdrv_truncate(BlockDriverState *bs, int64_t offset); > +int bdrv_apply_snapshot(BlockDriverState *bs, const char *snapshot_id, uint64_t snapshot_size); > int64_t bdrv_nb_sectors(BlockDriverState *bs); > int64_t bdrv_getlength(BlockDriverState *bs); > int64_t bdrv_get_allocated_file_size(BlockDriverState *bs); > diff --git a/include/block/snapshot.h b/include/block/snapshot.h > index e5c0553..7279c12 100644 > --- a/include/block/snapshot.h > +++ b/include/block/snapshot.h > @@ -44,6 +44,7 @@ typedef struct QEMUSnapshotInfo { > uint32_t date_sec; /* UTC date of the snapshot */ > uint32_t date_nsec; > uint64_t vm_clock_nsec; /* VM clock relative to boot */ > + uint64_t disk_size; > } QEMUSnapshotInfo; > > int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, > diff --git a/pixman b/pixman > index 87eea99..7c6066b 160000 > --- a/pixman > +++ b/pixman > @@ -1 +1 @@ > -Subproject commit 87eea99e443b389c978cf37efc52788bf03a0ee0 > +Subproject commit 7c6066b700c7cdd4aeb8be426b14b3a5f0de4b6c > > -- > > diff --git a/block.c b/block.c index 047698a..ff83134 100644 --- a/block.c +++ b/block.c @@ -2641,7 +2641,7 @@ int bdrv_apply_snapshot(BlockDriverState *bs, const char *snapshot_id, uint64_t return ret; } - ret = refresh_total_sectors(bs, snapshot_size); + ret = refresh_total_sectors(bs, snapshot_size >> BDRV_SECTOR_BITS); bdrv_dirty_bitmap_truncate(bs); if (bs->blk) { blk_dev_resize_cb(bs->blk); diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 9bc987f..d872cf2 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -528,6 +528,14 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id) goto fail; } + /* write updated header.size */ + uint64_t be_disk_size = cpu_to_be64(sn->disk_size); + ret = bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, size), + &be_disk_size, sizeof(uint64_t)); + if (ret < 0) { + goto fail; + } + /* * Decrease refcount of clusters of current L1 table. *