xref: /openbmc/linux/fs/erofs/xattr.c (revision 8e823961)
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 
10*8e823961SJingbo 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;
15*8e823961SJingbo Xu 
16*8e823961SJingbo Xu 	char *buffer;
17*8e823961SJingbo Xu 	int buffer_size, buffer_ofs;
18*8e823961SJingbo Xu 
19*8e823961SJingbo Xu 	/* getxattr */
20*8e823961SJingbo Xu 	int index, infix_len;
21*8e823961SJingbo Xu 	struct qstr name;
22*8e823961SJingbo Xu 
23*8e823961SJingbo Xu 	/* listxattr */
24*8e823961SJingbo 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);
30*8e823961SJingbo 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 {
134*8e823961SJingbo Xu 	int (*entry)(struct erofs_xattr_iter *it, struct erofs_xattr_entry *entry);
135*8e823961SJingbo Xu 	int (*name)(struct erofs_xattr_iter *it, unsigned int processed, char *buf,
13647e4937aSGao Xiang 		    unsigned int len);
137*8e823961SJingbo Xu 	int (*alloc_buffer)(struct erofs_xattr_iter *it, unsigned int value_sz);
138*8e823961SJingbo Xu 	void (*value)(struct erofs_xattr_iter *it, unsigned int processed, char *buf,
13947e4937aSGao Xiang 		      unsigned int len);
14047e4937aSGao Xiang };
14147e4937aSGao Xiang 
142*8e823961SJingbo Xu static int inline_xattr_iter_begin(struct erofs_xattr_iter *it,
14347e4937aSGao Xiang 				   struct inode *inode)
14447e4937aSGao Xiang {
145a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(inode);
146eba67eb6SJingbo Xu 	unsigned int xattr_header_sz;
14747e4937aSGao Xiang 
148399f36d8SJingbo Xu 	xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) +
149399f36d8SJingbo Xu 			  sizeof(u32) * vi->xattr_shared_count;
1508d8a09b0SGao Xiang 	if (xattr_header_sz >= vi->xattr_isize) {
15147e4937aSGao Xiang 		DBG_BUGON(xattr_header_sz > vi->xattr_isize);
15247e4937aSGao Xiang 		return -ENOATTR;
15347e4937aSGao Xiang 	}
15447e4937aSGao Xiang 
155eba67eb6SJingbo Xu 	it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz;
156eba67eb6SJingbo Xu 	it->kaddr = erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos),
157eba67eb6SJingbo Xu 				EROFS_KMAP);
158bb88e8daSGao Xiang 	if (IS_ERR(it->kaddr))
159bb88e8daSGao Xiang 		return PTR_ERR(it->kaddr);
16047e4937aSGao Xiang 	return vi->xattr_isize - xattr_header_sz;
16147e4937aSGao Xiang }
16247e4937aSGao Xiang 
16347e4937aSGao Xiang /*
16447e4937aSGao Xiang  * Regardless of success or failure, `xattr_foreach' will end up with
165eba67eb6SJingbo Xu  * `pos' pointing to the next xattr item rather than an arbitrary position.
16647e4937aSGao Xiang  */
167*8e823961SJingbo Xu static int xattr_foreach(struct erofs_xattr_iter *it,
16847e4937aSGao Xiang 			 const struct xattr_iter_handlers *op,
16947e4937aSGao Xiang 			 unsigned int *tlimit)
17047e4937aSGao Xiang {
17147e4937aSGao Xiang 	struct erofs_xattr_entry entry;
172eba67eb6SJingbo Xu 	struct super_block *sb = it->sb;
17347e4937aSGao Xiang 	unsigned int value_sz, processed, slice;
17447e4937aSGao Xiang 	int err;
17547e4937aSGao Xiang 
176eba67eb6SJingbo Xu 	/* 0. fixup blkaddr, pos */
177eba67eb6SJingbo Xu 	it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos), EROFS_KMAP);
178eba67eb6SJingbo Xu 	if (IS_ERR(it->kaddr))
179eba67eb6SJingbo Xu 		return PTR_ERR(it->kaddr);
18047e4937aSGao Xiang 
18147e4937aSGao Xiang 	/*
18247e4937aSGao Xiang 	 * 1. read xattr entry to the memory,
18347e4937aSGao Xiang 	 *    since we do EROFS_XATTR_ALIGN
18447e4937aSGao Xiang 	 *    therefore entry should be in the page
18547e4937aSGao Xiang 	 */
186eba67eb6SJingbo Xu 	entry = *(struct erofs_xattr_entry *)
187eba67eb6SJingbo Xu 		(it->kaddr + erofs_blkoff(sb, it->pos));
18847e4937aSGao Xiang 	if (tlimit) {
189b6796abdSGao Xiang 		unsigned int entry_sz = erofs_xattr_entry_size(&entry);
19047e4937aSGao Xiang 
19147e4937aSGao Xiang 		/* xattr on-disk corruption: xattr entry beyond xattr_isize */
1928d8a09b0SGao Xiang 		if (*tlimit < entry_sz) {
19347e4937aSGao Xiang 			DBG_BUGON(1);
19447e4937aSGao Xiang 			return -EFSCORRUPTED;
19547e4937aSGao Xiang 		}
19647e4937aSGao Xiang 		*tlimit -= entry_sz;
19747e4937aSGao Xiang 	}
19847e4937aSGao Xiang 
199eba67eb6SJingbo Xu 	it->pos += sizeof(struct erofs_xattr_entry);
20047e4937aSGao Xiang 	value_sz = le16_to_cpu(entry.e_value_size);
20147e4937aSGao Xiang 
20247e4937aSGao Xiang 	/* handle entry */
20347e4937aSGao Xiang 	err = op->entry(it, &entry);
20447e4937aSGao Xiang 	if (err) {
205eba67eb6SJingbo Xu 		it->pos += entry.e_name_len + value_sz;
20647e4937aSGao Xiang 		goto out;
20747e4937aSGao Xiang 	}
20847e4937aSGao Xiang 
209eba67eb6SJingbo Xu 	/* 2. handle xattr name (pos will finally be at the end of name) */
21047e4937aSGao Xiang 	processed = 0;
21147e4937aSGao Xiang 
21247e4937aSGao Xiang 	while (processed < entry.e_name_len) {
213eba67eb6SJingbo Xu 		it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
214eba67eb6SJingbo Xu 					EROFS_KMAP);
215eba67eb6SJingbo Xu 		if (IS_ERR(it->kaddr)) {
216eba67eb6SJingbo Xu 			err = PTR_ERR(it->kaddr);
21747e4937aSGao Xiang 			goto out;
21847e4937aSGao Xiang 		}
21947e4937aSGao Xiang 
220eba67eb6SJingbo Xu 		slice = min_t(unsigned int,
221eba67eb6SJingbo Xu 			      sb->s_blocksize - erofs_blkoff(sb, it->pos),
22247e4937aSGao Xiang 			      entry.e_name_len - processed);
22347e4937aSGao Xiang 
22447e4937aSGao Xiang 		/* handle name */
225eba67eb6SJingbo Xu 		err = op->name(it, processed,
226eba67eb6SJingbo Xu 			       it->kaddr + erofs_blkoff(sb, it->pos), slice);
22747e4937aSGao Xiang 		if (err) {
228eba67eb6SJingbo Xu 			it->pos += entry.e_name_len - processed + value_sz;
22947e4937aSGao Xiang 			goto out;
23047e4937aSGao Xiang 		}
23147e4937aSGao Xiang 
232eba67eb6SJingbo Xu 		it->pos += slice;
23347e4937aSGao Xiang 		processed += slice;
23447e4937aSGao Xiang 	}
23547e4937aSGao Xiang 
23647e4937aSGao Xiang 	/* 3. handle xattr value */
23747e4937aSGao Xiang 	processed = 0;
23847e4937aSGao Xiang 
23947e4937aSGao Xiang 	if (op->alloc_buffer) {
24047e4937aSGao Xiang 		err = op->alloc_buffer(it, value_sz);
24147e4937aSGao Xiang 		if (err) {
242eba67eb6SJingbo Xu 			it->pos += value_sz;
24347e4937aSGao Xiang 			goto out;
24447e4937aSGao Xiang 		}
24547e4937aSGao Xiang 	}
24647e4937aSGao Xiang 
24747e4937aSGao Xiang 	while (processed < value_sz) {
248eba67eb6SJingbo Xu 		it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
249eba67eb6SJingbo Xu 					EROFS_KMAP);
250eba67eb6SJingbo Xu 		if (IS_ERR(it->kaddr)) {
251eba67eb6SJingbo Xu 			err = PTR_ERR(it->kaddr);
25247e4937aSGao Xiang 			goto out;
25347e4937aSGao Xiang 		}
25447e4937aSGao Xiang 
255eba67eb6SJingbo Xu 		slice = min_t(unsigned int,
256eba67eb6SJingbo Xu 			      sb->s_blocksize - erofs_blkoff(sb, it->pos),
25747e4937aSGao Xiang 			      value_sz - processed);
258eba67eb6SJingbo Xu 		op->value(it, processed, it->kaddr + erofs_blkoff(sb, it->pos),
259eba67eb6SJingbo Xu 			  slice);
260eba67eb6SJingbo Xu 		it->pos += slice;
26147e4937aSGao Xiang 		processed += slice;
26247e4937aSGao Xiang 	}
26347e4937aSGao Xiang 
26447e4937aSGao Xiang out:
26547e4937aSGao Xiang 	/* xattrs should be 4-byte aligned (on-disk constraint) */
266eba67eb6SJingbo Xu 	it->pos = EROFS_XATTR_ALIGN(it->pos);
26747e4937aSGao Xiang 	return err < 0 ? err : 0;
26847e4937aSGao Xiang }
26947e4937aSGao Xiang 
270*8e823961SJingbo Xu static int erofs_xattr_long_entrymatch(struct erofs_xattr_iter *it,
27182bc1ef4SJingbo Xu 				       struct erofs_xattr_entry *entry)
27282bc1ef4SJingbo Xu {
273*8e823961SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(it->sb);
27482bc1ef4SJingbo Xu 	struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
27582bc1ef4SJingbo Xu 		(entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
27682bc1ef4SJingbo Xu 
27782bc1ef4SJingbo Xu 	if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
27882bc1ef4SJingbo Xu 		return -ENOATTR;
27982bc1ef4SJingbo Xu 
28082bc1ef4SJingbo Xu 	if (it->index != pf->prefix->base_index ||
28182bc1ef4SJingbo Xu 	    it->name.len != entry->e_name_len + pf->infix_len)
28282bc1ef4SJingbo Xu 		return -ENOATTR;
28382bc1ef4SJingbo Xu 
28482bc1ef4SJingbo Xu 	if (memcmp(it->name.name, pf->prefix->infix, pf->infix_len))
28582bc1ef4SJingbo Xu 		return -ENOATTR;
28682bc1ef4SJingbo Xu 
28782bc1ef4SJingbo Xu 	it->infix_len = pf->infix_len;
28882bc1ef4SJingbo Xu 	return 0;
28982bc1ef4SJingbo Xu }
29082bc1ef4SJingbo Xu 
291*8e823961SJingbo Xu static int xattr_entrymatch(struct erofs_xattr_iter *it,
29247e4937aSGao Xiang 			    struct erofs_xattr_entry *entry)
29347e4937aSGao Xiang {
29482bc1ef4SJingbo Xu 	/* should also match the infix for long name prefixes */
29582bc1ef4SJingbo Xu 	if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX)
29682bc1ef4SJingbo Xu 		return erofs_xattr_long_entrymatch(it, entry);
29782bc1ef4SJingbo Xu 
29882bc1ef4SJingbo Xu 	if (it->index != entry->e_name_index ||
29982bc1ef4SJingbo Xu 	    it->name.len != entry->e_name_len)
30082bc1ef4SJingbo Xu 		return -ENOATTR;
30182bc1ef4SJingbo Xu 	it->infix_len = 0;
30282bc1ef4SJingbo Xu 	return 0;
30347e4937aSGao Xiang }
30447e4937aSGao Xiang 
305*8e823961SJingbo Xu static int xattr_namematch(struct erofs_xattr_iter *it,
30647e4937aSGao Xiang 			   unsigned int processed, char *buf, unsigned int len)
30747e4937aSGao Xiang {
30882bc1ef4SJingbo Xu 	if (memcmp(buf, it->name.name + it->infix_len + processed, len))
30982bc1ef4SJingbo Xu 		return -ENOATTR;
31082bc1ef4SJingbo Xu 	return 0;
31147e4937aSGao Xiang }
31247e4937aSGao Xiang 
313*8e823961SJingbo Xu static int xattr_checkbuffer(struct erofs_xattr_iter *it,
31447e4937aSGao Xiang 			     unsigned int value_sz)
31547e4937aSGao Xiang {
31647e4937aSGao Xiang 	int err = it->buffer_size < value_sz ? -ERANGE : 0;
31747e4937aSGao Xiang 
31847e4937aSGao Xiang 	it->buffer_size = value_sz;
31947e4937aSGao Xiang 	return !it->buffer ? 1 : err;
32047e4937aSGao Xiang }
32147e4937aSGao Xiang 
322*8e823961SJingbo Xu static void xattr_copyvalue(struct erofs_xattr_iter *it,
32347e4937aSGao Xiang 			    unsigned int processed,
32447e4937aSGao Xiang 			    char *buf, unsigned int len)
32547e4937aSGao Xiang {
32647e4937aSGao Xiang 	memcpy(it->buffer + processed, buf, len);
32747e4937aSGao Xiang }
32847e4937aSGao Xiang 
32947e4937aSGao Xiang static const struct xattr_iter_handlers find_xattr_handlers = {
33047e4937aSGao Xiang 	.entry = xattr_entrymatch,
33147e4937aSGao Xiang 	.name = xattr_namematch,
33247e4937aSGao Xiang 	.alloc_buffer = xattr_checkbuffer,
33347e4937aSGao Xiang 	.value = xattr_copyvalue
33447e4937aSGao Xiang };
33547e4937aSGao Xiang 
336*8e823961SJingbo Xu static int inline_getxattr(struct inode *inode, struct erofs_xattr_iter *it)
33747e4937aSGao Xiang {
33847e4937aSGao Xiang 	int ret;
33947e4937aSGao Xiang 	unsigned int remaining;
34047e4937aSGao Xiang 
341*8e823961SJingbo Xu 	ret = inline_xattr_iter_begin(it, inode);
34247e4937aSGao Xiang 	if (ret < 0)
34347e4937aSGao Xiang 		return ret;
34447e4937aSGao Xiang 
34547e4937aSGao Xiang 	remaining = ret;
34647e4937aSGao Xiang 	while (remaining) {
347*8e823961SJingbo Xu 		ret = xattr_foreach(it, &find_xattr_handlers, &remaining);
34847e4937aSGao Xiang 		if (ret != -ENOATTR)
34947e4937aSGao Xiang 			break;
35047e4937aSGao Xiang 	}
35147e4937aSGao Xiang 	return ret ? ret : it->buffer_size;
35247e4937aSGao Xiang }
35347e4937aSGao Xiang 
354*8e823961SJingbo Xu static int shared_getxattr(struct inode *inode, struct erofs_xattr_iter *it)
35547e4937aSGao Xiang {
356a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(inode);
357*8e823961SJingbo Xu 	struct super_block *const sb = it->sb;
358eba67eb6SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
359eba67eb6SJingbo Xu 	unsigned int i;
36047e4937aSGao Xiang 	int ret = -ENOATTR;
36147e4937aSGao Xiang 
36247e4937aSGao Xiang 	for (i = 0; i < vi->xattr_shared_count; ++i) {
363*8e823961SJingbo Xu 		it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
364eba67eb6SJingbo Xu 				vi->xattr_shared_xattrs[i] * sizeof(__le32);
365*8e823961SJingbo Xu 		it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
366*8e823961SJingbo Xu 					EROFS_KMAP);
367*8e823961SJingbo Xu 		if (IS_ERR(it->kaddr))
368*8e823961SJingbo Xu 			return PTR_ERR(it->kaddr);
36947e4937aSGao Xiang 
370*8e823961SJingbo Xu 		ret = xattr_foreach(it, &find_xattr_handlers, NULL);
37147e4937aSGao Xiang 		if (ret != -ENOATTR)
37247e4937aSGao Xiang 			break;
37347e4937aSGao Xiang 	}
37447e4937aSGao Xiang 	return ret ? ret : it->buffer_size;
37547e4937aSGao Xiang }
37647e4937aSGao Xiang 
37747e4937aSGao Xiang static bool erofs_xattr_user_list(struct dentry *dentry)
37847e4937aSGao Xiang {
379e6242465SGao Xiang 	return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
38047e4937aSGao Xiang }
38147e4937aSGao Xiang 
38247e4937aSGao Xiang static bool erofs_xattr_trusted_list(struct dentry *dentry)
38347e4937aSGao Xiang {
38447e4937aSGao Xiang 	return capable(CAP_SYS_ADMIN);
38547e4937aSGao Xiang }
38647e4937aSGao Xiang 
38747e4937aSGao Xiang int erofs_getxattr(struct inode *inode, int index,
38847e4937aSGao Xiang 		   const char *name,
38947e4937aSGao Xiang 		   void *buffer, size_t buffer_size)
39047e4937aSGao Xiang {
39147e4937aSGao Xiang 	int ret;
392*8e823961SJingbo Xu 	struct erofs_xattr_iter it;
39347e4937aSGao Xiang 
3948d8a09b0SGao Xiang 	if (!name)
39547e4937aSGao Xiang 		return -EINVAL;
39647e4937aSGao Xiang 
3976020ffe7SJingbo Xu 	ret = erofs_init_inode_xattrs(inode);
39847e4937aSGao Xiang 	if (ret)
39947e4937aSGao Xiang 		return ret;
40047e4937aSGao Xiang 
40147e4937aSGao Xiang 	it.index = index;
402*8e823961SJingbo Xu 	it.name = (struct qstr)QSTR_INIT(name, strlen(name));
40347e4937aSGao Xiang 	if (it.name.len > EROFS_NAME_LEN)
40447e4937aSGao Xiang 		return -ERANGE;
405bb88e8daSGao Xiang 
406*8e823961SJingbo Xu 	it.sb = inode->i_sb;
407*8e823961SJingbo Xu 	it.buf = __EROFS_BUF_INITIALIZER;
408*8e823961SJingbo Xu 	erofs_init_metabuf(&it.buf, it.sb);
40947e4937aSGao Xiang 	it.buffer = buffer;
41047e4937aSGao Xiang 	it.buffer_size = buffer_size;
411*8e823961SJingbo Xu 	it.buffer_ofs = 0;
41247e4937aSGao Xiang 
41347e4937aSGao Xiang 	ret = inline_getxattr(inode, &it);
41447e4937aSGao Xiang 	if (ret == -ENOATTR)
41547e4937aSGao Xiang 		ret = shared_getxattr(inode, &it);
416*8e823961SJingbo Xu 	erofs_put_metabuf(&it.buf);
41747e4937aSGao Xiang 	return ret;
41847e4937aSGao Xiang }
41947e4937aSGao Xiang 
42047e4937aSGao Xiang static int erofs_xattr_generic_get(const struct xattr_handler *handler,
42147e4937aSGao Xiang 				   struct dentry *unused, struct inode *inode,
42247e4937aSGao Xiang 				   const char *name, void *buffer, size_t size)
42347e4937aSGao Xiang {
424b05f30bcSJingbo Xu 	if (handler->flags == EROFS_XATTR_INDEX_USER &&
425b05f30bcSJingbo Xu 	    !test_opt(&EROFS_I_SB(inode)->opt, XATTR_USER))
42647e4937aSGao Xiang 		return -EOPNOTSUPP;
42747e4937aSGao Xiang 
42847e4937aSGao Xiang 	return erofs_getxattr(inode, handler->flags, name, buffer, size);
42947e4937aSGao Xiang }
43047e4937aSGao Xiang 
43147e4937aSGao Xiang const struct xattr_handler erofs_xattr_user_handler = {
43247e4937aSGao Xiang 	.prefix	= XATTR_USER_PREFIX,
43347e4937aSGao Xiang 	.flags	= EROFS_XATTR_INDEX_USER,
43447e4937aSGao Xiang 	.list	= erofs_xattr_user_list,
43547e4937aSGao Xiang 	.get	= erofs_xattr_generic_get,
43647e4937aSGao Xiang };
43747e4937aSGao Xiang 
43847e4937aSGao Xiang const struct xattr_handler erofs_xattr_trusted_handler = {
43947e4937aSGao Xiang 	.prefix	= XATTR_TRUSTED_PREFIX,
44047e4937aSGao Xiang 	.flags	= EROFS_XATTR_INDEX_TRUSTED,
44147e4937aSGao Xiang 	.list	= erofs_xattr_trusted_list,
44247e4937aSGao Xiang 	.get	= erofs_xattr_generic_get,
44347e4937aSGao Xiang };
44447e4937aSGao Xiang 
44547e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_SECURITY
44647e4937aSGao Xiang const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
44747e4937aSGao Xiang 	.prefix	= XATTR_SECURITY_PREFIX,
44847e4937aSGao Xiang 	.flags	= EROFS_XATTR_INDEX_SECURITY,
44947e4937aSGao Xiang 	.get	= erofs_xattr_generic_get,
45047e4937aSGao Xiang };
45147e4937aSGao Xiang #endif
45247e4937aSGao Xiang 
45347e4937aSGao Xiang const struct xattr_handler *erofs_xattr_handlers[] = {
45447e4937aSGao Xiang 	&erofs_xattr_user_handler,
45547e4937aSGao Xiang 	&erofs_xattr_trusted_handler,
45647e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_SECURITY
45747e4937aSGao Xiang 	&erofs_xattr_security_handler,
45847e4937aSGao Xiang #endif
45947e4937aSGao Xiang 	NULL,
46047e4937aSGao Xiang };
46147e4937aSGao Xiang 
462*8e823961SJingbo Xu static int xattr_entrylist(struct erofs_xattr_iter *it,
46347e4937aSGao Xiang 			   struct erofs_xattr_entry *entry)
46447e4937aSGao Xiang {
46582bc1ef4SJingbo Xu 	unsigned int base_index = entry->e_name_index;
46682bc1ef4SJingbo Xu 	unsigned int prefix_len, infix_len = 0;
46782bc1ef4SJingbo Xu 	const char *prefix, *infix = NULL;
46847e4937aSGao Xiang 
46982bc1ef4SJingbo Xu 	if (entry->e_name_index & EROFS_XATTR_LONG_PREFIX) {
470*8e823961SJingbo Xu 		struct erofs_sb_info *sbi = EROFS_SB(it->sb);
47182bc1ef4SJingbo Xu 		struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
47282bc1ef4SJingbo Xu 			(entry->e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
47347e4937aSGao Xiang 
47482bc1ef4SJingbo Xu 		if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
47582bc1ef4SJingbo Xu 			return 1;
47682bc1ef4SJingbo Xu 		infix = pf->prefix->infix;
47782bc1ef4SJingbo Xu 		infix_len = pf->infix_len;
47882bc1ef4SJingbo Xu 		base_index = pf->prefix->base_index;
47982bc1ef4SJingbo Xu 	}
48082bc1ef4SJingbo Xu 
48161d325dcSLinus Torvalds 	prefix = erofs_xattr_prefix(base_index, it->dentry);
482a5488f29SChristian Brauner 	if (!prefix)
48347e4937aSGao Xiang 		return 1;
48447e4937aSGao Xiang 	prefix_len = strlen(prefix);
48547e4937aSGao Xiang 
48647e4937aSGao Xiang 	if (!it->buffer) {
48782bc1ef4SJingbo Xu 		it->buffer_ofs += prefix_len + infix_len +
48882bc1ef4SJingbo Xu 					entry->e_name_len + 1;
48947e4937aSGao Xiang 		return 1;
49047e4937aSGao Xiang 	}
49147e4937aSGao Xiang 
49282bc1ef4SJingbo Xu 	if (it->buffer_ofs + prefix_len + infix_len +
49347e4937aSGao Xiang 		+ entry->e_name_len + 1 > it->buffer_size)
49447e4937aSGao Xiang 		return -ERANGE;
49547e4937aSGao Xiang 
49647e4937aSGao Xiang 	memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
49782bc1ef4SJingbo Xu 	memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len);
49882bc1ef4SJingbo Xu 	it->buffer_ofs += prefix_len + infix_len;
49947e4937aSGao Xiang 	return 0;
50047e4937aSGao Xiang }
50147e4937aSGao Xiang 
502*8e823961SJingbo Xu static int xattr_namelist(struct erofs_xattr_iter *it,
50347e4937aSGao Xiang 			  unsigned int processed, char *buf, unsigned int len)
50447e4937aSGao Xiang {
50547e4937aSGao Xiang 	memcpy(it->buffer + it->buffer_ofs, buf, len);
50647e4937aSGao Xiang 	it->buffer_ofs += len;
50747e4937aSGao Xiang 	return 0;
50847e4937aSGao Xiang }
50947e4937aSGao Xiang 
510*8e823961SJingbo Xu static int xattr_skipvalue(struct erofs_xattr_iter *it,
51147e4937aSGao Xiang 			   unsigned int value_sz)
51247e4937aSGao Xiang {
51347e4937aSGao Xiang 	it->buffer[it->buffer_ofs++] = '\0';
51447e4937aSGao Xiang 	return 1;
51547e4937aSGao Xiang }
51647e4937aSGao Xiang 
51747e4937aSGao Xiang static const struct xattr_iter_handlers list_xattr_handlers = {
51847e4937aSGao Xiang 	.entry = xattr_entrylist,
51947e4937aSGao Xiang 	.name = xattr_namelist,
52047e4937aSGao Xiang 	.alloc_buffer = xattr_skipvalue,
52147e4937aSGao Xiang 	.value = NULL
52247e4937aSGao Xiang };
52347e4937aSGao Xiang 
524*8e823961SJingbo Xu static int inline_listxattr(struct erofs_xattr_iter *it)
52547e4937aSGao Xiang {
52647e4937aSGao Xiang 	int ret;
52747e4937aSGao Xiang 	unsigned int remaining;
52847e4937aSGao Xiang 
529*8e823961SJingbo Xu 	ret = inline_xattr_iter_begin(it, d_inode(it->dentry));
53047e4937aSGao Xiang 	if (ret < 0)
53147e4937aSGao Xiang 		return ret;
53247e4937aSGao Xiang 
53347e4937aSGao Xiang 	remaining = ret;
53447e4937aSGao Xiang 	while (remaining) {
535*8e823961SJingbo Xu 		ret = xattr_foreach(it, &list_xattr_handlers, &remaining);
53647e4937aSGao Xiang 		if (ret)
53747e4937aSGao Xiang 			break;
53847e4937aSGao Xiang 	}
53947e4937aSGao Xiang 	return ret ? ret : it->buffer_ofs;
54047e4937aSGao Xiang }
54147e4937aSGao Xiang 
542*8e823961SJingbo Xu static int shared_listxattr(struct erofs_xattr_iter *it)
54347e4937aSGao Xiang {
54447e4937aSGao Xiang 	struct inode *const inode = d_inode(it->dentry);
545a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(inode);
546*8e823961SJingbo Xu 	struct super_block *const sb = it->sb;
547eba67eb6SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
548eba67eb6SJingbo Xu 	unsigned int i;
54947e4937aSGao Xiang 	int ret = 0;
55047e4937aSGao Xiang 
55147e4937aSGao Xiang 	for (i = 0; i < vi->xattr_shared_count; ++i) {
552*8e823961SJingbo Xu 		it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
553eba67eb6SJingbo Xu 				vi->xattr_shared_xattrs[i] * sizeof(__le32);
554*8e823961SJingbo Xu 		it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
555*8e823961SJingbo Xu 					EROFS_KMAP);
556*8e823961SJingbo Xu 		if (IS_ERR(it->kaddr))
557*8e823961SJingbo Xu 			return PTR_ERR(it->kaddr);
55847e4937aSGao Xiang 
559*8e823961SJingbo Xu 		ret = xattr_foreach(it, &list_xattr_handlers, NULL);
56047e4937aSGao Xiang 		if (ret)
56147e4937aSGao Xiang 			break;
56247e4937aSGao Xiang 	}
56347e4937aSGao Xiang 	return ret ? ret : it->buffer_ofs;
56447e4937aSGao Xiang }
56547e4937aSGao Xiang 
56647e4937aSGao Xiang ssize_t erofs_listxattr(struct dentry *dentry,
56747e4937aSGao Xiang 			char *buffer, size_t buffer_size)
56847e4937aSGao Xiang {
56947e4937aSGao Xiang 	int ret;
570*8e823961SJingbo Xu 	struct erofs_xattr_iter it;
57147e4937aSGao Xiang 
5726020ffe7SJingbo Xu 	ret = erofs_init_inode_xattrs(d_inode(dentry));
573926d1650SGao Xiang 	if (ret == -ENOATTR)
574926d1650SGao Xiang 		return 0;
57547e4937aSGao Xiang 	if (ret)
57647e4937aSGao Xiang 		return ret;
57747e4937aSGao Xiang 
578*8e823961SJingbo Xu 	it.sb = dentry->d_sb;
579*8e823961SJingbo Xu 	it.buf = __EROFS_BUF_INITIALIZER;
580*8e823961SJingbo Xu 	erofs_init_metabuf(&it.buf, it.sb);
58147e4937aSGao Xiang 	it.dentry = dentry;
58247e4937aSGao Xiang 	it.buffer = buffer;
58347e4937aSGao Xiang 	it.buffer_size = buffer_size;
58447e4937aSGao Xiang 	it.buffer_ofs = 0;
58547e4937aSGao Xiang 
58647e4937aSGao Xiang 	ret = inline_listxattr(&it);
587bb88e8daSGao Xiang 	if (ret >= 0 || ret == -ENOATTR)
588bb88e8daSGao Xiang 		ret = shared_listxattr(&it);
589*8e823961SJingbo Xu 	erofs_put_metabuf(&it.buf);
59047e4937aSGao Xiang 	return ret;
59147e4937aSGao Xiang }
59247e4937aSGao Xiang 
5939e382914SJingbo Xu void erofs_xattr_prefixes_cleanup(struct super_block *sb)
5949e382914SJingbo Xu {
5959e382914SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
5969e382914SJingbo Xu 	int i;
5979e382914SJingbo Xu 
5989e382914SJingbo Xu 	if (sbi->xattr_prefixes) {
5999e382914SJingbo Xu 		for (i = 0; i < sbi->xattr_prefix_count; i++)
6009e382914SJingbo Xu 			kfree(sbi->xattr_prefixes[i].prefix);
6019e382914SJingbo Xu 		kfree(sbi->xattr_prefixes);
6029e382914SJingbo Xu 		sbi->xattr_prefixes = NULL;
6039e382914SJingbo Xu 	}
6049e382914SJingbo Xu }
6059e382914SJingbo Xu 
6069e382914SJingbo Xu int erofs_xattr_prefixes_init(struct super_block *sb)
6079e382914SJingbo Xu {
6089e382914SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
6099e382914SJingbo Xu 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
6109e382914SJingbo Xu 	erofs_off_t pos = (erofs_off_t)sbi->xattr_prefix_start << 2;
6119e382914SJingbo Xu 	struct erofs_xattr_prefix_item *pfs;
6129e382914SJingbo Xu 	int ret = 0, i, len;
6139e382914SJingbo Xu 
6149e382914SJingbo Xu 	if (!sbi->xattr_prefix_count)
6159e382914SJingbo Xu 		return 0;
6169e382914SJingbo Xu 
6179e382914SJingbo Xu 	pfs = kzalloc(sbi->xattr_prefix_count * sizeof(*pfs), GFP_KERNEL);
6189e382914SJingbo Xu 	if (!pfs)
6199e382914SJingbo Xu 		return -ENOMEM;
6209e382914SJingbo Xu 
6210a17567bSJingbo Xu 	if (sbi->packed_inode)
6229e382914SJingbo Xu 		buf.inode = sbi->packed_inode;
6239e382914SJingbo Xu 	else
6249e382914SJingbo Xu 		erofs_init_metabuf(&buf, sb);
6259e382914SJingbo Xu 
6269e382914SJingbo Xu 	for (i = 0; i < sbi->xattr_prefix_count; i++) {
6279e382914SJingbo Xu 		void *ptr = erofs_read_metadata(sb, &buf, &pos, &len);
6289e382914SJingbo Xu 
6299e382914SJingbo Xu 		if (IS_ERR(ptr)) {
6309e382914SJingbo Xu 			ret = PTR_ERR(ptr);
6319e382914SJingbo Xu 			break;
6329e382914SJingbo Xu 		} else if (len < sizeof(*pfs->prefix) ||
6339e382914SJingbo Xu 			   len > EROFS_NAME_LEN + sizeof(*pfs->prefix)) {
6349e382914SJingbo Xu 			kfree(ptr);
6359e382914SJingbo Xu 			ret = -EFSCORRUPTED;
6369e382914SJingbo Xu 			break;
6379e382914SJingbo Xu 		}
6389e382914SJingbo Xu 		pfs[i].prefix = ptr;
6399e382914SJingbo Xu 		pfs[i].infix_len = len - sizeof(struct erofs_xattr_long_prefix);
6409e382914SJingbo Xu 	}
6419e382914SJingbo Xu 
6429e382914SJingbo Xu 	erofs_put_metabuf(&buf);
6439e382914SJingbo Xu 	sbi->xattr_prefixes = pfs;
6449e382914SJingbo Xu 	if (ret)
6459e382914SJingbo Xu 		erofs_xattr_prefixes_cleanup(sb);
6469e382914SJingbo Xu 	return ret;
6479e382914SJingbo Xu }
6489e382914SJingbo Xu 
64947e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_POSIX_ACL
6500cad6246SMiklos Szeredi struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
65147e4937aSGao Xiang {
65247e4937aSGao Xiang 	struct posix_acl *acl;
65347e4937aSGao Xiang 	int prefix, rc;
65447e4937aSGao Xiang 	char *value = NULL;
65547e4937aSGao Xiang 
6560cad6246SMiklos Szeredi 	if (rcu)
6570cad6246SMiklos Szeredi 		return ERR_PTR(-ECHILD);
6580cad6246SMiklos Szeredi 
65947e4937aSGao Xiang 	switch (type) {
66047e4937aSGao Xiang 	case ACL_TYPE_ACCESS:
66147e4937aSGao Xiang 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
66247e4937aSGao Xiang 		break;
66347e4937aSGao Xiang 	case ACL_TYPE_DEFAULT:
66447e4937aSGao Xiang 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
66547e4937aSGao Xiang 		break;
66647e4937aSGao Xiang 	default:
66747e4937aSGao Xiang 		return ERR_PTR(-EINVAL);
66847e4937aSGao Xiang 	}
66947e4937aSGao Xiang 
67047e4937aSGao Xiang 	rc = erofs_getxattr(inode, prefix, "", NULL, 0);
67147e4937aSGao Xiang 	if (rc > 0) {
67247e4937aSGao Xiang 		value = kmalloc(rc, GFP_KERNEL);
67347e4937aSGao Xiang 		if (!value)
67447e4937aSGao Xiang 			return ERR_PTR(-ENOMEM);
67547e4937aSGao Xiang 		rc = erofs_getxattr(inode, prefix, "", value, rc);
67647e4937aSGao Xiang 	}
67747e4937aSGao Xiang 
67847e4937aSGao Xiang 	if (rc == -ENOATTR)
67947e4937aSGao Xiang 		acl = NULL;
68047e4937aSGao Xiang 	else if (rc < 0)
68147e4937aSGao Xiang 		acl = ERR_PTR(rc);
68247e4937aSGao Xiang 	else
68347e4937aSGao Xiang 		acl = posix_acl_from_xattr(&init_user_ns, value, rc);
68447e4937aSGao Xiang 	kfree(value);
68547e4937aSGao Xiang 	return acl;
68647e4937aSGao Xiang }
68747e4937aSGao Xiang #endif
688