linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "zhangyi (F)" <yi.zhang@huawei.com>
To: <viro@zeniv.linux.org.uk>
Cc: <linux-fsdevel@vger.kernel.org>, <yi.zhang@huawei.com>,
	<miaoxie@huawei.com>
Subject: [PATCH 1/2] block: add info when opening an exclusive opened block device for write
Date: Tue, 14 May 2019 22:45:05 +0800	[thread overview]
Message-ID: <1557845106-60163-2-git-send-email-yi.zhang@huawei.com> (raw)
In-Reply-To: <1557845106-60163-1-git-send-email-yi.zhang@huawei.com>

Opening an exclusive opened block device for write make the exclusive
open isn't exclusive enough, it may lead to data corruption when some
one writing data through the counterpart raw block device, such as
corrupt a mounted file system. This patch add an info message when
opening an exclusive opened block device for write to hint the
potential data corruption.

Note that there are some legal cases such as file system or device
mapper online resize, so this message is just a hint and isn't always
mean that a risky written happens.

Signed-off-by: zhangyi (F) <yi.zhang@huawei.com>
---
 fs/block_dev.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/fs/block_dev.c b/fs/block_dev.c
index bb28e2e..d92aa45 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -1667,13 +1667,13 @@ int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
 
 	res = __blkdev_get(bdev, mode, 0);
 
+	mutex_lock(&bdev->bd_mutex);
+	spin_lock(&bdev_lock);
+
 	if (whole) {
 		struct gendisk *disk = whole->bd_disk;
 
 		/* finish claiming */
-		mutex_lock(&bdev->bd_mutex);
-		spin_lock(&bdev_lock);
-
 		if (!res) {
 			BUG_ON(!bd_may_claim(bdev, whole, holder));
 			/*
@@ -1710,6 +1710,22 @@ int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
 
 		mutex_unlock(&bdev->bd_mutex);
 		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
+			 * mounted file system, give a hint here.
+			 */
+			pr_info_ratelimited("VFS: Open an exclusive opened "
+				    "block device for write %s [%d %s].\n",
+				    bdevname(bdev, name), current->pid,
+				    current->comm);
+		}
+		spin_unlock(&bdev_lock);
+		mutex_unlock(&bdev->bd_mutex);
 	}
 
 	return res;
-- 
2.7.4


  reply	other threads:[~2019-05-14 14:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-14 14:45 [PATCH 0/2] block: add info messages when opening a block device O_WRITE and O_EXCL concurrently zhangyi (F)
2019-05-14 14:45 ` zhangyi (F) [this message]
2019-05-14 14:45 ` [PATCH 2/2] block: add info when opening a write opend block device exclusively zhangyi (F)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1557845106-60163-2-git-send-email-yi.zhang@huawei.com \
    --to=yi.zhang@huawei.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=miaoxie@huawei.com \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).