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>,
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>
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))
39 case F2FS_XATTR_INDEX_TRUSTED:
40 if (!capable(CAP_SYS_ADMIN))
43 case F2FS_XATTR_INDEX_SECURITY:
48 return f2fs_getxattr(inode, handler->flags, name,
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))
64 case F2FS_XATTR_INDEX_TRUSTED:
65 if (!capable(CAP_SYS_ADMIN))
68 case F2FS_XATTR_INDEX_SECURITY:
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)
94 *((char *)buffer) = F2FS_I(inode)->i_advise;
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))
108 F2FS_I(inode)->i_advise |= *(char *)value;
109 f2fs_mark_inode_dirty_sync(inode, true);
113 #ifdef CONFIG_F2FS_FS_SECURITY
114 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
117 const struct xattr *xattr;
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);
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);
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,
174 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
175 #ifdef CONFIG_F2FS_FS_SECURITY
176 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
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,
187 &f2fs_xattr_trusted_handler,
188 #ifdef CONFIG_F2FS_FS_SECURITY
189 &f2fs_xattr_security_handler,
191 &f2fs_xattr_advise_handler,
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];
204 static struct f2fs_xattr_entry *__find_xattr(void *base_addr,
205 void *last_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 ((void *)(entry) + sizeof(__u32) > last_base_addr ||
212 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr)
215 if (entry->e_name_index != index)
217 if (entry->e_name_len != len)
219 if (!memcmp(entry->e_name, name, len))
225 static struct f2fs_xattr_entry *__find_inline_xattr(void *base_addr,
226 void **last_addr, int index,
227 size_t len, const char *name)
229 struct f2fs_xattr_entry *entry;
230 unsigned int inline_size = F2FS_INLINE_XATTR_ADDRS << 2;
232 list_for_each_xattr(entry, base_addr) {
233 if ((void *)entry + sizeof(__u32) > base_addr + inline_size ||
234 (void *)XATTR_NEXT_ENTRY(entry) + sizeof(__u32) >
235 base_addr + inline_size) {
239 if (entry->e_name_index != index)
241 if (entry->e_name_len != len)
243 if (!memcmp(entry->e_name, name, len))
249 static int read_inline_xattr(struct inode *inode, struct page *ipage,
252 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
253 unsigned int inline_size = inline_xattr_size(inode);
254 struct page *page = NULL;
258 inline_addr = inline_xattr_addr(ipage);
260 page = get_node_page(sbi, inode->i_ino);
262 return PTR_ERR(page);
264 inline_addr = inline_xattr_addr(page);
266 memcpy(txattr_addr, inline_addr, inline_size);
267 f2fs_put_page(page, 1);
272 static int read_xattr_block(struct inode *inode, void *txattr_addr)
274 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
275 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
276 unsigned int inline_size = inline_xattr_size(inode);
280 /* The inode already has an extended attribute block. */
281 xpage = get_node_page(sbi, xnid);
283 return PTR_ERR(xpage);
285 xattr_addr = page_address(xpage);
286 memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
287 f2fs_put_page(xpage, 1);
292 static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
293 unsigned int index, unsigned int len,
294 const char *name, struct f2fs_xattr_entry **xe,
295 void **base_addr, int *base_size)
297 void *cur_addr, *txattr_addr, *last_txattr_addr;
298 void *last_addr = NULL;
299 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
300 unsigned int inline_size = inline_xattr_size(inode);
303 if (!xnid && !inline_size)
306 *base_size = XATTR_SIZE(xnid, inode) + XATTR_PADDING_SIZE;
307 txattr_addr = kzalloc(*base_size, GFP_F2FS_ZERO);
311 last_txattr_addr = (void *)txattr_addr + XATTR_SIZE(xnid, inode);
313 /* read from inline xattr */
315 err = read_inline_xattr(inode, ipage, txattr_addr);
319 *xe = __find_inline_xattr(txattr_addr, &last_addr,
322 *base_size = inline_size;
327 /* read from xattr node block */
329 err = read_xattr_block(inode, txattr_addr);
335 cur_addr = XATTR_HDR(last_addr) - 1;
337 cur_addr = txattr_addr;
339 *xe = __find_xattr(cur_addr, last_txattr_addr, index, len, name);
345 if (IS_XATTR_LAST_ENTRY(*xe)) {
350 *base_addr = txattr_addr;
357 static int read_all_xattrs(struct inode *inode, struct page *ipage,
360 struct f2fs_xattr_header *header;
361 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
362 unsigned int size = VALID_XATTR_BLOCK_SIZE;
363 unsigned int inline_size = inline_xattr_size(inode);
367 txattr_addr = kzalloc(inline_size + size + XATTR_PADDING_SIZE,
372 /* read from inline xattr */
374 err = read_inline_xattr(inode, ipage, txattr_addr);
379 /* read from xattr node block */
381 err = read_xattr_block(inode, txattr_addr);
386 header = XATTR_HDR(txattr_addr);
388 /* never been allocated xattrs */
389 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
390 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
391 header->h_refcount = cpu_to_le32(1);
393 *base_addr = txattr_addr;
400 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
401 void *txattr_addr, struct page *ipage)
403 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
404 size_t inline_size = inline_xattr_size(inode);
410 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
411 if (!alloc_nid(sbi, &new_nid))
414 /* write to inline xattr */
416 struct page *page = NULL;
420 inline_addr = inline_xattr_addr(ipage);
421 f2fs_wait_on_page_writeback(ipage, NODE, true);
422 set_page_dirty(ipage);
424 page = get_node_page(sbi, inode->i_ino);
426 alloc_nid_failed(sbi, new_nid);
427 return PTR_ERR(page);
429 inline_addr = inline_xattr_addr(page);
430 f2fs_wait_on_page_writeback(page, NODE, true);
432 memcpy(inline_addr, txattr_addr, inline_size);
433 f2fs_put_page(page, 1);
435 /* no need to use xattr node block */
436 if (hsize <= inline_size) {
437 err = truncate_xattr_node(inode, ipage);
438 alloc_nid_failed(sbi, new_nid);
443 /* write to xattr node block */
444 if (F2FS_I(inode)->i_xattr_nid) {
445 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
447 alloc_nid_failed(sbi, new_nid);
448 return PTR_ERR(xpage);
450 f2fs_bug_on(sbi, new_nid);
451 f2fs_wait_on_page_writeback(xpage, NODE, true);
453 struct dnode_of_data dn;
454 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
455 xpage = new_node_page(&dn, XATTR_NODE_OFFSET);
457 alloc_nid_failed(sbi, new_nid);
458 return PTR_ERR(xpage);
460 alloc_nid_done(sbi, new_nid);
463 xattr_addr = page_address(xpage);
464 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
465 set_page_dirty(xpage);
466 f2fs_put_page(xpage, 1);
471 int f2fs_getxattr(struct inode *inode, int index, const char *name,
472 void *buffer, size_t buffer_size, struct page *ipage)
474 struct f2fs_xattr_entry *entry = NULL;
476 unsigned int size, len;
477 void *base_addr = NULL;
484 if (len > F2FS_NAME_LEN)
487 down_read(&F2FS_I(inode)->i_xattr_sem);
488 error = lookup_all_xattrs(inode, ipage, index, len, name,
489 &entry, &base_addr, &base_size);
490 up_read(&F2FS_I(inode)->i_xattr_sem);
494 size = le16_to_cpu(entry->e_value_size);
496 if (buffer && size > buffer_size) {
502 char *pval = entry->e_name + entry->e_name_len;
504 if (base_size - (pval - (char *)base_addr) < size) {
508 memcpy(buffer, pval, size);
516 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
518 struct inode *inode = d_inode(dentry);
519 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
520 struct f2fs_xattr_entry *entry;
521 void *base_addr, *last_base_addr;
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 last_base_addr = (void *)base_addr + XATTR_SIZE(xnid, inode);
533 list_for_each_xattr(entry, base_addr) {
534 const struct xattr_handler *handler =
535 f2fs_xattr_handler(entry->e_name_index);
540 if ((void *)(entry) + sizeof(__u32) > last_base_addr ||
541 (void *)XATTR_NEXT_ENTRY(entry) > last_base_addr) {
542 f2fs_msg(dentry->d_sb, KERN_ERR,
543 "inode (%lu) has corrupted xattr",
545 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
546 error = -EFSCORRUPTED;
550 if (!handler || (handler->list && !handler->list(dentry)))
553 prefix = handler->prefix ?: handler->name;
554 prefix_len = strlen(prefix);
555 size = prefix_len + entry->e_name_len + 1;
561 memcpy(buffer, prefix, prefix_len);
562 buffer += prefix_len;
563 memcpy(buffer, entry->e_name, entry->e_name_len);
564 buffer += entry->e_name_len;
569 error = buffer_size - rest;
575 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
576 const void *value, size_t size)
578 void *pval = entry->e_name + entry->e_name_len;
580 return (le16_to_cpu(entry->e_value_size) == size) &&
581 !memcmp(pval, value, size);
584 static int __f2fs_setxattr(struct inode *inode, int index,
585 const char *name, const void *value, size_t size,
586 struct page *ipage, int flags)
588 struct f2fs_xattr_entry *here, *last;
589 void *base_addr, *last_base_addr;
590 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
604 if (len > F2FS_NAME_LEN)
607 if (size > MAX_VALUE_LEN(inode))
610 error = read_all_xattrs(inode, ipage, &base_addr);
614 last_base_addr = (void *)base_addr + XATTR_SIZE(xnid, inode);
616 /* find entry with wanted name. */
617 here = __find_xattr(base_addr, last_base_addr, index, len, name);
623 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
626 if ((flags & XATTR_CREATE)) {
631 if (f2fs_xattr_value_same(here, value, size))
633 } else if ((flags & XATTR_REPLACE)) {
639 while (!IS_XATTR_LAST_ENTRY(last)) {
640 if ((void *)(last) + sizeof(__u32) > last_base_addr ||
641 (void *)XATTR_NEXT_ENTRY(last) > last_base_addr) {
642 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_FSCK);
643 error = -EFSCORRUPTED;
646 last = XATTR_NEXT_ENTRY(last);
649 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
655 * If value is NULL, it is remove operation.
656 * In case of update operation, we calculate free.
658 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
660 free = free + ENTRY_SIZE(here);
662 if (unlikely(free < newsize)) {
668 /* 2. Remove old entry */
671 * If entry is found, remove old entry.
672 * If not found, remove operation is not needed.
674 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
675 int oldsize = ENTRY_SIZE(here);
677 memmove(here, next, (char *)last - (char *)next);
678 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
679 memset(last, 0, oldsize);
682 new_hsize = (char *)last - (char *)base_addr;
684 /* 3. Write new entry */
688 * Before we come here, old entry is removed.
689 * We just write new entry.
691 last->e_name_index = index;
692 last->e_name_len = len;
693 memcpy(last->e_name, name, len);
694 pval = last->e_name + len;
695 memcpy(pval, value, size);
696 last->e_value_size = cpu_to_le16(size);
697 new_hsize += newsize;
700 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
704 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
705 inode->i_mode = F2FS_I(inode)->i_acl_mode;
706 inode->i_ctime = current_time(inode);
707 clear_inode_flag(inode, FI_ACL_MODE);
709 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
710 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
711 f2fs_set_encrypted_inode(inode);
712 f2fs_mark_inode_dirty_sync(inode, true);
713 if (!error && S_ISDIR(inode->i_mode))
714 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
720 int f2fs_setxattr(struct inode *inode, int index, const char *name,
721 const void *value, size_t size,
722 struct page *ipage, int flags)
724 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
727 /* this case is only from init_inode_metadata */
729 return __f2fs_setxattr(inode, index, name, value,
731 f2fs_balance_fs(sbi, true);
734 /* protect xattr_ver */
735 down_write(&F2FS_I(inode)->i_sem);
736 down_write(&F2FS_I(inode)->i_xattr_sem);
737 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
738 up_write(&F2FS_I(inode)->i_xattr_sem);
739 up_write(&F2FS_I(inode)->i_sem);
742 f2fs_update_time(sbi, REQ_TIME);