block: Strip protocol prefixes from filenames
cover block: Strip protocol prefixes from filenames
As some kind of follow-up to the "block: Strip 'file:' prefix from filenames" series, this series does the same thing for other protocol drivers.
All protocol drivers which implement bdrv_probe() may rely on them being selected based on that function returning success alone. However, they may have been chosen through a protocol prefix as well. Thus, if they currently do not implement bdrv_parse_filename(), they may be unaware of that possible prefix and therefore fail to interpret such filenames (and, in fact, all of those are unaware).
This series makes these drivers strip their respective prefix through bdrv_parse_filename() and in bdrv_create(), if implemented.
The following protocol drivers are not touched by this series since they already implement bdrv_parse_filename() and are thus very likely aware of the prefix:
- vvfat, nbd, blkdebug, blkverify, ssh, curl
The following protocol drivers are not touched by this series since they do not implement bdrv_probe() and therefore always receive a prefixed filename (unless they are selected through QMP options) which makes them pretty much guaranteed to handle these prefixes correctly:
- nfs, sheepdog, rbd, quorum, gluster, iscsi
Thus, only drivers implementing bdrv_probe() and not implementing bdrv_parse_filename() have been touched.
Please note that this series does not strip the prefix in bdrv_probe(). This is due to the driver being selected anyway later on through the protocol prefix, even though bdrv_probe() returned 0. More importantly, according to a comment in bdrv_find_protocol() in block.c about why bdrv_probe() occurs before the protocol prefix is interpreted, it seems actually more desirable not to strip the prefix in bdrv_probe() (since it may in fact not be a prefix but rather some obscure device naming schema).
Max Reitz (5):
block/raw-posix: bdrv_parse_filename() for hdev block/raw-posix: bdrv_parse_filename() for floppy block/raw-posix: bdrv_parse_filename() for cdrom block/raw-posix: Strip protocol prefix on creation block/raw-win32: bdrv_parse_filename() for hdev
block/raw-posix.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ block/raw-win32.c | 10 ++++++++++ 2 files changed, 62 insertions(+)
Signature · 1 line
1.9.0
PATCH 1/5 block/raw-posix: bdrv_parse_filename() for hdev
The "host_device" protocol driver should strip the "host_device:" prefix from filenames if present.
--- block/raw-posix.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
block/raw-posix.c +10 -0
@@ -1561,6 +1561,15 @@ static int check_hdev_writable(BDRVRawState *s)return 0 ; } +static void hdev_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_device:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +static int hdev_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {
@@ -1805,6 +1814,7 @@ static BlockDriver bdrv_host_device = { .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, .bdrv_probe_device = hdev_probe_device, + .bdrv_parse_filename = hdev_parse_filename, .bdrv_file_open = hdev_open, .bdrv_close = raw_close, .bdrv_reopen_prepare = raw_reopen_prepare,
Signature · 1 line
1.9.0
Re: [Qemu-devel] [PATCH 1/5] block/raw-posix: bdrv_parse_filename() for hdev
Quoted 39 lines · Max Reitz
The "
host_device" protocol driver should strip the "host_device:" prefix from filenames if present.Signed-off-by Max Reitz <[email protected]>--- block/raw-posix.c | 10 ++++++++++ 1 file changed, 10 insertions(+)block/raw-posix.c +10
-0@@ -1561,6 +1561,15 @@ static int check_hdev_writable(BDRVRawState *s)return 0 ; } +static void hdev_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_device:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +static int hdev_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {@@ -1805,6 +1814,7 @@ static BlockDriver bdrv_host_device = { .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, .bdrv_probe_device = hdev_probe_device, + .bdrv_parse_filename = hdev_parse_filename, .bdrv_file_open = hdev_open, .bdrv_close = raw_close, .bdrv_reopen_prepare = raw_reopen_prepare,Signature · 1 line
1.9.0
PATCH 2/5 block/raw-posix: bdrv_parse_filename() for floppy
In reply to Max ReitzThe "host_floppy" protocol driver should strip the "host_floppy:" prefix from filenames if present.
--- block/raw-posix.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
block/raw-posix.c +10 -0
@@ -1844,6 +1844,15 @@ static BlockDriver bdrv_host_device = { };#ifdef __linux__ +static void floppy_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_floppy:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +static int floppy_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {
@@ -1949,6 +1958,7 @@ static BlockDriver bdrv_host_floppy = { .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, .bdrv_probe_device = floppy_probe_device, + .bdrv_parse_filename = floppy_parse_filename, .bdrv_file_open = floppy_open, .bdrv_close = raw_close, .bdrv_reopen_prepare = raw_reopen_prepare,
Signature · 1 line
1.9.0
Re: [Qemu-devel] [PATCH 2/5] block/raw-posix: bdrv_parse_filename() for floppy
Quoted 39 lines · Max Reitz
The "
host_floppy" protocol driver should strip the "host_floppy:" prefix from filenames if present.Signed-off-by Max Reitz <[email protected]>--- block/raw-posix.c | 10 ++++++++++ 1 file changed, 10 insertions(+)block/raw-posix.c +10
-0@@ -1844,6 +1844,15 @@ static BlockDriver bdrv_host_device = { };#ifdef __linux__ +static void floppy_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_floppy:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +static int floppy_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {@@ -1949,6 +1958,7 @@ static BlockDriver bdrv_host_floppy = { .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, .bdrv_probe_device = floppy_probe_device, + .bdrv_parse_filename = floppy_parse_filename, .bdrv_file_open = floppy_open, .bdrv_close = raw_close, .bdrv_reopen_prepare = raw_reopen_prepare,Signature · 1 line
1.9.0
PATCH 3/5 block/raw-posix: bdrv_parse_filename() for cdrom
In reply to Max ReitzThe "host_cdrom" protocol drivers should strip the "host_cdrom:" prefix from filenames if present.
--- block/raw-posix.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
block/raw-posix.c +20 -0
@@ -1984,6 +1984,15 @@ static BlockDriver bdrv_host_floppy = { .bdrv_eject = floppy_eject, }; +static void cdrom_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_cdrom:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +static int cdrom_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {
@@ -2070,6 +2079,7 @@ static BlockDriver bdrv_host_cdrom = { .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, .bdrv_probe_device = cdrom_probe_device, + .bdrv_parse_filename = cdrom_parse_filename, .bdrv_file_open = cdrom_open, .bdrv_close = raw_close, .bdrv_reopen_prepare = raw_reopen_prepare,
@@ -2101,6 +2111,15 @@ static BlockDriver bdrv_host_cdrom = {#endif /* __linux__ */ #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) +static void cdrom_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_cdrom:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +static int cdrom_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {
@@ -2200,6 +2219,7 @@ static BlockDriver bdrv_host_cdrom = { .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, .bdrv_probe_device = cdrom_probe_device, + .bdrv_parse_filename = cdrom_parse_filename, .bdrv_file_open = cdrom_open, .bdrv_close = raw_close, .bdrv_reopen_prepare = raw_reopen_prepare,
Signature · 1 line
1.9.0
Re: [Qemu-devel] [PATCH 3/5] block/raw-posix: bdrv_parse_filename() for cdrom
Quoted 39 lines · Max Reitz
The "
host_cdrom" protocol drivers should strip the "host_cdrom:" prefix from filenames if present.Signed-off-by Max Reitz <[email protected]>--- block/raw-posix.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)block/raw-posix.c +10
-0@@ -1984,6 +1984,15 @@ static BlockDriver bdrv_host_floppy = { .bdrv_eject = floppy_eject, }; +static void cdrom_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_cdrom:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +static int cdrom_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {@@ -2070,6 +2079,7 @@ static BlockDriver bdrv_host_cdrom = { .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, .bdrv_probe_device = cdrom_probe_device, + .bdrv_parse_filename = cdrom_parse_filename, .bdrv_file_open = cdrom_open, .bdrv_close = raw_close, .bdrv_reopen_prepare = raw_reopen_prepare,@@ -2101,6 +2111,15 @@ static BlockDriver bdrv_host_cdrom = {#endif /* __linux__ */
block/raw-posix.c static BlockDriver bdrv_host_cdrom = { · line 2122
Quoted 10 lines
block/raw-posix.c +9
-0#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) +static void cdrom_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_cdrom:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +
The cdrom_probe_device and cdrom_open function actually differs and are not duplicated whereas these two instances of cdrom_parse_filename are pretty much the same.
Why not putting a merged version on the top of the file and guarded by
#if defined(__linux__) || defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
?
Quoted 14 lines
block/raw-posix.c +1
-0static int cdrom_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {@@ -2200,6 +2219,7 @@ static BlockDriver bdrv_host_cdrom = { .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, .bdrv_probe_device = cdrom_probe_device, + .bdrv_parse_filename = cdrom_parse_filename, .bdrv_file_open = cdrom_open, .bdrv_close = raw_close, .bdrv_reopen_prepare = raw_reopen_prepare,Signature · 1 line
1.9.0
Re: [Qemu-devel] [PATCH 3/5] block/raw-posix: bdrv_parse_filename() for cdrom
Quoted 40 lines · Benoît Canet
Quoted 39 lines · Max Reitz
The "
host_cdrom" protocol drivers should strip the "host_cdrom:" prefix from filenames if present.Signed-off-by Max Reitz <[email protected]>--- block/raw-posix.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)block/raw-posix.c +10
-0@@ -1984,6 +1984,15 @@ static BlockDriver bdrv_host_floppy = { .bdrv_eject = floppy_eject, }; +static void cdrom_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_cdrom:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +static int cdrom_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {@@ -2070,6 +2079,7 @@ static BlockDriver bdrv_host_cdrom = { .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, .bdrv_probe_device = cdrom_probe_device, + .bdrv_parse_filename = cdrom_parse_filename, .bdrv_file_open = cdrom_open, .bdrv_close = raw_close, .bdrv_reopen_prepare = raw_reopen_prepare,@@ -2101,6 +2111,15 @@ static BlockDriver bdrv_host_cdrom = {#endif /* __linux__ */
Quoted 16 lines
block/raw-posix.c static BlockDriver bdrv_host_cdrom = { · line 2122
Quoted 10 lines
block/raw-posix.c +9
-0#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) +static void cdrom_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_cdrom:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +The
cdrom_probe_deviceandcdrom_openfunction actually differs and are not duplicated whereas these two instances ofcdrom_parse_filenameare pretty much the same.
Why not putting a merged version on the top of the file and guarded by
#ifdefined(__linux__)|| defined (__FreeBSD__) ||defined(__FreeBSD_kernel__)
?
I didn't want to since the #ifdef linux began before the floppy driver and ended after the Linux CD-ROM driver, so I'd have to split it up. But since you're suggesting it now, I'll do it. ;-)
Max
Quoted 14 lines
Quoted 14 lines
block/raw-posix.c +1
-0static int cdrom_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {@@ -2200,6 +2219,7 @@ static BlockDriver bdrv_host_cdrom = { .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, .bdrv_probe_device = cdrom_probe_device, + .bdrv_parse_filename = cdrom_parse_filename, .bdrv_file_open = cdrom_open, .bdrv_close = raw_close, .bdrv_reopen_prepare = raw_reopen_prepare,Signature · 1 line
1.9.0
PATCH 4/5 block/raw-posix: Strip protocol prefix on creation
In reply to Max ReitzThe hdev_create() implementation in block/raw-posix.c is used by the "host_device", "host_cdrom" and "host_floppy" protocol block drivers together. Thus, it any of the associated prefixes may occur and exactly one should be stripped, if it does (thus, "host_device:host_cdrom:/dev/cdrom" is not shortened to "/dev/cdrom").
--- block/raw-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
block/raw-posix.c +12 -0
@@ -1776,6 +1776,18 @@ static int hdev_create(const char *filename, QEMUOptionParameter *options,int ret =0 ;struct stat stat_buf;int64_t total_size =0 ; +bool has_prefix; + +/* This function is used by all three protocol block drivers and therefore +* any of these three prefixes may be given. +* The return value has to be stored somewhere, otherwise this is an error +* due to -Werror=unused-value. */ + has_prefix = +strstart (filename,"host_device:" , &filename) || +strstart (filename,"host_cdrom:" , &filename) || +strstart (filename,"host_floppy:" , &filename); + + (void )has_prefix;/* Read out options */ while (options && options->name) {
Signature · 1 line
1.9.0
Re: [Qemu-devel] [PATCH 4/5] block/raw-posix: Strip protocol prefix on creation
Quoted 38 lines · Max Reitz
The
hdev_create()implementation inblock/raw-posix.cis used by the "host_device", "host_cdrom" and "host_floppy" protocol block drivers together. Thus, it any of the associated prefixes may occur and exactly one should be stripped, if it does (thus, "host_device:host_cdrom:/dev/cdrom" is not shortened to "/dev/cdrom").Signed-off-by Max Reitz <[email protected]>--- block/raw-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)block/raw-posix.c +12
-0@@ -1776,6 +1776,18 @@ static int hdev_create(const char *filename, QEMUOptionParameter *options,int ret =0 ;struct stat stat_buf;int64_t total_size =0 ; +bool has_prefix; + +/* This function is used by all three protocol block drivers and therefore +* any of these three prefixes may be given. +* The return value has to be stored somewhere, otherwise this is an error +* due to -Werror=unused-value. */ + has_prefix = +strstart (filename,"host_device:" , &filename) || +strstart (filename,"host_cdrom:" , &filename) || +strstart (filename,"host_floppy:" , &filename); + + (void )has_prefix;/* Read out options */ while (options && options->name) {Signature · 1 line
1.9.0
Re: [Qemu-devel] [PATCH 4/5] block/raw-posix: Strip protocol prefix on creation
In reply to Max ReitzQuoted 3 lines · Max Reitz
The
hdev_create()implementation inblock/raw-posix.cis used by the "host_device", "host_cdrom" and "host_floppy" protocol block drivers together. Thus, it any of the associated prefixes may occur and exactly
s/it //
Quoted 7 lines
one should be stripped, if it does (thus, "host_device:host_cdrom:/dev/cdrom" is not shortened to "
/dev/cdrom").Signed-off-by Max Reitz <[email protected]>--- block/raw-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH 4/5 block/raw-posix: Strip protocol prefix on creation
In reply to Max ReitzThe hdev_create() implementation in block/raw-posix.c is used by the "host_device", "host_cdrom" and "host_floppy" protocol block drivers together. Thus, it any of the associated prefixes may occur and exactly one should be stripped, if it does (thus, "host_device:host_cdrom:/dev/cdrom" is not shortened to "/dev/cdrom").
--- block/raw-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
block/raw-posix.c +12 -0
@@ -1776,6 +1776,18 @@ static int hdev_create(const char *filename, QEMUOptionParameter *options,int ret =0 ;struct stat stat_buf;int64_t total_size =0 ; +bool has_prefix; + +/* This function is used by all three protocol block drivers and therefore +* any of these three prefixes may be given. +* The return value has to be stored somewhere, otherwise this is an error +* due to -Werror=unused-value. */ + has_prefix = +strstart (filename,"host_device:" , &filename) || +strstart (filename,"host_cdrom:" , &filename) || +strstart (filename,"host_floppy:" , &filename); + + (void )has_prefix;/* Read out options */ while (options && options->name) {
Signature · 1 line
1.9.0
PATCH 5/5 block/raw-win32: bdrv_parse_filename() for hdev
The "host_device" protocol driver should strip the "host_device:" prefix from filenames if present.
--- block/raw-win32.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
block/raw-win32.c +10 -0
@@ -593,6 +593,15 @@ static int hdev_probe_device(const char *filename)return 0 ; } +static void hdev_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_device:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +static int hdev_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {
@@ -663,6 +672,7 @@ static BlockDriver bdrv_host_device = { .protocol_name = "host_device", .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, + .bdrv_parse_filename = hdev_parse_filename, .bdrv_probe_device = hdev_probe_device, .bdrv_file_open = hdev_open, .bdrv_close = raw_close,
Signature · 1 line
1.9.0
Re: [Qemu-devel] [PATCH 5/5] block/raw-win32: bdrv_parse_filename() for hdev
Quoted 40 lines · Max Reitz
The "
host_device" protocol driver should strip the "host_device:" prefix from filenames if present.Signed-off-by Max Reitz <[email protected]>--- block/raw-win32.c | 10 ++++++++++ 1 file changed, 10 insertions(+)block/raw-win32.c +10
-0@@ -593,6 +593,15 @@ static int hdev_probe_device(const char *filename)return 0 ; } +static void hdev_parse_filename (const char *filename,QDict *options, +Error **errp) +{ +/* The prefix is optional, just as for "file". */ +strstart (filename,"host_device:" , &filename); + +qdict_put_obj (options,"filename" ,QOBJECT (qstring_from_str (filename))); +} +static int hdev_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {@@ -663,6 +672,7 @@ static BlockDriver bdrv_host_device = { .protocol_name = "host_device", .instance_size = sizeof(BDRVRawState), .bdrv_needs_filename = true, + .bdrv_parse_filename = hdev_parse_filename, .bdrv_probe_device = hdev_probe_device, .bdrv_file_open = hdev_open, .bdrv_close = raw_close,Signature · 1 line
1.9.0
Re: [Qemu-devel] [PATCH 4/5] block/raw-posix: Strip protocol prefix on creation
In reply to Max ReitzSorry for the duplicate 4/5 and the wrong in-reply-to for 5/5, but I deduced from the send-email output that 4/5 failed to send (and 5/5 wasn't even attempted) and forgot --no-thread for the resend of 4 and 5.
Max
Quoted 34 lines · Max Reitz
The
hdev_create()implementation inblock/raw-posix.cis used by the "host_device", "host_cdrom" and "host_floppy" protocol block drivers together. Thus, it any of the associated prefixes may occur and exactly one should be stripped, if it does (thus, "host_device:host_cdrom:/dev/cdrom" is not shortened to "/dev/cdrom").Signed-off-by Max Reitz <[email protected]>--- block/raw-posix.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)block/raw-posix.c +12
-0@@ -1776,6 +1776,18 @@ static int hdev_create(const char *filename, QEMUOptionParameter *options,int ret =0 ;struct stat stat_buf;int64_t total_size =0 ; +bool has_prefix; + +/* This function is used by all three protocol block drivers and therefore +* any of these three prefixes may be given. +* The return value has to be stored somewhere, otherwise this is an error +* due to -Werror=unused-value. */ + has_prefix = +strstart (filename,"host_device:" , &filename) || +strstart (filename,"host_cdrom:" , &filename) || +strstart (filename,"host_floppy:" , &filename); + + (void )has_prefix;/* Read out options */ while (options && options->name) {