1 /*
2  * fs/f2fs/xattr.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  *
7  * Portions of this code from linux/fs/ext2/xattr.c
8  *
9  * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
10  *
11  * Fix by Harrison Xing <harrison@mountainviewdata.com>.
12  * Extended attributes for symlinks and special files added per
13  *  suggestion of Luka Renko <luka.renko@hermes.si>.
14  * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
15  *  Red Hat Inc.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License version 2 as
19  * published by the Free Software Foundation.
20  */
21 #include <linux/rwsem.h>
22 #include <linux/f2fs_fs.h>
23 #include <linux/security.h>
24 #include <linux/posix_acl_xattr.h>
25 #include "f2fs.h"
26 #include "xattr.h"
27 
f2fs_xattr_generic_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)28 static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
29 		struct dentry *unused, struct inode *inode,
30 		const char *name, void *buffer, size_t size)
31 {
32 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
33 
34 	switch (handler->flags) {
35 	case F2FS_XATTR_INDEX_USER:
36 		if (!test_opt(sbi, XATTR_USER))
37 			return -EOPNOTSUPP;
38 		break;
39 	case F2FS_XATTR_INDEX_TRUSTED:
40 	case F2FS_XATTR_INDEX_SECURITY:
41 		break;
42 	default:
43 		return -EINVAL;
44 	}
45 	return f2fs_getxattr(inode, handler->flags, name,
46 			     buffer, size, NULL);
47 }
48 
f2fs_xattr_generic_set(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)49 static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
50 		struct dentry *unused, struct inode *inode,
51 		const char *name, const void *value,
52 		size_t size, int flags)
53 {
54 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
55 
56 	switch (handler->flags) {
57 	case F2FS_XATTR_INDEX_USER:
58 		if (!test_opt(sbi, XATTR_USER))
59 			return -EOPNOTSUPP;
60 		break;
61 	case F2FS_XATTR_INDEX_TRUSTED:
62 	case F2FS_XATTR_INDEX_SECURITY:
63 		break;
64 	default:
65 		return -EINVAL;
66 	}
67 	return f2fs_setxattr(inode, handler->flags, name,
68 					value, size, NULL, flags);
69 }
70 
f2fs_xattr_user_list(struct dentry * dentry)71 static bool f2fs_xattr_user_list(struct dentry *dentry)
72 {
73 	struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
74 
75 	return test_opt(sbi, XATTR_USER);
76 }
77 
f2fs_xattr_trusted_list(struct dentry * dentry)78 static bool f2fs_xattr_trusted_list(struct dentry *dentry)
79 {
80 	return capable(CAP_SYS_ADMIN);
81 }
82 
f2fs_xattr_advise_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)83 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
84 		struct dentry *unused, struct inode *inode,
85 		const char *name, void *buffer, size_t size)
86 {
87 	if (buffer)
88 		*((char *)buffer) = F2FS_I(inode)->i_advise;
89 	return sizeof(char);
90 }
91 
f2fs_xattr_advise_set(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)92 static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
93 		struct dentry *unused, struct inode *inode,
94 		const char *name, const void *value,
95 		size_t size, int flags)
96 {
97 	unsigned char old_advise = F2FS_I(inode)->i_advise;
98 	unsigned char new_advise;
99 
100 	if (!inode_owner_or_capable(inode))
101 		return -EPERM;
102 	if (value == NULL)
103 		return -EINVAL;
104 
105 	new_advise = *(char *)value;
106 	if (new_advise & ~FADVISE_MODIFIABLE_BITS)
107 		return -EINVAL;
108 
109 	new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
110 	new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
111 
112 	F2FS_I(inode)->i_advise = new_advise;
113 	f2fs_mark_inode_dirty_sync(inode, true);
114 	return 0;
115 }
116 
117 #ifdef CONFIG_F2FS_FS_SECURITY
f2fs_initxattrs(struct inode * inode,const struct xattr * xattr_array,void * page)118 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
119 		void *page)
120 {
121 	const struct xattr *xattr;
122 	int err = 0;
123 
124 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
125 		err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
126 				xattr->name, xattr->value,
127 				xattr->value_len, (struct page *)page, 0);
128 		if (err < 0)
129 			break;
130 	}
131 	return err;
132 }
133 
f2fs_init_security(struct inode * inode,struct inode * dir,const struct qstr * qstr,struct page * ipage)134 int f2fs_init_security(struct inode *inode, struct inode *dir,
135 				const struct qstr *qstr, struct page *ipage)
136 {
137 	return security_inode_init_security(inode, dir, qstr,
138 				&f2fs_initxattrs, ipage);
139 }
140 #endif
141 
142 const struct xattr_handler f2fs_xattr_user_handler = {
143 	.prefix	= XATTR_USER_PREFIX,
144 	.flags	= F2FS_XATTR_INDEX_USER,
145 	.list	= f2fs_xattr_user_list,
146 	.get	= f2fs_xattr_generic_get,
147 	.set	= f2fs_xattr_generic_set,
148 };
149 
150 const struct xattr_handler f2fs_xattr_trusted_handler = {
151 	.prefix	= XATTR_TRUSTED_PREFIX,
152 	.flags	= F2FS_XATTR_INDEX_TRUSTED,
153 	.list	= f2fs_xattr_trusted_list,
154 	.get	= f2fs_xattr_generic_get,
155 	.set	= f2fs_xattr_generic_set,
156 };
157 
158 const struct xattr_handler f2fs_xattr_advise_handler = {
159 	.name	= F2FS_SYSTEM_ADVISE_NAME,
160 	.flags	= F2FS_XATTR_INDEX_ADVISE,
161 	.get    = f2fs_xattr_advise_get,
162 	.set    = f2fs_xattr_advise_set,
163 };
164 
165 const struct xattr_handler f2fs_xattr_security_handler = {
166 	.prefix	= XATTR_SECURITY_PREFIX,
167 	.flags	= F2FS_XATTR_INDEX_SECURITY,
168 	.get	= f2fs_xattr_generic_get,
169 	.set	= f2fs_xattr_generic_set,
170 };
171 
172 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
173 	[F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
174 #ifdef CONFIG_F2FS_FS_POSIX_ACL
175 	[F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
176 	[F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
177 #endif
178 	[F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
179 #ifdef CONFIG_F2FS_FS_SECURITY
180 	[F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
181 #endif
182 	[F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
183 };
184 
185 const struct xattr_handler *f2fs_xattr_handlers[] = {
186 	&f2fs_xattr_user_handler,
187 #ifdef CONFIG_F2FS_FS_POSIX_ACL
188 	&posix_acl_access_xattr_handler,
189 	&posix_acl_default_xattr_handler,
190 #endif
191 	&f2fs_xattr_trusted_handler,
192 #ifdef CONFIG_F2FS_FS_SECURITY
193 	&f2fs_xattr_security_handler,
194 #endif
195 	&f2fs_xattr_advise_handler,
196 	NULL,
197 };
198 
f2fs_xattr_handler(int index)199 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
200 {
201 	const struct xattr_handler *handler = NULL;
202 
203 	if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
204 		handler = f2fs_xattr_handler_map[index];
205 	return handler;
206 }
207 
__find_xattr(void * base_addr,void * last_base_addr,int index,size_t len,const char * name)208 static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
209 				void *last_base_addr, int index,
210 				size_t len, const char *name)
211 {
212 	struct f2fs_xattr_entry *entry;
213 
214 	list_for_each_xattr(entry, base_addr) {
215 		if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
216 			(void *)XATTR_NEXT_ENTRY(entry) > last_base_addr)
217 			return NULL;
218 
219 		if (entry->e_name_index != index)
220 			continue;
221 		if (entry->e_name_len != len)
222 			continue;
223 		if (!memcmp(entry->e_name, name, len))
224 			break;
225 	}
226 	return entry;
227 }
228 
__find_inline_xattr(struct inode * inode,void * base_addr,void ** last_addr,int index,size_t len,const char * name)229 static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
230 				void *base_addr, void **last_addr, int index,
231 				size_t len, const char *name)
232 {
233 	struct f2fs_xattr_entry *entry;
234 	unsigned int inline_size = inline_xattr_size(inode);
235 	void *max_addr = base_addr + inline_size;
236 
237 	list_for_each_xattr(entry, base_addr) {
238 		if ((void *)entry + sizeof(__u32) > max_addr ||
239 			(void *)XATTR_NEXT_ENTRY(entry) > max_addr) {
240 			*last_addr = entry;
241 			return NULL;
242 		}
243 		if (entry->e_name_index != index)
244 			continue;
245 		if (entry->e_name_len != len)
246 			continue;
247 		if (!memcmp(entry->e_name, name, len))
248 			break;
249 	}
250 
251 	/* inline xattr header or entry across max inline xattr size */
252 	if (IS_XATTR_LAST_ENTRY(entry) &&
253 		(void *)entry + sizeof(__u32) > max_addr) {
254 		*last_addr = entry;
255 		return NULL;
256 	}
257 	return entry;
258 }
259 
read_inline_xattr(struct inode * inode,struct page * ipage,void * txattr_addr)260 static int read_inline_xattr(struct inode *inode, struct page *ipage,
261 							void *txattr_addr)
262 {
263 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
264 	unsigned int inline_size = inline_xattr_size(inode);
265 	struct page *page = NULL;
266 	void *inline_addr;
267 
268 	if (ipage) {
269 		inline_addr = inline_xattr_addr(inode, ipage);
270 	} else {
271 		page = f2fs_get_node_page(sbi, inode->i_ino);
272 		if (IS_ERR(page))
273 			return PTR_ERR(page);
274 
275 		inline_addr = inline_xattr_addr(inode, page);
276 	}
277 	memcpy(txattr_addr, inline_addr, inline_size);
278 	f2fs_put_page(page, 1);
279 
280 	return 0;
281 }
282 
read_xattr_block(struct inode * inode,void * txattr_addr)283 static int read_xattr_block(struct inode *inode, void *txattr_addr)
284 {
285 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
286 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
287 	unsigned int inline_size = inline_xattr_size(inode);
288 	struct page *xpage;
289 	void *xattr_addr;
290 
291 	/* The inode already has an extended attribute block. */
292 	xpage = f2fs_get_node_page(sbi, xnid);
293 	if (IS_ERR(xpage))
294 		return PTR_ERR(xpage);
295 
296 	xattr_addr = page_address(xpage);
297 	memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
298 	f2fs_put_page(xpage, 1);
299 
300 	return 0;
301 }
302 
lookup_all_xattrs(struct inode * inode,struct page * ipage,unsigned int index,unsigned int len,const char * name,struct f2fs_xattr_entry ** xe,void ** base_addr,int * base_size)303 static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
304 				unsigned int index, unsigned int len,
305 				const char *name, struct f2fs_xattr_entry **xe,
306 				void **base_addr, int *base_size)
307 {
308 	void *cur_addr, *txattr_addr, *last_txattr_addr;
309 	void *last_addr = NULL;
310 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
311 	unsigned int inline_size = inline_xattr_size(inode);
312 	int err = 0;
313 
314 	if (!xnid && !inline_size)
315 		return -ENODATA;
316 
317 	*base_size = XATTR_SIZE(xnid, inode) + XATTR_PADDING_SIZE;
318 	txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), *base_size, GFP_NOFS);
319 	if (!txattr_addr)
320 		return -ENOMEM;
321 
322 	last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(xnid, inode);
323 
324 	/* read from inline xattr */
325 	if (inline_size) {
326 		err = read_inline_xattr(inode, ipage, txattr_addr);
327 		if (err)
328 			goto out;
329 
330 		*xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
331 						index, len, name);
332 		if (*xe) {
333 			*base_size = inline_size;
334 			goto check;
335 		}
336 	}
337 
338 	/* read from xattr node block */
339 	if (xnid) {
340 		err = read_xattr_block(inode, txattr_addr);
341 		if (err)
342 			goto out;
343 	}
344 
345 	if (last_addr)
346 		cur_addr = XATTR_HDR(last_addr) - 1;
347 	else
348 		cur_addr = txattr_addr;
349 
350 	*xe = __find_xattr(cur_addr, last_txattr_addr, index, len, name);
351 	if (!*xe) {
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 	kzfree(txattr_addr);
365 	return err;
366 }
367 
read_all_xattrs(struct inode * inode,struct page * ipage,void ** base_addr)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 	kzfree(txattr_addr);
408 	return err;
409 }
410 
write_all_xattrs(struct inode * inode,__u32 hsize,void * txattr_addr,struct page * ipage)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);
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);
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 
f2fs_getxattr(struct inode * inode,int index,const char * name,void * buffer,size_t buffer_size,struct page * ipage)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 	kzfree(base_addr);
535 	return error;
536 }
537 
f2fs_listxattr(struct dentry * dentry,char * buffer,size_t buffer_size)538 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
539 {
540 	struct inode *inode = d_inode(dentry);
541 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
542 	struct f2fs_xattr_entry *entry;
543 	void *base_addr, *last_base_addr;
544 	int error = 0;
545 	size_t rest = buffer_size;
546 
547 	down_read(&F2FS_I(inode)->i_xattr_sem);
548 	error = read_all_xattrs(inode, NULL, &base_addr);
549 	up_read(&F2FS_I(inode)->i_xattr_sem);
550 	if (error)
551 		return error;
552 
553 	last_base_addr = (void *)base_addr + XATTR_SIZE(xnid, inode);
554 
555 	list_for_each_xattr(entry, base_addr) {
556 		const struct xattr_handler *handler =
557 			f2fs_xattr_handler(entry->e_name_index);
558 		const char *prefix;
559 		size_t prefix_len;
560 		size_t size;
561 
562 		if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
563 			(void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
564 			f2fs_msg(dentry->d_sb, KERN_ERR,
565 				 "inode (%lu) has corrupted xattr",
566 				 inode->i_ino);
567 			set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
568 			error = -EFSCORRUPTED;
569 			goto cleanup;
570 		}
571 
572 		if (!handler || (handler->list && !handler->list(dentry)))
573 			continue;
574 
575 		prefix = handler->prefix ?: handler->name;
576 		prefix_len = strlen(prefix);
577 		size = prefix_len + entry->e_name_len + 1;
578 		if (buffer) {
579 			if (size > rest) {
580 				error = -ERANGE;
581 				goto cleanup;
582 			}
583 			memcpy(buffer, prefix, prefix_len);
584 			buffer += prefix_len;
585 			memcpy(buffer, entry->e_name, entry->e_name_len);
586 			buffer += entry->e_name_len;
587 			*buffer++ = 0;
588 		}
589 		rest -= size;
590 	}
591 	error = buffer_size - rest;
592 cleanup:
593 	kzfree(base_addr);
594 	return error;
595 }
596 
f2fs_xattr_value_same(struct f2fs_xattr_entry * entry,const void * value,size_t size)597 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
598 					const void *value, size_t size)
599 {
600 	void *pval = entry->e_name + entry->e_name_len;
601 
602 	return (le16_to_cpu(entry->e_value_size) == size) &&
603 					!memcmp(pval, value, size);
604 }
605 
__f2fs_setxattr(struct inode * inode,int index,const char * name,const void * value,size_t size,struct page * ipage,int flags)606 static int __f2fs_setxattr(struct inode *inode, int index,
607 			const char *name, const void *value, size_t size,
608 			struct page *ipage, int flags)
609 {
610 	struct f2fs_xattr_entry *here, *last;
611 	void *base_addr, *last_base_addr;
612 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
613 	int found, newsize;
614 	size_t len;
615 	__u32 new_hsize;
616 	int error = 0;
617 
618 	if (name == NULL)
619 		return -EINVAL;
620 
621 	if (value == NULL)
622 		size = 0;
623 
624 	len = strlen(name);
625 
626 	if (len > F2FS_NAME_LEN)
627 		return -ERANGE;
628 
629 	if (size > MAX_VALUE_LEN(inode))
630 		return -E2BIG;
631 
632 	error = read_all_xattrs(inode, ipage, &base_addr);
633 	if (error)
634 		return error;
635 
636 	last_base_addr = (void *)base_addr + XATTR_SIZE(xnid, inode);
637 
638 	/* find entry with wanted name. */
639 	here = __find_xattr(base_addr, last_base_addr, index, len, name);
640 	if (!here) {
641 		error = -EFSCORRUPTED;
642 		goto exit;
643 	}
644 
645 	found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
646 
647 	if (found) {
648 		if ((flags & XATTR_CREATE)) {
649 			error = -EEXIST;
650 			goto exit;
651 		}
652 
653 		if (value && f2fs_xattr_value_same(here, value, size))
654 			goto exit;
655 	} else if ((flags & XATTR_REPLACE)) {
656 		error = -ENODATA;
657 		goto exit;
658 	}
659 
660 	last = here;
661 	while (!IS_XATTR_LAST_ENTRY(last)) {
662 		if ((void *)(last) + sizeof(__u32) > last_base_addr ||
663 			(void *)XATTR_NEXT_ENTRY(last) > last_base_addr) {
664 			set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
665 			error = -EFSCORRUPTED;
666 			goto exit;
667 		}
668 		last = XATTR_NEXT_ENTRY(last);
669 	}
670 
671 	newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
672 
673 	/* 1. Check space */
674 	if (value) {
675 		int free;
676 		/*
677 		 * If value is NULL, it is remove operation.
678 		 * In case of update operation, we calculate free.
679 		 */
680 		free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
681 		if (found)
682 			free = free + ENTRY_SIZE(here);
683 
684 		if (unlikely(free < newsize)) {
685 			error = -E2BIG;
686 			goto exit;
687 		}
688 	}
689 
690 	/* 2. Remove old entry */
691 	if (found) {
692 		/*
693 		 * If entry is found, remove old entry.
694 		 * If not found, remove operation is not needed.
695 		 */
696 		struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
697 		int oldsize = ENTRY_SIZE(here);
698 
699 		memmove(here, next, (char *)last - (char *)next);
700 		last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
701 		memset(last, 0, oldsize);
702 	}
703 
704 	new_hsize = (char *)last - (char *)base_addr;
705 
706 	/* 3. Write new entry */
707 	if (value) {
708 		char *pval;
709 		/*
710 		 * Before we come here, old entry is removed.
711 		 * We just write new entry.
712 		 */
713 		last->e_name_index = index;
714 		last->e_name_len = len;
715 		memcpy(last->e_name, name, len);
716 		pval = last->e_name + len;
717 		memcpy(pval, value, size);
718 		last->e_value_size = cpu_to_le16(size);
719 		new_hsize += newsize;
720 	}
721 
722 	error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
723 	if (error)
724 		goto exit;
725 
726 	if (is_inode_flag_set(inode, FI_ACL_MODE)) {
727 		inode->i_mode = F2FS_I(inode)->i_acl_mode;
728 		inode->i_ctime = current_time(inode);
729 		clear_inode_flag(inode, FI_ACL_MODE);
730 	}
731 	if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
732 			!strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
733 		f2fs_set_encrypted_inode(inode);
734 	f2fs_mark_inode_dirty_sync(inode, true);
735 	if (!error && S_ISDIR(inode->i_mode))
736 		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
737 exit:
738 	kzfree(base_addr);
739 	return error;
740 }
741 
f2fs_setxattr(struct inode * inode,int index,const char * name,const void * value,size_t size,struct page * ipage,int flags)742 int f2fs_setxattr(struct inode *inode, int index, const char *name,
743 				const void *value, size_t size,
744 				struct page *ipage, int flags)
745 {
746 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
747 	int err;
748 
749 	err = dquot_initialize(inode);
750 	if (err)
751 		return err;
752 
753 	/* this case is only from f2fs_init_inode_metadata */
754 	if (ipage)
755 		return __f2fs_setxattr(inode, index, name, value,
756 						size, ipage, flags);
757 	f2fs_balance_fs(sbi, true);
758 
759 	f2fs_lock_op(sbi);
760 	/* protect xattr_ver */
761 	down_write(&F2FS_I(inode)->i_sem);
762 	down_write(&F2FS_I(inode)->i_xattr_sem);
763 	err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
764 	up_write(&F2FS_I(inode)->i_xattr_sem);
765 	up_write(&F2FS_I(inode)->i_sem);
766 	f2fs_unlock_op(sbi);
767 
768 	f2fs_update_time(sbi, REQ_TIME);
769 	return err;
770 }
771