From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org, Christoph Hellwig <hch@lst.de>
Cc: Al Viro <viro@zeniv.linux.org.uk>,
Seth Forshee <sforshee@kernel.org>,
"Christian Brauner (Microsoft)" <brauner@kernel.org>
Subject: [PATCH v2 2/8] xattr: simplify listxattr helpers
Date: Mon, 30 Jan 2023 17:41:58 +0100 [thread overview]
Message-ID: <20230125-fs-acl-remove-generic-xattr-handlers-v2-2-214cfb88bb56@kernel.org> (raw)
In-Reply-To: <20230125-fs-acl-remove-generic-xattr-handlers-v2-0-214cfb88bb56@kernel.org>
The generic_listxattr() and simple_xattr_list() helpers list xattrs and
contain duplicated code. Add two helpers that both generic_listxattr()
and simple_xattr_list() can use.
Signed-off-by: Christian Brauner (Microsoft) <brauner@kernel.org>
---
Changes in v2:
- Christoph Hellwig <hch@lst.de>:
- Leave newline after variable declaration in xattr_list_one().
- Move posix_acl_listxattr() into fs/posix_acl.c.
---
fs/posix_acl.c | 25 +++++++++++++
fs/xattr.c | 89 ++++++++++++++++++-----------------------------
include/linux/posix_acl.h | 7 ++++
include/linux/xattr.h | 1 +
4 files changed, 66 insertions(+), 56 deletions(-)
diff --git a/fs/posix_acl.c b/fs/posix_acl.c
index 315d3926a13a..d5e9db0e4d66 100644
--- a/fs/posix_acl.c
+++ b/fs/posix_acl.c
@@ -958,6 +958,31 @@ set_posix_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
}
EXPORT_SYMBOL(set_posix_acl);
+int posix_acl_listxattr(struct inode *inode, char **buffer,
+ ssize_t *remaining_size)
+{
+ int err;
+
+ if (!IS_POSIXACL(inode))
+ return 0;
+
+ if (inode->i_acl) {
+ err = xattr_list_one(buffer, remaining_size,
+ XATTR_NAME_POSIX_ACL_ACCESS);
+ if (err)
+ return err;
+ }
+
+ if (inode->i_default_acl) {
+ err = xattr_list_one(buffer, remaining_size,
+ XATTR_NAME_POSIX_ACL_DEFAULT);
+ if (err)
+ return err;
+ }
+
+ return 0;
+}
+
static bool
posix_acl_xattr_list(struct dentry *dentry)
{
diff --git a/fs/xattr.c b/fs/xattr.c
index 89b6c122056d..0e8ce76cb76a 100644
--- a/fs/xattr.c
+++ b/fs/xattr.c
@@ -949,6 +949,21 @@ SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
return error;
}
+int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name)
+{
+ size_t len;
+
+ len = strlen(name) + 1;
+ if (*buffer) {
+ if (*remaining_size < len)
+ return -ERANGE;
+ memcpy(*buffer, name, len);
+ *buffer += len;
+ }
+ *remaining_size -= len;
+ return 0;
+}
+
/*
* Combine the results of the list() operation from every xattr_handler in the
* list.
@@ -957,33 +972,22 @@ ssize_t
generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
{
const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
- unsigned int size = 0;
-
- if (!buffer) {
- for_each_xattr_handler(handlers, handler) {
- if (!handler->name ||
- (handler->list && !handler->list(dentry)))
- continue;
- size += strlen(handler->name) + 1;
- }
- } else {
- char *buf = buffer;
- size_t len;
-
- for_each_xattr_handler(handlers, handler) {
- if (!handler->name ||
- (handler->list && !handler->list(dentry)))
- continue;
- len = strlen(handler->name);
- if (len + 1 > buffer_size)
- return -ERANGE;
- memcpy(buf, handler->name, len + 1);
- buf += len + 1;
- buffer_size -= len + 1;
- }
- size = buf - buffer;
+ ssize_t remaining_size = buffer_size;
+ int err = 0;
+
+ err = posix_acl_listxattr(d_inode(dentry), &buffer, &remaining_size);
+ if (err)
+ return err;
+
+ for_each_xattr_handler(handlers, handler) {
+ if (!handler->name || (handler->list && !handler->list(dentry)))
+ continue;
+ err = xattr_list_one(&buffer, &remaining_size, handler->name);
+ if (err)
+ return err;
}
- return size;
+
+ return err ? err : buffer_size - remaining_size;
}
EXPORT_SYMBOL(generic_listxattr);
@@ -1245,20 +1249,6 @@ static bool xattr_is_trusted(const char *name)
return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
}
-static int xattr_list_one(char **buffer, ssize_t *remaining_size,
- const char *name)
-{
- size_t len = strlen(name) + 1;
- if (*buffer) {
- if (*remaining_size < len)
- return -ERANGE;
- memcpy(*buffer, name, len);
- *buffer += len;
- }
- *remaining_size -= len;
- return 0;
-}
-
/**
* simple_xattr_list - list all xattr objects
* @inode: inode from which to get the xattrs
@@ -1287,22 +1277,9 @@ ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
ssize_t remaining_size = size;
int err = 0;
-#ifdef CONFIG_FS_POSIX_ACL
- if (IS_POSIXACL(inode)) {
- if (inode->i_acl) {
- err = xattr_list_one(&buffer, &remaining_size,
- XATTR_NAME_POSIX_ACL_ACCESS);
- if (err)
- return err;
- }
- if (inode->i_default_acl) {
- err = xattr_list_one(&buffer, &remaining_size,
- XATTR_NAME_POSIX_ACL_DEFAULT);
- if (err)
- return err;
- }
- }
-#endif
+ err = posix_acl_listxattr(inode, &buffer, &remaining_size);
+ if (err)
+ return err;
read_lock(&xattrs->lock);
for (rbp = rb_first(&xattrs->rb_root); rbp; rbp = rb_next(rbp)) {
diff --git a/include/linux/posix_acl.h b/include/linux/posix_acl.h
index ee608d22ecb9..bb229b98761a 100644
--- a/include/linux/posix_acl.h
+++ b/include/linux/posix_acl.h
@@ -106,6 +106,8 @@ struct posix_acl *vfs_get_acl(struct user_namespace *mnt_userns,
struct dentry *dentry, const char *acl_name);
int vfs_remove_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
const char *acl_name);
+int posix_acl_listxattr(struct inode *inode, char **buffer,
+ ssize_t *remaining_size);
#else
static inline int posix_acl_chmod(struct user_namespace *mnt_userns,
struct dentry *dentry, umode_t mode)
@@ -153,6 +155,11 @@ static inline int vfs_remove_acl(struct user_namespace *mnt_userns,
{
return -EOPNOTSUPP;
}
+int posix_acl_listxattr(struct inode *inode, char **buffer,
+ ssize_t *remaining_size)
+{
+ return 0;
+}
#endif /* CONFIG_FS_POSIX_ACL */
struct posix_acl *get_inode_acl(struct inode *inode, int type);
diff --git a/include/linux/xattr.h b/include/linux/xattr.h
index 23bbe98cfc16..db022d6548fb 100644
--- a/include/linux/xattr.h
+++ b/include/linux/xattr.h
@@ -120,5 +120,6 @@ static inline bool inode_xattr_disabled(struct inode *inode)
return !(inode->i_opflags & IOP_XATTR) &&
(inode->i_opflags & IOP_NOACL);
}
+int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name);
#endif /* _LINUX_XATTR_H */
--
2.34.1
next prev parent reply other threads:[~2023-01-30 16:42 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-30 16:41 [PATCH v2 0/8] acl: remove generic posix acl handlers from all xattr handlers Christian Brauner
2023-01-30 16:41 ` [PATCH v2 1/8] fs: don't use IOP_XATTR for posix acls Christian Brauner
2023-01-30 16:50 ` Christoph Hellwig
2023-01-30 18:09 ` Christian Brauner
2023-01-31 11:36 ` Christian Brauner
2023-01-31 14:55 ` Christian Brauner
2023-01-31 15:18 ` Christoph Hellwig
2023-01-30 16:41 ` Christian Brauner [this message]
2023-01-30 16:51 ` [PATCH v2 2/8] xattr: simplify listxattr helpers Christoph Hellwig
2023-01-30 16:41 ` [PATCH v2 3/8] xattr: add listxattr helper Christian Brauner
2023-01-30 16:51 ` Christoph Hellwig
2023-01-30 16:42 ` [PATCH v2 4/8] xattr: remove unused argument Christian Brauner
2023-01-30 16:42 ` [PATCH v2 5/8] fs: drop unused posix acl handlers Christian Brauner
2023-01-30 16:42 ` [PATCH v2 6/8] fs: simplify ->listxattr() implementation Christian Brauner
2023-01-30 16:52 ` Christoph Hellwig
2023-01-30 16:42 ` [PATCH v2 7/8] reiserfs: rework " Christian Brauner
2023-01-30 16:42 ` [PATCH v2 8/8] fs: rename generic posix acl handlers Christian Brauner
2023-01-30 16:53 ` Christoph Hellwig
2023-04-26 23:07 ` [f2fs-dev] [PATCH v2 0/8] acl: remove generic posix acl handlers from all xattr handlers patchwork-bot+f2fs
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=20230125-fs-acl-remove-generic-xattr-handlers-v2-2-214cfb88bb56@kernel.org \
--to=brauner@kernel.org \
--cc=hch@lst.de \
--cc=linux-fsdevel@vger.kernel.org \
--cc=sforshee@kernel.org \
--cc=viro@zeniv.linux.org.uk \
/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).