linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Miao Xie <miaox@cn.fujitsu.com>
To: Linux Btrfs <linux-btrfs@vger.kernel.org>
Subject: [PATCH 5/5] Btrfs: fix remount vs autodefrag
Date: Mon, 26 Nov 2012 17:28:13 +0800	[thread overview]
Message-ID: <50B3362D.1030409@cn.fujitsu.com> (raw)
In-Reply-To: <50B32FE7.5080908@cn.fujitsu.com>

If we remount the fs to close the auto defragment or make the fs R/O, we should
stop the auto defragment.

Signed-off-by: Miao Xie <miaox@cn.fujitsu.com>
---
 fs/btrfs/ctree.h |  1 +
 fs/btrfs/file.c  | 13 +++++++++++++
 fs/btrfs/super.c | 29 +++++++++++++++++++++++++++++
 3 files changed, 43 insertions(+)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 4ce24ce..01d671c 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1759,6 +1759,7 @@ struct btrfs_ioctl_defrag_range_args {
 
 #define btrfs_clear_opt(o, opt)		((o) &= ~BTRFS_MOUNT_##opt)
 #define btrfs_set_opt(o, opt)		((o) |= BTRFS_MOUNT_##opt)
+#define btrfs_raw_test_opt(o, opt)	((o) & BTRFS_MOUNT_##opt)
 #define btrfs_test_opt(root, opt)	((root)->fs_info->mount_opt & \
 					 BTRFS_MOUNT_##opt)
 /*
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 40b17d0..7aaae56 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -320,8 +320,21 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
 	range.start = defrag->last_offset;
 
 	sb_start_write(fs_info->sb);
+
+	/* Avoid defraging files on R/O fs */
+	if (!down_write_trylock(&fs_info->sb->s_umount)) {
+		sb_end_write(fs_info->sb);
+		btrfs_requeue_inode_defrag(inode, defrag);
+		iput(inode);
+		return -EBUSY;
+	}
+
+	BUG_ON(fs_info->sb->s_flags & MS_RDONLY);
+
 	num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
 				       BTRFS_DEFRAG_BATCH);
+
+	up_write(&fs_info->sb->s_umount);
 	sb_end_write(fs_info->sb);
 	/*
 	 * if we filled the whole defrag batch, there
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index b3b041a..2e7beee 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -1189,6 +1189,32 @@ static void btrfs_resize_thread_pool(struct btrfs_fs_info *fs_info,
 	btrfs_set_max_workers(&fs_info->scrub_workers, new_pool_size);
 }
 
+static inline void btrfs_remount_prepare(struct btrfs_fs_info *fs_info,
+					 unsigned long old_opts, int flags)
+{
+	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
+	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
+	     (flags & MS_RDONLY))) {
+		/* wait for any defraggers to finish */
+		wait_event(fs_info->transaction_wait,
+			   (atomic_read(&fs_info->defrag_running) == 0));
+	}
+}
+
+static inline void btrfs_remount_cleanup(struct btrfs_fs_info *fs_info,
+					 unsigned long old_opts, int flags)
+{
+	/*
+	 * We remount the fs successfully, then we need cleanup all defragable
+	 * inodes if the autodefragment is close or the fs is R/O.
+	 */
+	if (btrfs_raw_test_opt(old_opts, AUTO_DEFRAG) &&
+	    (!btrfs_raw_test_opt(fs_info->mount_opt, AUTO_DEFRAG) ||
+	     (flags & MS_RDONLY)))
+		btrfs_cleanup_defrag_inodes(fs_info);
+
+}
+
 static int btrfs_remount(struct super_block *sb, int *flags, char *data)
 {
 	struct btrfs_fs_info *fs_info = btrfs_sb(sb);
@@ -1214,6 +1240,8 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
 		return 0;
 
+	btrfs_remount_prepare(fs_info, old_opts, *flags);
+
 	if (*flags & MS_RDONLY) {
 		sb->s_flags |= MS_RDONLY;
 
@@ -1247,6 +1275,7 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
 		sb->s_flags &= ~MS_RDONLY;
 	}
 
+	btrfs_remount_cleanup(fs_info, old_opts, *flags);
 	return 0;
 
 restore:
-- 
1.7.11.7


  parent reply	other threads:[~2012-11-26  9:27 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-26  9:01 [PATCH 0/5] random fix for autodefrag Miao Xie
2012-11-26  9:24 ` [PATCH 1/5] Btrfs: use slabs for auto defrag allocation Miao Xie
2012-11-26  9:25 ` [PATCH 2/5] Btrfs: fix unprotected defragable inode insertion Miao Xie
2012-11-26  9:26 ` [PATCH 3/5] Btrfs: restructure btrfs_run_defrag_inodes() Miao Xie
2012-11-26  9:27 ` [PATCH 4/5] Btrfs: fix freeze vs auto defrag Miao Xie
2012-11-26  9:28 ` Miao Xie [this message]
2012-12-14 17:51   ` [PATCH 5/5] Btrfs: fix remount vs autodefrag Josef Bacik
2012-12-17  7:24     ` Miao Xie
2012-12-18 13:38       ` Josef Bacik
2013-02-21  6:32         ` [PATCH V2] " Miao Xie
2013-01-31  5:54   ` [PATCH 5/5] " Miao Xie

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=50B3362D.1030409@cn.fujitsu.com \
    --to=miaox@cn.fujitsu.com \
    --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).