[Devel] [PATCH hci-8.1 v7 3/6] dm-qcow2: extend the file in place_r2() before linking the refblock

Andrey Zhadchenko andrey.zhadchenko at virtuozzo.com
Mon Oct 26 13:52:53 MSK 2026


This patch definitely solves the problems but feels too hacky for me. I 
think it will be better to step back and rethink why 
find_unused_cluster() calls modification function (place_r2) while being 
named 'find'. Won't it be better if we can notify allocate_cluster() 
that we need new r2 and it will punch space + call place_r2 for us and 
then retry find_unused_cluster?
This way we will have all image size metadata bookkeeping in one place 
instead of having a secret second 'allocate refcount cluster if we run 
out of space'

On 10/22/26 16:50, Alexander Ivanov wrote:
> place_r2() links a new refblock into R1, but moves neither file_size
> nor file_preallocated_area_start. The refblock stays at or above the
> watermark if truncate_prealloc_safe() in allocate_cluster() fails
> after place_r2(), or if a parallel mark_cluster_unused() lowers
> free_cluster_search_pos before the second pass of
> find_unused_cluster(). Truncation of preallocations on suspend then
> cuts the file under the refblock: R1 on disk points past EOF, and
> the next session gets -EIO on allocations in the range it covers.
> 
> Extend the file and raise the watermark to the end of the refblock
> before linking it into R1, as allocate_cluster() does for data
> clusters.
> 
> https://virtuozzo.atlassian.net/browse/VSTOR-148171
> Feature: dm-qcow2: block device over QCOW2 files driver
> 
> Signed-off-by: Alexander Ivanov <alexander.ivanov at virtuozzo.com>
> ---
>   drivers/md/dm-qcow2-map.c | 8 ++++++++
>   1 file changed, 8 insertions(+)
> 
> diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
> index 78ee7cf5e881..810dd22425ae 100644
> --- a/drivers/md/dm-qcow2-map.c
> +++ b/drivers/md/dm-qcow2-map.c
> @@ -82,6 +82,7 @@ struct qcow2_bvec {
>   static int qcow2_handle_r1r2_maps(struct qcow2 *qcow2, loff_t pos, struct qio **qio,
>   	struct qcow2_map_item *r1, struct qcow2_map_item *r2, bool compressed);
>   static int qcow2_punch_hole(struct file *file, loff_t pos, loff_t len);
> +static int truncate_prealloc_safe(struct qcow2 *qcow2, loff_t len, const char *func);
>   static void handle_cleanup_mask(struct qio *qio);
>   static void process_read_qio(struct qcow2 *qcow2, struct qio *qio,
>   			     struct qcow2_map *map);
> @@ -2088,6 +2089,7 @@ static int place_r2(struct qcow2 *qcow2, struct qcow2_map_item *r1,
>   		    struct qcow2_map_item *r2, loff_t r2_pos, struct qio **qio)
>   {
>   	struct dm_target *ti = qcow2->tgt->ti;
> +	loff_t end = r2_pos + qcow2->clu_size;
>   	u64 page_id = r2_pos >> PAGE_SHIFT;
>   	int ret;
>   
> @@ -2100,6 +2102,12 @@ static int place_r2(struct qcow2 *qcow2, struct qcow2_map_item *r1,
>   		return ret;
>   	}
>   
> +	ret = truncate_prealloc_safe(qcow2, end, __func__);
> +	if (ret)
> +		return ret;
> +	if (end > qcow2->file_preallocated_area_start)
> +		qcow2->file_preallocated_area_start = end;
> +
>   	ret = qcow2_alloc_and_insert_md_page(qcow2, page_id, &r2->md);
>   	if (ret < 0) {
>   		QC_ERR(ti, "Can't alloc: ret=%d, page_id=%llu", ret, page_id);



More information about the Devel mailing list