xref: /openbmc/linux/fs/f2fs/xattr.c (revision eecfa427)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * fs/f2fs/xattr.c
4  *
5  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6  *             http://www.samsung.com/
7  *
8  * Portions of this code from linux/fs/ext2/xattr.c
9  *
10  * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
11  *
12  * Fix by Harrison Xing <harrison@mountainviewdata.com>.
13  * Extended attributes for symlinks and special files added per
14  *  suggestion of Luka Renko <luka.renko@hermes.si>.
15  * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
16  *  Red Hat Inc.
17  */
18 #include <linux/rwsem.h>
19 #include <linux/f2fs_fs.h>
20 #include <linux/security.h>
21 #include <linux/posix_acl_xattr.h>
22 #include "f2fs.h"
23 #include "xattr.h"
24 
25 static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
26 		struct dentry *unused, struct inode *inode,
27 		const char *name, void *buffer, size_t size)
28 {
29 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
30 
31 	switch (handler->flags) {
32 	case F2FS_XATTR_INDEX_USER:
33 		if (!test_opt(sbi, XATTR_USER))
34 			return -EOPNOTSUPP;
35 		break;
36 	case F2FS_XATTR_INDEX_TRUSTED:
37 	case F2FS_XATTR_INDEX_SECURITY:
38 		break;
39 	default:
40 		return -EINVAL;
41 	}
42 	return f2fs_getxattr(inode, handler->flags, name,
43 			     buffer, size, NULL);
44 }
45 
46 static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
47 		struct dentry *unused, struct inode *inode,
48 		const char *name, const void *value,
49 		size_t size, int flags)
50 {
51 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
52 
53 	switch (handler->flags) {
54 	case F2FS_XATTR_INDEX_USER:
55 		if (!test_opt(sbi, XATTR_USER))
56 			return -EOPNOTSUPP;
57 		break;
58 	case F2FS_XATTR_INDEX_TRUSTED:
59 	case F2FS_XATTR_INDEX_SECURITY:
60 		break;
61 	default:
62 		return -EINVAL;
63 	}
64 	return f2fs_setxattr(inode, handler->flags, name,
65 					value, size, NULL, flags);
66 }
67 
68 static bool f2fs_xattr_user_list(struct dentry *dentry)
69 {
70 	struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
71 
72 	return test_opt(sbi, XATTR_USER);
73 }
74 
75 static bool f2fs_xattr_trusted_list(struct dentry *dentry)
76 {
77 	return capable(CAP_SYS_ADMIN);
78 }
79 
80 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
81 		struct dentry *unused, struct inode *inode,
82 		const char *name, void *buffer, size_t size)
83 {
84 	if (buffer)
85 		*((char *)buffer) = F2FS_I(inode)->i_advise;
86 	return sizeof(char);
87 }
88 
89 static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
90 		struct dentry *unused, struct inode *inode,
91 		const char *name, const void *value,
92 		size_t size, int flags)
93 {
94 	unsigned char old_advise = F2FS_I(inode)->i_advise;
95 	unsigned char new_advise;
96 
97 	if (!inode_owner_or_capable(inode))
98 		return -EPERM;
99 	if (value == NULL)
100 		return -EINVAL;
101 
102 	new_advise = *(char *)value;
103 	if (new_advise & ~FADVISE_MODIFIABLE_BITS)
104 		return -EINVAL;
105 
106 	new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
107 	new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
108 
109 	F2FS_I(inode)->i_advise = new_advise;
110 	f2fs_mark_inode_dirty_sync(inode, true);
111 	return 0;
112 }
113 
114 #ifdef CONFIG_F2FS_FS_SECURITY
115 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
116 		void *page)
117 {
118 	const struct xattr *xattr;
119 	int err = 0;
120 
121 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
122 		err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
123 				xattr->name, xattr->value,
124 				xattr->value_len, (struct page *)page, 0);
125 		if (err < 0)
126 			break;
127 	}
128 	return err;
129 }
130 
131 int f2fs_init_security(struct inode *inode, struct inode *dir,
132 				const struct qstr *qstr, struct page *ipage)
133 {
134 	return security_inode_init_security(inode, dir, qstr,
135 				&f2fs_initxattrs, ipage);
136 }
137 #endif
138 
139 const struct xattr_handler f2fs_xattr_user_handler = {
140 	.prefix	= XATTR_USER_PREFIX,
141 	.flags	= F2FS_XATTR_INDEX_USER,
142 	.list	= f2fs_xattr_user_list,
143 	.get	= f2fs_xattr_generic_get,
144 	.set	= f2fs_xattr_generic_set,
145 };
146 
147 const struct xattr_handler f2fs_xattr_trusted_handler = {
148 	.prefix	= XATTR_TRUSTED_PREFIX,
149 	.flags	= F2FS_XATTR_INDEX_TRUSTED,
150 	.list	= f2fs_xattr_trusted_list,
151 	.get	= f2fs_xattr_generic_get,
152 	.set	= f2fs_xattr_generic_set,
153 };
154 
155 const struct xattr_handler f2fs_xattr_advise_handler = {
156 	.name	= F2FS_SYSTEM_ADVISE_NAME,
157 	.flags	= F2FS_XATTR_INDEX_ADVISE,
158 	.get    = f2fs_xattr_advise_get,
159 	.set    = f2fs_xattr_advise_set,
160 };
161 
162 const struct xattr_handler f2fs_xattr_security_handler = {
163 	.prefix	= XATTR_SECURITY_PREFIX,
164 	.flags	= F2FS_XATTR_INDEX_SECURITY,
165 	.get	= f2fs_xattr_generic_get,
166 	.set	= f2fs_xattr_generic_set,
167 };
168 
169 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
170 	[F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
171 #ifdef CONFIG_F2FS_FS_POSIX_ACL
172 	[F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
173 	[F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
174 #endif
175 	[F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
176 #ifdef CONFIG_F2FS_FS_SECURITY
177 	[F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
178 #endif
179 	[F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
180 };
181 
182 const struct xattr_handler *f2fs_xattr_handlers[] = {
183 	&f2fs_xattr_user_handler,
184 #ifdef CONFIG_F2FS_FS_POSIX_ACL
185 	&posix_acl_access_xattr_handler,
186 	&posix_acl_default_xattr_handler,
187 #endif
188 	&f2fs_xattr_trusted_handler,
189 #ifdef CONFIG_F2FS_FS_SECURITY
190 	&f2fs_xattr_security_handler,
191 #endif
192 	&f2fs_xattr_advise_handler,
193 	NULL,
194 };
195 
196 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
197 {
198 	const struct xattr_handler *handler = NULL;
199 
200 	if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
201 		handler = f2fs_xattr_handler_map[index];
202 	return handler;
203 }
204 
205 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
206 					size_t len, const char *name)
207 {
208 	struct f2fs_xattr_entry *entry;
209 
210 	list_for_each_xattr(entry, base_addr) {
211 		if (entry->e_name_index != index)
212 			continue;
213 		if (entry->e_name_len != len)
214 			continue;
215 		if (!memcmp(entry->e_name, name, len))
216 			break;
217 	}
218 	return entry;
219 }
220 
221 static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
222 				void *base_addr, void **last_addr, int index,
223 				size_t len, const char *name)
224 {
225 	struct f2fs_xattr_entry *entry;
226 	unsigned int inline_size = inline_xattr_size(inode);
227 
228 	list_for_each_xattr(entry, base_addr) {
229 		if ((void *)entry + sizeof(__u32) > base_addr + inline_size ||
230 			(void *)XATTR_NEXT_ENTRY(entry) + sizeof(__u32) >
231 			base_addr + inline_size) {
232 			*last_addr = entry;
233 			return NULL;
234 		}
235 		if (entry->e_name_index != index)
236 			continue;
237 		if (entry->e_name_len != len)
238 			continue;
239 		if (!memcmp(entry->e_name, name, len))
240 			break;
241 	}
242 	return entry;
243 }
244 
245 static int read_inline_xattr(struct inode *inode, struct page *ipage,
246 							void *txattr_addr)
247 {
248 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
249 	unsigned int inline_size = inline_xattr_size(inode);
250 	struct page *page = NULL;
251 	void *inline_addr;
252 
253 	if (ipage) {
254 		inline_addr = inline_xattr_addr(inode, ipage);
255 	} else {
256 		page = f2fs_get_node_page(sbi, inode->i_ino);
257 		if (IS_ERR(page))
258 			return PTR_ERR(page);
259 
260 		inline_addr = inline_xattr_addr(inode, page);
261 	}
262 	memcpy(txattr_addr, inline_addr, inline_size);
263 	f2fs_put_page(page, 1);
264 
265 	return 0;
266 }
267 
268 static int read_xattr_block(struct inode *inode, void *txattr_addr)
269 {
270 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
271 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
272 	unsigned int inline_size = inline_xattr_size(inode);
273 	struct page *xpage;
274 	void *xattr_addr;
275 
276 	/* The inode already has an extended attribute block. */
277 	xpage = f2fs_get_node_page(sbi, xnid);
278 	if (IS_ERR(xpage))
279 		return PTR_ERR(xpage);
280 
281 	xattr_addr = page_address(xpage);
282 	memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
283 	f2fs_put_page(xpage, 1);
284 
285 	return 0;
286 }
287 
288 static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
289 				unsigned int index, unsigned int len,
290 				const char *name, struct f2fs_xattr_entry **xe,
291 				void **base_addr, int *base_size)
292 {
293 	void *cur_addr, *txattr_addr, *last_addr = NULL;
294 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
295 	unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
296 	unsigned int inline_size = inline_xattr_size(inode);
297 	int err = 0;
298 
299 	if (!size && !inline_size)
300 		return -ENODATA;
301 
302 	*base_size = inline_size + size + XATTR_PADDING_SIZE;
303 	txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), *base_size, GFP_NOFS);
304 	if (!txattr_addr)
305 		return -ENOMEM;
306 
307 	/* read from inline xattr */
308 	if (inline_size) {
309 		err = read_inline_xattr(inode, ipage, txattr_addr);
310 		if (err)
311 			goto out;
312 
313 		*xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
314 						index, len, name);
315 		if (*xe) {
316 			*base_size = inline_size;
317 			goto check;
318 		}
319 	}
320 
321 	/* read from xattr node block */
322 	if (xnid) {
323 		err = read_xattr_block(inode, txattr_addr);
324 		if (err)
325 			goto out;
326 	}
327 
328 	if (last_addr)
329 		cur_addr = XATTR_HDR(last_addr) - 1;
330 	else
331 		cur_addr = txattr_addr;
332 
333 	*xe = __find_xattr(cur_addr, index, len, name);
334 check:
335 	if (IS_XATTR_LAST_ENTRY(*xe)) {
336 		err = -ENODATA;
337 		goto out;
338 	}
339 
340 	*base_addr = txattr_addr;
341 	return 0;
342 out:
343 	kzfree(txattr_addr);
344 	return err;
345 }
346 
347 static int read_all_xattrs(struct inode *inode, struct page *ipage,
348 							void **base_addr)
349 {
350 	struct f2fs_xattr_header *header;
351 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
352 	unsigned int size = VALID_XATTR_BLOCK_SIZE;
353 	unsigned int inline_size = inline_xattr_size(inode);
354 	void *txattr_addr;
355 	int err;
356 
357 	txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
358 			inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
359 	if (!txattr_addr)
360 		return -ENOMEM;
361 
362 	/* read from inline xattr */
363 	if (inline_size) {
364 		err = read_inline_xattr(inode, ipage, txattr_addr);
365 		if (err)
366 			goto fail;
367 	}
368 
369 	/* read from xattr node block */
370 	if (xnid) {
371 		err = read_xattr_block(inode, txattr_addr);
372 		if (err)
373 			goto fail;
374 	}
375 
376 	header = XATTR_HDR(txattr_addr);
377 
378 	/* never been allocated xattrs */
379 	if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
380 		header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
381 		header->h_refcount = cpu_to_le32(1);
382 	}
383 	*base_addr = txattr_addr;
384 	return 0;
385 fail:
386 	kzfree(txattr_addr);
387 	return err;
388 }
389 
390 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
391 				void *txattr_addr, struct page *ipage)
392 {
393 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
394 	size_t inline_size = inline_xattr_size(inode);
395 	struct page *in_page = NULL;
396 	void *xattr_addr;
397 	void *inline_addr = NULL;
398 	struct page *xpage;
399 	nid_t new_nid = 0;
400 	int err = 0;
401 
402 	if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
403 		if (!f2fs_alloc_nid(sbi, &new_nid))
404 			return -ENOSPC;
405 
406 	/* write to inline xattr */
407 	if (inline_size) {
408 		if (ipage) {
409 			inline_addr = inline_xattr_addr(inode, ipage);
410 		} else {
411 			in_page = f2fs_get_node_page(sbi, inode->i_ino);
412 			if (IS_ERR(in_page)) {
413 				f2fs_alloc_nid_failed(sbi, new_nid);
414 				return PTR_ERR(in_page);
415 			}
416 			inline_addr = inline_xattr_addr(inode, in_page);
417 		}
418 
419 		f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
420 							NODE, true, true);
421 		/* no need to use xattr node block */
422 		if (hsize <= inline_size) {
423 			err = f2fs_truncate_xattr_node(inode);
424 			f2fs_alloc_nid_failed(sbi, new_nid);
425 			if (err) {
426 				f2fs_put_page(in_page, 1);
427 				return err;
428 			}
429 			memcpy(inline_addr, txattr_addr, inline_size);
430 			set_page_dirty(ipage ? ipage : in_page);
431 			goto in_page_out;
432 		}
433 	}
434 
435 	/* write to xattr node block */
436 	if (F2FS_I(inode)->i_xattr_nid) {
437 		xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
438 		if (IS_ERR(xpage)) {
439 			err = PTR_ERR(xpage);
440 			f2fs_alloc_nid_failed(sbi, new_nid);
441 			goto in_page_out;
442 		}
443 		f2fs_bug_on(sbi, new_nid);
444 		f2fs_wait_on_page_writeback(xpage, NODE, true, true);
445 	} else {
446 		struct dnode_of_data dn;
447 		set_new_dnode(&dn, inode, NULL, NULL, new_nid);
448 		xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
449 		if (IS_ERR(xpage)) {
450 			err = PTR_ERR(xpage);
451 			f2fs_alloc_nid_failed(sbi, new_nid);
452 			goto in_page_out;
453 		}
454 		f2fs_alloc_nid_done(sbi, new_nid);
455 	}
456 	xattr_addr = page_address(xpage);
457 
458 	if (inline_size)
459 		memcpy(inline_addr, txattr_addr, inline_size);
460 	memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
461 
462 	if (inline_size)
463 		set_page_dirty(ipage ? ipage : in_page);
464 	set_page_dirty(xpage);
465 
466 	f2fs_put_page(xpage, 1);
467 in_page_out:
468 	f2fs_put_page(in_page, 1);
469 	return err;
470 }
471 
472 int f2fs_getxattr(struct inode *inode, int index, const char *name,
473 		void *buffer, size_t buffer_size, struct page *ipage)
474 {
475 	struct f2fs_xattr_entry *entry = NULL;
476 	int error = 0;
477 	unsigned int size, len;
478 	void *base_addr = NULL;
479 	int base_size;
480 
481 	if (name == NULL)
482 		return -EINVAL;
483 
484 	len = strlen(name);
485 	if (len > F2FS_NAME_LEN)
486 		return -ERANGE;
487 
488 	down_read(&F2FS_I(inode)->i_xattr_sem);
489 	error = lookup_all_xattrs(inode, ipage, index, len, name,
490 				&entry, &base_addr, &base_size);
491 	up_read(&F2FS_I(inode)->i_xattr_sem);
492 	if (error)
493 		return error;
494 
495 	size = le16_to_cpu(entry->e_value_size);
496 
497 	if (buffer && size > buffer_size) {
498 		error = -ERANGE;
499 		goto out;
500 	}
501 
502 	if (buffer) {
503 		char *pval = entry->e_name + entry->e_name_len;
504 
505 		if (base_size - (pval - (char *)base_addr) < size) {
506 			error = -ERANGE;
507 			goto out;
508 		}
509 		memcpy(buffer, pval, size);
510 	}
511 	error = size;
512 out:
513 	kzfree(base_addr);
514 	return error;
515 }
516 
517 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
518 {
519 	struct inode *inode = d_inode(dentry);
520 	struct f2fs_xattr_entry *entry;
521 	void *base_addr;
522 	int error = 0;
523 	size_t rest = buffer_size;
524 
525 	down_read(&F2FS_I(inode)->i_xattr_sem);
526 	error = read_all_xattrs(inode, NULL, &base_addr);
527 	up_read(&F2FS_I(inode)->i_xattr_sem);
528 	if (error)
529 		return error;
530 
531 	list_for_each_xattr(entry, base_addr) {
532 		const struct xattr_handler *handler =
533 			f2fs_xattr_handler(entry->e_name_index);
534 		const char *prefix;
535 		size_t prefix_len;
536 		size_t size;
537 
538 		if (!handler || (handler->list && !handler->list(dentry)))
539 			continue;
540 
541 		prefix = xattr_prefix(handler);
542 		prefix_len = strlen(prefix);
543 		size = prefix_len + entry->e_name_len + 1;
544 		if (buffer) {
545 			if (size > rest) {
546 				error = -ERANGE;
547 				goto cleanup;
548 			}
549 			memcpy(buffer, prefix, prefix_len);
550 			buffer += prefix_len;
551 			memcpy(buffer, entry->e_name, entry->e_name_len);
552 			buffer += entry->e_name_len;
553 			*buffer++ = 0;
554 		}
555 		rest -= size;
556 	}
557 	error = buffer_size - rest;
558 cleanup:
559 	kzfree(base_addr);
560 	return error;
561 }
562 
563 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
564 					const void *value, size_t size)
565 {
566 	void *pval = entry->e_name + entry->e_name_len;
567 
568 	return (le16_to_cpu(entry->e_value_size) == size) &&
569 					!memcmp(pval, value, size);
570 }
571 
572 static int __f2fs_setxattr(struct inode *inode, int index,
573 			const char *name, const void *value, size_t size,
574 			struct page *ipage, int flags)
575 {
576 	struct f2fs_xattr_entry *here, *last;
577 	void *base_addr;
578 	int found, newsize;
579 	size_t len;
580 	__u32 new_hsize;
581 	int error = 0;
582 
583 	if (name == NULL)
584 		return -EINVAL;
585 
586 	if (value == NULL)
587 		size = 0;
588 
589 	len = strlen(name);
590 
591 	if (len > F2FS_NAME_LEN)
592 		return -ERANGE;
593 
594 	if (size > MAX_VALUE_LEN(inode))
595 		return -E2BIG;
596 
597 	error = read_all_xattrs(inode, ipage, &base_addr);
598 	if (error)
599 		return error;
600 
601 	/* find entry with wanted name. */
602 	here = __find_xattr(base_addr, index, len, name);
603 
604 	found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
605 
606 	if (found) {
607 		if ((flags & XATTR_CREATE)) {
608 			error = -EEXIST;
609 			goto exit;
610 		}
611 
612 		if (value && f2fs_xattr_value_same(here, value, size))
613 			goto exit;
614 	} else if ((flags & XATTR_REPLACE)) {
615 		error = -ENODATA;
616 		goto exit;
617 	}
618 
619 	last = here;
620 	while (!IS_XATTR_LAST_ENTRY(last))
621 		last = XATTR_NEXT_ENTRY(last);
622 
623 	newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
624 
625 	/* 1. Check space */
626 	if (value) {
627 		int free;
628 		/*
629 		 * If value is NULL, it is remove operation.
630 		 * In case of update operation, we calculate free.
631 		 */
632 		free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
633 		if (found)
634 			free = free + ENTRY_SIZE(here);
635 
636 		if (unlikely(free < newsize)) {
637 			error = -E2BIG;
638 			goto exit;
639 		}
640 	}
641 
642 	/* 2. Remove old entry */
643 	if (found) {
644 		/*
645 		 * If entry is found, remove old entry.
646 		 * If not found, remove operation is not needed.
647 		 */
648 		struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
649 		int oldsize = ENTRY_SIZE(here);
650 
651 		memmove(here, next, (char *)last - (char *)next);
652 		last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
653 		memset(last, 0, oldsize);
654 	}
655 
656 	new_hsize = (char *)last - (char *)base_addr;
657 
658 	/* 3. Write new entry */
659 	if (value) {
660 		char *pval;
661 		/*
662 		 * Before we come here, old entry is removed.
663 		 * We just write new entry.
664 		 */
665 		last->e_name_index = index;
666 		last->e_name_len = len;
667 		memcpy(last->e_name, name, len);
668 		pval = last->e_name + len;
669 		memcpy(pval, value, size);
670 		last->e_value_size = cpu_to_le16(size);
671 		new_hsize += newsize;
672 	}
673 
674 	error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
675 	if (error)
676 		goto exit;
677 
678 	if (is_inode_flag_set(inode, FI_ACL_MODE)) {
679 		inode->i_mode = F2FS_I(inode)->i_acl_mode;
680 		inode->i_ctime = current_time(inode);
681 		clear_inode_flag(inode, FI_ACL_MODE);
682 	}
683 	if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
684 			!strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
685 		f2fs_set_encrypted_inode(inode);
686 	f2fs_mark_inode_dirty_sync(inode, true);
687 	if (!error && S_ISDIR(inode->i_mode))
688 		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
689 exit:
690 	kzfree(base_addr);
691 	return error;
692 }
693 
694 int f2fs_setxattr(struct inode *inode, int index, const char *name,
695 				const void *value, size_t size,
696 				struct page *ipage, int flags)
697 {
698 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
699 	int err;
700 
701 	err = dquot_initialize(inode);
702 	if (err)
703 		return err;
704 
705 	/* this case is only from f2fs_init_inode_metadata */
706 	if (ipage)
707 		return __f2fs_setxattr(inode, index, name, value,
708 						size, ipage, flags);
709 	f2fs_balance_fs(sbi, true);
710 
711 	f2fs_lock_op(sbi);
712 	/* protect xattr_ver */
713 	down_write(&F2FS_I(inode)->i_sem);
714 	down_write(&F2FS_I(inode)->i_xattr_sem);
715 	err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
716 	up_write(&F2FS_I(inode)->i_xattr_sem);
717 	up_write(&F2FS_I(inode)->i_sem);
718 	f2fs_unlock_op(sbi);
719 
720 	f2fs_update_time(sbi, REQ_TIME);
721 	return err;
722 }
723