From nobody Sun Nov 24 11:41:55 2024 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=1722892241; cv=none; d=zohomail.com; s=zohoarc; b=mSsSEwegScMyGRvqt6qY4x1fNoeZKR0bwANygnmqTP8w93UtPzHkDdPGohh7TxCRIQhqiIumPJ04KfebM/BNwv8G7DBRC+G/17wb5ZF8V82Q6k7SbNAsSZLqvZnmPfyXCL8aBsDvWyynOoJYBSLKB2OOA0RYvdXSGaxF7xbmdb4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892241; 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=sS+r8IfbbmrpLEZ6oy4QNDwezTjm3o7MWffy1eZKmxM=; b=S9F8GyhJcd2n7+/kDeNeMuIkbY3NXtfxRrM93dHIPG4rQmMU2iYuKAcYoaevZBOZyb1V08uHZWHxbHQl7vx4zUpRGaGiFdwZywCR5kJIXqqWWASA1cwpX22zJLdatzJSq42/vbOvq61lDAdIFNdjXe+J+tIViE1cW20pdQMiTUw= 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 172289224143440.69633730361352; Mon, 5 Aug 2024 14:10:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xD-0008UD-Ic; Mon, 05 Aug 2024 17:09:19 -0400 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 1sb4xC-0008TF-3c for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sb4x3-0008Sl-Cs for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:17 -0400 Received: from mx-prod-mc-04.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-74-0_6T_KrHNv-hRziup85vZw-1; Mon, 05 Aug 2024 17:09:05 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 24BF41955D42; Mon, 5 Aug 2024 21:09:04 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 54D8830001AA; Mon, 5 Aug 2024 21:08:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892146; 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=sS+r8IfbbmrpLEZ6oy4QNDwezTjm3o7MWffy1eZKmxM=; b=NzGmYSDXItasZ6UjxnKiAengyvX4vBK2xRXaoEB+8y1yUBJQ/yFteaNN1v3WEjsf4NPORs e1tdCWSgddmgQ55lWcAttvjJqHN73fA+MFO2JodznIcljnBR9a3ASrmvfc6yaB+5w0XU0h /FSmtn2A1slHMCNEr96x91V7zirTxcA= X-MC-Unique: 0_6T_KrHNv-hRziup85vZw-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 01/13] qapi-block-core: Clean up blockdev-snapshot-internal-sync doc Date: Mon, 5 Aug 2024 23:08:39 +0200 Message-ID: <20240805210851.314076-2-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: 1722892242850116601 Content-Type: text/plain; charset="utf-8" From: Markus Armbruster BlockdevSnapshotInternal is the arguments type of command blockdev-snapshot-internal-sync. Its doc comment contains this note: # .. note:: In a transaction, if @name is empty or any snapshot matching # @name exists, the operation will fail. Only some image formats # support it; for example, qcow2, and rbd. "In a transaction" is misleading, and "if @name is empty or any snapshot matching @name exists, the operation will fail" is redundant with the command's Errors documentation. Drop. The remainder is fine. Move it to the command's doc comment, where it is more prominently visible, with a slight rephrasing for clarity. Signed-off-by: Markus Armbruster Message-ID: <20240718123609.3063055-1-armbru@redhat.com> Reviewed-by: Kevin Wolf Signed-off-by: Kevin Wolf --- qapi/block-core.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index f400b334c8..994e384a71 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -6046,10 +6046,6 @@ # # @name: the name of the internal snapshot to be created # -# .. note:: In a transaction, if @name is empty or any snapshot matching -# @name exists, the operation will fail. Only some image formats -# support it; for example, qcow2, and rbd. -# # Since: 1.7 ## { 'struct': 'BlockdevSnapshotInternal', @@ -6070,6 +6066,9 @@ # - If the format of the image used does not support it, # GenericError # +# .. note:: Only some image formats such as qcow2 and rbd support +# internal snapshots. +# # Since: 1.7 # # .. qmp-example:: --=20 2.45.2 From nobody Sun Nov 24 11:41:55 2024 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=1722892187; cv=none; d=zohomail.com; s=zohoarc; b=l2IzmT+hZ4uJ6lNyY6GgtuWqqw5/Ihr+NOxqMF8heHPZo841bOpspXQMv03/BKrmEVtV1RNkWo1ZX6oH2N8cZ4vygT1n9CVIQwzNuFPvKGuFbyjzIEYGl7IotUcZT/c6tAAnU6M9ufAUZ5ugYzFMI8zmD3v8Z4lXzuk2rEaeURM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892187; 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=/g0sHsnAjTQQwqSUZTxHOBU5uq4XarRAoERO1KomibQ=; b=PxsqXASMgaBBkBu08Vvla+/GBPheiOJK98S/7nASaMtF3isokq5hwbB5npwBM6kAlLzCPN5hiXUaXvqTDaMieo8hvTn4VEp6CWq5MhBp1qB1uy9F/zUBoUeXIYi5TR9F1xA/LXHJzVa4mtvCds7Xu3CblQLcuqRI0n9oSbqCLa8= 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 1722892187514328.93449690408886; Mon, 5 Aug 2024 14:09:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xF-0000Cu-BS; Mon, 05 Aug 2024 17:09:21 -0400 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 1sb4xD-0008UR-HT for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:19 -0400 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 1sb4xB-0008T4-26 for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:19 -0400 Received: from mx-prod-mc-03.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-53-Ad9bJgZdO5CgvRA1zr2OJg-1; Mon, 05 Aug 2024 17:09:06 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C508519560B1; Mon, 5 Aug 2024 21:09:05 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6BE2530001B7; Mon, 5 Aug 2024 21:09:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892149; 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=/g0sHsnAjTQQwqSUZTxHOBU5uq4XarRAoERO1KomibQ=; b=JTFOksgqMtxNAzFLqmkE66g70lqoRzCXgg2hkSrHEoe3zZp+eEgT45hKyv1+Q+0MKLJw+9 bTuHdD0aDZTIu23nanQUDWSEw02dQQ8Q05T1DJGjVXgY949gxBmUCeLHOXR6+TRTFBwBwO UljPYIRWcuq5nOYsAmaB7Orbx0Yvfb4= X-MC-Unique: Ad9bJgZdO5CgvRA1zr2OJg-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 02/13] block-copy: Fix missing graph lock Date: Mon, 5 Aug 2024 23:08:40 +0200 Message-ID: <20240805210851.314076-3-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H3=0.001, RCVD_IN_MSPIKE_WL=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: 1722892188613116600 Content-Type: text/plain; charset="utf-8" The graph lock needs to be held when calling bdrv_co_pdiscard(). Fix block_copy_task_entry() to take it for the call. WITH_GRAPH_RDLOCK_GUARD() was implemented in a weak way because of limitations in clang's Thread Safety Analysis at the time, so that it only asserts that the lock is held (which allows calling functions that require the lock), but we never deal with the unlocking (so even after the scope of the guard, the compiler assumes that the lock is still held). This is why the compiler didn't catch this locking error. Signed-off-by: Kevin Wolf Message-ID: <20240627181245.281403-2-kwolf@redhat.com> Reviewed-by: Stefan Hajnoczi Signed-off-by: Kevin Wolf --- block/block-copy.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/block/block-copy.c b/block/block-copy.c index 7e3b378528..cc618e4561 100644 --- a/block/block-copy.c +++ b/block/block-copy.c @@ -595,7 +595,9 @@ static coroutine_fn int block_copy_task_entry(AioTask *= task) if (s->discard_source && ret =3D=3D 0) { int64_t nbytes =3D MIN(t->req.offset + t->req.bytes, s->len) - t->req.offset; - bdrv_co_pdiscard(s->source, t->req.offset, nbytes); + WITH_GRAPH_RDLOCK_GUARD() { + bdrv_co_pdiscard(s->source, t->req.offset, nbytes); + } } =20 return ret; --=20 2.45.2 From nobody Sun Nov 24 11:41:55 2024 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=1722892222; cv=none; d=zohomail.com; s=zohoarc; b=H85cu47qbe5OVy8AS+KnmGqPiv384a1ojUkm0OZJUacH1xmfVagsT7miXB5/eypm0aSRQMw/BjVDfXPugt1+XwIkdZdpQDNceiojPUpRprZE6bOqMc9mOYBSlH9dxXrImw8r5W+Ik6IDbBc+cPlzRNR0QpN0jXHFFqbiSzV8b9E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892222; 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=DUWqcmXjQgVAIGwNEeavlkl8JSNFY7ZhrAygvXVLmWM=; b=Gbg61IqrtDp15h1mdXWqdKEXd66o5U57nKr1D3WaEfSFH+AVujwbK1cwUjr92aC2K/gMxGzC2flJ/P5U2Wqqz79HoehNx8Y6HROanPGPpvywQN7Zai33X9m3nfqPE1JHcla1bLi66IaVrM1RGlGWZt6hIr6Qw1CSqfNbRI4Jbrw= 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 1722892221998142.23780414320595; Mon, 5 Aug 2024 14:10:21 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xJ-0000aU-EI; Mon, 05 Aug 2024 17:09:25 -0400 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 1sb4xF-0000CS-9J for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:21 -0400 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 1sb4xB-0008TC-4L for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:20 -0400 Received: from mx-prod-mc-03.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-231-Bg7X1643MqyWud0Ek7PUDA-1; Mon, 05 Aug 2024 17:09:08 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 693741956064; Mon, 5 Aug 2024 21:09:07 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1BC0330001AA; Mon, 5 Aug 2024 21:09:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892151; 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=DUWqcmXjQgVAIGwNEeavlkl8JSNFY7ZhrAygvXVLmWM=; b=AxAOSJB7Px1qxh33sDjvT/T6o3YRj7xO9YWtyAdHmsfknppDG+HtSacAJgkYJSridANAaV tOEGM+XIPK+nUnO/voIJc9RqWNIWu81z7R7OkuaunIJ/HBzMQ7BsyHjEK7OL5unmxKXoMu paHyOby7bQiDDeIfdwROolO6hgHZp9M= X-MC-Unique: Bg7X1643MqyWud0Ek7PUDA-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 03/13] block/graph-lock: Make WITH_GRAPH_RDLOCK_GUARD() fully checked Date: Mon, 5 Aug 2024 23:08:41 +0200 Message-ID: <20240805210851.314076-4-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: 1722892222744116600 Content-Type: text/plain; charset="utf-8" Upstream clang 18 (and backports to clang 17 in Fedora and RHEL) implemented support for __attribute__((cleanup())) in its Thread Safety Analysis, so we can now actually have a proper implementation of WITH_GRAPH_RDLOCK_GUARD() that understands when we acquire and when we release the lock. -Wthread-safety is now only enabled if the compiler is new enough to understand this pattern. In theory, we could have used some #ifdefs to keep the existing basic checks on old compilers, but as long as someone runs a newer compiler (and our CI does), we will catch locking problems, so it's probably not worth keeping multiple implementations for this. The implementation can't use g_autoptr any more because the glib macros define wrapper functions that don't have the right TSA attributes, so the compiler would complain about them. Just use the cleanup attribute directly instead. Signed-off-by: Kevin Wolf Message-ID: <20240627181245.281403-3-kwolf@redhat.com> Reviewed-by: Stefan Hajnoczi Reviewed-by: Manos Pitsidianakis Signed-off-by: Kevin Wolf --- include/block/graph-lock.h | 21 ++++++++++++++------- meson.build | 14 +++++++++++++- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/include/block/graph-lock.h b/include/block/graph-lock.h index d7545e82d0..dc8d949184 100644 --- a/include/block/graph-lock.h +++ b/include/block/graph-lock.h @@ -209,31 +209,38 @@ typedef struct GraphLockable { } GraphLockable; * unlocked. TSA_ASSERT_SHARED() makes sure that the following calls know = that * we hold the lock while unlocking is left unchecked. */ -static inline GraphLockable * TSA_ASSERT_SHARED(graph_lock) TSA_NO_TSA cor= outine_fn +static inline GraphLockable * TSA_ACQUIRE_SHARED(graph_lock) coroutine_fn graph_lockable_auto_lock(GraphLockable *x) { bdrv_graph_co_rdlock(); return x; } =20 -static inline void TSA_NO_TSA coroutine_fn -graph_lockable_auto_unlock(GraphLockable *x) +static inline void TSA_RELEASE_SHARED(graph_lock) coroutine_fn +graph_lockable_auto_unlock(GraphLockable **x) { bdrv_graph_co_rdunlock(); } =20 -G_DEFINE_AUTOPTR_CLEANUP_FUNC(GraphLockable, graph_lockable_auto_unlock) +#define GRAPH_AUTO_UNLOCK __attribute__((cleanup(graph_lockable_auto_unloc= k))) =20 +/* + * @var is only used to break the loop after the first iteration. + * @unlock_var can't be unlocked and then set to NULL because TSA wants th= e lock + * to be held at the start of every iteration of the loop. + */ #define WITH_GRAPH_RDLOCK_GUARD_(var) = \ - for (g_autoptr(GraphLockable) var =3D graph_lockable_auto_lock(GML_OBJ= _()); \ + for (GraphLockable *unlock_var GRAPH_AUTO_UNLOCK =3D = \ + graph_lockable_auto_lock(GML_OBJ_()), = \ + *var =3D unlock_var; = \ var; = \ - graph_lockable_auto_unlock(var), var =3D NULL) + var =3D NULL) =20 #define WITH_GRAPH_RDLOCK_GUARD() \ WITH_GRAPH_RDLOCK_GUARD_(glue(graph_lockable_auto, __COUNTER__)) =20 #define GRAPH_RDLOCK_GUARD(x) \ - g_autoptr(GraphLockable) \ + GraphLockable * GRAPH_AUTO_UNLOCK \ glue(graph_lockable_auto, __COUNTER__) G_GNUC_UNUSED =3D \ graph_lockable_auto_lock(GML_OBJ_()) =20 diff --git a/meson.build b/meson.build index 97f63aa86c..c2a050b844 100644 --- a/meson.build +++ b/meson.build @@ -649,7 +649,19 @@ warn_flags =3D [ ] =20 if host_os !=3D 'darwin' - warn_flags +=3D ['-Wthread-safety'] + tsa_has_cleanup =3D cc.compiles(''' + struct __attribute__((capability("mutex"))) mutex {}; + void lock(struct mutex *m) __attribute__((acquire_capability(m))); + void unlock(struct mutex *m) __attribute__((release_capability(m))); + + void test(void) { + struct mutex __attribute__((cleanup(unlock))) m; + lock(&m); + } + ''', args: ['-Wthread-safety', '-Werror']) + if tsa_has_cleanup + warn_flags +=3D ['-Wthread-safety'] + endif endif =20 # Set up C++ compiler flags --=20 2.45.2 From nobody Sun Nov 24 11:41:55 2024 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=1722892271; cv=none; d=zohomail.com; s=zohoarc; b=MPAwIjXq3rgXH4nEw3Rf8wsXqTyPdryjwhmTf4R6WeMuWqgG0ZakJziMjuNYD2eUT1NBT8k9q0dNMZWojp9EC7mN2wh7TJ/P4xhXrTNp0sZ9JXRggf8lDXs/mZauoEeGD/PUMy2GlFx8tCR8QHPyHkJttVRCQBB06E0BiGWlYdQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892271; 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=zyK8ju1u2MVFTs7Yjc1l/lw6D/rg7JG4csNR18vJ5LQ=; b=g9qH/vkIoOY2FgoHu7ghcUY/0Nh3jMe3cZyDUfen6hk8KNJ4vTpz1CjWgk5rMwlbP+1vuMw7ZyE6EnEHsJT4dVN9wpW+btlgudz51nHpg9KNo7Yuf8Hf6DZ7DiXIDCUUJKc0ZnmR+kZOyuDIOxK6G5AjLWz/sS/IhqOkxw1AR1w= 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 1722892271238611.1320720888851; Mon, 5 Aug 2024 14:11:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xK-0000cp-1R; Mon, 05 Aug 2024 17:09:26 -0400 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 1sb4xG-0000Jx-4s for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:22 -0400 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 1sb4xB-0008TK-4z for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:21 -0400 Received: from mx-prod-mc-04.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-587-zkjWd7XXNYarRqZOBdrxFA-1; Mon, 05 Aug 2024 17:09:10 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DFFAF1955D48; Mon, 5 Aug 2024 21:09:09 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5A54B30001AA; Mon, 5 Aug 2024 21:09:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892153; 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=zyK8ju1u2MVFTs7Yjc1l/lw6D/rg7JG4csNR18vJ5LQ=; b=V417U7dHR2BIyn5Blda7iFsO8/Lem2MKjYRDVQLDnfLcUt6AsljnK5PvvFOy8BiSjsw2cS o1S/99ZoyxwXYUbt3ICo1kNEbzrI8AtBRb4ZkhRa8so4mumWrK1nV9czAh7M5NrgO/t86Q z0FTYqXKGSfEuzh/ty/sZOe2Xyt65Lo= X-MC-Unique: zkjWd7XXNYarRqZOBdrxFA-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 04/13] scsi-disk: Use positive return value for status in dma_readv/writev Date: Mon, 5 Aug 2024 23:08:42 +0200 Message-ID: <20240805210851.314076-5-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H3=0.001, RCVD_IN_MSPIKE_WL=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: 1722892273008116600 Content-Type: text/plain; charset="utf-8" In some error cases, scsi_block_sgio_complete() never calls the passed callback, but directly completes the request. This leads to bugs because its error paths are not exact copies of what the callback would normally do. In preparation to fix this, allow passing positive return values to the callbacks that represent the status code that should be used to complete the request. scsi_handle_rw_error() already handles positive values for its ret parameter because scsi_block_sgio_complete() calls directly into it. Signed-off-by: Kevin Wolf Acked-by: Paolo Bonzini Message-ID: <20240731123207.27636-2-kwolf@redhat.com> Signed-off-by: Kevin Wolf --- hw/scsi/scsi-disk.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index a67092db6a..3ff6798bde 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -65,6 +65,10 @@ OBJECT_DECLARE_TYPE(SCSIDiskState, SCSIDiskClass, SCSI_D= ISK_BASE) =20 struct SCSIDiskClass { SCSIDeviceClass parent_class; + /* + * Callbacks receive ret =3D=3D 0 for success. Errors are represented = either as + * negative errno values, or as positive SAM status codes. + */ DMAIOFunc *dma_readv; DMAIOFunc *dma_writev; bool (*need_fua_emulation)(SCSICommand *cmd); @@ -283,7 +287,7 @@ static bool scsi_disk_req_check_error(SCSIDiskReq *r, i= nt ret, bool acct_failed) return true; } =20 - if (ret < 0) { + if (ret !=3D 0) { return scsi_handle_rw_error(r, ret, acct_failed); } =20 @@ -360,7 +364,7 @@ static void scsi_write_do_fua(SCSIDiskReq *r) static void scsi_dma_complete_noio(SCSIDiskReq *r, int ret) { assert(r->req.aiocb =3D=3D NULL); - if (scsi_disk_req_check_error(r, ret, false)) { + if (scsi_disk_req_check_error(r, ret, ret > 0)) { goto done; } =20 @@ -385,9 +389,10 @@ static void scsi_dma_complete(void *opaque, int ret) assert(r->req.aiocb !=3D NULL); r->req.aiocb =3D NULL; =20 + /* ret > 0 is accounted for in scsi_disk_req_check_error() */ if (ret < 0) { block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); - } else { + } else if (ret =3D=3D 0) { block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); } scsi_dma_complete_noio(r, ret); @@ -403,7 +408,7 @@ static void scsi_read_complete_noio(SCSIDiskReq *r, int= ret) qemu_get_current_aio_context()); =20 assert(r->req.aiocb =3D=3D NULL); - if (scsi_disk_req_check_error(r, ret, false)) { + if (scsi_disk_req_check_error(r, ret, ret > 0)) { goto done; } =20 @@ -424,9 +429,10 @@ static void scsi_read_complete(void *opaque, int ret) assert(r->req.aiocb !=3D NULL); r->req.aiocb =3D NULL; =20 + /* ret > 0 is accounted for in scsi_disk_req_check_error() */ if (ret < 0) { block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); - } else { + } else if (ret =3D=3D 0) { block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); trace_scsi_disk_read_complete(r->req.tag, r->qiov.size); } @@ -534,7 +540,7 @@ static void scsi_write_complete_noio(SCSIDiskReq *r, in= t ret) qemu_get_current_aio_context()); =20 assert (r->req.aiocb =3D=3D NULL); - if (scsi_disk_req_check_error(r, ret, false)) { + if (scsi_disk_req_check_error(r, ret, ret > 0)) { goto done; } =20 @@ -562,9 +568,10 @@ static void scsi_write_complete(void * opaque, int ret) assert (r->req.aiocb !=3D NULL); r->req.aiocb =3D NULL; =20 + /* ret > 0 is accounted for in scsi_disk_req_check_error() */ if (ret < 0) { block_acct_failed(blk_get_stats(s->qdev.conf.blk), &r->acct); - } else { + } else if (ret =3D=3D 0) { block_acct_done(blk_get_stats(s->qdev.conf.blk), &r->acct); } scsi_write_complete_noio(r, ret); --=20 2.45.2 From nobody Sun Nov 24 11:41:55 2024 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=1722892218; cv=none; d=zohomail.com; s=zohoarc; b=UdK0zrc8Kp5HDpC8i3CDUDHQMJO5ZKgHOXx+/h1I6kw6hEZvvdIwdcD9DxJH6sELFBznX7/jQ1F2cpvjnnerI3bXGLQLG5GaJaoOiVSEF4xj+aWUMKM+SxTllSUruso6o1CP3tUQ8138x38UuD4outYgqUsGkpavbnZNtWutI5M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892218; 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=8S4R7oZAhxgp9+WrqCsInnag+q6eChG8pRT5Mw+3qaA=; b=IdQuLaI1alGdHPT/jk7U8uRwoHyID+J2j8rb86Q19JXtxSv3VdospG4d47sCicdKCDK59SazeMfSHaIxKcqVHESNOSzj1nknyD6JBbn+8ttEngK1Ylc+NKScg8DP+rWteqiz9KFw+xopIYZW+2aQoBuHXu/5IYkyn+n2V1aVO/w= 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 1722892218675826.6087955262765; Mon, 5 Aug 2024 14:10:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xJ-0000Z3-Cz; Mon, 05 Aug 2024 17:09:25 -0400 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 1sb4xF-00009t-3J for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sb4xB-0008TO-4j for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:20 -0400 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-228-oO8LGGZzOMeggQp4xQtN5A-1; Mon, 05 Aug 2024 17:09:12 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 68A101955D44; Mon, 5 Aug 2024 21:09:11 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EF50D30001AA; Mon, 5 Aug 2024 21:09:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892154; 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=8S4R7oZAhxgp9+WrqCsInnag+q6eChG8pRT5Mw+3qaA=; b=bSSckfScUPXnn6e9MBnz9qRJ+v2CpzJcdXI5Ae7mcJ/+NwFxB7z3tUUdr/NS1lWdDLw+Ss hHinHickllZvZBMio2wViClNmk4XRzIqspPDxyjb4pll/k6JYO9J9NEuRARobb7ygYxnpS zEH7B8KSW6olmAK0u3sOfsjBE98JSYw= X-MC-Unique: oO8LGGZzOMeggQp4xQtN5A-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 05/13] scsi-block: Don't skip callback for sgio error status/driver_status Date: Mon, 5 Aug 2024 23:08:43 +0200 Message-ID: <20240805210851.314076-6-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: 1722892220789116600 Content-Type: text/plain; charset="utf-8" Instead of calling into scsi_handle_rw_error() directly from scsi_block_sgio_complete() and skipping the normal callback, go through the normal cleanup path by calling the callback with a positive error value. The important difference here is not only that the code path is cleaner, but that the callbacks set r->req.aiocb =3D NULL. If we skip setting this and the error action is BLOCK_ERROR_ACTION_STOP, resuming the VM runs into an assertion failure in scsi_read_data() or scsi_write_data() because the dangling aiocb pointer is unexpected. Fixes: a108557bbf ("scsi: inline sg_io_sense_from_errno() into the callers.= ") Buglink: https://issues.redhat.com/browse/RHEL-50000 Signed-off-by: Kevin Wolf Acked-by: Paolo Bonzini Message-ID: <20240731123207.27636-3-kwolf@redhat.com> Signed-off-by: Kevin Wolf --- hw/scsi/scsi-disk.c | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 3ff6798bde..6e1a5c98df 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -2832,16 +2832,6 @@ static void scsi_block_sgio_complete(void *opaque, i= nt ret) } else { ret =3D io_hdr->status; } - - if (ret > 0) { - if (scsi_handle_rw_error(r, ret, true)) { - scsi_req_unref(&r->req); - return; - } - - /* Ignore error. */ - ret =3D 0; - } } =20 req->cb(req->cb_opaque, ret); --=20 2.45.2 From nobody Sun Nov 24 11:41:55 2024 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=1722892247; cv=none; d=zohomail.com; s=zohoarc; b=mchNTeltbwWz2lrYd6ERpVeHqEezYfJ+3yDZDCjvcSU6q2z7DHX6VOveOdgAZGp5NPEu0DwXqQbpI8Rhtn71SqVxFU5SHRtHA1pP1N+9L0ru8pEqtL7wzhPavZ0K5C7HH/bZKHLjqAH1YygWNUUOjb6Y9acoo1DMl1e4I8AxnuI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892247; 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=efcBaXWkHAOOawEycsLB2uZn397zNxBIPFnI8W1gVfM=; b=PgiE5Ml0AsIos5ahZ+T6UmDse6W+n5zA99NirOmMYOHIcfB4+XxYJEVrislm6PXDdBwqYS25gyhrNcXGiSs33qm10QbtP6HrDVF422u6w/PLr19ENDjnVfjnEotlBpuLB/SvjcVDIYkAZKl4kIrhP23lz27cbkWK1BLus2GXzik= 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 1722892247982321.21624059749183; Mon, 5 Aug 2024 14:10:47 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xH-0000Mb-6Q; Mon, 05 Aug 2024 17:09:23 -0400 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 1sb4xD-0008VX-Ok for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:19 -0400 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 1sb4xB-0008Ta-39 for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:19 -0400 Received: from mx-prod-mc-03.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-261-3Nu1YoXkMtS0oV2URi73-A-1; Mon, 05 Aug 2024 17:09:13 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 214B61955D44; Mon, 5 Aug 2024 21:09:13 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9001730001AA; Mon, 5 Aug 2024 21:09:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892156; 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=efcBaXWkHAOOawEycsLB2uZn397zNxBIPFnI8W1gVfM=; b=JomunbDWVhMxT3lZOhrhK+wEXQqdAagJkjd1Jy5cH377fph1+/yflMuJs4bEYgkFJkapzF JK0NdwkjgHW+UhhwkADkRvbbagIlOarrXBY0gtX2wCsyHov6m36fcM+92ctsAJMv0xJLLp zTsXhl1pwHvK3EDbfScG9nExwPU3XKo= X-MC-Unique: 3Nu1YoXkMtS0oV2URi73-A-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 06/13] scsi-disk: Add warning comments that host_status errors take a shortcut Date: Mon, 5 Aug 2024 23:08:44 +0200 Message-ID: <20240805210851.314076-7-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: 1722892248952116600 Content-Type: text/plain; charset="utf-8" scsi_block_sgio_complete() has surprising behaviour in that there are error cases in which it directly completes the request and never calls the passed callback. In the current state of the code, this doesn't seem to result in bugs, but with future code changes, we must be careful to never rely on the callback doing some cleanup until this code smell is fixed. For now, just add warnings to make people aware of the trap. Signed-off-by: Kevin Wolf Acked-by: Paolo Bonzini Message-ID: <20240731123207.27636-4-kwolf@redhat.com> Signed-off-by: Kevin Wolf --- hw/scsi/scsi-disk.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 6e1a5c98df..69a195177e 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -68,6 +68,9 @@ struct SCSIDiskClass { /* * Callbacks receive ret =3D=3D 0 for success. Errors are represented = either as * negative errno values, or as positive SAM status codes. + * + * Beware: For errors returned in host_status, the function may direct= ly + * complete the request and never call the callback. */ DMAIOFunc *dma_readv; DMAIOFunc *dma_writev; @@ -381,6 +384,7 @@ done: scsi_req_unref(&r->req); } =20 +/* May not be called in all error cases, don't rely on cleanup here */ static void scsi_dma_complete(void *opaque, int ret) { SCSIDiskReq *r =3D (SCSIDiskReq *)opaque; @@ -421,6 +425,7 @@ done: scsi_req_unref(&r->req); } =20 +/* May not be called in all error cases, don't rely on cleanup here */ static void scsi_read_complete(void *opaque, int ret) { SCSIDiskReq *r =3D (SCSIDiskReq *)opaque; @@ -560,6 +565,7 @@ done: scsi_req_unref(&r->req); } =20 +/* May not be called in all error cases, don't rely on cleanup here */ static void scsi_write_complete(void * opaque, int ret) { SCSIDiskReq *r =3D (SCSIDiskReq *)opaque; @@ -2821,6 +2827,7 @@ static void scsi_block_sgio_complete(void *opaque, in= t ret) sg_io_hdr_t *io_hdr =3D &req->io_header; =20 if (ret =3D=3D 0) { + /* FIXME This skips calling req->cb() and any cleanup in it */ if (io_hdr->host_status !=3D SCSI_HOST_OK) { scsi_req_complete_failed(&r->req, io_hdr->host_status); scsi_req_unref(&r->req); --=20 2.45.2 From nobody Sun Nov 24 11:41:56 2024 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=1722892243; cv=none; d=zohomail.com; s=zohoarc; b=mpWn/jpiQTH2d7xQnsHiBgz01LYK/dN5U/Q/BVb4rLn87Z2zG968AfajUhj+bnxi6/ttYQlS4TSXfZ4vhzN/+WyL1QEHYDPXdFXcSH+3EdIaxYBIAGEvTiFpuPNBg6jY4s1w/UWE4Dup4hx5Ro+Jb2jlxDXWXLq6ioCqn957Fbk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892243; 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=m9VF2CIqCvid5CTeBFniXZul8mw6cXzWbndL0KUUQg0=; b=FZZtRMK+bZXUlfSR7BQLAz9rXLSI3iAU2BVQpsTXVf6KQs5y/eTr0e14azgm47+1TzJAfndwB87NHlvArsbcINAgs9lrYvmAD1CFmU15wnd075SkxFq3kvM1OCWSW4F860bV9dou4sLV8ZrK1ez19PJCH2EVCrCLUJNePBI5fDo= 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 1722892243094391.76503661019103; Mon, 5 Aug 2024 14:10:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xO-00011t-TI; Mon, 05 Aug 2024 17:09:30 -0400 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 1sb4xH-0000TF-To for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:24 -0400 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 1sb4xD-0008U3-5J for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:23 -0400 Received: from mx-prod-mc-01.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-167-yQ_j-ahMN9aUc29x9jAp6A-1; Mon, 05 Aug 2024 17:09:15 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 75EBB1955D56; Mon, 5 Aug 2024 21:09:14 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 301E530001AA; Mon, 5 Aug 2024 21:09:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892158; 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=m9VF2CIqCvid5CTeBFniXZul8mw6cXzWbndL0KUUQg0=; b=DqlhZTgzqrr4co+oZgWHILQXSDNOyvdTVtpaFNANHWAZA71bCk+TcOMVcJfB6mmJ25qyjm yuLbwrXhVs6jjYkIL6VfFtvJp+I3UgxNyrVK7iAlEuX7tvCT7swKrj2GfrVsRwOQAjjR8d ReJKDcxf+CM1MCgm1LTKXNq0eAfjmzk= X-MC-Unique: yQ_j-ahMN9aUc29x9jAp6A-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 07/13] scsi-disk: Always report RESERVATION_CONFLICT to guest Date: Mon, 5 Aug 2024 23:08:45 +0200 Message-ID: <20240805210851.314076-8-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H3=0.001, RCVD_IN_MSPIKE_WL=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: 1722892244850116600 Content-Type: text/plain; charset="utf-8" In the case of scsi-block, RESERVATION_CONFLICT is not a backend error, but indicates that the guest tried to make a request that it isn't allowed to execute. Pass the error to the guest so that it can decide what to do with it. Without this, if we stop the VM in response to a RESERVATION_CONFLICT (as is the default policy in management software such as oVirt or KubeVirt), it can happen that the VM cannot be resumed any more because every attempt to resume it immediately runs into the same error and stops the VM again. One case that expects RESERVATION_CONFLICT errors to be visible in the guest is running the validation tests in Windows 2019's Failover Cluster Manager, which intentionally tries to execute invalid requests to see if they are properly rejected. Buglink: https://issues.redhat.com/browse/RHEL-50000 Signed-off-by: Kevin Wolf Acked-by: Paolo Bonzini Message-ID: <20240731123207.27636-5-kwolf@redhat.com> Signed-off-by: Kevin Wolf --- hw/scsi/scsi-disk.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 69a195177e..4d94b2b816 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -224,7 +224,7 @@ static bool scsi_handle_rw_error(SCSIDiskReq *r, int re= t, bool acct_failed) SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, r->req.dev); SCSIDiskClass *sdc =3D (SCSIDiskClass *) object_get_class(OBJECT(s)); SCSISense sense =3D SENSE_CODE(NO_SENSE); - int error =3D 0; + int error; bool req_has_sense =3D false; BlockErrorAction action; int status; @@ -235,11 +235,35 @@ static bool scsi_handle_rw_error(SCSIDiskReq *r, int = ret, bool acct_failed) } else { /* A passthrough command has completed with nonzero status. */ status =3D ret; - if (status =3D=3D CHECK_CONDITION) { + switch (status) { + case CHECK_CONDITION: req_has_sense =3D true; error =3D scsi_sense_buf_to_errno(r->req.sense, sizeof(r->req.= sense)); - } else { + break; + case RESERVATION_CONFLICT: + /* + * Don't apply the error policy, always report to the guest. + * + * This is a passthrough code path, so it's not a backend erro= r, but + * a response to an invalid guest request. + * + * Windows Failover Cluster validation intentionally sends inv= alid + * requests to verify that reservations work as intended. It is + * crucial that it sees the resulting errors. + * + * Treating a reservation conflict as a guest-side error is ob= vious + * when a pr-manager is in use. Without one, the situation is = less + * clear, but there might be nothing that can be fixed on the = host + * (like in the above example), and we don't want to be stuck = in a + * loop where resuming the VM and retrying the request immedia= tely + * stops it again. So always reporting is still the safer opti= on in + * this case, too. + */ + error =3D 0; + break; + default: error =3D EINVAL; + break; } } =20 @@ -249,8 +273,9 @@ static bool scsi_handle_rw_error(SCSIDiskReq *r, int re= t, bool acct_failed) * are usually retried immediately, so do not post them to QMP and * do not account them as failed I/O. */ - if (req_has_sense && - scsi_sense_buf_is_guest_recoverable(r->req.sense, sizeof(r->req.se= nse))) { + if (!error || (req_has_sense && + scsi_sense_buf_is_guest_recoverable(r->req.sense, + sizeof(r->req.sense= )))) { action =3D BLOCK_ERROR_ACTION_REPORT; acct_failed =3D false; } else { --=20 2.45.2 From nobody Sun Nov 24 11:41:56 2024 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=1722892194; cv=none; d=zohomail.com; s=zohoarc; b=GXQB6GRMc0bfqskgd2SX++SvmsE1PURvC4yrlcLyuwUZ5KguMx2R6Zsrz9gwN/7bwWzizRAJ7PKRHA5VrjYFAtxrtd4QMknrZMdUcGip4QXGp6p/CTRbNaIs+dX4AJzDViuzcx/hRqmWrD4t082rur6C/7HO7jfBDFil3dnjPIg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892194; 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=RHQVwpRuRkRhw1lpa9Co9MTZ+LhW22+O6wkPEE1xR2k=; b=YVffMociGhSfZSiOjo+Onosg94irdTwvCkgBLyuhqowBnRt2hal6bdVgrfSHeB9ZgfLbr9l+WdUfHqUE2OhLKf2t+BhCobKDAD8J1T8+KijtP7Xyu/XN+z3GeyzK0hJRilCDoWtKeFA/7tf/ZVfSEGeTXqTkswen1Hkg0WNd9/I= 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 1722892194268586.6576996026442; Mon, 5 Aug 2024 14:09:54 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xQ-000166-H5; Mon, 05 Aug 2024 17:09:34 -0400 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 1sb4xH-0000T7-T3 for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sb4xD-0008UG-RS for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:23 -0400 Received: from mx-prod-mc-04.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-622-7-BAccifMUK6WwQtbNasaQ-1; Mon, 05 Aug 2024 17:09:17 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1973419560B3; Mon, 5 Aug 2024 21:09:16 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C0D2030001AA; Mon, 5 Aug 2024 21:09:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892159; 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=RHQVwpRuRkRhw1lpa9Co9MTZ+LhW22+O6wkPEE1xR2k=; b=ZK99qDLnY1l6lRDcFLA8HbuqXMFv0Gu0ZySAFTUvg3BMYXuBp0MQuSMTFpw5T0Huf2V1HR fg+A9x56UZuGebc6BosKSO9NJBIVl2R53UzCKk/FR/M58dk5z1c/UNkUcyCWinMiLbRhDL eRt0DwOGCeryNEMKOTzeyAlL7pZwxCc= X-MC-Unique: 7-BAccifMUK6WwQtbNasaQ-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 08/13] vvfat: Fix bug in writing to middle of file Date: Mon, 5 Aug 2024 23:08:46 +0200 Message-ID: <20240805210851.314076-9-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: 1722892194712116600 Content-Type: text/plain; charset="utf-8" From: Amjad Alsharafi Before this commit, the behavior when calling `commit_one_file` for example with `offset=3D0x2000` (second cluster), what will happen is that we won't fetch the next cluster from the fat, and instead use the first cluster for the read operation. This is due to off-by-one error here, where `i=3D0x2000 !< offset=3D0x2000`, thus not fetching the next cluster. Signed-off-by: Amjad Alsharafi Reviewed-by: Kevin Wolf Tested-by: Kevin Wolf Message-ID: Signed-off-by: Kevin Wolf --- block/vvfat.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/block/vvfat.c b/block/vvfat.c index 086fedf474..a67cfa823b 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -2525,8 +2525,9 @@ commit_one_file(BDRVVVFATState* s, int dir_index, uin= t32_t offset) return -1; } =20 - for (i =3D s->cluster_size; i < offset; i +=3D s->cluster_size) + for (i =3D 0; i < offset; i +=3D s->cluster_size) { c =3D modified_fat_get(s, c); + } =20 fd =3D qemu_open_old(mapping->path, O_RDWR | O_CREAT | O_BINARY, 0666); if (fd < 0) { --=20 2.45.2 From nobody Sun Nov 24 11:41:56 2024 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=1722892224; cv=none; d=zohomail.com; s=zohoarc; b=PhjS9jI8l4O2AC5+egbc0yCQn7JYfeaOk6DGfLNUZqJmlJ40Y+HaKxI/PVzFNow8kQnEuO3GUgCvj7G8e+uyg4nRELMkQkugJJVQJq4IIvS84H7z3hxm6Uf0+mRbSB47TolqXdC6tMXXIXMUyDlnb+Q5TE8fWRJLr3iH3rUl6Y0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892224; 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=7fUnXN9vjFAkhcC+JDQXUlQenfUijIgKJLPelVxzZjo=; b=XtajLnpiTNf/RkY7dV5yEOfnA5MAQy1kNvl/n6TdFfh6YAmK0MbFcv5fmjQpZUDppTddVfLHXlufPrZ+Pew+PQqHdmOdbw3UbXTQ+lNg5Kw5XhCckdai9JXYRizc96CM6BFrkcL8hMYxW5jSQ3OFU2TdmPXGbozjpRYsYlRDDUE= 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 1722892224029108.95962977878105; Mon, 5 Aug 2024 14:10:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xt-0003NT-Tx; Mon, 05 Aug 2024 17:10:02 -0400 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 1sb4xJ-0000Z8-1W for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sb4xG-0008VV-LU for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:24 -0400 Received: from mx-prod-mc-03.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-636-IIpnlJlcPuaxNyCw5H5PLA-1; Mon, 05 Aug 2024 17:09:18 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B57AC19560AA; Mon, 5 Aug 2024 21:09:17 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5E96430001AA; Mon, 5 Aug 2024 21:09:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892162; 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=7fUnXN9vjFAkhcC+JDQXUlQenfUijIgKJLPelVxzZjo=; b=X3SOcUSg0q5thCYLlzkDUjy3La2GYhGoGfmqRsHAB+LdQoEGtqeTR8oIS5vXlVDZiTDXyg wOJtv+ysIjjfoP8Xk4zFDNydd/SExvqHKpFrnQyOOWRk3+79e4zFF0XZ/m43yttdw4Dvp/ 1zmdd5f4UAYvHRk0EVDi8sUjfwHMuBc= X-MC-Unique: IIpnlJlcPuaxNyCw5H5PLA-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 09/13] vvfat: Fix usage of `info.file.offset` Date: Mon, 5 Aug 2024 23:08:47 +0200 Message-ID: <20240805210851.314076-10-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H4=0.001, RCVD_IN_MSPIKE_WL=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: 1722892224744116600 Content-Type: text/plain; charset="utf-8" From: Amjad Alsharafi The field is marked as "the offset in the file (in clusters)", but it was being used like this `cluster_size*(nums)+mapping->info.file.offset`, which is incorrect. Signed-off-by: Amjad Alsharafi Reviewed-by: Kevin Wolf Message-ID: <72f19a7903886dda1aa78bcae0e17702ee939262.1721470238.git.amjads= harafi10@gmail.com> Signed-off-by: Kevin Wolf --- block/vvfat.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/block/vvfat.c b/block/vvfat.c index a67cfa823b..cfde468c2e 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -1408,7 +1408,9 @@ read_cluster_directory: =20 assert(s->current_fd); =20 - offset=3Ds->cluster_size*(cluster_num-s->current_mapping->begin)+s= ->current_mapping->info.file.offset; + offset =3D s->cluster_size * + ((cluster_num - s->current_mapping->begin) + + s->current_mapping->info.file.offset); if(lseek(s->current_fd, offset, SEEK_SET)!=3Doffset) return -3; s->cluster=3Ds->cluster_buffer; @@ -1929,8 +1931,9 @@ get_cluster_count_for_direntry(BDRVVVFATState* s, dir= entry_t* direntry, const ch (mapping->mode & MODE_DIRECTORY) =3D=3D 0) { =20 /* was modified in qcow */ - if (offset !=3D mapping->info.file.offset + s->cluster= _size - * (cluster_num - mapping->begin)) { + if (offset !=3D s->cluster_size + * ((cluster_num - mapping->begin) + + mapping->info.file.offset)) { /* offset of this cluster in file chain has change= d */ abort(); copy_it =3D 1; @@ -2404,7 +2407,7 @@ static int commit_mappings(BDRVVVFATState* s, (mapping->end - mapping->begin); } else next_mapping->info.file.offset =3D mapping->info.file.offs= et + - mapping->end - mapping->begin; + (mapping->end - mapping->begin); =20 mapping =3D next_mapping; } --=20 2.45.2 From nobody Sun Nov 24 11:41:56 2024 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=1722892319; cv=none; d=zohomail.com; s=zohoarc; b=a0kHnaz/KxN2jJfTxOfz/46AKfFzThoDXL0c9WLqQprvlljZFIsYsi7QAcWka+drsNOxfCblhK7cM6kefSGLp6Ica/jzouoXtcEUTpHoURKGOgHW8PztfViQaTSMvkJ+Cw/5kmpHTwI5ivzafvCNe2MxykQ7keRi3ADxJgRZOX8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892319; 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=imRy/cCkdNUHP3mYUYZKXEbSa2NYjKrWcecXWJ8m3RY=; b=hz4SO3NCKW7d5hQcDERoJ4BhO/STOHKmCBoFt3f5+Wg30HpeOOv77O0lSyFGWuKeGZz4MQ9GquACdzbz+0+fQNeWhH8ZMsMgFSz16B9bkt1qlF4todQyc/p1OpUrzKlUSt7r88Dg43wpAyXY8cJ5/nUCgN7VmPlUS+kox30BDr0= 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 1722892319387872.2725368598351; Mon, 5 Aug 2024 14:11:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xr-0002Zh-K0; Mon, 05 Aug 2024 17:09:59 -0400 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 1sb4xJ-0000d1-RA for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:25 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sb4xI-0008W2-6R for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:25 -0400 Received: from mx-prod-mc-05.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-694-1bx2gr0iPhKokF0Vph--fg-1; Mon, 05 Aug 2024 17:09:20 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7EA671956046; Mon, 5 Aug 2024 21:09:19 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0F02930001AD; Mon, 5 Aug 2024 21:09:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892163; 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=imRy/cCkdNUHP3mYUYZKXEbSa2NYjKrWcecXWJ8m3RY=; b=cVNjjo779+4NHjUTYo6lET6syufY5GhAkQ//7pY7Rszk1puZ4bhznyAmddYataq9fRB10/ vAAMq1U3PlaJ9c/7JlEGohHEoMBROf8FKA8pfZ9lV+TG6RKwHVRojesWKrkmioHc30BQqq BR+7iP/CBI95uvswHQ5UT6hrRdEup9w= X-MC-Unique: 1bx2gr0iPhKokF0Vph--fg-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 10/13] vvfat: Fix wrong checks for cluster mappings invariant Date: Mon, 5 Aug 2024 23:08:48 +0200 Message-ID: <20240805210851.314076-11-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H4=0.001, RCVD_IN_MSPIKE_WL=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: 1722892321209116600 Content-Type: text/plain; charset="utf-8" From: Amjad Alsharafi How this `abort` was intended to check for was: - if the `mapping->first_mapping_index` is not the same as `first_mapping_index`, which **should** happen only in one case, when we are handling the first mapping, in that case `mapping->first_mapping_index =3D=3D -1`, in all other cases, the other mappings after the first should have the condition `true`. - From above, we know that this is the first mapping, so if the offset is not `0`, then abort, since this is an invalid state. The issue was that `first_mapping_index` is not set if we are checking from the middle, the variable `first_mapping_index` is only set if we passed through the check `cluster_was_modified` with the first mapping, and in the same function call we checked the other mappings. One approach is to go into the loop even if `cluster_was_modified` is not true so that we will be able to set `first_mapping_index` for the first mapping, but since `first_mapping_index` is only used here, another approach is to just check manually for the `mapping->first_mapping_index !=3D -1` since we know that this is the value for the only entry where `offset =3D=3D 0` (i.e. first mapping). Signed-off-by: Amjad Alsharafi Reviewed-by: Kevin Wolf Message-ID: Signed-off-by: Kevin Wolf --- block/vvfat.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/block/vvfat.c b/block/vvfat.c index cfde468c2e..e3a83fbc88 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -1880,7 +1880,6 @@ get_cluster_count_for_direntry(BDRVVVFATState* s, dir= entry_t* direntry, const ch =20 uint32_t cluster_num =3D begin_of_direntry(direntry); uint32_t offset =3D 0; - int first_mapping_index =3D -1; mapping_t* mapping =3D NULL; const char* basename2 =3D NULL; =20 @@ -1942,14 +1941,9 @@ get_cluster_count_for_direntry(BDRVVVFATState* s, di= rentry_t* direntry, const ch =20 if (strcmp(basename, basename2)) copy_it =3D 1; - first_mapping_index =3D array_index(&(s->mapping),= mapping); - } - - if (mapping->first_mapping_index !=3D first_mapping_in= dex - && mapping->info.file.offset > 0) { - abort(); - copy_it =3D 1; } + assert(mapping->first_mapping_index =3D=3D -1 + || mapping->info.file.offset > 0); =20 /* need to write out? */ if (!was_modified && is_file(direntry)) { --=20 2.45.2 From nobody Sun Nov 24 11:41:56 2024 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=1722892241; cv=none; d=zohomail.com; s=zohoarc; b=mMATg0XunEhBNBfemEvRVxPIZQV4Pz0yvWo04IrUQJulE2TTy082aRYh5j6P8mLoqF17bvvDECryjwJHQAiWYPiaKeFBHEWgQBNshjmrZ6g3FVH+yNEVgG8WqcbUavaq1cCRa23WZeNJ0mTanHtWIvKlBrTMEY6XYO0rncuGd9g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892241; 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=/Qu6i6/DIWfHhjmc4NvZgMJ7eTASrEHvQhpohLIlIPw=; b=lgdMnctEIm0WdRAAZgukerkRTQHgn/Csaqn/z7hNUlLd8X/95Vyx46guNiH3D8oVfcg6XS9BqmpHeQQPhCZMwE0idR/YDeJ6SzaUf7GJXx3XlYgPiSE0TU4JIPf05qq8RiCvd0Ne+3o1AYykz1nXGNU5VZlMzfamNGCCaagiCCc= 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 1722892241395449.8852929869879; Mon, 5 Aug 2024 14:10:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xb-0001gI-1D; Mon, 05 Aug 2024 17:09:46 -0400 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 1sb4xR-0001EZ-JJ for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:34 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sb4xP-00005m-Up for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:33 -0400 Received: from mx-prod-mc-04.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-564-OMg2fGSlMaSKCtOa9wDgZQ-1; Mon, 05 Aug 2024 17:09:21 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DB15C19560B0; Mon, 5 Aug 2024 21:09:20 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A18D830001AA; Mon, 5 Aug 2024 21:09:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892170; 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=/Qu6i6/DIWfHhjmc4NvZgMJ7eTASrEHvQhpohLIlIPw=; b=VQvR9b8XNFAa9/dNzjdDVgEyjQYYMzrvU/1g33QbNdo5xwJjoRk0nfKUHl7U3BZZWeNbwo rWHyI2v5kxm9frlYSyF4Xll7jCdi9u8oOx9JoZ8ciIzZgI8qliwj4CXN57qxb5sjAf+7Kr Xn9J9y6CI6M54yzZkI15z+fIOeXi+WM= X-MC-Unique: OMg2fGSlMaSKCtOa9wDgZQ-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 11/13] vvfat: Fix reading files with non-continuous clusters Date: Mon, 5 Aug 2024 23:08:49 +0200 Message-ID: <20240805210851.314076-12-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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.133.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H4=0.001, RCVD_IN_MSPIKE_WL=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: 1722892242852116600 Content-Type: text/plain; charset="utf-8" From: Amjad Alsharafi When reading with `read_cluster` we get the `mapping` with `find_mapping_for_cluster` and then we call `open_file` for this mapping. The issue appear when its the same file, but a second cluster that is not immediately after it, imagine clusters `500 -> 503`, this will give us 2 mappings one has the range `500..501` and another `503..504`, both point to the same file, but different offsets. When we don't open the file since the path is the same, we won't assign `s->current_mapping` and thus accessing way out of bound of the file. From our example above, after `open_file` (that didn't open anything) we will get the offset into the file with `s->cluster_size*(cluster_num-s->current_mapping->begin)`, which will give us `0x2000 * (504-500)`, which is out of bound for this mapping and will produce some issues. Signed-off-by: Amjad Alsharafi Message-ID: <1f3ea115779abab62ba32c788073cdc99f9ad5dd.1721470238.git.amjads= harafi10@gmail.com> [kwolf: Simplified the patch based on Amjad's analysis and input] Signed-off-by: Kevin Wolf --- block/vvfat.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/block/vvfat.c b/block/vvfat.c index e3a83fbc88..8ffe8b3b9b 100644 --- a/block/vvfat.c +++ b/block/vvfat.c @@ -1369,8 +1369,9 @@ static int open_file(BDRVVVFATState* s,mapping_t* map= ping) return -1; vvfat_close_current_file(s); s->current_fd =3D fd; - s->current_mapping =3D mapping; } + + s->current_mapping =3D mapping; return 0; } =20 --=20 2.45.2 From nobody Sun Nov 24 11:41:56 2024 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=1722892257; cv=none; d=zohomail.com; s=zohoarc; b=MialJxdyhuu8s5ViW/BnhlU9O8W6H6qaDxqiDrewMREJQJg1MA6tYloVwG04zieRsjmBaTlgkLM++/KV4FO99Y2NAzVcoWvNYbDZlieVvHQYAJY6n5gsR17aLkUk4hsjoTCT8eRzzn9LOSrxeg4eOTIauczfF7FLvT8pqUQEaJA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892257; 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=i0eVdd+PqctTc0P3FtcIisCqAC0OLtyrjJ4Kirh1OmU=; b=R+VInleDoC6qi9uCSaz6wilfA207TcxrbY+DUONSPhKb6vj9ASY0OnyN7lE3cuVGONu8MeOj/AKZpkfjP9GKnmzBejXzET6O88aW6a8Fbvs3oUsu2po68kKdPOiFSZcaf4QSR620waI4uY6hHdWWSyLAtMxglo9gh8lUMB6e/RU= 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 1722892256991433.083689298257; Mon, 5 Aug 2024 14:10:56 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xw-0003nW-0G; Mon, 05 Aug 2024 17:10:04 -0400 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 1sb4xQ-00018y-78 for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:34 -0400 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 1sb4xK-000050-CC for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:31 -0400 Received: from mx-prod-mc-03.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-421-ecZfW702MlWBGWh796l_YA-1; Mon, 05 Aug 2024 17:09:23 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EDAB919560B6; Mon, 5 Aug 2024 21:09:22 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 53A4730001AD; Mon, 5 Aug 2024 21:09:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892165; 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=i0eVdd+PqctTc0P3FtcIisCqAC0OLtyrjJ4Kirh1OmU=; b=G5Q0/pCAiocIUmAgrumhQv7svpD2baeACTlnjTSs+qypPG9LXSfjyTfL8oJwHsTq+wdhRH AckOPD+7QMtXDwSBheL24jCIcC9X+jUbeVNgTsjPfWAcBLND9Ieg+70GGzI3Q1cfdIo/6D lViPXxoiHCb/lBbkTXG6rHXMQv6+GRw= X-MC-Unique: ecZfW702MlWBGWh796l_YA-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 12/13] iotests: Add `vvfat` tests Date: Mon, 5 Aug 2024 23:08:50 +0200 Message-ID: <20240805210851.314076-13-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: 1722892259006116600 Content-Type: text/plain; charset="utf-8" From: Amjad Alsharafi Added several tests to verify the implementation of the vvfat driver. We needed a way to interact with it, so created a basic `fat16.py` driver that handled writing correct sectors for us. Added `vvfat` to the non-generic formats, as its not a normal image format. Signed-off-by: Amjad Alsharafi Reviewed-by: Kevin Wolf Tested-by: Kevin Wolf Message-ID: [kwolf: Made mypy and pylint happy to unbreak 297] Signed-off-by: Kevin Wolf --- tests/qemu-iotests/fat16.py | 690 +++++++++++++++++++++++++++++ tests/qemu-iotests/testenv.py | 2 +- tests/qemu-iotests/check | 2 +- tests/qemu-iotests/tests/vvfat | 485 ++++++++++++++++++++ tests/qemu-iotests/tests/vvfat.out | 5 + 5 files changed, 1182 insertions(+), 2 deletions(-) create mode 100644 tests/qemu-iotests/fat16.py create mode 100755 tests/qemu-iotests/tests/vvfat create mode 100755 tests/qemu-iotests/tests/vvfat.out diff --git a/tests/qemu-iotests/fat16.py b/tests/qemu-iotests/fat16.py new file mode 100644 index 0000000000..2c3c1cbb3d --- /dev/null +++ b/tests/qemu-iotests/fat16.py @@ -0,0 +1,690 @@ +# A simple FAT16 driver that is used to test the `vvfat` driver in QEMU. +# +# Copyright (C) 2024 Amjad Alsharafi +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +from typing import Callable, List, Optional, Protocol, Set +import string + +SECTOR_SIZE =3D 512 +DIRENTRY_SIZE =3D 32 +ALLOWED_FILE_CHARS =3D set( + "!#$%&'()-@^_`{}~" + string.digits + string.ascii_uppercase +) + + +class MBR: + def __init__(self, data: bytes): + assert len(data) =3D=3D 512 + self.partition_table =3D [] + for i in range(4): + partition =3D data[446 + i * 16 : 446 + (i + 1) * 16] + self.partition_table.append( + { + "status": partition[0], + "start_head": partition[1], + "start_sector": partition[2] & 0x3F, + "start_cylinder": ((partition[2] & 0xC0) << 2) + | partition[3], + "type": partition[4], + "end_head": partition[5], + "end_sector": partition[6] & 0x3F, + "end_cylinder": ((partition[6] & 0xC0) << 2) + | partition[7], + "start_lba": int.from_bytes(partition[8:12], "little"), + "size": int.from_bytes(partition[12:16], "little"), + } + ) + + def __str__(self): + return "\n".join( + [ + f"{i}: {partition}" + for i, partition in enumerate(self.partition_table) + ] + ) + + +class FatBootSector: + # pylint: disable=3Dtoo-many-instance-attributes + def __init__(self, data: bytes): + assert len(data) =3D=3D 512 + self.bytes_per_sector =3D int.from_bytes(data[11:13], "little") + self.sectors_per_cluster =3D data[13] + self.reserved_sectors =3D int.from_bytes(data[14:16], "little") + self.fat_count =3D data[16] + self.root_entries =3D int.from_bytes(data[17:19], "little") + total_sectors_16 =3D int.from_bytes(data[19:21], "little") + self.media_descriptor =3D data[21] + self.sectors_per_fat =3D int.from_bytes(data[22:24], "little") + self.sectors_per_track =3D int.from_bytes(data[24:26], "little") + self.heads =3D int.from_bytes(data[26:28], "little") + self.hidden_sectors =3D int.from_bytes(data[28:32], "little") + total_sectors_32 =3D int.from_bytes(data[32:36], "little") + assert ( + total_sectors_16 =3D=3D 0 or total_sectors_32 =3D=3D 0 + ), "Both total sectors (16 and 32) fields are non-zero" + self.total_sectors =3D total_sectors_16 or total_sectors_32 + self.drive_number =3D data[36] + self.volume_id =3D int.from_bytes(data[39:43], "little") + self.volume_label =3D data[43:54].decode("ascii").strip() + self.fs_type =3D data[54:62].decode("ascii").strip() + + def root_dir_start(self): + """ + Calculate the start sector of the root directory. + """ + return self.reserved_sectors + self.fat_count * self.sectors_per_f= at + + def root_dir_size(self): + """ + Calculate the size of the root directory in sectors. + """ + return ( + self.root_entries * DIRENTRY_SIZE + self.bytes_per_sector - 1 + ) // self.bytes_per_sector + + def data_sector_start(self): + """ + Calculate the start sector of the data region. + """ + return self.root_dir_start() + self.root_dir_size() + + def first_sector_of_cluster(self, cluster: int) -> int: + """ + Calculate the first sector of the given cluster. + """ + return ( + self.data_sector_start() + (cluster - 2) * self.sectors_per_cl= uster + ) + + def cluster_bytes(self): + """ + Calculate the number of bytes in a cluster. + """ + return self.bytes_per_sector * self.sectors_per_cluster + + def __str__(self): + return ( + f"Bytes per sector: {self.bytes_per_sector}\n" + f"Sectors per cluster: {self.sectors_per_cluster}\n" + f"Reserved sectors: {self.reserved_sectors}\n" + f"FAT count: {self.fat_count}\n" + f"Root entries: {self.root_entries}\n" + f"Total sectors: {self.total_sectors}\n" + f"Media descriptor: {self.media_descriptor}\n" + f"Sectors per FAT: {self.sectors_per_fat}\n" + f"Sectors per track: {self.sectors_per_track}\n" + f"Heads: {self.heads}\n" + f"Hidden sectors: {self.hidden_sectors}\n" + f"Drive number: {self.drive_number}\n" + f"Volume ID: {self.volume_id}\n" + f"Volume label: {self.volume_label}\n" + f"FS type: {self.fs_type}\n" + ) + + +class FatDirectoryEntry: + # pylint: disable=3Dtoo-many-instance-attributes + def __init__(self, data: bytes, sector: int, offset: int): + self.name =3D data[0:8].decode("ascii").strip() + self.ext =3D data[8:11].decode("ascii").strip() + self.attributes =3D data[11] + self.reserved =3D data[12] + self.create_time_tenth =3D data[13] + self.create_time =3D int.from_bytes(data[14:16], "little") + self.create_date =3D int.from_bytes(data[16:18], "little") + self.last_access_date =3D int.from_bytes(data[18:20], "little") + high_cluster =3D int.from_bytes(data[20:22], "little") + self.last_mod_time =3D int.from_bytes(data[22:24], "little") + self.last_mod_date =3D int.from_bytes(data[24:26], "little") + low_cluster =3D int.from_bytes(data[26:28], "little") + self.cluster =3D (high_cluster << 16) | low_cluster + self.size_bytes =3D int.from_bytes(data[28:32], "little") + + # extra (to help write back to disk) + self.sector =3D sector + self.offset =3D offset + + def as_bytes(self) -> bytes: + return ( + self.name.ljust(8, " ").encode("ascii") + + self.ext.ljust(3, " ").encode("ascii") + + self.attributes.to_bytes(1, "little") + + self.reserved.to_bytes(1, "little") + + self.create_time_tenth.to_bytes(1, "little") + + self.create_time.to_bytes(2, "little") + + self.create_date.to_bytes(2, "little") + + self.last_access_date.to_bytes(2, "little") + + (self.cluster >> 16).to_bytes(2, "little") + + self.last_mod_time.to_bytes(2, "little") + + self.last_mod_date.to_bytes(2, "little") + + (self.cluster & 0xFFFF).to_bytes(2, "little") + + self.size_bytes.to_bytes(4, "little") + ) + + def whole_name(self): + if self.ext: + return f"{self.name}.{self.ext}" + else: + return self.name + + def __str__(self): + return ( + f"Name: {self.name}\n" + f"Ext: {self.ext}\n" + f"Attributes: {self.attributes}\n" + f"Reserved: {self.reserved}\n" + f"Create time tenth: {self.create_time_tenth}\n" + f"Create time: {self.create_time}\n" + f"Create date: {self.create_date}\n" + f"Last access date: {self.last_access_date}\n" + f"Last mod time: {self.last_mod_time}\n" + f"Last mod date: {self.last_mod_date}\n" + f"Cluster: {self.cluster}\n" + f"Size: {self.size_bytes}\n" + ) + + def __repr__(self): + # convert to dict + return str(vars(self)) + + +class SectorReader(Protocol): + def __call__(self, start_sector: int, num_sectors: int =3D 1) -> bytes= : ... + +# pylint: disable=3Dbroad-exception-raised +class Fat16: + def __init__( + self, + start_sector: int, + size: int, + sector_reader: SectorReader, + sector_writer: Callable[[int, bytes], None] + ): + self.start_sector =3D start_sector + self.size_in_sectors =3D size + self.sector_reader =3D sector_reader + self.sector_writer =3D sector_writer + + self.boot_sector =3D FatBootSector(self.sector_reader(start_sector= , 1)) + + fat_size_in_sectors =3D ( + self.boot_sector.sectors_per_fat * self.boot_sector.fat_count + ) + self.fats =3D self.read_sectors( + self.boot_sector.reserved_sectors, fat_size_in_sectors + ) + self.fats_dirty_sectors: Set[int] =3D set() + + def read_sectors(self, start_sector: int, num_sectors: int) -> bytes: + return self.sector_reader(start_sector + self.start_sector, + num_sectors) + + def write_sectors(self, start_sector: int, data: bytes) -> None: + return self.sector_writer(start_sector + self.start_sector, data) + + def directory_from_bytes( + self, data: bytes, start_sector: int + ) -> List[FatDirectoryEntry]: + """ + Convert `bytes` into a list of `FatDirectoryEntry` objects. + Will ignore long file names. + Will stop when it encounters a 0x00 byte. + """ + + entries =3D [] + for i in range(0, len(data), DIRENTRY_SIZE): + entry =3D data[i : i + DIRENTRY_SIZE] + + current_sector =3D start_sector + (i // SECTOR_SIZE) + current_offset =3D i % SECTOR_SIZE + + if entry[0] =3D=3D 0: + break + + if entry[0] =3D=3D 0xE5: + # Deleted file + continue + + if entry[11] & 0xF =3D=3D 0xF: + # Long file name + continue + + entries.append( + FatDirectoryEntry(entry, current_sector, current_offset) + ) + return entries + + def read_root_directory(self) -> List[FatDirectoryEntry]: + root_dir =3D self.read_sectors( + self.boot_sector.root_dir_start(), self.boot_sector.root_dir_s= ize() + ) + return self.directory_from_bytes( + root_dir, self.boot_sector.root_dir_start() + ) + + def read_fat_entry(self, cluster: int) -> int: + """ + Read the FAT entry for the given cluster. + """ + fat_offset =3D cluster * 2 # FAT16 + return int.from_bytes(self.fats[fat_offset : fat_offset + 2], "lit= tle") + + def write_fat_entry(self, cluster: int, value: int) -> None: + """ + Write the FAT entry for the given cluster. + """ + fat_offset =3D cluster * 2 + self.fats =3D ( + self.fats[:fat_offset] + + value.to_bytes(2, "little") + + self.fats[fat_offset + 2 :] + ) + self.fats_dirty_sectors.add(fat_offset // SECTOR_SIZE) + + def flush_fats(self) -> None: + """ + Write the FATs back to the disk. + """ + for sector in self.fats_dirty_sectors: + data =3D self.fats[sector * SECTOR_SIZE : (sector + 1) * SECTO= R_SIZE] + sector =3D self.boot_sector.reserved_sectors + sector + self.write_sectors(sector, data) + self.fats_dirty_sectors =3D set() + + def next_cluster(self, cluster: int) -> int | None: + """ + Get the next cluster in the chain. + If its `None`, then its the last cluster. + The function will crash if the next cluster + is `FREE` (unexpected) or invalid entry. + """ + fat_entry =3D self.read_fat_entry(cluster) + if fat_entry =3D=3D 0: + raise Exception("Unexpected: FREE cluster") + if fat_entry =3D=3D 1: + raise Exception("Unexpected: RESERVED cluster") + if fat_entry >=3D 0xFFF8: + return None + if fat_entry >=3D 0xFFF7: + raise Exception("Invalid FAT entry") + + return fat_entry + + def next_free_cluster(self) -> int: + """ + Find the next free cluster. + """ + # simple linear search + for i in range(2, 0xFFFF): + if self.read_fat_entry(i) =3D=3D 0: + return i + raise Exception("No free clusters") + + def next_free_cluster_non_continuous(self) -> int: + """ + Find the next free cluster, but makes sure + that the cluster before and after it are not allocated. + """ + # simple linear search + before =3D False + for i in range(2, 0xFFFF): + if self.read_fat_entry(i) =3D=3D 0: + if before and self.read_fat_entry(i + 1) =3D=3D 0: + return i + else: + before =3D True + else: + before =3D False + + raise Exception("No free clusters") + + def read_cluster(self, cluster: int) -> bytes: + """ + Read the cluster at the given cluster. + """ + return self.read_sectors( + self.boot_sector.first_sector_of_cluster(cluster), + self.boot_sector.sectors_per_cluster, + ) + + def write_cluster(self, cluster: int, data: bytes) -> None: + """ + Write the cluster at the given cluster. + """ + assert len(data) =3D=3D self.boot_sector.cluster_bytes() + self.write_sectors( + self.boot_sector.first_sector_of_cluster(cluster), + data, + ) + + def read_directory( + self, cluster: Optional[int] + ) -> List[FatDirectoryEntry]: + """ + Read the directory at the given cluster. + """ + entries =3D [] + while cluster is not None: + data =3D self.read_cluster(cluster) + entries.extend( + self.directory_from_bytes( + data, self.boot_sector.first_sector_of_cluster(cluster) + ) + ) + cluster =3D self.next_cluster(cluster) + return entries + + def add_direntry( + self, cluster: int | None, name: str, ext: str, attributes: int + ) -> FatDirectoryEntry: + """ + Add a new directory entry to the given cluster. + If the cluster is `None`, then it will be added to the root direct= ory. + """ + + def find_free_entry(data: bytes) -> Optional[int]: + for i in range(0, len(data), DIRENTRY_SIZE): + entry =3D data[i : i + DIRENTRY_SIZE] + if entry[0] =3D=3D 0 or entry[0] =3D=3D 0xE5: + return i + return None + + assert len(name) <=3D 8, "Name must be 8 characters or less" + assert len(ext) <=3D 3, "Ext must be 3 characters or less" + assert attributes % 0x15 !=3D 0x15, "Invalid attributes" + + # initial dummy data + new_entry =3D FatDirectoryEntry(b"\0" * 32, 0, 0) + new_entry.name =3D name.ljust(8, " ") + new_entry.ext =3D ext.ljust(3, " ") + new_entry.attributes =3D attributes + new_entry.reserved =3D 0 + new_entry.create_time_tenth =3D 0 + new_entry.create_time =3D 0 + new_entry.create_date =3D 0 + new_entry.last_access_date =3D 0 + new_entry.last_mod_time =3D 0 + new_entry.last_mod_date =3D 0 + new_entry.cluster =3D self.next_free_cluster() + new_entry.size_bytes =3D 0 + + # mark as EOF + self.write_fat_entry(new_entry.cluster, 0xFFFF) + + if cluster is None: + for i in range(self.boot_sector.root_dir_size()): + sector_data =3D self.read_sectors( + self.boot_sector.root_dir_start() + i, 1 + ) + offset =3D find_free_entry(sector_data) + if offset is not None: + new_entry.sector =3D self.boot_sector.root_dir_start()= + i + new_entry.offset =3D offset + self.update_direntry(new_entry) + return new_entry + else: + while cluster is not None: + data =3D self.read_cluster(cluster) + offset =3D find_free_entry(data) + if offset is not None: + new_entry.sector =3D ( + self.boot_sector.first_sector_of_cluster(cluster) + + (offset // SECTOR_SIZE)) + new_entry.offset =3D offset % SECTOR_SIZE + self.update_direntry(new_entry) + return new_entry + cluster =3D self.next_cluster(cluster) + + raise Exception("No free directory entries") + + def update_direntry(self, entry: FatDirectoryEntry) -> None: + """ + Write the directory entry back to the disk. + """ + sector =3D self.read_sectors(entry.sector, 1) + sector =3D ( + sector[: entry.offset] + + entry.as_bytes() + + sector[entry.offset + DIRENTRY_SIZE :] + ) + self.write_sectors(entry.sector, sector) + + def find_direntry(self, path: str) -> FatDirectoryEntry | None: + """ + Find the directory entry for the given path. + """ + assert path[0] =3D=3D "/", "Path must start with /" + + path =3D path[1:] # remove the leading / + parts =3D path.split("/") + directory =3D self.read_root_directory() + + current_entry =3D None + + for i, part in enumerate(parts): + is_last =3D i =3D=3D len(parts) - 1 + + for entry in directory: + if entry.whole_name() =3D=3D part: + current_entry =3D entry + break + if current_entry is None: + return None + + if is_last: + return current_entry + + if current_entry.attributes & 0x10 =3D=3D 0: + raise Exception( + f"{current_entry.whole_name()} is not a directory" + ) + + directory =3D self.read_directory(current_entry.cluster) + + assert False, "Exited loop with is_last =3D=3D False" + + def read_file(self, entry: FatDirectoryEntry | None) -> bytes | None: + """ + Read the content of the file at the given path. + """ + if entry is None: + return None + if entry.attributes & 0x10 !=3D 0: + raise Exception(f"{entry.whole_name()} is a directory") + + data =3D b"" + cluster: Optional[int] =3D entry.cluster + while cluster is not None and len(data) <=3D entry.size_bytes: + data +=3D self.read_cluster(cluster) + cluster =3D self.next_cluster(cluster) + return data[: entry.size_bytes] + + def truncate_file( + self, + entry: FatDirectoryEntry, + new_size: int, + allocate_non_continuous: bool =3D False, + ) -> None: + """ + Truncate the file at the given path to the new size. + """ + if entry is None: + raise Exception("entry is None") + if entry.attributes & 0x10 !=3D 0: + raise Exception(f"{entry.whole_name()} is a directory") + + def clusters_from_size(size: int) -> int: + return ( + size + self.boot_sector.cluster_bytes() - 1 + ) // self.boot_sector.cluster_bytes() + + # First, allocate new FATs if we need to + required_clusters =3D clusters_from_size(new_size) + current_clusters =3D clusters_from_size(entry.size_bytes) + + affected_clusters =3D set() + + # Keep at least one cluster, easier to manage this way + if required_clusters =3D=3D 0: + required_clusters =3D 1 + if current_clusters =3D=3D 0: + current_clusters =3D 1 + + cluster: Optional[int] + + if required_clusters > current_clusters: + # Allocate new clusters + cluster =3D entry.cluster + to_add =3D required_clusters + for _ in range(current_clusters - 1): + to_add -=3D 1 + assert cluster is not None, "Cluster is None" + affected_clusters.add(cluster) + cluster =3D self.next_cluster(cluster) + assert required_clusters > 0, "No new clusters to allocate" + assert cluster is not None, "Cluster is None" + assert ( + self.next_cluster(cluster) is None + ), "Cluster is not the last cluster" + + # Allocate new clusters + for _ in range(to_add - 1): + if allocate_non_continuous: + new_cluster =3D self.next_free_cluster_non_continuous() + else: + new_cluster =3D self.next_free_cluster() + self.write_fat_entry(cluster, new_cluster) + self.write_fat_entry(new_cluster, 0xFFFF) + cluster =3D new_cluster + + elif required_clusters < current_clusters: + # Truncate the file + cluster =3D entry.cluster + for _ in range(required_clusters - 1): + assert cluster is not None, "Cluster is None" + cluster =3D self.next_cluster(cluster) + assert cluster is not None, "Cluster is None" + + next_cluster =3D self.next_cluster(cluster) + # mark last as EOF + self.write_fat_entry(cluster, 0xFFFF) + # free the rest + while next_cluster is not None: + cluster =3D next_cluster + next_cluster =3D self.next_cluster(next_cluster) + self.write_fat_entry(cluster, 0) + + self.flush_fats() + + # verify number of clusters + cluster =3D entry.cluster + count =3D 0 + while cluster is not None: + count +=3D 1 + affected_clusters.add(cluster) + cluster =3D self.next_cluster(cluster) + assert ( + count =3D=3D required_clusters + ), f"Expected {required_clusters} clusters, got {count}" + + # update the size + entry.size_bytes =3D new_size + self.update_direntry(entry) + + # trigger every affected cluster + for cluster in affected_clusters: + first_sector =3D self.boot_sector.first_sector_of_cluster(clus= ter) + first_sector_data =3D self.read_sectors(first_sector, 1) + self.write_sectors(first_sector, first_sector_data) + + def write_file(self, entry: FatDirectoryEntry, data: bytes) -> None: + """ + Write the content of the file at the given path. + """ + if entry is None: + raise Exception("entry is None") + if entry.attributes & 0x10 !=3D 0: + raise Exception(f"{entry.whole_name()} is a directory") + + data_len =3D len(data) + + self.truncate_file(entry, data_len) + + cluster: Optional[int] =3D entry.cluster + while cluster is not None: + data_to_write =3D data[: self.boot_sector.cluster_bytes()] + if len(data_to_write) < self.boot_sector.cluster_bytes(): + old_data =3D self.read_cluster(cluster) + data_to_write +=3D old_data[len(data_to_write) :] + + self.write_cluster(cluster, data_to_write) + data =3D data[self.boot_sector.cluster_bytes() :] + if len(data) =3D=3D 0: + break + cluster =3D self.next_cluster(cluster) + + assert ( + len(data) =3D=3D 0 + ), "Data was not written completely, clusters missing" + + def create_file(self, path: str) -> Optional[FatDirectoryEntry]: + """ + Create a new file at the given path. + """ + assert path[0] =3D=3D "/", "Path must start with /" + + path =3D path[1:] # remove the leading / + + parts =3D path.split("/") + + directory_cluster =3D None + directory =3D self.read_root_directory() + + parts, filename =3D parts[:-1], parts[-1] + + for _, part in enumerate(parts): + current_entry =3D None + for entry in directory: + if entry.whole_name() =3D=3D part: + current_entry =3D entry + break + if current_entry is None: + return None + + if current_entry.attributes & 0x10 =3D=3D 0: + raise Exception( + f"{current_entry.whole_name()} is not a directory" + ) + + directory =3D self.read_directory(current_entry.cluster) + directory_cluster =3D current_entry.cluster + + # add new entry to the directory + + filename, ext =3D filename.split(".") + + if len(ext) > 3: + raise Exception("Ext must be 3 characters or less") + if len(filename) > 8: + raise Exception("Name must be 8 characters or less") + + for c in filename + ext: + + if c not in ALLOWED_FILE_CHARS: + raise Exception("Invalid character in filename") + + return self.add_direntry(directory_cluster, filename, ext, 0) diff --git a/tests/qemu-iotests/testenv.py b/tests/qemu-iotests/testenv.py index 96d69e5696..c8848f2ec2 100644 --- a/tests/qemu-iotests/testenv.py +++ b/tests/qemu-iotests/testenv.py @@ -255,7 +255,7 @@ def __init__(self, source_dir: str, build_dir: str, self.qemu_img_options =3D os.getenv('QEMU_IMG_OPTIONS') self.qemu_nbd_options =3D os.getenv('QEMU_NBD_OPTIONS') =20 - is_generic =3D self.imgfmt not in ['bochs', 'cloop', 'dmg'] + is_generic =3D self.imgfmt not in ['bochs', 'cloop', 'dmg', 'vvfat= '] self.imgfmt_generic =3D 'true' if is_generic else 'false' =20 self.qemu_io_options =3D f'--cache {self.cachemode} --aio {self.ai= omode}' diff --git a/tests/qemu-iotests/check b/tests/qemu-iotests/check index 56d88ca423..545f9ec7bd 100755 --- a/tests/qemu-iotests/check +++ b/tests/qemu-iotests/check @@ -84,7 +84,7 @@ def make_argparser() -> argparse.ArgumentParser: p.set_defaults(imgfmt=3D'raw', imgproto=3D'file') =20 format_list =3D ['raw', 'bochs', 'cloop', 'parallels', 'qcow', 'qcow2', - 'qed', 'vdi', 'vpc', 'vhdx', 'vmdk', 'luks', 'dmg'] + 'qed', 'vdi', 'vpc', 'vhdx', 'vmdk', 'luks', 'dmg', 'vv= fat'] g_fmt =3D p.add_argument_group( ' image format options', 'The following options set the IMGFMT environment variable. ' diff --git a/tests/qemu-iotests/tests/vvfat b/tests/qemu-iotests/tests/vvfat new file mode 100755 index 0000000000..acdc6ce8ff --- /dev/null +++ b/tests/qemu-iotests/tests/vvfat @@ -0,0 +1,485 @@ +#!/usr/bin/env python3 +# group: rw vvfat +# +# Test vvfat driver implementation +# Here, we use a simple FAT16 implementation and check the behavior of +# the vvfat driver. +# +# Copyright (C) 2024 Amjad Alsharafi +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import os +import shutil +import iotests +from iotests import imgfmt, QMPTestCase +from fat16 import MBR, Fat16, DIRENTRY_SIZE + +filesystem =3D os.path.join(iotests.test_dir, "filesystem") + +nbd_sock =3D iotests.file_path("nbd.sock", base_dir=3Diotests.sock_dir) +nbd_uri =3D "nbd+unix:///disk?socket=3D" + nbd_sock + +SECTOR_SIZE =3D 512 + + +class TestVVFatDriver(QMPTestCase): + # pylint: disable=3Dbroad-exception-raised + def setUp(self) -> None: + if os.path.exists(filesystem): + if os.path.isdir(filesystem): + shutil.rmtree(filesystem) + else: + raise Exception(f"{filesystem} exists and is not a directo= ry") + + os.mkdir(filesystem) + + # Add some text files to the filesystem + for i in range(10): + with open(os.path.join(filesystem, f"file{i}.txt"), + "w", encoding=3D"ascii") as f: + f.write(f"Hello, world! {i}\n") + + # Add 2 large files, above the cluster size (8KB) + with open(os.path.join(filesystem, "large1.txt"), "wb") as f: + # write 'A' * 1KB, 'B' * 1KB, 'C' * 1KB, ... + for i in range(8 * 2): # two clusters + f.write(bytes([0x41 + i] * 1024)) + + with open(os.path.join(filesystem, "large2.txt"), "wb") as f: + # write 'A' * 1KB, 'B' * 1KB, 'C' * 1KB, ... + for i in range(8 * 3): # 3 clusters + f.write(bytes([0x41 + i] * 1024)) + + self.vm =3D iotests.VM() + + self.vm.add_blockdev( + self.vm.qmp_to_opts( + { + "driver": imgfmt, + "node-name": "disk", + "rw": "true", + "fat-type": "16", + "dir": filesystem, + } + ) + ) + + self.vm.launch() + + self.vm.qmp_log("block-dirty-bitmap-add", **{ + "node": "disk", + "name": "bitmap0", + }) + + # attach nbd server + self.vm.qmp_log( + "nbd-server-start", + **{"addr": {"type": "unix", "data": {"path": nbd_sock}}}, + filters=3D[], + ) + + self.vm.qmp_log( + "nbd-server-add", + **{"device": "disk", "writable": True, "bitmap": "bitmap0"}, + ) + + self.qio =3D iotests.QemuIoInteractive("-f", "raw", nbd_uri) + + def tearDown(self) -> None: + self.qio.close() + self.vm.shutdown() + # print(self.vm.get_log()) + shutil.rmtree(filesystem) + + def read_sectors(self, sector: int, num: int =3D 1) -> bytes: + """ + Read `num` sectors starting from `sector` from the `disk`. + This uses `QemuIoInteractive` to read the sectors into `stdout` and + then parse the output. + """ + self.assertGreater(num, 0) + + # The output contains the content of the sector in hex dump format + # We need to extract the content from it + output =3D self.qio.cmd( + f"read -v {sector * SECTOR_SIZE} {num * SECTOR_SIZE}") + + # Each row is 16 bytes long, and we are writing `num` sectors + rows =3D num * SECTOR_SIZE // 16 + output_rows =3D output.split("\n")[:rows] + + hex_content =3D "".join( + [(row.split(": ")[1]).split(" ")[0] for row in output_rows] + ) + bytes_content =3D bytes.fromhex(hex_content) + + self.assertEqual(len(bytes_content), num * SECTOR_SIZE) + + return bytes_content + + def write_sectors(self, sector: int, data: bytes) -> None: + """ + Write `data` to the `disk` starting from `sector`. + This uses `QemuIoInteractive` to write the data into the disk. + """ + + self.assertGreater(len(data), 0) + self.assertEqual(len(data) % SECTOR_SIZE, 0) + + temp_file =3D os.path.join(iotests.test_dir, "temp.bin") + with open(temp_file, "wb") as f: + f.write(data) + + self.qio.cmd( + f"write -s {temp_file} {sector * SECTOR_SIZE} {len(data)}" + ) + + os.remove(temp_file) + + def init_fat16(self): + mbr =3D MBR(self.read_sectors(0)) + return Fat16( + mbr.partition_table[0]["start_lba"], + mbr.partition_table[0]["size"], + self.read_sectors, + self.write_sectors, + ) + + # Tests + + def test_fat_filesystem(self): + """ + Test that vvfat produce a valid FAT16 and MBR sectors + """ + mbr =3D MBR(self.read_sectors(0)) + + self.assertEqual(mbr.partition_table[0]["status"], 0x80) + self.assertEqual(mbr.partition_table[0]["type"], 6) + + fat16 =3D Fat16( + mbr.partition_table[0]["start_lba"], + mbr.partition_table[0]["size"], + self.read_sectors, + self.write_sectors, + ) + self.assertEqual(fat16.boot_sector.bytes_per_sector, 512) + self.assertEqual(fat16.boot_sector.volume_label, "QEMU VVFAT") + + def test_read_root_directory(self): + """ + Test the content of the root directory + """ + fat16 =3D self.init_fat16() + + root_dir =3D fat16.read_root_directory() + + self.assertEqual(len(root_dir), 13) # 12 + 1 special file + + files =3D { + "QEMU VVF.AT": 0, # special empty file + "FILE0.TXT": 16, + "FILE1.TXT": 16, + "FILE2.TXT": 16, + "FILE3.TXT": 16, + "FILE4.TXT": 16, + "FILE5.TXT": 16, + "FILE6.TXT": 16, + "FILE7.TXT": 16, + "FILE8.TXT": 16, + "FILE9.TXT": 16, + "LARGE1.TXT": 0x2000 * 2, + "LARGE2.TXT": 0x2000 * 3, + } + + for entry in root_dir: + self.assertIn(entry.whole_name(), files) + self.assertEqual(entry.size_bytes, files[entry.whole_name()]) + + def test_direntry_as_bytes(self): + """ + Test if we can convert Direntry back to bytes, so that we can writ= e it + back to the disk safely. + """ + fat16 =3D self.init_fat16() + + root_dir =3D fat16.read_root_directory() + first_entry_bytes =3D fat16.read_sectors( + fat16.boot_sector.root_dir_start(), 1) + + # The first entry won't be deleted, so we can compare it with the = first + # entry in the root directory + self.assertEqual(root_dir[0].as_bytes(), + first_entry_bytes[:DIRENTRY_SIZE]) + + def test_read_files(self): + """ + Test reading the content of the files + """ + fat16 =3D self.init_fat16() + + for i in range(10): + file =3D fat16.find_direntry(f"/FILE{i}.TXT") + self.assertIsNotNone(file) + self.assertEqual( + fat16.read_file(file), f"Hello, world! {i}\n".encode("asci= i") + ) + + # test large files + large1 =3D fat16.find_direntry("/LARGE1.TXT") + with open(os.path.join(filesystem, "large1.txt"), "rb") as f: + self.assertEqual(fat16.read_file(large1), f.read()) + + large2 =3D fat16.find_direntry("/LARGE2.TXT") + self.assertIsNotNone(large2) + with open(os.path.join(filesystem, "large2.txt"), "rb") as f: + self.assertEqual(fat16.read_file(large2), f.read()) + + def test_write_file_same_content_direct(self): + """ + Similar to `test_write_file_in_same_content`, but we write the file + directly clusters and thus we don't go through the modification of + direntry. + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/FILE0.TXT") + self.assertIsNotNone(file) + + data =3D fat16.read_cluster(file.cluster) + fat16.write_cluster(file.cluster, data) + + with open(os.path.join(filesystem, "file0.txt"), "rb") as f: + self.assertEqual(fat16.read_file(file), f.read()) + + def test_write_file_in_same_content(self): + """ + Test writing the same content to the file back to it + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/FILE0.TXT") + self.assertIsNotNone(file) + + self.assertEqual(fat16.read_file(file), b"Hello, world! 0\n") + + fat16.write_file(file, b"Hello, world! 0\n") + self.assertEqual(fat16.read_file(file), b"Hello, world! 0\n") + + with open(os.path.join(filesystem, "file0.txt"), "rb") as f: + self.assertEqual(f.read(), b"Hello, world! 0\n") + + def test_modify_content_same_clusters(self): + """ + Test modifying the content of the file without changing the number= of + clusters + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/FILE0.TXT") + self.assertIsNotNone(file) + + new_content =3D b"Hello, world! Modified\n" + self.assertEqual(fat16.read_file(file), b"Hello, world! 0\n") + + fat16.write_file(file, new_content) + self.assertEqual(fat16.read_file(file), new_content) + + with open(os.path.join(filesystem, "file0.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + def test_truncate_file_same_clusters_less(self): + """ + Test truncating the file without changing number of clusters + Test decreasing the file size + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/FILE0.TXT") + self.assertIsNotNone(file) + + self.assertEqual(fat16.read_file(file), b"Hello, world! 0\n") + + fat16.truncate_file(file, 5) + new_content =3D fat16.read_file(file) + self.assertEqual(new_content, b"Hello") + + with open(os.path.join(filesystem, "file0.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + def test_truncate_file_same_clusters_more(self): + """ + Test truncating the file without changing number of clusters + Test increase the file size + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/FILE0.TXT") + self.assertIsNotNone(file) + + self.assertEqual(fat16.read_file(file), b"Hello, world! 0\n") + + fat16.truncate_file(file, 20) + new_content =3D fat16.read_file(file) + self.assertIsNotNone(new_content) + + # random pattern will be appended to the file, and its not always = the + # same + self.assertEqual(new_content[:16], b"Hello, world! 0\n") + self.assertEqual(len(new_content), 20) + + with open(os.path.join(filesystem, "file0.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + def test_write_large_file(self): + """ + Test writing a large file + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/LARGE1.TXT") + self.assertIsNotNone(file) + + # The content of LARGE1 is A * 1KB, B * 1KB, C * 1KB, ..., P * 1KB + # Lets change it to be Z * 1KB, Y * 1KB, X * 1KB, ..., K * 1KB + # without changing the number of clusters or filesize + new_content =3D b"".join([bytes([0x5A - i] * 1024) for i in range(= 16)]) + fat16.write_file(file, new_content) + self.assertEqual(fat16.read_file(file), new_content) + + with open(os.path.join(filesystem, "large1.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + def test_truncate_file_change_clusters_less(self): + """ + Test truncating a file by reducing the number of clusters + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/LARGE1.TXT") + self.assertIsNotNone(file) + + fat16.truncate_file(file, 1) + self.assertEqual(fat16.read_file(file), b"A") + + with open(os.path.join(filesystem, "large1.txt"), "rb") as f: + self.assertEqual(f.read(), b"A") + + def test_write_file_change_clusters_less(self): + """ + Test truncating a file by reducing the number of clusters + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/LARGE2.TXT") + self.assertIsNotNone(file) + + new_content =3D b"X" * 8 * 1024 + b"Y" * 8 * 1024 + fat16.write_file(file, new_content) + self.assertEqual(fat16.read_file(file), new_content) + + with open(os.path.join(filesystem, "large2.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + def test_write_file_change_clusters_more(self): + """ + Test truncating a file by increasing the number of clusters + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/LARGE2.TXT") + self.assertIsNotNone(file) + + # from 3 clusters to 4 clusters + new_content =3D ( + b"W" * 8 * 1024 + + b"X" * 8 * 1024 + + b"Y" * 8 * 1024 + + b"Z" * 8 * 1024 + ) + fat16.write_file(file, new_content) + self.assertEqual(fat16.read_file(file), new_content) + + with open(os.path.join(filesystem, "large2.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + def test_write_file_change_clusters_more_non_contiguous_2_mappings(sel= f): + """ + Test truncating a file by increasing the number of clusters Here we + allocate the new clusters in a way that makes them non-contiguous = so + that we will get 2 cluster mappings for the file + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/LARGE1.TXT") + self.assertIsNotNone(file) + + # from 2 clusters to 3 clusters with non-contiguous allocation + fat16.truncate_file(file, 3 * 0x2000, allocate_non_continuous=3DTr= ue) + new_content =3D b"X" * 8 * 1024 + b"Y" * 8 * 1024 + b"Z" * 8 * 1024 + fat16.write_file(file, new_content) + self.assertEqual(fat16.read_file(file), new_content) + + with open(os.path.join(filesystem, "large1.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + def test_write_file_change_clusters_more_non_contiguous_3_mappings(sel= f): + """ + Test truncating a file by increasing the number of clusters Here we + allocate the new clusters in a way that makes them non-contiguous = so + that we will get 3 cluster mappings for the file + """ + fat16 =3D self.init_fat16() + + file =3D fat16.find_direntry("/LARGE1.TXT") + self.assertIsNotNone(file) + + # from 2 clusters to 4 clusters with non-contiguous allocation + fat16.truncate_file(file, 4 * 0x2000, allocate_non_continuous=3DTr= ue) + new_content =3D ( + b"W" * 8 * 1024 + + b"X" * 8 * 1024 + + b"Y" * 8 * 1024 + + b"Z" * 8 * 1024 + ) + fat16.write_file(file, new_content) + self.assertEqual(fat16.read_file(file), new_content) + + with open(os.path.join(filesystem, "large1.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + def test_create_file(self): + """ + Test creating a new file + """ + fat16 =3D self.init_fat16() + + new_file =3D fat16.create_file("/NEWFILE.TXT") + + self.assertIsNotNone(new_file) + self.assertEqual(new_file.size_bytes, 0) + + new_content =3D b"Hello, world! New file\n" + fat16.write_file(new_file, new_content) + self.assertEqual(fat16.read_file(new_file), new_content) + + with open(os.path.join(filesystem, "newfile.txt"), "rb") as f: + self.assertEqual(f.read(), new_content) + + # TODO: support deleting files + + +if __name__ =3D=3D "__main__": + # This is a specific test for vvfat driver + iotests.main(supported_fmts=3D["vvfat"], supported_protocols=3D["file"= ]) diff --git a/tests/qemu-iotests/tests/vvfat.out b/tests/qemu-iotests/tests/= vvfat.out new file mode 100755 index 0000000000..b6f257674e --- /dev/null +++ b/tests/qemu-iotests/tests/vvfat.out @@ -0,0 +1,5 @@ +................ +---------------------------------------------------------------------- +Ran 16 tests + +OK --=20 2.45.2 From nobody Sun Nov 24 11:41:56 2024 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=1722892242; cv=none; d=zohomail.com; s=zohoarc; b=YGu1jRetdM93Afc+JrERTYoEdTZr1TB3gIuSB3VWau1S9/3Ji8boIUHtnckEv3RbbIScVAg7r16xxVsR/GS6dphIPqio1NfoAbzHktTJAxkdEEgkmfIwOntG3XeqxqVmE647rvq8Bed2lHeDyB3qXKCp++8JxcVnQbk64BpuTFk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722892242; 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=FJ15q8CYPj6PTN7hDi0BIfIjd8VlCM6ctQu3O7D8jso=; b=j64246xeQ4Ig0nOR75Tvonlb4DR1YxeFmqGpRumEehEYtUEFX3R/6yec9niA8ANCChKlz6N5ZWsgoC0bYG0MekXF1DqD07mxWoWzt9IeNWaQZe/m0ozIlEkRnB5+A4Y8RoGuNSuOyb1OEeYyEjf5Y5+MBB/6Hz0s8sgFwJEVnzM= 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 1722892242191875.231374398606; Mon, 5 Aug 2024 14:10:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sb4xk-0001yd-NL; Mon, 05 Aug 2024 17:09:58 -0400 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 1sb4xQ-00018k-6Y for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:34 -0400 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 1sb4xO-00005N-9Q for qemu-devel@nongnu.org; Mon, 05 Aug 2024 17:09:31 -0400 Received: from mx-prod-mc-04.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-501-WVYWbzNNN6S7Sqcvf7mejg-1; Mon, 05 Aug 2024 17:09:25 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8940B19560B3; Mon, 5 Aug 2024 21:09:24 +0000 (UTC) Received: from merkur.fritz.box (unknown [10.39.193.224]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 626D130001B4; Mon, 5 Aug 2024 21:09:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722892169; 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=FJ15q8CYPj6PTN7hDi0BIfIjd8VlCM6ctQu3O7D8jso=; b=HJcMEJPHvfFyqGQvt04xH3MBeBqEBcQYRQ1dNyQaq++mzPWfzt21ADutAS75lftu9glBjb FyxIrwQSaZY9oPiVnsyEFX8ZVxB+kYtqxhBhHOCgrE9oeR+LObXMQ0M596ApC+aroqtqyC XbhWiQh+AniiCjtnKRJMDsGAHlcvgAM= X-MC-Unique: WVYWbzNNN6S7Sqcvf7mejg-1 From: Kevin Wolf To: qemu-block@nongnu.org Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [PULL 13/13] iotests/024: exclude 'backing file format' field from the output Date: Mon, 5 Aug 2024 23:08:51 +0200 Message-ID: <20240805210851.314076-14-kwolf@redhat.com> In-Reply-To: <20240805210851.314076-1-kwolf@redhat.com> References: <20240805210851.314076-1-kwolf@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 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: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.143, 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_H3=0.001, RCVD_IN_MSPIKE_WL=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: 1722892242850116600 Content-Type: text/plain; charset="utf-8" From: Andrey Drobyshev Apparently 'qemu-img info' doesn't report the backing file format field for qed (as it does for qcow2): $ qemu-img create -f qed base.qed 1M && qemu-img create -f qed -b base.qed = -F qed top.qed 1M $ qemu-img create -f qcow2 base.qcow2 1M && qemu-img create -f qcow2 -b bas= e.qcow2 -F qcow2 top.qcow2 1M $ qemu-img info top.qed | grep 'backing file format' $ qemu-img info top.qcow2 | grep 'backing file format' backing file format: qcow2 This leads to the 024 test failure with -qed. Let's just filter the field out and exclude it from the output. This is a fixup for the commit f93e65ee51 ("iotests/{024, 271}: add testcases for qemu-img rebase"). Reported-by: Thomas Huth Signed-off-by: Andrey Drobyshev Message-ID: <20240730094701.790624-1-andrey.drobyshev@virtuozzo.com> Reviewed-by: Eric Blake Reviewed-by: Kevin Wolf Signed-off-by: Kevin Wolf --- tests/qemu-iotests/024 | 2 +- tests/qemu-iotests/024.out | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/qemu-iotests/024 b/tests/qemu-iotests/024 index 285f17e79f..b29c76e161 100755 --- a/tests/qemu-iotests/024 +++ b/tests/qemu-iotests/024 @@ -283,7 +283,7 @@ TEST_IMG=3D$BASE_OLD _make_test_img -b "$BASE_NEW" -F $= IMGFMT \ CLUSTER_SIZE=3D$(( CLUSTER_SIZE * 2 )) TEST_IMG=3D$OVERLAY \ _make_test_img -b "$BASE_OLD" -F $IMGFMT $(( CLUSTER_SIZE * 6 )) =20 -TEST_IMG=3D$OVERLAY _img_info +TEST_IMG=3D$OVERLAY _img_info | grep -v '^backing file format:' =20 echo echo "Fill backing files with data" diff --git a/tests/qemu-iotests/024.out b/tests/qemu-iotests/024.out index e1e8eea863..3d1e31927a 100644 --- a/tests/qemu-iotests/024.out +++ b/tests/qemu-iotests/024.out @@ -214,7 +214,6 @@ file format: IMGFMT virtual size: 384 KiB (393216 bytes) cluster_size: 131072 backing file: TEST_DIR/subdir/t.IMGFMT.base_old -backing file format: IMGFMT =20 Fill backing files with data =20 --=20 2.45.2