From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1M3WTG-00012G-3f for qemu-devel@nongnu.org; Mon, 11 May 2009 10:27:10 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1M3WTA-0000yk-9k for qemu-devel@nongnu.org; Mon, 11 May 2009 10:27:08 -0400 Received: from [199.232.76.173] (port=59243 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1M3WT9-0000yP-V5 for qemu-devel@nongnu.org; Mon, 11 May 2009 10:27:04 -0400 Received: from e5.ny.us.ibm.com ([32.97.182.145]:58676) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1M3WT9-00055F-D5 for qemu-devel@nongnu.org; Mon, 11 May 2009 10:27:03 -0400 Received: from d01relay06.pok.ibm.com (d01relay06.pok.ibm.com [9.56.227.116]) by e5.ny.us.ibm.com (8.13.1/8.13.1) with ESMTP id n4BELvSf006915 for ; Mon, 11 May 2009 10:21:57 -0400 Received: from d03av02.boulder.ibm.com (d03av02.boulder.ibm.com [9.17.195.168]) by d01relay06.pok.ibm.com (8.13.8/8.13.8/NCO v9.2) with ESMTP id n4BEQtW71638576 for ; Mon, 11 May 2009 10:26:55 -0400 Received: from d03av02.boulder.ibm.com (loopback [127.0.0.1]) by d03av02.boulder.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id n4BEQsKJ008634 for ; Mon, 11 May 2009 08:26:54 -0600 From: Anthony Liguori Date: Mon, 11 May 2009 09:26:47 -0500 Message-Id: <1242052009-27339-3-git-send-email-aliguori@us.ibm.com> In-Reply-To: <1242052009-27339-1-git-send-email-aliguori@us.ibm.com> References: <1242052009-27339-1-git-send-email-aliguori@us.ibm.com> Subject: [Qemu-devel] [PATCH 2/4] Convert block infrastructure to use new module init functionality List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Anthony Liguori , Paul Brook Signed-off-by: Anthony Liguori diff --git a/block-bochs.c b/block-bochs.c index 7a75412..5f7db83 100644 --- a/block-bochs.c +++ b/block-bochs.c @@ -24,6 +24,7 @@ */ #include "qemu-common.h" #include "block_int.h" +#include "module.h" /**************************************************************/ @@ -241,7 +242,7 @@ static void bochs_close(BlockDriverState *bs) close(s->fd); } -BlockDriver bdrv_bochs = { +static BlockDriver bdrv_bochs = { .format_name = "bochs", .instance_size = sizeof(BDRVBochsState), .bdrv_probe = bochs_probe, @@ -249,3 +250,11 @@ BlockDriver bdrv_bochs = { .bdrv_read = bochs_read, .bdrv_close = bochs_close, }; + +static int bdrv_bochs_init(void) +{ + bdrv_register(&bdrv_bochs); + return 0; +} + +block_init(bdrv_bochs_init); diff --git a/block-cloop.c b/block-cloop.c index 9414d10..8af9773 100644 --- a/block-cloop.c +++ b/block-cloop.c @@ -23,6 +23,7 @@ */ #include "qemu-common.h" #include "block_int.h" +#include "module.h" #include typedef struct BDRVCloopState { @@ -153,7 +154,7 @@ static void cloop_close(BlockDriverState *bs) inflateEnd(&s->zstream); } -BlockDriver bdrv_cloop = { +static BlockDriver bdrv_cloop = { .format_name = "cloop", .instance_size = sizeof(BDRVCloopState), .bdrv_probe = cloop_probe, @@ -161,3 +162,11 @@ BlockDriver bdrv_cloop = { .bdrv_read = cloop_read, .bdrv_close = cloop_close, }; + +static int bdrv_cloop_init(void) +{ + bdrv_register(&bdrv_cloop); + return 0; +} + +block_init(bdrv_cloop_init); diff --git a/block-cow.c b/block-cow.c index 17e3292..0a49491 100644 --- a/block-cow.c +++ b/block-cow.c @@ -24,6 +24,7 @@ #ifndef _WIN32 #include "qemu-common.h" #include "block_int.h" +#include "module.h" #include /**************************************************************/ @@ -252,7 +253,7 @@ static void cow_flush(BlockDriverState *bs) fsync(s->fd); } -BlockDriver bdrv_cow = { +static BlockDriver bdrv_cow = { .format_name = "cow", .instance_size = sizeof(BDRVCowState), .bdrv_probe = cow_probe, @@ -264,4 +265,12 @@ BlockDriver bdrv_cow = { .bdrv_flush = cow_flush, .bdrv_is_allocated = cow_is_allocated, }; + +static int bdrv_cow_init(void) +{ + bdrv_register(&bdrv_cow); + return 0; +} + +block_init(bdrv_cow_init); #endif diff --git a/block-dmg.c b/block-dmg.c index 82f6de1..6a78965 100644 --- a/block-dmg.c +++ b/block-dmg.c @@ -24,6 +24,7 @@ #include "qemu-common.h" #include "block_int.h" #include "bswap.h" +#include "module.h" #include typedef struct BDRVDMGState { @@ -92,7 +93,7 @@ static int dmg_open(BlockDriverState *bs, const char *filename, int flags) dmg_close: close(s->fd); /* open raw instead */ - bs->drv=&bdrv_raw; + bs->drv=bdrv_find_format("raw"); return bs->drv->bdrv_open(bs, filename, flags); } info_begin=read_off(s->fd); @@ -283,7 +284,7 @@ static void dmg_close(BlockDriverState *bs) inflateEnd(&s->zstream); } -BlockDriver bdrv_dmg = { +static BlockDriver bdrv_dmg = { .format_name = "dmg", .instance_size = sizeof(BDRVDMGState), .bdrv_probe = dmg_probe, @@ -291,3 +292,11 @@ BlockDriver bdrv_dmg = { .bdrv_read = dmg_read, .bdrv_close = dmg_close, }; + +static int bdrv_dmg_init(void) +{ + bdrv_register(&bdrv_dmg); + return 0; +} + +block_init(bdrv_dmg_init); diff --git a/block-nbd.c b/block-nbd.c index 632cb2d..59ceb4d 100644 --- a/block-nbd.c +++ b/block-nbd.c @@ -28,6 +28,7 @@ #include "qemu-common.h" #include "nbd.h" +#include "module.h" #include #include @@ -176,7 +177,7 @@ static int64_t nbd_getlength(BlockDriverState *bs) return s->size; } -BlockDriver bdrv_nbd = { +static BlockDriver bdrv_nbd = { .format_name = "nbd", .instance_size = sizeof(BDRVNBDState), .bdrv_open = nbd_open, @@ -186,3 +187,11 @@ BlockDriver bdrv_nbd = { .bdrv_getlength = nbd_getlength, .protocol_name = "nbd", }; + +static int bdrv_nbd_init(void) +{ + bdrv_register(&bdrv_nbd); + return 0; +} + +block_init(bdrv_nbd_init); diff --git a/block-parallels.c b/block-parallels.c index 18c3d83..3f46396 100644 --- a/block-parallels.c +++ b/block-parallels.c @@ -25,6 +25,7 @@ */ #include "qemu-common.h" #include "block_int.h" +#include "module.h" /**************************************************************/ @@ -163,7 +164,7 @@ static void parallels_close(BlockDriverState *bs) close(s->fd); } -BlockDriver bdrv_parallels = { +static BlockDriver bdrv_parallels = { .format_name = "parallels", .instance_size = sizeof(BDRVParallelsState), .bdrv_probe = parallels_probe, @@ -171,3 +172,11 @@ BlockDriver bdrv_parallels = { .bdrv_read = parallels_read, .bdrv_close = parallels_close, }; + +static int bdrv_parallels_init(void) +{ + bdrv_register(&bdrv_parallels); + return 0; +} + +block_init(bdrv_parallels_init); diff --git a/block-qcow.c b/block-qcow.c index fc6b809..cc25f69 100644 --- a/block-qcow.c +++ b/block-qcow.c @@ -23,6 +23,7 @@ */ #include "qemu-common.h" #include "block_int.h" +#include "module.h" #include #include "aes.h" @@ -917,7 +918,7 @@ static int qcow_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) return 0; } -BlockDriver bdrv_qcow = { +static BlockDriver bdrv_qcow = { .format_name = "qcow", .instance_size = sizeof(BDRVQcowState), .bdrv_probe = qcow_probe, @@ -935,3 +936,11 @@ BlockDriver bdrv_qcow = { .bdrv_write_compressed = qcow_write_compressed, .bdrv_get_info = qcow_get_info, }; + +static int bdrv_qcow_init(void) +{ + bdrv_register(&bdrv_qcow); + return 0; +} + +block_init(bdrv_qcow_init); diff --git a/block-qcow2.c b/block-qcow2.c index 9a49777..d8e5da3 100644 --- a/block-qcow2.c +++ b/block-qcow2.c @@ -23,6 +23,7 @@ */ #include "qemu-common.h" #include "block_int.h" +#include "module.h" #include #include "aes.h" #include @@ -2892,7 +2893,7 @@ static int qcow_get_buffer(BlockDriverState *bs, uint8_t *buf, return ret; } -BlockDriver bdrv_qcow2 = { +static BlockDriver bdrv_qcow2 = { .format_name = "qcow2", .instance_size = sizeof(BDRVQcowState), .bdrv_probe = qcow_probe, @@ -2922,3 +2923,11 @@ BlockDriver bdrv_qcow2 = { .bdrv_create2 = qcow_create2, .bdrv_check = qcow_check, }; + +static int bdrv_qcow2_init(void) +{ + bdrv_register(&bdrv_qcow2); + return 0; +} + +block_init(bdrv_qcow2_init); diff --git a/block-raw-posix.c b/block-raw-posix.c index 0663c06..74a64fc 100644 --- a/block-raw-posix.c +++ b/block-raw-posix.c @@ -25,6 +25,7 @@ #include "qemu-timer.h" #include "qemu-char.h" #include "block_int.h" +#include "module.h" #include #ifdef CONFIG_AIO #include "posix-aio-compat.h" @@ -846,7 +847,7 @@ static void raw_flush(BlockDriverState *bs) fsync(s->fd); } -BlockDriver bdrv_raw = { +static BlockDriver bdrv_raw = { .format_name = "raw", .instance_size = sizeof(BDRVRawState), .bdrv_probe = NULL, /* no probe for protocols */ @@ -1398,7 +1399,7 @@ static int hdev_create(const char *filename, int64_t total_size, } #endif -BlockDriver bdrv_host_device = { +static BlockDriver bdrv_host_device = { .format_name = "host_device", .instance_size = sizeof(BDRVRawState), .bdrv_open = hdev_open, @@ -1428,3 +1429,12 @@ BlockDriver bdrv_host_device = { .bdrv_aio_ioctl = raw_aio_ioctl, #endif }; + +static int bdrv_raw_init(void) +{ + bdrv_register(&bdrv_raw); + bdrv_register(&bdrv_host_device); + return 0; +} + +block_init(bdrv_raw_init); diff --git a/block-raw-win32.c b/block-raw-win32.c index b5287d2..e24adad 100644 --- a/block-raw-win32.c +++ b/block-raw-win32.c @@ -24,6 +24,7 @@ #include "qemu-common.h" #include "qemu-timer.h" #include "block_int.h" +#include "module.h" #include #include #include @@ -228,7 +229,7 @@ static int raw_create(const char *filename, int64_t total_size, return 0; } -BlockDriver bdrv_raw = { +static BlockDriver bdrv_raw = { .format_name = "raw", .instance_size = sizeof(BDRVRawState), .bdrv_open = raw_open, @@ -372,7 +373,7 @@ static int raw_set_locked(BlockDriverState *bs, int locked) } #endif -BlockDriver bdrv_host_device = { +static BlockDriver bdrv_host_device = { .format_name = "host_device", .instance_size = sizeof(BDRVRawState), .bdrv_open = hdev_open, @@ -383,3 +384,11 @@ BlockDriver bdrv_host_device = { .bdrv_write = raw_write, .bdrv_getlength = raw_getlength, }; + +static int bdrv_raw_init(void) +{ + bdrv_register(&bdrv_raw); + bdrv_register(&bdrv_host_device); +} + +block_init(bdrv_raw_init); diff --git a/block-vmdk.c b/block-vmdk.c index d47d483..35aca07 100644 --- a/block-vmdk.c +++ b/block-vmdk.c @@ -25,6 +25,7 @@ #include "qemu-common.h" #include "block_int.h" +#include "module.h" #define VMDK3_MAGIC (('C' << 24) | ('O' << 16) | ('W' << 8) | 'D') #define VMDK4_MAGIC (('K' << 24) | ('D' << 16) | ('M' << 8) | 'V') @@ -811,7 +812,7 @@ static void vmdk_flush(BlockDriverState *bs) bdrv_flush(s->hd); } -BlockDriver bdrv_vmdk = { +static BlockDriver bdrv_vmdk = { .format_name = "vmdk", .instance_size = sizeof(BDRVVmdkState), .bdrv_probe = vmdk_probe, @@ -823,3 +824,11 @@ BlockDriver bdrv_vmdk = { .bdrv_flush = vmdk_flush, .bdrv_is_allocated = vmdk_is_allocated, }; + +static int bdrv_vmdk_init(void) +{ + bdrv_register(&bdrv_vmdk); + return 0; +} + +block_init(bdrv_vmdk_init); diff --git a/block-vpc.c b/block-vpc.c index 71a171d..0bbda1e 100644 --- a/block-vpc.c +++ b/block-vpc.c @@ -24,6 +24,7 @@ */ #include "qemu-common.h" #include "block_int.h" +#include "module.h" /**************************************************************/ @@ -586,7 +587,7 @@ static void vpc_close(BlockDriverState *bs) bdrv_delete(s->hd); } -BlockDriver bdrv_vpc = { +static BlockDriver bdrv_vpc = { .format_name = "vpc", .instance_size = sizeof(BDRVVPCState), .bdrv_probe = vpc_probe, @@ -596,3 +597,11 @@ BlockDriver bdrv_vpc = { .bdrv_close = vpc_close, .bdrv_create = vpc_create, }; + +static int bdrv_vpc_init(void) +{ + bdrv_register(&bdrv_vpc); + return 0; +} + +block_init(bdrv_vpc_init); diff --git a/block-vvfat.c b/block-vvfat.c index 7905931..0a151f1 100644 --- a/block-vvfat.c +++ b/block-vvfat.c @@ -27,6 +27,7 @@ #include #include "qemu-common.h" #include "block_int.h" +#include "module.h" #ifndef S_IWGRP #define S_IWGRP 0 @@ -2777,7 +2778,7 @@ static int enable_write_target(BDRVVVFATState *s) s->qcow_filename = qemu_malloc(1024); get_tmp_filename(s->qcow_filename, 1024); - if (bdrv_create(&bdrv_qcow, + if (bdrv_create(bdrv_find_format("qcow"), s->qcow_filename, s->sector_count, "fat:", 0) < 0) return -1; s->qcow = bdrv_new(""); @@ -2807,7 +2808,7 @@ static void vvfat_close(BlockDriverState *bs) free(s->cluster_buffer); } -BlockDriver bdrv_vvfat = { +static BlockDriver bdrv_vvfat = { .format_name = "vvfat", .instance_size = sizeof(BDRVVVFATState), .bdrv_open = vvfat_open, @@ -2818,6 +2819,14 @@ BlockDriver bdrv_vvfat = { .protocol_name = "fat", }; +static int bdrv_vvfat_init(void) +{ + bdrv_register(&bdrv_vvfat); + return 0; +} + +block_init(bdrv_vvfat_init); + #ifdef DEBUG static void checkpoint(void) { assert(((mapping_t*)array_get(&(vvv->mapping), 0))->end == 2); diff --git a/block.c b/block.c index acb8976..0dd7436 100644 --- a/block.c +++ b/block.c @@ -30,6 +30,7 @@ #include "qemu-common.h" #include "monitor.h" #include "block_int.h" +#include "module.h" #ifdef HOST_BSD #include @@ -138,7 +139,7 @@ void path_combine(char *dest, int dest_size, } -static void bdrv_register(BlockDriver *bdrv) +void bdrv_register(BlockDriver *bdrv) { if (!bdrv->bdrv_aio_readv) { /* add AIO emulation layer */ @@ -259,11 +260,11 @@ static BlockDriver *find_protocol(const char *filename) #ifdef _WIN32 if (is_windows_drive(filename) || is_windows_drive_prefix(filename)) - return &bdrv_raw; + return bdrv_find_format("raw"); #endif p = strchr(filename, ':'); if (!p) - return &bdrv_raw; + return bdrv_find_format("raw"); len = p - filename; if (len > sizeof(protocol) - 1) len = sizeof(protocol) - 1; @@ -289,23 +290,23 @@ static BlockDriver *find_image_format(const char *filename) /* detect host devices. By convention, /dev/cdrom[N] is always recognized as a host CDROM */ if (strstart(filename, "/dev/cdrom", NULL)) - return &bdrv_host_device; + return bdrv_find_format("host_device"); #ifdef _WIN32 if (is_windows_drive(filename)) - return &bdrv_host_device; + return bdrv_find_format("host_device"); #else { struct stat st; if (stat(filename, &st) >= 0 && (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { - return &bdrv_host_device; + return bdrv_find_format("host_device"); } } #endif drv = find_protocol(filename); /* no need to test disk image formats for vvfat */ - if (drv == &bdrv_vvfat) + if (strcmp(drv->format_name, "vvfat") == 0) return drv; ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); @@ -396,14 +397,14 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, else realpath(filename, backing_filename); - ret = bdrv_create2(&bdrv_qcow2, tmp_filename, + ret = bdrv_create2(bdrv_find_format("qcow2"), tmp_filename, total_size, backing_filename, (drv ? drv->format_name : NULL), 0); if (ret < 0) { return ret; } filename = tmp_filename; - drv = &bdrv_qcow2; + drv = bdrv_find_format("qcow2"); bs->is_temporary = 1; } @@ -1494,21 +1495,10 @@ static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, void bdrv_init(void) { - bdrv_register(&bdrv_raw); - bdrv_register(&bdrv_host_device); -#ifndef _WIN32 - bdrv_register(&bdrv_cow); -#endif - bdrv_register(&bdrv_qcow); - bdrv_register(&bdrv_vmdk); - bdrv_register(&bdrv_cloop); - bdrv_register(&bdrv_dmg); - bdrv_register(&bdrv_bochs); - bdrv_register(&bdrv_vpc); - bdrv_register(&bdrv_vvfat); - bdrv_register(&bdrv_qcow2); - bdrv_register(&bdrv_parallels); - bdrv_register(&bdrv_nbd); + if (module_call_init(MOD_PRI_BLOCK) != 0) { + fprintf(stderr, "Failed to load one or more block drivers\n"); + exit(1); + } } void aio_pool_init(AIOPool *pool, int aiocb_size, diff --git a/block.h b/block.h index 5aef076..22df8ca 100644 --- a/block.h +++ b/block.h @@ -7,20 +7,6 @@ /* block.c */ typedef struct BlockDriver BlockDriver; -extern BlockDriver bdrv_raw; -extern BlockDriver bdrv_host_device; -extern BlockDriver bdrv_cow; -extern BlockDriver bdrv_qcow; -extern BlockDriver bdrv_vmdk; -extern BlockDriver bdrv_cloop; -extern BlockDriver bdrv_dmg; -extern BlockDriver bdrv_bochs; -extern BlockDriver bdrv_vpc; -extern BlockDriver bdrv_vvfat; -extern BlockDriver bdrv_qcow2; -extern BlockDriver bdrv_parallels; -extern BlockDriver bdrv_nbd; - typedef struct BlockDriverInfo { /* in bytes, 0 if irrelevant */ int cluster_size; @@ -87,6 +73,8 @@ int64_t bdrv_getlength(BlockDriverState *bs); void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr); void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs); int bdrv_commit(BlockDriverState *bs); +void bdrv_register(BlockDriver *bdrv); + /* async block I/O */ typedef struct BlockDriverAIOCB BlockDriverAIOCB; typedef void BlockDriverCompletionFunc(void *opaque, int ret); diff --git a/qemu-img.c b/qemu-img.c index 29149a2..018d32b 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -543,11 +543,11 @@ static int img_convert(int argc, char **argv) drv = bdrv_find_format(out_fmt); if (!drv) error("Unknown file format '%s'", out_fmt); - if (flags & BLOCK_FLAG_COMPRESS && drv != &bdrv_qcow && drv != &bdrv_qcow2) + if (flags & BLOCK_FLAG_COMPRESS && strcmp(drv->format_name, "qcow") && strcmp(drv->format_name, "qcow2")) error("Compression not supported for this file format"); - if (flags & BLOCK_FLAG_ENCRYPT && drv != &bdrv_qcow && drv != &bdrv_qcow2) + if (flags & BLOCK_FLAG_ENCRYPT && strcmp(drv->format_name, "qcow") && strcmp(drv->format_name, "qcow2")) error("Encryption not supported for this file format"); - if (flags & BLOCK_FLAG_COMPAT6 && drv != &bdrv_vmdk) + if (flags & BLOCK_FLAG_COMPAT6 && strcmp(drv->format_name, "vmdk")) error("Alternative compatibility level not supported for this file format"); if (flags & BLOCK_FLAG_ENCRYPT && flags & BLOCK_FLAG_COMPRESS) error("Compression and encryption not supported at the same time"); @@ -656,7 +656,7 @@ static int img_convert(int argc, char **argv) if (n > bs_offset + bs_sectors - sector_num) n = bs_offset + bs_sectors - sector_num; - if (drv != &bdrv_host_device) { + if (strcmp(drv->format_name, "host_device")) { if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset, n, &n1)) { sector_num += n1; @@ -683,7 +683,7 @@ static int img_convert(int argc, char **argv) If the output is to a host device, we also write out sectors that are entirely 0, since whatever data was already there is garbage, not 0s. */ - if (drv == &bdrv_host_device || out_baseimg || + if (strcmp(drv->format_name, "host_device") == 0 || out_baseimg || is_allocated_sectors(buf1, n, &n1)) { if (bdrv_write(out_bs, sector_num, buf1, n1) < 0) error("error while writing"); -- 1.6.0.6