Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Pekka Enberg <penberg@kernel.org>
To: Prasad Joshi <prasadjoshi124@gmail.com>
Cc: mingo@elte.hu, kvm@vger.kernel.org, asias.hejun@gmail.com,
	gorcunov@gmail.com, levinsasha928@gmail.com, kwolf@redhat.com,
	stefanha@linux.vnet.ibm.com, chaitanyakulkarni15@gmail.com
Subject: Re: [PATCH] kvm tool: QCOW version 1 write support.
Date: Sun, 17 Apr 2011 13:52:38 +0300 (EEST)	[thread overview]
Message-ID: <alpine.DEB.2.00.1104171345340.5085@tiger> (raw)
In-Reply-To: <1302890734-7871-1-git-send-email-prasadjoshi124@gmail.com>

On Fri, 15 Apr 2011, Prasad Joshi wrote:
> -static int qcow1_write_sector(struct disk_image *self, uint64_t sector, void *src, uint32_t src_len)
> +static inline u64 get_file_length(int fd)
> {
> +	struct stat st;
> +
> +	if (fstat(fd, &st) < 0)
> +		return 0;
> +	return st.st_size;
> +}

Wouldn't it be better to put this in 'struct disk_image'? We do fstat() 
upon startup anyway.

> +
> +static inline u64 align(u64 address, uint32_t size)
> +{
> +	return (address + size) & (~(size - 1));
> +}

There's ALIGN macro somewhere in include/linux. Use that.

> +static uint32_t qcow1_write_cluster(struct qcow *q, uint64_t offset, void *buf,
> +		uint32_t src_len)
> +{
> +	struct qcow1_header *header = q->header;
> +	struct qcow_table *table    = &q->table;
> +	uint32_t length;

Please use u32, u64, and friends.

> +
> +	uint32_t l2_table_size;
> +	u64 l2_table_offset;
> +	u64 *l2_table;
> +	u64 l2_idx;
> +
> +	uint32_t clust_size;
> +	u64 clust_offset;
> +	u64 clust_start;
> +
> +	u64 l1_idx;
> +	u64 tmp;
> +
> +	l2_table_size = 1 << header->l2_bits;
> +	clust_size    = 1 << header->cluster_bits;
> +
> +	l1_idx = get_l1_index(q, offset);
> +	if (l1_idx >= table->table_size)
> +		goto error;
> +
> +	l2_idx = get_l2_index(q, offset);
> +	if (l2_idx >= l2_table_size)
> +		goto error;
> +
> +	clust_offset = get_cluster_offset(q, offset);
> +	if (clust_offset >= clust_size)
> +		goto error;
> +
> +	length = clust_size - clust_offset;
> +	if (length > src_len)
> +		length = src_len;
> +
> +	l2_table = calloc(l2_table_size, sizeof(u64));
> +	if (!l2_table)
> +		goto error;
> +
> +	l2_table_offset = table->l1_table[l1_idx];
> +	if (l2_table_offset) {
> +		if (pread_in_full(q->fd, l2_table, l2_table_size * sizeof(u64),
> +					l2_table_offset) < 0)
> +			goto free_l2;
> +	} else {
> +		/*
> +		 * 1. write a level 2 table of zeros at the end of the file
> +		 * 2. update the level1 table
> +		 */
> +		l2_table_offset = align(get_file_length(q->fd), clust_size);
> +		table->l1_table[l1_idx] = l2_table_offset;
> +
> +		if (qcow_pwrite_with_sync(q->fd, l2_table,
> +					l2_table_size * sizeof(u64),
> +					l2_table_offset) < 0)
> +			goto free_l2;
> +
> +		tmp = cpu_to_be64(l2_table_offset);
> +		if (qcow_pwrite_with_sync(q->fd, &tmp, sizeof(tmp),
> +					header->l1_table_offset +
> +					(l1_idx * sizeof(u64))) < 0)
> +			goto free_l2;
> +	}

Maybe it's best to split this in a separate function? 
qcow_write_l2_table() or something?

It would also nice to have some comments explaining what exactly you're 
trying to do with fsync() calls there.

> +
> +	clust_start = be64_to_cpu(l2_table[l2_idx]);
> +	free(l2_table);
> +	if (clust_start) {
> +		if (qcow_pwrite_with_sync(q->fd, buf, length,
> +					clust_start + clust_offset) < 0)
> +			goto error;
> +	} else {
> +		/*
> +		 * Follow the write order to avoid metadata loss
> +		 * 1. write the data at the end of the file
> +		 * 2. update the l2_table with new
> +		 */
> +		clust_start = align(get_file_length(q->fd), clust_size);
> +		if (qcow_pwrite_with_sync(q->fd, buf, length,
> +					clust_start + clust_offset) < 0)
> +			goto error;
> +
> +		tmp = cpu_to_be64(clust_start);
> +		if (qcow_pwrite_with_sync(q->fd, &tmp, sizeof(tmp),
> +					l2_table_offset +
> +					(l2_idx * sizeof(u64))) < 0)
> +			goto error;
> +	}
> +	return length;

You can get rid of some duplication with:

 	clust_start = be64_to_cpu(l2_table[l2_idx]);
 	if (!clust_start) {
 		clust_start	= ALIGN(...);
 		update_metadata = true;
 	}

 	if (qcow_pwrite_with_sync(....) < 0)
 		goto error;

 	if (update_metadata) {
 		// ...
 	}

 			Pekka

      reply	other threads:[~2011-04-17 10:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-15 18:05 [PATCH] kvm tool: QCOW version 1 write support Prasad Joshi
2011-04-17 10:52 ` Pekka Enberg [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.00.1104171345340.5085@tiger \
    --to=penberg@kernel.org \
    --cc=asias.hejun@gmail.com \
    --cc=chaitanyakulkarni15@gmail.com \
    --cc=gorcunov@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=kwolf@redhat.com \
    --cc=levinsasha928@gmail.com \
    --cc=mingo@elte.hu \
    --cc=prasadjoshi124@gmail.com \
    --cc=stefanha@linux.vnet.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox