Linux 4.8.3
[linux/fpc-iii.git] / fs / f2fs / xattr.c
blobc8898b5148eb62134db2467d579d5401f8efdcfd
1 /*
2 * fs/f2fs/xattr.c
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * Portions of this code from linux/fs/ext2/xattr.c
9 * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
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.
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.
21 #include <linux/rwsem.h>
22 #include <linux/f2fs_fs.h>
23 #include <linux/security.h>
24 #include <linux/posix_acl_xattr.h>
25 #include "f2fs.h"
26 #include "xattr.h"
28 static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
29 struct dentry *unused, struct inode *inode,
30 const char *name, void *buffer, size_t size)
32 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
34 switch (handler->flags) {
35 case F2FS_XATTR_INDEX_USER:
36 if (!test_opt(sbi, XATTR_USER))
37 return -EOPNOTSUPP;
38 break;
39 case F2FS_XATTR_INDEX_TRUSTED:
40 if (!capable(CAP_SYS_ADMIN))
41 return -EPERM;
42 break;
43 case F2FS_XATTR_INDEX_SECURITY:
44 break;
45 default:
46 return -EINVAL;
48 return f2fs_getxattr(inode, handler->flags, name,
49 buffer, size, NULL);
52 static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
53 struct dentry *unused, struct inode *inode,
54 const char *name, const void *value,
55 size_t size, int flags)
57 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
59 switch (handler->flags) {
60 case F2FS_XATTR_INDEX_USER:
61 if (!test_opt(sbi, XATTR_USER))
62 return -EOPNOTSUPP;
63 break;
64 case F2FS_XATTR_INDEX_TRUSTED:
65 if (!capable(CAP_SYS_ADMIN))
66 return -EPERM;
67 break;
68 case F2FS_XATTR_INDEX_SECURITY:
69 break;
70 default:
71 return -EINVAL;
73 return f2fs_setxattr(inode, handler->flags, name,
74 value, size, NULL, flags);
77 static bool f2fs_xattr_user_list(struct dentry *dentry)
79 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
81 return test_opt(sbi, XATTR_USER);
84 static bool f2fs_xattr_trusted_list(struct dentry *dentry)
86 return capable(CAP_SYS_ADMIN);
89 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
90 struct dentry *unused, struct inode *inode,
91 const char *name, void *buffer, size_t size)
93 if (buffer)
94 *((char *)buffer) = F2FS_I(inode)->i_advise;
95 return sizeof(char);
98 static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
99 struct dentry *unused, struct inode *inode,
100 const char *name, const void *value,
101 size_t size, int flags)
103 if (!inode_owner_or_capable(inode))
104 return -EPERM;
105 if (value == NULL)
106 return -EINVAL;
108 F2FS_I(inode)->i_advise |= *(char *)value;
109 f2fs_mark_inode_dirty_sync(inode);
110 return 0;
113 #ifdef CONFIG_F2FS_FS_SECURITY
114 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
115 void *page)
117 const struct xattr *xattr;
118 int err = 0;
120 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
121 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
122 xattr->name, xattr->value,
123 xattr->value_len, (struct page *)page, 0);
124 if (err < 0)
125 break;
127 return err;
130 int f2fs_init_security(struct inode *inode, struct inode *dir,
131 const struct qstr *qstr, struct page *ipage)
133 return security_inode_init_security(inode, dir, qstr,
134 &f2fs_initxattrs, ipage);
136 #endif
138 const struct xattr_handler f2fs_xattr_user_handler = {
139 .prefix = XATTR_USER_PREFIX,
140 .flags = F2FS_XATTR_INDEX_USER,
141 .list = f2fs_xattr_user_list,
142 .get = f2fs_xattr_generic_get,
143 .set = f2fs_xattr_generic_set,
146 const struct xattr_handler f2fs_xattr_trusted_handler = {
147 .prefix = XATTR_TRUSTED_PREFIX,
148 .flags = F2FS_XATTR_INDEX_TRUSTED,
149 .list = f2fs_xattr_trusted_list,
150 .get = f2fs_xattr_generic_get,
151 .set = f2fs_xattr_generic_set,
154 const struct xattr_handler f2fs_xattr_advise_handler = {
155 .name = F2FS_SYSTEM_ADVISE_NAME,
156 .flags = F2FS_XATTR_INDEX_ADVISE,
157 .get = f2fs_xattr_advise_get,
158 .set = f2fs_xattr_advise_set,
161 const struct xattr_handler f2fs_xattr_security_handler = {
162 .prefix = XATTR_SECURITY_PREFIX,
163 .flags = F2FS_XATTR_INDEX_SECURITY,
164 .get = f2fs_xattr_generic_get,
165 .set = f2fs_xattr_generic_set,
168 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
169 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
170 #ifdef CONFIG_F2FS_FS_POSIX_ACL
171 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
172 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
173 #endif
174 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
175 #ifdef CONFIG_F2FS_FS_SECURITY
176 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
177 #endif
178 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
181 const struct xattr_handler *f2fs_xattr_handlers[] = {
182 &f2fs_xattr_user_handler,
183 #ifdef CONFIG_F2FS_FS_POSIX_ACL
184 &posix_acl_access_xattr_handler,
185 &posix_acl_default_xattr_handler,
186 #endif
187 &f2fs_xattr_trusted_handler,
188 #ifdef CONFIG_F2FS_FS_SECURITY
189 &f2fs_xattr_security_handler,
190 #endif
191 &f2fs_xattr_advise_handler,
192 NULL,
195 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
197 const struct xattr_handler *handler = NULL;
199 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
200 handler = f2fs_xattr_handler_map[index];
201 return handler;
204 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
205 size_t len, const char *name)
207 struct f2fs_xattr_entry *entry;
209 list_for_each_xattr(entry, base_addr) {
210 if (entry->e_name_index != index)
211 continue;
212 if (entry->e_name_len != len)
213 continue;
214 if (!memcmp(entry->e_name, name, len))
215 break;
217 return entry;
220 static void *read_all_xattrs(struct inode *inode, struct page *ipage)
222 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
223 struct f2fs_xattr_header *header;
224 size_t size = PAGE_SIZE, inline_size = 0;
225 void *txattr_addr;
227 inline_size = inline_xattr_size(inode);
229 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
230 if (!txattr_addr)
231 return NULL;
233 /* read from inline xattr */
234 if (inline_size) {
235 struct page *page = NULL;
236 void *inline_addr;
238 if (ipage) {
239 inline_addr = inline_xattr_addr(ipage);
240 } else {
241 page = get_node_page(sbi, inode->i_ino);
242 if (IS_ERR(page))
243 goto fail;
244 inline_addr = inline_xattr_addr(page);
246 memcpy(txattr_addr, inline_addr, inline_size);
247 f2fs_put_page(page, 1);
250 /* read from xattr node block */
251 if (F2FS_I(inode)->i_xattr_nid) {
252 struct page *xpage;
253 void *xattr_addr;
255 /* The inode already has an extended attribute block. */
256 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
257 if (IS_ERR(xpage))
258 goto fail;
260 xattr_addr = page_address(xpage);
261 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
262 f2fs_put_page(xpage, 1);
265 header = XATTR_HDR(txattr_addr);
267 /* never been allocated xattrs */
268 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
269 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
270 header->h_refcount = cpu_to_le32(1);
272 return txattr_addr;
273 fail:
274 kzfree(txattr_addr);
275 return NULL;
278 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
279 void *txattr_addr, struct page *ipage)
281 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
282 size_t inline_size = 0;
283 void *xattr_addr;
284 struct page *xpage;
285 nid_t new_nid = 0;
286 int err;
288 inline_size = inline_xattr_size(inode);
290 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
291 if (!alloc_nid(sbi, &new_nid))
292 return -ENOSPC;
294 /* write to inline xattr */
295 if (inline_size) {
296 struct page *page = NULL;
297 void *inline_addr;
299 if (ipage) {
300 inline_addr = inline_xattr_addr(ipage);
301 f2fs_wait_on_page_writeback(ipage, NODE, true);
302 set_page_dirty(ipage);
303 } else {
304 page = get_node_page(sbi, inode->i_ino);
305 if (IS_ERR(page)) {
306 alloc_nid_failed(sbi, new_nid);
307 return PTR_ERR(page);
309 inline_addr = inline_xattr_addr(page);
310 f2fs_wait_on_page_writeback(page, NODE, true);
312 memcpy(inline_addr, txattr_addr, inline_size);
313 f2fs_put_page(page, 1);
315 /* no need to use xattr node block */
316 if (hsize <= inline_size) {
317 err = truncate_xattr_node(inode, ipage);
318 alloc_nid_failed(sbi, new_nid);
319 return err;
323 /* write to xattr node block */
324 if (F2FS_I(inode)->i_xattr_nid) {
325 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
326 if (IS_ERR(xpage)) {
327 alloc_nid_failed(sbi, new_nid);
328 return PTR_ERR(xpage);
330 f2fs_bug_on(sbi, new_nid);
331 f2fs_wait_on_page_writeback(xpage, NODE, true);
332 } else {
333 struct dnode_of_data dn;
334 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
335 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
336 if (IS_ERR(xpage)) {
337 alloc_nid_failed(sbi, new_nid);
338 return PTR_ERR(xpage);
340 alloc_nid_done(sbi, new_nid);
343 xattr_addr = page_address(xpage);
344 memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
345 sizeof(struct node_footer));
346 set_page_dirty(xpage);
347 f2fs_put_page(xpage, 1);
349 /* need to checkpoint during fsync */
350 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
351 return 0;
354 int f2fs_getxattr(struct inode *inode, int index, const char *name,
355 void *buffer, size_t buffer_size, struct page *ipage)
357 struct f2fs_xattr_entry *entry;
358 void *base_addr;
359 int error = 0;
360 size_t size, len;
362 if (name == NULL)
363 return -EINVAL;
365 len = strlen(name);
366 if (len > F2FS_NAME_LEN)
367 return -ERANGE;
369 base_addr = read_all_xattrs(inode, ipage);
370 if (!base_addr)
371 return -ENOMEM;
373 entry = __find_xattr(base_addr, index, len, name);
374 if (IS_XATTR_LAST_ENTRY(entry)) {
375 error = -ENODATA;
376 goto cleanup;
379 size = le16_to_cpu(entry->e_value_size);
381 if (buffer && size > buffer_size) {
382 error = -ERANGE;
383 goto cleanup;
386 if (buffer) {
387 char *pval = entry->e_name + entry->e_name_len;
388 memcpy(buffer, pval, size);
390 error = size;
392 cleanup:
393 kzfree(base_addr);
394 return error;
397 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
399 struct inode *inode = d_inode(dentry);
400 struct f2fs_xattr_entry *entry;
401 void *base_addr;
402 int error = 0;
403 size_t rest = buffer_size;
405 base_addr = read_all_xattrs(inode, NULL);
406 if (!base_addr)
407 return -ENOMEM;
409 list_for_each_xattr(entry, base_addr) {
410 const struct xattr_handler *handler =
411 f2fs_xattr_handler(entry->e_name_index);
412 const char *prefix;
413 size_t prefix_len;
414 size_t size;
416 if (!handler || (handler->list && !handler->list(dentry)))
417 continue;
419 prefix = handler->prefix ?: handler->name;
420 prefix_len = strlen(prefix);
421 size = prefix_len + entry->e_name_len + 1;
422 if (buffer) {
423 if (size > rest) {
424 error = -ERANGE;
425 goto cleanup;
427 memcpy(buffer, prefix, prefix_len);
428 buffer += prefix_len;
429 memcpy(buffer, entry->e_name, entry->e_name_len);
430 buffer += entry->e_name_len;
431 *buffer++ = 0;
433 rest -= size;
435 error = buffer_size - rest;
436 cleanup:
437 kzfree(base_addr);
438 return error;
441 static int __f2fs_setxattr(struct inode *inode, int index,
442 const char *name, const void *value, size_t size,
443 struct page *ipage, int flags)
445 struct f2fs_xattr_entry *here, *last;
446 void *base_addr;
447 int found, newsize;
448 size_t len;
449 __u32 new_hsize;
450 int error = 0;
452 if (name == NULL)
453 return -EINVAL;
455 if (value == NULL)
456 size = 0;
458 len = strlen(name);
460 if (len > F2FS_NAME_LEN)
461 return -ERANGE;
463 if (size > MAX_VALUE_LEN(inode))
464 return -E2BIG;
466 base_addr = read_all_xattrs(inode, ipage);
467 if (!base_addr)
468 return -ENOMEM;
470 /* find entry with wanted name. */
471 here = __find_xattr(base_addr, index, len, name);
473 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
475 if ((flags & XATTR_REPLACE) && !found) {
476 error = -ENODATA;
477 goto exit;
478 } else if ((flags & XATTR_CREATE) && found) {
479 error = -EEXIST;
480 goto exit;
483 last = here;
484 while (!IS_XATTR_LAST_ENTRY(last))
485 last = XATTR_NEXT_ENTRY(last);
487 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
489 /* 1. Check space */
490 if (value) {
491 int free;
493 * If value is NULL, it is remove operation.
494 * In case of update operation, we calculate free.
496 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
497 if (found)
498 free = free + ENTRY_SIZE(here);
500 if (unlikely(free < newsize)) {
501 error = -E2BIG;
502 goto exit;
506 /* 2. Remove old entry */
507 if (found) {
509 * If entry is found, remove old entry.
510 * If not found, remove operation is not needed.
512 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
513 int oldsize = ENTRY_SIZE(here);
515 memmove(here, next, (char *)last - (char *)next);
516 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
517 memset(last, 0, oldsize);
520 new_hsize = (char *)last - (char *)base_addr;
522 /* 3. Write new entry */
523 if (value) {
524 char *pval;
526 * Before we come here, old entry is removed.
527 * We just write new entry.
529 last->e_name_index = index;
530 last->e_name_len = len;
531 memcpy(last->e_name, name, len);
532 pval = last->e_name + len;
533 memcpy(pval, value, size);
534 last->e_value_size = cpu_to_le16(size);
535 new_hsize += newsize;
538 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
539 if (error)
540 goto exit;
542 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
543 inode->i_mode = F2FS_I(inode)->i_acl_mode;
544 inode->i_ctime = CURRENT_TIME;
545 clear_inode_flag(inode, FI_ACL_MODE);
547 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
548 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
549 f2fs_set_encrypted_inode(inode);
550 f2fs_mark_inode_dirty_sync(inode);
551 exit:
552 kzfree(base_addr);
553 return error;
556 int f2fs_setxattr(struct inode *inode, int index, const char *name,
557 const void *value, size_t size,
558 struct page *ipage, int flags)
560 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
561 int err;
563 /* this case is only from init_inode_metadata */
564 if (ipage)
565 return __f2fs_setxattr(inode, index, name, value,
566 size, ipage, flags);
567 f2fs_balance_fs(sbi, true);
569 f2fs_lock_op(sbi);
570 /* protect xattr_ver */
571 down_write(&F2FS_I(inode)->i_sem);
572 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
573 up_write(&F2FS_I(inode)->i_sem);
574 f2fs_unlock_op(sbi);
576 f2fs_update_time(sbi, REQ_TIME);
577 return err;