Commit f22125e5 authored by Anand Jain's avatar Anand Jain Committed by David Sterba

btrfs: refactor btrfs_set_props to validate externally

In preparation to merge multiple transactions when setting the
compression flags, split btrfs_set_props() validation part outside of
it.
Signed-off-by: default avatarAnand Jain <anand.jain@oracle.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 7c15d410
...@@ -284,6 +284,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg) ...@@ -284,6 +284,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
binode->flags &= ~BTRFS_INODE_COMPRESS; binode->flags &= ~BTRFS_INODE_COMPRESS;
binode->flags |= BTRFS_INODE_NOCOMPRESS; binode->flags |= BTRFS_INODE_NOCOMPRESS;
/* set no-compression no need to validate prop here */
ret = btrfs_set_prop_trans(inode, "btrfs.compression", NULL, ret = btrfs_set_prop_trans(inode, "btrfs.compression", NULL,
0, 0); 0, 0);
if (ret && ret != -ENODATA) if (ret && ret != -ENODATA)
...@@ -299,6 +300,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg) ...@@ -299,6 +300,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
binode->flags |= BTRFS_INODE_COMPRESS; binode->flags |= BTRFS_INODE_COMPRESS;
binode->flags &= ~BTRFS_INODE_NOCOMPRESS; binode->flags &= ~BTRFS_INODE_NOCOMPRESS;
/* compress_type is already validated during mount options */
comp = btrfs_compress_type2str(fs_info->compress_type); comp = btrfs_compress_type2str(fs_info->compress_type);
if (!comp || comp[0] == 0) if (!comp || comp[0] == 0)
comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
...@@ -309,6 +311,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg) ...@@ -309,6 +311,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
goto out_drop; goto out_drop;
} else { } else {
/* reset prop, no need of validate prop here */
ret = btrfs_set_prop_trans(inode, "btrfs.compression", NULL, ret = btrfs_set_prop_trans(inode, "btrfs.compression", NULL,
0, 0); 0, 0);
if (ret && ret != -ENODATA) if (ret && ret != -ENODATA)
......
...@@ -55,6 +55,23 @@ find_prop_handler(const char *name, ...@@ -55,6 +55,23 @@ find_prop_handler(const char *name,
return NULL; return NULL;
} }
int btrfs_validate_prop(const char *name, const char *value, size_t value_len)
{
const struct prop_handler *handler;
if (strlen(name) <= XATTR_BTRFS_PREFIX_LEN)
return -EINVAL;
handler = find_prop_handler(name, NULL);
if (!handler)
return -EINVAL;
if (value_len == 0)
return 0;
return handler->validate(value, value_len);
}
static int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode, static int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
const char *name, const char *value, size_t value_len, const char *name, const char *value, size_t value_len,
int flags) int flags)
...@@ -62,9 +79,6 @@ static int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode, ...@@ -62,9 +79,6 @@ static int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
const struct prop_handler *handler; const struct prop_handler *handler;
int ret; int ret;
if (strlen(name) <= XATTR_BTRFS_PREFIX_LEN)
return -EINVAL;
handler = find_prop_handler(name, NULL); handler = find_prop_handler(name, NULL);
if (!handler) if (!handler)
return -EINVAL; return -EINVAL;
...@@ -85,9 +99,6 @@ static int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode, ...@@ -85,9 +99,6 @@ static int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
return ret; return ret;
} }
ret = handler->validate(value, value_len);
if (ret)
return ret;
if (trans) if (trans)
ret = btrfs_setxattr(trans, inode, handler->xattr_name, value, ret = btrfs_setxattr(trans, inode, handler->xattr_name, value,
value_len, flags); value_len, flags);
......
...@@ -12,6 +12,7 @@ void __init btrfs_props_init(void); ...@@ -12,6 +12,7 @@ void __init btrfs_props_init(void);
int btrfs_set_prop_trans(struct inode *inode, const char *name, int btrfs_set_prop_trans(struct inode *inode, const char *name,
const char *value, size_t value_len, int flags); const char *value, size_t value_len, int flags);
int btrfs_validate_prop(const char *name, const char *value, size_t value_len);
int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path); int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path);
......
...@@ -371,7 +371,12 @@ static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler, ...@@ -371,7 +371,12 @@ static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
const char *name, const void *value, const char *name, const void *value,
size_t size, int flags) size_t size, int flags)
{ {
int ret;
name = xattr_full_name(handler, name); name = xattr_full_name(handler, name);
ret = btrfs_validate_prop(name, value, size);
if (ret)
return ret;
return btrfs_set_prop_trans(inode, name, value, size, flags); return btrfs_set_prop_trans(inode, name, value, size, flags);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment