Skip to content

Commit 39b0ce9

Browse files
committed
Make espes memory block driver compile
1 parent 0d4f33b commit 39b0ce9

File tree

3 files changed

+163
-1
lines changed

3 files changed

+163
-1
lines changed

block/blkmemory.c

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* MemoryRegion backed block driver
3+
*
4+
* Copyright (c) 2013 espes
5+
*
6+
* Based on "Add an in-memory block device" patch
7+
* Copyright IBM, Corp. 2007
8+
* Authors:
9+
* Anthony Liguori <[email protected]>
10+
*
11+
* This program is free software; you can redistribute it and/or
12+
* modify it under the terms of the GNU General Public License as
13+
* published by the Free Software Foundation; either version 2 or
14+
* (at your option) version 3 of the License.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program; if not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
25+
#include "qemu/osdep.h"
26+
#include "qemu/cutils.h"
27+
28+
#include "exec/memory.h"
29+
#include "block/block_int.h"
30+
31+
#include "block/blkmemory.h"
32+
33+
// #define DEBUG_BLKMEMORY
34+
35+
typedef struct BDRVMemoryState {
36+
uint64_t size;
37+
AddressSpace *as;
38+
} BDRVMemoryState;
39+
40+
static int memory_open(BlockDriverState *bs, QDict *options, int flags,
41+
Error **errp)
42+
{
43+
/* We're kinda unique in that we're inited with a MemoryRegion instead
44+
* of a file. A MemoryRegion pointer can't be put in QDict, so we have
45+
* to be inited by hand. If something tries to init us normally, better
46+
* to fail than crash.
47+
*/
48+
return -1;
49+
}
50+
51+
// FIXME: crash on close
52+
static void memory_close(BlockDriverState *bs)
53+
{
54+
/* nothing to do..? */
55+
}
56+
57+
static int64_t memory_getlength(BlockDriverState *bs)
58+
{
59+
BDRVMemoryState *s = bs->opaque;
60+
61+
return s->size / BDRV_SECTOR_SIZE;
62+
}
63+
64+
static int memory_read(BlockDriverState *bs, int64_t offset,
65+
int64_t bytes, QEMUIOVector *qiov,
66+
BdrvRequestFlags flags)
67+
{
68+
BDRVMemoryState *s = bs->opaque;
69+
uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
70+
int nb_sectors = bytes >> BDRV_SECTOR_BITS;
71+
uint8_t *buf;
72+
void *orig_buf;
73+
74+
#ifdef DEBUG_BLKMEMORY
75+
printf("blkmemory read 0x%llx : %d\n", sector_num, nb_sectors);
76+
#endif
77+
78+
sector_num = MIN(sector_num, bs->total_sectors - 1);
79+
size_t size = MIN(s->size - sector_num * BDRV_SECTOR_SIZE,
80+
nb_sectors * BDRV_SECTOR_SIZE);
81+
82+
address_space_read(s->as, sector_num * BDRV_SECTOR_SIZE, MEMTXATTRS_UNSPECIFIED, buf, size);
83+
qemu_iovec_from_buf(qiov, 0, buf, size);
84+
85+
return 0;
86+
}
87+
88+
static int memory_write(BlockDriverState *bs,
89+
int64_t offset, int64_t bytes,
90+
QEMUIOVector *qiov,
91+
BdrvRequestFlags flags)
92+
{
93+
BDRVMemoryState *s = bs->opaque;
94+
uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
95+
int nb_sectors = bytes >> BDRV_SECTOR_BITS;
96+
uint8_t *buf;
97+
void *orig_buf;
98+
99+
#ifdef DEBUG_BLKMEMORY
100+
printf("blkmemory write 0x%llx : %d\n", sector_num, nb_sectors);
101+
#endif
102+
103+
sector_num = MIN(sector_num, bs->total_sectors - 1);
104+
size_t size = MIN(s->size - sector_num * BDRV_SECTOR_SIZE,
105+
nb_sectors * BDRV_SECTOR_SIZE);
106+
107+
address_space_write(s->as, sector_num * BDRV_SECTOR_SIZE, MEMTXATTRS_UNSPECIFIED, qiov->iov, size);
108+
109+
return 0;
110+
}
111+
112+
static BlockDriver bdrv_memory = {
113+
.format_name = "memory",
114+
.instance_size = sizeof(BDRVMemoryState),
115+
.bdrv_open = memory_open,
116+
.bdrv_close = memory_close,
117+
.bdrv_getlength = memory_getlength,
118+
119+
// FIXME: Is this what we want? What's the difference between CO and AIO?
120+
.bdrv_co_preadv = memory_read,
121+
.bdrv_co_pwritev = memory_write,
122+
};
123+
124+
static void bdrv_memory_init(void)
125+
{
126+
bdrv_register(&bdrv_memory);
127+
}
128+
129+
block_init(bdrv_memory_init);
130+
131+
132+
int bdrv_memory_open(BlockDriverState *bs, AddressSpace *as, uint64_t size)
133+
{
134+
bs->total_sectors = (size + BDRV_SECTOR_SIZE-1) / BDRV_SECTOR_SIZE;
135+
//bs->read_only = false;
136+
//bs->is_temporary = false;
137+
bs->encrypted = false;
138+
139+
pstrcpy(bs->filename, sizeof(bs->filename), "<mem>");
140+
141+
bs->drv = &bdrv_memory;
142+
bs->opaque = g_malloc0(bdrv_memory.instance_size);
143+
if (!bs->opaque) {
144+
return -1;
145+
}
146+
147+
BDRVMemoryState *s = bs->opaque;
148+
s->as = as;
149+
s->size = size;
150+
151+
return 0;
152+
}

block/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ block_ss.add(files(
3939
'throttle.c',
4040
'throttle-groups.c',
4141
'write-threshold.c',
42-
), zstd, zlib)
42+
'blkmemory.c',
43+
), zstd, zlib, gnutls)
4344

4445
system_ss.add(when: 'CONFIG_TCG', if_true: files('blkreplay.c'))
4546
system_ss.add(files('block-ram-registrar.c'))

include/block/blkmemory.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef BLKMEMORY_H
2+
#define BLKMEMORY_H
3+
4+
#include "block/block_int.h"
5+
#include "exec/memory.h"
6+
7+
int bdrv_memory_open(BlockDriverState *bs, AddressSpace *as, uint64_t size);
8+
9+
#endif

0 commit comments

Comments
 (0)