introduce BSD-licensed block driver for "raw"
v2 cover introduce BSD-licensed block driver for "raw"
v1->v2:
- added comment to raw_probe() [Kevin]
- fixed raw_reopen_prepare() [Paolo & Kevin]
- keeping BSDL: according to the v1 discussion, LGPLv2+ could work too, but "but most of the block layer is BSD" [Paolo] and I haven't been clearly instructed to switch to LGPLv2+.
diff --git a/block/raw_bsd.c b/block/raw_bsd.c
index 07e6f06..ab2b0fd 100644
--- a/block/raw_bsd.c
+++ b/block/raw_bsd.c
@@ -41,10 +41,7 @@ static QEMUOptionParameter raw_create_options[] = {
static int raw_reopen_prepare(BDRVReopenState *reopen_state,
BlockReopenQueue *queue, Error **errp)
{
- BDRVReopenState tmp = *reopen_state;
-
- tmp.bs = tmp.bs->file; - return bdrv_reopen_prepare(&tmp, queue, errp); + return 0;
}
static int coroutine_fn raw_co_readv(BlockDriverState *bs, int64_t sector_num,
@@ -150,6 +147,9 @@ static void raw_close(BlockDriverState *bs)
static int raw_probe(const uint8_t *buf, int buf_size, const char *filename) {
+ /* smallest possible positive score so that raw is used if and only if no
+ * other block driver works
+ */
return 1;
}
v1 blurb:
Paolo asked me to write such a driver based on his textual specification alone. The first patch captures his email in full, the rest re-quotes parts that are being implemented.
The tree compiles at each patch. The series passes "make check-block".
"block/raw.c" is not removed because I wanted to keep it out of my series and out of my brain.
Laszlo Ersek (7):
add skeleton for BSD licensed "raw" BlockDriver
raw_bsd: emit debug events in bdrv_co_readv() and bdrv_co_writev()
raw_bsd: add raw_create()
raw_bsd: introduce "special members"
raw_bsd: add raw_create_options
raw_bsd: register bdrv_raw
switch raw block driver from "raw.o" to "raw_bsd.o"
block/Makefile.objs | 2 +- block/raw_bsd.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+), 1 deletions(-) create mode 100644 block/raw_bsd.c
PATCH v2 1/7 add skeleton for BSD licensed "raw" BlockDriver
Quoted 87 lines · Paolo Bonzini
----- Original Message -----
Quoted 12 lines
From: "Laszlo Ersek" <[email protected]>
To: "Paolo Bonzini" <[email protected]>
Sent: Monday, August 5, 2013 2:43:46 PM
Subject: Re: [PATCH 1/2] raw: add license headerQuoted 6 lines · Paolo Bonzini
Quoted 5 lines · Christoph Hellwig
Quoted 3 lines · Paolo Bonzini
Most of the block layer is under the BSD license, thus it is reasonable to license
block/raw.cthe same way. CCed people should ACK by replying with a Signed-off-by line.Quoted 7 lines
Quoted 5 lines
Quoted 1 line
The coded was intended to be GPLv2.
Laszlo, would you be willing to do clean-room reverse engineering?
(No rants, please. :))
What's the scope exactly?
It's quite small, it's a file full of forwarders like
static void
raw_foo(BlockDriverState *bs){ return bdrv_foo(bs->file); }It's 170 lines of code, all as boring as this. I only picked you because I'm quite certain you have never seen the file (and the answer confirmed it).
Basically:
1) BlockDriver is a struct in which these function members are interesting:
.bdrv_reopen_prepare
.bdrv_co_readv
.bdrv_co_writev
.bdrv_co_is_allocated
.bdrv_co_write_zeroes
.bdrv_co_discard
.bdrv_getlength
.bdrv_get_info
.bdrv_truncate
.bdrv_is_inserted
.bdrv_media_changed
.bdrv_eject
.bdrv_lock_medium
.bdrv_ioctl
.bdrv_aio_ioctl
.bdrv_has_zero_initThey should be implemented as simple forwarders (see above). There are 16 functions listed here, you can easily see how this already accounts for 100+ SLOC roughly...
The implementations of
bdrv_co_readvandbdrv_co_writevshould also callBLKDBG_EVENTonbs->filetoo, before forwarding tobs->file. The events to be generated areBLKDBG_READ_AIOandBLKDBG_WRITE_AIO.2) This is also a simple forwarder function:
.bdrv_create
but there is no BlockDriverState argument so the forwarded-to function does not have a
bs->fileargument either. The forwarded-to function isbdrv_create_file.3) These members are special
.format_name is the string "raw" .bdrv_open raw_open should set bs->sg to bs->file->sg and return 0 .bdrv_close raw_close should do nothing .bdrv_probe raw_probe should just return 1.4) There is another member, .create_options, which is an array of QEMUOptionParameter structs, terminated by an all-zero item. The only option you need is for the virtual disk size. You will find something to copy from in other block drivers, for example
block/qcow2.c.5) Formats are registered with
bdrv_register(takes a BlockDriver*). You also need to pass the caller ofbdrv_registertoblock_init.
Quoted 8 lines
6) I'm not sure how to organize the patch series, so I'll leave this to your creativity. I guess in this case move/copy detection of git should be disabled. I would definitely include this spec in the commit message as a proof of clean-room reverse engineering.
7) Remember a BSD header like the one in
block.c.Paolo
This patch implements the email up to the paragraph ending with "100+ SLOC roughly". The skeleton is generated from the list there, with a simple shell loop using "sed" and the raw_foo() template.
The BSD license block is copied (and reflowed) from "util/qemu-progress.c".
--- block/raw_bsd.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 108 insertions(+), 0 deletions(-) create mode 100644 block/raw_bsd.c
block/raw_bsd.c new file +108 -0
@@ -0,0 +1,108 @@ +/* BlockDriver implementation for "raw" +* +* Copyright (C) 2013, Red Hat, Inc. +* +* Author: +* Laszlo Ersek <[email protected]> +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to +* deal in the Software without restriction, including without limitation the +* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +* sell copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +*/ + +#include "block/block_int.h" + +static TYPE raw_reopen_prepare (BlockDriverState *bs) +{ +return bdrv_reopen_prepare (bs->file); +} + +static TYPE raw_co_readv (BlockDriverState *bs) +{ +return bdrv_co_readv (bs->file); +} + +static TYPE raw_co_writev (BlockDriverState *bs) +{ +return bdrv_co_writev (bs->file); +} + +static TYPE raw_co_is_allocated (BlockDriverState *bs) +{ +return bdrv_co_is_allocated (bs->file); +} + +static TYPE raw_co_write_zeroes (BlockDriverState *bs) +{ +return bdrv_co_write_zeroes (bs->file); +} + +static TYPE raw_co_discard (BlockDriverState *bs) +{ +return bdrv_co_discard (bs->file); +} + +static TYPE raw_getlength (BlockDriverState *bs) +{ +return bdrv_getlength (bs->file); +} + +static TYPE raw_get_info (BlockDriverState *bs) +{ +return bdrv_get_info (bs->file); +} + +static TYPE raw_truncate (BlockDriverState *bs) +{ +return bdrv_truncate (bs->file); +} + +static TYPE raw_is_inserted (BlockDriverState *bs) +{ +return bdrv_is_inserted (bs->file); +} + +static TYPE raw_media_changed (BlockDriverState *bs) +{ +return bdrv_media_changed (bs->file); +} + +static TYPE raw_eject (BlockDriverState *bs) +{ +return bdrv_eject (bs->file); +} + +static TYPE raw_lock_medium (BlockDriverState *bs) +{ +return bdrv_lock_medium (bs->file); +} + +static TYPE raw_ioctl (BlockDriverState *bs) +{ +return bdrv_ioctl (bs->file); +} + +static TYPE raw_aio_ioctl (BlockDriverState *bs) +{ +return bdrv_aio_ioctl (bs->file); +} + +static TYPE raw_has_zero_init (BlockDriverState *bs) +{ +return bdrv_has_zero_init (bs->file); +} +
Signature · 1 line
1.7.1
PATCH v2 2/7 raw_bsd: emit debug events in bdrv_co_readv() and bdrv_co_writev()
In reply to Laszlo ErsekQuoted 23 lines · Paolo Bonzini
[...]
1) BlockDriver is a struct in which these function members are interesting:
.bdrv_reopen_prepare
.bdrv_co_readv
.bdrv_co_writev
.bdrv_co_is_allocated
.bdrv_co_write_zeroes
.bdrv_co_discard
.bdrv_getlength
.bdrv_get_info
.bdrv_truncate
.bdrv_is_inserted
.bdrv_media_changed
.bdrv_eject
.bdrv_lock_medium
.bdrv_ioctl
.bdrv_aio_ioctl
.bdrv_has_zero_init
Quoted 7 lines
They should be implemented as simple forwarders (see above). There are 16 functions listed here, you can easily see how this already accounts for 100+ SLOC roughly...
The implementations of
bdrv_co_readvandbdrv_co_writevshould also callBLKDBG_EVENTonbs->filetoo, before forwarding tobs->file. The events to be generated areBLKDBG_READ_AIOandBLKDBG_WRITE_AIO.
--- block/raw_bsd.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
block/raw_bsd.c +2 -0
@@ -33,11 +33,13 @@ static TYPE raw_reopen_prepare(BlockDriverState *bs)static TYPE raw_co_readv (BlockDriverState *bs) { +BLKDBG_EVENT (bs->file,BLKDBG_READ_AIO );return bdrv_co_readv (bs->file); }static TYPE raw_co_writev (BlockDriverState *bs) { +BLKDBG_EVENT (bs->file,BLKDBG_WRITE_AIO );return bdrv_co_writev (bs->file); }
Signature · 1 line
1.7.1
PATCH v2 3/7 raw_bsd: add raw_create()
In reply to Laszlo ErsekQuoted 10 lines · Paolo Bonzini
[...]
2) This is also a simple forwarder function:
.bdrv_create
but there is no BlockDriverState argument so the forwarded-to function does not have a
bs->fileargument either. The forwarded-to function isbdrv_create_file.
--- block/raw_bsd.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
block/raw_bsd.c +4 -0
@@ -108,3 +108,7 @@ static TYPE raw_has_zero_init(BlockDriverState *bs)return bdrv_has_zero_init (bs->file); } +static TYPE raw_create (void ) +{ +return bdrv_create_file (); +}
Signature · 1 line
1.7.1
PATCH v2 5/7 raw_bsd: add raw_create_options
In reply to Laszlo ErsekQuoted 7 lines · Paolo Bonzini
[...]
4) There is another member, .create_options, which is an array of QEMUOptionParameter structs, terminated by an all-zero item. The only option you need is for the virtual disk size. You will find something to copy from in other block drivers, for example
block/qcow2.c.
Code taken and adapted from "block/qcow2.c", as suggested. The code being copied/modified is blamed on
commit 20d97356c9df6d68fbd37d6334fdb7063f24eab6
Author: Blue Swirl <[email protected]>
Date: Fri Apr 23 20:19:47 2010 +0000
Fix OpenBSD build
and
commit 7c80ab3f21f0b1342f23057d4345ae266c7348d9
Author: Jes Sorensen <[email protected]>
Date: Fri Dec 17 16:02:39 2010 +0100
block/qcow2.c: rename qcow_ functions to qcow2_
--- block/raw_bsd.c | 13 ++++++++++++- 1 files changed, 12 insertions(+), 1 deletions(-)
block/raw_bsd.c +12 -1
@@ -1,6 +1,7 @@/* BlockDriver implementation for "raw" * -* Copyright (C) 2013, Red Hat, Inc. +* Copyright (C) 2010, 2013, Red Hat, Inc. +* Copyright (C) 2010, Blue Swirl <[email protected]> * * Author: * Laszlo Ersek <[email protected]>
@@ -25,6 +26,16 @@*/ #include "block/block_int.h" +#include "qemu/option.h" + +static const QEMUOptionParameter raw_create_options[] = { + { + .name =BLOCK_OPT_SIZE , + .type =OPT_SIZE , + .help ="Virtual disk size" + }, + {0 } +};static TYPE raw_reopen_prepare (BlockDriverState *bs) {
Signature · 1 line
1.7.1
PATCH v2 4/7 raw_bsd: introduce "special members"
In reply to Laszlo ErsekQuoted 9 lines · Paolo Bonzini
[...]
3) These members are special
.format_name is the string "raw" .bdrv_open raw_open should set bs->sg to bs->file->sg and return 0 .bdrv_close raw_close should do nothing .bdrv_probe raw_probe should just return 1.
v1->v2:
Quoted 9 lines · Kevin Wolf
Am 16.08.2013 um 16:15 hat Laszlo Ersek geschrieben:
Review
Quoted 4 lines
(quoted diff) +4
-0+static int raw_probe(void) +{ + return 1; +}Maybe add a comment here like "smallest possible positive score so that raw is used if and only if no other block driver works".
--- block/raw_bsd.c | 23 +++++++++++++++++++++++ 1 files changed, 23 insertions(+), 0 deletions(-)
block/raw_bsd.c +23 -0
@@ -112,3 +112,26 @@ static TYPE raw_create(void) {return bdrv_create_file (); } + +static const char *raw_format_name (void ) +{ +return "raw" ; +} + +static int raw_open (BlockDriverState *bs) +{ + bs->sg = bs->file->sg; +return 0 ; +} + +static void raw_close (void ) +{ +} + +static int raw_probe (void ) +{ +/* smallest possible positive score so that raw is used if and only if no +* other block driver works +*/ +return 1 ; +}
Signature · 1 line
1.7.1
PATCH v2 6/7 raw_bsd: register bdrv_raw
In reply to Laszlo ErsekQuoted 5 lines · Paolo Bonzini
[...]
5) Formats are registered with
bdrv_register(takes a BlockDriver*). You also need to pass the caller ofbdrv_registertoblock_init.
Fill in the BlockDriver structure with the raw_*() functions that have been added to "block/raw_bsd.c", in the order the fields are defined in "include/block/block_int.h".
I needed more explanation / naming examples for registering the driver than what Paolo gave me, so I copied / adapted from "block/qcow2.c". The parts I took as basis for modification are blamed on
commit 5efa9d5a8b18841c9c62208a494d7f519238979a
Author: Anthony Liguori <[email protected]>
Date: Sat May 9 17:03:42 2009 -0500
Convert block infrastructure to use new module init functionality
commit 20d97356c9df6d68fbd37d6334fdb7063f24eab6
Author: Blue Swirl <[email protected]>
Date: Fri Apr 23 20:19:47 2010 +0000
Fix OpenBSD build
--- block/raw_bsd.c | 38 +++++++++++++++++++++++++++++++++----- 1 files changed, 33 insertions(+), 5 deletions(-)
block/raw_bsd.c +33 -5
@@ -2,6 +2,7 @@* * Copyright (C) 2010, 2013, Red Hat, Inc. * Copyright (C) 2010, Blue Swirl <[email protected]> +* Copyright (C) 2009, Anthony Liguori <[email protected]> * * Author: * Laszlo Ersek <[email protected]>
@@ -124,11 +125,6 @@ static TYPE raw_create(void)return bdrv_create_file (); } -static const char *raw_format_name (void ) -{ -return "raw" ; -} -static int raw_open (BlockDriverState *bs) { bs->sg = bs->file->sg;
@@ -146,3 +142,35 @@ static int raw_probe(void)*/ return 1 ; } + +static BlockDriver bdrv_raw = { + .format_name ="raw" , + .bdrv_probe = &raw_probe, + .bdrv_reopen_prepare = &raw_reopen_prepare, + .bdrv_open = &raw_open, + .bdrv_close = &raw_close, + .bdrv_create = &raw_create, + .bdrv_co_readv = &raw_co_readv, + .bdrv_co_writev = &raw_co_writev, + .bdrv_co_write_zeroes = &raw_co_write_zeroes, + .bdrv_co_discard = &raw_co_discard, + .bdrv_co_is_allocated = &raw_co_is_allocated, + .bdrv_truncate = &raw_truncate, + .bdrv_getlength = &raw_getlength, + .bdrv_get_info = &raw_get_info, + .bdrv_is_inserted = &raw_is_inserted, + .bdrv_media_changed = &raw_media_changed, + .bdrv_eject = &raw_eject, + .bdrv_lock_medium = &raw_lock_medium, + .bdrv_ioctl = &raw_ioctl, + .bdrv_aio_ioctl = &raw_aio_ioctl, + .create_options = &raw_create_options[0 ], + .bdrv_has_zero_init = &raw_has_zero_init +}; + +static void bdrv_raw_init (void ) +{ +bdrv_register (&bdrv_raw); +} + +block_init (bdrv_raw_init);
Signature · 1 line
1.7.1
PATCH v2 7/7 switch raw block driver from "raw.o" to "raw_bsd.o"
In reply to Laszlo Ersek"Incoming" function prototypes and "outgoing" function calls must match reality. Implemented using the "struct BlockDriver" definition in "include/block/block_int.h", and gcc errors & warnings.
v1->v2:
Quoted 14 lines · Kevin Wolf
Am 18.08.2013 um 16:29 hat Paolo Bonzini geschrieben:
Quoted 12 lines
Il 16/08/2013 16:15, Laszlo Ersek ha scritto:
Review
Quoted 9 lines
(quoted diff) +6
-1+static int raw_reopen_prepare(BDRVReopenState *reopen_state, + BlockReopenQueue *queue, Error **errp) { - return bdrv_reopen_prepare(bs->file); + BDRVReopenState tmp = *reopen_state; + + tmp.bs = tmp.bs->file; + return bdrv_reopen_prepare(&tmp, queue, errp); }This should just return zero, my fault.
Quoted 3 lines
Which is because
bdrv_reopen_queue()already queuesbs->filefor reopen. The simple return 0; implementation is shared by all other format drivers that support reopening images.
--- block/Makefile.objs | 2 +- block/raw_bsd.c | 78 ++++++++++++++++++++++++++++---------------------- 2 files changed, 45 insertions(+), 35 deletions(-)
block/Makefile.objs +1 -1
@@ -1,4 +1,4 @@ -block-obj-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o +block-obj-y += raw_bsd.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o block-obj-y += qed-check.o
block/raw_bsd.c +44 -34
155 lines not on this page · raw message
Signature · 1 line
1.7.1
Quoted 53 lines · Laszlo Ersek
v1->v2:
- added comment toraw_probe()[Kevin]
- fixedraw_reopen_prepare()[Paolo & Kevin]
- keeping BSDL: according to the v1 discussion, LGPLv2+ could work too, but "but most of the block layer is BSD" [Paolo] and I haven't been clearly instructed to switch to LGPLv2+.diff --git
a/block/raw_bsd.cb/block/raw_bsd.c
index 07e6f06..ab2b0fd 100644
---a/block/raw_bsd.c
+++b/block/raw_bsd.c
@@ -41,10 +41,7 @@ static QEMUOptionParameter raw_create_options[] = {
static int raw_reopen_prepare(BDRVReopenState*reopen_state,
BlockReopenQueue *queue, Error **errp)
{
- BDRVReopenState tmp =*reopen_state;
-- tmp.bs = tmp.bs->file; - return bdrv_reopen_prepare(&tmp, queue, errp); + return 0;}
static int
coroutine_fnraw_co_readv(BlockDriverState *bs,int64_tsector_num,
@@ -150,6 +147,9 @@ static voidraw_close(BlockDriverState *bs)static int
raw_probe(const uint8_t *buf, int buf_size, const char *filename){
+ /* smallest possible positive score so that raw is used if and only if no
+ * other block driver works
+ */return 1;}
v1 blurb:
Paolo asked me to write such a driver based on his textual specification alone. The first patch captures his email in full, the rest re-quotes parts that are being implemented.
The tree compiles at each patch. The series passes "make check-block".
"
block/raw.c" is not removed because I wanted to keep it out of my series and out of my brain.Laszlo Ersek (7):
add skeleton for BSD licensed "raw" BlockDriver
raw_bsd: emit debug events inbdrv_co_readv()andbdrv_co_writev()
raw_bsd: addraw_create()
raw_bsd: introduce "special members"
raw_bsd: addraw_create_options
raw_bsd: registerbdrv_raw
switch raw block driver from "raw.o" to "raw_bsd.o"
Quoted 4 lines
block/Makefile.objs | 2 +- block/raw_bsd.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+), 1 deletions(-) create mode 100644 block/raw_bsd.c
Ping.
Laszlo
Kevin Wolf
AppliedAm 21.08.2013 um 12:41 hat Laszlo Ersek geschrieben:
Quoted 53 lines
v1->v2:
- added comment toraw_probe()[Kevin]
- fixedraw_reopen_prepare()[Paolo & Kevin]
- keeping BSDL: according to the v1 discussion, LGPLv2+ could work too, but "but most of the block layer is BSD" [Paolo] and I haven't been clearly instructed to switch to LGPLv2+.diff --git
a/block/raw_bsd.cb/block/raw_bsd.c
index 07e6f06..ab2b0fd 100644
---a/block/raw_bsd.c
+++b/block/raw_bsd.c
@@ -41,10 +41,7 @@ static QEMUOptionParameter raw_create_options[] = {
static int raw_reopen_prepare(BDRVReopenState*reopen_state,
BlockReopenQueue *queue, Error **errp)
{
- BDRVReopenState tmp =*reopen_state;
-- tmp.bs = tmp.bs->file; - return bdrv_reopen_prepare(&tmp, queue, errp); + return 0;}
static int
coroutine_fnraw_co_readv(BlockDriverState *bs,int64_tsector_num,
@@ -150,6 +147,9 @@ static voidraw_close(BlockDriverState *bs)static int
raw_probe(const uint8_t *buf, int buf_size, const char *filename){
+ /* smallest possible positive score so that raw is used if and only if no
+ * other block driver works
+ */return 1;}
v1 blurb:
Paolo asked me to write such a driver based on his textual specification alone. The first patch captures his email in full, the rest re-quotes parts that are being implemented.
The tree compiles at each patch. The series passes "make check-block".
"
block/raw.c" is not removed because I wanted to keep it out of my series and out of my brain.Laszlo Ersek (7):
add skeleton for BSD licensed "raw" BlockDriver
raw_bsd: emit debug events inbdrv_co_readv()andbdrv_co_writev()
raw_bsd: addraw_create()
raw_bsd: introduce "special members"
raw_bsd: addraw_create_options
raw_bsd: registerbdrv_raw
switch raw block driver from "raw.o" to "raw_bsd.o"
Quoted 4 lines
block/Makefile.objs | 2 +- block/raw_bsd.c | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+), 1 deletions(-) create mode 100644 block/raw_bsd.c
Thanks, applied to the block branch.
Kevin