From patchwork Wed Nov 1 17:43:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 13442965 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14AA1C0018C for ; Wed, 1 Nov 2023 17:43:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231233AbjKARnf (ORCPT ); Wed, 1 Nov 2023 13:43:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231974AbjKARnd (ORCPT ); Wed, 1 Nov 2023 13:43:33 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E747DC1; Wed, 1 Nov 2023 10:43:27 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 7073C1F74D; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BKgU7PXImH1iO/Gfidz43SCBxUDiM+b1vjlZXIIJoLw=; b=qT0SdyxC7R3WLtfNwZ+LOMxOmkeVc2EUmFHzRsAJQAqtgnzskRtCU1/whUV8EaITQDDtpR 0EHnK+43adFUTMMUHm1pj7fwQVb+9/GhvA8nAHCmMxnRJydjxLD4OqkyjAHK/JojzGkTop w8v5wR8X6uGNdCREOai98OrAlNPMT0g= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BKgU7PXImH1iO/Gfidz43SCBxUDiM+b1vjlZXIIJoLw=; b=gHqMXehBHu4edaJ6i3SAX0Y2E5Dw1F7tX7ckof6E7QNMBJh0ZHpNNq+gRel88g/d6SKrp8 z4/3Lao7E5MzGLCw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 572C013ADB; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id N8rHFD6OQmUlYQAAMHmgww (envelope-from ); Wed, 01 Nov 2023 17:43:26 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id C7ED7A06E5; Wed, 1 Nov 2023 18:43:25 +0100 (CET) From: Jan Kara To: Christian Brauner Cc: , , Jens Axboe , Christoph Hellwig , Kees Cook , syzkaller , Alexander Popov , , Dmitry Vyukov , Jan Kara , Kent Overstreet , Brian Foster , linux-bcachefs@vger.kernel.org Subject: [PATCH 1/7] bcachefs: Convert to bdev_open_by_path() Date: Wed, 1 Nov 2023 18:43:06 +0100 Message-Id: <20231101174325.10596-1-jack@suse.cz> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231101173542.23597-1-jack@suse.cz> References: <20231101173542.23597-1-jack@suse.cz> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2324; i=jack@suse.cz; h=from:subject; bh=Ob0PQzZ8Jq9WpAfJA+ZOLADwBqFzDEL4g6I7733eXBQ=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBlQo4qfY1b5vP0uNrwSfo0fCP5xtWPQ9ln3zTqpPUy F4mIL3aJATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCZUKOKgAKCRCcnaoHP2RA2fAzCA ChaPW1pZOanUyhjpFIhk50n7Cx6o2p9BvjqUsTzxPUUz6G+ZTyh35hpl9h4YHpckxjPFED8MtyL4AM EucBwvED00occzs6RxvHRQMfIGpuS6iqwnKnfZNSOVt/Vu19mh4deH5bpAxLj3anctfGzlJqjtZFOh yIBbw0RN3YnJWFyz6KoEkMbh8NGhNI4UkqhPlp9bPBINY8dXm5C5SKC1rbsMJ86/hW8xQlr2KIYZSc zZbMQOtNeOk4UXvrKV3ofQvEukJdqmHPrGt+i8Hf7Ebn40zaC7IbxeDfNi6PTfNgGI+kHVhmOj8CdC htqlWP5IT3AoEpVEokTfrUCLiA12pA X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Convert bcachefs to use bdev_open_by_path() and pass the handle around. CC: Kent Overstreet CC: Brian Foster CC: linux-bcachefs@vger.kernel.org Signed-off-by: Jan Kara Acked-by: Kent Overstreet --- fs/bcachefs/super-io.c | 19 ++++++++++--------- fs/bcachefs/super_types.h | 1 + 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/fs/bcachefs/super-io.c b/fs/bcachefs/super-io.c index 332d41e1c0a3..01a32c41a540 100644 --- a/fs/bcachefs/super-io.c +++ b/fs/bcachefs/super-io.c @@ -162,8 +162,8 @@ void bch2_sb_field_delete(struct bch_sb_handle *sb, void bch2_free_super(struct bch_sb_handle *sb) { kfree(sb->bio); - if (!IS_ERR_OR_NULL(sb->bdev)) - blkdev_put(sb->bdev, sb->holder); + if (!IS_ERR_OR_NULL(sb->bdev_handle)) + bdev_release(sb->bdev_handle); kfree(sb->holder); kfree(sb->sb); @@ -685,21 +685,22 @@ int bch2_read_super(const char *path, struct bch_opts *opts, if (!opt_get(*opts, nochanges)) sb->mode |= BLK_OPEN_WRITE; - sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); - if (IS_ERR(sb->bdev) && - PTR_ERR(sb->bdev) == -EACCES && + sb->bdev_handle = bdev_open_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); + if (IS_ERR(sb->bdev_handle) && + PTR_ERR(sb->bdev_handle) == -EACCES && opt_get(*opts, read_only)) { sb->mode &= ~BLK_OPEN_WRITE; - sb->bdev = blkdev_get_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); - if (!IS_ERR(sb->bdev)) + sb->bdev_handle = bdev_open_by_path(path, sb->mode, sb->holder, &bch2_sb_handle_bdev_ops); + if (!IS_ERR(sb->bdev_handle)) opt_set(*opts, nochanges, true); } - if (IS_ERR(sb->bdev)) { - ret = PTR_ERR(sb->bdev); + if (IS_ERR(sb->bdev_handle)) { + ret = PTR_ERR(sb->bdev_handle); goto out; } + sb->bdev = sb->bdev_handle->bdev; ret = bch2_sb_realloc(sb, 0); if (ret) { diff --git a/fs/bcachefs/super_types.h b/fs/bcachefs/super_types.h index 78d6138db62d..b77d8897c9fa 100644 --- a/fs/bcachefs/super_types.h +++ b/fs/bcachefs/super_types.h @@ -4,6 +4,7 @@ struct bch_sb_handle { struct bch_sb *sb; + struct bdev_handle *bdev_handle; struct block_device *bdev; struct bio *bio; void *holder; From patchwork Wed Nov 1 17:43:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 13442968 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id DFBFBC00A5A for ; Wed, 1 Nov 2023 17:43:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344526AbjKARnj (ORCPT ); Wed, 1 Nov 2023 13:43:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233698AbjKARnh (ORCPT ); Wed, 1 Nov 2023 13:43:37 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E7584E4; Wed, 1 Nov 2023 10:43:27 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 5D075219EC; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pOWlpyvyqUPB7G5rt7x1BI+QKqrBlv5E40CiHPTM+Kk=; b=mJ7woFMjXCtn39OPjthpw1UF9pYx1Zq56cDJO2SjF8xiH9xOmteSmSKgtANZVe3oIDzpx4 w9uzI8yALv6HiqCosooSTWzhatJxVYwdmkDj1K7v0d5StxDS5kQoQaJ4V9xMzCSR3SYq2R Wb0uy111HWYt+HuZpjEckaWNBOl/QsA= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pOWlpyvyqUPB7G5rt7x1BI+QKqrBlv5E40CiHPTM+Kk=; b=i1N6gTVgnGczSafQ+wX5fig7vT3ziax3S71S08nVTwUhwigtJEJd85iQRvQ90nDNqTZF2C 2UNgaoBKIFhXgLCg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 4D30213A92; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 7qHLEj6OQmUgYQAAMHmgww (envelope-from ); Wed, 01 Nov 2023 17:43:26 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id CE4EDA0759; Wed, 1 Nov 2023 18:43:25 +0100 (CET) From: Jan Kara To: Christian Brauner Cc: , , Jens Axboe , Christoph Hellwig , Kees Cook , syzkaller , Alexander Popov , , Dmitry Vyukov , Jan Kara , Christoph Hellwig Subject: [PATCH 2/7] block: Remove blkdev_get_by_*() functions Date: Wed, 1 Nov 2023 18:43:07 +0100 Message-Id: <20231101174325.10596-2-jack@suse.cz> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231101173542.23597-1-jack@suse.cz> References: <20231101173542.23597-1-jack@suse.cz> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=6836; i=jack@suse.cz; h=from:subject; bh=Cl6eHX1D7EnZV3pE1ncAUp3hwm40HBFOGX+U7sCElXc=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBlQo4r89CA+wmIKuIlHwK7un/fPUGSEqIL0wavySgc VUGA+hqJATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCZUKOKwAKCRCcnaoHP2RA2VnbB/ 9MGSRaVJUfXObNCBHtZLlpveGq32iGDwnfzHK+tjIp88gGD1khx86pGVoGWJ634DqbLVpTyA5iAiGp DC7YlHokAwof9gEF8uoT6t0QvsDtfmeYKpfcngsBuM8bQXNjlXpdAAgicqPWujieiVH/Nwmr/7PXOm 8tDsWGo/3GSsW/8FoJgsGSVp+MaFEt03qFy6FA53nzyOl/gsbwVzWHjJcQoMl0bYOCHWe3l26pBQ5g KSbS1yzMD7GmnLr9PnOE/m2rnA+piyz2a8v7eMcS+pG9XyhEXp4hMtN6Q3mSomTf5EA4XxVZAEHUOU YfZFcIF/qzrcC6yoZ2Gg160ry0ZZGf X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org blkdev_get_by_*() and blkdev_put() functions are now unused. Remove them. Acked-by: Christoph Hellwig Signed-off-by: Jan Kara Reviewed-by: Christian Brauner --- block/bdev.c | 94 ++++++++++++++---------------------------- include/linux/blkdev.h | 5 --- 2 files changed, 30 insertions(+), 69 deletions(-) diff --git a/block/bdev.c b/block/bdev.c index 4a502fb9b814..3f27939e02c6 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -732,7 +732,7 @@ void blkdev_put_no_open(struct block_device *bdev) } /** - * blkdev_get_by_dev - open a block device by device number + * bdev_open_by_dev - open a block device by device number * @dev: device number of block device to open * @mode: open mode (BLK_OPEN_*) * @holder: exclusive holder identifier @@ -744,32 +744,40 @@ void blkdev_put_no_open(struct block_device *bdev) * * Use this interface ONLY if you really do not have anything better - i.e. when * you are behind a truly sucky interface and all you are given is a device - * number. Everything else should use blkdev_get_by_path(). + * number. Everything else should use bdev_open_by_path(). * * CONTEXT: * Might sleep. * * RETURNS: - * Reference to the block_device on success, ERR_PTR(-errno) on failure. + * Handle with a reference to the block_device on success, ERR_PTR(-errno) on + * failure. */ -struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder, - const struct blk_holder_ops *hops) +struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, + const struct blk_holder_ops *hops) { - bool unblock_events = true; + struct bdev_handle *handle = kmalloc(sizeof(struct bdev_handle), + GFP_KERNEL); struct block_device *bdev; + bool unblock_events = true; struct gendisk *disk; int ret; + if (!handle) + return ERR_PTR(-ENOMEM); + ret = devcgroup_check_permission(DEVCG_DEV_BLOCK, MAJOR(dev), MINOR(dev), ((mode & BLK_OPEN_READ) ? DEVCG_ACC_READ : 0) | ((mode & BLK_OPEN_WRITE) ? DEVCG_ACC_WRITE : 0)); if (ret) - return ERR_PTR(ret); + goto free_handle; bdev = blkdev_get_no_open(dev); - if (!bdev) - return ERR_PTR(-ENXIO); + if (!bdev) { + ret = -ENXIO; + goto free_handle; + } disk = bdev->bd_disk; if (holder) { @@ -818,7 +826,10 @@ struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder, if (unblock_events) disk_unblock_events(disk); - return bdev; + handle->bdev = bdev; + handle->holder = holder; + handle->mode = mode; + return handle; put_module: module_put(disk->fops->owner); abort_claiming: @@ -828,34 +839,14 @@ struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder, disk_unblock_events(disk); put_blkdev: blkdev_put_no_open(bdev); +free_handle: + kfree(handle); return ERR_PTR(ret); } -EXPORT_SYMBOL(blkdev_get_by_dev); - -struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, - const struct blk_holder_ops *hops) -{ - struct bdev_handle *handle = kmalloc(sizeof(*handle), GFP_KERNEL); - struct block_device *bdev; - - if (!handle) - return ERR_PTR(-ENOMEM); - bdev = blkdev_get_by_dev(dev, mode, holder, hops); - if (IS_ERR(bdev)) { - kfree(handle); - return ERR_CAST(bdev); - } - handle->bdev = bdev; - handle->holder = holder; - if (holder) - mode |= BLK_OPEN_EXCL; - handle->mode = mode; - return handle; -} EXPORT_SYMBOL(bdev_open_by_dev); /** - * blkdev_get_by_path - open a block device by name + * bdev_open_by_path - open a block device by name * @path: path to the block device to open * @mode: open mode (BLK_OPEN_*) * @holder: exclusive holder identifier @@ -869,29 +860,9 @@ EXPORT_SYMBOL(bdev_open_by_dev); * Might sleep. * * RETURNS: - * Reference to the block_device on success, ERR_PTR(-errno) on failure. + * Handle with a reference to the block_device on success, ERR_PTR(-errno) on + * failure. */ -struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode, - void *holder, const struct blk_holder_ops *hops) -{ - struct block_device *bdev; - dev_t dev; - int error; - - error = lookup_bdev(path, &dev); - if (error) - return ERR_PTR(error); - - bdev = blkdev_get_by_dev(dev, mode, holder, hops); - if (!IS_ERR(bdev) && (mode & BLK_OPEN_WRITE) && bdev_read_only(bdev)) { - blkdev_put(bdev, holder); - return ERR_PTR(-EACCES); - } - - return bdev; -} -EXPORT_SYMBOL(blkdev_get_by_path); - struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode, void *holder, const struct blk_holder_ops *hops) { @@ -914,8 +885,9 @@ struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode, } EXPORT_SYMBOL(bdev_open_by_path); -void blkdev_put(struct block_device *bdev, void *holder) +void bdev_release(struct bdev_handle *handle) { + struct block_device *bdev = handle->bdev; struct gendisk *disk = bdev->bd_disk; /* @@ -929,8 +901,8 @@ void blkdev_put(struct block_device *bdev, void *holder) sync_blockdev(bdev); mutex_lock(&disk->open_mutex); - if (holder) - bd_end_claim(bdev, holder); + if (handle->holder) + bd_end_claim(bdev, handle->holder); /* * Trigger event checking and tell drivers to flush MEDIA_CHANGE @@ -947,12 +919,6 @@ void blkdev_put(struct block_device *bdev, void *holder) module_put(disk->fops->owner); blkdev_put_no_open(bdev); -} -EXPORT_SYMBOL(blkdev_put); - -void bdev_release(struct bdev_handle *handle) -{ - blkdev_put(handle->bdev, handle->holder); kfree(handle); } EXPORT_SYMBOL(bdev_release); diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index abf71cce785c..7afc10315dd5 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1500,10 +1500,6 @@ struct bdev_handle { blk_mode_t mode; }; -struct block_device *blkdev_get_by_dev(dev_t dev, blk_mode_t mode, void *holder, - const struct blk_holder_ops *hops); -struct block_device *blkdev_get_by_path(const char *path, blk_mode_t mode, - void *holder, const struct blk_holder_ops *hops); struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, const struct blk_holder_ops *hops); struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode, @@ -1511,7 +1507,6 @@ struct bdev_handle *bdev_open_by_path(const char *path, blk_mode_t mode, int bd_prepare_to_claim(struct block_device *bdev, void *holder, const struct blk_holder_ops *hops); void bd_abort_claiming(struct block_device *bdev, void *holder); -void blkdev_put(struct block_device *bdev, void *holder); void bdev_release(struct bdev_handle *handle); /* just for blk-cgroup, don't use elsewhere */ From patchwork Wed Nov 1 17:43:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 13442962 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7345FC4167B for ; Wed, 1 Nov 2023 17:43:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232487AbjKARne (ORCPT ); Wed, 1 Nov 2023 13:43:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231233AbjKARnd (ORCPT ); Wed, 1 Nov 2023 13:43:33 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC8C8A2; Wed, 1 Nov 2023 10:43:27 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 52D80219E9; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eMIoF0inTaosbwWde8+j7j13+H+INUH3fz6LsHWjOU4=; b=w8FV8ScfCq87E5op8uCEbUPpwD0FAOB4fUMs2eIU82Jpk3B+dUJiNF7UqbVJAFLqBYNwh9 I6nwklwiQRRw0p41XEstZurH7zM6N0b0XoS8hlJDZ5+IdBvD8NwmDkqHuB3fpuNX82QUuU ZxeQkb1CmLhchKNZVX635hQl0GKo8o8= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=eMIoF0inTaosbwWde8+j7j13+H+INUH3fz6LsHWjOU4=; b=0nBcvimER85KlyUHmdGE5E2igEJJwI0Cc88vu6Wok/vTeK+EzoN6qWLCt1kJRAnQLoWNiS 3p3Rr8bY/ymFQQCA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 434BE1348D; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id jLFkED6OQmUeYQAAMHmgww (envelope-from ); Wed, 01 Nov 2023 17:43:26 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id D724CA0766; Wed, 1 Nov 2023 18:43:25 +0100 (CET) From: Jan Kara To: Christian Brauner Cc: , , Jens Axboe , Christoph Hellwig , Kees Cook , syzkaller , Alexander Popov , , Dmitry Vyukov , Jan Kara Subject: [PATCH 3/7] block: Add config option to not allow writing to mounted devices Date: Wed, 1 Nov 2023 18:43:08 +0100 Message-Id: <20231101174325.10596-3-jack@suse.cz> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231101173542.23597-1-jack@suse.cz> References: <20231101173542.23597-1-jack@suse.cz> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=7299; i=jack@suse.cz; h=from:subject; bh=Io/HLB5m0IwEZ8NtYmCUZJhK6j2St7OcHbbQHkzcxho=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBlQo4sjypRwYB9k5A709UjDC3q/Jz8wurCuh+Aue8h FNhtX0+JATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCZUKOLAAKCRCcnaoHP2RA2c5rB/ wL3Az6qAn6ilgz/Gy0iFe+EF5zW3WUxaGfn30PQtP7FNkIyiOsC0d20VyhKKEeWeIlv2bwDAnjUDex v2teGClU+4FPCGujM4LlJlG2K/kgi8YNBn7EDA6dooQINEZ5r3Ne1yuV/Dl2ejt0OD+xgYJt4q+s6G hlvZA/SgPx1lPpeRpjXD3LGBgzQMkPl9iijTbyzzYYY1turcSDkxNhgPqQ98aP3NJ57IN49lLxWvQn jfteyV4NTPIpChHBq50T0aLOZLb1fhTuaK5gLOlEdMl+jbYFFNZ+D21h8UsALl/eb58YbwxYaatFlS ih9c/W2f//+sOQuJOhKbcyROyoLgkV X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Writing to mounted devices is dangerous and can lead to filesystem corruption as well as crashes. Furthermore syzbot comes with more and more involved examples how to corrupt block device under a mounted filesystem leading to kernel crashes and reports we can do nothing about. Add tracking of writers to each block device and a kernel cmdline argument which controls whether other writeable opens to block devices open with BLK_OPEN_RESTRICT_WRITES flag are allowed. We will make filesystems use this flag for used devices. Note that this effectively only prevents modification of the particular block device's page cache by other writers. The actual device content can still be modified by other means - e.g. by issuing direct scsi commands, by doing writes through devices lower in the storage stack (e.g. in case loop devices, DM, or MD are involved) etc. But blocking direct modifications of the block device page cache is enough to give filesystems a chance to perform data validation when loading data from the underlying storage and thus prevent kernel crashes. Syzbot can use this cmdline argument option to avoid uninteresting crashes. Also users whose userspace setup does not need writing to mounted block devices can set this option for hardening. Link: https://lore.kernel.org/all/60788e5d-5c7c-1142-e554-c21d709acfd9@linaro.org Signed-off-by: Jan Kara --- block/Kconfig | 20 +++++++++++++ block/bdev.c | 62 ++++++++++++++++++++++++++++++++++++++- include/linux/blk_types.h | 1 + include/linux/blkdev.h | 2 ++ 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/block/Kconfig b/block/Kconfig index f1364d1c0d93..ca04b657e058 100644 --- a/block/Kconfig +++ b/block/Kconfig @@ -78,6 +78,26 @@ config BLK_DEV_INTEGRITY_T10 select CRC_T10DIF select CRC64_ROCKSOFT +config BLK_DEV_WRITE_MOUNTED + bool "Allow writing to mounted block devices" + default y + help + When a block device is mounted, writing to its buffer cache is very + likely going to cause filesystem corruption. It is also rather easy to + crash the kernel in this way since the filesystem has no practical way + of detecting these writes to buffer cache and verifying its metadata + integrity. However there are some setups that need this capability + like running fsck on read-only mounted root device, modifying some + features on mounted ext4 filesystem, and similar. If you say N, the + kernel will prevent processes from writing to block devices that are + mounted by filesystems which provides some more protection from runaway + privileged processes and generally makes it much harder to crash + filesystem drivers. Note however that this does not prevent + underlying device(s) from being modified by other means, e.g. by + directly submitting SCSI commands or through access to lower layers of + storage stack. If in doubt, say Y. The configuration can be overridden + with the bdev_allow_write_mounted boot option. + config BLK_DEV_ZONED bool "Zoned block device support" select MQ_IOSCHED_DEADLINE diff --git a/block/bdev.c b/block/bdev.c index 3f27939e02c6..d75dd7dd2b31 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -30,6 +30,9 @@ #include "../fs/internal.h" #include "blk.h" +/* Should we allow writing to mounted block devices? */ +static bool bdev_allow_write_mounted = IS_ENABLED(CONFIG_BLK_DEV_WRITE_MOUNTED); + struct bdev_inode { struct block_device bdev; struct inode vfs_inode; @@ -730,7 +733,34 @@ void blkdev_put_no_open(struct block_device *bdev) { put_device(&bdev->bd_device); } - + +static bool bdev_writes_blocked(struct block_device *bdev) +{ + return bdev->bd_writers == -1; +} + +static void bdev_block_writes(struct block_device *bdev) +{ + bdev->bd_writers = -1; +} + +static void bdev_unblock_writes(struct block_device *bdev) +{ + bdev->bd_writers = 0; +} + +static bool blkdev_open_compatible(struct block_device *bdev, blk_mode_t mode) +{ + if (!bdev_allow_write_mounted) { + /* Writes blocked? */ + if (mode & BLK_OPEN_WRITE && bdev_writes_blocked(bdev)) + return false; + if (mode & BLK_OPEN_RESTRICT_WRITES && bdev->bd_writers > 0) + return false; + } + return true; +} + /** * bdev_open_by_dev - open a block device by device number * @dev: device number of block device to open @@ -773,6 +803,10 @@ struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, if (ret) goto free_handle; + /* Blocking writes requires exclusive opener */ + if (mode & BLK_OPEN_RESTRICT_WRITES && !holder) + return ERR_PTR(-EINVAL); + bdev = blkdev_get_no_open(dev); if (!bdev) { ret = -ENXIO; @@ -800,12 +834,21 @@ struct bdev_handle *bdev_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, goto abort_claiming; if (!try_module_get(disk->fops->owner)) goto abort_claiming; + ret = -EBUSY; + if (!blkdev_open_compatible(bdev, mode)) + goto abort_claiming; if (bdev_is_partition(bdev)) ret = blkdev_get_part(bdev, mode); else ret = blkdev_get_whole(bdev, mode); if (ret) goto put_module; + if (!bdev_allow_write_mounted) { + if (mode & BLK_OPEN_RESTRICT_WRITES) + bdev_block_writes(bdev); + else if (mode & BLK_OPEN_WRITE) + bdev->bd_writers++; + } if (holder) { bd_finish_claiming(bdev, holder, hops); @@ -901,6 +944,14 @@ void bdev_release(struct bdev_handle *handle) sync_blockdev(bdev); mutex_lock(&disk->open_mutex); + if (!bdev_allow_write_mounted) { + /* The exclusive opener was blocking writes? Unblock them. */ + if (handle->mode & BLK_OPEN_RESTRICT_WRITES) + bdev_unblock_writes(bdev); + else if (handle->mode & BLK_OPEN_WRITE) + bdev->bd_writers--; + } + if (handle->holder) bd_end_claim(bdev, handle->holder); @@ -1069,3 +1120,12 @@ void bdev_statx_dioalign(struct inode *inode, struct kstat *stat) blkdev_put_no_open(bdev); } + +static int __init setup_bdev_allow_write_mounted(char *str) +{ + if (kstrtobool(str, &bdev_allow_write_mounted)) + pr_warn("Invalid option string for bdev_allow_write_mounted:" + " '%s'\n", str); + return 1; +} +__setup("bdev_allow_write_mounted=", setup_bdev_allow_write_mounted); diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h index 749203277fee..52e264d5a830 100644 --- a/include/linux/blk_types.h +++ b/include/linux/blk_types.h @@ -66,6 +66,7 @@ struct block_device { #ifdef CONFIG_FAIL_MAKE_REQUEST bool bd_make_it_fail; #endif + int bd_writers; /* * keep this out-of-line as it's both big and not needed in the fast * path diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 7afc10315dd5..0e0c0186aa32 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -124,6 +124,8 @@ typedef unsigned int __bitwise blk_mode_t; #define BLK_OPEN_NDELAY ((__force blk_mode_t)(1 << 3)) /* open for "writes" only for ioctls (specialy hack for floppy.c) */ #define BLK_OPEN_WRITE_IOCTL ((__force blk_mode_t)(1 << 4)) +/* open is exclusive wrt all other BLK_OPEN_WRITE opens to the device */ +#define BLK_OPEN_RESTRICT_WRITES ((__force blk_mode_t)(1 << 5)) struct gendisk { /* From patchwork Wed Nov 1 17:43:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 13442967 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5FAA6C04A92 for ; Wed, 1 Nov 2023 17:43:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232170AbjKARnk (ORCPT ); Wed, 1 Nov 2023 13:43:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233957AbjKARnh (ORCPT ); Wed, 1 Nov 2023 13:43:37 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39E4710E; Wed, 1 Nov 2023 10:43:28 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 7001621A3E; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gAPb7AUyS8Sa7nt7J0Gh+N9Q7PPxJ73EwS5LWyR+yVM=; b=APcl+86OgTYkYFTAidJBzWL2x20ZzMNtZySekTI6YWTeppZB7Y4e7/WdgH+iOJMutGP3zu Is8213PlPglYz+YI2u8WE6M3MKyBimVDHCbZ7gaUa0iWRAI8xXx8cCeqrheMpIRjBa/4Je Z9EwxItfKVNdRNJRNm0E9PxJ+s1Ux/E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gAPb7AUyS8Sa7nt7J0Gh+N9Q7PPxJ73EwS5LWyR+yVM=; b=acpl0gTtnUUfB0RLR987knsM3JWjNCcCaQIlp1lqN/WIm18XtbHrdBJUfLRa4ymF1R/Je5 erXEyBrAeLoIdsDw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 5D6BF1348D; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id Z0fHFj6OQmUmYQAAMHmgww (envelope-from ); Wed, 01 Nov 2023 17:43:26 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id DCEFBA0767; Wed, 1 Nov 2023 18:43:25 +0100 (CET) From: Jan Kara To: Christian Brauner Cc: , , Jens Axboe , Christoph Hellwig , Kees Cook , syzkaller , Alexander Popov , , Dmitry Vyukov , Jan Kara , linux-btrfs@vger.kernel.org, David Sterba , Josef Bacik , Chris Mason Subject: [PATCH 4/7] btrfs: Do not restrict writes to btrfs devices Date: Wed, 1 Nov 2023 18:43:09 +0100 Message-Id: <20231101174325.10596-4-jack@suse.cz> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231101173542.23597-1-jack@suse.cz> References: <20231101173542.23597-1-jack@suse.cz> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=1121; i=jack@suse.cz; h=from:subject; bh=pEwCsUz1St/xR0Od9vpX4Oa1kDf92xqHr2SB5l976Yo=; b=owGbwMvMwME4Z+4qdvsUh5uMp9WSGFKd+nSniFlMt2oqNUz5OS2/U2bLxGidPevbJ87jnfaswy5H WWlTJ6MxCwMjB4OsmCLL6siL2tfmGXVtDdWQgRnEygQyhYGLUwAmErmcg6E7l9vGnE2y5kiInuaXRk ONX2yCO/I8b98Se81raHnQ391Bcl3pL6/Q33lXDR2SU546nn/p/OhW88bZjWduTU1uuRtV326zrfBl okbCR24mwW3X0hd7N81oN8xcGz6F8Y44d8rKl6Ir9A/P5dX+0rP49Zm9lp4b/jpWvtyifEy9zlP6tS aHZsuf/ZVr7+fH3H8ucyIvKJNjY0PuzfZApy25Xnlh1ZO0+D72Tjsa31Gwr0N8cS17SNif8pSIQxqf vE8wR811a6yL4Z6YrO8ccCdgX2RKaaWxaqK15av25kZm/dmbf1/czBEV8cgs91FZTsxnVz6pvy+374 rola7g9p66++++vRG6b+xSbz0GAA== X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Btrfs device probing code needs adaptation so that it works when writes are restricted to its mounted devices. Since btrfs maintainer wants to merge these changes through btrfs tree and there are review bandwidth issues with that, let's not block all other filesystems and just not restrict writes to btrfs devices for now. CC: linux-btrfs@vger.kernel.org CC: David Sterba CC: Josef Bacik CC: Chris Mason Signed-off-by: Jan Kara Acked-by: David Sterba --- fs/btrfs/super.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c index 6ecf78d09694..0ceeb9517177 100644 --- a/fs/btrfs/super.c +++ b/fs/btrfs/super.c @@ -1403,6 +1403,8 @@ static struct dentry *btrfs_mount_root(struct file_system_type *fs_type, return ERR_PTR(error); } + /* No support for restricting writes to btrfs devices yet... */ + mode &= ~BLK_OPEN_RESTRICT_WRITES; /* * Setup a dummy root and fs_info for test/set super. This is because * we don't actually fill this stuff out until open_ctree, but we need From patchwork Wed Nov 1 17:43:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 13442969 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B62EFC04E83 for ; Wed, 1 Nov 2023 17:43:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344596AbjKARnm (ORCPT ); Wed, 1 Nov 2023 13:43:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233394AbjKARni (ORCPT ); Wed, 1 Nov 2023 13:43:38 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56CFC115; Wed, 1 Nov 2023 10:43:33 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id B881A21A41; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BsxXilB8VtCWD9uJ48g6IisnMmSxhnqquY6XcPZlAKQ=; b=qfnzs8DT7yNoCyRCiEfgwweelCcCNnhDKYjy65J8od5FjaRZYtuQ+qQJhVUCDKTN3Ky0kS UhI8vabTpxc8luwHCzWIUJCnps+IwZP9QtTy5BDSSldLovGbrXcHaGu3Kf97dp+LDqjFD2 D4mtr1ewM2NojGSIJlu+zDox8YodfSM= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BsxXilB8VtCWD9uJ48g6IisnMmSxhnqquY6XcPZlAKQ=; b=qNX80e5RN09U7lmTJyabsszy6iJuhCmAGejuFXQWPv9qncGkn6WSRUk3YoTtlkdm5lnPIT ozEvR31D4EutLUCg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id A9C821348D; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id d8FuKT6OQmUuYQAAMHmgww (envelope-from ); Wed, 01 Nov 2023 17:43:26 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id E3E89A076D; Wed, 1 Nov 2023 18:43:25 +0100 (CET) From: Jan Kara To: Christian Brauner Cc: , , Jens Axboe , Christoph Hellwig , Kees Cook , syzkaller , Alexander Popov , , Dmitry Vyukov , Jan Kara Subject: [PATCH 5/7] fs: Block writes to mounted block devices Date: Wed, 1 Nov 2023 18:43:10 +0100 Message-Id: <20231101174325.10596-5-jack@suse.cz> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231101173542.23597-1-jack@suse.cz> References: <20231101173542.23597-1-jack@suse.cz> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=744; i=jack@suse.cz; h=from:subject; bh=T/eItSVb7GcAYjznkgJHNYn7HD+3ITlTRTvdImRwJ2E=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBlQo4ubiwBwif8qp1536oKopFx3GfrPsJ5t7c/7aOj 1jG1waqJATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCZUKOLgAKCRCcnaoHP2RA2TkYCA DrJ56qaR1IQHLAf07UimuEs8suyyRufEgDz9zttWEJ7ZIIN0LmxsFinrw7ZJWHbbV/DrKpB0oeX/Uh xE8r6AHulGkMbasTJNABoZDxovW6HqZ8QEWh+1/RdG5p6NloxvvMCdDHILjDQwJtQN6eDfdGuAVjSI q3WTHOfnkh6x6cpuQWEUwTLQWm8FBAOb2sKa3PQaN2Qnus+ukMGqF0JZhX9JpkO46S9ielqaiW3ZUi xuyR4j/7LyNTtjxEDWuQwZT62dkwA3JgaNu6tr1+Gvc1svvBjzwVgf/8pldrHC3dC5i+lmG53BCZTx wzY85qVmI3fqOg587aQp0FyMKTd0oh X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Ask block layer to block writes to block devices mounted by filesystems. Signed-off-by: Jan Kara Reviewed-by: Christian Brauner --- include/linux/blkdev.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 0e0c0186aa32..9f6c3373f9fc 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1494,7 +1494,8 @@ extern const struct blk_holder_ops fs_holder_ops; * as stored in sb->s_flags. */ #define sb_open_mode(flags) \ - (BLK_OPEN_READ | (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE)) + (BLK_OPEN_READ | BLK_OPEN_RESTRICT_WRITES | \ + (((flags) & SB_RDONLY) ? 0 : BLK_OPEN_WRITE)) struct bdev_handle { struct block_device *bdev; From patchwork Wed Nov 1 17:43:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 13442966 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C850C04A95 for ; Wed, 1 Nov 2023 17:43:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344540AbjKARnl (ORCPT ); Wed, 1 Nov 2023 13:43:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233960AbjKARnh (ORCPT ); Wed, 1 Nov 2023 13:43:37 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 54657111; Wed, 1 Nov 2023 10:43:28 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id B87B821A40; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=z+7Wpp/E2EdiZofVxhh2+hzQImgeg1xdtRejCKkMdy0=; b=fZfUCUm4xM5dJrCz+vcQLBI5c2/W8KX6Ldl0KACQbef+PgyVMxVxZq6EJ+hQ3CGQiFuhbA IOhJv9/cpQxd9egVJxnj0Qom1eJW/IkVPuPgXvkk4dup6+Kpl+uyIaNgsUSV/tDVSXFcLA WzvudiTgSdGb6uqQF51MZgnEiAMeWag= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=z+7Wpp/E2EdiZofVxhh2+hzQImgeg1xdtRejCKkMdy0=; b=IIiAq//S5YNMXLq/G4VScKDLNn9KndjS3FmVu8IYFB9R3YS00y0p7zCYYcaVJASuq0MtYH 93BdKwVQOc3I0ZBA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id AA76513A92; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id l2+ZKT6OQmUvYQAAMHmgww (envelope-from ); Wed, 01 Nov 2023 17:43:26 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id EB968A07C3; Wed, 1 Nov 2023 18:43:25 +0100 (CET) From: Jan Kara To: Christian Brauner Cc: , , Jens Axboe , Christoph Hellwig , Kees Cook , syzkaller , Alexander Popov , , Dmitry Vyukov , Jan Kara , "Darrick J . Wong" Subject: [PATCH 6/7] xfs: Block writes to log device Date: Wed, 1 Nov 2023 18:43:11 +0100 Message-Id: <20231101174325.10596-6-jack@suse.cz> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231101173542.23597-1-jack@suse.cz> References: <20231101173542.23597-1-jack@suse.cz> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=835; i=jack@suse.cz; h=from:subject; bh=0ohImTI7LWBrWprfMwJTEIQpEG4d+p/gxEPGWo26suU=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBlQo4vhDMGgCebAqMpWVpQbE0zs5AajPPDCxCP+6JG cQYw/H6JATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCZUKOLwAKCRCcnaoHP2RA2YEVCA DeHIHP2RD6bzLKIacMsYkpyVlZBCEV1xHudpKCL+HY3v7+/D2leHGekabtfj8pop2aBJ+vTGppmD7Z kQs2+mpgkW9xBzrE7NCJHi1JvgW+/48bivCd7iAh8uxRqMy797/nbqC3D7aPq4S54WNtcTqdo6PgUj 0ARxSXgu/hVWx772qTLM6pH8xcU9ay5s5YXpyA5F03lQk8IiXB0pKlIpDlBPEfMZVONnvrOHHamTIq /mzhCUB6oSYClZU5MNQWWHEiv1hxtbIwYKnFqtkmlvbpL05kY3Q04LIaet4cB4ggA4zxkHzII3RVad hI+FZpmAE5SThz0plXx+FmunZjadcJ X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Ask block layer to not allow other writers to open block devices used for xfs log and realtime devices. Reviewed-by: Darrick J. Wong Signed-off-by: Jan Kara --- fs/xfs/xfs_super.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fs/xfs/xfs_super.c b/fs/xfs/xfs_super.c index 84107d162e41..2f29a6810e6a 100644 --- a/fs/xfs/xfs_super.c +++ b/fs/xfs/xfs_super.c @@ -365,8 +365,9 @@ xfs_blkdev_get( { int error = 0; - *handlep = bdev_open_by_path(name, BLK_OPEN_READ | BLK_OPEN_WRITE, - mp->m_super, &fs_holder_ops); + *handlep = bdev_open_by_path(name, + BLK_OPEN_READ | BLK_OPEN_WRITE | BLK_OPEN_RESTRICT_WRITES, + mp->m_super, &fs_holder_ops); if (IS_ERR(*handlep)) { error = PTR_ERR(*handlep); *handlep = NULL; From patchwork Wed Nov 1 17:43:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kara X-Patchwork-Id: 13442964 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB00EC001DB for ; Wed, 1 Nov 2023 17:43:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234146AbjKARnj (ORCPT ); Wed, 1 Nov 2023 13:43:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231974AbjKARnh (ORCPT ); Wed, 1 Nov 2023 13:43:37 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39F1710F; Wed, 1 Nov 2023 10:43:28 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id C87EF1F750; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=InyU6IkAENGRjX63BgJMYCZOVDU0Rjela/KjykemRY8=; b=cGvBoQWo6D5nEnFeGQgDTCi7QnT4wupD/QljAmbwDHbcZn1j72Fs027p3jgbph1PW1A1Ov 4RGDW1H9TO1gKksQUlT2Dfv5TC1rlywu/rq+NpGM3uj8f/fWoVoq61jAxnpacxHmwRogtv 9/Rwa1jVqfe7TEruhKZlCtnL88z5wrU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1698860606; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=InyU6IkAENGRjX63BgJMYCZOVDU0Rjela/KjykemRY8=; b=25+c9h+abDDuZy8BL0dq8HZ5e9TOM6rvIzab1fUAvdd5zInZqzbrBG7auy13t6iaXx6be+ 2t0bBScapqHCRUAA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id B9C3F13ACD; Wed, 1 Nov 2023 17:43:26 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id SEVWLT6OQmUxYQAAMHmgww (envelope-from ); Wed, 01 Nov 2023 17:43:26 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id F21E3A07C6; Wed, 1 Nov 2023 18:43:25 +0100 (CET) From: Jan Kara To: Christian Brauner Cc: , , Jens Axboe , Christoph Hellwig , Kees Cook , syzkaller , Alexander Popov , , Dmitry Vyukov , Jan Kara Subject: [PATCH 7/7] ext4: Block writes to journal device Date: Wed, 1 Nov 2023 18:43:12 +0100 Message-Id: <20231101174325.10596-7-jack@suse.cz> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20231101173542.23597-1-jack@suse.cz> References: <20231101173542.23597-1-jack@suse.cz> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=868; i=jack@suse.cz; h=from:subject; bh=HuGESsNPSj9Qv49kiHt3CDXd9GHUB2GbWU0FnD28gkY=; b=owEBbQGS/pANAwAIAZydqgc/ZEDZAcsmYgBlQo4wQ8aD+R/KnoCeojDhZLI5knNBhVsl/OjJ/o0Z +jjVtsuJATMEAAEIAB0WIQSrWdEr1p4yirVVKBycnaoHP2RA2QUCZUKOMAAKCRCcnaoHP2RA2bkqCA DeV0ajP8MvFxfY0SoPVdnFN+Ci0WI0FX3TtMOzHvHlDPwzVSS295Td2GrHVubbNYSOWjLVrwpGyMA4 kcw2C27bO5IRVJk1QfgND8bVdjQoUJ86gkkHIqn3DR5rWR0DrOUTI3McIIB40TyiqgLwUbFPX8AECn bqxoM3P/HvtT4+6P5fo1hHxSwo4aV8skY99gz6jtizORzxzu5wSvbhaqpcH92xUAwjlu5D5BDDDhyE iQWsrne50JSw2iZaHLv8CaB2ofc2CUNj1TvxPr5vqbVXPG/AloXQaIwtcjesSinypusiMIepQzRzXz FU/ETjwNyO43qynz7or1O0fn6F1jO+ X-Developer-Key: i=jack@suse.cz; a=openpgp; fpr=93C6099A142276A28BBE35D815BC833443038D8C Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org Ask block layer to not allow other writers to open block device used for ext4 journal. Signed-off-by: Jan Kara --- fs/ext4/super.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fs/ext4/super.c b/fs/ext4/super.c index 439e37ac219b..f96398f8aac9 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -5855,8 +5855,9 @@ static struct bdev_handle *ext4_get_journal_blkdev(struct super_block *sb, struct ext4_super_block *es; int errno; - bdev_handle = bdev_open_by_dev(j_dev, BLK_OPEN_READ | BLK_OPEN_WRITE, - sb, &fs_holder_ops); + bdev_handle = bdev_open_by_dev(j_dev, + BLK_OPEN_READ | BLK_OPEN_WRITE | BLK_OPEN_RESTRICT_WRITES, + sb, &fs_holder_ops); if (IS_ERR(bdev_handle)) { ext4_msg(sb, KERN_ERR, "failed to open journal device unknown-block(%u,%u) %ld",