Commit 8e823961 authored by Jingbo Xu's avatar Jingbo Xu Committed by Gao Xiang

erofs: unify xattr_iter structures

Unify xattr_iter/listxattr_iter/getxattr_iter structures into
erofs_xattr_iter structure.

This is in preparation for the following further cleanup.
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-3-jefflexu@linux.alibaba.comSigned-off-by: default avatarGao Xiang <hsiangkao@linux.alibaba.com>
parent eba67eb6
...@@ -7,17 +7,27 @@ ...@@ -7,17 +7,27 @@
#include <linux/security.h> #include <linux/security.h>
#include "xattr.h" #include "xattr.h"
struct xattr_iter { struct erofs_xattr_iter {
struct super_block *sb; struct super_block *sb;
struct erofs_buf buf; struct erofs_buf buf;
erofs_off_t pos; erofs_off_t pos;
void *kaddr; void *kaddr;
char *buffer;
int buffer_size, buffer_ofs;
/* getxattr */
int index, infix_len;
struct qstr name;
/* listxattr */
struct dentry *dentry;
}; };
static int erofs_init_inode_xattrs(struct inode *inode) static int erofs_init_inode_xattrs(struct inode *inode)
{ {
struct erofs_inode *const vi = EROFS_I(inode); struct erofs_inode *const vi = EROFS_I(inode);
struct xattr_iter it; struct erofs_xattr_iter it;
unsigned int i; unsigned int i;
struct erofs_xattr_ibody_header *ih; struct erofs_xattr_ibody_header *ih;
struct super_block *sb = inode->i_sb; struct super_block *sb = inode->i_sb;
...@@ -121,15 +131,15 @@ static int erofs_init_inode_xattrs(struct inode *inode) ...@@ -121,15 +131,15 @@ static int erofs_init_inode_xattrs(struct inode *inode)
* and need to be handled * and need to be handled
*/ */
struct xattr_iter_handlers { struct xattr_iter_handlers {
int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry); int (*entry)(struct erofs_xattr_iter *it, struct erofs_xattr_entry *entry);
int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf, int (*name)(struct erofs_xattr_iter *it, unsigned int processed, char *buf,
unsigned int len); unsigned int len);
int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz); int (*alloc_buffer)(struct erofs_xattr_iter *it, unsigned int value_sz);
void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf, void (*value)(struct erofs_xattr_iter *it, unsigned int processed, char *buf,
unsigned int len); unsigned int len);
}; };
static int inline_xattr_iter_begin(struct xattr_iter *it, static int inline_xattr_iter_begin(struct erofs_xattr_iter *it,
struct inode *inode) struct inode *inode)
{ {
struct erofs_inode *const vi = EROFS_I(inode); struct erofs_inode *const vi = EROFS_I(inode);
...@@ -154,7 +164,7 @@ static int inline_xattr_iter_begin(struct xattr_iter *it, ...@@ -154,7 +164,7 @@ static int inline_xattr_iter_begin(struct xattr_iter *it,
* 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.
*/ */
static int xattr_foreach(struct xattr_iter *it, static int xattr_foreach(struct erofs_xattr_iter *it,
const struct xattr_iter_handlers *op, const struct xattr_iter_handlers *op,
unsigned int *tlimit) unsigned int *tlimit)
{ {
...@@ -257,18 +267,10 @@ static int xattr_foreach(struct xattr_iter *it, ...@@ -257,18 +267,10 @@ static int xattr_foreach(struct xattr_iter *it,
return err < 0 ? err : 0; return err < 0 ? err : 0;
} }
struct getxattr_iter { static int erofs_xattr_long_entrymatch(struct erofs_xattr_iter *it,
struct xattr_iter it;
char *buffer;
int buffer_size, index, infix_len;
struct qstr name;
};
static int erofs_xattr_long_entrymatch(struct getxattr_iter *it,
struct erofs_xattr_entry *entry) struct erofs_xattr_entry *entry)
{ {
struct erofs_sb_info *sbi = EROFS_SB(it->it.sb); struct erofs_sb_info *sbi = EROFS_SB(it->sb);
struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes + struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
(entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); (entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
...@@ -286,11 +288,9 @@ static int erofs_xattr_long_entrymatch(struct getxattr_iter *it, ...@@ -286,11 +288,9 @@ static int erofs_xattr_long_entrymatch(struct getxattr_iter *it,
return 0; return 0;
} }
static int xattr_entrymatch(struct xattr_iter *_it, static int xattr_entrymatch(struct erofs_xattr_iter *it,
struct erofs_xattr_entry *entry) struct erofs_xattr_entry *entry)
{ {
struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
/* should also match the infix for long name prefixes */ /* should also match the infix for long name prefixes */
if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX) if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX)
return erofs_xattr_long_entrymatch(it, entry); return erofs_xattr_long_entrymatch(it, entry);
...@@ -302,32 +302,27 @@ static int xattr_entrymatch(struct xattr_iter *_it, ...@@ -302,32 +302,27 @@ static int xattr_entrymatch(struct xattr_iter *_it,
return 0; return 0;
} }
static int xattr_namematch(struct xattr_iter *_it, static int xattr_namematch(struct erofs_xattr_iter *it,
unsigned int processed, char *buf, unsigned int len) unsigned int processed, char *buf, unsigned int len)
{ {
struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
if (memcmp(buf, it->name.name + it->infix_len + processed, len)) if (memcmp(buf, it->name.name + it->infix_len + processed, len))
return -ENOATTR; return -ENOATTR;
return 0; return 0;
} }
static int xattr_checkbuffer(struct xattr_iter *_it, static int xattr_checkbuffer(struct erofs_xattr_iter *it,
unsigned int value_sz) unsigned int value_sz)
{ {
struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
int err = it->buffer_size < value_sz ? -ERANGE : 0; int err = it->buffer_size < value_sz ? -ERANGE : 0;
it->buffer_size = value_sz; it->buffer_size = value_sz;
return !it->buffer ? 1 : err; return !it->buffer ? 1 : err;
} }
static void xattr_copyvalue(struct xattr_iter *_it, static void xattr_copyvalue(struct erofs_xattr_iter *it,
unsigned int processed, unsigned int processed,
char *buf, unsigned int len) char *buf, unsigned int len)
{ {
struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
memcpy(it->buffer + processed, buf, len); memcpy(it->buffer + processed, buf, len);
} }
...@@ -338,41 +333,41 @@ static const struct xattr_iter_handlers find_xattr_handlers = { ...@@ -338,41 +333,41 @@ static const struct xattr_iter_handlers find_xattr_handlers = {
.value = xattr_copyvalue .value = xattr_copyvalue
}; };
static int inline_getxattr(struct inode *inode, struct getxattr_iter *it) static int inline_getxattr(struct inode *inode, struct erofs_xattr_iter *it)
{ {
int ret; int ret;
unsigned int remaining; unsigned int remaining;
ret = inline_xattr_iter_begin(&it->it, inode); ret = inline_xattr_iter_begin(it, inode);
if (ret < 0) if (ret < 0)
return ret; return ret;
remaining = ret; remaining = ret;
while (remaining) { while (remaining) {
ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining); ret = xattr_foreach(it, &find_xattr_handlers, &remaining);
if (ret != -ENOATTR) if (ret != -ENOATTR)
break; break;
} }
return ret ? ret : it->buffer_size; return ret ? ret : it->buffer_size;
} }
static int shared_getxattr(struct inode *inode, struct getxattr_iter *it) static int shared_getxattr(struct inode *inode, struct erofs_xattr_iter *it)
{ {
struct erofs_inode *const vi = EROFS_I(inode); struct erofs_inode *const vi = EROFS_I(inode);
struct super_block *const sb = it->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 = -ENOATTR; int ret = -ENOATTR;
for (i = 0; i < vi->xattr_shared_count; ++i) { for (i = 0; i < vi->xattr_shared_count; ++i) {
it->it.pos = erofs_pos(sb, sbi->xattr_blkaddr) + it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
vi->xattr_shared_xattrs[i] * sizeof(__le32); vi->xattr_shared_xattrs[i] * sizeof(__le32);
it->it.kaddr = erofs_bread(&it->it.buf, it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
erofs_blknr(sb, it->it.pos), EROFS_KMAP); EROFS_KMAP);
if (IS_ERR(it->it.kaddr)) if (IS_ERR(it->kaddr))
return PTR_ERR(it->it.kaddr); return PTR_ERR(it->kaddr);
ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL); ret = xattr_foreach(it, &find_xattr_handlers, NULL);
if (ret != -ENOATTR) if (ret != -ENOATTR)
break; break;
} }
...@@ -394,7 +389,7 @@ int erofs_getxattr(struct inode *inode, int index, ...@@ -394,7 +389,7 @@ int erofs_getxattr(struct inode *inode, int index,
void *buffer, size_t buffer_size) void *buffer, size_t buffer_size)
{ {
int ret; int ret;
struct getxattr_iter it; struct erofs_xattr_iter it;
if (!name) if (!name)
return -EINVAL; return -EINVAL;
...@@ -404,22 +399,21 @@ int erofs_getxattr(struct inode *inode, int index, ...@@ -404,22 +399,21 @@ int erofs_getxattr(struct inode *inode, int index,
return ret; return ret;
it.index = index; it.index = index;
it.name.len = strlen(name); it.name = (struct qstr)QSTR_INIT(name, strlen(name));
if (it.name.len > EROFS_NAME_LEN) if (it.name.len > EROFS_NAME_LEN)
return -ERANGE; return -ERANGE;
it.it.sb = inode->i_sb; it.sb = inode->i_sb;
it.it.buf = __EROFS_BUF_INITIALIZER; it.buf = __EROFS_BUF_INITIALIZER;
erofs_init_metabuf(&it.it.buf, it.it.sb); erofs_init_metabuf(&it.buf, it.sb);
it.name.name = name;
it.buffer = buffer; it.buffer = buffer;
it.buffer_size = buffer_size; it.buffer_size = buffer_size;
it.buffer_ofs = 0;
ret = inline_getxattr(inode, &it); ret = inline_getxattr(inode, &it);
if (ret == -ENOATTR) if (ret == -ENOATTR)
ret = shared_getxattr(inode, &it); ret = shared_getxattr(inode, &it);
erofs_put_metabuf(&it.it.buf); erofs_put_metabuf(&it.buf);
return ret; return ret;
} }
...@@ -465,25 +459,15 @@ const struct xattr_handler *erofs_xattr_handlers[] = { ...@@ -465,25 +459,15 @@ const struct xattr_handler *erofs_xattr_handlers[] = {
NULL, NULL,
}; };
struct listxattr_iter { static int xattr_entrylist(struct erofs_xattr_iter *it,
struct xattr_iter it;
struct dentry *dentry;
char *buffer;
int buffer_size, buffer_ofs;
};
static int xattr_entrylist(struct xattr_iter *_it,
struct erofs_xattr_entry *entry) struct erofs_xattr_entry *entry)
{ {
struct listxattr_iter *it =
container_of(_it, struct listxattr_iter, it);
unsigned int base_index = entry->e_name_index; unsigned int base_index = entry->e_name_index;
unsigned int prefix_len, infix_len = 0; unsigned int prefix_len, infix_len = 0;
const char *prefix, *infix = NULL; const char *prefix, *infix = NULL;
if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX) { if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX) {
struct erofs_sb_info *sbi = EROFS_SB(_it->sb); struct erofs_sb_info *sbi = EROFS_SB(it->sb);
struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes + struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
(entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK); (entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
...@@ -515,23 +499,17 @@ static int xattr_entrylist(struct xattr_iter *_it, ...@@ -515,23 +499,17 @@ static int xattr_entrylist(struct xattr_iter *_it,
return 0; return 0;
} }
static int xattr_namelist(struct xattr_iter *_it, static int xattr_namelist(struct erofs_xattr_iter *it,
unsigned int processed, char *buf, unsigned int len) unsigned int processed, char *buf, unsigned int len)
{ {
struct listxattr_iter *it =
container_of(_it, struct listxattr_iter, it);
memcpy(it->buffer + it->buffer_ofs, buf, len); memcpy(it->buffer + it->buffer_ofs, buf, len);
it->buffer_ofs += len; it->buffer_ofs += len;
return 0; return 0;
} }
static int xattr_skipvalue(struct xattr_iter *_it, static int xattr_skipvalue(struct erofs_xattr_iter *it,
unsigned int value_sz) unsigned int value_sz)
{ {
struct listxattr_iter *it =
container_of(_it, struct listxattr_iter, it);
it->buffer[it->buffer_ofs++] = '\0'; it->buffer[it->buffer_ofs++] = '\0';
return 1; return 1;
} }
...@@ -543,42 +521,42 @@ static const struct xattr_iter_handlers list_xattr_handlers = { ...@@ -543,42 +521,42 @@ static const struct xattr_iter_handlers list_xattr_handlers = {
.value = NULL .value = NULL
}; };
static int inline_listxattr(struct listxattr_iter *it) static int inline_listxattr(struct erofs_xattr_iter *it)
{ {
int ret; int ret;
unsigned int remaining; unsigned int remaining;
ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry)); ret = inline_xattr_iter_begin(it, d_inode(it->dentry));
if (ret < 0) if (ret < 0)
return ret; return ret;
remaining = ret; remaining = ret;
while (remaining) { while (remaining) {
ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining); ret = xattr_foreach(it, &list_xattr_handlers, &remaining);
if (ret) if (ret)
break; break;
} }
return ret ? ret : it->buffer_ofs; return ret ? ret : it->buffer_ofs;
} }
static int shared_listxattr(struct listxattr_iter *it) static int shared_listxattr(struct erofs_xattr_iter *it)
{ {
struct inode *const inode = d_inode(it->dentry); 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->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; int ret = 0;
for (i = 0; i < vi->xattr_shared_count; ++i) { for (i = 0; i < vi->xattr_shared_count; ++i) {
it->it.pos = erofs_pos(sb, sbi->xattr_blkaddr) + it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
vi->xattr_shared_xattrs[i] * sizeof(__le32); vi->xattr_shared_xattrs[i] * sizeof(__le32);
it->it.kaddr = erofs_bread(&it->it.buf, it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
erofs_blknr(sb, it->it.pos), EROFS_KMAP); EROFS_KMAP);
if (IS_ERR(it->it.kaddr)) if (IS_ERR(it->kaddr))
return PTR_ERR(it->it.kaddr); return PTR_ERR(it->kaddr);
ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL); ret = xattr_foreach(it, &list_xattr_handlers, NULL);
if (ret) if (ret)
break; break;
} }
...@@ -589,7 +567,7 @@ ssize_t erofs_listxattr(struct dentry *dentry, ...@@ -589,7 +567,7 @@ ssize_t erofs_listxattr(struct dentry *dentry,
char *buffer, size_t buffer_size) char *buffer, size_t buffer_size)
{ {
int ret; int ret;
struct listxattr_iter it; struct erofs_xattr_iter it;
ret = erofs_init_inode_xattrs(d_inode(dentry)); ret = erofs_init_inode_xattrs(d_inode(dentry));
if (ret == -ENOATTR) if (ret == -ENOATTR)
...@@ -597,9 +575,9 @@ ssize_t erofs_listxattr(struct dentry *dentry, ...@@ -597,9 +575,9 @@ ssize_t erofs_listxattr(struct dentry *dentry,
if (ret) if (ret)
return ret; return ret;
it.it.sb = dentry->d_sb; it.sb = dentry->d_sb;
it.it.buf = __EROFS_BUF_INITIALIZER; it.buf = __EROFS_BUF_INITIALIZER;
erofs_init_metabuf(&it.it.buf, it.it.sb); erofs_init_metabuf(&it.buf, it.sb);
it.dentry = dentry; it.dentry = dentry;
it.buffer = buffer; it.buffer = buffer;
it.buffer_size = buffer_size; it.buffer_size = buffer_size;
...@@ -608,7 +586,7 @@ ssize_t erofs_listxattr(struct dentry *dentry, ...@@ -608,7 +586,7 @@ ssize_t erofs_listxattr(struct dentry *dentry,
ret = inline_listxattr(&it); ret = inline_listxattr(&it);
if (ret >= 0 || ret == -ENOATTR) if (ret >= 0 || ret == -ENOATTR)
ret = shared_listxattr(&it); ret = shared_listxattr(&it);
erofs_put_metabuf(&it.it.buf); erofs_put_metabuf(&it.buf);
return ret; return ret;
} }
......
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