From patchwork Thu Mar 1 14:08:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeremy Cline X-Patchwork-Id: 10251503 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 88CBF602B5 for ; Thu, 1 Mar 2018 14:09:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 78EE12239C for ; Thu, 1 Mar 2018 14:09:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7796F26E16; Thu, 1 Mar 2018 14:09:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9FD5527E63 for ; Thu, 1 Mar 2018 14:08:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1030995AbeCAOIM (ORCPT ); Thu, 1 Mar 2018 09:08:12 -0500 Received: from a8-57.smtp-out.amazonses.com ([54.240.8.57]:52818 "EHLO a8-57.smtp-out.amazonses.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1030876AbeCAOIL (ORCPT ); Thu, 1 Mar 2018 09:08:11 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=rdybrs3533vx7mghocfwl3vdwgpl2v5u; d=jcline.org; t=1519913290; h=From:To:Cc:Subject:Date:Message-Id; bh=vtZRQCbLYt4AyztzalS6I3Rf54lSbXzyg8fYRVdf+hw=; b=QuIEo1RxRFgGDNQmZqla47FEL3KZoJPtGljgOqJeTBVZC8/3xDeDs2k7DYYSsghJ Ydo2RJVtxTyW/3nWvioiuBZ6oU4FFyNr452ACunTAIsyxEOeO5g1ChyHEQGZvImyj68 zgDiNeCLHt/5EjuowAUq274DGRL75i7ZOEeHIN0g= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=224i4yxa5dv7c2xz3womw6peuasteono; d=amazonses.com; t=1519913290; h=From:To:Cc:Subject:Date:Message-Id:Feedback-ID; bh=vtZRQCbLYt4AyztzalS6I3Rf54lSbXzyg8fYRVdf+hw=; b=Yavmi0QXjETZPm/ecU8xbqycnbjDYPmRZoey7uaeg9/6gq/hgvxgJaeoTFx4e7J0 MaAFgVJvAwBEKkASHmkNivYYqd3oRM/vXlhq8AW9dqG0r3ltnelFZ2NZBdwlA4g1iCW ag27e5MhPgAFbY2HCrxdH7Cy86bpQ/OD7p3iR3dA= X-Virus-Scanned: amavisd-new at jcline.org From: Jeremy Cline To: "James E . J . Bottomley" , "Martin K . Petersen" Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org, Jeremy Cline Subject: [PATCH] scsi: sd: Keep disk read-only when re-reading partition Date: Thu, 1 Mar 2018 14:08:10 +0000 Message-ID: <01000161e1e448a5-d54e05d2-58f2-4480-b05b-67185d4ffae7-000000@email.amazonses.com> X-Mailer: git-send-email 2.16.2 X-SES-Outgoing: 2018.03.01-54.240.8.57 Feedback-ID: 1.us-east-1.z18Isoc/FaoPOvCyJyi1mnTt8STwoRuibXVNoUcvG6g=:AmazonSES Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If the read-only flag is true on a SCSI disk, re-reading the partition table sets the flag back to false. To observe this bug, you can run: 1. blockdev --setro /dev/sda 2. blockdev --rereadpt /dev/sda 3. blockdev --getro /dev/sda This commit reads the disk's old state and combines it with the device disk-reported state rather than unconditionally marking it as RW. Reported-by: Li Ning Signed-off-by: Jeremy Cline --- drivers/scsi/sd.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index bff21e636ddd..7a3a66a7890f 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -2595,6 +2595,7 @@ sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer) int res; struct scsi_device *sdp = sdkp->device; struct scsi_mode_data data; + int disk_ro = get_disk_ro(sdkp->disk); int old_wp = sdkp->write_prot; set_disk_ro(sdkp->disk, 0); @@ -2634,7 +2635,8 @@ sd_read_write_protect_flag(struct scsi_disk *sdkp, unsigned char *buffer) sd_first_printk(KERN_WARNING, sdkp, "Test WP failed, assume Write Enabled\n"); } else { - sdkp->write_prot = ((data.device_specific & 0x80) != 0); + sdkp->write_prot = ((data.device_specific & 0x80) != 0) || + disk_ro; set_disk_ro(sdkp->disk, sdkp->write_prot); if (sdkp->first_scan || old_wp != sdkp->write_prot) { sd_printk(KERN_NOTICE, sdkp, "Write Protect is %s\n",