Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3920147ybl; Mon, 9 Dec 2019 02:20:53 -0800 (PST) X-Google-Smtp-Source: APXvYqyJizFCxj1G62TBFaAQNTxoaL399L/d3iTmz7GCv7Bmc76yC7qL6PmihrWsYofqzMBE2Jd2 X-Received: by 2002:a05:6830:1d7b:: with SMTP id l27mr19582956oti.251.1575886852988; Mon, 09 Dec 2019 02:20:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575886852; cv=none; d=google.com; s=arc-20160816; b=SUL635p+yNFUELJH/62NMH1cCap5deKXVypXhTe5zDOlZg1+aWO5x2pFHgGx6V54P4 0o1Mc0kTp0YcvmTR3BEPIG9foOiDqraNAfz90Eoydv3aMFL7UaOepT9LA30t4CdJ8n5s 3hR3/cFGs37l2awJKtPd2MAtUA+OFz1rQCY7/mgpmXhge++0Xykc9g6N2r4Aj7zF7apP YN1mdOHDu5uv50Alebzmihot0HVok05SouM+fji1eRntUxZQFNbGHc0pyR2sOpk8cUgG g5A+CRPS5udpWBMfUfYRI/AgbhVVeOPzneQOUAXN6mrZgMfye+fAhLFliMxtX7mdiwtk 5vig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :date:cc:to:from:subject:message-id; bh=oXcUrkbReKne+rZ3FO5YbktFPw6I7yGcTWqGT5y4g4M=; b=UMyNtXVJQrzC8Rf8dg9Jg2lZ4KwZM4TUjTDiDrmmianom+bBri35dNBjjl0fb21Jes NEfIVxiMPKJcq4H5gZVeowXDwD0dLHpIjH6AOiEOMG/LgpAEMO+I857SK8KO1Gh9wmPV I78eLkLgGX+T0H08mkHqSP1dgq1JlpjybO8jBFOa37iKm26Yy0l3XUBcZSZ6hWOkJdg9 pTg8eSfLRSb8Ss7ZVOqR+qWXP2Maexc6U5ffcmf8SPsSWouMFm2BLhxiFu+OooEXBTOe ZzUu8WGtx1E6JPgggTbvp56dt+RoKJxbo3lFx3AwZ+b7y28D/2WfcOwsLG2ZBUNtel2x 89NQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d206si10334794oif.203.2019.12.09.02.20.40; Mon, 09 Dec 2019 02:20:52 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727386AbfLIKUH (ORCPT + 99 others); Mon, 9 Dec 2019 05:20:07 -0500 Received: from authsmtp31.register.it ([81.88.54.72]:44115 "EHLO authsmtp.register.it" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726279AbfLIKUE (ORCPT ); Mon, 9 Dec 2019 05:20:04 -0500 X-Greylist: delayed 642 seconds by postgrey-1.27 at vger.kernel.org; Mon, 09 Dec 2019 05:20:03 EST Received: from [192.168.1.1] ([93.41.32.9]) by cmsmtp with ESMTPSA id eFwHidjUzRIgteFwHisCIW; Mon, 09 Dec 2019 11:06:50 +0100 X-Rid: guido@trentalancia.com@93.41.32.9 Message-ID: <1575886009.5344.17.camel@trentalancia.com> Subject: [PATCH v2] scsi: ignore Synchronize Cache command failures to keep using drives not supporting it From: Guido Trentalancia To: linux-scsi@vger.kernel.org Cc: drew@colorado.edu, linux-kernel@vger.kernel.org Date: Mon, 09 Dec 2019 11:06:49 +0100 X-Priority: 1 Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.26.2 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit X-CMAE-Envelope: MS4wfPdHbJXj2Dh+hR6D3iWJA9SqLmvvYMfvd3NxBZs+adXa33U2fMEX+s0reVw7wu4hMRTjURDbaqwlk2lPOVMoDp3r2fkAGMWPjLJP96Qc69vQqkRSqK7K qv4Mw84x504BjqSE9xGtFnnGnRE4pI1z5v4rPvOSHwCbp51H2NKhaDDg6J45EQr117CaGujz+RUae0astppuV8zcBPNHQ9g5TDOKvNYN1fTKpiCWa6jmki0S RtZ90dDh5beeCFjGwsukRMUQ2QI2aIBv7ZLjeaRf5FU= Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Many obsolete hard drives do not support the Synchronize Cache SCSI command. Such command is generally issued during fsync() calls which at the moment therefore fail with the ILLEGAL_REQUEST sense key. Since this failure is currently treated as critical in the kernel SCSI disk driver, such obsolete hard drives cannot be used anymore (at least since kernel 4.10, maybe even earlier): they cannot be formatted, mounted and/or checked using tools such as e2fsprogs. Because there is nothing which can be done if the drive does not support such command, such ILLEGAL_REQUEST should be treated as non-critical so that the underlying operation does not fail and the obsolete hard drive can be used normally. This second version of the patch (v2) disables the Write Cache feature as a precaution on hard drives which do not support the Synchronize Cache command and therefore the cache flushing functionality. Although the Write Cache is disabled (v2) when using such hard drives which do not feature the Synchronize Cache SCSI command, there is still some risk of data loss in case of power cuts, USB cable disconnects and so on, which depend on the drive itself and not on this patch or the rest of the kernel code: YOU HAVE BEEN WARNED - THE DRIVE MANIFACTURER SHOULD HAVE BEEN WARNED YOU IN THE FIRST PLACE - IN DOUBT, UPGRADE TO A NEWER HARD DRIVE !! Tested on a Maxtor OneTouch USB 200Gb External Hard Drive. Signed-off-by: Guido Trentalancia --- drivers/scsi/sd.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff -pru a/drivers/scsi/sd.c b/drivers/scsi/sd.c --- a/drivers/scsi/sd.c 2019-03-17 18:22:04.822720851 +0100 +++ b/drivers/scsi/sd.c 2019-03-20 17:41:44.526957307 +0100 @@ -22,6 +22,10 @@ * - Badari Pulavarty , Matthew Wilcox * , Kurt Garloff : * Support 32k/1M disks. + * - Guido Trentalancia ignore Synchronize + * Cache command failures on hard-drives that do not support it + * and disable the Write Cache functionality on such devices as a + * precaution: this allows to keep using several obsolete drives. * * Logging policy (needs CONFIG_SCSI_LOGGING defined): * - setting up transfer: SCSI_LOG_HLQUEUE levels 1 and 2 @@ -1633,6 +1637,20 @@ static int sd_sync_cache(struct scsi_dis } if (res) { + /* + * sshdr.sense_key == ILLEGAL_REQUEST means this drive + * doesn't support sync. There's not much to do and + * sync shouldn't fail. + */ + if (sshdr->sense_key == ILLEGAL_REQUEST && sshdr->asc == 0x20) { + if (sdkp->WCE) { + sdkp->WCE = 0; + sd_printk(KERN_NOTICE, sdkp, "Drive does not support Synchronize Cache(10) command: disabling write cache.\n"); + sd_set_flush_flag(sdkp); + } + return 0; + } + sd_print_result(sdkp, "Synchronize Cache(10) failed", res); if (driver_byte(res) == DRIVER_SENSE) @@ -2022,6 +2040,17 @@ static int sd_done(struct scsi_cmnd *SCp req->rq_flags |= RQF_QUIET; } break; + case SYNCHRONIZE_CACHE: + if (sshdr.asc == 0x20) { + if (sdkp->WCE) { + sdkp->WCE = 0; + sd_printk(KERN_NOTICE, sdkp, "Drive does not support Synchronize Cache(10) command: disabling write cache.\n"); + sd_set_flush_flag(sdkp); + } + SCpnt->result = 0; + good_bytes = scsi_bufflen(SCpnt); + } + break; } } break;