btrfs: fix property bugs
cover btrfs: fix property bugs
1/4 Fixes a bug that we were applying an incomplete string zstd. 2/4 Fixes a bug that the compression property gets reset to NULL after a
failed attempt to change the compression parameter on the object.
3/4 preparatory patch so that btrfs_set_prop_trans() can be merged with
btrfs_set_prop() in 4/4.
4/4 Fixes a bug that we were incrementing the generation even when the
property validation check failed.
3/4 and 4/4 are only added here.
Anand Jain (4):
btrfs: fix zstd compression parameter
btrfs: fix vanished compression property after failed set
btrfs: open code btrfs_set_prop in inherit_prop
btrfs: fix property validate fail should not increment generation
fs/btrfs/ioctl.c | 10 +++--- fs/btrfs/props.c | 94 ++++++++++++++++++++++++++++++-------------------------- fs/btrfs/props.h | 4 +-- fs/btrfs/xattr.c | 2 +- 4 files changed, 58 insertions(+), 52 deletions(-)
Signature · 1 line
1.8.3.1
PATCH 1/4 btrfs: fix zstd compression parameter
We let to pass zstd compression parameter even if its not fully written. For example:
btrfs prop set /btrfs compression zst
btrfs prop get /btrfs compression
compression=zst
zlib and lzo are fine.
Fix it by using the expected number of char in strncmp().
--- fs/btrfs/props.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/props.c +1 -1
@@ -304,7 +304,7 @@ static int prop_compression_apply(struct inode *inode, const char *value,btrfs_set_fs_incompat (fs_info,COMPRESS_LZO ); }else if (!strncmp ("zlib" , value,4 )) { type =BTRFS_COMPRESS_ZLIB ; - }else if (!strncmp ("zstd" , value, len)) { + }else if (!strncmp ("zstd" , value,4 )) { type =BTRFS_COMPRESS_ZSTD ;btrfs_set_fs_incompat (fs_info,COMPRESS_ZSTD ); }else {
Signature · 1 line
1.8.3.1
PATCH 2/4 btrfs: fix vanished compression property after failed set
In reply to Anand JainThe compression property resets to NULL, instead of the old value if we fail to set the new compression parameter.
btrfs prop get /btrfs compression
compression=lzo
btrfs prop set /btrfs compression zli
ERROR: failed to set compression for /btrfs: Invalid argument
btrfs prop get /btrfs compression
This is because the compression property ->validate() is successful for 'zli' as the strncmp() used the len passed from the userland.
Fix it by using the expected string length in strncmp().
--- fs/btrfs/props.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/props.c +3 -3
@@ -275,11 +275,11 @@ static int prop_compression_validate(const char *value, size_t len)if (!value)return 0 ; -if (!strncmp ("lzo" , value, len)) +if (!strncmp ("lzo" , value,3 ))return 0 ; -else if (!strncmp ("zlib" , value, len)) +else if (!strncmp ("zlib" , value,4 ))return 0 ; -else if (!strncmp ("zstd" , value, len)) +else if (!strncmp ("zstd" , value,4 ))return 0 ;return -EINVAL ;
Signature · 1 line
1.8.3.1
Re: [PATCH 2/4] btrfs: fix vanished compression property after failed set
Quoted 9 lines · Anand Jain
The compression property resets to NULL, instead of the old value if we fail to set the new compression parameter.
btrfs prop get /btrfs compression
compression=lzo
btrfs prop set /btrfs compression zli
ERROR: failed to set compression for /btrfs: Invalid argument
btrfs prop get /btrfs compression
Quoted 6 lines
This is because the compression property ->validate() is successful for 'zli' as the
strncmp()used the len passed from the userland.Fix it by using the expected string length in
strncmp().Signed-off-by Anand Jain <[email protected]>
The changelog could be a bit clearer. The failure is due to the fact that ->validate succeeds but ->apply fails and the handling code calls
btrfs_setxattr(trans, inode, handler->xattr_name,
NULL, 0, flags);However, if you make this patch be the first in the series you obsolete the existing patch 1 and fix the issue in this one. I.e 2 birds with 1 stone.
Also I believe btrfs/048 could use some extension in the part where it validates compression properties to test for the bugs discovered/fixed in this series.
Quoted 24 lines
--- fs/btrfs/props.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)fs/btrfs/props.c +3
-3@@ -275,11 +275,11 @@ static int prop_compression_validate(const char *value, size_t len)if (!value)return 0 ; -if (!strncmp ("lzo" , value, len)) +if (!strncmp ("lzo" , value,3 ))return 0 ; -else if (!strncmp ("zlib" , value, len)) +else if (!strncmp ("zlib" , value,4 ))return 0 ; -else if (!strncmp ("zstd" , value, len)) +else if (!strncmp ("zstd" , value,4 ))return 0 ;return -EINVAL ;
Re: [PATCH 2/4] btrfs: fix vanished compression property after failed set
Quoted 19 lines · Nikolay Borisov
Quoted 9 lines · Anand Jain
The compression property resets to NULL, instead of the old value if we fail to set the new compression parameter.
btrfs prop get /btrfs compression
compression=lzo
btrfs prop set /btrfs compression zli
ERROR: failed to set compression for /btrfs: Invalid argument
btrfs prop get /btrfs compressionQuoted 6 lines
This is because the compression property ->validate() is successful for 'zli' as the
strncmp()used the len passed from the userland.Fix it by using the expected string length in
strncmp().Signed-off-by Anand Jain <[email protected]>
Quoted 8 lines
The changelog could be a bit clearer. The failure is due to the fact that ->validate succeeds but ->apply fails and the handling code calls
Quoted 1 line
btrfs_setxattr(trans, inode, handler->xattr_name,NULL, 0, flags);However, if you make this patch be the first in the series you obsolete the existing patch 1 and fix the issue in this one. I.e 2 birds with 1 stone.
Right. I wanted to show the bugs, its reason, and fix separately.
Quoted 3 lines
Also I believe btrfs/048 could use some extension in the part where it validates compression properties to test for the bugs discovered/fixed in this series.
Oh. Let me try. Thanks.
-Anand
Quoted 24 lines
Quoted 24 lines
--- fs/btrfs/props.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)fs/btrfs/props.c +3
-3@@ -275,11 +275,11 @@ static int prop_compression_validate(const char *value, size_t len)if (!value)return 0 ; -if (!strncmp ("lzo" , value, len)) +if (!strncmp ("lzo" , value,3 ))return 0 ; -else if (!strncmp ("zlib" , value, len)) +else if (!strncmp ("zlib" , value,4 ))return 0 ; -else if (!strncmp ("zstd" , value, len)) +else if (!strncmp ("zstd" , value,4 ))return 0 ;return -EINVAL ;
PATCH 3/4 btrfs: open code btrfs_set_prop in inherit_prop
In reply to Anand JainWhen an inode inherits property from its parent, we call btrfs_set_prop(). btrfs_set_prop() does an elaborate checks, which is not required in the context of inheriting a property. Instead just open-code only the required items from btrfs_set_prop() and then call btrfs_setxattr() directly. So now the only user of btrfs_set_prop() is gone, (except for the wraper function btrfs_set_prop_trans()).
--- fs/btrfs/props.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-)
fs/btrfs/props.c +21 -6
@@ -367,20 +367,35 @@ static int inherit_props(struct btrfs_trans_handle *trans,if (!value)continue ; +/* may be removed */ + ret = h->validate (value,strlen (value)); +if (ret) +continue ; + num_bytes =btrfs_calc_trans_metadata_size (fs_info,1 ); ret =btrfs_block_rsv_add (root, trans->block_rsv, num_bytes,BTRFS_RESERVE_NO_FLUSH );if (ret) - goto out; - ret =btrfs_set_prop (trans, inode, h->xattr_name, value, +return ret; + + ret =btrfs_setxattr (trans, inode, h->xattr_name, value,strlen (value),0 ); +if (!ret) { + ret = h->apply (inode, value,strlen (value)); +if (ret) +btrfs_setxattr (trans, inode, h->xattr_name, +NULL ,0 ,0 ); +else +set_bit (BTRFS_INODE_HAS_PROPS , + &BTRFS_I (inode)->runtime_flags); + } +btrfs_block_rsv_release (fs_info, trans->block_rsv, num_bytes);if (ret) - goto out; +return ret; } - ret =0 ; -out: -return ret; + +return 0 ; } int btrfs_inode_inherit_props(struct btrfs_trans_handle *trans,
Signature · 1 line
1.8.3.1
Re: [PATCH 3/4] btrfs: open code btrfs_set_prop in inherit_prop
Quoted 6 lines · Anand Jain
When an inode inherits property from its parent, we call
btrfs_set_prop().btrfs_set_prop()does an elaborate checks, which is not required in the context of inheriting a property. Instead just open-code only the required items frombtrfs_set_prop()and then callbtrfs_setxattr()directly. So now the only user ofbtrfs_set_prop()is gone, (except for the wraper functionbtrfs_set_prop_trans()).
Um, no:
git grep -P '(?<!__)btrfs_set_prop' fs/btrfs/
fs/btrfs/ioctl.c: ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); fs/btrfs/ioctl.c: ret = btrfs_set_prop(inode, "btrfs.compression", fs/btrfs/ioctl.c: ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
fs/btrfs/props.c:int btrfs_set_prop(struct inode *inode, fs/btrfs/props.h:int btrfs_set_prop(struct inode *inode,fs/btrfs/xattr.c: return btrfs_set_prop(inode, name, value, size, flags);
Rebase the patch on misc-next and remove the last sentence since it's factually wrong.
Quoted 15 lines
Signed-off-by Anand Jain <[email protected]>--- fs/btrfs/props.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-)fs/btrfs/props.c +1
-0@@ -367,20 +367,35 @@ static int inherit_props(struct btrfs_trans_handle *trans,if (!value)continue ; +/* may be removed */
fs/btrfs/props.c static int inherit_props(struct btrfs_trans_handle *trans, · line 375
Quoted 10 lines
fs/btrfs/props.c +4
-2+ ret = h->validate (value,strlen (value)); +if (ret) +continue ; + num_bytes =btrfs_calc_trans_metadata_size (fs_info,1 ); ret =btrfs_block_rsv_add (root, trans->block_rsv, num_bytes,BTRFS_RESERVE_NO_FLUSH );if (ret) - goto out; - ret = btrfs_set_prop(trans, inode, h->xattr_name, value,
What branch is this code based on, currently misc-next is using (and has been since 2014) __btrfs_set_prop yet you remove btrfs_set_prop?
Quoted 28 lines
fs/btrfs/props.c +16
-4+return ret; + + ret =btrfs_setxattr (trans, inode, h->xattr_name, value,strlen (value),0 ); +if (!ret) { + ret = h->apply (inode, value,strlen (value)); +if (ret) +btrfs_setxattr (trans, inode, h->xattr_name, +NULL ,0 ,0 ); +else +set_bit (BTRFS_INODE_HAS_PROPS , + &BTRFS_I (inode)->runtime_flags); + } +btrfs_block_rsv_release (fs_info, trans->block_rsv, num_bytes);if (ret) - goto out; +return ret; } - ret =0 ; -out: -return ret; + +return 0 ; } int btrfs_inode_inherit_props(struct btrfs_trans_handle *trans,
Re: [PATCH 3/4] btrfs: open code btrfs_set_prop in inherit_prop
Quoted 16 lines · Nikolay Borisov
Quoted 6 lines · Anand Jain
When an inode inherits property from its parent, we call
btrfs_set_prop().btrfs_set_prop()does an elaborate checks, which is not required in the context of inheriting a property. Instead just open-code only the required items frombtrfs_set_prop()and then callbtrfs_setxattr()directly. So now the only user ofbtrfs_set_prop()is gone, (except for the wraper functionbtrfs_set_prop_trans()).Um, no:
git grep -P '(?<!__)btrfs_set_prop'
fs/btrfs/fs/btrfs/ioctl.c: ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); fs/btrfs/ioctl.c: ret = btrfs_set_prop(inode, "btrfs.compression", fs/btrfs/ioctl.c: ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0);
Quoted 7 lines
fs/btrfs/props.c:int btrfs_set_prop(struct inode *inode, fs/btrfs/props.h:int btrfs_set_prop(struct inode *inode,
fs/btrfs/xattr.c: returnbtrfs_set_prop(inode, name, value, size, flags);Rebase the patch on misc-next and remove the last sentence since it's factually wrong.
Oh. This is based on misc-5.2.
Oops I should have mentioned.
Thanks. Anand
PATCH 4/4 btrfs: fix property validate fail should not increment generation
In reply to Anand JainWhen the property fails to pass the prop_handlers::validate() check, the thread should exit with no changes in the kernel, but as we are starting the transaction too early, we have just updated the generation even if there is no change.
For example:
btrfs prop get /btrfs compression
compression=lzo
sync
btrfs in dump-super /dev/sdb | grep "^generation"
generation 32
Try to set an incomplete compression type
btrfs prop set /btrfs compression zli
ERROR: failed to set compression for /btrfs: Invalid argument
sync
Set failed but generation is incremented
btrfs in dump-super /dev/sdb | grep "^generation"
generation 33 <--
Fix it by collapsing btrfs_set_prop_trans() into btrfs_set_prop(), which provides flexibility to start the transaction after the prop_handlers::validate() has been checked.
As of now if the prop_handlers::apply() fails then we still increment the generation, but if there is strong prop_handlers::validate() then the prop_handlers::apply() can never fail.
--- fs/btrfs/ioctl.c | 10 ++++------ fs/btrfs/props.c | 59 +++++++++++++++++++++++++------------------------------- fs/btrfs/props.h | 4 ++-- fs/btrfs/xattr.c | 2 +- 4 files changed, 33 insertions(+), 42 deletions(-)
fs/btrfs/ioctl.c +4 -6
@@ -284,8 +284,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg) binode->flags &= ~BTRFS_INODE_COMPRESS ; binode->flags |=BTRFS_INODE_NOCOMPRESS ; - ret =btrfs_set_prop_trans (inode,"btrfs.compression" ,NULL , -0 ,0 ); + ret =btrfs_set_prop (inode,"btrfs.compression" ,NULL ,0 ,0 );if (ret && ret != -ENODATA ) goto out_drop; }else if (fsflags &FS_COMPR_FL ) {
@@ -303,14 +302,13 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)if (!comp || comp[0 ] ==0 ) comp =btrfs_compress_type2str (BTRFS_COMPRESS_ZLIB ); - ret =btrfs_set_prop_trans (inode,"btrfs.compression" , comp, -strlen (comp),0 ); + ret =btrfs_set_prop (inode,"btrfs.compression" , comp, +strlen (comp),0 );if (ret) goto out_drop; }else { - ret =btrfs_set_prop_trans (inode,"btrfs.compression" ,NULL , -0 ,0 ); + ret =btrfs_set_prop (inode,"btrfs.compression" ,NULL ,0 ,0 );if (ret && ret != -ENODATA ) goto out_drop; binode->flags &= ~(BTRFS_INODE_COMPRESS |BTRFS_INODE_NOCOMPRESS );
fs/btrfs/props.c +26 -33
93 lines not on this page · raw message
fs/btrfs/props.h +2 -2
@@ -10,8 +10,8 @@void __init btrfs_props_init (void ); -int btrfs_set_prop_trans (struct inode *inode,const char *name, -const char *value,size_t value_len,int flags); +int btrfs_set_prop (struct inode *inode,const char *name, +const char *value,size_t value_len,int flags);int btrfs_load_inode_props (struct inode *inode,struct btrfs_path *path);
fs/btrfs/xattr.c +1 -1
@@ -371,7 +371,7 @@ static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler, size_t size, int flags) { name =xattr_full_name (handler, name); -return btrfs_set_prop_trans (inode, name, value, size, flags); +return btrfs_set_prop (inode, name, value, size, flags); } static const struct xattr_handler btrfs_security_xattr_handler = {
Signature · 1 line
1.8.3.1