blob: e64317363debac7582db130db7c7d9d71094f7e9 [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>
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -080024#include <linux/posix_acl_xattr.h>
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090025#include "f2fs.h"
26#include "xattr.h"
27
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020028static size_t f2fs_xattr_generic_list(const struct xattr_handler *handler,
29 struct dentry *dentry, char *list, size_t list_size,
30 const char *name, size_t len)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090031{
32 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
33 int total_len, prefix_len = 0;
34 const char *prefix = NULL;
35
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020036 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090037 case F2FS_XATTR_INDEX_USER:
38 if (!test_opt(sbi, XATTR_USER))
39 return -EOPNOTSUPP;
40 prefix = XATTR_USER_PREFIX;
41 prefix_len = XATTR_USER_PREFIX_LEN;
42 break;
43 case F2FS_XATTR_INDEX_TRUSTED:
44 if (!capable(CAP_SYS_ADMIN))
45 return -EPERM;
46 prefix = XATTR_TRUSTED_PREFIX;
47 prefix_len = XATTR_TRUSTED_PREFIX_LEN;
48 break;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090049 case F2FS_XATTR_INDEX_SECURITY:
50 prefix = XATTR_SECURITY_PREFIX;
51 prefix_len = XATTR_SECURITY_PREFIX_LEN;
52 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090053 default:
54 return -EINVAL;
55 }
56
Jaegeuk Kime1123262014-04-23 12:17:25 +090057 total_len = prefix_len + len + 1;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090058 if (list && total_len <= list_size) {
59 memcpy(list, prefix, prefix_len);
Jaegeuk Kime1123262014-04-23 12:17:25 +090060 memcpy(list + prefix_len, name, len);
61 list[prefix_len + len] = '\0';
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090062 }
63 return total_len;
64}
65
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020066static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
67 struct dentry *dentry, const char *name, void *buffer,
68 size_t size)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090069{
70 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
71
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020072 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090073 case F2FS_XATTR_INDEX_USER:
74 if (!test_opt(sbi, XATTR_USER))
75 return -EOPNOTSUPP;
76 break;
77 case F2FS_XATTR_INDEX_TRUSTED:
78 if (!capable(CAP_SYS_ADMIN))
79 return -EPERM;
80 break;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +090081 case F2FS_XATTR_INDEX_SECURITY:
82 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090083 default:
84 return -EINVAL;
85 }
86 if (strcmp(name, "") == 0)
87 return -EINVAL;
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020088 return f2fs_getxattr(d_inode(dentry), handler->flags, name,
89 buffer, size, NULL);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090090}
91
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020092static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
93 struct dentry *dentry, const char *name, const void *value,
94 size_t size, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090095{
96 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
97
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +020098 switch (handler->flags) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +090099 case F2FS_XATTR_INDEX_USER:
100 if (!test_opt(sbi, XATTR_USER))
101 return -EOPNOTSUPP;
102 break;
103 case F2FS_XATTR_INDEX_TRUSTED:
104 if (!capable(CAP_SYS_ADMIN))
105 return -EPERM;
106 break;
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900107 case F2FS_XATTR_INDEX_SECURITY:
108 break;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900109 default:
110 return -EINVAL;
111 }
112 if (strcmp(name, "") == 0)
113 return -EINVAL;
114
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200115 return f2fs_setxattr(d_inode(dentry), handler->flags, name,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900116 value, size, NULL, flags);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900117}
118
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200119static size_t f2fs_xattr_advise_list(const struct xattr_handler *handler,
120 struct dentry *dentry, char *list, size_t list_size,
121 const char *name, size_t len)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900122{
123 const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
124 size_t size;
125
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200126 if (handler->flags != F2FS_XATTR_INDEX_ADVISE)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900127 return 0;
128
129 size = strlen(xname) + 1;
130 if (list && size <= list_size)
131 memcpy(list, xname, size);
132 return size;
133}
134
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200135static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
136 struct dentry *dentry, const char *name, void *buffer,
137 size_t size)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900138{
David Howells2b0143b2015-03-17 22:25:59 +0000139 struct inode *inode = d_inode(dentry);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900140
141 if (strcmp(name, "") != 0)
142 return -EINVAL;
143
Chao Yu84e97c22015-03-23 10:36:15 +0800144 if (buffer)
145 *((char *)buffer) = F2FS_I(inode)->i_advise;
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900146 return sizeof(char);
147}
148
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200149static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
150 struct dentry *dentry, const char *name, const void *value,
151 size_t size, int flags)
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900152{
David Howells2b0143b2015-03-17 22:25:59 +0000153 struct inode *inode = d_inode(dentry);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900154
155 if (strcmp(name, "") != 0)
156 return -EINVAL;
157 if (!inode_owner_or_capable(inode))
158 return -EPERM;
159 if (value == NULL)
160 return -EINVAL;
161
162 F2FS_I(inode)->i_advise |= *(char *)value;
Chao Yu30c62fd2015-03-23 10:37:39 +0800163 mark_inode_dirty(inode);
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900164 return 0;
165}
166
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900167#ifdef CONFIG_F2FS_FS_SECURITY
168static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
169 void *page)
170{
171 const struct xattr *xattr;
172 int err = 0;
173
174 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Jaegeuk Kimd631abd2014-06-01 23:24:30 +0900175 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900176 xattr->name, xattr->value,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900177 xattr->value_len, (struct page *)page, 0);
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900178 if (err < 0)
179 break;
180 }
181 return err;
182}
183
184int f2fs_init_security(struct inode *inode, struct inode *dir,
185 const struct qstr *qstr, struct page *ipage)
186{
187 return security_inode_init_security(inode, dir, qstr,
188 &f2fs_initxattrs, ipage);
189}
190#endif
191
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900192const struct xattr_handler f2fs_xattr_user_handler = {
193 .prefix = XATTR_USER_PREFIX,
194 .flags = F2FS_XATTR_INDEX_USER,
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_trusted_handler = {
201 .prefix = XATTR_TRUSTED_PREFIX,
202 .flags = F2FS_XATTR_INDEX_TRUSTED,
203 .list = f2fs_xattr_generic_list,
204 .get = f2fs_xattr_generic_get,
205 .set = f2fs_xattr_generic_set,
206};
207
Jaegeuk Kim573ea5f2012-11-30 17:32:08 +0900208const struct xattr_handler f2fs_xattr_advise_handler = {
209 .prefix = F2FS_SYSTEM_ADVISE_PREFIX,
210 .flags = F2FS_XATTR_INDEX_ADVISE,
211 .list = f2fs_xattr_advise_list,
212 .get = f2fs_xattr_advise_get,
213 .set = f2fs_xattr_advise_set,
214};
215
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900216const struct xattr_handler f2fs_xattr_security_handler = {
217 .prefix = XATTR_SECURITY_PREFIX,
218 .flags = F2FS_XATTR_INDEX_SECURITY,
219 .list = f2fs_xattr_generic_list,
220 .get = f2fs_xattr_generic_get,
221 .set = f2fs_xattr_generic_set,
222};
223
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900224static const struct xattr_handler *f2fs_xattr_handler_map[] = {
225 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
226#ifdef CONFIG_F2FS_FS_POSIX_ACL
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800227 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
228 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900229#endif
230 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900231#ifdef CONFIG_F2FS_FS_SECURITY
232 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
233#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900234 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
235};
236
237const struct xattr_handler *f2fs_xattr_handlers[] = {
238 &f2fs_xattr_user_handler,
239#ifdef CONFIG_F2FS_FS_POSIX_ACL
Christoph Hellwiga6dda0e2013-12-20 05:16:45 -0800240 &posix_acl_access_xattr_handler,
241 &posix_acl_default_xattr_handler,
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900242#endif
243 &f2fs_xattr_trusted_handler,
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900244#ifdef CONFIG_F2FS_FS_SECURITY
245 &f2fs_xattr_security_handler,
246#endif
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900247 &f2fs_xattr_advise_handler,
248 NULL,
249};
250
Jaegeuk Kime1123262014-04-23 12:17:25 +0900251static inline const struct xattr_handler *f2fs_xattr_handler(int index)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900252{
253 const struct xattr_handler *handler = NULL;
254
Jaegeuk Kime1123262014-04-23 12:17:25 +0900255 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
256 handler = f2fs_xattr_handler_map[index];
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900257 return handler;
258}
259
Jaegeuk Kime1123262014-04-23 12:17:25 +0900260static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
261 size_t len, const char *name)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900262{
263 struct f2fs_xattr_entry *entry;
264
265 list_for_each_xattr(entry, base_addr) {
Jaegeuk Kime1123262014-04-23 12:17:25 +0900266 if (entry->e_name_index != index)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900267 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900268 if (entry->e_name_len != len)
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900269 continue;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900270 if (!memcmp(entry->e_name, name, len))
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900271 break;
272 }
273 return entry;
274}
275
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900276static void *read_all_xattrs(struct inode *inode, struct page *ipage)
277{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700278 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900279 struct f2fs_xattr_header *header;
280 size_t size = PAGE_SIZE, inline_size = 0;
281 void *txattr_addr;
282
283 inline_size = inline_xattr_size(inode);
284
Jaegeuk Kim808a1d72014-03-20 22:21:08 +0900285 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900286 if (!txattr_addr)
287 return NULL;
288
289 /* read from inline xattr */
290 if (inline_size) {
291 struct page *page = NULL;
292 void *inline_addr;
293
294 if (ipage) {
295 inline_addr = inline_xattr_addr(ipage);
296 } else {
297 page = get_node_page(sbi, inode->i_ino);
298 if (IS_ERR(page))
299 goto fail;
300 inline_addr = inline_xattr_addr(page);
301 }
302 memcpy(txattr_addr, inline_addr, inline_size);
303 f2fs_put_page(page, 1);
304 }
305
306 /* read from xattr node block */
307 if (F2FS_I(inode)->i_xattr_nid) {
308 struct page *xpage;
309 void *xattr_addr;
310
311 /* The inode already has an extended attribute block. */
312 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
313 if (IS_ERR(xpage))
314 goto fail;
315
316 xattr_addr = page_address(xpage);
317 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
318 f2fs_put_page(xpage, 1);
319 }
320
321 header = XATTR_HDR(txattr_addr);
322
323 /* never been allocated xattrs */
324 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
325 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
326 header->h_refcount = cpu_to_le32(1);
327 }
328 return txattr_addr;
329fail:
330 kzfree(txattr_addr);
331 return NULL;
332}
333
334static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
335 void *txattr_addr, struct page *ipage)
336{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700337 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900338 size_t inline_size = 0;
339 void *xattr_addr;
340 struct page *xpage;
341 nid_t new_nid = 0;
342 int err;
343
344 inline_size = inline_xattr_size(inode);
345
346 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
347 if (!alloc_nid(sbi, &new_nid))
348 return -ENOSPC;
349
350 /* write to inline xattr */
351 if (inline_size) {
352 struct page *page = NULL;
353 void *inline_addr;
354
355 if (ipage) {
356 inline_addr = inline_xattr_addr(ipage);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900357 f2fs_wait_on_page_writeback(ipage, NODE);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900358 } else {
359 page = get_node_page(sbi, inode->i_ino);
360 if (IS_ERR(page)) {
361 alloc_nid_failed(sbi, new_nid);
362 return PTR_ERR(page);
363 }
364 inline_addr = inline_xattr_addr(page);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900365 f2fs_wait_on_page_writeback(page, NODE);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900366 }
367 memcpy(inline_addr, txattr_addr, inline_size);
368 f2fs_put_page(page, 1);
369
370 /* no need to use xattr node block */
371 if (hsize <= inline_size) {
372 err = truncate_xattr_node(inode, ipage);
373 alloc_nid_failed(sbi, new_nid);
374 return err;
375 }
376 }
377
378 /* write to xattr node block */
379 if (F2FS_I(inode)->i_xattr_nid) {
380 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
381 if (IS_ERR(xpage)) {
382 alloc_nid_failed(sbi, new_nid);
383 return PTR_ERR(xpage);
384 }
Jaegeuk Kim9850cf42014-09-02 15:52:58 -0700385 f2fs_bug_on(sbi, new_nid);
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900386 f2fs_wait_on_page_writeback(xpage, NODE);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900387 } else {
388 struct dnode_of_data dn;
389 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
390 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
391 if (IS_ERR(xpage)) {
392 alloc_nid_failed(sbi, new_nid);
393 return PTR_ERR(xpage);
394 }
395 alloc_nid_done(sbi, new_nid);
396 }
397
398 xattr_addr = page_address(xpage);
399 memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
400 sizeof(struct node_footer));
401 set_page_dirty(xpage);
402 f2fs_put_page(xpage, 1);
403
404 /* need to checkpoint during fsync */
405 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
406 return 0;
407}
408
Jaegeuk Kime1123262014-04-23 12:17:25 +0900409int f2fs_getxattr(struct inode *inode, int index, const char *name,
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700410 void *buffer, size_t buffer_size, struct page *ipage)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900411{
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900412 struct f2fs_xattr_entry *entry;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900413 void *base_addr;
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900414 int error = 0;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900415 size_t size, len;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900416
417 if (name == NULL)
418 return -EINVAL;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900419
420 len = strlen(name);
421 if (len > F2FS_NAME_LEN)
Chao Yu6e452d62014-03-22 14:59:45 +0800422 return -ERANGE;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900423
Jaegeuk Kimbce8d112014-10-13 19:42:53 -0700424 base_addr = read_all_xattrs(inode, ipage);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900425 if (!base_addr)
426 return -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900427
Jaegeuk Kime1123262014-04-23 12:17:25 +0900428 entry = __find_xattr(base_addr, index, len, name);
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900429 if (IS_XATTR_LAST_ENTRY(entry)) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900430 error = -ENODATA;
431 goto cleanup;
432 }
433
Jaegeuk Kime1123262014-04-23 12:17:25 +0900434 size = le16_to_cpu(entry->e_value_size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900435
Jaegeuk Kime1123262014-04-23 12:17:25 +0900436 if (buffer && size > buffer_size) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900437 error = -ERANGE;
438 goto cleanup;
439 }
440
441 if (buffer) {
442 char *pval = entry->e_name + entry->e_name_len;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900443 memcpy(buffer, pval, size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900444 }
Jaegeuk Kime1123262014-04-23 12:17:25 +0900445 error = size;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900446
447cleanup:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900448 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900449 return error;
450}
451
452ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
453{
David Howells2b0143b2015-03-17 22:25:59 +0000454 struct inode *inode = d_inode(dentry);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900455 struct f2fs_xattr_entry *entry;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900456 void *base_addr;
457 int error = 0;
458 size_t rest = buffer_size;
459
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900460 base_addr = read_all_xattrs(inode, NULL);
461 if (!base_addr)
462 return -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900463
464 list_for_each_xattr(entry, base_addr) {
465 const struct xattr_handler *handler =
466 f2fs_xattr_handler(entry->e_name_index);
467 size_t size;
468
469 if (!handler)
470 continue;
471
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +0200472 size = handler->list(handler, dentry, buffer, rest,
473 entry->e_name, entry->e_name_len);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900474 if (buffer && size > rest) {
475 error = -ERANGE;
476 goto cleanup;
477 }
478
479 if (buffer)
480 buffer += size;
481 rest -= size;
482 }
483 error = buffer_size - rest;
484cleanup:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900485 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900486 return error;
487}
488
Jaegeuk Kime1123262014-04-23 12:17:25 +0900489static int __f2fs_setxattr(struct inode *inode, int index,
490 const char *name, const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900491 struct page *ipage, int flags)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900492{
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900493 struct f2fs_inode_info *fi = F2FS_I(inode);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900494 struct f2fs_xattr_entry *here, *last;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900495 void *base_addr;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900496 int found, newsize;
Jaegeuk Kime1123262014-04-23 12:17:25 +0900497 size_t len;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900498 __u32 new_hsize;
499 int error = -ENOMEM;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900500
501 if (name == NULL)
502 return -EINVAL;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900503
504 if (value == NULL)
Jaegeuk Kime1123262014-04-23 12:17:25 +0900505 size = 0;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900506
Jaegeuk Kime1123262014-04-23 12:17:25 +0900507 len = strlen(name);
Namjae Jeon7c909772013-03-17 17:26:39 +0900508
Chao Yu037fe702015-07-13 17:45:19 +0800509 if (len > F2FS_NAME_LEN)
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900510 return -ERANGE;
511
Chao Yu037fe702015-07-13 17:45:19 +0800512 if (size > MAX_VALUE_LEN(inode))
513 return -E2BIG;
514
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900515 base_addr = read_all_xattrs(inode, ipage);
516 if (!base_addr)
517 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900518
519 /* find entry with wanted name. */
Jaegeuk Kime1123262014-04-23 12:17:25 +0900520 here = __find_xattr(base_addr, index, len, name);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900521
Jaegeuk Kimdd9cfe22013-08-13 10:13:55 +0900522 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900523
Jaegeuk Kim916decb2014-04-23 12:28:18 +0900524 if ((flags & XATTR_REPLACE) && !found) {
525 error = -ENODATA;
526 goto exit;
527 } else if ((flags & XATTR_CREATE) && found) {
528 error = -EEXIST;
529 goto exit;
530 }
531
532 last = here;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900533 while (!IS_XATTR_LAST_ENTRY(last))
534 last = XATTR_NEXT_ENTRY(last);
535
Jaegeuk Kime1123262014-04-23 12:17:25 +0900536 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900537
538 /* 1. Check space */
539 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900540 int free;
541 /*
542 * If value is NULL, it is remove operation.
arter97e1c42042014-08-06 23:22:50 +0900543 * In case of update operation, we calculate free.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900544 */
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900545 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900546 if (found)
Chao Yucc3de6a2013-10-29 14:17:05 +0800547 free = free + ENTRY_SIZE(here);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900548
Jaegeuk Kim6bacf522013-12-06 15:00:58 +0900549 if (unlikely(free < newsize)) {
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900550 error = -ENOSPC;
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900551 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900552 }
553 }
554
555 /* 2. Remove old entry */
556 if (found) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900557 /*
558 * If entry is found, remove old entry.
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900559 * If not found, remove operation is not needed.
560 */
561 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
562 int oldsize = ENTRY_SIZE(here);
563
564 memmove(here, next, (char *)last - (char *)next);
565 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
566 memset(last, 0, oldsize);
567 }
568
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900569 new_hsize = (char *)last - (char *)base_addr;
570
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900571 /* 3. Write new entry */
572 if (value) {
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900573 char *pval;
574 /*
575 * Before we come here, old entry is removed.
576 * We just write new entry.
577 */
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900578 memset(last, 0, newsize);
Jaegeuk Kime1123262014-04-23 12:17:25 +0900579 last->e_name_index = index;
580 last->e_name_len = len;
581 memcpy(last->e_name, name, len);
582 pval = last->e_name + len;
583 memcpy(pval, value, size);
584 last->e_value_size = cpu_to_le16(size);
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900585 new_hsize += newsize;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900586 }
587
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900588 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
589 if (error)
590 goto exit;
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900591
592 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
593 inode->i_mode = fi->i_acl_mode;
594 inode->i_ctime = CURRENT_TIME;
595 clear_inode_flag(fi, FI_ACL_MODE);
596 }
Jaegeuk Kimf424f662015-04-20 15:19:06 -0700597 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
598 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
599 f2fs_set_encrypted_inode(inode);
Jaegeuk Kime518ff82013-08-09 14:46:15 +0900600
Jaegeuk Kim8ae8f162013-06-03 19:46:19 +0900601 if (ipage)
602 update_inode(inode, ipage);
603 else
604 update_inode_page(inode);
Namjae Jeon7c909772013-03-17 17:26:39 +0900605exit:
Jaegeuk Kim65985d92013-08-14 21:57:27 +0900606 kzfree(base_addr);
Jaegeuk Kimaf48b852012-11-02 17:12:17 +0900607 return error;
608}
Russ Knize52ab9562013-09-24 15:49:23 -0500609
Jaegeuk Kime1123262014-04-23 12:17:25 +0900610int f2fs_setxattr(struct inode *inode, int index, const char *name,
611 const void *value, size_t size,
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900612 struct page *ipage, int flags)
Russ Knize52ab9562013-09-24 15:49:23 -0500613{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700614 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Russ Knize52ab9562013-09-24 15:49:23 -0500615 int err;
616
Jaegeuk Kimd631abd2014-06-01 23:24:30 +0900617 /* this case is only from init_inode_metadata */
618 if (ipage)
619 return __f2fs_setxattr(inode, index, name, value,
620 size, ipage, flags);
Russ Knize52ab9562013-09-24 15:49:23 -0500621 f2fs_balance_fs(sbi);
622
Gu Zhenge4795562013-09-27 18:08:30 +0800623 f2fs_lock_op(sbi);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900624 /* protect xattr_ver */
625 down_write(&F2FS_I(inode)->i_sem);
Jaegeuk Kimc02745e2014-04-23 12:23:14 +0900626 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
Jaegeuk Kimd928bfb2014-03-20 19:10:08 +0900627 up_write(&F2FS_I(inode)->i_sem);
Gu Zhenge4795562013-09-27 18:08:30 +0800628 f2fs_unlock_op(sbi);
Russ Knize52ab9562013-09-24 15:49:23 -0500629
630 return err;
631}