From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1J02NA-0006v0-4Q for qemu-devel@nongnu.org; Wed, 05 Dec 2007 17:05:40 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1J02N9-0006ti-1Q for qemu-devel@nongnu.org; Wed, 05 Dec 2007 17:05:39 -0500 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J02N8-0006tc-Uo for qemu-devel@nongnu.org; Wed, 05 Dec 2007 17:05:38 -0500 Received: from e32.co.us.ibm.com ([32.97.110.150]) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1J02N8-0007bW-Bl for qemu-devel@nongnu.org; Wed, 05 Dec 2007 17:05:38 -0500 Received: from d03relay04.boulder.ibm.com (d03relay04.boulder.ibm.com [9.17.195.106]) by e32.co.us.ibm.com (8.13.8/8.13.8) with ESMTP id lB5L3xwG006556 for ; Wed, 5 Dec 2007 16:03:59 -0500 Received: from d03av03.boulder.ibm.com (d03av03.boulder.ibm.com [9.17.195.169]) by d03relay04.boulder.ibm.com (8.13.8/8.13.8/NCO v8.7) with ESMTP id lB5M5KIG108132 for ; Wed, 5 Dec 2007 15:05:21 -0700 Received: from d03av03.boulder.ibm.com (loopback [127.0.0.1]) by d03av03.boulder.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id lB5M5Kse009022 for ; Wed, 5 Dec 2007 15:05:20 -0700 Received: from [9.53.41.166] (squirrel-009053041166.austin.ibm.com [9.53.41.166]) by d03av03.boulder.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id lB5M5Khe008970 for ; Wed, 5 Dec 2007 15:05:20 -0700 Message-ID: <4757209E.4020102@us.ibm.com> Date: Wed, 05 Dec 2007 16:05:18 -0600 From: Anthony Liguori MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------040805090100060602020301" Subject: [Qemu-devel] [PATCH 1/2] Add an in-memory block device Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org This is a multi-part message in MIME format. --------------040805090100060602020301 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit This is a generic in-memory block device. It is needed by the next patch. Regards, Anthony Liguori --------------040805090100060602020301 Content-Type: text/x-patch; name="mem-block-dev.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="mem-block-dev.diff" Index: qemu/Makefile =================================================================== --- qemu.orig/Makefile 2007-12-05 15:39:09.000000000 -0600 +++ qemu/Makefile 2007-12-05 15:39:39.000000000 -0600 @@ -40,7 +40,7 @@ BLOCK_OBJS=cutils.o BLOCK_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o BLOCK_OBJS+=block-dmg.o block-bochs.o block-vpc.o block-vvfat.o -BLOCK_OBJS+=block-qcow2.o block-parallels.o +BLOCK_OBJS+=block-qcow2.o block-parallels.o block-mem.o ###################################################################### # libqemu_common.a: Target indepedent part of system emulation. The Index: qemu/block-mem.c =================================================================== --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ qemu/block-mem.c 2007-12-05 15:54:02.000000000 -0600 @@ -0,0 +1,99 @@ +/* + * In-memory block driver + * + * Copyright IBM, Corp. 2007 + * + * Authors: + * Anthony Liguori + * + * This work is licensed under the terms of the GNU GPL, version 2. See + * the COPYING file in the top-level directory. + * + */ + +#include "qemu-common.h" +#include "block_int.h" + +typedef struct BDRVMemState { + void *mem; + size_t size; +} BDRVMemState; + +static int mem_probe(const uint8_t *buf, int buf_size, const char *filename) +{ + return 0; +} + +static int mem_open(BlockDriverState *bs, const char *filename, int flags) +{ + return -1; +} + +int bdrv_mem_open(BlockDriverState *bs, size_t size) +{ + BDRVMemState *s; + + bs->read_only = 0; + bs->is_temporary = 0; + bs->encrypted = 0; + pstrcpy(bs->filename, sizeof(bs->filename), ""); + bs->drv = &bdrv_mem; + bs->total_sectors = ((size + 511) & ~511) / 512; + + bs->opaque = qemu_mallocz(bdrv_mem.instance_size); + if (bs->opaque == NULL) + return -1; + + s = bs->opaque; + s->mem = qemu_mallocz(size); + if (s->mem == NULL) + return -1; + s->size = size; + + return 0; +} + +static int mem_read(BlockDriverState *bs, int64_t sector_num, + uint8_t *buf, int nb_sectors) +{ + BDRVMemState *s = bs->opaque; + size_t size; + + sector_num = MIN(sector_num, bs->total_sectors - 1); + size = MIN(s->size - (sector_num * 512), nb_sectors * 512); + + memcpy(buf, s->mem + (sector_num * 512), size); + + return 0; +} + +static int mem_write(BlockDriverState *bs, int64_t sector_num, + const uint8_t *buf, int nb_sectors) +{ + BDRVMemState *s = bs->opaque; + size_t size; + + sector_num = MIN(sector_num, bs->total_sectors - 1); + size = MIN(s->size - (sector_num * 512), nb_sectors * 512); + + memcpy(s->mem + (sector_num * 512), buf, size); + + return 0; +} + +static void mem_close(BlockDriverState *bs) +{ + BDRVMemState *s = bs->opaque; + + qemu_free(s->mem); +} + +BlockDriver bdrv_mem = { + "mem", + sizeof(BDRVMemState), + mem_probe, + mem_open, + mem_read, + mem_write, + mem_close, +}; Index: qemu/block.h =================================================================== --- qemu.orig/block.h 2007-12-05 15:38:48.000000000 -0600 +++ qemu/block.h 2007-12-05 15:47:36.000000000 -0600 @@ -16,6 +16,7 @@ extern BlockDriver bdrv_vvfat; extern BlockDriver bdrv_qcow2; extern BlockDriver bdrv_parallels; +extern BlockDriver bdrv_mem; typedef struct BlockDriverInfo { /* in bytes, 0 if irrelevant */ @@ -155,4 +156,6 @@ const char *base_path, const char *filename); +int bdrv_mem_open(BlockDriverState *bs, size_t size); + #endif --------------040805090100060602020301--