• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "segment.h"
25 
xattr_alloc(struct f2fs_sb_info * sbi,int size,bool * is_inline)26 static void *xattr_alloc(struct f2fs_sb_info *sbi, int size, bool *is_inline)
27 {
28 	if (likely(size == sbi->inline_xattr_slab_size)) {
29 		*is_inline = true;
30 		return f2fs_kmem_cache_alloc(sbi->inline_xattr_slab,
31 					GFP_F2FS_ZERO, false, sbi);
32 	}
33 	*is_inline = false;
34 	return f2fs_kzalloc(sbi, size, GFP_NOFS);
35 }
36 
xattr_free(struct f2fs_sb_info * sbi,void * xattr_addr,bool is_inline)37 static void xattr_free(struct f2fs_sb_info *sbi, void *xattr_addr,
38 							bool is_inline)
39 {
40 	if (is_inline)
41 		kmem_cache_free(sbi->inline_xattr_slab, xattr_addr);
42 	else
43 		kfree(xattr_addr);
44 }
45 
f2fs_xattr_generic_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)46 static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
47 		struct dentry *unused, struct inode *inode,
48 		const char *name, void *buffer, size_t size)
49 {
50 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
51 
52 	switch (handler->flags) {
53 	case F2FS_XATTR_INDEX_USER:
54 		if (!test_opt(sbi, XATTR_USER))
55 			return -EOPNOTSUPP;
56 		break;
57 	case F2FS_XATTR_INDEX_TRUSTED:
58 	case F2FS_XATTR_INDEX_SECURITY:
59 		break;
60 	default:
61 		return -EINVAL;
62 	}
63 	return f2fs_getxattr(inode, handler->flags, name,
64 			     buffer, size, NULL);
65 }
66 
f2fs_xattr_generic_set(const struct xattr_handler * handler,struct user_namespace * mnt_userns,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)67 static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
68 		struct user_namespace *mnt_userns,
69 		struct dentry *unused, struct inode *inode,
70 		const char *name, const void *value,
71 		size_t size, int flags)
72 {
73 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
74 
75 	switch (handler->flags) {
76 	case F2FS_XATTR_INDEX_USER:
77 		if (!test_opt(sbi, XATTR_USER))
78 			return -EOPNOTSUPP;
79 		break;
80 	case F2FS_XATTR_INDEX_TRUSTED:
81 	case F2FS_XATTR_INDEX_SECURITY:
82 		break;
83 	default:
84 		return -EINVAL;
85 	}
86 	return f2fs_setxattr(inode, handler->flags, name,
87 					value, size, NULL, flags);
88 }
89 
f2fs_xattr_user_list(struct dentry * dentry)90 static bool f2fs_xattr_user_list(struct dentry *dentry)
91 {
92 	struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
93 
94 	return test_opt(sbi, XATTR_USER);
95 }
96 
f2fs_xattr_trusted_list(struct dentry * dentry)97 static bool f2fs_xattr_trusted_list(struct dentry *dentry)
98 {
99 	return capable(CAP_SYS_ADMIN);
100 }
101 
f2fs_xattr_advise_get(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * name,void * buffer,size_t size)102 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
103 		struct dentry *unused, struct inode *inode,
104 		const char *name, void *buffer, size_t size)
105 {
106 	if (buffer)
107 		*((char *)buffer) = F2FS_I(inode)->i_advise;
108 	return sizeof(char);
109 }
110 
f2fs_xattr_advise_set(const struct xattr_handler * handler,struct user_namespace * mnt_userns,struct dentry * unused,struct inode * inode,const char * name,const void * value,size_t size,int flags)111 static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
112 		struct user_namespace *mnt_userns,
113 		struct dentry *unused, struct inode *inode,
114 		const char *name, const void *value,
115 		size_t size, int flags)
116 {
117 	unsigned char old_advise = F2FS_I(inode)->i_advise;
118 	unsigned char new_advise;
119 
120 	if (!inode_owner_or_capable(&init_user_ns, inode))
121 		return -EPERM;
122 	if (value == NULL)
123 		return -EINVAL;
124 
125 	new_advise = *(char *)value;
126 	if (new_advise & ~FADVISE_MODIFIABLE_BITS)
127 		return -EINVAL;
128 
129 	new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
130 	new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
131 
132 	F2FS_I(inode)->i_advise = new_advise;
133 	f2fs_mark_inode_dirty_sync(inode, true);
134 	return 0;
135 }
136 
137 #ifdef CONFIG_F2FS_FS_SECURITY
f2fs_initxattrs(struct inode * inode,const struct xattr * xattr_array,void * page)138 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
139 		void *page)
140 {
141 	const struct xattr *xattr;
142 	int err = 0;
143 
144 	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
145 		err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
146 				xattr->name, xattr->value,
147 				xattr->value_len, (struct page *)page, 0);
148 		if (err < 0)
149 			break;
150 	}
151 	return err;
152 }
153 
f2fs_init_security(struct inode * inode,struct inode * dir,const struct qstr * qstr,struct page * ipage)154 int f2fs_init_security(struct inode *inode, struct inode *dir,
155 				const struct qstr *qstr, struct page *ipage)
156 {
157 	return security_inode_init_security(inode, dir, qstr,
158 				&f2fs_initxattrs, ipage);
159 }
160 #endif
161 
162 const struct xattr_handler f2fs_xattr_user_handler = {
163 	.prefix	= XATTR_USER_PREFIX,
164 	.flags	= F2FS_XATTR_INDEX_USER,
165 	.list	= f2fs_xattr_user_list,
166 	.get	= f2fs_xattr_generic_get,
167 	.set	= f2fs_xattr_generic_set,
168 };
169 
170 const struct xattr_handler f2fs_xattr_trusted_handler = {
171 	.prefix	= XATTR_TRUSTED_PREFIX,
172 	.flags	= F2FS_XATTR_INDEX_TRUSTED,
173 	.list	= f2fs_xattr_trusted_list,
174 	.get	= f2fs_xattr_generic_get,
175 	.set	= f2fs_xattr_generic_set,
176 };
177 
178 const struct xattr_handler f2fs_xattr_advise_handler = {
179 	.name	= F2FS_SYSTEM_ADVISE_NAME,
180 	.flags	= F2FS_XATTR_INDEX_ADVISE,
181 	.get	= f2fs_xattr_advise_get,
182 	.set	= f2fs_xattr_advise_set,
183 };
184 
185 const struct xattr_handler f2fs_xattr_security_handler = {
186 	.prefix	= XATTR_SECURITY_PREFIX,
187 	.flags	= F2FS_XATTR_INDEX_SECURITY,
188 	.get	= f2fs_xattr_generic_get,
189 	.set	= f2fs_xattr_generic_set,
190 };
191 
192 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
193 	[F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
194 #ifdef CONFIG_F2FS_FS_POSIX_ACL
195 	[F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
196 	[F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
197 #endif
198 	[F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
199 #ifdef CONFIG_F2FS_FS_SECURITY
200 	[F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
201 #endif
202 	[F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
203 };
204 
205 const struct xattr_handler *f2fs_xattr_handlers[] = {
206 	&f2fs_xattr_user_handler,
207 #ifdef CONFIG_F2FS_FS_POSIX_ACL
208 	&posix_acl_access_xattr_handler,
209 	&posix_acl_default_xattr_handler,
210 #endif
211 	&f2fs_xattr_trusted_handler,
212 #ifdef CONFIG_F2FS_FS_SECURITY
213 	&f2fs_xattr_security_handler,
214 #endif
215 	&f2fs_xattr_advise_handler,
216 	NULL,
217 };
218 
f2fs_xattr_handler(int index)219 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
220 {
221 	const struct xattr_handler *handler = NULL;
222 
223 	if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
224 		handler = f2fs_xattr_handler_map[index];
225 	return handler;
226 }
227 
__find_xattr(void * base_addr,void * last_base_addr,void ** last_addr,int index,size_t len,const char * name)228 static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
229 				void *last_base_addr, void **last_addr,
230 				int index, size_t len, const char *name)
231 {
232 	struct f2fs_xattr_entry *entry;
233 
234 	list_for_each_xattr(entry, base_addr) {
235 		if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
236 			(void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
237 			if (last_addr)
238 				*last_addr = entry;
239 			return NULL;
240 		}
241 
242 		if (entry->e_name_index != index)
243 			continue;
244 		if (entry->e_name_len != len)
245 			continue;
246 		if (!memcmp(entry->e_name, name, len))
247 			break;
248 	}
249 	return entry;
250 }
251 
__find_inline_xattr(struct inode * inode,void * base_addr,void ** last_addr,int index,size_t len,const char * name)252 static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
253 				void *base_addr, void **last_addr, int index,
254 				size_t len, const char *name)
255 {
256 	struct f2fs_xattr_entry *entry;
257 	unsigned int inline_size = inline_xattr_size(inode);
258 	void *max_addr = base_addr + inline_size;
259 
260 	entry = __find_xattr(base_addr, max_addr, last_addr, index, len, name);
261 	if (!entry)
262 		return NULL;
263 
264 	/* inline xattr header or entry across max inline xattr size */
265 	if (IS_XATTR_LAST_ENTRY(entry) &&
266 		(void *)entry + sizeof(__u32) > max_addr) {
267 		*last_addr = entry;
268 		return NULL;
269 	}
270 	return entry;
271 }
272 
read_inline_xattr(struct inode * inode,struct page * ipage,void * txattr_addr)273 static int read_inline_xattr(struct inode *inode, struct page *ipage,
274 							void *txattr_addr)
275 {
276 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
277 	unsigned int inline_size = inline_xattr_size(inode);
278 	struct page *page = NULL;
279 	void *inline_addr;
280 
281 	if (ipage) {
282 		inline_addr = inline_xattr_addr(inode, ipage);
283 	} else {
284 		page = f2fs_get_node_page(sbi, inode->i_ino);
285 		if (IS_ERR(page))
286 			return PTR_ERR(page);
287 
288 		inline_addr = inline_xattr_addr(inode, page);
289 	}
290 	memcpy(txattr_addr, inline_addr, inline_size);
291 	f2fs_put_page(page, 1);
292 
293 	return 0;
294 }
295 
read_xattr_block(struct inode * inode,void * txattr_addr)296 static int read_xattr_block(struct inode *inode, void *txattr_addr)
297 {
298 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
299 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
300 	unsigned int inline_size = inline_xattr_size(inode);
301 	struct page *xpage;
302 	void *xattr_addr;
303 
304 	/* The inode already has an extended attribute block. */
305 	xpage = f2fs_get_node_page(sbi, xnid);
306 	if (IS_ERR(xpage))
307 		return PTR_ERR(xpage);
308 
309 	xattr_addr = page_address(xpage);
310 	memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
311 	f2fs_put_page(xpage, 1);
312 
313 	return 0;
314 }
315 
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,bool * is_inline)316 static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
317 				unsigned int index, unsigned int len,
318 				const char *name, struct f2fs_xattr_entry **xe,
319 				void **base_addr, int *base_size,
320 				bool *is_inline)
321 {
322 	void *cur_addr, *txattr_addr, *last_txattr_addr;
323 	void *last_addr = NULL;
324 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
325 	unsigned int inline_size = inline_xattr_size(inode);
326 	int err;
327 
328 	if (!xnid && !inline_size)
329 		return -ENODATA;
330 
331 	*base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE;
332 	txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline);
333 	if (!txattr_addr)
334 		return -ENOMEM;
335 
336 	last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode);
337 
338 	/* read from inline xattr */
339 	if (inline_size) {
340 		err = read_inline_xattr(inode, ipage, txattr_addr);
341 		if (err)
342 			goto out;
343 
344 		*xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
345 						index, len, name);
346 		if (*xe) {
347 			*base_size = inline_size;
348 			goto check;
349 		}
350 	}
351 
352 	/* read from xattr node block */
353 	if (xnid) {
354 		err = read_xattr_block(inode, txattr_addr);
355 		if (err)
356 			goto out;
357 	}
358 
359 	if (last_addr)
360 		cur_addr = XATTR_HDR(last_addr) - 1;
361 	else
362 		cur_addr = txattr_addr;
363 
364 	*xe = __find_xattr(cur_addr, last_txattr_addr, NULL, index, len, name);
365 	if (!*xe) {
366 		f2fs_err(F2FS_I_SB(inode), "lookup inode (%lu) has corrupted xattr",
367 								inode->i_ino);
368 		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
369 		err = -ENODATA;
370 		f2fs_handle_error(F2FS_I_SB(inode),
371 					ERROR_CORRUPTED_XATTR);
372 		goto out;
373 	}
374 check:
375 	if (IS_XATTR_LAST_ENTRY(*xe)) {
376 		err = -ENODATA;
377 		goto out;
378 	}
379 
380 	*base_addr = txattr_addr;
381 	return 0;
382 out:
383 	xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline);
384 	return err;
385 }
386 
read_all_xattrs(struct inode * inode,struct page * ipage,void ** base_addr)387 static int read_all_xattrs(struct inode *inode, struct page *ipage,
388 							void **base_addr)
389 {
390 	struct f2fs_xattr_header *header;
391 	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
392 	unsigned int size = VALID_XATTR_BLOCK_SIZE;
393 	unsigned int inline_size = inline_xattr_size(inode);
394 	void *txattr_addr;
395 	int err;
396 
397 	txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
398 			inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
399 	if (!txattr_addr)
400 		return -ENOMEM;
401 
402 	/* read from inline xattr */
403 	if (inline_size) {
404 		err = read_inline_xattr(inode, ipage, txattr_addr);
405 		if (err)
406 			goto fail;
407 	}
408 
409 	/* read from xattr node block */
410 	if (xnid) {
411 		err = read_xattr_block(inode, txattr_addr);
412 		if (err)
413 			goto fail;
414 	}
415 
416 	header = XATTR_HDR(txattr_addr);
417 
418 	/* never been allocated xattrs */
419 	if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
420 		header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
421 		header->h_refcount = cpu_to_le32(1);
422 	}
423 	*base_addr = txattr_addr;
424 	return 0;
425 fail:
426 	kfree(txattr_addr);
427 	return err;
428 }
429 
write_all_xattrs(struct inode * inode,__u32 hsize,void * txattr_addr,struct page * ipage)430 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
431 				void *txattr_addr, struct page *ipage)
432 {
433 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
434 	size_t inline_size = inline_xattr_size(inode);
435 	struct page *in_page = NULL;
436 	void *xattr_addr;
437 	void *inline_addr = NULL;
438 	struct page *xpage;
439 	nid_t new_nid = 0;
440 	int err = 0;
441 
442 	if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
443 		if (!f2fs_alloc_nid(sbi, &new_nid))
444 			return -ENOSPC;
445 
446 	/* write to inline xattr */
447 	if (inline_size) {
448 		if (ipage) {
449 			inline_addr = inline_xattr_addr(inode, ipage);
450 		} else {
451 			in_page = f2fs_get_node_page(sbi, inode->i_ino);
452 			if (IS_ERR(in_page)) {
453 				f2fs_alloc_nid_failed(sbi, new_nid);
454 				return PTR_ERR(in_page);
455 			}
456 			inline_addr = inline_xattr_addr(inode, in_page);
457 		}
458 
459 		f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
460 							NODE, true, true);
461 		/* no need to use xattr node block */
462 		if (hsize <= inline_size) {
463 			err = f2fs_truncate_xattr_node(inode);
464 			f2fs_alloc_nid_failed(sbi, new_nid);
465 			if (err) {
466 				f2fs_put_page(in_page, 1);
467 				return err;
468 			}
469 			memcpy(inline_addr, txattr_addr, inline_size);
470 			set_page_dirty(ipage ? ipage : in_page);
471 			goto in_page_out;
472 		}
473 	}
474 
475 	/* write to xattr node block */
476 	if (F2FS_I(inode)->i_xattr_nid) {
477 		xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
478 		if (IS_ERR(xpage)) {
479 			err = PTR_ERR(xpage);
480 			f2fs_alloc_nid_failed(sbi, new_nid);
481 			goto in_page_out;
482 		}
483 		f2fs_bug_on(sbi, new_nid);
484 		f2fs_wait_on_page_writeback(xpage, NODE, true, true);
485 	} else {
486 		struct dnode_of_data dn;
487 
488 		set_new_dnode(&dn, inode, NULL, NULL, new_nid);
489 		xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
490 		if (IS_ERR(xpage)) {
491 			err = PTR_ERR(xpage);
492 			f2fs_alloc_nid_failed(sbi, new_nid);
493 			goto in_page_out;
494 		}
495 		f2fs_alloc_nid_done(sbi, new_nid);
496 	}
497 	xattr_addr = page_address(xpage);
498 
499 	if (inline_size)
500 		memcpy(inline_addr, txattr_addr, inline_size);
501 	memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
502 
503 	if (inline_size)
504 		set_page_dirty(ipage ? ipage : in_page);
505 	set_page_dirty(xpage);
506 
507 	f2fs_put_page(xpage, 1);
508 in_page_out:
509 	f2fs_put_page(in_page, 1);
510 	return err;
511 }
512 
f2fs_getxattr(struct inode * inode,int index,const char * name,void * buffer,size_t buffer_size,struct page * ipage)513 int f2fs_getxattr(struct inode *inode, int index, const char *name,
514 		void *buffer, size_t buffer_size, struct page *ipage)
515 {
516 	struct f2fs_xattr_entry *entry = NULL;
517 	int error;
518 	unsigned int size, len;
519 	void *base_addr = NULL;
520 	int base_size;
521 	bool is_inline;
522 
523 	if (name == NULL)
524 		return -EINVAL;
525 
526 	len = strlen(name);
527 	if (len > F2FS_NAME_LEN)
528 		return -ERANGE;
529 
530 	if (!ipage)
531 		f2fs_down_read(&F2FS_I(inode)->i_xattr_sem);
532 	error = lookup_all_xattrs(inode, ipage, index, len, name,
533 				&entry, &base_addr, &base_size, &is_inline);
534 	if (!ipage)
535 		f2fs_up_read(&F2FS_I(inode)->i_xattr_sem);
536 	if (error)
537 		return error;
538 
539 	size = le16_to_cpu(entry->e_value_size);
540 
541 	if (buffer && size > buffer_size) {
542 		error = -ERANGE;
543 		goto out;
544 	}
545 
546 	if (buffer) {
547 		char *pval = entry->e_name + entry->e_name_len;
548 
549 		if (base_size - (pval - (char *)base_addr) < size) {
550 			error = -ERANGE;
551 			goto out;
552 		}
553 		memcpy(buffer, pval, size);
554 	}
555 	error = size;
556 out:
557 	xattr_free(F2FS_I_SB(inode), base_addr, is_inline);
558 	return error;
559 }
560 
f2fs_listxattr(struct dentry * dentry,char * buffer,size_t buffer_size)561 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
562 {
563 	struct inode *inode = d_inode(dentry);
564 	struct f2fs_xattr_entry *entry;
565 	void *base_addr, *last_base_addr;
566 	int error;
567 	size_t rest = buffer_size;
568 
569 	f2fs_down_read(&F2FS_I(inode)->i_xattr_sem);
570 	error = read_all_xattrs(inode, NULL, &base_addr);
571 	f2fs_up_read(&F2FS_I(inode)->i_xattr_sem);
572 	if (error)
573 		return error;
574 
575 	last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
576 
577 	list_for_each_xattr(entry, base_addr) {
578 		const struct xattr_handler *handler =
579 			f2fs_xattr_handler(entry->e_name_index);
580 		const char *prefix;
581 		size_t prefix_len;
582 		size_t size;
583 
584 		if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
585 			(void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
586 			f2fs_err(F2FS_I_SB(inode), "list inode (%lu) has corrupted xattr",
587 						inode->i_ino);
588 			set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
589 			f2fs_handle_error(F2FS_I_SB(inode),
590 						ERROR_CORRUPTED_XATTR);
591 			break;
592 		}
593 
594 		if (!handler || (handler->list && !handler->list(dentry)))
595 			continue;
596 
597 		prefix = xattr_prefix(handler);
598 		prefix_len = strlen(prefix);
599 		size = prefix_len + entry->e_name_len + 1;
600 		if (buffer) {
601 			if (size > rest) {
602 				error = -ERANGE;
603 				goto cleanup;
604 			}
605 			memcpy(buffer, prefix, prefix_len);
606 			buffer += prefix_len;
607 			memcpy(buffer, entry->e_name, entry->e_name_len);
608 			buffer += entry->e_name_len;
609 			*buffer++ = 0;
610 		}
611 		rest -= size;
612 	}
613 	error = buffer_size - rest;
614 cleanup:
615 	kfree(base_addr);
616 	return error;
617 }
618 
f2fs_xattr_value_same(struct f2fs_xattr_entry * entry,const void * value,size_t size)619 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
620 					const void *value, size_t size)
621 {
622 	void *pval = entry->e_name + entry->e_name_len;
623 
624 	return (le16_to_cpu(entry->e_value_size) == size) &&
625 					!memcmp(pval, value, size);
626 }
627 
__f2fs_setxattr(struct inode * inode,int index,const char * name,const void * value,size_t size,struct page * ipage,int flags)628 static int __f2fs_setxattr(struct inode *inode, int index,
629 			const char *name, const void *value, size_t size,
630 			struct page *ipage, int flags)
631 {
632 	struct f2fs_xattr_entry *here, *last;
633 	void *base_addr, *last_base_addr;
634 	int found, newsize;
635 	size_t len;
636 	__u32 new_hsize;
637 	int error;
638 
639 	if (name == NULL)
640 		return -EINVAL;
641 
642 	if (value == NULL)
643 		size = 0;
644 
645 	len = strlen(name);
646 
647 	if (len > F2FS_NAME_LEN)
648 		return -ERANGE;
649 
650 	if (size > MAX_VALUE_LEN(inode))
651 		return -E2BIG;
652 retry:
653 	error = read_all_xattrs(inode, ipage, &base_addr);
654 	if (error)
655 		return error;
656 
657 	last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
658 
659 	/* find entry with wanted name. */
660 	here = __find_xattr(base_addr, last_base_addr, NULL, index, len, name);
661 	if (!here) {
662 		if (!F2FS_I(inode)->i_xattr_nid) {
663 			f2fs_notice(F2FS_I_SB(inode),
664 				"recover xattr in inode (%lu)", inode->i_ino);
665 			f2fs_recover_xattr_data(inode, NULL);
666 			kfree(base_addr);
667 			goto retry;
668 		}
669 		f2fs_err(F2FS_I_SB(inode), "set inode (%lu) has corrupted xattr",
670 								inode->i_ino);
671 		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
672 		error = -EFSCORRUPTED;
673 		f2fs_handle_error(F2FS_I_SB(inode),
674 					ERROR_CORRUPTED_XATTR);
675 		goto exit;
676 	}
677 
678 	found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
679 
680 	if (found) {
681 		if ((flags & XATTR_CREATE)) {
682 			error = -EEXIST;
683 			goto exit;
684 		}
685 
686 		if (value && f2fs_xattr_value_same(here, value, size))
687 			goto same;
688 	} else if ((flags & XATTR_REPLACE)) {
689 		error = -ENODATA;
690 		goto exit;
691 	}
692 
693 	last = here;
694 	while (!IS_XATTR_LAST_ENTRY(last)) {
695 		if ((void *)(last) + sizeof(__u32) > last_base_addr ||
696 			(void *)XATTR_NEXT_ENTRY(last) > last_base_addr) {
697 			f2fs_err(F2FS_I_SB(inode), "inode (%lu) has invalid last xattr entry, entry_size: %zu",
698 					inode->i_ino, ENTRY_SIZE(last));
699 			set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
700 			error = -EFSCORRUPTED;
701 			f2fs_handle_error(F2FS_I_SB(inode),
702 						ERROR_CORRUPTED_XATTR);
703 			goto exit;
704 		}
705 		last = XATTR_NEXT_ENTRY(last);
706 	}
707 
708 	newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
709 
710 	/* 1. Check space */
711 	if (value) {
712 		int free;
713 		/*
714 		 * If value is NULL, it is remove operation.
715 		 * In case of update operation, we calculate free.
716 		 */
717 		free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
718 		if (found)
719 			free = free + ENTRY_SIZE(here);
720 
721 		if (unlikely(free < newsize)) {
722 			error = -E2BIG;
723 			goto exit;
724 		}
725 	}
726 
727 	/* 2. Remove old entry */
728 	if (found) {
729 		/*
730 		 * If entry is found, remove old entry.
731 		 * If not found, remove operation is not needed.
732 		 */
733 		struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
734 		int oldsize = ENTRY_SIZE(here);
735 
736 		memmove(here, next, (char *)last - (char *)next);
737 		last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
738 		memset(last, 0, oldsize);
739 	}
740 
741 	new_hsize = (char *)last - (char *)base_addr;
742 
743 	/* 3. Write new entry */
744 	if (value) {
745 		char *pval;
746 		/*
747 		 * Before we come here, old entry is removed.
748 		 * We just write new entry.
749 		 */
750 		last->e_name_index = index;
751 		last->e_name_len = len;
752 		memcpy(last->e_name, name, len);
753 		pval = last->e_name + len;
754 		memcpy(pval, value, size);
755 		last->e_value_size = cpu_to_le16(size);
756 		new_hsize += newsize;
757 		/*
758 		 * Explicitly add the null terminator.  The unused xattr space
759 		 * is supposed to always be zeroed, which would make this
760 		 * unnecessary, but don't depend on that.
761 		 */
762 		*(u32 *)((u8 *)last + newsize) = 0;
763 	}
764 
765 	error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
766 	if (error)
767 		goto exit;
768 
769 	if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
770 			!strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
771 		f2fs_set_encrypted_inode(inode);
772 	f2fs_mark_inode_dirty_sync(inode, true);
773 	if (!error && S_ISDIR(inode->i_mode))
774 		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
775 
776 same:
777 	if (is_inode_flag_set(inode, FI_ACL_MODE)) {
778 		inode->i_mode = F2FS_I(inode)->i_acl_mode;
779 		inode->i_ctime = current_time(inode);
780 		clear_inode_flag(inode, FI_ACL_MODE);
781 	}
782 
783 exit:
784 	kfree(base_addr);
785 	return error;
786 }
787 
f2fs_setxattr(struct inode * inode,int index,const char * name,const void * value,size_t size,struct page * ipage,int flags)788 int f2fs_setxattr(struct inode *inode, int index, const char *name,
789 				const void *value, size_t size,
790 				struct page *ipage, int flags)
791 {
792 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
793 	int err;
794 
795 	if (unlikely(f2fs_cp_error(sbi)))
796 		return -EIO;
797 	if (!f2fs_is_checkpoint_ready(sbi))
798 		return -ENOSPC;
799 
800 	err = f2fs_dquot_initialize(inode);
801 	if (err)
802 		return err;
803 
804 	/* this case is only from f2fs_init_inode_metadata */
805 	if (ipage)
806 		return __f2fs_setxattr(inode, index, name, value,
807 						size, ipage, flags);
808 	f2fs_balance_fs(sbi, true);
809 
810 	f2fs_lock_op(sbi);
811 	f2fs_down_write(&F2FS_I(inode)->i_xattr_sem);
812 	err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
813 	f2fs_up_write(&F2FS_I(inode)->i_xattr_sem);
814 	f2fs_unlock_op(sbi);
815 
816 	f2fs_update_time(sbi, REQ_TIME);
817 	return err;
818 }
819 
f2fs_init_xattr_caches(struct f2fs_sb_info * sbi)820 int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi)
821 {
822 	dev_t dev = sbi->sb->s_bdev->bd_dev;
823 	char slab_name[32];
824 
825 	sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev));
826 
827 	sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size *
828 					sizeof(__le32) + XATTR_PADDING_SIZE;
829 
830 	sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name,
831 					sbi->inline_xattr_slab_size);
832 	if (!sbi->inline_xattr_slab)
833 		return -ENOMEM;
834 
835 	return 0;
836 }
837 
f2fs_destroy_xattr_caches(struct f2fs_sb_info * sbi)838 void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi)
839 {
840 	kmem_cache_destroy(sbi->inline_xattr_slab);
841 }
842