Add 'offset' and 'size' options
v8 cover Add 'offset' and 'size' options
v7 -> v8:
- handling offset in raw_co_pwrite_zeroes and raw_co_pdiscard (+tests)
- offset and size are not allowed for SCSI generic devices
- raw_aio_ioctl: not allowed when offset or size is set
- raw_probe_blocksizes: return -ENOTSUP if offset is not multiple of block size
- raw_probe_geometry: return -ENOTSUP if offset or size is set
v6 -> v7:
- added tests
v5 -> v6:
- fix alignment check condition
- when size is not specified and device size is being used take offset into account
v4 -> v5:
- added two missing overflow checks
- comments from Eric Blake:
- renamed 'fail' label to 'end'
- fixed optional fields in JSON scheme
- no punctuation at the end of error_setg() message
- spaces around PRI* macros
- using QEMU_IS_ALIGNED
- typos
v3 -> v4:
- fix stupid compilation error and formatting issue
v2 -> v3:
- changed overflow check to make it clearer
- produce error instead of warning when size is not multiple of sector size
v1 -> v2:
- options were moved from 'file' driver into 'raw' driver as suggested
- added support for writing, reopen and truncate when possible
Tomáš Golembiovský (2):
raw_bsd: add offset and size options
qemu-iotests: test 'offset' and 'size' options in raw driver
block/raw_bsd.c | 217 ++++++++++++++++++++++++++++++- qapi/block-core.json | 16 ++- tests/qemu-iotests/171 | 212 ++++++++++++++++++++++++++++++ tests/qemu-iotests/171.out | 313 +++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 5 files changed, 755 insertions(+), 4 deletions(-) create mode 100755 tests/qemu-iotests/171 create mode 100644 tests/qemu-iotests/171.out
Signature · 1 line
2.10.1
PATCH v8 1/2 raw_bsd: add offset and size options
Added two new options 'offset' and 'size'. This makes it possible to use only part of the file as a device. This can be used e.g. to limit the access only to single partition in a disk image or use a disk inside a tar archive (like OVA).
When 'size' is specified we do our best to honour it.
--- block/raw_bsd.c | 217 ++++++++++++++++++++++++++++++++++++++++++++++++++- qapi/block-core.json | 16 +++- 2 files changed, 229 insertions(+), 4 deletions(-)
block/raw_bsd.c +214 -3
@@ -31,6 +31,30 @@#include "qapi/error.h" #include "qemu/option.h" +typedef struct BDRVRawState { +uint64_t offset; +uint64_t size; +bool has_size; +}BDRVRawState ; + +static QemuOptsList raw_runtime_opts = { + .name ="raw" , + .head =QTAILQ_HEAD_INITIALIZER (raw_runtime_opts.head), + .desc = { + { + .name ="offset" , + .type =QEMU_OPT_SIZE , + .help ="offset in the disk where the image starts" , + }, + { + .name ="size" , + .type =QEMU_OPT_SIZE , + .help ="virtual disk size" , + }, + {/* end of list */ } + }, +}; +static QemuOptsList raw_create_opts = { .name ="raw-create-opts" , .head =QTAILQ_HEAD_INITIALIZER (raw_create_opts.head),
@@ -44,16 +68,108 @@ static QemuOptsList raw_create_opts = { } }; +static int raw_read_options (QDict *options,BlockDriverState *bs, +BDRVRawState *s,Error **errp) +{ +Error *local_err =NULL ; +QemuOpts *opts =NULL ; +int64_t real_size =0 ; +int ret; + + real_size =bdrv_getlength (bs->file->bs); +if (real_size <0 ) { +error_setg_errno (errp, -real_size,"Could not get image size" ); +return real_size; + } + + opts =qemu_opts_create (&raw_runtime_opts,NULL ,0 , &error_abort); +qemu_opts_absorb_qdict (opts, options, &local_err); +if (local_err) { +error_propagate (errp, local_err); + ret = -EINVAL ; + goto end; + } + + s->offset =qemu_opt_get_size (opts,"offset" ,0 ); +if (qemu_opt_find (opts,"size" ) !=NULL ) { + s->size =qemu_opt_get_size (opts,"size" ,0 ); + s->has_size = true; + }else { + s->has_size = false; + s->size = real_size - s->offset; + } + +/* Check size and offset */ +if (real_size < s->offset || (real_size - s->offset) < s->size) { +error_setg (errp,"The sum of offset (%" PRIu64") and size " +"(%" PRIu64") has to be smaller or equal to the " +" actual size of the containing file (%" PRId64")" , + s->offset, s->size, real_size); + ret = -EINVAL ; + goto end; + } + +/* Make sure size is multiple of BDRV_SECTOR_SIZE to prevent rounding +* up and leaking out of the specified area. */ +if (!QEMU_IS_ALIGNED (s->size,BDRV_SECTOR_SIZE )) { +error_setg (errp,"Specified size is not multiple of %llu" , +BDRV_SECTOR_SIZE ); + ret = -EINVAL ; + goto end; + } + + ret =0 ; + +end: + +qemu_opts_del (opts); + +return ret; +} +static int raw_reopen_prepare (BDRVReopenState *reopen_state,BlockReopenQueue *queue,Error **errp) { -return 0 ; +assert (reopen_state !=NULL ); +assert (reopen_state->bs !=NULL ); + + reopen_state->opaque =g_new0 (BDRVRawState,1 ); + +return raw_read_options ( + reopen_state->options, + reopen_state->bs, + reopen_state->opaque, + errp); +} + +static void raw_reopen_commit (BDRVReopenState *state) +{ +BDRVRawState *new_s = state->opaque; +BDRVRawState *s = state->bs->opaque; + +memcpy (s, new_s,sizeof (BDRVRawState)); + +g_free (state->opaque); + state->opaque =NULL ; +} + +static void raw_reopen_abort (BDRVReopenState *state) +{ +g_free (state->opaque); + state->opaque =NULL ; } static int coroutine_fn raw_co_preadv(BlockDriverState *bs, uint64_t offset,uint64_t bytes,QEMUIOVector *qiov,int flags) { +BDRVRawState *s = bs->opaque; + +if (offset >UINT64_MAX - s->offset) { +return -EINVAL ; + } + offset += s->offset; +BLKDBG_EVENT (bs->file,BLKDBG_READ_AIO );return bdrv_co_preadv (bs->file, offset, bytes, qiov, flags); }
@@ -62,11 +178,23 @@ static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) { + BDRVRawState *s = bs->opaque;void *buf =NULL ;BlockDriver *drv;QEMUIOVector local_qiov;int ret; +if (s->has_size && (offset > s->size || bytes > (s->size - offset))) { +/* There's not enough space for the data. Don't write anything and just +* fail to prevent leaking out of the size specified in options. */ +return -ENOSPC ; + } + +if (offset >UINT64_MAX - s->offset) { + ret = -EINVAL ; + goto fail; + } +if (bs->probed && offset <BLOCK_PROBE_BUF_SIZE && bytes) {/* Handling partial writes would be a pain - so we just * require that guests have 512-byte request alignment if
@@ -101,6 +229,8 @@ static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset, qiov = &local_qiov; } + offset += s->offset; +BLKDBG_EVENT (bs->file,BLKDBG_WRITE_AIO ); ret =bdrv_co_pwritev (bs->file, offset, bytes, qiov, flags);
@@ -117,8 +247,10 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs, int nb_sectors, int *pnum, BlockDriverState **file) { + BDRVRawState *s = bs->opaque; *pnum = nb_sectors; *file = bs->file->bs; + sector_num += s->offset /BDRV_SECTOR_SIZE ;return BDRV_BLOCK_RAW |BDRV_BLOCK_OFFSET_VALID |BDRV_BLOCK_DATA | (sector_num <<BDRV_SECTOR_BITS ); }
@@ -127,18 +259,49 @@ static int coroutine_fn raw_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int count, BdrvRequestFlags flags) { + BDRVRawState *s = bs->opaque; +if (offset >UINT64_MAX - s->offset) { +return -EINVAL ; + } + offset += s->offset;return bdrv_co_pwrite_zeroes (bs->file, offset, count, flags); } static int coroutine_fn raw_co_pdiscard(BlockDriverState *bs,int64_t offset,int count) { +BDRVRawState *s = bs->opaque; +if (offset >UINT64_MAX - s->offset) { +return -EINVAL ; + } + offset += s->offset;return bdrv_co_pdiscard (bs->file->bs, offset, count); }static int64_t raw_getlength (BlockDriverState *bs) { -return bdrv_getlength (bs->file->bs); +int64_t len; +BDRVRawState *s = bs->opaque; + +/* Update size. It should not change unless the file was externally +* modified. */ + len =bdrv_getlength (bs->file->bs); +if (len <0 ) { +return len; + } + +if (len < s->offset) { + s->size =0 ; + }else { +if (s->has_size) { +/* Try to honour the size */ + s->size =MIN (s->size, len - s->offset); + }else { + s->size = len - s->offset; + } + } + +return s->size; }static int raw_get_info (BlockDriverState *bs,BlockDriverInfo *bdi)
@@ -158,6 +321,18 @@ static void raw_refresh_limits(BlockDriverState *bs, Error **errp)static int raw_truncate (BlockDriverState *bs,int64_t offset) { +BDRVRawState *s = bs->opaque; + +if (s->has_size) { +return -ENOTSUP ; + } + +if (INT64_MAX - offset < s->offset) { +return -EINVAL ; + } + + s->size = offset; + offset += s->offset;return bdrv_truncate (bs->file->bs, offset); }
@@ -181,6 +356,10 @@ static BlockAIOCB *raw_aio_ioctl(BlockDriverState *bs, BlockCompletionFunc *cb, void *opaque) { +BDRVRawState *s = bs->opaque; +if (s->offset || s->has_size) { +return NULL ; + }return bdrv_aio_ioctl (bs->file->bs, req, buf, cb, opaque); }
@@ -197,6 +376,9 @@ static int raw_create(const char *filename, QemuOpts *opts, Error **errp)static int raw_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) { +BDRVRawState *s = bs->opaque; +int ret; + bs->sg = bs->file->bs->sg; bs->supported_write_flags =BDRV_REQ_FUA & bs->file->bs->supported_write_flags;
@@ -214,6 +396,16 @@ static int raw_open(BlockDriverState *bs, QDict *options, int flags, bs->file->bs->filename); } + ret =raw_read_options (options, bs, s, errp); +if (ret <0 ) { +return ret; + } + +if (bs->sg && (s->offset || s->has_size)) { +error_setg (errp,"Cannot use offset/size with SCSI generic devices" ); +return -EINVAL ; + } +return 0 ; }
@@ -231,18 +423,37 @@ static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)static int raw_probe_blocksizes (BlockDriverState *bs,BlockSizes *bsz) { -return bdrv_probe_blocksizes (bs->file->bs, bsz); +BDRVRawState *s = bs->opaque; +int ret; + + ret =bdrv_probe_blocksizes (bs->file->bs, bsz); +if (ret <0 ) { +return ret; + } + +if (!QEMU_IS_ALIGNED (s->offset,MAX (bsz->log, bsz->phys))) { +return -ENOTSUP ; + } + +return 0 ; }static int raw_probe_geometry (BlockDriverState *bs,HDGeometry *geo) { +BDRVRawState *s = bs->opaque; +if (s->offset || s->has_size) { +return -ENOTSUP ; + }return bdrv_probe_geometry (bs->file->bs, geo); }BlockDriver bdrv_raw = { .format_name ="raw" , + .instance_size =sizeof (BDRVRawState), .bdrv_probe = &raw_probe, .bdrv_reopen_prepare = &raw_reopen_prepare, + .bdrv_reopen_commit = &raw_reopen_commit, + .bdrv_reopen_abort = &raw_reopen_abort, .bdrv_open = &raw_open, .bdrv_close = &raw_close, .bdrv_create = &raw_create,
qapi/block-core.json +15 -1
@@ -2224,6 +2224,20 @@ 'data': { 'filename': 'str' } } ## +# @BlockdevOptionsRaw +# +# Driver specific block device options for the raw driver. +# +# @offset: #optional position where the block device starts +# @size: #optional the assumed size of the device +# +# Since: 2.8 +## +{ 'struct': 'BlockdevOptionsRaw', + 'base': 'BlockdevOptionsGenericFormat', + 'data': { '*offset': 'int', '*size': 'int' } } + +## # @BlockdevOptions # # Options for creating a block device. Many options are available for all
@@ -2277,7 +2291,7 @@ 'qcow': 'BlockdevOptionsGenericCOWFormat', 'qed': 'BlockdevOptionsGenericCOWFormat', 'quorum': 'BlockdevOptionsQuorum', - 'raw': 'BlockdevOptionsGenericFormat', + 'raw': 'BlockdevOptionsRaw', # TODO rbd: Wait for structured options 'replication':'BlockdevOptionsReplication', # TODO sheepdog: Wait for structured options
Signature · 1 line
2.10.1
Re: [Qemu-devel] [PATCH v8 1/2] raw_bsd: add offset and size options
Am 31.10.2016 um 11:27 hat Tomáš Golembiovský geschrieben:
Quoted 10 lines
Added two new options 'offset' and 'size'. This makes it possible to use only part of the file as a device. This can be used e.g. to limit the access only to single partition in a disk image or use a disk inside a tar archive (like OVA).
When 'size' is specified we do our best to honour it.
Signed-off-by Tomáš Golembiovský <[email protected]>
block/raw_bsd.c static BlockAIOCB *raw_aio_ioctl(BlockDriverState *bs, · line 365
Quoted 10 lines
block/raw_bsd.c +4
-0@@ -181,6 +356,10 @@ static BlockAIOCB *raw_aio_ioctl(BlockDriverState *bs, BlockCompletionFunc *cb, void *opaque) { +BDRVRawState *s = bs->opaque; +if (s->offset || s->has_size) { +return NULL ; + }return bdrv_aio_ioctl (bs->file->bs, req, buf, cb, opaque); }
This hunk had a real merge conflict (aio -> co), so I'm posting my conflict resolution below.
Kevin
@@ -178,6 +353,10 @@ static void raw_lock_medium(BlockDriverState *bs, bool locked)
static int raw_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) {
+ BDRVRawState *s = bs->opaque;
+ if (s->offset || s->has_size) {
+ return -ENOTSUP;
+ }
return bdrv_co_ioctl(bs->file->bs, req, buf);
}
PATCH v8 2/2 qemu-iotests: test 'offset' and 'size' options in raw driver
In reply to Tomáš Golembiovský--- tests/qemu-iotests/171 | 212 ++++++++++++++++++++++++++++++ tests/qemu-iotests/171.out | 313 +++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/group | 1 + 3 files changed, 526 insertions(+) create mode 100755 tests/qemu-iotests/171 create mode 100644 tests/qemu-iotests/171.out
tests/qemu-iotests/171 new file +212 -0
@@ -0,0 +1,212 @@ +#!/bin/bash +# +# Test 'offset' and 'size' options of the raw driver. Make sure we can't +# (or can) read and write outside of the image size. +# +# Copyright (C) 2016 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# + +# creator [email protected] + +seq=`basename $0` +echo "QA output created by $seq" + +here=`pwd` +status=1 # failure is the default! + +_cleanup() +{ + _cleanup_test_img +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter + +_supported_fmt raw +_supported_proto file +_supported_os Linux + + +# Create JSON with options +img_json() { + echo -n 'json:{"driver":"raw", ' + echo -n "\"offset\":\"$img_offset\", " + if [ "$img_size" -ne -1 ] ; then + echo -n "\"size\":\"$img_size\", " + fi + echo -n '"file": {' + echo -n '"driver":"file", ' + echo -n "\"filename\":\"$TEST_IMG\" " + echo -n "} }" +} + +do_general_test() { + if [ "$img_size" -ge 0 ] ; then + test_size=$img_size + else + test_size=$((size-img_offset)) + fi + + echo + echo "write to image" + $QEMU_IO -c "write -P 0x0a 0 $test_size" "$(img_json)" | _filter_qemu_io + + echo + echo "read the image" + $QEMU_IO -c "read -P 0x0a 0 $test_size" "$(img_json)" | _filter_qemu_io + + echo + echo "check that offset is respected" + $QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io + + echo + echo "write before image boundary" + $QEMU_IO -c "write $((test_size-1)) 1" "$(img_json)" | _filter_qemu_io + + echo + echo "write across image boundary" + $QEMU_IO -c "write $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io + + echo + echo "write at image boundary" + $QEMU_IO -c "write $test_size 1" "$(img_json)" | _filter_qemu_io + + echo + echo "write after image boundary" + $QEMU_IO -c "write $((test_size+512)) 1" "$(img_json)" | _filter_qemu_io + + echo + echo "writev before/after image boundary" + $QEMU_IO -c "writev $((test_size-512)) 512 512" "$(img_json)" | _filter_qemu_io + + echo + echo "read before image boundary" + $QEMU_IO -c "read $((test_size-1)) 1" "$(img_json)" | _filter_qemu_io + + echo + echo "read across image boundary" + $QEMU_IO -c "read $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io + + echo + echo "read at image boundary" + $QEMU_IO -c "read $test_size 1" "$(img_json)" | _filter_qemu_io + + echo + echo "read after image boundary" + $QEMU_IO -c "read $((test_size+512)) 1" "$(img_json)" | _filter_qemu_io + + echo + echo "readv before/after image boundary" + $QEMU_IO -c "readv $((test_size-512)) 512 512" "$(img_json)" | _filter_qemu_io + + echo + echo "fill image with pattern" + $QEMU_IO -c "write -P 0x0a 0 $size" $TEST_IMG | _filter_qemu_io + + echo + echo "write zeroes and check" + $QEMU_IO -c "write -z 0 512" "$(img_json)" | _filter_qemu_io + $QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io + + echo + echo "write zeroes across image boundary" + $QEMU_IO -c "write -z $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io + + echo + echo "write zeroes at image boundary and check" + $QEMU_IO -c "write -z $((test_size-2)) 2" "$(img_json)" | _filter_qemu_io + $QEMU_IO -c "read -v $((img_offset+test_size-2)) 2" $TEST_IMG | _filter_qemu_io + $QEMU_IO -c "read -v $((img_offset+test_size)) 2" $TEST_IMG | _filter_qemu_io + + echo + echo "fill image with pattern" + $QEMU_IO -c "write -P 0x0a 0 $size" $TEST_IMG | _filter_qemu_io + + echo + echo "discard and check" + $QEMU_IO -c "discard 0 512" "$(img_json)" | _filter_qemu_io + $QEMU_IO -c "read -v $((img_offset-2)) 4" $TEST_IMG | _filter_qemu_io + + echo + echo "discard across image boundary" + $QEMU_IO -c "discard $((test_size-1)) 2" "$(img_json)" | _filter_qemu_io + + echo + echo "discard at image boundary and check" + $QEMU_IO -c "discard $((test_size-2)) 2" "$(img_json)" | _filter_qemu_io + $QEMU_IO -c "read -v $((img_offset+test_size-2)) 2" $TEST_IMG | _filter_qemu_io + $QEMU_IO -c "read -v $((img_offset+test_size)) 2" $TEST_IMG | _filter_qemu_io +} + +echo +echo "== test 'offset' option ==" +size=4096 +img_offset=512 +img_size=-1 +_make_test_img $size +do_general_test +_cleanup_test_img + +echo +echo "== test 'offset' and 'size' options ==" +size=4096 +img_offset=512 +img_size=2048 +_make_test_img $size +do_general_test +_cleanup_test_img + +echo +echo "== test misaligned 'offset' ==" +size=4096 +img_offset=10 +img_size=2048 +_make_test_img $size +do_general_test +_cleanup_test_img + +echo +echo "== test reopen ==" +size=4096 +img_offset=512 +img_size=512 +_make_test_img $size +( +$QEMU_IO "$(img_json)" <<EOT +write -P 0x0a 0 512 +write -P 0x0a 511 1 +write -P 0x0a 512 1 +reopen -o driver=raw,offset=1536,size=1024 +write -P 0x0a 0 1024 +write -P 0x0a 1023 1 +write -P 0x0a 1024 1 +EOT +) | _filter_qemu_io +echo "checking boundaries" +$QEMU_IO -c "read -v 510 4" $TEST_IMG | _filter_qemu_io +$QEMU_IO -c "read -v 1022 4" $TEST_IMG | _filter_qemu_io +$QEMU_IO -c "read -v 1534 4" $TEST_IMG | _filter_qemu_io +$QEMU_IO -c "read -v 2558 4" $TEST_IMG | _filter_qemu_io +_cleanup_test_img + +# success, all done +echo +echo "*** done" +rm -f $seq.full +status=0
tests/qemu-iotests/171.out new file +313 -0
313 lines not on this page · raw message
tests/qemu-iotests/group +1 -0
@@ -162,3 +162,4 @@ 160 rw auto quick 162 auto quick 170 rw auto quick +171 rw auto quick
Signature · 1 line
2.10.1
Am 31.10.2016 um 11:27 hat Tomáš Golembiovský geschrieben:
Quoted 6 lines
v7 -> v8:
- handling offset inraw_co_pwrite_zeroesandraw_co_pdiscard(+tests)
- offset and size are not allowed for SCSI generic devices
-raw_aio_ioctl: not allowed when offset or size is set
-raw_probe_blocksizes: return-ENOTSUPif offset is not multiple of block size
-raw_probe_geometry: return-ENOTSUPif offset or size is set
Thanks, rebased and applied to the block branch.
Kevin