From patchwork Tue Dec 5 20:54:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Disseldorp X-Patchwork-Id: 10093769 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 D09406056E for ; Tue, 5 Dec 2017 20:54:57 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C2D1A28EDF for ; Tue, 5 Dec 2017 20:54:57 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B5F62291B2; Tue, 5 Dec 2017 20:54:57 +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=-6.9 required=2.0 tests=BAYES_00,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 2ADB828EDF for ; Tue, 5 Dec 2017 20:54:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751453AbdLEUy4 (ORCPT ); Tue, 5 Dec 2017 15:54:56 -0500 Received: from mx2.suse.de ([195.135.220.15]:37750 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751112AbdLEUy4 (ORCPT ); Tue, 5 Dec 2017 15:54:56 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id B9585AAB9 for ; Tue, 5 Dec 2017 20:54:54 +0000 (UTC) From: David Disseldorp To: target-devel@vger.kernel.org Cc: Hannes Reinecke , Johannes Thumshirn , David Disseldorp Subject: [RFC PATCH] target: detect read-only from underlying iblock device Date: Tue, 5 Dec 2017 21:54:42 +0100 Message-Id: <20171205205442.25414-1-ddiss@suse.de> X-Mailer: git-send-email 2.13.6 Sender: target-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: target-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP A read-only block device must currently be flagged as such when configuring the iblock backstore, via a readonly=1 configfs parameter. If a read-only block device is used without explicitly providing the parameter, then backstore enablement fails with EACCES, e.g. losetup --read-only /dev/loop0 /lofile mkdir -p /sys/kernel/config/target/core/iblock_0/loopy echo "udev_path=/dev/loop0" \ > /sys/kernel/config/target/core/iblock_0/loopy/control echo "1" > /sys/kernel/config/target/core/iblock_0/loopy/enable sh: echo: write error: Permission denied This change allows an iblock backstore to be demoted to read-only if the underlying device is detected as such, via a -EACCES return value from blkdev_get_by_path(udev_path=$readonly_device, mode & FMODE_WRITE, ...). Signed-off-by: David Disseldorp --- drivers/target/target_core_iblock.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/target/target_core_iblock.c b/drivers/target/target_core_iblock.c index 07c814c42648..cafba262a440 100644 --- a/drivers/target/target_core_iblock.c +++ b/drivers/target/target_core_iblock.c @@ -103,6 +103,7 @@ static int iblock_configure_device(struct se_device *dev) pr_debug( "IBLOCK: Claiming struct block_device: %s\n", ib_dev->ibd_udev_path); +ro_fallback: mode = FMODE_READ|FMODE_EXCL; if (!ib_dev->ibd_readonly) mode |= FMODE_WRITE; @@ -112,6 +113,17 @@ static int iblock_configure_device(struct se_device *dev) bd = blkdev_get_by_path(ib_dev->ibd_udev_path, mode, ib_dev); if (IS_ERR(bd)) { ret = PTR_ERR(bd); + if (!ib_dev->ibd_readonly && (ret == -EACCES)) { + /* + * EACCES is returned if FMODE_WRITE is set while + * getting a read-only block device. Try once again + * without FMODE_WRITE to allow for configuration of a + * read-only block device without an explicit readonly=1 + * configfs parameter in iblock_set_configfs_dev_params(). + */ + ib_dev->ibd_readonly = 1; + goto ro_fallback; + } goto out_free_bioset; } ib_dev->ibd_bd = bd;