blob: 06e167925b30db108e7a03ac20718eda9538960f [file] [log] [blame]
Linus Torvalds8005ecc2012-12-20 13:54:51 -08001/*
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 "f2fs.h"
25#include "xattr.h"
26
27static size_t f2fs_xattr_generic_list(struct dentry *dentry, char *list,
Jaegeuk Kimb5614a32014-04-23 12:17:25 +090028 size_t list_size, const char *name, size_t len, int type)
Linus Torvalds8005ecc2012-12-20 13:54:51 -080029{
30 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
31 int total_len, prefix_len = 0;
32 const char *prefix = NULL;
33
34 switch (type) {
35 case F2FS_XATTR_INDEX_USER:
36 if (!test_opt(sbi, XATTR_USER))
37 return -EOPNOTSUPP;
38 prefix = XATTR_USER_PREFIX;
39 prefix_len = XATTR_USER_PREFIX_LEN;
40 break;
41 case F2FS_XATTR_INDEX_TRUSTED:
42 if (!capable(CAP_SYS_ADMIN))
43 return -EPERM;
44 prefix = XATTR_TRUSTED_PREFIX;
45 prefix_len = XATTR_TRUSTED_PREFIX_LEN;
46 break;
47 case F2FS_XATTR_INDEX_SECURITY:
48 prefix = XATTR_SECURITY_PREFIX;
49 prefix_len = XATTR_SECURITY_PREFIX_LEN;
50 break;
51 default:
52 return -EINVAL;
53 }
54
Jaegeuk Kimb5614a32014-04-23 12:17:25 +090055 total_len = prefix_len + len + 1;
Linus Torvalds8005ecc2012-12-20 13:54:51 -080056 if (list && total_len <= list_size) {
57 memcpy(list, prefix, prefix_len);
Jaegeuk Kimb5614a32014-04-23 12:17:25 +090058 memcpy(list + prefix_len, name, len);
59 list[prefix_len + len] = '\0';
Linus Torvalds8005ecc2012-12-20 13:54:51 -080060 }
61 return total_len;
62}
63
64static int f2fs_xattr_generic_get(struct dentry *dentry, const char *name,
65 void *buffer, size_t size, int type)
66{
67 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
68
69 switch (type) {
70 case F2FS_XATTR_INDEX_USER:
71 if (!test_opt(sbi, XATTR_USER))
72 return -EOPNOTSUPP;
73 break;
74 case F2FS_XATTR_INDEX_TRUSTED:
75 if (!capable(CAP_SYS_ADMIN))
76 return -EPERM;
77 break;
78 case F2FS_XATTR_INDEX_SECURITY:
79 break;
80 default:
81 return -EINVAL;
82 }
83 if (strcmp(name, "") == 0)
84 return -EINVAL;
85 return f2fs_getxattr(dentry->d_inode, type, name, buffer, size);
86}
87
88static int f2fs_xattr_generic_set(struct dentry *dentry, const char *name,
89 const void *value, size_t size, int flags, int type)
90{
91 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
92
93 switch (type) {
94 case F2FS_XATTR_INDEX_USER:
95 if (!test_opt(sbi, XATTR_USER))
96 return -EOPNOTSUPP;
97 break;
98 case F2FS_XATTR_INDEX_TRUSTED:
99 if (!capable(CAP_SYS_ADMIN))
100 return -EPERM;
101 break;
102 case F2FS_XATTR_INDEX_SECURITY:
103 break;
104 default:
105 return -EINVAL;
106 }
107 if (strcmp(name, "") == 0)
108 return -EINVAL;
109
Jaegeuk Kim2eb8e7c2014-04-23 12:23:14 +0900110 return f2fs_setxattr(dentry->d_inode, type, name,
111 value, size, NULL, flags);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800112}
113
114static size_t f2fs_xattr_advise_list(struct dentry *dentry, char *list,
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900115 size_t list_size, const char *name, size_t len, int type)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800116{
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
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800158static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
159 void *page)
160{
161 const struct xattr *xattr;
162 int err = 0;
163
164 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Jaegeuk Kim26d712d2014-06-01 23:24:30 +0900165 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800166 xattr->name, xattr->value,
Jaegeuk Kim2eb8e7c2014-04-23 12:23:14 +0900167 xattr->value_len, (struct page *)page, 0);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800168 if (err < 0)
169 break;
170 }
171 return err;
172}
173
174int f2fs_init_security(struct inode *inode, struct inode *dir,
175 const struct qstr *qstr, struct page *ipage)
176{
177 return security_inode_init_security(inode, dir, qstr,
178 &f2fs_initxattrs, ipage);
179}
180#endif
181
182const struct xattr_handler f2fs_xattr_user_handler = {
183 .prefix = XATTR_USER_PREFIX,
184 .flags = F2FS_XATTR_INDEX_USER,
185 .list = f2fs_xattr_generic_list,
186 .get = f2fs_xattr_generic_get,
187 .set = f2fs_xattr_generic_set,
188};
189
190const struct xattr_handler f2fs_xattr_trusted_handler = {
191 .prefix = XATTR_TRUSTED_PREFIX,
192 .flags = F2FS_XATTR_INDEX_TRUSTED,
193 .list = f2fs_xattr_generic_list,
194 .get = f2fs_xattr_generic_get,
195 .set = f2fs_xattr_generic_set,
196};
197
198const struct xattr_handler f2fs_xattr_advise_handler = {
199 .prefix = F2FS_SYSTEM_ADVISE_PREFIX,
200 .flags = F2FS_XATTR_INDEX_ADVISE,
201 .list = f2fs_xattr_advise_list,
202 .get = f2fs_xattr_advise_get,
203 .set = f2fs_xattr_advise_set,
204};
205
206const struct xattr_handler f2fs_xattr_security_handler = {
207 .prefix = XATTR_SECURITY_PREFIX,
208 .flags = F2FS_XATTR_INDEX_SECURITY,
209 .list = f2fs_xattr_generic_list,
210 .get = f2fs_xattr_generic_get,
211 .set = f2fs_xattr_generic_set,
212};
213
214static const struct xattr_handler *f2fs_xattr_handler_map[] = {
215 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
216#ifdef CONFIG_F2FS_FS_POSIX_ACL
217 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &f2fs_xattr_acl_access_handler,
218 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &f2fs_xattr_acl_default_handler,
219#endif
220 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
221#ifdef CONFIG_F2FS_FS_SECURITY
222 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
223#endif
224 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
225};
226
227const struct xattr_handler *f2fs_xattr_handlers[] = {
228 &f2fs_xattr_user_handler,
229#ifdef CONFIG_F2FS_FS_POSIX_ACL
230 &f2fs_xattr_acl_access_handler,
231 &f2fs_xattr_acl_default_handler,
232#endif
233 &f2fs_xattr_trusted_handler,
234#ifdef CONFIG_F2FS_FS_SECURITY
235 &f2fs_xattr_security_handler,
236#endif
237 &f2fs_xattr_advise_handler,
238 NULL,
239};
240
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900241static inline const struct xattr_handler *f2fs_xattr_handler(int index)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800242{
243 const struct xattr_handler *handler = NULL;
244
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900245 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
246 handler = f2fs_xattr_handler_map[index];
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800247 return handler;
248}
249
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900250static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
251 size_t len, const char *name)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800252{
253 struct f2fs_xattr_entry *entry;
254
255 list_for_each_xattr(entry, base_addr) {
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900256 if (entry->e_name_index != index)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800257 continue;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900258 if (entry->e_name_len != len)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800259 continue;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900260 if (!memcmp(entry->e_name, name, len))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800261 break;
262 }
263 return entry;
264}
265
266static void *read_all_xattrs(struct inode *inode, struct page *ipage)
267{
268 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
269 struct f2fs_xattr_header *header;
270 size_t size = PAGE_SIZE, inline_size = 0;
271 void *txattr_addr;
272
273 inline_size = inline_xattr_size(inode);
274
Jaegeuk Kim395204b2014-03-20 22:21:08 +0900275 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800276 if (!txattr_addr)
277 return NULL;
278
279 /* read from inline xattr */
280 if (inline_size) {
281 struct page *page = NULL;
282 void *inline_addr;
283
284 if (ipage) {
285 inline_addr = inline_xattr_addr(ipage);
286 } else {
287 page = get_node_page(sbi, inode->i_ino);
288 if (IS_ERR(page))
289 goto fail;
290 inline_addr = inline_xattr_addr(page);
291 }
292 memcpy(txattr_addr, inline_addr, inline_size);
293 f2fs_put_page(page, 1);
294 }
295
296 /* read from xattr node block */
297 if (F2FS_I(inode)->i_xattr_nid) {
298 struct page *xpage;
299 void *xattr_addr;
300
301 /* The inode already has an extended attribute block. */
302 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
303 if (IS_ERR(xpage))
304 goto fail;
305
306 xattr_addr = page_address(xpage);
307 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
308 f2fs_put_page(xpage, 1);
309 }
310
311 header = XATTR_HDR(txattr_addr);
312
313 /* never been allocated xattrs */
314 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
315 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
316 header->h_refcount = cpu_to_le32(1);
317 }
318 return txattr_addr;
319fail:
320 kzfree(txattr_addr);
321 return NULL;
322}
323
324static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
325 void *txattr_addr, struct page *ipage)
326{
327 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
328 size_t inline_size = 0;
329 void *xattr_addr;
330 struct page *xpage;
331 nid_t new_nid = 0;
332 int err;
333
334 inline_size = inline_xattr_size(inode);
335
336 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
337 if (!alloc_nid(sbi, &new_nid))
338 return -ENOSPC;
339
340 /* write to inline xattr */
341 if (inline_size) {
342 struct page *page = NULL;
343 void *inline_addr;
344
345 if (ipage) {
346 inline_addr = inline_xattr_addr(ipage);
Jaegeuk Kim0f9e6b52014-04-29 17:28:32 +0900347 f2fs_wait_on_page_writeback(ipage, NODE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800348 } else {
349 page = get_node_page(sbi, inode->i_ino);
350 if (IS_ERR(page)) {
351 alloc_nid_failed(sbi, new_nid);
352 return PTR_ERR(page);
353 }
354 inline_addr = inline_xattr_addr(page);
Jaegeuk Kim0f9e6b52014-04-29 17:28:32 +0900355 f2fs_wait_on_page_writeback(page, NODE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800356 }
357 memcpy(inline_addr, txattr_addr, inline_size);
358 f2fs_put_page(page, 1);
359
360 /* no need to use xattr node block */
361 if (hsize <= inline_size) {
362 err = truncate_xattr_node(inode, ipage);
363 alloc_nid_failed(sbi, new_nid);
364 return err;
365 }
366 }
367
368 /* write to xattr node block */
369 if (F2FS_I(inode)->i_xattr_nid) {
370 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
371 if (IS_ERR(xpage)) {
372 alloc_nid_failed(sbi, new_nid);
373 return PTR_ERR(xpage);
374 }
375 f2fs_bug_on(new_nid);
Jaegeuk Kim0f9e6b52014-04-29 17:28:32 +0900376 f2fs_wait_on_page_writeback(xpage, NODE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800377 } 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
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900399int f2fs_getxattr(struct inode *inode, int index, const char *name,
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800400 void *buffer, size_t buffer_size)
401{
402 struct f2fs_xattr_entry *entry;
403 void *base_addr;
404 int error = 0;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900405 size_t size, len;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800406
407 if (name == NULL)
408 return -EINVAL;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900409
410 len = strlen(name);
411 if (len > F2FS_NAME_LEN)
Chao Yu26a9bf52014-03-22 14:59:45 +0800412 return -ERANGE;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800413
414 base_addr = read_all_xattrs(inode, NULL);
415 if (!base_addr)
416 return -ENOMEM;
417
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900418 entry = __find_xattr(base_addr, index, len, name);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800419 if (IS_XATTR_LAST_ENTRY(entry)) {
420 error = -ENODATA;
421 goto cleanup;
422 }
423
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900424 size = le16_to_cpu(entry->e_value_size);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800425
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900426 if (buffer && size > buffer_size) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800427 error = -ERANGE;
428 goto cleanup;
429 }
430
431 if (buffer) {
432 char *pval = entry->e_name + entry->e_name_len;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900433 memcpy(buffer, pval, size);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800434 }
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900435 error = size;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800436
437cleanup:
438 kzfree(base_addr);
439 return error;
440}
441
442ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
443{
444 struct inode *inode = dentry->d_inode;
445 struct f2fs_xattr_entry *entry;
446 void *base_addr;
447 int error = 0;
448 size_t rest = buffer_size;
449
450 base_addr = read_all_xattrs(inode, NULL);
451 if (!base_addr)
452 return -ENOMEM;
453
454 list_for_each_xattr(entry, base_addr) {
455 const struct xattr_handler *handler =
456 f2fs_xattr_handler(entry->e_name_index);
457 size_t size;
458
459 if (!handler)
460 continue;
461
462 size = handler->list(dentry, buffer, rest, entry->e_name,
463 entry->e_name_len, handler->flags);
464 if (buffer && size > rest) {
465 error = -ERANGE;
466 goto cleanup;
467 }
468
469 if (buffer)
470 buffer += size;
471 rest -= size;
472 }
473 error = buffer_size - rest;
474cleanup:
475 kzfree(base_addr);
476 return error;
477}
478
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900479static int __f2fs_setxattr(struct inode *inode, int index,
480 const char *name, const void *value, size_t size,
Jaegeuk Kim2eb8e7c2014-04-23 12:23:14 +0900481 struct page *ipage, int flags)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800482{
483 struct f2fs_inode_info *fi = F2FS_I(inode);
484 struct f2fs_xattr_entry *here, *last;
485 void *base_addr;
486 int found, newsize;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900487 size_t len;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800488 __u32 new_hsize;
489 int error = -ENOMEM;
490
491 if (name == NULL)
492 return -EINVAL;
493
494 if (value == NULL)
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900495 size = 0;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800496
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900497 len = strlen(name);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800498
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900499 if (len > F2FS_NAME_LEN || size > MAX_VALUE_LEN(inode))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800500 return -ERANGE;
501
502 base_addr = read_all_xattrs(inode, ipage);
503 if (!base_addr)
504 goto exit;
505
506 /* find entry with wanted name. */
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900507 here = __find_xattr(base_addr, index, len, name);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800508
509 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800510
Jaegeuk Kim75809482014-04-23 12:28:18 +0900511 if ((flags & XATTR_REPLACE) && !found) {
512 error = -ENODATA;
513 goto exit;
514 } else if ((flags & XATTR_CREATE) && found) {
515 error = -EEXIST;
516 goto exit;
517 }
518
519 last = here;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800520 while (!IS_XATTR_LAST_ENTRY(last))
521 last = XATTR_NEXT_ENTRY(last);
522
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900523 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800524
525 /* 1. Check space */
526 if (value) {
527 int free;
528 /*
529 * If value is NULL, it is remove operation.
arter97f4081402014-08-06 23:22:50 +0900530 * In case of update operation, we calculate free.
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800531 */
532 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
533 if (found)
534 free = free + ENTRY_SIZE(here);
535
Changman Leeb1a94e82013-11-15 10:42:51 +0900536 if (unlikely(free < newsize)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800537 error = -ENOSPC;
538 goto exit;
539 }
540 }
541
542 /* 2. Remove old entry */
543 if (found) {
544 /*
545 * If entry is found, remove old entry.
546 * If not found, remove operation is not needed.
547 */
548 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
549 int oldsize = ENTRY_SIZE(here);
550
551 memmove(here, next, (char *)last - (char *)next);
552 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
553 memset(last, 0, oldsize);
554 }
555
556 new_hsize = (char *)last - (char *)base_addr;
557
558 /* 3. Write new entry */
559 if (value) {
560 char *pval;
561 /*
562 * Before we come here, old entry is removed.
563 * We just write new entry.
564 */
565 memset(last, 0, newsize);
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900566 last->e_name_index = index;
567 last->e_name_len = len;
568 memcpy(last->e_name, name, len);
569 pval = last->e_name + len;
570 memcpy(pval, value, size);
571 last->e_value_size = cpu_to_le16(size);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800572 new_hsize += newsize;
573 }
574
575 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
576 if (error)
577 goto exit;
578
579 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
580 inode->i_mode = fi->i_acl_mode;
581 inode->i_ctime = CURRENT_TIME;
582 clear_inode_flag(fi, FI_ACL_MODE);
583 }
584
585 if (ipage)
586 update_inode(inode, ipage);
587 else
588 update_inode_page(inode);
589exit:
590 kzfree(base_addr);
591 return error;
592}
593
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900594int f2fs_setxattr(struct inode *inode, int index, const char *name,
595 const void *value, size_t size,
Jaegeuk Kim2eb8e7c2014-04-23 12:23:14 +0900596 struct page *ipage, int flags)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800597{
598 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
599 int err;
600
Jaegeuk Kim26d712d2014-06-01 23:24:30 +0900601 /* this case is only from init_inode_metadata */
602 if (ipage)
603 return __f2fs_setxattr(inode, index, name, value,
604 size, ipage, flags);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800605 f2fs_balance_fs(sbi);
606
607 f2fs_lock_op(sbi);
Jaegeuk Kimee722542014-03-20 19:10:08 +0900608 /* protect xattr_ver */
609 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kim2eb8e7c2014-04-23 12:23:14 +0900610 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
Jaegeuk Kimee722542014-03-20 19:10:08 +0900611 up_write(&F2FS_I(inode)->i_sem);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800612 f2fs_unlock_op(sbi);
613
614 return err;
615}