Unverified Commit 22650a99 authored by Christian Brauner's avatar Christian Brauner
Browse files

fs,block: yield devices early

Currently a device is only really released once the umount returns to
userspace due to how file closing works. That ultimately could cause
an old umount assumption to be violated that concurrent umount and mount
don't fail. So an exclusively held device with a temporary holder should
be yielded before the filesystem is gone. Add a helper that allows
callers to do that. This also allows us to remove the two holder ops
that Linus wasn't excited about.

Link: https://lore.kernel.org/r/20240326-vfs-bdev-end_holder-v1-1-20af85202918@kernel.org


Fixes: f3a60882 ("bdev: open block device as files") # mainline only
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarJan Kara <jack@suse.cz>
Suggested-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: default avatarChristian Brauner <brauner@kernel.org>
parent 3ff56e28
Loading
Loading
Loading
Loading
+56 −8
Original line number Diff line number Diff line
@@ -583,9 +583,6 @@ static void bd_finish_claiming(struct block_device *bdev, void *holder,
	mutex_unlock(&bdev->bd_holder_lock);
	bd_clear_claiming(whole, holder);
	mutex_unlock(&bdev_lock);

	if (hops && hops->get_holder)
		hops->get_holder(holder);
}

/**
@@ -608,7 +605,6 @@ EXPORT_SYMBOL(bd_abort_claiming);
static void bd_end_claim(struct block_device *bdev, void *holder)
{
	struct block_device *whole = bdev_whole(bdev);
	const struct blk_holder_ops *hops = bdev->bd_holder_ops;
	bool unblock = false;

	/*
@@ -631,9 +627,6 @@ static void bd_end_claim(struct block_device *bdev, void *holder)
		whole->bd_holder = NULL;
	mutex_unlock(&bdev_lock);

	if (hops && hops->put_holder)
		hops->put_holder(holder);

	/*
	 * If this was the last claim, remove holder link and unblock evpoll if
	 * it was a write holder.
@@ -813,6 +806,11 @@ static void bdev_claim_write_access(struct block_device *bdev, blk_mode_t mode)
		bdev->bd_writers++;
}

static inline bool bdev_unclaimed(const struct file *bdev_file)
{
	return bdev_file->private_data == BDEV_I(bdev_file->f_mapping->host);
}

static void bdev_yield_write_access(struct file *bdev_file)
{
	struct block_device *bdev;
@@ -820,6 +818,9 @@ static void bdev_yield_write_access(struct file *bdev_file)
	if (bdev_allow_write_mounted)
		return;

	if (bdev_unclaimed(bdev_file))
		return;

	bdev = file_bdev(bdev_file);

	if (bdev_file->f_mode & FMODE_WRITE_RESTRICTED)
@@ -1012,6 +1013,20 @@ struct file *bdev_file_open_by_path(const char *path, blk_mode_t mode,
}
EXPORT_SYMBOL(bdev_file_open_by_path);

static inline void bd_yield_claim(struct file *bdev_file)
{
	struct block_device *bdev = file_bdev(bdev_file);
	void *holder = bdev_file->private_data;

	lockdep_assert_held(&bdev->bd_disk->open_mutex);

	if (WARN_ON_ONCE(IS_ERR_OR_NULL(holder)))
		return;

	if (!bdev_unclaimed(bdev_file))
		bd_end_claim(bdev, holder);
}

void bdev_release(struct file *bdev_file)
{
	struct block_device *bdev = file_bdev(bdev_file);
@@ -1036,7 +1051,7 @@ void bdev_release(struct file *bdev_file)
	bdev_yield_write_access(bdev_file);

	if (holder)
		bd_end_claim(bdev, holder);
		bd_yield_claim(bdev_file);

	/*
	 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
@@ -1056,6 +1071,39 @@ void bdev_release(struct file *bdev_file)
	blkdev_put_no_open(bdev);
}

/**
 * bdev_fput - yield claim to the block device and put the file
 * @bdev_file: open block device
 *
 * Yield claim on the block device and put the file. Ensure that the
 * block device can be reclaimed before the file is closed which is a
 * deferred operation.
 */
void bdev_fput(struct file *bdev_file)
{
	if (WARN_ON_ONCE(bdev_file->f_op != &def_blk_fops))
		return;

	if (bdev_file->private_data) {
		struct block_device *bdev = file_bdev(bdev_file);
		struct gendisk *disk = bdev->bd_disk;

		mutex_lock(&disk->open_mutex);
		bdev_yield_write_access(bdev_file);
		bd_yield_claim(bdev_file);
		/*
		 * Tell release we already gave up our hold on the
		 * device and if write restrictions are available that
		 * we already gave up write access to the device.
		 */
		bdev_file->private_data = BDEV_I(bdev_file->f_mapping->host);
		mutex_unlock(&disk->open_mutex);
	}

	fput(bdev_file);
}
EXPORT_SYMBOL(bdev_fput);

/**
 * lookup_bdev() - Look up a struct block_device by name.
 * @pathname: Name of the block device in the filesystem.
+1 −1
Original line number Diff line number Diff line
@@ -209,7 +209,7 @@ static void block2mtd_free_device(struct block2mtd_dev *dev)

	if (dev->bdev_file) {
		invalidate_mapping_pages(dev->bdev_file->f_mapping, 0, -1);
		fput(dev->bdev_file);
		bdev_fput(dev->bdev_file);
	}

	kfree(dev);
+1 −1
Original line number Diff line number Diff line
@@ -143,7 +143,7 @@ void bch2_free_super(struct bch_sb_handle *sb)
{
	kfree(sb->bio);
	if (!IS_ERR_OR_NULL(sb->s_bdev_file))
		fput(sb->s_bdev_file);
		bdev_fput(sb->s_bdev_file);
	kfree(sb->holder);
	kfree(sb->sb_name);

+1 −1
Original line number Diff line number Diff line
@@ -495,7 +495,7 @@ static void cramfs_kill_sb(struct super_block *sb)
		sb->s_mtd = NULL;
	} else if (IS_ENABLED(CONFIG_CRAMFS_BLOCKDEV) && sb->s_bdev) {
		sync_blockdev(sb->s_bdev);
		fput(sb->s_bdev_file);
		bdev_fput(sb->s_bdev_file);
	}
	kfree(sbi);
}
+4 −4
Original line number Diff line number Diff line
@@ -5668,7 +5668,7 @@ failed_mount9: __maybe_unused
	brelse(sbi->s_sbh);
	if (sbi->s_journal_bdev_file) {
		invalidate_bdev(file_bdev(sbi->s_journal_bdev_file));
		fput(sbi->s_journal_bdev_file);
		bdev_fput(sbi->s_journal_bdev_file);
	}
out_fail:
	invalidate_bdev(sb->s_bdev);
@@ -5913,7 +5913,7 @@ static struct file *ext4_get_journal_blkdev(struct super_block *sb,
out_bh:
	brelse(bh);
out_bdev:
	fput(bdev_file);
	bdev_fput(bdev_file);
	return ERR_PTR(errno);
}

@@ -5952,7 +5952,7 @@ static journal_t *ext4_open_dev_journal(struct super_block *sb,
out_journal:
	jbd2_journal_destroy(journal);
out_bdev:
	fput(bdev_file);
	bdev_fput(bdev_file);
	return ERR_PTR(errno);
}

@@ -7327,7 +7327,7 @@ static void ext4_kill_sb(struct super_block *sb)
	kill_block_super(sb);

	if (bdev_file)
		fput(bdev_file);
		bdev_fput(bdev_file);
}

static struct file_system_type ext4_fs_type = {
Loading