#include "kvm/disk-image.h"
#include "kvm/read-write.h"
+#include "kvm/mutex.h"
#include "kvm/util.h"
#include <sys/types.h>
l1_idx = get_l1_index(q, offset);
if (l1_idx >= table->table_size)
- goto out_error;
+ return -1;
clust_offset = get_cluster_offset(q, offset);
if (clust_offset >= cluster_size)
- goto out_error;
+ return -1;
length = cluster_size - clust_offset;
if (length > dst_len)
length = dst_len;
+ mutex_lock(&q->mutex);
l2_table_offset = table->l1_table[l1_idx] & ~header->oflag_mask;
if (!l2_table_offset)
goto zero_cluster;
if (!clust_start)
goto zero_cluster;
+ mutex_unlock(&q->mutex);
+
if (pread_in_full(q->fd, dst, length, clust_start + clust_offset) < 0)
- goto out_error;
+ return -1;
-out:
return length;
zero_cluster:
+ mutex_unlock(&q->mutex);
memset(dst, 0, length);
- goto out;
+ return length;
out_error:
+ mutex_unlock(&q->mutex);
length = -1;
- goto out;
+ return -1;
}
static ssize_t qcow_read_sector(struct disk_image *disk, u64 sector, void *dst, u32 dst_len)
l1t_idx = get_l1_index(q, offset);
if (l1t_idx >= table->table_size)
- goto error;
+ return -1;
l2t_idx = get_l2_index(q, offset);
if (l2t_idx >= l2t_sz)
- goto error;
+ return -1;
clust_off = get_cluster_offset(q, offset);
if (clust_off >= clust_sz)
- goto error;
+ return -1;
len = clust_sz - clust_off;
if (len > src_len)
len = src_len;
+ mutex_lock(&q->mutex);
+
l2t_off = table->l1_table[l1t_idx] & ~header->oflag_mask;
if (l2t_off) {
/* read and cache l2 table */
l2t->table[l2t_idx] = clust_start;
}
+ mutex_unlock(&q->mutex);
+
return len;
free_cache:
free(l2t);
error:
+ mutex_unlock(&q->mutex);
return -1;
}
if (!q)
goto error;
+ mutex_init(&q->mutex);
q->fd = fd;
q->root = RB_ROOT;
INIT_LIST_HEAD(&q->lru_list);
if (!q)
goto error;
+ mutex_init(&q->mutex);
q->fd = fd;
q->root = RB_ROOT;
INIT_LIST_HEAD(&q->lru_list);
#define DEFINE_MUTEX(mutex) pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER
+static inline void mutex_init(pthread_mutex_t *mutex)
+{
+ if (pthread_mutex_init(mutex, NULL) != 0)
+ die("unexpected pthread_mutex_init() failure!");
+}
+
static inline void mutex_lock(pthread_mutex_t *mutex)
{
if (pthread_mutex_lock(mutex) != 0)