* [Qemu-devel] [PATCH 3/3] cow: use qemu block API
@ 2010-06-07 10:06 Christoph Hellwig
2010-06-07 12:27 ` Markus Armbruster
2010-06-08 16:50 ` Kevin Wolf
0 siblings, 2 replies; 5+ messages in thread
From: Christoph Hellwig @ 2010-06-07 10:06 UTC (permalink / raw)
To: qemu-devel
Use bdrv_pwrite to access the backing device instead of pread, and
convert the driver to implementing the bdrv_open method which gives
it an already opened BlockDriverState for the underlying device.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Index: qemu/block/cow.c
===================================================================
--- qemu.orig/block/cow.c 2010-05-07 17:07:56.000000000 +0200
+++ qemu/block/cow.c 2010-05-07 17:11:11.498255489 +0200
@@ -42,7 +42,6 @@ struct cow_header_v2 {
};
typedef struct BDRVCowState {
- int fd;
int64_t cow_sectors_offset;
} BDRVCowState;
@@ -58,23 +57,16 @@ static int cow_probe(const uint8_t *buf,
return 0;
}
-static int cow_open(BlockDriverState *bs, const char *filename, int flags)
+static int cow_open(BlockDriverState *bs, int flags)
{
BDRVCowState *s = bs->opaque;
- int fd;
struct cow_header_v2 cow_header;
int bitmap_size;
int64_t size;
- fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
- if (fd < 0) {
- fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
- if (fd < 0)
- return -1;
- }
- s->fd = fd;
/* see if it is a cow image */
- if (pread(fd, &cow_header, sizeof(cow_header), 0) != sizeof(cow_header)) {
+ if (bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header)) !=
+ sizeof(cow_header)) {
goto fail;
}
@@ -94,7 +86,6 @@ static int cow_open(BlockDriverState *bs
s->cow_sectors_offset = (bitmap_size + 511) & ~511;
return 0;
fail:
- close(fd);
return -1;
}
@@ -104,18 +95,17 @@ static int cow_open(BlockDriverState *bs
*/
static inline int cow_set_bit(BlockDriverState *bs, int64_t bitnum)
{
- BDRVCowState *s = bs->opaque;
uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
uint8_t bitmap;
- if (pread(s->fd, &bitmap, sizeof(bitmap), offset) !=
+ if (bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)) !=
sizeof(bitmap)) {
return -errno;
}
bitmap |= (1 << (bitnum % 8));
- if (pwrite(s->fd, &bitmap, sizeof(bitmap), offset) !=
+ if (bdrv_pwrite(bs->file, offset, &bitmap, sizeof(bitmap)) !=
sizeof(bitmap)) {
return -errno;
}
@@ -124,11 +114,10 @@ static inline int cow_set_bit(BlockDrive
static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum)
{
- BDRVCowState *s = bs->opaque;
uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
uint8_t bitmap;
- if (pread(s->fd, &bitmap, sizeof(bitmap), offset) !=
+ if (bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)) !=
sizeof(bitmap)) {
return -errno;
}
@@ -186,8 +175,9 @@ static int cow_read(BlockDriverState *bs
while (nb_sectors > 0) {
if (cow_is_allocated(bs, sector_num, nb_sectors, &n)) {
- ret = pread(s->fd, buf, n * 512,
- s->cow_sectors_offset + sector_num * 512);
+ ret = bdrv_pread(bs->file,
+ s->cow_sectors_offset + sector_num * 512,
+ buf, n * 512);
if (ret != n * 512)
return -1;
} else {
@@ -213,8 +203,8 @@ static int cow_write(BlockDriverState *b
BDRVCowState *s = bs->opaque;
int ret;
- ret = pwrite(s->fd, buf, nb_sectors * 512,
- s->cow_sectors_offset + sector_num * 512);
+ ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512,
+ buf, nb_sectors * 512);
if (ret != nb_sectors * 512)
return -1;
@@ -223,8 +213,6 @@ static int cow_write(BlockDriverState *b
static void cow_close(BlockDriverState *bs)
{
- BDRVCowState *s = bs->opaque;
- close(s->fd);
}
static int cow_create(const char *filename, QEMUOptionParameter *options)
@@ -294,8 +282,7 @@ exit:
static void cow_flush(BlockDriverState *bs)
{
- BDRVCowState *s = bs->opaque;
- qemu_fdatasync(s->fd);
+ bdrv_flush(bs->file);
}
static QEMUOptionParameter cow_create_options[] = {
@@ -316,7 +303,7 @@ static BlockDriver bdrv_cow = {
.format_name = "cow",
.instance_size = sizeof(BDRVCowState),
.bdrv_probe = cow_probe,
- .bdrv_file_open = cow_open,
+ .bdrv_open = cow_open,
.bdrv_read = cow_read,
.bdrv_write = cow_write,
.bdrv_close = cow_close,
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH 3/3] cow: use qemu block API
2010-06-07 10:06 [Qemu-devel] [PATCH 3/3] cow: use qemu block API Christoph Hellwig
@ 2010-06-07 12:27 ` Markus Armbruster
2010-06-08 8:52 ` Christoph Hellwig
2010-06-08 16:50 ` Kevin Wolf
1 sibling, 1 reply; 5+ messages in thread
From: Markus Armbruster @ 2010-06-07 12:27 UTC (permalink / raw)
To: Christoph Hellwig; +Cc: qemu-devel
Christoph Hellwig <hch@lst.de> writes:
> Use bdrv_pwrite to access the backing device instead of pread, and
> convert the driver to implementing the bdrv_open method which gives
> it an already opened BlockDriverState for the underlying device.
>
> Signed-off-by: Christoph Hellwig <hch@lst.de>
In my (admittedly limited) understanding, we need this for sane protocol
handling in -blockdev & friends.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH 3/3] cow: use qemu block API
2010-06-07 12:27 ` Markus Armbruster
@ 2010-06-08 8:52 ` Christoph Hellwig
0 siblings, 0 replies; 5+ messages in thread
From: Christoph Hellwig @ 2010-06-08 8:52 UTC (permalink / raw)
To: Markus Armbruster; +Cc: qemu-devel
On Mon, Jun 07, 2010 at 02:27:40PM +0200, Markus Armbruster wrote:
> Christoph Hellwig <hch@lst.de> writes:
>
> > Use bdrv_pwrite to access the backing device instead of pread, and
> > convert the driver to implementing the bdrv_open method which gives
> > it an already opened BlockDriverState for the underlying device.
> >
> > Signed-off-by: Christoph Hellwig <hch@lst.de>
>
> In my (admittedly limited) understanding, we need this for sane protocol
> handling in -blockdev & friends.
Yes - not just in -blockdev but also in general.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH 3/3] cow: use qemu block API
2010-06-07 10:06 [Qemu-devel] [PATCH 3/3] cow: use qemu block API Christoph Hellwig
2010-06-07 12:27 ` Markus Armbruster
@ 2010-06-08 16:50 ` Kevin Wolf
1 sibling, 0 replies; 5+ messages in thread
From: Kevin Wolf @ 2010-06-08 16:50 UTC (permalink / raw)
To: Christoph Hellwig; +Cc: qemu-devel
Am 07.06.2010 12:06, schrieb Christoph Hellwig:
> Use bdrv_pwrite to access the backing device instead of pread, and
> convert the driver to implementing the bdrv_open method which gives
> it an already opened BlockDriverState for the underlying device.
>
> Signed-off-by: Christoph Hellwig <hch@lst.de>
Thanks, applied all to the block branch.
Kevin
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Qemu-devel] [PATCH 0/3] cow conversion to the block API
@ 2010-05-07 15:16 Christoph Hellwig
2010-05-07 15:17 ` [Qemu-devel] [PATCH 3/3] cow: use qemu " Christoph Hellwig
0 siblings, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2010-05-07 15:16 UTC (permalink / raw)
To: qemu-devel
This series converts the cow image driver to the block API. This fixes
a failure in qemu-iotests 012, and allows compiling it on window by
removing the dependency on mmap. The downside is that it's possible
a lot slower for bitmap accesses, at least with the current naive
implementation. Anyone who cares seriously enough about cow is welcome
to optimize the bitmap access.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Qemu-devel] [PATCH 3/3] cow: use qemu block API
2010-05-07 15:16 [Qemu-devel] [PATCH 0/3] cow conversion to the " Christoph Hellwig
@ 2010-05-07 15:17 ` Christoph Hellwig
0 siblings, 0 replies; 5+ messages in thread
From: Christoph Hellwig @ 2010-05-07 15:17 UTC (permalink / raw)
To: qemu-devel
Use bdrv_pwrite to access the backing device instead of pread, and
convert the driver to implementing the bdrv_open method which gives
it an already opened BlockDriverState for the underlying device.
Signed-off-by: Christoph Hellwig <hch@lst.de>
Index: qemu/block/cow.c
===================================================================
--- qemu.orig/block/cow.c 2010-05-07 17:07:56.000000000 +0200
+++ qemu/block/cow.c 2010-05-07 17:11:11.498255489 +0200
@@ -42,7 +42,6 @@ struct cow_header_v2 {
};
typedef struct BDRVCowState {
- int fd;
int64_t cow_sectors_offset;
} BDRVCowState;
@@ -58,23 +57,16 @@ static int cow_probe(const uint8_t *buf,
return 0;
}
-static int cow_open(BlockDriverState *bs, const char *filename, int flags)
+static int cow_open(BlockDriverState *bs, int flags)
{
BDRVCowState *s = bs->opaque;
- int fd;
struct cow_header_v2 cow_header;
int bitmap_size;
int64_t size;
- fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
- if (fd < 0) {
- fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
- if (fd < 0)
- return -1;
- }
- s->fd = fd;
/* see if it is a cow image */
- if (pread(fd, &cow_header, sizeof(cow_header), 0) != sizeof(cow_header)) {
+ if (bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header)) !=
+ sizeof(cow_header)) {
goto fail;
}
@@ -94,7 +86,6 @@ static int cow_open(BlockDriverState *bs
s->cow_sectors_offset = (bitmap_size + 511) & ~511;
return 0;
fail:
- close(fd);
return -1;
}
@@ -104,18 +95,17 @@ static int cow_open(BlockDriverState *bs
*/
static inline int cow_set_bit(BlockDriverState *bs, int64_t bitnum)
{
- BDRVCowState *s = bs->opaque;
uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
uint8_t bitmap;
- if (pread(s->fd, &bitmap, sizeof(bitmap), offset) !=
+ if (bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)) !=
sizeof(bitmap)) {
return -errno;
}
bitmap |= (1 << (bitnum % 8));
- if (pwrite(s->fd, &bitmap, sizeof(bitmap), offset) !=
+ if (bdrv_pwrite(bs->file, offset, &bitmap, sizeof(bitmap)) !=
sizeof(bitmap)) {
return -errno;
}
@@ -124,11 +114,10 @@ static inline int cow_set_bit(BlockDrive
static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum)
{
- BDRVCowState *s = bs->opaque;
uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
uint8_t bitmap;
- if (pread(s->fd, &bitmap, sizeof(bitmap), offset) !=
+ if (bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap)) !=
sizeof(bitmap)) {
return -errno;
}
@@ -186,8 +175,9 @@ static int cow_read(BlockDriverState *bs
while (nb_sectors > 0) {
if (cow_is_allocated(bs, sector_num, nb_sectors, &n)) {
- ret = pread(s->fd, buf, n * 512,
- s->cow_sectors_offset + sector_num * 512);
+ ret = bdrv_pread(bs->file,
+ s->cow_sectors_offset + sector_num * 512,
+ buf, n * 512);
if (ret != n * 512)
return -1;
} else {
@@ -213,8 +203,8 @@ static int cow_write(BlockDriverState *b
BDRVCowState *s = bs->opaque;
int ret;
- ret = pwrite(s->fd, buf, nb_sectors * 512,
- s->cow_sectors_offset + sector_num * 512);
+ ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512,
+ buf, nb_sectors * 512);
if (ret != nb_sectors * 512)
return -1;
@@ -223,8 +213,6 @@ static int cow_write(BlockDriverState *b
static void cow_close(BlockDriverState *bs)
{
- BDRVCowState *s = bs->opaque;
- close(s->fd);
}
static int cow_create(const char *filename, QEMUOptionParameter *options)
@@ -294,8 +282,7 @@ exit:
static void cow_flush(BlockDriverState *bs)
{
- BDRVCowState *s = bs->opaque;
- qemu_fdatasync(s->fd);
+ bdrv_flush(bs->file);
}
static QEMUOptionParameter cow_create_options[] = {
@@ -316,7 +303,7 @@ static BlockDriver bdrv_cow = {
.format_name = "cow",
.instance_size = sizeof(BDRVCowState),
.bdrv_probe = cow_probe,
- .bdrv_file_open = cow_open,
+ .bdrv_open = cow_open,
.bdrv_read = cow_read,
.bdrv_write = cow_write,
.bdrv_close = cow_close,
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-06-08 16:50 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-06-07 10:06 [Qemu-devel] [PATCH 3/3] cow: use qemu block API Christoph Hellwig
2010-06-07 12:27 ` Markus Armbruster
2010-06-08 8:52 ` Christoph Hellwig
2010-06-08 16:50 ` Kevin Wolf
-- strict thread matches above, loose matches on Subject: below --
2010-05-07 15:16 [Qemu-devel] [PATCH 0/3] cow conversion to the " Christoph Hellwig
2010-05-07 15:17 ` [Qemu-devel] [PATCH 3/3] cow: use qemu " Christoph Hellwig
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).