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
13 * Extended attributes for symlinks and special files added per
18 #include <linux/rwsem.h>
19 #include <linux/f2fs_fs.h>
20 #include <linux/security.h>
21 #include <linux/posix_acl_xattr.h>
25 static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
26 struct dentry *unused, struct inode *inode,
27 const char *name, void *buffer, size_t size)
29 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
31 switch (handler->flags) {
32 case F2FS_XATTR_INDEX_USER:
33 if (!test_opt(sbi, XATTR_USER))
36 case F2FS_XATTR_INDEX_TRUSTED:
37 case F2FS_XATTR_INDEX_SECURITY:
42 return f2fs_getxattr(inode, handler->flags, name,
46 static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
47 struct dentry *unused, struct inode *inode,
48 const char *name, const void *value,
49 size_t size, int flags)
51 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
53 switch (handler->flags) {
54 case F2FS_XATTR_INDEX_USER:
55 if (!test_opt(sbi, XATTR_USER))
58 case F2FS_XATTR_INDEX_TRUSTED:
59 case F2FS_XATTR_INDEX_SECURITY:
64 return f2fs_setxattr(inode, handler->flags, name,
65 value, size, NULL, flags);
68 static bool f2fs_xattr_user_list(struct dentry *dentry)
70 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
72 return test_opt(sbi, XATTR_USER);
75 static bool f2fs_xattr_trusted_list(struct dentry *dentry)
77 return capable(CAP_SYS_ADMIN);
80 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
81 struct dentry *unused, struct inode *inode,
82 const char *name, void *buffer, size_t size)
85 *((char *)buffer) = F2FS_I(inode)->i_advise;
89 static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
90 struct dentry *unused, struct inode *inode,
91 const char *name, const void *value,
92 size_t size, int flags)
94 unsigned char old_advise = F2FS_I(inode)->i_advise;
95 unsigned char new_advise;
97 if (!inode_owner_or_capable(inode))
102 new_advise = *(char *)value;
103 if (new_advise & ~FADVISE_MODIFIABLE_BITS)
106 new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
107 new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
109 F2FS_I(inode)->i_advise = new_advise;
110 f2fs_mark_inode_dirty_sync(inode, true);
114 #ifdef CONFIG_F2FS_FS_SECURITY
115 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
118 const struct xattr *xattr;
121 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
122 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
123 xattr->name, xattr->value,
124 xattr->value_len, (struct page *)page, 0);
131 int f2fs_init_security(struct inode *inode, struct inode *dir,
132 const struct qstr *qstr, struct page *ipage)
134 return security_inode_init_security(inode, dir, qstr,
135 &f2fs_initxattrs, ipage);
139 const struct xattr_handler f2fs_xattr_user_handler = {
140 .prefix = XATTR_USER_PREFIX,
141 .flags = F2FS_XATTR_INDEX_USER,
142 .list = f2fs_xattr_user_list,
143 .get = f2fs_xattr_generic_get,
144 .set = f2fs_xattr_generic_set,
147 const struct xattr_handler f2fs_xattr_trusted_handler = {
148 .prefix = XATTR_TRUSTED_PREFIX,
149 .flags = F2FS_XATTR_INDEX_TRUSTED,
150 .list = f2fs_xattr_trusted_list,
151 .get = f2fs_xattr_generic_get,
152 .set = f2fs_xattr_generic_set,
155 const struct xattr_handler f2fs_xattr_advise_handler = {
156 .name = F2FS_SYSTEM_ADVISE_NAME,
157 .flags = F2FS_XATTR_INDEX_ADVISE,
158 .get = f2fs_xattr_advise_get,
159 .set = f2fs_xattr_advise_set,
162 const struct xattr_handler f2fs_xattr_security_handler = {
163 .prefix = XATTR_SECURITY_PREFIX,
164 .flags = F2FS_XATTR_INDEX_SECURITY,
165 .get = f2fs_xattr_generic_get,
166 .set = f2fs_xattr_generic_set,
169 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
170 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
171 #ifdef CONFIG_F2FS_FS_POSIX_ACL
172 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
173 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
175 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
176 #ifdef CONFIG_F2FS_FS_SECURITY
177 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
179 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
182 const struct xattr_handler *f2fs_xattr_handlers[] = {
183 &f2fs_xattr_user_handler,
184 #ifdef CONFIG_F2FS_FS_POSIX_ACL
185 &posix_acl_access_xattr_handler,
186 &posix_acl_default_xattr_handler,
188 &f2fs_xattr_trusted_handler,
189 #ifdef CONFIG_F2FS_FS_SECURITY
190 &f2fs_xattr_security_handler,
192 &f2fs_xattr_advise_handler,
196 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
198 const struct xattr_handler *handler = NULL;
200 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
201 handler = f2fs_xattr_handler_map[index];
205 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
206 size_t len, const char *name)
208 struct f2fs_xattr_entry *entry;
210 list_for_each_xattr(entry, base_addr) {
211 if (entry->e_name_index != index)
213 if (entry->e_name_len != len)
215 if (!memcmp(entry->e_name, name, len))
221 static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
222 void *base_addr, void **last_addr, int index,
223 size_t len, const char *name)
225 struct f2fs_xattr_entry *entry;
226 unsigned int inline_size = inline_xattr_size(inode);
228 list_for_each_xattr(entry, base_addr) {
229 if ((void *)entry + sizeof(__u32) > base_addr + inline_size ||
230 (void *)XATTR_NEXT_ENTRY(entry) + sizeof(__u32) >
231 base_addr + inline_size) {
235 if (entry->e_name_index != index)
237 if (entry->e_name_len != len)
239 if (!memcmp(entry->e_name, name, len))
245 static int read_inline_xattr(struct inode *inode, struct page *ipage,
248 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
249 unsigned int inline_size = inline_xattr_size(inode);
250 struct page *page = NULL;
254 inline_addr = inline_xattr_addr(inode, ipage);
256 page = f2fs_get_node_page(sbi, inode->i_ino);
258 return PTR_ERR(page);
260 inline_addr = inline_xattr_addr(inode, page);
262 memcpy(txattr_addr, inline_addr, inline_size);
263 f2fs_put_page(page, 1);
268 static int read_xattr_block(struct inode *inode, void *txattr_addr)
270 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
271 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
272 unsigned int inline_size = inline_xattr_size(inode);
276 /* The inode already has an extended attribute block. */
277 xpage = f2fs_get_node_page(sbi, xnid);
279 return PTR_ERR(xpage);
281 xattr_addr = page_address(xpage);
282 memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
283 f2fs_put_page(xpage, 1);
288 static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
289 unsigned int index, unsigned int len,
290 const char *name, struct f2fs_xattr_entry **xe,
291 void **base_addr, int *base_size)
293 void *cur_addr, *txattr_addr, *last_addr = NULL;
294 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
295 unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
296 unsigned int inline_size = inline_xattr_size(inode);
299 if (!size && !inline_size)
302 *base_size = inline_size + size + XATTR_PADDING_SIZE;
303 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode), *base_size, GFP_NOFS);
307 /* read from inline xattr */
309 err = read_inline_xattr(inode, ipage, txattr_addr);
313 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
316 *base_size = inline_size;
321 /* read from xattr node block */
323 err = read_xattr_block(inode, txattr_addr);
329 cur_addr = XATTR_HDR(last_addr) - 1;
331 cur_addr = txattr_addr;
333 *xe = __find_xattr(cur_addr, index, len, name);
335 if (IS_XATTR_LAST_ENTRY(*xe)) {
340 *base_addr = txattr_addr;
347 static int read_all_xattrs(struct inode *inode, struct page *ipage,
350 struct f2fs_xattr_header *header;
351 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
352 unsigned int size = VALID_XATTR_BLOCK_SIZE;
353 unsigned int inline_size = inline_xattr_size(inode);
357 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
358 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
362 /* read from inline xattr */
364 err = read_inline_xattr(inode, ipage, txattr_addr);
369 /* read from xattr node block */
371 err = read_xattr_block(inode, txattr_addr);
376 header = XATTR_HDR(txattr_addr);
378 /* never been allocated xattrs */
379 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
380 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
381 header->h_refcount = cpu_to_le32(1);
383 *base_addr = txattr_addr;
390 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
391 void *txattr_addr, struct page *ipage)
393 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
394 size_t inline_size = inline_xattr_size(inode);
395 struct page *in_page = NULL;
397 void *inline_addr = NULL;
402 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
403 if (!f2fs_alloc_nid(sbi, &new_nid))
406 /* write to inline xattr */
409 inline_addr = inline_xattr_addr(inode, ipage);
411 in_page = f2fs_get_node_page(sbi, inode->i_ino);
412 if (IS_ERR(in_page)) {
413 f2fs_alloc_nid_failed(sbi, new_nid);
414 return PTR_ERR(in_page);
416 inline_addr = inline_xattr_addr(inode, in_page);
419 f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
421 /* no need to use xattr node block */
422 if (hsize <= inline_size) {
423 err = f2fs_truncate_xattr_node(inode);
424 f2fs_alloc_nid_failed(sbi, new_nid);
426 f2fs_put_page(in_page, 1);
429 memcpy(inline_addr, txattr_addr, inline_size);
430 set_page_dirty(ipage ? ipage : in_page);
435 /* write to xattr node block */
436 if (F2FS_I(inode)->i_xattr_nid) {
437 xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
439 err = PTR_ERR(xpage);
440 f2fs_alloc_nid_failed(sbi, new_nid);
443 f2fs_bug_on(sbi, new_nid);
444 f2fs_wait_on_page_writeback(xpage, NODE, true, true);
446 struct dnode_of_data dn;
447 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
448 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
450 err = PTR_ERR(xpage);
451 f2fs_alloc_nid_failed(sbi, new_nid);
454 f2fs_alloc_nid_done(sbi, new_nid);
456 xattr_addr = page_address(xpage);
459 memcpy(inline_addr, txattr_addr, inline_size);
460 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
463 set_page_dirty(ipage ? ipage : in_page);
464 set_page_dirty(xpage);
466 f2fs_put_page(xpage, 1);
468 f2fs_put_page(in_page, 1);
472 int f2fs_getxattr(struct inode *inode, int index, const char *name,
473 void *buffer, size_t buffer_size, struct page *ipage)
475 struct f2fs_xattr_entry *entry = NULL;
477 unsigned int size, len;
478 void *base_addr = NULL;
485 if (len > F2FS_NAME_LEN)
488 down_read(&F2FS_I(inode)->i_xattr_sem);
489 error = lookup_all_xattrs(inode, ipage, index, len, name,
490 &entry, &base_addr, &base_size);
491 up_read(&F2FS_I(inode)->i_xattr_sem);
495 size = le16_to_cpu(entry->e_value_size);
497 if (buffer && size > buffer_size) {
503 char *pval = entry->e_name + entry->e_name_len;
505 if (base_size - (pval - (char *)base_addr) < size) {
509 memcpy(buffer, pval, size);
517 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
519 struct inode *inode = d_inode(dentry);
520 struct f2fs_xattr_entry *entry;
523 size_t rest = buffer_size;
525 down_read(&F2FS_I(inode)->i_xattr_sem);
526 error = read_all_xattrs(inode, NULL, &base_addr);
527 up_read(&F2FS_I(inode)->i_xattr_sem);
531 list_for_each_xattr(entry, base_addr) {
532 const struct xattr_handler *handler =
533 f2fs_xattr_handler(entry->e_name_index);
538 if (!handler || (handler->list && !handler->list(dentry)))
541 prefix = handler->prefix ?: handler->name;
542 prefix_len = strlen(prefix);
543 size = prefix_len + entry->e_name_len + 1;
549 memcpy(buffer, prefix, prefix_len);
550 buffer += prefix_len;
551 memcpy(buffer, entry->e_name, entry->e_name_len);
552 buffer += entry->e_name_len;
557 error = buffer_size - rest;
563 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
564 const void *value, size_t size)
566 void *pval = entry->e_name + entry->e_name_len;
568 return (le16_to_cpu(entry->e_value_size) == size) &&
569 !memcmp(pval, value, size);
572 static int __f2fs_setxattr(struct inode *inode, int index,
573 const char *name, const void *value, size_t size,
574 struct page *ipage, int flags)
576 struct f2fs_xattr_entry *here, *last;
591 if (len > F2FS_NAME_LEN)
594 if (size > MAX_VALUE_LEN(inode))
597 error = read_all_xattrs(inode, ipage, &base_addr);
601 /* find entry with wanted name. */
602 here = __find_xattr(base_addr, index, len, name);
604 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
607 if ((flags & XATTR_CREATE)) {
612 if (value && f2fs_xattr_value_same(here, value, size))
614 } else if ((flags & XATTR_REPLACE)) {
620 while (!IS_XATTR_LAST_ENTRY(last))
621 last = XATTR_NEXT_ENTRY(last);
623 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
629 * If value is NULL, it is remove operation.
630 * In case of update operation, we calculate free.
632 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
634 free = free + ENTRY_SIZE(here);
636 if (unlikely(free < newsize)) {
642 /* 2. Remove old entry */
645 * If entry is found, remove old entry.
646 * If not found, remove operation is not needed.
648 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
649 int oldsize = ENTRY_SIZE(here);
651 memmove(here, next, (char *)last - (char *)next);
652 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
653 memset(last, 0, oldsize);
656 new_hsize = (char *)last - (char *)base_addr;
658 /* 3. Write new entry */
662 * Before we come here, old entry is removed.
663 * We just write new entry.
665 last->e_name_index = index;
666 last->e_name_len = len;
667 memcpy(last->e_name, name, len);
668 pval = last->e_name + len;
669 memcpy(pval, value, size);
670 last->e_value_size = cpu_to_le16(size);
671 new_hsize += newsize;
674 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
678 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
679 inode->i_mode = F2FS_I(inode)->i_acl_mode;
680 inode->i_ctime = current_time(inode);
681 clear_inode_flag(inode, FI_ACL_MODE);
683 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
684 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
685 f2fs_set_encrypted_inode(inode);
686 f2fs_mark_inode_dirty_sync(inode, true);
687 if (!error && S_ISDIR(inode->i_mode))
688 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
694 int f2fs_setxattr(struct inode *inode, int index, const char *name,
695 const void *value, size_t size,
696 struct page *ipage, int flags)
698 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
701 err = dquot_initialize(inode);
705 /* this case is only from f2fs_init_inode_metadata */
707 return __f2fs_setxattr(inode, index, name, value,
709 f2fs_balance_fs(sbi, true);
712 /* protect xattr_ver */
713 down_write(&F2FS_I(inode)->i_sem);
714 down_write(&F2FS_I(inode)->i_xattr_sem);
715 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
716 up_write(&F2FS_I(inode)->i_xattr_sem);
717 up_write(&F2FS_I(inode)->i_sem);
720 f2fs_update_time(sbi, REQ_TIME);