Skip to content

Commit a5488f2

Browse files
committed
fs: simplify ->listxattr() implementation
The ext{2,4}, erofs, f2fs, and jffs2 filesystems use the same logic to check whether a given xattr can be listed. Simplify them and avoid open-coding the same check by calling the helper we introduced earlier. Reviewed-by: Christoph Hellwig <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Signed-off-by: Christian Brauner (Microsoft) <[email protected]>
1 parent 0c95c02 commit a5488f2

File tree

6 files changed

+55
-38
lines changed

6 files changed

+55
-38
lines changed

fs/erofs/xattr.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,9 @@ static int xattr_entrylist(struct xattr_iter *_it,
486486
unsigned int prefix_len;
487487
const char *prefix;
488488

489-
const struct xattr_handler *h =
490-
erofs_xattr_handler(entry->e_name_index);
491-
492-
if (!h || (h->list && !h->list(it->dentry)))
489+
prefix = erofs_xattr_prefix(entry->e_name_index, it->dentry);
490+
if (!prefix)
493491
return 1;
494-
495-
prefix = xattr_prefix(h);
496492
prefix_len = strlen(prefix);
497493

498494
if (!it->buffer) {

fs/erofs/xattr.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ extern const struct xattr_handler erofs_xattr_user_handler;
4141
extern const struct xattr_handler erofs_xattr_trusted_handler;
4242
extern const struct xattr_handler erofs_xattr_security_handler;
4343

44-
static inline const struct xattr_handler *erofs_xattr_handler(unsigned int idx)
44+
static inline const char *erofs_xattr_prefix(unsigned int idx,
45+
struct dentry *dentry)
4546
{
47+
const struct xattr_handler *handler = NULL;
48+
4649
static const struct xattr_handler *xattr_handler_map[] = {
4750
[EROFS_XATTR_INDEX_USER] = &erofs_xattr_user_handler,
4851
#ifdef CONFIG_EROFS_FS_POSIX_ACL
@@ -57,8 +60,13 @@ static inline const struct xattr_handler *erofs_xattr_handler(unsigned int idx)
5760
#endif
5861
};
5962

60-
return idx && idx < ARRAY_SIZE(xattr_handler_map) ?
61-
xattr_handler_map[idx] : NULL;
63+
if (idx && idx < ARRAY_SIZE(xattr_handler_map))
64+
handler = xattr_handler_map[idx];
65+
66+
if (!xattr_handler_can_list(handler, dentry))
67+
return NULL;
68+
69+
return xattr_prefix(handler);
6270
}
6371

6472
extern const struct xattr_handler *erofs_xattr_handlers[];

fs/ext2/xattr.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,18 @@ const struct xattr_handler *ext2_xattr_handlers[] = {
121121

122122
#define EA_BLOCK_CACHE(inode) (EXT2_SB(inode->i_sb)->s_ea_block_cache)
123123

124-
static inline const struct xattr_handler *
125-
ext2_xattr_handler(int name_index)
124+
static inline const char *ext2_xattr_prefix(int name_index,
125+
struct dentry *dentry)
126126
{
127127
const struct xattr_handler *handler = NULL;
128128

129129
if (name_index > 0 && name_index < ARRAY_SIZE(ext2_xattr_handler_map))
130130
handler = ext2_xattr_handler_map[name_index];
131-
return handler;
131+
132+
if (!xattr_handler_can_list(handler, dentry))
133+
return NULL;
134+
135+
return xattr_prefix(handler);
132136
}
133137

134138
static bool
@@ -329,11 +333,10 @@ ext2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
329333
/* list the attribute names */
330334
for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
331335
entry = EXT2_XATTR_NEXT(entry)) {
332-
const struct xattr_handler *handler =
333-
ext2_xattr_handler(entry->e_name_index);
336+
const char *prefix;
334337

335-
if (handler && (!handler->list || handler->list(dentry))) {
336-
const char *prefix = handler->prefix ?: handler->name;
338+
prefix = ext2_xattr_prefix(entry->e_name_index, dentry);
339+
if (prefix) {
337340
size_t prefix_len = strlen(prefix);
338341
size_t size = prefix_len + entry->e_name_len + 1;
339342

fs/ext4/xattr.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,18 @@ static void ext4_xattr_block_csum_set(struct inode *inode,
169169
bh->b_blocknr, BHDR(bh));
170170
}
171171

172-
static inline const struct xattr_handler *
173-
ext4_xattr_handler(int name_index)
172+
static inline const char *ext4_xattr_prefix(int name_index,
173+
struct dentry *dentry)
174174
{
175175
const struct xattr_handler *handler = NULL;
176176

177177
if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
178178
handler = ext4_xattr_handler_map[name_index];
179-
return handler;
179+
180+
if (!xattr_handler_can_list(handler, dentry))
181+
return NULL;
182+
183+
return xattr_prefix(handler);
180184
}
181185

182186
static int
@@ -736,11 +740,10 @@ ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
736740
size_t rest = buffer_size;
737741

738742
for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
739-
const struct xattr_handler *handler =
740-
ext4_xattr_handler(entry->e_name_index);
743+
const char *prefix;
741744

742-
if (handler && (!handler->list || handler->list(dentry))) {
743-
const char *prefix = handler->prefix ?: handler->name;
745+
prefix = ext4_xattr_prefix(entry->e_name_index, dentry);
746+
if (prefix) {
744747
size_t prefix_len = strlen(prefix);
745748
size_t size = prefix_len + entry->e_name_len + 1;
746749

fs/f2fs/xattr.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,18 @@ const struct xattr_handler *f2fs_xattr_handlers[] = {
212212
NULL,
213213
};
214214

215-
static inline const struct xattr_handler *f2fs_xattr_handler(int index)
215+
static inline const char *f2fs_xattr_prefix(int index,
216+
struct dentry *dentry)
216217
{
217218
const struct xattr_handler *handler = NULL;
218219

219220
if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
220221
handler = f2fs_xattr_handler_map[index];
221-
return handler;
222+
223+
if (!xattr_handler_can_list(handler, dentry))
224+
return NULL;
225+
226+
return xattr_prefix(handler);
222227
}
223228

224229
static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
@@ -569,12 +574,12 @@ ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
569574
last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
570575

571576
list_for_each_xattr(entry, base_addr) {
572-
const struct xattr_handler *handler =
573-
f2fs_xattr_handler(entry->e_name_index);
574577
const char *prefix;
575578
size_t prefix_len;
576579
size_t size;
577580

581+
prefix = f2fs_xattr_prefix(entry->e_name_index, dentry);
582+
578583
if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
579584
(void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
580585
f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
@@ -586,10 +591,9 @@ ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
586591
goto cleanup;
587592
}
588593

589-
if (!handler || (handler->list && !handler->list(dentry)))
594+
if (!prefix)
590595
continue;
591596

592-
prefix = xattr_prefix(handler);
593597
prefix_len = strlen(prefix);
594598
size = prefix_len + entry->e_name_len + 1;
595599
if (buffer) {

fs/jffs2/xattr.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,9 @@ const struct xattr_handler *jffs2_xattr_handlers[] = {
924924
NULL
925925
};
926926

927-
static const struct xattr_handler *xprefix_to_handler(int xprefix) {
928-
const struct xattr_handler *ret;
927+
static const char *jffs2_xattr_prefix(int xprefix, struct dentry *dentry)
928+
{
929+
const struct xattr_handler *ret = NULL;
929930

930931
switch (xprefix) {
931932
case JFFS2_XPREFIX_USER:
@@ -948,10 +949,13 @@ static const struct xattr_handler *xprefix_to_handler(int xprefix) {
948949
ret = &jffs2_trusted_xattr_handler;
949950
break;
950951
default:
951-
ret = NULL;
952-
break;
952+
return NULL;
953953
}
954-
return ret;
954+
955+
if (!xattr_handler_can_list(ret, dentry))
956+
return NULL;
957+
958+
return xattr_prefix(ret);
955959
}
956960

957961
ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
@@ -962,7 +966,6 @@ ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
962966
struct jffs2_inode_cache *ic = f->inocache;
963967
struct jffs2_xattr_ref *ref, **pref;
964968
struct jffs2_xattr_datum *xd;
965-
const struct xattr_handler *xhandle;
966969
const char *prefix;
967970
ssize_t prefix_len, len, rc;
968971
int retry = 0;
@@ -994,10 +997,10 @@ ssize_t jffs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
994997
goto out;
995998
}
996999
}
997-
xhandle = xprefix_to_handler(xd->xprefix);
998-
if (!xhandle || (xhandle->list && !xhandle->list(dentry)))
1000+
1001+
prefix = jffs2_xattr_prefix(xd->xprefix, dentry);
1002+
if (!prefix)
9991003
continue;
1000-
prefix = xhandle->prefix ?: xhandle->name;
10011004
prefix_len = strlen(prefix);
10021005
rc = prefix_len + xd->name_len + 1;
10031006

0 commit comments

Comments
 (0)