xref: /linux/fs/erofs/xattr.c (revision 6a01f5478d208544c8ba5ddbd674ea660f1b7047)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017-2018 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  * Copyright (C) 2021-2022, Alibaba Cloud
6  */
7 #include <linux/security.h>
8 #include <linux/xxhash.h>
9 #include "xattr.h"
10 
11 struct erofs_xattr_iter {
12 	struct super_block *sb;
13 	struct erofs_buf buf;
14 	erofs_off_t pos;
15 	void *kaddr;
16 
17 	char *buffer;
18 	int buffer_size, buffer_ofs;
19 
20 	/* getxattr */
21 	int index, infix_len;
22 	struct qstr name;
23 
24 	/* listxattr */
25 	struct dentry *dentry;
26 };
27 
28 static const char *erofs_xattr_prefix(unsigned int idx, struct dentry *dentry);
29 
30 static int erofs_init_inode_xattrs(struct inode *inode)
31 {
32 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
33 	struct erofs_inode *vi = EROFS_I(inode);
34 	struct super_block *sb = inode->i_sb;
35 	const struct erofs_xattr_ibody_header *ih;
36 	__le32 *xattr_id;
37 	erofs_off_t pos;
38 	unsigned int i;
39 	int ret = 0;
40 
41 	if (!vi->xattr_isize)
42 		return -ENODATA;
43 
44 	/* the most case is that xattrs of this inode are initialized. */
45 	if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags)) {
46 		/*
47 		 * paired with smp_mb() at the end of the function to ensure
48 		 * fields will only be observed after the bit is set.
49 		 */
50 		smp_mb();
51 		return 0;
52 	}
53 	if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_XATTR_BIT, TASK_KILLABLE))
54 		return -ERESTARTSYS;
55 
56 	/* someone has initialized xattrs for us? */
57 	if (test_bit(EROFS_I_EA_INITED_BIT, &vi->flags))
58 		goto out_unlock;
59 
60 	/*
61 	 * bypass all xattr operations if ->xattr_isize is not greater than
62 	 * sizeof(struct erofs_xattr_ibody_header), in detail:
63 	 * 1) it is not enough to contain erofs_xattr_ibody_header then
64 	 *    ->xattr_isize should be 0 (it means no xattr);
65 	 * 2) it is just to contain erofs_xattr_ibody_header, which is on-disk
66 	 *    undefined right now (maybe use later with some new sb feature).
67 	 */
68 	if (vi->xattr_isize == sizeof(struct erofs_xattr_ibody_header)) {
69 		erofs_err(sb, "xattr_isize %d of nid %llu is not supported yet",
70 			  vi->xattr_isize, vi->nid);
71 		ret = -EOPNOTSUPP;
72 		goto out_unlock;
73 	} else if (vi->xattr_isize < sizeof(struct erofs_xattr_ibody_header)) {
74 		erofs_err(sb, "bogus xattr ibody @ nid %llu", vi->nid);
75 		DBG_BUGON(1);
76 		ret = -EFSCORRUPTED;
77 		goto out_unlock;
78 	}
79 
80 	pos = erofs_iloc(inode) + vi->inode_isize;
81 	ih = erofs_read_metabuf(&buf, sb, pos, erofs_inode_in_metabox(inode));
82 	if (IS_ERR(ih)) {
83 		ret = PTR_ERR(ih);
84 		goto out_unlock;
85 	}
86 	vi->xattr_name_filter = le32_to_cpu(ih->h_name_filter);
87 	vi->xattr_shared_count = ih->h_shared_count;
88 	if ((u32)vi->xattr_shared_count * sizeof(__le32) >
89 	    vi->xattr_isize - sizeof(struct erofs_xattr_ibody_header)) {
90 		erofs_err(sb, "invalid h_shared_count %u @ nid %llu",
91 			  vi->xattr_shared_count, vi->nid);
92 		erofs_put_metabuf(&buf);
93 		ret = -EFSCORRUPTED;
94 		goto out_unlock;
95 	}
96 	vi->xattr_shared_xattrs = kmalloc_objs(uint, vi->xattr_shared_count);
97 	if (!vi->xattr_shared_xattrs) {
98 		erofs_put_metabuf(&buf);
99 		ret = -ENOMEM;
100 		goto out_unlock;
101 	}
102 
103 	/* skip the ibody header and read the shared xattr array */
104 	pos += sizeof(struct erofs_xattr_ibody_header);
105 	for (i = 0; i < vi->xattr_shared_count; ++i) {
106 		xattr_id = erofs_bread(&buf, pos + i * sizeof(__le32), true);
107 		if (IS_ERR(xattr_id)) {
108 			kfree(vi->xattr_shared_xattrs);
109 			vi->xattr_shared_xattrs = NULL;
110 			ret = PTR_ERR(xattr_id);
111 			goto out_unlock;
112 		}
113 		vi->xattr_shared_xattrs[i] = le32_to_cpu(*xattr_id);
114 	}
115 	erofs_put_metabuf(&buf);
116 
117 	/* paired with smp_mb() at the beginning of the function. */
118 	smp_mb();
119 	set_bit(EROFS_I_EA_INITED_BIT, &vi->flags);
120 out_unlock:
121 	clear_and_wake_up_bit(EROFS_I_BL_XATTR_BIT, &vi->flags);
122 	return ret;
123 }
124 
125 static int erofs_xattr_copy_to_buffer(struct erofs_xattr_iter *it,
126 				      unsigned int len)
127 {
128 	unsigned int slice, processed;
129 	struct super_block *sb = it->sb;
130 
131 	for (processed = 0; processed < len; processed += slice) {
132 		it->kaddr = erofs_bread(&it->buf, it->pos, true);
133 		if (IS_ERR(it->kaddr))
134 			return PTR_ERR(it->kaddr);
135 
136 		slice = min_t(unsigned int, sb->s_blocksize -
137 				erofs_blkoff(sb, it->pos), len - processed);
138 		memcpy(it->buffer + it->buffer_ofs, it->kaddr, slice);
139 		it->buffer_ofs += slice;
140 		it->pos += slice;
141 	}
142 	return 0;
143 }
144 
145 static int erofs_listxattr_foreach(struct erofs_xattr_iter *it)
146 {
147 	struct erofs_xattr_entry entry;
148 	unsigned int base_index, name_total, prefix_len, infix_len = 0;
149 	const char *prefix, *infix = NULL;
150 	int err;
151 
152 	/* 1. handle xattr entry */
153 	entry = *(struct erofs_xattr_entry *)it->kaddr;
154 	it->pos += sizeof(struct erofs_xattr_entry);
155 
156 	base_index = entry.e_name_index;
157 	if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
158 		struct erofs_sb_info *sbi = EROFS_SB(it->sb);
159 		struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
160 			(entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
161 
162 		if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
163 			return 0;
164 		infix = pf->prefix->infix;
165 		infix_len = pf->infix_len;
166 		base_index = pf->prefix->base_index;
167 	}
168 
169 	prefix = erofs_xattr_prefix(base_index, it->dentry);
170 	if (!prefix)
171 		return 0;
172 	prefix_len = strlen(prefix);
173 	name_total = prefix_len + infix_len + entry.e_name_len + 1;
174 
175 	if (!it->buffer) {
176 		it->buffer_ofs += name_total;
177 		return 0;
178 	}
179 
180 	if (it->buffer_ofs + name_total > it->buffer_size)
181 		return -ERANGE;
182 
183 	memcpy(it->buffer + it->buffer_ofs, prefix, prefix_len);
184 	memcpy(it->buffer + it->buffer_ofs + prefix_len, infix, infix_len);
185 	it->buffer_ofs += prefix_len + infix_len;
186 
187 	/* 2. handle xattr name */
188 	err = erofs_xattr_copy_to_buffer(it, entry.e_name_len);
189 	if (err)
190 		return err;
191 
192 	it->buffer[it->buffer_ofs++] = '\0';
193 	return 0;
194 }
195 
196 static int erofs_getxattr_foreach(struct erofs_xattr_iter *it)
197 {
198 	struct super_block *sb = it->sb;
199 	struct erofs_xattr_entry entry;
200 	unsigned int slice, processed, value_sz;
201 
202 	/* 1. handle xattr entry */
203 	entry = *(struct erofs_xattr_entry *)it->kaddr;
204 	it->pos += sizeof(struct erofs_xattr_entry);
205 	value_sz = le16_to_cpu(entry.e_value_size);
206 
207 	/* should also match the infix for long name prefixes */
208 	if (entry.e_name_index & EROFS_XATTR_LONG_PREFIX) {
209 		struct erofs_sb_info *sbi = EROFS_SB(sb);
210 		struct erofs_xattr_prefix_item *pf = sbi->xattr_prefixes +
211 			(entry.e_name_index & EROFS_XATTR_LONG_PREFIX_MASK);
212 
213 		if (pf >= sbi->xattr_prefixes + sbi->xattr_prefix_count)
214 			return -ENODATA;
215 
216 		if (it->index != pf->prefix->base_index ||
217 		    it->name.len != entry.e_name_len + pf->infix_len)
218 			return -ENODATA;
219 
220 		if (memcmp(it->name.name, pf->prefix->infix, pf->infix_len))
221 			return -ENODATA;
222 
223 		it->infix_len = pf->infix_len;
224 	} else {
225 		if (it->index != entry.e_name_index ||
226 		    it->name.len != entry.e_name_len)
227 			return -ENODATA;
228 
229 		it->infix_len = 0;
230 	}
231 
232 	/* 2. handle xattr name */
233 	for (processed = 0; processed < entry.e_name_len; processed += slice) {
234 		it->kaddr = erofs_bread(&it->buf, it->pos, true);
235 		if (IS_ERR(it->kaddr))
236 			return PTR_ERR(it->kaddr);
237 
238 		slice = min_t(unsigned int,
239 				sb->s_blocksize - erofs_blkoff(sb, it->pos),
240 				entry.e_name_len - processed);
241 		if (memcmp(it->name.name + it->infix_len + processed,
242 			   it->kaddr, slice))
243 			return -ENODATA;
244 		it->pos += slice;
245 	}
246 
247 	/* 3. handle xattr value */
248 	if (!it->buffer) {
249 		it->buffer_ofs = value_sz;
250 		return 0;
251 	}
252 
253 	if (it->buffer_size < value_sz)
254 		return -ERANGE;
255 
256 	return erofs_xattr_copy_to_buffer(it, value_sz);
257 }
258 
259 static int erofs_xattr_iter_inline(struct erofs_xattr_iter *it,
260 				   struct inode *inode, bool getxattr)
261 {
262 	struct erofs_inode *const vi = EROFS_I(inode);
263 	unsigned int xattr_header_sz, remaining, entry_sz;
264 	erofs_off_t next_pos;
265 	int ret;
266 
267 	xattr_header_sz = sizeof(struct erofs_xattr_ibody_header) +
268 			  sizeof(u32) * vi->xattr_shared_count;
269 	if (xattr_header_sz >= vi->xattr_isize) {
270 		DBG_BUGON(xattr_header_sz > vi->xattr_isize);
271 		return -ENODATA;
272 	}
273 
274 	ret = erofs_init_metabuf(&it->buf, it->sb, erofs_inode_in_metabox(inode));
275 	if (ret)
276 		return ret;
277 	remaining = vi->xattr_isize - xattr_header_sz;
278 	it->pos = erofs_iloc(inode) + vi->inode_isize + xattr_header_sz;
279 
280 	while (remaining) {
281 		it->kaddr = erofs_bread(&it->buf, it->pos, true);
282 		if (IS_ERR(it->kaddr))
283 			return PTR_ERR(it->kaddr);
284 
285 		entry_sz = erofs_xattr_entry_size(it->kaddr);
286 		/* xattr on-disk corruption: xattr entry beyond xattr_isize */
287 		if (remaining < entry_sz) {
288 			DBG_BUGON(1);
289 			return -EFSCORRUPTED;
290 		}
291 		remaining -= entry_sz;
292 		next_pos = it->pos + entry_sz;
293 
294 		if (getxattr)
295 			ret = erofs_getxattr_foreach(it);
296 		else
297 			ret = erofs_listxattr_foreach(it);
298 		if ((getxattr && ret != -ENODATA) || (!getxattr && ret))
299 			break;
300 
301 		it->pos = next_pos;
302 	}
303 	return ret;
304 }
305 
306 static int erofs_xattr_iter_shared(struct erofs_xattr_iter *it,
307 				   struct inode *inode, bool getxattr)
308 {
309 	struct erofs_inode *const vi = EROFS_I(inode);
310 	struct super_block *const sb = it->sb;
311 	struct erofs_sb_info *sbi = EROFS_SB(sb);
312 	unsigned int i = 0;
313 	int ret;
314 
315 	ret = erofs_init_metabuf(&it->buf, sb,
316 				 erofs_sb_has_shared_ea_in_metabox(sbi));
317 	if (ret)
318 		return ret;
319 
320 	while (i < vi->xattr_shared_count) {
321 		it->pos = erofs_pos(sb, sbi->xattr_blkaddr) +
322 				vi->xattr_shared_xattrs[i++] * sizeof(__le32);
323 		it->kaddr = erofs_bread(&it->buf, it->pos, true);
324 		if (IS_ERR(it->kaddr))
325 			return PTR_ERR(it->kaddr);
326 
327 		if (getxattr)
328 			ret = erofs_getxattr_foreach(it);
329 		else
330 			ret = erofs_listxattr_foreach(it);
331 		if ((getxattr && ret != -ENODATA) || (!getxattr && ret))
332 			break;
333 	}
334 	return i ? ret : -ENODATA;
335 }
336 
337 static int erofs_getxattr(struct inode *inode, int index, const char *name,
338 			  void *buffer, size_t buffer_size)
339 {
340 	int ret;
341 	unsigned int hashbit;
342 	struct erofs_xattr_iter it;
343 	struct erofs_inode *vi = EROFS_I(inode);
344 	struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
345 
346 	if (!name)
347 		return -EINVAL;
348 
349 	ret = erofs_init_inode_xattrs(inode);
350 	if (ret)
351 		return ret;
352 
353 	/* reserved flag is non-zero if there's any change of on-disk format */
354 	if (erofs_sb_has_xattr_filter(sbi) && !sbi->xattr_filter_reserved) {
355 		hashbit = xxh32(name, strlen(name),
356 				EROFS_XATTR_FILTER_SEED + index);
357 		hashbit &= EROFS_XATTR_FILTER_BITS - 1;
358 		if (vi->xattr_name_filter & (1U << hashbit))
359 			return -ENODATA;
360 	}
361 
362 	it.index = index;
363 	it.name = QSTR(name);
364 	if (it.name.len > EROFS_NAME_LEN)
365 		return -ERANGE;
366 
367 	it.sb = inode->i_sb;
368 	it.buf = __EROFS_BUF_INITIALIZER;
369 	it.buffer = buffer;
370 	it.buffer_size = buffer_size;
371 	it.buffer_ofs = 0;
372 
373 	ret = erofs_xattr_iter_inline(&it, inode, true);
374 	if (ret == -ENODATA)
375 		ret = erofs_xattr_iter_shared(&it, inode, true);
376 	erofs_put_metabuf(&it.buf);
377 	return ret ? ret : it.buffer_ofs;
378 }
379 
380 ssize_t erofs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
381 {
382 	int ret;
383 	struct erofs_xattr_iter it;
384 	struct inode *inode = d_inode(dentry);
385 
386 	ret = erofs_init_inode_xattrs(inode);
387 	if (ret == -ENODATA)
388 		return 0;
389 	if (ret)
390 		return ret;
391 
392 	it.sb = dentry->d_sb;
393 	it.buf = __EROFS_BUF_INITIALIZER;
394 	it.dentry = dentry;
395 	it.buffer = buffer;
396 	it.buffer_size = buffer_size;
397 	it.buffer_ofs = 0;
398 
399 	ret = erofs_xattr_iter_inline(&it, inode, false);
400 	if (!ret || ret == -ENODATA)
401 		ret = erofs_xattr_iter_shared(&it, inode, false);
402 	if (ret == -ENODATA)
403 		ret = 0;
404 	erofs_put_metabuf(&it.buf);
405 	return ret ? ret : it.buffer_ofs;
406 }
407 
408 static bool erofs_xattr_user_list(struct dentry *dentry)
409 {
410 	return test_opt(&EROFS_SB(dentry->d_sb)->opt, XATTR_USER);
411 }
412 
413 static bool erofs_xattr_trusted_list(struct dentry *dentry)
414 {
415 	return capable(CAP_SYS_ADMIN);
416 }
417 
418 static int erofs_xattr_generic_get(const struct xattr_handler *handler,
419 				   struct dentry *unused, struct inode *inode,
420 				   const char *name, void *buffer, size_t size)
421 {
422 	if (handler->flags == EROFS_XATTR_INDEX_USER &&
423 	    !test_opt(&EROFS_I_SB(inode)->opt, XATTR_USER))
424 		return -EOPNOTSUPP;
425 
426 	return erofs_getxattr(inode, handler->flags, name, buffer, size);
427 }
428 
429 static const struct xattr_handler erofs_xattr_user_handler = {
430 	.prefix	= XATTR_USER_PREFIX,
431 	.flags	= EROFS_XATTR_INDEX_USER,
432 	.list	= erofs_xattr_user_list,
433 	.get	= erofs_xattr_generic_get,
434 };
435 
436 static const struct xattr_handler erofs_xattr_trusted_handler = {
437 	.prefix	= XATTR_TRUSTED_PREFIX,
438 	.flags	= EROFS_XATTR_INDEX_TRUSTED,
439 	.list	= erofs_xattr_trusted_list,
440 	.get	= erofs_xattr_generic_get,
441 };
442 
443 #ifdef CONFIG_EROFS_FS_SECURITY
444 static const struct xattr_handler erofs_xattr_security_handler = {
445 	.prefix	= XATTR_SECURITY_PREFIX,
446 	.flags	= EROFS_XATTR_INDEX_SECURITY,
447 	.get	= erofs_xattr_generic_get,
448 };
449 #endif
450 
451 const struct xattr_handler * const erofs_xattr_handlers[] = {
452 	&erofs_xattr_user_handler,
453 	&erofs_xattr_trusted_handler,
454 #ifdef CONFIG_EROFS_FS_SECURITY
455 	&erofs_xattr_security_handler,
456 #endif
457 	NULL,
458 };
459 
460 static const char *erofs_xattr_prefix(unsigned int idx, struct dentry *dentry)
461 {
462 	static const struct xattr_handler * const xattr_handler_map[] = {
463 		[EROFS_XATTR_INDEX_USER] = &erofs_xattr_user_handler,
464 #ifdef CONFIG_EROFS_FS_POSIX_ACL
465 		[EROFS_XATTR_INDEX_POSIX_ACL_ACCESS] = &nop_posix_acl_access,
466 		[EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &nop_posix_acl_default,
467 #endif
468 		[EROFS_XATTR_INDEX_TRUSTED] = &erofs_xattr_trusted_handler,
469 #ifdef CONFIG_EROFS_FS_SECURITY
470 		[EROFS_XATTR_INDEX_SECURITY] = &erofs_xattr_security_handler,
471 #endif
472 	};
473 	const struct xattr_handler *handler = NULL;
474 
475 	if (idx && idx < ARRAY_SIZE(xattr_handler_map)) {
476 		handler = xattr_handler_map[idx];
477 		if (xattr_handler_can_list(handler, dentry))
478 			return xattr_prefix(handler);
479 	}
480 	return NULL;
481 }
482 
483 void erofs_xattr_prefixes_cleanup(struct super_block *sb)
484 {
485 	struct erofs_sb_info *sbi = EROFS_SB(sb);
486 	int i;
487 
488 	if (sbi->xattr_prefixes) {
489 		for (i = 0; i < sbi->xattr_prefix_count; i++)
490 			kfree(sbi->xattr_prefixes[i].prefix);
491 		kfree(sbi->xattr_prefixes);
492 		sbi->xattr_prefixes = NULL;
493 	}
494 }
495 
496 int erofs_xattr_prefixes_init(struct super_block *sb)
497 {
498 	struct erofs_sb_info *sbi = EROFS_SB(sb);
499 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
500 	erofs_off_t pos = (erofs_off_t)sbi->xattr_prefix_start << 2;
501 	struct erofs_xattr_prefix_item *pfs;
502 	int ret = 0, i, len;
503 	bool plain = erofs_sb_has_plain_xattr_pfx(sbi);
504 
505 	if (!sbi->xattr_prefix_count)
506 		return 0;
507 
508 	pfs = kzalloc_objs(*pfs, sbi->xattr_prefix_count);
509 	if (!pfs)
510 		return -ENOMEM;
511 
512 	if (!plain) {
513 		if (erofs_sb_has_metabox(sbi))
514 			(void)erofs_init_metabuf(&buf, sb, true);
515 		else if (sbi->packed_inode)
516 			buf.mapping = sbi->packed_inode->i_mapping;
517 		else
518 			plain = true;
519 	}
520 	if (plain)
521 		(void)erofs_init_metabuf(&buf, sb, false);
522 
523 	for (i = 0; i < sbi->xattr_prefix_count; i++) {
524 		void *ptr = erofs_read_metadata(sb, &buf, &pos, &len);
525 
526 		if (IS_ERR(ptr)) {
527 			ret = PTR_ERR(ptr);
528 			break;
529 		} else if (len < sizeof(*pfs->prefix) ||
530 			   len > EROFS_NAME_LEN + sizeof(*pfs->prefix)) {
531 			kfree(ptr);
532 			ret = -EFSCORRUPTED;
533 			break;
534 		}
535 		pfs[i].prefix = ptr;
536 		pfs[i].infix_len = len - sizeof(struct erofs_xattr_long_prefix);
537 	}
538 
539 	erofs_put_metabuf(&buf);
540 	if (!ret && erofs_sb_has_ishare_xattrs(sbi)) {
541 		struct erofs_xattr_prefix_item *pf = pfs + sbi->ishare_xattr_prefix_id;
542 		struct erofs_xattr_long_prefix *newpfx;
543 
544 		newpfx = krealloc(pf->prefix,
545 			sizeof(*newpfx) + pf->infix_len + 1, GFP_KERNEL);
546 		if (newpfx) {
547 			newpfx->infix[pf->infix_len] = '\0';
548 			pf->prefix = newpfx;
549 		} else {
550 			ret = -ENOMEM;
551 		}
552 	}
553 	sbi->xattr_prefixes = pfs;
554 	if (ret)
555 		erofs_xattr_prefixes_cleanup(sb);
556 	return ret;
557 }
558 
559 #ifdef CONFIG_EROFS_FS_POSIX_ACL
560 struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu)
561 {
562 	struct posix_acl *acl;
563 	int prefix, rc;
564 	char *value = NULL;
565 
566 	if (rcu)
567 		return ERR_PTR(-ECHILD);
568 
569 	switch (type) {
570 	case ACL_TYPE_ACCESS:
571 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_ACCESS;
572 		break;
573 	case ACL_TYPE_DEFAULT:
574 		prefix = EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT;
575 		break;
576 	default:
577 		return ERR_PTR(-EINVAL);
578 	}
579 
580 	rc = erofs_getxattr(inode, prefix, "", NULL, 0);
581 	if (rc > 0) {
582 		value = kmalloc(rc, GFP_KERNEL);
583 		if (!value)
584 			return ERR_PTR(-ENOMEM);
585 		rc = erofs_getxattr(inode, prefix, "", value, rc);
586 	}
587 
588 	if (rc == -ENODATA)
589 		acl = NULL;
590 	else if (rc < 0)
591 		acl = ERR_PTR(rc);
592 	else
593 		acl = posix_acl_from_xattr(&init_user_ns, value, rc);
594 	kfree(value);
595 	return acl;
596 }
597 
598 bool erofs_inode_has_noacl(struct inode *inode, void *kaddr, unsigned int ofs)
599 {
600 	static const unsigned int bitmask =
601 		BIT(21) |	/* system.posix_acl_default */
602 		BIT(30);	/* system.posix_acl_access */
603 	struct erofs_sb_info *sbi = EROFS_I_SB(inode);
604 	const struct erofs_xattr_ibody_header *ih = kaddr + ofs;
605 
606 	if (EROFS_I(inode)->xattr_isize < sizeof(*ih))
607 		return true;
608 
609 	if (erofs_sb_has_xattr_filter(sbi) && !sbi->xattr_filter_reserved &&
610 	    !check_add_overflow(ofs, sizeof(*ih), &ofs) &&
611 	    ofs <= i_blocksize(inode)) {
612 		if ((le32_to_cpu(ih->h_name_filter) & bitmask) == bitmask)
613 			return true;
614 	}
615 	return false;
616 }
617 #endif
618 
619 #ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE
620 int erofs_xattr_fill_inode_fingerprint(struct erofs_inode_fingerprint *fp,
621 				       struct inode *inode, const char *domain_id)
622 {
623 	struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
624 	struct erofs_xattr_prefix_item *prefix;
625 	const char *infix;
626 	int valuelen, base_index;
627 
628 	if (!test_opt(&sbi->opt, INODE_SHARE))
629 		return -EOPNOTSUPP;
630 	if (!sbi->xattr_prefixes)
631 		return -EINVAL;
632 	prefix = sbi->xattr_prefixes + sbi->ishare_xattr_prefix_id;
633 	infix = prefix->prefix->infix;
634 	base_index = prefix->prefix->base_index;
635 	valuelen = erofs_getxattr(inode, base_index, infix, NULL, 0);
636 	if (valuelen <= 0 || valuelen > (1 << sbi->blkszbits))
637 		return -EFSCORRUPTED;
638 	fp->size = valuelen + (domain_id ? strlen(domain_id) : 0);
639 	fp->opaque = kmalloc(fp->size, GFP_KERNEL);
640 	if (!fp->opaque)
641 		return -ENOMEM;
642 	if (valuelen != erofs_getxattr(inode, base_index, infix,
643 				       fp->opaque, valuelen)) {
644 		kfree(fp->opaque);
645 		fp->opaque = NULL;
646 		return -EFSCORRUPTED;
647 	}
648 	memcpy(fp->opaque + valuelen, domain_id, fp->size - valuelen);
649 	return 0;
650 }
651 #endif
652