4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * Portions of this code from linux/fs/ext2/xattr.c
12 * Extended attributes for symlinks and special files added per
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, 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)
212 if (entry->e_name_len != len)
214 if (!memcmp(entry->e_name, name, len))
220 static struct f2fs_xattr_entry *__find_inline_xattr(void *base_addr,
221 void **last_addr, int index,
222 size_t len, const char *name)
224 struct f2fs_xattr_entry *entry;
225 unsigned int inline_size = F2FS_INLINE_XATTR_ADDRS << 2;
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) {
234 if (entry->e_name_index != index)
236 if (entry->e_name_len != len)
238 if (!memcmp(entry->e_name, name, len))
244 static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
245 unsigned int index, unsigned int len,
246 const char *name, struct f2fs_xattr_entry **xe,
249 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
250 void *cur_addr, *txattr_addr, *last_addr = NULL;
251 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
252 unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
253 unsigned int inline_size = 0;
256 inline_size = inline_xattr_size(inode);
258 if (!size && !inline_size)
261 txattr_addr = kzalloc(inline_size + size + sizeof(__u32),
266 /* read from inline xattr */
268 struct page *page = NULL;
272 inline_addr = inline_xattr_addr(ipage);
274 page = get_node_page(sbi, inode->i_ino);
279 inline_addr = inline_xattr_addr(page);
281 memcpy(txattr_addr, inline_addr, inline_size);
282 f2fs_put_page(page, 1);
284 *xe = __find_inline_xattr(txattr_addr, &last_addr,
290 /* read from xattr node block */
295 /* The inode already has an extended attribute block. */
296 xpage = get_node_page(sbi, xnid);
298 err = PTR_ERR(xpage);
302 xattr_addr = page_address(xpage);
303 memcpy(txattr_addr + inline_size, xattr_addr, size);
304 f2fs_put_page(xpage, 1);
308 cur_addr = XATTR_HDR(last_addr) - 1;
310 cur_addr = txattr_addr;
312 *xe = __find_xattr(cur_addr, index, len, name);
314 if (IS_XATTR_LAST_ENTRY(*xe)) {
319 *base_addr = txattr_addr;
326 static int read_all_xattrs(struct inode *inode, struct page *ipage,
329 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
330 struct f2fs_xattr_header *header;
331 size_t size = PAGE_SIZE, inline_size = 0;
335 inline_size = inline_xattr_size(inode);
337 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
341 /* read from inline xattr */
343 struct page *page = NULL;
347 inline_addr = inline_xattr_addr(ipage);
349 page = get_node_page(sbi, inode->i_ino);
354 inline_addr = inline_xattr_addr(page);
356 memcpy(txattr_addr, inline_addr, inline_size);
357 f2fs_put_page(page, 1);
360 /* read from xattr node block */
361 if (F2FS_I(inode)->i_xattr_nid) {
365 /* The inode already has an extended attribute block. */
366 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
368 err = PTR_ERR(xpage);
372 xattr_addr = page_address(xpage);
373 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
374 f2fs_put_page(xpage, 1);
377 header = XATTR_HDR(txattr_addr);
379 /* never been allocated xattrs */
380 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
381 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
382 header->h_refcount = cpu_to_le32(1);
384 *base_addr = txattr_addr;
391 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
392 void *txattr_addr, struct page *ipage)
394 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
395 size_t inline_size = 0;
401 inline_size = inline_xattr_size(inode);
403 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
404 if (!alloc_nid(sbi, &new_nid))
407 /* write to inline xattr */
409 struct page *page = NULL;
413 inline_addr = inline_xattr_addr(ipage);
414 f2fs_wait_on_page_writeback(ipage, NODE, true);
415 set_page_dirty(ipage);
417 page = get_node_page(sbi, inode->i_ino);
419 alloc_nid_failed(sbi, new_nid);
420 return PTR_ERR(page);
422 inline_addr = inline_xattr_addr(page);
423 f2fs_wait_on_page_writeback(page, NODE, true);
425 memcpy(inline_addr, txattr_addr, inline_size);
426 f2fs_put_page(page, 1);
428 /* no need to use xattr node block */
429 if (hsize <= inline_size) {
430 err = truncate_xattr_node(inode, ipage);
431 alloc_nid_failed(sbi, new_nid);
436 /* write to xattr node block */
437 if (F2FS_I(inode)->i_xattr_nid) {
438 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
440 alloc_nid_failed(sbi, new_nid);
441 return PTR_ERR(xpage);
443 f2fs_bug_on(sbi, new_nid);
444 f2fs_wait_on_page_writeback(xpage, NODE, true);
446 struct dnode_of_data dn;
447 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
448 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
450 alloc_nid_failed(sbi, new_nid);
451 return PTR_ERR(xpage);
453 alloc_nid_done(sbi, new_nid);
456 xattr_addr = page_address(xpage);
457 memcpy(xattr_addr, txattr_addr + inline_size, MAX_XATTR_BLOCK_SIZE);
458 set_page_dirty(xpage);
459 f2fs_put_page(xpage, 1);
464 int f2fs_getxattr(struct inode *inode, int index, const char *name,
465 void *buffer, size_t buffer_size, struct page *ipage)
467 struct f2fs_xattr_entry *entry = NULL;
469 unsigned int size, len;
470 void *base_addr = NULL;
476 if (len > F2FS_NAME_LEN)
479 error = lookup_all_xattrs(inode, ipage, index, len, name,
484 size = le16_to_cpu(entry->e_value_size);
486 if (buffer && size > buffer_size) {
492 char *pval = entry->e_name + entry->e_name_len;
493 memcpy(buffer, pval, size);
501 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
503 struct inode *inode = d_inode(dentry);
504 struct f2fs_xattr_entry *entry;
507 size_t rest = buffer_size;
509 error = read_all_xattrs(inode, NULL, &base_addr);
513 list_for_each_xattr(entry, base_addr) {
514 const struct xattr_handler *handler =
515 f2fs_xattr_handler(entry->e_name_index);
520 if (!handler || (handler->list && !handler->list(dentry)))
523 prefix = handler->prefix ?: handler->name;
524 prefix_len = strlen(prefix);
525 size = prefix_len + entry->e_name_len + 1;
531 memcpy(buffer, prefix, prefix_len);
532 buffer += prefix_len;
533 memcpy(buffer, entry->e_name, entry->e_name_len);
534 buffer += entry->e_name_len;
539 error = buffer_size - rest;
545 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
546 const void *value, size_t size)
548 void *pval = entry->e_name + entry->e_name_len;
549 return (entry->e_value_size == size) && !memcmp(pval, value, size);
552 static int __f2fs_setxattr(struct inode *inode, int index,
553 const char *name, const void *value, size_t size,
554 struct page *ipage, int flags)
556 struct f2fs_xattr_entry *here, *last;
571 if (len > F2FS_NAME_LEN)
574 if (size > MAX_VALUE_LEN(inode))
577 error = read_all_xattrs(inode, ipage, &base_addr);
581 /* find entry with wanted name. */
582 here = __find_xattr(base_addr, index, len, name);
584 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
587 if ((flags & XATTR_CREATE)) {
592 if (f2fs_xattr_value_same(here, value, size))
594 } else if ((flags & XATTR_REPLACE)) {
600 while (!IS_XATTR_LAST_ENTRY(last))
601 last = XATTR_NEXT_ENTRY(last);
603 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
609 * If value is NULL, it is remove operation.
610 * In case of update operation, we calculate free.
612 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
614 free = free + ENTRY_SIZE(here);
616 if (unlikely(free < newsize)) {
622 /* 2. Remove old entry */
625 * If entry is found, remove old entry.
626 * If not found, remove operation is not needed.
628 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
629 int oldsize = ENTRY_SIZE(here);
631 memmove(here, next, (char *)last - (char *)next);
632 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
633 memset(last, 0, oldsize);
636 new_hsize = (char *)last - (char *)base_addr;
638 /* 3. Write new entry */
642 * Before we come here, old entry is removed.
643 * We just write new entry.
645 last->e_name_index = index;
646 last->e_name_len = len;
647 memcpy(last->e_name, name, len);
648 pval = last->e_name + len;
649 memcpy(pval, value, size);
650 last->e_value_size = cpu_to_le16(size);
651 new_hsize += newsize;
654 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
658 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
659 inode->i_mode = F2FS_I(inode)->i_acl_mode;
660 inode->i_ctime = current_time(inode);
661 clear_inode_flag(inode, FI_ACL_MODE);
663 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
664 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
665 f2fs_set_encrypted_inode(inode);
666 f2fs_mark_inode_dirty_sync(inode, true);
667 if (!error && S_ISDIR(inode->i_mode))
668 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
674 int f2fs_setxattr(struct inode *inode, int index, const char *name,
675 const void *value, size_t size,
676 struct page *ipage, int flags)
678 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
681 /* this case is only from init_inode_metadata */
683 return __f2fs_setxattr(inode, index, name, value,
685 f2fs_balance_fs(sbi, true);
688 /* protect xattr_ver */
689 down_write(&F2FS_I(inode)->i_sem);
690 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
691 up_write(&F2FS_I(inode)->i_sem);
694 f2fs_update_time(sbi, REQ_TIME);