qcow2: Support BDRV_REQ_MAY_UNMAP
PATCH 1/1 qcow2: Support BDRV_REQ_MAY_UNMAP
Handling this is similar to what is done to the L2 entry in the case of compressed clusters.
--- block/qcow2-cluster.c | 9 +++++---- block/qcow2.c | 3 ++- block/qcow2.h | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-)
block/qcow2-cluster.c +5 -4
@@ -1558,7 +1558,7 @@ fail:* clusters. */ static int zero_single_l2 (BlockDriverState *bs,uint64_t offset, -uint64_t nb_clusters) +uint64_t nb_clusters,int flags) {BDRVQcow2State *s = bs->opaque;uint64_t *l2_table;
@@ -1582,7 +1582,7 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,/* Update L2 entries */ qcow2_cache_entry_mark_dirty (bs, s->l2_table_cache, l2_table); -if (old_offset &QCOW_OFLAG_COMPRESSED ) { +if (old_offset &QCOW_OFLAG_COMPRESSED || flags &BDRV_REQ_MAY_UNMAP ) { l2_table[l2_index + i] =cpu_to_be64 (QCOW_OFLAG_ZERO );qcow2_free_any_clusters (bs, old_offset,1 ,QCOW2_DISCARD_REQUEST ); }else {
@@ -1595,7 +1595,8 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,return nb_clusters; } -int qcow2_zero_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors) +int qcow2_zero_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors, +int flags) {BDRVQcow2State *s = bs->opaque;uint64_t nb_clusters;
@@ -1612,7 +1613,7 @@ int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors) s->cache_discards = true;while (nb_clusters >0 ) { - ret =zero_single_l2 (bs, offset, nb_clusters); + ret =zero_single_l2 (bs, offset, nb_clusters, flags);if (ret <0 ) { goto fail; }
block/qcow2.c +2 -1
@@ -1154,6 +1154,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,/* Initialise locks */ qemu_co_mutex_init (&s->lock); + bs->supported_zero_flags =BDRV_REQ_MAY_UNMAP ;/* Repair image if dirty */ if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&
@@ -2476,7 +2477,7 @@ static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,trace_qcow2_pwrite_zeroes (qemu_coroutine_self (), offset, count);/* Whatever is left can use real zero clusters */ - ret =qcow2_zero_clusters (bs, offset, count >>BDRV_SECTOR_BITS ); + ret =qcow2_zero_clusters (bs, offset, count >>BDRV_SECTOR_BITS , flags);qemu_co_mutex_unlock (&s->lock);return ret;
block/qcow2.h +2 -1
@@ -547,7 +547,8 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m);int qcow2_discard_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors,enum qcow2_discard_type type,bool full_discard); -int qcow2_zero_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors); +int qcow2_zero_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors, +int flags); int qcow2_expand_zero_clusters(BlockDriverState *bs, BlockDriverAmendStatusCB *status_cb,
Signature · 1 line
2.7.4
Quoted 24 lines · Fam Zheng
Handling this is similar to what is done to the L2 entry in the case of compressed clusters.
Signed-off-by Fam Zheng <[email protected]>--- block/qcow2-cluster.c | 9 +++++---- block/qcow2.c | 3 ++- block/qcow2.h | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-)block/qcow2-cluster.c +1
-1@@ -1558,7 +1558,7 @@ fail:* clusters. */ static int zero_single_l2 (BlockDriverState *bs,uint64_t offset, -uint64_t nb_clusters) +uint64_t nb_clusters,int flags) {BDRVQcow2State *s = bs->opaque;uint64_t *l2_table;
block/qcow2-cluster.c static int zero_single_l2(BlockDriverState *bs, uint64_t offset, · line 1587
Quoted 7 lines
block/qcow2-cluster.c +1
-1@@ -1582,7 +1582,7 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,/* Update L2 entries */ qcow2_cache_entry_mark_dirty (bs, s->l2_table_cache, l2_table); -if (old_offset &QCOW_OFLAG_COMPRESSED ) { +if (old_offset &QCOW_OFLAG_COMPRESSED || flags &BDRV_REQ_MAY_UNMAP ) { l2_table[l2_index + i] =cpu_to_be64 (QCOW_OFLAG_ZERO );qcow2_free_any_clusters (bs, old_offset,1 ,QCOW2_DISCARD_REQUEST );
I don't quite understand the reasoning behind this. How is this more efficient than just using the existing path where we don't discard anything?
Note that BDRV_REQ_MAY_UNMAP does not mean "Yes, please discard" but just "You may discard if it's easier for you". But it's actually not easier for us, so I don't see why we're doing it.
As far as I can guess you actually want some way to tell a block driver to actually make an effort to discard clusters as long they then read back as zero (which is why you cannot simply use bdrv_pdiscard()). However, I think this would require a new flag called BDRV_REQ_SHOULD_UNMAP (which should imply BDRV_REQ_MAY_UNMAP).
Note that there is actually a case where qcow2 should support BDRV_REQ_MAY_UNMAP, which is for v2 images. Currently, we just return -ENOTSUP for them, but if we don't have a backing file and BDRV_REQ_MAY_UNMAP is set, we could go on and make qcow2_zero_clusters() work for them.
Max
Quoted 56 lines
block/qcow2-cluster.c +3
-2}else {@@ -1595,7 +1595,8 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,return nb_clusters; } -int qcow2_zero_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors) +int qcow2_zero_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors, +int flags) {BDRVQcow2State *s = bs->opaque;uint64_t nb_clusters;@@ -1612,7 +1613,7 @@ int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors) s->cache_discards = true;while (nb_clusters >0 ) { - ret =zero_single_l2 (bs, offset, nb_clusters); + ret =zero_single_l2 (bs, offset, nb_clusters, flags);if (ret <0 ) { goto fail; }block/qcow2.c +2
-1@@ -1154,6 +1154,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,/* Initialise locks */ qemu_co_mutex_init (&s->lock); + bs->supported_zero_flags =BDRV_REQ_MAY_UNMAP ;/* Repair image if dirty */ if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&@@ -2476,7 +2477,7 @@ static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,trace_qcow2_pwrite_zeroes (qemu_coroutine_self (), offset, count);/* Whatever is left can use real zero clusters */ - ret =qcow2_zero_clusters (bs, offset, count >>BDRV_SECTOR_BITS ); + ret =qcow2_zero_clusters (bs, offset, count >>BDRV_SECTOR_BITS , flags);qemu_co_mutex_unlock (&s->lock);return ret;block/qcow2.h +2
-1@@ -547,7 +547,8 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,int qcow2_alloc_cluster_link_l2 (BlockDriverState *bs,QCowL2Meta *m);int qcow2_discard_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors,enum qcow2_discard_type type,bool full_discard); -int qcow2_zero_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors); +int qcow2_zero_clusters (BlockDriverState *bs,uint64_t offset,int nb_sectors, +int flags); int qcow2_expand_zero_clusters(BlockDriverState *bs, BlockDriverAmendStatusCB *status_cb,
Quoted 26 lines · Max Reitz
Quoted 25 lines · Fam Zheng
Handling this is similar to what is done to the L2 entry in the case of compressed clusters.
Signed-off-by Fam Zheng <[email protected]>--- block/qcow2-cluster.c | 9 +++++---- block/qcow2.c | 3 ++- block/qcow2.h | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-)block/qcow2-cluster.c +1
-1@@ -1558,7 +1558,7 @@ fail:* clusters. */ static int zero_single_l2 (BlockDriverState *bs,uint64_t offset, -uint64_t nb_clusters) +uint64_t nb_clusters,int flags) {BDRVQcow2State *s = bs->opaque;uint64_t *l2_table;@@ -1582,7 +1582,7 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,
Quoted 9 lines
block/qcow2-cluster.c static int zero_single_l2(BlockDriverState *bs, uint64_t offset, · line 1587
Quoted 6 lines
block/qcow2-cluster.c +1
-1/* Update L2 entries */ qcow2_cache_entry_mark_dirty (bs, s->l2_table_cache, l2_table); -if (old_offset &QCOW_OFLAG_COMPRESSED ) { +if (old_offset &QCOW_OFLAG_COMPRESSED || flags &BDRV_REQ_MAY_UNMAP ) { l2_table[l2_index + i] =cpu_to_be64 (QCOW_OFLAG_ZERO );qcow2_free_any_clusters (bs, old_offset,1 ,QCOW2_DISCARD_REQUEST );I don't quite understand the reasoning behind this. How is this more efficient than just using the existing path where we don't discard anything?
It's more efficient in disk space. I didn't mention because it is so not specific to this, but: what virt-sparsify does is creating an overlay -> fstrim it -> qemu-img commit. This flow revealed to me that BDRV_REQ_MAY_UNMAP should have been honored this way (after a hint of "how" by Kevin).
Quoted 10 lines
Note that
BDRV_REQ_MAY_UNMAPdoes not mean "Yes, please discard" but just "You may discard if it's easier for you". But it's actually not easier for us, so I don't see why we're doing it.As far as I can guess you actually want some way to tell a block driver to actually make an effort to discard clusters as long they then read back as zero (which is why you cannot simply use
bdrv_pdiscard()). However, I think this would require a new flag calledBDRV_REQ_SHOULD_UNMAP(which should implyBDRV_REQ_MAY_UNMAP).
This flag doesn't make sense to me, if the protocol doesn't know how to unmap, it can ignore BDRV_REQ_MAY_UNMAP, but not BDRV_REQ_SHOULD_UNMAP. It just complicates things a little.
Fam
Quoted 11 lines · Fam Zheng
Quoted 9 lines · Max Reitz
Note that
BDRV_REQ_MAY_UNMAPdoes not mean "Yes, please discard" but just "You may discard if it's easier for you". But it's actually not easier for us, so I don't see why we're doing it.As far as I can guess you actually want some way to tell a block driver to actually make an effort to discard clusters as long they then read back as zero (which is why you cannot simply use
bdrv_pdiscard()). However, I think this would require a new flag calledBDRV_REQ_SHOULD_UNMAP(which should implyBDRV_REQ_MAY_UNMAP).
Quoted 3 lines
This flag doesn't make sense to me, if the protocol doesn't know how to unmap, it can ignore
BDRV_REQ_MAY_UNMAP, but notBDRV_REQ_SHOULD_UNMAP. It just complicates things a little.
I don't think we actually have a use for a "MAY" unmap flag. Either we keep the not-so-perfect name or we replace MAY_UNMAP with "should" or "want" or "would_like" unmap... But Fam's patch does do what was intended for the flag (which is the equivalent of the UNMAP bit in the SCSI WRITE SAME command).
Paolo
Quoted 18 lines · Paolo Bonzini
Quoted 11 lines · Fam Zheng
Quoted 9 lines · Max Reitz
Note that
BDRV_REQ_MAY_UNMAPdoes not mean "Yes, please discard" but just "You may discard if it's easier for you". But it's actually not easier for us, so I don't see why we're doing it.As far as I can guess you actually want some way to tell a block driver to actually make an effort to discard clusters as long they then read back as zero (which is why you cannot simply use
bdrv_pdiscard()). However, I think this would require a new flag calledBDRV_REQ_SHOULD_UNMAP(which should implyBDRV_REQ_MAY_UNMAP).Quoted 3 lines
This flag doesn't make sense to me, if the protocol doesn't know how to unmap, it can ignore
BDRV_REQ_MAY_UNMAP, but notBDRV_REQ_SHOULD_UNMAP. It just complicates things a little.
Quoted 5 lines
I don't think we actually have a use for a "MAY" unmap flag. Either we keep the not-so-perfect name or we replace
MAY_UNMAPwith "should" or "want" or "would_like" unmap... But Fam's patch does do what was intended for the flag (which is the equivalent of the UNMAP bit in the SCSI WRITE SAME command).
After reading rfc2119, now I agree that "SHOULD" is better. :)
Fam
Quoted 19 lines · Fam Zheng
Quoted 18 lines · Paolo Bonzini
Quoted 14 lines · Fam Zheng
Quoted 9 lines · Max Reitz
Note that
BDRV_REQ_MAY_UNMAPdoes not mean "Yes, please discard" but just "You may discard if it's easier for you". But it's actually not easier for us, so I don't see why we're doing it.As far as I can guess you actually want some way to tell a block driver to actually make an effort to discard clusters as long they then read back as zero (which is why you cannot simply use
bdrv_pdiscard()). However, I think this would require a new flag calledBDRV_REQ_SHOULD_UNMAP(which should implyBDRV_REQ_MAY_UNMAP).This flag doesn't make sense to me, if the protocol doesn't know how to unmap, it can ignore
BDRV_REQ_MAY_UNMAP, but notBDRV_REQ_SHOULD_UNMAP. It just complicates things a little.
Quoted 7 lines
Quoted 5 lines
I don't think we actually have a use for a "MAY" unmap flag. Either we keep the not-so-perfect name or we replace
MAY_UNMAPwith "should" or "want" or "would_like" unmap... But Fam's patch does do what was intended for the flag (which is the equivalent of the UNMAP bit in the SCSI WRITE SAME command).After reading rfc2119, now I agree that "SHOULD" is better. :)
That's OK with me, then.
Max
Am 28.09.2016 um 09:04 hat Fam Zheng geschrieben:
Quoted 24 lines
Handling this is similar to what is done to the L2 entry in the case of compressed clusters.
Signed-off-by Fam Zheng <[email protected]>--- block/qcow2-cluster.c | 9 +++++---- block/qcow2.c | 3 ++- block/qcow2.h | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-)block/qcow2-cluster.c +1
-1@@ -1558,7 +1558,7 @@ fail:* clusters. */ static int zero_single_l2 (BlockDriverState *bs,uint64_t offset, -uint64_t nb_clusters) +uint64_t nb_clusters,int flags) {BDRVQcow2State *s = bs->opaque;uint64_t *l2_table;
block/qcow2-cluster.c static int zero_single_l2(BlockDriverState *bs, uint64_t offset, · line 1588
Quoted 8 lines
block/qcow2-cluster.c +1
-1@@ -1582,7 +1582,7 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,/* Update L2 entries */ qcow2_cache_entry_mark_dirty (bs, s->l2_table_cache, l2_table); -if (old_offset &QCOW_OFLAG_COMPRESSED ) { +if (old_offset &QCOW_OFLAG_COMPRESSED || flags &BDRV_REQ_MAY_UNMAP ) { l2_table[l2_index + i] =cpu_to_be64 (QCOW_OFLAG_ZERO );qcow2_free_any_clusters (bs, old_offset,1 ,QCOW2_DISCARD_REQUEST ); }else {
I don't think we should always do this for BDRV_REQ_MAY_UNMAP. The user may want to keep the image fully allocated even if the guest (or block job etc.) thinks this can be discarded.
When we discussed this in another email thread (?), I think I suggested checking whether the image was opened with pass-discard-request=on. If so, go ahead and deallocate the cluster, otherwise keep it allocated.
In those cases where pass-discard-request=off, it doesn't make a lot of sense to deallocate the cluster anyway because it won't shrink the file size.
Kevin
Quoted 38 lines · Kevin Wolf
Am 28.09.2016 um 09:04 hat Fam Zheng geschrieben:
Quoted 24 lines
Handling this is similar to what is done to the L2 entry in the case of compressed clusters.
Signed-off-by Fam Zheng <[email protected]>--- block/qcow2-cluster.c | 9 +++++---- block/qcow2.c | 3 ++- block/qcow2.h | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-)block/qcow2-cluster.c +1
-1@@ -1558,7 +1558,7 @@ fail:* clusters. */ static int zero_single_l2 (BlockDriverState *bs,uint64_t offset, -uint64_t nb_clusters) +uint64_t nb_clusters,int flags) {BDRVQcow2State *s = bs->opaque;uint64_t *l2_table;block/qcow2-cluster.c static int zero_single_l2(BlockDriverState *bs, uint64_t offset, · line 1588
Quoted 8 lines
block/qcow2-cluster.c +1
-1@@ -1582,7 +1582,7 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,/* Update L2 entries */ qcow2_cache_entry_mark_dirty (bs, s->l2_table_cache, l2_table); -if (old_offset &QCOW_OFLAG_COMPRESSED ) { +if (old_offset &QCOW_OFLAG_COMPRESSED || flags &BDRV_REQ_MAY_UNMAP ) { l2_table[l2_index + i] =cpu_to_be64 (QCOW_OFLAG_ZERO );qcow2_free_any_clusters (bs, old_offset,1 ,QCOW2_DISCARD_REQUEST ); }else {I don't think we should always do this for
BDRV_REQ_MAY_UNMAP. The user may want to keep the image fully allocated even if the guest (or block job etc.) thinks this can be discarded.
Quoted 7 lines
When we discussed this in another email thread (?), I think I suggested checking whether the image was opened with pass-discard-request=on. If so, go ahead and deallocate the cluster, otherwise keep it allocated.
In those cases where pass-discard-request=off, it doesn't make a lot of sense to deallocate the cluster anyway because it won't shrink the file size.
I think this patch does what you mean:
$ strace -f -e fallocate qemu-io \
-c 'open -o pass-discard-request=on /var/tmp/test' \
-c 'write 0 1M' \
-c 'write -u -z 0 1M' \
2>&1 >/dev/null | \
grep fallocate
[pid 17548] fallocate(17, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 327680, 1048576) = 0
$ strace -f -e fallocate qemu-io \
-c 'open -o pass-discard-request=off /var/tmp/test' \
-c 'write 0 1M' \
-c 'write -u -z 0 1M' \
2>&1 >/dev/null | \
grep fallocateBecause there is another check of pass-discard-request value in update_refcount:
if (refcount == 0 && s->discard_passthrough[type]) {
update_refcount_discard(bs, cluster_offset, s->cluster_size);
}Fam
Am 29.09.2016 um 11:55 hat Fam Zheng geschrieben:
Quoted 63 lines · Kevin Wolf
Quoted 38 lines
Am 28.09.2016 um 09:04 hat Fam Zheng geschrieben:
Quoted 24 lines
Handling this is similar to what is done to the L2 entry in the case of compressed clusters.
Signed-off-by Fam Zheng <[email protected]>--- block/qcow2-cluster.c | 9 +++++---- block/qcow2.c | 3 ++- block/qcow2.h | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-)block/qcow2-cluster.c +1
-1@@ -1558,7 +1558,7 @@ fail:* clusters. */ static int zero_single_l2 (BlockDriverState *bs,uint64_t offset, -uint64_t nb_clusters) +uint64_t nb_clusters,int flags) {BDRVQcow2State *s = bs->opaque;uint64_t *l2_table;block/qcow2-cluster.c static int zero_single_l2(BlockDriverState *bs, uint64_t offset, · line 1588
Quoted 8 lines
block/qcow2-cluster.c +1
-1@@ -1582,7 +1582,7 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,/* Update L2 entries */ qcow2_cache_entry_mark_dirty (bs, s->l2_table_cache, l2_table); -if (old_offset &QCOW_OFLAG_COMPRESSED ) { +if (old_offset &QCOW_OFLAG_COMPRESSED || flags &BDRV_REQ_MAY_UNMAP ) { l2_table[l2_index + i] =cpu_to_be64 (QCOW_OFLAG_ZERO );qcow2_free_any_clusters (bs, old_offset,1 ,QCOW2_DISCARD_REQUEST ); }else {I don't think we should always do this for
BDRV_REQ_MAY_UNMAP. The user may want to keep the image fully allocated even if the guest (or block job etc.) thinks this can be discarded.Quoted 7 lines
When we discussed this in another email thread (?), I think I suggested checking whether the image was opened with pass-discard-request=on. If so, go ahead and deallocate the cluster, otherwise keep it allocated.
In those cases where pass-discard-request=off, it doesn't make a lot of sense to deallocate the cluster anyway because it won't shrink the file size.
I think this patch does what you mean:
$ strace -f -e fallocate qemu-io \ -c 'open -o pass-discard-request=on /var/tmp/test' \ -c 'write 0 1M' \ -c 'write -u -z 0 1M' \ 2>&1 >/dev/null | \ grep fallocate [pid 17548] fallocate(17, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 327680, 1048576) = 0 $ strace -f -e fallocate qemu-io \ -c 'open -o pass-discard-request=off /var/tmp/test' \ -c 'write 0 1M' \ -c 'write -u -z 0 1M' \ 2>&1 >/dev/null | \ grep fallocate
Quoted 6 lines
Because there is another check of pass-discard-request value in
update_refcount:if (refcount == 0 && s->discard_passthrough[type]) { update_refcount_discard(bs, cluster_offset, s->cluster_size); }
What I mean is that in the second case, you're still uselessly deallocating the cluster on the qcow2 level while you can't reclaim it on the filesystem level. So it would be better to leave it allocated in qcow2, too, so that you don't get an expensive reallocation the next time you write to it.
Kevin
Quoted 4 lines · Kevin Wolf
Quoted 4 lines
Quoted 4 lines
Because there is another check of pass-discard-request value in
update_refcount:
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
if (refcount == 0 && s->discard_passthrough[type]) { update_refcount_discard(bs, cluster_offset, s->cluster_size); }What I mean is that in the second case, you're still uselessly deallocating the cluster on the qcow2 level while you can't reclaim it on the filesystem level. So it would be better to leave it allocated in qcow2, too, so that you don't get an expensive reallocation the next time you write to it.
But if you do a qemu-img convert, the deallocated cluster wouldn't be in the destination.
Paolo
Am 29.09.2016 um 14:14 hat Paolo Bonzini geschrieben:
Quoted 7 lines · Kevin Wolf
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Because there is another check of pass-discard-request value in
update_refcount:if (refcount == 0 && s->discard_passthrough[type]) { update_refcount_discard(bs, cluster_offset, s->cluster_size); }
Quoted 8 lines
Quoted 5 lines
What I mean is that in the second case, you're still uselessly deallocating the cluster on the qcow2 level while you can't reclaim it on the filesystem level. So it would be better to leave it allocated in qcow2, too, so that you don't get an expensive reallocation the next time you write to it.
But if you do a qemu-img convert, the deallocated cluster wouldn't be in the destination.
Right. I still think that there has to be an option to keep the image fully allocated. Perhaps what we really need to check is BDRV_O_UNMAP.
Kevin
Quoted 14 lines · Kevin Wolf
Am 29.09.2016 um 14:14 hat Paolo Bonzini geschrieben:
Quoted 13 lines · Kevin Wolf
Quoted 11 lines
Quoted 6 lines
Quoted 6 lines
Because there is another check of pass-discard-request value in
update_refcount:if (refcount == 0 && s->discard_passthrough[type]) { update_refcount_discard(bs, cluster_offset, s->cluster_size); }What I mean is that in the second case, you're still uselessly deallocating the cluster on the qcow2 level while you can't reclaim it on the filesystem level. So it would be better to leave it allocated in qcow2, too, so that you don't get an expensive reallocation the next time you write to it.
Quoted 5 lines
Quoted 2 lines
But if you do a qemu-img convert, the deallocated cluster wouldn't be in the destination.
Right. I still think that there has to be an option to keep the image fully allocated. Perhaps what we really need to check is
BDRV_O_UNMAP.
Duh, of course it is.
Paolo
Quoted 17 lines · Paolo Bonzini
Quoted 14 lines · Kevin Wolf
Am 29.09.2016 um 14:14 hat Paolo Bonzini geschrieben:
Quoted 13 lines · Kevin Wolf
Quoted 11 lines
Quoted 6 lines
Quoted 6 lines
Because there is another check of pass-discard-request value in
update_refcount:if (refcount == 0 && s->discard_passthrough[type]) { update_refcount_discard(bs, cluster_offset, s->cluster_size); }What I mean is that in the second case, you're still uselessly deallocating the cluster on the qcow2 level while you can't reclaim it on the filesystem level. So it would be better to leave it allocated in qcow2, too, so that you don't get an expensive reallocation the next time you write to it.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
But if you do a qemu-img convert, the deallocated cluster wouldn't be in the destination.
Right. I still think that there has to be an option to keep the image fully allocated. Perhaps what we really need to check is
BDRV_O_UNMAP.Duh, of course it is.
... and it's handled in bdrv_co_pwrite_zeroes, so Fam's patch should be okay:
if (!(child->bs->open_flags & BDRV_O_UNMAP)) {
flags &= ~BDRV_REQ_MAY_UNMAP;
}
return bdrv_co_pwritev(child, offset, count, NULL,
BDRV_REQ_ZERO_WRITE | flags);Paolo
Quoted 71 lines · Kevin Wolf
Am 29.09.2016 um 11:55 hat Fam Zheng geschrieben:
Quoted 63 lines · Kevin Wolf
Quoted 38 lines
Am 28.09.2016 um 09:04 hat Fam Zheng geschrieben:
Quoted 24 lines
Handling this is similar to what is done to the L2 entry in the case of compressed clusters.
Signed-off-by Fam Zheng <[email protected]>--- block/qcow2-cluster.c | 9 +++++---- block/qcow2.c | 3 ++- block/qcow2.h | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-)block/qcow2-cluster.c +1
-1@@ -1558,7 +1558,7 @@ fail:* clusters. */ static int zero_single_l2 (BlockDriverState *bs,uint64_t offset, -uint64_t nb_clusters) +uint64_t nb_clusters,int flags) {BDRVQcow2State *s = bs->opaque;uint64_t *l2_table;block/qcow2-cluster.c static int zero_single_l2(BlockDriverState *bs, uint64_t offset, · line 1588
Quoted 8 lines
block/qcow2-cluster.c +1
-1@@ -1582,7 +1582,7 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,/* Update L2 entries */ qcow2_cache_entry_mark_dirty (bs, s->l2_table_cache, l2_table); -if (old_offset &QCOW_OFLAG_COMPRESSED ) { +if (old_offset &QCOW_OFLAG_COMPRESSED || flags &BDRV_REQ_MAY_UNMAP ) { l2_table[l2_index + i] =cpu_to_be64 (QCOW_OFLAG_ZERO );qcow2_free_any_clusters (bs, old_offset,1 ,QCOW2_DISCARD_REQUEST ); }else {I don't think we should always do this for
BDRV_REQ_MAY_UNMAP. The user may want to keep the image fully allocated even if the guest (or block job etc.) thinks this can be discarded.Quoted 7 lines
When we discussed this in another email thread (?), I think I suggested checking whether the image was opened with pass-discard-request=on. If so, go ahead and deallocate the cluster, otherwise keep it allocated.
In those cases where pass-discard-request=off, it doesn't make a lot of sense to deallocate the cluster anyway because it won't shrink the file size.
I think this patch does what you mean:
$ strace -f -e fallocate qemu-io \ -c 'open -o pass-discard-request=on /var/tmp/test' \ -c 'write 0 1M' \ -c 'write -u -z 0 1M' \ 2>&1 >/dev/null | \ grep fallocate [pid 17548] fallocate(17, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 327680, 1048576) = 0 $ strace -f -e fallocate qemu-io \ -c 'open -o pass-discard-request=off /var/tmp/test' \ -c 'write 0 1M' \ -c 'write -u -z 0 1M' \ 2>&1 >/dev/null | \ grep fallocateQuoted 6 lines
Because there is another check of pass-discard-request value in
update_refcount:if (refcount == 0 && s->discard_passthrough[type]) { update_refcount_discard(bs, cluster_offset, s->cluster_size); }
Quoted 6 lines
What I mean is that in the second case, you're still uselessly deallocating the cluster on the qcow2 level while you can't reclaim it on the filesystem level. So it would be better to leave it allocated in qcow2, too, so that you don't get an expensive reallocation the next time you write to it.
Like this?
---
block/qcow2-cluster.c +3 -1
@@ -1582,7 +1582,9 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,/* Update L2 entries */ qcow2_cache_entry_mark_dirty (bs, s->l2_table_cache, l2_table); -if (old_offset &QCOW_OFLAG_COMPRESSED || flags &BDRV_REQ_MAY_UNMAP ) { +if (old_offset &QCOW_OFLAG_COMPRESSED || + ((flags &BDRV_REQ_MAY_UNMAP ) && + s->discard_passthrough[QCOW2_DISCARD_REQUEST ])) { l2_table[l2_index + i] =cpu_to_be64 (QCOW_OFLAG_ZERO );qcow2_free_any_clusters (bs, old_offset,1 ,QCOW2_DISCARD_REQUEST ); }else {
Am 30.09.2016 um 04:04 hat Fam Zheng geschrieben:
Quoted 72 lines · Kevin Wolf
Quoted 71 lines
Am 29.09.2016 um 11:55 hat Fam Zheng geschrieben:
Quoted 69 lines · Kevin Wolf
Quoted 38 lines
Am 28.09.2016 um 09:04 hat Fam Zheng geschrieben:
Quoted 24 lines
Handling this is similar to what is done to the L2 entry in the case of compressed clusters.
Signed-off-by Fam Zheng <[email protected]>--- block/qcow2-cluster.c | 9 +++++---- block/qcow2.c | 3 ++- block/qcow2.h | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-)block/qcow2-cluster.c +1
-1@@ -1558,7 +1558,7 @@ fail:* clusters. */ static int zero_single_l2 (BlockDriverState *bs,uint64_t offset, -uint64_t nb_clusters) +uint64_t nb_clusters,int flags) {BDRVQcow2State *s = bs->opaque;uint64_t *l2_table;block/qcow2-cluster.c static int zero_single_l2(BlockDriverState *bs, uint64_t offset, · line 1588
Quoted 8 lines
block/qcow2-cluster.c +1
-1@@ -1582,7 +1582,7 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,/* Update L2 entries */ qcow2_cache_entry_mark_dirty (bs, s->l2_table_cache, l2_table); -if (old_offset &QCOW_OFLAG_COMPRESSED ) { +if (old_offset &QCOW_OFLAG_COMPRESSED || flags &BDRV_REQ_MAY_UNMAP ) { l2_table[l2_index + i] =cpu_to_be64 (QCOW_OFLAG_ZERO );qcow2_free_any_clusters (bs, old_offset,1 ,QCOW2_DISCARD_REQUEST ); }else {I don't think we should always do this for
BDRV_REQ_MAY_UNMAP. The user may want to keep the image fully allocated even if the guest (or block job etc.) thinks this can be discarded.Quoted 7 lines
When we discussed this in another email thread (?), I think I suggested checking whether the image was opened with pass-discard-request=on. If so, go ahead and deallocate the cluster, otherwise keep it allocated.
In those cases where pass-discard-request=off, it doesn't make a lot of sense to deallocate the cluster anyway because it won't shrink the file size.
I think this patch does what you mean:
$ strace -f -e fallocate qemu-io \ -c 'open -o pass-discard-request=on /var/tmp/test' \ -c 'write 0 1M' \ -c 'write -u -z 0 1M' \ 2>&1 >/dev/null | \ grep fallocate [pid 17548] fallocate(17, FALLOC_FL_KEEP_SIZE|FALLOC_FL_PUNCH_HOLE, 327680, 1048576) = 0 $ strace -f -e fallocate qemu-io \ -c 'open -o pass-discard-request=off /var/tmp/test' \ -c 'write 0 1M' \ -c 'write -u -z 0 1M' \ 2>&1 >/dev/null | \ grep fallocateBecause there is another check of pass-discard-request value in
update_refcount:if (refcount == 0 && s->discard_passthrough[type]) { update_refcount_discard(bs, cluster_offset, s->cluster_size); }
Quoted 8 lines
Quoted 6 lines
What I mean is that in the second case, you're still uselessly deallocating the cluster on the qcow2 level while you can't reclaim it on the filesystem level. So it would be better to leave it allocated in qcow2, too, so that you don't get an expensive reallocation the next time you write to it.
Like this?
That's what I had in mind, but actually I think Paolo is right and the BDRV_O_UNMAP check in bdrv_co_pwrite_zeroes() already does all that is needed. So the approach of the original patch should be okay.
Kevin
Quoted 17 lines
---
block/qcow2-cluster.c +3
-1@@ -1582,7 +1582,9 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset,/* Update L2 entries */ qcow2_cache_entry_mark_dirty (bs, s->l2_table_cache, l2_table); -if (old_offset &QCOW_OFLAG_COMPRESSED || flags &BDRV_REQ_MAY_UNMAP ) { +if (old_offset &QCOW_OFLAG_COMPRESSED || + ((flags &BDRV_REQ_MAY_UNMAP ) && + s->discard_passthrough[QCOW2_DISCARD_REQUEST ])) { l2_table[l2_index + i] =cpu_to_be64 (QCOW_OFLAG_ZERO );qcow2_free_any_clusters (bs, old_offset,1 ,QCOW2_DISCARD_REQUEST ); }else {
Quoted 2 lines · Fam Zheng
Handling this is similar to what is done to the L2 entry in the case of compressed clusters.
Kevin, Max, is there anything else I need to do before this patch can be applied?
Fam
Kevin Wolf
AppliedAm 12.10.2016 um 03:14 hat Fam Zheng geschrieben:
Quoted 6 lines · Fam Zheng
Quoted 2 lines
Handling this is similar to what is done to the L2 entry in the case of compressed clusters.
Kevin, Max, is there anything else I need to do before this patch can be applied?
Hm, actually, it looks like we had a few discussions, but came to the conclusions that the patch is alright.
Thanks, applied to the block branch.
Kevin