linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: <linux-btrfs@vger.kernel.org>
Cc: <dsterba@suse.cz>
Subject: [PATCH RFC v2 3/5] btrfs: Handle pending changes in btrfs_freeze().
Date: Tue, 20 Jan 2015 17:05:35 +0800	[thread overview]
Message-ID: <1421744737-16586-4-git-send-email-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <1421744737-16586-1-git-send-email-quwenruo@cn.fujitsu.com>

Before the patch, btrfs_freeze() does not start a transaction if there
is pending changes but no transaction is running.

This will lead to deadlock if the fs is frozen but there are pending
changes, since sync_fs will start a transaction with s_umount mutex,
blocked by frozen fs. And later unfreeze will wait sync_fs to release
the s_umount mutex.

This patch will ensure the frozen fs from having pending changes, except
some one use sysfs interfaces to create pending changes again.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 fs/btrfs/super.c | 52 +++++++++++++++++++++++++---------------------------
 1 file changed, 25 insertions(+), 27 deletions(-)

diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 60f7cbe..5229786 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -975,32 +975,22 @@ fail_close:
 	return err;
 }
 
-int btrfs_sync_fs(struct super_block *sb, int wait)
+static int commit_current_or_new_trans(struct btrfs_fs_info *fs_info,
+				       int freezing)
 {
 	struct btrfs_trans_handle *trans;
-	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
 	struct btrfs_root *root = fs_info->tree_root;
 
-	trace_btrfs_sync_fs(wait);
-
-	if (!wait) {
-		filemap_flush(fs_info->btree_inode->i_mapping);
-		return 0;
-	}
-
-	btrfs_wait_ordered_roots(fs_info, -1);
-
 	trans = btrfs_attach_transaction_barrier(root);
 	if (IS_ERR(trans)) {
-		/* no transaction, don't bother */
 		if (PTR_ERR(trans) == -ENOENT) {
-			/*
-			 * Exit unless we have some pending changes
-			 * that need to go through commit
-			 */
+			/* No pending changes, don't bother*/
 			if (fs_info->pending_changes == 0)
 				return 0;
-			trans = btrfs_start_transaction(root, 0);
+			if (freezing)
+				trans = btrfs_start_transaction_freeze(root, 0);
+			else
+				trans = btrfs_start_transaction(root, 0);
 		} else {
 			return PTR_ERR(trans);
 		}
@@ -1008,6 +998,22 @@ int btrfs_sync_fs(struct super_block *sb, int wait)
 	return btrfs_commit_transaction(trans, root);
 }
 
+int btrfs_sync_fs(struct super_block *sb, int wait)
+{
+	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
+
+	trace_btrfs_sync_fs(wait);
+
+	if (!wait) {
+		filemap_flush(fs_info->btree_inode->i_mapping);
+		return 0;
+	}
+
+	btrfs_wait_ordered_roots(fs_info, -1);
+
+	return commit_current_or_new_trans(fs_info, 0);
+}
+
 static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
 {
 	struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
@@ -1935,17 +1941,9 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
 
 static int btrfs_freeze(struct super_block *sb)
 {
-	struct btrfs_trans_handle *trans;
-	struct btrfs_root *root = btrfs_sb(sb)->tree_root;
+	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
 
-	trans = btrfs_attach_transaction_barrier(root);
-	if (IS_ERR(trans)) {
-		/* no transaction, don't bother */
-		if (PTR_ERR(trans) == -ENOENT)
-			return 0;
-		return PTR_ERR(trans);
-	}
-	return btrfs_commit_transaction(trans, root);
+	return commit_current_or_new_trans(fs_info, 1);
 }
 
 static int btrfs_unfreeze(struct super_block *sb)
-- 
2.2.2


  parent reply	other threads:[~2015-01-20  9:07 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-20  9:05 [PATCH RFC v2 0/5] Fix bugs introduced by the new pending changes Qu Wenruo
2015-01-20  9:05 ` [PATCH RFC v2 1/5] btrfs: Fix the bug that fs_info->pending_changes is never cleared Qu Wenruo
2015-01-20 16:01   ` David Sterba
2015-01-20  9:05 ` [PATCH RFC v2 2/5] btrfs: Add btrfs_start_transaction_freeze() to start transaction in btrfs_freeze() Qu Wenruo
2015-01-20  9:05 ` Qu Wenruo [this message]
2015-01-20  9:05 ` [PATCH RFC v2 4/5] Revert "btrfs: move commit out of sysfs when changing label" Qu Wenruo
2015-01-20  9:05 ` [PATCH RFC v2 5/5] Revert "btrfs: move commit out of sysfs when changing features" Qu Wenruo
2015-01-20 15:53 ` [PATCH RFC v2 0/5] Fix bugs introduced by the new pending changes David Sterba

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=1421744737-16586-4-git-send-email-quwenruo@cn.fujitsu.com \
    --to=quwenruo@cn.fujitsu.com \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    /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).