]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/erofs/xattr.c
fs: simplify ->listxattr() implementation
[thirdparty/linux.git] / fs / erofs / xattr.c
CommitLineData
29b24f6c 1// SPDX-License-Identifier: GPL-2.0-only
b17500a0 2/*
b17500a0 3 * Copyright (C) 2017-2018 HUAWEI, Inc.
592e7cd0 4 * https://www.huawei.com/
bb88e8da 5 * Copyright (C) 2021-2022, Alibaba Cloud
b17500a0
GX
6 */
7#include <linux/security.h>
8#include "xattr.h"
9
10struct xattr_iter {
11 struct super_block *sb;
bb88e8da 12 struct erofs_buf buf;
b17500a0
GX
13 void *kaddr;
14
15 erofs_blk_t blkaddr;
7dd68b14 16 unsigned int ofs;
b17500a0
GX
17};
18
cadf1ccf 19static int init_inode_xattrs(struct inode *inode)
b17500a0 20{
a5876e24 21 struct erofs_inode *const vi = EROFS_I(inode);
b17500a0 22 struct xattr_iter it;
7dd68b14 23 unsigned int i;
b17500a0 24 struct erofs_xattr_ibody_header *ih;
b780d3fc 25 struct super_block *sb = inode->i_sb;
62dc4597 26 int ret = 0;
b17500a0 27
62dc4597 28 /* the most case is that xattrs of this inode are initialized. */
ce063129
GX
29 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
30 /*
31 * paired with smp_mb() at the end of the function to ensure
32 * fields will only be observed after the bit is set.
33 */
34 smp_mb();
cadf1ccf 35 return 0;
ce063129 36 }
b17500a0 37
a5876e24 38 if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
62dc4597
GX
39 return -ERESTARTSYS;
40
41 /* someone has initialized xattrs for us? */
a5876e24 42 if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
62dc4597 43 goto out_unlock;
7077fffc
GX
44
45 /*
46 * bypass all xattr operations if ->xattr_isize is not greater than
47 * sizeof(struct erofs_xattr_ibody_header), in detail:
48 * 1) it is not enough to contain erofs_xattr_ibody_header then
49 * ->xattr_isize should be 0 (it means no xattr);
50 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
51 * undefined right now (maybe use later with some new sb feature).
52 */
53 if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
b780d3fc 54 erofs_err(sb,
4f761fa2
GX
55 "xattr_isize %d of nid %llu is not supported yet",
56 vi->xattr_isize, vi->nid);
ff784a78 57 ret = -EOPNOTSUPP;
62dc4597 58 goto out_unlock;
7077fffc 59 } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
8d8a09b0 60 if (vi->xattr_isize) {
b780d3fc 61 erofs_err(sb, "bogus xattr ibody @ nid %llu", vi->nid);
7077fffc 62 DBG_BUGON(1);
a6b9b1d5 63 ret = -EFSCORRUPTED;
62dc4597 64 goto out_unlock; /* xattr ondisk layout error */
7077fffc 65 }
62dc4597
GX
66 ret = -ENOATTR;
67 goto out_unlock;
7077fffc 68 }
b17500a0 69
bb88e8da 70 it.buf = __EROFS_BUF_INITIALIZER;
b780d3fc
GX
71 it.blkaddr = erofs_blknr(erofs_iloc(inode) + vi->inode_isize);
72 it.ofs = erofs_blkoff(erofs_iloc(inode) + vi->inode_isize);
b17500a0 73
bb88e8da
GX
74 /* read in shared xattr array (non-atomic, see kmalloc below) */
75 it.kaddr = erofs_read_metabuf(&it.buf, sb, it.blkaddr, EROFS_KMAP);
76 if (IS_ERR(it.kaddr)) {
77 ret = PTR_ERR(it.kaddr);
62dc4597
GX
78 goto out_unlock;
79 }
b17500a0 80
b17500a0 81 ih = (struct erofs_xattr_ibody_header *)(it.kaddr + it.ofs);
b17500a0 82 vi->xattr_shared_count = ih->h_shared_count;
cadf1ccf
GX
83 vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
84 sizeof(uint), GFP_KERNEL);
561fb35a 85 if (!vi->xattr_shared_xattrs) {
bb88e8da 86 erofs_put_metabuf(&it.buf);
62dc4597
GX
87 ret = -ENOMEM;
88 goto out_unlock;
cadf1ccf 89 }
b17500a0
GX
90
91 /* let's skip ibody header */
92 it.ofs += sizeof(struct erofs_xattr_ibody_header);
93
94 for (i = 0; i < vi->xattr_shared_count; ++i) {
8d8a09b0 95 if (it.ofs >= EROFS_BLKSIZ) {
b17500a0 96 /* cannot be unaligned */
9ddc7004 97 DBG_BUGON(it.ofs != EROFS_BLKSIZ);
b17500a0 98
bb88e8da
GX
99 it.kaddr = erofs_read_metabuf(&it.buf, sb, ++it.blkaddr,
100 EROFS_KMAP);
101 if (IS_ERR(it.kaddr)) {
3b1b5291
SY
102 kfree(vi->xattr_shared_xattrs);
103 vi->xattr_shared_xattrs = NULL;
bb88e8da 104 ret = PTR_ERR(it.kaddr);
62dc4597 105 goto out_unlock;
3b1b5291 106 }
b17500a0
GX
107 it.ofs = 0;
108 }
109 vi->xattr_shared_xattrs[i] =
110 le32_to_cpu(*(__le32 *)(it.kaddr + it.ofs));
111 it.ofs += sizeof(__le32);
112 }
bb88e8da 113 erofs_put_metabuf(&it.buf);
b17500a0 114
ce063129
GX
115 /* paired with smp_mb() at the beginning of the function. */
116 smp_mb();
a5876e24 117 set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
62dc4597
GX
118
119out_unlock:
a5876e24 120 clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
62dc4597 121 return ret;
b17500a0
GX
122}
123
bdf30cef
GX
124/*
125 * the general idea for these return values is
126 * if 0 is returned, go on processing the current xattr;
127 * 1 (> 0) is returned, skip this round to process the next xattr;
128 * -err (< 0) is returned, an error (maybe ENOXATTR) occurred
129 * and need to be handled
130 */
b17500a0 131struct xattr_iter_handlers {
4b03f3f4
SY
132 int (*entry)(struct xattr_iter *_it, struct erofs_xattr_entry *entry);
133 int (*name)(struct xattr_iter *_it, unsigned int processed, char *buf,
134 unsigned int len);
135 int (*alloc_buffer)(struct xattr_iter *_it, unsigned int value_sz);
136 void (*value)(struct xattr_iter *_it, unsigned int processed, char *buf,
137 unsigned int len);
b17500a0
GX
138};
139
cadf1ccf 140static inline int xattr_iter_fixup(struct xattr_iter *it)
b17500a0 141{
cadf1ccf
GX
142 if (it->ofs < EROFS_BLKSIZ)
143 return 0;
144
cadf1ccf 145 it->blkaddr += erofs_blknr(it->ofs);
bb88e8da 146 it->kaddr = erofs_read_metabuf(&it->buf, it->sb, it->blkaddr,
927e5010 147 EROFS_KMAP);
bb88e8da
GX
148 if (IS_ERR(it->kaddr))
149 return PTR_ERR(it->kaddr);
cadf1ccf
GX
150 it->ofs = erofs_blkoff(it->ofs);
151 return 0;
b17500a0
GX
152}
153
154static int inline_xattr_iter_begin(struct xattr_iter *it,
447a3621 155 struct inode *inode)
b17500a0 156{
a5876e24 157 struct erofs_inode *const vi = EROFS_I(inode);
7dd68b14 158 unsigned int xattr_header_sz, inline_xattr_ofs;
b17500a0
GX
159
160 xattr_header_sz = inlinexattr_header_size(inode);
8d8a09b0 161 if (xattr_header_sz >= vi->xattr_isize) {
9ddc7004 162 DBG_BUGON(xattr_header_sz > vi->xattr_isize);
b17500a0
GX
163 return -ENOATTR;
164 }
165
166 inline_xattr_ofs = vi->inode_isize + xattr_header_sz;
167
b780d3fc
GX
168 it->blkaddr = erofs_blknr(erofs_iloc(inode) + inline_xattr_ofs);
169 it->ofs = erofs_blkoff(erofs_iloc(inode) + inline_xattr_ofs);
bb88e8da 170 it->kaddr = erofs_read_metabuf(&it->buf, inode->i_sb, it->blkaddr,
927e5010 171 EROFS_KMAP);
bb88e8da
GX
172 if (IS_ERR(it->kaddr))
173 return PTR_ERR(it->kaddr);
b17500a0
GX
174 return vi->xattr_isize - xattr_header_sz;
175}
176
bdf30cef
GX
177/*
178 * Regardless of success or failure, `xattr_foreach' will end up with
179 * `ofs' pointing to the next xattr item rather than an arbitrary position.
180 */
b17500a0 181static int xattr_foreach(struct xattr_iter *it,
447a3621
JM
182 const struct xattr_iter_handlers *op,
183 unsigned int *tlimit)
b17500a0
GX
184{
185 struct erofs_xattr_entry entry;
7dd68b14 186 unsigned int value_sz, processed, slice;
b17500a0
GX
187 int err;
188
189 /* 0. fixup blkaddr, ofs, ipage */
cadf1ccf
GX
190 err = xattr_iter_fixup(it);
191 if (err)
192 return err;
b17500a0
GX
193
194 /*
195 * 1. read xattr entry to the memory,
196 * since we do EROFS_XATTR_ALIGN
197 * therefore entry should be in the page
198 */
199 entry = *(struct erofs_xattr_entry *)(it->kaddr + it->ofs);
561fb35a 200 if (tlimit) {
b6796abd 201 unsigned int entry_sz = erofs_xattr_entry_size(&entry);
b17500a0 202
9ddc7004 203 /* xattr on-disk corruption: xattr entry beyond xattr_isize */
8d8a09b0 204 if (*tlimit < entry_sz) {
9ddc7004 205 DBG_BUGON(1);
a6b9b1d5 206 return -EFSCORRUPTED;
9ddc7004 207 }
b17500a0
GX
208 *tlimit -= entry_sz;
209 }
210
211 it->ofs += sizeof(struct erofs_xattr_entry);
212 value_sz = le16_to_cpu(entry.e_value_size);
213
214 /* handle entry */
215 err = op->entry(it, &entry);
216 if (err) {
217 it->ofs += entry.e_name_len + value_sz;
218 goto out;
219 }
220
221 /* 2. handle xattr name (ofs will finally be at the end of name) */
222 processed = 0;
223
224 while (processed < entry.e_name_len) {
225 if (it->ofs >= EROFS_BLKSIZ) {
9ddc7004 226 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
b17500a0 227
cadf1ccf
GX
228 err = xattr_iter_fixup(it);
229 if (err)
230 goto out;
b17500a0
GX
231 it->ofs = 0;
232 }
233
bb88e8da 234 slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs,
7dd68b14 235 entry.e_name_len - processed);
b17500a0
GX
236
237 /* handle name */
238 err = op->name(it, processed, it->kaddr + it->ofs, slice);
239 if (err) {
240 it->ofs += entry.e_name_len - processed + value_sz;
241 goto out;
242 }
243
244 it->ofs += slice;
245 processed += slice;
246 }
247
248 /* 3. handle xattr value */
249 processed = 0;
250
561fb35a 251 if (op->alloc_buffer) {
b17500a0
GX
252 err = op->alloc_buffer(it, value_sz);
253 if (err) {
254 it->ofs += value_sz;
255 goto out;
256 }
257 }
258
259 while (processed < value_sz) {
260 if (it->ofs >= EROFS_BLKSIZ) {
9ddc7004 261 DBG_BUGON(it->ofs > EROFS_BLKSIZ);
cadf1ccf
GX
262
263 err = xattr_iter_fixup(it);
264 if (err)
265 goto out;
b17500a0
GX
266 it->ofs = 0;
267 }
268
bb88e8da 269 slice = min_t(unsigned int, EROFS_BLKSIZ - it->ofs,
7dd68b14 270 value_sz - processed);
b17500a0
GX
271 op->value(it, processed, it->kaddr + it->ofs, slice);
272 it->ofs += slice;
273 processed += slice;
274 }
275
276out:
bdf30cef 277 /* xattrs should be 4-byte aligned (on-disk constraint) */
b17500a0 278 it->ofs = EROFS_XATTR_ALIGN(it->ofs);
6614f765 279 return err < 0 ? err : 0;
b17500a0
GX
280}
281
282struct getxattr_iter {
283 struct xattr_iter it;
284
285 char *buffer;
286 int buffer_size, index;
287 struct qstr name;
288};
289
290static int xattr_entrymatch(struct xattr_iter *_it,
447a3621 291 struct erofs_xattr_entry *entry)
b17500a0
GX
292{
293 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
294
295 return (it->index != entry->e_name_index ||
296 it->name.len != entry->e_name_len) ? -ENOATTR : 0;
297}
298
299static int xattr_namematch(struct xattr_iter *_it,
447a3621 300 unsigned int processed, char *buf, unsigned int len)
b17500a0
GX
301{
302 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
303
304 return memcmp(buf, it->name.name + processed, len) ? -ENOATTR : 0;
305}
306
307static int xattr_checkbuffer(struct xattr_iter *_it,
447a3621 308 unsigned int value_sz)
b17500a0
GX
309{
310 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
311 int err = it->buffer_size < value_sz ? -ERANGE : 0;
312
313 it->buffer_size = value_sz;
561fb35a 314 return !it->buffer ? 1 : err;
b17500a0
GX
315}
316
317static void xattr_copyvalue(struct xattr_iter *_it,
447a3621
JM
318 unsigned int processed,
319 char *buf, unsigned int len)
b17500a0
GX
320{
321 struct getxattr_iter *it = container_of(_it, struct getxattr_iter, it);
322
323 memcpy(it->buffer + processed, buf, len);
324}
325
cadf1ccf 326static const struct xattr_iter_handlers find_xattr_handlers = {
b17500a0
GX
327 .entry = xattr_entrymatch,
328 .name = xattr_namematch,
329 .alloc_buffer = xattr_checkbuffer,
330 .value = xattr_copyvalue
331};
332
333static int inline_getxattr(struct inode *inode, struct getxattr_iter *it)
334{
335 int ret;
7dd68b14 336 unsigned int remaining;
b17500a0
GX
337
338 ret = inline_xattr_iter_begin(&it->it, inode);
339 if (ret < 0)
340 return ret;
341
342 remaining = ret;
343 while (remaining) {
2bc75964 344 ret = xattr_foreach(&it->it, &find_xattr_handlers, &remaining);
6614f765 345 if (ret != -ENOATTR)
cadf1ccf 346 break;
b17500a0 347 }
6614f765 348 return ret ? ret : it->buffer_size;
b17500a0
GX
349}
350
351static int shared_getxattr(struct inode *inode, struct getxattr_iter *it)
352{
a5876e24 353 struct erofs_inode *const vi = EROFS_I(inode);
6e78901a
GX
354 struct super_block *const sb = inode->i_sb;
355 struct erofs_sb_info *const sbi = EROFS_SB(sb);
7dd68b14 356 unsigned int i;
b17500a0
GX
357 int ret = -ENOATTR;
358
359 for (i = 0; i < vi->xattr_shared_count; ++i) {
360 erofs_blk_t blkaddr =
361 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
362
363 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
bb88e8da 364 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
927e5010 365 EROFS_KMAP);
bb88e8da
GX
366 if (IS_ERR(it->it.kaddr))
367 return PTR_ERR(it->it.kaddr);
368 it->it.blkaddr = blkaddr;
b17500a0 369
2bc75964 370 ret = xattr_foreach(&it->it, &find_xattr_handlers, NULL);
6614f765 371 if (ret != -ENOATTR)
cadf1ccf 372 break;
b17500a0 373 }
6614f765 374 return ret ? ret : it->buffer_size;
b17500a0
GX
375}
376
377static bool erofs_xattr_user_list(struct dentry *dentry)
378{
e6242465 379 return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
b17500a0
GX
380}
381
382static bool erofs_xattr_trusted_list(struct dentry *dentry)
383{
384 return capable(CAP_SYS_ADMIN);
385}
386
387int erofs_getxattr(struct inode *inode, int index,
447a3621
JM
388 const char *name,
389 void *buffer, size_t buffer_size)
b17500a0
GX
390{
391 int ret;
392 struct getxattr_iter it;
393
8d8a09b0 394 if (!name)
b17500a0
GX
395 return -EINVAL;
396
cadf1ccf
GX
397 ret = init_inode_xattrs(inode);
398 if (ret)
399 return ret;
b17500a0
GX
400
401 it.index = index;
b17500a0
GX
402 it.name.len = strlen(name);
403 if (it.name.len > EROFS_NAME_LEN)
404 return -ERANGE;
bb88e8da
GX
405
406 it.it.buf = __EROFS_BUF_INITIALIZER;
b17500a0
GX
407 it.name.name = name;
408
409 it.buffer = buffer;
410 it.buffer_size = buffer_size;
411
412 it.it.sb = inode->i_sb;
413 ret = inline_getxattr(inode, &it);
414 if (ret == -ENOATTR)
415 ret = shared_getxattr(inode, &it);
bb88e8da 416 erofs_put_metabuf(&it.it.buf);
b17500a0
GX
417 return ret;
418}
419
420static int erofs_xattr_generic_get(const struct xattr_handler *handler,
447a3621
JM
421 struct dentry *unused, struct inode *inode,
422 const char *name, void *buffer, size_t size)
b17500a0 423{
b17500a0
GX
424 struct erofs_sb_info *const sbi = EROFS_I_SB(inode);
425
426 switch (handler->flags) {
427 case EROFS_XATTR_INDEX_USER:
e6242465 428 if (!test_opt(&sbi->opt, XATTR_USER))
b17500a0
GX
429 return -EOPNOTSUPP;
430 break;
431 case EROFS_XATTR_INDEX_TRUSTED:
b17500a0
GX
432 break;
433 case EROFS_XATTR_INDEX_SECURITY:
434 break;
435 default:
436 return -EINVAL;
437 }
438
b17500a0
GX
439 return erofs_getxattr(inode, handler->flags, name, buffer, size);
440}
441
442const struct xattr_handler erofs_xattr_user_handler = {
443 .prefix = XATTR_USER_PREFIX,
444 .flags = EROFS_XATTR_INDEX_USER,
445 .list = erofs_xattr_user_list,
446 .get = erofs_xattr_generic_get,
447};
448
449const struct xattr_handler erofs_xattr_trusted_handler = {
450 .prefix = XATTR_TRUSTED_PREFIX,
451 .flags = EROFS_XATTR_INDEX_TRUSTED,
452 .list = erofs_xattr_trusted_list,
453 .get = erofs_xattr_generic_get,
454};
455
456#ifdef CONFIG_EROFS_FS_SECURITY
457const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
458 .prefix = XATTR_SECURITY_PREFIX,
459 .flags = EROFS_XATTR_INDEX_SECURITY,
460 .get = erofs_xattr_generic_get,
461};
462#endif
463
b17500a0
GX
464const struct xattr_handler *erofs_xattr_handlers[] = {
465 &erofs_xattr_user_handler,
b17500a0
GX
466 &erofs_xattr_trusted_handler,
467#ifdef CONFIG_EROFS_FS_SECURITY
468 &erofs_xattr_security_handler,
469#endif
470 NULL,
471};
b17500a0
GX
472
473struct listxattr_iter {
474 struct xattr_iter it;
475
476 struct dentry *dentry;
477 char *buffer;
478 int buffer_size, buffer_ofs;
479};
480
481static int xattr_entrylist(struct xattr_iter *_it,
447a3621 482 struct erofs_xattr_entry *entry)
b17500a0
GX
483{
484 struct listxattr_iter *it =
485 container_of(_it, struct listxattr_iter, it);
7dd68b14 486 unsigned int prefix_len;
b17500a0
GX
487 const char *prefix;
488
a5488f29
CB
489 prefix = erofs_xattr_prefix(entry->e_name_index, it->dentry);
490 if (!prefix)
b17500a0 491 return 1;
b17500a0
GX
492 prefix_len = strlen(prefix);
493
561fb35a 494 if (!it->buffer) {
b17500a0
GX
495 it->buffer_ofs += prefix_len + entry->e_name_len + 1;
496 return 1;
497 }
498
499 if (it->buffer_ofs + prefix_len
500 + entry->e_name_len + 1 > it->buffer_size)
501 return -ERANGE;
502
503 memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
504 it->buffer_ofs += prefix_len;
505 return 0;
506}
507
508static int xattr_namelist(struct xattr_iter *_it,
447a3621 509 unsigned int processed, char *buf, unsigned int len)
b17500a0
GX
510{
511 struct listxattr_iter *it =
512 container_of(_it, struct listxattr_iter, it);
513
514 memcpy(it->buffer + it->buffer_ofs, buf, len);
515 it->buffer_ofs += len;
516 return 0;
517}
518
519static int xattr_skipvalue(struct xattr_iter *_it,
447a3621 520 unsigned int value_sz)
b17500a0
GX
521{
522 struct listxattr_iter *it =
523 container_of(_it, struct listxattr_iter, it);
524
525 it->buffer[it->buffer_ofs++] = '\0';
526 return 1;
527}
528
cadf1ccf 529static const struct xattr_iter_handlers list_xattr_handlers = {
b17500a0
GX
530 .entry = xattr_entrylist,
531 .name = xattr_namelist,
532 .alloc_buffer = xattr_skipvalue,
533 .value = NULL
534};
535
536static int inline_listxattr(struct listxattr_iter *it)
537{
538 int ret;
7dd68b14 539 unsigned int remaining;
b17500a0
GX
540
541 ret = inline_xattr_iter_begin(&it->it, d_inode(it->dentry));
542 if (ret < 0)
543 return ret;
544
545 remaining = ret;
546 while (remaining) {
2bc75964 547 ret = xattr_foreach(&it->it, &list_xattr_handlers, &remaining);
6614f765 548 if (ret)
b17500a0
GX
549 break;
550 }
6614f765 551 return ret ? ret : it->buffer_ofs;
b17500a0
GX
552}
553
554static int shared_listxattr(struct listxattr_iter *it)
555{
556 struct inode *const inode = d_inode(it->dentry);
a5876e24 557 struct erofs_inode *const vi = EROFS_I(inode);
6e78901a
GX
558 struct super_block *const sb = inode->i_sb;
559 struct erofs_sb_info *const sbi = EROFS_SB(sb);
7dd68b14 560 unsigned int i;
b17500a0
GX
561 int ret = 0;
562
563 for (i = 0; i < vi->xattr_shared_count; ++i) {
564 erofs_blk_t blkaddr =
565 xattrblock_addr(sbi, vi->xattr_shared_xattrs[i]);
566
567 it->it.ofs = xattrblock_offset(sbi, vi->xattr_shared_xattrs[i]);
bb88e8da 568 it->it.kaddr = erofs_read_metabuf(&it->it.buf, sb, blkaddr,
927e5010 569 EROFS_KMAP);
bb88e8da
GX
570 if (IS_ERR(it->it.kaddr))
571 return PTR_ERR(it->it.kaddr);
572 it->it.blkaddr = blkaddr;
b17500a0 573
2bc75964 574 ret = xattr_foreach(&it->it, &list_xattr_handlers, NULL);
6614f765 575 if (ret)
b17500a0
GX
576 break;
577 }
6614f765 578 return ret ? ret : it->buffer_ofs;
b17500a0
GX
579}
580
581ssize_t erofs_listxattr(struct dentry *dentry,
447a3621 582 char *buffer, size_t buffer_size)
b17500a0
GX
583{
584 int ret;
585 struct listxattr_iter it;
586
cadf1ccf 587 ret = init_inode_xattrs(d_inode(dentry));
926d1650
GX
588 if (ret == -ENOATTR)
589 return 0;
cadf1ccf
GX
590 if (ret)
591 return ret;
b17500a0 592
bb88e8da 593 it.it.buf = __EROFS_BUF_INITIALIZER;
b17500a0
GX
594 it.dentry = dentry;
595 it.buffer = buffer;
596 it.buffer_size = buffer_size;
597 it.buffer_ofs = 0;
598
599 it.it.sb = dentry->d_sb;
600
601 ret = inline_listxattr(&it);
bb88e8da
GX
602 if (ret >= 0 || ret == -ENOATTR)
603 ret = shared_listxattr(&it);
604 erofs_put_metabuf(&it.it.buf);
605 return ret;
b17500a0
GX
606}
607
516c115c 608#ifdef CONFIG_EROFS_FS_POSIX_ACL
0cad6246 609struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
516c115c
GX
610{
611 struct posix_acl *acl;
612 int prefix, rc;
613 char *value = NULL;
614
0cad6246
MS
615 if (rcu)
616 return ERR_PTR(-ECHILD);
617
516c115c
GX
618 switch (type) {
619 case ACL_TYPE_ACCESS:
620 prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
621 break;
622 case ACL_TYPE_DEFAULT:
623 prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
624 break;
625 default:
626 return ERR_PTR(-EINVAL);
627 }
628
629 rc = erofs_getxattr(inode, prefix, "", NULL, 0);
630 if (rc > 0) {
631 value = kmalloc(rc, GFP_KERNEL);
632 if (!value)
633 return ERR_PTR(-ENOMEM);
634 rc = erofs_getxattr(inode, prefix, "", value, rc);
635 }
636
637 if (rc == -ENOATTR)
638 acl = NULL;
639 else if (rc < 0)
640 acl = ERR_PTR(rc);
641 else
642 acl = posix_acl_from_xattr(&init_user_ns, value, rc);
643 kfree(value);
644 return acl;
645}
646#endif