From: Anand Jain <anand.jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.cz
Subject: [PATCH 1/2 RFC] btrfs: refactor btrfs_set_props to validate externally
Date: Wed, 17 Apr 2019 23:37:05 +0800 [thread overview]
Message-ID: <20190417153706.29942-2-anand.jain@oracle.com> (raw)
In-Reply-To: <20190417153706.29942-1-anand.jain@oracle.com>
In preparation to merge multiple transactions when setting the
inode attribute flags, move btrfs_set_props()'s validation part
to outside.
Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
fs/btrfs/ioctl.c | 3 +++
fs/btrfs/props.c | 23 +++++++++++++++++------
fs/btrfs/props.h | 1 +
fs/btrfs/xattr.c | 5 +++++
4 files changed, 26 insertions(+), 6 deletions(-)
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 8c9a908d3acc..63e6e2f5b659 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -284,6 +284,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
binode->flags &= ~BTRFS_INODE_COMPRESS;
binode->flags |= BTRFS_INODE_NOCOMPRESS;
+ /* set no-compression no need to validate prop here */
ret = btrfs_set_prop_trans(inode, "btrfs.compression", NULL,
0, 0);
if (ret && ret != -ENODATA)
@@ -299,6 +300,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
binode->flags |= BTRFS_INODE_COMPRESS;
binode->flags &= ~BTRFS_INODE_NOCOMPRESS;
+ /* compress_type is already validated during mount options */
comp = btrfs_compress_type2str(fs_info->compress_type);
if (!comp || comp[0] == 0)
comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB);
@@ -309,6 +311,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
goto out_drop;
} else {
+ /* reset prop, no need of validate prop here */
ret = btrfs_set_prop_trans(inode, "btrfs.compression", NULL,
0, 0);
if (ret && ret != -ENODATA)
diff --git a/fs/btrfs/props.c b/fs/btrfs/props.c
index 44b7bf647ab3..e356dd2a0f73 100644
--- a/fs/btrfs/props.c
+++ b/fs/btrfs/props.c
@@ -55,6 +55,23 @@ find_prop_handler(const char *name,
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,
const char *name, const char *value, size_t value_len,
int flags)
@@ -62,9 +79,6 @@ static int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
const struct prop_handler *handler;
int ret;
- if (strlen(name) <= XATTR_BTRFS_PREFIX_LEN)
- return -EINVAL;
-
handler = find_prop_handler(name, NULL);
if (!handler)
return -EINVAL;
@@ -85,9 +99,6 @@ static int btrfs_set_prop(struct btrfs_trans_handle *trans, struct inode *inode,
return ret;
}
- ret = handler->validate(value, value_len);
- if (ret)
- return ret;
if (trans)
ret = btrfs_setxattr(trans, inode, handler->xattr_name, value,
value_len, flags);
diff --git a/fs/btrfs/props.h b/fs/btrfs/props.h
index b1a6b233b774..01d2c1899bc7 100644
--- a/fs/btrfs/props.h
+++ b/fs/btrfs/props.h
@@ -12,6 +12,7 @@ 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_validate_prop(const char *name, const char *value, size_t value_len);
int btrfs_load_inode_props(struct inode *inode, struct btrfs_path *path);
diff --git a/fs/btrfs/xattr.c b/fs/btrfs/xattr.c
index 10da873d11f5..807cc24dc885 100644
--- a/fs/btrfs/xattr.c
+++ b/fs/btrfs/xattr.c
@@ -373,7 +373,12 @@ static int btrfs_xattr_handler_set_prop(const struct xattr_handler *handler,
const char *name, const void *value,
size_t size, int flags)
{
+ int ret;
+
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);
}
--
2.17.1
next prev parent reply other threads:[~2019-04-17 15:37 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-17 15:37 [PATCH 0/2 RFC] fix duplicate code in btrfs_ioctl_setflags Anand Jain
2019-04-17 15:37 ` Anand Jain [this message]
2019-04-17 15:37 ` [PATCH 2/2 RFC] btrfs: start transaction in btrfs_ioctl_setflags() Anand Jain
2019-04-18 13:23 ` David Sterba
2019-04-18 14:27 ` Anand Jain
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190417153706.29942-2-anand.jain@oracle.com \
--to=anand.jain@oracle.com \
--cc=dsterba@suse.cz \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).