stm class: Add SPDX GPL-2.0 header to replace GPLv2 boilerplate
[linux/fpc-iii.git] / fs / f2fs / xattr.c
blobae2dfa709f5dcf2f8e5b1084f5bce89377942ccc
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, true);
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 struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
221 void *base_addr, void **last_addr, int index,
222 size_t len, const char *name)
224 struct f2fs_xattr_entry *entry;
225 unsigned int inline_size = inline_xattr_size(inode);
227 list_for_each_xattr(entry, base_addr) {
228 if ((void *)entry + sizeof(__u32) > base_addr + inline_size ||
229 (void *)XATTR_NEXT_ENTRY(entry) + sizeof(__u32) >
230 base_addr + inline_size) {
231 *last_addr = entry;
232 return NULL;
234 if (entry->e_name_index != index)
235 continue;
236 if (entry->e_name_len != len)
237 continue;
238 if (!memcmp(entry->e_name, name, len))
239 break;
241 return entry;
244 static int read_inline_xattr(struct inode *inode, struct page *ipage,
245 void *txattr_addr)
247 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
248 unsigned int inline_size = inline_xattr_size(inode);
249 struct page *page = NULL;
250 void *inline_addr;
252 if (ipage) {
253 inline_addr = inline_xattr_addr(inode, ipage);
254 } else {
255 page = get_node_page(sbi, inode->i_ino);
256 if (IS_ERR(page))
257 return PTR_ERR(page);
259 inline_addr = inline_xattr_addr(inode, page);
261 memcpy(txattr_addr, inline_addr, inline_size);
262 f2fs_put_page(page, 1);
264 return 0;
267 static int read_xattr_block(struct inode *inode, void *txattr_addr)
269 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
270 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
271 unsigned int inline_size = inline_xattr_size(inode);
272 struct page *xpage;
273 void *xattr_addr;
275 /* The inode already has an extended attribute block. */
276 xpage = get_node_page(sbi, xnid);
277 if (IS_ERR(xpage))
278 return PTR_ERR(xpage);
280 xattr_addr = page_address(xpage);
281 memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
282 f2fs_put_page(xpage, 1);
284 return 0;
287 static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
288 unsigned int index, unsigned int len,
289 const char *name, struct f2fs_xattr_entry **xe,
290 void **base_addr)
292 void *cur_addr, *txattr_addr, *last_addr = NULL;
293 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
294 unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
295 unsigned int inline_size = inline_xattr_size(inode);
296 int err = 0;
298 if (!size && !inline_size)
299 return -ENODATA;
301 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
302 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
303 if (!txattr_addr)
304 return -ENOMEM;
306 /* read from inline xattr */
307 if (inline_size) {
308 err = read_inline_xattr(inode, ipage, txattr_addr);
309 if (err)
310 goto out;
312 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
313 index, len, name);
314 if (*xe)
315 goto check;
318 /* read from xattr node block */
319 if (xnid) {
320 err = read_xattr_block(inode, txattr_addr);
321 if (err)
322 goto out;
325 if (last_addr)
326 cur_addr = XATTR_HDR(last_addr) - 1;
327 else
328 cur_addr = txattr_addr;
330 *xe = __find_xattr(cur_addr, index, len, name);
331 check:
332 if (IS_XATTR_LAST_ENTRY(*xe)) {
333 err = -ENODATA;
334 goto out;
337 *base_addr = txattr_addr;
338 return 0;
339 out:
340 kzfree(txattr_addr);
341 return err;
344 static int read_all_xattrs(struct inode *inode, struct page *ipage,
345 void **base_addr)
347 struct f2fs_xattr_header *header;
348 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
349 unsigned int size = VALID_XATTR_BLOCK_SIZE;
350 unsigned int inline_size = inline_xattr_size(inode);
351 void *txattr_addr;
352 int err;
354 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
355 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
356 if (!txattr_addr)
357 return -ENOMEM;
359 /* read from inline xattr */
360 if (inline_size) {
361 err = read_inline_xattr(inode, ipage, txattr_addr);
362 if (err)
363 goto fail;
366 /* read from xattr node block */
367 if (xnid) {
368 err = read_xattr_block(inode, txattr_addr);
369 if (err)
370 goto fail;
373 header = XATTR_HDR(txattr_addr);
375 /* never been allocated xattrs */
376 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
377 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
378 header->h_refcount = cpu_to_le32(1);
380 *base_addr = txattr_addr;
381 return 0;
382 fail:
383 kzfree(txattr_addr);
384 return err;
387 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
388 void *txattr_addr, struct page *ipage)
390 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
391 size_t inline_size = inline_xattr_size(inode);
392 struct page *in_page = NULL;
393 void *xattr_addr;
394 void *inline_addr = NULL;
395 struct page *xpage;
396 nid_t new_nid = 0;
397 int err = 0;
399 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
400 if (!alloc_nid(sbi, &new_nid))
401 return -ENOSPC;
403 /* write to inline xattr */
404 if (inline_size) {
405 if (ipage) {
406 inline_addr = inline_xattr_addr(inode, ipage);
407 } else {
408 in_page = get_node_page(sbi, inode->i_ino);
409 if (IS_ERR(in_page)) {
410 alloc_nid_failed(sbi, new_nid);
411 return PTR_ERR(in_page);
413 inline_addr = inline_xattr_addr(inode, in_page);
416 f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
417 NODE, true);
418 /* no need to use xattr node block */
419 if (hsize <= inline_size) {
420 err = truncate_xattr_node(inode);
421 alloc_nid_failed(sbi, new_nid);
422 if (err) {
423 f2fs_put_page(in_page, 1);
424 return err;
426 memcpy(inline_addr, txattr_addr, inline_size);
427 set_page_dirty(ipage ? ipage : in_page);
428 goto in_page_out;
432 /* write to xattr node block */
433 if (F2FS_I(inode)->i_xattr_nid) {
434 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
435 if (IS_ERR(xpage)) {
436 err = PTR_ERR(xpage);
437 alloc_nid_failed(sbi, new_nid);
438 goto in_page_out;
440 f2fs_bug_on(sbi, new_nid);
441 f2fs_wait_on_page_writeback(xpage, NODE, true);
442 } else {
443 struct dnode_of_data dn;
444 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
445 xpage = new_node_page(&dn, XATTR_NODE_OFFSET);
446 if (IS_ERR(xpage)) {
447 err = PTR_ERR(xpage);
448 alloc_nid_failed(sbi, new_nid);
449 goto in_page_out;
451 alloc_nid_done(sbi, new_nid);
453 xattr_addr = page_address(xpage);
455 if (inline_size)
456 memcpy(inline_addr, txattr_addr, inline_size);
457 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
459 if (inline_size)
460 set_page_dirty(ipage ? ipage : in_page);
461 set_page_dirty(xpage);
463 f2fs_put_page(xpage, 1);
464 in_page_out:
465 f2fs_put_page(in_page, 1);
466 return err;
469 int f2fs_getxattr(struct inode *inode, int index, const char *name,
470 void *buffer, size_t buffer_size, struct page *ipage)
472 struct f2fs_xattr_entry *entry = NULL;
473 int error = 0;
474 unsigned int size, len;
475 void *base_addr = NULL;
477 if (name == NULL)
478 return -EINVAL;
480 len = strlen(name);
481 if (len > F2FS_NAME_LEN)
482 return -ERANGE;
484 down_read(&F2FS_I(inode)->i_xattr_sem);
485 error = lookup_all_xattrs(inode, ipage, index, len, name,
486 &entry, &base_addr);
487 up_read(&F2FS_I(inode)->i_xattr_sem);
488 if (error)
489 return error;
491 size = le16_to_cpu(entry->e_value_size);
493 if (buffer && size > buffer_size) {
494 error = -ERANGE;
495 goto out;
498 if (buffer) {
499 char *pval = entry->e_name + entry->e_name_len;
500 memcpy(buffer, pval, size);
502 error = size;
503 out:
504 kzfree(base_addr);
505 return error;
508 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
510 struct inode *inode = d_inode(dentry);
511 struct f2fs_xattr_entry *entry;
512 void *base_addr;
513 int error = 0;
514 size_t rest = buffer_size;
516 down_read(&F2FS_I(inode)->i_xattr_sem);
517 error = read_all_xattrs(inode, NULL, &base_addr);
518 up_read(&F2FS_I(inode)->i_xattr_sem);
519 if (error)
520 return error;
522 list_for_each_xattr(entry, base_addr) {
523 const struct xattr_handler *handler =
524 f2fs_xattr_handler(entry->e_name_index);
525 const char *prefix;
526 size_t prefix_len;
527 size_t size;
529 if (!handler || (handler->list && !handler->list(dentry)))
530 continue;
532 prefix = handler->prefix ?: handler->name;
533 prefix_len = strlen(prefix);
534 size = prefix_len + entry->e_name_len + 1;
535 if (buffer) {
536 if (size > rest) {
537 error = -ERANGE;
538 goto cleanup;
540 memcpy(buffer, prefix, prefix_len);
541 buffer += prefix_len;
542 memcpy(buffer, entry->e_name, entry->e_name_len);
543 buffer += entry->e_name_len;
544 *buffer++ = 0;
546 rest -= size;
548 error = buffer_size - rest;
549 cleanup:
550 kzfree(base_addr);
551 return error;
554 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
555 const void *value, size_t size)
557 void *pval = entry->e_name + entry->e_name_len;
559 return (le16_to_cpu(entry->e_value_size) == size) &&
560 !memcmp(pval, value, size);
563 static int __f2fs_setxattr(struct inode *inode, int index,
564 const char *name, const void *value, size_t size,
565 struct page *ipage, int flags)
567 struct f2fs_xattr_entry *here, *last;
568 void *base_addr;
569 int found, newsize;
570 size_t len;
571 __u32 new_hsize;
572 int error = 0;
574 if (name == NULL)
575 return -EINVAL;
577 if (value == NULL)
578 size = 0;
580 len = strlen(name);
582 if (len > F2FS_NAME_LEN)
583 return -ERANGE;
585 if (size > MAX_VALUE_LEN(inode))
586 return -E2BIG;
588 error = read_all_xattrs(inode, ipage, &base_addr);
589 if (error)
590 return error;
592 /* find entry with wanted name. */
593 here = __find_xattr(base_addr, index, len, name);
595 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
597 if (found) {
598 if ((flags & XATTR_CREATE)) {
599 error = -EEXIST;
600 goto exit;
603 if (value && f2fs_xattr_value_same(here, value, size))
604 goto exit;
605 } else if ((flags & XATTR_REPLACE)) {
606 error = -ENODATA;
607 goto exit;
610 last = here;
611 while (!IS_XATTR_LAST_ENTRY(last))
612 last = XATTR_NEXT_ENTRY(last);
614 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
616 /* 1. Check space */
617 if (value) {
618 int free;
620 * If value is NULL, it is remove operation.
621 * In case of update operation, we calculate free.
623 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
624 if (found)
625 free = free + ENTRY_SIZE(here);
627 if (unlikely(free < newsize)) {
628 error = -E2BIG;
629 goto exit;
633 /* 2. Remove old entry */
634 if (found) {
636 * If entry is found, remove old entry.
637 * If not found, remove operation is not needed.
639 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
640 int oldsize = ENTRY_SIZE(here);
642 memmove(here, next, (char *)last - (char *)next);
643 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
644 memset(last, 0, oldsize);
647 new_hsize = (char *)last - (char *)base_addr;
649 /* 3. Write new entry */
650 if (value) {
651 char *pval;
653 * Before we come here, old entry is removed.
654 * We just write new entry.
656 last->e_name_index = index;
657 last->e_name_len = len;
658 memcpy(last->e_name, name, len);
659 pval = last->e_name + len;
660 memcpy(pval, value, size);
661 last->e_value_size = cpu_to_le16(size);
662 new_hsize += newsize;
665 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
666 if (error)
667 goto exit;
669 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
670 inode->i_mode = F2FS_I(inode)->i_acl_mode;
671 inode->i_ctime = current_time(inode);
672 clear_inode_flag(inode, FI_ACL_MODE);
674 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
675 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
676 f2fs_set_encrypted_inode(inode);
677 f2fs_mark_inode_dirty_sync(inode, true);
678 if (!error && S_ISDIR(inode->i_mode))
679 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
680 exit:
681 kzfree(base_addr);
682 return error;
685 int f2fs_setxattr(struct inode *inode, int index, const char *name,
686 const void *value, size_t size,
687 struct page *ipage, int flags)
689 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
690 int err;
692 err = dquot_initialize(inode);
693 if (err)
694 return err;
696 /* this case is only from init_inode_metadata */
697 if (ipage)
698 return __f2fs_setxattr(inode, index, name, value,
699 size, ipage, flags);
700 f2fs_balance_fs(sbi, true);
702 f2fs_lock_op(sbi);
703 /* protect xattr_ver */
704 down_write(&F2FS_I(inode)->i_sem);
705 down_write(&F2FS_I(inode)->i_xattr_sem);
706 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
707 up_write(&F2FS_I(inode)->i_xattr_sem);
708 up_write(&F2FS_I(inode)->i_sem);
709 f2fs_unlock_op(sbi);
711 f2fs_update_time(sbi, REQ_TIME);
712 return err;