f2fs: don't trigger read IO for beyond EOF page
[linux-2.6-block.git] / fs / f2fs / xattr.c
CommitLineData
7c1a000d 1// SPDX-License-Identifier: GPL-2.0
0a8165d7 2/*
af48b85b
JK
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.
af48b85b
JK
17 */
18#include <linux/rwsem.h>
19#include <linux/f2fs_fs.h>
8ae8f162 20#include <linux/security.h>
a6dda0e6 21#include <linux/posix_acl_xattr.h>
af48b85b
JK
22#include "f2fs.h"
23#include "xattr.h"
24
d9a82a04 25static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
b296821a
AV
26 struct dentry *unused, struct inode *inode,
27 const char *name, void *buffer, size_t size)
af48b85b 28{
b296821a 29 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
af48b85b 30
d9a82a04 31 switch (handler->flags) {
af48b85b
JK
32 case F2FS_XATTR_INDEX_USER:
33 if (!test_opt(sbi, XATTR_USER))
34 return -EOPNOTSUPP;
35 break;
36 case F2FS_XATTR_INDEX_TRUSTED:
8ae8f162
JK
37 case F2FS_XATTR_INDEX_SECURITY:
38 break;
af48b85b
JK
39 default:
40 return -EINVAL;
41 }
b296821a 42 return f2fs_getxattr(inode, handler->flags, name,
d9a82a04 43 buffer, size, NULL);
af48b85b
JK
44}
45
d9a82a04 46static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
59301226
AV
47 struct dentry *unused, struct inode *inode,
48 const char *name, const void *value,
d9a82a04 49 size_t size, int flags)
af48b85b 50{
59301226 51 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
af48b85b 52
d9a82a04 53 switch (handler->flags) {
af48b85b
JK
54 case F2FS_XATTR_INDEX_USER:
55 if (!test_opt(sbi, XATTR_USER))
56 return -EOPNOTSUPP;
57 break;
58 case F2FS_XATTR_INDEX_TRUSTED:
8ae8f162
JK
59 case F2FS_XATTR_INDEX_SECURITY:
60 break;
af48b85b
JK
61 default:
62 return -EINVAL;
63 }
59301226 64 return f2fs_setxattr(inode, handler->flags, name,
c02745ef 65 value, size, NULL, flags);
af48b85b
JK
66}
67
764a5c6b 68static bool f2fs_xattr_user_list(struct dentry *dentry)
573ea5fc 69{
764a5c6b
AG
70 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
71
72 return test_opt(sbi, XATTR_USER);
73}
573ea5fc 74
764a5c6b
AG
75static bool f2fs_xattr_trusted_list(struct dentry *dentry)
76{
77 return capable(CAP_SYS_ADMIN);
573ea5fc
JK
78}
79
d9a82a04 80static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
b296821a
AV
81 struct dentry *unused, struct inode *inode,
82 const char *name, void *buffer, size_t size)
573ea5fc 83{
84e97c27
CY
84 if (buffer)
85 *((char *)buffer) = F2FS_I(inode)->i_advise;
573ea5fc
JK
86 return sizeof(char);
87}
88
d9a82a04 89static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
59301226
AV
90 struct dentry *unused, struct inode *inode,
91 const char *name, const void *value,
d9a82a04 92 size_t size, int flags)
573ea5fc 93{
797c1cb5
CY
94 unsigned char old_advise = F2FS_I(inode)->i_advise;
95 unsigned char new_advise;
96
573ea5fc
JK
97 if (!inode_owner_or_capable(inode))
98 return -EPERM;
99 if (value == NULL)
100 return -EINVAL;
101
797c1cb5
CY
102 new_advise = *(char *)value;
103 if (new_advise & ~FADVISE_MODIFIABLE_BITS)
104 return -EINVAL;
105
106 new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
107 new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
108
109 F2FS_I(inode)->i_advise = new_advise;
7c45729a 110 f2fs_mark_inode_dirty_sync(inode, true);
573ea5fc
JK
111 return 0;
112}
113
8ae8f162
JK
114#ifdef CONFIG_F2FS_FS_SECURITY
115static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
116 void *page)
117{
118 const struct xattr *xattr;
119 int err = 0;
120
121 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
d631abda 122 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
8ae8f162 123 xattr->name, xattr->value,
c02745ef 124 xattr->value_len, (struct page *)page, 0);
8ae8f162
JK
125 if (err < 0)
126 break;
127 }
128 return err;
129}
130
131int f2fs_init_security(struct inode *inode, struct inode *dir,
132 const struct qstr *qstr, struct page *ipage)
133{
134 return security_inode_init_security(inode, dir, qstr,
135 &f2fs_initxattrs, ipage);
136}
137#endif
138
af48b85b
JK
139const struct xattr_handler f2fs_xattr_user_handler = {
140 .prefix = XATTR_USER_PREFIX,
141 .flags = F2FS_XATTR_INDEX_USER,
764a5c6b 142 .list = f2fs_xattr_user_list,
af48b85b
JK
143 .get = f2fs_xattr_generic_get,
144 .set = f2fs_xattr_generic_set,
145};
146
147const struct xattr_handler f2fs_xattr_trusted_handler = {
148 .prefix = XATTR_TRUSTED_PREFIX,
149 .flags = F2FS_XATTR_INDEX_TRUSTED,
764a5c6b 150 .list = f2fs_xattr_trusted_list,
af48b85b
JK
151 .get = f2fs_xattr_generic_get,
152 .set = f2fs_xattr_generic_set,
153};
154
573ea5fc 155const struct xattr_handler f2fs_xattr_advise_handler = {
98e9cb57 156 .name = F2FS_SYSTEM_ADVISE_NAME,
573ea5fc 157 .flags = F2FS_XATTR_INDEX_ADVISE,
573ea5fc
JK
158 .get = f2fs_xattr_advise_get,
159 .set = f2fs_xattr_advise_set,
160};
161
8ae8f162
JK
162const struct xattr_handler f2fs_xattr_security_handler = {
163 .prefix = XATTR_SECURITY_PREFIX,
164 .flags = F2FS_XATTR_INDEX_SECURITY,
8ae8f162
JK
165 .get = f2fs_xattr_generic_get,
166 .set = f2fs_xattr_generic_set,
167};
168
af48b85b
JK
169static const struct xattr_handler *f2fs_xattr_handler_map[] = {
170 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
171#ifdef CONFIG_F2FS_FS_POSIX_ACL
a6dda0e6
CH
172 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
173 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
af48b85b
JK
174#endif
175 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
8ae8f162
JK
176#ifdef CONFIG_F2FS_FS_SECURITY
177 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
178#endif
af48b85b
JK
179 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
180};
181
182const struct xattr_handler *f2fs_xattr_handlers[] = {
183 &f2fs_xattr_user_handler,
184#ifdef CONFIG_F2FS_FS_POSIX_ACL
a6dda0e6
CH
185 &posix_acl_access_xattr_handler,
186 &posix_acl_default_xattr_handler,
af48b85b
JK
187#endif
188 &f2fs_xattr_trusted_handler,
8ae8f162
JK
189#ifdef CONFIG_F2FS_FS_SECURITY
190 &f2fs_xattr_security_handler,
191#endif
af48b85b
JK
192 &f2fs_xattr_advise_handler,
193 NULL,
194};
195
e1123268 196static inline const struct xattr_handler *f2fs_xattr_handler(int index)
af48b85b
JK
197{
198 const struct xattr_handler *handler = NULL;
199
e1123268
JK
200 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
201 handler = f2fs_xattr_handler_map[index];
af48b85b
JK
202 return handler;
203}
204
e1123268
JK
205static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
206 size_t len, const char *name)
dd9cfe23
JK
207{
208 struct f2fs_xattr_entry *entry;
209
210 list_for_each_xattr(entry, base_addr) {
e1123268 211 if (entry->e_name_index != index)
dd9cfe23 212 continue;
e1123268 213 if (entry->e_name_len != len)
dd9cfe23 214 continue;
e1123268 215 if (!memcmp(entry->e_name, name, len))
dd9cfe23
JK
216 break;
217 }
218 return entry;
219}
220
6afc662e
CY
221static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
222 void *base_addr, void **last_addr, int index,
223 size_t len, const char *name)
ba38c27e
CY
224{
225 struct f2fs_xattr_entry *entry;
6afc662e 226 unsigned int inline_size = inline_xattr_size(inode);
ba38c27e
CY
227
228 list_for_each_xattr(entry, base_addr) {
229 if ((void *)entry + sizeof(__u32) > base_addr + inline_size ||
230 (void *)XATTR_NEXT_ENTRY(entry) + sizeof(__u32) >
231 base_addr + inline_size) {
232 *last_addr = entry;
233 return NULL;
234 }
235 if (entry->e_name_index != index)
236 continue;
237 if (entry->e_name_len != len)
238 continue;
239 if (!memcmp(entry->e_name, name, len))
240 break;
241 }
242 return entry;
243}
244
a5f433f7
CY
245static int read_inline_xattr(struct inode *inode, struct page *ipage,
246 void *txattr_addr)
247{
248 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
249 unsigned int inline_size = inline_xattr_size(inode);
250 struct page *page = NULL;
251 void *inline_addr;
252
253 if (ipage) {
6afc662e 254 inline_addr = inline_xattr_addr(inode, ipage);
a5f433f7 255 } else {
4d57b86d 256 page = f2fs_get_node_page(sbi, inode->i_ino);
a5f433f7
CY
257 if (IS_ERR(page))
258 return PTR_ERR(page);
259
6afc662e 260 inline_addr = inline_xattr_addr(inode, page);
a5f433f7
CY
261 }
262 memcpy(txattr_addr, inline_addr, inline_size);
263 f2fs_put_page(page, 1);
264
265 return 0;
266}
267
63840695
CY
268static int read_xattr_block(struct inode *inode, void *txattr_addr)
269{
270 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
271 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
272 unsigned int inline_size = inline_xattr_size(inode);
273 struct page *xpage;
274 void *xattr_addr;
275
276 /* The inode already has an extended attribute block. */
4d57b86d 277 xpage = f2fs_get_node_page(sbi, xnid);
63840695
CY
278 if (IS_ERR(xpage))
279 return PTR_ERR(xpage);
280
281 xattr_addr = page_address(xpage);
282 memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
283 f2fs_put_page(xpage, 1);
284
285 return 0;
286}
287
ba38c27e
CY
288static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
289 unsigned int index, unsigned int len,
290 const char *name, struct f2fs_xattr_entry **xe,
64beba05 291 void **base_addr, int *base_size)
ba38c27e 292{
ba38c27e
CY
293 void *cur_addr, *txattr_addr, *last_addr = NULL;
294 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
295 unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
89e9eabd 296 unsigned int inline_size = inline_xattr_size(inode);
ba38c27e
CY
297 int err = 0;
298
ba38c27e
CY
299 if (!size && !inline_size)
300 return -ENODATA;
301
64beba05
JK
302 *base_size = inline_size + size + XATTR_PADDING_SIZE;
303 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), *base_size, GFP_NOFS);
ba38c27e
CY
304 if (!txattr_addr)
305 return -ENOMEM;
306
307 /* read from inline xattr */
308 if (inline_size) {
a5f433f7
CY
309 err = read_inline_xattr(inode, ipage, txattr_addr);
310 if (err)
311 goto out;
ba38c27e 312
6afc662e 313 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
ba38c27e 314 index, len, name);
64beba05
JK
315 if (*xe) {
316 *base_size = inline_size;
ba38c27e 317 goto check;
64beba05 318 }
ba38c27e
CY
319 }
320
321 /* read from xattr node block */
322 if (xnid) {
63840695
CY
323 err = read_xattr_block(inode, txattr_addr);
324 if (err)
ba38c27e 325 goto out;
ba38c27e
CY
326 }
327
328 if (last_addr)
329 cur_addr = XATTR_HDR(last_addr) - 1;
330 else
331 cur_addr = txattr_addr;
332
333 *xe = __find_xattr(cur_addr, index, len, name);
334check:
335 if (IS_XATTR_LAST_ENTRY(*xe)) {
336 err = -ENODATA;
337 goto out;
338 }
339
340 *base_addr = txattr_addr;
341 return 0;
342out:
2a6a7e72 343 kvfree(txattr_addr);
ba38c27e
CY
344 return err;
345}
346
86696966
CY
347static int read_all_xattrs(struct inode *inode, struct page *ipage,
348 void **base_addr)
65985d93 349{
65985d93 350 struct f2fs_xattr_header *header;
89e9eabd
CY
351 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
352 unsigned int size = VALID_XATTR_BLOCK_SIZE;
353 unsigned int inline_size = inline_xattr_size(inode);
65985d93 354 void *txattr_addr;
86696966 355 int err;
65985d93 356
acbf054d
CY
357 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
358 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
65985d93 359 if (!txattr_addr)
86696966 360 return -ENOMEM;
65985d93
JK
361
362 /* read from inline xattr */
363 if (inline_size) {
a5f433f7
CY
364 err = read_inline_xattr(inode, ipage, txattr_addr);
365 if (err)
366 goto fail;
65985d93
JK
367 }
368
369 /* read from xattr node block */
89e9eabd 370 if (xnid) {
63840695
CY
371 err = read_xattr_block(inode, txattr_addr);
372 if (err)
65985d93 373 goto fail;
65985d93
JK
374 }
375
376 header = XATTR_HDR(txattr_addr);
377
378 /* never been allocated xattrs */
379 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
380 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
381 header->h_refcount = cpu_to_le32(1);
382 }
86696966
CY
383 *base_addr = txattr_addr;
384 return 0;
65985d93 385fail:
2a6a7e72 386 kvfree(txattr_addr);
86696966 387 return err;
65985d93
JK
388}
389
390static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
391 void *txattr_addr, struct page *ipage)
392{
4081363f 393 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
89e9eabd 394 size_t inline_size = inline_xattr_size(inode);
bf9c1427 395 struct page *in_page = NULL;
65985d93 396 void *xattr_addr;
bf9c1427 397 void *inline_addr = NULL;
65985d93
JK
398 struct page *xpage;
399 nid_t new_nid = 0;
bf9c1427 400 int err = 0;
65985d93 401
65985d93 402 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
4d57b86d 403 if (!f2fs_alloc_nid(sbi, &new_nid))
65985d93
JK
404 return -ENOSPC;
405
406 /* write to inline xattr */
407 if (inline_size) {
65985d93 408 if (ipage) {
6afc662e 409 inline_addr = inline_xattr_addr(inode, ipage);
65985d93 410 } else {
4d57b86d 411 in_page = f2fs_get_node_page(sbi, inode->i_ino);
bf9c1427 412 if (IS_ERR(in_page)) {
4d57b86d 413 f2fs_alloc_nid_failed(sbi, new_nid);
bf9c1427 414 return PTR_ERR(in_page);
65985d93 415 }
bf9c1427 416 inline_addr = inline_xattr_addr(inode, in_page);
65985d93 417 }
65985d93 418
bf9c1427 419 f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
bae0ee7a 420 NODE, true, true);
65985d93
JK
421 /* no need to use xattr node block */
422 if (hsize <= inline_size) {
4d57b86d
CY
423 err = f2fs_truncate_xattr_node(inode);
424 f2fs_alloc_nid_failed(sbi, new_nid);
bf9c1427
JK
425 if (err) {
426 f2fs_put_page(in_page, 1);
427 return err;
428 }
429 memcpy(inline_addr, txattr_addr, inline_size);
430 set_page_dirty(ipage ? ipage : in_page);
431 goto in_page_out;
65985d93
JK
432 }
433 }
434
435 /* write to xattr node block */
436 if (F2FS_I(inode)->i_xattr_nid) {
4d57b86d 437 xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
65985d93 438 if (IS_ERR(xpage)) {
d620439f 439 err = PTR_ERR(xpage);
4d57b86d 440 f2fs_alloc_nid_failed(sbi, new_nid);
bf9c1427 441 goto in_page_out;
65985d93 442 }
9850cf4a 443 f2fs_bug_on(sbi, new_nid);
bae0ee7a 444 f2fs_wait_on_page_writeback(xpage, NODE, true, true);
65985d93
JK
445 } else {
446 struct dnode_of_data dn;
447 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
4d57b86d 448 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
65985d93 449 if (IS_ERR(xpage)) {
d620439f 450 err = PTR_ERR(xpage);
4d57b86d 451 f2fs_alloc_nid_failed(sbi, new_nid);
bf9c1427 452 goto in_page_out;
65985d93 453 }
4d57b86d 454 f2fs_alloc_nid_done(sbi, new_nid);
65985d93 455 }
65985d93 456 xattr_addr = page_address(xpage);
bf9c1427
JK
457
458 if (inline_size)
459 memcpy(inline_addr, txattr_addr, inline_size);
22588f87 460 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
bf9c1427
JK
461
462 if (inline_size)
463 set_page_dirty(ipage ? ipage : in_page);
65985d93 464 set_page_dirty(xpage);
65985d93 465
bf9c1427
JK
466 f2fs_put_page(xpage, 1);
467in_page_out:
468 f2fs_put_page(in_page, 1);
469 return err;
65985d93
JK
470}
471
e1123268 472int f2fs_getxattr(struct inode *inode, int index, const char *name,
bce8d112 473 void *buffer, size_t buffer_size, struct page *ipage)
af48b85b 474{
ba38c27e 475 struct f2fs_xattr_entry *entry = NULL;
dd9cfe23 476 int error = 0;
ba38c27e 477 unsigned int size, len;
ba38c27e 478 void *base_addr = NULL;
64beba05 479 int base_size;
af48b85b
JK
480
481 if (name == NULL)
482 return -EINVAL;
e1123268
JK
483
484 len = strlen(name);
485 if (len > F2FS_NAME_LEN)
6e452d69 486 return -ERANGE;
af48b85b 487
27161f13 488 down_read(&F2FS_I(inode)->i_xattr_sem);
ba38c27e 489 error = lookup_all_xattrs(inode, ipage, index, len, name,
64beba05 490 &entry, &base_addr, &base_size);
27161f13 491 up_read(&F2FS_I(inode)->i_xattr_sem);
86696966
CY
492 if (error)
493 return error;
af48b85b 494
e1123268 495 size = le16_to_cpu(entry->e_value_size);
af48b85b 496
e1123268 497 if (buffer && size > buffer_size) {
af48b85b 498 error = -ERANGE;
ba38c27e 499 goto out;
af48b85b
JK
500 }
501
502 if (buffer) {
503 char *pval = entry->e_name + entry->e_name_len;
64beba05
JK
504
505 if (base_size - (pval - (char *)base_addr) < size) {
506 error = -ERANGE;
507 goto out;
508 }
e1123268 509 memcpy(buffer, pval, size);
af48b85b 510 }
e1123268 511 error = size;
ba38c27e 512out:
2a6a7e72 513 kvfree(base_addr);
af48b85b
JK
514 return error;
515}
516
517ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
518{
2b0143b5 519 struct inode *inode = d_inode(dentry);
af48b85b 520 struct f2fs_xattr_entry *entry;
af48b85b
JK
521 void *base_addr;
522 int error = 0;
523 size_t rest = buffer_size;
524
27161f13 525 down_read(&F2FS_I(inode)->i_xattr_sem);
86696966 526 error = read_all_xattrs(inode, NULL, &base_addr);
27161f13 527 up_read(&F2FS_I(inode)->i_xattr_sem);
86696966
CY
528 if (error)
529 return error;
af48b85b
JK
530
531 list_for_each_xattr(entry, base_addr) {
532 const struct xattr_handler *handler =
533 f2fs_xattr_handler(entry->e_name_index);
764a5c6b
AG
534 const char *prefix;
535 size_t prefix_len;
af48b85b
JK
536 size_t size;
537
764a5c6b 538 if (!handler || (handler->list && !handler->list(dentry)))
af48b85b
JK
539 continue;
540
eecfa427 541 prefix = xattr_prefix(handler);
764a5c6b
AG
542 prefix_len = strlen(prefix);
543 size = prefix_len + entry->e_name_len + 1;
544 if (buffer) {
545 if (size > rest) {
546 error = -ERANGE;
547 goto cleanup;
548 }
549 memcpy(buffer, prefix, prefix_len);
550 buffer += prefix_len;
551 memcpy(buffer, entry->e_name, entry->e_name_len);
552 buffer += entry->e_name_len;
553 *buffer++ = 0;
af48b85b 554 }
af48b85b
JK
555 rest -= size;
556 }
557 error = buffer_size - rest;
558cleanup:
2a6a7e72 559 kvfree(base_addr);
af48b85b
JK
560 return error;
561}
562
5f35a2cd
KM
563static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
564 const void *value, size_t size)
565{
566 void *pval = entry->e_name + entry->e_name_len;
b71deadb
JK
567
568 return (le16_to_cpu(entry->e_value_size) == size) &&
569 !memcmp(pval, value, size);
5f35a2cd
KM
570}
571
e1123268
JK
572static int __f2fs_setxattr(struct inode *inode, int index,
573 const char *name, const void *value, size_t size,
c02745ef 574 struct page *ipage, int flags)
af48b85b 575{
af48b85b 576 struct f2fs_xattr_entry *here, *last;
af48b85b 577 void *base_addr;
65985d93 578 int found, newsize;
e1123268 579 size_t len;
65985d93 580 __u32 new_hsize;
a0995af6 581 int error = 0;
af48b85b
JK
582
583 if (name == NULL)
584 return -EINVAL;
af48b85b
JK
585
586 if (value == NULL)
e1123268 587 size = 0;
af48b85b 588
e1123268 589 len = strlen(name);
7c909772 590
037fe70c 591 if (len > F2FS_NAME_LEN)
af48b85b
JK
592 return -ERANGE;
593
037fe70c
CY
594 if (size > MAX_VALUE_LEN(inode))
595 return -E2BIG;
596
86696966
CY
597 error = read_all_xattrs(inode, ipage, &base_addr);
598 if (error)
599 return error;
af48b85b
JK
600
601 /* find entry with wanted name. */
e1123268 602 here = __find_xattr(base_addr, index, len, name);
af48b85b 603
dd9cfe23 604 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
af48b85b 605
5f35a2cd
KM
606 if (found) {
607 if ((flags & XATTR_CREATE)) {
608 error = -EEXIST;
609 goto exit;
610 }
611
b2c4692b 612 if (value && f2fs_xattr_value_same(here, value, size))
5f35a2cd
KM
613 goto exit;
614 } else if ((flags & XATTR_REPLACE)) {
916decbf
JK
615 error = -ENODATA;
616 goto exit;
916decbf
JK
617 }
618
619 last = here;
af48b85b
JK
620 while (!IS_XATTR_LAST_ENTRY(last))
621 last = XATTR_NEXT_ENTRY(last);
622
e1123268 623 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
af48b85b
JK
624
625 /* 1. Check space */
626 if (value) {
65985d93
JK
627 int free;
628 /*
629 * If value is NULL, it is remove operation.
e1c42045 630 * In case of update operation, we calculate free.
af48b85b 631 */
65985d93 632 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
af48b85b 633 if (found)
cc3de6a3 634 free = free + ENTRY_SIZE(here);
af48b85b 635
6bacf52f 636 if (unlikely(free < newsize)) {
58457f1c 637 error = -E2BIG;
65985d93 638 goto exit;
af48b85b
JK
639 }
640 }
641
642 /* 2. Remove old entry */
643 if (found) {
65985d93
JK
644 /*
645 * If entry is found, remove old entry.
af48b85b
JK
646 * If not found, remove operation is not needed.
647 */
648 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
649 int oldsize = ENTRY_SIZE(here);
650
651 memmove(here, next, (char *)last - (char *)next);
652 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
653 memset(last, 0, oldsize);
654 }
655
65985d93
JK
656 new_hsize = (char *)last - (char *)base_addr;
657
af48b85b
JK
658 /* 3. Write new entry */
659 if (value) {
65985d93
JK
660 char *pval;
661 /*
662 * Before we come here, old entry is removed.
663 * We just write new entry.
664 */
e1123268
JK
665 last->e_name_index = index;
666 last->e_name_len = len;
667 memcpy(last->e_name, name, len);
668 pval = last->e_name + len;
669 memcpy(pval, value, size);
670 last->e_value_size = cpu_to_le16(size);
65985d93 671 new_hsize += newsize;
af48b85b
JK
672 }
673
65985d93
JK
674 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
675 if (error)
676 goto exit;
af48b85b 677
91942321
JK
678 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
679 inode->i_mode = F2FS_I(inode)->i_acl_mode;
c2050a45 680 inode->i_ctime = current_time(inode);
91942321 681 clear_inode_flag(inode, FI_ACL_MODE);
af48b85b 682 }
f424f664
JK
683 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
684 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
685 f2fs_set_encrypted_inode(inode);
7c45729a 686 f2fs_mark_inode_dirty_sync(inode, true);
bbf156f7
JK
687 if (!error && S_ISDIR(inode->i_mode))
688 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
7c909772 689exit:
2a6a7e72 690 kvfree(base_addr);
af48b85b
JK
691 return error;
692}
52ab9560 693
e1123268
JK
694int f2fs_setxattr(struct inode *inode, int index, const char *name,
695 const void *value, size_t size,
c02745ef 696 struct page *ipage, int flags)
52ab9560 697{
4081363f 698 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
52ab9560
RK
699 int err;
700
d8d1389e
JK
701 err = dquot_initialize(inode);
702 if (err)
703 return err;
704
4d57b86d 705 /* this case is only from f2fs_init_inode_metadata */
d631abda
JK
706 if (ipage)
707 return __f2fs_setxattr(inode, index, name, value,
708 size, ipage, flags);
2c4db1a6 709 f2fs_balance_fs(sbi, true);
52ab9560 710
e479556b 711 f2fs_lock_op(sbi);
d928bfbf
JK
712 /* protect xattr_ver */
713 down_write(&F2FS_I(inode)->i_sem);
27161f13 714 down_write(&F2FS_I(inode)->i_xattr_sem);
c02745ef 715 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
27161f13 716 up_write(&F2FS_I(inode)->i_xattr_sem);
d928bfbf 717 up_write(&F2FS_I(inode)->i_sem);
e479556b 718 f2fs_unlock_op(sbi);
52ab9560 719
d0239e1b 720 f2fs_update_time(sbi, REQ_TIME);
52ab9560
RK
721 return err;
722}