147e4937aSGao Xiang // SPDX-License-Identifier: GPL-2.0-only
247e4937aSGao Xiang /*
347e4937aSGao Xiang * Copyright (C) 2017-2018 HUAWEI, Inc.
4592e7cd0SAlexander A. Klimov * https://www.huawei.com/
5bb88e8daSGao Xiang * Copyright (C) 2021-2022, Alibaba Cloud
647e4937aSGao Xiang */
747e4937aSGao Xiang #include <linux/security.h>
8*fd73a439SJingbo Xu #include <linux/xxhash.h>
947e4937aSGao Xiang #include "xattr.h"
1047e4937aSGao Xiang
118e823961SJingbo Xu struct erofs_xattr_iter {
1247e4937aSGao Xiang struct super_block *sb;
13bb88e8daSGao Xiang struct erofs_buf buf;
14eba67eb6SJingbo Xu erofs_off_t pos;
1547e4937aSGao Xiang void *kaddr;
168e823961SJingbo Xu
178e823961SJingbo Xu char *buffer;
188e823961SJingbo Xu int buffer_size, buffer_ofs;
198e823961SJingbo Xu
208e823961SJingbo Xu /* getxattr */
218e823961SJingbo Xu int index, infix_len;
228e823961SJingbo Xu struct qstr name;
238e823961SJingbo Xu
248e823961SJingbo Xu /* listxattr */
258e823961SJingbo Xu struct dentry *dentry;
2647e4937aSGao Xiang };
2747e4937aSGao Xiang
erofs_init_inode_xattrs(struct inode * inode)286020ffe7SJingbo Xu static int erofs_init_inode_xattrs(struct inode *inode)
2947e4937aSGao Xiang {
30a5876e24SGao Xiang struct erofs_inode *const vi = EROFS_I(inode);
318e823961SJingbo Xu struct erofs_xattr_iter it;
3247e4937aSGao Xiang unsigned int i;
3347e4937aSGao Xiang struct erofs_xattr_ibody_header *ih;
34b780d3fcSGao Xiang struct super_block *sb = inode->i_sb;
3547e4937aSGao Xiang int ret = 0;
3647e4937aSGao Xiang
3747e4937aSGao Xiang /* the most case is that xattrs of this inode are initialized. */
38ce063129SGao Xiang if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
39ce063129SGao Xiang /*
40ce063129SGao Xiang * paired with smp_mb() at the end of the function to ensure
41ce063129SGao Xiang * fields will only be observed after the bit is set.
42ce063129SGao Xiang */
43ce063129SGao Xiang smp_mb();
4447e4937aSGao Xiang return 0;
45ce063129SGao Xiang }
4647e4937aSGao Xiang
47a5876e24SGao Xiang if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
4847e4937aSGao Xiang return -ERESTARTSYS;
4947e4937aSGao Xiang
5047e4937aSGao Xiang /* someone has initialized xattrs for us? */
51a5876e24SGao Xiang if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
5247e4937aSGao Xiang goto out_unlock;
5347e4937aSGao Xiang
5447e4937aSGao Xiang /*
5547e4937aSGao Xiang * bypass all xattr operations if ->xattr_isize is not greater than
5647e4937aSGao Xiang * sizeof(struct erofs_xattr_ibody_header), in detail:
5747e4937aSGao Xiang * 1) it is not enough to contain erofs_xattr_ibody_header then
5847e4937aSGao Xiang * ->xattr_isize should be 0 (it means no xattr);
5947e4937aSGao Xiang * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
6047e4937aSGao Xiang * undefined right now (maybe use later with some new sb feature).
6147e4937aSGao Xiang */
6247e4937aSGao Xiang if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
63b780d3fcSGao Xiang erofs_err(sb,
644f761fa2SGao Xiang "xattr_isize %d of nid %llu is not supported yet",
6547e4937aSGao Xiang vi->xattr_isize, vi->nid);
6647e4937aSGao Xiang ret = -EOPNOTSUPP;
6747e4937aSGao Xiang goto out_unlock;
6847e4937aSGao Xiang } else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
698d8a09b0SGao Xiang if (vi->xattr_isize) {
70b780d3fcSGao Xiang erofs_err(sb, "bogus xattr ibody @ nid %llu", vi->nid);
7147e4937aSGao Xiang DBG_BUGON(1);
7247e4937aSGao Xiang ret = -EFSCORRUPTED;
7347e4937aSGao Xiang goto out_unlock; /* xattr ondisk layout error */
7447e4937aSGao Xiang }
7547e4937aSGao Xiang ret = -ENOATTR;
7647e4937aSGao Xiang goto out_unlock;
7747e4937aSGao Xiang }
7847e4937aSGao Xiang
79bb88e8daSGao Xiang it.buf = __EROFS_BUF_INITIALIZER;
809c39ec0cSJingbo Xu erofs_init_metabuf(&it.buf, sb);
81eba67eb6SJingbo Xu it.pos = erofs_iloc(inode) + vi->inode_isize;
8247e4937aSGao Xiang
83bb88e8daSGao Xiang /* read in shared xattr array (non-atomic, see kmalloc below) */
84eba67eb6SJingbo Xu it.kaddr = erofs_bread(&it.buf, erofs_blknr(sb, it.pos), EROFS_KMAP);
85bb88e8daSGao Xiang if (IS_ERR(it.kaddr)) {
86bb88e8daSGao Xiang ret = PTR_ERR(it.kaddr);
8747e4937aSGao Xiang goto out_unlock;
8847e4937aSGao Xiang }
8947e4937aSGao Xiang
90eba67eb6SJingbo Xu ih = it.kaddr + erofs_blkoff(sb, it.pos);
91*fd73a439SJingbo Xu vi->xattr_name_filter = le32_to_cpu(ih->h_name_filter);
9247e4937aSGao Xiang vi->xattr_shared_count = ih->h_shared_count;
9347e4937aSGao Xiang vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
9447e4937aSGao Xiang sizeof(uint), GFP_KERNEL);
9547e4937aSGao Xiang if (!vi->xattr_shared_xattrs) {
96bb88e8daSGao Xiang erofs_put_metabuf(&it.buf);
9747e4937aSGao Xiang ret = -ENOMEM;
9847e4937aSGao Xiang goto out_unlock;
9947e4937aSGao Xiang }
10047e4937aSGao Xiang
10147e4937aSGao Xiang /* let's skip ibody header */
102eba67eb6SJingbo Xu it.pos += sizeof(struct erofs_xattr_ibody_header);
10347e4937aSGao Xiang
10447e4937aSGao Xiang for (i = 0; i < vi->xattr_shared_count; ++i) {
105eba67eb6SJingbo Xu it.kaddr = erofs_bread(&it.buf, erofs_blknr(sb, it.pos),
106eba67eb6SJingbo Xu EROFS_KMAP);
107bb88e8daSGao Xiang if (IS_ERR(it.kaddr)) {
10847e4937aSGao Xiang kfree(vi->xattr_shared_xattrs);
10947e4937aSGao Xiang vi->xattr_shared_xattrs = NULL;
110bb88e8daSGao Xiang ret = PTR_ERR(it.kaddr);
11147e4937aSGao Xiang goto out_unlock;
11247e4937aSGao Xiang }
113eba67eb6SJingbo Xu vi->xattr_shared_xattrs[i] = le32_to_cpu(*(__le32 *)
114eba67eb6SJingbo Xu (it.kaddr + erofs_blkoff(sb, it.pos)));
115eba67eb6SJingbo Xu it.pos += sizeof(__le32);
11647e4937aSGao Xiang }
117bb88e8daSGao Xiang erofs_put_metabuf(&it.buf);
11847e4937aSGao Xiang
119ce063129SGao Xiang /* paired with smp_mb() at the beginning of the function. */
120ce063129SGao Xiang smp_mb();
121a5876e24SGao Xiang set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
12247e4937aSGao Xiang
12347e4937aSGao Xiang out_unlock:
124a5876e24SGao Xiang clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
12547e4937aSGao Xiang return ret;
12647e4937aSGao Xiang }
12747e4937aSGao Xiang
erofs_xattr_user_list(struct dentry * dentry)12847e4937aSGao Xiang static bool erofs_xattr_user_list(struct dentry *dentry)
12947e4937aSGao Xiang {
130e6242465SGao Xiang return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
13147e4937aSGao Xiang }
13247e4937aSGao Xiang
erofs_xattr_trusted_list(struct dentry * dentry)13347e4937aSGao Xiang static bool erofs_xattr_trusted_list(struct dentry *dentry)
13447e4937aSGao Xiang {
13547e4937aSGao Xiang return capable(CAP_SYS_ADMIN);
13647e4937aSGao Xiang }
13747e4937aSGao Xiang
erofs_xattr_generic_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)13847e4937aSGao Xiang static int erofs_xattr_generic_get(const struct xattr_handler *handler,
13947e4937aSGao Xiang struct dentry *unused, struct inode *inode,
14047e4937aSGao Xiang const char *name, void *buffer, size_t size)
14147e4937aSGao Xiang {
142b05f30bcSJingbo Xu if (handler->flags == EROFS_XATTR_INDEX_USER &&
143b05f30bcSJingbo Xu !test_opt(&EROFS_I_SB(inode)->opt, XATTR_USER))
14447e4937aSGao Xiang return -EOPNOTSUPP;
14547e4937aSGao Xiang
14647e4937aSGao Xiang return erofs_getxattr(inode, handler->flags, name, buffer, size);
14747e4937aSGao Xiang }
14847e4937aSGao Xiang
14947e4937aSGao Xiang const struct xattr_handler erofs_xattr_user_handler = {
15047e4937aSGao Xiang .prefix = XATTR_USER_PREFIX,
15147e4937aSGao Xiang .flags = EROFS_XATTR_INDEX_USER,
15247e4937aSGao Xiang .list = erofs_xattr_user_list,
15347e4937aSGao Xiang .get = erofs_xattr_generic_get,
15447e4937aSGao Xiang };
15547e4937aSGao Xiang
15647e4937aSGao Xiang const struct xattr_handler erofs_xattr_trusted_handler = {
15747e4937aSGao Xiang .prefix = XATTR_TRUSTED_PREFIX,
15847e4937aSGao Xiang .flags = EROFS_XATTR_INDEX_TRUSTED,
15947e4937aSGao Xiang .list = erofs_xattr_trusted_list,
16047e4937aSGao Xiang .get = erofs_xattr_generic_get,
16147e4937aSGao Xiang };
16247e4937aSGao Xiang
16347e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_SECURITY
16447e4937aSGao Xiang const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
16547e4937aSGao Xiang .prefix = XATTR_SECURITY_PREFIX,
16647e4937aSGao Xiang .flags = EROFS_XATTR_INDEX_SECURITY,
16747e4937aSGao Xiang .get = erofs_xattr_generic_get,
16847e4937aSGao Xiang };
16947e4937aSGao Xiang #endif
17047e4937aSGao Xiang
17147e4937aSGao Xiang const struct xattr_handler *erofs_xattr_handlers[] = {
17247e4937aSGao Xiang &erofs_xattr_user_handler,
17347e4937aSGao Xiang &erofs_xattr_trusted_handler,
17447e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_SECURITY
17547e4937aSGao Xiang &erofs_xattr_security_handler,
17647e4937aSGao Xiang #endif
17747e4937aSGao Xiang NULL,
17847e4937aSGao Xiang };
17947e4937aSGao Xiang
erofs_xattr_copy_to_buffer(struct erofs_xattr_iter * it,unsigned int len)180f02615ebSJingbo Xu static int erofs_xattr_copy_to_buffer(struct erofs_xattr_iter *it,
181f02615ebSJingbo Xu unsigned int len)
18247e4937aSGao Xiang {
183f02615ebSJingbo Xu unsigned int slice, processed;
184f02615ebSJingbo Xu struct super_block *sb = it->sb;
185f02615ebSJingbo Xu void *src;
18647e4937aSGao Xiang
187f02615ebSJingbo Xu for (processed = 0; processed < len; processed += slice) {
188f02615ebSJingbo Xu it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
189f02615ebSJingbo Xu EROFS_KMAP);
190f02615ebSJingbo Xu if (IS_ERR(it->kaddr))
191f02615ebSJingbo Xu return PTR_ERR(it->kaddr);
192f02615ebSJingbo Xu
193f02615ebSJingbo Xu src = it->kaddr + erofs_blkoff(sb, it->pos);
194f02615ebSJingbo Xu slice = min_t(unsigned int, sb->s_blocksize -
195f02615ebSJingbo Xu erofs_blkoff(sb, it->pos), len - processed);
196f02615ebSJingbo Xu memcpy(it->buffer + it->buffer_ofs, src, slice);
197f02615ebSJingbo Xu it->buffer_ofs += slice;
198f02615ebSJingbo Xu it->pos += slice;
199f02615ebSJingbo Xu }
200f02615ebSJingbo Xu return 0;
201f02615ebSJingbo Xu }
202f02615ebSJingbo Xu
erofs_listxattr_foreach(struct erofs_xattr_iter * it)203f02615ebSJingbo Xu static int erofs_listxattr_foreach(struct erofs_xattr_iter *it)
204f02615ebSJingbo Xu {
205f02615ebSJingbo Xu struct erofs_xattr_entry entry;
206f02615ebSJingbo Xu unsigned int base_index, name_total, prefix_len, infix_len = 0;
207f02615ebSJingbo Xu const char *prefix, *infix = NULL;
208f02615ebSJingbo Xu int err;
209f02615ebSJingbo Xu
210f02615ebSJingbo Xu /* 1. handle xattr entry */
211f02615ebSJingbo Xu entry = *(struct erofs_xattr_entry *)
212f02615ebSJingbo Xu (it->kaddr + erofs_blkoff(it->sb, it->pos));
213f02615ebSJingbo Xu it->pos += sizeof(struct erofs_xattr_entry);
214f02615ebSJingbo Xu
215f02615ebSJingbo Xu base_index = entry.e_name_index;
216f02615ebSJingbo Xu if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
2178e823961SJingbo Xu struct erofs_sb_info *sbi = EROFS_SB(it->sb);
21882bc1ef4SJingbo Xu struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
219f02615ebSJingbo Xu (entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
22047e4937aSGao Xiang
22182bc1ef4SJingbo Xu if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
222f02615ebSJingbo Xu return 0;
22382bc1ef4SJingbo Xu infix = pf->prefix->infix;
22482bc1ef4SJingbo Xu infix_len = pf->infix_len;
22582bc1ef4SJingbo Xu base_index = pf->prefix->base_index;
22682bc1ef4SJingbo Xu }
22782bc1ef4SJingbo Xu
22861d325dcSLinus Torvalds prefix = erofs_xattr_prefix(base_index, it->dentry);
229a5488f29SChristian Brauner if (!prefix)
230f02615ebSJingbo Xu return 0;
23147e4937aSGao Xiang prefix_len = strlen(prefix);
232f02615ebSJingbo Xu name_total = prefix_len + infix_len + entry.e_name_len + 1;
23347e4937aSGao Xiang
23447e4937aSGao Xiang if (!it->buffer) {
235f02615ebSJingbo Xu it->buffer_ofs += name_total;
236f02615ebSJingbo Xu return 0;
23747e4937aSGao Xiang }
23847e4937aSGao Xiang
239f02615ebSJingbo Xu if (it->buffer_ofs + name_total > it->buffer_size)
24047e4937aSGao Xiang return -ERANGE;
24147e4937aSGao Xiang
24247e4937aSGao Xiang memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
24382bc1ef4SJingbo Xu memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len);
24482bc1ef4SJingbo Xu it->buffer_ofs += prefix_len + infix_len;
24547e4937aSGao Xiang
246f02615ebSJingbo Xu /* 2. handle xattr name */
247f02615ebSJingbo Xu err = erofs_xattr_copy_to_buffer(it, entry.e_name_len);
248f02615ebSJingbo Xu if (err)
249f02615ebSJingbo Xu return err;
25047e4937aSGao Xiang
25147e4937aSGao Xiang it->buffer[it->buffer_ofs++] = '\0';
252f02615ebSJingbo Xu return 0;
25347e4937aSGao Xiang }
25447e4937aSGao Xiang
erofs_getxattr_foreach(struct erofs_xattr_iter * it)255f02615ebSJingbo Xu static int erofs_getxattr_foreach(struct erofs_xattr_iter *it)
256f02615ebSJingbo Xu {
257f02615ebSJingbo Xu struct super_block *sb = it->sb;
258f02615ebSJingbo Xu struct erofs_xattr_entry entry;
259f02615ebSJingbo Xu unsigned int slice, processed, value_sz;
260f02615ebSJingbo Xu
261f02615ebSJingbo Xu /* 1. handle xattr entry */
262f02615ebSJingbo Xu entry = *(struct erofs_xattr_entry *)
263f02615ebSJingbo Xu (it->kaddr + erofs_blkoff(sb, it->pos));
264f02615ebSJingbo Xu it->pos += sizeof(struct erofs_xattr_entry);
265f02615ebSJingbo Xu value_sz = le16_to_cpu(entry.e_value_size);
266f02615ebSJingbo Xu
267f02615ebSJingbo Xu /* should also match the infix for long name prefixes */
268f02615ebSJingbo Xu if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
269f02615ebSJingbo Xu struct erofs_sb_info *sbi = EROFS_SB(sb);
270f02615ebSJingbo Xu struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
271f02615ebSJingbo Xu (entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
272f02615ebSJingbo Xu
273f02615ebSJingbo Xu if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
274f02615ebSJingbo Xu return -ENOATTR;
275f02615ebSJingbo Xu
276f02615ebSJingbo Xu if (it->index != pf->prefix->base_index ||
277f02615ebSJingbo Xu it->name.len != entry.e_name_len + pf->infix_len)
278f02615ebSJingbo Xu return -ENOATTR;
279f02615ebSJingbo Xu
280f02615ebSJingbo Xu if (memcmp(it->name.name, pf->prefix->infix, pf->infix_len))
281f02615ebSJingbo Xu return -ENOATTR;
282f02615ebSJingbo Xu
283f02615ebSJingbo Xu it->infix_len = pf->infix_len;
284f02615ebSJingbo Xu } else {
285f02615ebSJingbo Xu if (it->index != entry.e_name_index ||
286f02615ebSJingbo Xu it->name.len != entry.e_name_len)
287f02615ebSJingbo Xu return -ENOATTR;
288f02615ebSJingbo Xu
289f02615ebSJingbo Xu it->infix_len = 0;
290f02615ebSJingbo Xu }
291f02615ebSJingbo Xu
292f02615ebSJingbo Xu /* 2. handle xattr name */
293f02615ebSJingbo Xu for (processed = 0; processed < entry.e_name_len; processed += slice) {
294f02615ebSJingbo Xu it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
295f02615ebSJingbo Xu EROFS_KMAP);
296f02615ebSJingbo Xu if (IS_ERR(it->kaddr))
297f02615ebSJingbo Xu return PTR_ERR(it->kaddr);
298f02615ebSJingbo Xu
299f02615ebSJingbo Xu slice = min_t(unsigned int,
300f02615ebSJingbo Xu sb->s_blocksize - erofs_blkoff(sb, it->pos),
301f02615ebSJingbo Xu entry.e_name_len - processed);
302f02615ebSJingbo Xu if (memcmp(it->name.name + it->infix_len + processed,
303f02615ebSJingbo Xu it->kaddr + erofs_blkoff(sb, it->pos), slice))
304f02615ebSJingbo Xu return -ENOATTR;
305f02615ebSJingbo Xu it->pos += slice;
306f02615ebSJingbo Xu }
307f02615ebSJingbo Xu
308f02615ebSJingbo Xu /* 3. handle xattr value */
309f02615ebSJingbo Xu if (!it->buffer) {
310f02615ebSJingbo Xu it->buffer_ofs = value_sz;
311f02615ebSJingbo Xu return 0;
312f02615ebSJingbo Xu }
313f02615ebSJingbo Xu
314f02615ebSJingbo Xu if (it->buffer_size < value_sz)
315f02615ebSJingbo Xu return -ERANGE;
316f02615ebSJingbo Xu
317f02615ebSJingbo Xu return erofs_xattr_copy_to_buffer(it, value_sz);
318f02615ebSJingbo Xu }
31947e4937aSGao Xiang
erofs_xattr_iter_inline(struct erofs_xattr_iter * it,struct inode * inode,bool getxattr)3204b077b50SJingbo Xu static int erofs_xattr_iter_inline(struct erofs_xattr_iter *it,
3214b077b50SJingbo Xu struct inode *inode, bool getxattr)
32247e4937aSGao Xiang {
3234b077b50SJingbo Xu struct erofs_inode *const vi = EROFS_I(inode);
324f02615ebSJingbo Xu unsigned int xattr_header_sz, remaining, entry_sz;
325f02615ebSJingbo Xu erofs_off_t next_pos;
32647e4937aSGao Xiang int ret;
32747e4937aSGao Xiang
3284b077b50SJingbo Xu xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) +
3294b077b50SJingbo Xu sizeof(u32) * vi->xattr_shared_count;
3304b077b50SJingbo Xu if (xattr_header_sz >= vi->xattr_isize) {
3314b077b50SJingbo Xu DBG_BUGON(xattr_header_sz > vi->xattr_isize);
3324b077b50SJingbo Xu return -ENOATTR;
3334b077b50SJingbo Xu }
33447e4937aSGao Xiang
335f02615ebSJingbo Xu remaining = vi->xattr_isize - xattr_header_sz;
3364b077b50SJingbo Xu it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz;
337f02615ebSJingbo Xu
338f02615ebSJingbo Xu while (remaining) {
3394b077b50SJingbo Xu it->kaddr = erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos),
3404b077b50SJingbo Xu EROFS_KMAP);
3414b077b50SJingbo Xu if (IS_ERR(it->kaddr))
3424b077b50SJingbo Xu return PTR_ERR(it->kaddr);
3434b077b50SJingbo Xu
344f02615ebSJingbo Xu entry_sz = erofs_xattr_entry_size(it->kaddr +
345f02615ebSJingbo Xu erofs_blkoff(it->sb, it->pos));
346f02615ebSJingbo Xu /* xattr on-disk corruption: xattr entry beyond xattr_isize */
347f02615ebSJingbo Xu if (remaining < entry_sz) {
348f02615ebSJingbo Xu DBG_BUGON(1);
349f02615ebSJingbo Xu return -EFSCORRUPTED;
350f02615ebSJingbo Xu }
351f02615ebSJingbo Xu remaining -= entry_sz;
352f02615ebSJingbo Xu next_pos = it->pos + entry_sz;
3534b077b50SJingbo Xu
354f02615ebSJingbo Xu if (getxattr)
355f02615ebSJingbo Xu ret = erofs_getxattr_foreach(it);
356f02615ebSJingbo Xu else
357f02615ebSJingbo Xu ret = erofs_listxattr_foreach(it);
3584b077b50SJingbo Xu if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
35947e4937aSGao Xiang break;
360f02615ebSJingbo Xu
361f02615ebSJingbo Xu it->pos = next_pos;
36247e4937aSGao Xiang }
3634b077b50SJingbo Xu return ret;
36447e4937aSGao Xiang }
36547e4937aSGao Xiang
erofs_xattr_iter_shared(struct erofs_xattr_iter * it,struct inode * inode,bool getxattr)3664b077b50SJingbo Xu static int erofs_xattr_iter_shared(struct erofs_xattr_iter *it,
3674b077b50SJingbo Xu struct inode *inode, bool getxattr)
36847e4937aSGao Xiang {
369a5876e24SGao Xiang struct erofs_inode *const vi = EROFS_I(inode);
3708e823961SJingbo Xu struct super_block *const sb = it->sb;
371eba67eb6SJingbo Xu struct erofs_sb_info *sbi = EROFS_SB(sb);
372eba67eb6SJingbo Xu unsigned int i;
3734b077b50SJingbo Xu int ret = -ENOATTR;
3744b077b50SJingbo Xu
37547e4937aSGao Xiang for (i = 0; i < vi->xattr_shared_count; ++i) {
3768e823961SJingbo Xu it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
377eba67eb6SJingbo Xu vi->xattr_shared_xattrs[i] * sizeof(__le32);
3788e823961SJingbo Xu it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
3798e823961SJingbo Xu EROFS_KMAP);
3808e823961SJingbo Xu if (IS_ERR(it->kaddr))
3818e823961SJingbo Xu return PTR_ERR(it->kaddr);
38247e4937aSGao Xiang
383f02615ebSJingbo Xu if (getxattr)
384f02615ebSJingbo Xu ret = erofs_getxattr_foreach(it);
385f02615ebSJingbo Xu else
386f02615ebSJingbo Xu ret = erofs_listxattr_foreach(it);
3874b077b50SJingbo Xu if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
38847e4937aSGao Xiang break;
38947e4937aSGao Xiang }
3904b077b50SJingbo Xu return ret;
39147e4937aSGao Xiang }
39247e4937aSGao Xiang
erofs_getxattr(struct inode * inode,int index,const char * name,void * buffer,size_t buffer_size)3934b077b50SJingbo Xu int erofs_getxattr(struct inode *inode, int index, const char *name,
3944b077b50SJingbo Xu void *buffer, size_t buffer_size)
39547e4937aSGao Xiang {
39647e4937aSGao Xiang int ret;
397*fd73a439SJingbo Xu unsigned int hashbit;
3988e823961SJingbo Xu struct erofs_xattr_iter it;
399*fd73a439SJingbo Xu struct erofs_inode *vi = EROFS_I(inode);
400*fd73a439SJingbo Xu struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
40147e4937aSGao Xiang
4024b077b50SJingbo Xu if (!name)
4034b077b50SJingbo Xu return -EINVAL;
4044b077b50SJingbo Xu
4054b077b50SJingbo Xu ret = erofs_init_inode_xattrs(inode);
4064b077b50SJingbo Xu if (ret)
4074b077b50SJingbo Xu return ret;
4084b077b50SJingbo Xu
409*fd73a439SJingbo Xu /* reserved flag is non-zero if there's any change of on-disk format */
410*fd73a439SJingbo Xu if (erofs_sb_has_xattr_filter(sbi) && !sbi->xattr_filter_reserved) {
411*fd73a439SJingbo Xu hashbit = xxh32(name, strlen(name),
412*fd73a439SJingbo Xu EROFS_XATTR_FILTER_SEED + index);
413*fd73a439SJingbo Xu hashbit &= EROFS_XATTR_FILTER_BITS - 1;
414*fd73a439SJingbo Xu if (vi->xattr_name_filter & (1U << hashbit))
415*fd73a439SJingbo Xu return -ENOATTR;
416*fd73a439SJingbo Xu }
417*fd73a439SJingbo Xu
4184b077b50SJingbo Xu it.index = index;
4194b077b50SJingbo Xu it.name = (struct qstr)QSTR_INIT(name, strlen(name));
4204b077b50SJingbo Xu if (it.name.len > EROFS_NAME_LEN)
4214b077b50SJingbo Xu return -ERANGE;
4224b077b50SJingbo Xu
4234b077b50SJingbo Xu it.sb = inode->i_sb;
4244b077b50SJingbo Xu it.buf = __EROFS_BUF_INITIALIZER;
4254b077b50SJingbo Xu erofs_init_metabuf(&it.buf, it.sb);
4264b077b50SJingbo Xu it.buffer = buffer;
4274b077b50SJingbo Xu it.buffer_size = buffer_size;
4284b077b50SJingbo Xu it.buffer_ofs = 0;
4294b077b50SJingbo Xu
4304b077b50SJingbo Xu ret = erofs_xattr_iter_inline(&it, inode, true);
4314b077b50SJingbo Xu if (ret == -ENOATTR)
4324b077b50SJingbo Xu ret = erofs_xattr_iter_shared(&it, inode, true);
4334b077b50SJingbo Xu erofs_put_metabuf(&it.buf);
4344b077b50SJingbo Xu return ret ? ret : it.buffer_ofs;
4354b077b50SJingbo Xu }
4364b077b50SJingbo Xu
erofs_listxattr(struct dentry * dentry,char * buffer,size_t buffer_size)4374b077b50SJingbo Xu ssize_t erofs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
4384b077b50SJingbo Xu {
4394b077b50SJingbo Xu int ret;
4404b077b50SJingbo Xu struct erofs_xattr_iter it;
4414b077b50SJingbo Xu struct inode *inode = d_inode(dentry);
4424b077b50SJingbo Xu
4434b077b50SJingbo Xu ret = erofs_init_inode_xattrs(inode);
444926d1650SGao Xiang if (ret == -ENOATTR)
445926d1650SGao Xiang return 0;
44647e4937aSGao Xiang if (ret)
44747e4937aSGao Xiang return ret;
44847e4937aSGao Xiang
4498e823961SJingbo Xu it.sb = dentry->d_sb;
4508e823961SJingbo Xu it.buf = __EROFS_BUF_INITIALIZER;
4518e823961SJingbo Xu erofs_init_metabuf(&it.buf, it.sb);
45247e4937aSGao Xiang it.dentry = dentry;
45347e4937aSGao Xiang it.buffer = buffer;
45447e4937aSGao Xiang it.buffer_size = buffer_size;
45547e4937aSGao Xiang it.buffer_ofs = 0;
45647e4937aSGao Xiang
4574b077b50SJingbo Xu ret = erofs_xattr_iter_inline(&it, inode, false);
4584b077b50SJingbo Xu if (!ret || ret == -ENOATTR)
4594b077b50SJingbo Xu ret = erofs_xattr_iter_shared(&it, inode, false);
4604b077b50SJingbo Xu if (ret == -ENOATTR)
4614b077b50SJingbo Xu ret = 0;
4628e823961SJingbo Xu erofs_put_metabuf(&it.buf);
4634b077b50SJingbo Xu return ret ? ret : it.buffer_ofs;
46447e4937aSGao Xiang }
46547e4937aSGao Xiang
erofs_xattr_prefixes_cleanup(struct super_block * sb)4669e382914SJingbo Xu void erofs_xattr_prefixes_cleanup(struct super_block *sb)
4679e382914SJingbo Xu {
4689e382914SJingbo Xu struct erofs_sb_info *sbi = EROFS_SB(sb);
4699e382914SJingbo Xu int i;
4709e382914SJingbo Xu
4719e382914SJingbo Xu if (sbi->xattr_prefixes) {
4729e382914SJingbo Xu for (i = 0; i < sbi->xattr_prefix_count; i++)
4739e382914SJingbo Xu kfree(sbi->xattr_prefixes[i].prefix);
4749e382914SJingbo Xu kfree(sbi->xattr_prefixes);
4759e382914SJingbo Xu sbi->xattr_prefixes = NULL;
4769e382914SJingbo Xu }
4779e382914SJingbo Xu }
4789e382914SJingbo Xu
erofs_xattr_prefixes_init(struct super_block * sb)4799e382914SJingbo Xu int erofs_xattr_prefixes_init(struct super_block *sb)
4809e382914SJingbo Xu {
4819e382914SJingbo Xu struct erofs_sb_info *sbi = EROFS_SB(sb);
4829e382914SJingbo Xu struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
4839e382914SJingbo Xu erofs_off_t pos = (erofs_off_t)sbi->xattr_prefix_start << 2;
4849e382914SJingbo Xu struct erofs_xattr_prefix_item *pfs;
4859e382914SJingbo Xu int ret = 0, i, len;
4869e382914SJingbo Xu
4879e382914SJingbo Xu if (!sbi->xattr_prefix_count)
4889e382914SJingbo Xu return 0;
4899e382914SJingbo Xu
4909e382914SJingbo Xu pfs = kzalloc(sbi->xattr_prefix_count * sizeof(*pfs), GFP_KERNEL);
4919e382914SJingbo Xu if (!pfs)
4929e382914SJingbo Xu return -ENOMEM;
4939e382914SJingbo Xu
4940a17567bSJingbo Xu if (sbi->packed_inode)
4959e382914SJingbo Xu buf.inode = sbi->packed_inode;
4969e382914SJingbo Xu else
4979e382914SJingbo Xu erofs_init_metabuf(&buf, sb);
4989e382914SJingbo Xu
4999e382914SJingbo Xu for (i = 0; i < sbi->xattr_prefix_count; i++) {
5009e382914SJingbo Xu void *ptr = erofs_read_metadata(sb, &buf, &pos, &len);
5019e382914SJingbo Xu
5029e382914SJingbo Xu if (IS_ERR(ptr)) {
5039e382914SJingbo Xu ret = PTR_ERR(ptr);
5049e382914SJingbo Xu break;
5059e382914SJingbo Xu } else if (len < sizeof(*pfs->prefix) ||
5069e382914SJingbo Xu len > EROFS_NAME_LEN + sizeof(*pfs->prefix)) {
5079e382914SJingbo Xu kfree(ptr);
5089e382914SJingbo Xu ret = -EFSCORRUPTED;
5099e382914SJingbo Xu break;
5109e382914SJingbo Xu }
5119e382914SJingbo Xu pfs[i].prefix = ptr;
5129e382914SJingbo Xu pfs[i].infix_len = len - sizeof(struct erofs_xattr_long_prefix);
5139e382914SJingbo Xu }
5149e382914SJingbo Xu
5159e382914SJingbo Xu erofs_put_metabuf(&buf);
5169e382914SJingbo Xu sbi->xattr_prefixes = pfs;
5179e382914SJingbo Xu if (ret)
5189e382914SJingbo Xu erofs_xattr_prefixes_cleanup(sb);
5199e382914SJingbo Xu return ret;
5209e382914SJingbo Xu }
5219e382914SJingbo Xu
52247e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_POSIX_ACL
erofs_get_acl(struct inode * inode,int type,bool rcu)5230cad6246SMiklos Szeredi struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
52447e4937aSGao Xiang {
52547e4937aSGao Xiang struct posix_acl *acl;
52647e4937aSGao Xiang int prefix, rc;
52747e4937aSGao Xiang char *value = NULL;
52847e4937aSGao Xiang
5290cad6246SMiklos Szeredi if (rcu)
5300cad6246SMiklos Szeredi return ERR_PTR(-ECHILD);
5310cad6246SMiklos Szeredi
53247e4937aSGao Xiang switch (type) {
53347e4937aSGao Xiang case ACL_TYPE_ACCESS:
53447e4937aSGao Xiang prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
53547e4937aSGao Xiang break;
53647e4937aSGao Xiang case ACL_TYPE_DEFAULT:
53747e4937aSGao Xiang prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
53847e4937aSGao Xiang break;
53947e4937aSGao Xiang default:
54047e4937aSGao Xiang return ERR_PTR(-EINVAL);
54147e4937aSGao Xiang }
54247e4937aSGao Xiang
54347e4937aSGao Xiang rc = erofs_getxattr(inode, prefix, "", NULL, 0);
54447e4937aSGao Xiang if (rc > 0) {
54547e4937aSGao Xiang value = kmalloc(rc, GFP_KERNEL);
54647e4937aSGao Xiang if (!value)
54747e4937aSGao Xiang return ERR_PTR(-ENOMEM);
54847e4937aSGao Xiang rc = erofs_getxattr(inode, prefix, "", value, rc);
54947e4937aSGao Xiang }
55047e4937aSGao Xiang
55147e4937aSGao Xiang if (rc == -ENOATTR)
55247e4937aSGao Xiang acl = NULL;
55347e4937aSGao Xiang else if (rc < 0)
55447e4937aSGao Xiang acl = ERR_PTR(rc);
55547e4937aSGao Xiang else
55647e4937aSGao Xiang acl = posix_acl_from_xattr(&init_user_ns, value, rc);
55747e4937aSGao Xiang kfree(value);
55847e4937aSGao Xiang return acl;
55947e4937aSGao Xiang }
56047e4937aSGao Xiang #endif
561