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