xattr.c 17.6 KB
Newer Older
C
Chao Yu 已提交
1
// SPDX-License-Identifier: GPL-2.0
J
Jaegeuk Kim 已提交
2
/*
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * fs/f2fs/xattr.c
 *
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *             http://www.samsung.com/
 *
 * Portions of this code from linux/fs/ext2/xattr.c
 *
 * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
 *
 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
 * Extended attributes for symlinks and special files added per
 *  suggestion of Luka Renko <luka.renko@hermes.si>.
 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
 *  Red Hat Inc.
 */
#include <linux/rwsem.h>
#include <linux/f2fs_fs.h>
20
#include <linux/security.h>
21
#include <linux/posix_acl_xattr.h>
22 23 24
#include "f2fs.h"
#include "xattr.h"

25
static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
26 27
		struct dentry *unused, struct inode *inode,
		const char *name, void *buffer, size_t size)
28
{
29
	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
30

31
	switch (handler->flags) {
32 33 34 35 36
	case F2FS_XATTR_INDEX_USER:
		if (!test_opt(sbi, XATTR_USER))
			return -EOPNOTSUPP;
		break;
	case F2FS_XATTR_INDEX_TRUSTED:
37 38
	case F2FS_XATTR_INDEX_SECURITY:
		break;
39 40 41
	default:
		return -EINVAL;
	}
42
	return f2fs_getxattr(inode, handler->flags, name,
43
			     buffer, size, NULL);
44 45
}

46
static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
47 48
		struct dentry *unused, struct inode *inode,
		const char *name, const void *value,
49
		size_t size, int flags)
50
{
51
	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
52

53
	switch (handler->flags) {
54 55 56 57 58
	case F2FS_XATTR_INDEX_USER:
		if (!test_opt(sbi, XATTR_USER))
			return -EOPNOTSUPP;
		break;
	case F2FS_XATTR_INDEX_TRUSTED:
59 60
	case F2FS_XATTR_INDEX_SECURITY:
		break;
61 62 63
	default:
		return -EINVAL;
	}
64
	return f2fs_setxattr(inode, handler->flags, name,
65
					value, size, NULL, flags);
66 67
}

68
static bool f2fs_xattr_user_list(struct dentry *dentry)
J
Jaegeuk Kim 已提交
69
{
70 71 72 73
	struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);

	return test_opt(sbi, XATTR_USER);
}
J
Jaegeuk Kim 已提交
74

75 76 77
static bool f2fs_xattr_trusted_list(struct dentry *dentry)
{
	return capable(CAP_SYS_ADMIN);
J
Jaegeuk Kim 已提交
78 79
}

80
static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
81 82
		struct dentry *unused, struct inode *inode,
		const char *name, void *buffer, size_t size)
J
Jaegeuk Kim 已提交
83
{
84 85
	if (buffer)
		*((char *)buffer) = F2FS_I(inode)->i_advise;
J
Jaegeuk Kim 已提交
86 87 88
	return sizeof(char);
}

89
static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
90 91
		struct dentry *unused, struct inode *inode,
		const char *name, const void *value,
92
		size_t size, int flags)
J
Jaegeuk Kim 已提交
93
{
94 95 96
	unsigned char old_advise = F2FS_I(inode)->i_advise;
	unsigned char new_advise;

J
Jaegeuk Kim 已提交
97 98 99 100 101
	if (!inode_owner_or_capable(inode))
		return -EPERM;
	if (value == NULL)
		return -EINVAL;

102 103 104 105 106 107 108 109
	new_advise = *(char *)value;
	if (new_advise & ~FADVISE_MODIFIABLE_BITS)
		return -EINVAL;

	new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
	new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;

	F2FS_I(inode)->i_advise = new_advise;
110
	f2fs_mark_inode_dirty_sync(inode, true);
J
Jaegeuk Kim 已提交
111 112 113
	return 0;
}

114 115 116 117 118 119 120 121
#ifdef CONFIG_F2FS_FS_SECURITY
static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
		void *page)
{
	const struct xattr *xattr;
	int err = 0;

	for (xattr = xattr_array; xattr->name != NULL; xattr++) {
122
		err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
123
				xattr->name, xattr->value,
124
				xattr->value_len, (struct page *)page, 0);
125 126 127 128 129 130 131 132 133 134 135 136 137 138
		if (err < 0)
			break;
	}
	return err;
}

int f2fs_init_security(struct inode *inode, struct inode *dir,
				const struct qstr *qstr, struct page *ipage)
{
	return security_inode_init_security(inode, dir, qstr,
				&f2fs_initxattrs, ipage);
}
#endif

139 140 141
const struct xattr_handler f2fs_xattr_user_handler = {
	.prefix	= XATTR_USER_PREFIX,
	.flags	= F2FS_XATTR_INDEX_USER,
142
	.list	= f2fs_xattr_user_list,
143 144 145 146 147 148 149
	.get	= f2fs_xattr_generic_get,
	.set	= f2fs_xattr_generic_set,
};

const struct xattr_handler f2fs_xattr_trusted_handler = {
	.prefix	= XATTR_TRUSTED_PREFIX,
	.flags	= F2FS_XATTR_INDEX_TRUSTED,
150
	.list	= f2fs_xattr_trusted_list,
151 152 153 154
	.get	= f2fs_xattr_generic_get,
	.set	= f2fs_xattr_generic_set,
};

J
Jaegeuk Kim 已提交
155
const struct xattr_handler f2fs_xattr_advise_handler = {
156
	.name	= F2FS_SYSTEM_ADVISE_NAME,
J
Jaegeuk Kim 已提交
157 158 159 160 161
	.flags	= F2FS_XATTR_INDEX_ADVISE,
	.get    = f2fs_xattr_advise_get,
	.set    = f2fs_xattr_advise_set,
};

162 163 164 165 166 167 168
const struct xattr_handler f2fs_xattr_security_handler = {
	.prefix	= XATTR_SECURITY_PREFIX,
	.flags	= F2FS_XATTR_INDEX_SECURITY,
	.get	= f2fs_xattr_generic_get,
	.set	= f2fs_xattr_generic_set,
};

169 170 171
static const struct xattr_handler *f2fs_xattr_handler_map[] = {
	[F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
#ifdef CONFIG_F2FS_FS_POSIX_ACL
172 173
	[F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
	[F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
174 175
#endif
	[F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
176 177 178
#ifdef CONFIG_F2FS_FS_SECURITY
	[F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
#endif
179 180 181 182 183 184
	[F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
};

const struct xattr_handler *f2fs_xattr_handlers[] = {
	&f2fs_xattr_user_handler,
#ifdef CONFIG_F2FS_FS_POSIX_ACL
185 186
	&posix_acl_access_xattr_handler,
	&posix_acl_default_xattr_handler,
187 188
#endif
	&f2fs_xattr_trusted_handler,
189 190 191
#ifdef CONFIG_F2FS_FS_SECURITY
	&f2fs_xattr_security_handler,
#endif
192 193 194 195
	&f2fs_xattr_advise_handler,
	NULL,
};

J
Jaegeuk Kim 已提交
196
static inline const struct xattr_handler *f2fs_xattr_handler(int index)
197 198 199
{
	const struct xattr_handler *handler = NULL;

J
Jaegeuk Kim 已提交
200 201
	if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
		handler = f2fs_xattr_handler_map[index];
202 203 204
	return handler;
}

J
Jaegeuk Kim 已提交
205 206
static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
					size_t len, const char *name)
207 208 209 210
{
	struct f2fs_xattr_entry *entry;

	list_for_each_xattr(entry, base_addr) {
J
Jaegeuk Kim 已提交
211
		if (entry->e_name_index != index)
212
			continue;
J
Jaegeuk Kim 已提交
213
		if (entry->e_name_len != len)
214
			continue;
J
Jaegeuk Kim 已提交
215
		if (!memcmp(entry->e_name, name, len))
216 217 218 219 220
			break;
	}
	return entry;
}

C
Chao Yu 已提交
221 222 223
static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
				void *base_addr, void **last_addr, int index,
				size_t len, const char *name)
C
Chao Yu 已提交
224 225
{
	struct f2fs_xattr_entry *entry;
C
Chao Yu 已提交
226
	unsigned int inline_size = inline_xattr_size(inode);
227
	void *max_addr = base_addr + inline_size;
C
Chao Yu 已提交
228 229

	list_for_each_xattr(entry, base_addr) {
230 231
		if ((void *)entry + sizeof(__u32) > max_addr ||
			(void *)XATTR_NEXT_ENTRY(entry) > max_addr) {
C
Chao Yu 已提交
232 233 234 235 236 237 238 239 240 241
			*last_addr = entry;
			return NULL;
		}
		if (entry->e_name_index != index)
			continue;
		if (entry->e_name_len != len)
			continue;
		if (!memcmp(entry->e_name, name, len))
			break;
	}
242 243 244 245 246 247 248

	/* inline xattr header or entry across max inline xattr size */
	if (IS_XATTR_LAST_ENTRY(entry) &&
		(void *)entry + sizeof(__u32) > max_addr) {
		*last_addr = entry;
		return NULL;
	}
C
Chao Yu 已提交
249 250 251
	return entry;
}

C
Chao Yu 已提交
252 253 254 255 256 257 258 259 260
static int read_inline_xattr(struct inode *inode, struct page *ipage,
							void *txattr_addr)
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
	unsigned int inline_size = inline_xattr_size(inode);
	struct page *page = NULL;
	void *inline_addr;

	if (ipage) {
C
Chao Yu 已提交
261
		inline_addr = inline_xattr_addr(inode, ipage);
C
Chao Yu 已提交
262
	} else {
C
Chao Yu 已提交
263
		page = f2fs_get_node_page(sbi, inode->i_ino);
C
Chao Yu 已提交
264 265 266
		if (IS_ERR(page))
			return PTR_ERR(page);

C
Chao Yu 已提交
267
		inline_addr = inline_xattr_addr(inode, page);
C
Chao Yu 已提交
268 269 270 271 272 273 274
	}
	memcpy(txattr_addr, inline_addr, inline_size);
	f2fs_put_page(page, 1);

	return 0;
}

C
Chao Yu 已提交
275 276 277 278 279 280 281 282 283
static int read_xattr_block(struct inode *inode, void *txattr_addr)
{
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
	unsigned int inline_size = inline_xattr_size(inode);
	struct page *xpage;
	void *xattr_addr;

	/* The inode already has an extended attribute block. */
C
Chao Yu 已提交
284
	xpage = f2fs_get_node_page(sbi, xnid);
C
Chao Yu 已提交
285 286 287 288 289 290 291 292 293 294
	if (IS_ERR(xpage))
		return PTR_ERR(xpage);

	xattr_addr = page_address(xpage);
	memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
	f2fs_put_page(xpage, 1);

	return 0;
}

C
Chao Yu 已提交
295 296 297
static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
				unsigned int index, unsigned int len,
				const char *name, struct f2fs_xattr_entry **xe,
298
				void **base_addr, int *base_size)
C
Chao Yu 已提交
299 300 301 302
{
	void *cur_addr, *txattr_addr, *last_addr = NULL;
	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
	unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
C
Chao Yu 已提交
303
	unsigned int inline_size = inline_xattr_size(inode);
C
Chao Yu 已提交
304 305 306 307 308
	int err = 0;

	if (!size && !inline_size)
		return -ENODATA;

309 310
	*base_size = inline_size + size + XATTR_PADDING_SIZE;
	txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), *base_size, GFP_NOFS);
C
Chao Yu 已提交
311 312 313 314 315
	if (!txattr_addr)
		return -ENOMEM;

	/* read from inline xattr */
	if (inline_size) {
C
Chao Yu 已提交
316 317 318
		err = read_inline_xattr(inode, ipage, txattr_addr);
		if (err)
			goto out;
C
Chao Yu 已提交
319

C
Chao Yu 已提交
320
		*xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
C
Chao Yu 已提交
321
						index, len, name);
322 323
		if (*xe) {
			*base_size = inline_size;
C
Chao Yu 已提交
324
			goto check;
325
		}
C
Chao Yu 已提交
326 327 328 329
	}

	/* read from xattr node block */
	if (xnid) {
C
Chao Yu 已提交
330 331
		err = read_xattr_block(inode, txattr_addr);
		if (err)
C
Chao Yu 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
			goto out;
	}

	if (last_addr)
		cur_addr = XATTR_HDR(last_addr) - 1;
	else
		cur_addr = txattr_addr;

	*xe = __find_xattr(cur_addr, index, len, name);
check:
	if (IS_XATTR_LAST_ENTRY(*xe)) {
		err = -ENODATA;
		goto out;
	}

	*base_addr = txattr_addr;
	return 0;
out:
350
	kvfree(txattr_addr);
C
Chao Yu 已提交
351 352 353
	return err;
}

354 355
static int read_all_xattrs(struct inode *inode, struct page *ipage,
							void **base_addr)
J
Jaegeuk Kim 已提交
356 357
{
	struct f2fs_xattr_header *header;
C
Chao Yu 已提交
358 359 360
	nid_t xnid = F2FS_I(inode)->i_xattr_nid;
	unsigned int size = VALID_XATTR_BLOCK_SIZE;
	unsigned int inline_size = inline_xattr_size(inode);
J
Jaegeuk Kim 已提交
361
	void *txattr_addr;
362
	int err;
J
Jaegeuk Kim 已提交
363

C
Chao Yu 已提交
364 365
	txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
			inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
J
Jaegeuk Kim 已提交
366
	if (!txattr_addr)
367
		return -ENOMEM;
J
Jaegeuk Kim 已提交
368 369 370

	/* read from inline xattr */
	if (inline_size) {
C
Chao Yu 已提交
371 372 373
		err = read_inline_xattr(inode, ipage, txattr_addr);
		if (err)
			goto fail;
J
Jaegeuk Kim 已提交
374 375 376
	}

	/* read from xattr node block */
C
Chao Yu 已提交
377
	if (xnid) {
C
Chao Yu 已提交
378 379
		err = read_xattr_block(inode, txattr_addr);
		if (err)
J
Jaegeuk Kim 已提交
380 381 382 383 384 385 386 387 388 389
			goto fail;
	}

	header = XATTR_HDR(txattr_addr);

	/* never been allocated xattrs */
	if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
		header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
		header->h_refcount = cpu_to_le32(1);
	}
390 391
	*base_addr = txattr_addr;
	return 0;
J
Jaegeuk Kim 已提交
392
fail:
393
	kvfree(txattr_addr);
394
	return err;
J
Jaegeuk Kim 已提交
395 396 397 398 399
}

static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
				void *txattr_addr, struct page *ipage)
{
400
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
C
Chao Yu 已提交
401
	size_t inline_size = inline_xattr_size(inode);
402
	struct page *in_page = NULL;
J
Jaegeuk Kim 已提交
403
	void *xattr_addr;
404
	void *inline_addr = NULL;
J
Jaegeuk Kim 已提交
405 406
	struct page *xpage;
	nid_t new_nid = 0;
407
	int err = 0;
J
Jaegeuk Kim 已提交
408 409

	if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
C
Chao Yu 已提交
410
		if (!f2fs_alloc_nid(sbi, &new_nid))
J
Jaegeuk Kim 已提交
411 412 413 414 415
			return -ENOSPC;

	/* write to inline xattr */
	if (inline_size) {
		if (ipage) {
C
Chao Yu 已提交
416
			inline_addr = inline_xattr_addr(inode, ipage);
J
Jaegeuk Kim 已提交
417
		} else {
C
Chao Yu 已提交
418
			in_page = f2fs_get_node_page(sbi, inode->i_ino);
419
			if (IS_ERR(in_page)) {
C
Chao Yu 已提交
420
				f2fs_alloc_nid_failed(sbi, new_nid);
421
				return PTR_ERR(in_page);
J
Jaegeuk Kim 已提交
422
			}
423
			inline_addr = inline_xattr_addr(inode, in_page);
J
Jaegeuk Kim 已提交
424 425
		}

426
		f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
427
							NODE, true, true);
J
Jaegeuk Kim 已提交
428 429
		/* no need to use xattr node block */
		if (hsize <= inline_size) {
C
Chao Yu 已提交
430 431
			err = f2fs_truncate_xattr_node(inode);
			f2fs_alloc_nid_failed(sbi, new_nid);
432 433 434 435 436 437 438
			if (err) {
				f2fs_put_page(in_page, 1);
				return err;
			}
			memcpy(inline_addr, txattr_addr, inline_size);
			set_page_dirty(ipage ? ipage : in_page);
			goto in_page_out;
J
Jaegeuk Kim 已提交
439 440 441 442 443
		}
	}

	/* write to xattr node block */
	if (F2FS_I(inode)->i_xattr_nid) {
C
Chao Yu 已提交
444
		xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
J
Jaegeuk Kim 已提交
445
		if (IS_ERR(xpage)) {
446
			err = PTR_ERR(xpage);
C
Chao Yu 已提交
447
			f2fs_alloc_nid_failed(sbi, new_nid);
448
			goto in_page_out;
J
Jaegeuk Kim 已提交
449
		}
450
		f2fs_bug_on(sbi, new_nid);
451
		f2fs_wait_on_page_writeback(xpage, NODE, true, true);
J
Jaegeuk Kim 已提交
452 453 454
	} else {
		struct dnode_of_data dn;
		set_new_dnode(&dn, inode, NULL, NULL, new_nid);
C
Chao Yu 已提交
455
		xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
J
Jaegeuk Kim 已提交
456
		if (IS_ERR(xpage)) {
457
			err = PTR_ERR(xpage);
C
Chao Yu 已提交
458
			f2fs_alloc_nid_failed(sbi, new_nid);
459
			goto in_page_out;
J
Jaegeuk Kim 已提交
460
		}
C
Chao Yu 已提交
461
		f2fs_alloc_nid_done(sbi, new_nid);
J
Jaegeuk Kim 已提交
462 463
	}
	xattr_addr = page_address(xpage);
464 465 466

	if (inline_size)
		memcpy(inline_addr, txattr_addr, inline_size);
467
	memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
468 469 470

	if (inline_size)
		set_page_dirty(ipage ? ipage : in_page);
J
Jaegeuk Kim 已提交
471 472
	set_page_dirty(xpage);

473 474 475 476
	f2fs_put_page(xpage, 1);
in_page_out:
	f2fs_put_page(in_page, 1);
	return err;
J
Jaegeuk Kim 已提交
477 478
}

J
Jaegeuk Kim 已提交
479
int f2fs_getxattr(struct inode *inode, int index, const char *name,
480
		void *buffer, size_t buffer_size, struct page *ipage)
481
{
C
Chao Yu 已提交
482
	struct f2fs_xattr_entry *entry = NULL;
483
	int error = 0;
C
Chao Yu 已提交
484 485
	unsigned int size, len;
	void *base_addr = NULL;
486
	int base_size;
487 488 489

	if (name == NULL)
		return -EINVAL;
J
Jaegeuk Kim 已提交
490 491 492

	len = strlen(name);
	if (len > F2FS_NAME_LEN)
493
		return -ERANGE;
494

495
	down_read(&F2FS_I(inode)->i_xattr_sem);
C
Chao Yu 已提交
496
	error = lookup_all_xattrs(inode, ipage, index, len, name,
497
				&entry, &base_addr, &base_size);
498
	up_read(&F2FS_I(inode)->i_xattr_sem);
499 500
	if (error)
		return error;
501

J
Jaegeuk Kim 已提交
502
	size = le16_to_cpu(entry->e_value_size);
503

J
Jaegeuk Kim 已提交
504
	if (buffer && size > buffer_size) {
505
		error = -ERANGE;
C
Chao Yu 已提交
506
		goto out;
507 508 509 510
	}

	if (buffer) {
		char *pval = entry->e_name + entry->e_name_len;
511 512 513 514 515

		if (base_size - (pval - (char *)base_addr) < size) {
			error = -ERANGE;
			goto out;
		}
J
Jaegeuk Kim 已提交
516
		memcpy(buffer, pval, size);
517
	}
J
Jaegeuk Kim 已提交
518
	error = size;
C
Chao Yu 已提交
519
out:
520
	kvfree(base_addr);
521 522 523 524 525
	return error;
}

ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
{
526
	struct inode *inode = d_inode(dentry);
527 528 529 530 531
	struct f2fs_xattr_entry *entry;
	void *base_addr;
	int error = 0;
	size_t rest = buffer_size;

532
	down_read(&F2FS_I(inode)->i_xattr_sem);
533
	error = read_all_xattrs(inode, NULL, &base_addr);
534
	up_read(&F2FS_I(inode)->i_xattr_sem);
535 536
	if (error)
		return error;
537 538 539 540

	list_for_each_xattr(entry, base_addr) {
		const struct xattr_handler *handler =
			f2fs_xattr_handler(entry->e_name_index);
541 542
		const char *prefix;
		size_t prefix_len;
543 544
		size_t size;

545
		if (!handler || (handler->list && !handler->list(dentry)))
546 547
			continue;

G
Gao Xiang 已提交
548
		prefix = xattr_prefix(handler);
549 550 551 552 553 554 555 556 557 558 559 560
		prefix_len = strlen(prefix);
		size = prefix_len + entry->e_name_len + 1;
		if (buffer) {
			if (size > rest) {
				error = -ERANGE;
				goto cleanup;
			}
			memcpy(buffer, prefix, prefix_len);
			buffer += prefix_len;
			memcpy(buffer, entry->e_name, entry->e_name_len);
			buffer += entry->e_name_len;
			*buffer++ = 0;
561 562 563 564 565
		}
		rest -= size;
	}
	error = buffer_size - rest;
cleanup:
566
	kvfree(base_addr);
567 568 569
	return error;
}

570 571 572 573
static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
					const void *value, size_t size)
{
	void *pval = entry->e_name + entry->e_name_len;
574 575 576

	return (le16_to_cpu(entry->e_value_size) == size) &&
					!memcmp(pval, value, size);
577 578
}

J
Jaegeuk Kim 已提交
579 580
static int __f2fs_setxattr(struct inode *inode, int index,
			const char *name, const void *value, size_t size,
581
			struct page *ipage, int flags)
582 583 584
{
	struct f2fs_xattr_entry *here, *last;
	void *base_addr;
J
Jaegeuk Kim 已提交
585
	int found, newsize;
J
Jaegeuk Kim 已提交
586
	size_t len;
J
Jaegeuk Kim 已提交
587
	__u32 new_hsize;
588
	int error = 0;
589 590 591 592 593

	if (name == NULL)
		return -EINVAL;

	if (value == NULL)
J
Jaegeuk Kim 已提交
594
		size = 0;
595

J
Jaegeuk Kim 已提交
596
	len = strlen(name);
N
Namjae Jeon 已提交
597

598
	if (len > F2FS_NAME_LEN)
599 600
		return -ERANGE;

601 602 603
	if (size > MAX_VALUE_LEN(inode))
		return -E2BIG;

604 605 606
	error = read_all_xattrs(inode, ipage, &base_addr);
	if (error)
		return error;
607 608

	/* find entry with wanted name. */
J
Jaegeuk Kim 已提交
609
	here = __find_xattr(base_addr, index, len, name);
610

611
	found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
612

613 614 615 616 617 618
	if (found) {
		if ((flags & XATTR_CREATE)) {
			error = -EEXIST;
			goto exit;
		}

619
		if (value && f2fs_xattr_value_same(here, value, size))
620 621
			goto exit;
	} else if ((flags & XATTR_REPLACE)) {
622 623 624 625 626
		error = -ENODATA;
		goto exit;
	}

	last = here;
627 628 629
	while (!IS_XATTR_LAST_ENTRY(last))
		last = XATTR_NEXT_ENTRY(last);

J
Jaegeuk Kim 已提交
630
	newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
631 632 633

	/* 1. Check space */
	if (value) {
J
Jaegeuk Kim 已提交
634 635 636
		int free;
		/*
		 * If value is NULL, it is remove operation.
A
arter97 已提交
637
		 * In case of update operation, we calculate free.
638
		 */
J
Jaegeuk Kim 已提交
639
		free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
640
		if (found)
641
			free = free + ENTRY_SIZE(here);
642

643
		if (unlikely(free < newsize)) {
644
			error = -E2BIG;
J
Jaegeuk Kim 已提交
645
			goto exit;
646 647 648 649 650
		}
	}

	/* 2. Remove old entry */
	if (found) {
J
Jaegeuk Kim 已提交
651 652
		/*
		 * If entry is found, remove old entry.
653 654 655 656 657 658 659 660 661 662
		 * If not found, remove operation is not needed.
		 */
		struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
		int oldsize = ENTRY_SIZE(here);

		memmove(here, next, (char *)last - (char *)next);
		last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
		memset(last, 0, oldsize);
	}

J
Jaegeuk Kim 已提交
663 664
	new_hsize = (char *)last - (char *)base_addr;

665 666
	/* 3. Write new entry */
	if (value) {
J
Jaegeuk Kim 已提交
667 668 669 670 671
		char *pval;
		/*
		 * Before we come here, old entry is removed.
		 * We just write new entry.
		 */
J
Jaegeuk Kim 已提交
672 673 674 675 676 677
		last->e_name_index = index;
		last->e_name_len = len;
		memcpy(last->e_name, name, len);
		pval = last->e_name + len;
		memcpy(pval, value, size);
		last->e_value_size = cpu_to_le16(size);
J
Jaegeuk Kim 已提交
678
		new_hsize += newsize;
679 680
	}

J
Jaegeuk Kim 已提交
681 682 683
	error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
	if (error)
		goto exit;
684

685 686
	if (is_inode_flag_set(inode, FI_ACL_MODE)) {
		inode->i_mode = F2FS_I(inode)->i_acl_mode;
687
		inode->i_ctime = current_time(inode);
688
		clear_inode_flag(inode, FI_ACL_MODE);
689
	}
690 691 692
	if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
			!strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
		f2fs_set_encrypted_inode(inode);
693
	f2fs_mark_inode_dirty_sync(inode, true);
694 695
	if (!error && S_ISDIR(inode->i_mode))
		set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
N
Namjae Jeon 已提交
696
exit:
697
	kvfree(base_addr);
698 699
	return error;
}
700

J
Jaegeuk Kim 已提交
701 702
int f2fs_setxattr(struct inode *inode, int index, const char *name,
				const void *value, size_t size,
703
				struct page *ipage, int flags)
704
{
705
	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
706 707
	int err;

J
Jaegeuk Kim 已提交
708 709 710 711
	err = dquot_initialize(inode);
	if (err)
		return err;

C
Chao Yu 已提交
712
	/* this case is only from f2fs_init_inode_metadata */
713 714 715
	if (ipage)
		return __f2fs_setxattr(inode, index, name, value,
						size, ipage, flags);
J
Jaegeuk Kim 已提交
716
	f2fs_balance_fs(sbi, true);
717

718
	f2fs_lock_op(sbi);
719 720
	/* protect xattr_ver */
	down_write(&F2FS_I(inode)->i_sem);
721
	down_write(&F2FS_I(inode)->i_xattr_sem);
722
	err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
723
	up_write(&F2FS_I(inode)->i_xattr_sem);
724
	up_write(&F2FS_I(inode)->i_sem);
725
	f2fs_unlock_op(sbi);
726

727
	f2fs_update_time(sbi, REQ_TIME);
728 729
	return err;
}