blob: 1ac8a5f6e38096a702c62d2520b5b001a87f9fa2 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimaf48b852012-11-02 17:12:17 +09002 * 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>
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090023#include <linux/security.h>
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090024#include "f2fs.h"
25#include "xattr.h"
26
27static size_t f2fs_xattr_generic_list(struct dentry *dentry, char *list,
28 size_t list_size, const char *name, size_t name_len, int type)
29{
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;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090047 case F2FS_XATTR_INDEX_SECURITY:
48 prefix = XATTR_SECURITY_PREFIX;
49 prefix_len = XATTR_SECURITY_PREFIX_LEN;
50 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090051 default:
52 return -EINVAL;
53 }
54
55 total_len = prefix_len + name_len + 1;
56 if (list && total_len <= list_size) {
57 memcpy(list, prefix, prefix_len);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090058 memcpy(list + prefix_len, name, name_len);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090059 list[prefix_len + name_len] = '\0';
60 }
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;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090078 case F2FS_XATTR_INDEX_SECURITY:
79 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090080 default:
81 return -EINVAL;
82 }
83 if (strcmp(name, "") == 0)
84 return -EINVAL;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090085 return f2fs_getxattr(dentry->d_inode, type, name, buffer, size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090086}
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;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900102 case F2FS_XATTR_INDEX_SECURITY:
103 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900104 default:
105 return -EINVAL;
106 }
107 if (strcmp(name, "") == 0)
108 return -EINVAL;
109
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900110 return f2fs_setxattr(dentry->d_inode, type, name, value, size, NULL);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900111}
112
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900113static size_t f2fs_xattr_advise_list(struct dentry *dentry, char *list,
114 size_t list_size, const char *name, size_t name_len, int type)
115{
116 const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
117 size_t size;
118
119 if (type != F2FS_XATTR_INDEX_ADVISE)
120 return 0;
121
122 size = strlen(xname) + 1;
123 if (list && size <= list_size)
124 memcpy(list, xname, size);
125 return size;
126}
127
128static int f2fs_xattr_advise_get(struct dentry *dentry, const char *name,
129 void *buffer, size_t size, int type)
130{
131 struct inode *inode = dentry->d_inode;
132
133 if (strcmp(name, "") != 0)
134 return -EINVAL;
135
136 *((char *)buffer) = F2FS_I(inode)->i_advise;
137 return sizeof(char);
138}
139
140static int f2fs_xattr_advise_set(struct dentry *dentry, const char *name,
141 const void *value, size_t size, int flags, int type)
142{
143 struct inode *inode = dentry->d_inode;
144
145 if (strcmp(name, "") != 0)
146 return -EINVAL;
147 if (!inode_owner_or_capable(inode))
148 return -EPERM;
149 if (value == NULL)
150 return -EINVAL;
151
152 F2FS_I(inode)->i_advise |= *(char *)value;
153 return 0;
154}
155
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900156#ifdef CONFIG_F2FS_FS_SECURITY
157static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
158 void *page)
159{
160 const struct xattr *xattr;
161 int err = 0;
162
163 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
164 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
165 xattr->name, xattr->value,
166 xattr->value_len, (struct page *)page);
167 if (err < 0)
168 break;
169 }
170 return err;
171}
172
173int f2fs_init_security(struct inode *inode, struct inode *dir,
174 const struct qstr *qstr, struct page *ipage)
175{
176 return security_inode_init_security(inode, dir, qstr,
177 &f2fs_initxattrs, ipage);
178}
179#endif
180
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900181const struct xattr_handler f2fs_xattr_user_handler = {
182 .prefix = XATTR_USER_PREFIX,
183 .flags = F2FS_XATTR_INDEX_USER,
184 .list = f2fs_xattr_generic_list,
185 .get = f2fs_xattr_generic_get,
186 .set = f2fs_xattr_generic_set,
187};
188
189const struct xattr_handler f2fs_xattr_trusted_handler = {
190 .prefix = XATTR_TRUSTED_PREFIX,
191 .flags = F2FS_XATTR_INDEX_TRUSTED,
192 .list = f2fs_xattr_generic_list,
193 .get = f2fs_xattr_generic_get,
194 .set = f2fs_xattr_generic_set,
195};
196
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900197const struct xattr_handler f2fs_xattr_advise_handler = {
198 .prefix = F2FS_SYSTEM_ADVISE_PREFIX,
199 .flags = F2FS_XATTR_INDEX_ADVISE,
200 .list = f2fs_xattr_advise_list,
201 .get = f2fs_xattr_advise_get,
202 .set = f2fs_xattr_advise_set,
203};
204
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900205const struct xattr_handler f2fs_xattr_security_handler = {
206 .prefix = XATTR_SECURITY_PREFIX,
207 .flags = F2FS_XATTR_INDEX_SECURITY,
208 .list = f2fs_xattr_generic_list,
209 .get = f2fs_xattr_generic_get,
210 .set = f2fs_xattr_generic_set,
211};
212
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900213static const struct xattr_handler *f2fs_xattr_handler_map[] = {
214 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
215#ifdef CONFIG_F2FS_FS_POSIX_ACL
216 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &f2fs_xattr_acl_access_handler,
217 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &f2fs_xattr_acl_default_handler,
218#endif
219 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900220#ifdef CONFIG_F2FS_FS_SECURITY
221 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
222#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900223 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
224};
225
226const struct xattr_handler *f2fs_xattr_handlers[] = {
227 &f2fs_xattr_user_handler,
228#ifdef CONFIG_F2FS_FS_POSIX_ACL
229 &f2fs_xattr_acl_access_handler,
230 &f2fs_xattr_acl_default_handler,
231#endif
232 &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900233#ifdef CONFIG_F2FS_FS_SECURITY
234 &f2fs_xattr_security_handler,
235#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900236 &f2fs_xattr_advise_handler,
237 NULL,
238};
239
240static inline const struct xattr_handler *f2fs_xattr_handler(int name_index)
241{
242 const struct xattr_handler *handler = NULL;
243
244 if (name_index > 0 && name_index < ARRAY_SIZE(f2fs_xattr_handler_map))
245 handler = f2fs_xattr_handler_map[name_index];
246 return handler;
247}
248
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900249static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int name_index,
250 size_t name_len, const char *name)
251{
252 struct f2fs_xattr_entry *entry;
253
254 list_for_each_xattr(entry, base_addr) {
255 if (entry->e_name_index != name_index)
256 continue;
257 if (entry->e_name_len != name_len)
258 continue;
259 if (!memcmp(entry->e_name, name, name_len))
260 break;
261 }
262 return entry;
263}
264
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900265static void *read_all_xattrs(struct inode *inode, struct page *ipage)
266{
267 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
268 struct f2fs_xattr_header *header;
269 size_t size = PAGE_SIZE, inline_size = 0;
270 void *txattr_addr;
271
272 inline_size = inline_xattr_size(inode);
273
274 txattr_addr = kzalloc(inline_size + size, GFP_KERNEL);
275 if (!txattr_addr)
276 return NULL;
277
278 /* read from inline xattr */
279 if (inline_size) {
280 struct page *page = NULL;
281 void *inline_addr;
282
283 if (ipage) {
284 inline_addr = inline_xattr_addr(ipage);
285 } else {
286 page = get_node_page(sbi, inode->i_ino);
287 if (IS_ERR(page))
288 goto fail;
289 inline_addr = inline_xattr_addr(page);
290 }
291 memcpy(txattr_addr, inline_addr, inline_size);
292 f2fs_put_page(page, 1);
293 }
294
295 /* read from xattr node block */
296 if (F2FS_I(inode)->i_xattr_nid) {
297 struct page *xpage;
298 void *xattr_addr;
299
300 /* The inode already has an extended attribute block. */
301 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
302 if (IS_ERR(xpage))
303 goto fail;
304
305 xattr_addr = page_address(xpage);
306 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
307 f2fs_put_page(xpage, 1);
308 }
309
310 header = XATTR_HDR(txattr_addr);
311
312 /* never been allocated xattrs */
313 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
314 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
315 header->h_refcount = cpu_to_le32(1);
316 }
317 return txattr_addr;
318fail:
319 kzfree(txattr_addr);
320 return NULL;
321}
322
323static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
324 void *txattr_addr, struct page *ipage)
325{
326 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
327 size_t inline_size = 0;
328 void *xattr_addr;
329 struct page *xpage;
330 nid_t new_nid = 0;
331 int err;
332
333 inline_size = inline_xattr_size(inode);
334
335 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
336 if (!alloc_nid(sbi, &new_nid))
337 return -ENOSPC;
338
339 /* write to inline xattr */
340 if (inline_size) {
341 struct page *page = NULL;
342 void *inline_addr;
343
344 if (ipage) {
345 inline_addr = inline_xattr_addr(ipage);
346 } else {
347 page = get_node_page(sbi, inode->i_ino);
348 if (IS_ERR(page)) {
349 alloc_nid_failed(sbi, new_nid);
350 return PTR_ERR(page);
351 }
352 inline_addr = inline_xattr_addr(page);
353 }
354 memcpy(inline_addr, txattr_addr, inline_size);
355 f2fs_put_page(page, 1);
356
357 /* no need to use xattr node block */
358 if (hsize <= inline_size) {
359 err = truncate_xattr_node(inode, ipage);
360 alloc_nid_failed(sbi, new_nid);
361 return err;
362 }
363 }
364
365 /* write to xattr node block */
366 if (F2FS_I(inode)->i_xattr_nid) {
367 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
368 if (IS_ERR(xpage)) {
369 alloc_nid_failed(sbi, new_nid);
370 return PTR_ERR(xpage);
371 }
372 BUG_ON(new_nid);
373 } else {
374 struct dnode_of_data dn;
375 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
376 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
377 if (IS_ERR(xpage)) {
378 alloc_nid_failed(sbi, new_nid);
379 return PTR_ERR(xpage);
380 }
381 alloc_nid_done(sbi, new_nid);
382 }
383
384 xattr_addr = page_address(xpage);
385 memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
386 sizeof(struct node_footer));
387 set_page_dirty(xpage);
388 f2fs_put_page(xpage, 1);
389
390 /* need to checkpoint during fsync */
391 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
392 return 0;
393}
394
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900395int f2fs_getxattr(struct inode *inode, int name_index, const char *name,
396 void *buffer, size_t buffer_size)
397{
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900398 struct f2fs_xattr_entry *entry;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900399 void *base_addr;
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900400 int error = 0;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200401 size_t value_len, name_len;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900402
403 if (name == NULL)
404 return -EINVAL;
405 name_len = strlen(name);
406
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900407 base_addr = read_all_xattrs(inode, NULL);
408 if (!base_addr)
409 return -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900410
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900411 entry = __find_xattr(base_addr, name_index, name_len, name);
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900412 if (IS_XATTR_LAST_ENTRY(entry)) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900413 error = -ENODATA;
414 goto cleanup;
415 }
416
417 value_len = le16_to_cpu(entry->e_value_size);
418
419 if (buffer && value_len > buffer_size) {
420 error = -ERANGE;
421 goto cleanup;
422 }
423
424 if (buffer) {
425 char *pval = entry->e_name + entry->e_name_len;
426 memcpy(buffer, pval, value_len);
427 }
428 error = value_len;
429
430cleanup:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900431 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900432 return error;
433}
434
435ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
436{
437 struct inode *inode = dentry->d_inode;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900438 struct f2fs_xattr_entry *entry;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900439 void *base_addr;
440 int error = 0;
441 size_t rest = buffer_size;
442
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900443 base_addr = read_all_xattrs(inode, NULL);
444 if (!base_addr)
445 return -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900446
447 list_for_each_xattr(entry, base_addr) {
448 const struct xattr_handler *handler =
449 f2fs_xattr_handler(entry->e_name_index);
450 size_t size;
451
452 if (!handler)
453 continue;
454
455 size = handler->list(dentry, buffer, rest, entry->e_name,
456 entry->e_name_len, handler->flags);
457 if (buffer && size > rest) {
458 error = -ERANGE;
459 goto cleanup;
460 }
461
462 if (buffer)
463 buffer += size;
464 rest -= size;
465 }
466 error = buffer_size - rest;
467cleanup:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900468 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900469 return error;
470}
471
472int f2fs_setxattr(struct inode *inode, int name_index, const char *name,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900473 const void *value, size_t value_len, struct page *ipage)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900474{
475 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
476 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900477 struct f2fs_xattr_entry *here, *last;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900478 void *base_addr;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900479 int found, newsize;
Leon Romanovsky9836b8b2012-12-27 19:55:46 +0200480 size_t name_len;
Jaegeuk Kim39936832012-11-22 16:21:29 +0900481 int ilock;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900482 __u32 new_hsize;
483 int error = -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900484
485 if (name == NULL)
486 return -EINVAL;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900487
488 if (value == NULL)
489 value_len = 0;
490
Namjae Jeon7c909772013-03-17 17:26:39 +0900491 name_len = strlen(name);
492
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900493 if (name_len > F2FS_NAME_LEN || value_len > MAX_VALUE_LEN(inode))
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900494 return -ERANGE;
495
Jaegeuk Kim7d82db82013-01-11 13:10:49 +0900496 f2fs_balance_fs(sbi);
497
Jaegeuk Kim39936832012-11-22 16:21:29 +0900498 ilock = mutex_lock_op(sbi);
499
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900500 base_addr = read_all_xattrs(inode, ipage);
501 if (!base_addr)
502 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900503
504 /* find entry with wanted name. */
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900505 here = __find_xattr(base_addr, name_index, name_len, name);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900506
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900507 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900508 last = here;
509
510 while (!IS_XATTR_LAST_ENTRY(last))
511 last = XATTR_NEXT_ENTRY(last);
512
513 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) +
514 name_len + value_len);
515
516 /* 1. Check space */
517 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900518 int free;
519 /*
520 * If value is NULL, it is remove operation.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900521 * In case of update operation, we caculate free.
522 */
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900523 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900524 if (found)
525 free = free - ENTRY_SIZE(here);
526
527 if (free < newsize) {
528 error = -ENOSPC;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900529 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900530 }
531 }
532
533 /* 2. Remove old entry */
534 if (found) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900535 /*
536 * If entry is found, remove old entry.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900537 * If not found, remove operation is not needed.
538 */
539 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
540 int oldsize = ENTRY_SIZE(here);
541
542 memmove(here, next, (char *)last - (char *)next);
543 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
544 memset(last, 0, oldsize);
545 }
546
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900547 new_hsize = (char *)last - (char *)base_addr;
548
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900549 /* 3. Write new entry */
550 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900551 char *pval;
552 /*
553 * Before we come here, old entry is removed.
554 * We just write new entry.
555 */
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900556 memset(last, 0, newsize);
557 last->e_name_index = name_index;
558 last->e_name_len = name_len;
559 memcpy(last->e_name, name, name_len);
560 pval = last->e_name + name_len;
561 memcpy(pval, value, value_len);
562 last->e_value_size = cpu_to_le16(value_len);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900563 new_hsize += newsize;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900564 }
565
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900566 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
567 if (error)
568 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900569
570 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
571 inode->i_mode = fi->i_acl_mode;
572 inode->i_ctime = CURRENT_TIME;
573 clear_inode_flag(fi, FI_ACL_MODE);
574 }
Jaegeuk Kime518ff82013-08-09 14:46:15 +0900575
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900576 if (ipage)
577 update_inode(inode, ipage);
578 else
579 update_inode_page(inode);
Namjae Jeon7c909772013-03-17 17:26:39 +0900580exit:
Jaegeuk Kim39936832012-11-22 16:21:29 +0900581 mutex_unlock_op(sbi, ilock);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900582 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900583 return error;
584}