xref: /openbmc/linux/fs/f2fs/xattr.c (revision e657c18a)
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 	void *max_addr = base_addr + inline_size;
228 
229 	list_for_each_xattr(entry, base_addr) {
230 		if ((void *)entry + sizeof(__u32) > max_addr ||
231 			(void *)XATTR_NEXT_ENTRY(entry) > max_addr) {
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 
243 	/* inline xattr header or entry across max inline xattr size */
244 	if (IS_XATTR_LAST_ENTRY(entry) &&
245 		(void *)entry + sizeof(__u32) > max_addr) {
246 		*last_addr = entry;
247 		return NULL;
248 	}
249 	return entry;
250 }
251 
252 static int read_inline_xattr(struct inode *inode, struct page *ipage,
253 							void *txattr_addr)
254 {
255 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
256 	unsigned int inline_size = inline_xattr_size(inode);
257 	struct page *page = NULL;
258 	void *inline_addr;
259 
260 	if (ipage) {
261 		inline_addr = inline_xattr_addr(inode, ipage);
262 	} else {
263 		page = f2fs_get_node_page(sbi, inode->i_ino);
264 		if (IS_ERR(page))
265 			return PTR_ERR(page);
266 
267 		inline_addr = inline_xattr_addr(inode, page);
268 	}
269 	memcpy(txattr_addr, inline_addr, inline_size);
270 	f2fs_put_page(page, 1);
271 
272 	return 0;
273 }
274 
275 static int read_xattr_block(struct inode *inode, void *txattr_addr)
276 {
277 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
278 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
279 	unsigned int inline_size = inline_xattr_size(inode);
280 	struct page *xpage;
281 	void *xattr_addr;
282 
283 	/* The inode already has an extended attribute block. */
284 	xpage = f2fs_get_node_page(sbi, xnid);
285 	if (IS_ERR(xpage))
286 		return PTR_ERR(xpage);
287 
288 	xattr_addr = page_address(xpage);
289 	memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
290 	f2fs_put_page(xpage, 1);
291 
292 	return 0;
293 }
294 
295 static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
296 				unsigned int index, unsigned int len,
297 				const char *name, struct f2fs_xattr_entry **xe,
298 				void **base_addr, int *base_size)
299 {
300 	void *cur_addr, *txattr_addr, *last_addr = NULL;
301 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
302 	unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
303 	unsigned int inline_size = inline_xattr_size(inode);
304 	int err = 0;
305 
306 	if (!size && !inline_size)
307 		return -ENODATA;
308 
309 	*base_size = inline_size + size + XATTR_PADDING_SIZE;
310 	txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), *base_size, GFP_NOFS);
311 	if (!txattr_addr)
312 		return -ENOMEM;
313 
314 	/* read from inline xattr */
315 	if (inline_size) {
316 		err = read_inline_xattr(inode, ipage, txattr_addr);
317 		if (err)
318 			goto out;
319 
320 		*xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
321 						index, len, name);
322 		if (*xe) {
323 			*base_size = inline_size;
324 			goto check;
325 		}
326 	}
327 
328 	/* read from xattr node block */
329 	if (xnid) {
330 		err = read_xattr_block(inode, txattr_addr);
331 		if (err)
332 			goto out;
333 	}
334 
335 	if (last_addr)
336 		cur_addr = XATTR_HDR(last_addr) - 1;
337 	else
338 		cur_addr = txattr_addr;
339 
340 	*xe = __find_xattr(cur_addr, index, len, name);
341 check:
342 	if (IS_XATTR_LAST_ENTRY(*xe)) {
343 		err = -ENODATA;
344 		goto out;
345 	}
346 
347 	*base_addr = txattr_addr;
348 	return 0;
349 out:
350 	kvfree(txattr_addr);
351 	return err;
352 }
353 
354 static int read_all_xattrs(struct inode *inode, struct page *ipage,
355 							void **base_addr)
356 {
357 	struct f2fs_xattr_header *header;
358 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
359 	unsigned int size = VALID_XATTR_BLOCK_SIZE;
360 	unsigned int inline_size = inline_xattr_size(inode);
361 	void *txattr_addr;
362 	int err;
363 
364 	txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
365 			inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
366 	if (!txattr_addr)
367 		return -ENOMEM;
368 
369 	/* read from inline xattr */
370 	if (inline_size) {
371 		err = read_inline_xattr(inode, ipage, txattr_addr);
372 		if (err)
373 			goto fail;
374 	}
375 
376 	/* read from xattr node block */
377 	if (xnid) {
378 		err = read_xattr_block(inode, txattr_addr);
379 		if (err)
380 			goto fail;
381 	}
382 
383 	header = XATTR_HDR(txattr_addr);
384 
385 	/* never been allocated xattrs */
386 	if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
387 		header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
388 		header->h_refcount = cpu_to_le32(1);
389 	}
390 	*base_addr = txattr_addr;
391 	return 0;
392 fail:
393 	kvfree(txattr_addr);
394 	return err;
395 }
396 
397 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
398 				void *txattr_addr, struct page *ipage)
399 {
400 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
401 	size_t inline_size = inline_xattr_size(inode);
402 	struct page *in_page = NULL;
403 	void *xattr_addr;
404 	void *inline_addr = NULL;
405 	struct page *xpage;
406 	nid_t new_nid = 0;
407 	int err = 0;
408 
409 	if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
410 		if (!f2fs_alloc_nid(sbi, &new_nid))
411 			return -ENOSPC;
412 
413 	/* write to inline xattr */
414 	if (inline_size) {
415 		if (ipage) {
416 			inline_addr = inline_xattr_addr(inode, ipage);
417 		} else {
418 			in_page = f2fs_get_node_page(sbi, inode->i_ino);
419 			if (IS_ERR(in_page)) {
420 				f2fs_alloc_nid_failed(sbi, new_nid);
421 				return PTR_ERR(in_page);
422 			}
423 			inline_addr = inline_xattr_addr(inode, in_page);
424 		}
425 
426 		f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
427 							NODE, true, true);
428 		/* no need to use xattr node block */
429 		if (hsize <= inline_size) {
430 			err = f2fs_truncate_xattr_node(inode);
431 			f2fs_alloc_nid_failed(sbi, new_nid);
432 			if (err) {
433 				f2fs_put_page(in_page, 1);
434 				return err;
435 			}
436 			memcpy(inline_addr, txattr_addr, inline_size);
437 			set_page_dirty(ipage ? ipage : in_page);
438 			goto in_page_out;
439 		}
440 	}
441 
442 	/* write to xattr node block */
443 	if (F2FS_I(inode)->i_xattr_nid) {
444 		xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
445 		if (IS_ERR(xpage)) {
446 			err = PTR_ERR(xpage);
447 			f2fs_alloc_nid_failed(sbi, new_nid);
448 			goto in_page_out;
449 		}
450 		f2fs_bug_on(sbi, new_nid);
451 		f2fs_wait_on_page_writeback(xpage, NODE, true, true);
452 	} else {
453 		struct dnode_of_data dn;
454 		set_new_dnode(&dn, inode, NULL, NULL, new_nid);
455 		xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
456 		if (IS_ERR(xpage)) {
457 			err = PTR_ERR(xpage);
458 			f2fs_alloc_nid_failed(sbi, new_nid);
459 			goto in_page_out;
460 		}
461 		f2fs_alloc_nid_done(sbi, new_nid);
462 	}
463 	xattr_addr = page_address(xpage);
464 
465 	if (inline_size)
466 		memcpy(inline_addr, txattr_addr, inline_size);
467 	memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
468 
469 	if (inline_size)
470 		set_page_dirty(ipage ? ipage : in_page);
471 	set_page_dirty(xpage);
472 
473 	f2fs_put_page(xpage, 1);
474 in_page_out:
475 	f2fs_put_page(in_page, 1);
476 	return err;
477 }
478 
479 int f2fs_getxattr(struct inode *inode, int index, const char *name,
480 		void *buffer, size_t buffer_size, struct page *ipage)
481 {
482 	struct f2fs_xattr_entry *entry = NULL;
483 	int error = 0;
484 	unsigned int size, len;
485 	void *base_addr = NULL;
486 	int base_size;
487 
488 	if (name == NULL)
489 		return -EINVAL;
490 
491 	len = strlen(name);
492 	if (len > F2FS_NAME_LEN)
493 		return -ERANGE;
494 
495 	down_read(&F2FS_I(inode)->i_xattr_sem);
496 	error = lookup_all_xattrs(inode, ipage, index, len, name,
497 				&entry, &base_addr, &base_size);
498 	up_read(&F2FS_I(inode)->i_xattr_sem);
499 	if (error)
500 		return error;
501 
502 	size = le16_to_cpu(entry->e_value_size);
503 
504 	if (buffer && size > buffer_size) {
505 		error = -ERANGE;
506 		goto out;
507 	}
508 
509 	if (buffer) {
510 		char *pval = entry->e_name + entry->e_name_len;
511 
512 		if (base_size - (pval - (char *)base_addr) < size) {
513 			error = -ERANGE;
514 			goto out;
515 		}
516 		memcpy(buffer, pval, size);
517 	}
518 	error = size;
519 out:
520 	kvfree(base_addr);
521 	return error;
522 }
523 
524 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
525 {
526 	struct inode *inode = d_inode(dentry);
527 	struct f2fs_xattr_entry *entry;
528 	void *base_addr;
529 	int error = 0;
530 	size_t rest = buffer_size;
531 
532 	down_read(&F2FS_I(inode)->i_xattr_sem);
533 	error = read_all_xattrs(inode, NULL, &base_addr);
534 	up_read(&F2FS_I(inode)->i_xattr_sem);
535 	if (error)
536 		return error;
537 
538 	list_for_each_xattr(entry, base_addr) {
539 		const struct xattr_handler *handler =
540 			f2fs_xattr_handler(entry->e_name_index);
541 		const char *prefix;
542 		size_t prefix_len;
543 		size_t size;
544 
545 		if (!handler || (handler->list && !handler->list(dentry)))
546 			continue;
547 
548 		prefix = xattr_prefix(handler);
549 		prefix_len = strlen(prefix);
550 		size = prefix_len + entry->e_name_len + 1;
551 		if (buffer) {
552 			if (size > rest) {
553 				error = -ERANGE;
554 				goto cleanup;
555 			}
556 			memcpy(buffer, prefix, prefix_len);
557 			buffer += prefix_len;
558 			memcpy(buffer, entry->e_name, entry->e_name_len);
559 			buffer += entry->e_name_len;
560 			*buffer++ = 0;
561 		}
562 		rest -= size;
563 	}
564 	error = buffer_size - rest;
565 cleanup:
566 	kvfree(base_addr);
567 	return error;
568 }
569 
570 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
571 					const void *value, size_t size)
572 {
573 	void *pval = entry->e_name + entry->e_name_len;
574 
575 	return (le16_to_cpu(entry->e_value_size) == size) &&
576 					!memcmp(pval, value, size);
577 }
578 
579 static int __f2fs_setxattr(struct inode *inode, int index,
580 			const char *name, const void *value, size_t size,
581 			struct page *ipage, int flags)
582 {
583 	struct f2fs_xattr_entry *here, *last;
584 	void *base_addr;
585 	int found, newsize;
586 	size_t len;
587 	__u32 new_hsize;
588 	int error = 0;
589 
590 	if (name == NULL)
591 		return -EINVAL;
592 
593 	if (value == NULL)
594 		size = 0;
595 
596 	len = strlen(name);
597 
598 	if (len > F2FS_NAME_LEN)
599 		return -ERANGE;
600 
601 	if (size > MAX_VALUE_LEN(inode))
602 		return -E2BIG;
603 
604 	error = read_all_xattrs(inode, ipage, &base_addr);
605 	if (error)
606 		return error;
607 
608 	/* find entry with wanted name. */
609 	here = __find_xattr(base_addr, index, len, name);
610 
611 	found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
612 
613 	if (found) {
614 		if ((flags & XATTR_CREATE)) {
615 			error = -EEXIST;
616 			goto exit;
617 		}
618 
619 		if (value && f2fs_xattr_value_same(here, value, size))
620 			goto exit;
621 	} else if ((flags & XATTR_REPLACE)) {
622 		error = -ENODATA;
623 		goto exit;
624 	}
625 
626 	last = here;
627 	while (!IS_XATTR_LAST_ENTRY(last))
628 		last = XATTR_NEXT_ENTRY(last);
629 
630 	newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
631 
632 	/* 1. Check space */
633 	if (value) {
634 		int free;
635 		/*
636 		 * If value is NULL, it is remove operation.
637 		 * In case of update operation, we calculate free.
638 		 */
639 		free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
640 		if (found)
641 			free = free + ENTRY_SIZE(here);
642 
643 		if (unlikely(free < newsize)) {
644 			error = -E2BIG;
645 			goto exit;
646 		}
647 	}
648 
649 	/* 2. Remove old entry */
650 	if (found) {
651 		/*
652 		 * If entry is found, remove old entry.
653 		 * If not found, remove operation is not needed.
654 		 */
655 		struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
656 		int oldsize = ENTRY_SIZE(here);
657 
658 		memmove(here, next, (char *)last - (char *)next);
659 		last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
660 		memset(last, 0, oldsize);
661 	}
662 
663 	new_hsize = (char *)last - (char *)base_addr;
664 
665 	/* 3. Write new entry */
666 	if (value) {
667 		char *pval;
668 		/*
669 		 * Before we come here, old entry is removed.
670 		 * We just write new entry.
671 		 */
672 		last->e_name_index = index;
673 		last->e_name_len = len;
674 		memcpy(last->e_name, name, len);
675 		pval = last->e_name + len;
676 		memcpy(pval, value, size);
677 		last->e_value_size = cpu_to_le16(size);
678 		new_hsize += newsize;
679 	}
680 
681 	error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
682 	if (error)
683 		goto exit;
684 
685 	if (is_inode_flag_set(inode, FI_ACL_MODE)) {
686 		inode->i_mode = F2FS_I(inode)->i_acl_mode;
687 		inode->i_ctime = current_time(inode);
688 		clear_inode_flag(inode, FI_ACL_MODE);
689 	}
690 	if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
691 			!strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
692 		f2fs_set_encrypted_inode(inode);
693 	f2fs_mark_inode_dirty_sync(inode, true);
694 	if (!error && S_ISDIR(inode->i_mode))
695 		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
696 exit:
697 	kvfree(base_addr);
698 	return error;
699 }
700 
701 int f2fs_setxattr(struct inode *inode, int index, const char *name,
702 				const void *value, size_t size,
703 				struct page *ipage, int flags)
704 {
705 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
706 	int err;
707 
708 	err = dquot_initialize(inode);
709 	if (err)
710 		return err;
711 
712 	/* this case is only from f2fs_init_inode_metadata */
713 	if (ipage)
714 		return __f2fs_setxattr(inode, index, name, value,
715 						size, ipage, flags);
716 	f2fs_balance_fs(sbi, true);
717 
718 	f2fs_lock_op(sbi);
719 	/* protect xattr_ver */
720 	down_write(&F2FS_I(inode)->i_sem);
721 	down_write(&F2FS_I(inode)->i_xattr_sem);
722 	err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
723 	up_write(&F2FS_I(inode)->i_xattr_sem);
724 	up_write(&F2FS_I(inode)->i_sem);
725 	f2fs_unlock_op(sbi);
726 
727 	f2fs_update_time(sbi, REQ_TIME);
728 	return err;
729 }
730