1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
8 * Portions of this code from linux/fs/ext2/xattr.c
10 * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
12 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
13 * Extended attributes for symlinks and special files added per
14 * suggestion of Luka Renko <luka.renko@hermes.si>.
15 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
18 #include <linux/rwsem.h>
19 #include <linux/f2fs_fs.h>
20 #include <linux/security.h>
21 #include <linux/posix_acl_xattr.h>
26 static void *xattr_alloc(struct f2fs_sb_info *sbi, int size, bool *is_inline)
28 if (likely(size == sbi->inline_xattr_slab_size)) {
30 return kmem_cache_zalloc(sbi->inline_xattr_slab, GFP_NOFS);
33 return f2fs_kzalloc(sbi, size, GFP_NOFS);
36 static void xattr_free(struct f2fs_sb_info *sbi, void *xattr_addr,
40 kmem_cache_free(sbi->inline_xattr_slab, xattr_addr);
45 static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
46 struct dentry *unused, struct inode *inode,
47 const char *name, void *buffer, size_t size)
49 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
51 switch (handler->flags) {
52 case F2FS_XATTR_INDEX_USER:
53 if (!test_opt(sbi, XATTR_USER))
56 case F2FS_XATTR_INDEX_TRUSTED:
57 case F2FS_XATTR_INDEX_SECURITY:
62 return f2fs_getxattr(inode, handler->flags, name,
66 static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
67 struct user_namespace *mnt_userns,
68 struct dentry *unused, struct inode *inode,
69 const char *name, const void *value,
70 size_t size, int flags)
72 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
74 switch (handler->flags) {
75 case F2FS_XATTR_INDEX_USER:
76 if (!test_opt(sbi, XATTR_USER))
79 case F2FS_XATTR_INDEX_TRUSTED:
80 case F2FS_XATTR_INDEX_SECURITY:
85 return f2fs_setxattr(inode, handler->flags, name,
86 value, size, NULL, flags);
89 static bool f2fs_xattr_user_list(struct dentry *dentry)
91 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
93 return test_opt(sbi, XATTR_USER);
96 static bool f2fs_xattr_trusted_list(struct dentry *dentry)
98 return capable(CAP_SYS_ADMIN);
101 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
102 struct dentry *unused, struct inode *inode,
103 const char *name, void *buffer, size_t size)
106 *((char *)buffer) = F2FS_I(inode)->i_advise;
110 static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
111 struct user_namespace *mnt_userns,
112 struct dentry *unused, struct inode *inode,
113 const char *name, const void *value,
114 size_t size, int flags)
116 unsigned char old_advise = F2FS_I(inode)->i_advise;
117 unsigned char new_advise;
119 if (!inode_owner_or_capable(&init_user_ns, inode))
124 new_advise = *(char *)value;
125 if (new_advise & ~FADVISE_MODIFIABLE_BITS)
128 new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
129 new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
131 F2FS_I(inode)->i_advise = new_advise;
132 f2fs_mark_inode_dirty_sync(inode, true);
136 #ifdef CONFIG_F2FS_FS_SECURITY
137 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
140 const struct xattr *xattr;
143 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
144 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
145 xattr->name, xattr->value,
146 xattr->value_len, (struct page *)page, 0);
153 int f2fs_init_security(struct inode *inode, struct inode *dir,
154 const struct qstr *qstr, struct page *ipage)
156 return security_inode_init_security(inode, dir, qstr,
157 &f2fs_initxattrs, ipage);
161 const struct xattr_handler f2fs_xattr_user_handler = {
162 .prefix = XATTR_USER_PREFIX,
163 .flags = F2FS_XATTR_INDEX_USER,
164 .list = f2fs_xattr_user_list,
165 .get = f2fs_xattr_generic_get,
166 .set = f2fs_xattr_generic_set,
169 const struct xattr_handler f2fs_xattr_trusted_handler = {
170 .prefix = XATTR_TRUSTED_PREFIX,
171 .flags = F2FS_XATTR_INDEX_TRUSTED,
172 .list = f2fs_xattr_trusted_list,
173 .get = f2fs_xattr_generic_get,
174 .set = f2fs_xattr_generic_set,
177 const struct xattr_handler f2fs_xattr_advise_handler = {
178 .name = F2FS_SYSTEM_ADVISE_NAME,
179 .flags = F2FS_XATTR_INDEX_ADVISE,
180 .get = f2fs_xattr_advise_get,
181 .set = f2fs_xattr_advise_set,
184 const struct xattr_handler f2fs_xattr_security_handler = {
185 .prefix = XATTR_SECURITY_PREFIX,
186 .flags = F2FS_XATTR_INDEX_SECURITY,
187 .get = f2fs_xattr_generic_get,
188 .set = f2fs_xattr_generic_set,
191 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
192 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
193 #ifdef CONFIG_F2FS_FS_POSIX_ACL
194 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
195 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
197 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
198 #ifdef CONFIG_F2FS_FS_SECURITY
199 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
201 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
204 const struct xattr_handler *f2fs_xattr_handlers[] = {
205 &f2fs_xattr_user_handler,
206 #ifdef CONFIG_F2FS_FS_POSIX_ACL
207 &posix_acl_access_xattr_handler,
208 &posix_acl_default_xattr_handler,
210 &f2fs_xattr_trusted_handler,
211 #ifdef CONFIG_F2FS_FS_SECURITY
212 &f2fs_xattr_security_handler,
214 &f2fs_xattr_advise_handler,
218 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
220 const struct xattr_handler *handler = NULL;
222 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
223 handler = f2fs_xattr_handler_map[index];
227 static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
228 void *last_base_addr, int index,
229 size_t len, const char *name)
231 struct f2fs_xattr_entry *entry;
233 list_for_each_xattr(entry, base_addr) {
234 if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
235 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr)
238 if (entry->e_name_index != index)
240 if (entry->e_name_len != len)
242 if (!memcmp(entry->e_name, name, len))
248 static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
249 void *base_addr, void **last_addr, int index,
250 size_t len, const char *name)
252 struct f2fs_xattr_entry *entry;
253 unsigned int inline_size = inline_xattr_size(inode);
254 void *max_addr = base_addr + inline_size;
256 list_for_each_xattr(entry, base_addr) {
257 if ((void *)entry + sizeof(__u32) > max_addr ||
258 (void *)XATTR_NEXT_ENTRY(entry) > max_addr) {
262 if (entry->e_name_index != index)
264 if (entry->e_name_len != len)
266 if (!memcmp(entry->e_name, name, len))
270 /* inline xattr header or entry across max inline xattr size */
271 if (IS_XATTR_LAST_ENTRY(entry) &&
272 (void *)entry + sizeof(__u32) > max_addr) {
279 static int read_inline_xattr(struct inode *inode, struct page *ipage,
282 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
283 unsigned int inline_size = inline_xattr_size(inode);
284 struct page *page = NULL;
288 inline_addr = inline_xattr_addr(inode, ipage);
290 page = f2fs_get_node_page(sbi, inode->i_ino);
292 return PTR_ERR(page);
294 inline_addr = inline_xattr_addr(inode, page);
296 memcpy(txattr_addr, inline_addr, inline_size);
297 f2fs_put_page(page, 1);
302 static int read_xattr_block(struct inode *inode, void *txattr_addr)
304 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
305 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
306 unsigned int inline_size = inline_xattr_size(inode);
310 /* The inode already has an extended attribute block. */
311 xpage = f2fs_get_node_page(sbi, xnid);
313 return PTR_ERR(xpage);
315 xattr_addr = page_address(xpage);
316 memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
317 f2fs_put_page(xpage, 1);
322 static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
323 unsigned int index, unsigned int len,
324 const char *name, struct f2fs_xattr_entry **xe,
325 void **base_addr, int *base_size,
328 void *cur_addr, *txattr_addr, *last_txattr_addr;
329 void *last_addr = NULL;
330 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
331 unsigned int inline_size = inline_xattr_size(inode);
334 if (!xnid && !inline_size)
337 *base_size = XATTR_SIZE(inode) + XATTR_PADDING_SIZE;
338 txattr_addr = xattr_alloc(F2FS_I_SB(inode), *base_size, is_inline);
342 last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(inode);
344 /* read from inline xattr */
346 err = read_inline_xattr(inode, ipage, txattr_addr);
350 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
353 *base_size = inline_size;
358 /* read from xattr node block */
360 err = read_xattr_block(inode, txattr_addr);
366 cur_addr = XATTR_HDR(last_addr) - 1;
368 cur_addr = txattr_addr;
370 *xe = __find_xattr(cur_addr, last_txattr_addr, index, len, name);
372 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
374 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
379 if (IS_XATTR_LAST_ENTRY(*xe)) {
384 *base_addr = txattr_addr;
387 xattr_free(F2FS_I_SB(inode), txattr_addr, *is_inline);
391 static int read_all_xattrs(struct inode *inode, struct page *ipage,
394 struct f2fs_xattr_header *header;
395 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
396 unsigned int size = VALID_XATTR_BLOCK_SIZE;
397 unsigned int inline_size = inline_xattr_size(inode);
401 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
402 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
406 /* read from inline xattr */
408 err = read_inline_xattr(inode, ipage, txattr_addr);
413 /* read from xattr node block */
415 err = read_xattr_block(inode, txattr_addr);
420 header = XATTR_HDR(txattr_addr);
422 /* never been allocated xattrs */
423 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
424 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
425 header->h_refcount = cpu_to_le32(1);
427 *base_addr = txattr_addr;
434 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
435 void *txattr_addr, struct page *ipage)
437 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
438 size_t inline_size = inline_xattr_size(inode);
439 struct page *in_page = NULL;
441 void *inline_addr = NULL;
446 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
447 if (!f2fs_alloc_nid(sbi, &new_nid))
450 /* write to inline xattr */
453 inline_addr = inline_xattr_addr(inode, ipage);
455 in_page = f2fs_get_node_page(sbi, inode->i_ino);
456 if (IS_ERR(in_page)) {
457 f2fs_alloc_nid_failed(sbi, new_nid);
458 return PTR_ERR(in_page);
460 inline_addr = inline_xattr_addr(inode, in_page);
463 f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
465 /* no need to use xattr node block */
466 if (hsize <= inline_size) {
467 err = f2fs_truncate_xattr_node(inode);
468 f2fs_alloc_nid_failed(sbi, new_nid);
470 f2fs_put_page(in_page, 1);
473 memcpy(inline_addr, txattr_addr, inline_size);
474 set_page_dirty(ipage ? ipage : in_page);
479 /* write to xattr node block */
480 if (F2FS_I(inode)->i_xattr_nid) {
481 xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
483 err = PTR_ERR(xpage);
484 f2fs_alloc_nid_failed(sbi, new_nid);
487 f2fs_bug_on(sbi, new_nid);
488 f2fs_wait_on_page_writeback(xpage, NODE, true, true);
490 struct dnode_of_data dn;
492 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
493 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
495 err = PTR_ERR(xpage);
496 f2fs_alloc_nid_failed(sbi, new_nid);
499 f2fs_alloc_nid_done(sbi, new_nid);
501 xattr_addr = page_address(xpage);
504 memcpy(inline_addr, txattr_addr, inline_size);
505 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
508 set_page_dirty(ipage ? ipage : in_page);
509 set_page_dirty(xpage);
511 f2fs_put_page(xpage, 1);
513 f2fs_put_page(in_page, 1);
517 int f2fs_getxattr(struct inode *inode, int index, const char *name,
518 void *buffer, size_t buffer_size, struct page *ipage)
520 struct f2fs_xattr_entry *entry = NULL;
522 unsigned int size, len;
523 void *base_addr = NULL;
531 if (len > F2FS_NAME_LEN)
534 down_read(&F2FS_I(inode)->i_xattr_sem);
535 error = lookup_all_xattrs(inode, ipage, index, len, name,
536 &entry, &base_addr, &base_size, &is_inline);
537 up_read(&F2FS_I(inode)->i_xattr_sem);
541 size = le16_to_cpu(entry->e_value_size);
543 if (buffer && size > buffer_size) {
549 char *pval = entry->e_name + entry->e_name_len;
551 if (base_size - (pval - (char *)base_addr) < size) {
555 memcpy(buffer, pval, size);
559 xattr_free(F2FS_I_SB(inode), base_addr, is_inline);
563 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
565 struct inode *inode = d_inode(dentry);
566 struct f2fs_xattr_entry *entry;
567 void *base_addr, *last_base_addr;
569 size_t rest = buffer_size;
571 down_read(&F2FS_I(inode)->i_xattr_sem);
572 error = read_all_xattrs(inode, NULL, &base_addr);
573 up_read(&F2FS_I(inode)->i_xattr_sem);
577 last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
579 list_for_each_xattr(entry, base_addr) {
580 const struct xattr_handler *handler =
581 f2fs_xattr_handler(entry->e_name_index);
586 if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
587 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
588 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
590 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
591 error = -EFSCORRUPTED;
595 if (!handler || (handler->list && !handler->list(dentry)))
598 prefix = xattr_prefix(handler);
599 prefix_len = strlen(prefix);
600 size = prefix_len + entry->e_name_len + 1;
606 memcpy(buffer, prefix, prefix_len);
607 buffer += prefix_len;
608 memcpy(buffer, entry->e_name, entry->e_name_len);
609 buffer += entry->e_name_len;
614 error = buffer_size - rest;
620 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
621 const void *value, size_t size)
623 void *pval = entry->e_name + entry->e_name_len;
625 return (le16_to_cpu(entry->e_value_size) == size) &&
626 !memcmp(pval, value, size);
629 static int __f2fs_setxattr(struct inode *inode, int index,
630 const char *name, const void *value, size_t size,
631 struct page *ipage, int flags)
633 struct f2fs_xattr_entry *here, *last;
634 void *base_addr, *last_base_addr;
648 if (len > F2FS_NAME_LEN)
651 if (size > MAX_VALUE_LEN(inode))
654 error = read_all_xattrs(inode, ipage, &base_addr);
658 last_base_addr = (void *)base_addr + XATTR_SIZE(inode);
660 /* find entry with wanted name. */
661 here = __find_xattr(base_addr, last_base_addr, index, len, name);
663 f2fs_err(F2FS_I_SB(inode), "inode (%lu) has corrupted xattr",
665 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
666 error = -EFSCORRUPTED;
670 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
673 if ((flags & XATTR_CREATE)) {
678 if (value && f2fs_xattr_value_same(here, value, size))
680 } else if ((flags & XATTR_REPLACE)) {
686 while (!IS_XATTR_LAST_ENTRY(last))
687 last = XATTR_NEXT_ENTRY(last);
689 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
695 * If value is NULL, it is remove operation.
696 * In case of update operation, we calculate free.
698 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
700 free = free + ENTRY_SIZE(here);
702 if (unlikely(free < newsize)) {
708 /* 2. Remove old entry */
711 * If entry is found, remove old entry.
712 * If not found, remove operation is not needed.
714 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
715 int oldsize = ENTRY_SIZE(here);
717 memmove(here, next, (char *)last - (char *)next);
718 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
719 memset(last, 0, oldsize);
722 new_hsize = (char *)last - (char *)base_addr;
724 /* 3. Write new entry */
728 * Before we come here, old entry is removed.
729 * We just write new entry.
731 last->e_name_index = index;
732 last->e_name_len = len;
733 memcpy(last->e_name, name, len);
734 pval = last->e_name + len;
735 memcpy(pval, value, size);
736 last->e_value_size = cpu_to_le16(size);
737 new_hsize += newsize;
740 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
744 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
745 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
746 f2fs_set_encrypted_inode(inode);
747 f2fs_mark_inode_dirty_sync(inode, true);
748 if (!error && S_ISDIR(inode->i_mode))
749 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
752 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
753 inode->i_mode = F2FS_I(inode)->i_acl_mode;
754 inode->i_ctime = current_time(inode);
755 clear_inode_flag(inode, FI_ACL_MODE);
763 int f2fs_setxattr(struct inode *inode, int index, const char *name,
764 const void *value, size_t size,
765 struct page *ipage, int flags)
767 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
770 if (unlikely(f2fs_cp_error(sbi)))
772 if (!f2fs_is_checkpoint_ready(sbi))
775 err = dquot_initialize(inode);
779 /* this case is only from f2fs_init_inode_metadata */
781 return __f2fs_setxattr(inode, index, name, value,
783 f2fs_balance_fs(sbi, true);
786 down_write(&F2FS_I(inode)->i_xattr_sem);
787 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
788 up_write(&F2FS_I(inode)->i_xattr_sem);
791 f2fs_update_time(sbi, REQ_TIME);
795 int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi)
797 dev_t dev = sbi->sb->s_bdev->bd_dev;
800 sprintf(slab_name, "f2fs_xattr_entry-%u:%u", MAJOR(dev), MINOR(dev));
802 sbi->inline_xattr_slab_size = F2FS_OPTION(sbi).inline_xattr_size *
803 sizeof(__le32) + XATTR_PADDING_SIZE;
805 sbi->inline_xattr_slab = f2fs_kmem_cache_create(slab_name,
806 sbi->inline_xattr_slab_size);
807 if (!sbi->inline_xattr_slab)
813 void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi)
815 kmem_cache_destroy(sbi->inline_xattr_slab);