From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:32860) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bG5jH-0000kq-2Y for qemu-devel@nongnu.org; Thu, 23 Jun 2016 10:35:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bG5j7-000242-Hd for qemu-devel@nongnu.org; Thu, 23 Jun 2016 10:35:37 -0400 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2070.6\)) From: zhangzhiming In-Reply-To: <9D2BE2B0-2397-402F-BD0E-C8C1B42FF22D@meituan.com> Date: Thu, 23 Jun 2016 22:34:48 +0800 Message-Id: <7BF5F4CB-12CB-4E4B-A08E-2407C47792D0@meituan.com> References: <8E87729A-E996-4D12-B940-87B77C1D22EC@meituan.com> <574DC0CB.7020707@redhat.com> <467DE7EB-A927-4100-96FB-736444263B06@meituan.com> <6DA33516-D7FB-44C2-A4B8-01974A3E3B77@meituan.com> <9D2BE2B0-2397-402F-BD0E-C8C1B42FF22D@meituan.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [Qemu-block] [PATCH] qcow2 resize with snapshot List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-block@nongnu.org Cc: Kevin Wolf , lihuiba , qemu-devel@nongnu.org, Max Reitz , eblake@redhat.com hi, please help to review my code. i have checked it for serval times. =20= it is short and very easy too read. and i will be very grateful to you = for taking a very little time to read it. thanks very much! zhangzhiming zhangzhiming02@meituan.com Signed-off-by: zhangzhiming > --- block.c | 19 +++++++++++++++++++ block/qcow2-cluster.c | 29 +++++++++++++++++++++++++++++ block/qcow2-snapshot.c | 37 +++++++++++++++++++++++++------------ block/qcow2.c | 29 ++++++++++++++++++++--------- block/qcow2.h | 1 + include/block/snapshot.h | 1 + 6 files changed, 95 insertions(+), 21 deletions(-) diff --git a/block.c b/block.c index f54bc25..0de7b2d 100644 --- a/block.c +++ b/block.c @@ -2586,6 +2586,25 @@ int bdrv_truncate(BlockDriverState *bs, int64_t = offset) return ret; } =20 +int bdrv_apply_snapshot(BlockDriverState *bs, const char *snapshot_id, + uint64_t snapshot_size) +{ + int ret =3D bdrv_snapshot_goto(bs, snapshot_id); + if (ret < 0) { + return ret; + } + + ret =3D refresh_total_sectors(bs, snapshot_size >> = BDRV_SECTOR_BITS); + if (ret < 0) { + return ret; + } + 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-cluster.c b/block/qcow2-cluster.c index b04bfaf..ebadddf 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -32,6 +32,35 @@ #include "qemu/bswap.h" #include "trace.h" =20 +int shrink_l1_table(BlockDriverState *bs, int64_t new_l1_size) +{ + BDRVQcow2State *s =3D bs->opaque; + int64_t old_l1_size =3D s->l1_size; + s->l1_size =3D new_l1_size; + int ret =3D qcow2_update_snapshot_refcount(bs, s->l1_table_offset, + s->l1_size, 1); + if (ret < 0) { + return ret; + } + + s->l1_size =3D old_l1_size; + ret =3D qcow2_update_snapshot_refcount(bs, s->l1_table_offset, + s->l1_size, -1); + if (ret < 0) { + return ret; + } + s->l1_size =3D new_l1_size; + + uint32_t be_l1_size =3D cpu_to_be32(s->l1_size); + ret =3D bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, = l1_size), + &be_l1_size, sizeof(be_l1_size)); + if (ret < 0) { + return ret; + } + + return 0; +} + int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, bool exact_size) { diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c index 242fb21..6dd6769 100644 --- a/block/qcow2-snapshot.c +++ b/block/qcow2-snapshot.c @@ -478,13 +478,6 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const = char *snapshot_id) } sn =3D &s->snapshots[snapshot_index]; =20 - if (sn->disk_size !=3D bs->total_sectors * BDRV_SECTOR_SIZE) { - error_report("qcow2: Loading snapshots with different disk " - "size is not implemented"); - ret =3D -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 @@ -506,8 +499,8 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const = char *snapshot_id) * Decrease the refcount referenced by the old one only when the L1 * table is overwritten. */ - sn_l1_table =3D g_try_malloc0(cur_l1_bytes); - if (cur_l1_bytes && sn_l1_table =3D=3D NULL) { + sn_l1_table =3D g_try_malloc0(sn_l1_bytes); + if (sn_l1_bytes && sn_l1_table =3D=3D NULL) { ret =3D -ENOMEM; goto fail; } @@ -525,17 +518,34 @@ int qcow2_snapshot_goto(BlockDriverState *bs, = const char *snapshot_id) } =20 ret =3D qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L1, - s->l1_table_offset, = cur_l1_bytes); + s->l1_table_offset, = sn_l1_bytes); if (ret < 0) { goto fail; } =20 ret =3D bdrv_pwrite_sync(bs->file->bs, s->l1_table_offset, = sn_l1_table, - cur_l1_bytes); + sn_l1_bytes); + if (ret < 0) { + goto fail; + } + + /* write updated header.size */ + uint64_t be_disk_size =3D cpu_to_be64(sn->disk_size); + ret =3D bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, size), + &be_disk_size, sizeof(uint64_t)); if (ret < 0) { goto fail; } =20 + uint32_t be_l1_size =3D cpu_to_be32(sn->l1_size); + ret =3D bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, = l1_size), + &be_l1_size, sizeof(be_l1_size)); + if (ret < 0) { + goto fail; + } + + s->l1_vm_state_index =3D sn->l1_size; + /* * Decrease refcount of clusters of current L1 table. * @@ -553,7 +563,8 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const = char *snapshot_id) * Now update the in-memory L1 table to be in sync with the on-disk = one. We * need to do this even if updating refcounts failed. */ - for(i =3D 0;i < s->l1_size; i++) { + memset(s->l1_table, 0, s->l1_size * sizeof(uint64_t)); + for (i =3D 0; i < sn->l1_size; i++) { s->l1_table[i] =3D be64_to_cpu(sn_l1_table[i]); } =20 @@ -564,6 +575,7 @@ int qcow2_snapshot_goto(BlockDriverState *bs, const = char *snapshot_id) g_free(sn_l1_table); sn_l1_table =3D NULL; =20 + s->l1_size =3D sn->l1_size; /* * Update QCOW_OFLAG_COPIED in the active L1 table (it may have = changed * when we decreased the refcount of the old snapshot. @@ -676,6 +688,7 @@ int qcow2_snapshot_list(BlockDriverState *bs, = QEMUSnapshotInfo **psn_tab) sn_info->date_sec =3D sn->date_sec; sn_info->date_nsec =3D sn->date_nsec; sn_info->vm_clock_nsec =3D sn->vm_clock_nsec; + sn_info->disk_size =3D sn->disk_size; } *psn_tab =3D sn_tab; return s->nb_snapshots; diff --git a/block/qcow2.c b/block/qcow2.c index 6f5fb81..612a534 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -2495,22 +2495,33 @@ static int qcow2_truncate(BlockDriverState *bs, = int64_t offset) return -EINVAL; } =20 - /* cannot proceed if image has snapshots */ - if (s->nb_snapshots) { - error_report("Can't resize an image which has snapshots"); + bool v3_truncate =3D (s->qcow_version =3D=3D 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; } =20 - /* 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; } =20 new_l1_size =3D size_to_l1(s, offset); - ret =3D qcow2_grow_l1_table(bs, new_l1_size, true); - if (ret < 0) { - return ret; + if (offset < bs->total_sectors * 512) { + ret =3D shrink_l1_table(bs, new_l1_size); + if (ret < 0) { + return ret; + } + } else { + ret =3D qcow2_grow_l1_table(bs, new_l1_size, true); + if (ret < 0) { + return ret; + } } =20 /* write updated header.size */ diff --git a/block/qcow2.h b/block/qcow2.h index 7db9795..8efa735 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -534,6 +534,7 @@ int qcow2_change_refcount_order(BlockDriverState = *bs, int refcount_order, void *cb_opaque, Error **errp); =20 /* qcow2-cluster.c functions */ +int shrink_l1_table(BlockDriverState *bs, int64_t new_l1_size); int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, bool exact_size); int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index); 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; =20 int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, --=20 1.7.1 > On Jun 7, 2016, at 4:25 PM, zhangzhiming = wrote: >=20 > hi, need for someone's review of my code. no one does it for nearly = one week. > thanks!=20 >=20 > zhangzhiming > zhangzhiming02@meituan.com >=20 >=20 >=20 >> On Jun 1, 2016, at 6:24 PM, zhangzhiming > wrote: >>=20 >> hi, here are all changes of my code. thanks for the review! =20 >>=20 >> zhangzhiming >> zhangzhiming02@meituan.com >>=20 >> Signed-off-by: zhangzhiming > >> --- >> block.c | 19 +++++++++++++++++++ >> block/qcow2-cluster.c | 29 +++++++++++++++++++++++++++++ >> block/qcow2-snapshot.c | 33 +++++++++++++++++++++++---------- >> block/qcow2.c | 29 ++++++++++++++++++++--------- >> block/qcow2.h | 1 + >> include/block/snapshot.h | 1 + >> 6 files changed, 93 insertions(+), 19 deletions(-) >>=20 >> diff --git a/block.c b/block.c >> index 18a497f..b6f2004 100644 >> --- a/block.c >> +++ b/block.c >> @@ -2631,6 +2631,25 @@ int bdrv_truncate(BlockDriverState *bs, = int64_t offset) >> return ret; >> } >> =20 >> +int bdrv_apply_snapshot(BlockDriverState *bs, const char = *snapshot_id, >> + uint64_t snapshot_size) >> +{ >> + int ret =3D bdrv_snapshot_goto(bs, snapshot_id); >> + if (ret < 0) { >> + return ret; >> + } >> + >> + ret =3D refresh_total_sectors(bs, snapshot_size >> = BDRV_SECTOR_BITS); >> + if (ret < 0) { >> + return ret; >> + } >> + 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-cluster.c b/block/qcow2-cluster.c >> index 31ecc10..f921fd8 100644 >> --- a/block/qcow2-cluster.c >> +++ b/block/qcow2-cluster.c >> @@ -31,6 +31,35 @@ >> #include "block/qcow2.h" >> #include "trace.h" >> =20 >> +int shrink_l1_table(BlockDriverState *bs, int64_t new_l1_size) >> +{ >> + BDRVQcow2State *s =3D bs->opaque; >> + int64_t old_l1_size =3D s->l1_size; >> + s->l1_size =3D new_l1_size; >> + int ret =3D qcow2_update_snapshot_refcount(bs, = s->l1_table_offset, >> + s->l1_size, 1); >> + if (ret < 0) { >> + return ret; >> + } >> + >> + s->l1_size =3D old_l1_size; >> + ret =3D qcow2_update_snapshot_refcount(bs, s->l1_table_offset, >> + s->l1_size, -1); >> + if (ret < 0) { >> + return ret; >> + } >> + s->l1_size =3D new_l1_size; >> + >> + uint32_t be_l1_size =3D cpu_to_be32(s->l1_size); >> + ret =3D bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, = l1_size), >> + &be_l1_size, sizeof(be_l1_size)); >> + if (ret < 0) { >> + return ret; >> + } >> + >> + return 0; >> +} >> + >> int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, >> bool exact_size) >> { >> diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c >> index 5f4a17e..1ed0e18 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 =3D &s->snapshots[snapshot_index]; >> =20 >> - if (sn->disk_size !=3D bs->total_sectors * BDRV_SECTOR_SIZE) { >> - error_report("qcow2: Loading snapshots with different disk " >> - "size is not implemented"); >> - ret =3D -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 >> @@ -505,7 +498,7 @@ int qcow2_snapshot_goto(BlockDriverState *bs, = const char *snapshot_id) >> * Decrease the refcount referenced by the old one only when the = L1 >> * table is overwritten. >> */ >> - sn_l1_table =3D g_try_malloc0(cur_l1_bytes); >> + sn_l1_table =3D g_try_malloc0(sn_l1_bytes); >> if (cur_l1_bytes && sn_l1_table =3D=3D NULL) { >> ret =3D -ENOMEM; >> goto fail; >> @@ -530,11 +523,28 @@ int qcow2_snapshot_goto(BlockDriverState *bs, = const char *snapshot_id) >> } >> =20 >> ret =3D bdrv_pwrite_sync(bs->file->bs, s->l1_table_offset, = sn_l1_table, >> - cur_l1_bytes); >> + sn_l1_bytes); >> + if (ret < 0) { >> + goto fail; >> + } >> + >> + /* write updated header.size */ >> + uint64_t be_disk_size =3D cpu_to_be64(sn->disk_size); >> + ret =3D bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, = size), >> + &be_disk_size, sizeof(uint64_t)); >> if (ret < 0) { >> goto fail; >> } >> =20 >> + uint32_t be_l1_size =3D cpu_to_be32(sn->l1_size); >> + ret =3D bdrv_pwrite_sync(bs->file->bs, offsetof(QCowHeader, = l1_size), >> + &be_l1_size, sizeof(be_l1_size)); >> + if (ret < 0) { >> + goto fail; >> + } >> + >> + s->l1_vm_state_index =3D sn->l1_size; >> + >> /* >> * Decrease refcount of clusters of current L1 table. >> * >> @@ -552,7 +562,8 @@ int qcow2_snapshot_goto(BlockDriverState *bs, = const char *snapshot_id) >> * Now update the in-memory L1 table to be in sync with the = on-disk one. We >> * need to do this even if updating refcounts failed. >> */ >> - for(i =3D 0;i < s->l1_size; i++) { >> + memset(s->l1_table, 0, s->l1_size * sizeof(uint64_t)); >> + for (i =3D 0; i < sn->l1_size; i++) { >> s->l1_table[i] =3D be64_to_cpu(sn_l1_table[i]); >> } >> =20 >> @@ -563,6 +574,7 @@ int qcow2_snapshot_goto(BlockDriverState *bs, = const char *snapshot_id) >> g_free(sn_l1_table); >> sn_l1_table =3D NULL; >> =20 >> + s->l1_size =3D sn->l1_size; >> /* >> * Update QCOW_OFLAG_COPIED in the active L1 table (it may have = changed >> * when we decreased the refcount of the old snapshot. >> @@ -675,6 +687,7 @@ int qcow2_snapshot_list(BlockDriverState *bs, = QEMUSnapshotInfo **psn_tab) >> sn_info->date_sec =3D sn->date_sec; >> sn_info->date_nsec =3D sn->date_nsec; >> sn_info->vm_clock_nsec =3D sn->vm_clock_nsec; >> + sn_info->disk_size =3D sn->disk_size; >> } >> *psn_tab =3D sn_tab; >> return s->nb_snapshots; >> diff --git a/block/qcow2.c b/block/qcow2.c >> index 62febfc..58b53e1 100644 >> --- a/block/qcow2.c >> +++ b/block/qcow2.c >> @@ -2501,22 +2501,33 @@ static int qcow2_truncate(BlockDriverState = *bs, int64_t offset) >> return -EINVAL; >> } >> =20 >> - /* cannot proceed if image has snapshots */ >> - if (s->nb_snapshots) { >> - error_report("Can't resize an image which has snapshots"); >> + bool v3_truncate =3D (s->qcow_version =3D=3D 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; >> } >> =20 >> - /* 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; >> } >> =20 >> new_l1_size =3D size_to_l1(s, offset); >> - ret =3D qcow2_grow_l1_table(bs, new_l1_size, true); >> - if (ret < 0) { >> - return ret; >> + if (offset < bs->total_sectors * 512) { >> + ret =3D shrink_l1_table(bs, new_l1_size); >> + if (ret < 0) { >> + return ret; >> + } >> + } else { >> + ret =3D qcow2_grow_l1_table(bs, new_l1_size, true); >> + if (ret < 0) { >> + return ret; >> + } >> } >> =20 >> /* write updated header.size */ >> diff --git a/block/qcow2.h b/block/qcow2.h >> index a063a3c..dab9e48 100644 >> --- a/block/qcow2.h >> +++ b/block/qcow2.h >> @@ -534,6 +534,7 @@ int qcow2_change_refcount_order(BlockDriverState = *bs, int refcount_order, >> void *cb_opaque, Error **errp); >> =20 >> /* qcow2-cluster.c functions */ >> +int shrink_l1_table(BlockDriverState *bs, int64_t new_l1_size); >> int qcow2_grow_l1_table(BlockDriverState *bs, uint64_t min_size, >> bool exact_size); >> int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index); >> 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; >> =20 >> int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo = *sn_info, >> --=20 >> 1.7.1 >>=20 >>=20 >>> On Jun 1, 2016, at 12:15 PM, zhangzhiming = > wrote: >>>=20 >>> hi, thanks for the review and the format of code changed. >>> have a good day! >>>=20 >>> zhangzhiming >>> zhangzhiming02@meituan.com >>>=20 >>> Signed-off-by: zhangzhiming > >>> --- >>> block.c | 4 ++-- >>> block/qcow2-cluster.c | 4 ++-- >>> block/qcow2-snapshot.c | 5 ++--- >>> block/qcow2.c | 4 ++-- >>> 4 files changed, 8 insertions(+), 9 deletions(-) >>>=20 >>> diff --git a/block.c b/block.c >>> index 729f820..b6f2004 100644 >>> --- a/block.c >>> +++ b/block.c >>> @@ -2643,9 +2643,9 @@ int bdrv_apply_snapshot(BlockDriverState *bs, = const char *snapshot_id, >>> if (ret < 0) { >>> return ret; >>> } >>> - bdrv_dirty_bitmap_truncate(bs); /* void return */ >>> + bdrv_dirty_bitmap_truncate(bs); >>> if (bs->blk) { >>> - blk_dev_resize_cb(bs->blk); /* void return too */ >>> + blk_dev_resize_cb(bs->blk); >>> } >>> return ret; >>> } >>> diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c >>> index e4c5c05..f921fd8 100644 >>> --- a/block/qcow2-cluster.c >>> +++ b/block/qcow2-cluster.c >>> @@ -37,14 +37,14 @@ int shrink_l1_table(BlockDriverState *bs, = int64_t new_l1_size) >>> int64_t old_l1_size =3D s->l1_size; >>> s->l1_size =3D new_l1_size; >>> int ret =3D qcow2_update_snapshot_refcount(bs, = s->l1_table_offset, >>> - s->l1_size, = 1); >>> + s->l1_size, 1); >>> if (ret < 0) { >>> return ret; >>> } >>> =20 >>> s->l1_size =3D old_l1_size; >>> ret =3D qcow2_update_snapshot_refcount(bs, s->l1_table_offset, >>> - s->l1_size, = -1); >>> + s->l1_size, -1); >>> if (ret < 0) { >>> return ret; >>> } >>> diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c >>> index 9c77096..1ed0e18 100644 >>> --- a/block/qcow2-snapshot.c >>> +++ b/block/qcow2-snapshot.c >>> @@ -562,12 +562,11 @@ int qcow2_snapshot_goto(BlockDriverState *bs, = const char *snapshot_id) >>> * Now update the in-memory L1 table to be in sync with the = on-disk one. We >>> * need to do this even if updating refcounts failed. >>> */ >>> - memset(s->l1_table, 0, s->l1_size*sizeof(uint64_t)); >>> - for(i =3D 0;i < sn->l1_size; i++) { >>> + memset(s->l1_table, 0, s->l1_size * sizeof(uint64_t)); >>> + for (i =3D 0; i < sn->l1_size; i++) { >>> s->l1_table[i] =3D be64_to_cpu(sn_l1_table[i]); >>> } >>> =20 >>> - >>> if (ret < 0) { >>> goto fail; >>> } >>> diff --git a/block/qcow2.c b/block/qcow2.c >>> index 70ec890..58b53e1 100644 >>> --- a/block/qcow2.c >>> +++ b/block/qcow2.c >>> @@ -2503,14 +2503,14 @@ static int qcow2_truncate(BlockDriverState = *bs, int64_t offset) >>> =20 >>> bool v3_truncate =3D (s->qcow_version =3D=3D 3); >>> =20 >>> - /* cannot proceed if image has snapshots and qcow_version is = not 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; >>> } >>> =20 >>> - /* shrinking is supported from version 3*/ >>> + /* 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"); >>> --=20 >>> 1.7.1 >>>=20 >>>=20 >>>> On Jun 1, 2016, at 12:50 AM, Eric Blake > wrote: >>>>=20 >>>> On 05/27/2016 02:14 AM, zhangzhiming wrote: >>>>> Hi, i modified my code for qcow2 resize, and delete some code = related to=20 >>>>> qemu monitor. >>>>>=20 >>>>> and thanks for the review. >>>>>=20 >>>>> zhangzhiming >>>>> zhangzhiming02@meituan.com = > >>>>=20 >>>> Still missing a Signed-off-by designation, so it can't be applied = as-is. >>>>=20 >>>>>=20 >>>>> --- >>>>> block.c | 19 +++++++++++++++++++ >>>>> block/qcow2-cluster.c | 29 +++++++++++++++++++++++++++++ >>>>> block/qcow2-snapshot.c | 34 ++++++++++++++++++++++++---------- >>>>> block/qcow2.c | 29 ++++++++++++++++++++--------- >>>>> block/qcow2.h | 1 + >>>>> include/block/snapshot.h | 1 + >>>>> 6 files changed, 94 insertions(+), 19 deletions(-) >>>>>=20 >>>>> diff --git a/block.c b/block.c >>>>> index 18a497f..729f820 100644 >>>>> --- a/block.c >>>>> +++ b/block.c >>>>> @@ -2631,6 +2631,25 @@ int bdrv_truncate(BlockDriverState *bs, = int64_t offset) >>>>> return ret; >>>>> } >>>>>=20 >>>>> +int bdrv_apply_snapshot(BlockDriverState *bs, const char = *snapshot_id, >>>>> + uint64_t snapshot_size) >>>>> +{ >>>>> + int ret =3D bdrv_snapshot_goto(bs, snapshot_id); >>>>> + if (ret < 0) { >>>>> + return ret; >>>>> + } >>>>> + >>>>> + ret =3D refresh_total_sectors(bs, snapshot_size >> = BDRV_SECTOR_BITS); >>>>> + if (ret < 0) { >>>>> + return ret; >>>>> + } >>>>> + bdrv_dirty_bitmap_truncate(bs); /* void return */ >>>>> + if (bs->blk) { >>>>> + blk_dev_resize_cb(bs->blk); /* void return too */ >>>>=20 >>>> The comments don't add anything here. >>>>=20 >>>>> + } >>>>> + 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-cluster.c b/block/qcow2-cluster.c >>>>> index 31ecc10..e4c5c05 100644 >>>>> --- a/block/qcow2-cluster.c >>>>> +++ b/block/qcow2-cluster.c >>>>> @@ -31,6 +31,35 @@ >>>>> #include "block/qcow2.h" >>>>> #include "trace.h" >>>>>=20 >>>>> +int shrink_l1_table(BlockDriverState *bs, int64_t new_l1_size) >>>>> +{ >>>>> + BDRVQcow2State *s =3D bs->opaque; >>>>> + int64_t old_l1_size =3D s->l1_size; >>>>> + s->l1_size =3D new_l1_size; >>>>> + int ret =3D qcow2_update_snapshot_refcount(bs, = s->l1_table_offset, >>>>> + s->l1_size, = 1); >>>>=20 >>>> Indentation is off. >>>>=20 >>>>> + if (ret < 0) { >>>>> + return ret; >>>>> + } >>>>> + >>>>> + s->l1_size =3D old_l1_size; >>>>> + ret =3D qcow2_update_snapshot_refcount(bs, = s->l1_table_offset, >>>>> + s->l1_size, = -1); >>>>=20 >>>> and again. >>>>=20 >>>>> + if (ret < 0) { >>>>> + return ret; >>>>> + } >>>>> + s->l1_size =3D new_l1_size; >>>>> + >>>>=20 >>>>> +++ b/block/qcow2-snapshot.c >>>>=20 >>>>> @@ -552,10 +562,12 @@ int qcow2_snapshot_goto(BlockDriverState = *bs, const char *snapshot_id) >>>>> * Now update the in-memory L1 table to be in sync with the = on-disk one. We >>>>> * need to do this even if updating refcounts failed. >>>>> */ >>>>> - for(i =3D 0;i < s->l1_size; i++) { >>>>> + memset(s->l1_table, 0, s->l1_size*sizeof(uint64_t)); >>>>> + for(i =3D 0;i < sn->l1_size; i++) { >>>>=20 >>>> As long as you are touching this, use the preferred spacing: >>>>=20 >>>> for (i =3D 0; i < sn->l1_size; i++) { >>>>=20 >>>>> s->l1_table[i] =3D be64_to_cpu(sn_l1_table[i]); >>>>> } >>>>>=20 >>>>> + >>>>> if (ret < 0) { >>>>=20 >>>> Why the added blank line? >>>>=20 >>>>=20 >>>>> +++ b/block/qcow2.c >>>>> @@ -2501,22 +2501,33 @@ static int qcow2_truncate(BlockDriverState = *bs, int64_t offset) >>>>> return -EINVAL; >>>>> } >>>>>=20 >>>>> - /* cannot proceed if image has snapshots */ >>>>> - if (s->nb_snapshots) { >>>>> - error_report("Can't resize an image which has = snapshots"); >>>>> + bool v3_truncate =3D (s->qcow_version =3D=3D 3); >>>>> + >>>>> + /* cannot proceed if image has snapshots and qcow_version is = not 3*/ >>>>=20 >>>> Space before */ >>>>=20 >>>>> + if (!v3_truncate && s->nb_snapshots) { >>>>> + error_report("Can't resize an image which has snapshots = and " >>>>> + "qcow_version is not 3"); >>>>> return -ENOTSUP; >>>>> } >>>>>=20 >>>>> - /* 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*/ >>>>=20 >>>> and again >>>>=20 >>>>> + 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; >>>>> } >>>>=20 >>>>=20 >>>> --=20 >>>> Eric Blake eblake redhat com +1-919-301-3266 >>>> Libvirt virtualization library http://libvirt.org = >>>>=20 >>>=20 >>=20 >=20