From nobody Sat May 4 20:12:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1568923810; cv=none; d=zoho.com; s=zohoarc; b=bUrT3uX8npMJckUohsuqOFX8zA2P9eiHKkPAdSzoaA2mSTQFLlJqO8rKkN+awrYY1X5CkbJnf+5cGWaIQI8Mn4u65Phfpo4NhQwxBm5Ew+RY/6Lmq/ClhZDYOIRC9W98ITeAWPaChUQGPrl4keZ47cWwoxihjFft3/bdF4hWkWA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568923810; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=ERZlZXg/Ax90SEg4ScNbuCvd2tTqX+Kk+JoNaL/ZXfs=; b=Bpd/cLdbJprQ2ogxp+9xDtM+weatkns86bSrVRDSn5S75psN5TIDIuAUvhcO0ChFpx8hPzvEPJPx95ERkyKjGGC3/N6Gh4schiVDjQSJoM6/DBPKU7d9Gjq/w4o+zPdyV02CzENajXuOKMbD/FqGwxuMFzcykEYBq8o4VYWhTJA= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 15689238107941022.2556653863774; Thu, 19 Sep 2019 13:10:10 -0700 (PDT) Received: from localhost ([::1]:48330 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iB2kj-00066f-FI for importer@patchew.org; Thu, 19 Sep 2019 16:10:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45151) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iB2dA-0005cB-5S for qemu-devel@nongnu.org; Thu, 19 Sep 2019 16:02:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iB2QA-00026x-Ci for qemu-devel@nongnu.org; Thu, 19 Sep 2019 15:48:56 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55336) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iB2Q8-00025e-Cl for qemu-devel@nongnu.org; Thu, 19 Sep 2019 15:48:52 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 66B2A18C8900 for ; Thu, 19 Sep 2019 19:48:50 +0000 (UTC) Received: from sulaco.redhat.com (ovpn-112-12.phx2.redhat.com [10.3.112.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id 88CF45D6D0; Thu, 19 Sep 2019 19:48:49 +0000 (UTC) From: Tony Asleson To: qemu-devel@nongnu.org, kwolf@redhat.com Subject: [RFC 1/4] Add qapi for block error injection Date: Thu, 19 Sep 2019 14:48:44 -0500 Message-Id: <20190919194847.18518-2-tasleson@redhat.com> In-Reply-To: <20190919194847.18518-1-tasleson@redhat.com> References: <20190919194847.18518-1-tasleson@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.6.2 (mx1.redhat.com [10.5.110.70]); Thu, 19 Sep 2019 19:48:50 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 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" Content-Type: text/plain; charset="utf-8" Proof of concept code to dynamically create/delete media errors for block devices using the qapi interface. This is useful for testing the OS and the block and FS layers for error handling. Utilizing this in a VM allows it to be OS agnostic and test the OS at it's lowest levels of hardware interaction. Signed-off-by: Tony Asleson --- block/Makefile.objs | 2 +- block/error_inject.c | 179 +++++++++++++++++++++++++++++++++++++++++++ block/error_inject.h | 43 +++++++++++ block/qapi.c | 18 +++++ qapi/block.json | 36 +++++++++ 5 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 block/error_inject.c create mode 100644 block/error_inject.h diff --git a/block/Makefile.objs b/block/Makefile.objs index 35f3bca4d9..c8fcbc276b 100644 --- a/block/Makefile.objs +++ b/block/Makefile.objs @@ -1,4 +1,4 @@ -block-obj-y +=3D raw-format.o vmdk.o vpc.o +block-obj-y +=3D raw-format.o vmdk.o vpc.o error_inject.o block-obj-$(CONFIG_QCOW1) +=3D qcow.o block-obj-$(CONFIG_VDI) +=3D vdi.o block-obj-$(CONFIG_CLOOP) +=3D cloop.o diff --git a/block/error_inject.c b/block/error_inject.c new file mode 100644 index 0000000000..26a47dfb8c --- /dev/null +++ b/block/error_inject.c @@ -0,0 +1,179 @@ +/* + * Error injection code for block devices + * + * Copyright (c) 2019 Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +#include "error_inject.h" + +#include + +#include "qemu/thread.h" + +static QemuMutex error_inject_lock; +static GHashTable *error_inject_data; + + +static void delete_lba_entries(void *entry) +{ + GSequence *e =3D (GSequence *) entry; + g_sequence_free(e); +} + +struct value { + uint64_t error_lba; + + /* + * TODO Actually do something with behavior + */ + MediaErrorBehavior behavior; + /* + * TODO Add data for generating bitrot, when we do change free function + */ +}; + +static int key_compare(gconstpointer a, gconstpointer b, gpointer data) +{ + uint64_t left =3D ((struct value *)a)->error_lba; + uint64_t right =3D ((struct value *)b)->error_lba; + + if (left < right) { + return -1; + } else if (left > right) { + return 1; + } else { + return 0; + } +} + +static uint64_t error_lba_get(GSequenceIter *iter) +{ + gpointer tmp =3D g_sequence_get(iter); + return ((struct value *)tmp)->error_lba; +} + +void media_error_create(const char *device_id, uint64_t lba, + MediaErrorBehavior behavior) +{ + qemu_mutex_lock(&error_inject_lock); + + GSequence *block_device =3D g_hash_table_lookup(error_inject_data, dev= ice_id); + if (!block_device) { + block_device =3D g_sequence_new(g_free); + char *key =3D strdup(device_id); + g_hash_table_insert(error_inject_data, key, block_device); + } + + struct value lookup =3D {lba, MEDIA_ERROR_BEHAVIOR__MAX}; + if (!g_sequence_lookup(block_device, &lookup, key_compare, NULL)) { + struct value *val =3D g_new(struct value, 1); + val->error_lba =3D lba; + val->behavior =3D behavior; + + g_sequence_insert_sorted(block_device, val, key_compare, NULL); + } + + qemu_mutex_unlock(&error_inject_lock); +} + +void media_error_delete(const char *device_id, uint64_t lba) +{ + qemu_mutex_lock(&error_inject_lock); + + GSequence *block_device =3D g_hash_table_lookup(error_inject_data, dev= ice_id); + if (block_device) { + struct value find =3D { lba, MEDIA_ERROR_BEHAVIOR__MAX}; + GSequenceIter *found =3D g_sequence_lookup(block_device, &find, + key_compare, NULL); + if (found) { + g_sequence_remove(found); + } + } + + qemu_mutex_unlock(&error_inject_lock); +} + +int error_in_read(const char *device_id, uint64_t lba, uint64_t len, + uint64_t *error_lba) +{ + uint64_t error_sector =3D 0; + const uint64_t transfer_end =3D lba + len; + int ec =3D 0; + *error_lba =3D 0xFFFFFFFFFFFFFFFF; + + qemu_mutex_lock(&error_inject_lock); + + GSequence *block_device =3D g_hash_table_lookup(error_inject_data, dev= ice_id); + if (block_device && g_sequence_get_length(block_device) !=3D 0) { + struct value find =3D {lba, MEDIA_ERROR_BEHAVIOR__MAX}; + GSequenceIter *iter =3D g_sequence_search(block_device, &find, + key_compare, NULL); + + /* + * g_sequence_seach returns where the item would be inserted. + * In the case of a direct match, it's spot is inserted after the + * existing, thus we need to check the one immediately before + * the insertion point to see if it is the one we are looking for. + */ + GSequenceIter *prev =3D g_sequence_iter_prev(iter); + error_sector =3D error_lba_get(prev); + + if (error_sector >=3D lba && error_sector < transfer_end) { + *error_lba =3D error_sector; + ec =3D 1; + } else { + /* + * Lets look at next until we find one in our transfer or bail + * if the error(s) logical block address are greater than the + * end of our transfer. + */ + while (!g_sequence_iter_is_end(iter)) { + error_sector =3D error_lba_get(iter); + + if (error_sector >=3D transfer_end) { + break; + } + if (error_sector >=3D lba && error_sector < transfer_end) { + *error_lba =3D error_sector; + ec =3D 1; + break; + } else { + iter =3D g_sequence_iter_next(iter); + } + } + } + } + + qemu_mutex_unlock(&error_inject_lock); + + return ec; +} + + +static void __attribute__((__constructor__)) error_inject_init(void) +{ + qemu_mutex_init(&error_inject_lock); + + error_inject_data =3D g_hash_table_new_full(g_str_hash, + g_str_equal, + g_free, + delete_lba_entries); +} diff --git a/block/error_inject.h b/block/error_inject.h new file mode 100644 index 0000000000..5f2d143c90 --- /dev/null +++ b/block/error_inject.h @@ -0,0 +1,43 @@ +/* + * Error injection code for block devices + * + * Copyright (c) 2019 Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +#ifndef ERROR_INJECT +#define ERROR_INJECT +#include "qemu/osdep.h" + +#include + +#include "qapi/qapi-commands-block.h" +#include "qapi/qapi-types-block.h" + + +void media_error_create(const char *device_id, uint64_t lba, + MediaErrorBehavior behavior); +void media_error_delete(const char *device_id, uint64_t lba); + + +int error_in_read(const char *device_id, uint64_t lba, uint64_t len, + uint64_t *error_lba); + +#endif diff --git a/block/qapi.c b/block/qapi.c index 15f1030264..d66201a831 100644 --- a/block/qapi.c +++ b/block/qapi.c @@ -26,11 +26,14 @@ #include "qemu-common.h" #include "block/qapi.h" #include "block/block_int.h" +#include "block/error_inject.h" #include "block/throttle-groups.h" #include "block/write-threshold.h" #include "qapi/error.h" +#include "qapi/qapi-commands-block.h" #include "qapi/qapi-commands-block-core.h" #include "qapi/qobject-output-visitor.h" +#include "qapi/qapi-types-block.h" #include "qapi/qapi-visit-block-core.h" #include "qapi/qmp/qbool.h" #include "qapi/qmp/qdict.h" @@ -841,3 +844,18 @@ void bdrv_image_info_dump(ImageInfo *info) bdrv_image_info_specific_dump(info->format_specific); } } + +void qmp_media_error_create(const char *device_id, uint64_t lba, + MediaErrorBehavior behavior, Error **errp) +{ + /* + * We could validate the device_id and lba for existence and range, bu= t we + * want to be able to add entries before a device is hot plugged too. + */ + media_error_create(device_id, lba, behavior); +} + +void qmp_media_error_delete(const char *device_id, uint64_t lba, Error **e= rrp) +{ + media_error_delete(device_id, lba); +} diff --git a/qapi/block.json b/qapi/block.json index 145c268bb6..2b11954b44 100644 --- a/qapi/block.json +++ b/qapi/block.json @@ -453,3 +453,39 @@ { 'event': 'QUORUM_REPORT_BAD', 'data': { 'type': 'QuorumOpType', '*error': 'str', 'node-name': 'str', 'sector-num': 'int', 'sectors-count': 'int' } } + +## +# @MediaErrorBehavior: +# +# Enumerated type for classifying type of read error behavior. +# +## +{ 'enum': 'MediaErrorBehavior', 'data': ['hard', 'fixed_on_write', 'flakey= '] } + +## +# @media-error-create: +# +# Example: +# -> {"execute": "media-error-create", +# "arguments": {"device_id": "12345678, "lba" : 10000, "behavior" : "h= ard"}} +# <- { "return": {} } +# +# Create a synthetic media error for the specified logical block address w= hen +# it is read by the guest OS. +# +## +{ 'command':'media-error-create', + 'data': {'device_id': 'str', 'lba': 'uint64', 'behavior': 'MediaErrorBeh= avior'}} + +## +# @media-error-delete: +# +# Delete a synthetic media error for the specified logical block address. +# +# # Example: +# -> {"execute": "media-error-delete", +# "arguments": {"device_id": "01020304", "lba" : 10000} +# <- { "return": {} } +## +{ 'command':'media-error-delete', + 'data': {'device_id': 'str', 'lba': 'uint64'}} --=20 2.21.0 From nobody Sat May 4 20:12:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1568923730; cv=none; d=zoho.com; s=zohoarc; b=TTZNXmKTPDt+QmjlGklekKfzan26x9JomHc/jAwMEQXDQb4OWs/vxeIoPJjF50g+NfOqJ32uj752dQsqkI435OGF4qVWP6kRU9An+8PRtfNkGMUrV93P4MHzvE4nVVsmoK6z4sB6AHjbI5YsYa6MqCwRbAHulYqlLGNDidlyC5w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568923730; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=XaJcsL6RqF5+8ZEDyEsGAVqnJRmN4N6y1lQfToXY8vQ=; b=SDrLl+yo8r1B3591/6P/W0ypwGRMK+pYwID33ee50qVIihZssM1xgBUKStOtgTl2lACVw/Dt0+Jsvj/N8NXYT/sQ8c6Uo7uKcARxeoPB5Azziy7aJoi/t5Yn07y2wFetnlQaJg8SRzwkDDLieNFY5PT0EN4gkpWmUuopVE30WP8= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1568923730426735.8367383429686; Thu, 19 Sep 2019 13:08:50 -0700 (PDT) Received: from localhost ([::1]:48322 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iB2jM-0004bF-FV for importer@patchew.org; Thu, 19 Sep 2019 16:08:44 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45434) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iB2dB-00068Y-2n for qemu-devel@nongnu.org; Thu, 19 Sep 2019 16:02:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iB2QA-000273-D7 for qemu-devel@nongnu.org; Thu, 19 Sep 2019 15:48:55 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36960) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iB2Q8-000263-DT for qemu-devel@nongnu.org; Thu, 19 Sep 2019 15:48:53 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 73C0A3D965 for ; Thu, 19 Sep 2019 19:48:51 +0000 (UTC) Received: from sulaco.redhat.com (ovpn-112-12.phx2.redhat.com [10.3.112.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id B6C855D6D0; Thu, 19 Sep 2019 19:48:50 +0000 (UTC) From: Tony Asleson To: qemu-devel@nongnu.org, kwolf@redhat.com Subject: [RFC 2/4] SCSI media error reporting Date: Thu, 19 Sep 2019 14:48:45 -0500 Message-Id: <20190919194847.18518-3-tasleson@redhat.com> In-Reply-To: <20190919194847.18518-1-tasleson@redhat.com> References: <20190919194847.18518-1-tasleson@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 19 Sep 2019 19:48:51 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 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" Content-Type: text/plain; charset="utf-8" Add abilility to return media errors for SCSI block devices. Needs improvement. Signed-off-by: Tony Asleson --- hw/scsi/scsi-disk.c | 33 +++++++++++++++++++++++++++++++++ include/scsi/utils.h | 4 ++++ scsi/utils.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/hw/scsi/scsi-disk.c b/hw/scsi/scsi-disk.c index 915641a0f1..a7f3274cdf 100644 --- a/hw/scsi/scsi-disk.c +++ b/hw/scsi/scsi-disk.c @@ -37,6 +37,7 @@ #include "sysemu/dma.h" #include "qemu/cutils.h" #include "trace.h" +#include "block/error_inject.h" =20 #ifdef __linux #include @@ -132,6 +133,18 @@ static void scsi_check_condition(SCSIDiskReq *r, SCSIS= ense sense) scsi_req_complete(&r->req, CHECK_CONDITION); } =20 +/* Helper function for SCSI media error */ +static void scsi_media_error(SCSIDiskReq *r, SCSISense sense, uint32_t lba) +{ + trace_scsi_disk_check_condition(r->req.tag, sense.key, sense.asc, + sense.ascq); + + r->req.sense_len =3D scsi_build_sense_buf_info(r->req.sense, + SCSI_SENSE_LEN, sense, l= ba, + 0x80, 32); + scsi_req_complete(&r->req, CHECK_CONDITION); +} + static void scsi_init_iovec(SCSIDiskReq *r, size_t size) { SCSIDiskState *s =3D DO_UPCAST(SCSIDiskState, qdev, r->req.dev); @@ -2170,6 +2183,26 @@ static int32_t scsi_disk_dma_command(SCSIRequest *re= q, uint8_t *buf) } r->sector =3D r->req.cmd.lba * (s->qdev.blocksize / 512); r->sector_count =3D len * (s->qdev.blocksize / 512); + + /* + * TODO Move this check to a more appropriate spot. Additionally = we + * also need to transfer the data that was read up before encounte= ring + * the media error. + */ + uint64_t error_sector =3D 0; + char device_id[32]; + sprintf(device_id, "%lu", s->qdev.wwn); + if (error_in_read(device_id, r->sector, r->sector_count, + &error_sector)) { + /* + * TODO Fix error reporting for disks > 2TiB + */ + if (error_sector > 0xFFFFFFFF) { + error_sector =3D 0xFFFFFFFF; + } + scsi_media_error(r, SENSE_CODE(READ_ERROR), (uint32_t)error_se= ctor); + return 0; + } break; case WRITE_6: case WRITE_10: diff --git a/include/scsi/utils.h b/include/scsi/utils.h index fbc5588279..02ae74287f 100644 --- a/include/scsi/utils.h +++ b/include/scsi/utils.h @@ -35,6 +35,10 @@ SCSISense scsi_parse_sense_buf(const uint8_t *in_buf, in= t in_len); int scsi_build_sense_buf(uint8_t *buf, size_t max_size, SCSISense sense, bool fixed_sense); =20 +int scsi_build_sense_buf_info(uint8_t *out_buf, size_t size, SCSISense sen= se, + uint32_t information, uint8_t sksv, + uint16_t sense_key_specific_info); + /* * Predefined sense codes */ diff --git a/scsi/utils.c b/scsi/utils.c index c50e81fdb8..9c9f1735d0 100644 --- a/scsi/utils.c +++ b/scsi/utils.c @@ -147,6 +147,37 @@ int scsi_build_sense_buf(uint8_t *out_buf, size_t size= , SCSISense sense, return len; } =20 +int scsi_build_sense_buf_info(uint8_t *out_buf, size_t size, SCSISense sen= se, + uint32_t information, uint8_t sksv, + uint16_t sense_key_specific_info) +{ + uint8_t buf[SCSI_SENSE_LEN] =3D { 0 }; + + buf[0] =3D 0xF0; /* Valid bit set. */ + buf[2] =3D sense.key; + + /* + * Set bytes 3, 4, 5, 6 value of information field + */ + *((uint32_t *)(&buf[3])) =3D cpu_to_be32(information); + + buf[7] =3D 10; + buf[12] =3D sense.asc; + buf[13] =3D sense.ascq; + + if (sksv) { + buf[15] =3D sksv; + /* + * Set bytes 16, 17 to Sense-key specific bytes + */ + *((uint16_t *)&buf[16]) =3D cpu_to_be16(sense_key_specific_info); + } + + int len =3D MIN(SCSI_SENSE_LEN, size); + memcpy(out_buf, buf, len); + return len; +} + int scsi_build_sense(uint8_t *buf, SCSISense sense) { return scsi_build_sense_buf(buf, SCSI_SENSE_LEN, sense, true); --=20 2.21.0 From nobody Sat May 4 20:12:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1568923756; cv=none; d=zoho.com; s=zohoarc; b=dhpiSfsOtFrUjZhW54qZyfgtEhBkTR2WtZeA0GZk5++3/XoN4zcMxIOhpHjcrswMzL/+W1qwEOXeoUcOScJ9yRcn5xvGVoQMpvSvtHC80HvrEZvH0+TuCY8ofCMwVTwk3y+8wz1aiCU4DTYsU/vvcaeQRcrtVUw6dzHGYqttbAg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568923756; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=jrcMFKRo4EbyXmx8Dca37uh1jvU4zogHr0EiJhTNyHs=; b=dMwD1J8hq/P8QxVLGbZfHLcNYqukIMWuCBwni72HeduWRjV8/cqco5++tQBELOQlMX1kreH7HlFO68AIVA775IjqeorLFPyTyyFTwJ0iUqSHRqcDZgthsEySd9J5+yWtrVZuw+jOhCbL9FWLtsm/24dJ4bhJoR5kAGGw7B7HakU= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1568923756037185.60132560512932; Thu, 19 Sep 2019 13:09:16 -0700 (PDT) Received: from localhost ([::1]:48324 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iB2jq-000556-Ft for importer@patchew.org; Thu, 19 Sep 2019 16:09:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45474) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iB2dC-0006EP-It for qemu-devel@nongnu.org; Thu, 19 Sep 2019 16:02:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iB2QA-00027H-HA for qemu-devel@nongnu.org; Thu, 19 Sep 2019 15:48:55 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34836) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iB2QA-00026Q-Br for qemu-devel@nongnu.org; Thu, 19 Sep 2019 15:48:54 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6D3E03C92D for ; Thu, 19 Sep 2019 19:48:52 +0000 (UTC) Received: from sulaco.redhat.com (ovpn-112-12.phx2.redhat.com [10.3.112.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id C3B4D5D6D0; Thu, 19 Sep 2019 19:48:51 +0000 (UTC) From: Tony Asleson To: qemu-devel@nongnu.org, kwolf@redhat.com Subject: [RFC 3/4] NVMe media error reporting Date: Thu, 19 Sep 2019 14:48:46 -0500 Message-Id: <20190919194847.18518-4-tasleson@redhat.com> In-Reply-To: <20190919194847.18518-1-tasleson@redhat.com> References: <20190919194847.18518-1-tasleson@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 19 Sep 2019 19:48:52 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 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" Content-Type: text/plain; charset="utf-8" Rudimentary and basic support for returning NVMe errors. Signed-off-by: Tony Asleson --- hw/block/nvme.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/hw/block/nvme.c b/hw/block/nvme.c index 12d8254250..faf72c2b8c 100644 --- a/hw/block/nvme.c +++ b/hw/block/nvme.c @@ -25,6 +25,7 @@ * offset 0 in BAR2 and supports only WDS, RDS and SQS for now. */ =20 +#include "block/error_inject.h" #include "qemu/osdep.h" #include "qemu/units.h" #include "hw/block/block.h" @@ -390,6 +391,13 @@ static uint16_t nvme_rw(NvmeCtrl *n, NvmeNamespace *ns= , NvmeCmd *cmd, return NVME_LBA_RANGE | NVME_DNR; } =20 + if (!is_write) { + uint64_t error_sector =3D 0; + if (error_in_read(n->serial, slba, nlb, &error_sector)) { + return NVME_UNRECOVERED_READ | NVME_DNR; + } + } + if (nvme_map_prp(&req->qsg, &req->iov, prp1, prp2, data_size, n)) { block_acct_invalid(blk_get_stats(n->conf.blk), acct); return NVME_INVALID_FIELD | NVME_DNR; --=20 2.21.0 From nobody Sat May 4 20:12:03 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1568923523; cv=none; d=zoho.com; s=zohoarc; b=edYKDaWsNSqCcGERw3B5eiFD5NJG4RhKOIHE0w5pD5zmfnfPDcxGQq6LouHc61+N+08WmQHFhaDTyodFnsSdCK5zKzyp5ZBrfC0iQTTMxUMxeq/8QFFwNog3Vud2lgiGlZ8F4eX4hxMFZyCdGYd5vGi28Z9Idc9hW4r4XpNasdc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1568923523; h=Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=GNlKdEE4dWRYaByGHy2fDjW0KB9g5+3oIBboKnUlQIs=; b=n5DTojlK+566UZ2vReC4ZPFZb+1rUCvegFFSCB0liXcDvl3+tey2GQlKv151u+Pz8tSWt0PcZ02DcyWnncbXmqQ+2A1fJU56q8Fq/DGdSCimzjR0Mdroi4Y85XYrM5FqvnsfY3rs0AItMo6/ExOwfIQ6sPoDK+GcFrGvdIKfs/8= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1568923523874951.0085767575785; Thu, 19 Sep 2019 13:05:23 -0700 (PDT) Received: from localhost ([::1]:48288 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iB2g5-0000PX-7b for importer@patchew.org; Thu, 19 Sep 2019 16:05:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45488) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iB2dA-0006FQ-6N for qemu-devel@nongnu.org; Thu, 19 Sep 2019 16:02:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iB2QA-00027N-JA for qemu-devel@nongnu.org; Thu, 19 Sep 2019 15:48:55 -0400 Received: from mx1.redhat.com ([209.132.183.28]:44528) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iB2QA-00026j-Ds for qemu-devel@nongnu.org; Thu, 19 Sep 2019 15:48:54 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 77C2230860DF for ; Thu, 19 Sep 2019 19:48:53 +0000 (UTC) Received: from sulaco.redhat.com (ovpn-112-12.phx2.redhat.com [10.3.112.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id BC4DF5D6D0; Thu, 19 Sep 2019 19:48:52 +0000 (UTC) From: Tony Asleson To: qemu-devel@nongnu.org, kwolf@redhat.com Subject: [RFC 4/4] ahci media error reporting Date: Thu, 19 Sep 2019 14:48:47 -0500 Message-Id: <20190919194847.18518-5-tasleson@redhat.com> In-Reply-To: <20190919194847.18518-1-tasleson@redhat.com> References: <20190919194847.18518-1-tasleson@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Thu, 19 Sep 2019 19:48:53 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 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" Content-Type: text/plain; charset="utf-8" Initial attempt at returning a media error for ahci. This is certainly wrong and needs serious improvement. Signed-off-by: Tony Asleson --- hw/ide/ahci.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c index d45393c019..f487764106 100644 --- a/hw/ide/ahci.c +++ b/hw/ide/ahci.c @@ -36,6 +36,7 @@ #include "hw/ide/internal.h" #include "hw/ide/pci.h" #include "ahci_internal.h" +#include "block/error_inject.h" =20 #include "trace.h" =20 @@ -999,6 +1000,22 @@ static void ncq_err(NCQTransferState *ncq_tfs) ncq_tfs->used =3D 0; } =20 +/* + * Figure out correct way to report media error, this is at best a guess + * and based on the output of linux kernel, not even remotely close. + */ +static void ncq_media_err(NCQTransferState *ncq_tfs, uint64_t err_sector) +{ + IDEState *ide_state =3D &ncq_tfs->drive->port.ifs[0]; + + ide_state->error =3D ECC_ERR; + ide_state->status =3D READY_STAT | ERR_STAT; + ncq_tfs->drive->port_regs.scr_err |=3D (1 << ncq_tfs->tag); + ncq_tfs->lba =3D err_sector; + qemu_sglist_destroy(&ncq_tfs->sglist); + ncq_tfs->used =3D 0; +} + static void ncq_finish(NCQTransferState *ncq_tfs) { /* If we didn't error out, set our finished bit. Errored commands @@ -1065,6 +1082,8 @@ static void execute_ncq_command(NCQTransferState *ncq= _tfs) { AHCIDevice *ad =3D ncq_tfs->drive; IDEState *ide_state =3D &ad->port.ifs[0]; + uint64_t error_sector =3D 0; + char device_id[32]; int port =3D ad->port_no; =20 g_assert(is_ncq(ncq_tfs->cmd)); @@ -1072,6 +1091,14 @@ static void execute_ncq_command(NCQTransferState *nc= q_tfs) =20 switch (ncq_tfs->cmd) { case READ_FPDMA_QUEUED: + sprintf(device_id, "%lu", ide_state->wwn); + + if (error_in_read(device_id, ncq_tfs->lba, + ncq_tfs->sector_count, &error_sector)) { + ncq_media_err(ncq_tfs, error_sector); + return; + } + trace_execute_ncq_command_read(ad->hba, port, ncq_tfs->tag, ncq_tfs->sector_count, ncq_tfs->lba= ); dma_acct_start(ide_state->blk, &ncq_tfs->acct, --=20 2.21.0