diff mbox

[9/9] btrfs: Use sb_want_write() to protect sysfs feature change.

Message ID 1423705473-8597-1-git-send-email-quwenruo@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Qu Wenruo Feb. 12, 2015, 1:44 a.m. UTC
Just like label change, use sb_want_write() to do a correct protection.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
v4:
  Newly introduced.
v5:
  Change to sb_want_write().
v6:
  Move sb_want_write() to the beginning of the function.
v7:
  None
v8:
  Move sb_want_write() after getting fs_info.
---
 fs/btrfs/sysfs.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

Comments

David Sterba Feb. 13, 2015, 3:48 p.m. UTC | #1
On Thu, Feb 12, 2015 at 09:44:33AM +0800, Qu Wenruo wrote:
> --- a/fs/btrfs/sysfs.c
> +++ b/fs/btrfs/sysfs.c
> @@ -118,13 +118,19 @@ static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
>  	int ret;
>  
>  	fs_info = to_fs_info(kobj);
> -	if (!fs_info)
> -		return -EPERM;
> +	if (!fs_info) {
> +		ret = -EPERM;
> +		goto out;

This would do a call to sb_drop_write() without a pairing
sb_want_write(), immediate 'return' was correct.

[...]
> +	}
>  
> -	ret = kstrtoul(skip_spaces(buf), 0, &val);
> +	ret = sb_want_write(fs_info->sb);
>  	if (ret)
>  		return ret;
[...]
>  
> +out:
> +	sb_drop_write(fs_info->sb);
> +	if (!ret)
> +		return count;
> +	return ret;
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index 7e548f7..85848fc 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -118,13 +118,19 @@  static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
 	int ret;
 
 	fs_info = to_fs_info(kobj);
-	if (!fs_info)
-		return -EPERM;
+	if (!fs_info) {
+		ret = -EPERM;
+		goto out;
+	}
 
-	ret = kstrtoul(skip_spaces(buf), 0, &val);
+	ret = sb_want_write(fs_info->sb);
 	if (ret)
 		return ret;
 
+	ret = kstrtoul(skip_spaces(buf), 0, &val);
+	if (ret)
+		goto out;
+
 	if (fa->feature_set == FEAT_COMPAT) {
 		set = BTRFS_FEATURE_COMPAT_SAFE_SET;
 		clear = BTRFS_FEATURE_COMPAT_SAFE_CLEAR;
@@ -141,22 +147,25 @@  static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
 	/* Nothing to do */
 	if ((val && (features & fa->feature_bit)) ||
 	    (!val && !(features & fa->feature_bit)))
-		return count;
+		goto out;
 
 	if ((val && !(set & fa->feature_bit)) ||
 	    (!val && !(clear & fa->feature_bit))) {
 		btrfs_info(fs_info,
 			"%sabling feature %s on mounted fs is not supported.",
 			val ? "En" : "Dis", fa->kobj_attr.attr.name);
-		return -EPERM;
+		ret = -EPERM;
+		goto out;
 	}
 
 	btrfs_info(fs_info, "%s %s feature flag",
 		   val ? "Setting" : "Clearing", fa->kobj_attr.attr.name);
 
 	trans = btrfs_start_transaction(fs_info->fs_root, 0);
-	if (IS_ERR(trans))
-		return PTR_ERR(trans);
+	if (IS_ERR(trans)) {
+		ret = PTR_ERR(trans);
+		goto out;
+	}
 
 	spin_lock(&fs_info->super_lock);
 	features = get_features(fs_info, fa->feature_set);
@@ -168,10 +177,12 @@  static ssize_t btrfs_feature_attr_store(struct kobject *kobj,
 	spin_unlock(&fs_info->super_lock);
 
 	ret = btrfs_commit_transaction(trans, fs_info->fs_root);
-	if (ret)
-		return ret;
 
-	return count;
+out:
+	sb_drop_write(fs_info->sb);
+	if (!ret)
+		return count;
+	return ret;
 }
 
 static umode_t btrfs_feature_visible(struct kobject *kobj,