Commit 9a8887a9 authored by zhangyi (F)'s avatar zhangyi (F) Committed by Xie XiuQi
Browse files

block: add info when opening a write opend block device exclusively



euler inclusion
category: feature
bugzilla: 14367
CVE: NA
---------------------------

Just like open an exclusive opened block device for write, open a block
device exclusively which has been opened for write by some other
processes may also lead to potential data corruption. This patch record
the write openers and give a hint if that happens.

Signed-off-by: default avatarzhangyi (F) <yi.zhang@huawei.com>
Reviewed-by: default avatarMiao Xie <miaoxie@huawei.com>
Signed-off-by: default avatarYang Yingliang <yangyingliang@huawei.com>
parent 14eae9d6
Loading
Loading
Loading
Loading
+18 −2
Original line number Diff line number Diff line
@@ -1563,6 +1563,8 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
	bdev->bd_openers++;
	if (for_part)
		bdev->bd_part_count++;
	if (mode & FMODE_WRITE)
		bdev->bd_write_openers++;
	mutex_unlock(&bdev->bd_mutex);
	disk_unblock_events(disk);
	/* only one opener holds refs to the module and disk */
@@ -1611,6 +1613,7 @@ int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
{
	struct block_device *whole = NULL;
	int res;
	char name[BDEVNAME_SIZE];

	WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);

@@ -1630,6 +1633,19 @@ int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
	if (whole) {
		struct gendisk *disk = whole->bd_disk;

		/*
		 * Open an write opened block device exclusively, the
		 * writing process may probability corrupt the device,
		 * such as a mounted file system, give a hint here.
		 */
		if (!res && (bdev->bd_write_openers >
		    ((mode & FMODE_WRITE) ? 1 : 0)) && !bdev->bd_holders) {
			pr_info_ratelimited("VFS: Open an write opened "
				"block device exclusively %s [%d %s].\n",
				bdevname(bdev, name), current->pid,
				current->comm);
		}

		/* finish claiming */
		if (!res) {
			BUG_ON(!bd_may_claim(bdev, whole, holder));
@@ -1669,8 +1685,6 @@ int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
		bdput(whole);
	} else {
		if (!res && (mode & FMODE_WRITE) && bdev->bd_holders) {
			char name[BDEVNAME_SIZE];

			/*
			 * Open an exclusive opened device for write may
			 * probability corrupt the device, such as a
@@ -1805,6 +1819,8 @@ static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
	struct block_device *victim = NULL;

	mutex_lock_nested(&bdev->bd_mutex, for_part);
	if (mode & FMODE_WRITE)
		bdev->bd_write_openers--;
	if (for_part)
		bdev->bd_part_count--;

+1 −0
Original line number Diff line number Diff line
@@ -455,6 +455,7 @@ struct request_queue;
struct block_device {
	dev_t			bd_dev;  /* not a kdev_t - it's a search key */
	int			bd_openers;
	int			bd_write_openers;
	struct inode *		bd_inode;	/* will die */
	struct super_block *	bd_super;
	struct mutex		bd_mutex;	/* open/close mutex */