From patchwork Wed Sep 27 13:21:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13400786 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 3446CE7F14E for ; Wed, 27 Sep 2023 13:21:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231913AbjI0NVs (ORCPT ); Wed, 27 Sep 2023 09:21:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231825AbjI0NVp (ORCPT ); Wed, 27 Sep 2023 09:21:45 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8B1E10A for ; Wed, 27 Sep 2023 06:21:43 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 75273C433C8; Wed, 27 Sep 2023 13:21:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695820903; bh=2+q1/rsiNak5I7ASCGHkTOJGLy2SFqNaMqS7c9Nqwws=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=dZS6khex+kXcMX94sPuGzt41I2Hsv1tv6w00hX6rg47v6/pfdyu6O7QAlJTR8p84t c6QeggFBj5LQWkOb2/z48xaRbq282yZEIhKAHR3EuuPkllv3S2ZLKggaaZfkBl2pI1 xd6WrWvH9CkCk70RQfhsDpJTcWBlMniKGfG2vOEqrM51q1X5vTQN1ewItAQOV0W684 HDht8M1CkehBIsJqiPxCI5JdyoNrhfTTKNIcZ8SirwfbvGAgEbcGzf5W0GXt9vtdQw gtGrJjy1tGBmcVngHNN+6YF3RewDwtTmuRqZp67G5/82lDCHCZXZJyT4BsRgb7v9pF fww3yE9JCfmVA== From: Christian Brauner Date: Wed, 27 Sep 2023 15:21:14 +0200 Subject: [PATCH 1/7] bdev: rename freeze and thaw helpers MIME-Version: 1.0 Message-Id: <20230927-vfs-super-freeze-v1-1-ecc36d9ab4d9@kernel.org> References: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> In-Reply-To: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> To: Jan Kara , Christoph Hellwig Cc: linux-fsdevel@vger.kernel.org, "Darrick J. Wong" , Christian Brauner X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=6578; i=brauner@kernel.org; h=from:subject:message-id; bh=2+q1/rsiNak5I7ASCGHkTOJGLy2SFqNaMqS7c9Nqwws=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaSK6CSnrMyvF/YxDl9h4xNzcNe+W1sZ7gXPkqnMOn/olTSv WOqHjlIWBjEuBlkxRRaHdpNwueU8FZuNMjVg5rAygQxh4OIUgIl072Bk6J54VINX+cZOJd2rf43Lz8 7q4Vjcn3XgM//cU0fKLa8sOcPI0PHbrbqb/9DMncpHUxnPbtK/9/rGxH4tm3Nq/+Sc4vfVcAAA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org We have bdev_mark_dead() etc and we're going to move block device freezing to holder ops in the next patch. Make the naming consistent: * freeze_bdev() -> bdev_freeze() * thaw_bdev() -> bdev_thaw() Also document the return code. Signed-off-by: Christian Brauner Reviewed-by: Darrick J. Wong Reviewed-by: Christoph Hellwig Reviewed-by: Jan Kara --- block/bdev.c | 22 +++++++++++++--------- drivers/md/dm.c | 4 ++-- fs/ext4/ioctl.c | 4 ++-- fs/f2fs/file.c | 4 ++-- fs/super.c | 4 ++-- fs/xfs/xfs_fsops.c | 4 ++-- include/linux/blkdev.h | 4 ++-- 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/block/bdev.c b/block/bdev.c index f3b13aa1b7d4..0d27db3e69e7 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -207,18 +207,20 @@ int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend) EXPORT_SYMBOL(sync_blockdev_range); /** - * freeze_bdev - lock a filesystem and force it into a consistent state + * bdev_freeze - lock a filesystem and force it into a consistent state * @bdev: blockdevice to lock * * If a superblock is found on this device, we take the s_umount semaphore * on it to make sure nobody unmounts until the snapshot creation is done. * The reference counter (bd_fsfreeze_count) guarantees that only the last * unfreeze process can unfreeze the frozen filesystem actually when multiple - * freeze requests arrive simultaneously. It counts up in freeze_bdev() and - * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze + * freeze requests arrive simultaneously. It counts up in bdev_freeze() and + * count down in bdev_thaw(). When it becomes 0, thaw_bdev() will unfreeze * actually. + * + * Return: On success zero is returned, negative error code on failure. */ -int freeze_bdev(struct block_device *bdev) +int bdev_freeze(struct block_device *bdev) { struct super_block *sb; int error = 0; @@ -248,15 +250,17 @@ int freeze_bdev(struct block_device *bdev) mutex_unlock(&bdev->bd_fsfreeze_mutex); return error; } -EXPORT_SYMBOL(freeze_bdev); +EXPORT_SYMBOL(bdev_freeze); /** - * thaw_bdev - unlock filesystem + * bdev_thaw - unlock filesystem * @bdev: blockdevice to unlock * - * Unlocks the filesystem and marks it writeable again after freeze_bdev(). + * Unlocks the filesystem and marks it writeable again after bdev_freeze(). + * + * Return: On success zero is returned, negative error code on failure. */ -int thaw_bdev(struct block_device *bdev) +int bdev_thaw(struct block_device *bdev) { struct super_block *sb; int error = -EINVAL; @@ -285,7 +289,7 @@ int thaw_bdev(struct block_device *bdev) mutex_unlock(&bdev->bd_fsfreeze_mutex); return error; } -EXPORT_SYMBOL(thaw_bdev); +EXPORT_SYMBOL(bdev_thaw); /* * pseudo-fs diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 64a1f306c96c..6fa309e8efb0 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -2648,7 +2648,7 @@ static int lock_fs(struct mapped_device *md) WARN_ON(test_bit(DMF_FROZEN, &md->flags)); - r = freeze_bdev(md->disk->part0); + r = bdev_freeze(md->disk->part0); if (!r) set_bit(DMF_FROZEN, &md->flags); return r; @@ -2658,7 +2658,7 @@ static void unlock_fs(struct mapped_device *md) { if (!test_bit(DMF_FROZEN, &md->flags)) return; - thaw_bdev(md->disk->part0); + bdev_thaw(md->disk->part0); clear_bit(DMF_FROZEN, &md->flags); } diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c index 0bfe2ce589e2..c1390219c945 100644 --- a/fs/ext4/ioctl.c +++ b/fs/ext4/ioctl.c @@ -810,11 +810,11 @@ int ext4_force_shutdown(struct super_block *sb, u32 flags) switch (flags) { case EXT4_GOING_FLAGS_DEFAULT: - ret = freeze_bdev(sb->s_bdev); + ret = bdev_freeze(sb->s_bdev); if (ret) return ret; set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); - thaw_bdev(sb->s_bdev); + bdev_thaw(sb->s_bdev); break; case EXT4_GOING_FLAGS_LOGFLUSH: set_bit(EXT4_FLAGS_SHUTDOWN, &sbi->s_ext4_flags); diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index ca5904129b16..c22aeb9ffb61 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -2239,11 +2239,11 @@ static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg) switch (in) { case F2FS_GOING_DOWN_FULLSYNC: - ret = freeze_bdev(sb->s_bdev); + ret = bdev_freeze(sb->s_bdev); if (ret) goto out; f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN); - thaw_bdev(sb->s_bdev); + bdev_thaw(sb->s_bdev); break; case F2FS_GOING_DOWN_METASYNC: /* do checkpoint only */ diff --git a/fs/super.c b/fs/super.c index 2d762ce67f6e..e54866345dc7 100644 --- a/fs/super.c +++ b/fs/super.c @@ -1210,7 +1210,7 @@ static void do_thaw_all_callback(struct super_block *sb) if (born && sb->s_root) { if (IS_ENABLED(CONFIG_BLOCK)) - while (sb->s_bdev && !thaw_bdev(sb->s_bdev)) + while (sb->s_bdev && !bdev_thaw(sb->s_bdev)) pr_warn("Emergency Thaw on %pg\n", sb->s_bdev); thaw_super_locked(sb, FREEZE_HOLDER_USERSPACE); } else { @@ -1501,7 +1501,7 @@ int setup_bdev_super(struct super_block *sb, int sb_flags, /* * Until SB_BORN flag is set, there can be no active superblock * references and thus no filesystem freezing. get_active_super() will - * just loop waiting for SB_BORN so even freeze_bdev() cannot proceed. + * just loop waiting for SB_BORN so even bdev_freeze() cannot proceed. * * It is enough to check bdev was not frozen before we set s_bdev. */ diff --git a/fs/xfs/xfs_fsops.c b/fs/xfs/xfs_fsops.c index 7cb75cb6b8e9..57076a25f17d 100644 --- a/fs/xfs/xfs_fsops.c +++ b/fs/xfs/xfs_fsops.c @@ -482,9 +482,9 @@ xfs_fs_goingdown( { switch (inflags) { case XFS_FSOP_GOING_FLAGS_DEFAULT: { - if (!freeze_bdev(mp->m_super->s_bdev)) { + if (!bdev_freeze(mp->m_super->s_bdev)) { xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT); - thaw_bdev(mp->m_super->s_bdev); + bdev_thaw(mp->m_super->s_bdev); } break; } diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index eef450f25982..bf25b63e13d5 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1530,8 +1530,8 @@ static inline int early_lookup_bdev(const char *pathname, dev_t *dev) } #endif /* CONFIG_BLOCK */ -int freeze_bdev(struct block_device *bdev); -int thaw_bdev(struct block_device *bdev); +int bdev_freeze(struct block_device *bdev); +int bdev_thaw(struct block_device *bdev); struct io_comp_batch { struct request *req_list; From patchwork Wed Sep 27 13:21:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13400787 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 2C8A6E80A8A for ; Wed, 27 Sep 2023 13:21:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231915AbjI0NVt (ORCPT ); Wed, 27 Sep 2023 09:21:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231907AbjI0NVq (ORCPT ); Wed, 27 Sep 2023 09:21:46 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C398121 for ; Wed, 27 Sep 2023 06:21:45 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB430C433C9; Wed, 27 Sep 2023 13:21:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695820905; bh=cN9tY8OGDpR9T5D37vXQPEyp2WgyMszQnYS5olbSug8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ZmM7nnnbAeMM3BytwvXLbN5xstBK4IGnYc47ePZxj/834w4hCsP4Z2Rc7b552soqC 8fROxfqA4OER5/UFELNnFv/tCpYnfNM1JXnuh12k717kub7NFEt95mBek6jhPC7kEY JwEOaSEbpdtwyoOeRtoYmR2/s8i2vpzbSEe4wpRanjtKYsOq3wOBVe5zOPmXu9NWAG je4C/Ip7aZl7TYecDm7H6Qr986TjCaeBQchz/rKlyyfPpIQBkwamYembh5eLTPuIvY Zmyrgu6y4PVflGgGASu8ASmzTfHuYGNcdOE9flM8g8wjxjiCOTOm9UWXHdSoDh7ZyW 8N7LUzSi2FUmw== From: Christian Brauner Date: Wed, 27 Sep 2023 15:21:15 +0200 Subject: [PATCH 2/7] bdev: add freeze and thaw holder operations MIME-Version: 1.0 Message-Id: <20230927-vfs-super-freeze-v1-2-ecc36d9ab4d9@kernel.org> References: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> In-Reply-To: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> To: Jan Kara , Christoph Hellwig Cc: linux-fsdevel@vger.kernel.org, "Darrick J. Wong" , Christian Brauner X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=915; i=brauner@kernel.org; h=from:subject:message-id; bh=cN9tY8OGDpR9T5D37vXQPEyp2WgyMszQnYS5olbSug8=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaSK6KSsU0lyjol42bpwf7Zp5JFzDBJfDq0+tcagdFns72mb wv6t6ihlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZhI4lJGhmXXXeQu+i39E1LV8bb/tH xRbz33FSUzb1GrkOI0j3Ur3zL8Zrn6TeWv/zkZPpcDv79JWGcJbbn3avJT5Rq14Ay96XmneQA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Add block device freeze and thaw holder operations. Follow-up patches will implement block device freeze and thaw based on stuct blk_holder_ops. Signed-off-by: Christian Brauner Reviewed-by: Darrick J. Wong Reviewed-by: Jan Kara --- include/linux/blkdev.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index bf25b63e13d5..f2ddccaaef4d 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1468,6 +1468,16 @@ struct blk_holder_ops { * Sync the file system mounted on the block device. */ void (*sync)(struct block_device *bdev); + + /* + * Freeze the file system mounted on the block device. + */ + int (*freeze)(struct block_device *bdev); + + /* + * Thaw the file system mounted on the block device. + */ + int (*thaw)(struct block_device *bdev); }; extern const struct blk_holder_ops fs_holder_ops; From patchwork Wed Sep 27 13:21:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13400788 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 ABDBDE7F14F for ; Wed, 27 Sep 2023 13:21:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231918AbjI0NVv (ORCPT ); Wed, 27 Sep 2023 09:21:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231869AbjI0NVs (ORCPT ); Wed, 27 Sep 2023 09:21:48 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9A14126 for ; Wed, 27 Sep 2023 06:21:46 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 733C3C433C7; Wed, 27 Sep 2023 13:21:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695820906; bh=e3dxJ8qoiB7NgJZnSkhlqKwYWJ2UUavF00bupdY/SvI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=jCwenzCyccn1PgUNwnq55fUmnCWQ1DT/5f51Vk5GJtUblDMiNSDtJ/o7k+VVd2mEj upGXDjbYCfZ74+ysjWjCmd42d3e4zk1SiaUaok/4Sqzze1Ws0N3h5aIjnkcpuBBoR9 +GnQ9KuQT+N729TsL4REBo89cKOFRr3U6n3sTr+/WLPW/YLxwNhfRriq/M3kPmARgD gl6QwnN46mgx9TA6he14SAndMxo7NqL7gbC2CSgHanFef5RdTq1bM0Ccy+nnHSccuU JAMHpQr+T0t58iUWk3Tic2AjwQ2LpeQrGSkYQhqWCaYxA+0amKdHgaNP7GsSGPp+rC LKSssqBL6IAKg== From: Christian Brauner Date: Wed, 27 Sep 2023 15:21:16 +0200 Subject: [PATCH 3/7] bdev: implement freeze and thaw holder operations MIME-Version: 1.0 Message-Id: <20230927-vfs-super-freeze-v1-3-ecc36d9ab4d9@kernel.org> References: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> In-Reply-To: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> To: Jan Kara , Christoph Hellwig Cc: linux-fsdevel@vger.kernel.org, "Darrick J. Wong" , Christian Brauner X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=8090; i=brauner@kernel.org; h=from:subject:message-id; bh=e3dxJ8qoiB7NgJZnSkhlqKwYWJ2UUavF00bupdY/SvI=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaSK6KS8OGf9UeTCkt3s8fxP3nB+6H6Z1hjIVit15/rm6y8u cH3N6yhlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZhIiC0jw4lXrYwfzzVw1D+JU2PY4f nNkkWhxjKJYYN146nAD6vNJjMyLI09alOusefZyhWxUg5ZEY8C/7odt10Uzl0gmctset6YAQA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org The old method of implementing block device freeze and thaw operations required us to rely on get_active_super() to walk the list of all superblocks on the system to find any superblock that might use the block device. This is wasteful and not very pleasant overall. Now that we can finally go straight from block device to owning superblock things become way simpler. In fact, we can even get rid of bd_fsfreeze_mutex and rely on the holder lock for freezing. We let fs_bdev_thaw() grab its own active reference so we can hold bd_holder_lock across freeze and thaw without risk of deadlocks. That in turn allows us to get rid of bd_fsfreeze_mutex. This means fs_bdev_thaw() might put the last reference to the superblock instead of thaw_super(). This shouldn't be an issue. If it turns out to be one we can reshuffle the code to simply hold s_umount when thaw_super() returns. Thanks to Jan and Christoph who pointed out that bd_fsfreeze_mutex is unnecessary now. Signed-off-by: Christian Brauner --- block/bdev.c | 74 +++++++++++++++++-------------------- fs/super.c | 94 ++++++++++++++++++++++++++++++++++++++++++----- include/linux/blk_types.h | 2 +- 3 files changed, 119 insertions(+), 51 deletions(-) diff --git a/block/bdev.c b/block/bdev.c index 0d27db3e69e7..3deccd0ffcf2 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -222,32 +222,23 @@ EXPORT_SYMBOL(sync_blockdev_range); */ int bdev_freeze(struct block_device *bdev) { - struct super_block *sb; int error = 0; - mutex_lock(&bdev->bd_fsfreeze_mutex); - if (++bdev->bd_fsfreeze_count > 1) - goto done; + mutex_lock(&bdev->bd_holder_lock); - sb = get_active_super(bdev); - if (!sb) - goto sync; - if (sb->s_op->freeze_super) - error = sb->s_op->freeze_super(sb, FREEZE_HOLDER_USERSPACE); - else - error = freeze_super(sb, FREEZE_HOLDER_USERSPACE); - deactivate_super(sb); + if (atomic_inc_return(&bdev->bd_fsfreeze_count) > 1) { + mutex_unlock(&bdev->bd_holder_lock); + return 0; + } - if (error) { - bdev->bd_fsfreeze_count--; - goto done; + if (bdev->bd_holder_ops && bdev->bd_holder_ops->freeze) { + error = bdev->bd_holder_ops->freeze(bdev); + lockdep_assert_not_held(&bdev->bd_holder_lock); + } else { + sync_blockdev(bdev); + mutex_unlock(&bdev->bd_holder_lock); } - bdev->bd_fsfreeze_sb = sb; -sync: - sync_blockdev(bdev); -done: - mutex_unlock(&bdev->bd_fsfreeze_mutex); return error; } EXPORT_SYMBOL(bdev_freeze); @@ -262,31 +253,32 @@ EXPORT_SYMBOL(bdev_freeze); */ int bdev_thaw(struct block_device *bdev) { - struct super_block *sb; - int error = -EINVAL; + int error = 0, nr_freeze; - mutex_lock(&bdev->bd_fsfreeze_mutex); - if (!bdev->bd_fsfreeze_count) - goto out; + mutex_lock(&bdev->bd_holder_lock); - error = 0; - if (--bdev->bd_fsfreeze_count > 0) - goto out; + /* + * If this returns < 0 it means that @bd_fsfreeze_count was + * already 0 and no decrement was performed. + */ + nr_freeze = atomic_dec_if_positive(&bdev->bd_fsfreeze_count); + if (nr_freeze < 0) { + mutex_unlock(&bdev->bd_holder_lock); + return -EINVAL; + } - sb = bdev->bd_fsfreeze_sb; - if (!sb) - goto out; + if (nr_freeze > 0) { + mutex_unlock(&bdev->bd_holder_lock); + return 0; + } + + if (bdev->bd_holder_ops && bdev->bd_holder_ops->thaw) { + error = bdev->bd_holder_ops->thaw(bdev); + lockdep_assert_not_held(&bdev->bd_holder_lock); + } else { + mutex_unlock(&bdev->bd_holder_lock); + } - if (sb->s_op->thaw_super) - error = sb->s_op->thaw_super(sb, FREEZE_HOLDER_USERSPACE); - else - error = thaw_super(sb, FREEZE_HOLDER_USERSPACE); - if (error) - bdev->bd_fsfreeze_count++; - else - bdev->bd_fsfreeze_sb = NULL; -out: - mutex_unlock(&bdev->bd_fsfreeze_mutex); return error; } EXPORT_SYMBOL(bdev_thaw); diff --git a/fs/super.c b/fs/super.c index e54866345dc7..672f1837fbef 100644 --- a/fs/super.c +++ b/fs/super.c @@ -1469,9 +1469,91 @@ static void fs_bdev_sync(struct block_device *bdev) super_unlock_shared(sb); } +static struct super_block *get_bdev_super(const struct block_device *bdev) +{ + struct super_block *sb_bdev = bdev->bd_holder, *sb = NULL; + + if (!sb_bdev) + return NULL; + if (super_lock_excl(sb_bdev) && atomic_inc_not_zero(&sb_bdev->s_active)) + sb = sb_bdev; + super_unlock_excl(sb_bdev); + return sb; +} + +static int fs_bdev_freeze(struct block_device *bdev) + __releases(&bdev->bd_holder_lock) +{ + struct super_block *sb; + int error = 0; + + lockdep_assert_held(&bdev->bd_holder_lock); + + sb = get_bdev_super(bdev); + if (sb) { + if (sb->s_op->freeze_super) + error = sb->s_op->freeze_super(sb, FREEZE_HOLDER_USERSPACE); + else + error = freeze_super(sb, FREEZE_HOLDER_USERSPACE); + if (error) + atomic_dec(&bdev->bd_fsfreeze_count); + } + + /* + * We have grabbed an active reference which means that the + * superblock and the block device cannot go away. But we might + * end up holding the last reference and so end up shutting the + * superblock down. So drop @bdev->bd_holder_lock to avoid + * deadlocks with blkdev_put(). + */ + mutex_unlock(&bdev->bd_holder_lock); + + if (sb) + deactivate_super(sb); + + if (!error) + sync_blockdev(bdev); + + return error; +} + +static int fs_bdev_thaw(struct block_device *bdev) + __releases(&bdev->bd_holder_lock) +{ + struct super_block *sb; + int error; + + lockdep_assert_held(&bdev->bd_holder_lock); + + sb = get_bdev_super(bdev); + if (WARN_ON_ONCE(!sb)) + return -EINVAL; + + if (sb->s_op->thaw_super) + error = sb->s_op->thaw_super(sb, FREEZE_HOLDER_USERSPACE); + else + error = thaw_super(sb, FREEZE_HOLDER_USERSPACE); + if (error) + atomic_inc(&bdev->bd_fsfreeze_count); + + /* + * We have grabbed an active reference which means that the + * superblock and the block device cannot go away. But we might + * end up holding the last reference and so end up shutting the + * superblock down. So drop @bdev->bd_holder_lock to avoid + * deadlocks with blkdev_put(). + */ + mutex_unlock(&bdev->bd_holder_lock); + deactivate_super(sb); + + return error; +} + const struct blk_holder_ops fs_holder_ops = { .mark_dead = fs_bdev_mark_dead, .sync = fs_bdev_sync, + .freeze = fs_bdev_freeze, + .thaw = fs_bdev_thaw, }; EXPORT_SYMBOL_GPL(fs_holder_ops); @@ -1499,15 +1581,10 @@ int setup_bdev_super(struct super_block *sb, int sb_flags, } /* - * Until SB_BORN flag is set, there can be no active superblock - * references and thus no filesystem freezing. get_active_super() will - * just loop waiting for SB_BORN so even bdev_freeze() cannot proceed. - * - * It is enough to check bdev was not frozen before we set s_bdev. + * It is enough to check bdev was not frozen before we set + * s_bdev as freezing will wait until SB_BORN is set. */ - mutex_lock(&bdev->bd_fsfreeze_mutex); - if (bdev->bd_fsfreeze_count > 0) { - mutex_unlock(&bdev->bd_fsfreeze_mutex); + if (atomic_read(&bdev->bd_fsfreeze_count) > 0) { if (fc) warnf(fc, "%pg: Can't mount, blockdev is frozen", bdev); blkdev_put(bdev, sb); @@ -1519,7 +1596,6 @@ int setup_bdev_super(struct super_block *sb, int sb_flags, if (bdev_stable_writes(bdev)) sb->s_iflags |= SB_I_STABLE_WRITES; spin_unlock(&sb_lock); - mutex_unlock(&bdev->bd_fsfreeze_mutex); snprintf(sb->s_id, sizeof(sb->s_id), "%pg", bdev); shrinker_debugfs_rename(&sb->s_shrink, "sb-%s:%s", sb->s_type->name, diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h index d5c5e59ddbd2..88e1848b0869 100644 --- a/include/linux/blk_types.h +++ b/include/linux/blk_types.h @@ -57,7 +57,7 @@ struct block_device { const struct blk_holder_ops *bd_holder_ops; struct mutex bd_holder_lock; /* The counter of freeze processes */ - int bd_fsfreeze_count; + atomic_t bd_fsfreeze_count; int bd_holders; struct kobject *bd_holder_dir; From patchwork Wed Sep 27 13:21:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13400789 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 20659E7F14E for ; Wed, 27 Sep 2023 13:21:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231924AbjI0NVy (ORCPT ); Wed, 27 Sep 2023 09:21:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231906AbjI0NVt (ORCPT ); Wed, 27 Sep 2023 09:21:49 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62B21F5 for ; Wed, 27 Sep 2023 06:21:48 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F1719C433C9; Wed, 27 Sep 2023 13:21:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695820908; bh=6x9XQvQKQKgR2VWtdpDr+idgrWCaL8uQrl0V0nxjp3M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=aC2WVdQJ8HXAe8JDizM9FllqvJo4gHz6cZOUN1f+/CUMbxWNjvh2Sk2xKnMqDNZHw S1x4Z/yUDOkxrG8CuegxVj44TCNqNIycy4BPOV9vmdzE9eOhcRcFNOeH2tBs2LbxnO WbzZzuC+DlA+Z4Bie37+/xpn4QD7Sj2PmhiMIQ4Za4Jj6wecaTib9rsg9mnieyI837 HS/rmiFaAYqXulSAgfQcCb9iHM2Qmi4M5uOSYw/YgPC2pNGqHa/u5olEEdLV4svto+ 6tu+BwHIDOXvmG+qyhY1XgP1lbeRuRST9UFSnS0OaeR9bIy1ytcUaeuL3MknMN/XQP Qjc4ntF1VbaYQ== From: Christian Brauner Date: Wed, 27 Sep 2023 15:21:17 +0200 Subject: [PATCH 4/7] fs: remove get_active_super() MIME-Version: 1.0 Message-Id: <20230927-vfs-super-freeze-v1-4-ecc36d9ab4d9@kernel.org> References: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> In-Reply-To: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> To: Jan Kara , Christoph Hellwig Cc: linux-fsdevel@vger.kernel.org, "Darrick J. Wong" , Christian Brauner X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=2043; i=brauner@kernel.org; h=from:subject:message-id; bh=6x9XQvQKQKgR2VWtdpDr+idgrWCaL8uQrl0V0nxjp3M=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaSK6KR4rFRZrxb7qeq0TmasLf+RmX8PhLmGsE8Tu3bt6gPB 69JsHaUsDGJcDLJiiiwO7Sbhcst5KjYbZWrAzGFlAhnCwMUpABMpC2dkWGE7b6l1tVhxVfifY9YVGw rVpvPv//h73e9L87ad0tqSGcbIcPys58UJL/WZfx2/qG/q/Cd/YxOP++bNrWKiMkGmNw8osAMA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org This function is now unused so remove it. One less function that uses the global superblock list. Signed-off-by: Christian Brauner Reviewed-by: Darrick J. Wong Reviewed-by: Christoph Hellwig Reviewed-by: Jan Kara --- fs/super.c | 28 ---------------------------- include/linux/fs.h | 1 - 2 files changed, 29 deletions(-) diff --git a/fs/super.c b/fs/super.c index 672f1837fbef..181ac8501301 100644 --- a/fs/super.c +++ b/fs/super.c @@ -1016,34 +1016,6 @@ void iterate_supers_type(struct file_system_type *type, EXPORT_SYMBOL(iterate_supers_type); -/** - * get_active_super - get an active reference to the superblock of a device - * @bdev: device to get the superblock for - * - * Scans the superblock list and finds the superblock of the file system - * mounted on the device given. Returns the superblock with an active - * reference or %NULL if none was found. - */ -struct super_block *get_active_super(struct block_device *bdev) -{ - struct super_block *sb; - - if (!bdev) - return NULL; - - spin_lock(&sb_lock); - list_for_each_entry(sb, &super_blocks, s_list) { - if (sb->s_bdev == bdev) { - if (!grab_super(sb)) - return NULL; - super_unlock_excl(sb); - return sb; - } - } - spin_unlock(&sb_lock); - return NULL; -} - struct super_block *user_get_super(dev_t dev, bool excl) { struct super_block *sb; diff --git a/include/linux/fs.h b/include/linux/fs.h index b528f063e8ff..ad0ddc10d560 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -3052,7 +3052,6 @@ extern int vfs_readlink(struct dentry *, char __user *, int); extern struct file_system_type *get_filesystem(struct file_system_type *fs); extern void put_filesystem(struct file_system_type *fs); extern struct file_system_type *get_fs_type(const char *name); -extern struct super_block *get_active_super(struct block_device *bdev); extern void drop_super(struct super_block *sb); extern void drop_super_exclusive(struct super_block *sb); extern void iterate_supers(void (*)(struct super_block *, void *), void *); From patchwork Wed Sep 27 13:21:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13400790 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 7B743E80A8A for ; Wed, 27 Sep 2023 13:21:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231926AbjI0NV4 (ORCPT ); Wed, 27 Sep 2023 09:21:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231907AbjI0NVu (ORCPT ); Wed, 27 Sep 2023 09:21:50 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D99CB10A for ; Wed, 27 Sep 2023 06:21:49 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 785CAC433C8; Wed, 27 Sep 2023 13:21:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695820909; bh=nA+pNkC2onY4V6wYGRls95uNWFt4sgRwu5dgcJrBK4E=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=u74mEHwCoBksQvUjW7ZQ9Mc2ROZj0tnNeymv1MEteDYhhlNslW3TrEFqDJtNF/D2L SWMa+pfCPqDKTHXDN9Uk4JojEdcanGTseDKKmFSloqbn+RHECZXJBHAN14rlQzRVyG IbwAAg2xlK4VYyvS5ScS9YSs0nQX7hczPjl7l4heiXwdhLwtcmKNHjtTTKHWrkrHFc 1XoHEtkjNBU8XNoEK+et/Pea7OumHqwaYfETGhACprYis+aVkpMUGWhU8e6PmzZdfE jr1yKwA59CsmeiHcXWa5XVy2vWqDI5bQ3srcWIVowYxOcLK76se24/DZ1ETknZWF62 CMU6j4T45RUBQ== From: Christian Brauner Date: Wed, 27 Sep 2023 15:21:18 +0200 Subject: [PATCH 5/7] super: remove bd_fsfreeze_{mutex,sb} MIME-Version: 1.0 Message-Id: <20230927-vfs-super-freeze-v1-5-ecc36d9ab4d9@kernel.org> References: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> In-Reply-To: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> To: Jan Kara , Christoph Hellwig Cc: linux-fsdevel@vger.kernel.org, "Darrick J. Wong" , Christian Brauner X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=1613; i=brauner@kernel.org; h=from:subject:message-id; bh=nA+pNkC2onY4V6wYGRls95uNWFt4sgRwu5dgcJrBK4E=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaSK6KS8299W5Xpgxt3qzgs1O5i+3FIQLmL/uGjajBzBgrS1 VWEzO0pZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDKEgYtTACaS5Mfwvypp7yJJvU/vmtvr4nY8a2 V5Y+WyTmbK+mWpqlXSBaVfHzIybPBjai79I3px5u0s28N+VT6Pcr/HX6mcpd+wLDllaaIIEwA= X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Both bd_fsfreeze_mutex and bd_fsfreeze_sb are now unused and can be removed. Also move bd_fsfreeze_count down to not have it weirdly placed in the middle of the holder fields. Suggested-by: Jan Kara Suggested-by: Christoph Hellwig Signed-off-by: Christian Brauner Reviewed-by: Darrick J. Wong Reviewed-by: Jan Kara --- block/bdev.c | 1 - include/linux/blk_types.h | 7 ++----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/block/bdev.c b/block/bdev.c index 3deccd0ffcf2..084855b669f7 100644 --- a/block/bdev.c +++ b/block/bdev.c @@ -392,7 +392,6 @@ struct block_device *bdev_alloc(struct gendisk *disk, u8 partno) mapping_set_gfp_mask(&inode->i_data, GFP_USER); bdev = I_BDEV(inode); - mutex_init(&bdev->bd_fsfreeze_mutex); spin_lock_init(&bdev->bd_size_lock); mutex_init(&bdev->bd_holder_lock); bdev->bd_partno = partno; diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h index 88e1848b0869..0238236852b7 100644 --- a/include/linux/blk_types.h +++ b/include/linux/blk_types.h @@ -56,14 +56,11 @@ struct block_device { void * bd_holder; const struct blk_holder_ops *bd_holder_ops; struct mutex bd_holder_lock; - /* The counter of freeze processes */ - atomic_t bd_fsfreeze_count; int bd_holders; struct kobject *bd_holder_dir; - /* Mutex for freeze */ - struct mutex bd_fsfreeze_mutex; - struct super_block *bd_fsfreeze_sb; + /* The counter of freeze processes */ + atomic_t bd_fsfreeze_count; struct partition_meta_info *bd_meta_info; #ifdef CONFIG_FAIL_MAKE_REQUEST From patchwork Wed Sep 27 13:21:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13400791 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 65F47E7F14E for ; Wed, 27 Sep 2023 13:21:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231925AbjI0NV6 (ORCPT ); Wed, 27 Sep 2023 09:21:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231869AbjI0NVw (ORCPT ); Wed, 27 Sep 2023 09:21:52 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B23111D for ; Wed, 27 Sep 2023 06:21:51 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 00EE5C433CC; Wed, 27 Sep 2023 13:21:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695820911; bh=Ab9aMVTQyLsDIBkwuQkryPznzQAdsSK3MSBOrueCy+U=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=k7vQRcLq/DcjL7CF7yzJFY2xUGadb0qyamYWdZFwVKM70lJJrPrkM/HBRFbbxL3af UxYKehVoL75k14tDYEcTWLT6BZZeT9KUsJBEEtnwtqm0rzJiPxqmrtRy9QMPcTu+dl SX4mN9Fq/gBpoLKus9HFbOFFRTIxBDCYqM7iScuUP4sK6rPiOAUki9Bv3kq+xuRU+5 TZEwpq4YlW4ItruDYXEwSyb3Dp0CkbDiRrQrumLVWdvav+peOQRPTBGt4o4RJYI2Us lubasC1fBUkOcpoMlZyZ8Iknv4IG4NkVMatiZKeTuBWg5mHn6IOmS0IXE9j1EriKXV 1M/i1RP0VNkaA== From: Christian Brauner Date: Wed, 27 Sep 2023 15:21:19 +0200 Subject: [PATCH 6/7] fs: remove unused helper MIME-Version: 1.0 Message-Id: <20230927-vfs-super-freeze-v1-6-ecc36d9ab4d9@kernel.org> References: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> In-Reply-To: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> To: Jan Kara , Christoph Hellwig Cc: linux-fsdevel@vger.kernel.org, "Darrick J. Wong" , Christian Brauner X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=3152; i=brauner@kernel.org; h=from:subject:message-id; bh=Ab9aMVTQyLsDIBkwuQkryPznzQAdsSK3MSBOrueCy+U=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaSK6KRw9PUr6ky4NG322xWSn1v2yXSsKvvQ/nHH5Mv7Y0tv ddZd6yhlYRDjYpAVU2RxaDcJl1vOU7HZKFMDZg4rE8gQBi5OAZjIya8Mf2Xvn0+99dDsTeUSd2tW9g 87Z0R3nNbeEaAk8s/v7f3VC9IY/nusXv3xVHXzBxmmteKp2R8uyq89zRfxaGn0g0muSRYVDgwA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org The grab_super() helper is now only used by grab_super_dead(). Merge the two helpers into one. Signed-off-by: Christian Brauner Reviewed-by: Darrick J. Wong Reviewed-by: Christoph Hellwig Reviewed-by: Jan Kara --- fs/super.c | 44 +++++++------------------------------------- 1 file changed, 7 insertions(+), 37 deletions(-) diff --git a/fs/super.c b/fs/super.c index 181ac8501301..6cdce2b31622 100644 --- a/fs/super.c +++ b/fs/super.c @@ -517,35 +517,6 @@ void deactivate_super(struct super_block *s) EXPORT_SYMBOL(deactivate_super); -/** - * grab_super - acquire an active reference - * @s: reference we are trying to make active - * - * Tries to acquire an active reference. grab_super() is used when we - * had just found a superblock in super_blocks or fs_type->fs_supers - * and want to turn it into a full-blown active reference. grab_super() - * is called with sb_lock held and drops it. Returns 1 in case of - * success, 0 if we had failed (superblock contents was already dead or - * dying when grab_super() had been called). Note that this is only - * called for superblocks not in rundown mode (== ones still on ->fs_supers - * of their type), so increment of ->s_count is OK here. - */ -static int grab_super(struct super_block *s) __releases(sb_lock) -{ - bool born; - - s->s_count++; - spin_unlock(&sb_lock); - born = super_lock_excl(s); - if (born && atomic_inc_not_zero(&s->s_active)) { - put_super(s); - return 1; - } - super_unlock_excl(s); - put_super(s); - return 0; -} - static inline bool wait_dead(struct super_block *sb) { unsigned int flags; @@ -559,7 +530,7 @@ static inline bool wait_dead(struct super_block *sb) } /** - * grab_super_dead - acquire an active reference to a superblock + * grab_super - acquire an active reference to a superblock * @sb: superblock to acquire * * Acquire a temporary reference on a superblock and try to trade it for @@ -570,17 +541,16 @@ static inline bool wait_dead(struct super_block *sb) * Return: This returns true if an active reference could be acquired, * false if not. */ -static bool grab_super_dead(struct super_block *sb) +static bool grab_super(struct super_block *sb) { - sb->s_count++; - if (grab_super(sb)) { + spin_unlock(&sb_lock); + if (super_lock_excl(sb) && atomic_inc_not_zero(&sb->s_active)) { put_super(sb); - lockdep_assert_held(&sb->s_umount); return true; } + super_unlock_excl(sb); wait_var_event(&sb->s_flags, wait_dead(sb)); - lockdep_assert_not_held(&sb->s_umount); put_super(sb); return false; } @@ -831,7 +801,7 @@ struct super_block *sget_fc(struct fs_context *fc, warnfc(fc, "reusing existing filesystem in another namespace not allowed"); return ERR_PTR(-EBUSY); } - if (!grab_super_dead(old)) + if (!grab_super(old)) goto retry; destroy_unused_super(s); return old; @@ -875,7 +845,7 @@ struct super_block *sget(struct file_system_type *type, destroy_unused_super(s); return ERR_PTR(-EBUSY); } - if (!grab_super_dead(old)) + if (!grab_super(old)) goto retry; destroy_unused_super(s); return old; From patchwork Wed Sep 27 13:21:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Brauner X-Patchwork-Id: 13400792 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 2BFACE80A8A for ; Wed, 27 Sep 2023 13:22:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231928AbjI0NV7 (ORCPT ); Wed, 27 Sep 2023 09:21:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231920AbjI0NVy (ORCPT ); Wed, 27 Sep 2023 09:21:54 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EF364126 for ; Wed, 27 Sep 2023 06:21:52 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87E81C433C7; Wed, 27 Sep 2023 13:21:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1695820912; bh=OQoK06lrcpD3fVrzh9jJ814jEBCF92IPWwOUbqrz2fQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=bREBQSlsXSrNTHIIgsZsRlEuXTmRhiMOk5AebtjsC2p5DoxKC15cU/3wVYbII3zlB FXbWpE9XttVQgXM0HhiqPoXd6xPhgQZy1Yse5Jm1JFvm9y34TkGtVZO93qx8LEFXqg 06eztUvnNmzHPwN6MB6lYRujiKpPl+pv6Ld2gV/nlVsz2BjVgj3PW4DXMMtTrqLUPz xVMt7P4uy6FWz0snKBl02Z+Ul7yawdlw+XkGd9hAMn4sJvE23q5YvUIOQKKr9m2uiX HuE8AZhQ+ZDn03TvJkP9zsDz6fwVkFLoW59dtLgC3GEiDJzaaBWOinSuLFdmdKtQDP iapwXFoawi5/A== From: Christian Brauner Date: Wed, 27 Sep 2023 15:21:20 +0200 Subject: [PATCH 7/7] porting: document block device freeze and thaw changes MIME-Version: 1.0 Message-Id: <20230927-vfs-super-freeze-v1-7-ecc36d9ab4d9@kernel.org> References: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> In-Reply-To: <20230927-vfs-super-freeze-v1-0-ecc36d9ab4d9@kernel.org> To: Jan Kara , Christoph Hellwig Cc: linux-fsdevel@vger.kernel.org, "Darrick J. Wong" , Christian Brauner X-Mailer: b4 0.13-dev-0438c X-Developer-Signature: v=1; a=openpgp-sha256; l=1966; i=brauner@kernel.org; h=from:subject:message-id; bh=OQoK06lrcpD3fVrzh9jJ814jEBCF92IPWwOUbqrz2fQ=; b=owGbwMvMwCU28Zj0gdSKO4sYT6slMaSK6KQYni6uetBxtG3LrDZGrZgHTxxPBDF7m4ayTL/6fNHC aR8CO0pZGMS4GGTFFFkc2k3C5ZbzVGw2ytSAmcPKBDKEgYtTACaS0MrIcDq2VVj8gG3QBY0Vxi7JLt O0NLV+nf9ctWehvvyGXzts+RgZXvzRq1ppefbE9AncZ6/3nxGO7rnKPWdD+wOJ5wdLw5cdZgYA X-Developer-Key: i=brauner@kernel.org; a=openpgp; fpr=4880B8C9BD0E5106FC070F4F7B3C391EFEA93624 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Signed-off-by: Christian Brauner --- Documentation/filesystems/porting.rst | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Documentation/filesystems/porting.rst b/Documentation/filesystems/porting.rst index 4d05b9862451..fef97a2e6729 100644 --- a/Documentation/filesystems/porting.rst +++ b/Documentation/filesystems/porting.rst @@ -1045,3 +1045,28 @@ filesystem type is now moved to a later point when the devices are closed: As this is a VFS level change it has no practical consequences for filesystems other than that all of them must use one of the provided kill_litter_super(), kill_anon_super(), or kill_block_super() helpers. + +--- + +**mandatory** + +Block device freezing and thawing have been moved to holder operations. As we +can now go straight from block devcie to superblock the get_active_super() +and bd_fsfreeze_sb members in struct block_device are gone. + +The bd_fsfreeze_mutex is gone as well since we can rely on the bd_holder_lock +to protect against concurrent freeze and thaw. + +Before this change, get_active_super() would only be able to find the +superblock of the main block device, i.e., the one stored in sb->s_bdev. Block +device freezing now works for any block device owned by a given superblock, not +just the main block device. + +When thawing we now grab an active reference so we can hold bd_holder_lock +across thaw without the risk of deadlocks (because the superblock goes away +which would require us to take bd_holder_lock). That allows us to get rid of +bd_fsfreeze_mutex. Currently we just reacquire s_umount after thaw_super() and +drop the active reference we took before. This someone could grab an active +reference before we dropped the last one. This shouldn't be an issue. If it +turns out to be one we can reshuffle the code to simply hold s_umount when +thaw_super() returns and drop the reference.