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