Fast Virtual Disk (FVD) Proposal Part 1
Subsystems (2)
PATCH 1/5 Fast Virtual Disk (FVD) Proposal Part 1
Part 1 of the block device driver for the proposed FVD image format. Multiple patches are used in order to manage the size of each patch. This patch includes existing files that are modified by FVD.
See the related discussions at
http://lists.gnu.org/archive/html/qemu-devel/2011-01/msg00426.html .
--- Makefile | 10 +++++--- Makefile.objs | 1 + block.c | 12 +++++----- block_int.h | 5 ++- configure | 2 +- qemu-img-cmds.hx | 6 +++++ qemu-img.c | 62 ++++++++++++++++++++++++++++++++++++++++++++--------- qemu-io.c | 3 ++ qemu-option.c | 4 +++ qemu-tool.c | 36 ------------------------------- 10 files changed, 81 insertions(+), 60 deletions(-)
Makefile +6 -4
@@ -151,13 +151,15 @@ version-obj-$(CONFIG_WIN32) += version.o ###################################################################### qemu-img.o: qemu-img-cmds.h -qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o cmd.o: $(GENERATED_HEADERS) +qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o cmd.o qemu-test.o: $(GENERATED_HEADERS) -qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o +qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-tool-time.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o -qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o +qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-tool-time.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o -qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o +qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-tool-time.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o + +qemu-test$(EXESUF): qemu-test.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@," GEN $@")
Makefile.objs +1 -0
@@ -23,6 +23,7 @@ block-nested-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o block-nested-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o block-nested-y += qed-check.o block-nested-y += parallels.o nbd.o blkdebug.o sheepdog.o blkverify.o +block-nested-y += blksim.o fvd.o block-nested-$(CONFIG_WIN32) += raw-win32.o block-nested-$(CONFIG_POSIX) += raw-posix.o block-nested-$(CONFIG_CURL) += curl.o
block.c +6 -6
@@ -58,7 +58,7 @@ static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,static int bdrv_write_em (BlockDriverState *bs,int64_t sector_num,const uint8_t *buf,int nb_sectors); -static QTAILQ_HEAD(, BlockDriverState) bdrv_states = +QTAILQ_HEAD(, BlockDriverState) bdrv_states =QTAILQ_HEAD_INITIALIZER (bdrv_states); static QLIST_HEAD(, BlockDriver) bdrv_drivers =
@@ -768,7 +768,7 @@ int bdrv_commit(BlockDriverState *bs)if (!drv)return -ENOMEDIUM ; - +if (!bs->backing_hd) {return -ENOTSUP ; }
@@ -1538,7 +1538,7 @@ int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)* 'nb_sectors' is the max value 'pnum' should be set to. */ int bdrv_is_allocated (BlockDriverState *bs,int64_t sector_num,int nb_sectors, -int *pnum) +int *pnum) {int64_t n;if (!bs->drv->bdrv_is_allocated) {
@@ -2050,9 +2050,9 @@ BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, cb, opaque);if (ret) { -/* Update stats even though technically transfer has not happened. */ - bs->rd_bytes += (unsigned ) nb_sectors *BDRV_SECTOR_SIZE ; - bs->rd_ops ++; +/* Update stats even though technically transfer has not happened. */ + bs->rd_bytes += (unsigned ) nb_sectors *BDRV_SECTOR_SIZE ; + bs->rd_ops ++; }return ret;
block_int.h +3 -2
@@ -28,8 +28,8 @@#include "qemu-option.h" #include "qemu-queue.h" -#define BLOCK_FLAG_ENCRYPT 1 -#define BLOCK_FLAG_COMPAT6 4 +#define BLOCK_FLAG_ENCRYPT 1 +#define BLOCK_FLAG_COMPAT6 4#define BLOCK_OPT_SIZE "size"#define BLOCK_OPT_ENCRYPT "encryption"
@@ -98,6 +98,7 @@ struct BlockDriver {int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,const char *snapshot_name);int (*bdrv_get_info)(BlockDriverState *bs,BlockDriverInfo *bdi); +int (*bdrv_update)(BlockDriverState *bs,int argc,char **argv);int (*bdrv_save_vmstate)(BlockDriverState *bs,const uint8_t *buf,int64_t pos,int size);
configure +1 -1
@@ -2362,7 +2362,7 @@ confdir=$sysconfdir$confsuffix tools= if test "$softmmu" = yes ; then - tools="qemu-img\$(EXESUF) qemu-io\$(EXESUF) $tools" + tools="qemu-img\$(EXESUF) qemu-io\$(EXESUF) qemu-test\$(EXESUF) $tools" if [ "$linux" = "yes" -o "$bsd" = "yes" -o "$solaris" = "yes" ] ; then tools="qemu-nbd\$(EXESUF) $tools" if [ "$check_utests" = "yes" ]; then
qemu-img-cmds.hx +6 -0
@@ -39,6 +39,12 @@ STEXI @item info [-f @var{fmt}] @var{filename} ETEXI +DEF("update", img_update, + "update [-f fmt] filename [attr1=val1 attr2=val2 ...]") +STEXI +@item update [-f @var{fmt}] @var{filename} [@var{attr1=val1 attr2=val2 ...}]") +ETEXI + DEF("snapshot", img_snapshot, "snapshot [-l | -a snapshot | -c snapshot | -d snapshot] filename") STEXI
qemu-img.c +51 -11
97 lines not on this page · raw message
qemu-io.c +3 -0
@@ -1701,6 +1701,8 @@ init_check_command(return 1 ; } +#include "qemu-io-sim.c" +static void usage (const char *name) { printf(
@@ -1807,6 +1809,7 @@ int main(int argc, char **argv)add_command (&discard_cmd);add_command (&alloc_cmd);add_command (&map_cmd); +add_command (&sim_cmd);add_args_command (init_args_command);add_check_command (init_check_command);
qemu-option.c +4 -0
@@ -289,6 +289,10 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name,return -1 ;break ; +case OPT_NUMBER : + list->value.n =atoi (value); +break ; +default :fprintf (stderr,"Bug: Option '%s' has an unknown type\n" , name);return -1 ;
qemu-tool.c +0 -36
@@ -23,12 +23,6 @@ QEMUClock *rt_clock;FILE *logfile; -struct QEMUBH -{ -QEMUBHFunc *cb; -void *opaque; -}; -void qemu_service_io (void ) { }
@@ -73,36 +67,6 @@ void monitor_protocol_event(MonitorEvent event, QObject *data) { } -QEMUBH *qemu_bh_new (QEMUBHFunc *cb,void *opaque) -{ -QEMUBH *bh; - - bh =qemu_malloc (sizeof (*bh)); - bh->cb = cb; - bh->opaque = opaque; - -return bh; -} - -int qemu_bh_poll (void ) -{ -return 0 ; -} - -void qemu_bh_schedule (QEMUBH *bh) -{ - bh->cb (bh->opaque); -} - -void qemu_bh_cancel (QEMUBH *bh) -{ -} - -void qemu_bh_delete (QEMUBH *bh) -{ -qemu_free (bh); -} - int qemu_set_fd_handler2(int fd, IOCanReadHandler *fd_read_poll, IOHandler *fd_read,
Signature · 1 line
1.7.0.4
PATCH 2/5 Fast Virtual Disk (FVD) Proposal Part 2
Part 2 of the block device driver for the proposed FVD image format. Multiple patches are used in order to manage the size of each patch. This patch includes the new testing tools developed together with FVD.
See the related discussions at
http://lists.gnu.org/archive/html/qemu-devel/2011-01/msg00426.html .
--- block/blksim.c | 688 ++++++++++++++++++++++++++++++++++++++++++++++ block/blksim.h | 30 ++ qemu-io-sim.c | 107 ++++++++ qemu-test.c | 794 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ qemu-tool-time.c | 88 ++++++ test-fvd.sh | 120 ++++++++ test-qcow2.sh | 75 +++++ 7 files changed, 1902 insertions(+), 0 deletions(-) create mode 100644 block/blksim.c create mode 100644 block/blksim.h create mode 100644 qemu-io-sim.c create mode 100644 qemu-test.c create mode 100644 qemu-tool-time.c create mode 100755 test-fvd.sh create mode 100755 test-qcow2.sh
block/blksim.c new file +688 -0
@@ -0,0 +1,688 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this module implements a simulated block device +* driver "blksim". It works with qemu-io and qemu-test to perform testing, +* allowing changing the order of disk I/O and callback activities to test +* rare race conditions. See qemu-test.c, qemu-io.c, and qemu-io-sim.c. +*============================================================================*/ + +#include <sys/vfs.h> +#include <sys/mman.h> +#include <pthread.h> +#include <execinfo.h> +#include <stdlib.h> +#include <sys/ioctl.h> +#include <stdint.h> +#include <stdio.h> +#include <inttypes.h> +#include "block_int.h" +#include "osdep.h" +#include "qemu-option.h" +#include "qemu-timer.h" +#include "block.h" +#include "qemu-queue.h" +#include "qemu-common.h" +#include "block/blksim.h" +#include "block/fvd-ext.h" + +typedef enum { +SIM_NULL , +SIM_READ , +SIM_WRITE , +SIM_FLUSH , +SIM_READ_CALLBACK , +SIM_WRITE_CALLBACK , +SIM_FLUSH_CALLBACK , +SIM_TIMER +}sim_op_t ; + +static void sim_aio_cancel (BlockDriverAIOCB * acb); +static int64_t sim_uuid =0 ; +static int64_t current_time =0 ; +static int64_t rand_time =0 ; +static int interactive_print = FALSE; +struct SimAIOCB ; + +/* +* Note: disk_io_return_code, set_disk_io_return_code(), and insert_task() work +* together to ensure that multiple subrequests triggered by the same +* outtermost request either succeed together or fail together. This behavior +* is required by qemu-test. Here is one example of problems caused by +* departuring from this behavior. Consider a write request that generates +* two subrequests, w1 and w2. If w1 succeeds but w2 fails, the data will not +* be written into qemu-test's "truth image" but the part of the data handled +* by w1 will be written into qemu-test's "test image". As a result, their +* contents diverge can automated testing cannot continue. +*/ +static int disk_io_return_code =0 ; + +typedef struct BDRVSimState { +int fd; +}BDRVSimState ; + +typedef struct SimAIOCB { +BlockDriverAIOCB common; +int64_t uuid; +sim_op_t op; +int64_t sector_num; +QEMUIOVector *qiov; +int nb_sectors; +int ret; +int64_t time; +struct SimAIOCB *next; +struct SimAIOCB *prev; + +}SimAIOCB ; + +static AIOPool sim_aio_pool = { + .aiocb_size =sizeof (SimAIOCB), + .cancel = sim_aio_cancel, +}; + +static SimAIOCB head = { + .uuid =-1 , + .time = (int64_t ) (9223372036854775807ULL ), + .op =SIM_NULL , + .next = &head, + .prev = &head, +}; + +/* Debug a specific task.*/ +#if 1 +# define CHECK_TASK (acb) do { } while (0) +#else +static inline void CHECK_TASK (int64_t uuid) +{ +if (uuid ==19LL ) { +printf ("CHECK_TASK pause for task %" PRId64"\n" , uuid); + } +} +#endif + +/* do_io() should never fail. A failure indicates a bug in the upper layer +* block device driver, or failure in the real hardware. */ +static int do_io (BlockDriverState * bs,int64_t sector_num,uint8_t * buf, +int nb_sectors,int do_read) +{ +BDRVSimState *s = bs->opaque; +size_t size = nb_sectors *512 ; +int ret; + +if (lseek (s->fd, sector_num *512 ,SEEK_SET ) <0 ) { +fprintf (stderr,"Error: lseek %s sector_num=%" PRId64". " +"Pause process %d for debugging...\n" , + bs->filename, sector_num,getpid ()); +fgetc (stdin); + } + +while (size >0 ) { + +if (do_read) { + ret =read (s->fd, buf, size); +if (ret ==0 ) { +fprintf (stderr, +"Error: read beyond the size of %s sector_num=%" PRId64 +" nb_sectors=%d. Pause process %d for debugging...\n" , + bs->filename, sector_num, nb_sectors,getpid ()); +fgetc (stdin); + } + }else { + ret =write (s->fd, buf, size); + } + +if (ret >=0 ) { + size -= ret; + buf += ret; + }else if (errno !=EINTR ) { +fprintf (stderr,"Error: %s %s sector_num=%" PRId64 +" nb_sectors=%d. Pause process %d for debugging...\n" , + do_read ?"READ" :"WRITE" , bs->filename, sector_num, + nb_sectors,getpid ()); +fgetc (stdin); +return -errno; + } + } + +return 0 ; +} + +static int sim_read (BlockDriverState * bs,int64_t sector_num,uint8_t * buf, +int nb_sectors) +{ +return do_io (bs, sector_num, buf, nb_sectors, TRUE); +} + +static int sim_write (BlockDriverState * bs,int64_t sector_num, +const uint8_t * buf,int nb_sectors) +{ +return do_io (bs, sector_num, (uint8_t *) buf, nb_sectors, FALSE); +} + +static void insert_in_list (SimAIOCB * acb) +{ +int64_t new_id = sim_uuid++; +CHECK_TASK (new_id); + acb->uuid = new_id; + +if (rand_time <=0 ) { +/* Working with qemu-io.c and not doing delay randomization. +* Insert it to the tail. */ + acb->time =0 ; + acb->prev = head.prev; + acb->next = &head; + head.prev->next = acb; + head.prev = acb; +return ; + } + +if (acb->time >=0 ) { +/* Introduce a random delay to better trigger rare race conditions. */ + acb->time +=random () % rand_time; + } + +/* Find the position to insert. The list is sorted in ascending time. */ +SimAIOCB *p = head.next; +while (1 ) { +if (p->time > acb->time) { +break ; + } +if (p->time == acb->time && (random () %2 ==0 )) { +break ; + } + p = p->next; + } + +/* Insert acb before p. */ + acb->next = p; + acb->prev = p->prev; + p->prev->next = acb; + p->prev = acb; +} + +/* Debug problems related to reusing task objects. Problem already solved.*/ +#if 1 +# define my_qemu_aio_get qemu_aio_get +# define my_qemu_aio_release qemu_aio_release + +#else +static SimAIOCB *search_task_list (SimAIOCB * acb) +{ +SimAIOCB *p; +for (p = head.next; p != &head; p = p->next) { +if (p == acb) { +return p; + } + } + +return NULL ; +} + +static inline void *my_qemu_aio_get (AIOPool * pool,BlockDriverState * bs, +BlockDriverCompletionFunc * cb, +void *opaque) +{ +SimAIOCB *acb = (SimAIOCB *)qemu_aio_get (&sim_aio_pool, bs, cb, opaque); +QDEBUG ("SIM: qemu_aio_get reuse old task%" PRId64"\n" , acb->uuid); +ASSERT (!search_task_list (acb)); +return acb; +} + +static inline void my_qemu_aio_release (SimAIOCB * acb) +{ +QDEBUG ("SIM: qemu_aio_release task%" PRId64"\n" , acb->uuid); +qemu_aio_release (acb); +} +#endif + +static BlockDriverAIOCB *insert_task (int op,BlockDriverState * bs, +int64_t sector_num,QEMUIOVector * qiov, +int nb_sectors, +BlockDriverCompletionFunc * cb, +void *opaque) +{ +SimAIOCB *acb =my_qemu_aio_get (&sim_aio_pool, bs, cb, opaque); +if (!acb) { +return NULL ; + } + + acb->op = op; + acb->sector_num = sector_num; + acb->qiov = qiov; + acb->nb_sectors = nb_sectors; + acb->ret = disk_io_return_code; + acb->time = current_time; +insert_in_list (acb); + +if (interactive_print) { +if (op ==SIM_READ ) { +printf ("Added READ uuid=%" PRId64" filename=%s sector_num=%" + PRId64" nb_sectors=%d\n" , acb->uuid, + acb->common.bs->filename, acb->sector_num, acb->nb_sectors); + }else if (op ==SIM_WRITE ) { +printf ("Added WRITE uuid=%" PRId64" filename=%s sector_num=%" + PRId64" nb_sectors=%d\n" , acb->uuid, + acb->common.bs->filename, acb->sector_num, acb->nb_sectors); + }else { +fprintf (stderr,"Unknown op %d\n" , op); +exit (1 ); + } + } + +return &acb->common; +} + +static void insert_aio_callback (SimAIOCB * acb) +{ + acb->time = current_time; +insert_in_list (acb); + +if (acb->op ==SIM_FLUSH ) { + acb->op =SIM_FLUSH_CALLBACK ; +if (interactive_print) { +printf ("Added FLUSH_CALLBACK uuid=%" PRId64" filename=%s\n" , + acb->uuid, acb->common.bs->filename); + } + }else if (acb->op ==SIM_READ ) { + acb->op =SIM_READ_CALLBACK ; +if (interactive_print) { +printf ("Added READ_CALLBACK uuid=%" PRId64 +" filename=%s sector_num=%" PRId64" nb_sectors=%d\n" , + acb->uuid, acb->common.bs->filename, acb->sector_num, + acb->nb_sectors); + } + }else if (acb->op ==SIM_WRITE ) { + acb->op =SIM_WRITE_CALLBACK ; +if (interactive_print) { +printf ("Added WRITE_CALLBACK uuid=%" PRId64 +" filename=%s sector_num=%" PRId64" nb_sectors=%d\n" , + acb->uuid, acb->common.bs->filename, acb->sector_num, + acb->nb_sectors); + } + }else { +fprintf (stderr,"Wrong op %d\n" , acb->op); +exit (1 ); + } +} + +void sim_list_tasks (void ) +{ +SimAIOCB *acb; + +for (acb = head.next; acb != &head; acb = acb->next) { +if (acb->op ==SIM_READ ) { +printf ("uuid=%" PRId64" READ file=%s sector_num=%" + PRIu64" nb_sectors=%d\n" , acb->uuid, + acb->common.bs->filename, acb->sector_num, acb->nb_sectors); + }else if (acb->op ==SIM_WRITE ) { +printf ("uuid=%" PRId64" WRITE file=%s sector_num=%" + PRIu64" nb_sectors=%d\n" , acb->uuid, + acb->common.bs->filename, acb->sector_num, acb->nb_sectors); + }else if (acb->op ==SIM_READ_CALLBACK ) { +printf ("uuid=%" PRId64" CALLBACK READ file=%s sector_num=%" + PRIu64" nb_sectors=%d\n" , acb->uuid, + acb->common.bs->filename, acb->sector_num, acb->nb_sectors); + }else if (acb->op ==SIM_WRITE_CALLBACK ) { +printf ("uuid=%" PRId64" CALLBACK WRITE file=%s sector_num=%" + PRIu64" nb_sectors=%d\n" , acb->uuid, + acb->common.bs->filename, acb->sector_num, acb->nb_sectors); + }else { +fprintf (stderr,"Wrong OP %d\n" , acb->op); +exit (1 ); + } + } +} + +static inline void sim_callback (SimAIOCB * acb) +{ +ASSERT (disk_io_return_code ==0 ); +FVD_DEBUG_ACB (acb->common.opaque); + acb->common.cb (acb->common.opaque, acb->ret); +} + +int64_t sim_get_time (void ) +{ +return current_time; +} + +void *sim_new_timer (void *cb,void *opaque) +{ +SimAIOCB *acb =my_qemu_aio_get (&sim_aio_pool,NULL , cb, opaque); + acb->op =SIM_TIMER ; + acb->prev =NULL ; +return acb; +} + +void sim_mod_timer (void *ts,int64_t expire_time) +{ +SimAIOCB *acb = ts; + +if (acb->prev) { +/* Remove it first. */ + acb->next->prev = acb->prev; + acb->prev->next = acb->next; + } + acb->time = expire_time; +insert_in_list (acb); +} + +void sim_free_timer (void *ts) +{ +SimAIOCB *acb = ts; +CHECK_TASK (acb->uuid); +my_qemu_aio_release (acb); +} + +void sim_del_timer (void *ts) +{ +SimAIOCB *acb = ts; + +CHECK_TASK (acb->uuid); +if (acb->prev) { +/* Remove it from the list. */ + acb->next->prev = acb->prev; + acb->prev->next = acb->next; + +/* Mark it as not in list. */ + acb->prev =NULL ; + } +} + +void sim_set_disk_io_return_code (int ret) +{ + disk_io_return_code = ret; +} + +static void sim_task_by_acb (SimAIOCB * acb) +{ +CHECK_TASK (acb->uuid); + +/* Remove it from the list. */ + acb->next->prev = acb->prev; + acb->prev->next = acb->next; + acb->prev =NULL ;/* Indicate that it is no longer in the list. */ + +if (acb->time > current_time) { + current_time = acb->time; + } + +if (acb->op ==SIM_TIMER ) { +QDEBUG ("SIM: execute task%" PRId64" time=%" PRId64" TIMER \n" , + acb->uuid, acb->time); + +FVD_DEBUG_ACB (acb->common.opaque); + ((QEMUTimerCB *) acb->common.cb) (acb->common.opaque); +return ; + } + +BlockDriverState *bs = acb->common.bs; + +if (acb->op ==SIM_READ ) { +QDEBUG ("SIM: execute task%" PRId64" time=%" PRId64 +" READ sector_num=%" PRId64" nb_sectors=%d\n" , + acb->uuid, acb->time, acb->sector_num, acb->nb_sectors); + +if (acb->ret ==0 ) { +if (acb->qiov->niov ==1 ) { +if (sim_read + (bs, acb->sector_num, acb->qiov->iov->iov_base, + acb->nb_sectors) !=0 ) { +fprintf (stderr,"Error in reading %s sector_num=%lld " +"nb_sectors=%d\n" , acb->common.bs->filename, + acb->sector_num, acb->nb_sectors); +exit (1 ); + } + }else { +uint8_t *buf = +qemu_blockalign (acb->common.bs, acb->qiov->size); +if (sim_read (bs, acb->sector_num, buf, acb->nb_sectors) !=0 ) { +fprintf (stderr,"Error in reading %s sector_num=%lld " +"nb_sectors=%d\n" , acb->common.bs->filename, + acb->sector_num, acb->nb_sectors); +exit (1 ); + } +qemu_iovec_from_buffer (acb->qiov, buf, acb->qiov->size); +qemu_vfree (buf); + } + } + +insert_aio_callback (acb); + }else if (acb->op ==SIM_WRITE ) { +QDEBUG ("SIM: execute task%" PRId64" time=%" PRId64 +" WRITE sector_num=%" PRId64" nb_sectors=%d\n" , + acb->uuid, acb->time, acb->sector_num, acb->nb_sectors); + +if (acb->ret ==0 ) { +if (acb->qiov->niov ==1 ) { +if (sim_write + (bs, acb->sector_num, acb->qiov->iov->iov_base, + acb->nb_sectors) !=0 ) { +fprintf (stderr,"Error in writing %s sector_num=%lld " +"nb_sectors=%d\n" , acb->common.bs->filename, + acb->sector_num, acb->nb_sectors); +exit (1 ); + } + }else { +uint8_t *buf =qemu_blockalign (acb->common.bs, + acb->qiov->size); +qemu_iovec_to_buffer (acb->qiov, buf); +if (sim_write (bs, acb->sector_num, buf, acb->nb_sectors)!=0 ) { +fprintf (stderr,"Error in writing %s sector_num=%lld " +"nb_sectors=%d\n" , acb->common.bs->filename, + acb->sector_num, acb->nb_sectors); +exit (1 ); + } +qemu_vfree (buf); + } + } + +insert_aio_callback (acb); + }else if (acb->op ==SIM_FLUSH ) { +QDEBUG ("SIM: execute task%" PRId64" time=%" PRId64" FLUSH\n" , + acb->uuid, acb->time); +/* Skip real flushing to speed up simulation: +* if (ret == 0) { * fdatasync (s->fd); } */ +insert_aio_callback (acb); + }else if (acb->op ==SIM_WRITE_CALLBACK || acb->op ==SIM_READ_CALLBACK + || acb->op ==SIM_FLUSH_CALLBACK ) { +QDEBUG ("SIM: execute task%" PRId64" time=%" PRId64" CALLBACK\n" , + acb->uuid, acb->time); +sim_callback (acb); +CHECK_TASK (acb->uuid); +my_qemu_aio_release (acb); + }else { +fprintf (stderr,"Unknown op %d\n" , acb->op); +exit (1 ); + } +} + +int sim_task_by_uuid (int64_t uuid) +{ +SimAIOCB *acb; + +for (acb = head.next; acb != &head; acb = acb->next) { +if (acb->uuid == uuid) { +sim_task_by_acb (acb); +return 0 ; + } + } + +return -1 ; +} + +int sim_all_tasks (void ) +{ +int n =0 ; + +while (1 ) { +SimAIOCB *acb = head.next; +if (acb == &head) { +return n; + } + +sim_task_by_acb (acb); + n++; + } +} + +static BlockDriverAIOCB *sim_aio_readv (BlockDriverState * bs, +int64_t sector_num, +QEMUIOVector * qiov, +int nb_sectors, +BlockDriverCompletionFunc * cb, +void *opaque) +{ +return insert_task (SIM_READ , bs, sector_num, qiov, nb_sectors, cb, opaque); +} + +static BlockDriverAIOCB *sim_aio_writev (BlockDriverState * bs, +int64_t sector_num, +QEMUIOVector * qiov, +int nb_sectors, +BlockDriverCompletionFunc * cb, +void *opaque) +{ +return insert_task (SIM_WRITE , bs, sector_num, qiov, nb_sectors, cb, + opaque); +} + +static BlockDriverAIOCB *sim_aio_flush (BlockDriverState * bs, +BlockDriverCompletionFunc * cb, +void *opaque) +{ +return insert_task (SIM_FLUSH , bs,0 ,NULL ,0 , cb, opaque); +} + +static void sim_aio_cancel (BlockDriverAIOCB * blockacb) +{ +SimAIOCB *acb =container_of (blockacb, SimAIOCB, common); + +CHECK_TASK (acb->uuid); + +if (acb->prev) { + acb->next->prev = acb->prev; + acb->prev->next = acb->next; + acb->prev =NULL ; +my_qemu_aio_release (acb); + }else { +ASSERT (FALSE);/* Cancel a task not in the list. */ + } +} + +static int sim_probe (const uint8_t * buf,int buf_size,const char *filename) +{ +/* Return a score higher than RAW so that the image will be openned using +* the 'sim' format. */ +return 2 ; +} + +static int sim_open (BlockDriverState * bs,const char *filename, +int bdrv_flags) +{ +BDRVSimState *s = bs->opaque; +int open_flags =O_BINARY |O_LARGEFILE ; + +if ((bdrv_flags &BDRV_O_RDWR )) { + open_flags |=O_RDWR ; + }else { + open_flags |=O_RDONLY ; + } + +if ((bdrv_flags &BDRV_O_NOCACHE )) { + open_flags |=O_DIRECT ; + }else if (!(bdrv_flags &BDRV_O_CACHE_WB )) { + open_flags |=O_DSYNC ; + } + +/* Parse the "blksim:" prefix */ +if (!strncmp (filename,"blksim:" ,strlen ("blksim:" ))) { + filename +=strlen ("blksim:" ); + } + + s->fd =open (filename, open_flags); +if (s->fd <0 ) +return -1 ; + +int64_t len =lseek (s->fd,0 ,SEEK_END ); +if (len >=0 ) { + bs->total_sectors = len /512 ; + }else { + bs->total_sectors =0 ; + } + + bs->growable =1 ; +return 0 ; +} + +static void sim_close (BlockDriverState * bs) +{ +BDRVSimState *s = bs->opaque; +close (s->fd); +} + +static int sim_flush (BlockDriverState * bs) +{ +/* +* Skip real flushing to speed up simulation. +* BDRVSimState *s = bs->opaque; +* fdatasync (s->fd); +*/ +return 0 ; +} + +static int sim_has_zero_init (BlockDriverState * bs) +{ +struct stat buf; + +if (stat (bs->filename, &buf) !=0 ) { +fprintf (stderr,"Failed to stat() %s\n" , bs->filename); +exit (1 ); + } + +if (S_ISBLK (buf.st_mode) ||S_ISCHR (buf.st_mode)) { +return 0 ; + } + +return 1 ; +} + +static int sim_truncate (BlockDriverState * bs,int64_t offset) +{ +BDRVSimState *s = bs->opaque; +return ftruncate (s->fd, offset); +} + +BlockDriver bdrv_sim = { + .format_name ="blksim" , + .protocol_name ="blksim" , + .instance_size =sizeof (BDRVSimState), + .bdrv_probe = sim_probe, + .bdrv_file_open = sim_open, + .bdrv_close = sim_close, + .bdrv_flush = sim_flush, + .bdrv_read = sim_read, + .bdrv_write = sim_write, + .bdrv_aio_readv = sim_aio_readv, + .bdrv_aio_writev = sim_aio_writev, + .bdrv_aio_flush = sim_aio_flush, + .bdrv_has_zero_init = sim_has_zero_init, + .bdrv_truncate = sim_truncate, +}; + +void enable_block_sim (int print,int64_t _rand_time) +{ +BlockDriver *drv =bdrv_find_format ("blksim" ); +if (!drv) { +bdrv_register (&bdrv_sim); + } + interactive_print = print; + rand_time = _rand_time; +}
block/blksim.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this is the header of the simulated block device +* driver "sim". +*============================================================================*/ + +#ifndef __block_sim_h__ +#define __block_sim_h__ + +void enable_block_sim (int print,int64_t _rand_time); +void sim_list_tasks (void ); +int sim_task_by_uuid (int64_t uuid); +int sim_all_tasks (void ); +int64_t sim_get_time (void ); +void *sim_new_timer (void *cb,void *opaque); +void sim_mod_timer (void *ts,int64_t expire_time); +void sim_free_timer (void *ts); +void sim_del_timer (void *ts); +void sim_set_disk_io_return_code (int ret); + +#endif
qemu-io-sim.c new file +107 -0
107 lines not on this page · raw message
qemu-test.c new file +794 -0
794 lines not on this page · raw message
qemu-tool-time.c new file +88 -0
88 lines not on this page · raw message
test-fvd.sh new file +120 -0
120 lines not on this page · raw message
test-qcow2.sh new file +75 -0
75 lines not on this page · raw message
Signature · 1 line
1.7.0.4
PATCH 4/5 Fast Virtual Disk (FVD) Proposal Part 4
In reply to Chunqiang TangPart 4 of the block device driver for the proposed FVD image format. Multiple patches are used in order to manage the size of each patch. This patch includes some new files for FVD.
See the related discussions at
http://lists.gnu.org/archive/html/qemu-devel/2011-01/msg00426.html .
--- block/fvd-journal.c | 558 +++++++++++++++++++++++++++++++++++++++++++++ block/fvd-load.c | 364 +++++++++++++++++++++++++++++ block/fvd-misc.c | 616 ++++++++++++++++++++++++++++++++++++++++++++++++++ block/fvd-open.c | 446 ++++++++++++++++++++++++++++++++++++ block/fvd-prefetch.c | 598 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 2582 insertions(+), 0 deletions(-) create mode 100644 block/fvd-journal.c create mode 100644 block/fvd-load.c create mode 100644 block/fvd-misc.c create mode 100644 block/fvd-open.c create mode 100644 block/fvd-prefetch.c
block/fvd-journal.c new file +558 -0
@@ -0,0 +1,558 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this FVD module implements a journal for committing +* metadata changes. Each sector in the journal is self-contained so that +* updates are atomic. A sector may contain one or multiple journal records. +* There are two types of journal records: +* bitmap_update and table_update. +* Format of a bitmap_update record: +* + BITMAP_JRECORD (uint32_t) +* + num_dirty_sectors (uint32_t) +* + dirty_sector_begin (int64_t) +* Format of a table_update record: +* + TABLE_JRECORD (uint32_t) +* + dirty_table_offset (uint32_t) +* + num_dirty_table_entries (uint32_t) +* + table_entry_1 (uint32_t) +* + table_entry_2 (uint32_t) +* + ... +* If both the bitmap and the table need update, one sector contains a +* TABLE_JRECORD and a BITMAP_JRECORD, and these two records cover +* the same range of virtual disk data so that the corresponding parts of the +* bitmap and the table are always updated in one atomic operation. +*============================================================================*/ + +#define BITMAP_JRECORD ((uint32_t)0x3F2AB8ED) +#define TABLE_JRECORD ((uint32_t)0xB4E6F7AC) +#define EMPTY_JRECORD ((uint32_t)0) +#define BITMAP_JRECORD_SIZE (2*sizeof(uint32_t) + sizeof(int64_t)) +#define TABLE_JRECORD_HDR_SIZE (3*sizeof(uint32_t)) +#define TABLE_JRECORDS_PER_SECTOR \ + ((512 - TABLE_JRECORD_HDR_SIZE)/sizeof(uint32_t)) + +/* One BITMAP_JRECORD and this number of BITMAP_JRECORDs can fit +* in one journal sector. */ +#define MIXED_JRECORDS_PER_SECTOR ((512 - TABLE_JRECORD_HDR_SIZE - \ + BITMAP_JRECORD_SIZE) / sizeof(uint32_t)) + +static inline int64_t calc_min_journal_size (int64_t table_entries) +{ +return (table_entries +MIXED_JRECORDS_PER_SECTOR -1 ) + /MIXED_JRECORDS_PER_SECTOR *512 ; +} + +static int init_journal (int read_only,BlockDriverState * bs, +FvdHeader * header) +{ +/* A trick to figure out whether it is runningin a qemu tool. */ +const int in_qemu_tool = (rt_clock ==NULL ); + +BDRVFvdState *s = bs->opaque; + s->journal_size = header->journal_size /512 ; + s->journal_offset = header->journal_offset /512 ; + s->next_journal_sector =0 ; + +if (read_only) { +return 0 ; + } + +if (s->journal_size <=0 ) { +if (!s->table && !s->fresh_bitmap) { +return 0 ;/* No need to use the journal. */ + } + +if (!header->clean_shutdown) { +fprintf (stderr,"ERROR: the image may be corrupted because it was " +"not shut down gracefully last\ntime and it does not use " +"a journal. You may continue to use the image at your\n" +"own risk by manually resetting the clean_shutdown flag " +"in the image.\n\n" ); + s->dirty_image = TRUE; +if (in_qemu_tool) { +return 0 ;/* Allow qemu tools to use the image. */ + }else { +/* Do not allow boot the VM until the clean_shutdown flag is +* manually cleaned. */ +return -1 ; + } + } + +QDEBUG ("Journal is disabled\n" ); +return 0 ; + } + +if (header->clean_shutdown) { +QDEBUG ("Journal is skipped as the VM was shut down gracefully " +"last time.\n" ); +return 0 ; + } + +QDEBUG ("Recover from the journal as the VM was not shut down gracefully " +"last time.\n" ); + +uint8_t *journal =my_qemu_blockalign (s->fvd_metadata, + s->journal_size *512 ); +int ret =bdrv_read (s->fvd_metadata, s->journal_offset, + journal, s->journal_size); +if (ret <0 ) { +my_qemu_vfree (journal); +fprintf (stderr,"Failed to read the journal (%" PRId64") bytes\n" , + s->journal_size *512 ); +return -1 ; + } + +/* Go through every journal sector. */ +uint8_t *sector = journal; +uint8_t *journal_end = journal + s->journal_size *512 ; +while (sector < journal_end) { +uint32_t *type = (uint32_t *) sector;/* Journal record type. */ +while ((uint8_t *) type < (sector +512 )) { +if (le32_to_cpu (*type) ==BITMAP_JRECORD ) { +uint32_t *nb_sectors = type +1 ;/* BITMAP_JRECORD field 2. */ +int64_t *sector_num = (int64_t *) (type +2 );/* field 3. */ +if (s->stale_bitmap) { +update_both_bitmaps (s,le64_to_cpu (*sector_num), +le32_to_cpu (*nb_sectors)); +QDEBUG ("JOURNAL: recover BITMAP_JRECORD sector_num=%" + PRId64" nb_sectors=%u\n" , +le64_to_cpu (*sector_num), +le32_to_cpu (*nb_sectors)); + } + +/* First field of the next journal record. */ + type = (uint32_t *) sector_num +1 ; + }else if (le32_to_cpu (*type) ==TABLE_JRECORD ) { +uint32_t *offset = type +1 ;/* TABLE_JRECORD field 2. */ +uint32_t *count = type +2 ;/* TABLE_JRECORD field 3. */ +uint32_t *content = type +3 ;/* fields 4 and beyond. */ +const uint32_t chunk =le32_to_cpu (*offset); +const uint32_t n =le32_to_cpu (*count); +uint32_t i; +for (i =0 ; i < n; i++) { + s->table[chunk + i] = content[i]; + +/* The dirty bit was not cleaned when the table entry was +* saved in the journal. */ +CLEAN_DIRTY2 (s->table[chunk + i]); + } + type = content + n;/* First field of the next record. */ +QDEBUG ("JOURNAL: recover TABLE_JRECORD chunk_start=%u " +"nb_chunks=%u\n" , chunk, n); + }else { +/* End of valid records in this journal sector. */ +ASSERT (le32_to_cpu (*type) ==EMPTY_JRECORD ); +break ; + } + } + + sector +=512 ; + } +my_qemu_vfree (journal); +flush_metadata_to_disk (bs);/* Write the recovered metadata. */ + +return 0 ; +} + +/* +* This function first flushes in-memory metadata to disk and then recycle the +* used journal sectors. It is possible to make this operation asynchronous so +* that the performance is better. However, the overall performance +* improvement may be limited since recycling the journal happens very +* infrequently and updating on-disk metadata finishes quickly because of the +* small size of the metadata. +*/ +static void recycle_journal (BDRVFvdState * s) +{ +#ifdef FVD_DEBUG +static int64_t recycle_count =0 ; +QDEBUG ("JOURNAL: start journal recycle %" PRId64".\n" , recycle_count); + recycle_count++; +int64_t begin_time =qemu_get_clock (rt_clock); +#endif + +/* Write fresh_bitmap to disk. */ +if (s->fresh_bitmap) { +int nb = (int ) (s->bitmap_size /512 ); +QDEBUG ("JOURNAL: flush bitmap (%d sectors) to disk\n" , nb); + +/* How to recover if this write fails? */ +bdrv_write (s->fvd_metadata, s->bitmap_offset, s->fresh_bitmap, nb); + +if (s->fresh_bitmap != s->stale_bitmap) { +memcpy (s->stale_bitmap, s->fresh_bitmap, s->bitmap_size); + } + } + +/* Clean DIRTY_TABLE bit and write the table to disk. */ +if (s->table) { +int table_entries = + (int ) (ROUND_UP (s->virtual_disk_size, s->chunk_size *512 ) / + (s->chunk_size *512 )); +int i; +for (i =0 ; i < table_entries; i++) { +CLEAN_DIRTY (s->table[i]); + } + +int64_t table_size =sizeof (uint32_t ) * table_entries; + table_size =ROUND_UP (table_size,DEF_PAGE_SIZE ); +int nb = (int ) (table_size /512 ); +QDEBUG ("JOURNAL: flush table (%d sectors) to disk\n" , nb); + +/* How to recover if this write fails? */ +bdrv_write (s->fvd_metadata, s->table_offset, (uint8_t *) s->table, nb); + } + s->next_journal_sector =0 ; + +#ifdef FVD_DEBUG +int64_t end_time =qemu_get_clock (rt_clock); +QDEBUG ("JOURNAL: journal recycle took %" PRId64" ms.\n" , + (end_time - begin_time)); +#endif +} + +static void free_journal_sectors (BDRVFvdState * s) +{ +if (s->journal_size <=0 ) { +return ; + } + + s->ongoing_journal_updates--; +ASSERT (s->ongoing_journal_updates >=0 ); +if (s->ongoing_journal_updates >0 ||QLIST_EMPTY (&s->wait_for_journal)) { +return ; + } + +/* Some requests are waiting for the journal to be recycled in order to +* get free journal sectors. */ +recycle_journal (s); + +/* Restart requests in the wait_for_journal list. First make a copy of +* the head and then empty the head. */ +FvdAIOCB *acb =QLIST_FIRST (&s->wait_for_journal); +QLIST_INIT (&s->wait_for_journal); +FvdAIOCB *next; + +/* Restart all dependent requests. Cannot use QLIST_FOREACH here, because +* the next link might not be the same any more after the callback. */ +while (acb) { + next = acb->jcb.next_wait_for_journal.le_next; + acb->jcb.next_wait_for_journal.le_prev =NULL ; +QDEBUG ("WRITE: acb%llu-%p restart_write_metadata_to_journal " +"after recycle_journal\n" , acb->uuid, acb); +write_metadata_to_journal (acb); + acb = next; + } +} + +static int64_t allocate_journal_sectors (BDRVFvdState * s,FvdAIOCB * acb, +int num_sectors) +{ +ASSERT (num_sectors <= s->journal_size); + +if (!QLIST_EMPTY (&s->wait_for_journal)) { +/* Waiting for journal recycle to finish. */ +ASSERT (s->ongoing_journal_updates >0 ); +QDEBUG ("WRITE: acb%llu-%p wait_for_journal_recycle\n" , + acb->uuid, acb); +QLIST_INSERT_HEAD (&s->wait_for_journal, acb, + jcb.next_wait_for_journal); +return -1 ; + } + +int64_t journal_sec; +if (s->next_journal_sector + num_sectors <= s->journal_size) { + alloc_sector: + journal_sec = s->next_journal_sector; + s->next_journal_sector += num_sectors; + s->ongoing_journal_updates++; +return journal_sec; + } + +/* No free journal sector is available. Check if the journal can be +* recycled now. */ +if (s->ongoing_journal_updates ==0 ) { +recycle_journal (s); + goto alloc_sector; + } + +/* Waiting for journal recycle to finish. It will be waken up later in +* free_journal_sectors(). */ +QLIST_INSERT_HEAD (&s->wait_for_journal, acb, jcb.next_wait_for_journal); +QDEBUG ("WRITE: acb%llu-%p wait_for_journal_recycle\n" , acb->uuid, acb); +return -1 ; +} + +static void finish_write_journal (void *opaque,int ret) +{ +FvdAIOCB *acb = (FvdAIOCB *) opaque; +BlockDriverState *bs = acb->common.bs; +BDRVFvdState *s = bs->opaque; + +if (ret ==0 ) { +QDEBUG ("JOURNAL: acb%llu-%p finish_write_journal\n" , acb->uuid, acb); + +if (s->table) { +/* Update the table. */ +int i; +const uint32_t first_chunk = acb->sector_num / s->chunk_size; +const uint32_t last_chunk = (acb->sector_num + acb->nb_sectors -1 ) + / s->chunk_size; +for (i = first_chunk; i <= last_chunk; i++) { +CLEAN_DIRTY2 (s->table[i]); + } + } + +if (s->stale_bitmap) { +/* If fresh_bitmap differs from stale_bitmap, fresh_bitmap has +* already been updated in finish_write_data() when invoking +* update_fresh_bitmap_and_check_stale_bitmap(). */ +update_stale_bitmap (s, acb->sector_num, acb->nb_sectors); + } + }else { +QDEBUG ("JOURNAL: acb%llu-%p finish_write_journal error ret=%d\n" , + acb->uuid, acb, ret); + } + +/* Clean up. */ +if (acb->type ==OP_STORE_COMPACT ) { + acb->common.cb (acb->common.opaque, ret); +if (acb->jcb.iov.iov_base !=NULL ) { +my_qemu_vfree (acb->jcb.iov.iov_base); + } +my_qemu_aio_release (acb); + }else { +ASSERT (acb->type ==OP_WRITE ); +finish_write (acb, ret); + } + +free_journal_sectors (s); +} + +static void write_metadata_to_journal (FvdAIOCB * acb) +{ +BlockDriverState *bs = acb->common.bs; +BDRVFvdState *s = bs->opaque; +int64_t journal_sec; +int num_journal_sectors; + +ASSERT ((s->table || s->fresh_bitmap) + && (acb->type ==OP_WRITE || acb->type ==OP_STORE_COMPACT )); + +/* Is journal is disabled? */ +if (s->journal_size <=0 ) { +finish_write_journal (acb,0 ); +return ; + } + +if (!s->table) { +/* Only update the bitmap. */ + num_journal_sectors =1 ; + journal_sec =allocate_journal_sectors (s, acb, num_journal_sectors); +if (journal_sec <0 ) { +/* No journal sector is available now. It will be waken up later +* in free_journal_sectors(). */ +return ; + } + acb->jcb.iov.iov_len =512 ; + acb->jcb.iov.iov_base =my_qemu_blockalign (s->fvd_metadata,512 ); + +uint32_t *type = (uint32_t *) acb->jcb.iov.iov_base;/* Field 1. */ +uint32_t *nb_sectors = type +1 ;/* BITMAP_JRECORD field 2. */ +int64_t *sector_num = (int64_t *) (type +2 );/* field 3. */ + *type =cpu_to_le32 (BITMAP_JRECORD ); + *nb_sectors =cpu_to_le32 ((uint32_t ) acb->nb_sectors); + *sector_num =cpu_to_le64 (acb->sector_num); + *((uint32_t *) (sector_num +1 )) =EMPTY_JRECORD ;/* Mark record end. */ + + }else if (!s->fresh_bitmap) { +/* Only update the table. */ +const int64_t first_chunk = acb->sector_num / s->chunk_size; +const int64_t last_chunk = (acb->sector_num + acb->nb_sectors -1 ) + / s->chunk_size; +int num_chunks = last_chunk - first_chunk +1 ; + num_journal_sectors = (num_chunks +TABLE_JRECORDS_PER_SECTOR -1 ) + /TABLE_JRECORDS_PER_SECTOR ; + journal_sec =allocate_journal_sectors (s, acb, num_journal_sectors); +if (journal_sec <0 ) { +/* No journal sector is available now. It will be waken up later +* in free_journal_sectors(). */ +return ; + } + + acb->jcb.iov.iov_len = num_journal_sectors *512 ; + acb->jcb.iov.iov_base =my_qemu_blockalign (s->fvd_metadata, + acb->jcb.iov.iov_len); + +uint32_t *type = (uint32_t *) acb->jcb.iov.iov_base;/* Field 1. */ +int64_t chunk = first_chunk; + +while (1 ) { +/* Start a new journal sector. */ +uint32_t *offset = type +1 ;/* TABLE_JRECORD field 2. */ +uint32_t *count = type +2 ;/* TABLE_JRECORD field 3. */ +uint32_t *content = type +3 ;/* Fields 4 and beyond. */ + *type =cpu_to_le32 (TABLE_JRECORD ); + *offset =cpu_to_le32 (chunk); + +if (num_chunks <=TABLE_JRECORDS_PER_SECTOR ) { +/* This is the last journal sector. */ + *count =cpu_to_le32 (num_chunks); +memcpy (content, &s->table[chunk], +sizeof (uint32_t ) * num_chunks); +if (num_chunks <TABLE_JRECORDS_PER_SECTOR ) { + *(content + num_chunks) =EMPTY_JRECORD ;/* Mark end. */ + } +break ; + } + + *count =cpu_to_le32 (TABLE_JRECORDS_PER_SECTOR ); +memcpy (content, &s->table[chunk], +sizeof (uint32_t ) *TABLE_JRECORDS_PER_SECTOR ); + chunk +=TABLE_JRECORDS_PER_SECTOR ; + num_chunks -=TABLE_JRECORDS_PER_SECTOR ; + +/* Next TABLE_JRECORD field 1. */ + type = content +TABLE_JRECORDS_PER_SECTOR ; + } + }else { +/* Update both the table and the bitmap. It may use multiple journal +* sectors. Each sector is self-contained, including a TABLE_JRECORD +* and a BITMAP_JRECORD. The two records one the same sector cover the +* same range of virtual disk data. The purpose is to update the +* corresponding parts of the bitmap and the table in one atomic +* operation. */ +const int64_t first_chunk = acb->sector_num / s->chunk_size; +const int64_t last_chunk = (acb->sector_num + acb->nb_sectors -1 ) + / s->chunk_size; +int num_chunks = last_chunk - first_chunk +1 ; + num_journal_sectors = (num_chunks +MIXED_JRECORDS_PER_SECTOR -1 ) + /MIXED_JRECORDS_PER_SECTOR ; + journal_sec =allocate_journal_sectors (s, acb, num_journal_sectors); +if (journal_sec <0 ) { +/* No journal sector is available now. It will be waken up later +* in free_journal_sectors(). */ +return ; + } + acb->jcb.iov.iov_len = num_journal_sectors *512 ; + acb->jcb.iov.iov_base =my_qemu_blockalign (s->fvd_metadata, + acb->jcb.iov.iov_len); + +uint32_t *type = (uint32_t *) acb->jcb.iov.iov_base;/* Field 1. */ +int64_t chunk = first_chunk; +int64_t sector_num = acb->sector_num; +uint32_t nb_sectors; +if (num_journal_sectors ==1 ) { + nb_sectors = acb->nb_sectors; + }else { +/* Number of sectors that fall into the first chunk. */ + nb_sectors = (first_chunk +MIXED_JRECORDS_PER_SECTOR ) + * s->chunk_size - acb->sector_num; + } + +while (1 ) { +/* Start a new journal sector. */ +uint32_t *offset = type +1 ;/* TABLE_JRECORD field 2. */ +uint32_t *count = type +2 ;/* TABLE_JRECORD field 3. */ +uint32_t *content = type +3 ;/* Fields 4 and beyond. */ + *type =cpu_to_le32 (TABLE_JRECORD ); + *offset =cpu_to_le32 (chunk); + +if (num_chunks <=MIXED_JRECORDS_PER_SECTOR ) { +/* This is the last journal sector. */ + *count =cpu_to_le32 (num_chunks); +memcpy (content, &s->table[chunk], +sizeof (uint32_t ) * num_chunks); + +/* A BITMAP_JRECORD follows a TABLE_JRECORD so that they are +* updated in one atomic operatoin. */ + type = content + num_chunks;/* BITMAP_JRECORD field 1. */ +uint32_t *p_nb_sectors = type +1 ;/* BITMAP_JRECORD field 2. */ +int64_t *p_sector_num = (int64_t *) (type +2 );/* Field 3. */ + *type =cpu_to_le32 (BITMAP_JRECORD ); + *p_nb_sectors =cpu_to_le32 (nb_sectors); + *p_sector_num =cpu_to_le64 (sector_num); + +if (num_chunks <MIXED_JRECORDS_PER_SECTOR ) { + *((uint32_t *) (p_sector_num +1 )) =EMPTY_JRECORD ;/*End*/ + } +break ; + } + + *count =cpu_to_le32 (MIXED_JRECORDS_PER_SECTOR ); +memcpy (content, &s->table[chunk], +sizeof (uint32_t ) *MIXED_JRECORDS_PER_SECTOR ); + +/* A BITMAP_JRECORD follows a TABLE_JRECORD so that they are +* updated in one atomic operatoin. */ + type = content +MIXED_JRECORDS_PER_SECTOR ;/* Field 1. */ +uint32_t *p_nb_sectors = type +1 ;/* BITMAP_JRECORD field 2. */ +int64_t *p_sector_num = (int64_t *) (type +2 );/* Field 3. */ + *type =cpu_to_le32 (BITMAP_JRECORD ); + *p_nb_sectors =cpu_to_le32 (nb_sectors); + *p_sector_num =cpu_to_le64 (sector_num); + +/* Prepare for the next journal sector. */ + type = (uint32_t *) (p_sector_num +1 ); + chunk +=MIXED_JRECORDS_PER_SECTOR ; + sector_num = chunk * s->chunk_size; + num_chunks -=MIXED_JRECORDS_PER_SECTOR ; +if (num_chunks <=MIXED_JRECORDS_PER_SECTOR ) { +/* Data sectors covered by the last journal sector. */ + nb_sectors = (acb->sector_num + acb->nb_sectors) + - chunk * s->chunk_size; + }else { + nb_sectors = s->chunk_size *MIXED_JRECORDS_PER_SECTOR ; + } + } + } + +QDEBUG ("JOURNAL: acb%llu-%p write_metadata_to_journal journal_sec=%" + PRId64" nb_journal_sectors=%d\n" , acb->uuid, acb, journal_sec, + num_journal_sectors); +qemu_iovec_init_external (&acb->jcb.qiov, &acb->jcb.iov,1 ); + acb->jcb.hd_acb =bdrv_aio_writev (s->fvd_metadata, + s->journal_offset + journal_sec, + &acb->jcb.qiov, num_journal_sectors, + finish_write_journal, acb); +if (!acb->jcb.hd_acb) { +finish_write_journal (acb,-1 ); + } +} + +#ifdef FVD_DEBUG +static int emulate_host_crash = TRUE; +#else +static int emulate_host_crash = FALSE; +#endif + +static void flush_metadata_to_disk_on_exit (BlockDriverState *bs) +{ +BDRVFvdState *s = bs->opaque; + +if (bs->read_only || !s->fvd_metadata) { +return ; + } + +/* If (emulate_host_crash==TRUE), do not flush metadata to disk +* so that it has to rely on journal for recovery. */ +if (s->journal_size <=0 || !emulate_host_crash) { +flush_metadata_to_disk (bs); +if (!s->dirty_image) { +update_clean_shutdown_flag (s, TRUE); + } + } +} + +void fvd_enable_host_crash_test (void ) +{ + emulate_host_crash = TRUE; +}
block/fvd-load.c new file +364 -0
364 lines not on this page · raw message
block/fvd-misc.c new file +616 -0
616 lines not on this page · raw message
block/fvd-open.c new file +446 -0
446 lines not on this page · raw message
block/fvd-prefetch.c new file +598 -0
598 lines not on this page · raw message
Signature · 1 line
1.7.0.4
PATCH 3/5 Fast Virtual Disk (FVD) Proposal Part 3
In reply to Chunqiang TangPart 3 of the block device driver for the proposed FVD image format. Multiple patches are used in order to manage the size of each patch. This patch includes some new files for FVD.
See the related discussions at
http://lists.gnu.org/archive/html/qemu-devel/2011-01/msg00426.html .
--- block/fvd-create.c | 475 +++++++++++++++++++++++++++++++++++++++++++++++++++ block/fvd-debug.c | 406 ++++++++++++++++++++++++++++++++++++++++++++ block/fvd-ext.h | 71 ++++++++ block/fvd.c | 127 ++++++++++++++ block/fvd.h | 481 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1560 insertions(+), 0 deletions(-) create mode 100644 block/fvd-create.c create mode 100644 block/fvd-debug.c create mode 100644 block/fvd-ext.h create mode 100644 block/fvd.c create mode 100644 block/fvd.h
block/fvd-create.c new file +475 -0
@@ -0,0 +1,475 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this module implements bdrv_create() for FVD. +*============================================================================*/ + +static inline int64_t calc_min_journal_size (int64_t table_entries); +static inline int search_holes (const char *filename,size_t bitmap_size, +int32_t bitmap_start_offset,BlockDriverState * bs, +int64_t nb_sectors,int32_t hole_size,int32_t block_size); + +static int fvd_create (const char *filename,QEMUOptionParameter * options) +{ +int fd, ret; +FvdHeader *header; +int64_t virtual_disk_size =DEF_PAGE_SIZE ; +int32_t header_size; +const char *base_img =NULL ; +const char *base_img_fmt =NULL ; +const char *data_file =NULL ; +const char *data_file_fmt =NULL ; +int32_t hole_size =0 ; +int copy_on_read = FALSE; +int prefetch_start_delay =-1 ; +int64_t prefetch_profile_size =0 ; +BlockDriverState *bs =NULL ; +int bitmap_size =0 ; +int64_t base_img_size =0 ; +int64_t table_size =0 ; +int64_t journal_size =0 ; +int32_t block_size =0 ; + + header_size =sizeof (FvdHeader); + header_size =ROUND_UP (header_size,DEF_PAGE_SIZE ); + header =my_qemu_mallocz (header_size); + +/* Read out options */ +while (options && options->name) { +if (!strcmp (options->name,BLOCK_OPT_SIZE )) { + virtual_disk_size = options->value.n; + }else if (!strcmp (options->name,"prefetch_start_delay" )) { +if (options->value.n <=0 ) { + prefetch_start_delay =-1 ; + }else { + prefetch_start_delay = options->value.n; + } + }else if (!strcmp (options->name,BLOCK_OPT_BACKING_FILE )) { + base_img = options->value.s; + }else if (!strcmp (options->name,BLOCK_OPT_BACKING_FMT )) { + base_img_fmt = options->value.s; + }else if (!strcmp (options->name,"copy_on_read" )) { + copy_on_read = options->value.n; + }else if (!strcmp (options->name,"data_file" )) { + data_file = options->value.s; + }else if (!strcmp (options->name,"data_file_fmt" )) { + data_file_fmt = options->value.s; + }else if (!strcmp (options->name,"detect_sparse_hole" )) { + hole_size = options->value.n; + }else if (!strcmp (options->name,"compact_image" )) { + header->compact_image = options->value.n; + }else if (!strcmp (options->name,"block_size" )) { + block_size = options->value.n; + }else if (!strcmp (options->name,"chunk_size" )) { + header->chunk_size = options->value.n; + }else if (!strcmp (options->name,"journal_size" )) { + journal_size = options->value.n; + }else if (!strcmp (options->name,"storage_grow_unit" )) { + header->storage_grow_unit = options->value.n; + }else if (!strcmp (options->name,"add_storage_cmd" ) + && options->value.s) { +pstrcpy (header->add_storage_cmd,sizeof (header->add_storage_cmd), + options->value.s); + } + options++; + } + + virtual_disk_size =ROUND_UP (virtual_disk_size,512 ); + +/* Check if arguments are valid. */ +if (base_img &&strlen (base_img) >1023 ) { +fprintf (stderr,"The base image name is longer than 1023 characters, " +"which is not allowed.\n" ); +return -EINVAL ; + } + +if (base_img && hole_size >0 ) { +if (header->compact_image) { +fprintf (stderr,"compact_image and detect_sparse_hole cannot be " +"enabled together. Please disable detect_sparse_hole. \n" ); +return -EINVAL ; + } + header->need_zero_init = TRUE; + }else { + header->need_zero_init = FALSE; + } + +if (data_file) { +pstrcpy (header->data_file,1024 , data_file); +if (data_file_fmt) { +pstrcpy (header->data_file_fmt,16 , data_file_fmt); + } + } + + header->magic =FVD_MAGIC ; + header->version =FVD_VERSION ; + header->virtual_disk_size = virtual_disk_size; + header->clean_shutdown = TRUE; + +if (!base_img) { + header->all_data_in_fvd_img = TRUE; + }else { +int ret; + + bs =bdrv_new ("" ); +if (!bs) { +fprintf (stderr,"Failed to create a new block driver\n" ); +return -1 ; + } + +pstrcpy (header->base_img,1024 , base_img); +if (base_img_fmt) { +pstrcpy (header->base_img_fmt,16 , base_img_fmt); +BlockDriver *drv =bdrv_find_format (base_img_fmt); +if (!drv) { +fprintf (stderr,"Failed to find driver for format '%s'\n" , + base_img_fmt); +return -1 ; + } + ret =bdrv_open (bs, header->data_file,0 , drv); + }else { + ret =bdrv_open (bs, base_img,0 ,NULL ); + } + +if (ret <0 ) { +fprintf (stderr,"Failed to open the base image %s\n" , base_img); +return -1 ; + } + + base_img_size =bdrv_getlength (bs); + base_img_size =MIN (virtual_disk_size, base_img_size); + base_img_size =ROUND_UP (base_img_size,512 ); + +if (block_size <=0 ) { +/* No block size is provided. Find the smallest block size that +* does not make the bitmap too big. */ + block_size =512 ; +while (1 ) { +int64_t blocks = (base_img_size + block_size -1 ) / block_size; + bitmap_size = (blocks +7 ) /8 ; +if (bitmap_size <=MODERATE_BITMAP_SIZE ) { +break ; + } + block_size *=2 ; + } + }else { + block_size =ROUND_UP (block_size,512 ); +int64_t blocks = (base_img_size + block_size -1 ) / block_size; + bitmap_size = (blocks +7 ) /8 ; + } + + bitmap_size =ROUND_UP (bitmap_size,DEF_PAGE_SIZE ); + header->bitmap_size = bitmap_size; + header->block_size = block_size; + header->bitmap_offset = header_size; + + prefetch_profile_size = header->prefetch_profile_entries * +sizeof (PrefetchProfileEntry); + prefetch_profile_size =ROUND_UP (prefetch_profile_size,DEF_PAGE_SIZE ); + header->base_img_size = base_img_size; + header->max_outstanding_copy_on_read_data = +MAX_OUTSTANDING_COPY_ON_READ_DATA ; + header->copy_on_read = copy_on_read; + header->prefetch_start_delay = + prefetch_start_delay; + header->num_prefetch_slots =NUM_PREFETCH_SLOTS ; + header->bytes_per_prefetch =ROUND_UP (BYTES_PER_PREFETCH , block_size); + header->prefetch_throttle_time =PREFETCH_THROTTLING_TIME ; + header->prefetch_read_throughput_measure_time = +PREFETCH_MIN_MEASURE_READ_TIME ; + header->prefetch_write_throughput_measure_time = +PREFETCH_MIN_MEASURE_WRITE_TIME ; + header->prefetch_perf_calc_alpha =PREFETCH_PERF_CALC_ALPHA ; + header->prefetch_min_read_throughput =PREFETCH_MIN_READ_THROUGHPUT ; + header->prefetch_min_write_throughput =PREFETCH_MIN_WRITE_THROUGHPUT ; + header->prefetch_max_read_throughput =PREFETCH_MAX_READ_THROUGHPUT ; + header->prefetch_max_write_throughput =PREFETCH_MAX_WRITE_THROUGHPUT ; + header->all_data_in_fvd_img = FALSE; + header->unit_of_PrefetchProfileEntry_len =DEF_PAGE_SIZE ; + header->generate_prefetch_profile = FALSE;/* To be implemented. */ + header->profile_directed_prefetch_start_delay =-1 ;/*To be implemented*/ + } + +/* Set the table size. */ +if (header->compact_image) { +if (header->chunk_size <=0 ) { + header->chunk_size =CHUNK_SIZE ; + } + header->chunk_size =ROUND_UP (header->chunk_size,DEF_PAGE_SIZE ); +if (header->storage_grow_unit <=0 ) { + header->storage_grow_unit =STORAGE_GROW_UNIT ; + } +if (header->storage_grow_unit < header->chunk_size) { + header->storage_grow_unit = header->chunk_size; + } +int64_t table_entries = + (virtual_disk_size + header->chunk_size -1 ) / header->chunk_size; + table_size =sizeof (uint32_t ) * table_entries; + table_size =ROUND_UP (table_size,DEF_PAGE_SIZE ); + header->table_offset = header_size + bitmap_size; + } + +/* Set the journal size. */ +if (bitmap_size <=0 && table_size <=0 ) { + header->journal_size =0 ;/* No need to use journal. */ + }else if (journal_size <0 ) { +/* Disable the use of journal, which reduces overhead but may cause +* data corruption if the host crashes. This is a valid configuration +* for some use cases, where data integrity is not critical. */ + header->journal_size =0 ; + }else { +if (journal_size ==0 ) { +/* No journal size is specified. Use a default size. */ + journal_size =JOURNAL_SIZE ; + } +if (table_size >0 ) { +/* Make sure that the journal is at least large enough to record +* all table changes in one shot, which is the extremely unlikely +* worst case. */ +int64_t vsize = virtual_disk_size + header->chunk_size -1 ; +int64_t table_entries = vsize / header->chunk_size; +int64_t min_journal_size =calc_min_journal_size (table_entries); +if (journal_size < min_journal_size) { + journal_size = min_journal_size; + } + } + journal_size =ROUND_UP (journal_size,DEF_PAGE_SIZE ); + header->journal_size = journal_size; + header->journal_offset = header_size + bitmap_size + table_size; + } + +const int64_t metadata_size = header_size + bitmap_size + table_size + + prefetch_profile_size +MAX (0 , journal_size); + header->metadata_size = metadata_size; + + fd =open (filename,O_WRONLY |O_CREAT |O_TRUNC |O_BINARY ,0644 ); +if (fd <0 ) { +fprintf (stderr,"Failed to open %s\n" , filename); + goto fail; + } +fvd_header_cpu_to_le (header); + +if (qemu_write_full (fd, header, header_size) != header_size) { +fprintf (stderr,"Failed to write the header of %s\n" , filename); + goto fail; + } + +/* Initialize the bitmap. */ +if (bitmap_size >0 ) { +uint8_t *bitmap =my_qemu_mallocz (bitmap_size); + ret =qemu_write_full (fd, bitmap, bitmap_size); +my_qemu_free (bitmap); +if (ret != bitmap_size) { +fprintf (stderr,"Failed to zero out the bitmap of %s\n" , filename); + goto fail; + } + } + +/* Initialize the table. */ +if (table_size >0 ) { +/* Set all entries to EMPTY_TABLE (0xFFFFFFFF). */ +uint8_t *empty_table =my_qemu_malloc (table_size); +memset (empty_table,0xFF , table_size); + ret =qemu_write_full (fd, empty_table, table_size); +my_qemu_free (empty_table); +if (ret != table_size) { +fprintf (stderr,"Failed to write the table of %s\n." , filename); + goto fail; + } + } + +/* Initialize the journal. */ +if (journal_size >0 ) { +uint8_t *empty_journal =my_qemu_mallocz (journal_size); + ret =qemu_write_full (fd, empty_journal, journal_size); +my_qemu_free (empty_journal); +if (ret != journal_size) { +fprintf (stderr,"Failed to initialize the journal for %s\n." , + filename); + goto fail; + } + } + +close (fd); + ret =0 ; + +if (bs && hole_size >0 ) { + ret =search_holes (filename, (size_t ) bitmap_size, header_size, bs, + base_img_size /512 , hole_size, block_size); + } + +if (bs) { +bdrv_close (bs); + } +my_qemu_free (header); +return ret; + + fail: +if (bs) { +bdrv_close (bs); + } +close (fd); +my_qemu_free (header); +return -1 ; +} + +/* For the optimization called "free write to zero-filled blocks". See Section +* 3.3.3 of the FVD-cow paper. */ +static inline int search_holes (const char *filename,size_t bitmap_size, +int32_t bitmap_start_offset, +BlockDriverState * bs,int64_t nb_sectors, +int32_t hole_size,int32_t block_size) +{ +const int fd =open (filename,O_RDWR |O_BINARY |O_LARGEFILE ,0 ); +if (fd <0 ) { +fprintf (stderr,"Failed to open %s for read and write.\n" , filename); +return -1 ; + } + +printf ("Searching zero-filled sectors in the base image. Please wait..." ); +fflush (stdout); + +uint8_t *bitmap = + (uint8_t *)mmap (NULL , bitmap_size,PROT_READ |PROT_WRITE ,MAP_SHARED , + fd, (off_t ) bitmap_start_offset); +if (bitmap ==MAP_FAILED ) { +fprintf (stderr,"Failed to mmap() %s\n" , filename); +close (fd); +return -1 ; + } + +if (hole_size < block_size) { + hole_size = block_size; + } + hole_size =ROUND_UP (hole_size, block_size); + nb_sectors =ROUND_DOWN (nb_sectors, hole_size); +const int sectors_per_hole = hole_size /512 ; +const int sectors_per_block = block_size /512 ; +int num_int64_in_hole = hole_size /8 ; +int64_t hole_count =0 ; +int i, ret =0 ; +int64_t sec =0 ; +uint8_t *p =my_qemu_blockalign (bs, hole_size); + +while (sec < nb_sectors) { +int64_t *q; + +if (bdrv_read (bs, sec, p, sectors_per_hole) <0 ) { +fprintf (stderr,"Error in reading the base image\n" ); + ret =-1 ; + goto done; + } + +/* All zeros? */ + q = (int64_t *) p; +for (i =0 ; i < num_int64_in_hole; i++) { +if (*q !=0 ) { +break ; + } + q++; + } + +if (i < num_int64_in_hole) { +/* This is not a hole. */ + sec += sectors_per_hole; + }else { +/* These sectors consist of only zeros. Set the flag to +* indicate that there is no need to read this sector from the +* base image. See Section 3.3.3 of the FVD-cow paper for the +* rationale. */ + hole_count++; +int64_t end = sec + sectors_per_hole; +while (sec < end) { +int block_num = sec / sectors_per_block; +int64_t bitmap_byte_offset = block_num /8 ; +uint8_t bitmap_bit_offset = block_num %8 ; +int8_t mask = (uint8_t ) (0x01 << bitmap_bit_offset); +uint8_t b = bitmap[bitmap_byte_offset]; +if (!(b & mask)) { + b |= mask; + bitmap[bitmap_byte_offset] |= mask; + } + sec += sectors_per_block; + } + } + } + + done: +printf ("\nFound %" PRId64 +" zero-filled hole regions. Image creation done.\n" , hole_count); +my_qemu_vfree (p); +munmap (bitmap, bitmap_size); +close (fd); +return ret; +} + +static QEMUOptionParameter fvd_create_options[] = { + { + .name =BLOCK_OPT_SIZE , + .type =OPT_SIZE , + .help ="Virtual disk size" }, + { + .name ="compact_image" , + .type =OPT_FLAG , + .help ="compact_image=on|off" }, + { + .name ="block_size" , + .type =OPT_SIZE , + .help ="Block size" }, + { + .name ="chunk_size" , + .type =OPT_SIZE , + .help ="Chunk size" }, + { + .name ="storage_grow_unit" , + .type =OPT_SIZE , + .help ="Storage grow unit" }, + { + .name ="add_storage_cmd" , + .type =OPT_STRING , + .help ="Command to add storage when FSI runs out of space" }, + { + .name =BLOCK_OPT_BACKING_FILE , + .type =OPT_STRING , + .help ="File name of a backing image" }, + { + .name =BLOCK_OPT_BACKING_FMT , + .type =OPT_STRING , + .help ="Image format of the backing image" }, + { + .name ="data_file" , + .type =OPT_STRING , + .help ="File name of a separate data file" }, + { + .name ="data_file_fmt" , + .type =OPT_STRING , + .help ="Image format of the separate data file" }, + { + .name ="copy_on_read" , + .type =OPT_FLAG , + .help ="copy_on_read=on|off" }, + { + .name ="prefetch_start_delay" , + .type =OPT_NUMBER , + .help ="Delay in seconds before starting whole image prefetching. " +"Prefetching is disabled if the delay is not a positive number." }, + { + .name ="detect_sparse_hole" , + .type =OPT_SIZE , + .help ="Minimum size (in bytes) of a continuous zero-filled region to be " +"considered as a sparse file hole in the backing image (setting it " +"to 0 turns off sparse file detection)" }, + { + .name ="journal_size" , + .type =OPT_SIZE , + .help ="Journal size" }, + {NULL } +};
block/fvd-debug.c new file +406 -0
406 lines not on this page · raw message
block/fvd-ext.h new file +71 -0
71 lines not on this page · raw message
block/fvd.c new file +127 -0
127 lines not on this page · raw message
block/fvd.h new file +481 -0
481 lines not on this page · raw message
Signature · 1 line
1.7.0.4
PATCH 5/5 Fast Virtual Disk (FVD) Proposal Part 5
In reply to Chunqiang TangPart 5 of the block device driver for the proposed FVD image format. Multiple patches are used in order to manage the size of each patch. This patch includes some new files for FVD.
See the related discussions at
http://lists.gnu.org/archive/html/qemu-devel/2011-01/msg00426.html .
--- block/fvd-read.c | 562 ++++++++++++++++++++++++++++++++++++++++++++++++ block/fvd-store.c | 494 ++++++++++++++++++++++++++++++++++++++++++ block/fvd-utils.c | 612 +++++++++++++++++++++++++++++++++++++++++++++++++++++ block/fvd-write.c | 449 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 2117 insertions(+), 0 deletions(-) create mode 100644 block/fvd-read.c create mode 100644 block/fvd-store.c create mode 100644 block/fvd-utils.c create mode 100644 block/fvd-write.c
block/fvd-read.c new file +562 -0
@@ -0,0 +1,562 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this module implements bdrv_aio_readv() for FVD. +*============================================================================*/ + +static void finish_read_backing_for_copy_on_read (void *opaque,int ret); +static void finish_read_fvd (void *opaque,int ret); +static inline void calc_read_region (BDRVFvdState * s,int64_t sector_num, +int nb_sectors, +int64_t * p_first_sec_in_fvd, +int64_t * p_last_sec_in_fvd, +int64_t * p_first_sec_in_backing, +int64_t * p_last_sec_in_backing); + +static BlockDriverAIOCB *fvd_aio_readv (BlockDriverState * bs, +int64_t sector_num,QEMUIOVector * qiov, +int nb_sectors, +BlockDriverCompletionFunc * cb, +void *opaque) +{ +BDRVFvdState *s = bs->opaque; + +TRACE_REQUEST (FALSE, sector_num, nb_sectors); + +if (!s->data_region_prepared) { +init_data_region (s); + } + +if (s->prefetch_state ==PREFETCH_STATE_FINISHED + || sector_num >= s->nb_sectors_in_base_img) { +/* This is an efficient case. See Section 3.3.5 of the FVD-cow paper. +* This also covers the case of no base image. */ +return load_data (NULL , bs, sector_num, qiov, nb_sectors, cb, opaque); + } + +/* Figure out data regions in the base image and in the FVD data file. */ +int64_t last_sec_in_backing, first_sec_in_backing; +int64_t last_sec_in_fvd, first_sec_in_fvd; +calc_read_region (s, sector_num, nb_sectors, &first_sec_in_fvd, + &last_sec_in_fvd, &first_sec_in_backing, + &last_sec_in_backing); + +if (first_sec_in_backing <0 ) { +/* A simple case: all requested data are in the FVD data file. */ +return load_data (NULL , bs, sector_num, qiov, nb_sectors, cb, opaque); + } + +/* Do copy-on-read only if the context id is 0, i.e., it is not emulating +* synchronous I/O. Doing copy-on-read in emulated synchronous I/O may +* leave the copy-on-read callbacks never being processed due to +* mismatching contextid. */ +const int copy_on_read = s->copy_on_read && (get_async_context_id () ==0 ); + +if (first_sec_in_fvd <0 && !copy_on_read) { +/* A simple case: all requested data are in the base image and no need +* to do copy_on_read. */ +return bdrv_aio_readv (bs->backing_hd, sector_num, qiov, nb_sectors, cb, + opaque); + } + +/* The remaining cases are more complicated, which can be: 1. Data are +* only in the base image and copy-on-read is needed. 2. Data are in both +* the base image and the FVD data file. Copy-on-read may be either TRUE +* or FALSE. */ +FvdAIOCB *acb =my_qemu_aio_get (&fvd_aio_pool, bs, cb, opaque); +if (!acb) { +return NULL ; + } + +QDEBUG ("READ: acb%llu-%p start sector_num=%" PRId64" nb_sectors=%d\n" , + acb->uuid, acb, sector_num, nb_sectors); + + acb->type =OP_READ ; + acb->sector_num = sector_num; + acb->nb_sectors = nb_sectors; + acb->read.qiov = qiov; + acb->read.ret =0 ; + acb->read.read_backing.hd_acb =NULL ; + acb->read.read_backing.done = FALSE; + acb->read.read_backing.iov.iov_base =NULL ; + acb->read.read_fvd.hd_acb =NULL ; + acb->read.read_fvd.iov.iov_base =NULL ; + acb->read.read_fvd.done = (first_sec_in_fvd <0 ); + +/* Read from the base image. */ +if (copy_on_read) { +/* Round the request to the block boundary. */ + acb->read.read_backing.sector_num = +ROUND_DOWN (first_sec_in_backing, s->block_size); +int64_t end =ROUND_UP (last_sec_in_backing +1 , s->block_size); +if (end > s->nb_sectors_in_base_img) { + end = s->nb_sectors_in_base_img; + } + acb->read.read_backing.nb_sectors = + end - acb->read.read_backing.sector_num; + }else { + acb->read.read_backing.sector_num = first_sec_in_backing; + acb->read.read_backing.nb_sectors = + last_sec_in_backing - first_sec_in_backing +1 ; + } + + acb->read.read_backing.iov.iov_len = + acb->read.read_backing.nb_sectors *512 ; + acb->read.read_backing.iov.iov_base = +my_qemu_blockalign (bs->backing_hd, acb->read.read_backing.iov.iov_len); +qemu_iovec_init_external (&acb->read.read_backing.qiov, + &acb->read.read_backing.iov,1 ); + acb->read.read_backing.hd_acb = +bdrv_aio_readv (bs->backing_hd, acb->read.read_backing.sector_num, + &acb->read.read_backing.qiov, + acb->read.read_backing.nb_sectors, + finish_read_backing_for_copy_on_read, acb); +QDEBUG ("READ: acb%llu-%p read_backing backing_sector_num=%" PRId64 +" backing_nb_sectors=%d\n" , acb->uuid, acb, + acb->read.read_backing.sector_num, + acb->read.read_backing.nb_sectors); + +if (!acb->read.read_backing.hd_acb) { +my_qemu_vfree (acb->read.read_backing.iov.iov_base); +my_qemu_aio_release (acb); +return NULL ; + } + +if (first_sec_in_fvd >=0 ) { +/* Read the FVD data file. */ + acb->read.read_fvd.sector_num = first_sec_in_fvd; + acb->read.read_fvd.nb_sectors = last_sec_in_fvd - first_sec_in_fvd +1 ; + acb->read.read_fvd.iov.iov_len = acb->read.read_fvd.nb_sectors *512 ; + +/* Make a copy of the current bitmap because it may change when the +* read requests finish. */ +int64_t b =MIN (acb->read.read_backing.sector_num, + acb->read.read_fvd.sector_num); + b = b / s->block_size /8 ;/* First byte of the bitmap we need. */ +int64_t e1 = acb->read.read_backing.sector_num + + acb->read.read_backing.nb_sectors; +int64_t e2 = acb->read.read_fvd.sector_num + + acb->read.read_fvd.nb_sectors; +int64_t e =MAX (e1, e2); +if (e > s->nb_sectors_in_base_img) { + e = s->nb_sectors_in_base_img; + } + e = (e -1 ) / s->block_size /8 ;/* Last byte of the bitmap we need. */ +int bitmap_bytes = e - b +1 ; +int buf_size = acb->read.read_fvd.iov.iov_len + +ROUND_UP (bitmap_bytes,512 ); + acb->read.read_fvd.iov.iov_base = +my_qemu_blockalign (s->fvd_data, buf_size); +uint8_t *saved_bitmap = ((uint8_t *) acb->read.read_fvd.iov.iov_base) + + acb->read.read_fvd.iov.iov_len; +memcpy (saved_bitmap, s->fresh_bitmap + b, bitmap_bytes); + +qemu_iovec_init_external (&acb->read.read_fvd.qiov, + &acb->read.read_fvd.iov,1 ); +QDEBUG ("READ: acb%llu-%p read_fvd fvd_sector_num=%" PRId64 +" fvd_nb_sectors=%d\n" , acb->uuid, acb, + acb->read.read_fvd.sector_num, acb->read.read_fvd.nb_sectors); + acb->read.read_fvd.hd_acb =load_data (acb, bs, first_sec_in_fvd, + &acb->read.read_fvd.qiov, + acb->read.read_fvd.nb_sectors, + finish_read_fvd, acb); +if (!acb->read.read_fvd.hd_acb) { +if (acb->read.read_backing.hd_acb) { +bdrv_aio_cancel (acb->read.read_backing.hd_acb); +my_qemu_vfree (acb->read.read_backing.iov.iov_base); + } +my_qemu_vfree (acb->read.read_fvd.iov.iov_base); +my_qemu_aio_release (acb); +return NULL ; + } + } + +return &acb->common; +} + +static void finish_copy_on_read (void *opaque,int ret) +{ +FvdAIOCB *acb = opaque; +BlockDriverState *bs = acb->common.bs; +BDRVFvdState *s = bs->opaque; + +if (ret ==0 ) { +/* Update fresh_bitmap but do not update stale_bitmap or the on-disk +* bitmap. See Section 3.3.4 of the FVD-cow paper. */ +update_fresh_bitmap (acb->sector_num, acb->nb_sectors, s); + } + + s->outstanding_copy_on_read_data -= acb->nb_sectors *512 ; + +#ifdef FVD_DEBUG + s->total_copy_on_read_data += acb->nb_sectors *512 ; +#endif +QDEBUG ("READ: acb%llu-%p finish_copy_on_read buffer_sector_num=%" PRId64 +" buffer_nb_sectors=%d write_sector_num=%" PRId64 +" write_nb_sectors=%d outstanding_copy_on_read=%" PRId64"\n" , + acb->uuid, acb, acb->copy.buffered_sector_begin, + (int ) (acb->copy.buffered_sector_end - + acb->copy.buffered_sector_begin), acb->sector_num, + acb->nb_sectors, s->outstanding_copy_on_read_data); + +QLIST_REMOVE (acb, copy_lock.next); +restart_dependent_writes (acb); + +int64_t begin = acb->sector_num + acb->nb_sectors; +int64_t end = acb->copy.buffered_sector_end; + +if (find_region_in_base_img (s, &begin, &end)) { + acb->sector_num = begin; + acb->nb_sectors = end - begin; + acb->copy.iov.iov_base = acb->copy.buf + + (begin - acb->copy.buffered_sector_begin) *512 ; + acb->copy.iov.iov_len = acb->nb_sectors *512 ; +qemu_iovec_init_external (&acb->copy.qiov, &acb->copy.iov,1 ); +QDEBUG ("READ: acb%llu-%p copy_on_read buffer_sector_num=%" PRId64 +" buffer_nb_sectors=%d write_sector_num=%" PRId64 +" write_nb_sectors=%d outstanding_copy_on_read=%" PRId64"\n" , + acb->uuid, acb, acb->copy.buffered_sector_begin, + (int ) (acb->copy.buffered_sector_end - + acb->copy.buffered_sector_begin), acb->sector_num, + acb->nb_sectors, s->outstanding_copy_on_read_data); + acb->copy.hd_acb =store_data (TRUE, acb, bs, acb->sector_num, + &acb->copy.qiov, acb->nb_sectors, + finish_copy_on_read, acb); +if (acb->copy.hd_acb) { +QLIST_INIT (&acb->copy_lock.dependent_writes); + acb->copy_lock.begin = begin; + acb->copy_lock.end = end; +QLIST_INSERT_HEAD (&s->copy_locks, acb, copy_lock.next); + s->outstanding_copy_on_read_data += acb->copy.iov.iov_len; +return ; + } + } + +QDEBUG ("READ: acb%llu-%p no_more_copy_on_read\n" , acb->uuid, acb); +my_qemu_vfree (acb->copy.buf); +my_qemu_aio_release (acb); +} + +static void finish_read (FvdAIOCB * acb) +{ +BlockDriverState *bs = acb->common.bs; +BDRVFvdState *s = bs->opaque; + +if (acb->read.ret !=0 ) { +QDEBUG ("READ: acb%llu-%p finish_read error ret=%d sector_num=%" PRId64 +" nb_sectors=%d\n" , acb->uuid, acb, acb->read.ret, + acb->sector_num, acb->nb_sectors); + acb->common.cb (acb->common.opaque, acb->read.ret); +if (acb->read.read_backing.iov.iov_base) { +my_qemu_vfree (acb->read.read_backing.iov.iov_base); + } +if (acb->read.read_fvd.iov.iov_base) { +my_qemu_vfree (acb->read.read_fvd.iov.iov_base); + } +my_qemu_aio_release (acb); + +return ; + } + +if (!acb->read.read_fvd.iov.iov_base) { +/* Only read data from the base image. */ +uint8_t *data = ((uint8_t *) acb->read.read_backing.iov.iov_base) + + (acb->sector_num - acb->read.read_backing.sector_num) *512 ; +qemu_iovec_from_buffer (acb->read.qiov, data, acb->nb_sectors *512 ); + }else { +/* Under the guidance of the saved bitmap, merge data from the FVD +* data file and the base image. */ +uint8_t *saved_bitmap = ((uint8_t *) acb->read.read_fvd.iov.iov_base) + + acb->read.read_fvd.iov.iov_len; +int64_t bitmap_offset =MIN (acb->read.read_backing.sector_num, + acb->read.read_fvd.sector_num); + bitmap_offset = bitmap_offset / s->block_size /8 ; +int iov_index =0 ; +uint8_t *iov_buf = acb->read.qiov->iov[0 ].iov_base; +int iov_left = acb->read.qiov->iov[0 ].iov_len; +int64_t sec = acb->sector_num; +const int64_t end = acb->sector_num + acb->nb_sectors; +int64_t first_sec; +uint8_t *source; + +if (bitmap_show_sector_in_base_img + (sec, s, bitmap_offset, saved_bitmap)) { + goto in_backing; + } + +while (1 ) { +/* For a section of data in the FVD data file. */ +if (sec >= end) { +break ; + } + + first_sec = sec; +do { + sec++; + }while (sec < end && !bitmap_show_sector_in_base_img (sec, s, + bitmap_offset, saved_bitmap)); + + source = ((uint8_t *) acb->read.read_fvd.iov.iov_base) + + (first_sec - acb->read.read_fvd.sector_num) *512 ; +copy_to_iov (acb->read.qiov->iov, &iov_index, &iov_buf, &iov_left, + source, (sec - first_sec) *512 ); + + in_backing: +/* For a section of data in the base image. */ +if (sec >= end) { +break ; + } + + first_sec = sec; +do { + sec++; + }while (sec < end &&bitmap_show_sector_in_base_img (sec, s, + bitmap_offset, saved_bitmap)); + + source = ((uint8_t *) acb->read.read_backing.iov.iov_base) + + (first_sec - acb->read.read_backing.sector_num) *512 ; +copy_to_iov (acb->read.qiov->iov, &iov_index, &iov_buf, &iov_left, + source, (sec - first_sec) *512 ); + } + +ASSERT (iov_index == acb->read.qiov->niov -1 && iov_left ==0 ); +my_qemu_vfree (acb->read.read_fvd.iov.iov_base); + } + +QDEBUG ("READ: acb%llu-%p finish_read ret=%d\n" , acb->uuid, acb, + acb->read.ret); + acb->common.cb (acb->common.opaque, acb->read.ret); + +if (!s->copy_on_read ||get_async_context_id () !=0 ) { +/* Do copy-on-read only if the context id is 0, i.e., it is not +* emulating synchronous I/O. Doing copy-on-read in emulated +* synchronous I/O may leave the copy-on-read callbacks never being +* processed due to mismatching context id. */ +my_qemu_vfree (acb->read.read_backing.iov.iov_base); +my_qemu_aio_release (acb); +return ; + } + +/* Convert AIOReadCB into a AIOCopyCB for copy-on-read. */ +uint8_t *buf = acb->read.read_backing.iov.iov_base; +int64_t begin = acb->read.read_backing.sector_num; +int64_t end = begin + acb->read.read_backing.nb_sectors; + + acb->type =OP_COPY ; + acb->copy.buf = buf; + acb->copy.buffered_sector_begin = begin; + acb->copy.buffered_sector_end = end; + +if (s->outstanding_copy_on_read_data < s->max_outstanding_copy_on_read_data + &&find_region_in_base_img (s, &begin, &end)) { +/* Write to the FVD data file. */ + acb->sector_num = begin; + acb->nb_sectors = end - begin; + acb->copy.iov.iov_base = + buf + (begin - acb->copy.buffered_sector_begin) *512 ; + acb->copy.iov.iov_len = acb->nb_sectors *512 ; +qemu_iovec_init_external (&acb->copy.qiov, &acb->copy.iov,1 ); +QDEBUG ("READ: acb%llu-%p copy_on_read buffer_sector_num=%" PRId64 +" buffer_nb_sectors=%d write_sector_num=%" PRId64 +" write_nb_sectors=%d outstanding_copy_on_read=%" PRId64"\n" , + acb->uuid, acb, acb->copy.buffered_sector_begin, + (int ) (acb->copy.buffered_sector_end - + acb->copy.buffered_sector_begin), acb->sector_num, + acb->nb_sectors, s->outstanding_copy_on_read_data); + acb->copy.hd_acb =store_data (TRUE, acb, bs, acb->sector_num, + &acb->copy.qiov, acb->nb_sectors, + finish_copy_on_read, acb); +if (acb->copy.hd_acb) { +QLIST_INIT (&acb->copy_lock.dependent_writes); + acb->copy_lock.begin = begin; + acb->copy_lock.end = end; +QLIST_INSERT_HEAD (&s->copy_locks, acb, copy_lock.next); + s->outstanding_copy_on_read_data += acb->copy.iov.iov_len; +return ; + } + } + +/* No more copy-on-read to do. */ +my_qemu_vfree (acb->copy.buf); +my_qemu_aio_release (acb); +} + +static void finish_read_fvd (void *opaque,int ret) +{ +FvdAIOCB *acb = opaque; + +QDEBUG ("READ: acb%llu-%p finish_read_fvd ret=%d\n" , acb->uuid, acb, ret); + acb->read.read_fvd.hd_acb =NULL ; + acb->read.read_fvd.done = TRUE; +if (acb->read.ret ==0 ) { + acb->read.ret = ret; + } + +if (acb->read.read_backing.done) { +finish_read (acb);/* The other request also finished. */ + } +} + +static void finish_read_backing_for_copy_on_read (void *opaque,int ret) +{ +FvdAIOCB *acb = opaque; + +QDEBUG ("READ: acb%llu-%p finish_read_backing ret=%d\n" , acb->uuid, acb, + ret); + acb->read.read_backing.hd_acb =NULL ; + acb->read.read_backing.done = TRUE; +if (acb->read.ret ==0 ) { + acb->read.ret = ret; + } + +if (acb->read.read_fvd.done) { +finish_read (acb);/* The other request also finished. */ + } +} + +static inline void calc_read_region (BDRVFvdState * s,int64_t sector_num, +int nb_sectors, +int64_t * p_first_sec_in_fvd, +int64_t * p_last_sec_in_fvd, +int64_t * p_first_sec_in_backing, +int64_t * p_last_sec_in_backing) +{ +int64_t last_sec_in_backing =-1 , first_sec_in_backing =-1 ; +int64_t last_sec_in_fvd =-1 , first_sec_in_fvd =-1 ; +int prev_block_in_backing; + +if (fresh_bitmap_show_sector_in_base_img (sector_num, s)) { + first_sec_in_backing = last_sec_in_backing = sector_num; + prev_block_in_backing = TRUE; + }else { + first_sec_in_fvd = last_sec_in_fvd = sector_num; + prev_block_in_backing = FALSE; + } + +/* Begin of next block. */ +int64_t sec =ROUND_UP (sector_num +1 , s->block_size); + +const int64_t sec_end = sector_num + nb_sectors; +int64_t last_sec =MIN (sec_end, s->nb_sectors_in_base_img) -1 ; + +while (1 ) { +if (sec > last_sec) { + sec = last_sec; + } + +if (fresh_bitmap_show_sector_in_base_img (sec, s)) { +if (first_sec_in_backing <0 ) { + first_sec_in_backing = sec; + } +if (!prev_block_in_backing) { + last_sec_in_fvd = sec -1 ; + prev_block_in_backing = TRUE; + } + last_sec_in_backing = sec; + }else { +if (first_sec_in_fvd <0 ) { + first_sec_in_fvd = sec; + } +if (prev_block_in_backing) { + last_sec_in_backing = sec -1 ; + prev_block_in_backing = FALSE; + } + last_sec_in_fvd = sec; + } + +if (sec == last_sec) { +break ; + } + sec += s->block_size; + } + +if (sec_end > s->nb_sectors_in_base_img) { +if (first_sec_in_fvd <0 ) { + first_sec_in_fvd = s->nb_sectors_in_base_img; + } + last_sec_in_fvd = sec_end -1 ; + } + + *p_first_sec_in_fvd = first_sec_in_fvd; + *p_last_sec_in_fvd = last_sec_in_fvd; + *p_first_sec_in_backing = first_sec_in_backing; + *p_last_sec_in_backing = last_sec_in_backing; +} + +static void fvd_read_cancel (FvdAIOCB * acb) +{ +if (acb->read.read_backing.hd_acb) { +bdrv_aio_cancel (acb->read.read_backing.hd_acb); + } +if (acb->read.read_fvd.hd_acb) { +bdrv_aio_cancel (acb->read.read_fvd.hd_acb); + } +if (acb->read.read_backing.iov.iov_base) { +my_qemu_vfree (acb->read.read_backing.iov.iov_base); + } +if (acb->read.read_fvd.iov.iov_base) { +my_qemu_vfree (acb->read.read_fvd.iov.iov_base); + } +my_qemu_aio_release (acb); +} + +static void fvd_copy_cancel (FvdAIOCB * acb) +{ +BlockDriverState *bs = acb->common.bs; +BDRVFvdState *s = bs->opaque; + +if (acb->copy.hd_acb) { +bdrv_aio_cancel (acb->copy.hd_acb); + } +if (acb->copy_lock.next.le_prev !=NULL ) { +QLIST_REMOVE (acb, copy_lock.next); +restart_dependent_writes (acb); + } +my_qemu_vfree (acb->copy.buf); +if (acb->common.cb != null_prefetch_cb) { +/* This is a copy-on-read operation. */ + s->outstanding_copy_on_read_data -= acb->nb_sectors *512 ; + } +my_qemu_aio_release (acb); +} + +static void restart_dependent_writes (FvdAIOCB * acb) +{ + acb->copy_lock.next.le_prev =NULL ; +FvdAIOCB *req = acb->copy_lock.dependent_writes.lh_first; + +while (req) { +/* Keep a copy of 'next' as it may be changed in do_aiO_write(). */ +FvdAIOCB *next = req->write.next_dependent_write.le_next; + +/* Indicate that this write is no longer on any depedent list. This +* helps fvd_read_cancel() work properly. */ + req->write.next_dependent_write.le_prev =NULL ; + +if (acb->type ==OP_WRITE ) { +QDEBUG ("WRITE: acb%llu-%p finished_and_restart_conflict_write " +"acb%llu-%p\n" , acb->uuid, acb, req->uuid, req); + }else { +QDEBUG ("READ: copy_on_read acb%llu-%p " +"finished_and_restart_conflict_write acb%llu-%p\n" , + acb->uuid, acb, req->uuid, req); + } + +if (do_aio_write (req) <0 ) { +QDEBUG ("WRITE: acb%llu-%p finished with error ret=%d\n" , + req->uuid, req,-1 ); + req->common.cb (req->common.opaque,-1 ); +my_qemu_aio_release (req); + } + + req = next; + } +}
block/fvd-store.c new file +494 -0
494 lines not on this page · raw message
block/fvd-utils.c new file +612 -0
612 lines not on this page · raw message
block/fvd-write.c new file +449 -0
449 lines not on this page · raw message
Signature · 1 line
1.7.0.4
Quoted 3 lines · Chunqiang Tang
Part 1 of the block device driver for the proposed FVD image format. Multiple patches are used in order to manage the size of each patch. This patch includes existing files that are modified by FVD.
Please try to split the patches into logical parts, and use descriptive subject lines for each patch.
E.g. adding the new sim command to qemu-io could be one patch, adding the img_update (why not just update?) command to qemu-img another, moving code into qemu-tool-time.c one more, etc.
Review
Quoted 2 lines
(quoted diff) +1
-1- +
Please do not introduce random whitespace changes in patches.
Quoted 5 lines
Please try to split the patches into logical parts, and use descriptive subject lines for each patch.
E.g. adding the new sim command to qemu-io could be one patch, adding theimg_update(why not just update?) command to qemu-img another, moving code intoqemu-tool-time.cone more, etc.
Will do and thank you for the detailed instructions.
Quoted 3 lines
Quoted 2 lines
-
+Please do not introduce random whitespace changes in patches.
Stefan Weil previously suggested removing spaces at the end of a line, and I used a script to do that. It seems that in this example, the old code has multiple spaces on an empty line, which were automatically removed by the script.
Am 20.01.2011 15:49, schrieb Chunqiang Tang:
Quoted 8 lines
Quoted 5 lines
Please try to split the patches into logical parts, and use descriptive subject lines for each patch.
E.g. adding the new sim command to qemu-io could be one patch, adding theimg_update(why not just update?) command to qemu-img another, moving code intoqemu-tool-time.cone more, etc.Will do and thank you for the detailed instructions.
Quoted 8 lines
Quoted 3 lines
Review
Quoted 2 lines
(quoted diff) +1
-1- +Please do not introduce random whitespace changes in patches.
Stefan Weil previously suggested removing spaces at the end of a line, and I used a script to do that. It seems that in this example, the old code has multiple spaces on an empty line, which were automatically removed by the script.
Yes, that's a problem with some parts of the old code. For files which you want to modify, you could remove the spaces with your script before applying your other modifications and create a separate patch which only removes the superfluous spaces.
So your patch series would start with patches which only remove spaces at line endings (and say so in the patch descriptions). Then these changes are no longer random whitespace changes.
Regards,
Stefan Weil
Quoted 62 lines · Chunqiang Tang
Part 1 of the block device driver for the proposed FVD image format. Multiple patches are used in order to manage the size of each patch. This patch includes existing files that are modified by FVD.
See the related discussions at
http://lists.gnu.org/archive/html/qemu-devel/2011-01/msg00426.html .Signed-off-by Chunqiang Tang<[email protected]>--- Makefile | 10 +++++--- Makefile.objs | 1 + block.c | 12 +++++----- block_int.h | 5 ++- configure | 2 +- qemu-img-cmds.hx | 6 +++++ qemu-img.c | 62 ++++++++++++++++++++++++++++++++++++++++++++--------- qemu-io.c | 3 ++ qemu-option.c | 4 +++ qemu-tool.c | 36 ------------------------------- 10 files changed, 81 insertions(+), 60 deletions(-)Makefile +6
-4@@ -151,13 +151,15 @@ version-obj-$(CONFIG_WIN32) += version.o ###################################################################### qemu-img.o: qemu-img-cmds.h -qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o cmd.o: $(GENERATED_HEADERS) +qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o cmd.o qemu-test.o: $(GENERATED_HEADERS) -qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o +qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-tool-time.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o -qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o +qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-tool-time.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o -qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o +qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-tool-time.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o + +qemu-test$(EXESUF): qemu-test.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(call quiet-command,sh $(SRC_PATH)/hxtool -h< $< > $@," GEN $@")Makefile.objs +1
-0@@ -23,6 +23,7 @@ block-nested-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o block-nested-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o block-nested-y += qed-check.o block-nested-y += parallels.o nbd.o blkdebug.o sheepdog.o blkverify.o +block-nested-y += blksim.o fvd.o block-nested-$(CONFIG_WIN32) += raw-win32.o block-nested-$(CONFIG_POSIX) += raw-posix.o block-nested-$(CONFIG_CURL) += curl.oblock.c +0
-0
block.c static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, · line 63
Quoted 7 lines
block.c +1
-1@@ -58,7 +58,7 @@ static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,static int bdrv_write_em (BlockDriverState *bs,int64_t sector_num,const uint8_t *buf,int nb_sectors); -static QTAILQ_HEAD(, BlockDriverState) bdrv_states = +QTAILQ_HEAD(, BlockDriverState) bdrv_states =QTAILQ_HEAD_INITIALIZER (bdrv_states);
This looks suspicious and indicates your doing something bad.
Quoted 49 lines
block.c +5
-5static QLIST_HEAD(, BlockDriver) bdrv_drivers =@@ -768,7 +768,7 @@ int bdrv_commit(BlockDriverState *bs)if (!drv)return -ENOMEDIUM ; - +if (!bs->backing_hd) {return -ENOTSUP ; }@@ -1538,7 +1538,7 @@ int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)* 'nb_sectors' is the max value 'pnum' should be set to. */ int bdrv_is_allocated (BlockDriverState *bs,int64_t sector_num,int nb_sectors, -int *pnum) +int *pnum) {int64_t n;if (!bs->drv->bdrv_is_allocated) {@@ -2050,9 +2050,9 @@ BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, cb, opaque);if (ret) { -/* Update stats even though technically transfer has not happened. */ - bs->rd_bytes += (unsigned ) nb_sectors *BDRV_SECTOR_SIZE ; - bs->rd_ops ++; +/* Update stats even though technically transfer has not happened. */ + bs->rd_bytes += (unsigned ) nb_sectors *BDRV_SECTOR_SIZE ; + bs->rd_ops ++; }return ret;block_int.h +2
-2@@ -28,8 +28,8 @@#include "qemu-option.h" #include "qemu-queue.h" -#define BLOCK_FLAG_ENCRYPT 1 -#define BLOCK_FLAG_COMPAT6 4 +#define BLOCK_FLAG_ENCRYPT 1 +#define BLOCK_FLAG_COMPAT6 4#define BLOCK_OPT_SIZE "size"#define BLOCK_OPT_ENCRYPT "encryption"
block_int.h struct BlockDriver { · line 102
Quoted 5 lines
block_int.h +1
-0@@ -98,6 +98,7 @@ struct BlockDriver {int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,const char *snapshot_name);int (*bdrv_get_info)(BlockDriverState *bs,BlockDriverInfo *bdi); +int (*bdrv_update)(BlockDriverState *bs,int argc,char **argv);
argc/argv is an awful interface because the semantics end up varying widely. If we want to support changing disk format parameters, we should use a structured option format so we can ensure it's exposed to the user in a consistent way. IOW, a size is always parsed as <integer>[SUFFIX] and not 8 different variations of that theme.
block_int.h static int img_convert(int argc, char **argv) · line 869
Quoted 10 lines
block_int.h +0
-0bs =qemu_mallocz (bs_n *sizeof (BlockDriverState *));total_sectors = 0;block_int.h +1
-1@@ -865,7 +865,7 @@ static int img_convert(int argc, char **argv)assume that sectors which are unallocated in the input image are present in both the output's and input's base images (no need to copy them). */ -if (out_baseimg) { +if (out_baseimg || bs[bs_i]->backing_file[0 ]==0 ) {
This looks like a bug fix of some sort and should be it's own patch with an explanation.
Quoted 92 lines
block_int.h +0
-0if (!bdrv_is_allocated (bs[bs_i], sector_num - bs_offset, n,&n1)) {sector_num += n1;block_int.h +49
-9@@ -941,10 +941,10 @@ static int64_t get_allocated_file_size(const char *filename)/* WinNT support GetCompressedFileSize to determine allocate size */ get_compressed = (get_compressed_t )GetProcAddress (GetModuleHandle ("kernel32" ),"GetCompressedFileSizeA" );if (get_compressed) { -DWORD high, low; - low =get_compressed (filename,&high); -if (low !=0xFFFFFFFFlu ||GetLastError () ==NO_ERROR ) -return (((int64_t ) high)<<32 ) + low; +DWORD high, low; + low =get_compressed (filename,&high); +if (low !=0xFFFFFFFFlu ||GetLastError () ==NO_ERROR ) +return (((int64_t ) high)<<32 ) + low; }if (_stati64 (filename,&st)<0 )@@ -1036,11 +1036,6 @@ static int img_info(int argc, char **argv)if (bdrv_is_encrypted (bs)) {printf ("encrypted: yes\n" ); } -if (bdrv_get_info (bs,&bdi)>=0 ) { -if (bdi.cluster_size !=0 ) { -printf ("cluster_size: %d\n" , bdi.cluster_size); - } - }bdrv_get_backing_filename (bs, backing_filename,sizeof (backing_filename));if (backing_filename[0 ] !='\0' ) { path_combine(backing_filename2, sizeof(backing_filename2),@@ -1049,11 +1044,56 @@ static int img_info(int argc, char **argv) backing_filename, backing_filename2); } +if (bdrv_get_info (bs,&bdi)>=0 ) { +if (bdi.cluster_size !=0 ) +printf ("cluster_size: %d\n" , bdi.cluster_size); + }dump_snapshots (bs);bdrv_delete (bs);return 0 ; } +static int img_update (int argc,char **argv) +{ +int c; +const char *filename, *fmt; +BlockDriverState *bs; + + fmt =NULL ; +for (;;) { + c =getopt (argc, argv,"f:h" ); +if (c ==-1 ) +break ; +switch (c) { +case 'h' : +help (); +break ; +case 'f' : + fmt = optarg; +break ; + } + } +if (optind>= argc) +help (); + filename = argv[optind++]; + + bs =bdrv_new_open (filename, fmt,BDRV_O_FLAGS |BDRV_O_NO_BACKING |BDRV_O_RDWR ); +if (!bs) { +return 1 ; + } + +if (bs->drv->bdrv_update==NULL ) { +char fmt_name[128 ]; +bdrv_get_format (bs, fmt_name,sizeof (fmt_name)); +error_report ("the 'update' command is not supported for the '%s' image format." , fmt_name); + } + + bs->drv->bdrv_update (bs, argc-optind,&argv[optind]); + +bdrv_delete (bs); +return 0 ; +} +#define SNAPSHOT_LIST 1#define SNAPSHOT_CREATE 2#define SNAPSHOT_APPLY 3qemu-io.c +0
-0
qemu-io.c init_check_command( · line 1706
Quoted 6 lines
qemu-io.c +2
-0@@ -1701,6 +1701,8 @@ init_check_command(return 1 ; } +#include "qemu-io-sim.c" +
1) I don't see qemu-io-sim.c in this patch which means this breaks the build
2) Including C files is evil.
Quoted 71 lines
qemu-io.c +0
-0static void usage (const char *name) {printf(
qemu-io.c +1
-0@@ -1807,6 +1809,7 @@ int main(int argc, char **argv)add_command (&discard_cmd);add_command (&alloc_cmd);add_command (&map_cmd); +add_command (&sim_cmd);add_args_command (init_args_command);add_check_command (init_check_command);qemu-option.c +4
-0@@ -289,6 +289,10 @@ int set_option_parameter(QEMUOptionParameter *list, const char *name,return -1 ;break ; +case OPT_NUMBER : + list->value.n =atoi (value); +break ; +default :fprintf (stderr,"Bug: Option '%s' has an unknown type\n" , name);return -1 ;qemu-tool.c +0
-29@@ -23,12 +23,6 @@ QEMUClock *rt_clock;FILE *logfile; -struct QEMUBH -{ -QEMUBHFunc *cb; -void *opaque; -}; -void qemu_service_io (void ) { }@@ -73,36 +67,6 @@ void monitor_protocol_event(MonitorEvent event, QObject *data) { } -QEMUBH *qemu_bh_new (QEMUBHFunc *cb,void *opaque) -{ -QEMUBH *bh; - - bh =qemu_malloc (sizeof (*bh)); - bh->cb = cb; - bh->opaque = opaque; - -return bh; -} - -int qemu_bh_poll (void ) -{ -return 0 ; -} - -void qemu_bh_schedule (QEMUBH *bh) -{ - bh->cb (bh->opaque); -} - -void qemu_bh_cancel (QEMUBH *bh) -{
qemu-tool.c void monitor_protocol_event(MonitorEvent event, QObject *data) · line 72
Quoted 10 lines
qemu-tool.c +0
-7-} - -void qemu_bh_delete (QEMUBH *bh) -{ -qemu_free (bh); -} - int qemu_set_fd_handler2(int fd, IOCanReadHandler *fd_read_poll, IOHandler *fd_read,
These functions surely cannot just be deleted like this.
Regards,
Anthony Liguori
Re: [Qemu-devel] [PATCH 3/5] Fast Virtual Disk (FVD) Proposal Part 3
In reply to Chunqiang TangQuoted 7 lines · Chunqiang Tang
Part 3 of the block device driver for the proposed FVD image format. Multiple patches are used in order to manage the size of each patch. This patch includes some new files for FVD.
See the related discussions at
http://lists.gnu.org/archive/html/qemu-devel/2011-01/msg00426.html .
Before going any further with this series, I'd like to see
1) a specification (on the QEMU wiki) describing this image format that can be reviewed
2) a concise explanation of why qcow2/qed cannot satisfy the use cases addressed by FVD
3) performance data to backup the claims of (2)
I don't want a reference to a website or a paper. Just a concise and clear explanation on the mailing list.
I think blocksim might be worth merging but I'm still extremely sceptical of the claimed benefits of FVD.
Comparing FVD performance to qcow2 is not really all that interesting. qcow2 is terrible from a performance perspective in the face of any rigorous benchmarking. I would suggest focusing on QED in terms of comparative performance.
Regards,
Anthony Liguori
Quoted 1,605 lines
Signed-off-by Chunqiang Tang<[email protected]>--- block/fvd-create.c | 475 +++++++++++++++++++++++++++++++++++++++++++++++++++ block/fvd-debug.c | 406 ++++++++++++++++++++++++++++++++++++++++++++ block/fvd-ext.h | 71 ++++++++ block/fvd.c | 127 ++++++++++++++ block/fvd.h | 481 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1560 insertions(+), 0 deletions(-)create mode 100644
block/fvd-create.c
create mode 100644block/fvd-debug.c
create mode 100644block/fvd-ext.h
create mode 100644block/fvd.c
create mode 100644block/fvd.hblock/fvd-create.c new file +475
-0@@ -0,0 +1,475 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang<[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this module implements bdrv_create() for FVD. +*============================================================================*/ + +static inline int64_t calc_min_journal_size (int64_t table_entries); +static inline int search_holes (const char *filename,size_t bitmap_size, +int32_t bitmap_start_offset,BlockDriverState * bs, +int64_t nb_sectors,int32_t hole_size,int32_t block_size); + +static int fvd_create (const char *filename,QEMUOptionParameter * options) +{ +int fd, ret; +FvdHeader *header; +int64_t virtual_disk_size =DEF_PAGE_SIZE ; +int32_t header_size; +const char *base_img =NULL ; +const char *base_img_fmt =NULL ; +const char *data_file =NULL ; +const char *data_file_fmt =NULL ; +int32_t hole_size =0 ; +int copy_on_read = FALSE; +int prefetch_start_delay =-1 ; +int64_t prefetch_profile_size =0 ; +BlockDriverState *bs =NULL ; +int bitmap_size =0 ; +int64_t base_img_size =0 ; +int64_t table_size =0 ; +int64_t journal_size =0 ; +int32_t block_size =0 ; + + header_size =sizeof (FvdHeader); + header_size =ROUND_UP (header_size,DEF_PAGE_SIZE ); + header =my_qemu_mallocz (header_size); + +/* Read out options */ +while (options&& options->name) { +if (!strcmp (options->name,BLOCK_OPT_SIZE )) { + virtual_disk_size = options->value.n; + }else if (!strcmp (options->name,"prefetch_start_delay" )) { +if (options->value.n<=0 ) { + prefetch_start_delay =-1 ; + }else { + prefetch_start_delay = options->value.n; + } + }else if (!strcmp (options->name,BLOCK_OPT_BACKING_FILE )) { + base_img = options->value.s; + }else if (!strcmp (options->name,BLOCK_OPT_BACKING_FMT )) { + base_img_fmt = options->value.s; + }else if (!strcmp (options->name,"copy_on_read" )) { + copy_on_read = options->value.n; + }else if (!strcmp (options->name,"data_file" )) { + data_file = options->value.s; + }else if (!strcmp (options->name,"data_file_fmt" )) { + data_file_fmt = options->value.s; + }else if (!strcmp (options->name,"detect_sparse_hole" )) { + hole_size = options->value.n; + }else if (!strcmp (options->name,"compact_image" )) { + header->compact_image = options->value.n; + }else if (!strcmp (options->name,"block_size" )) { + block_size = options->value.n; + }else if (!strcmp (options->name,"chunk_size" )) { + header->chunk_size = options->value.n; + }else if (!strcmp (options->name,"journal_size" )) { + journal_size = options->value.n; + }else if (!strcmp (options->name,"storage_grow_unit" )) { + header->storage_grow_unit = options->value.n; + }else if (!strcmp (options->name,"add_storage_cmd" ) +&& options->value.s) { +pstrcpy (header->add_storage_cmd,sizeof (header->add_storage_cmd), + options->value.s); + } + options++; + } + + virtual_disk_size =ROUND_UP (virtual_disk_size,512 ); + +/* Check if arguments are valid. */ +if (base_img&&strlen (base_img)>1023 ) { +fprintf (stderr,"The base image name is longer than 1023 characters, " +"which is not allowed.\n" ); +return -EINVAL ; + } + +if (base_img&& hole_size>0 ) { +if (header->compact_image) { +fprintf (stderr,"compact_image and detect_sparse_hole cannot be " +"enabled together. Please disable detect_sparse_hole. \n" ); +return -EINVAL ; + } + header->need_zero_init = TRUE; + }else { + header->need_zero_init = FALSE; + } + +if (data_file) { +pstrcpy (header->data_file,1024 , data_file); +if (data_file_fmt) { +pstrcpy (header->data_file_fmt,16 , data_file_fmt); + } + } + + header->magic =FVD_MAGIC ; + header->version =FVD_VERSION ; + header->virtual_disk_size = virtual_disk_size; + header->clean_shutdown = TRUE; + +if (!base_img) { + header->all_data_in_fvd_img = TRUE; + }else { +int ret; + + bs =bdrv_new ("" ); +if (!bs) { +fprintf (stderr,"Failed to create a new block driver\n" ); +return -1 ; + } + +pstrcpy (header->base_img,1024 , base_img); +if (base_img_fmt) { +pstrcpy (header->base_img_fmt,16 , base_img_fmt); +BlockDriver *drv =bdrv_find_format (base_img_fmt); +if (!drv) { +fprintf (stderr,"Failed to find driver for format '%s'\n" , + base_img_fmt); +return -1 ; + } + ret =bdrv_open (bs, header->data_file,0 , drv); + }else { + ret =bdrv_open (bs, base_img,0 ,NULL ); + } + +if (ret<0 ) { +fprintf (stderr,"Failed to open the base image %s\n" , base_img); +return -1 ; + } + + base_img_size =bdrv_getlength (bs); + base_img_size =MIN (virtual_disk_size, base_img_size); + base_img_size =ROUND_UP (base_img_size,512 ); + +if (block_size<=0 ) { +/* No block size is provided. Find the smallest block size that +* does not make the bitmap too big. */ + block_size =512 ; +while (1 ) { +int64_t blocks = (base_img_size + block_size -1 ) / block_size; + bitmap_size = (blocks +7 ) /8 ; +if (bitmap_size<=MODERATE_BITMAP_SIZE ) { +break ; + } + block_size *=2 ; + } + }else { + block_size =ROUND_UP (block_size,512 ); +int64_t blocks = (base_img_size + block_size -1 ) / block_size; + bitmap_size = (blocks +7 ) /8 ; + } + + bitmap_size =ROUND_UP (bitmap_size,DEF_PAGE_SIZE ); + header->bitmap_size = bitmap_size; + header->block_size = block_size; + header->bitmap_offset = header_size; + + prefetch_profile_size = header->prefetch_profile_entries * +sizeof (PrefetchProfileEntry); + prefetch_profile_size =ROUND_UP (prefetch_profile_size,DEF_PAGE_SIZE ); + header->base_img_size = base_img_size; + header->max_outstanding_copy_on_read_data = +MAX_OUTSTANDING_COPY_ON_READ_DATA ; + header->copy_on_read = copy_on_read; + header->prefetch_start_delay = + prefetch_start_delay; + header->num_prefetch_slots =NUM_PREFETCH_SLOTS ; + header->bytes_per_prefetch =ROUND_UP (BYTES_PER_PREFETCH , block_size); + header->prefetch_throttle_time =PREFETCH_THROTTLING_TIME ; + header->prefetch_read_throughput_measure_time = +PREFETCH_MIN_MEASURE_READ_TIME ; + header->prefetch_write_throughput_measure_time = +PREFETCH_MIN_MEASURE_WRITE_TIME ; + header->prefetch_perf_calc_alpha =PREFETCH_PERF_CALC_ALPHA ; + header->prefetch_min_read_throughput =PREFETCH_MIN_READ_THROUGHPUT ; + header->prefetch_min_write_throughput =PREFETCH_MIN_WRITE_THROUGHPUT ; + header->prefetch_max_read_throughput =PREFETCH_MAX_READ_THROUGHPUT ; + header->prefetch_max_write_throughput =PREFETCH_MAX_WRITE_THROUGHPUT ; + header->all_data_in_fvd_img = FALSE; + header->unit_of_PrefetchProfileEntry_len =DEF_PAGE_SIZE ; + header->generate_prefetch_profile = FALSE;/* To be implemented. */ + header->profile_directed_prefetch_start_delay =-1 ;/*To be implemented*/ + } + +/* Set the table size. */ +if (header->compact_image) { +if (header->chunk_size<=0 ) { + header->chunk_size =CHUNK_SIZE ; + } + header->chunk_size =ROUND_UP (header->chunk_size,DEF_PAGE_SIZE ); +if (header->storage_grow_unit<=0 ) { + header->storage_grow_unit =STORAGE_GROW_UNIT ; + } +if (header->storage_grow_unit< header->chunk_size) { + header->storage_grow_unit = header->chunk_size; + } +int64_t table_entries = + (virtual_disk_size + header->chunk_size -1 ) / header->chunk_size; + table_size =sizeof (uint32_t ) * table_entries; + table_size =ROUND_UP (table_size,DEF_PAGE_SIZE ); + header->table_offset = header_size + bitmap_size; + } + +/* Set the journal size. */ +if (bitmap_size<=0 && table_size<=0 ) { + header->journal_size =0 ;/* No need to use journal. */ + }else if (journal_size<0 ) { +/* Disable the use of journal, which reduces overhead but may cause +* data corruption if the host crashes. This is a valid configuration +* for some use cases, where data integrity is not critical. */ + header->journal_size =0 ; + }else { +if (journal_size ==0 ) { +/* No journal size is specified. Use a default size. */ + journal_size =JOURNAL_SIZE ; + } +if (table_size>0 ) { +/* Make sure that the journal is at least large enough to record +* all table changes in one shot, which is the extremely unlikely +* worst case. */ +int64_t vsize = virtual_disk_size + header->chunk_size -1 ; +int64_t table_entries = vsize / header->chunk_size; +int64_t min_journal_size =calc_min_journal_size (table_entries); +if (journal_size< min_journal_size) { + journal_size = min_journal_size; + } + } + journal_size =ROUND_UP (journal_size,DEF_PAGE_SIZE ); + header->journal_size = journal_size; + header->journal_offset = header_size + bitmap_size + table_size; + } + +const int64_t metadata_size = header_size + bitmap_size + table_size + + prefetch_profile_size +MAX (0 , journal_size); + header->metadata_size = metadata_size; + + fd =open (filename,O_WRONLY |O_CREAT |O_TRUNC |O_BINARY ,0644 ); +if (fd<0 ) { +fprintf (stderr,"Failed to open %s\n" , filename); + goto fail; + } +fvd_header_cpu_to_le (header); + +if (qemu_write_full (fd, header, header_size) != header_size) { +fprintf (stderr,"Failed to write the header of %s\n" , filename); + goto fail; + } + +/* Initialize the bitmap. */ +if (bitmap_size>0 ) { +uint8_t *bitmap =my_qemu_mallocz (bitmap_size); + ret =qemu_write_full (fd, bitmap, bitmap_size); +my_qemu_free (bitmap); +if (ret != bitmap_size) { +fprintf (stderr,"Failed to zero out the bitmap of %s\n" , filename); + goto fail; + } + } + +/* Initialize the table. */ +if (table_size>0 ) { +/* Set all entries to EMPTY_TABLE (0xFFFFFFFF). */ +uint8_t *empty_table =my_qemu_malloc (table_size); +memset (empty_table,0xFF , table_size); + ret =qemu_write_full (fd, empty_table, table_size); +my_qemu_free (empty_table); +if (ret != table_size) { +fprintf (stderr,"Failed to write the table of %s\n." , filename); + goto fail; + } + } + +/* Initialize the journal. */ +if (journal_size>0 ) { +uint8_t *empty_journal =my_qemu_mallocz (journal_size); + ret =qemu_write_full (fd, empty_journal, journal_size); +my_qemu_free (empty_journal); +if (ret != journal_size) { +fprintf (stderr,"Failed to initialize the journal for %s\n." , + filename); + goto fail; + } + } + +close (fd); + ret =0 ; + +if (bs&& hole_size>0 ) { + ret =search_holes (filename, (size_t ) bitmap_size, header_size, bs, + base_img_size /512 , hole_size, block_size); + } + +if (bs) { +bdrv_close (bs); + } +my_qemu_free (header); +return ret; + + fail: +if (bs) { +bdrv_close (bs); + } +close (fd); +my_qemu_free (header); +return -1 ; +} + +/* For the optimization called "free write to zero-filled blocks". See Section +* 3.3.3 of the FVD-cow paper. */ +static inline int search_holes (const char *filename,size_t bitmap_size, +int32_t bitmap_start_offset, +BlockDriverState * bs,int64_t nb_sectors, +int32_t hole_size,int32_t block_size) +{ +const int fd =open (filename,O_RDWR |O_BINARY |O_LARGEFILE ,0 ); +if (fd<0 ) { +fprintf (stderr,"Failed to open %s for read and write.\n" , filename); +return -1 ; + } + +printf ("Searching zero-filled sectors in the base image. Please wait..." ); +fflush (stdout); + +uint8_t *bitmap = + (uint8_t *)mmap (NULL , bitmap_size,PROT_READ |PROT_WRITE ,MAP_SHARED , + fd, (off_t ) bitmap_start_offset); +if (bitmap ==MAP_FAILED ) { +fprintf (stderr,"Failed to mmap() %s\n" , filename); +close (fd); +return -1 ; + } + +if (hole_size< block_size) { + hole_size = block_size; + } + hole_size =ROUND_UP (hole_size, block_size); + nb_sectors =ROUND_DOWN (nb_sectors, hole_size); +const int sectors_per_hole = hole_size /512 ; +const int sectors_per_block = block_size /512 ; +int num_int64_in_hole = hole_size /8 ; +int64_t hole_count =0 ; +int i, ret =0 ; +int64_t sec =0 ; +uint8_t *p =my_qemu_blockalign (bs, hole_size); + +while (sec< nb_sectors) { +int64_t *q; + +if (bdrv_read (bs, sec, p, sectors_per_hole)<0 ) { +fprintf (stderr,"Error in reading the base image\n" ); + ret =-1 ; + goto done; + } + +/* All zeros? */ + q = (int64_t *) p; +for (i =0 ; i< num_int64_in_hole; i++) { +if (*q !=0 ) { +break ; + } + q++; + } + +if (i< num_int64_in_hole) { +/* This is not a hole. */ + sec += sectors_per_hole; + }else { +/* These sectors consist of only zeros. Set the flag to +* indicate that there is no need to read this sector from the +* base image. See Section 3.3.3 of the FVD-cow paper for the +* rationale. */ + hole_count++; +int64_t end = sec + sectors_per_hole; +while (sec< end) { +int block_num = sec / sectors_per_block; +int64_t bitmap_byte_offset = block_num /8 ; +uint8_t bitmap_bit_offset = block_num %8 ; +int8_t mask = (uint8_t ) (0x01 << bitmap_bit_offset); +uint8_t b = bitmap[bitmap_byte_offset]; +if (!(b& mask)) { + b |= mask; + bitmap[bitmap_byte_offset] |= mask; + } + sec += sectors_per_block; + } + } + } + + done: +printf ("\nFound %" PRId64 +" zero-filled hole regions. Image creation done.\n" , hole_count); +my_qemu_vfree (p); +munmap (bitmap, bitmap_size); +close (fd); +return ret; +} + +static QEMUOptionParameter fvd_create_options[] = { + { + .name =BLOCK_OPT_SIZE , + .type =OPT_SIZE , + .help ="Virtual disk size" }, + { + .name ="compact_image" , + .type =OPT_FLAG , + .help ="compact_image=on|off" }, + { + .name ="block_size" , + .type =OPT_SIZE , + .help ="Block size" }, + { + .name ="chunk_size" , + .type =OPT_SIZE , + .help ="Chunk size" }, + { + .name ="storage_grow_unit" , + .type =OPT_SIZE , + .help ="Storage grow unit" }, + { + .name ="add_storage_cmd" , + .type =OPT_STRING , + .help ="Command to add storage when FSI runs out of space" }, + { + .name =BLOCK_OPT_BACKING_FILE , + .type =OPT_STRING , + .help ="File name of a backing image" }, + { + .name =BLOCK_OPT_BACKING_FMT , + .type =OPT_STRING , + .help ="Image format of the backing image" }, + { + .name ="data_file" , + .type =OPT_STRING , + .help ="File name of a separate data file" }, + { + .name ="data_file_fmt" , + .type =OPT_STRING , + .help ="Image format of the separate data file" }, + { + .name ="copy_on_read" , + .type =OPT_FLAG , + .help ="copy_on_read=on|off" }, + { + .name ="prefetch_start_delay" , + .type =OPT_NUMBER , + .help ="Delay in seconds before starting whole image prefetching. " +"Prefetching is disabled if the delay is not a positive number." }, + { + .name ="detect_sparse_hole" , + .type =OPT_SIZE , + .help ="Minimum size (in bytes) of a continuous zero-filled region to be " +"considered as a sparse file hole in the backing image (setting it " +"to 0 turns off sparse file detection)" }, + { + .name ="journal_size" , + .type =OPT_SIZE , + .help ="Journal size" }, + {NULL } +};block/fvd-debug.c new file +406
-0@@ -0,0 +1,406 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang<[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this module implements debugging functions for +* the Fast Virtual Disk (FVD) format. +*============================================================================*/ + +#ifndef ENABLE_TRACE_IO +# define TRACE_REQUEST (...) do {} while (0) +# define TRACE_STORE_IN_FVD (...) do {} while (0) + +#else +/* Monitor IO on a specific sector that triggers bugs. */ +static inline void debug_sector (int64_t sector_num) +{ +if (FALSE) { +if (sector_num == ((int64_t )1023990LL )) { +QPAUSE ("right sector" ); + } + } +} + +static void TRACE_REQUEST (int do_write,int64_t sector_num,int nb_sectors) +{ +if (do_write) { +QDEBUG ("TRACE_REQUEST: write sector_num=%" PRId64 +" nb_sectors=%d\n [ " , sector_num, nb_sectors); + }else { +QDEBUG ("TRACE_REQUEST: read sector_num=%" PRId64" nb_sectors=%d\n" +"[ " , sector_num, nb_sectors); + } + +int64_t end = sector_num + nb_sectors; +int64_t sec; +for (sec = sector_num; sec< end; sec++) { +QDEBUG ("sec%" PRId64" " , sec); +debug_sector (sec); + } +QDEBUG (" ]\n" ); +} + +static void TRACE_STORE_IN_FVD (const char *str,int64_t sector_num, +int nb_sectors) +{ +QDEBUG ("TRACE_STORE: %s sector_num=%" PRId64" nb_sectors=%d\n [ " , + str, sector_num, nb_sectors); +int64_t end = sector_num + nb_sectors; +int64_t sec; +for (sec = sector_num; sec< end; sec++) { +QDEBUG ("sec%" PRId64" " , sec); +debug_sector (sec); + } +QDEBUG (" ]\n" ); +} +#endif + +#ifndef FVD_DEBUG +# define my_qemu_malloc qemu_malloc +# define my_qemu_mallocz qemu_mallocz +# define my_qemu_blockalign qemu_blockalign +# define my_qemu_free qemu_free +# define my_qemu_vfree qemu_vfree +# define my_qemu_aio_get qemu_aio_get +# define my_qemu_aio_release qemu_aio_release +# define COPY_UUID (to,from) do {} while (0) + +#else +FILE *__fvd_debug_fp; +static unsigned long long int fvd_uuid =1 ; +static int64_t pending_qemu_malloc =0 ; +static int64_t pending_qemu_aio_get =0 ; +static int64_t pending_local_writes =0 ; +static const char *alloc_file; +static int alloc_line; + +#define my_qemu_malloc (size) \ + ((void*)(alloc_file=__FILE__, alloc_line=__LINE__, _my_qemu_malloc(size))) + +#define my_qemu_mallocz (size) \ + ((void*)(alloc_file=__FILE__, alloc_line=__LINE__, _my_qemu_mallocz(size))) + +#define my_qemu_blockalign (bs,size) \ + ((void*)(alloc_file=__FILE__, \ + alloc_line=__LINE__, \ + _my_qemu_blockalign(bs,size))) + +#define my_qemu_aio_get (pool,bs,cb,op) \ + ((void*)(alloc_file=__FILE__, \ + alloc_line=__LINE__, \ + _my_qemu_aio_get(pool,bs,cb,op))) + +#define my_qemu_free (p) \ + (alloc_file=__FILE__, alloc_line=__LINE__, _my_qemu_free(p)) + +#define my_qemu_vfree (p) \ + (alloc_file=__FILE__, alloc_line=__LINE__, _my_qemu_vfree(p)) + +static void COPY_UUID (FvdAIOCB * to,FvdAIOCB * from) +{ +if (from) { + to->uuid = from->uuid; +FVD_DEBUG_ACB (to); + } +} + +#ifdef DEBUG_MEMORY_LEAK +# define MAX_TRACER 10485760 +static int alloc_tracer_used =1 ;/* slot 0 is not used. */ +static void **alloc_tracers =NULL ; + +static void __attribute__ ((constructor))init_mem_alloc_tracers (void ) +{ +if (!alloc_tracers) { + alloc_tracers =qemu_mallocz (sizeof (void *) *MAX_TRACER ); + } +} + +static void trace_alloc (void *p,size_t size) +{ +alloc_tracer_t *t = p; + t->magic =FVD_ALLOC_MAGIC ; + t->alloc_file = alloc_file; + t->alloc_line = alloc_line; + t->size = size; + +if (alloc_tracer_used<MAX_TRACER ) { + t->alloc_tracer = alloc_tracer_used++; + alloc_tracers[t->alloc_tracer] = t; +QDEBUG ("Allocate memory using tracer%d in %s on line %d.\n" , + t->alloc_tracer, alloc_file, alloc_line); + }else { + t->alloc_tracer =0 ; + } + +/* Set header and footer to detect out-of-range writes. */ +if (size != (size_t ) -1 ) { +uint8_t *q = (uint8_t *) p; +uint64_t *header = (uint64_t *) (q +512 -sizeof (uint64_t )); +uint64_t *footer = (uint64_t *) (q + size -512 ); + *header =FVD_ALLOC_MAGIC ; + *footer =FVD_ALLOC_MAGIC ; + } +} + +static void trace_free (void *p) +{ +alloc_tracer_t *t = p; + +QDEBUG ("Free memory with tracer%d in %s on line %d.\n" , + t->alloc_tracer, alloc_file, alloc_line); +ASSERT (t->magic ==FVD_ALLOC_MAGIC && t->alloc_tracer>=0 ); + +/* Check header and footer to detect out-of-range writes. */ +if (t->size != (size_t ) -1 ) { +uint8_t *q = (uint8_t *) p; +uint64_t *header = (uint64_t *) (q +512 -sizeof (uint64_t )); +uint64_t *footer = (uint64_t *) (q + t->size -512 ); +ASSERT (*header ==FVD_ALLOC_MAGIC ); +ASSERT (*footer ==FVD_ALLOC_MAGIC ); + } + +if (t->alloc_tracer) { +ASSERT (alloc_tracers[t->alloc_tracer] == t); + alloc_tracers[t->alloc_tracer] =NULL ; + t->alloc_tracer = -INT_MAX ; + }else { + t->alloc_tracer *=-1 ;/* Guard against double free. */ + } +} + +static void dump_alloc_tracers (void ) +{ +int unfreed =0 ; +int i; +for (i =1 ; i< alloc_tracer_used; i++) { +if (!alloc_tracers[i]) { +continue ; + } + + unfreed++; +alloc_tracer_t *t = alloc_tracers[i]; + +if (t->size == (size_t ) -1 ) { +FvdAIOCB *acb =container_of (alloc_tracers[i], FvdAIOCB, tracer); +ASSERT (acb->magic ==FVDAIOCB_MAGIC ); +QDEBUG ("Memory %p with tracer%d allocated in %s on line %d " +"(FvdAIOCB acb%llu-%p) is not freed. magic %s\n" , + alloc_tracers[i], i, t->alloc_file, t->alloc_line, + acb->uuid, acb, + t->magic ==FVD_ALLOC_MAGIC ?"correct" :"wrong" ); + }else { +QDEBUG ("Memory %p with tracer%d allocated in %s on line %d is " +"not freed. magic %s\n" , + alloc_tracers[i], i, t->alloc_file, t->alloc_line, + t->magic ==FVD_ALLOC_MAGIC ?"correct" :"wrong" ); + +uint8_t *q = (uint8_t *) t; +uint64_t *header = (uint64_t *) (q +512 -sizeof (uint64_t )); +uint64_t *footer = (uint64_t *) (q + t->size -512 ); +ASSERT (*header ==FVD_ALLOC_MAGIC ); +ASSERT (*footer ==FVD_ALLOC_MAGIC ); + } + } + +QDEBUG ("Unfreed memory allocations: %d\n" , unfreed); +} +#endif + +static inline void *_my_qemu_aio_get (AIOPool * pool,BlockDriverState * bs, +BlockDriverCompletionFunc * cb, +void *opaque) +{ + pending_qemu_aio_get++; +FvdAIOCB *acb = (FvdAIOCB *)qemu_aio_get (&fvd_aio_pool, bs, cb, opaque); + acb->uuid = ++fvd_uuid; + acb->magic =FVDAIOCB_MAGIC ; + +FVD_DEBUG_ACB (acb); + +#ifdef DEBUG_MEMORY_LEAK +trace_alloc (&acb->tracer,-1 ); +#endif + +return acb; +} + +static inline void my_qemu_aio_release (void *p) +{ + pending_qemu_aio_get--; +ASSERT (pending_qemu_aio_get>=0 ); + +#ifdef DEBUG_MEMORY_LEAK +FvdAIOCB *acb = p; +trace_free (&acb->tracer); +#endif + +qemu_aio_release (p); +} + +static inline void *_my_qemu_malloc (size_t size) +{ +ASSERT (size>0 ); + pending_qemu_malloc++; +#ifndef DEBUG_MEMORY_LEAK +return qemu_malloc (size); +#else + + size +=1024 ;/* 512 bytes header and 512 bytes footer. */ +uint8_t *ret =qemu_malloc (size); +trace_alloc (ret, size); +return ret +512 ; +#endif +} + +static inline void *_my_qemu_mallocz (size_t size) +{ +ASSERT (size>0 ); + pending_qemu_malloc++; +#ifndef DEBUG_MEMORY_LEAK +return qemu_mallocz (size); +#else + + size +=1024 ;/* 512 bytes header and 512 bytes footer. */ +uint8_t *ret =qemu_mallocz (size); +trace_alloc (ret, size); +return ret +512 ; +#endif +} + +static inline void *_my_qemu_blockalign (BlockDriverState * bs,size_t size) +{ +ASSERT (size>0 ); + pending_qemu_malloc++; + +#ifndef DEBUG_MEMORY_LEAK +return qemu_blockalign (bs, size); +#else + + size +=1024 ;/* 512 bytes header and 512 bytes footer. */ +uint8_t *ret =qemu_blockalign (bs, size); +trace_alloc (ret, size); +return ret +512 ; +#endif +} + +static inline void _my_qemu_free (void *ptr) +{ + pending_qemu_malloc--; +ASSERT (pending_qemu_malloc>=0 ); +#ifndef DEBUG_MEMORY_LEAK +qemu_free (ptr); +#else + +uint8_t *q = ((uint8_t *) ptr) -512 ; +trace_free (q); +qemu_free (q); +#endif +} + +static inline void _my_qemu_vfree (void *ptr) +{ + pending_qemu_malloc--; +ASSERT (pending_qemu_malloc>=0 ); +#ifndef DEBUG_MEMORY_LEAK +qemu_vfree (ptr); +#else + +uint8_t *q = ((uint8_t *) ptr) -512 ; +trace_free (q); +qemu_vfree (q); +#endif +} + +static void count_pending_requests (BDRVFvdState * s) +{ +int m =0 , k =0 ; +FvdAIOCB *w; + + QLIST_FOREACH (w,&s->copy_locks, copy_lock.next) { + m++; +QDEBUG ("copy_lock: acb%llu-%p\n" , w->uuid, w); + } + + QLIST_FOREACH (w,&s->write_locks, write.next_write_lock) { + k++; +QDEBUG ("write_lock: acb%llu-%p\n" , w->uuid, w); + } + +QDEBUG ("Debug_memory_leak: copy_locks=%d write_locks=%d\n" , m, k); +} + +static void dump_resource_summary (BDRVFvdState * s) +{ +#ifdef DEBUG_MEMORY_LEAK +dump_alloc_tracers (); +#endif + +QDEBUG ("Resource summary: outstanding_copy_on_read_data=%" PRId64 +" total_copy_on_read_data=%" PRId64" total_prefetch_data=%" PRId64 +" " " pending_qemu_malloc=%" PRId64" pending_qemu_aio_get=%" PRId64 +" pending_local_writes=%" PRId64"\n" , + s->outstanding_copy_on_read_data, s->total_copy_on_read_data, + s->total_prefetch_data, pending_qemu_malloc, pending_qemu_aio_get, + pending_local_writes); +count_pending_requests (s); +} + +/* Monitor processing a specific FvdAIOCB that triggers bugs. */ +void FVD_DEBUG_ACB (void *p) +{ +if (FALSE) { +FvdAIOCB *acb = p; + +/* Is it FvdAIOCB? */ +if (acb->magic !=FVDAIOCB_MAGIC || acb->common.bs->drv !=&bdrv_fvd) { +/* Is it CompactChildCB? */ +CompactChildCB *child = p; + acb = child->acb; +if (acb->magic !=FVDAIOCB_MAGIC + || acb->common.bs->drv !=&bdrv_fvd + || (acb->type !=OP_LOAD_COMPACT +&& acb->type !=OP_STORE_COMPACT )) { +return ; + } + } + +if (acb->uuid ==20ULL ) { +QPAUSE ("Processing the right acb" ); + } + } +} + +void init_fvd_debug_fp (void ) +{ +char buf[256 ]; +sprintf (buf,"/tmp/fvd.log-%d" ,getpid ()); +if ((__fvd_debug_fp =fopen (buf,"wt" )) ==NULL ) { + __fvd_debug_fp = stdout; + } +} +#endif + +void fvd_check_memory_usage (void ) +{ +ASSERT (pending_qemu_malloc ==0 ); +} + +int fvd_get_copy_on_read (BlockDriverState * bs) +{ +BDRVFvdState *s = bs->opaque; +return s->copy_on_read; +} + +void fvd_set_copy_on_read (BlockDriverState * bs,int copy_on_read) +{ +BDRVFvdState *s = bs->opaque; + s->copy_on_read = copy_on_read; +}block/fvd-ext.h new file +71
-0@@ -0,0 +1,71 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang<[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this header file contains functions of the FVD block +* device driver that are used by other external modules. These functions are +* mainly for testing and debugging urposes. +*============================================================================*/ + +#ifndef __fvd_debug_h__ +#define __fvd_debug_h__ + +//#define FVD_DEBUG + +int fvd_get_copy_on_read (BlockDriverState *bs); +void fvd_set_copy_on_read (BlockDriverState *bs,int copy_on_read); +void fvd_check_memory_usage (void ); +void fvd_init_prefetch (void * bs); +void fvd_enable_host_crash_test (void ); + +#ifndef TRUE +# define TRUE 1 +#endif +#ifndef FALSE +# define FALSE 0 +#endif + +#ifndef FVD_DEBUG +# define QDEBUG (format,...) do {} while (0) +# define ASSERT (x) do {} while (0) +# define FVD_DEBUG_ACB (...) do {} while (0) +# define QPAUSE (...) do {} while (0) + +#else + +extern FILE *__fvd_debug_fp; +void init_fvd_debug_fp (void ); +void FVD_DEBUG_ACB (void *p); +# define QDEBUG (format,...) \ + do { \ + if (__fvd_debug_fp==NULL) init_fvd_debug_fp(); \ + fprintf (__fvd_debug_fp, format, ##__VA_ARGS__); \ + fflush(__fvd_debug_fp); \ + } while(0) + +# define ASSERT (x) \ + do { \ + if (!(x)) { \ + fprintf (stderr, "Assertion failed in process %d at %s:%d. " \ + "Waiting for debugging...\n", getpid(),__FILE__, __LINE__); \ + fgetc (stdin); exit (1); \ + } \ + } while (0) \ + +# define QPAUSE (format,...) \ + do { \ + printf (format, ##__VA_ARGS__); \ + printf (" Pause process %d for debugging...\n", getpid()); \ + fgetc (stdin); \ + } while (0) + +#endif + +#endif block/fvd.c new file +127
-0@@ -0,0 +1,127 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang<[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this module implements the QEMU block device driver +* for the Fast Virtual Disk (FVD) format. See the following companion +* papers for a detailed description of FVD: +* 1. The so-called "FVD-cow paper": +* "FVD: a High-Performance Virtual Machine Image Format for Cloud", +* by Chunqiang Tang, 2010. +* 2. The so-called "FVD-compact paper": +* "FVD: a High-Performance Virtual Machine Image Format for Cloud +* with Sparse Image Capability", by Chunqiang Tang, 2010. +*============================================================================*/ + +#include "block/fvd.h" + +//#define ENABLE_TRACE_IO +//#define DEBUG_MEMORY_LEAK +//#define SIMULATED_TEST_WITH_QEMU_IO + +#ifndef FVD_DEBUG +#undef DEBUG_MEMORY_LEAK +#undef ENABLE_TRACE_IO +#undef SIMULATED_TEST_WITH_QEMU_IO +#endif + +/* Use include to avoid exposing too many FVD symbols, and to allow inline +* function optimization. */ +#include "block/fvd-utils.c" +#include "block/fvd-debug.c" +#include "block/fvd-misc.c" +#include "block/fvd-create.c" +#include "block/fvd-open.c" +#include "block/fvd-read.c" +#include "block/fvd-write.c" +#include "block/fvd-load.c" +#include "block/fvd-store.c" +#include "block/fvd-journal.c" +#include "block/fvd-prefetch.c" + +static AIOPool fvd_aio_pool = { + .aiocb_size =sizeof (FvdAIOCB), + .cancel = fvd_aio_cancel, +}; + +static BlockDriver bdrv_fvd = { + .format_name ="fvd" , + .instance_size =sizeof (BDRVFvdState), + .bdrv_create = fvd_create, + .bdrv_probe = fvd_probe, + .bdrv_file_open = fvd_open, + .bdrv_close = fvd_close, + .bdrv_is_allocated = fvd_is_allocated, + .bdrv_flush = fvd_flush, + .bdrv_aio_readv = fvd_aio_readv, + .bdrv_aio_writev = fvd_aio_writev, + .bdrv_aio_flush = fvd_aio_flush, + .create_options = fvd_create_options, + .bdrv_get_info = fvd_get_info, + .bdrv_update = fvd_update, + .bdrv_has_zero_init = fvd_has_zero_init +}; + +static void bdrv_fvd_init (void ) +{ +bdrv_register (&bdrv_fvd); +} + +block_init (bdrv_fvd_init); + +/* +* Since bdrv_close may not be properly invoked on a VM shutdown, we +* use a destructor to flush metadata to disk. This only affects +* performance and does not affect correctness. +* See Section 3.3.4 of the FVD-cow paper for the rationale. +*/ +extern QTAILQ_HEAD (, BlockDriverState) bdrv_states; +static void __attribute__ ((destructor))flush_fvd_bitmap_to_disk (void ) +{ +BlockDriverState *bs; + QTAILQ_FOREACH (bs,&bdrv_states, list) { +if (bs->drv ==&bdrv_fvd) { +flush_metadata_to_disk_on_exit (bs); + +#ifdef FVD_DEBUG +dump_resource_summary (bs->opaque); +#endif + } + } +} + +/* +* TODOs: Below are some potential enhancements for future development: +* 1. Handle storage leak on failure. +* +* 2. Profile-directed prefetch. See Section 3.4.1 of the FVD-cow paper. +* Related metadata are FvdHeader.prefetch_profile_offset and +* FvdHeader.prefetch_profile_entries, +* FvdHeader.profile_directed_prefetch_start_delay, +* FvdHeader.generate_prefetch_profile. +* +* 3. Cap the prefetch throughput at the upper limit. See Section 3.4.2 of +* the FVD-cow paper. Related metadata are +* FvdHeader.prefetch_max_read_throughput and +* FvdHeader.prefetch_max_write_throughput. +* +* 4. Support write through to the base image. When a VM issues a write +* request, in addition to saving the data in the FVD data file, also save the +* data in the base image if the address of write request is not beyond the +* size of the base image (this of course requires the base image NOT to be +* 'read_only'. This feature changes the semantics of copy-on-write, but it +* suits a different use case, where the base image is stored on a remote +* storage server, and the FVD image is stored on a local disk and acts as a +* write-through cache of the base image. This can be used to cache and +* improve the performance of persistent storage on network-attached storage, +* e.g., Amazon EBS. This feature is not described in the FVD-cow paper as it +* would complicate the discussion. Related metadata are +* FvdHeader.write_updates_base_img. +*/ block/fvd.h new file +481
-0@@ -0,0 +1,481 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang<[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this is the header of the FVD block device driver. +*============================================================================*/ + +#include <sys/vfs.h> +#include <sys/mman.h> +#include <pthread.h> +#include <execinfo.h> +#include <stdlib.h> +#include <sys/ioctl.h> +#include <stdint.h> +#include <stdio.h> +#include <inttypes.h> +#include "block_int.h" +#include "osdep.h" +#include "qemu-option.h" +#include "qemu-timer.h" +#include "block.h" +#include "qemu-queue.h" +#include "qemu-common.h" +#include "block/blksim.h" +#include "block/fvd-ext.h" + +#define FVD_MAGIC (('Q'<< 24) | ('C'<< 16) | (0xF5<< 8) | 0xA9) +#define FVD_VERSION 1 + +/* Profile-directed prefetch. (to be implemented). */ +typedef struct __attribute__ ((__packed__ ))PrefetchProfileEntry { +int64_t offset;/* in bytes */ + +/* In the unit of FvdHeader.prefetch_profile_entry_len_unit, i.e., +* len_in_bytes = len * FvdHeader.unit_of_PrefetchProfileEntry_len. */ +uint32_t len; +}PrefetchProfileEntry ; + +/* +* The FVD format consists of: +* + Header fields of FvdHeader. +* + Bitmap, starting on a 4KB page boundary at a location specified by +* FvdHeader.bitmap_offset. +* + Table, starting on a 4KB page boundary at a location specified by +* FvdHeader.table_offset. +* + Journal, starting on a 4KB page boundary at a location specified by +* FvdHeader.journal_offset. +* + Prefetch profile entries, starting on a 4KB page boundary at a location +* specified by FvdHeader.prefetch_profile_offset. (to be implemented) +* + Virtual disk data, starting on a 4KB page boundary. Optionally, disk +* data can be stored in a separate data file specified by +* FvdHeader.data_file. +*/ +typedef struct __attribute__ ((__packed__ ))FvdHeader { +uint32_t magic; +uint32_t version; + +/* This field is set to TRUE after whole-image prefetching finishes. */ +int32_t all_data_in_fvd_img; + +int64_t virtual_disk_size;/* in bytes. Disk size perceived by the VM. */ +int64_t metadata_size;/* in bytes. */ +char base_img[1024 ]; +char base_img_fmt[16 ]; +int64_t base_img_size;/* in bytes. */ +int64_t bitmap_offset;/* in bytes. Aligned on DEF_PAGE_SIZE. */ +int64_t bitmap_size;/* in bytes. Rounded up to DEF_PAGE_SIZE */ +int32_t block_size;/* in bytes. */ +int32_t copy_on_read;/* TRUE or FALSE */ +int64_t max_outstanding_copy_on_read_data;/* in bytes. */ + +/* If (data_file[0]==0), the FVD metadata and data are stored in one file.*/ +char data_file[1024 ]; +char data_file_fmt[16 ]; + +/******** Begin: for prefetching. *******************************/ +/* in seconds. -1 means disable whole image prefetching. */ +int32_t prefetch_start_delay; + +/* in bytes. Aligned on DEF_PAGE_SIZE. (to be implemented) */ +int64_t prefetch_profile_offset; + +/* Number of PrefetchProfileEntry. (to be implemented) */ +int64_t prefetch_profile_entries; + +int32_t num_prefetch_slots;/* Max number of oustanding prefetch writes. */ +int32_t bytes_per_prefetch;/* For whole image prefetching. */ +int32_t prefetch_read_throughput_measure_time;/* in milliseconds. */ +int32_t prefetch_write_throughput_measure_time;/* in milliseconds. */ + +/* Controls the calculation of the moving average of throughput. Must be a +* value between [0,100]. +* actual_normalized_alpha = * prefetch_perf_calc_alpha / 100.0 */ +int32_t prefetch_perf_calc_alpha; + +int32_t prefetch_min_read_throughput;/* in KB/second. */ +int32_t prefetch_min_write_throughput;/* in KB/second. */ +int32_t prefetch_max_read_throughput;/* in KB/second. */ +int32_t prefetch_max_write_throughput;/* in KB/second. */ + +/* in milliseconds. When prefetch read/write throughput is low, prefetch +* pauses for a random time uniformly distributed in +* [0, prefetch_throttle_time]. */ +int32_t prefetch_throttle_time; +/******** End: for prefetching. *******************************/ + +/******** Begin: for compact image. *****************************/ +int32_t compact_image;/* TRUE or FALSE */ +int64_t table_offset;/* in bytes. */ +int64_t chunk_size;/* in bytes. */ +int64_t storage_grow_unit;/* in bytes. */ +char add_storage_cmd[2048 ]; +/******** End: for compact image. *******************************/ + +/******** Begin: for journal. ***********************************/ +int64_t journal_offset;/* in bytes. */ +int64_t journal_size;/* in bytes. */ +int32_t clean_shutdown;/* TRUE if VM's last shutdown was graceful. */ +/******** End: for journal. *************************************/ + +/* +* This field is TRUE if the image mandates that the storage layer +* (BDRVFvdState.fvd_data) must return TRUE for bdrv_has_zero_init(). +* This is the case if the optimization described in Section 3.3.3 of the +* FVD-cow paper is enabled (see function search_holes()). If 'qemu-img +* create' sets need_zero_init to TRUE, 'qemu-img update' can be used to +* manually reset it to FALSE, if the user always manually pre-fills the +* storage (e.g., a raw partition) with zeros. If the image is stored on a +* file system, it already supports zero_init, and hence there is no need +* to manually manipulate this field. +*/ +int32_t need_zero_init; + +/* If TRUE, FVD dumps a prefetch profile after the VM shuts down. +* (to be implemented) */ +int32_t generate_prefetch_profile; + +/* See the comment on PrefetchProfileEntry.len. (to be implemented) */ +int32_t unit_of_PrefetchProfileEntry_len; + +/* in seconds. -1 means disable profile-directed prefetching. +* (to be implemented) */ +int32_t profile_directed_prefetch_start_delay; + +/* Possible values are "no", "writethrough", "writeback", or +* "writenocache". (to be implemented) */ +char write_updates_base_img[16 ]; +}FvdHeader ; + +typedef struct BDRVFvdState { +BlockDriverState *fvd_metadata; +BlockDriverState *fvd_data; +int64_t virtual_disk_size;/*in bytes. */ +int64_t bitmap_offset;/* in sectors */ +int64_t bitmap_size;/* in bytes. */ +int64_t data_offset;/* in sectors. Begin of real data. */ +int64_t nb_sectors_in_base_img; +int32_t block_size;/* in sectors. */ +int copy_on_read;/* TRUE or FALSE */ +int64_t max_outstanding_copy_on_read_data;/* in bytes. */ +int64_t outstanding_copy_on_read_data;/* in bytes. */ +int data_region_prepared;/* TRUE or FALSE */ + QLIST_HEAD(WriteLocks, FvdAIOCB) write_locks; /* All writes. */ + QLIST_HEAD(CopyLocks, FvdAIOCB) copy_locks; /* copy-on-read and CoW. */ + +/* Keep two copies of bitmap to reduce the overhead of updating the +* on-disk bitmap, i.e., copy-on-read and prefetching do not update the +* on-disk bitmap. See Section 3.3.4 of the FVD-cow paper. */ +uint8_t *fresh_bitmap; +uint8_t *stale_bitmap; + +/******** Begin: for prefetching. ***********************************/ +struct FvdAIOCB **prefetch_acb; +int prefetch_state;/* PREFETCH_STATE_RUNNING, FINISHED, or DISABLED. */ +int prefetch_error;/* TRUE or FALSE */ +int num_prefetch_slots; +int num_filled_prefetch_slots; +int next_prefetch_read_slot; +int prefetch_read_active;/* TRUE or FALSE */ +int pause_prefetch_requested;/* TRUE or FALSE */ +int prefetch_start_delay;/* in seconds */ +int64_t unclaimed_prefetch_region_start; +int64_t prefetch_read_time;/* in milliseconds. */ +int64_t prefetch_write_time;/* in milliseconds. */ +int64_t prefetch_data_read;/* in bytes. */ +int64_t prefetch_data_written;/* in bytes. */ +double prefetch_read_throughput;/* in bytes/millisecond. */ +double prefetch_write_throughput;/* in bytes/millisecond. */ +double prefetch_min_read_throughput;/* in bytes/millisecond. */ +double prefetch_min_write_throughput;/* in bytes/millisecond. */ +int64_t prefetch_read_throughput_measure_time;/* in millisecond. */ +int64_t prefetch_write_throughput_measure_time;/* in millisecond.*/ +int prefetch_throttle_time;/* in millisecond. */ +int sectors_per_prefetch; +QEMUTimer *prefetch_timer; +/* prefetch_perf_calc_alpha = FvdHeader.prefetch_perf_calc_alpha/100.0 */ +double prefetch_perf_calc_alpha; +/******** End: for prefetching. ***********************************/ + +/******** Begin: for compact image. *************************************/ +uint32_t *table;/* Mapping table stored in memory in little endian. */ +int64_t data_storage;/* in sectors. */ +int64_t used_storage;/* in sectors. */ +int64_t chunk_size;/* in sectors. */ +int64_t storage_grow_unit;/* in sectors. */ +int64_t table_offset;/* in sectors. */ +char *add_storage_cmd; +/******** Begin: for compact image. *************************************/ + +/******** Begin: for journal. *******************************************/ +int64_t journal_offset;/* in sectors. */ +int64_t journal_size;/* in sectors. */ +int64_t next_journal_sector;/* in sector. */ +int ongoing_journal_updates;/* Number of ongoing journal updates. */ +int dirty_image;/* TRUE or FALSE. */ + +/* Requests waiting for metadata flush and journal recycle to finish. */ + QLIST_HEAD(JournalFlush, FvdAIOCB) wait_for_journal; +/******** End: for journal. ********************************************/ + +#ifdef FVD_DEBUG +int64_t total_copy_on_read_data;/* in bytes. */ +int64_t total_prefetch_data;/* in bytes. */ +#endif +}BDRVFvdState ; + +/* Begin of data type definitions. */ +struct FvdAIOCB ; + +typedef struct JournalCB { +BlockDriverAIOCB *hd_acb; +QEMUIOVector qiov; +struct iovec iov; +QLIST_ENTRY (FvdAIOCB) next_wait_for_journal; +}JournalCB ; + +/* CopyLock is used by AIOWriteCB and AIOCopyCB. */ +typedef struct CopyLock { +QLIST_ENTRY (FvdAIOCB) next; +int64_t begin; +int64_t end; + QLIST_HEAD(DependentWritesHead, FvdAIOCB) dependent_writes; +}CopyLock ; + +typedef struct ChildAIOReadCB { +BlockDriverAIOCB *hd_acb; +struct iovec iov; +QEMUIOVector qiov; +int64_t sector_num; +int nb_sectors; +int done; +}ChildAIOReadCB ; + +typedef struct AIOReadCB { +QEMUIOVector *qiov; +int ret; +ChildAIOReadCB read_backing; +ChildAIOReadCB read_fvd; +}AIOReadCB ; + +/* For copy-on-read and prefetching. */ +typedef struct AIOCopyCB { +BlockDriverAIOCB *hd_acb; +struct iovec iov; +QEMUIOVector qiov; +uint8_t *buf; +int64_t buffered_sector_begin; +int64_t buffered_sector_end; +int64_t last_prefetch_op_start_time;/* For prefetch only. */ +}AIOCopyCB ; + +typedef struct AIOWriteCB { +BlockDriverAIOCB *hd_acb; +QEMUIOVector *qiov; +uint8_t *cow_buf; +QEMUIOVector *cow_qiov; +int64_t cow_start_sector; +int update_table;/* TRUE or FALSE. */ +int ret; +QLIST_ENTRY (FvdAIOCB) next_write_lock;/* See BDRVFvdState.write_locks */ + +/* See FvdAIOCB.write.dependent_writes. */ +QLIST_ENTRY (FvdAIOCB) next_dependent_write; +}AIOWriteCB ; + +/* For AIOStoreCompactCB and AIOLoadCompactCB. */ +typedef struct CompactChildCB { +struct FvdAIOCB *acb; +BlockDriverAIOCB *hd_acb; +}CompactChildCB ; + +/* For storing data to a compact image. */ +typedef struct AIOStoreCompactCB { +CompactChildCB one_child; +CompactChildCB *children; +int update_table; +int num_children; +int finished_children; +struct FvdAIOCB *parent_acb; +int ret; +int soft_write;/*TRUE if the store is caused by copy-on-read or prefetch.*/ +QEMUIOVector *orig_qiov; +}AIOStoreCompactCB ; + +/* For loading data from a compact image. */ +typedef struct AIOLoadCompactCB { +CompactChildCB *children; +CompactChildCB one_child; +int num_children; +int finished_children; +struct FvdAIOCB *parent_acb; +int ret; +QEMUIOVector *orig_qiov; +}AIOLoadCompactCB ; + +typedef struct AIOFlushCB { +BlockDriverAIOCB *data_acb; +BlockDriverAIOCB *metadata_acb; +int num_finished; +int ret; +}AIOFlushCB ; + +typedef struct AIOWrapperCB { +QEMUBH *bh; +}AIOWrapperCB ; + +typedef enum {OP_READ =1 ,OP_WRITE ,OP_COPY ,OP_STORE_COMPACT , +OP_LOAD_COMPACT ,OP_WRAPPER ,OP_FLUSH }op_type ; + +#ifdef FVD_DEBUG +/* For debugging memory leadk. */ +typedef struct alloc_tracer_t { +int64_t magic; +int alloc_tracer; +const char *alloc_file; +int alloc_line; +size_t size; +}alloc_tracer_t ; +#endif + +typedef struct FvdAIOCB { +BlockDriverAIOCB common; +op_type type; +int64_t sector_num; +int nb_sectors; +JournalCB jcb;/* For AIOWriteCB and AIOStoreCompactCB. */ +CopyLock copy_lock;/* For AIOWriteCB and AIOCopyCB. */ + +/* Use a union so that all requests can efficiently share one big AIOPool.*/ +union { +AIOWrapperCB wrapper; +AIOReadCB read; +AIOWriteCB write; +AIOCopyCB copy; +AIOLoadCompactCB load; +AIOStoreCompactCB store; +AIOFlushCB flush; + }; + +#ifdef FVD_DEBUG +int64_t magic; +alloc_tracer_t tracer; + +/* Uniquely identifies a request across all processing activities. */ +unsigned long long int uuid; +#endif +}FvdAIOCB ; + +static AIOPool fvd_aio_pool; +static BlockDriver bdrv_fvd; +static QEMUOptionParameter fvd_create_options[]; + +/* Function prototypes. */ +static int do_aio_write (struct FvdAIOCB *acb); +static void finish_write_data (void *opaque,int ret); +static void restart_dependent_writes (struct FvdAIOCB *acb); +static void finish_prefetch_read (void *opaque,int ret); +static int read_fvd_header (BDRVFvdState * s,FvdHeader * header); +static int update_fvd_header (BDRVFvdState * s,FvdHeader * header); +static void fvd_aio_cancel (BlockDriverAIOCB * blockacb); +static BlockDriverAIOCB *store_data_in_compact_image (struct FvdAIOCB *acb, +int soft_write,struct FvdAIOCB *parent_acb,BlockDriverState * bs, +int64_t sector_num,QEMUIOVector * qiov,int nb_sectors, +BlockDriverCompletionFunc * cb,void *opaque); +static BlockDriverAIOCB *load_data_from_compact_image (struct FvdAIOCB *acb, +struct FvdAIOCB *parent_acb,BlockDriverState * bs, +int64_t sector_num,QEMUIOVector * qiov,int nb_sectors, +BlockDriverCompletionFunc * cb,void *opaque); +static void free_write_resource (struct FvdAIOCB *acb); +static void write_metadata_to_journal (struct FvdAIOCB *acb); +static void flush_metadata_to_disk (BlockDriverState * bs); +static void free_journal_sectors (BDRVFvdState * s); +static int fvd_create (const char *filename,QEMUOptionParameter * options); +static int fvd_probe (const uint8_t * buf,int buf_size,const char *filename); +static int fvd_open (BlockDriverState * bs,const char *filename,int flags); +static void fvd_close (BlockDriverState * bs); +static int fvd_is_allocated (BlockDriverState * bs,int64_t sector_num, +int nb_sectors,int *pnum); +static int fvd_flush (BlockDriverState * bs); +static BlockDriverAIOCB *fvd_aio_readv (BlockDriverState * bs, +int64_t sector_num,QEMUIOVector * qiov,int nb_sectors, +BlockDriverCompletionFunc * cb,void *opaque); +static BlockDriverAIOCB *fvd_aio_writev (BlockDriverState * bs, +int64_t sector_num,QEMUIOVector * qiov,int nb_sectors, +BlockDriverCompletionFunc * cb,void *opaque); +static BlockDriverAIOCB *fvd_aio_flush (BlockDriverState * bs, +BlockDriverCompletionFunc * cb,void *opaque); +static int fvd_get_info (BlockDriverState * bs,BlockDriverInfo * bdi); +static int fvd_update (BlockDriverState * bs,int argc,char **argv); +static int fvd_has_zero_init (BlockDriverState * bs); +static void fvd_read_cancel (FvdAIOCB * acb); +static void fvd_write_cancel (FvdAIOCB * acb); +static void fvd_copy_cancel (FvdAIOCB * acb); +static void fvd_load_compact_cancel (FvdAIOCB * acb); +static void fvd_store_compact_cancel (FvdAIOCB * acb); +static void fvd_wrapper_cancel (FvdAIOCB * acb); +static void flush_metadata_to_disk_on_exit (BlockDriverState *bs); +static inline BlockDriverAIOCB *load_data (FvdAIOCB * parent_acb, +BlockDriverState * bs,int64_t sector_num,QEMUIOVector * orig_qiov, +int nb_sectors,BlockDriverCompletionFunc * cb,void *opaque); +static inline BlockDriverAIOCB *store_data (int soft_write, +FvdAIOCB * parent_acb,BlockDriverState * bs,int64_t sector_num, +QEMUIOVector * orig_qiov,int nb_sectors, +BlockDriverCompletionFunc * cb,void *opaque); + +/* Default configurations. */ +#define DEF_PAGE_SIZE 4096/* bytes */ +#define BYTES_PER_PREFETCH 1048576/* bytes */ +#define PREFETCH_THROTTLING_TIME 30000/* milliseconds */ +#define NUM_PREFETCH_SLOTS 2 +#define PREFETCH_MIN_MEASURE_READ_TIME 100/* milliseconds */ +#define PREFETCH_MIN_MEASURE_WRITE_TIME 100/* milliseconds */ +#define PREFETCH_MIN_READ_THROUGHPUT 5120/* KB/s */ +#define PREFETCH_MIN_WRITE_THROUGHPUT 5120/* KB/s */ +#define PREFETCH_MAX_READ_THROUGHPUT 1000000000L/* KB/s */ +#define PREFETCH_MAX_WRITE_THROUGHPUT 1000000000L/* KB/s */ +#define PREFETCH_PERF_CALC_ALPHA 80/* in [0,100]. */ +#define MAX_OUTSTANDING_COPY_ON_READ_DATA 2000000/* bytes */ +#define MODERATE_BITMAP_SIZE 4194304L/* bytes */ +#define CHUNK_SIZE 1048576LL/* bytes */ +#define JOURNAL_SIZE 16777216LL/* bytes */ +#define STORAGE_GROW_UNIT 104857600LL/* bytes */ + +/* State of BDRVFvdState.prefetch_state. */ +#define PREFETCH_STATE_RUNNING 1 +#define PREFETCH_STATE_FINISHED 2 +#define PREFETCH_STATE_DISABLED 3 + +/* For convience. */ +#define ROUND_UP (x, base) ((((x)+(base)-1) / (base)) * (base)) +#define ROUND_DOWN (x, base) ((((x) / (base)) * (base))) +#define BOOL (x) ((x) ? "true" : "false") +#define EMPTY_TABLE ((uint32_t)0xFFFFFFFF) +#define DIRTY_TABLE ((uint32_t)0x80000000) +#define READ_TABLE (entry) (le32_to_cpu(entry)& ~DIRTY_TABLE) +# define FVDAIOCB_MAGIC ((uint64_t)0x3A8FCE89325B976DULL) +# define FVD_ALLOC_MAGIC ((uint64_t)0x4A7dCEF9925B976DULL) +#define IS_EMPTY (entry) ((entry) == EMPTY_TABLE) +#define IS_DIRTY (entry) (le32_to_cpu(entry)& DIRTY_TABLE) +#define WRITE_TABLE (entry,id) ((entry) = cpu_to_le32(id)) +#define READ_TABLE2 (entry) \ + ((entry)==EMPTY_TABLE ? EMPTY_TABLE : (le32_to_cpu(entry)& ~DIRTY_TABLE)) + +#define CLEAN_DIRTY (entry) \ + do { \ + if (!IS_EMPTY(entry)) \ + entry = cpu_to_le32(le32_to_cpu(entry)& ~DIRTY_TABLE); \ + } while (0) + +#define CLEAN_DIRTY2 (entry) \ + do { \ + ASSERT(!IS_EMPTY(entry)); \ + entry = cpu_to_le32(le32_to_cpu(entry)& ~DIRTY_TABLE); \ + } while (0)
Re: [Qemu-devel] [PATCH 3/5] Fast Virtual Disk (FVD) Proposal Part 3
Quoted 5 lines · Anthony Liguori
Quoted 4 lines · Chunqiang Tang
Part 3 of the block device driver for the proposed FVD image format. Multiple patches are used in order to manage the size of each patch. This patch includes some new files for FVD.
Quoted 8 lines
Quoted 2 lines
See the related discussions at
http://lists.gnu.org/archive/html/qemu-devel/2011-01/msg00426.html .Before going any further with this series, I'd like to see
1) a specification (on the QEMU wiki) describing this image format that can be reviewed
2) a concise explanation of why qcow2/qed cannot satisfy the use cases addressed by FVD
FYI, I don't accept fragmentation avoidance as a justification. This has been discussed at great length in the past on the mailing list but essentially:
a. any reasonable block format can perform online defragmentation such that for a fully allocated image, with a fixed cost that's amortized over the lifetime of the image, you have an ideal layout
b. it's only applicable to simple scenarios where you're directly on top of raw spindles which is increasingly rare. File systems reorder data on sparsely allocated files such you're no better off with a growing image vs. a sparse image if the extent size is large enough.
c. most storage has some degree of virtualization today so assumptions about contiguous allocation are more or less irrelevant
d. flash drives don't have a seek cost
Regards,
Anthony Liguori
Quoted 1,805 lines
3) performance data to backup the claims of (2)
I don't want a reference to a website or a paper. Just a concise and clear explanation on the mailing list.
I think blocksim might be worth merging but I'm still extremely sceptical of the claimed benefits of FVD.
Comparing FVD performance to qcow2 is not really all that interesting. qcow2 is terrible from a performance perspective in the face of any rigorous benchmarking. I would suggest focusing on QED in terms of comparative performance.
Regards,
Anthony Liguori
Quoted 1,792 lines
Signed-off-by Chunqiang Tang<[email protected]>--- block/fvd-create.c | 475 +++++++++++++++++++++++++++++++++++++++++++++++++++ block/fvd-debug.c | 406 ++++++++++++++++++++++++++++++++++++++++++++ block/fvd-ext.h | 71 ++++++++ block/fvd.c | 127 ++++++++++++++ block/fvd.h | 481 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 1560 insertions(+), 0 deletions(-)create mode 100644
block/fvd-create.c
create mode 100644block/fvd-debug.c
create mode 100644block/fvd-ext.h
create mode 100644block/fvd.c
create mode 100644block/fvd.hblock/fvd-create.c new file +13
-0@@ -0,0 +1,475 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang<[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this module implements bdrv_create() for FVD. +*============================================================================*/
block/fvd-create.c new file +3
-0+ +static inline int64_t calc_min_journal_size (int64_t table_entries); +static inline int search_holes(const char *filename, size_t
bitmap_size,block/fvd-create.c new file +2
-0+ int32_t bitmap_start_offset, BlockDriverState * bs, + int64_t nb_sectors, int32_t hole_size, int32_t
block_size);block/fvd-create.c new file +2
-0+ +static int fvd_create (const char *filename, QEMUOptionParameter *options)
block/fvd-create.c new file +59
-0+{ +int fd, ret; +FvdHeader *header; +int64_t virtual_disk_size =DEF_PAGE_SIZE ; +int32_t header_size; +const char *base_img =NULL ; +const char *base_img_fmt =NULL ; +const char *data_file =NULL ; +const char *data_file_fmt =NULL ; +int32_t hole_size =0 ; +int copy_on_read = FALSE; +int prefetch_start_delay =-1 ; +int64_t prefetch_profile_size =0 ; +BlockDriverState *bs =NULL ; +int bitmap_size =0 ; +int64_t base_img_size =0 ; +int64_t table_size =0 ; +int64_t journal_size =0 ; +int32_t block_size =0 ; + + header_size =sizeof (FvdHeader); + header_size =ROUND_UP (header_size,DEF_PAGE_SIZE ); + header =my_qemu_mallocz (header_size); + +/* Read out options */ +while (options&& options->name) { +if (!strcmp (options->name,BLOCK_OPT_SIZE )) { + virtual_disk_size = options->value.n; + }else if (!strcmp (options->name,"prefetch_start_delay" )) { +if (options->value.n<=0 ) { + prefetch_start_delay =-1 ; + }else { + prefetch_start_delay = options->value.n; + } + }else if (!strcmp (options->name,BLOCK_OPT_BACKING_FILE )) { + base_img = options->value.s; + }else if (!strcmp (options->name,BLOCK_OPT_BACKING_FMT )) { + base_img_fmt = options->value.s; + }else if (!strcmp (options->name,"copy_on_read" )) { + copy_on_read = options->value.n; + }else if (!strcmp (options->name,"data_file" )) { + data_file = options->value.s; + }else if (!strcmp (options->name,"data_file_fmt" )) { + data_file_fmt = options->value.s; + }else if (!strcmp (options->name,"detect_sparse_hole" )) { + hole_size = options->value.n; + }else if (!strcmp (options->name,"compact_image" )) { + header->compact_image = options->value.n; + }else if (!strcmp (options->name,"block_size" )) { + block_size = options->value.n; + }else if (!strcmp (options->name,"chunk_size" )) { + header->chunk_size = options->value.n; + }else if (!strcmp (options->name,"journal_size" )) { + journal_size = options->value.n; + }else if (!strcmp (options->name,"storage_grow_unit" )) { + header->storage_grow_unit = options->value.n; + }else if (!strcmp (options->name,"add_storage_cmd" ) +&& options->value.s) { + pstrcpy (header->add_storage_cmd, sizeof(
header->add_storage_cmd),block/fvd-create.c new file +10
-0+ options->value.s); + } + options++; + } + + virtual_disk_size =ROUND_UP (virtual_disk_size,512 ); + +/* Check if arguments are valid. */ +if (base_img&&strlen (base_img)>1023 ) { + fprintf (stderr, "The base image name is longer than 1023characters, "
block/fvd-create.c new file +7
-0+ "which is not allowed.\n"); +return -EINVAL ; + } + +if (base_img&& hole_size>0 ) { +if (header->compact_image) { + fprintf (stderr, "compact_image and detect_sparse_holecannot be "
+ "enabled together. Please disabledetect_sparse_hole. \n");block/fvd-create.c new file +36
-0+return -EINVAL ; + } + header->need_zero_init = TRUE; + }else { + header->need_zero_init = FALSE; + } + +if (data_file) { +pstrcpy (header->data_file,1024 , data_file); +if (data_file_fmt) { +pstrcpy (header->data_file_fmt,16 , data_file_fmt); + } + } + + header->magic =FVD_MAGIC ; + header->version =FVD_VERSION ; + header->virtual_disk_size = virtual_disk_size; + header->clean_shutdown = TRUE; + +if (!base_img) { + header->all_data_in_fvd_img = TRUE; + }else { +int ret; + + bs =bdrv_new ("" ); +if (!bs) { +fprintf (stderr,"Failed to create a new block driver\n" ); +return -1 ; + } + +pstrcpy (header->base_img,1024 , base_img); +if (base_img_fmt) { +pstrcpy (header->base_img_fmt,16 , base_img_fmt); +BlockDriver *drv =bdrv_find_format (base_img_fmt); +if (!drv) { + fprintf (stderr, "Failed to find driver for format'%s'\n",
block/fvd-create.c new file +10
-0+ base_img_fmt); +return -1 ; + } + ret =bdrv_open (bs, header->data_file,0 , drv); + }else { + ret =bdrv_open (bs, base_img,0 ,NULL ); + } + +if (ret<0 ) { + fprintf (stderr, "Failed to open the base image %s\n",
base_img);block/fvd-create.c new file +9
-0+return -1 ; + } + + base_img_size =bdrv_getlength (bs); + base_img_size =MIN (virtual_disk_size, base_img_size); + base_img_size =ROUND_UP (base_img_size,512 ); + +if (block_size<=0 ) { +/* No block size is provided. Find the smallest block size that
block/fvd-create.c new file +4
-0+* does not make the bitmap too big. */ + block_size =512 ; +while (1 ) { + int64_t blocks = (base_img_size + block_size - 1) /
block_size;block/fvd-create.c new file +9
-0+ bitmap_size = (blocks +7 ) /8 ; +if (bitmap_size<=MODERATE_BITMAP_SIZE ) { +break ; + } + block_size *=2 ; + } + }else { + block_size =ROUND_UP (block_size,512 ); + int64_t blocks = (base_img_size + block_size - 1) /
block_size;block/fvd-create.c new file +11
-0+ bitmap_size = (blocks +7 ) /8 ; + } + + bitmap_size =ROUND_UP (bitmap_size,DEF_PAGE_SIZE ); + header->bitmap_size = bitmap_size; + header->block_size = block_size; + header->bitmap_offset = header_size; + + prefetch_profile_size = header->prefetch_profile_entries * +sizeof (PrefetchProfileEntry); + prefetch_profile_size = ROUND_UP (prefetch_profile_size,
DEF_PAGE_SIZE);block/fvd-create.c new file +8
-0+ header->base_img_size = base_img_size; + header->max_outstanding_copy_on_read_data = +MAX_OUTSTANDING_COPY_ON_READ_DATA ; + header->copy_on_read = copy_on_read; + header->prefetch_start_delay = + prefetch_start_delay; + header->num_prefetch_slots =NUM_PREFETCH_SLOTS ; + header->bytes_per_prefetch = ROUND_UP (BYTES_PER_PREFETCH,
block_size);block/fvd-create.c new file +7
-0+ header->prefetch_throttle_time =PREFETCH_THROTTLING_TIME ; + header->prefetch_read_throughput_measure_time = +PREFETCH_MIN_MEASURE_READ_TIME ; + header->prefetch_write_throughput_measure_time = +PREFETCH_MIN_MEASURE_WRITE_TIME ; + header->prefetch_perf_calc_alpha =PREFETCH_PERF_CALC_ALPHA ; + header->prefetch_min_read_throughput =
PREFETCH_MIN_READ_THROUGHPUT;
+header->prefetch_min_write_throughput=PREFETCH_MIN_WRITE_THROUGHPUT;
+header->prefetch_max_read_throughput=PREFETCH_MAX_READ_THROUGHPUT;
+header->prefetch_max_write_throughput=PREFETCH_MAX_WRITE_THROUGHPUT;block/fvd-create.c new file +3
-0+ header->all_data_in_fvd_img = FALSE; + header->unit_of_PrefetchProfileEntry_len =DEF_PAGE_SIZE ; + header->generate_prefetch_profile = FALSE;/* To be implemented. */
+header->profile_directed_prefetch_start_delay= -1;/*To be
implemented*/block/fvd-create.c new file +8
-0+ } + +/* Set the table size. */ +if (header->compact_image) { +if (header->chunk_size<=0 ) { + header->chunk_size =CHUNK_SIZE ; + } + header->chunk_size = ROUND_UP (header->chunk_size,
DEF_PAGE_SIZE);block/fvd-create.c new file +8
-0+if (header->storage_grow_unit<=0 ) { + header->storage_grow_unit =STORAGE_GROW_UNIT ; + } +if (header->storage_grow_unit< header->chunk_size) { + header->storage_grow_unit = header->chunk_size; + } +int64_t table_entries = + (virtual_disk_size + header->chunk_size - 1) /
header->chunk_size;block/fvd-create.c new file +10
-0+ table_size =sizeof (uint32_t ) * table_entries; + table_size =ROUND_UP (table_size,DEF_PAGE_SIZE ); + header->table_offset = header_size + bitmap_size; + } + +/* Set the journal size. */ +if (bitmap_size<=0 && table_size<=0 ) { + header->journal_size =0 ;/* No need to use journal. */ + }else if (journal_size<0 ) { +/* Disable the use of journal, which reduces overhead but may cause
+ * data corruption if the host crashes. This is a valid
configuration
+ * for some use cases, where data integrity is not
critical. */block/fvd-create.c new file +8
-0+ header->journal_size =0 ; + }else { +if (journal_size ==0 ) { +/* No journal size is specified. Use a default size. */ + journal_size =JOURNAL_SIZE ; + } +if (table_size>0 ) { +/* Make sure that the journal is at least large enough to record
+ * all table changes in one shot, which is the extremely
unlikelyblock/fvd-create.c new file +4
-0+* worst case. */ +int64_t vsize = virtual_disk_size + header->chunk_size -1 ; +int64_t table_entries = vsize / header->chunk_size; +int64_t min_journal_size = calc_min_journal_size(
table_entries);block/fvd-create.c new file +7
-0+if (journal_size< min_journal_size) { + journal_size = min_journal_size; + } + } + journal_size =ROUND_UP (journal_size,DEF_PAGE_SIZE ); + header->journal_size = journal_size; + header->journal_offset = header_size + bitmap_size +
table_size;block/fvd-create.c new file +3
-0+ } + + const int64_t metadata_size = header_size + bitmap_size +
table_size+
+prefetch_profile_size+ MAX (0,journal_size);block/fvd-create.c new file +3
-0+ header->metadata_size = metadata_size; + + fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,0644);
block/fvd-create.c new file +8
-0+if (fd<0 ) { +fprintf (stderr,"Failed to open %s\n" , filename); + goto fail; + } +fvd_header_cpu_to_le (header); + +if (qemu_write_full (fd, header, header_size) != header_size) { + fprintf (stderr, "Failed to write the header of %s\n",filename);
block/fvd-create.c new file +10
-0+ goto fail; + } + +/* Initialize the bitmap. */ +if (bitmap_size>0 ) { +uint8_t *bitmap =my_qemu_mallocz (bitmap_size); + ret =qemu_write_full (fd, bitmap, bitmap_size); +my_qemu_free (bitmap); +if (ret != bitmap_size) { + fprintf (stderr, "Failed to zero out the bitmap of%s\n", filename);
block/fvd-create.c new file +13
-0+ goto fail; + } + } + +/* Initialize the table. */ +if (table_size>0 ) { +/* Set all entries to EMPTY_TABLE (0xFFFFFFFF). */ +uint8_t *empty_table =my_qemu_malloc (table_size); +memset (empty_table,0xFF , table_size); + ret =qemu_write_full (fd, empty_table, table_size); +my_qemu_free (empty_table); +if (ret != table_size) { + fprintf (stderr, "Failed to write the table of %s\n.",filename);
block/fvd-create.c new file +11
-0+ goto fail; + } + } + +/* Initialize the journal. */ +if (journal_size>0 ) { +uint8_t *empty_journal =my_qemu_mallocz (journal_size); + ret =qemu_write_full (fd, empty_journal, journal_size); +my_qemu_free (empty_journal); +if (ret != journal_size) { + fprintf (stderr, "Failed to initialize the journal for%s\n.",
block/fvd-create.c new file +10
-0+ filename); + goto fail; + } + } + +close (fd); + ret =0 ; + +if (bs&& hole_size>0 ) { + ret = search_holes (filename, (size_t) bitmap_size,
header_size, bs,
+base_img_size/ 512,hole_size,block_size);block/fvd-create.c new file +18
-0+ } + +if (bs) { +bdrv_close (bs); + } +my_qemu_free (header); +return ret; + + fail: +if (bs) { +bdrv_close (bs); + } +close (fd); +my_qemu_free (header); +return -1 ; +} + +/* For the optimization called "free write to zero-filled blocks". See Section
+ * 3.3.3 of the FVD-cow paper. */
+static inline intsearch_holes(const char *filename,size_tbitmap_size,block/fvd-create.c new file +2
-0+int32_t bitmap_start_offset, + BlockDriverState * bs, int64_t
nb_sectors,block/fvd-create.c new file +5
-0+ int32_t hole_size, int32_t block_size) +{ +const int fd =open (filename,O_RDWR |O_BINARY |O_LARGEFILE ,0 ); +if (fd<0 ) { + fprintf (stderr, "Failed to open %s for read and write.\n",filename);
block/fvd-create.c new file +4
-0+return -1 ; + } + + printf ("Searching zero-filled sectors in the base image. Pleasewait...");
block/fvd-create.c new file +4
-0+fflush (stdout); + +uint8_t *bitmap = + (uint8_t *) mmap (NULL, bitmap_size, PROT_READ | PROT_WRITE,
MAP_SHARED,block/fvd-create.c new file +44
-0+ fd, (off_t) bitmap_start_offset); +if (bitmap ==MAP_FAILED ) { +fprintf (stderr,"Failed to mmap() %s\n" , filename); +close (fd); +return -1 ; + } + +if (hole_size< block_size) { + hole_size = block_size; + } + hole_size =ROUND_UP (hole_size, block_size); + nb_sectors =ROUND_DOWN (nb_sectors, hole_size); +const int sectors_per_hole = hole_size /512 ; +const int sectors_per_block = block_size /512 ; +int num_int64_in_hole = hole_size /8 ; +int64_t hole_count =0 ; +int i, ret =0 ; +int64_t sec =0 ; +uint8_t *p =my_qemu_blockalign (bs, hole_size); + +while (sec< nb_sectors) { +int64_t *q; + +if (bdrv_read (bs, sec, p, sectors_per_hole)<0 ) { +fprintf (stderr,"Error in reading the base image\n" ); + ret =-1 ; + goto done; + } + +/* All zeros? */ + q = (int64_t *) p; +for (i =0 ; i< num_int64_in_hole; i++) { +if (*q !=0 ) { +break ; + } + q++; + } + +if (i< num_int64_in_hole) { +/* This is not a hole. */ + sec += sectors_per_hole; + }else { +/* These sectors consist of only zeros. Set the flag to +* indicate that there is no need to read this sector from the
+ * base image. See Section 3.3.3 of the FVD-cow paper
for theblock/fvd-create.c new file +21
-0+* rationale. */ + hole_count++; +int64_t end = sec + sectors_per_hole; +while (sec< end) { +int block_num = sec / sectors_per_block; +int64_t bitmap_byte_offset = block_num /8 ; +uint8_t bitmap_bit_offset = block_num %8 ; +int8_t mask = (uint8_t ) (0x01 << bitmap_bit_offset); +uint8_t b = bitmap[bitmap_byte_offset]; +if (!(b& mask)) { + b |= mask; + bitmap[bitmap_byte_offset] |= mask; + } + sec += sectors_per_block; + } + } + } + + done: + printf ("\nFound %" PRId64 + " zero-filled hole regions. Image creation done.\n",
hole_count);block/fvd-create.c new file +55
-0+my_qemu_vfree (p); +munmap (bitmap, bitmap_size); +close (fd); +return ret; +} + +static QEMUOptionParameter fvd_create_options[] = { + { + .name =BLOCK_OPT_SIZE , + .type =OPT_SIZE , + .help ="Virtual disk size" }, + { + .name ="compact_image" , + .type =OPT_FLAG , + .help ="compact_image=on|off" }, + { + .name ="block_size" , + .type =OPT_SIZE , + .help ="Block size" }, + { + .name ="chunk_size" , + .type =OPT_SIZE , + .help ="Chunk size" }, + { + .name ="storage_grow_unit" , + .type =OPT_SIZE , + .help ="Storage grow unit" }, + { + .name ="add_storage_cmd" , + .type =OPT_STRING , + .help ="Command to add storage when FSI runs out of space" }, + { + .name =BLOCK_OPT_BACKING_FILE , + .type =OPT_STRING , + .help ="File name of a backing image" }, + { + .name =BLOCK_OPT_BACKING_FMT , + .type =OPT_STRING , + .help ="Image format of the backing image" }, + { + .name ="data_file" , + .type =OPT_STRING , + .help ="File name of a separate data file" }, + { + .name ="data_file_fmt" , + .type =OPT_STRING , + .help ="Image format of the separate data file" }, + { + .name ="copy_on_read" , + .type =OPT_FLAG , + .help ="copy_on_read=on|off" }, + { + .name ="prefetch_start_delay" , + .type =OPT_NUMBER , + .help = "Delay in seconds before starting whole imageprefetching. "
+ "Prefetching is disabled if the delay is not a positive
number."},block/fvd-create.c new file +4
-0+ { + .name = "detect_sparse_hole", + .type = OPT_SIZE, + .help = "Minimum size (in bytes) of a continuous zero-filledregion to be "
+ "considered as a sparse file hole in the backing image
(setting it "block/fvd-create.c new file +7
-0+ "to 0 turns off sparse file detection)"}, + { + .name = "journal_size", + .type = OPT_SIZE, + .help = "Journal size"}, + {NULL} +};block/fvd-debug.c new file +14
-0@@ -0,0 +1,406 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang<[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this module implements debugging functions for +* the Fast Virtual Disk (FVD) format. +*============================================================================*/
block/fvd-debug.c new file +17
-0+ +#ifndef ENABLE_TRACE_IO +# define TRACE_REQUEST (...) do {} while (0) +# define TRACE_STORE_IN_FVD (...) do {} while (0) + +#else +/* Monitor IO on a specific sector that triggers bugs. */ +static inline void debug_sector (int64_t sector_num) +{ +if (FALSE) { +if (sector_num == ((int64_t )1023990LL )) { +QPAUSE ("right sector" ); + } + } +} + +static void TRACE_REQUEST (int do_write, int64_t sector_num, int
nb_sectors)block/fvd-debug.c new file +6
-0+{ + if (do_write) { + QDEBUG ("TRACE_REQUEST: write sector_num=%" PRId64 + " nb_sectors=%d\n [ ", sector_num, nb_sectors); + } else { + QDEBUG ("TRACE_REQUEST: read sector_num=%" PRId64 "nb_sectors=%d\n"
block/fvd-debug.c new file +16
-0+ "[ ", sector_num, nb_sectors); + } + +int64_t end = sector_num + nb_sectors; +int64_t sec; +for (sec = sector_num; sec< end; sec++) { +QDEBUG ("sec%" PRId64" " , sec); +debug_sector (sec); + } +QDEBUG (" ]\n" ); +} + +static void TRACE_STORE_IN_FVD (const char *str, int64_t sector_num, + int nb_sectors) +{ + QDEBUG ("TRACE_STORE: %s sector_num=%" PRId64 "nb_sectors=%d\n [ ",
block/fvd-debug.c new file +32
-0+ str, sector_num, nb_sectors); +int64_t end = sector_num + nb_sectors; +int64_t sec; +for (sec = sector_num; sec< end; sec++) { +QDEBUG ("sec%" PRId64" " , sec); +debug_sector (sec); + } + QDEBUG (" ]\n"); +} +#endif + +#ifndef FVD_DEBUG +# define my_qemu_malloc qemu_malloc +# define my_qemu_mallocz qemu_mallocz +# define my_qemu_blockalign qemu_blockalign +# define my_qemu_free qemu_free +# define my_qemu_vfree qemu_vfree +# define my_qemu_aio_get qemu_aio_get +# define my_qemu_aio_release qemu_aio_release +# define COPY_UUID (to,from) do {} while (0) + +#else +FILE *__fvd_debug_fp; +static unsigned long long int fvd_uuid =1 ; +static int64_t pending_qemu_malloc =0 ; +static int64_t pending_qemu_aio_get =0 ; +static int64_t pending_local_writes =0 ; +static const char *alloc_file; +static int alloc_line; + +#define my_qemu_malloc (size) \ + ((void*)(alloc_file=__FILE__, alloc_line=__LINE__,
_my_qemu_malloc(size)))block/fvd-debug.c new file +3
-0+ +#define my_qemu_mallocz (size) \ + ((void*)(alloc_file=__FILE__, alloc_line=__LINE__,
_my_qemu_mallocz(size)))block/fvd-debug.c new file +104
-0+ +#define my_qemu_blockalign (bs,size) \ + ((void*)(alloc_file=__FILE__, \ + alloc_line=__LINE__, \ + _my_qemu_blockalign(bs,size))) + +#define my_qemu_aio_get (pool,bs,cb,op) \ + ((void*)(alloc_file=__FILE__, \ + alloc_line=__LINE__, \ + _my_qemu_aio_get(pool,bs,cb,op))) + +#define my_qemu_free (p) \ + (alloc_file=__FILE__, alloc_line=__LINE__, _my_qemu_free(p)) + +#define my_qemu_vfree (p) \ + (alloc_file=__FILE__, alloc_line=__LINE__, _my_qemu_vfree(p)) + +static void COPY_UUID (FvdAIOCB * to,FvdAIOCB * from) +{ +if (from) { + to->uuid = from->uuid; +FVD_DEBUG_ACB (to); + } +} + +#ifdef DEBUG_MEMORY_LEAK +# define MAX_TRACER 10485760 +static int alloc_tracer_used =1 ;/* slot 0 is not used. */ +static void **alloc_tracers =NULL ; + +static void __attribute__ ((constructor))init_mem_alloc_tracers (void ) +{ +if (!alloc_tracers) { + alloc_tracers =qemu_mallocz (sizeof (void *) *MAX_TRACER ); + } +} + +static void trace_alloc (void *p,size_t size) +{ +alloc_tracer_t *t = p; + t->magic =FVD_ALLOC_MAGIC ; + t->alloc_file = alloc_file; + t->alloc_line = alloc_line; + t->size = size; + +if (alloc_tracer_used<MAX_TRACER ) { + t->alloc_tracer = alloc_tracer_used++; + alloc_tracers[t->alloc_tracer] = t; +QDEBUG ("Allocate memory using tracer%d in %s on line %d.\n" , + t->alloc_tracer, alloc_file, alloc_line); + }else { + t->alloc_tracer =0 ; + } + +/* Set header and footer to detect out-of-range writes. */ +if (size != (size_t ) -1 ) { +uint8_t *q = (uint8_t *) p; +uint64_t *header = (uint64_t *) (q +512 -sizeof (uint64_t )); +uint64_t *footer = (uint64_t *) (q + size -512 ); + *header =FVD_ALLOC_MAGIC ; + *footer =FVD_ALLOC_MAGIC ; + } +} + +static void trace_free (void *p) +{ +alloc_tracer_t *t = p; + +QDEBUG ("Free memory with tracer%d in %s on line %d.\n" , + t->alloc_tracer, alloc_file, alloc_line); +ASSERT (t->magic ==FVD_ALLOC_MAGIC && t->alloc_tracer>=0 ); + +/* Check header and footer to detect out-of-range writes. */ +if (t->size != (size_t ) -1 ) { +uint8_t *q = (uint8_t *) p; +uint64_t *header = (uint64_t *) (q +512 -sizeof (uint64_t )); +uint64_t *footer = (uint64_t *) (q + t->size -512 ); +ASSERT (*header ==FVD_ALLOC_MAGIC ); +ASSERT (*footer ==FVD_ALLOC_MAGIC ); + } + +if (t->alloc_tracer) { +ASSERT (alloc_tracers[t->alloc_tracer] == t); + alloc_tracers[t->alloc_tracer] =NULL ; + t->alloc_tracer = -INT_MAX ; + }else { + t->alloc_tracer *=-1 ;/* Guard against double free. */ + } +} + +static void dump_alloc_tracers (void ) +{ +int unfreed =0 ; +int i; +for (i =1 ; i< alloc_tracer_used; i++) { +if (!alloc_tracers[i]) { +continue ; + } + + unfreed++; +alloc_tracer_t *t = alloc_tracers[i]; + +if (t->size == (size_t ) -1 ) { + FvdAIOCB *acb = container_of (alloc_tracers[i],FvdAIOCB, tracer);
block/fvd-debug.c new file +2
-0+ASSERT (acb->magic ==FVDAIOCB_MAGIC ); + QDEBUG ("Memory %p with tracer%d allocated in %s on line%d "
block/fvd-debug.c new file +6
-0+"(FvdAIOCB acb%llu-%p) is not freed. magic %s\n" , + alloc_tracers[i], i, t->alloc_file, t->alloc_line, + acb->uuid, acb, + t->magic == FVD_ALLOC_MAGIC ? "correct" : "wrong"); + }else { + QDEBUG ("Memory %p with tracer%d allocated in %s on line%d is "
block/fvd-debug.c new file +6
-0+"not freed. magic %s\n" , + alloc_tracers[i], i, t->alloc_file, t->alloc_line, + t->magic == FVD_ALLOC_MAGIC ? "correct" : "wrong"); + +uint8_t *q = (uint8_t *) t; + uint64_t *header = (uint64_t *) (q + 512 - sizeof(
uint64_t));block/fvd-debug.c new file +11
-0+uint64_t *footer = (uint64_t *) (q + t->size -512 ); +ASSERT (*header ==FVD_ALLOC_MAGIC ); + ASSERT (*footer == FVD_ALLOC_MAGIC); + } + } + +QDEBUG ("Unfreed memory allocations: %d\n" , unfreed); +} +#endif + +static inline void *_my_qemu_aio_get (AIOPool * pool,BlockDriverState * bs,
block/fvd-debug.c new file +5
-0+ BlockDriverCompletionFunc * cb, + void *opaque) +{ + pending_qemu_aio_get++; + FvdAIOCB *acb = (FvdAIOCB *) qemu_aio_get (&fvd_aio_pool, bs,cb, opaque);
block/fvd-debug.c new file +56
-0+ acb->uuid = ++fvd_uuid; + acb->magic =FVDAIOCB_MAGIC ; + +FVD_DEBUG_ACB (acb); + +#ifdef DEBUG_MEMORY_LEAK +trace_alloc (&acb->tracer,-1 ); +#endif + +return acb; +} + +static inline void my_qemu_aio_release (void *p) +{ + pending_qemu_aio_get--; +ASSERT (pending_qemu_aio_get>=0 ); + +#ifdef DEBUG_MEMORY_LEAK +FvdAIOCB *acb = p; +trace_free (&acb->tracer); +#endif + +qemu_aio_release (p); +} + +static inline void *_my_qemu_malloc (size_t size) +{ +ASSERT (size>0 ); + pending_qemu_malloc++; +#ifndef DEBUG_MEMORY_LEAK +return qemu_malloc (size); +#else + + size +=1024 ;/* 512 bytes header and 512 bytes footer. */ +uint8_t *ret =qemu_malloc (size); +trace_alloc (ret, size); +return ret +512 ; +#endif +} + +static inline void *_my_qemu_mallocz (size_t size) +{ +ASSERT (size>0 ); + pending_qemu_malloc++; +#ifndef DEBUG_MEMORY_LEAK +return qemu_mallocz (size); +#else + + size +=1024 ;/* 512 bytes header and 512 bytes footer. */ +uint8_t *ret =qemu_mallocz (size); +trace_alloc (ret, size); +return ret +512 ; +#endif +} + +static inline void *_my_qemu_blockalign (BlockDriverState * bs,
size_tsize)block/fvd-debug.c new file +59
-0+{ +ASSERT (size>0 ); + pending_qemu_malloc++; + +#ifndef DEBUG_MEMORY_LEAK +return qemu_blockalign (bs, size); +#else + + size +=1024 ;/* 512 bytes header and 512 bytes footer. */ +uint8_t *ret =qemu_blockalign (bs, size); +trace_alloc (ret, size); +return ret +512 ; +#endif +} + +static inline void _my_qemu_free (void *ptr) +{ + pending_qemu_malloc--; +ASSERT (pending_qemu_malloc>=0 ); +#ifndef DEBUG_MEMORY_LEAK +qemu_free (ptr); +#else + +uint8_t *q = ((uint8_t *) ptr) -512 ; +trace_free (q); +qemu_free (q); +#endif +} + +static inline void _my_qemu_vfree (void *ptr) +{ + pending_qemu_malloc--; +ASSERT (pending_qemu_malloc>=0 ); +#ifndef DEBUG_MEMORY_LEAK +qemu_vfree (ptr); +#else + +uint8_t *q = ((uint8_t *) ptr) -512 ; +trace_free (q); +qemu_vfree (q); +#endif +} + +static void count_pending_requests (BDRVFvdState * s) +{ +int m =0 , k =0 ; +FvdAIOCB *w; + + QLIST_FOREACH (w,&s->copy_locks, copy_lock.next) { + m++; +QDEBUG ("copy_lock: acb%llu-%p\n" , w->uuid, w); + } + + QLIST_FOREACH (w,&s->write_locks, write.next_write_lock) { + k++; +QDEBUG ("write_lock: acb%llu-%p\n" , w->uuid, w); + } + + QDEBUG ("Debug_memory_leak: copy_locks=%d write_locks=%d\n", m,k);
block/fvd-debug.c new file +10
-0+} + +static void dump_resource_summary (BDRVFvdState * s) +{ +#ifdef DEBUG_MEMORY_LEAK + dump_alloc_tracers (); +#endif + + QDEBUG ("Resource summary: outstanding_copy_on_read_data=%" PRId64 + " total_copy_on_read_data=%" PRId64 "total_prefetch_data=%" PRId64
+ " " " pending_qemu_malloc=%" PRId64 "
pending_qemu_aio_get=%" PRId64block/fvd-debug.c new file +2
-0+" pending_local_writes=%" PRId64"\n" , + s->outstanding_copy_on_read_data,
s->total_copy_on_read_data,
+s->total_prefetch_data,pending_qemu_malloc,pending_qemu_aio_get,block/fvd-debug.c new file +12
-0+ pending_local_writes); +count_pending_requests (s); +} + +/* Monitor processing a specific FvdAIOCB that triggers bugs. */ +void FVD_DEBUG_ACB (void *p) +{ +if (FALSE) { +FvdAIOCB *acb = p; + +/* Is it FvdAIOCB? */ + if (acb->magic != FVDAIOCB_MAGIC || acb->common.bs->drv!=&bdrv_fvd) {
block/fvd-debug.c new file +43
-0+/* Is it CompactChildCB? */ +CompactChildCB *child = p; + acb = child->acb; +if (acb->magic !=FVDAIOCB_MAGIC + || acb->common.bs->drv !=&bdrv_fvd + || (acb->type !=OP_LOAD_COMPACT +&& acb->type !=OP_STORE_COMPACT )) { +return ; + } + } + +if (acb->uuid ==20ULL ) { +QPAUSE ("Processing the right acb" ); + } + } +} + +void init_fvd_debug_fp (void ) +{ +char buf[256 ]; +sprintf (buf,"/tmp/fvd.log-%d" ,getpid ()); +if ((__fvd_debug_fp =fopen (buf,"wt" )) ==NULL ) { + __fvd_debug_fp = stdout; + } +} +#endif + +void fvd_check_memory_usage (void ) +{ +ASSERT (pending_qemu_malloc ==0 ); +} + +int fvd_get_copy_on_read (BlockDriverState * bs) +{ +BDRVFvdState *s = bs->opaque; +return s->copy_on_read; +} + +void fvd_set_copy_on_read (BlockDriverState * bs,int copy_on_read) +{ +BDRVFvdState *s = bs->opaque; + s->copy_on_read = copy_on_read; +}block/fvd-ext.h new file +12
-0@@ -0,0 +1,71 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang<[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this header file contains functions of the FVD block
+ * device driver that are used by other external modules. These functions are
+ * mainly for testing and debugging urposes.
+ *============================================================================*/block/fvd-ext.h new file +41
-0+ +#ifndef __fvd_debug_h__ +#define __fvd_debug_h__ + +//#define FVD_DEBUG + +int fvd_get_copy_on_read (BlockDriverState *bs); +void fvd_set_copy_on_read (BlockDriverState *bs,int copy_on_read); +void fvd_check_memory_usage (void ); +void fvd_init_prefetch (void * bs); +void fvd_enable_host_crash_test (void ); + +#ifndef TRUE +# define TRUE 1 +#endif +#ifndef FALSE +# define FALSE 0 +#endif + +#ifndef FVD_DEBUG +# define QDEBUG (format,...) do {} while (0) +# define ASSERT (x) do {} while (0) +# define FVD_DEBUG_ACB (...) do {} while (0) +# define QPAUSE (...) do {} while (0) + +#else + +extern FILE *__fvd_debug_fp; +void init_fvd_debug_fp (void ); +void FVD_DEBUG_ACB (void *p); +# define QDEBUG (format,...) \ + do { \ + if (__fvd_debug_fp==NULL) init_fvd_debug_fp(); \ + fprintf (__fvd_debug_fp, format, ##__VA_ARGS__); \ + fflush(__fvd_debug_fp); \ + } while(0) + +# define ASSERT (x) \ + do { \ + if (!(x)) { \ + fprintf (stderr, "Assertion failed in process %d at%s:%d. " \
+ "Waiting for debugging...\n", getpid(),__FILE__,__LINE__); \block/fvd-ext.h new file +14
-0+fgetc (stdin);exit (1 ); \ + } \ + } while (0) \ + +# define QPAUSE (format,...) \ + do { \ + printf (format, ##__VA_ARGS__); \ + printf (" Pause process %d for debugging...\n", getpid()); \ + fgetc (stdin); \ + } while (0) + +#endif + +#endif block/fvd.c new file +12
-0@@ -0,0 +1,127 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang<[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this module implements the QEMU block device driver
+ * for the Fast Virtual Disk (FVD) format. See the following companion
+ * papers for a detailed description of FVD:
+ * 1. The so-called "FVD-cow paper":
+ * "FVD: a High-Performance Virtual Machine Image Format
for Cloud",
+ * by Chunqiang Tang, 2010.
+ * 2. The so-called "FVD-compact paper":
+ * "FVD: a High-Performance Virtual Machine Image Format
for Cloud
+ * with Sparse Image Capability", by Chunqiang Tang, 2010.
+ *============================================================================*/block/fvd.c new file +14
-0+ +#include "block/fvd.h" + +//#define ENABLE_TRACE_IO +//#define DEBUG_MEMORY_LEAK +//#define SIMULATED_TEST_WITH_QEMU_IO + +#ifndef FVD_DEBUG +#undef DEBUG_MEMORY_LEAK +#undef ENABLE_TRACE_IO +#undef SIMULATED_TEST_WITH_QEMU_IO +#endif + +/* Use include to avoid exposing too many FVD symbols, and to allow inline
+ * function optimization. */block/fvd.c new file +50
-0+#include "block/fvd-utils.c" +#include "block/fvd-debug.c" +#include "block/fvd-misc.c" +#include "block/fvd-create.c" +#include "block/fvd-open.c" +#include "block/fvd-read.c" +#include "block/fvd-write.c" +#include "block/fvd-load.c" +#include "block/fvd-store.c" +#include "block/fvd-journal.c" +#include "block/fvd-prefetch.c" + +static AIOPool fvd_aio_pool = { + .aiocb_size =sizeof (FvdAIOCB), + .cancel = fvd_aio_cancel, +}; + +static BlockDriver bdrv_fvd = { + .format_name ="fvd" , + .instance_size =sizeof (BDRVFvdState), + .bdrv_create = fvd_create, + .bdrv_probe = fvd_probe, + .bdrv_file_open = fvd_open, + .bdrv_close = fvd_close, + .bdrv_is_allocated = fvd_is_allocated, + .bdrv_flush = fvd_flush, + .bdrv_aio_readv = fvd_aio_readv, + .bdrv_aio_writev = fvd_aio_writev, + .bdrv_aio_flush = fvd_aio_flush, + .create_options = fvd_create_options, + .bdrv_get_info = fvd_get_info, + .bdrv_update = fvd_update, + .bdrv_has_zero_init = fvd_has_zero_init +}; + +static void bdrv_fvd_init (void ) +{ +bdrv_register (&bdrv_fvd); +} + +block_init (bdrv_fvd_init); + +/* +* Since bdrv_close may not be properly invoked on a VM shutdown, we +* use a destructor to flush metadata to disk. This only affects +* performance and does not affect correctness. +* See Section 3.3.4 of the FVD-cow paper for the rationale. +*/ +extern QTAILQ_HEAD (, BlockDriverState) bdrv_states; +static void __attribute__ ((destructor)) flush_fvd_bitmap_to_disk(void)
block/fvd.c new file +18
-0+{ +BlockDriverState *bs; + QTAILQ_FOREACH (bs,&bdrv_states, list) { +if (bs->drv ==&bdrv_fvd) { +flush_metadata_to_disk_on_exit (bs); + +#ifdef FVD_DEBUG +dump_resource_summary (bs->opaque); +#endif + } + } +} + +/* +* TODOs: Below are some potential enhancements for future development: +* 1. Handle storage leak on failure. +* +* 2. Profile-directed prefetch. See Section 3.4.1 of the FVD-cow paper.
+ * Related metadata are FvdHeader.prefetch_profile_offset and
+ * FvdHeader.prefetch_profile_entries,
+ * FvdHeader.profile_directed_prefetch_start_delay,
+ * FvdHeader.generate_prefetch_profile.
+ *
+ * 3. Cap the prefetch throughput at the upper limit. See Section 3.4.2 of
+ * the FVD-cow paper. Related metadata are
+ * FvdHeader.prefetch_max_read_throughput and
+ * FvdHeader.prefetch_max_write_throughput.
+ *
+ * 4. Support write through to the base image. When a VM issues a write
+ * request, in addition to saving the data in the FVD data file, also save the
+ * data in the base image if the address of write request is not beyond the
+ * size of the base image (this of course requires the base image NOT to be
+ * 'read_only'. This feature changes the semantics of copy-on-write, but it
+ * suits a different use case, where the base image is stored on a remote
+ * storage server, and the FVD image is stored on a local disk and acts as a
+ * write-through cache of the base image. This can be used to cache and
+ * improve the performance of persistent storage on network-attached storage,
+ * e.g., Amazon EBS. This feature is not described in the FVD-cow paper as it
+ * would complicate the discussion. Related metadata are
+ * FvdHeader.write_updates_base_img.
+ */block/fvd.h new file +12
-0@@ -0,0 +1,481 @@ +/* +* Copyright (c) 2010-2011 IBM +* +* Authors: +* Chunqiang Tang<[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2. +* See the COPYING file in the top-level directory. +*/ + +/*============================================================================= +* A short description: this is the header of the FVD block device driver.
+ *============================================================================*/block/fvd.h new file +21
-0+ +#include <sys/vfs.h> +#include <sys/mman.h> +#include <pthread.h> +#include <execinfo.h> +#include <stdlib.h> +#include <sys/ioctl.h> +#include <stdint.h> +#include <stdio.h> +#include <inttypes.h> +#include "block_int.h" +#include "osdep.h" +#include "qemu-option.h" +#include "qemu-timer.h" +#include "block.h" +#include "qemu-queue.h" +#include "qemu-common.h" +#include "block/blksim.h" +#include "block/fvd-ext.h" + +#define FVD_MAGIC (('Q'<< 24) | ('C'<< 16) | (0xF5<< 8) |
0xA9)block/fvd.h new file +8
-0+#define FVD_VERSION 1 + +/* Profile-directed prefetch. (to be implemented). */ +typedef struct __attribute__ ((__packed__)) PrefetchProfileEntry { + int64_t offset; /* in bytes */ + + /* In the unit of FvdHeader.prefetch_profile_entry_len_unit, i.e., + * len_in_bytes = len *FvdHeader.unit_of_PrefetchProfileEntry_len. */
block/fvd.h new file +7
-0+uint32_t len; +} PrefetchProfileEntry; + +/* +* The FVD format consists of: +* + Header fields of FvdHeader. +* + Bitmap, starting on a 4KB page boundary at a location specified by
+ * FvdHeader.bitmap_offset.
+ * + Table, starting on a 4KB page boundary at a location
specified by
+ * FvdHeader.table_offset.
+ * + Journal, starting on a 4KB page boundary at a location
specified by
+ * FvdHeader.journal_offset.
+ * + Prefetch profile entries, starting on a 4KB page boundary at
a location
+ * specified by FvdHeader.prefetch_profile_offset. (to be
implemented)
+ * + Virtual disk data, starting on a 4KB page boundary.
Optionally, disk
+ * data can be stored in a separate data file specified by
+ * FvdHeader.data_file.
+ */block/fvd.h new file +5
-0+typedef struct __attribute__ ((__packed__)) FvdHeader { + uint32_t magic; + uint32_t version; + + /* This field is set to TRUE after whole-image prefetchingfinishes. */
block/fvd.h new file +3
-0+int32_t all_data_in_fvd_img; + +int64_t virtual_disk_size;/* in bytes. Disk size perceived by the VM. */
block/fvd.h new file +5
-0+int64_t metadata_size;/* in bytes. */ +char base_img[1024 ]; +char base_img_fmt[16 ]; +int64_t base_img_size;/* in bytes. */ +int64_t bitmap_offset;/* in bytes. Aligned on
DEF_PAGE_SIZE. */
+int64_tbitmap_size; /* in bytes. Rounded up toDEF_PAGE_SIZE*/block/fvd.h new file +5
-0+int32_t block_size;/* in bytes. */ +int32_t copy_on_read;/* TRUE or FALSE */ +int64_t max_outstanding_copy_on_read_data;/* in bytes. */ + +/* If (data_file[0]==0), the FVD metadata and data are stored in one file.*/
block/fvd.h new file +14
-0+char data_file[1024 ]; +char data_file_fmt[16 ]; + +/******** Begin: for prefetching. *******************************/ +/* in seconds. -1 means disable whole image prefetching. */ +int32_t prefetch_start_delay; + +/* in bytes. Aligned on DEF_PAGE_SIZE. (to be implemented) */ +int64_t prefetch_profile_offset; + +/* Number of PrefetchProfileEntry. (to be implemented) */ +int64_t prefetch_profile_entries; + +int32_t num_prefetch_slots;/* Max number of oustanding prefetch writes. */
+int32_tbytes_per_prefetch; /* For whole image
prefetching. */
+int32_tprefetch_read_throughput_measure_time; /* in
milliseconds. */
+int32_tprefetch_write_throughput_measure_time; /* in
milliseconds. */block/fvd.h new file +2
-0+ +/* Controls the calculation of the moving average of throughput. Must be a
block/fvd.h new file +2
-0+* value between [0,100]. +* actual_normalized_alpha = * prefetch_perf_calc_alpha / 100.0 */
block/fvd.h new file +8
-0+int32_t prefetch_perf_calc_alpha; + +int32_t prefetch_min_read_throughput;/* in KB/second. */ +int32_t prefetch_min_write_throughput;/* in KB/second. */ +int32_t prefetch_max_read_throughput;/* in KB/second. */ +int32_t prefetch_max_write_throughput;/* in KB/second. */ + +/* in milliseconds. When prefetch read/write throughput is low, prefetch
block/fvd.h new file +17
-0+* pauses for a random time uniformly distributed in +* [0, prefetch_throttle_time]. */ +int32_t prefetch_throttle_time; +/******** End: for prefetching. *******************************/ + +/******** Begin: for compact image. *****************************/ +int32_t compact_image;/* TRUE or FALSE */ +int64_t table_offset;/* in bytes. */ +int64_t chunk_size;/* in bytes. */ +int64_t storage_grow_unit;/* in bytes. */ +char add_storage_cmd[2048 ]; +/******** End: for compact image. *******************************/ + +/******** Begin: for journal. ***********************************/ +int64_t journal_offset;/* in bytes. */ +int64_t journal_size;/* in bytes. */ +int32_t clean_shutdown;/* TRUE if VM's last shutdown was graceful. */
block/fvd.h new file +5
-0+/******** End: for journal. *************************************/ + +/* +* This field is TRUE if the image mandates that the storage layer +* (BDRVFvdState.fvd_data) must return TRUE for
bdrv_has_zero_init().
+ * This is the case if the optimization described in Section
3.3.3 of the
+ * FVD-cow paper is enabled (see functionsearch_holes()). If
'qemu-img
+ * create' setsneed_zero_initto TRUE, 'qemu-img update' can be
used to
+ * manually reset it to FALSE, if the user always manually
pre-fills the
+ * storage (e.g., a raw partition) with zeros. If the image is
stored on a
+ * file system, it already supportszero_init, and hence there
is no needblock/fvd.h new file +9
-0+* to manually manipulate this field. +*/ +int32_t need_zero_init; + +/* If TRUE, FVD dumps a prefetch profile after the VM shuts down. +* (to be implemented) */ +int32_t generate_prefetch_profile; + +/* See the comment on PrefetchProfileEntry.len. (to be implemented) */
block/fvd.h new file +26
-0+int32_t unit_of_PrefetchProfileEntry_len; + +/* in seconds. -1 means disable profile-directed prefetching. +* (to be implemented) */ +int32_t profile_directed_prefetch_start_delay; + +/* Possible values are "no", "writethrough", "writeback", or +* "writenocache". (to be implemented) */ +char write_updates_base_img[16 ]; +} FvdHeader; + +typedef struct BDRVFvdState { + BlockDriverState *fvd_metadata; + BlockDriverState *fvd_data; + int64_t virtual_disk_size; /*in bytes. */ + int64_t bitmap_offset; /* in sectors */ + int64_t bitmap_size; /* in bytes. */ + int64_t data_offset; /* in sectors. Begin of real data. */ + int64_t nb_sectors_in_base_img; + int32_t block_size; /* in sectors. */ + int copy_on_read; /* TRUE or FALSE */ + int64_t max_outstanding_copy_on_read_data; /* in bytes. */ + int64_t outstanding_copy_on_read_data; /* in bytes. */ + int data_region_prepared; /* TRUE or FALSE */ + QLIST_HEAD(WriteLocks, FvdAIOCB) write_locks; /* All writes. */ + QLIST_HEAD(CopyLocks, FvdAIOCB) copy_locks; /* copy-on-read andCoW. */
block/fvd.h new file +3
-0+ +/* Keep two copies of bitmap to reduce the overhead of updating the +* on-disk bitmap, i.e., copy-on-read and prefetching do not update the
block/fvd.h new file +5
-0+* on-disk bitmap. See Section 3.3.4 of the FVD-cow paper. */ +uint8_t *fresh_bitmap; +uint8_t *stale_bitmap; + +/******** Begin: for prefetching. ***********************************/
block/fvd.h new file +2
-0+struct FvdAIOCB **prefetch_acb; +int prefetch_state;/* PREFETCH_STATE_RUNNING, FINISHED, or DISABLED. */
block/fvd.h new file +5
-0+int prefetch_error;/* TRUE or FALSE */ +int num_prefetch_slots; +int num_filled_prefetch_slots; +int next_prefetch_read_slot; +int prefetch_read_active;/* TRUE or FALSE */
block/fvd.h new file +4
-0+int pause_prefetch_requested;/* TRUE or FALSE */ +int prefetch_start_delay;/* in seconds */ +int64_t unclaimed_prefetch_region_start; +int64_t prefetch_read_time;/* in milliseconds. */
block/fvd.h new file +4
-0+int64_t prefetch_write_time;/* in milliseconds. */ +int64_t prefetch_data_read;/* in bytes. */ +int64_t prefetch_data_written;/* in bytes. */ +double prefetch_read_throughput;/* in bytes/millisecond. */
+ doubleprefetch_write_throughput; /* in
bytes/millisecond. */
+ doubleprefetch_min_read_throughput; /* in
bytes/millisecond. */
+ doubleprefetch_min_write_throughput; /* in
bytes/millisecond. */
+int64_tprefetch_read_throughput_measure_time; /* in
millisecond. */
+int64_tprefetch_write_throughput_measure_time; /* in
millisecond.*/block/fvd.h new file +4
-0+int prefetch_throttle_time;/* in millisecond. */ +int sectors_per_prefetch; +QEMUTimer *prefetch_timer; +/* prefetch_perf_calc_alpha = FvdHeader.prefetch_perf_calc_alpha/100.0 */
block/fvd.h new file +2
-0+double prefetch_perf_calc_alpha; +/******** End: for prefetching. ***********************************/
block/fvd.h new file +2
-0+ +/******** Begin: for compact image. *************************************/
+uint32_t*table; /* Mapping table stored in memory in
little endian. */block/fvd.h new file +7
-0+int64_t data_storage;/* in sectors. */ +int64_t used_storage;/* in sectors. */ +int64_t chunk_size;/* in sectors. */ +int64_t storage_grow_unit;/* in sectors. */ +int64_t table_offset;/* in sectors. */ +char *add_storage_cmd; +/******** Begin: for compact image. *************************************/
block/fvd.h new file +2
-0+ +/******** Begin: for journal. *******************************************/
block/fvd.h new file +4
-0+int64_t journal_offset;/* in sectors. */ +int64_t journal_size;/* in sectors. */ +int64_t next_journal_sector;/* in sector. */ +int ongoing_journal_updates;/* Number of ongoing journal updates. */
block/fvd.h new file +3
-0+int dirty_image;/* TRUE or FALSE. */ + +/* Requests waiting for metadata flush and journal recycle to finish. */
block/fvd.h new file +2
-0+ QLIST_HEAD(JournalFlush, FvdAIOCB) wait_for_journal; +/******** End: for journal. ********************************************/
block/fvd.h new file +50
-0+ +#ifdef FVD_DEBUG +int64_t total_copy_on_read_data;/* in bytes. */ +int64_t total_prefetch_data;/* in bytes. */ +#endif +} BDRVFvdState; + +/* Begin of data type definitions. */ +struct FvdAIOCB ; + +typedef struct JournalCB { +BlockDriverAIOCB *hd_acb; +QEMUIOVector qiov; +struct iovec iov; +QLIST_ENTRY (FvdAIOCB) next_wait_for_journal; +}JournalCB ; + +/* CopyLock is used by AIOWriteCB and AIOCopyCB. */ +typedef struct CopyLock { +QLIST_ENTRY (FvdAIOCB) next; +int64_t begin; +int64_t end; + QLIST_HEAD(DependentWritesHead, FvdAIOCB) dependent_writes; +}CopyLock ; + +typedef struct ChildAIOReadCB { +BlockDriverAIOCB *hd_acb; +struct iovec iov; +QEMUIOVector qiov; +int64_t sector_num; +int nb_sectors; +int done; +}ChildAIOReadCB ; + +typedef struct AIOReadCB { +QEMUIOVector *qiov; +int ret; +ChildAIOReadCB read_backing; +ChildAIOReadCB read_fvd; +}AIOReadCB ; + +/* For copy-on-read and prefetching. */ +typedef struct AIOCopyCB { + BlockDriverAIOCB *hd_acb; + struct iovec iov; + QEMUIOVector qiov; + uint8_t *buf; + int64_t buffered_sector_begin; + int64_t buffered_sector_end; + int64_t last_prefetch_op_start_time; /* For prefetchonly. */
block/fvd.h new file +11
-0+} AIOCopyCB; + +typedef struct AIOWriteCB { + BlockDriverAIOCB *hd_acb; + QEMUIOVector *qiov; + uint8_t *cow_buf; + QEMUIOVector *cow_qiov; + int64_t cow_start_sector; + int update_table; /* TRUE or FALSE. */ + int ret; + QLIST_ENTRY(FvdAIOCB) next_write_lock; /* SeeBDRVFvdState.write_locks */
block/fvd.h new file +21
-0+ +/* See FvdAIOCB.write.dependent_writes. */ +QLIST_ENTRY (FvdAIOCB) next_dependent_write; +} AIOWriteCB; + +/* For AIOStoreCompactCB and AIOLoadCompactCB. */ +typedef struct CompactChildCB { +struct FvdAIOCB *acb; +BlockDriverAIOCB *hd_acb; +}CompactChildCB ; + +/* For storing data to a compact image. */ +typedef struct AIOStoreCompactCB { + CompactChildCB one_child; + CompactChildCB *children; + int update_table; + int num_children; + int finished_children; + struct FvdAIOCB *parent_acb; + int ret; + int soft_write; /*TRUE if the store is caused by copy-on-read orprefetch.*/
block/fvd.h new file +48
-0+QEMUIOVector *orig_qiov; +} AIOStoreCompactCB; + +/* For loading data from a compact image. */ +typedef struct AIOLoadCompactCB { +CompactChildCB *children; +CompactChildCB one_child; +int num_children; +int finished_children; +struct FvdAIOCB *parent_acb; +int ret; +QEMUIOVector *orig_qiov; +}AIOLoadCompactCB ; + +typedef struct AIOFlushCB { +BlockDriverAIOCB *data_acb; +BlockDriverAIOCB *metadata_acb; +int num_finished; +int ret; +}AIOFlushCB ; + +typedef struct AIOWrapperCB { +QEMUBH *bh; +}AIOWrapperCB ; + +typedef enum {OP_READ =1 ,OP_WRITE ,OP_COPY ,OP_STORE_COMPACT , +OP_LOAD_COMPACT ,OP_WRAPPER ,OP_FLUSH }op_type ; + +#ifdef FVD_DEBUG +/* For debugging memory leadk. */ +typedef struct alloc_tracer_t { +int64_t magic; +int alloc_tracer; +const char *alloc_file; +int alloc_line; +size_t size; +}alloc_tracer_t ; +#endif + +typedef struct FvdAIOCB { + BlockDriverAIOCB common; + op_type type; + int64_t sector_num; + int nb_sectors; + JournalCB jcb; /* For AIOWriteCB and AIOStoreCompactCB. */ + CopyLock copy_lock; /* For AIOWriteCB and AIOCopyCB. */ + + /* Use a union so that all requests can efficiently share onebig AIOPool.*/
block/fvd.h new file +15
-0+union { +AIOWrapperCB wrapper; +AIOReadCB read; +AIOWriteCB write; +AIOCopyCB copy; +AIOLoadCompactCB load; +AIOStoreCompactCB store; +AIOFlushCB flush; + }; + +#ifdef FVD_DEBUG +int64_t magic; +alloc_tracer_t tracer; + +/* Uniquely identifies a request across all processing activities. */
block/fvd.h new file +17
-0+unsigned long long int uuid; +#endif +} FvdAIOCB; + +static AIOPool fvd_aio_pool; +static BlockDriver bdrv_fvd; +static QEMUOptionParameter fvd_create_options[]; + +/* Function prototypes. */ +static int do_aio_write (struct FvdAIOCB *acb); +static void finish_write_data (void *opaque,int ret); +static void restart_dependent_writes (struct FvdAIOCB *acb); +static void finish_prefetch_read (void *opaque,int ret); +static int read_fvd_header (BDRVFvdState * s,FvdHeader * header); +static int update_fvd_header (BDRVFvdState * s,FvdHeader * header); +static void fvd_aio_cancel (BlockDriverAIOCB * blockacb); +static BlockDriverAIOCB *store_data_in_compact_image(struct FvdAIOCB*acb,
+ intsoft_write, struct FvdAIOCB*parent_acb,
BlockDriverState * bs,block/fvd.h new file +3
-0+ int64_t sector_num, QEMUIOVector * qiov, int nb_sectors, + BlockDriverCompletionFunc * cb, void *opaque); +static BlockDriverAIOCB *load_data_from_compact_image(structFvdAIOCB *acb,
block/fvd.h new file +8
-0+ struct FvdAIOCB *parent_acb, BlockDriverState * bs, + int64_t sector_num, QEMUIOVector * qiov, int nb_sectors, + BlockDriverCompletionFunc * cb, void *opaque); +static void free_write_resource (struct FvdAIOCB *acb); +static void write_metadata_to_journal (struct FvdAIOCB *acb); +static void flush_metadata_to_disk (BlockDriverState * bs); +static void free_journal_sectors (BDRVFvdState * s); +static int fvd_create(const char *filename, QEMUOptionParameter *options);
+static intfvd_probe(const uint8_t * buf, int buf_size, const char *filename);
+static intfvd_open(BlockDriverState * bs, const char *filename, int flags);block/fvd.h new file +24
-0+static void fvd_close (BlockDriverState * bs); +static int fvd_is_allocated (BlockDriverState * bs,int64_t sector_num, +int nb_sectors,int *pnum); +static int fvd_flush (BlockDriverState * bs); +static BlockDriverAIOCB *fvd_aio_readv (BlockDriverState * bs, +int64_t sector_num,QEMUIOVector * qiov,int nb_sectors, +BlockDriverCompletionFunc * cb,void *opaque); +static BlockDriverAIOCB *fvd_aio_writev (BlockDriverState * bs, +int64_t sector_num,QEMUIOVector * qiov,int nb_sectors, +BlockDriverCompletionFunc * cb,void *opaque); +static BlockDriverAIOCB *fvd_aio_flush (BlockDriverState * bs, +BlockDriverCompletionFunc * cb,void *opaque); +static int fvd_get_info (BlockDriverState * bs,BlockDriverInfo * bdi); +static int fvd_update (BlockDriverState * bs,int argc,char **argv); +static int fvd_has_zero_init (BlockDriverState * bs); +static void fvd_read_cancel (FvdAIOCB * acb); +static void fvd_write_cancel (FvdAIOCB * acb); +static void fvd_copy_cancel (FvdAIOCB * acb); +static void fvd_load_compact_cancel (FvdAIOCB * acb); +static void fvd_store_compact_cancel (FvdAIOCB * acb); +static void fvd_wrapper_cancel (FvdAIOCB * acb); +static void flush_metadata_to_disk_on_exit (BlockDriverState *bs); +static inline BlockDriverAIOCB *load_data (FvdAIOCB * parent_acb, + BlockDriverState * bs, int64_t sector_num, QEMUIOVector*
orig_qiov,
+ intnb_sectors, BlockDriverCompletionFunc * cb, void
*opaque);block/fvd.h new file +2
-0+static inline BlockDriverAIOCB *store_data (int soft_write, + FvdAIOCB * parent_acb, BlockDriverState * bs, int64_t
sector_num,block/fvd.h new file +5
-0+ QEMUIOVector * orig_qiov, int nb_sectors, + BlockDriverCompletionFunc * cb, void *opaque); + +/* Default configurations. */ +#define DEF_PAGE_SIZE 4096/* bytes */
+#defineBYTES_PER_PREFETCH1048576 /*
bytes */
+#definePREFETCH_THROTTLING_TIME30000 /*
milliseconds */block/fvd.h new file +2
-0+#define NUM_PREFETCH_SLOTS 2 +#define PREFETCH_MIN_MEASURE_READ_TIME 100/* milliseconds */
+#definePREFETCH_MIN_MEASURE_WRITE_TIME100 /*
milliseconds */
+#definePREFETCH_MIN_READ_THROUGHPUT5120 /*
KB/s */
+#definePREFETCH_MIN_WRITE_THROUGHPUT5120 /*
KB/s */
+#definePREFETCH_MAX_READ_THROUGHPUT
1000000000L /* KB/s */
+#definePREFETCH_MAX_WRITE_THROUGHPUT
1000000000L /* KB/s */
+#definePREFETCH_PERF_CALC_ALPHA80 /* in
[0,100]. */
+#defineMAX_OUTSTANDING_COPY_ON_READ_DATA
2000000 /* bytes */
+#defineMODERATE_BITMAP_SIZE4194304L
/* bytes */
+#defineCHUNK_SIZE1048576LL
/* bytes */
+#defineJOURNAL_SIZE
16777216LL /* bytes */
+#defineSTORAGE_GROW_UNIT104857600LL
/* bytes */block/fvd.h new file +8
-0+ +/* State of BDRVFvdState.prefetch_state. */ +#define PREFETCH_STATE_RUNNING 1 +#define PREFETCH_STATE_FINISHED 2 +#define PREFETCH_STATE_DISABLED 3 + +/* For convience. */ +#define ROUND_UP (x, base) ((((x)+(base)-1) / (base)) *(base))
block/fvd.h new file +12
-0+#define ROUND_DOWN (x, base) ((((x) / (base)) * (base))) +#define BOOL (x) ((x) ? "true" : "false") +#define EMPTY_TABLE ((uint32_t)0xFFFFFFFF) +#define DIRTY_TABLE ((uint32_t)0x80000000) +#define READ_TABLE (entry) (le32_to_cpu(entry)& ~DIRTY_TABLE) +# define FVDAIOCB_MAGIC ((uint64_t)0x3A8FCE89325B976DULL) +# define FVD_ALLOC_MAGIC ((uint64_t)0x4A7dCEF9925B976DULL) +#define IS_EMPTY (entry) ((entry) == EMPTY_TABLE) +#define IS_DIRTY (entry) (le32_to_cpu(entry)& DIRTY_TABLE) +#define WRITE_TABLE (entry,id) ((entry) = cpu_to_le32(id)) +#define READ_TABLE2 (entry) \ + ((entry)==EMPTY_TABLE ? EMPTY_TABLE : (le32_to_cpu(entry)&~DIRTY_TABLE))
block/fvd.h new file +12
-0+ +#define CLEAN_DIRTY (entry) \ + do { \ + if (!IS_EMPTY(entry)) \ + entry = cpu_to_le32(le32_to_cpu(entry)& ~DIRTY_TABLE); \ + } while (0) + +#define CLEAN_DIRTY2 (entry) \ + do { \ + ASSERT(!IS_EMPTY(entry)); \ + entry = cpu_to_le32(le32_to_cpu(entry)& ~DIRTY_TABLE); \ + } while (0)
Quoted 12 lines
Quoted 12 lines
qemu-tool.c +0
-12-void qemu_bh_schedule (QEMUBH *bh) -{ - bh->cb (bh->opaque); -} - -void qemu_bh_cancel (QEMUBH *bh) -{ -} - -void qemu_bh_delete (QEMUBH *bh) -{ -qemu_free (bh);
Quoted 8 lines
qemu-tool.c void monitor_protocol_event(MonitorEvent event, QObject *data) · line 72
Quoted 6 lines
qemu-tool.c +0
-2-} - int qemu_set_fd_handler2(int fd, IOCanReadHandler *fd_read_poll, IOHandler *fd_read,These functions surely cannot just be deleted like this.
These functions were not deleted but instead moved into a separate file qemu-tool-time.c, because those functions are time related and the implementations are different in the simulation mode and in the real mode. In the latest patches, these functions are kept in qemu-tool.c but their implementations support both cases based on a switch.
Quoted 5 lines · Stefan Weil
Yes, that's a problem with some parts of the old code. For files which you want to modify, you could remove the spaces with your script before applying your other modifications and create a separate patch which only removes the superfluous spaces.
(This kind of came up in the other thread about fixing non-C89 comments. I don't have any particular interest in this area of the qemu source so this is a general remark.)
I definitely dislike patches which change whitespace or indentation for an entire file, even if they are standalone "only fixing whitespace" patches; they make it much harder to deal with forks and branches of qemu. I would prefer it if we only fix whitespace, indent and bracing for lines we're touching anyway.
-- PMM
Quoted 21 lines · Chunqiang Tang
Quoted 12 lines
Quoted 12 lines
qemu-tool.c +0
-12-void qemu_bh_schedule (QEMUBH *bh) -{ - bh->cb (bh->opaque); -} - -void qemu_bh_cancel (QEMUBH *bh) -{ -} - -void qemu_bh_delete (QEMUBH *bh) -{ -qemu_free (bh);Quoted 9 lines
qemu-tool.c void monitor_protocol_event(MonitorEvent event, QObject *data) · line 72
Quoted 7 lines
qemu-tool.c +0
-2-} - int qemu_set_fd_handler2(int fd, IOCanReadHandler *fd_read_poll, IOHandler *fd_read,These functions surely cannot just be deleted like this.
Quoted 6 lines
These functions were not deleted but instead moved into a separate file
qemu-tool-time.c, because those functions are time related and the implementations are different in the simulation mode and in the real mode. In the latest patches, these functions are kept inqemu-tool.cbut their implementations support both cases based on a switch.
I think the root of the problem is that your series didn't maintain bisectability.
IOW, each patch needs to be able to be applied one at a time such that at each point, the build doesn't break and functionality doesn't break.
Otherwise, tools like git bisect don't work.
Regards,
Anthony Liguori
Quoted 7 lines
I think the root of the problem is that your series didn't maintain bisectability.
IOW, each patch needs to be able to be applied one at a time such that at each point, the build doesn't break and functionality doesn't break.
Otherwise, tools like git bisect don't work.
This was true with the old, big FVD patches you reviewed. Following Christoph Hellwig's suggestion, the new series of FVD patches submitted last Friday, e.g., "FVD: Added the simulated 'blksim' driver", add individual smaller functions and breaks neither compilation nor execution.
Quoted 11 lines · Stefan Weil
Quoted 5 lines
Yes, that's a problem with some parts of the old code. For files which you want to modify, you could remove the spaces with your script before applying your other modifications and create a separate patch which only removes the superfluous spaces.
(This kind of came up in the other thread about fixing non-C89 comments. I don't have any particular interest in this area of the qemu source so this is a general remark.)
Quoted 6 lines
I definitely dislike patches which change whitespace or indentation for an entire file, even if they are standalone "only fixing whitespace" patches; they make it much harder to deal with forks and branches of qemu. I would prefer it if we only fix whitespace, indent and bracing for lines we're touching anyway.
I agree with this, i.e., only fixing the lines we are touching anyway. This is the approach the new series of FVD patches took.
Re: [Qemu-devel] [PATCH 3/5] Fast Virtual Disk (FVD) Proposal Part 3
In reply to Anthony LiguoriQuoted 9 lines
Before going any further with this series, I'd like to see
1) a specification (on the QEMU wiki) describing this image format that can be reviewed
2) a concise explanation of why qcow2/qed cannot satisfy the use cases addressed by FVD
3) performance data to backup the claims of (2)
Page created at http://wiki.qemu.org/Features/FVD. Content will be added soon.
Quoted 6 lines · Chunqiang Tang
Quoted 6 lines
I think the root of the problem is that your series didn't maintain bisectability.
IOW, each patch needs to be able to be applied one at a time such that at each point, the build doesn't break and functionality doesn't break.
Quoted 6 lines
Quoted 1 line
Otherwise, tools like git bisect don't work.
This was true with the old, big FVD patches you reviewed. Following Christoph Hellwig's suggestion, the new series of FVD patches submitted last Friday, e.g., "FVD: Added the simulated 'blksim' driver", add individual smaller functions and breaks neither compilation nor execution.
Then you need to break up the new patch into smaller chunks and make sure they can each be applied without breaking the build.
Having a separate patch that moves functions to another file, because they are too be shared, is a completely valid patch.
Jes