Skip to contentSkip to conversation

btrfs: Defer setting new inode mode until after do_set_acl succeeds

PATCH Superseded Nikolay Borisov 3 participants 5 messages Mar 21, 2019 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: Defer setting new inode mode until after do_set_acl succeeds

Currently a reference to inode->i_mode is passed directly to posix_acl_update_mode when setting an ACL which results in the inode's mode always being changed. In case of errors (e.g. in do_set_acl or even starting a transaction) the old mode needs to be re-assigned to ->i_mode. This mode recovery is done only in case do_set_acl fails, which leads to buggy behavior in case btrfs_start_transaction fails.

Fix it by simply setting the new mode to a temporary variable which is assigned to inode->i_mode's only when do_set_acl succeeds. This covers both failure cases explained above.

Fixes db0f220e98eb ("btrfs: start transaction in btrfs_set_acl")
---
 fs/btrfs/acl.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/btrfs/acl.c +4 -6
index b722866e1442..a0cfd2049ea5 100644
@@ -99,7 +99,6 @@ static int do_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
 	}
 
 	ret = btrfs_setxattr(trans, inode, name, value, size, 0);
-
 out:
 	kfree(value);
 
@@ -112,12 +111,12 @@ static int do_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
 int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 {
 	int ret;
-	umode_t old_mode = inode->i_mode;
+	umode_t mode;
 	struct btrfs_trans_handle *trans;
 	struct btrfs_root *root = BTRFS_I(inode)->root;
 
 	if (type == ACL_TYPE_ACCESS && acl) {
-		ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
+		ret = posix_acl_update_mode(inode, &mode, &acl);
 		if (ret)
 			return ret;
 	}
@@ -127,9 +126,8 @@ int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 		return PTR_ERR(trans);
 
 	ret = do_set_acl(trans, inode, acl, type);
-	if (ret) {
-		inode->i_mode = old_mode;
-	} else {
+	if (!ret) {
+		inode->i_mode = mode;
 		inode_inc_iversion(inode);
 		inode->i_ctime = current_time(inode);
 		set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Signature · 1 line
2.17.1
#1 lore raw
Quoted 11 lines · Nikolay Borisov

Currently a reference to inode->i_mode is passed directly to posix_acl_update_mode when setting an ACL which results in the inode's mode always being changed. In case of errors (e.g. in do_set_acl or even starting a transaction) the old mode needs to be re-assigned to ->i_mode. This mode recovery is done only in case do_set_acl fails, which leads to buggy behavior in case btrfs_start_transaction fails.

Fix it by simply setting the new mode to a temporary variable which is assigned to inode->i_mode's only when do_set_acl succeeds. This covers both failure cases explained above.

Quoted 5 lines
Fixes db0f220e98eb ("btrfs: start transaction in btrfs_set_acl")
---
 fs/btrfs/acl.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)

This patch is broken and causes inconsistencies on the fs that btrfs check detects. Test that triggers it - generic/077. I'm trying to understand why, meanwhile this works:

fs/btrfs/acl.c +0 -0
index b722866e1442..f56ed759e0a4 100644
@@ -123,8 +123,10 @@ int btrfs_set_acl(struct inode *inode, struct

posix_acl *acl, int type)

        }

        trans = btrfs_start_transaction(root, 2);
-       if (IS_ERR(trans))
+       if (IS_ERR(trans)) {
+               inode->i_mode = old_mode;
                return PTR_ERR(trans);

+ }

        ret = do_set_acl(trans, inode, acl, type);
        if (ret) {

However it feels more like proliferating the bad style btrfs_set_acl is structured in.

Quoted 41 lines
fs/btrfs/acl.c +4 -6
index b722866e1442..a0cfd2049ea5 100644
@@ -99,7 +99,6 @@ static int do_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
 	}
 
 	ret = btrfs_setxattr(trans, inode, name, value, size, 0);
-
 out:
 	kfree(value);
 
@@ -112,12 +111,12 @@ static int do_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
 int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 {
 	int ret;
-	umode_t old_mode = inode->i_mode;
+	umode_t mode;
 	struct btrfs_trans_handle *trans;
 	struct btrfs_root *root = BTRFS_I(inode)->root;
 
 	if (type == ACL_TYPE_ACCESS && acl) {
-		ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
+		ret = posix_acl_update_mode(inode, &mode, &acl);
 		if (ret)
 			return ret;
 	}
@@ -127,9 +126,8 @@ int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 		return PTR_ERR(trans);
 
 	ret = do_set_acl(trans, inode, acl, type);
-	if (ret) {
-		inode->i_mode = old_mode;
-	} else {
+	if (!ret) {
+		inode->i_mode = mode;
 		inode_inc_iversion(inode);
 		inode->i_ctime = current_time(inode);
 		set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
#2 lore raw
Quoted 3 lines · Nikolay Borisov

This patch is broken and causes inconsistencies on the fs that btrfs check detects. Test that triggers it - generic/077. I'm trying to understand why, meanwhile this works:

If you find the root cause, could you share it please?

Signature · 6 lines
Johannes Thumshirn                            SUSE Labs Filesystems
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Mary Higgins, Sri Rasiah
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
Reviewed-by on v1 1/1
#3 lore raw
In reply to Nikolay Borisov

On 21/03/2019 10:27, Nikolay Borisov wrote:
[...]

fs/btrfs/acl.c static int do_set_acl(struct btrfs_trans_handle *trans, struct inode *inode, · line 102

Quoted 8 lines

index b722866e1442..a0cfd2049ea5 100644

fs/btrfs/acl.c +0 -1
@@ -99,7 +99,6 @@ static int do_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
 	}
 
 	ret = btrfs_setxattr(trans, inode, name, value, size, 0);
-

Nit: unrelated change

Looks good,

Signature · 6 lines
Johannes Thumshirn                            SUSE Labs Filesystems
[email protected]                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Mary Higgins, Sri Rasiah
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
#4 lore raw
In reply to Nikolay Borisov
Quoted 45 lines · Nikolay Borisov

Currently a reference to inode->i_mode is passed directly to posix_acl_update_mode when setting an ACL which results in the inode's mode always being changed. In case of errors (e.g. in do_set_acl or even starting a transaction) the old mode needs to be re-assigned to ->i_mode. This mode recovery is done only in case do_set_acl fails, which leads to buggy behavior in case btrfs_start_transaction fails.

Fix it by simply setting the new mode to a temporary variable which is assigned to inode->i_mode's only when do_set_acl succeeds. This covers both failure cases explained above.

Fixes db0f220e98eb ("btrfs: start transaction in btrfs_set_acl")
---
 fs/btrfs/acl.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
fs/btrfs/acl.c +2 -3
index b722866e1442..a0cfd2049ea5 100644
@@ -99,7 +99,6 @@ static int do_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
 	}
 
 	ret = btrfs_setxattr(trans, inode, name, value, size, 0);
-
 out:
 	kfree(value);
 
@@ -112,12 +111,12 @@ static int do_set_acl(struct btrfs_trans_handle *trans, struct inode *inode,
 int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 {
 	int ret;
-	umode_t old_mode = inode->i_mode;
+	umode_t mode;
 	struct btrfs_trans_handle *trans;
 	struct btrfs_root *root = BTRFS_I(inode)->root;
 
 	if (type == ACL_TYPE_ACCESS && acl) {
-		ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
+		ret = posix_acl_update_mode(inode, &mode, &acl);
 		if (ret)
 			return ret;
 	}

fs/btrfs/acl.c int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) · line 130

Quoted 8 lines
fs/btrfs/acl.c +2 -3
@@ -127,9 +126,8 @@ int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
 		return PTR_ERR(trans);
 
 	ret = do_set_acl(trans, inode, acl, type);
-	if (ret) {
-		inode->i_mode = old_mode;
-	} else {
+	if (!ret) {
+		inode->i_mode = mode;

I like this flow much better, ie. update i_mod only if it's correct, otherwise it's a no-op. Then we don't have to revert the change on each possible error.

The patch that causes it is still in misc-next so the hash is not stable and we can't use it for reference. So I suggest to fold the single i_mode after transaction to "btrfs: start transaction in btrfs_set_acl" and your patch can rework the i_mode handling.

We'll see when you see V2 if this could work or just go with a single patch.