Linux Audio

Check our new training course

Buildroot integration, development and maintenance

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