From patchwork Wed Jan 10 16:18:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Dryomov X-Patchwork-Id: 10155539 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 1150E60231 for ; Wed, 10 Jan 2018 16:19:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 03A7628414 for ; Wed, 10 Jan 2018 16:19:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EC68D2854A; Wed, 10 Jan 2018 16:19:10 +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, FREEMAIL_FROM, 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 6B3EA28414 for ; Wed, 10 Jan 2018 16:19:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965945AbeAJQTJ (ORCPT ); Wed, 10 Jan 2018 11:19:09 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:46909 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S965951AbeAJQTI (ORCPT ); Wed, 10 Jan 2018 11:19:08 -0500 Received: by mail-wm0-f68.google.com with SMTP id 143so24134wma.5 for ; Wed, 10 Jan 2018 08:19:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=yPVp4lvCFJ/pcZS2bJItcQwE6+IKkWYYY/wjkmdDEQ8=; b=Vxicy04puXZoFYwD87XXttnPmebLEDNqKU57jNBPaElnTb6CmHaysqxk8r7LTu52T+ LkihBKhE0dN8IcUnc+1z6E1XvXamYhSi+VZieH74u642ynmTOZ+Ob7l3VwGKBO1h1XP4 stCcgljv6gWXQd2TEgizzq6Z9kHUXqjvJFX5NRjh4nmcaDwYT/j0KzZCBkOQ2booPj5N 5zH2r0DK6WqFPOyKKGXqw6O5qpqBJH7rObiWQ+snQSSDabKkZGWnlu2U8kd4LWYgADLb I8MzeuqCwIaPcIbBC8aSSMItvF5D3rDSuSCFCRMvSKBVkjy/zeg+OcF6YUsZQaeHqwXr TSng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=yPVp4lvCFJ/pcZS2bJItcQwE6+IKkWYYY/wjkmdDEQ8=; b=bFfPCLzPJ+ZwD/v3OvThKoMX3jSAOezcZDp7nD16vje8DIunko9fkKTIonAau3tQaE uZRYQeBchxm3Bbob6JHRcBDGDki8IOL4nUI81Wc4B27QQXQGwBgXUgdkXe3uicZIc4x1 WlliXjvforZrcIqmYm4tvDWRvN7EgST4hfJTEQjRH/Gq0bNYEktTi30/bkTFa8+L+hKI 48V/aabZ2zJhSwX/d8fibq4CTceNcoEAyEMWPB6IppNLoTbP3/tJHDVISJRSS/46blpC WxncX6gy4mUsw3fKypPZFosiYfabFBy+OUDVfAHY78nCjp4Le5kfZzxMHXcZqxbs6Pld fhDA== X-Gm-Message-State: AKGB3mKgiyZVflnF9ibCMCeXY4GRXMnNCjrAaFzeb9pnAGSGAQZuQsSz kxA5jnMU4IkFfucEMmPHdCeEssOs X-Google-Smtp-Source: ACJfBotgeRW/bbSYGVctUGB6TWQan4S1nZgovw4w8Qdw34P5zINLkpTuL7pTuAKuItcF+eMCrVS1MA== X-Received: by 10.28.216.79 with SMTP id p76mr14866344wmg.44.1515601146363; Wed, 10 Jan 2018 08:19:06 -0800 (PST) Received: from orange.redhat.com ([213.175.37.12]) by smtp.gmail.com with ESMTPSA id r68sm20250487wmd.47.2018.01.10.08.19.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Jan 2018 08:19:05 -0800 (PST) From: Ilya Dryomov To: linux-block@vger.kernel.org Cc: Jens Axboe , Christoph Hellwig , Tejun Heo , David Disseldorp , Sagi Grimberg Subject: [PATCH v2 1/2] block: fail op_is_write() requests to read-only partitions Date: Wed, 10 Jan 2018 17:18:37 +0100 Message-Id: <1515601118-23192-2-git-send-email-idryomov@gmail.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1515601118-23192-1-git-send-email-idryomov@gmail.com> References: <1515601118-23192-1-git-send-email-idryomov@gmail.com> Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Regular block device writes go through blkdev_write_iter(), which does bdev_read_only(), while zeroout/discard/etc requests are never checked, both userspace- and kernel-triggered. Add a generic catch-all check to generic_make_request_checks() to actually enforce ioctl(BLKROSET) and set_disk_ro(), which is used by quite a few drivers for things like snapshots, read-only backing files/images, etc. Signed-off-by: Ilya Dryomov Reviewed-by: Sagi Grimberg --- block/blk-core.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/block/blk-core.c b/block/blk-core.c index f843ae4f858d..d132bec4a266 100644 --- a/block/blk-core.c +++ b/block/blk-core.c @@ -2123,6 +2123,20 @@ static inline int bio_check_eod(struct bio *bio, unsigned int nr_sectors) return 0; } +static inline bool bio_check_ro(struct bio *bio) +{ + struct hd_struct *p; + bool ret = false; + + rcu_read_lock(); + p = __disk_get_part(bio->bi_disk, bio->bi_partno); + if (!p || (p->policy && op_is_write(bio_op(bio)))) + ret = true; + rcu_read_unlock(); + + return ret; +} + static noinline_for_stack bool generic_make_request_checks(struct bio *bio) { @@ -2145,11 +2159,18 @@ generic_make_request_checks(struct bio *bio) goto end_io; } + if (unlikely(bio_check_ro(bio))) { + printk(KERN_ERR + "generic_make_request: Trying to write " + "to read-only block-device %s (partno %d)\n", + bio_devname(bio, b), bio->bi_partno); + goto end_io; + } + /* * For a REQ_NOWAIT based request, return -EOPNOTSUPP * if queue is not a request based queue. */ - if ((bio->bi_opf & REQ_NOWAIT) && !queue_is_rq_based(q)) goto not_supported;