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 case F2FS_XATTR_INDEX_SECURITY:
45 return f2fs_getxattr(inode, handler->flags, name,
49 static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
50 struct dentry *unused, struct inode *inode,
51 const char *name, const void *value,
52 size_t size, int flags)
54 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
56 switch (handler->flags) {
57 case F2FS_XATTR_INDEX_USER:
58 if (!test_opt(sbi, XATTR_USER))
61 case F2FS_XATTR_INDEX_TRUSTED:
62 case F2FS_XATTR_INDEX_SECURITY:
67 return f2fs_setxattr(inode, handler->flags, name,
68 value, size, NULL, flags);
71 static bool f2fs_xattr_user_list(struct dentry *dentry)
73 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
75 return test_opt(sbi, XATTR_USER);
78 static bool f2fs_xattr_trusted_list(struct dentry *dentry)
80 return capable(CAP_SYS_ADMIN);
83 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
84 struct dentry *unused, struct inode *inode,
85 const char *name, void *buffer, size_t size)
88 *((char *)buffer) = F2FS_I(inode)->i_advise;
92 static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
93 struct dentry *unused, struct inode *inode,
94 const char *name, const void *value,
95 size_t size, int flags)
97 unsigned char old_advise = F2FS_I(inode)->i_advise;
98 unsigned char new_advise;
100 if (!inode_owner_or_capable(inode))
105 new_advise = *(char *)value;
106 if (new_advise & ~FADVISE_MODIFIABLE_BITS)
109 new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
110 new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
112 F2FS_I(inode)->i_advise = new_advise;
113 f2fs_mark_inode_dirty_sync(inode, true);
117 #ifdef CONFIG_F2FS_FS_SECURITY
118 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
121 const struct xattr *xattr;
124 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
125 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
126 xattr->name, xattr->value,
127 xattr->value_len, (struct page *)page, 0);
134 int f2fs_init_security(struct inode *inode, struct inode *dir,
135 const struct qstr *qstr, struct page *ipage)
137 return security_inode_init_security(inode, dir, qstr,
138 &f2fs_initxattrs, ipage);
142 const struct xattr_handler f2fs_xattr_user_handler = {
143 .prefix = XATTR_USER_PREFIX,
144 .flags = F2FS_XATTR_INDEX_USER,
145 .list = f2fs_xattr_user_list,
146 .get = f2fs_xattr_generic_get,
147 .set = f2fs_xattr_generic_set,
150 const struct xattr_handler f2fs_xattr_trusted_handler = {
151 .prefix = XATTR_TRUSTED_PREFIX,
152 .flags = F2FS_XATTR_INDEX_TRUSTED,
153 .list = f2fs_xattr_trusted_list,
154 .get = f2fs_xattr_generic_get,
155 .set = f2fs_xattr_generic_set,
158 const struct xattr_handler f2fs_xattr_advise_handler = {
159 .name = F2FS_SYSTEM_ADVISE_NAME,
160 .flags = F2FS_XATTR_INDEX_ADVISE,
161 .get = f2fs_xattr_advise_get,
162 .set = f2fs_xattr_advise_set,
165 const struct xattr_handler f2fs_xattr_security_handler = {
166 .prefix = XATTR_SECURITY_PREFIX,
167 .flags = F2FS_XATTR_INDEX_SECURITY,
168 .get = f2fs_xattr_generic_get,
169 .set = f2fs_xattr_generic_set,
172 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
173 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
174 #ifdef CONFIG_F2FS_FS_POSIX_ACL
175 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
176 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
178 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
179 #ifdef CONFIG_F2FS_FS_SECURITY
180 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
182 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
185 const struct xattr_handler *f2fs_xattr_handlers[] = {
186 &f2fs_xattr_user_handler,
187 #ifdef CONFIG_F2FS_FS_POSIX_ACL
188 &posix_acl_access_xattr_handler,
189 &posix_acl_default_xattr_handler,
191 &f2fs_xattr_trusted_handler,
192 #ifdef CONFIG_F2FS_FS_SECURITY
193 &f2fs_xattr_security_handler,
195 &f2fs_xattr_advise_handler,
199 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
201 const struct xattr_handler *handler = NULL;
203 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
204 handler = f2fs_xattr_handler_map[index];
208 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
209 size_t len, const char *name)
211 struct f2fs_xattr_entry *entry;
213 list_for_each_xattr(entry, base_addr) {
214 if (entry->e_name_index != index)
216 if (entry->e_name_len != len)
218 if (!memcmp(entry->e_name, name, len))
224 static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
225 void *base_addr, void **last_addr, int index,
226 size_t len, const char *name)
228 struct f2fs_xattr_entry *entry;
229 unsigned int inline_size = inline_xattr_size(inode);
231 list_for_each_xattr(entry, base_addr) {
232 if ((void *)entry + sizeof(__u32) > base_addr + inline_size ||
233 (void *)XATTR_NEXT_ENTRY(entry) + sizeof(__u32) >
234 base_addr + inline_size) {
238 if (entry->e_name_index != index)
240 if (entry->e_name_len != len)
242 if (!memcmp(entry->e_name, name, len))
248 static int read_inline_xattr(struct inode *inode, struct page *ipage,
251 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
252 unsigned int inline_size = inline_xattr_size(inode);
253 struct page *page = NULL;
257 inline_addr = inline_xattr_addr(inode, ipage);
259 page = f2fs_get_node_page(sbi, inode->i_ino);
261 return PTR_ERR(page);
263 inline_addr = inline_xattr_addr(inode, page);
265 memcpy(txattr_addr, inline_addr, inline_size);
266 f2fs_put_page(page, 1);
271 static int read_xattr_block(struct inode *inode, void *txattr_addr)
273 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
274 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
275 unsigned int inline_size = inline_xattr_size(inode);
279 /* The inode already has an extended attribute block. */
280 xpage = f2fs_get_node_page(sbi, xnid);
282 return PTR_ERR(xpage);
284 xattr_addr = page_address(xpage);
285 memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
286 f2fs_put_page(xpage, 1);
291 static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
292 unsigned int index, unsigned int len,
293 const char *name, struct f2fs_xattr_entry **xe,
296 void *cur_addr, *txattr_addr, *last_addr = NULL;
297 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
298 unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
299 unsigned int inline_size = inline_xattr_size(inode);
302 if (!size && !inline_size)
305 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
306 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
310 /* read from inline xattr */
312 err = read_inline_xattr(inode, ipage, txattr_addr);
316 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
322 /* read from xattr node block */
324 err = read_xattr_block(inode, txattr_addr);
330 cur_addr = XATTR_HDR(last_addr) - 1;
332 cur_addr = txattr_addr;
334 *xe = __find_xattr(cur_addr, index, len, name);
336 if (IS_XATTR_LAST_ENTRY(*xe)) {
341 *base_addr = txattr_addr;
348 static int read_all_xattrs(struct inode *inode, struct page *ipage,
351 struct f2fs_xattr_header *header;
352 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
353 unsigned int size = VALID_XATTR_BLOCK_SIZE;
354 unsigned int inline_size = inline_xattr_size(inode);
358 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
359 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
363 /* read from inline xattr */
365 err = read_inline_xattr(inode, ipage, txattr_addr);
370 /* read from xattr node block */
372 err = read_xattr_block(inode, txattr_addr);
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 = inline_xattr_size(inode);
396 struct page *in_page = NULL;
398 void *inline_addr = NULL;
403 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
404 if (!f2fs_alloc_nid(sbi, &new_nid))
407 /* write to inline xattr */
410 inline_addr = inline_xattr_addr(inode, ipage);
412 in_page = f2fs_get_node_page(sbi, inode->i_ino);
413 if (IS_ERR(in_page)) {
414 f2fs_alloc_nid_failed(sbi, new_nid);
415 return PTR_ERR(in_page);
417 inline_addr = inline_xattr_addr(inode, in_page);
420 f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
422 /* no need to use xattr node block */
423 if (hsize <= inline_size) {
424 err = f2fs_truncate_xattr_node(inode);
425 f2fs_alloc_nid_failed(sbi, new_nid);
427 f2fs_put_page(in_page, 1);
430 memcpy(inline_addr, txattr_addr, inline_size);
431 set_page_dirty(ipage ? ipage : in_page);
436 /* write to xattr node block */
437 if (F2FS_I(inode)->i_xattr_nid) {
438 xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
440 err = PTR_ERR(xpage);
441 f2fs_alloc_nid_failed(sbi, new_nid);
444 f2fs_bug_on(sbi, new_nid);
445 f2fs_wait_on_page_writeback(xpage, NODE, true);
447 struct dnode_of_data dn;
448 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
449 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
451 err = PTR_ERR(xpage);
452 f2fs_alloc_nid_failed(sbi, new_nid);
455 f2fs_alloc_nid_done(sbi, new_nid);
457 xattr_addr = page_address(xpage);
460 memcpy(inline_addr, txattr_addr, inline_size);
461 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
464 set_page_dirty(ipage ? ipage : in_page);
465 set_page_dirty(xpage);
467 f2fs_put_page(xpage, 1);
469 f2fs_put_page(in_page, 1);
473 int f2fs_getxattr(struct inode *inode, int index, const char *name,
474 void *buffer, size_t buffer_size, struct page *ipage)
476 struct f2fs_xattr_entry *entry = NULL;
478 unsigned int size, len;
479 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,
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;
504 memcpy(buffer, pval, size);
512 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
514 struct inode *inode = d_inode(dentry);
515 struct f2fs_xattr_entry *entry;
518 size_t rest = buffer_size;
520 down_read(&F2FS_I(inode)->i_xattr_sem);
521 error = read_all_xattrs(inode, NULL, &base_addr);
522 up_read(&F2FS_I(inode)->i_xattr_sem);
526 list_for_each_xattr(entry, base_addr) {
527 const struct xattr_handler *handler =
528 f2fs_xattr_handler(entry->e_name_index);
533 if (!handler || (handler->list && !handler->list(dentry)))
536 prefix = handler->prefix ?: handler->name;
537 prefix_len = strlen(prefix);
538 size = prefix_len + entry->e_name_len + 1;
544 memcpy(buffer, prefix, prefix_len);
545 buffer += prefix_len;
546 memcpy(buffer, entry->e_name, entry->e_name_len);
547 buffer += entry->e_name_len;
552 error = buffer_size - rest;
558 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
559 const void *value, size_t size)
561 void *pval = entry->e_name + entry->e_name_len;
563 return (le16_to_cpu(entry->e_value_size) == size) &&
564 !memcmp(pval, value, size);
567 static int __f2fs_setxattr(struct inode *inode, int index,
568 const char *name, const void *value, size_t size,
569 struct page *ipage, int flags)
571 struct f2fs_xattr_entry *here, *last;
586 if (len > F2FS_NAME_LEN)
589 if (size > MAX_VALUE_LEN(inode))
592 error = read_all_xattrs(inode, ipage, &base_addr);
596 /* find entry with wanted name. */
597 here = __find_xattr(base_addr, index, len, name);
599 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
602 if ((flags & XATTR_CREATE)) {
607 if (value && f2fs_xattr_value_same(here, value, size))
609 } else if ((flags & XATTR_REPLACE)) {
615 while (!IS_XATTR_LAST_ENTRY(last))
616 last = XATTR_NEXT_ENTRY(last);
618 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
624 * If value is NULL, it is remove operation.
625 * In case of update operation, we calculate free.
627 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
629 free = free + ENTRY_SIZE(here);
631 if (unlikely(free < newsize)) {
637 /* 2. Remove old entry */
640 * If entry is found, remove old entry.
641 * If not found, remove operation is not needed.
643 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
644 int oldsize = ENTRY_SIZE(here);
646 memmove(here, next, (char *)last - (char *)next);
647 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
648 memset(last, 0, oldsize);
651 new_hsize = (char *)last - (char *)base_addr;
653 /* 3. Write new entry */
657 * Before we come here, old entry is removed.
658 * We just write new entry.
660 last->e_name_index = index;
661 last->e_name_len = len;
662 memcpy(last->e_name, name, len);
663 pval = last->e_name + len;
664 memcpy(pval, value, size);
665 last->e_value_size = cpu_to_le16(size);
666 new_hsize += newsize;
669 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
673 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
674 inode->i_mode = F2FS_I(inode)->i_acl_mode;
675 inode->i_ctime = current_time(inode);
676 clear_inode_flag(inode, FI_ACL_MODE);
678 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
679 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
680 f2fs_set_encrypted_inode(inode);
681 f2fs_mark_inode_dirty_sync(inode, true);
682 if (!error && S_ISDIR(inode->i_mode))
683 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
689 int f2fs_setxattr(struct inode *inode, int index, const char *name,
690 const void *value, size_t size,
691 struct page *ipage, int flags)
693 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
696 err = dquot_initialize(inode);
700 /* this case is only from f2fs_init_inode_metadata */
702 return __f2fs_setxattr(inode, index, name, value,
704 f2fs_balance_fs(sbi, true);
707 /* protect xattr_ver */
708 down_write(&F2FS_I(inode)->i_sem);
709 down_write(&F2FS_I(inode)->i_xattr_sem);
710 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
711 up_write(&F2FS_I(inode)->i_xattr_sem);
712 up_write(&F2FS_I(inode)->i_sem);
715 f2fs_update_time(sbi, REQ_TIME);