From patchwork Wed May 11 17:49:02 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pekka Enberg X-Patchwork-Id: 777732 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter2.kernel.org (8.14.4/8.14.3) with ESMTP id p4BHnBKX013301 for ; Wed, 11 May 2011 17:49:11 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752879Ab1EKRtH (ORCPT ); Wed, 11 May 2011 13:49:07 -0400 Received: from filtteri2.pp.htv.fi ([213.243.153.185]:41386 "EHLO filtteri2.pp.htv.fi" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752572Ab1EKRtG (ORCPT ); Wed, 11 May 2011 13:49:06 -0400 Received: from localhost (localhost [127.0.0.1]) by filtteri2.pp.htv.fi (Postfix) with ESMTP id 55CDA1DF13F; Wed, 11 May 2011 20:49:04 +0300 (EEST) X-Virus-Scanned: Debian amavisd-new at pp.htv.fi Received: from smtp4.welho.com ([213.243.153.38]) by localhost (filtteri2.pp.htv.fi [213.243.153.185]) (amavisd-new, port 10024) with ESMTP id oLzfpXhzAmD6; Wed, 11 May 2011 20:49:03 +0300 (EEST) Received: from localhost.localdomain (cs181148025.pp.htv.fi [82.181.148.25]) by smtp4.welho.com (Postfix) with ESMTP id 8D1E35BC005; Wed, 11 May 2011 20:49:03 +0300 (EEST) From: Pekka Enberg To: kvm@vger.kernel.org Cc: Pekka Enberg , Asias He , Avi Kivity , Cyrill Gorcunov , Ingo Molnar , Prasad Joshi , Sasha Levin Subject: [PATCH] kvm tools: QCOW code cleanups Date: Wed, 11 May 2011 20:49:02 +0300 Message-Id: <1305136142-14018-1-git-send-email-penberg@kernel.org> X-Mailer: git-send-email 1.7.0.4 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter2.kernel.org [140.211.167.43]); Wed, 11 May 2011 17:49:11 +0000 (UTC) Fix up coding style issues in QCOW code. Cc: Asias He Cc: Avi Kivity Cc: Cyrill Gorcunov Cc: Ingo Molnar Cc: Prasad Joshi Cc: Sasha Levin Signed-off-by: Pekka Enberg --- tools/kvm/qcow.c | 153 ++++++++++++++++++++++++++++-------------------------- 1 files changed, 80 insertions(+), 73 deletions(-) diff --git a/tools/kvm/qcow.c b/tools/kvm/qcow.c index 103a5a1..f8b52d4 100644 --- a/tools/kvm/qcow.c +++ b/tools/kvm/qcow.c @@ -13,8 +13,8 @@ #include #include -#include #include +#include static inline u64 get_l1_index(struct qcow *q, u64 offset) { @@ -41,16 +41,18 @@ static ssize_t qcow1_read_cluster(struct qcow *q, u64 offset, void *dst, u32 dst { struct qcow_header *header = q->header; struct qcow_table *table = &q->table; - u64 *l2_table = NULL; u64 l2_table_offset; u64 l2_table_size; u64 cluster_size; u64 clust_offset; u64 clust_start; size_t length; + u64 *l2_table; u64 l1_idx; u64 l2_idx; + l2_table = NULL; + cluster_size = 1 << header->cluster_bits; l1_idx = get_l1_index(q, offset); @@ -74,8 +76,7 @@ static ssize_t qcow1_read_cluster(struct qcow *q, u64 offset, void *dst, u32 dst if (!l2_table) goto out_error; - if (pread_in_full(q->fd, l2_table, l2_table_size * sizeof(u64), - l2_table_offset) < 0) + if (pread_in_full(q->fd, l2_table, l2_table_size * sizeof(u64), l2_table_offset) < 0) goto out_error; l2_idx = get_l2_index(q, offset); @@ -102,80 +103,82 @@ out_error: goto out; } -static int qcow1_read_sector(struct disk_image *disk, u64 sector, - void *dst, u32 dst_len) +static int qcow1_read_sector(struct disk_image *disk, u64 sector, void *dst, u32 dst_len) { struct qcow *q = disk->priv; struct qcow_header *header = q->header; - char *buf = dst; - u64 offset; u32 nr_read; + u64 offset; + char *buf; u32 nr; - nr_read = 0; + buf = dst; + nr_read = 0; + while (nr_read < dst_len) { - offset = sector << SECTOR_SHIFT; + offset = sector << SECTOR_SHIFT; if (offset >= header->size) - goto out_error; + return -1; nr = qcow1_read_cluster(q, offset, buf, dst_len - nr_read); if (nr <= 0) - goto out_error; + return -1; nr_read += nr; buf += nr; sector += (nr >> SECTOR_SHIFT); } + return 0; -out_error: - return -1; } static inline u64 file_size(int fd) { struct stat st; + if (fstat(fd, &st) < 0) return 0; + return st.st_size; } -static inline int pwrite_sync(int fd, void *buf, size_t count, off_t offset) +#define SYNC_FLAGS (SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE) + +static inline int qcow_pwrite_sync(int fd, void *buf, size_t count, off_t offset) { if (pwrite_in_full(fd, buf, count, offset) < 0) return -1; - if (sync_file_range(fd, offset, count, - SYNC_FILE_RANGE_WAIT_BEFORE | - SYNC_FILE_RANGE_WRITE) < 0) - return -1; - return 0; + + return sync_file_range(fd, offset, count, SYNC_FLAGS); } /* Writes a level 2 table at the end of the file. */ static u64 qcow1_write_l2_table(struct qcow *q, u64 *table) { struct qcow_header *header = q->header; - u64 sz; u64 clust_sz; - u64 off; u64 f_sz; + u64 off; + u64 sz; - f_sz = file_size(q->fd); + f_sz = file_size(q->fd); if (!f_sz) return 0; - sz = 1 << header->l2_bits; - clust_sz = 1 << header->cluster_bits; - off = ALIGN(f_sz, clust_sz); + sz = 1 << header->l2_bits; + clust_sz = 1 << header->cluster_bits; + off = ALIGN(f_sz, clust_sz); - if (pwrite_sync(q->fd, table, sz * sizeof(u64), off) < 0) + if (qcow_pwrite_sync(q->fd, table, sz * sizeof(u64), off) < 0) return 0; + return off; } /* * QCOW file might grow during a write operation. Not only data but metadata is * also written at the end of the file. Therefore it is necessary to ensure - * every write is committed to disk. Hence we use uses pwrite_sync() to + * every write is committed to disk. Hence we use uses qcow_pwrite_sync() to * synchronize the in-core state of QCOW image to disk. * * We also try to restore the image to a consistent state if the metdata @@ -186,36 +189,35 @@ static ssize_t qcow1_write_cluster(struct qcow *q, u64 offset, void *buf, u32 sr { struct qcow_header *header = q->header; struct qcow_table *table = &q->table; - - u64 l2t_sz; + bool update_meta; + u64 clust_start; + u64 clust_off; u64 clust_sz; u64 l1t_idx; u64 l2t_idx; - u64 clust_off; - u64 len; + u64 l2t_off; + u64 l2t_sz; u64 *l2t; u64 f_sz; - u64 l2t_off; + u64 len; u64 t; - u64 clust_start; - bool update_meta = false; - l2t_sz = 1 << header->l2_bits; - clust_sz = 1 << header->cluster_bits; + l2t_sz = 1 << header->l2_bits; + clust_sz = 1 << header->cluster_bits; - l1t_idx = get_l1_index(q, offset); + l1t_idx = get_l1_index(q, offset); if (l1t_idx >= table->table_size) goto error; - l2t_idx = get_l2_index(q, offset); + l2t_idx = get_l2_index(q, offset); if (l2t_idx >= l2t_sz) goto error; - clust_off = get_cluster_offset(q, offset); + clust_off = get_cluster_offset(q, offset); if (clust_off >= clust_sz) goto error; - len = clust_sz - clust_off; + len = clust_sz - clust_off; if (len > src_len) len = src_len; @@ -223,61 +225,65 @@ static ssize_t qcow1_write_cluster(struct qcow *q, u64 offset, void *buf, u32 sr if (!l2t) goto error; - l2t_off = table->l1_table[l1t_idx] & ~header->oflag_mask; + l2t_off = table->l1_table[l1t_idx] & ~header->oflag_mask; if (l2t_off) { if (pread_in_full(q->fd, l2t, l2t_sz * sizeof(u64), l2t_off) < 0) goto free_l2; } else { - /* capture the state of the consistent QCOW image */ - f_sz = file_size(q->fd); + /* Capture the state of the consistent QCOW image */ + f_sz = file_size(q->fd); if (!f_sz) goto free_l2; /* Write the l2 table of 0's at the end of the file */ - l2t_off = qcow1_write_l2_table(q, l2t); + l2t_off = qcow1_write_l2_table(q, l2t); if (!l2t_off) goto free_l2; /* Metadata update: update on disk level 1 table */ - t = cpu_to_be64(l2t_off); - if (pwrite_sync(q->fd, &t, sizeof(t), header->l1_table_offset + - l1t_idx * sizeof(u64)) < 0) { + t = cpu_to_be64(l2t_off); + + if (qcow_pwrite_sync(q->fd, &t, sizeof(t), header->l1_table_offset + l1t_idx * sizeof(u64)) < 0) { /* restore file to consistent state */ if (ftruncate(q->fd, f_sz) < 0) goto free_l2; + goto free_l2; } - /* update the in-core entry */ + /* Update the in-core entry */ table->l1_table[l1t_idx] = l2t_off; } - /* capture the state of the consistent QCOW image */ - f_sz = file_size(q->fd); + /* Capture the state of the consistent QCOW image */ + f_sz = file_size(q->fd); if (!f_sz) goto free_l2; - clust_start = be64_to_cpu(l2t[l2t_idx]) & ~header->oflag_mask; - free(l2t); + clust_start = be64_to_cpu(l2t[l2t_idx]) & ~header->oflag_mask; if (!clust_start) { - clust_start = ALIGN(f_sz, clust_sz); - update_meta = true; - } + clust_start = ALIGN(f_sz, clust_sz); + update_meta = true; + } else + update_meta = false; - /* write actual data */ + free(l2t); + + /* Write actual data */ if (pwrite_in_full(q->fd, buf, len, clust_start + clust_off) < 0) goto error; if (update_meta) { t = cpu_to_be64(clust_start); - if (pwrite_sync(q->fd, &t, sizeof(t), l2t_off + - l2t_idx * sizeof(u64)) < 0) { - /* restore the file to consistent state */ + if (qcow_pwrite_sync(q->fd, &t, sizeof(t), l2t_off + l2t_idx * sizeof(u64)) < 0) { + /* Restore the file to consistent state */ if (ftruncate(q->fd, f_sz) < 0) goto error; + goto error; } } + return len; free_l2: free(l2t); @@ -289,28 +295,29 @@ static int qcow1_write_sector(struct disk_image *disk, u64 sector, void *src, u3 { struct qcow *q = disk->priv; struct qcow_header *header = q->header; - char *buf = src; - ssize_t nr_write; + ssize_t nr_written; + char *buf; u64 offset; ssize_t nr; - nr_write = 0; - offset = sector << SECTOR_SHIFT; - while (nr_write < src_len) { + buf = src; + nr_written = 0; + offset = sector << SECTOR_SHIFT; + + while (nr_written < src_len) { if (offset >= header->size) - goto error; + return -1; - nr = qcow1_write_cluster(q, offset, buf, src_len - nr_write); + nr = qcow1_write_cluster(q, offset, buf, src_len - nr_written); if (nr < 0) - goto error; + return -1; - nr_write += nr; - buf += nr; - offset += nr; + nr_written += nr; + buf += nr; + offset += nr; } + return 0; -error: - return -1; } static void qcow1_disk_close(struct disk_image *disk)