blob: dd0646a56874a1313cba0e1a4d0031dc423b4dbe [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;
Jaegeuk Kime0cea842015-02-18 20:43:11 -060085 return f2fs_getxattr(dentry->d_inode, type, name, buffer, size, NULL);
Linus Torvalds8005ecc2012-12-20 13:54:51 -080086}
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
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600137 if (buffer)
138 *((char *)buffer) = F2FS_I(inode)->i_advise;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800139 return sizeof(char);
140}
141
142static int f2fs_xattr_advise_set(struct dentry *dentry, const char *name,
143 const void *value, size_t size, int flags, int type)
144{
145 struct inode *inode = dentry->d_inode;
146
147 if (strcmp(name, "") != 0)
148 return -EINVAL;
149 if (!inode_owner_or_capable(inode))
150 return -EPERM;
151 if (value == NULL)
152 return -EINVAL;
153
154 F2FS_I(inode)->i_advise |= *(char *)value;
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600155 mark_inode_dirty(inode);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800156 return 0;
157}
158
159#ifdef CONFIG_F2FS_FS_SECURITY
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800160static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
161 void *page)
162{
163 const struct xattr *xattr;
164 int err = 0;
165
166 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Jaegeuk Kim26d712d2014-06-01 23:24:30 +0900167 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800168 xattr->name, xattr->value,
Jaegeuk Kim2eb8e7c2014-04-23 12:23:14 +0900169 xattr->value_len, (struct page *)page, 0);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800170 if (err < 0)
171 break;
172 }
173 return err;
174}
175
176int f2fs_init_security(struct inode *inode, struct inode *dir,
177 const struct qstr *qstr, struct page *ipage)
178{
179 return security_inode_init_security(inode, dir, qstr,
180 &f2fs_initxattrs, ipage);
181}
182#endif
183
184const struct xattr_handler f2fs_xattr_user_handler = {
185 .prefix = XATTR_USER_PREFIX,
186 .flags = F2FS_XATTR_INDEX_USER,
187 .list = f2fs_xattr_generic_list,
188 .get = f2fs_xattr_generic_get,
189 .set = f2fs_xattr_generic_set,
190};
191
192const struct xattr_handler f2fs_xattr_trusted_handler = {
193 .prefix = XATTR_TRUSTED_PREFIX,
194 .flags = F2FS_XATTR_INDEX_TRUSTED,
195 .list = f2fs_xattr_generic_list,
196 .get = f2fs_xattr_generic_get,
197 .set = f2fs_xattr_generic_set,
198};
199
200const struct xattr_handler f2fs_xattr_advise_handler = {
201 .prefix = F2FS_SYSTEM_ADVISE_PREFIX,
202 .flags = F2FS_XATTR_INDEX_ADVISE,
203 .list = f2fs_xattr_advise_list,
204 .get = f2fs_xattr_advise_get,
205 .set = f2fs_xattr_advise_set,
206};
207
208const struct xattr_handler f2fs_xattr_security_handler = {
209 .prefix = XATTR_SECURITY_PREFIX,
210 .flags = F2FS_XATTR_INDEX_SECURITY,
211 .list = f2fs_xattr_generic_list,
212 .get = f2fs_xattr_generic_get,
213 .set = f2fs_xattr_generic_set,
214};
215
216static const struct xattr_handler *f2fs_xattr_handler_map[] = {
217 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
218#ifdef CONFIG_F2FS_FS_POSIX_ACL
219 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &f2fs_xattr_acl_access_handler,
220 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &f2fs_xattr_acl_default_handler,
221#endif
222 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
223#ifdef CONFIG_F2FS_FS_SECURITY
224 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
225#endif
226 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
227};
228
229const struct xattr_handler *f2fs_xattr_handlers[] = {
230 &f2fs_xattr_user_handler,
231#ifdef CONFIG_F2FS_FS_POSIX_ACL
232 &f2fs_xattr_acl_access_handler,
233 &f2fs_xattr_acl_default_handler,
234#endif
235 &f2fs_xattr_trusted_handler,
236#ifdef CONFIG_F2FS_FS_SECURITY
237 &f2fs_xattr_security_handler,
238#endif
239 &f2fs_xattr_advise_handler,
240 NULL,
241};
242
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900243static inline const struct xattr_handler *f2fs_xattr_handler(int index)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800244{
245 const struct xattr_handler *handler = NULL;
246
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900247 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
248 handler = f2fs_xattr_handler_map[index];
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800249 return handler;
250}
251
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900252static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
253 size_t len, const char *name)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800254{
255 struct f2fs_xattr_entry *entry;
256
257 list_for_each_xattr(entry, base_addr) {
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900258 if (entry->e_name_index != index)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800259 continue;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900260 if (entry->e_name_len != len)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800261 continue;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900262 if (!memcmp(entry->e_name, name, len))
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800263 break;
264 }
265 return entry;
266}
267
268static void *read_all_xattrs(struct inode *inode, struct page *ipage)
269{
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600270 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800271 struct f2fs_xattr_header *header;
272 size_t size = PAGE_SIZE, inline_size = 0;
273 void *txattr_addr;
274
275 inline_size = inline_xattr_size(inode);
276
Jaegeuk Kim395204b2014-03-20 22:21:08 +0900277 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800278 if (!txattr_addr)
279 return NULL;
280
281 /* read from inline xattr */
282 if (inline_size) {
283 struct page *page = NULL;
284 void *inline_addr;
285
286 if (ipage) {
287 inline_addr = inline_xattr_addr(ipage);
288 } else {
289 page = get_node_page(sbi, inode->i_ino);
290 if (IS_ERR(page))
291 goto fail;
292 inline_addr = inline_xattr_addr(page);
293 }
294 memcpy(txattr_addr, inline_addr, inline_size);
295 f2fs_put_page(page, 1);
296 }
297
298 /* read from xattr node block */
299 if (F2FS_I(inode)->i_xattr_nid) {
300 struct page *xpage;
301 void *xattr_addr;
302
303 /* The inode already has an extended attribute block. */
304 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
305 if (IS_ERR(xpage))
306 goto fail;
307
308 xattr_addr = page_address(xpage);
309 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
310 f2fs_put_page(xpage, 1);
311 }
312
313 header = XATTR_HDR(txattr_addr);
314
315 /* never been allocated xattrs */
316 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
317 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
318 header->h_refcount = cpu_to_le32(1);
319 }
320 return txattr_addr;
321fail:
322 kzfree(txattr_addr);
323 return NULL;
324}
325
326static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
327 void *txattr_addr, struct page *ipage)
328{
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600329 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800330 size_t inline_size = 0;
331 void *xattr_addr;
332 struct page *xpage;
333 nid_t new_nid = 0;
334 int err;
335
336 inline_size = inline_xattr_size(inode);
337
338 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
339 if (!alloc_nid(sbi, &new_nid))
340 return -ENOSPC;
341
342 /* write to inline xattr */
343 if (inline_size) {
344 struct page *page = NULL;
345 void *inline_addr;
346
347 if (ipage) {
348 inline_addr = inline_xattr_addr(ipage);
Jaegeuk Kim0f9e6b52014-04-29 17:28:32 +0900349 f2fs_wait_on_page_writeback(ipage, NODE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800350 } 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);
Jaegeuk Kim0f9e6b52014-04-29 17:28:32 +0900357 f2fs_wait_on_page_writeback(page, NODE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800358 }
359 memcpy(inline_addr, txattr_addr, inline_size);
360 f2fs_put_page(page, 1);
361
362 /* no need to use xattr node block */
363 if (hsize <= inline_size) {
364 err = truncate_xattr_node(inode, ipage);
365 alloc_nid_failed(sbi, new_nid);
366 return err;
367 }
368 }
369
370 /* write to xattr node block */
371 if (F2FS_I(inode)->i_xattr_nid) {
372 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
373 if (IS_ERR(xpage)) {
374 alloc_nid_failed(sbi, new_nid);
375 return PTR_ERR(xpage);
376 }
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600377 f2fs_bug_on(sbi, new_nid);
Jaegeuk Kim0f9e6b52014-04-29 17:28:32 +0900378 f2fs_wait_on_page_writeback(xpage, NODE);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800379 } else {
380 struct dnode_of_data dn;
381 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
382 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
383 if (IS_ERR(xpage)) {
384 alloc_nid_failed(sbi, new_nid);
385 return PTR_ERR(xpage);
386 }
387 alloc_nid_done(sbi, new_nid);
388 }
389
390 xattr_addr = page_address(xpage);
391 memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
392 sizeof(struct node_footer));
393 set_page_dirty(xpage);
394 f2fs_put_page(xpage, 1);
395
396 /* need to checkpoint during fsync */
397 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
398 return 0;
399}
400
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900401int f2fs_getxattr(struct inode *inode, int index, const char *name,
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600402 void *buffer, size_t buffer_size, struct page *ipage)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800403{
404 struct f2fs_xattr_entry *entry;
405 void *base_addr;
406 int error = 0;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900407 size_t size, len;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800408
409 if (name == NULL)
410 return -EINVAL;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900411
412 len = strlen(name);
413 if (len > F2FS_NAME_LEN)
Chao Yu26a9bf52014-03-22 14:59:45 +0800414 return -ERANGE;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800415
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600416 base_addr = read_all_xattrs(inode, ipage);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800417 if (!base_addr)
418 return -ENOMEM;
419
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900420 entry = __find_xattr(base_addr, index, len, name);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800421 if (IS_XATTR_LAST_ENTRY(entry)) {
422 error = -ENODATA;
423 goto cleanup;
424 }
425
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900426 size = le16_to_cpu(entry->e_value_size);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800427
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900428 if (buffer && size > buffer_size) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800429 error = -ERANGE;
430 goto cleanup;
431 }
432
433 if (buffer) {
434 char *pval = entry->e_name + entry->e_name_len;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900435 memcpy(buffer, pval, size);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800436 }
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900437 error = size;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800438
439cleanup:
440 kzfree(base_addr);
441 return error;
442}
443
444ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
445{
446 struct inode *inode = dentry->d_inode;
447 struct f2fs_xattr_entry *entry;
448 void *base_addr;
449 int error = 0;
450 size_t rest = buffer_size;
451
452 base_addr = read_all_xattrs(inode, NULL);
453 if (!base_addr)
454 return -ENOMEM;
455
456 list_for_each_xattr(entry, base_addr) {
457 const struct xattr_handler *handler =
458 f2fs_xattr_handler(entry->e_name_index);
459 size_t size;
460
461 if (!handler)
462 continue;
463
464 size = handler->list(dentry, buffer, rest, entry->e_name,
465 entry->e_name_len, handler->flags);
466 if (buffer && size > rest) {
467 error = -ERANGE;
468 goto cleanup;
469 }
470
471 if (buffer)
472 buffer += size;
473 rest -= size;
474 }
475 error = buffer_size - rest;
476cleanup:
477 kzfree(base_addr);
478 return error;
479}
480
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900481static int __f2fs_setxattr(struct inode *inode, int index,
482 const char *name, const void *value, size_t size,
Jaegeuk Kim2eb8e7c2014-04-23 12:23:14 +0900483 struct page *ipage, int flags)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800484{
485 struct f2fs_inode_info *fi = F2FS_I(inode);
486 struct f2fs_xattr_entry *here, *last;
487 void *base_addr;
488 int found, newsize;
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900489 size_t len;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800490 __u32 new_hsize;
491 int error = -ENOMEM;
492
493 if (name == NULL)
494 return -EINVAL;
495
496 if (value == NULL)
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900497 size = 0;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800498
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900499 len = strlen(name);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800500
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600501 if (len > F2FS_NAME_LEN)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800502 return -ERANGE;
503
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600504 if (size > MAX_VALUE_LEN(inode))
505 return -E2BIG;
506
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800507 base_addr = read_all_xattrs(inode, ipage);
508 if (!base_addr)
509 goto exit;
510
511 /* find entry with wanted name. */
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900512 here = __find_xattr(base_addr, index, len, name);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800513
514 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800515
Jaegeuk Kim75809482014-04-23 12:28:18 +0900516 if ((flags & XATTR_REPLACE) && !found) {
517 error = -ENODATA;
518 goto exit;
519 } else if ((flags & XATTR_CREATE) && found) {
520 error = -EEXIST;
521 goto exit;
522 }
523
524 last = here;
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800525 while (!IS_XATTR_LAST_ENTRY(last))
526 last = XATTR_NEXT_ENTRY(last);
527
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900528 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800529
530 /* 1. Check space */
531 if (value) {
532 int free;
533 /*
534 * If value is NULL, it is remove operation.
arter97f4081402014-08-06 23:22:50 +0900535 * In case of update operation, we calculate free.
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800536 */
537 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
538 if (found)
539 free = free + ENTRY_SIZE(here);
540
Changman Leeb1a94e82013-11-15 10:42:51 +0900541 if (unlikely(free < newsize)) {
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800542 error = -ENOSPC;
543 goto exit;
544 }
545 }
546
547 /* 2. Remove old entry */
548 if (found) {
549 /*
550 * If entry is found, remove old entry.
551 * If not found, remove operation is not needed.
552 */
553 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
554 int oldsize = ENTRY_SIZE(here);
555
556 memmove(here, next, (char *)last - (char *)next);
557 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
558 memset(last, 0, oldsize);
559 }
560
561 new_hsize = (char *)last - (char *)base_addr;
562
563 /* 3. Write new entry */
564 if (value) {
565 char *pval;
566 /*
567 * Before we come here, old entry is removed.
568 * We just write new entry.
569 */
570 memset(last, 0, newsize);
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900571 last->e_name_index = index;
572 last->e_name_len = len;
573 memcpy(last->e_name, name, len);
574 pval = last->e_name + len;
575 memcpy(pval, value, size);
576 last->e_value_size = cpu_to_le16(size);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800577 new_hsize += newsize;
578 }
579
580 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
581 if (error)
582 goto exit;
583
584 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
585 inode->i_mode = fi->i_acl_mode;
586 inode->i_ctime = CURRENT_TIME;
587 clear_inode_flag(fi, FI_ACL_MODE);
588 }
Jaegeuk Kim4fae7dd2015-11-12 10:23:18 -0600589 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
590 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
591 f2fs_set_encrypted_inode(inode);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800592
593 if (ipage)
594 update_inode(inode, ipage);
595 else
596 update_inode_page(inode);
597exit:
598 kzfree(base_addr);
599 return error;
600}
601
Jaegeuk Kimb5614a32014-04-23 12:17:25 +0900602int f2fs_setxattr(struct inode *inode, int index, const char *name,
603 const void *value, size_t size,
Jaegeuk Kim2eb8e7c2014-04-23 12:23:14 +0900604 struct page *ipage, int flags)
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800605{
Jaegeuk Kime0cea842015-02-18 20:43:11 -0600606 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800607 int err;
608
Jaegeuk Kim26d712d2014-06-01 23:24:30 +0900609 /* this case is only from init_inode_metadata */
610 if (ipage)
611 return __f2fs_setxattr(inode, index, name, value,
612 size, ipage, flags);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800613 f2fs_balance_fs(sbi);
614
615 f2fs_lock_op(sbi);
Jaegeuk Kimee722542014-03-20 19:10:08 +0900616 /* protect xattr_ver */
617 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kim2eb8e7c2014-04-23 12:23:14 +0900618 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
Jaegeuk Kimee722542014-03-20 19:10:08 +0900619 up_write(&F2FS_I(inode)->i_sem);
Linus Torvalds8005ecc2012-12-20 13:54:51 -0800620 f2fs_unlock_op(sbi);
621
622 return err;
623}