From nobody Fri May 3 01:35:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492531206658557.921080907961; Tue, 18 Apr 2017 09:00:06 -0700 (PDT) Received: from localhost ([::1]:42659 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0VXx-0001VV-1k for importer@patchew.org; Tue, 18 Apr 2017 12:00:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37921) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0VWF-0000Qf-SA for qemu-devel@nongnu.org; Tue, 18 Apr 2017 11:58:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d0VWB-0008Lx-Nn for qemu-devel@nongnu.org; Tue, 18 Apr 2017 11:58:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48040) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d0VW1-0008IL-Kd; Tue, 18 Apr 2017 11:58:06 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7DD33D1FD0; Tue, 18 Apr 2017 15:58:04 +0000 (UTC) Received: from localhost (ovpn-204-72.brq.redhat.com [10.40.204.72]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0F10778425; Tue, 18 Apr 2017 15:58:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 7DD33D1FD0 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mreitz@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 7DD33D1FD0 From: Max Reitz To: qemu-block@nongnu.org Date: Sat, 1 Apr 2017 17:57:48 +0200 Message-Id: <20170401155751.14322-2-mreitz@redhat.com> In-Reply-To: <20170401155751.14322-1-mreitz@redhat.com> References: <20170401155751.14322-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Tue, 18 Apr 2017 15:58:04 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC for-3.0 1/4] block: Add Rust interface X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , John Snow , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This patch adds an FFI interface for block drivers written in Rust, the language of the future. It's a very good interface, in fact, it's the best interface there is. We are truly making QEMU great again! Signed-off-by: Max Reitz --- configure | 2 + Makefile | 6 +- Makefile.target | 2 +- block/rust/Cargo.toml | 10 + block/rust/src/interface/c_constants.rs | 8 + block/rust/src/interface/c_functions.rs | 37 ++ block/rust/src/interface/c_structs.rs | 587 ++++++++++++++++++ block/rust/src/interface/mod.rs | 1012 +++++++++++++++++++++++++++= ++++ block/rust/src/lib.rs | 9 + 9 files changed, 1671 insertions(+), 2 deletions(-) create mode 100644 block/rust/Cargo.toml create mode 100644 block/rust/src/interface/c_constants.rs create mode 100644 block/rust/src/interface/c_functions.rs create mode 100644 block/rust/src/interface/c_structs.rs create mode 100644 block/rust/src/interface/mod.rs create mode 100644 block/rust/src/lib.rs diff --git a/configure b/configure index be4d326ae0..f99d19e47b 100755 --- a/configure +++ b/configure @@ -745,6 +745,8 @@ if test "$mingw32" =3D "yes" ; then libs_qga=3D"-lws2_32 -lwinmm -lpowrprof -liphlpapi -lnetapi32 $libs_qga" fi =20 +LIBS=3D"-ldl $LIBS" + werror=3D"" =20 for opt do diff --git a/Makefile b/Makefile index 6c359b2f86..5cf071fb30 100644 --- a/Makefile +++ b/Makefile @@ -368,9 +368,13 @@ Makefile: $(version-obj-y) libqemustub.a: $(stub-obj-y) libqemuutil.a: $(util-obj-y) =20 +.PHONY: rust_block_drivers_source +rust/debug/libqemu_rust_block_drivers.a: rust_block_drivers_source + cd "$(SRC_PATH)/block/rust" && CARGO_TARGET_DIR=3D"$(BUILD_DIR)/rust" car= go build + ###################################################################### =20 -COMMON_LDADDS =3D $(trace-obj-y) libqemuutil.a libqemustub.a +COMMON_LDADDS =3D $(trace-obj-y) libqemuutil.a libqemustub.a rust/debug/li= bqemu_rust_block_drivers.a =20 qemu-img.o: qemu-img-cmds.h =20 diff --git a/Makefile.target b/Makefile.target index 7df2b8c149..0dc4c0a35b 100644 --- a/Makefile.target +++ b/Makefile.target @@ -201,7 +201,7 @@ all-obj-$(CONFIG_SOFTMMU) +=3D $(io-obj-y) =20 $(QEMU_PROG_BUILD): config-devices.mak =20 -COMMON_LDADDS =3D $(trace-obj-y) ../libqemuutil.a ../libqemustub.a +COMMON_LDADDS =3D $(trace-obj-y) ../libqemuutil.a ../libqemustub.a ../rust= /debug/libqemu_rust_block_drivers.a =20 # build either PROG or PROGW $(QEMU_PROG_BUILD): $(all-obj-y) $(COMMON_LDADDS) diff --git a/block/rust/Cargo.toml b/block/rust/Cargo.toml new file mode 100644 index 0000000000..13cdf068b3 --- /dev/null +++ b/block/rust/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name =3D "qemu-rust-block-drivers" +version =3D "3.0.0" +authors =3D ["Max Reitz "] + +[dependencies] +libc =3D "0.2.0" + +[lib] +crate-type =3D ["staticlib"] diff --git a/block/rust/src/interface/c_constants.rs b/block/rust/src/inter= face/c_constants.rs new file mode 100644 index 0000000000..56919f1a04 --- /dev/null +++ b/block/rust/src/interface/c_constants.rs @@ -0,0 +1,8 @@ +use interface::c_structs::*; + + +extern { + pub static child_file: BdrvChildRole; + pub static child_format: BdrvChildRole; + pub static child_backing: BdrvChildRole; +} diff --git a/block/rust/src/interface/c_functions.rs b/block/rust/src/inter= face/c_functions.rs new file mode 100644 index 0000000000..1be8d85ccb --- /dev/null +++ b/block/rust/src/interface/c_functions.rs @@ -0,0 +1,37 @@ +use interface::c_structs::*; +use libc::{c_char,c_int,c_void,size_t}; + + +extern { + pub fn bdrv_is_read_only(bs: *mut BlockDriverState) -> bool; + + pub fn bdrv_open_child(filename: *const c_char, options: *mut QDict, + bdref_key: *const c_char, + parent: *mut BlockDriverState, + child_role: *const BdrvChildRole, + allow_none: bool, errp: *mut *mut Error) + -> *mut BdrvChild; + + pub fn bdrv_pread(child: *mut BdrvChild, offset: i64, buf: *mut c_void, + bytes: c_int) + -> c_int; + + pub fn bdrv_pwrite(child: *mut BdrvChild, offset: i64, buf: *const c_v= oid, + bytes: c_int) + -> c_int; + + pub fn bdrv_register(bdrv: *mut BlockDriver); + + pub fn error_get_pretty(err: *mut Error) -> *const c_char; + + pub fn error_setg_internal(errp: *mut *mut Error, src: *const c_char, + line: c_int, func: *const c_char, + fmt: *const c_char, ...); + + pub fn g_strdup(str: *const c_char) -> *mut c_char; + + pub fn qemu_blockalign(bs: *mut BlockDriverState, size: size_t) + -> *mut c_void; + + pub fn qemu_vfree(ptr: *mut c_void); +} diff --git a/block/rust/src/interface/c_structs.rs b/block/rust/src/interfa= ce/c_structs.rs new file mode 100644 index 0000000000..74e342b0c7 --- /dev/null +++ b/block/rust/src/interface/c_structs.rs @@ -0,0 +1,587 @@ +use libc::{c_char,c_int,c_uint,c_ulong,c_void,size_t}; + + +const BLOCK_OP_TYPE_MAX: usize =3D 16; + + +#[repr(C)] +pub struct BlockDriver { + pub format_name: *const c_char, + pub instance_size: c_int, + + pub is_filter: bool, + + pub bdrv_recurse_is_first_non_filter: Option< + extern fn(bs: *mut BlockDriverState, + candidate: *mut BlockDriverState) + -> bool + >, + + pub bdrv_probe: Option< + extern fn(buf: *const c_char, buf_size: c_int, + filename: *const c_char) + -> c_int + >, + pub bdrv_probe_device: Option c_= int>, + + pub bdrv_parse_filename: Option< + extern fn(filename: *const c_char, + options: *mut QDict, + errp: *mut *mut Error) + >, + pub bdrv_needs_filename: bool, + + pub supports_backing: bool, + + pub bdrv_reopen_prepare: Option< + extern fn(reopen_state: *mut BDRVReopenState, + queue: *mut BlockReopenQueue, + errp: *mut *mut Error) + -> c_int + >, + pub bdrv_reopen_commit: Option< + extern fn(reopen_state: *mut BDRVReopenState) + >, + pub bdrv_reopen_abort: Option< + extern fn(reopen_state: *mut BDRVReopenState) + >, + pub bdrv_join_options: Option< + extern fn(options: *mut QDict, old_options: *mut QDict) + >, + + pub bdrv_open: Option< + extern fn(bs: *mut BlockDriverState, options: *mut QDict, + flags: c_int, errp: *mut *mut Error) + -> c_int + >, + pub bdrv_file_open: Option< + extern fn(bs: *mut BlockDriverState, options: *mut QDict, + flags: c_int, errp: *mut *mut Error) + -> c_int + >, + pub bdrv_close: Option, + pub bdrv_create: Option< + extern fn(filename: *const c_char, opts: *mut QemuOpts, + errp: *mut *mut Error) + -> c_int + >, + pub bdrv_set_key: Option, + pub bdrv_make_empty: Option, + + pub bdrv_refresh_filename: Option< + extern fn(bs: *mut BlockDriverState, options: *mut QDict) + >, + + pub bdrv_aio_readv: Option, + pub bdrv_aio_writev: Option, + pub bdrv_aio_flush: Option, + pub bdrv_aio_pdiscard: Option, + + pub bdrv_co_readv: Option, + pub bdrv_co_preadv: Option< + extern fn(bs: *mut BlockDriverState, offset: u64, bytes: u64, + qiov: *mut QEMUIOVector, flags: c_int) + -> c_int + >, + pub bdrv_co_writev: Option, + pub bdrv_co_writev_flags: Option, + pub bdrv_co_pwritev: Option< + extern fn(bs: *mut BlockDriverState, offset: u64, bytes: u64, + qiov: *mut QEMUIOVector, flags: c_int) + -> c_int + >, + + pub bdrv_co_pwrite_zeroes: Option< + extern fn(bs: *mut BlockDriverState, offset: i64, count: c_int, + flags: c_int /* BdrvRequestFlags */) + -> c_int + >, + pub bdrv_co_pdiscard: Option< + extern fn(bs: *mut BlockDriverState, offset: i64, count: c_int) + -> c_int + >, + pub bdrv_co_get_block_status: Option< + extern fn(bs: *mut BlockDriverState, sector_num: i64, + nb_sectors: c_int, pnum: *mut c_int, + file: *mut *mut BlockDriverState) + -> i64 + >, + + pub bdrv_invalidate_cache: Option< + extern fn(bs: *mut BlockDriverState, errp: *mut *mut Error) + >, + pub bdrv_inactivate: Option c_= int>, + + pub bdrv_co_flush: Option c_in= t>, + + pub bdrv_co_flush_to_disk: Option< + extern fn(bs: *mut BlockDriverState) -> c_int + >, + + pub bdrv_co_flush_to_os: Option< + extern fn(bs: *mut BlockDriverState) -> c_int + >, + + pub protocol_name: *const c_char, + pub bdrv_truncate: Option< + extern fn(bs: *mut BlockDriverState, offset: i64, + errp: *mut *mut Error) + -> c_int + >, + + pub bdrv_getlength: Option c_i= nt>, + pub has_variable_length: bool, + pub bdrv_get_allocated_file_size: Option< + extern fn(bs: *mut BlockDriverState) -> i64 + >, + + pub bdrv_co_pwritev_compressed: Option< + extern fn(bs: *mut BlockDriverState, offset: u64, bytes: u64, + qiov: *mut QEMUIOVector) + -> c_int + >, + + pub bdrv_snapshot_create: Option< + extern fn(bs: *mut BlockDriverState, sn_info: *mut QEMUSnapsho= tInfo) + -> c_int + >, + pub bdrv_snapshot_goto: Option< + extern fn(bs: *mut BlockDriverState, snapshot_id: *const c_cha= r) + -> c_int + >, + pub bdrv_snapshot_delete: Option< + extern fn(bs: *mut BlockDriverState, snapshot_id: *const c_cha= r, + name: *const c_char, errp: *mut *mut Error) + -> c_int + >, + pub bdrv_snapshot_list: Option< + extern fn(bs: *mut BlockDriverState, + psn_info: *mut *mut QEMUSnapshotInfo) + -> c_int + >, + pub bdrv_snapshot_load_tmp: Option< + extern fn(bs: *mut BlockDriverState, snapshot_id: *const c_cha= r, + name: *const c_char, errp: *mut *mut Error) + -> c_int + >, + + pub bdrv_get_info: Option< + extern fn(bs: *mut BlockDriverState, bdi: *mut BlockDriverInfo) + -> c_int + >, + /* Note that the return object should be allocated in the C program */ + pub bdrv_get_specific_info: Option< + extern fn(bs: *mut BlockDriverState) -> *mut ImageInfoSpecific + >, + + pub bdrv_save_vmstate: Option< + extern fn(bs: *mut BlockDriverState, qiov: *mut QEMUIOVector, + pos: i64) + -> c_int + >, + pub bdrv_load_vmstate: Option< + extern fn(bs: *mut BlockDriverState, qiov: *mut QEMUIOVector, + pos: i64) + -> c_int + >, + + pub bdrv_change_backing_file: Option< + extern fn(bs: *mut BlockDriverState, + backing_file: *const c_char, backing_fmt: *const c_c= har) + -> c_int + >, + + pub bdrv_is_inserted: Option b= ool>, + pub bdrv_media_changed: Option< + extern fn(bs: *mut BlockDriverState) -> c_int + >, + pub bdrv_eject: Option< + extern fn(bs: *mut BlockDriverState, eject_flag: bool) + >, + pub bdrv_lock_medium: Option< + extern fn(bs: *mut BlockDriverState, locked: bool) + >, + + pub bdrv_aio_ioctl: Option, + pub bdrv_co_ioctl: Option< + extern fn(bs: *mut BlockDriverState, req: c_ulong, buf: *mut c= _void) + -> c_int + >, + + pub create_opts: *mut QemuOptsList, + + pub bdrv_check: Option< + extern fn(bs: *mut BlockDriverState, result: *mut BdrvCheckRes= ult, + fix: c_int /* BdrvCheckResult */) + -> c_int + >, + + pub bdrv_amend_options: Option< + extern fn(bs: *mut BlockDriverState, opts: *mut QemuOpts, + status_cb: BlockDriverAmendStatusCB, + cb_opaque: *mut c_void) + -> c_int + >, + + pub bdrv_debug_event: Option< + extern fn(bs: *mut BlockDriverState, + event: c_int /* BlkdebugEvent */) + >, + + pub bdrv_debug_breakpoint: Option< + extern fn(bs: *mut BlockDriverState, event: *const c_char, + tag: *const c_char) + -> c_int + >, + pub bdrv_debug_remove_breakpoint: Option< + extern fn(bs: *mut BlockDriverState, tag: *const c_char) -> c_= int + >, + pub bdrv_debug_resume: Option< + extern fn(bs: *mut BlockDriverState, tag: *const c_char) -> c_= int + >, + pub bdrv_debug_is_suspended: Option< + extern fn(bs: *mut BlockDriverState, tag: *const c_char) -> bo= ol + >, + + pub bdrv_refresh_limits: Option< + extern fn(bs: *mut BlockDriverState, errp: *mut *mut Error) + >, + + pub bdrv_has_zero_init: Option< + extern fn(bs: *mut BlockDriverState) -> c_int + >, + + pub bdrv_detach_aio_context: Option, + + pub bdrv_attach_aio_context: Option< + extern fn(bs: *mut BlockDriverState, new_context: *mut AioCont= ext) + >, + + pub bdrv_io_plug: Option, + pub bdrv_io_unplug: Option, + + pub bdrv_probe_blocksizes: Option< + extern fn(bs: *mut BlockDriverState, bsz: *mut BlockSizes) + -> c_int + >, + pub bdrv_probe_geometry: Option< + extern fn(bs: *mut BlockDriverState, geo: *mut HDGeometry) + -> c_int + >, + + pub bdrv_drain: Option, + + pub bdrv_add_child: Option< + extern fn(parent: *mut BlockDriverState, + child: *mut BlockDriverState, + errp: *mut *mut Error) + >, + pub bdrv_del_child: Option< + extern fn(parent: *mut BlockDriverState, + child: *mut BlockDriverState, + errp: *mut *mut Error) + >, + + pub bdrv_check_perm: Option< + extern fn(bs: *mut BlockDriverState, perm: u64, shared: u64, + errp: *mut *mut Error) + -> c_int + >, + pub bdrv_set_perm: Option< + extern fn(bs: *mut BlockDriverState, perm: u64, shared: u64) + >, + pub bdrv_abort_perm_update: Option, + + pub bdrv_child_perm: Option< + extern fn(bs: *mut BlockDriverState, c: *mut BdrvChild, + role: *const BdrvChildRole, + parent_perm: u64, parent_shared: u64, + nperm: *mut u64, nshared: *mut u64) + >, + + pub list: QListEntry, +} + +#[repr(C)] +pub struct BlockDriverState { + pub open_flags: c_int, + pub read_only: bool, + pub encrypted: bool, + pub valid_key: bool, + pub sg: bool, + pub probed: bool, + + pub drv: *mut BlockDriver, + pub opaque: *mut c_void, + + pub aio_context: *mut AioContext, + pub aio_notifiers: *mut BdrvAioNotifier, + pub walking_aio_notifiers: bool, + + pub filename: [c_char; 4096], + pub backing_file: [c_char; 4096], + + pub backing_format: [c_char; 16], + + pub full_open_options: *mut QDict, + pub exact_filename: [c_char; 4096], + + pub backing: *mut BdrvChild, + pub file: *mut BdrvChild, + + pub bl: BlockLimits, + + pub supported_write_flags: c_uint, + pub supported_zero_flags: c_uint, + + pub node_name: [c_char; 32], + pub node_list: QTailQEntry, + pub bs_list: QTailQEntry, + pub monitor_list: QTailQEntry, + pub refcnt: c_int, + + pub op_blockers: [QListHead; BLOCK_OP_TYPE_MAX], + + pub job: *mut BlockJob, + + pub inherits_from: *mut BlockDriverState, + pub children: QListHead, + pub parents: QListHead, + + pub options: *mut QDict, + pub explicit_options: *mut QDict, + pub detect_zeroes: c_int, /* BlockdevDetectZeroesOptions */ + + pub backing_blocker: *mut Error, + + pub copy_on_read: c_int, + + pub total_sectors: i64, + + pub before_write_notifiers: NotifierWithReturnList, + + pub in_flight: c_uint, + pub serialising_in_flight: c_uint, + + pub wakeup: bool, + + pub wr_highest_offset: u64, + + pub write_threshold_offset: u64, + pub write_threshold_notifier: NotifierWithReturn, + + pub io_plugged: c_uint, + + pub tracked_requests: QListHead, + pub flush_queue: CoQueue, + pub active_flush_req: bool, + pub write_gen: c_uint, + pub flushed_gen: c_uint, + + pub dirty_bitmaps: QListHead, + + pub enable_write_cache: c_int, + + pub quiesce_counter: c_int, +} + +#[repr(C)] +pub struct BDRVReopenState { + pub bs: *mut BlockDriverState, + pub flags: c_int, + pub options: *mut QDict, + pub explicit_options: *mut QDict, + pub opaque: *mut c_void, +} + +#[repr(C)] +pub struct BdrvCheckResult { + pub corruptions: c_int, + pub leaks: c_int, + pub check_errors: c_int, + pub corruptions_fixed: c_int, + pub leaks_fixed: c_int, + pub image_end_offset: i64, + pub bfi: c_int, /* BlockFragInfo */ +} + +#[repr(C)] +pub struct BlockSizes { + pub phys: u32, + pub log: u32, +} + +#[repr(C)] +pub struct HDGeometry { + pub heads: u32, + pub sectors: u32, + pub cylinders: u32, +} + +#[repr(C)] +pub struct BlockLimits { + pub request_alignment: u32, + pub max_pdiscard: i32, + pub pdiscard_alignment: u32, + pub max_pwrite_zeroes: i32, + pub pwrite_zeroes_alignment: u32, + pub opt_transfer: u32, + pub max_transfer: u32, + pub min_mem_alignment: size_t, + pub opt_mem_alignment: size_t, + pub max_iov: c_int, +} + +#[repr(C)] +pub struct BdrvChild { + pub bs: *mut BlockDriverState, + pub name: *mut c_char, + pub role: *const BdrvChildRole, + pub opaque: *mut c_void, + + pub perm: u64, + + pub shared_perm: u64, + + pub next: QListEntry, + pub next_parent: QListEntry, +} + +#[repr(C)] +pub struct BdrvChildRole { + pub stay_at_node: bool, + + pub inherit_options: Option< + extern fn(child_flags: *mut c_int, child_options: *mut QDict, + parent_flags: c_int, parent_options: *mut QDict) + >, + + pub change_media: Option, + pub resize: Option, + + pub get_name: Option *const c_char= >, + + /* Return value should probably be allocated in the C program */ + pub get_parent_desc: Option< + extern fn(child: *mut BdrvChild) -> *mut c_char + >, + + pub drained_begin: Option, + pub drained_end: Option, + + pub attach: Option, + pub detach: Option, +} + +#[repr(C)] +pub struct BdrvAioNotifier { + pub attached_aio_context: Option< + extern fn(new_context: *mut AioContext, opaque: *mut c_void) + >, + pub detach_aio_context: Option, + + pub opaque: *mut c_void, + pub deleted: bool, + + pub list: QListEntry, +} + +#[repr(C)] +pub struct BlockDriverInfo { + pub cluster_size: c_int, + pub vm_state_offset: i64, + pub is_dirty: bool, + pub unallocated_blocks_are_zero: bool, + pub can_write_zeroes_with_unmap: bool, + pub needs_compressed_writes: bool, +} + +#[repr(C)] +pub struct ImageInfoSpecific { + pub kind: c_int, /* ImageInfoSpecificKind */ + pub date: *mut c_void, /* type depends on kind */ +} + +#[repr(C)] +pub struct QEMUIOVector { + pub iov: *mut iovec, + pub niov: c_int, + pub nalloc: c_int, + pub size: size_t, +} + +#[repr(C)] +pub struct iovec { + pub iov_base: *mut c_void, + pub iov_len: size_t, +} + +#[repr(C)] +pub struct QEMUSnapshotInfo { + pub id_str: [c_char; 128], + pub name: [c_char; 256], + pub vm_state_size: u64, + pub date_sec: u32, + pub date_nsec: u32, + pub vm_clock_nsec: u64, +} + +#[repr(C)] +pub struct NotifierWithReturnList { + pub notifiers: *mut NotifierWithReturn, +} + +#[repr(C)] +pub struct NotifierWithReturn { + pub notify: extern fn(notifier: *mut NotifierWithReturn, data: *mut c_= void) + -> c_int, + pub node: QListEntry, +} + +#[repr(C)] +pub struct CoQueue { + pub entries: CoroutineQSimpleQHead, +} + +#[repr(C)] +pub struct CoroutineQSimpleQHead { + pub sqh_first: *mut Coroutine, + pub sqh_last: *mut *mut Coroutine, +} + +#[repr(C)] +pub struct QListHead { + pub lh_first: *mut T, +} + +#[repr(C)] +pub struct QListEntry { + pub le_next: *mut T, + pub le_prev: *mut *mut T, +} + +#[repr(C)] +pub struct QTailQEntry { + pub tqe_next: *mut T, + pub tqe_prev: *mut *mut T, +} + +type BlockDriverAmendStatusCB =3D extern fn(bs: *mut BlockDriverState, + offset: i64, total_work_size: i6= 4, + opaque: *mut c_void); + +/* Opaque types */ +pub enum AioContext {} +pub enum BdrvDirtyBitmap {} +pub enum BdrvOpBlocker {} +pub enum BdrvTrackedRequest {} +pub enum BlockReopenQueue {} +pub enum BlockJob {} +pub enum Coroutine {} +pub enum Error {} +pub enum QDict {} +pub enum QemuOpts {} +pub enum QemuOptsList {} + +/* Used for deprecated function pointers */ +type DeprecatedFn =3D extern fn(); diff --git a/block/rust/src/interface/mod.rs b/block/rust/src/interface/mod= .rs new file mode 100644 index 0000000000..8afcbe36c5 --- /dev/null +++ b/block/rust/src/interface/mod.rs @@ -0,0 +1,1012 @@ +pub mod c_constants; +pub mod c_functions; +pub mod c_structs; + + +use core::intrinsics::transmute; +use core::marker::Sized; +pub use core::ops::{Deref,DerefMut}; +use libc; +use libc::{c_char,c_int,c_void,size_t,EINVAL,EIO,ENOSPC,ENOTSUP,EPERM}; +use self::c_functions::error_setg_internal; +use std::{mem,ptr,slice}; +use std::cell::RefCell; +use std::ffi::CString; +use std::io; +use std::io::Write; +use std::marker::PhantomData; +use std::rc::{Rc,Weak}; + + +pub type QDict =3D *mut c_structs::QDict; +pub type CBDS =3D c_structs::BlockDriverState; + + +pub const BDRV_SECTOR_SIZE: u64 =3D 512u64; +pub const BDRV_SECTOR_SHIFT: i32 =3D 9; + + +pub enum IOError { + GenericError, + NoSpaceLeft, + InvalidMetadata, + UnsupportedImageFeature, +} + +impl IOError { + pub fn to_errno(&self) -> c_int + { + match *self { + IOError::GenericError =3D> -EIO, + IOError::NoSpaceLeft =3D> -ENOSPC, + IOError::InvalidMetadata =3D> -EIO, + IOError::UnsupportedImageFeature =3D> -ENOTSUP, + } + } +} + + +pub struct BDSOpaqueLink { + pub opaque: Option>>>, +} + +pub struct BDSOpaque { + c_obj: *mut CBDS, + pub driver_obj: RefCell, +} + + +/* + * Macros for extracting the block driver's opaque BDS from a &mut CBDS + * + * Note that we have to pass a CBDS to the block driver functions instead = of + * the opaque BDS itself because a block driver is allowed to do recursive= calls + * (and even if it does not do so actively, the qemu block layer may just = do it + * anyway). The borrow checker will prevent us from borrowing the opaque B= DS in + * recursive function invocations, so the functions will have to borrow it= only + * when needed. + */ +#[macro_export] +macro_rules! let_bds { + ($result:ident, $cbds:expr) =3D> ( + let _bds_opaque =3D get_bds_opaque_link::($cbds).unwrap(); + let mut _driver_obj_ref =3D _bds_opaque.driver_obj.borrow(); + let $result =3D _driver_obj_ref.deref(); + ); +} + +#[macro_export] +macro_rules! let_mut_bds { + ($result:ident, $cbds:expr) =3D> ( + let _bds_opaque =3D get_bds_opaque_link::($cbds).unwrap(); + let mut _driver_obj_ref =3D _bds_opaque.driver_obj.borrow_mut(); + let $result =3D _driver_obj_ref.deref_mut(); + ); +} + + +/* Allows you to prepend an error string to an error string on error */ +#[macro_export] +macro_rules! try_prepend { + ($e:expr, $m:expr) =3D> (match $e { + Ok(val) =3D> val, + Err(err) =3D> return Err(String::from($m) + ": " + err.as_str()), + }); +} + + +/* try! that executes a qemu_vfree() on error */ +#[macro_export] +macro_rules! try_vfree { + ($e:expr, $buffer:ident) =3D> (match $e { + Ok(ok) =3D> ok, + Err(err) =3D> { + qemu_vfree($buffer); + return Err(err); + }, + }); +} + + + +pub struct BDSBacklink { + pub link: Weak>>, +} + +pub struct BDSCommon { + backlink: Option>, +} + +impl BDSCommon { + pub fn new() -> Self + { + Self { + backlink: None, + } + } + + pub fn set_backlink(&mut self, backlink: BDSBacklink) + { + self.backlink =3D Some(backlink); + } + + pub fn get_cbds(&self) -> &mut CBDS + { + let backlink =3D self.backlink.as_ref().unwrap(); + let bds_opaque_rc =3D backlink.link.upgrade(); + let bds_opaque =3D Rc::as_ref(bds_opaque_rc.as_ref().unwrap()); + + let res =3D unsafe { &mut *bds_opaque.c_obj }; + return res; + } + + pub fn has_file(&self) -> bool + { + let cbds =3D self.get_cbds(); + !cbds.file.is_null() + } + + pub fn file(&self) -> BdrvChild + { + let cbds =3D self.get_cbds(); + BdrvChild { c_ptr: cbds.file } + } + + pub fn set_file(&mut self, file: Option) + { + let cbds =3D self.get_cbds(); + + match file { + None =3D> cbds.file =3D ptr::null_mut(), + Some(ref child) =3D> cbds.file =3D child.c_ptr, + }; + } + + pub fn has_backing(&self) -> bool + { + let cbds =3D self.get_cbds(); + !cbds.backing.is_null() + } + + pub fn backing(&self) -> BdrvChild + { + let cbds =3D self.get_cbds(); + BdrvChild { c_ptr: cbds.backing } + } + + pub fn set_backing(&mut self, backing: Option) + { + let cbds =3D self.get_cbds(); + + match backing { + None =3D> cbds.file =3D ptr::null_mut(), + Some(ref child) =3D> cbds.file =3D child.c_ptr, + }; + } +} + + +pub trait BlockDriverState where Self: Sized { + fn new() -> Self; + fn common(&mut self) -> &mut BDSCommon; + + fn set_backlink(&mut self, backlink: BDSBacklink) + { + self.common().set_backlink(backlink); + } +} + +pub trait BlockDriverOpen: BlockDriverState { + fn bdrv_open(cbds: &mut CBDS, options: QDict, flags: u32) + -> Result<(), String>; +} + +pub trait BlockDriverClose: BlockDriverState { + fn bdrv_close(cbds: &mut CBDS); +} + +pub trait BlockDriverRead: BlockDriverState { + /* iov is ordered backwards so you can pop in order */ + fn bdrv_co_preadv(cbds: &mut CBDS, offset: u64, bytes: u64, + iov: Vec<&mut [u8]>, flags: u32) + -> Result<(), IOError>; +} + +pub trait BlockDriverWrite: BlockDriverState { + /* iov is ordered backwards so you can pop in order */ + fn bdrv_co_pwritev(cbds: &mut CBDS, offset: u64, bytes: u64, + iov: Vec<&[u8]>, flags: u32) + -> Result<(), IOError>; +} + +pub trait BlockDriverPerm: BlockDriverState { + fn bdrv_check_perm(cbds: &mut CBDS, perm: u64, shared: u64) + -> Result<(), String>; + fn bdrv_set_perm(cbds: &mut CBDS, perm: u64, shared: u64); + fn bdrv_abort_perm_update(cbds: &mut CBDS); +} + +pub trait BlockDriverChildPerm: BlockDriverState { + fn bdrv_child_perm(cbds: &mut CBDS, c: Option<&mut BdrvChild>, + role: &c_structs::BdrvChildRole, + parent_perm: u64, parent_shared: u64) + -> (u64, u64); /* nperm, nshared */ +} + +pub trait BlockDriverInfo: BlockDriverState { + fn bdrv_get_info(cbds: &mut CBDS, bdi: &mut c_structs::BlockDriverInfo) + -> Result<(), String>; +} + + +pub struct BlockDriver { + c_obj: c_structs::BlockDriver, + + _phantom: PhantomData, +} + + +pub fn get_bds_opaque_link<'a, T>(bds: *mut CBDS) + -> &'a mut BDSOpaqueLink +{ + unsafe { + let r =3D transmute::<*mut c_void, *mut BDSOpaqueLink>((*bds).o= paque); + return &mut *r; + } +} + +impl BDSOpaqueLink { + pub fn unwrap(&self) -> &BDSOpaque + { + Rc::as_ref(self.opaque.as_ref().unwrap()).as_ref() + } +} + + +pub struct BdrvChild { + c_ptr: *mut c_structs::BdrvChild, +} + + +impl BdrvChild { + pub fn perm(&self) -> u64 + { + unsafe { + (*self.c_ptr).perm + } + } + + pub fn shared(&self) -> u64 + { + unsafe { + (*self.c_ptr).shared_perm + } + } + + pub fn borrow(&self) -> Self + { + BdrvChild { + c_ptr: self.c_ptr + } + } + + pub fn bdrv_pread(&self, offset: u64, buf: &mut [u8]) + -> Result<(), String> + { + let ret =3D unsafe { + c_functions::bdrv_pread(self.c_ptr, offset as i64, + buf.as_mut_ptr() as *mut c_void, + buf.len() as c_int) + }; + + if ret < 0 { + Err(strerror(-ret)) + } else { + Ok(()) + } + } + + pub fn bdrv_pwrite(&self, offset: u64, buf: &[u8]) + -> Result<(), String> + { + let ret =3D unsafe { + c_functions::bdrv_pwrite(self.c_ptr, offset as i64, + buf.as_ptr() as *const c_void, + buf.len() as c_int) + }; + + if ret < 0 { + Err(strerror(-ret)) + } else { + Ok(()) + } + } +} + + +impl BlockDriver { + extern fn invoke_open(bds: *mut CBDS, options: *mut c_structs::QDict, + flags: c_int, errp: *mut *mut c_structs::Error) + -> c_int + { + let bds_opaque_link =3D get_bds_opaque_link::(bds); + + assert!(bds_opaque_link.opaque.is_none()); + + let weak_bds_opaque =3D { + let rc_bds_opaque =3D Rc::new(Box::new(BDSOpaque:: { + c_obj: bds, + driver_obj: RefCell::new(T::new()), + })); + + let weak_link =3D Rc::downgrade(&rc_bds_opaque); + bds_opaque_link.opaque =3D Some(rc_bds_opaque); + + weak_link + }; + + { + let bds_opaque =3D bds_opaque_link.unwrap(); + let mut driver_obj_ref =3D bds_opaque.driver_obj.borrow_mut(); + let backlink =3D BDSBacklink:: { + link: weak_bds_opaque + }; + + driver_obj_ref.deref_mut().set_backlink(backlink); + } + + let cbds =3D unsafe { &mut *bds }; + let res =3D T::bdrv_open(cbds, options, flags as u32); + + match res { + Ok(_) =3D> 0, + Err(msg) =3D> { + bds_opaque_link.opaque =3D None; + error_set_message(errp, msg); + + -EINVAL + } + } + } +} + + +impl BlockDriver { + extern fn invoke_close(bds: *mut CBDS) + { + let cbds =3D unsafe { &mut *bds }; + T::bdrv_close(cbds); + + get_bds_opaque_link::(bds).opaque =3D None; + } +} + + +impl BlockDriver { + extern fn invoke_co_preadv(bds: *mut CBDS, offset: u64, bytes: u64, + qiov: *mut c_structs::QEMUIOVector, flags: = c_int) + -> c_int + { + let cbds =3D unsafe { &mut *bds }; + let qiov_deref =3D unsafe { &*qiov }; + + let qiov_vecs =3D unsafe { + slice::from_raw_parts(qiov_deref.iov, qiov_deref.niov as u= size) + }; + + let mut iov =3D Vec::new(); + /* Push backwards so the driver can pop in the right order */ + for vec in qiov_vecs.iter().rev() { + iov.push(iov_as_mut_byte_slice(vec)); + } + + let res =3D T::bdrv_co_preadv(cbds, offset, bytes, iov, flags as u= 32); + match res { + Ok(_) =3D> 0, + Err(err) =3D> err.to_errno(), + } + } +} + + +impl BlockDriver { + extern fn invoke_co_pwritev(bds: *mut CBDS, offset: u64, bytes: u64, + qiov: *mut c_structs::QEMUIOVector, flags:= c_int) + -> c_int + { + let cbds =3D unsafe { &mut *bds }; + let qiov_deref =3D unsafe { &*qiov }; + + let qiov_vecs =3D unsafe { + slice::from_raw_parts(qiov_deref.iov, qiov_deref.niov as u= size) + }; + + let mut iov =3D Vec::new(); + /* Push backwards so the driver can pop in the right order */ + for vec in qiov_vecs.iter().rev() { + iov.push(iov_as_byte_slice(vec)); + } + + let res =3D T::bdrv_co_pwritev(cbds, offset, bytes, iov, flags as = u32); + match res { + Ok(_) =3D> 0, + Err(err) =3D> err.to_errno(), + } + } +} + + +impl BlockDriver { + extern fn invoke_check_perm(bds: *mut CBDS, perm: u64, shared: u64, + errp: *mut *mut c_structs::Error) + -> c_int + { + let cbds =3D unsafe { &mut *bds }; + let res =3D T::bdrv_check_perm(cbds, perm, shared); + match res { + Ok(_) =3D> 0, + Err(msg) =3D> { + error_set_message(errp, msg); + -EPERM + } + } + } + + extern fn invoke_set_perm(bds: *mut CBDS, perm: u64, shared: u64) + { + let cbds =3D unsafe { &mut *bds }; + T::bdrv_set_perm(cbds, perm, shared); + } + + extern fn invoke_abort_perm_update(bds: *mut CBDS) + { + let cbds =3D unsafe { &mut *bds }; + T::bdrv_abort_perm_update(cbds); + } +} + + +impl BlockDriver { + extern fn invoke_child_perm(bds: *mut CBDS, c: *mut c_structs::BdrvChi= ld, + role: *const c_structs::BdrvChildRole, + parent_perm: u64, parent_shared: u64, + nperm: *mut u64, nshared: *mut u64) + { + let cbds =3D unsafe { &mut *bds }; + let role_deref =3D unsafe { &*role }; + + let res =3D if c.is_null() { + T::bdrv_child_perm(cbds, None, role_deref, + parent_perm, parent_shared) + } else { + let mut child =3D BdrvChild { c_ptr: c }; + T::bdrv_child_perm(cbds, Some(&mut child), role_deref, + parent_perm, parent_shared) + }; + + unsafe { + *nperm =3D res.0; + *nshared =3D res.1; + } + } +} + + +impl BlockDriver { + extern fn invoke_get_info(bds: *mut CBDS, + bdi: *mut c_structs::BlockDriverInfo) + -> c_int + { + let cbds =3D unsafe { &mut *bds }; + let bdi_deref =3D unsafe { &mut *bdi }; + + let res =3D T::bdrv_get_info(cbds, bdi_deref); + match res { + Ok(_) =3D> 0, + Err(msg) =3D> { + writeln!(&mut io::stderr(), "{}", msg).unwrap(); + -EIO + } + } + } +} + + +impl BlockDriver { + pub fn new(name: String) -> BlockDriver + { + let instance_size =3D mem::size_of::>(); + let /*mut*/ bdrv =3D BlockDriver:: { + c_obj: c_structs::BlockDriver { + format_name: CString::new(name).unwrap().into_raw(), + instance_size: instance_size as c_int, + + is_filter: false, + + bdrv_recurse_is_first_non_filter: None, + + bdrv_probe: None, + bdrv_probe_device: None, + + bdrv_parse_filename: None, + bdrv_needs_filename: false, + + supports_backing: false, + + bdrv_reopen_prepare: None, + bdrv_reopen_commit: None, + bdrv_reopen_abort: None, + bdrv_join_options: None, + + bdrv_open: None, + bdrv_file_open: None, + bdrv_close: None, + bdrv_create: None, + bdrv_set_key: None, + bdrv_make_empty: None, + + bdrv_refresh_filename: None, + + bdrv_aio_readv: None, + bdrv_aio_writev: None, + bdrv_aio_flush: None, + bdrv_aio_pdiscard: None, + + bdrv_co_readv: None, + bdrv_co_preadv: None, + bdrv_co_writev: None, + bdrv_co_writev_flags: None, + bdrv_co_pwritev: None, + + bdrv_co_pwrite_zeroes: None, + bdrv_co_pdiscard: None, + bdrv_co_get_block_status: None, + + bdrv_invalidate_cache: None, + bdrv_inactivate: None, + + bdrv_co_flush: None, + + bdrv_co_flush_to_disk: None, + + bdrv_co_flush_to_os: None, + + protocol_name: ptr::null(), + bdrv_truncate: None, + + bdrv_getlength: None, + has_variable_length: false, + bdrv_get_allocated_file_size: None, + + bdrv_co_pwritev_compressed: None, + + bdrv_snapshot_create: None, + bdrv_snapshot_goto: None, + bdrv_snapshot_delete: None, + bdrv_snapshot_list: None, + bdrv_snapshot_load_tmp: None, + + bdrv_get_info: None, + bdrv_get_specific_info: None, + + bdrv_save_vmstate: None, + bdrv_load_vmstate: None, + + bdrv_change_backing_file: None, + + bdrv_is_inserted: None, + bdrv_media_changed: None, + bdrv_eject: None, + bdrv_lock_medium: None, + + bdrv_aio_ioctl: None, + bdrv_co_ioctl: None, + + create_opts: ptr::null_mut(), + + bdrv_check: None, + + bdrv_amend_options: None, + + bdrv_debug_event: None, + bdrv_debug_breakpoint: None, + bdrv_debug_remove_breakpoint: None, + bdrv_debug_resume: None, + bdrv_debug_is_suspended: None, + + bdrv_refresh_limits: None, + + bdrv_has_zero_init: None, + + bdrv_detach_aio_context: None, + + bdrv_attach_aio_context: None, + + bdrv_io_plug: None, + bdrv_io_unplug: None, + + bdrv_probe_blocksizes: None, + bdrv_probe_geometry: None, + + bdrv_drain: None, + + bdrv_add_child: None, + bdrv_del_child: None, + + bdrv_check_perm: None, + bdrv_set_perm: None, + bdrv_abort_perm_update: None, + + bdrv_child_perm: None, + + list: c_structs::QListEntry:: { + le_next: ptr::null_mut(), + le_prev: ptr::null_mut(), + }, + }, + + _phantom: PhantomData, + }; + + /* TODO: Call provides_* automatically + * (We cannot do this currently because there is no way to either + * (1) Check whether T implements a trait in an if clause + * (2) Implement provides_* if T does not implement a trait + * (The latter of which is something that Rust is expected to imp= lement + * at some point in the future.)) */ + + return bdrv; + } + + pub fn supports_backing(&mut self) + { + self.c_obj.supports_backing =3D true; + } + + pub fn has_variable_length(&mut self) + { + self.c_obj.has_variable_length =3D true; + } +} + + +impl BlockDriver { + pub fn provides_open(&mut self) + { + self.c_obj.bdrv_open =3D Some(BlockDriver::::invoke_open); + } +} + + +impl BlockDriver { + pub fn provides_close(&mut self) + { + self.c_obj.bdrv_close =3D Some(BlockDriver::::invoke_close); + } +} + + +impl BlockDriver { + pub fn provides_read(&mut self) + { + self.c_obj.bdrv_co_preadv =3D Some(BlockDriver::::invoke_co_pre= adv); + } +} + + +impl BlockDriver { + pub fn provides_write(&mut self) + { + self.c_obj.bdrv_co_pwritev =3D Some(BlockDriver::::invoke_co_pw= ritev); + } +} + + +impl BlockDriver { + pub fn provides_perm(&mut self) + { + self.c_obj.bdrv_check_perm =3D Some(BlockDriver::::invoke_check= _perm); + self.c_obj.bdrv_set_perm =3D Some(BlockDriver::::invoke_set_per= m); + self.c_obj.bdrv_abort_perm_update =3D + Some(BlockDriver::::invoke_abort_perm_update); + } +} + + +impl BlockDriver { + pub fn provides_child_perm(&mut self) + { + self.c_obj.bdrv_child_perm =3D Some(BlockDriver::::invoke_child= _perm); + } +} + + +impl BlockDriver { + pub fn provides_info(&mut self) + { + self.c_obj.bdrv_get_info =3D Some(BlockDriver::::invoke_get_inf= o); + } +} + + +pub fn bdrv_register(bdrv: BlockDriver) +{ + /* Box so it doesn't go away */ + let bdrv_box =3D Box::new(bdrv); + + unsafe { + c_functions::bdrv_register(&mut (*Box::into_raw(bdrv_box)).c_obj); + } +} + + +pub fn bdrv_open_child(filename: Option, options: Option, + bdref_key: String, parent: &mut CBDS, + child_role: &c_structs::BdrvChildRole, allow_none: = bool) + -> Result +{ + let c_filename =3D match filename { + None =3D> ptr::null(), + Some(x) =3D> CString::new(x).unwrap().into_raw(), + }; + + let c_options =3D match options { + None =3D> ptr::null_mut(), + Some(x) =3D> x, + }; + + let c_bdref_key =3D CString::new(bdref_key).unwrap().into_raw(); + let c_parent =3D parent as *mut CBDS; + let c_child_role =3D child_role as *const c_structs::BdrvChildRole; + + let c_allow_none =3D allow_none; + + unsafe { + let mut local_err: *mut c_structs::Error =3D ptr::null_mut(); + let c_errp =3D &mut local_err as *mut *mut c_structs::Error; + + let child =3D c_functions::bdrv_open_child(c_filename, c_options, + c_bdref_key, c_parent, + c_child_role, c_allow_non= e, + c_errp); + + if child.is_null() { + Err(error_get_string(local_err)) + } else { + Ok(BdrvChild { c_ptr: child }) + } + } +} + + +pub enum StandardChildRole { + File, + Format, + Backing, +} + +pub fn bdrv_get_standard_child_role(role: StandardChildRole) + -> &'static c_structs::BdrvChildRole +{ + unsafe { + match role { + StandardChildRole::File =3D> &c_constants::child_file, + StandardChildRole::Format =3D> &c_constants::child_format, + StandardChildRole::Backing =3D> &c_constants::child_backing, + } + } +} + + +const BLK_PERM_CONSISTENT_READ : u64 =3D 0x01u64; +const BLK_PERM_WRITE : u64 =3D 0x02u64; +const BLK_PERM_WRITE_UNCHANGED : u64 =3D 0x04u64; +const BLK_PERM_RESIZE : u64 =3D 0x08u64; +const BLK_PERM_GRAPH_MOD : u64 =3D 0x10u64; + +const BLK_PERM_ALL : u64 =3D 0x1fu64; + + +pub fn bdrv_filter_default_perms(c: Option<&mut BdrvChild>, + _: &c_structs::BdrvChildRole, + perm: u64, shared: u64) + -> (u64, u64) +{ + let default_perm_passthrough =3D BLK_PERM_CONSISTENT_READ + | BLK_PERM_WRITE + | BLK_PERM_WRITE_UNCHANGED + | BLK_PERM_RESIZE; + let default_perm_unchanged =3D BLK_PERM_ALL & !default_perm_passthroug= h; + + if c.is_none() { + (perm & default_perm_passthrough, + (shared & default_perm_passthrough) | default_perm_unchanged) + } else { + let child =3D c.unwrap(); + + ((perm & default_perm_passthrough) | + (child.perm() & default_perm_unchanged), + + (shared & default_perm_passthrough) | + (child.shared() & default_perm_unchanged)) + } +} + + +pub fn bdrv_format_default_perms(c: Option<&mut BdrvChild>, + role: &c_structs::BdrvChildRole, + perm: u64, shared: u64, is_read_only: boo= l) + -> (u64, u64) +{ + let backing_role =3D + bdrv_get_standard_child_role(StandardChildRole::Backing); + + let mut p =3D perm; + let mut s =3D shared; + + if role as *const c_structs::BdrvChildRole + =3D=3D backing_role as *const c_structs::BdrvChildRole + { + p &=3D BLK_PERM_CONSISTENT_READ; + + if (s & BLK_PERM_WRITE) !=3D 0 { + s =3D BLK_PERM_WRITE | BLK_PERM_RESIZE; + } else { + s =3D 0; + } + + s |=3D BLK_PERM_CONSISTENT_READ | BLK_PERM_GRAPH_MOD | + BLK_PERM_WRITE_UNCHANGED; + } else { + let filter =3D bdrv_filter_default_perms(c, role, p, s); + p =3D filter.0; + s =3D filter.1; + + if !is_read_only { + p |=3D BLK_PERM_WRITE | BLK_PERM_RESIZE; + } + + p |=3D BLK_PERM_CONSISTENT_READ; + s &=3D !(BLK_PERM_WRITE | BLK_PERM_RESIZE); + } + + (p, s) +} + + +pub fn bdrv_is_read_only(bds: *mut CBDS) -> bool +{ + unsafe { + c_functions::bdrv_is_read_only(bds) + } +} + + +fn error_get_string(err: *mut c_structs::Error) -> String +{ + unsafe { + let msg =3D c_functions::error_get_pretty(err); + let dmsg =3D c_functions::g_strdup(msg); + + CString::from_raw(dmsg).into_string().unwrap() + } +} + + +fn error_set_message(errp: *mut *mut c_structs::Error, msg: String) +{ + let file =3D CString::new("").unwrap(); + let func =3D CString::new("").unwrap(); + let format =3D CString::new("%.*s").unwrap(); + + unsafe { + error_setg_internal(errp, file.as_ptr(), -1, func.as_ptr(), + format.as_ptr(), + msg.len() as c_int, + msg.as_ptr() as *const c_char); + } +} + + +fn strerror(errno: c_int) -> String +{ + unsafe { + let msg =3D libc::strerror(errno); + let dmsg =3D c_functions::g_strdup(msg); + + CString::from_raw(dmsg).into_string().unwrap() + } +} + + +/* Attention: The content of the slice is undefined! + * (Also: Remember that the slice will not be automatically freed; you hav= e to + * manually call qemu_vfree() for that.) */ +pub fn qemu_blockalign(bds: *mut CBDS, size: usize) -> &'static mut [u8] +{ + unsafe { + let p =3D c_functions::qemu_blockalign(bds, size as size_t); + slice::from_raw_parts_mut(p as *mut u8, size) + } +} + + +pub fn qemu_vfree(mem: &mut [u8]) +{ + unsafe { + c_functions::qemu_vfree(mem.as_mut_ptr() as *mut c_void); + } +} + + +pub fn object_as_mut_byte_slice(obj: &mut T) -> &mut [u8] +{ + unsafe { + let p =3D obj as *mut T; + slice::from_raw_parts_mut(p as *mut u8, mem::size_of::()) + } +} + + +pub fn object_as_byte_slice(obj: &T) -> &[u8] +{ + unsafe { + let p =3D obj as *const T; + slice::from_raw_parts(p as *const u8, mem::size_of::()) + } +} + + +pub fn vec_as_mut_byte_slice(obj: &mut Vec) -> &mut [u8] +{ + unsafe { + let p =3D &mut obj[0] as *mut T; + slice::from_raw_parts_mut(p as *mut u8, obj.len() * mem::size_of::= ()) + } +} + + +pub fn slice_as_mut_byte_slice(obj: &mut [T]) -> &mut [u8] +{ + unsafe { + slice::from_raw_parts_mut(obj.as_mut_ptr() as *mut u8, + obj.len() * mem::size_of::()) + } +} + + +pub fn iov_as_mut_byte_slice(obj: &c_structs::iovec) -> &mut [u8] +{ + unsafe { + slice::from_raw_parts_mut(obj.iov_base as *mut u8, obj.iov_len) + } +} + + +pub fn iov_as_byte_slice(obj: &c_structs::iovec) -> &[u8] +{ + unsafe { + slice::from_raw_parts(obj.iov_base as *const u8, obj.iov_len) + } +} + + +pub fn zero_byte_slice(slice: &mut [u8]) +{ + unsafe { + libc::memset(slice.as_mut_ptr() as *mut c_void, 0, slice.len()); + } +} + + +pub fn copy_into_byte_slice(dest: &mut [u8], offset: usize, src: &[u8]) +{ + assert!(dest.len() >=3D offset + src.len()); + unsafe { + let ptr =3D dest.as_mut_ptr(); + ptr.offset(offset as isize); + + libc::memcpy(ptr as *mut c_void, src.as_ptr() as *const c_void, + src.len()); + } +} diff --git a/block/rust/src/lib.rs b/block/rust/src/lib.rs new file mode 100644 index 0000000000..2aa2f365ba --- /dev/null +++ b/block/rust/src/lib.rs @@ -0,0 +1,9 @@ +/* Rust cannot not complain about unused public interfaces, which is rather + * annoying */ +#![allow(dead_code)] + +extern crate core; +extern crate libc; + +#[macro_use] +mod interface; --=20 2.12.2 From nobody Fri May 3 01:35:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492531191039943.0763430718408; Tue, 18 Apr 2017 08:59:51 -0700 (PDT) Received: from localhost ([::1]:42658 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0VXh-0001LX-7H for importer@patchew.org; Tue, 18 Apr 2017 11:59:49 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37898) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0VWF-0000Pu-2c for qemu-devel@nongnu.org; Tue, 18 Apr 2017 11:58:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d0VWC-0008ML-LW for qemu-devel@nongnu.org; Tue, 18 Apr 2017 11:58:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35072) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d0VW5-0008Jz-Ig; Tue, 18 Apr 2017 11:58:09 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 726CD3D970; Tue, 18 Apr 2017 15:58:08 +0000 (UTC) Received: from localhost (ovpn-204-72.brq.redhat.com [10.40.204.72]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5EB8183296; Tue, 18 Apr 2017 15:58:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 726CD3D970 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mreitz@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 726CD3D970 From: Max Reitz To: qemu-block@nongnu.org Date: Sat, 1 Apr 2017 17:57:49 +0200 Message-Id: <20170401155751.14322-3-mreitz@redhat.com> In-Reply-To: <20170401155751.14322-1-mreitz@redhat.com> References: <20170401155751.14322-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 18 Apr 2017 15:58:08 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC for-3.0 2/4] block/qcow2-rust: Add qcow2-rust block driver X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , John Snow , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Currently, this MUCH MORE SECURE block driver than the LEGACY C qcow2 driver (SAD!) only has read support. But this makes it actually much less likely to destroy your data, so this is a GOOD thing. Signed-off-by: Max Reitz --- block/rust/src/lib.rs | 3 + block/rust/src/qcow2/io.rs | 322 +++++++++++++++++++++++++= ++ block/rust/src/qcow2/mod.rs | 334 +++++++++++++++++++++++++= ++++ block/rust/src/qcow2/on_disk_structures.rs | 59 +++++ 4 files changed, 718 insertions(+) create mode 100644 block/rust/src/qcow2/io.rs create mode 100644 block/rust/src/qcow2/mod.rs create mode 100644 block/rust/src/qcow2/on_disk_structures.rs diff --git a/block/rust/src/lib.rs b/block/rust/src/lib.rs index 2aa2f365ba..9892a22b84 100644 --- a/block/rust/src/lib.rs +++ b/block/rust/src/lib.rs @@ -7,3 +7,6 @@ extern crate libc; =20 #[macro_use] mod interface; + +mod qcow2; +pub use qcow2::*; /* Export symbols */ diff --git a/block/rust/src/qcow2/io.rs b/block/rust/src/qcow2/io.rs new file mode 100644 index 0000000000..069cc78303 --- /dev/null +++ b/block/rust/src/qcow2/io.rs @@ -0,0 +1,322 @@ +use interface::*; +use qcow2::*; + + +pub enum MNMIOV<'a> { + Mut(Vec<&'a mut [u8]>), + Const(Vec<&'a [u8]>), +} + +pub enum MNMIOVSlice<'a> { + Mut(&'a mut [u8]), + Const(&'a [u8]), +} + + +pub struct HostOffsetInfo { + pub guest_offset: u64, + + pub cluster_size: u32, + pub compressed_shift: u8, + + pub file: BdrvChild, + + pub l1_index: u32, + pub l2_index: u32, + pub offset_in_cluster: u32, + + pub l1_entry: L1Entry, + pub l2_entry: Option, +} + +pub enum L1Entry { + Unallocated, + + /* L2 offset, COPIED */ + Allocated(u64, bool), +} + +pub enum L2Entry { + Unallocated, + + /* Offset, COPIED */ + Normal(u64, bool), + + /* Offset (if allocated), COPIED */ + Zero(Option, bool), + + /* Offset, compressed length */ + Compressed(u64, usize), +} + +impl L1Entry { + pub fn from_bits(l1_entry: u64, cluster_size: u32) -> Result + { + let l2_offset =3D l1_entry & L1E_OFFSET_MASK; + + if l2_offset =3D=3D 0 { + Ok(L1Entry::Unallocated) + } else if (l2_offset & ((cluster_size - 1) as u64)) !=3D 0 { + Err(IOError::InvalidMetadata) + } else { + Ok(L1Entry::Allocated(l2_offset, (l1_entry & OFLAG_COPIED) != =3D 0)) + } + } + + + pub fn to_bits(&self) -> u64 + { + match *self { + L1Entry::Unallocated =3D> 0u64, + L1Entry::Allocated(offset, false) =3D> offset, + L1Entry::Allocated(offset, true) =3D> offset | OFLAG_COPIED, + } + } +} + +impl L2Entry { + pub fn from_bits(l2_entry: u64, cluster_size: u32, compressed_shift: u= 8) + -> Result + { + if (l2_entry & OFLAG_COMPRESSED) !=3D 0 { + let offset =3D l2_entry & ((1u64 << compressed_shift) - 1); + let sectors =3D (l2_entry & L2E_COMPRESSED_MASK) >> compressed= _shift; + let length =3D sectors * BDRV_SECTOR_SIZE; + + Ok(L2Entry::Compressed(offset, length as usize)) + } else { + let offset =3D l2_entry & L2E_OFFSET_MASK; + let copied =3D (l2_entry & OFLAG_COPIED) !=3D 0; + + if (offset & ((cluster_size - 1) as u64)) !=3D 0 { + Err(IOError::InvalidMetadata) + } else { + if (l2_entry & OFLAG_ZERO) !=3D 0 { + if offset =3D=3D 0 { + Ok(L2Entry::Zero(None, false)) + } else { + Ok(L2Entry::Zero(Some(offset), copied)) + } + } else { + if offset =3D=3D 0 { + Ok(L2Entry::Unallocated) + } else { + Ok(L2Entry::Normal(offset, copied)) + } + } + } + } + } + + + pub fn to_bits(&self, compressed_shift: u8) -> u64 + { + match *self { + L2Entry::Unallocated =3D> 0u64, + L2Entry::Normal(offset, false) =3D> offset, + L2Entry::Normal(offset, true) =3D> offset | OFLAG_COPIED, + L2Entry::Zero(None, _) =3D> OFLAG_ZERO, + L2Entry::Zero(Some(offset), false) =3D> offset | OFLAG_ZERO, + L2Entry::Zero(Some(offset), true) =3D> offset + | OFLAG_COPIED | OFLAG_= ZERO, + + L2Entry::Compressed(offset, length) =3D> { + let secs =3D ((length as u64) + BDRV_SECTOR_SIZE - 1) + / BDRV_SECTOR_SIZE; + + assert!((offset & !((1u64 << compressed_shift) - 1)) =3D= =3D 0); + assert!((secs << compressed_shift) >> compressed_shift =3D= =3D secs); + + offset | (secs << compressed_shift) | OFLAG_COMPRESSED + } + } + } +} + + +impl QCow2BDS { + pub fn split_io_to_clusters(cbds: &mut CBDS, mut offset: u64, bytes: u= 64, + mut iov_mnm: MNMIOV, flags: u32, + func: &Fn(&mut CBDS, u64, u32, &mut MNMIOV= Slice, + u32) + -> Result<(), IOError>) + -> Result<(), IOError> + { + let cluster_size =3D { + let_bds!(this, cbds); + this.cluster_size + }; + + let mut current_slice_outer: Option =3D None; + + let end_offset =3D offset + bytes; + while offset < end_offset { + /* Using a single current_slice variable does not work, and my + * knowledge of Rust does not suffice to explain why. */ + current_slice_outer =3D { + let mut current_slice_opt =3D current_slice_outer; + + let mut cs_len; + + while (cs_len =3D match current_slice_opt { + None =3D> 0, + Some(MNMIOVSlice::Mut(ref slice)) =3D> slice.len= (), + Some(MNMIOVSlice::Const(ref slice)) =3D> slice.len= (), + }, cs_len) =3D=3D ((), 0) + { + current_slice_opt =3D match iov_mnm { + MNMIOV::Mut(ref mut iov) =3D> + Some(MNMIOVSlice::Mut(iov.pop().unwrap())), + + MNMIOV::Const(ref mut iov) =3D> + Some(MNMIOVSlice::Const(iov.pop().unwrap())), + } + } + + let mut current_slice =3D current_slice_opt.unwrap(); + + let mut this_bytes: u32 =3D cluster_size; + if cs_len < (this_bytes as usize) { + this_bytes =3D cs_len as u32; + } + if end_offset - offset < (this_bytes as u64) { + this_bytes =3D (end_offset - offset) as u32; + } + + try!(func(cbds, offset, this_bytes, &mut current_slice, fl= ags)); + + offset +=3D this_bytes as u64; + + Some(match current_slice { + MNMIOVSlice::Mut(iov) =3D> + MNMIOVSlice::Mut(iov.split_at_mut(this_bytes as us= ize).1), + + MNMIOVSlice::Const(iov) =3D> + MNMIOVSlice::Const(iov.split_at(this_bytes as usiz= e).1), + }) + }; + } + + Ok(()) + } + + + fn do_backing_read(cbds: &mut CBDS, offset: u64, dest: &mut [u8]) + -> Result<(), IOError> + { + let backing =3D { + let_mut_bds!(this, cbds); + + if !this.common.has_backing() { + zero_byte_slice(dest); + return Ok(()); + } + + this.common.backing() + }; + + match backing.bdrv_pread(offset, dest) { + Ok(_) =3D> Ok(()), + Err(_) =3D> Err(IOError::GenericError), + } + } + + + fn find_host_offset(cbds: &mut CBDS, offset: u64) + -> Result + { + let mut res =3D { + let_mut_bds!(this, cbds); + + let cluster_offset_mask =3D (this.cluster_size - 1) as u64; + let l2_mask =3D (this.l2_size - 1) as u32; + + let l1_index =3D (offset >> this.l1_bits) as usize; + + HostOffsetInfo { + guest_offset: offset, + + cluster_size: this.cluster_size, + compressed_shift: 63 - (this.cluster_bits - 8), + + file: this.common.file(), + + l1_index: l1_index as u32, + l2_index: ((offset >> this.cluster_bits) as u32) & l2_mask, + offset_in_cluster: (offset & cluster_offset_mask) as u32, + + l1_entry: try!(L1Entry::from_bits(this.l1_table[l1_index], + this.cluster_size)), + l2_entry: None, + } + }; + + let mut l2_entry_offset; + + match res.l1_entry { + L1Entry::Unallocated =3D> return Ok(res), + L1Entry::Allocated(l2_offset, _) =3D> l2_entry_offset =3D l2_o= ffset, + } + + l2_entry_offset +=3D (res.l2_index as u64) * 8; + + let mut l2_entry =3D 0u64; + if let Err(_) =3D + res.file.bdrv_pread(l2_entry_offset, + object_as_mut_byte_slice(&mut l2_entry)) + { + return Err(IOError::GenericError); + } + + let l2_entry =3D try!(L2Entry::from_bits(u64::from_be(l2_entry), + res.cluster_size, + res.compressed_shift)); + res.l2_entry =3D Some(l2_entry); + return Ok(res); + } + + + fn do_read_cluster(cbds: &mut CBDS, hoi: &HostOffsetInfo, dest: &mut [= u8], + _: u32) + -> Result<(), IOError> + { + match hoi.l2_entry { + None | Some(L2Entry::Unallocated) =3D> + Self::do_backing_read(cbds, hoi.guest_offset, dest), + + Some(L2Entry::Zero(_, _)) =3D> { + zero_byte_slice(dest); + Ok(()) + }, + + Some(L2Entry::Compressed(_, _)) =3D> + Err(IOError::UnsupportedImageFeature), + + Some(L2Entry::Normal(offset, _)) =3D> { + let full_offset =3D offset + (hoi.offset_in_cluster as u64= ); + if let Err(_) =3D hoi.file.bdrv_pread(full_offset, dest) { + Err(IOError::GenericError) + } else { + Ok(()) + } + } + } + } + + + pub fn read_cluster(cbds: &mut CBDS, offset: u64, bytes: u32, + full_dest_mnm: &mut MNMIOVSlice, flags: u32) + -> Result<(), IOError> + { + let mut dest =3D match *full_dest_mnm { + MNMIOVSlice::Mut(ref mut full_dest) =3D> + full_dest.split_at_mut(bytes as usize).0, + + MNMIOVSlice::Const(_) =3D> + panic!("read_cluster() requires a mutable I/O vector"), + }; + + let hoi =3D try!(Self::find_host_offset(cbds, offset)); + Self::do_read_cluster(cbds, &hoi, dest, flags) + } +} diff --git a/block/rust/src/qcow2/mod.rs b/block/rust/src/qcow2/mod.rs new file mode 100644 index 0000000000..5fb523c93b --- /dev/null +++ b/block/rust/src/qcow2/mod.rs @@ -0,0 +1,334 @@ +mod io; +mod on_disk_structures; + + +use interface::*; +use self::on_disk_structures::*; + + +const MIN_CLUSTER_BITS: u32 =3D 9; +const MAX_CLUSTER_BITS: u32 =3D 21; +const MAX_L1_SIZE : u32 =3D 0x02000000u32; +const MAX_REFTABLE_SIZE : u32 =3D 0x00800000u32; +const L1E_OFFSET_MASK : u64 =3D 0x00fffffffffffe00u64; +const L2E_OFFSET_MASK : u64 =3D 0x00fffffffffffe00u64; +const L2E_COMPRESSED_MASK : u64 =3D 0x3fffffffffffffffu64; +const REFT_OFFSET_MASK : u64 =3D 0xfffffffffffffe00u64; + +const OFLAG_COPIED : u64 =3D 1u64 << 63; +const OFLAG_COMPRESSED : u64 =3D 1u64 << 62; +const OFLAG_ZERO : u64 =3D 1u64 << 0; + + +pub struct QCow2BDS { + common: BDSCommon, + + qcow_version: u8, + + cluster_bits: u8, + cluster_size: u32, + cluster_sectors: u32, + + l1_bits: u8, + l1_size: u32, + l2_bits: u8, + l2_size: u32, + + l1_offset: u64, + l1_table: Vec, + + refcount_order: u8, + reftable_bits: u8, + refblock_size: u32, + + reftable_offset: u64, + reftable_size: u32, + reftable: Vec, + + first_free_cluster_offset: u64, +} + + +impl QCow2BDS { + fn do_open(cbds: &mut CBDS, _: QDict, _: u32) + -> Result<(), String> + { + let file =3D { + let_mut_bds!(this, cbds); + this.common.file() + }; + + let mut header =3D QCow2Header::default(); + try_prepend!(file.bdrv_pread(0, object_as_mut_byte_slice(&mut head= er)), + "Could not read qcow2 header"); + + header.from_be(); + + let reftable_size; + + { + let_mut_bds!(this, cbds); + + if header.magic !=3D 0x514649fb { + return Err(String::from("Image is not in qcow2 format")); + } + if header.version < 2 || header.version > 3 { + return Err(format!("Unsupported qcow2 version {}", + header.version)); + } + + this.qcow_version =3D header.version as u8; + + if header.cluster_bits < MIN_CLUSTER_BITS || + header.cluster_bits > MAX_CLUSTER_BITS + { + return Err(format!("Unsupported cluster size: 2^{}", + header.cluster_bits)); + } + + this.cluster_bits =3D header.cluster_bits as u8; + this.cluster_size =3D 1u32 << this.cluster_bits; + this.cluster_sectors =3D this.cluster_size >> BDRV_SECTOR_SHIF= T; + + if this.qcow_version > 2 { + if header.header_length < 104 { + return Err(String::from("qcow2 header too short")); + } + if header.header_length > this.cluster_size { + return Err(String::from("qcow2 header exceeds cluster \ + size")); + } + } + + if header.backing_file_offset > (this.cluster_size as u64) { + return Err(String::from("Invalid backing file offset")); + } + + if this.qcow_version > 2 { + if header.incompatible_features !=3D 0 { + return Err(format!("Unsupported incompatible features:= \ + {:x}", header.incompatible_feature= s)); + } + + if header.refcount_order > 6 { + return Err(String::from("Refcount width may not exceed= 64 \ + bits")); + } + this.refcount_order =3D header.refcount_order as u8; + } + + /* No need to do anything about snapshots, compression, encryp= tion, + * or other funky extensions: We do not support them */ + + if header.crypt_method !=3D 0 { + return Err(format!("Unsupported encryption method: {}", + header.crypt_method)); + } + + if header.backing_file_size > 1023 || + (header.backing_file_size as u64) > + (this.cluster_size as u64) - header.backing_file_offset + { + return Err(String::from("Backing file name too long")); + } + + + this.l2_bits =3D this.cluster_bits - 3 /* ld(sizeof(u64)) */; + this.l2_size =3D 1u32 << this.l2_bits; + + cbds.total_sectors =3D (header.size / BDRV_SECTOR_SIZE) as i64; + + this.l1_offset =3D header.l1_table_offset; + this.l1_bits =3D this.cluster_bits + this.l2_bits; + + if header.l1_size > MAX_L1_SIZE / 8 { + return Err(String::from("Active L1 table too large")); + } + + let min_l1_size =3D (header.size + (1u64 << this.l1_bits) - 1)= >> + this.l1_bits; + if (header.l1_size as u64) < min_l1_size || header.l1_size =3D= =3D 0 { + return Err(String::from("Active L1 table too small")); + } + + this.l1_size =3D header.l1_size; + + this.reftable_offset =3D header.refcount_table_offset; + + reftable_size =3D (header.refcount_table_clusters as u64) << + (this.cluster_bits - 3); + if reftable_size > (MAX_REFTABLE_SIZE as u64) { + return Err(String::from("Refcount table too large")); + } + + this.reftable_size =3D reftable_size as u32; + + let refblock_bits =3D this.cluster_bits + this.refcount_order = - 3; + this.reftable_bits =3D this.cluster_bits + refblock_bits; + this.refblock_size =3D 1u32 << refblock_bits; + } + + /* Read L1 table */ + let mut l1_table =3D Vec::::new(); + l1_table.resize(header.l1_size as usize, 0); + + try_prepend!(file.bdrv_pread(header.l1_table_offset, + vec_as_mut_byte_slice(&mut l1_table)), + "Could not read L1 table"); + + for i in 0..header.l1_size { + l1_table[i as usize] =3D u64::from_be(l1_table[i as usize]); + } + + /* Read reftable */ + let mut reftable =3D Vec::::new(); + reftable.resize(reftable_size as usize, 0); + + try_prepend!(file.bdrv_pread(header.refcount_table_offset, + vec_as_mut_byte_slice(&mut reftable)), + "Could not read refcount table"); + + for i in 0..reftable_size { + reftable[i as usize] =3D u64::from_be(reftable[i as usize]); + } + + /* Read backing file name */ + try_prepend!( + file.bdrv_pread(header.backing_file_offset, + slice_as_mut_byte_slice(&mut cbds.backing_file= )), + "Could not read backing file name"); + cbds.backing_file[header.backing_file_size as usize] =3D 0; + + { + let_mut_bds!(this, cbds); + this.l1_table =3D l1_table; + this.reftable =3D reftable; + } + + Ok(()) + } +} + + +impl BlockDriverState for QCow2BDS { + fn new() -> Self + { + QCow2BDS { + common: BDSCommon::::new(), + + qcow_version: 0, + + cluster_bits: 0, + cluster_size: 0, + cluster_sectors: 0, + + l1_bits: 0, + l1_size: 0, + l2_bits: 0, + l2_size: 0, + + l1_offset: 0, + l1_table: Vec::new(), + + refcount_order: 0, + reftable_bits: 0, + refblock_size: 0, + + reftable_offset: 0, + reftable_size: 0, + reftable: Vec::new(), + + first_free_cluster_offset: 0, + } + } + + /* Required for the generic BlockDriverState implementation */ + fn common(&mut self) -> &mut BDSCommon + { + &mut self.common + } +} + + +impl BlockDriverOpen for QCow2BDS { + fn bdrv_open(cbds: &mut CBDS, options: QDict, flags: u32) + -> Result<(), String> + { + let role =3D bdrv_get_standard_child_role(StandardChildRole::File); + let file =3D try!(bdrv_open_child(None, Some(options), + String::from("file"), cbds, role, + false)); + + { + let_mut_bds!(this, cbds); + this.common.set_file(Some(file)); + } + + cbds.read_only =3D true; + + QCow2BDS::do_open(cbds, options, flags) + } +} + + +impl BlockDriverClose for QCow2BDS { + fn bdrv_close(_: &mut CBDS) + { + } +} + + +impl BlockDriverRead for QCow2BDS { + fn bdrv_co_preadv(cbds: &mut CBDS, offset: u64, bytes: u64, + iov: Vec<&mut [u8]>, flags: u32) + -> Result<(), IOError> + { + /* TODO: Do not split */ + Self::split_io_to_clusters(cbds, offset, bytes, io::MNMIOV::Mut(io= v), + flags, &Self::read_cluster) + } +} + + +impl BlockDriverChildPerm for QCow2BDS { + fn bdrv_child_perm(cbds: &mut CBDS, c: Option<&mut BdrvChild>, + role: &c_structs::BdrvChildRole, perm: u64, shared:= u64) + -> (u64, u64) + { + bdrv_format_default_perms(c, role, perm, shared, + bdrv_is_read_only(cbds)) + } +} + + +impl BlockDriverInfo for QCow2BDS { + fn bdrv_get_info(cbds: &mut CBDS, bdi: &mut c_structs::BlockDriverInfo) + -> Result<(), String> + { + let_bds!(this, cbds); + + bdi.unallocated_blocks_are_zero =3D true; + bdi.can_write_zeroes_with_unmap =3D false; /* no discard support */ + bdi.cluster_size =3D this.cluster_size as i32; + /* no VM state support */ + + Ok(()) + } +} + + +#[no_mangle] +pub extern fn bdrv_qcow2_rust_init() +{ + let mut bdrv =3D BlockDriver::::new(String::from("qcow2-rust= ")); + + bdrv.provides_open(); + bdrv.provides_close(); + bdrv.provides_read(); + bdrv.provides_child_perm(); + bdrv.provides_info(); + + bdrv.supports_backing(); + + bdrv_register(bdrv); +} diff --git a/block/rust/src/qcow2/on_disk_structures.rs b/block/rust/src/qc= ow2/on_disk_structures.rs new file mode 100644 index 0000000000..bdc8be7418 --- /dev/null +++ b/block/rust/src/qcow2/on_disk_structures.rs @@ -0,0 +1,59 @@ +/* TODO: Write a derive(Endianness) macro */ + +#[repr(C, packed)] +#[derive(Default)] +pub struct QCow2Header { + pub magic: u32, + pub version: u32, + pub backing_file_offset: u64, + pub backing_file_size: u32, + pub cluster_bits: u32, + pub size: u64, + pub crypt_method: u32, + pub l1_size: u32, + pub l1_table_offset: u64, + pub refcount_table_offset: u64, + pub refcount_table_clusters: u32, + pub nb_snapshots: u32, + pub snapshots_offset: u64, + + pub incompatible_features: u64, + pub compatible_features: u64, + pub autoclear_features: u64, + + pub refcount_order: u32, + pub header_length: u32, +} + + +impl QCow2Header { + pub fn from_be(&mut self) + { + self.magic =3D u32::from_be(self.magic); + self.version =3D u32::from_be(self.version); + + self.backing_file_offset =3D u64::from_be(self.backing_file_off= set); + self.backing_file_size =3D u32::from_be(self.backing_file_siz= e); + + self.cluster_bits =3D u32::from_be(self.cluster_bits); + self.size =3D u64::from_be(self.size); + self.crypt_method =3D u32::from_be(self.crypt_method); + + self.l1_size =3D u32::from_be(self.l1_size); + self.l1_table_offset =3D u64::from_be(self.l1_table_offset); + + self.refcount_table_offset =3D u64::from_be(self.refcount_table_o= ffset); + self.refcount_table_clusters + =3D u32::from_be(self.refcount_table_clusters); + + self.nb_snapshots =3D u32::from_be(self.nb_snapshots); + self.snapshots_offset =3D u64::from_be(self.snapshots_offset= ); + + self.incompatible_features =3D u64::from_be(self.incompatible_fea= tures); + self.compatible_features =3D u64::from_be(self.compatible_featu= res); + self.autoclear_features =3D u64::from_be(self.autoclear_featur= es); + + self.refcount_order =3D u32::from_be(self.refcount_order); + self.header_length =3D u32::from_be(self.header_length); + } +} --=20 2.12.2 From nobody Fri May 3 01:35:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492531342203720.7756831930509; Tue, 18 Apr 2017 09:02:22 -0700 (PDT) Received: from localhost ([::1]:42714 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0Va7-0003KX-Uu for importer@patchew.org; Tue, 18 Apr 2017 12:02:20 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37947) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0VWI-0000Sn-AU for qemu-devel@nongnu.org; Tue, 18 Apr 2017 11:58:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d0VWG-0008Nb-9k for qemu-devel@nongnu.org; Tue, 18 Apr 2017 11:58:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56686) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d0VWB-0008Lq-Tz; Tue, 18 Apr 2017 11:58:16 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AED4BC059742; Tue, 18 Apr 2017 15:58:14 +0000 (UTC) Received: from localhost (ovpn-204-72.brq.redhat.com [10.40.204.72]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5233D71D62; Tue, 18 Apr 2017 15:58:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com AED4BC059742 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mreitz@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com AED4BC059742 From: Max Reitz To: qemu-block@nongnu.org Date: Sat, 1 Apr 2017 17:57:50 +0200 Message-Id: <20170401155751.14322-4-mreitz@redhat.com> In-Reply-To: <20170401155751.14322-1-mreitz@redhat.com> References: <20170401155751.14322-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Tue, 18 Apr 2017 15:58:14 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC for-3.0 3/4] block/qcow2-rust: Add partial write support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , John Snow , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Some people may want to lead you to believe write support may destroy your data. These entirely BASELESS and FALSE accusations are COMPLETE and UTTER LIES because you actually cannot use this driver yet at all, as it does not register itself as a qemu block driver. This is a very modest approach, in contrast to some other LEGACY drivers that do just assume they are worthy of this honor (which of course is utterly wrong, they are INSECURE and NOT SUITED FOR THE JOB) and just want to bury the gem the QEMU project is under a pile of not-greatness. VERY SAD! Also, we do not allocate any new clusters because we do not TAKE from HONEST HARD-WORKING other programs that may want to use disk space, but we just make do with what we have and that is enough! Signed-off-by: Max Reitz --- block/rust/src/qcow2/allocation.rs | 162 +++++++++++++++++++++++++++++++++= ++++ block/rust/src/qcow2/io.rs | 117 +++++++++++++++++++++++++++ block/rust/src/qcow2/mod.rs | 17 +++- block/rust/src/qcow2/refcount.rs | 96 ++++++++++++++++++++++ 4 files changed, 390 insertions(+), 2 deletions(-) create mode 100644 block/rust/src/qcow2/allocation.rs create mode 100644 block/rust/src/qcow2/refcount.rs diff --git a/block/rust/src/qcow2/allocation.rs b/block/rust/src/qcow2/allo= cation.rs new file mode 100644 index 0000000000..9ed975d853 --- /dev/null +++ b/block/rust/src/qcow2/allocation.rs @@ -0,0 +1,162 @@ +use interface::*; +use qcow2::*; +use qcow2::io::*; + + +impl QCow2BDS { + fn allocate_cluster(cbds: &mut CBDS) -> Result + { + let (mut offset, cluster_size) =3D { + let_bds!(this, cbds); + (this.first_free_cluster_offset, this.cluster_size) + }; + + /* TODO: Optimize by scanning whole refblocks */ + while try!(Self::get_refcount(cbds, offset)) > 0 { + offset +=3D cluster_size as u64; + } + + try!(Self::change_refcount(cbds, offset, 1)); + + { + let_mut_bds!(this, cbds); + this.first_free_cluster_offset =3D offset + (cluster_size as u= 64); + } + + Ok(offset) + } + + + pub fn allocate_l2(cbds: &mut CBDS, mut hoi: HostOffsetInfo) + -> Result + { + let offset =3D try!(Self::allocate_cluster(cbds)); + + /* Zero the new table */ + { + let zero_data =3D qemu_blockalign(cbds, hoi.cluster_size as us= ize); + zero_byte_slice(zero_data); + + let res =3D hoi.file.bdrv_pwrite(offset, zero_data); + qemu_vfree(zero_data); + + if let Err(_) =3D res { + return Err(IOError::GenericError); + } + } + + hoi.l1_entry =3D L1Entry::Allocated(offset, true); + try!(Self::update_l1_entry(cbds, &hoi)); + + hoi.l2_entry =3D Some(L2Entry::Unallocated); + + Ok(hoi) + } + + + pub fn allocate_data_cluster(cbds: &mut CBDS, mut hoi: HostOffsetInfo) + -> Result + { + let offset =3D try!(Self::allocate_cluster(cbds)); + + hoi.l2_entry =3D Some(L2Entry::Normal(offset, true)); + hoi =3D try!(Self::update_l2_entry(cbds, hoi)); + + Ok(hoi) + } + + + pub fn free_cluster(cbds: &mut CBDS, l2e: L2Entry) -> Result<(), IOErr= or> + { + match l2e { + L2Entry::Unallocated =3D> Ok(()), + L2Entry::Zero(None, _) =3D> Ok(()), + + L2Entry::Normal(offset, _) + | L2Entry::Zero(Some(offset), _) + | L2Entry::Compressed(offset, _) =3D> { + { + let_mut_bds!(this, cbds); + if offset < this.first_free_cluster_offset { + this.first_free_cluster_offset =3D offset; + } + } + + Self::change_refcount(cbds, offset, -1) + }, + } + } + + + pub fn update_l1_entry(cbds: &mut CBDS, hoi: &HostOffsetInfo) + -> Result<(), IOError> + { + let (l1_offset, l1_size) =3D { + let_bds!(this, cbds); + (this.l1_offset, this.l1_table.len()) + }; + + assert!((hoi.l1_index as usize) < l1_size); + let entry_offset =3D l1_offset + (hoi.l1_index * 8) as u64; + let l1_entry_cpu =3D hoi.l1_entry.to_bits(); + let l1_entry =3D u64::to_be(l1_entry_cpu); + + if let Err(_) =3D hoi.file.bdrv_pwrite(entry_offset, + object_as_byte_slice(&l1_entr= y)) + { + return Err(IOError::GenericError); + } + + let_mut_bds!(this, cbds); + this.l1_table[hoi.l1_index as usize] =3D l1_entry_cpu; + + Ok(()) + } + + + /* hoi.l2_entry must be Some(_) */ + pub fn update_l2_entry(cbds: &mut CBDS, mut hoi: HostOffsetInfo) + -> Result + { + let (l2_offset, copied) =3D match hoi.l1_entry { + L1Entry::Unallocated =3D> panic!("L2 table must be allocated"), + L1Entry::Allocated(o, c) =3D> (o, c), + }; + + let l2_entry =3D u64::to_be(hoi.l2_entry.as_ref().unwrap() + .to_bits(hoi.compressed_shif= t)); + + if copied { + let entry_offset =3D l2_offset + (hoi.l2_index * 8) as u64; + if let Err(_) =3D hoi.file.bdrv_pwrite(entry_offset, + object_as_byte_slice(&l2_= entry)) + { + return Err(IOError::GenericError); + } + } else { + let table_data =3D qemu_blockalign(cbds, hoi.cluster_size as u= size); + + if let Err(_) =3D hoi.file.bdrv_pread(l2_offset, table_data) { + qemu_vfree(table_data); + return Err(IOError::GenericError); + } + + copy_into_byte_slice(table_data, (hoi.l2_index * 8) as usize, + object_as_byte_slice(&l2_entry)); + + let new_offset =3D try_vfree!(Self::allocate_cluster(cbds), + table_data); + let res =3D hoi.file.bdrv_pwrite(new_offset, table_data); + qemu_vfree(table_data); + + if let Err(_) =3D res { + return Err(IOError::GenericError); + } + + hoi.l1_entry =3D L1Entry::Allocated(new_offset, true); + try!(Self::update_l1_entry(cbds, &hoi)); + } + + Ok(hoi) + } +} diff --git a/block/rust/src/qcow2/io.rs b/block/rust/src/qcow2/io.rs index 069cc78303..36556282de 100644 --- a/block/rust/src/qcow2/io.rs +++ b/block/rust/src/qcow2/io.rs @@ -319,4 +319,121 @@ impl QCow2BDS { let hoi =3D try!(Self::find_host_offset(cbds, offset)); Self::do_read_cluster(cbds, &hoi, dest, flags) } + + + /* If necessary. copies a cluster somewhere else (freeing the original + * allocation), overlaying the data with the given slice (if given). */ + fn copy_and_overlay_cluster(cbds: &mut CBDS, mut hoi: HostOffsetInfo, + overlay_opt: Option<&[u8]>, flags: u32) + -> Result<(), IOError> + { + let free_original: L2Entry; + + let mut cluster_data =3D qemu_blockalign(cbds, hoi.cluster_size as= usize); + try_vfree!(Self::do_read_cluster(cbds, &hoi, cluster_data, 0), + cluster_data); + + match hoi.l2_entry { + None =3D> panic!("L2 entry expected"), + + Some(L2Entry::Zero(Some(offset), true)) =3D> { + /* We are going to use this cluster, so do not free it */ + free_original =3D L2Entry::Unallocated; + hoi.l2_entry =3D Some(L2Entry::Normal(offset, true)); + hoi =3D try_vfree!(Self::update_l2_entry(cbds, hoi), + cluster_data); + }, + + Some(unwrapped_entry) =3D> { + free_original =3D unwrapped_entry; + hoi.l2_entry =3D Some(L2Entry::Unallocated); + + hoi =3D try_vfree!(Self::allocate_data_cluster(cbds, hoi), + cluster_data); + } + } + + try_vfree!(Self::free_cluster(cbds, free_original), + cluster_data); + + if let Some(overlay) =3D overlay_opt { + copy_into_byte_slice(cluster_data, hoi.offset_in_cluster as us= ize, + overlay); + } + + /* TODO: Maybe on failure we should try pointing back to the origi= nal + * cluster? */ + hoi.offset_in_cluster =3D 0; + try_vfree!(Self::do_write_cluster(cbds, hoi, cluster_data, flags), + cluster_data); + + qemu_vfree(cluster_data); + + Ok(()) + } + + + fn do_write_cluster(cbds: &mut CBDS, mut hoi: HostOffsetInfo, src: &[u= 8], + flags: u32) + -> Result<(), IOError> + { + /* Try to set COPIED flag */ + match hoi.l2_entry { + Some(L2Entry::Normal(offset, false)) =3D> { + if try!(Self::get_refcount(cbds, offset)) =3D=3D 1 { + hoi.l2_entry =3D Some(L2Entry::Normal(offset, true)); + /* We are going to write directly into this cluster wi= thout + * updating the L2 table, so do it now */ + hoi =3D try!(Self::update_l2_entry(cbds, hoi)); + } + }, + + Some(L2Entry::Zero(Some(offset), false)) =3D> { + if try!(Self::get_refcount(cbds, offset)) =3D=3D 1 { + hoi.l2_entry =3D Some(L2Entry::Zero(Some(offset), true= )); + /* No need to update the L2 table entry now: We will c= all + * copy_and_overlay_cluster() and that will do the upd= ate */ + } + }, + + _ =3D> (), + } + + match hoi.l2_entry { + None =3D> { + hoi =3D try!(Self::allocate_l2(cbds, hoi)); + Self::do_write_cluster(cbds, hoi, src, flags) + }, + + Some(L2Entry::Normal(offset, true)) =3D> { + let full_offset =3D offset + (hoi.offset_in_cluster as u64= ); + if let Err(_) =3D hoi.file.bdrv_pwrite(full_offset, src) { + Err(IOError::GenericError) + } else { + Ok(()) + } + }, + + _ =3D> { + Self::copy_and_overlay_cluster(cbds, hoi, Some(src), flags) + }, + } + } + + + pub fn write_cluster(cbds: &mut CBDS, offset: u64, bytes: u32, + full_src_mnm: &mut MNMIOVSlice, flags: u32) + -> Result<(), IOError> + { + let src =3D match *full_src_mnm { + MNMIOVSlice::Mut(ref mut full_src) =3D> + full_src.split_at(bytes as usize).0, + + MNMIOVSlice::Const(ref mut full_src) =3D> + full_src.split_at(bytes as usize).0, + }; + + let hoi =3D try!(Self::find_host_offset(cbds, offset)); + Self::do_write_cluster(cbds, hoi, src, flags) + } } diff --git a/block/rust/src/qcow2/mod.rs b/block/rust/src/qcow2/mod.rs index 5fb523c93b..6cd413c5cb 100644 --- a/block/rust/src/qcow2/mod.rs +++ b/block/rust/src/qcow2/mod.rs @@ -1,4 +1,6 @@ +mod allocation; mod io; +mod refcount; mod on_disk_structures; =20 =20 @@ -264,8 +266,6 @@ impl BlockDriverOpen for QCow2BDS { this.common.set_file(Some(file)); } =20 - cbds.read_only =3D true; - QCow2BDS::do_open(cbds, options, flags) } } @@ -290,6 +290,18 @@ impl BlockDriverRead for QCow2BDS { } =20 =20 +impl BlockDriverWrite for QCow2BDS { + fn bdrv_co_pwritev(cbds: &mut CBDS, offset: u64, bytes: u64, + iov: Vec<&[u8]>, flags: u32) + -> Result<(), IOError> + { + /* TODO: Do not split */ + Self::split_io_to_clusters(cbds, offset, bytes, io::MNMIOV::Const(= iov), + flags, &Self::write_cluster) + } +} + + impl BlockDriverChildPerm for QCow2BDS { fn bdrv_child_perm(cbds: &mut CBDS, c: Option<&mut BdrvChild>, role: &c_structs::BdrvChildRole, perm: u64, shared:= u64) @@ -325,6 +337,7 @@ pub extern fn bdrv_qcow2_rust_init() bdrv.provides_open(); bdrv.provides_close(); bdrv.provides_read(); + bdrv.provides_write(); bdrv.provides_child_perm(); bdrv.provides_info(); =20 diff --git a/block/rust/src/qcow2/refcount.rs b/block/rust/src/qcow2/refcou= nt.rs new file mode 100644 index 0000000000..4f5e14a9f8 --- /dev/null +++ b/block/rust/src/qcow2/refcount.rs @@ -0,0 +1,96 @@ +use interface::*; +use qcow2::*; + + +impl QCow2BDS { + pub fn get_refcount(cbds: &mut CBDS, offset: u64) -> Result + { + let refblock_index; + let refcount_order; + let cluster_size; + let file; + + let reftable_entry =3D { + let_bds!(this, cbds); + + refcount_order =3D this.refcount_order; + cluster_size =3D this.cluster_size; + file =3D this.common.file(); + + let cluster_index =3D offset >> this.cluster_bits; + let reftable_index =3D offset >> this.reftable_bits; + + refblock_index =3D (cluster_index as u32) & (this.refblock_siz= e - 1); + + if reftable_index >=3D (this.reftable_size as u64) { + 0 + } else { + this.reftable[reftable_index as usize] + } + }; + + let refblock_offset =3D reftable_entry & REFT_OFFSET_MASK; + + if refblock_offset =3D=3D 0 { + return Ok(0); + } + + if (refblock_offset & ((cluster_size - 1) as u64)) !=3D 0 { + return Err(IOError::InvalidMetadata); + } + + assert!(refcount_order <=3D 6); + if refcount_order =3D=3D 6 { + let mut refcount: u64 =3D 0; + let byte_offset =3D (refblock_index * 8) as u64; + if let Err(_) =3D + file.bdrv_pread(refblock_offset + byte_offset, + object_as_mut_byte_slice(&mut refcount)) + { + return Err(IOError::GenericError); + } + Ok(u64::from_be(refcount)) + } else if refcount_order =3D=3D 5 { + let mut refcount: u32 =3D 0; + let byte_offset =3D (refblock_index * 4) as u64; + if let Err(_) =3D + file.bdrv_pread(refblock_offset + byte_offset, + object_as_mut_byte_slice(&mut refcount)) + { + return Err(IOError::GenericError); + } + Ok(u32::from_be(refcount) as u64) + } else if refcount_order =3D=3D 4 { + let mut refcount: u16 =3D 0; + let byte_offset =3D (refblock_index * 2) as u64; + if let Err(_) =3D + file.bdrv_pread(refblock_offset + byte_offset, + object_as_mut_byte_slice(&mut refcount)) + { + return Err(IOError::GenericError); + } + Ok(u16::from_be(refcount) as u64) + } else { + let mut refcount_byte: u8 =3D 0; + let byte_offset =3D (refblock_index >> (3 - refcount_order)) a= s u64; + if let Err(_) =3D + file.bdrv_pread(refblock_offset + byte_offset, + object_as_mut_byte_slice(&mut refcount_byt= e)) + { + return Err(IOError::GenericError); + } + + let mask =3D ((1u16 << (1u8 << refcount_order)) - 1) as u8; + let shift =3D (refblock_index << refcount_order) & 0x7; + + Ok(((refcount_byte >> shift) & mask) as u64) + } + } + + + pub fn change_refcount(_: &mut CBDS, _: u64, _: i8) + -> Result<(), IOError> + { + Err(IOError::UnsupportedImageFeature) + } +} --=20 2.12.2 From nobody Fri May 3 01:35:25 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1492531512438652.7876441839153; Tue, 18 Apr 2017 09:05:12 -0700 (PDT) Received: from localhost ([::1]:42796 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0Vcs-0005TD-EV for importer@patchew.org; Tue, 18 Apr 2017 12:05:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37980) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d0VWO-0000aC-V8 for qemu-devel@nongnu.org; Tue, 18 Apr 2017 11:58:30 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d0VWK-0008PC-2E for qemu-devel@nongnu.org; Tue, 18 Apr 2017 11:58:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:32677) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d0VWH-0008Nq-Fg; Tue, 18 Apr 2017 11:58:21 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5325575EBA; Tue, 18 Apr 2017 15:58:20 +0000 (UTC) Received: from localhost (ovpn-204-72.brq.redhat.com [10.40.204.72]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 90BCD7DE3B; Tue, 18 Apr 2017 15:58:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5325575EBA Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mreitz@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 5325575EBA From: Max Reitz To: qemu-block@nongnu.org Date: Sat, 1 Apr 2017 17:57:51 +0200 Message-Id: <20170401155751.14322-5-mreitz@redhat.com> In-Reply-To: <20170401155751.14322-1-mreitz@redhat.com> References: <20170401155751.14322-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 18 Apr 2017 15:58:20 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC for-3.0 4/4] block/qcow2-rust: Register block driver X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , John Snow , qemu-devel@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The rust qcow2 driver is now actually MUCH BETTER than the LEGACY CROOKED qcow2 driver, so let's not beat around the bush and just register it as a block driver. Has always been my opinion, never said anything different. The QEMU project will deal with the C issue in a decisive way. Q.M.U. Signed-off-by: Max Reitz --- block/Makefile.objs | 1 + block/qcow2-rust.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 block/qcow2-rust.c diff --git a/block/Makefile.objs b/block/Makefile.objs index de96f8ee80..4802946e4e 100644 --- a/block/Makefile.objs +++ b/block/Makefile.objs @@ -24,6 +24,7 @@ block-obj-y +=3D accounting.o dirty-bitmap.o block-obj-y +=3D write-threshold.o block-obj-y +=3D backup.o block-obj-$(CONFIG_REPLICATION) +=3D replication.o +block-obj-y +=3D qcow2-rust.o =20 block-obj-y +=3D crypto.o =20 diff --git a/block/qcow2-rust.c b/block/qcow2-rust.c new file mode 100644 index 0000000000..1465fa9fe1 --- /dev/null +++ b/block/qcow2-rust.c @@ -0,0 +1,38 @@ +/* C basis of the Rust qcow2 block driver + * + * Copyright (C) 2017 Red Hat, Inc. + * + * 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, an= d/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 DEA= LINGS + * IN THE SOFTWARE. + */ + +#include "qemu/osdep.h" +#include "block/block_int.h" +#include "qapi/error.h" +#include "qemu/option.h" + + +/* This is just for the module loading system to detect this driver */ +static BlockDriver _ __attribute__((used)) =3D { + .format_name =3D "qcow2-rust", +}; + + +extern void bdrv_qcow2_rust_init(void); + +block_init(bdrv_qcow2_rust_init); --=20 2.12.2