linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yongqiang Yang <xiaoqiangnk@gmail.com>
To: linux-ext4@vger.kernel.org
Cc: aedilger@gmail.com, Yongqiang Yang <xiaoqiangnk@gmail.com>
Subject: [PATCH 01/12] ext4: prevent parallel resizers by atomic bit ops
Date: Mon, 18 Jul 2011 10:52:24 +0800	[thread overview]
Message-ID: <1310957555-15617-2-git-send-email-xiaoqiangnk@gmail.com> (raw)
In-Reply-To: <1310957555-15617-1-git-send-email-xiaoqiangnk@gmail.com>

Before this patch, parallel resizers are allowed and protected by a mutex lock,
actually, there is no need to support parallel resizer, so this patch prevents
parallel resizers by atmoic bit ops, like lock_page() and unlock_page() do.

To do this, the patch removed the mutex lock s_resize_lock from struct ext4_sb_info
and added a unsigned long field named s_resize_flags which inidicates if there is
a resizer.

Signed-off-by: Yongqiang Yang <xiaoqiangnk@gmail.com>
---
 fs/ext4/ext4.h   |    7 +++++-
 fs/ext4/ioctl.c  |   12 +++++++---
 fs/ext4/resize.c |   55 ++++++++++++++++++++---------------------------------
 fs/ext4/super.c  |    2 +-
 4 files changed, 36 insertions(+), 40 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 62cee2b..bb0f776 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1127,7 +1127,8 @@ struct ext4_sb_info {
 	struct journal_s *s_journal;
 	struct list_head s_orphan;
 	struct mutex s_orphan_lock;
-	struct mutex s_resize_lock;
+	unsigned long s_resize_flags;		/* Flags indicating if there
+						   is a resizer */
 	unsigned long s_commit_interval;
 	u32 s_max_batch_time;
 	u32 s_min_batch_time;
@@ -2269,6 +2270,10 @@ static inline void set_bitmap_uptodate(struct buffer_head *bh)
 extern wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
 extern struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
 
+#define EXT4_RESIZING	0
+extern int ext4_resize_begin(struct super_block *sb);
+extern void ext4_resize_end(struct super_block *sb);
+
 #endif	/* __KERNEL__ */
 
 #endif	/* _EXT4_H */
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 808c554..f18bfe3 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -202,8 +202,9 @@ setversion_out:
 		struct super_block *sb = inode->i_sb;
 		int err, err2=0;
 
-		if (!capable(CAP_SYS_RESOURCE))
-			return -EPERM;
+		err = ext4_resize_begin(sb);
+		if (err)
+			return err;
 
 		if (get_user(n_blocks_count, (__u32 __user *)arg))
 			return -EFAULT;
@@ -221,6 +222,7 @@ setversion_out:
 		if (err == 0)
 			err = err2;
 		mnt_drop_write(filp->f_path.mnt);
+		ext4_resize_end(sb);
 
 		return err;
 	}
@@ -271,8 +273,9 @@ mext_out:
 		struct super_block *sb = inode->i_sb;
 		int err, err2=0;
 
-		if (!capable(CAP_SYS_RESOURCE))
-			return -EPERM;
+		err = ext4_resize_begin(sb);
+		if (err)
+			return err;
 
 		if (copy_from_user(&input, (struct ext4_new_group_input __user *)arg,
 				sizeof(input)))
@@ -291,6 +294,7 @@ mext_out:
 		if (err == 0)
 			err = err2;
 		mnt_drop_write(filp->f_path.mnt);
+		ext4_resize_end(sb);
 
 		return err;
 	}
diff --git a/fs/ext4/resize.c b/fs/ext4/resize.c
index 80bbc9c..0213f63 100644
--- a/fs/ext4/resize.c
+++ b/fs/ext4/resize.c
@@ -16,6 +16,25 @@
 
 #include "ext4_jbd2.h"
 
+int ext4_resize_begin(struct super_block *sb)
+{
+	int ret = 0;
+
+	if (!capable(CAP_SYS_RESOURCE))
+		return -EPERM;
+
+	if (test_and_set_bit_lock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags))
+		ret = -EBUSY;
+
+	return ret;
+}
+
+void ext4_resize_end(struct super_block *sb)
+{
+	clear_bit_unlock(EXT4_RESIZING, &EXT4_SB(sb)->s_resize_flags);
+	smp_mb__after_clear_bit();
+}
+
 #define outside(b, first, last)	((b) < (first) || (b) >= (last))
 #define inside(b, first, last)	((b) >= (first) && (b) < (last))
 
@@ -181,11 +200,7 @@ static int setup_new_group_blocks(struct super_block *sb,
 	if (IS_ERR(handle))
 		return PTR_ERR(handle);
 
-	mutex_lock(&sbi->s_resize_lock);
-	if (input->group != sbi->s_groups_count) {
-		err = -EBUSY;
-		goto exit_journal;
-	}
+	BUG_ON(input->group != sbi->s_groups_count);
 
 	if (IS_ERR(bh = bclean(handle, sb, input->block_bitmap))) {
 		err = PTR_ERR(bh);
@@ -285,7 +300,6 @@ exit_bh:
 	brelse(bh);
 
 exit_journal:
-	mutex_unlock(&sbi->s_resize_lock);
 	if ((err2 = ext4_journal_stop(handle)) && !err)
 		err = err2;
 
@@ -799,13 +813,6 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
 		goto exit_put;
 	}
 
-	mutex_lock(&sbi->s_resize_lock);
-	if (input->group != sbi->s_groups_count) {
-		ext4_warning(sb, "multiple resizers run on filesystem!");
-		err = -EBUSY;
-		goto exit_journal;
-	}
-
 	if ((err = ext4_journal_get_write_access(handle, sbi->s_sbh)))
 		goto exit_journal;
 
@@ -829,7 +836,6 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
         /*
          * OK, now we've set up the new group.  Time to make it active.
          *
-         * We do not lock all allocations via s_resize_lock
          * so we have to be safe wrt. concurrent accesses the group
          * data.  So we need to be careful to set all of the relevant
          * group descriptor data etc. *before* we enable the group.
@@ -886,13 +892,9 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
 	 *
 	 * The precise rules we use are:
 	 *
-	 * * Writers of s_groups_count *must* hold s_resize_lock
-	 * AND
 	 * * Writers must perform a smp_wmb() after updating all dependent
 	 *   data and before modifying the groups count
 	 *
-	 * * Readers must hold s_resize_lock over the access
-	 * OR
 	 * * Readers must perform an smp_rmb() after reading the groups count
 	 *   and before reading any dependent data.
 	 *
@@ -937,7 +939,6 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
 	ext4_handle_dirty_super(handle, sb);
 
 exit_journal:
-	mutex_unlock(&sbi->s_resize_lock);
 	if ((err2 = ext4_journal_stop(handle)) && !err)
 		err = err2;
 	if (!err) {
@@ -972,9 +973,6 @@ int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
 	int err;
 	ext4_group_t group;
 
-	/* We don't need to worry about locking wrt other resizers just
-	 * yet: we're going to revalidate es->s_blocks_count after
-	 * taking the s_resize_lock below. */
 	o_blocks_count = ext4_blocks_count(es);
 
 	if (test_opt(sb, DEBUG))
@@ -995,7 +993,7 @@ int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
 
 	if (n_blocks_count < o_blocks_count) {
 		ext4_warning(sb, "can't shrink FS - resize aborted");
-		return -EBUSY;
+		return -EINVAL;
 	}
 
 	/* Handle the remaining blocks in the last group only. */
@@ -1038,24 +1036,13 @@ int ext4_group_extend(struct super_block *sb, struct ext4_super_block *es,
 		goto exit_put;
 	}
 
-	mutex_lock(&EXT4_SB(sb)->s_resize_lock);
-	if (o_blocks_count != ext4_blocks_count(es)) {
-		ext4_warning(sb, "multiple resizers run on filesystem!");
-		mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
-		ext4_journal_stop(handle);
-		err = -EBUSY;
-		goto exit_put;
-	}
-
 	if ((err = ext4_journal_get_write_access(handle,
 						 EXT4_SB(sb)->s_sbh))) {
 		ext4_warning(sb, "error %d on journal write access", err);
-		mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
 		ext4_journal_stop(handle);
 		goto exit_put;
 	}
 	ext4_blocks_count_set(es, o_blocks_count + add);
-	mutex_unlock(&EXT4_SB(sb)->s_resize_lock);
 	ext4_debug("freeing blocks %llu through %llu\n", o_blocks_count,
 		   o_blocks_count + add);
 	/* We add the blocks to the bitmap and set the group need init bit */
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 7910e61..39c2992 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -3492,7 +3492,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
 
 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
 	mutex_init(&sbi->s_orphan_lock);
-	mutex_init(&sbi->s_resize_lock);
+	sbi->s_resize_flags = 0;
 
 	sb->s_root = NULL;
 
-- 
1.7.5.1


  reply	other threads:[~2011-07-18  2:56 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-18  2:52 prevent parallel resizers and fix some error handling in resize Yongqiang Yang
2011-07-18  2:52 ` Yongqiang Yang [this message]
2011-07-18  2:52 ` [PATCH 02/12] ext4: prevent a fs with errors from being resized Yongqiang Yang
2011-07-18  2:52 ` [PATCH 03/12] ext4: prevent a fs without journal " Yongqiang Yang
2011-07-18  3:17   ` Tao Ma
2011-07-18  3:28     ` Yongqiang Yang
2011-07-18  3:44       ` Yongqiang Yang
2011-07-18  7:00   ` Andreas Dilger
2011-07-18  2:52 ` [PATCH 04/12] ext4: rename ext4_add_groupblocks() to ext4_group_add_blocks() Yongqiang Yang
2011-07-18  6:32   ` Amir Goldstein
2011-07-18 10:15     ` Yongqiang Yang
2011-07-18  2:52 ` [PATCH 05/12] ext4: let ext4_group_add_blocks return an error code Yongqiang Yang
2011-07-18  6:19   ` Andreas Dilger
2011-07-18  6:47     ` Amir Goldstein
2011-07-18  6:48   ` Amir Goldstein
2011-07-18 12:55     ` Yongqiang Yang
2011-07-18  2:52 ` [PATCH 06/12] ext4: let ext4_group_add_blocks() handle 0 blocks quickly Yongqiang Yang
2011-07-18  2:52 ` [PATCH 07/12] ext4: fix a typo in ext4_group_extend() Yongqiang Yang
2011-07-18  2:52 ` [PATCH 08/12] ext4: let setup_new_group_blocks set multi-bits each time Yongqiang Yang
2011-07-18  6:26   ` Andreas Dilger
2011-07-18  6:56     ` Amir Goldstein
2011-07-18  2:52 ` [PATCH 09/12] ext4: simplify journal handling in ext4_setup_new_group_blocks Yongqiang Yang
2011-07-18  3:09   ` Tao Ma
2011-07-18  3:21     ` Yongqiang Yang
2011-07-18  3:40       ` Tao Ma
2011-07-18  3:46         ` Yongqiang Yang
2011-07-18  2:52 ` [PATCH 10/12] ext4: remove lock_buffer in bclean() and setup_new_group_blocks Yongqiang Yang
2011-07-18  2:52 ` [PATCH 11/12] ext4: simplify parameters of add_new_gdb() Yongqiang Yang
2011-07-18  6:46   ` Andreas Dilger
2011-07-18  2:52 ` [PATCH 12/12] ext4: simplify parameters of reserve_backup_gdb() Yongqiang Yang
2011-07-18  7:12   ` Amir Goldstein
2011-07-18  6:16 ` prevent parallel resizers and fix some error handling in resize Amir Goldstein
2011-07-18 10:16   ` Yongqiang Yang

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=1310957555-15617-2-git-send-email-xiaoqiangnk@gmail.com \
    --to=xiaoqiangnk@gmail.com \
    --cc=aedilger@gmail.com \
    --cc=linux-ext4@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).