From nobody Fri Apr 4 21:13:25 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1739902975; cv=none; d=zohomail.com; s=zohoarc; b=WZvJNw97Kw3uwYoy/7Mb9+IpgLAEEHOWRpBVzMWOXZAHnQ+cUsbtg7NJ9Fw0mZQ9SM8QElt3TNma5YFgk1sqO0xZnWU8itQ6Y3RdUSKpEMxWZNXh+yWUE7ugRpJN+XA7J3vzNrZNrWziwqRT+rNdyNGx89MIDcusln/KcYJN4Vg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1739902975; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=qbJRLhkTHhbgND2eqUgNnvV/fnQHqoFQy8tGkOv51fc=; b=Q8HDiTlkHZBKEoHXasZw0DxpN1feTmOU+NcdiYkxLOvaKxg0A1x/3fsS1/ePdJ25hoF9dg54Gex+u5pSNKiyyr6pmxSgiXnhHgUr2qYTGEtKtNV8ruON4ZAoWptJfyAyits3sG08cQJgyXQzen6pGIcV3JZkwC35+EuN1DM52BE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1739902975169138.57725505223755; Tue, 18 Feb 2025 10:22:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tkSEt-0007Lg-FP; Tue, 18 Feb 2025 13:22:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tkSDX-000645-5t for qemu-devel@nongnu.org; Tue, 18 Feb 2025 13:21:12 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tkSDU-0007da-Nl for qemu-devel@nongnu.org; Tue, 18 Feb 2025 13:21:10 -0500 Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-299-8myATExoOACfc_2Jg-DQmA-1; Tue, 18 Feb 2025 13:21:03 -0500 Received: from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3B14F196E078; Tue, 18 Feb 2025 18:21:02 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.45.226.66]) by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id ACA2E180034D; Tue, 18 Feb 2025 18:20:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739902868; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=qbJRLhkTHhbgND2eqUgNnvV/fnQHqoFQy8tGkOv51fc=; b=KDHzY5dwIVwOO6YRR+keyiES0s9kjr1LTCzt89bOoqz/U+rtJNS4ymkZ83Ou68vauEkKc7 m0HxVm2U4CSZnSwfdN6pADlsk60gmaYRAtrM4iNKxQQ6mtPnyY7wWYlWadAxmXnU/Dz46B OM1wEiNoAr+oM2kR6YOZJwuZ2JP85xw= X-MC-Unique: 8myATExoOACfc_2Jg-DQmA-1 X-Mimecast-MFC-AGG-ID: 8myATExoOACfc_2Jg-DQmA_1739902862 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, hreitz@redhat.com, pbonzini@redhat.com, manos.pitsidianakis@linaro.org, philmd@linaro.org, qemu-devel@nongnu.org, qemu-rust@nongnu.org Subject: [PATCH v2 11/11] rust/block: Add format probing Date: Tue, 18 Feb 2025 19:20:19 +0100 Message-ID: <20250218182019.111467-12-kwolf@redhat.com> In-Reply-To: <20250218182019.111467-1-kwolf@redhat.com> References: <20250218182019.111467-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.111 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -24 X-Spam_score: -2.5 X-Spam_bar: -- X-Spam_report: (-2.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.423, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1739902977036019100 Content-Type: text/plain; charset="utf-8" This adds format probing both to the BlockDriver trait and the bochs-rs block driver. With this, bochs-rs achieves feature parity with its C counterpart. Its probe function returns a higher priority so that it is preferred when both drivers are available. Signed-off-by: Kevin Wolf --- rust/block/src/bochs.rs | 20 ++++++++++++++++++++ rust/block/src/driver.rs | 26 +++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/rust/block/src/bochs.rs b/rust/block/src/bochs.rs index 9dd84446e1..b1247a6bd5 100644 --- a/rust/block/src/bochs.rs +++ b/rust/block/src/bochs.rs @@ -188,6 +188,26 @@ pub async fn new(file: BdrvChild) -> io::Result { impl BlockDriver for BochsImage { type Options =3D bindings::BlockdevOptionsGenericFormat; =20 + fn probe(buf: &[u8], _filename: &str) -> u16 { + let header =3D match BochsHeader::from_byte_slice(buf) { + Some(header) =3D> header, + None =3D> return 0, + }; + + if header.magic !=3D HEADER_MAGIC + || header.imgtype !=3D HEADER_TYPE_REDOLOG + || header.subtype !=3D HEADER_SUBTYPE_GROWING + { + return 0; + } + + // This driver is better than the C one which returns 100, give it= priority + match header.version { + HEADER_VERSION | HEADER_V1 =3D> 200, + _ =3D> 0, + } + } + unsafe fn parse_options( v: &mut bindings::Visitor, opts: &mut *mut Self::Options, diff --git a/rust/block/src/driver.rs b/rust/block/src/driver.rs index baeaf47eda..1b132bc8de 100644 --- a/rust/block/src/driver.rs +++ b/rust/block/src/driver.rs @@ -7,7 +7,7 @@ use qemu_api::errno::Errno; use qemu_api::futures::qemu_co_run_future; use std::cmp::min; -use std::ffi::c_void; +use std::ffi::{c_void, CStr}; use std::io::{self, Error, ErrorKind}; use std::mem::MaybeUninit; use std::ptr; @@ -65,6 +65,16 @@ unsafe fn open( errp: *mut *mut bindings::Error, ) -> std::os::raw::c_int; =20 + /// Returns the image format probing priority of this block driver for= disk images starting + /// with the byte sequence in `buf`. Probing selects the driver that r= eturns the highest + /// number. + /// + /// If the driver doesn't support images starting with `buf`, 0 is ret= urned. + fn probe(buf: &[u8], filename: &str) -> u16 { + let _ =3D (buf, filename); + 0 + } + /// Returns the size of the image in bytes fn size(&self) -> u64; =20 @@ -161,6 +171,19 @@ pub async fn read_uninit( } } =20 +#[doc(hidden)] +pub unsafe extern "C" fn bdrv_probe( + buf: *const u8, + buf_size: std::os::raw::c_int, + filename: *const std::os::raw::c_char, +) -> std::os::raw::c_int { + let buf =3D unsafe { std::slice::from_raw_parts(buf, buf_size as usize= ) }; + match unsafe { CStr::from_ptr(filename) }.to_str() { + Ok(filename) =3D> D::probe(buf, filename).into(), + Err(_) =3D> 0, + } +} + #[doc(hidden)] pub unsafe extern "C" fn bdrv_open( bs: *mut bindings::BlockDriverState, @@ -266,6 +289,7 @@ macro_rules! block_driver { ::qemu_api::bindings::BlockDriver { format_name: ::qemu_api::c_str!($fmtname).as_ptr(), instance_size: ::std::mem::size_of::<$typ>() as i32, + bdrv_probe: Some($crate::driver::bdrv_probe::<$typ>), bdrv_open: Some($crate::driver::bdrv_open::<$typ>), bdrv_close: Some($crate::driver::bdrv_close::<$typ>), bdrv_co_preadv_part: Some($crate::driver::bdrv_co_prea= dv_part::<$typ>), --=20 2.48.1