aboutsummaryrefslogtreecommitdiffstats
path: root/block
diff options
context:
space:
mode:
authorDavid Turner <digit@android.com>2010-09-10 10:15:07 +0200
committerDavid 'Digit' Turner <digit@android.com>2010-09-13 00:30:34 -0700
commita25351325187eb8eff8b9b090acd8f2d7684c6ff (patch)
treea692498d0473066d512d5fc39ad1c6327cb6be81 /block
parent2abe02c0511b2278af9386e7ac5e266d890a38b1 (diff)
downloadexternal_qemu-a25351325187eb8eff8b9b090acd8f2d7684c6ff.zip
external_qemu-a25351325187eb8eff8b9b090acd8f2d7684c6ff.tar.gz
external_qemu-a25351325187eb8eff8b9b090acd8f2d7684c6ff.tar.bz2
upstream: minow hw updates.
Diffstat (limited to 'block')
-rw-r--r--block/vmdk.c296
-rw-r--r--block/vvfat.c58
2 files changed, 184 insertions, 170 deletions
diff --git a/block/vmdk.c b/block/vmdk.c
index f21f02b..2d4ba42 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -76,7 +76,6 @@ typedef struct BDRVVmdkState {
unsigned int cluster_sectors;
uint32_t parent_cid;
- int is_parent;
} BDRVVmdkState;
typedef struct VmdkMetaData {
@@ -87,14 +86,6 @@ typedef struct VmdkMetaData {
int valid;
} VmdkMetaData;
-typedef struct ActiveBDRVState{
- BlockDriverState *hd; // active image handler
- uint64_t cluster_offset; // current write offset
-}ActiveBDRVState;
-
-static ActiveBDRVState activeBDRV;
-
-
static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
{
uint32_t magic;
@@ -117,14 +108,13 @@ static int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename)
static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
{
- BDRVVmdkState *s = bs->opaque;
char desc[DESC_SIZE];
uint32_t cid;
const char *p_name, *cid_str;
size_t cid_str_size;
/* the descriptor offset = 0x200 */
- if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
+ if (bdrv_pread(bs->file, 0x200, desc, DESC_SIZE) != DESC_SIZE)
return 0;
if (parent) {
@@ -145,12 +135,11 @@ static uint32_t vmdk_read_cid(BlockDriverState *bs, int parent)
static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
{
- BDRVVmdkState *s = bs->opaque;
char desc[DESC_SIZE], tmp_desc[DESC_SIZE];
char *p_name, *tmp_str;
/* the descriptor offset = 0x200 */
- if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
+ if (bdrv_pread(bs->file, 0x200, desc, DESC_SIZE) != DESC_SIZE)
return -1;
tmp_str = strstr(desc,"parentCID");
@@ -161,7 +150,7 @@ static int vmdk_write_cid(BlockDriverState *bs, uint32_t cid)
pstrcat(desc, sizeof(desc), tmp_desc);
}
- if (bdrv_pwrite(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
+ if (bdrv_pwrite_sync(bs->file, 0x200, desc, DESC_SIZE) < 0)
return -1;
return 0;
}
@@ -170,7 +159,7 @@ static int vmdk_is_cid_valid(BlockDriverState *bs)
{
#ifdef CHECK_CID
BDRVVmdkState *s = bs->opaque;
- BlockDriverState *p_bs = s->hd->backing_hd;
+ BlockDriverState *p_bs = bs->backing_hd;
uint32_t cur_pcid;
if (p_bs) {
@@ -187,6 +176,7 @@ static int vmdk_is_cid_valid(BlockDriverState *bs)
static int vmdk_snapshot_create(const char *filename, const char *backing_file)
{
int snp_fd, p_fd;
+ int ret;
uint32_t p_cid;
char *p_name, *gd_buf, *rgd_buf;
const char *real_filename, *temp_str;
@@ -211,34 +201,49 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
snp_fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 0644);
if (snp_fd < 0)
- return -1;
+ return -errno;
p_fd = open(backing_file, O_RDONLY | O_BINARY | O_LARGEFILE);
if (p_fd < 0) {
close(snp_fd);
- return -1;
+ return -errno;
}
/* read the header */
- if (lseek(p_fd, 0x0, SEEK_SET) == -1)
+ if (lseek(p_fd, 0x0, SEEK_SET) == -1) {
+ ret = -errno;
goto fail;
- if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE)
+ }
+ if (read(p_fd, hdr, HEADER_SIZE) != HEADER_SIZE) {
+ ret = -errno;
goto fail;
+ }
/* write the header */
- if (lseek(snp_fd, 0x0, SEEK_SET) == -1)
+ if (lseek(snp_fd, 0x0, SEEK_SET) == -1) {
+ ret = -errno;
goto fail;
- if (write(snp_fd, hdr, HEADER_SIZE) == -1)
+ }
+ if (write(snp_fd, hdr, HEADER_SIZE) == -1) {
+ ret = -errno;
goto fail;
+ }
memset(&header, 0, sizeof(header));
memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC
- ftruncate(snp_fd, header.grain_offset << 9);
+ if (ftruncate(snp_fd, header.grain_offset << 9)) {
+ ret = -errno;
+ goto fail;
+ }
/* the descriptor offset = 0x200 */
- if (lseek(p_fd, 0x200, SEEK_SET) == -1)
+ if (lseek(p_fd, 0x200, SEEK_SET) == -1) {
+ ret = -errno;
goto fail;
- if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE)
+ }
+ if (read(p_fd, p_desc, DESC_SIZE) != DESC_SIZE) {
+ ret = -errno;
goto fail;
+ }
if ((p_name = strstr(p_desc,"CID")) != NULL) {
p_name += sizeof("CID");
@@ -257,10 +262,14 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
(uint32_t)header.capacity, real_filename);
/* write the descriptor */
- if (lseek(snp_fd, 0x200, SEEK_SET) == -1)
+ if (lseek(snp_fd, 0x200, SEEK_SET) == -1) {
+ ret = -errno;
goto fail;
- if (write(snp_fd, s_desc, strlen(s_desc)) == -1)
+ }
+ if (write(snp_fd, s_desc, strlen(s_desc)) == -1) {
+ ret = -errno;
goto fail;
+ }
gd_offset = header.gd_offset * SECTOR_SIZE; // offset of GD table
rgd_offset = header.rgd_offset * SECTOR_SIZE; // offset of RGD table
@@ -270,122 +279,100 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file)
* 512 GTE per GT, each GTE points to grain
*/
gt_size = (int64_t)header.num_gtes_per_gte * header.granularity * SECTOR_SIZE;
- if (!gt_size)
+ if (!gt_size) {
+ ret = -EINVAL;
goto fail;
+ }
gde_entries = (uint32_t)(capacity / gt_size); // number of gde/rgde
gd_size = gde_entries * sizeof(uint32_t);
/* write RGD */
rgd_buf = qemu_malloc(gd_size);
- if (lseek(p_fd, rgd_offset, SEEK_SET) == -1)
+ if (lseek(p_fd, rgd_offset, SEEK_SET) == -1) {
+ ret = -errno;
goto fail_rgd;
- if (read(p_fd, rgd_buf, gd_size) != gd_size)
+ }
+ if (read(p_fd, rgd_buf, gd_size) != gd_size) {
+ ret = -errno;
goto fail_rgd;
- if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1)
+ }
+ if (lseek(snp_fd, rgd_offset, SEEK_SET) == -1) {
+ ret = -errno;
goto fail_rgd;
- if (write(snp_fd, rgd_buf, gd_size) == -1)
+ }
+ if (write(snp_fd, rgd_buf, gd_size) == -1) {
+ ret = -errno;
goto fail_rgd;
- qemu_free(rgd_buf);
+ }
/* write GD */
gd_buf = qemu_malloc(gd_size);
- if (lseek(p_fd, gd_offset, SEEK_SET) == -1)
+ if (lseek(p_fd, gd_offset, SEEK_SET) == -1) {
+ ret = -errno;
goto fail_gd;
- if (read(p_fd, gd_buf, gd_size) != gd_size)
+ }
+ if (read(p_fd, gd_buf, gd_size) != gd_size) {
+ ret = -errno;
goto fail_gd;
- if (lseek(snp_fd, gd_offset, SEEK_SET) == -1)
+ }
+ if (lseek(snp_fd, gd_offset, SEEK_SET) == -1) {
+ ret = -errno;
goto fail_gd;
- if (write(snp_fd, gd_buf, gd_size) == -1)
+ }
+ if (write(snp_fd, gd_buf, gd_size) == -1) {
+ ret = -errno;
goto fail_gd;
- qemu_free(gd_buf);
-
- close(p_fd);
- close(snp_fd);
- return 0;
+ }
+ ret = 0;
- fail_gd:
+fail_gd:
qemu_free(gd_buf);
- fail_rgd:
+fail_rgd:
qemu_free(rgd_buf);
- fail:
+fail:
close(p_fd);
close(snp_fd);
- return -1;
+ return ret;
}
-static void vmdk_parent_close(BlockDriverState *bs)
+static int vmdk_parent_open(BlockDriverState *bs)
{
- if (bs->backing_hd)
- bdrv_close(bs->backing_hd);
-}
-
-static int parent_open = 0;
-static int vmdk_parent_open(BlockDriverState *bs, const char * filename)
-{
- BDRVVmdkState *s = bs->opaque;
char *p_name;
char desc[DESC_SIZE];
- char parent_img_name[1024];
/* the descriptor offset = 0x200 */
- if (bdrv_pread(s->hd, 0x200, desc, DESC_SIZE) != DESC_SIZE)
+ if (bdrv_pread(bs->file, 0x200, desc, DESC_SIZE) != DESC_SIZE)
return -1;
if ((p_name = strstr(desc,"parentFileNameHint")) != NULL) {
char *end_name;
- struct stat file_buf;
p_name += sizeof("parentFileNameHint") + 1;
if ((end_name = strchr(p_name,'\"')) == NULL)
return -1;
- if ((end_name - p_name) > sizeof (s->hd->backing_file) - 1)
+ if ((end_name - p_name) > sizeof (bs->backing_file) - 1)
return -1;
- pstrcpy(s->hd->backing_file, end_name - p_name + 1, p_name);
- if (stat(s->hd->backing_file, &file_buf) != 0) {
- path_combine(parent_img_name, sizeof(parent_img_name),
- filename, s->hd->backing_file);
- } else {
- pstrcpy(parent_img_name, sizeof(parent_img_name),
- s->hd->backing_file);
- }
-
- s->hd->backing_hd = bdrv_new("");
- if (!s->hd->backing_hd) {
- failure:
- bdrv_close(s->hd);
- return -1;
- }
- parent_open = 1;
- if (bdrv_open(s->hd->backing_hd, parent_img_name, BDRV_O_RDONLY) < 0)
- goto failure;
- parent_open = 0;
+ pstrcpy(bs->backing_file, end_name - p_name + 1, p_name);
}
return 0;
}
-static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
+static int vmdk_open(BlockDriverState *bs, int flags)
{
BDRVVmdkState *s = bs->opaque;
uint32_t magic;
- int l1_size, i, ret;
+ int l1_size, i;
- if (parent_open)
- // Parent must be opened as RO.
- flags = BDRV_O_RDONLY;
-
- ret = bdrv_file_open(&s->hd, filename, flags);
- if (ret < 0)
- return ret;
- if (bdrv_pread(s->hd, 0, &magic, sizeof(magic)) != sizeof(magic))
+ if (bdrv_pread(bs->file, 0, &magic, sizeof(magic)) != sizeof(magic))
goto fail;
magic = be32_to_cpu(magic);
if (magic == VMDK3_MAGIC) {
VMDK3Header header;
- if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
+ if (bdrv_pread(bs->file, sizeof(magic), &header, sizeof(header)) != sizeof(header))
goto fail;
s->cluster_sectors = le32_to_cpu(header.granularity);
s->l2_size = 1 << 9;
@@ -397,7 +384,7 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
} else if (magic == VMDK4_MAGIC) {
VMDK4Header header;
- if (bdrv_pread(s->hd, sizeof(magic), &header, sizeof(header)) != sizeof(header))
+ if (bdrv_pread(bs->file, sizeof(magic), &header, sizeof(header)) != sizeof(header))
goto fail;
bs->total_sectors = le64_to_cpu(header.capacity);
s->cluster_sectors = le64_to_cpu(header.granularity);
@@ -410,13 +397,8 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
s->l1_table_offset = le64_to_cpu(header.rgd_offset) << 9;
s->l1_backup_table_offset = le64_to_cpu(header.gd_offset) << 9;
- if (parent_open)
- s->is_parent = 1;
- else
- s->is_parent = 0;
-
// try to open parent images, if exist
- if (vmdk_parent_open(bs, filename) != 0)
+ if (vmdk_parent_open(bs) != 0)
goto fail;
// write the CID once after the image creation
s->parent_cid = vmdk_read_cid(bs,1);
@@ -427,7 +409,7 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
/* read the L1 table */
l1_size = s->l1_size * sizeof(uint32_t);
s->l1_table = qemu_malloc(l1_size);
- if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, l1_size) != l1_size)
+ if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, l1_size) != l1_size)
goto fail;
for(i = 0; i < s->l1_size; i++) {
le32_to_cpus(&s->l1_table[i]);
@@ -435,7 +417,7 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
if (s->l1_backup_table_offset) {
s->l1_backup_table = qemu_malloc(l1_size);
- if (bdrv_pread(s->hd, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size)
+ if (bdrv_pread(bs->file, s->l1_backup_table_offset, s->l1_backup_table, l1_size) != l1_size)
goto fail;
for(i = 0; i < s->l1_size; i++) {
le32_to_cpus(&s->l1_backup_table[i]);
@@ -448,7 +430,6 @@ static int vmdk_open(BlockDriverState *bs, const char *filename, int flags)
qemu_free(s->l1_backup_table);
qemu_free(s->l1_table);
qemu_free(s->l2_cache);
- bdrv_delete(s->hd);
return -1;
}
@@ -458,29 +439,28 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
static int get_whole_cluster(BlockDriverState *bs, uint64_t cluster_offset,
uint64_t offset, int allocate)
{
- uint64_t parent_cluster_offset;
BDRVVmdkState *s = bs->opaque;
uint8_t whole_grain[s->cluster_sectors*512]; // 128 sectors * 512 bytes each = grain size 64KB
// we will be here if it's first write on non-exist grain(cluster).
// try to read from parent image, if exist
- if (s->hd->backing_hd) {
- BDRVVmdkState *ps = s->hd->backing_hd->opaque;
+ if (bs->backing_hd) {
+ int ret;
if (!vmdk_is_cid_valid(bs))
return -1;
- parent_cluster_offset = get_cluster_offset(s->hd->backing_hd, NULL, offset, allocate);
-
- if (parent_cluster_offset) {
- BDRVVmdkState *act_s = activeBDRV.hd->opaque;
-
- if (bdrv_pread(ps->hd, parent_cluster_offset, whole_grain, ps->cluster_sectors*512) != ps->cluster_sectors*512)
- return -1;
+ ret = bdrv_read(bs->backing_hd, offset >> 9, whole_grain,
+ s->cluster_sectors);
+ if (ret < 0) {
+ return -1;
+ }
- //Write grain only into the active image
- if (bdrv_pwrite(act_s->hd, activeBDRV.cluster_offset << 9, whole_grain, sizeof(whole_grain)) != sizeof(whole_grain))
- return -1;
+ //Write grain only into the active image
+ ret = bdrv_write(bs->file, cluster_offset, whole_grain,
+ s->cluster_sectors);
+ if (ret < 0) {
+ return -1;
}
}
return 0;
@@ -491,14 +471,14 @@ static int vmdk_L2update(BlockDriverState *bs, VmdkMetaData *m_data)
BDRVVmdkState *s = bs->opaque;
/* update L2 table */
- if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
- &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset))
+ if (bdrv_pwrite_sync(bs->file, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
+ &(m_data->offset), sizeof(m_data->offset)) < 0)
return -1;
/* update backup L2 table */
if (s->l1_backup_table_offset != 0) {
m_data->l2_offset = s->l1_backup_table[m_data->l1_index];
- if (bdrv_pwrite(s->hd, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
- &(m_data->offset), sizeof(m_data->offset)) != sizeof(m_data->offset))
+ if (bdrv_pwrite_sync(bs->file, ((int64_t)m_data->l2_offset * 512) + (m_data->l2_index * sizeof(m_data->offset)),
+ &(m_data->offset), sizeof(m_data->offset)) < 0)
return -1;
}
@@ -545,7 +525,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
}
}
l2_table = s->l2_cache + (min_index * s->l2_size);
- if (bdrv_pread(s->hd, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) !=
+ if (bdrv_pread(bs->file, (int64_t)l2_offset * 512, l2_table, s->l2_size * sizeof(uint32_t)) !=
s->l2_size * sizeof(uint32_t))
return 0;
@@ -558,18 +538,15 @@ static uint64_t get_cluster_offset(BlockDriverState *bs, VmdkMetaData *m_data,
if (!cluster_offset) {
if (!allocate)
return 0;
+
// Avoid the L2 tables update for the images that have snapshots.
- if (!s->is_parent) {
- cluster_offset = bdrv_getlength(s->hd);
- bdrv_truncate(s->hd, cluster_offset + (s->cluster_sectors << 9));
-
- cluster_offset >>= 9;
- tmp = cpu_to_le32(cluster_offset);
- l2_table[l2_index] = tmp;
- // Save the active image state
- activeBDRV.cluster_offset = cluster_offset;
- activeBDRV.hd = bs;
- }
+ cluster_offset = bdrv_getlength(bs->file);
+ bdrv_truncate(bs->file, cluster_offset + (s->cluster_sectors << 9));
+
+ cluster_offset >>= 9;
+ tmp = cpu_to_le32(cluster_offset);
+ l2_table[l2_index] = tmp;
+
/* First of all we write grain itself, to avoid race condition
* that may to corrupt the image.
* This problem may occur because of insufficient space on host disk
@@ -621,17 +598,17 @@ static int vmdk_read(BlockDriverState *bs, int64_t sector_num,
n = nb_sectors;
if (!cluster_offset) {
// try to read from parent image, if exist
- if (s->hd->backing_hd) {
+ if (bs->backing_hd) {
if (!vmdk_is_cid_valid(bs))
return -1;
- ret = bdrv_read(s->hd->backing_hd, sector_num, buf, n);
+ ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
if (ret < 0)
return -1;
} else {
memset(buf, 0, 512 * n);
}
} else {
- if(bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
+ if(bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
return -1;
}
nb_sectors -= n;
@@ -667,7 +644,7 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
if (!cluster_offset)
return -1;
- if (bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
+ if (bdrv_pwrite(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512) != n * 512)
return -1;
if (m_data.valid) {
/* update L2 tables */
@@ -715,6 +692,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options)
int64_t total_size = 0;
const char *backing_file = NULL;
int flags = 0;
+ int ret;
// Read out options
while (options && options->name) {
@@ -736,7 +714,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options)
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
0644);
if (fd < 0)
- return -1;
+ return -errno;
magic = cpu_to_be32(VMDK4_MAGIC);
memset(&header, 0, sizeof(header));
header.version = cpu_to_le32(1);
@@ -771,22 +749,44 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options)
header.check_bytes[3] = 0xa;
/* write all the data */
- write(fd, &magic, sizeof(magic));
- write(fd, &header, sizeof(header));
+ ret = qemu_write_full(fd, &magic, sizeof(magic));
+ if (ret != sizeof(magic)) {
+ ret = -errno;
+ goto exit;
+ }
+ ret = qemu_write_full(fd, &header, sizeof(header));
+ if (ret != sizeof(header)) {
+ ret = -errno;
+ goto exit;
+ }
- ftruncate(fd, header.grain_offset << 9);
+ ret = ftruncate(fd, header.grain_offset << 9);
+ if (ret < 0) {
+ ret = -errno;
+ goto exit;
+ }
/* write grain directory */
lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET);
for (i = 0, tmp = header.rgd_offset + gd_size;
- i < gt_count; i++, tmp += gt_size)
- write(fd, &tmp, sizeof(tmp));
+ i < gt_count; i++, tmp += gt_size) {
+ ret = qemu_write_full(fd, &tmp, sizeof(tmp));
+ if (ret != sizeof(tmp)) {
+ ret = -errno;
+ goto exit;
+ }
+ }
/* write backup grain directory */
lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET);
for (i = 0, tmp = header.gd_offset + gd_size;
- i < gt_count; i++, tmp += gt_size)
- write(fd, &tmp, sizeof(tmp));
+ i < gt_count; i++, tmp += gt_size) {
+ ret = qemu_write_full(fd, &tmp, sizeof(tmp));
+ if (ret != sizeof(tmp)) {
+ ret = -errno;
+ goto exit;
+ }
+ }
/* compose the descriptor */
real_filename = filename;
@@ -803,10 +803,16 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options)
/* write the descriptor */
lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET);
- write(fd, desc, strlen(desc));
+ ret = qemu_write_full(fd, desc, strlen(desc));
+ if (ret != strlen(desc)) {
+ ret = -errno;
+ goto exit;
+ }
+ ret = 0;
+exit:
close(fd);
- return 0;
+ return ret;
}
static void vmdk_close(BlockDriverState *bs)
@@ -815,15 +821,11 @@ static void vmdk_close(BlockDriverState *bs)
qemu_free(s->l1_table);
qemu_free(s->l2_cache);
- // try to close parent image, if exist
- vmdk_parent_close(s->hd);
- bdrv_delete(s->hd);
}
static void vmdk_flush(BlockDriverState *bs)
{
- BDRVVmdkState *s = bs->opaque;
- bdrv_flush(s->hd);
+ bdrv_flush(bs->file);
}
@@ -850,7 +852,7 @@ static BlockDriver bdrv_vmdk = {
.format_name = "vmdk",
.instance_size = sizeof(BDRVVmdkState),
.bdrv_probe = vmdk_probe,
- .bdrv_open = vmdk_open,
+ .bdrv_open = vmdk_open,
.bdrv_read = vmdk_read,
.bdrv_write = vmdk_write,
.bdrv_close = vmdk_close,
diff --git a/block/vvfat.c b/block/vvfat.c
index 1e37b9f..de50dc7 100644
--- a/block/vvfat.c
+++ b/block/vvfat.c
@@ -379,7 +379,7 @@ static void init_mbr(BDRVVVFATState* s)
{
/* TODO: if the files mbr.img and bootsect.img exist, use them */
mbr_t* real_mbr=(mbr_t*)s->first_sectors;
- partition_t* partition=&(real_mbr->partition[0]);
+ partition_t* partition = &(real_mbr->partition[0]);
int lba;
memset(s->first_sectors,0,512);
@@ -512,7 +512,7 @@ static inline uint8_t fat_chksum(const direntry_t* entry)
for(i=0;i<11;i++) {
unsigned char c;
- c = (i <= 8) ? entry->name[i] : entry->extension[i-8];
+ c = (i < 8) ? entry->name[i] : entry->extension[i-8];
chksum=(((chksum&0xfe)>>1)|((chksum&0x01)?0x80:0)) + c;
}
@@ -526,7 +526,7 @@ static uint16_t fat_datetime(time_t time,int return_time) {
t=localtime(&time); /* this is not thread safe */
#else
struct tm t1;
- t=&t1;
+ t = &t1;
localtime_r(&time,t);
#endif
if(return_time)
@@ -868,7 +868,8 @@ static int init_directories(BDRVVVFATState* s,
{
direntry_t* entry=array_get_next(&(s->directory));
entry->attributes=0x28; /* archive | volume label */
- snprintf((char*)entry->name,11,"QEMU VVFAT");
+ memcpy(entry->name,"QEMU VVF",8);
+ memcpy(entry->extension,"AT ",3);
}
/* Now build FAT, and write back information into directory */
@@ -882,7 +883,7 @@ static int init_directories(BDRVVVFATState* s,
mapping->dir_index = 0;
mapping->info.dir.parent_mapping_index = -1;
mapping->first_mapping_index = -1;
- mapping->path = strdup(dirname);
+ mapping->path = qemu_strdup(dirname);
i = strlen(mapping->path);
if (i > 0 && mapping->path[i - 1] == '/')
mapping->path[i - 1] = '\0';
@@ -1098,8 +1099,8 @@ static inline void vvfat_close_current_file(BDRVVVFATState *s)
*/
static inline int find_mapping_for_cluster_aux(BDRVVVFATState* s,int cluster_num,int index1,int index2)
{
- int index3=index1+1;
while(1) {
+ int index3;
mapping_t* mapping;
index3=(index1+index2)/2;
mapping=array_get(&(s->mapping),index3);
@@ -1243,7 +1244,7 @@ static void print_direntry(const direntry_t* direntry)
int j = 0;
char buffer[1024];
- fprintf(stderr, "direntry 0x%x: ", (int)direntry);
+ fprintf(stderr, "direntry %p: ", direntry);
if(!direntry)
return;
if(is_long_name(direntry)) {
@@ -1272,7 +1273,11 @@ static void print_direntry(const direntry_t* direntry)
static void print_mapping(const mapping_t* mapping)
{
- fprintf(stderr, "mapping (0x%x): begin, end = %d, %d, dir_index = %d, first_mapping_index = %d, name = %s, mode = 0x%x, " , (int)mapping, mapping->begin, mapping->end, mapping->dir_index, mapping->first_mapping_index, mapping->path, mapping->mode);
+ fprintf(stderr, "mapping (%p): begin, end = %d, %d, dir_index = %d, "
+ "first_mapping_index = %d, name = %s, mode = 0x%x, " ,
+ mapping, mapping->begin, mapping->end, mapping->dir_index,
+ mapping->first_mapping_index, mapping->path, mapping->mode);
+
if (mapping->mode & MODE_DIRECTORY)
fprintf(stderr, "parent_mapping_index = %d, first_dir_index = %d\n", mapping->info.dir.parent_mapping_index, mapping->info.dir.first_dir_index);
else
@@ -1632,12 +1637,12 @@ static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,
/* rename */
if (strcmp(basename, basename2))
- schedule_rename(s, cluster_num, strdup(path));
+ schedule_rename(s, cluster_num, qemu_strdup(path));
} else if (is_file(direntry))
/* new file */
- schedule_new_file(s, strdup(path), cluster_num);
+ schedule_new_file(s, qemu_strdup(path), cluster_num);
else {
- assert(0);
+ abort();
return 0;
}
}
@@ -1658,7 +1663,7 @@ static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,
if (offset != mapping->info.file.offset + s->cluster_size
* (cluster_num - mapping->begin)) {
/* offset of this cluster in file chain has changed */
- assert(0);
+ abort();
copy_it = 1;
} else if (offset == 0) {
const char* basename = get_basename(mapping->path);
@@ -1670,7 +1675,7 @@ static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,
if (mapping->first_mapping_index != first_mapping_index
&& mapping->info.file.offset > 0) {
- assert(0);
+ abort();
copy_it = 1;
}
@@ -1752,10 +1757,10 @@ static int check_directory_consistency(BDRVVVFATState *s,
mapping->mode &= ~MODE_DELETED;
if (strcmp(basename, basename2))
- schedule_rename(s, cluster_num, strdup(path));
+ schedule_rename(s, cluster_num, qemu_strdup(path));
} else
/* new directory */
- schedule_mkdir(s, cluster_num, strdup(path));
+ schedule_mkdir(s, cluster_num, qemu_strdup(path));
lfn_init(&lfn);
do {
@@ -1836,7 +1841,7 @@ DLOG(fprintf(stderr, "check direntry %d: \n", i); print_direntry(direntries + i)
goto fail;
}
} else
- assert(0); /* cluster_count = 0; */
+ abort(); /* cluster_count = 0; */
ret += cluster_count;
}
@@ -2256,7 +2261,11 @@ static int commit_one_file(BDRVVVFATState* s,
c = c1;
}
- ftruncate(fd, size);
+ if (ftruncate(fd, size)) {
+ perror("ftruncate()");
+ close(fd);
+ return -4;
+ }
close(fd);
return commit_mappings(s, first_cluster, dir_index);
@@ -2453,14 +2462,17 @@ static int handle_commits(BDRVVVFATState* s)
commit_t* commit = array_get(&(s->commits), i);
switch(commit->action) {
case ACTION_RENAME: case ACTION_MKDIR:
- assert(0);
+ abort();
fail = -2;
break;
case ACTION_WRITEOUT: {
+#ifndef NDEBUG
+ /* these variables are only used by assert() below */
direntry_t* entry = array_get(&(s->directory),
commit->param.writeout.dir_index);
uint32_t begin = begin_of_direntry(entry);
mapping_t* mapping = find_mapping_for_cluster(s, begin);
+#endif
assert(mapping);
assert(mapping->begin == begin);
@@ -2511,7 +2523,7 @@ static int handle_commits(BDRVVVFATState* s)
break;
}
default:
- assert(0);
+ abort();
}
}
if (i > 0 && array_remove_slice(&(s->commits), 0, i))
@@ -2599,7 +2611,7 @@ static int do_commit(BDRVVVFATState* s)
ret = handle_renames_and_mkdirs(s);
if (ret) {
fprintf(stderr, "Error handling renames (%d)\n", ret);
- assert(0);
+ abort();
return ret;
}
@@ -2610,21 +2622,21 @@ static int do_commit(BDRVVVFATState* s)
ret = commit_direntries(s, 0, -1);
if (ret) {
fprintf(stderr, "Fatal: error while committing (%d)\n", ret);
- assert(0);
+ abort();
return ret;
}
ret = handle_commits(s);
if (ret) {
fprintf(stderr, "Error handling commits (%d)\n", ret);
- assert(0);
+ abort();
return ret;
}
ret = handle_deletes(s);
if (ret) {
fprintf(stderr, "Error deleting\n");
- assert(0);
+ abort();
return ret;
}