From patchwork Thu Nov 9 18:45:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Dryomov X-Patchwork-Id: 10051629 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 8F098601EA for ; Thu, 9 Nov 2017 18:45:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 836B42ABC5 for ; Thu, 9 Nov 2017 18:45:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 785232AFD4; Thu, 9 Nov 2017 18:45:39 +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 693222ABC5 for ; Thu, 9 Nov 2017 18:45:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753807AbdKISph (ORCPT ); Thu, 9 Nov 2017 13:45:37 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:52157 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753777AbdKISpf (ORCPT ); Thu, 9 Nov 2017 13:45:35 -0500 Received: by mail-wm0-f68.google.com with SMTP id b9so19780120wmh.0 for ; Thu, 09 Nov 2017 10:45:35 -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=v/SoMT+RJSefQRm9JIWO+jZtyYPD5OkysgBV3nltSxE=; b=tpcNswmGPr9oLHVjP4SK1l4h2PxD7x1m+6iqa3wqMbAQ1ofIpIrTO4POwlsewYd0Od zL2fAdQoEo6l0WiT7kYPbfDPt3Fz7vRX2XeYmku5wtYOYavUPQpBriHJK5quAlGSXbul 2Q7fdilvsDURZ4j+7onMJUqQqKrFyqG20ML/+BRc/eouqn1S2oEcIzoZcY37fbwrdAm4 wrwa0euGwAOraV0E8TLKDjHcML1f/Yyut5DZTsui7o0qFoVpwp9IoxyEYD8SHCjT0H4E CQl7UNqug7wLn/O4dJkKRtCGYhmjvs1qSlvn8unAezEDGSGLuXLFJlfP3mS2MutdbOcv 0A5A== 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=v/SoMT+RJSefQRm9JIWO+jZtyYPD5OkysgBV3nltSxE=; b=W0dnus5oRUrMuEcDLqbsAlBvusI5cdzAVqMQPEIFTzrM/awV7ecdn20PRyiX0QMMv3 FLt2poV1Ys7GwyzN6R9L0e1I8Ns1rJdPSseLTXmxOYFiWFRM5l/Ghg1/8J66s6LxBmwa CfMhFGcV6fCv/LXhWn2ksYAALoDB7lpNWc7wGMAcTeebBep6pdlhy95PZYEAY/KeNRek ZwXP5nRFlXyaaxt3Enom1M5BHZHQVkXP+8kWeANsTVNf7KYyBeRK2FfFiwljOyXu/Fzy Qqk0Xad0TQA/5GJlmnvwF4UxGazUJD97M4K4L3OF9kfSQ58p/i6jydjoFlqv8gFB5fF9 gmjg== X-Gm-Message-State: AJaThX5nKP54OPWEIWB5Mj/mugqiqCtuun7xEB/YxDp+Rfmw02woLe2T 183WWYst7dB3tQls9ueiGdL5CvmH X-Google-Smtp-Source: AGs4zMZ837p8FZZcP4qGkUBhV/YyOmXlfDY0FeNTDSPMMls77tHWmxJf2PPezrFieItjnkax++jSsw== X-Received: by 10.28.215.194 with SMTP id o185mr595243wmg.105.1510253134308; Thu, 09 Nov 2017 10:45:34 -0800 (PST) Received: from orange.local (ip-62-245-115-99.net.upcbroadband.cz. [62.245.115.99]) by smtp.gmail.com with ESMTPSA id x52sm3251569wrb.25.2017.11.09.10.45.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 Nov 2017 10:45:33 -0800 (PST) From: Ilya Dryomov To: linux-block@vger.kernel.org Cc: Christoph Hellwig , Jens Axboe , Tejun Heo , David Disseldorp Subject: [PATCH 1/2] block: fail op_is_write() requests to read-only partitions Date: Thu, 9 Nov 2017 19:45:00 +0100 Message-Id: <1510253101-10291-2-git-send-email-idryomov@gmail.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1510253101-10291-1-git-send-email-idryomov@gmail.com> References: <1510253101-10291-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 b8d1aa2d1008..139ff47caf4a 100644 --- a/block/blk-core.c +++ b/block/blk-core.c @@ -2022,6 +2022,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; + int 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) { @@ -2044,11 +2058,18 @@ generic_make_request_checks(struct bio *bio) goto end_io; } + if (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;