xref: /openbmc/linux/fs/erofs/xattr.c (revision 4b077b50)
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>
847e4937aSGao Xiang #include "xattr.h"
947e4937aSGao Xiang 
108e823961SJingbo Xu struct erofs_xattr_iter {
1147e4937aSGao Xiang 	struct super_block *sb;
12bb88e8daSGao Xiang 	struct erofs_buf buf;
13eba67eb6SJingbo Xu 	erofs_off_t pos;
1447e4937aSGao Xiang 	void *kaddr;
158e823961SJingbo Xu 
168e823961SJingbo Xu 	char *buffer;
178e823961SJingbo Xu 	int buffer_size, buffer_ofs;
188e823961SJingbo Xu 
198e823961SJingbo Xu 	/* getxattr */
208e823961SJingbo Xu 	int index, infix_len;
218e823961SJingbo Xu 	struct qstr name;
228e823961SJingbo Xu 
238e823961SJingbo Xu 	/* listxattr */
248e823961SJingbo Xu 	struct dentry *dentry;
2547e4937aSGao Xiang };
2647e4937aSGao Xiang 
276020ffe7SJingbo Xu static int erofs_init_inode_xattrs(struct inode *inode)
2847e4937aSGao Xiang {
29a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(inode);
308e823961SJingbo Xu 	struct erofs_xattr_iter it;
3147e4937aSGao Xiang 	unsigned int i;
3247e4937aSGao Xiang 	struct erofs_xattr_ibody_header *ih;
33b780d3fcSGao Xiang 	struct super_block *sb = inode->i_sb;
3447e4937aSGao Xiang 	int ret = 0;
3547e4937aSGao Xiang 
3647e4937aSGao Xiang 	/* the most case is that xattrs of this inode are initialized. */
37ce063129SGao Xiang 	if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
38ce063129SGao Xiang 		/*
39ce063129SGao Xiang 		 * paired with smp_mb() at the end of the function to ensure
40ce063129SGao Xiang 		 * fields will only be observed after the bit is set.
41ce063129SGao Xiang 		 */
42ce063129SGao Xiang 		smp_mb();
4347e4937aSGao Xiang 		return 0;
44ce063129SGao Xiang 	}
4547e4937aSGao Xiang 
46a5876e24SGao Xiang 	if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
4747e4937aSGao Xiang 		return -ERESTARTSYS;
4847e4937aSGao Xiang 
4947e4937aSGao Xiang 	/* someone has initialized xattrs for us? */
50a5876e24SGao Xiang 	if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
5147e4937aSGao Xiang 		goto out_unlock;
5247e4937aSGao Xiang 
5347e4937aSGao Xiang 	/*
5447e4937aSGao Xiang 	 * bypass all xattr operations if ->xattr_isize is not greater than
5547e4937aSGao Xiang 	 * sizeof(struct erofs_xattr_ibody_header), in detail:
5647e4937aSGao Xiang 	 * 1) it is not enough to contain erofs_xattr_ibody_header then
5747e4937aSGao Xiang 	 *    ->xattr_isize should be 0 (it means no xattr);
5847e4937aSGao Xiang 	 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
5947e4937aSGao Xiang 	 *    undefined right now (maybe use later with some new sb feature).
6047e4937aSGao Xiang 	 */
6147e4937aSGao Xiang 	if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
62b780d3fcSGao Xiang 		erofs_err(sb,
634f761fa2SGao Xiang 			  "xattr_isize %d of nid %llu is not supported yet",
6447e4937aSGao Xiang 			  vi->xattr_isize, vi->nid);
6547e4937aSGao Xiang 		ret = -EOPNOTSUPP;
6647e4937aSGao Xiang 		goto out_unlock;
6747e4937aSGao Xiang 	} else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
688d8a09b0SGao Xiang 		if (vi->xattr_isize) {
69b780d3fcSGao Xiang 			erofs_err(sb, "bogus xattr ibody @ nid %llu", vi->nid);
7047e4937aSGao Xiang 			DBG_BUGON(1);
7147e4937aSGao Xiang 			ret = -EFSCORRUPTED;
7247e4937aSGao Xiang 			goto out_unlock;	/* xattr ondisk layout error */
7347e4937aSGao Xiang 		}
7447e4937aSGao Xiang 		ret = -ENOATTR;
7547e4937aSGao Xiang 		goto out_unlock;
7647e4937aSGao Xiang 	}
7747e4937aSGao Xiang 
78bb88e8daSGao Xiang 	it.buf = __EROFS_BUF_INITIALIZER;
799c39ec0cSJingbo Xu 	erofs_init_metabuf(&it.buf, sb);
80eba67eb6SJingbo Xu 	it.pos = erofs_iloc(inode) + vi->inode_isize;
8147e4937aSGao Xiang 
82bb88e8daSGao Xiang 	/* read in shared xattr array (non-atomic, see kmalloc below) */
83eba67eb6SJingbo Xu 	it.kaddr = erofs_bread(&it.buf, erofs_blknr(sb, it.pos), EROFS_KMAP);
84bb88e8daSGao Xiang 	if (IS_ERR(it.kaddr)) {
85bb88e8daSGao Xiang 		ret = PTR_ERR(it.kaddr);
8647e4937aSGao Xiang 		goto out_unlock;
8747e4937aSGao Xiang 	}
8847e4937aSGao Xiang 
89eba67eb6SJingbo Xu 	ih = it.kaddr + erofs_blkoff(sb, it.pos);
9047e4937aSGao Xiang 	vi->xattr_shared_count = ih->h_shared_count;
9147e4937aSGao Xiang 	vi->xattr_shared_xattrs = kmalloc_array(vi->xattr_shared_count,
9247e4937aSGao Xiang 						sizeof(uint), GFP_KERNEL);
9347e4937aSGao Xiang 	if (!vi->xattr_shared_xattrs) {
94bb88e8daSGao Xiang 		erofs_put_metabuf(&it.buf);
9547e4937aSGao Xiang 		ret = -ENOMEM;
9647e4937aSGao Xiang 		goto out_unlock;
9747e4937aSGao Xiang 	}
9847e4937aSGao Xiang 
9947e4937aSGao Xiang 	/* let's skip ibody header */
100eba67eb6SJingbo Xu 	it.pos += sizeof(struct erofs_xattr_ibody_header);
10147e4937aSGao Xiang 
10247e4937aSGao Xiang 	for (i = 0; i < vi->xattr_shared_count; ++i) {
103eba67eb6SJingbo Xu 		it.kaddr = erofs_bread(&it.buf, erofs_blknr(sb, it.pos),
104eba67eb6SJingbo Xu 				       EROFS_KMAP);
105bb88e8daSGao Xiang 		if (IS_ERR(it.kaddr)) {
10647e4937aSGao Xiang 			kfree(vi->xattr_shared_xattrs);
10747e4937aSGao Xiang 			vi->xattr_shared_xattrs = NULL;
108bb88e8daSGao Xiang 			ret = PTR_ERR(it.kaddr);
10947e4937aSGao Xiang 			goto out_unlock;
11047e4937aSGao Xiang 		}
111eba67eb6SJingbo Xu 		vi->xattr_shared_xattrs[i] = le32_to_cpu(*(__le32 *)
112eba67eb6SJingbo Xu 				(it.kaddr + erofs_blkoff(sb, it.pos)));
113eba67eb6SJingbo Xu 		it.pos += sizeof(__le32);
11447e4937aSGao Xiang 	}
115bb88e8daSGao Xiang 	erofs_put_metabuf(&it.buf);
11647e4937aSGao Xiang 
117ce063129SGao Xiang 	/* paired with smp_mb() at the beginning of the function. */
118ce063129SGao Xiang 	smp_mb();
119a5876e24SGao Xiang 	set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
12047e4937aSGao Xiang 
12147e4937aSGao Xiang out_unlock:
122a5876e24SGao Xiang 	clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
12347e4937aSGao Xiang 	return ret;
12447e4937aSGao Xiang }
12547e4937aSGao Xiang 
12647e4937aSGao Xiang /*
12747e4937aSGao Xiang  * the general idea for these return values is
12847e4937aSGao Xiang  * if    0 is returned, go on processing the current xattr;
12947e4937aSGao Xiang  *       1 (> 0) is returned, skip this round to process the next xattr;
13047e4937aSGao Xiang  *    -err (< 0) is returned, an error (maybe ENOXATTR) occurred
13147e4937aSGao Xiang  *                            and need to be handled
13247e4937aSGao Xiang  */
13347e4937aSGao Xiang struct xattr_iter_handlers {
1348e823961SJingbo Xu 	int (*entry)(struct erofs_xattr_iter *it, struct erofs_xattr_entry *entry);
1358e823961SJingbo Xu 	int (*name)(struct erofs_xattr_iter *it, unsigned int processed, char *buf,
13647e4937aSGao Xiang 		    unsigned int len);
1378e823961SJingbo Xu 	int (*alloc_buffer)(struct erofs_xattr_iter *it, unsigned int value_sz);
1388e823961SJingbo Xu 	void (*value)(struct erofs_xattr_iter *it, unsigned int processed, char *buf,
13947e4937aSGao Xiang 		      unsigned int len);
14047e4937aSGao Xiang };
14147e4937aSGao Xiang 
14247e4937aSGao Xiang /*
14347e4937aSGao Xiang  * Regardless of success or failure, `xattr_foreach' will end up with
144eba67eb6SJingbo Xu  * `pos' pointing to the next xattr item rather than an arbitrary position.
14547e4937aSGao Xiang  */
1468e823961SJingbo Xu static int xattr_foreach(struct erofs_xattr_iter *it,
14747e4937aSGao Xiang 			 const struct xattr_iter_handlers *op,
14847e4937aSGao Xiang 			 unsigned int *tlimit)
14947e4937aSGao Xiang {
15047e4937aSGao Xiang 	struct erofs_xattr_entry entry;
151eba67eb6SJingbo Xu 	struct super_block *sb = it->sb;
15247e4937aSGao Xiang 	unsigned int value_sz, processed, slice;
15347e4937aSGao Xiang 	int err;
15447e4937aSGao Xiang 
155eba67eb6SJingbo Xu 	/* 0. fixup blkaddr, pos */
156eba67eb6SJingbo Xu 	it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos), EROFS_KMAP);
157eba67eb6SJingbo Xu 	if (IS_ERR(it->kaddr))
158eba67eb6SJingbo Xu 		return PTR_ERR(it->kaddr);
15947e4937aSGao Xiang 
16047e4937aSGao Xiang 	/*
16147e4937aSGao Xiang 	 * 1. read xattr entry to the memory,
16247e4937aSGao Xiang 	 *    since we do EROFS_XATTR_ALIGN
16347e4937aSGao Xiang 	 *    therefore entry should be in the page
16447e4937aSGao Xiang 	 */
165eba67eb6SJingbo Xu 	entry = *(struct erofs_xattr_entry *)
166eba67eb6SJingbo Xu 		(it->kaddr + erofs_blkoff(sb, it->pos));
16747e4937aSGao Xiang 	if (tlimit) {
168b6796abdSGao Xiang 		unsigned int entry_sz = erofs_xattr_entry_size(&entry);
16947e4937aSGao Xiang 
17047e4937aSGao Xiang 		/* xattr on-disk corruption: xattr entry beyond xattr_isize */
1718d8a09b0SGao Xiang 		if (*tlimit < entry_sz) {
17247e4937aSGao Xiang 			DBG_BUGON(1);
17347e4937aSGao Xiang 			return -EFSCORRUPTED;
17447e4937aSGao Xiang 		}
17547e4937aSGao Xiang 		*tlimit -= entry_sz;
17647e4937aSGao Xiang 	}
17747e4937aSGao Xiang 
178eba67eb6SJingbo Xu 	it->pos += sizeof(struct erofs_xattr_entry);
17947e4937aSGao Xiang 	value_sz = le16_to_cpu(entry.e_value_size);
18047e4937aSGao Xiang 
18147e4937aSGao Xiang 	/* handle entry */
18247e4937aSGao Xiang 	err = op->entry(it, &entry);
18347e4937aSGao Xiang 	if (err) {
184eba67eb6SJingbo Xu 		it->pos += entry.e_name_len + value_sz;
18547e4937aSGao Xiang 		goto out;
18647e4937aSGao Xiang 	}
18747e4937aSGao Xiang 
188eba67eb6SJingbo Xu 	/* 2. handle xattr name (pos will finally be at the end of name) */
18947e4937aSGao Xiang 	processed = 0;
19047e4937aSGao Xiang 
19147e4937aSGao Xiang 	while (processed < entry.e_name_len) {
192eba67eb6SJingbo Xu 		it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
193eba67eb6SJingbo Xu 					EROFS_KMAP);
194eba67eb6SJingbo Xu 		if (IS_ERR(it->kaddr)) {
195eba67eb6SJingbo Xu 			err = PTR_ERR(it->kaddr);
19647e4937aSGao Xiang 			goto out;
19747e4937aSGao Xiang 		}
19847e4937aSGao Xiang 
199eba67eb6SJingbo Xu 		slice = min_t(unsigned int,
200eba67eb6SJingbo Xu 			      sb->s_blocksize - erofs_blkoff(sb, it->pos),
20147e4937aSGao Xiang 			      entry.e_name_len - processed);
20247e4937aSGao Xiang 
20347e4937aSGao Xiang 		/* handle name */
204eba67eb6SJingbo Xu 		err = op->name(it, processed,
205eba67eb6SJingbo Xu 			       it->kaddr + erofs_blkoff(sb, it->pos), slice);
20647e4937aSGao Xiang 		if (err) {
207eba67eb6SJingbo Xu 			it->pos += entry.e_name_len - processed + value_sz;
20847e4937aSGao Xiang 			goto out;
20947e4937aSGao Xiang 		}
21047e4937aSGao Xiang 
211eba67eb6SJingbo Xu 		it->pos += slice;
21247e4937aSGao Xiang 		processed += slice;
21347e4937aSGao Xiang 	}
21447e4937aSGao Xiang 
21547e4937aSGao Xiang 	/* 3. handle xattr value */
21647e4937aSGao Xiang 	processed = 0;
21747e4937aSGao Xiang 
21847e4937aSGao Xiang 	if (op->alloc_buffer) {
21947e4937aSGao Xiang 		err = op->alloc_buffer(it, value_sz);
22047e4937aSGao Xiang 		if (err) {
221eba67eb6SJingbo Xu 			it->pos += value_sz;
22247e4937aSGao Xiang 			goto out;
22347e4937aSGao Xiang 		}
22447e4937aSGao Xiang 	}
22547e4937aSGao Xiang 
22647e4937aSGao Xiang 	while (processed < value_sz) {
227eba67eb6SJingbo Xu 		it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
228eba67eb6SJingbo Xu 					EROFS_KMAP);
229eba67eb6SJingbo Xu 		if (IS_ERR(it->kaddr)) {
230eba67eb6SJingbo Xu 			err = PTR_ERR(it->kaddr);
23147e4937aSGao Xiang 			goto out;
23247e4937aSGao Xiang 		}
23347e4937aSGao Xiang 
234eba67eb6SJingbo Xu 		slice = min_t(unsigned int,
235eba67eb6SJingbo Xu 			      sb->s_blocksize - erofs_blkoff(sb, it->pos),
23647e4937aSGao Xiang 			      value_sz - processed);
237eba67eb6SJingbo Xu 		op->value(it, processed, it->kaddr + erofs_blkoff(sb, it->pos),
238eba67eb6SJingbo Xu 			  slice);
239eba67eb6SJingbo Xu 		it->pos += slice;
24047e4937aSGao Xiang 		processed += slice;
24147e4937aSGao Xiang 	}
24247e4937aSGao Xiang 
24347e4937aSGao Xiang out:
24447e4937aSGao Xiang 	/* xattrs should be 4-byte aligned (on-disk constraint) */
245eba67eb6SJingbo Xu 	it->pos = EROFS_XATTR_ALIGN(it->pos);
24647e4937aSGao Xiang 	return err < 0 ? err : 0;
24747e4937aSGao Xiang }
24847e4937aSGao Xiang 
2498e823961SJingbo Xu static int erofs_xattr_long_entrymatch(struct erofs_xattr_iter *it,
25082bc1ef4SJingbo Xu 				       struct erofs_xattr_entry *entry)
25182bc1ef4SJingbo Xu {
2528e823961SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(it->sb);
25382bc1ef4SJingbo Xu 	struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
25482bc1ef4SJingbo Xu 		(entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
25582bc1ef4SJingbo Xu 
25682bc1ef4SJingbo Xu 	if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
25782bc1ef4SJingbo Xu 		return -ENOATTR;
25882bc1ef4SJingbo Xu 
25982bc1ef4SJingbo Xu 	if (it->index != pf->prefix->base_index ||
26082bc1ef4SJingbo Xu 	    it->name.len != entry->e_name_len + pf->infix_len)
26182bc1ef4SJingbo Xu 		return -ENOATTR;
26282bc1ef4SJingbo Xu 
26382bc1ef4SJingbo Xu 	if (memcmp(it->name.name, pf->prefix->infix, pf->infix_len))
26482bc1ef4SJingbo Xu 		return -ENOATTR;
26582bc1ef4SJingbo Xu 
26682bc1ef4SJingbo Xu 	it->infix_len = pf->infix_len;
26782bc1ef4SJingbo Xu 	return 0;
26882bc1ef4SJingbo Xu }
26982bc1ef4SJingbo Xu 
2708e823961SJingbo Xu static int xattr_entrymatch(struct erofs_xattr_iter *it,
27147e4937aSGao Xiang 			    struct erofs_xattr_entry *entry)
27247e4937aSGao Xiang {
27382bc1ef4SJingbo Xu 	/* should also match the infix for long name prefixes */
27482bc1ef4SJingbo Xu 	if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX)
27582bc1ef4SJingbo Xu 		return erofs_xattr_long_entrymatch(it, entry);
27682bc1ef4SJingbo Xu 
27782bc1ef4SJingbo Xu 	if (it->index != entry->e_name_index ||
27882bc1ef4SJingbo Xu 	    it->name.len != entry->e_name_len)
27982bc1ef4SJingbo Xu 		return -ENOATTR;
28082bc1ef4SJingbo Xu 	it->infix_len = 0;
28182bc1ef4SJingbo Xu 	return 0;
28247e4937aSGao Xiang }
28347e4937aSGao Xiang 
2848e823961SJingbo Xu static int xattr_namematch(struct erofs_xattr_iter *it,
28547e4937aSGao Xiang 			   unsigned int processed, char *buf, unsigned int len)
28647e4937aSGao Xiang {
28782bc1ef4SJingbo Xu 	if (memcmp(buf, it->name.name + it->infix_len + processed, len))
28882bc1ef4SJingbo Xu 		return -ENOATTR;
28982bc1ef4SJingbo Xu 	return 0;
29047e4937aSGao Xiang }
29147e4937aSGao Xiang 
2928e823961SJingbo Xu static int xattr_checkbuffer(struct erofs_xattr_iter *it,
29347e4937aSGao Xiang 			     unsigned int value_sz)
29447e4937aSGao Xiang {
29547e4937aSGao Xiang 	int err = it->buffer_size < value_sz ? -ERANGE : 0;
29647e4937aSGao Xiang 
2975a8ffb19SJingbo Xu 	it->buffer_ofs = value_sz;
29847e4937aSGao Xiang 	return !it->buffer ? 1 : err;
29947e4937aSGao Xiang }
30047e4937aSGao Xiang 
3018e823961SJingbo Xu static void xattr_copyvalue(struct erofs_xattr_iter *it,
30247e4937aSGao Xiang 			    unsigned int processed,
30347e4937aSGao Xiang 			    char *buf, unsigned int len)
30447e4937aSGao Xiang {
30547e4937aSGao Xiang 	memcpy(it->buffer + processed, buf, len);
30647e4937aSGao Xiang }
30747e4937aSGao Xiang 
30847e4937aSGao Xiang static const struct xattr_iter_handlers find_xattr_handlers = {
30947e4937aSGao Xiang 	.entry = xattr_entrymatch,
31047e4937aSGao Xiang 	.name = xattr_namematch,
31147e4937aSGao Xiang 	.alloc_buffer = xattr_checkbuffer,
31247e4937aSGao Xiang 	.value = xattr_copyvalue
31347e4937aSGao Xiang };
31447e4937aSGao Xiang 
31547e4937aSGao Xiang static bool erofs_xattr_user_list(struct dentry *dentry)
31647e4937aSGao Xiang {
317e6242465SGao Xiang 	return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
31847e4937aSGao Xiang }
31947e4937aSGao Xiang 
32047e4937aSGao Xiang static bool erofs_xattr_trusted_list(struct dentry *dentry)
32147e4937aSGao Xiang {
32247e4937aSGao Xiang 	return capable(CAP_SYS_ADMIN);
32347e4937aSGao Xiang }
32447e4937aSGao Xiang 
32547e4937aSGao Xiang static int erofs_xattr_generic_get(const struct xattr_handler *handler,
32647e4937aSGao Xiang 				   struct dentry *unused, struct inode *inode,
32747e4937aSGao Xiang 				   const char *name, void *buffer, size_t size)
32847e4937aSGao Xiang {
329b05f30bcSJingbo Xu 	if (handler->flags == EROFS_XATTR_INDEX_USER &&
330b05f30bcSJingbo Xu 	    !test_opt(&EROFS_I_SB(inode)->opt, XATTR_USER))
33147e4937aSGao Xiang 		return -EOPNOTSUPP;
33247e4937aSGao Xiang 
33347e4937aSGao Xiang 	return erofs_getxattr(inode, handler->flags, name, buffer, size);
33447e4937aSGao Xiang }
33547e4937aSGao Xiang 
33647e4937aSGao Xiang const struct xattr_handler erofs_xattr_user_handler = {
33747e4937aSGao Xiang 	.prefix	= XATTR_USER_PREFIX,
33847e4937aSGao Xiang 	.flags	= EROFS_XATTR_INDEX_USER,
33947e4937aSGao Xiang 	.list	= erofs_xattr_user_list,
34047e4937aSGao Xiang 	.get	= erofs_xattr_generic_get,
34147e4937aSGao Xiang };
34247e4937aSGao Xiang 
34347e4937aSGao Xiang const struct xattr_handler erofs_xattr_trusted_handler = {
34447e4937aSGao Xiang 	.prefix	= XATTR_TRUSTED_PREFIX,
34547e4937aSGao Xiang 	.flags	= EROFS_XATTR_INDEX_TRUSTED,
34647e4937aSGao Xiang 	.list	= erofs_xattr_trusted_list,
34747e4937aSGao Xiang 	.get	= erofs_xattr_generic_get,
34847e4937aSGao Xiang };
34947e4937aSGao Xiang 
35047e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_SECURITY
35147e4937aSGao Xiang const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
35247e4937aSGao Xiang 	.prefix	= XATTR_SECURITY_PREFIX,
35347e4937aSGao Xiang 	.flags	= EROFS_XATTR_INDEX_SECURITY,
35447e4937aSGao Xiang 	.get	= erofs_xattr_generic_get,
35547e4937aSGao Xiang };
35647e4937aSGao Xiang #endif
35747e4937aSGao Xiang 
35847e4937aSGao Xiang const struct xattr_handler *erofs_xattr_handlers[] = {
35947e4937aSGao Xiang 	&erofs_xattr_user_handler,
36047e4937aSGao Xiang 	&erofs_xattr_trusted_handler,
36147e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_SECURITY
36247e4937aSGao Xiang 	&erofs_xattr_security_handler,
36347e4937aSGao Xiang #endif
36447e4937aSGao Xiang 	NULL,
36547e4937aSGao Xiang };
36647e4937aSGao Xiang 
3678e823961SJingbo Xu static int xattr_entrylist(struct erofs_xattr_iter *it,
36847e4937aSGao Xiang 			   struct erofs_xattr_entry *entry)
36947e4937aSGao Xiang {
37082bc1ef4SJingbo Xu 	unsigned int base_index = entry->e_name_index;
37182bc1ef4SJingbo Xu 	unsigned int prefix_len, infix_len = 0;
37282bc1ef4SJingbo Xu 	const char *prefix, *infix = NULL;
37347e4937aSGao Xiang 
37482bc1ef4SJingbo Xu 	if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX) {
3758e823961SJingbo Xu 		struct erofs_sb_info *sbi = EROFS_SB(it->sb);
37682bc1ef4SJingbo Xu 		struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
37782bc1ef4SJingbo Xu 			(entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
37847e4937aSGao Xiang 
37982bc1ef4SJingbo Xu 		if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
38082bc1ef4SJingbo Xu 			return 1;
38182bc1ef4SJingbo Xu 		infix = pf->prefix->infix;
38282bc1ef4SJingbo Xu 		infix_len = pf->infix_len;
38382bc1ef4SJingbo Xu 		base_index = pf->prefix->base_index;
38482bc1ef4SJingbo Xu 	}
38582bc1ef4SJingbo Xu 
38661d325dcSLinus Torvalds 	prefix = erofs_xattr_prefix(base_index, it->dentry);
387a5488f29SChristian Brauner 	if (!prefix)
38847e4937aSGao Xiang 		return 1;
38947e4937aSGao Xiang 	prefix_len = strlen(prefix);
39047e4937aSGao Xiang 
39147e4937aSGao Xiang 	if (!it->buffer) {
39282bc1ef4SJingbo Xu 		it->buffer_ofs += prefix_len + infix_len +
39382bc1ef4SJingbo Xu 					entry->e_name_len + 1;
39447e4937aSGao Xiang 		return 1;
39547e4937aSGao Xiang 	}
39647e4937aSGao Xiang 
39782bc1ef4SJingbo Xu 	if (it->buffer_ofs + prefix_len + infix_len +
39847e4937aSGao Xiang 		+ entry->e_name_len + 1 > it->buffer_size)
39947e4937aSGao Xiang 		return -ERANGE;
40047e4937aSGao Xiang 
40147e4937aSGao Xiang 	memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
40282bc1ef4SJingbo Xu 	memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len);
40382bc1ef4SJingbo Xu 	it->buffer_ofs += prefix_len + infix_len;
40447e4937aSGao Xiang 	return 0;
40547e4937aSGao Xiang }
40647e4937aSGao Xiang 
4078e823961SJingbo Xu static int xattr_namelist(struct erofs_xattr_iter *it,
40847e4937aSGao Xiang 			  unsigned int processed, char *buf, unsigned int len)
40947e4937aSGao Xiang {
41047e4937aSGao Xiang 	memcpy(it->buffer + it->buffer_ofs, buf, len);
41147e4937aSGao Xiang 	it->buffer_ofs += len;
41247e4937aSGao Xiang 	return 0;
41347e4937aSGao Xiang }
41447e4937aSGao Xiang 
4158e823961SJingbo Xu static int xattr_skipvalue(struct erofs_xattr_iter *it,
41647e4937aSGao Xiang 			   unsigned int value_sz)
41747e4937aSGao Xiang {
41847e4937aSGao Xiang 	it->buffer[it->buffer_ofs++] = '\0';
41947e4937aSGao Xiang 	return 1;
42047e4937aSGao Xiang }
42147e4937aSGao Xiang 
42247e4937aSGao Xiang static const struct xattr_iter_handlers list_xattr_handlers = {
42347e4937aSGao Xiang 	.entry = xattr_entrylist,
42447e4937aSGao Xiang 	.name = xattr_namelist,
42547e4937aSGao Xiang 	.alloc_buffer = xattr_skipvalue,
42647e4937aSGao Xiang 	.value = NULL
42747e4937aSGao Xiang };
42847e4937aSGao Xiang 
429*4b077b50SJingbo Xu static int erofs_xattr_iter_inline(struct erofs_xattr_iter *it,
430*4b077b50SJingbo Xu 				   struct inode *inode, bool getxattr)
43147e4937aSGao Xiang {
432*4b077b50SJingbo Xu 	struct erofs_inode *const vi = EROFS_I(inode);
433*4b077b50SJingbo Xu 	const struct xattr_iter_handlers *op;
434*4b077b50SJingbo Xu 	unsigned int xattr_header_sz, remaining;
43547e4937aSGao Xiang 	int ret;
43647e4937aSGao Xiang 
437*4b077b50SJingbo Xu 	xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) +
438*4b077b50SJingbo Xu 			  sizeof(u32) * vi->xattr_shared_count;
439*4b077b50SJingbo Xu 	if (xattr_header_sz >= vi->xattr_isize) {
440*4b077b50SJingbo Xu 		DBG_BUGON(xattr_header_sz > vi->xattr_isize);
441*4b077b50SJingbo Xu 		return -ENOATTR;
442*4b077b50SJingbo Xu 	}
44347e4937aSGao Xiang 
444*4b077b50SJingbo Xu 	it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz;
445*4b077b50SJingbo Xu 	it->kaddr = erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos),
446*4b077b50SJingbo Xu 				EROFS_KMAP);
447*4b077b50SJingbo Xu 	if (IS_ERR(it->kaddr))
448*4b077b50SJingbo Xu 		return PTR_ERR(it->kaddr);
449*4b077b50SJingbo Xu 
450*4b077b50SJingbo Xu 	remaining = vi->xattr_isize - xattr_header_sz;
451*4b077b50SJingbo Xu 	op = getxattr ? &find_xattr_handlers : &list_xattr_handlers;
452*4b077b50SJingbo Xu 
45347e4937aSGao Xiang 	while (remaining) {
454*4b077b50SJingbo Xu 		ret = xattr_foreach(it, op, &remaining);
455*4b077b50SJingbo Xu 		if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
45647e4937aSGao Xiang 			break;
45747e4937aSGao Xiang 	}
458*4b077b50SJingbo Xu 	return ret;
45947e4937aSGao Xiang }
46047e4937aSGao Xiang 
461*4b077b50SJingbo Xu static int erofs_xattr_iter_shared(struct erofs_xattr_iter *it,
462*4b077b50SJingbo Xu 				   struct inode *inode, bool getxattr)
46347e4937aSGao Xiang {
464a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(inode);
4658e823961SJingbo Xu 	struct super_block *const sb = it->sb;
466eba67eb6SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
467eba67eb6SJingbo Xu 	unsigned int i;
468*4b077b50SJingbo Xu 	const struct xattr_iter_handlers *op;
469*4b077b50SJingbo Xu 	int ret = -ENOATTR;
470*4b077b50SJingbo Xu 
471*4b077b50SJingbo Xu 	op = getxattr ? &find_xattr_handlers : &list_xattr_handlers;
47247e4937aSGao Xiang 
47347e4937aSGao Xiang 	for (i = 0; i < vi->xattr_shared_count; ++i) {
4748e823961SJingbo Xu 		it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
475eba67eb6SJingbo Xu 				vi->xattr_shared_xattrs[i] * sizeof(__le32);
4768e823961SJingbo Xu 		it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
4778e823961SJingbo Xu 					EROFS_KMAP);
4788e823961SJingbo Xu 		if (IS_ERR(it->kaddr))
4798e823961SJingbo Xu 			return PTR_ERR(it->kaddr);
48047e4937aSGao Xiang 
481*4b077b50SJingbo Xu 		ret = xattr_foreach(it, op, NULL);
482*4b077b50SJingbo Xu 		if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
48347e4937aSGao Xiang 			break;
48447e4937aSGao Xiang 	}
485*4b077b50SJingbo Xu 	return ret;
48647e4937aSGao Xiang }
48747e4937aSGao Xiang 
488*4b077b50SJingbo Xu int erofs_getxattr(struct inode *inode, int index, const char *name,
489*4b077b50SJingbo Xu 		   void *buffer, size_t buffer_size)
49047e4937aSGao Xiang {
49147e4937aSGao Xiang 	int ret;
4928e823961SJingbo Xu 	struct erofs_xattr_iter it;
49347e4937aSGao Xiang 
494*4b077b50SJingbo Xu 	if (!name)
495*4b077b50SJingbo Xu 		return -EINVAL;
496*4b077b50SJingbo Xu 
497*4b077b50SJingbo Xu 	ret = erofs_init_inode_xattrs(inode);
498*4b077b50SJingbo Xu 	if (ret)
499*4b077b50SJingbo Xu 		return ret;
500*4b077b50SJingbo Xu 
501*4b077b50SJingbo Xu 	it.index = index;
502*4b077b50SJingbo Xu 	it.name = (struct qstr)QSTR_INIT(name, strlen(name));
503*4b077b50SJingbo Xu 	if (it.name.len > EROFS_NAME_LEN)
504*4b077b50SJingbo Xu 		return -ERANGE;
505*4b077b50SJingbo Xu 
506*4b077b50SJingbo Xu 	it.sb = inode->i_sb;
507*4b077b50SJingbo Xu 	it.buf = __EROFS_BUF_INITIALIZER;
508*4b077b50SJingbo Xu 	erofs_init_metabuf(&it.buf, it.sb);
509*4b077b50SJingbo Xu 	it.buffer = buffer;
510*4b077b50SJingbo Xu 	it.buffer_size = buffer_size;
511*4b077b50SJingbo Xu 	it.buffer_ofs = 0;
512*4b077b50SJingbo Xu 
513*4b077b50SJingbo Xu 	ret = erofs_xattr_iter_inline(&it, inode, true);
514*4b077b50SJingbo Xu 	if (ret == -ENOATTR)
515*4b077b50SJingbo Xu 		ret = erofs_xattr_iter_shared(&it, inode, true);
516*4b077b50SJingbo Xu 	erofs_put_metabuf(&it.buf);
517*4b077b50SJingbo Xu 	return ret ? ret : it.buffer_ofs;
518*4b077b50SJingbo Xu }
519*4b077b50SJingbo Xu 
520*4b077b50SJingbo Xu ssize_t erofs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
521*4b077b50SJingbo Xu {
522*4b077b50SJingbo Xu 	int ret;
523*4b077b50SJingbo Xu 	struct erofs_xattr_iter it;
524*4b077b50SJingbo Xu 	struct inode *inode = d_inode(dentry);
525*4b077b50SJingbo Xu 
526*4b077b50SJingbo Xu 	ret = erofs_init_inode_xattrs(inode);
527926d1650SGao Xiang 	if (ret == -ENOATTR)
528926d1650SGao Xiang 		return 0;
52947e4937aSGao Xiang 	if (ret)
53047e4937aSGao Xiang 		return ret;
53147e4937aSGao Xiang 
5328e823961SJingbo Xu 	it.sb = dentry->d_sb;
5338e823961SJingbo Xu 	it.buf = __EROFS_BUF_INITIALIZER;
5348e823961SJingbo Xu 	erofs_init_metabuf(&it.buf, it.sb);
53547e4937aSGao Xiang 	it.dentry = dentry;
53647e4937aSGao Xiang 	it.buffer = buffer;
53747e4937aSGao Xiang 	it.buffer_size = buffer_size;
53847e4937aSGao Xiang 	it.buffer_ofs = 0;
53947e4937aSGao Xiang 
540*4b077b50SJingbo Xu 	ret = erofs_xattr_iter_inline(&it, inode, false);
541*4b077b50SJingbo Xu 	if (!ret || ret == -ENOATTR)
542*4b077b50SJingbo Xu 		ret = erofs_xattr_iter_shared(&it, inode, false);
543*4b077b50SJingbo Xu 	if (ret == -ENOATTR)
544*4b077b50SJingbo Xu 		ret = 0;
5458e823961SJingbo Xu 	erofs_put_metabuf(&it.buf);
546*4b077b50SJingbo Xu 	return ret ? ret : it.buffer_ofs;
54747e4937aSGao Xiang }
54847e4937aSGao Xiang 
5499e382914SJingbo Xu void erofs_xattr_prefixes_cleanup(struct super_block *sb)
5509e382914SJingbo Xu {
5519e382914SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
5529e382914SJingbo Xu 	int i;
5539e382914SJingbo Xu 
5549e382914SJingbo Xu 	if (sbi->xattr_prefixes) {
5559e382914SJingbo Xu 		for (i = 0; i < sbi->xattr_prefix_count; i++)
5569e382914SJingbo Xu 			kfree(sbi->xattr_prefixes[i].prefix);
5579e382914SJingbo Xu 		kfree(sbi->xattr_prefixes);
5589e382914SJingbo Xu 		sbi->xattr_prefixes = NULL;
5599e382914SJingbo Xu 	}
5609e382914SJingbo Xu }
5619e382914SJingbo Xu 
5629e382914SJingbo Xu int erofs_xattr_prefixes_init(struct super_block *sb)
5639e382914SJingbo Xu {
5649e382914SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
5659e382914SJingbo Xu 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
5669e382914SJingbo Xu 	erofs_off_t pos = (erofs_off_t)sbi->xattr_prefix_start << 2;
5679e382914SJingbo Xu 	struct erofs_xattr_prefix_item *pfs;
5689e382914SJingbo Xu 	int ret = 0, i, len;
5699e382914SJingbo Xu 
5709e382914SJingbo Xu 	if (!sbi->xattr_prefix_count)
5719e382914SJingbo Xu 		return 0;
5729e382914SJingbo Xu 
5739e382914SJingbo Xu 	pfs = kzalloc(sbi->xattr_prefix_count * sizeof(*pfs), GFP_KERNEL);
5749e382914SJingbo Xu 	if (!pfs)
5759e382914SJingbo Xu 		return -ENOMEM;
5769e382914SJingbo Xu 
5770a17567bSJingbo Xu 	if (sbi->packed_inode)
5789e382914SJingbo Xu 		buf.inode = sbi->packed_inode;
5799e382914SJingbo Xu 	else
5809e382914SJingbo Xu 		erofs_init_metabuf(&buf, sb);
5819e382914SJingbo Xu 
5829e382914SJingbo Xu 	for (i = 0; i < sbi->xattr_prefix_count; i++) {
5839e382914SJingbo Xu 		void *ptr = erofs_read_metadata(sb, &buf, &pos, &len);
5849e382914SJingbo Xu 
5859e382914SJingbo Xu 		if (IS_ERR(ptr)) {
5869e382914SJingbo Xu 			ret = PTR_ERR(ptr);
5879e382914SJingbo Xu 			break;
5889e382914SJingbo Xu 		} else if (len < sizeof(*pfs->prefix) ||
5899e382914SJingbo Xu 			   len > EROFS_NAME_LEN + sizeof(*pfs->prefix)) {
5909e382914SJingbo Xu 			kfree(ptr);
5919e382914SJingbo Xu 			ret = -EFSCORRUPTED;
5929e382914SJingbo Xu 			break;
5939e382914SJingbo Xu 		}
5949e382914SJingbo Xu 		pfs[i].prefix = ptr;
5959e382914SJingbo Xu 		pfs[i].infix_len = len - sizeof(struct erofs_xattr_long_prefix);
5969e382914SJingbo Xu 	}
5979e382914SJingbo Xu 
5989e382914SJingbo Xu 	erofs_put_metabuf(&buf);
5999e382914SJingbo Xu 	sbi->xattr_prefixes = pfs;
6009e382914SJingbo Xu 	if (ret)
6019e382914SJingbo Xu 		erofs_xattr_prefixes_cleanup(sb);
6029e382914SJingbo Xu 	return ret;
6039e382914SJingbo Xu }
6049e382914SJingbo Xu 
60547e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_POSIX_ACL
6060cad6246SMiklos Szeredi struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
60747e4937aSGao Xiang {
60847e4937aSGao Xiang 	struct posix_acl *acl;
60947e4937aSGao Xiang 	int prefix, rc;
61047e4937aSGao Xiang 	char *value = NULL;
61147e4937aSGao Xiang 
6120cad6246SMiklos Szeredi 	if (rcu)
6130cad6246SMiklos Szeredi 		return ERR_PTR(-ECHILD);
6140cad6246SMiklos Szeredi 
61547e4937aSGao Xiang 	switch (type) {
61647e4937aSGao Xiang 	case ACL_TYPE_ACCESS:
61747e4937aSGao Xiang 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
61847e4937aSGao Xiang 		break;
61947e4937aSGao Xiang 	case ACL_TYPE_DEFAULT:
62047e4937aSGao Xiang 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
62147e4937aSGao Xiang 		break;
62247e4937aSGao Xiang 	default:
62347e4937aSGao Xiang 		return ERR_PTR(-EINVAL);
62447e4937aSGao Xiang 	}
62547e4937aSGao Xiang 
62647e4937aSGao Xiang 	rc = erofs_getxattr(inode, prefix, "", NULL, 0);
62747e4937aSGao Xiang 	if (rc > 0) {
62847e4937aSGao Xiang 		value = kmalloc(rc, GFP_KERNEL);
62947e4937aSGao Xiang 		if (!value)
63047e4937aSGao Xiang 			return ERR_PTR(-ENOMEM);
63147e4937aSGao Xiang 		rc = erofs_getxattr(inode, prefix, "", value, rc);
63247e4937aSGao Xiang 	}
63347e4937aSGao Xiang 
63447e4937aSGao Xiang 	if (rc == -ENOATTR)
63547e4937aSGao Xiang 		acl = NULL;
63647e4937aSGao Xiang 	else if (rc < 0)
63747e4937aSGao Xiang 		acl = ERR_PTR(rc);
63847e4937aSGao Xiang 	else
63947e4937aSGao Xiang 		acl = posix_acl_from_xattr(&init_user_ns, value, rc);
64047e4937aSGao Xiang 	kfree(value);
64147e4937aSGao Xiang 	return acl;
64247e4937aSGao Xiang }
64347e4937aSGao Xiang #endif
644