Commit 4b077b50 authored by Jingbo Xu's avatar Jingbo Xu Committed by Gao Xiang

erofs: unify inline/shared xattr iterators for listxattr/getxattr

Make inline_{list,get}xattr() as well as inline_xattr_iter_begin()
unified as erofs_xattr_iter_inline(), and shared_{list,get}xattr()
unified as erofs_xattr_iter_shared().

After these changes, both erofs_xattr_iter_{inline,shared}() return 0 on
success, and negative error on failure.

One thing worth noting is that, the logic of returning it->buffer_ofs
when there's no shared xattrs in shared_listxattr() is moved to
erofs_listxattr() to make the unification possible.  The only difference
is that, semantically the old behavior will return ENOATTR rather than
it->buffer_ofs if ENOATTR encountered when listxattr is parsing upon a
specific shared xattr, while now the new behavior will return
it->buffer_ofs in this case.  This is not an issue, as listxattr upon a
specific xattr won't return ENOATTR.
Signed-off-by: default avatarJingbo Xu <jefflexu@linux.alibaba.com>
Reviewed-by: default avatarGao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20230613074114.120115-5-jefflexu@linux.alibaba.comSigned-off-by: default avatarGao Xiang <hsiangkao@linux.alibaba.com>
parent 5a8ffb19
...@@ -139,27 +139,6 @@ struct xattr_iter_handlers { ...@@ -139,27 +139,6 @@ struct xattr_iter_handlers {
unsigned int len); unsigned int len);
}; };
static int inline_xattr_iter_begin(struct erofs_xattr_iter *it,
struct inode *inode)
{
struct erofs_inode *const vi = EROFS_I(inode);
unsigned int xattr_header_sz;
xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) +
sizeof(u32) * vi->xattr_shared_count;
if (xattr_header_sz >= vi->xattr_isize) {
DBG_BUGON(xattr_header_sz > vi->xattr_isize);
return -ENOATTR;
}
it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz;
it->kaddr = erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos),
EROFS_KMAP);
if (IS_ERR(it->kaddr))
return PTR_ERR(it->kaddr);
return vi->xattr_isize - xattr_header_sz;
}
/* /*
* Regardless of success or failure, `xattr_foreach' will end up with * Regardless of success or failure, `xattr_foreach' will end up with
* `pos' pointing to the next xattr item rather than an arbitrary position. * `pos' pointing to the next xattr item rather than an arbitrary position.
...@@ -333,47 +312,6 @@ static const struct xattr_iter_handlers find_xattr_handlers = { ...@@ -333,47 +312,6 @@ static const struct xattr_iter_handlers find_xattr_handlers = {
.value = xattr_copyvalue .value = xattr_copyvalue
}; };
static int inline_getxattr(struct inode *inode, struct erofs_xattr_iter *it)
{
int ret;
unsigned int remaining;
ret = inline_xattr_iter_begin(it, inode);
if (ret < 0)
return ret;
remaining = ret;
while (remaining) {
ret = xattr_foreach(it, &find_xattr_handlers, &remaining);
if (ret != -ENOATTR)
break;
}
return ret ? ret : it->buffer_ofs;
}
static int shared_getxattr(struct inode *inode, struct erofs_xattr_iter *it)
{
struct erofs_inode *const vi = EROFS_I(inode);
struct super_block *const sb = it->sb;
struct erofs_sb_info *sbi = EROFS_SB(sb);
unsigned int i;
int ret = -ENOATTR;
for (i = 0; i < vi->xattr_shared_count; ++i) {
it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
vi->xattr_shared_xattrs[i] * sizeof(__le32);
it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
EROFS_KMAP);
if (IS_ERR(it->kaddr))
return PTR_ERR(it->kaddr);
ret = xattr_foreach(it, &find_xattr_handlers, NULL);
if (ret != -ENOATTR)
break;
}
return ret ? ret : it->buffer_ofs;
}
static bool erofs_xattr_user_list(struct dentry *dentry) static bool erofs_xattr_user_list(struct dentry *dentry)
{ {
return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER); return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
...@@ -384,39 +322,6 @@ static bool erofs_xattr_trusted_list(struct dentry *dentry) ...@@ -384,39 +322,6 @@ static bool erofs_xattr_trusted_list(struct dentry *dentry)
return capable(CAP_SYS_ADMIN); return capable(CAP_SYS_ADMIN);
} }
int erofs_getxattr(struct inode *inode, int index,
const char *name,
void *buffer, size_t buffer_size)
{
int ret;
struct erofs_xattr_iter it;
if (!name)
return -EINVAL;
ret = erofs_init_inode_xattrs(inode);
if (ret)
return ret;
it.index = index;
it.name = (struct qstr)QSTR_INIT(name, strlen(name));
if (it.name.len > EROFS_NAME_LEN)
return -ERANGE;
it.sb = inode->i_sb;
it.buf = __EROFS_BUF_INITIALIZER;
erofs_init_metabuf(&it.buf, it.sb);
it.buffer = buffer;
it.buffer_size = buffer_size;
it.buffer_ofs = 0;
ret = inline_getxattr(inode, &it);
if (ret == -ENOATTR)
ret = shared_getxattr(inode, &it);
erofs_put_metabuf(&it.buf);
return ret;
}
static int erofs_xattr_generic_get(const struct xattr_handler *handler, static int erofs_xattr_generic_get(const struct xattr_handler *handler,
struct dentry *unused, struct inode *inode, struct dentry *unused, struct inode *inode,
const char *name, void *buffer, size_t size) const char *name, void *buffer, size_t size)
...@@ -521,32 +426,49 @@ static const struct xattr_iter_handlers list_xattr_handlers = { ...@@ -521,32 +426,49 @@ static const struct xattr_iter_handlers list_xattr_handlers = {
.value = NULL .value = NULL
}; };
static int inline_listxattr(struct erofs_xattr_iter *it) static int erofs_xattr_iter_inline(struct erofs_xattr_iter *it,
struct inode *inode, bool getxattr)
{ {
struct erofs_inode *const vi = EROFS_I(inode);
const struct xattr_iter_handlers *op;
unsigned int xattr_header_sz, remaining;
int ret; int ret;
unsigned int remaining;
ret = inline_xattr_iter_begin(it, d_inode(it->dentry)); xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) +
if (ret < 0) sizeof(u32) * vi->xattr_shared_count;
return ret; if (xattr_header_sz >= vi->xattr_isize) {
DBG_BUGON(xattr_header_sz > vi->xattr_isize);
return -ENOATTR;
}
it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz;
it->kaddr = erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos),
EROFS_KMAP);
if (IS_ERR(it->kaddr))
return PTR_ERR(it->kaddr);
remaining = vi->xattr_isize - xattr_header_sz;
op = getxattr ? &find_xattr_handlers : &list_xattr_handlers;
remaining = ret;
while (remaining) { while (remaining) {
ret = xattr_foreach(it, &list_xattr_handlers, &remaining); ret = xattr_foreach(it, op, &remaining);
if (ret) if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
break; break;
} }
return ret ? ret : it->buffer_ofs; return ret;
} }
static int shared_listxattr(struct erofs_xattr_iter *it) static int erofs_xattr_iter_shared(struct erofs_xattr_iter *it,
struct inode *inode, bool getxattr)
{ {
struct inode *const inode = d_inode(it->dentry);
struct erofs_inode *const vi = EROFS_I(inode); struct erofs_inode *const vi = EROFS_I(inode);
struct super_block *const sb = it->sb; struct super_block *const sb = it->sb;
struct erofs_sb_info *sbi = EROFS_SB(sb); struct erofs_sb_info *sbi = EROFS_SB(sb);
unsigned int i; unsigned int i;
int ret = 0; const struct xattr_iter_handlers *op;
int ret = -ENOATTR;
op = getxattr ? &find_xattr_handlers : &list_xattr_handlers;
for (i = 0; i < vi->xattr_shared_count; ++i) { for (i = 0; i < vi->xattr_shared_count; ++i) {
it->pos = erofs_pos(sb, sbi->xattr_blkaddr) + it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
...@@ -556,20 +478,52 @@ static int shared_listxattr(struct erofs_xattr_iter *it) ...@@ -556,20 +478,52 @@ static int shared_listxattr(struct erofs_xattr_iter *it)
if (IS_ERR(it->kaddr)) if (IS_ERR(it->kaddr))
return PTR_ERR(it->kaddr); return PTR_ERR(it->kaddr);
ret = xattr_foreach(it, &list_xattr_handlers, NULL); ret = xattr_foreach(it, op, NULL);
if (ret) if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
break; break;
} }
return ret ? ret : it->buffer_ofs; return ret;
} }
ssize_t erofs_listxattr(struct dentry *dentry, int erofs_getxattr(struct inode *inode, int index, const char *name,
char *buffer, size_t buffer_size) void *buffer, size_t buffer_size)
{ {
int ret; int ret;
struct erofs_xattr_iter it; struct erofs_xattr_iter it;
ret = erofs_init_inode_xattrs(d_inode(dentry)); if (!name)
return -EINVAL;
ret = erofs_init_inode_xattrs(inode);
if (ret)
return ret;
it.index = index;
it.name = (struct qstr)QSTR_INIT(name, strlen(name));
if (it.name.len > EROFS_NAME_LEN)
return -ERANGE;
it.sb = inode->i_sb;
it.buf = __EROFS_BUF_INITIALIZER;
erofs_init_metabuf(&it.buf, it.sb);
it.buffer = buffer;
it.buffer_size = buffer_size;
it.buffer_ofs = 0;
ret = erofs_xattr_iter_inline(&it, inode, true);
if (ret == -ENOATTR)
ret = erofs_xattr_iter_shared(&it, inode, true);
erofs_put_metabuf(&it.buf);
return ret ? ret : it.buffer_ofs;
}
ssize_t erofs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
{
int ret;
struct erofs_xattr_iter it;
struct inode *inode = d_inode(dentry);
ret = erofs_init_inode_xattrs(inode);
if (ret == -ENOATTR) if (ret == -ENOATTR)
return 0; return 0;
if (ret) if (ret)
...@@ -583,11 +537,13 @@ ssize_t erofs_listxattr(struct dentry *dentry, ...@@ -583,11 +537,13 @@ ssize_t erofs_listxattr(struct dentry *dentry,
it.buffer_size = buffer_size; it.buffer_size = buffer_size;
it.buffer_ofs = 0; it.buffer_ofs = 0;
ret = inline_listxattr(&it); ret = erofs_xattr_iter_inline(&it, inode, false);
if (ret >= 0 || ret == -ENOATTR) if (!ret || ret == -ENOATTR)
ret = shared_listxattr(&it); ret = erofs_xattr_iter_shared(&it, inode, false);
if (ret == -ENOATTR)
ret = 0;
erofs_put_metabuf(&it.buf); erofs_put_metabuf(&it.buf);
return ret; return ret ? ret : it.buffer_ofs;
} }
void erofs_xattr_prefixes_cleanup(struct super_block *sb) void erofs_xattr_prefixes_cleanup(struct super_block *sb)
......
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