xref: /openbmc/linux/fs/erofs/xattr.c (revision f02615eb)
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 static bool erofs_xattr_user_list(struct dentry *dentry)
12747e4937aSGao Xiang {
128e6242465SGao Xiang 	return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
12947e4937aSGao Xiang }
13047e4937aSGao Xiang 
13147e4937aSGao Xiang static bool erofs_xattr_trusted_list(struct dentry *dentry)
13247e4937aSGao Xiang {
13347e4937aSGao Xiang 	return capable(CAP_SYS_ADMIN);
13447e4937aSGao Xiang }
13547e4937aSGao Xiang 
13647e4937aSGao Xiang static int erofs_xattr_generic_get(const struct xattr_handler *handler,
13747e4937aSGao Xiang 				   struct dentry *unused, struct inode *inode,
13847e4937aSGao Xiang 				   const char *name, void *buffer, size_t size)
13947e4937aSGao Xiang {
140b05f30bcSJingbo Xu 	if (handler->flags == EROFS_XATTR_INDEX_USER &&
141b05f30bcSJingbo Xu 	    !test_opt(&EROFS_I_SB(inode)->opt, XATTR_USER))
14247e4937aSGao Xiang 		return -EOPNOTSUPP;
14347e4937aSGao Xiang 
14447e4937aSGao Xiang 	return erofs_getxattr(inode, handler->flags, name, buffer, size);
14547e4937aSGao Xiang }
14647e4937aSGao Xiang 
14747e4937aSGao Xiang const struct xattr_handler erofs_xattr_user_handler = {
14847e4937aSGao Xiang 	.prefix	= XATTR_USER_PREFIX,
14947e4937aSGao Xiang 	.flags	= EROFS_XATTR_INDEX_USER,
15047e4937aSGao Xiang 	.list	= erofs_xattr_user_list,
15147e4937aSGao Xiang 	.get	= erofs_xattr_generic_get,
15247e4937aSGao Xiang };
15347e4937aSGao Xiang 
15447e4937aSGao Xiang const struct xattr_handler erofs_xattr_trusted_handler = {
15547e4937aSGao Xiang 	.prefix	= XATTR_TRUSTED_PREFIX,
15647e4937aSGao Xiang 	.flags	= EROFS_XATTR_INDEX_TRUSTED,
15747e4937aSGao Xiang 	.list	= erofs_xattr_trusted_list,
15847e4937aSGao Xiang 	.get	= erofs_xattr_generic_get,
15947e4937aSGao Xiang };
16047e4937aSGao Xiang 
16147e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_SECURITY
16247e4937aSGao Xiang const struct xattr_handler __maybe_unused erofs_xattr_security_handler = {
16347e4937aSGao Xiang 	.prefix	= XATTR_SECURITY_PREFIX,
16447e4937aSGao Xiang 	.flags	= EROFS_XATTR_INDEX_SECURITY,
16547e4937aSGao Xiang 	.get	= erofs_xattr_generic_get,
16647e4937aSGao Xiang };
16747e4937aSGao Xiang #endif
16847e4937aSGao Xiang 
16947e4937aSGao Xiang const struct xattr_handler *erofs_xattr_handlers[] = {
17047e4937aSGao Xiang 	&erofs_xattr_user_handler,
17147e4937aSGao Xiang 	&erofs_xattr_trusted_handler,
17247e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_SECURITY
17347e4937aSGao Xiang 	&erofs_xattr_security_handler,
17447e4937aSGao Xiang #endif
17547e4937aSGao Xiang 	NULL,
17647e4937aSGao Xiang };
17747e4937aSGao Xiang 
178*f02615ebSJingbo Xu static int erofs_xattr_copy_to_buffer(struct erofs_xattr_iter *it,
179*f02615ebSJingbo Xu 				      unsigned int len)
18047e4937aSGao Xiang {
181*f02615ebSJingbo Xu 	unsigned int slice, processed;
182*f02615ebSJingbo Xu 	struct super_block *sb = it->sb;
183*f02615ebSJingbo Xu 	void *src;
18447e4937aSGao Xiang 
185*f02615ebSJingbo Xu 	for (processed = 0; processed < len; processed += slice) {
186*f02615ebSJingbo Xu 		it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
187*f02615ebSJingbo Xu 					EROFS_KMAP);
188*f02615ebSJingbo Xu 		if (IS_ERR(it->kaddr))
189*f02615ebSJingbo Xu 			return PTR_ERR(it->kaddr);
190*f02615ebSJingbo Xu 
191*f02615ebSJingbo Xu 		src = it->kaddr + erofs_blkoff(sb, it->pos);
192*f02615ebSJingbo Xu 		slice = min_t(unsigned int, sb->s_blocksize -
193*f02615ebSJingbo Xu 				erofs_blkoff(sb, it->pos), len - processed);
194*f02615ebSJingbo Xu 		memcpy(it->buffer + it->buffer_ofs, src, slice);
195*f02615ebSJingbo Xu 		it->buffer_ofs += slice;
196*f02615ebSJingbo Xu 		it->pos += slice;
197*f02615ebSJingbo Xu 	}
198*f02615ebSJingbo Xu 	return 0;
199*f02615ebSJingbo Xu }
200*f02615ebSJingbo Xu 
201*f02615ebSJingbo Xu static int erofs_listxattr_foreach(struct erofs_xattr_iter *it)
202*f02615ebSJingbo Xu {
203*f02615ebSJingbo Xu 	struct erofs_xattr_entry entry;
204*f02615ebSJingbo Xu 	unsigned int base_index, name_total, prefix_len, infix_len = 0;
205*f02615ebSJingbo Xu 	const char *prefix, *infix = NULL;
206*f02615ebSJingbo Xu 	int err;
207*f02615ebSJingbo Xu 
208*f02615ebSJingbo Xu 	/* 1. handle xattr entry */
209*f02615ebSJingbo Xu 	entry = *(struct erofs_xattr_entry *)
210*f02615ebSJingbo Xu 			(it->kaddr + erofs_blkoff(it->sb, it->pos));
211*f02615ebSJingbo Xu 	it->pos += sizeof(struct erofs_xattr_entry);
212*f02615ebSJingbo Xu 
213*f02615ebSJingbo Xu 	base_index = entry.e_name_index;
214*f02615ebSJingbo Xu 	if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
2158e823961SJingbo Xu 		struct erofs_sb_info *sbi = EROFS_SB(it->sb);
21682bc1ef4SJingbo Xu 		struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
217*f02615ebSJingbo Xu 			(entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
21847e4937aSGao Xiang 
21982bc1ef4SJingbo Xu 		if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
220*f02615ebSJingbo Xu 			return 0;
22182bc1ef4SJingbo Xu 		infix = pf->prefix->infix;
22282bc1ef4SJingbo Xu 		infix_len = pf->infix_len;
22382bc1ef4SJingbo Xu 		base_index = pf->prefix->base_index;
22482bc1ef4SJingbo Xu 	}
22582bc1ef4SJingbo Xu 
22661d325dcSLinus Torvalds 	prefix = erofs_xattr_prefix(base_index, it->dentry);
227a5488f29SChristian Brauner 	if (!prefix)
228*f02615ebSJingbo Xu 		return 0;
22947e4937aSGao Xiang 	prefix_len = strlen(prefix);
230*f02615ebSJingbo Xu 	name_total = prefix_len + infix_len + entry.e_name_len + 1;
23147e4937aSGao Xiang 
23247e4937aSGao Xiang 	if (!it->buffer) {
233*f02615ebSJingbo Xu 		it->buffer_ofs += name_total;
234*f02615ebSJingbo Xu 		return 0;
23547e4937aSGao Xiang 	}
23647e4937aSGao Xiang 
237*f02615ebSJingbo Xu 	if (it->buffer_ofs + name_total > it->buffer_size)
23847e4937aSGao Xiang 		return -ERANGE;
23947e4937aSGao Xiang 
24047e4937aSGao Xiang 	memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
24182bc1ef4SJingbo Xu 	memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len);
24282bc1ef4SJingbo Xu 	it->buffer_ofs += prefix_len + infix_len;
24347e4937aSGao Xiang 
244*f02615ebSJingbo Xu 	/* 2. handle xattr name */
245*f02615ebSJingbo Xu 	err = erofs_xattr_copy_to_buffer(it, entry.e_name_len);
246*f02615ebSJingbo Xu 	if (err)
247*f02615ebSJingbo Xu 		return err;
24847e4937aSGao Xiang 
24947e4937aSGao Xiang 	it->buffer[it->buffer_ofs++] = '\0';
250*f02615ebSJingbo Xu 	return 0;
25147e4937aSGao Xiang }
25247e4937aSGao Xiang 
253*f02615ebSJingbo Xu static int erofs_getxattr_foreach(struct erofs_xattr_iter *it)
254*f02615ebSJingbo Xu {
255*f02615ebSJingbo Xu 	struct super_block *sb = it->sb;
256*f02615ebSJingbo Xu 	struct erofs_xattr_entry entry;
257*f02615ebSJingbo Xu 	unsigned int slice, processed, value_sz;
258*f02615ebSJingbo Xu 
259*f02615ebSJingbo Xu 	/* 1. handle xattr entry */
260*f02615ebSJingbo Xu 	entry = *(struct erofs_xattr_entry *)
261*f02615ebSJingbo Xu 			(it->kaddr + erofs_blkoff(sb, it->pos));
262*f02615ebSJingbo Xu 	it->pos += sizeof(struct erofs_xattr_entry);
263*f02615ebSJingbo Xu 	value_sz = le16_to_cpu(entry.e_value_size);
264*f02615ebSJingbo Xu 
265*f02615ebSJingbo Xu 	/* should also match the infix for long name prefixes */
266*f02615ebSJingbo Xu 	if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
267*f02615ebSJingbo Xu 		struct erofs_sb_info *sbi = EROFS_SB(sb);
268*f02615ebSJingbo Xu 		struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
269*f02615ebSJingbo Xu 			(entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
270*f02615ebSJingbo Xu 
271*f02615ebSJingbo Xu 		if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
272*f02615ebSJingbo Xu 			return -ENOATTR;
273*f02615ebSJingbo Xu 
274*f02615ebSJingbo Xu 		if (it->index != pf->prefix->base_index ||
275*f02615ebSJingbo Xu 		    it->name.len != entry.e_name_len + pf->infix_len)
276*f02615ebSJingbo Xu 			return -ENOATTR;
277*f02615ebSJingbo Xu 
278*f02615ebSJingbo Xu 		if (memcmp(it->name.name, pf->prefix->infix, pf->infix_len))
279*f02615ebSJingbo Xu 			return -ENOATTR;
280*f02615ebSJingbo Xu 
281*f02615ebSJingbo Xu 		it->infix_len = pf->infix_len;
282*f02615ebSJingbo Xu 	} else {
283*f02615ebSJingbo Xu 		if (it->index != entry.e_name_index ||
284*f02615ebSJingbo Xu 		    it->name.len != entry.e_name_len)
285*f02615ebSJingbo Xu 			return -ENOATTR;
286*f02615ebSJingbo Xu 
287*f02615ebSJingbo Xu 		it->infix_len = 0;
288*f02615ebSJingbo Xu 	}
289*f02615ebSJingbo Xu 
290*f02615ebSJingbo Xu 	/* 2. handle xattr name */
291*f02615ebSJingbo Xu 	for (processed = 0; processed < entry.e_name_len; processed += slice) {
292*f02615ebSJingbo Xu 		it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
293*f02615ebSJingbo Xu 					EROFS_KMAP);
294*f02615ebSJingbo Xu 		if (IS_ERR(it->kaddr))
295*f02615ebSJingbo Xu 			return PTR_ERR(it->kaddr);
296*f02615ebSJingbo Xu 
297*f02615ebSJingbo Xu 		slice = min_t(unsigned int,
298*f02615ebSJingbo Xu 				sb->s_blocksize - erofs_blkoff(sb, it->pos),
299*f02615ebSJingbo Xu 				entry.e_name_len - processed);
300*f02615ebSJingbo Xu 		if (memcmp(it->name.name + it->infix_len + processed,
301*f02615ebSJingbo Xu 			   it->kaddr + erofs_blkoff(sb, it->pos), slice))
302*f02615ebSJingbo Xu 			return -ENOATTR;
303*f02615ebSJingbo Xu 		it->pos += slice;
304*f02615ebSJingbo Xu 	}
305*f02615ebSJingbo Xu 
306*f02615ebSJingbo Xu 	/* 3. handle xattr value */
307*f02615ebSJingbo Xu 	if (!it->buffer) {
308*f02615ebSJingbo Xu 		it->buffer_ofs = value_sz;
309*f02615ebSJingbo Xu 		return 0;
310*f02615ebSJingbo Xu 	}
311*f02615ebSJingbo Xu 
312*f02615ebSJingbo Xu 	if (it->buffer_size < value_sz)
313*f02615ebSJingbo Xu 		return -ERANGE;
314*f02615ebSJingbo Xu 
315*f02615ebSJingbo Xu 	return erofs_xattr_copy_to_buffer(it, value_sz);
316*f02615ebSJingbo Xu }
31747e4937aSGao Xiang 
3184b077b50SJingbo Xu static int erofs_xattr_iter_inline(struct erofs_xattr_iter *it,
3194b077b50SJingbo Xu 				   struct inode *inode, bool getxattr)
32047e4937aSGao Xiang {
3214b077b50SJingbo Xu 	struct erofs_inode *const vi = EROFS_I(inode);
322*f02615ebSJingbo Xu 	unsigned int xattr_header_sz, remaining, entry_sz;
323*f02615ebSJingbo Xu 	erofs_off_t next_pos;
32447e4937aSGao Xiang 	int ret;
32547e4937aSGao Xiang 
3264b077b50SJingbo Xu 	xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) +
3274b077b50SJingbo Xu 			  sizeof(u32) * vi->xattr_shared_count;
3284b077b50SJingbo Xu 	if (xattr_header_sz >= vi->xattr_isize) {
3294b077b50SJingbo Xu 		DBG_BUGON(xattr_header_sz > vi->xattr_isize);
3304b077b50SJingbo Xu 		return -ENOATTR;
3314b077b50SJingbo Xu 	}
33247e4937aSGao Xiang 
333*f02615ebSJingbo Xu 	remaining = vi->xattr_isize - xattr_header_sz;
3344b077b50SJingbo Xu 	it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz;
335*f02615ebSJingbo Xu 
336*f02615ebSJingbo Xu 	while (remaining) {
3374b077b50SJingbo Xu 		it->kaddr = erofs_bread(&it->buf, erofs_blknr(it->sb, it->pos),
3384b077b50SJingbo Xu 					EROFS_KMAP);
3394b077b50SJingbo Xu 		if (IS_ERR(it->kaddr))
3404b077b50SJingbo Xu 			return PTR_ERR(it->kaddr);
3414b077b50SJingbo Xu 
342*f02615ebSJingbo Xu 		entry_sz = erofs_xattr_entry_size(it->kaddr +
343*f02615ebSJingbo Xu 				erofs_blkoff(it->sb, it->pos));
344*f02615ebSJingbo Xu 		/* xattr on-disk corruption: xattr entry beyond xattr_isize */
345*f02615ebSJingbo Xu 		if (remaining < entry_sz) {
346*f02615ebSJingbo Xu 			DBG_BUGON(1);
347*f02615ebSJingbo Xu 			return -EFSCORRUPTED;
348*f02615ebSJingbo Xu 		}
349*f02615ebSJingbo Xu 		remaining -= entry_sz;
350*f02615ebSJingbo Xu 		next_pos = it->pos + entry_sz;
3514b077b50SJingbo Xu 
352*f02615ebSJingbo Xu 		if (getxattr)
353*f02615ebSJingbo Xu 			ret = erofs_getxattr_foreach(it);
354*f02615ebSJingbo Xu 		else
355*f02615ebSJingbo Xu 			ret = erofs_listxattr_foreach(it);
3564b077b50SJingbo Xu 		if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
35747e4937aSGao Xiang 			break;
358*f02615ebSJingbo Xu 
359*f02615ebSJingbo Xu 		it->pos = next_pos;
36047e4937aSGao Xiang 	}
3614b077b50SJingbo Xu 	return ret;
36247e4937aSGao Xiang }
36347e4937aSGao Xiang 
3644b077b50SJingbo Xu static int erofs_xattr_iter_shared(struct erofs_xattr_iter *it,
3654b077b50SJingbo Xu 				   struct inode *inode, bool getxattr)
36647e4937aSGao Xiang {
367a5876e24SGao Xiang 	struct erofs_inode *const vi = EROFS_I(inode);
3688e823961SJingbo Xu 	struct super_block *const sb = it->sb;
369eba67eb6SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
370eba67eb6SJingbo Xu 	unsigned int i;
3714b077b50SJingbo Xu 	int ret = -ENOATTR;
3724b077b50SJingbo Xu 
37347e4937aSGao Xiang 	for (i = 0; i < vi->xattr_shared_count; ++i) {
3748e823961SJingbo Xu 		it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
375eba67eb6SJingbo Xu 				vi->xattr_shared_xattrs[i] * sizeof(__le32);
3768e823961SJingbo Xu 		it->kaddr = erofs_bread(&it->buf, erofs_blknr(sb, it->pos),
3778e823961SJingbo Xu 					EROFS_KMAP);
3788e823961SJingbo Xu 		if (IS_ERR(it->kaddr))
3798e823961SJingbo Xu 			return PTR_ERR(it->kaddr);
38047e4937aSGao Xiang 
381*f02615ebSJingbo Xu 		if (getxattr)
382*f02615ebSJingbo Xu 			ret = erofs_getxattr_foreach(it);
383*f02615ebSJingbo Xu 		else
384*f02615ebSJingbo Xu 			ret = erofs_listxattr_foreach(it);
3854b077b50SJingbo Xu 		if ((getxattr && ret != -ENOATTR) || (!getxattr && ret))
38647e4937aSGao Xiang 			break;
38747e4937aSGao Xiang 	}
3884b077b50SJingbo Xu 	return ret;
38947e4937aSGao Xiang }
39047e4937aSGao Xiang 
3914b077b50SJingbo Xu int erofs_getxattr(struct inode *inode, int index, const char *name,
3924b077b50SJingbo Xu 		   void *buffer, size_t buffer_size)
39347e4937aSGao Xiang {
39447e4937aSGao Xiang 	int ret;
3958e823961SJingbo Xu 	struct erofs_xattr_iter it;
39647e4937aSGao Xiang 
3974b077b50SJingbo Xu 	if (!name)
3984b077b50SJingbo Xu 		return -EINVAL;
3994b077b50SJingbo Xu 
4004b077b50SJingbo Xu 	ret = erofs_init_inode_xattrs(inode);
4014b077b50SJingbo Xu 	if (ret)
4024b077b50SJingbo Xu 		return ret;
4034b077b50SJingbo Xu 
4044b077b50SJingbo Xu 	it.index = index;
4054b077b50SJingbo Xu 	it.name = (struct qstr)QSTR_INIT(name, strlen(name));
4064b077b50SJingbo Xu 	if (it.name.len > EROFS_NAME_LEN)
4074b077b50SJingbo Xu 		return -ERANGE;
4084b077b50SJingbo Xu 
4094b077b50SJingbo Xu 	it.sb = inode->i_sb;
4104b077b50SJingbo Xu 	it.buf = __EROFS_BUF_INITIALIZER;
4114b077b50SJingbo Xu 	erofs_init_metabuf(&it.buf, it.sb);
4124b077b50SJingbo Xu 	it.buffer = buffer;
4134b077b50SJingbo Xu 	it.buffer_size = buffer_size;
4144b077b50SJingbo Xu 	it.buffer_ofs = 0;
4154b077b50SJingbo Xu 
4164b077b50SJingbo Xu 	ret = erofs_xattr_iter_inline(&it, inode, true);
4174b077b50SJingbo Xu 	if (ret == -ENOATTR)
4184b077b50SJingbo Xu 		ret = erofs_xattr_iter_shared(&it, inode, true);
4194b077b50SJingbo Xu 	erofs_put_metabuf(&it.buf);
4204b077b50SJingbo Xu 	return ret ? ret : it.buffer_ofs;
4214b077b50SJingbo Xu }
4224b077b50SJingbo Xu 
4234b077b50SJingbo Xu ssize_t erofs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
4244b077b50SJingbo Xu {
4254b077b50SJingbo Xu 	int ret;
4264b077b50SJingbo Xu 	struct erofs_xattr_iter it;
4274b077b50SJingbo Xu 	struct inode *inode = d_inode(dentry);
4284b077b50SJingbo Xu 
4294b077b50SJingbo Xu 	ret = erofs_init_inode_xattrs(inode);
430926d1650SGao Xiang 	if (ret == -ENOATTR)
431926d1650SGao Xiang 		return 0;
43247e4937aSGao Xiang 	if (ret)
43347e4937aSGao Xiang 		return ret;
43447e4937aSGao Xiang 
4358e823961SJingbo Xu 	it.sb = dentry->d_sb;
4368e823961SJingbo Xu 	it.buf = __EROFS_BUF_INITIALIZER;
4378e823961SJingbo Xu 	erofs_init_metabuf(&it.buf, it.sb);
43847e4937aSGao Xiang 	it.dentry = dentry;
43947e4937aSGao Xiang 	it.buffer = buffer;
44047e4937aSGao Xiang 	it.buffer_size = buffer_size;
44147e4937aSGao Xiang 	it.buffer_ofs = 0;
44247e4937aSGao Xiang 
4434b077b50SJingbo Xu 	ret = erofs_xattr_iter_inline(&it, inode, false);
4444b077b50SJingbo Xu 	if (!ret || ret == -ENOATTR)
4454b077b50SJingbo Xu 		ret = erofs_xattr_iter_shared(&it, inode, false);
4464b077b50SJingbo Xu 	if (ret == -ENOATTR)
4474b077b50SJingbo Xu 		ret = 0;
4488e823961SJingbo Xu 	erofs_put_metabuf(&it.buf);
4494b077b50SJingbo Xu 	return ret ? ret : it.buffer_ofs;
45047e4937aSGao Xiang }
45147e4937aSGao Xiang 
4529e382914SJingbo Xu void erofs_xattr_prefixes_cleanup(struct super_block *sb)
4539e382914SJingbo Xu {
4549e382914SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
4559e382914SJingbo Xu 	int i;
4569e382914SJingbo Xu 
4579e382914SJingbo Xu 	if (sbi->xattr_prefixes) {
4589e382914SJingbo Xu 		for (i = 0; i < sbi->xattr_prefix_count; i++)
4599e382914SJingbo Xu 			kfree(sbi->xattr_prefixes[i].prefix);
4609e382914SJingbo Xu 		kfree(sbi->xattr_prefixes);
4619e382914SJingbo Xu 		sbi->xattr_prefixes = NULL;
4629e382914SJingbo Xu 	}
4639e382914SJingbo Xu }
4649e382914SJingbo Xu 
4659e382914SJingbo Xu int erofs_xattr_prefixes_init(struct super_block *sb)
4669e382914SJingbo Xu {
4679e382914SJingbo Xu 	struct erofs_sb_info *sbi = EROFS_SB(sb);
4689e382914SJingbo Xu 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
4699e382914SJingbo Xu 	erofs_off_t pos = (erofs_off_t)sbi->xattr_prefix_start << 2;
4709e382914SJingbo Xu 	struct erofs_xattr_prefix_item *pfs;
4719e382914SJingbo Xu 	int ret = 0, i, len;
4729e382914SJingbo Xu 
4739e382914SJingbo Xu 	if (!sbi->xattr_prefix_count)
4749e382914SJingbo Xu 		return 0;
4759e382914SJingbo Xu 
4769e382914SJingbo Xu 	pfs = kzalloc(sbi->xattr_prefix_count * sizeof(*pfs), GFP_KERNEL);
4779e382914SJingbo Xu 	if (!pfs)
4789e382914SJingbo Xu 		return -ENOMEM;
4799e382914SJingbo Xu 
4800a17567bSJingbo Xu 	if (sbi->packed_inode)
4819e382914SJingbo Xu 		buf.inode = sbi->packed_inode;
4829e382914SJingbo Xu 	else
4839e382914SJingbo Xu 		erofs_init_metabuf(&buf, sb);
4849e382914SJingbo Xu 
4859e382914SJingbo Xu 	for (i = 0; i < sbi->xattr_prefix_count; i++) {
4869e382914SJingbo Xu 		void *ptr = erofs_read_metadata(sb, &buf, &pos, &len);
4879e382914SJingbo Xu 
4889e382914SJingbo Xu 		if (IS_ERR(ptr)) {
4899e382914SJingbo Xu 			ret = PTR_ERR(ptr);
4909e382914SJingbo Xu 			break;
4919e382914SJingbo Xu 		} else if (len < sizeof(*pfs->prefix) ||
4929e382914SJingbo Xu 			   len > EROFS_NAME_LEN + sizeof(*pfs->prefix)) {
4939e382914SJingbo Xu 			kfree(ptr);
4949e382914SJingbo Xu 			ret = -EFSCORRUPTED;
4959e382914SJingbo Xu 			break;
4969e382914SJingbo Xu 		}
4979e382914SJingbo Xu 		pfs[i].prefix = ptr;
4989e382914SJingbo Xu 		pfs[i].infix_len = len - sizeof(struct erofs_xattr_long_prefix);
4999e382914SJingbo Xu 	}
5009e382914SJingbo Xu 
5019e382914SJingbo Xu 	erofs_put_metabuf(&buf);
5029e382914SJingbo Xu 	sbi->xattr_prefixes = pfs;
5039e382914SJingbo Xu 	if (ret)
5049e382914SJingbo Xu 		erofs_xattr_prefixes_cleanup(sb);
5059e382914SJingbo Xu 	return ret;
5069e382914SJingbo Xu }
5079e382914SJingbo Xu 
50847e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_POSIX_ACL
5090cad6246SMiklos Szeredi struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
51047e4937aSGao Xiang {
51147e4937aSGao Xiang 	struct posix_acl *acl;
51247e4937aSGao Xiang 	int prefix, rc;
51347e4937aSGao Xiang 	char *value = NULL;
51447e4937aSGao Xiang 
5150cad6246SMiklos Szeredi 	if (rcu)
5160cad6246SMiklos Szeredi 		return ERR_PTR(-ECHILD);
5170cad6246SMiklos Szeredi 
51847e4937aSGao Xiang 	switch (type) {
51947e4937aSGao Xiang 	case ACL_TYPE_ACCESS:
52047e4937aSGao Xiang 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
52147e4937aSGao Xiang 		break;
52247e4937aSGao Xiang 	case ACL_TYPE_DEFAULT:
52347e4937aSGao Xiang 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
52447e4937aSGao Xiang 		break;
52547e4937aSGao Xiang 	default:
52647e4937aSGao Xiang 		return ERR_PTR(-EINVAL);
52747e4937aSGao Xiang 	}
52847e4937aSGao Xiang 
52947e4937aSGao Xiang 	rc = erofs_getxattr(inode, prefix, "", NULL, 0);
53047e4937aSGao Xiang 	if (rc > 0) {
53147e4937aSGao Xiang 		value = kmalloc(rc, GFP_KERNEL);
53247e4937aSGao Xiang 		if (!value)
53347e4937aSGao Xiang 			return ERR_PTR(-ENOMEM);
53447e4937aSGao Xiang 		rc = erofs_getxattr(inode, prefix, "", value, rc);
53547e4937aSGao Xiang 	}
53647e4937aSGao Xiang 
53747e4937aSGao Xiang 	if (rc == -ENOATTR)
53847e4937aSGao Xiang 		acl = NULL;
53947e4937aSGao Xiang 	else if (rc < 0)
54047e4937aSGao Xiang 		acl = ERR_PTR(rc);
54147e4937aSGao Xiang 	else
54247e4937aSGao Xiang 		acl = posix_acl_from_xattr(&init_user_ns, value, rc);
54347e4937aSGao Xiang 	kfree(value);
54447e4937aSGao Xiang 	return acl;
54547e4937aSGao Xiang }
54647e4937aSGao Xiang #endif
547