From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id BFF9DDF02
 for <pbs-devel@lists.proxmox.com>; Wed, 20 Apr 2022 15:40:28 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id B01A2C1B6
 for <pbs-devel@lists.proxmox.com>; Wed, 20 Apr 2022 15:39:58 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (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 firstgate.proxmox.com (Proxmox) with ESMTPS id 773ADC1AB
 for <pbs-devel@lists.proxmox.com>; Wed, 20 Apr 2022 15:39:57 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 51BF5422AC
 for <pbs-devel@lists.proxmox.com>; Wed, 20 Apr 2022 15:39:57 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Wed, 20 Apr 2022 15:39:56 +0200
Message-Id: <20220420133956.1571816-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.137 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [sgutils2.rs]
Subject: [pbs-devel] [PATCH proxmox-backup] pbs-tape: sgutils2: check sense
 data when status is 'CHECK_CONDITION'
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Wed, 20 Apr 2022 13:40:28 -0000

some raid controllers return a 'transport error' when we expected a
'sense error'. it seems the correct way to check the sense data is when
either the result category is 'SENSE' or when the status i 'CHECK_CONDITION',
so do that. (similar to how 'sg_raw' returns the errors)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 pbs-tape/src/sgutils2.rs | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/pbs-tape/src/sgutils2.rs b/pbs-tape/src/sgutils2.rs
index 1fc487be..888f74ec 100644
--- a/pbs-tape/src/sgutils2.rs
+++ b/pbs-tape/src/sgutils2.rs
@@ -141,6 +141,9 @@ pub const SENSE_KEY_ABORTED_COMMAND: u8 = 0x0b;
 pub const SENSE_KEY_VOLUME_OVERFLOW: u8 = 0x0d;
 pub const SENSE_KEY_MISCOMPARE: u8 = 0x0e;
 
+// SAM STAT
+const SAM_STAT_CHECK_CONDITION: i32 = 0x02;
+
 /// Sense Key Descriptions
 pub const SENSE_KEY_DESCRIPTIONS: [&str; 16] = [
     "No Sense",
@@ -458,10 +461,10 @@ impl<'a, F: AsRawFd> SgRaw<'a, F> {
         let sense_len = unsafe { get_scsi_pt_sense_len(ptvp.as_ptr()) };
 
         let res_cat = unsafe { get_scsi_pt_result_category(ptvp.as_ptr()) };
-        match res_cat {
-            SCSI_PT_RESULT_GOOD => Ok(()),
-            SCSI_PT_RESULT_STATUS => {
-                let status = unsafe { get_scsi_pt_status_response(ptvp.as_ptr()) };
+        let status = unsafe { get_scsi_pt_status_response(ptvp.as_ptr()) };
+        match (res_cat, status) {
+            (SCSI_PT_RESULT_GOOD, _) => Ok(()),
+            (SCSI_PT_RESULT_STATUS, status) => {
                 if status != 0 {
                     return Err(
                         format_err!("unknown scsi error - status response {}", status).into(),
@@ -469,7 +472,7 @@ impl<'a, F: AsRawFd> SgRaw<'a, F> {
                 }
                 Ok(())
             }
-            SCSI_PT_RESULT_SENSE => {
+            (SCSI_PT_RESULT_SENSE, _) | (_, SAM_STAT_CHECK_CONDITION) => {
                 if sense_len == 0 {
                     return Err(format_err!("scsi command failed, but got no sense data").into());
                 }
@@ -511,15 +514,15 @@ impl<'a, F: AsRawFd> SgRaw<'a, F> {
 
                 Err(ScsiError::Sense(sense))
             }
-            SCSI_PT_RESULT_TRANSPORT_ERR => {
-                return Err(format_err!("scsi command failed: transport error").into())
+            (SCSI_PT_RESULT_TRANSPORT_ERR, _) => {
+                return Err(format_err!("scsi command failed: transport error").into());
             }
-            SCSI_PT_RESULT_OS_ERR => {
+            (SCSI_PT_RESULT_OS_ERR, _) => {
                 let errno = unsafe { get_scsi_pt_os_err(ptvp.as_ptr()) };
                 let err = nix::Error::from_errno(nix::errno::Errno::from_i32(errno));
                 return Err(format_err!("scsi command failed with err {}", err).into());
             }
-            unknown => {
+            (unknown, _) => {
                 return Err(
                     format_err!("scsi command failed: unknown result category {}", unknown).into(),
                 )
-- 
2.30.2