All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] btrfs: Defer setting new inode mode until after do_set_acl succeeds
@ 2019-03-21 15:57 Nikolay Borisov
  2019-03-21 16:01 ` Johannes Thumshirn
  2019-03-22 18:59 ` David Sterba
  0 siblings, 2 replies; 3+ messages in thread
From: Nikolay Borisov @ 2019-03-21 15:57 UTC (permalink / raw)
  To: linux-btrfs; +Cc: 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")
Signed-off-by: Nikolay Borisov <nborisov@suse.com>
---

Changes since v1:
 * Eliminated unrealted newline change (Johannes)
 * Fixed logic of when i_mode is set, v1 was causing an uninitialised mode
 to be assigned to inode->i_mode resulting in fs consistency problems. Fix this
 by introducing a variable which is set to true when the i_mode needs to be 
 changed. I know this variable could be eliminated by simply initialising 
 mode = inode->i_mode and always assigning it in the if (!ret) branch but I 
 find this somewhat subtle and rather be explicit with the boolean variable. 

 fs/btrfs/acl.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/fs/btrfs/acl.c b/fs/btrfs/acl.c
index b722866e1442..ac12a4530540 100644
--- a/fs/btrfs/acl.c
+++ b/fs/btrfs/acl.c
@@ -112,14 +112,16 @@ 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;
+	bool change_mode = false;
 	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;
+		change_mode = true;
 	}
 
 	trans = btrfs_start_transaction(root, 2);
@@ -127,9 +129,9 @@ 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) {
+		if (change_mode)
+			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.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] btrfs: Defer setting new inode mode until after do_set_acl succeeds
  2019-03-21 15:57 [PATCH v2] btrfs: Defer setting new inode mode until after do_set_acl succeeds Nikolay Borisov
@ 2019-03-21 16:01 ` Johannes Thumshirn
  2019-03-22 18:59 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: Johannes Thumshirn @ 2019-03-21 16:01 UTC (permalink / raw)
  To: Nikolay Borisov, linux-btrfs

On 21/03/2019 16:57, Nikolay Borisov wrote:
[...]

>  * Fixed logic of when i_mode is set, v1 was causing an uninitialised mode
>  to be assigned to inode->i_mode resulting in fs consistency problems. Fix this
>  by introducing a variable which is set to true when the i_mode needs to be 
>  changed. I know this variable could be eliminated by simply initialising 
>  mode = inode->i_mode and always assigning it in the if (!ret) branch but I 
>  find this somewhat subtle and rather be explicit with the boolean variable. 

[...]

> +	if (!ret) {
> +		if (change_mode)
> +			inode->i_mode = mode;

But what about initializing mode by inode->i_mode *and* adding a comment
that this is either the saved state or the new one, depending on
posix_acl_update_mode()'s outcome?

-- 
Johannes Thumshirn                            SUSE Labs Filesystems
jthumshirn@suse.de                                +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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] btrfs: Defer setting new inode mode until after do_set_acl succeeds
  2019-03-21 15:57 [PATCH v2] btrfs: Defer setting new inode mode until after do_set_acl succeeds Nikolay Borisov
  2019-03-21 16:01 ` Johannes Thumshirn
@ 2019-03-22 18:59 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2019-03-22 18:59 UTC (permalink / raw)
  To: Nikolay Borisov; +Cc: linux-btrfs

On Thu, Mar 21, 2019 at 05:57:06PM +0200, Nikolay Borisov wrote:
> 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")
> Signed-off-by: Nikolay Borisov <nborisov@suse.com>
> ---
> 
> Changes since v1:
>  * Eliminated unrealted newline change (Johannes)
>  * Fixed logic of when i_mode is set, v1 was causing an uninitialised mode
>  to be assigned to inode->i_mode resulting in fs consistency problems. Fix this
>  by introducing a variable which is set to true when the i_mode needs to be 
>  changed. I know this variable could be eliminated by simply initialising 
>  mode = inode->i_mode and always assigning it in the if (!ret) branch but I 
>  find this somewhat subtle and rather be explicit with the boolean variable. 

Agreed. I folded the missing mode reset to the patch that introduced it
as the Fixes: reference would not work, and adjusted your patch to
simplify it again. Changelog adapted accordingly, only that it's not a
faix but simplification. Please check it once it's pushed to misc-next.

Reviewed-by: David Sterba <dsterba@suse.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-03-22 18:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-21 15:57 [PATCH v2] btrfs: Defer setting new inode mode until after do_set_acl succeeds Nikolay Borisov
2019-03-21 16:01 ` Johannes Thumshirn
2019-03-22 18:59 ` David Sterba

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.