Skip to content

Commit 0e852ab

Browse files
ccchengkdave
authored andcommitted
btrfs: do not allow compression on nodatacow files
Compression and nodatacow are mutually exclusive. A similar issue was fixed by commit f37c563 ("btrfs: add missing check for nocow and compression inode flags"). Besides ioctl, there is another way to enable/disable/reset compression directly via xattr. The following steps will result in a invalid combination. $ touch bar $ chattr +C bar $ lsattr bar ---------------C-- bar $ setfattr -n btrfs.compression -v zstd bar $ lsattr bar --------c------C-- bar To align with the logic in check_fsflags, nocompress will also be unacceptable after this patch, to prevent mix any compression-related options with nodatacow. $ touch bar $ chattr +C bar $ lsattr bar ---------------C-- bar $ setfattr -n btrfs.compression -v zstd bar setfattr: bar: Invalid argument $ setfattr -n btrfs.compression -v no bar setfattr: bar: Invalid argument When both compression and nodatacow are enabled, then btrfs_run_delalloc_range prefers nodatacow and no compression happens. Reported-by: Jayce Lin <jaycelin@synology.com> CC: stable@vger.kernel.org # 5.10.x: e6f9d69: btrfs: export a helper for compression hard check CC: stable@vger.kernel.org # 5.10.x Reviewed-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Chung-Chiang Cheng <cccheng@synology.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent e6f9d69 commit 0e852ab

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

fs/btrfs/props.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ static DEFINE_HASHTABLE(prop_handlers_ht, BTRFS_PROP_HANDLERS_HT_BITS);
1717
struct prop_handler {
1818
struct hlist_node node;
1919
const char *xattr_name;
20-
int (*validate)(const char *value, size_t len);
20+
int (*validate)(const struct btrfs_inode *inode, const char *value,
21+
size_t len);
2122
int (*apply)(struct inode *inode, const char *value, size_t len);
2223
const char *(*extract)(struct inode *inode);
2324
int inheritable;
@@ -55,7 +56,8 @@ find_prop_handler(const char *name,
5556
return NULL;
5657
}
5758

58-
int btrfs_validate_prop(const char *name, const char *value, size_t value_len)
59+
int btrfs_validate_prop(const struct btrfs_inode *inode, const char *name,
60+
const char *value, size_t value_len)
5961
{
6062
const struct prop_handler *handler;
6163

@@ -69,7 +71,7 @@ int btrfs_validate_prop(const char *name, const char *value, size_t value_len)
6971
if (value_len == 0)
7072
return 0;
7173

72-
return handler->validate(value, value_len);
74+
return handler->validate(inode, value, value_len);
7375
}
7476

7577
int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
@@ -252,8 +254,12 @@ int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path)
252254
return ret;
253255
}
254256

255-
static int prop_compression_validate(const char *value, size_t len)
257+
static int prop_compression_validate(const struct btrfs_inode *inode,
258+
const char *value, size_t len)
256259
{
260+
if (!btrfs_inode_can_compress(inode))
261+
return -EINVAL;
262+
257263
if (!value)
258264
return 0;
259265

@@ -364,7 +370,7 @@ static int inherit_props(struct btrfs_trans_handle *trans,
364370
* This is not strictly necessary as the property should be
365371
* valid, but in case it isn't, don't propagate it further.
366372
*/
367-
ret = h->validate(value, strlen(value));
373+
ret = h->validate(BTRFS_I(inode), value, strlen(value));
368374
if (ret)
369375
continue;
370376

fs/btrfs/props.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ void __init btrfs_props_init(void);
1313
int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
1414
const char *name, const char *value, size_t value_len,
1515
int flags);
16-
int btrfs_validate_prop(const char *name, const char *value, size_t value_len);
16+
int btrfs_validate_prop(const struct btrfs_inode *inode, const char *name,
17+
const char *value, size_t value_len);
1718

1819
int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path);
1920

fs/btrfs/xattr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
403403
struct btrfs_root *root = BTRFS_I(inode)->root;
404404

405405
name = xattr_full_name(handler, name);
406-
ret = btrfs_validate_prop(name, value, size);
406+
ret = btrfs_validate_prop(BTRFS_I(inode), name, value, size);
407407
if (ret)
408408
return ret;
409409

0 commit comments

Comments
 (0)