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 size_t f2fs_xattr_generic_list(const struct xattr_handler *handler,
29 struct dentry *dentry, char *list, size_t list_size,
30 const char *name, size_t len)
32 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
33 int total_len, prefix_len = 0;
34 const char *prefix = NULL;
36 switch (handler->flags) {
37 case F2FS_XATTR_INDEX_USER:
38 if (!test_opt(sbi, XATTR_USER))
40 prefix = XATTR_USER_PREFIX;
41 prefix_len = XATTR_USER_PREFIX_LEN;
43 case F2FS_XATTR_INDEX_TRUSTED:
44 if (!capable(CAP_SYS_ADMIN))
46 prefix = XATTR_TRUSTED_PREFIX;
47 prefix_len = XATTR_TRUSTED_PREFIX_LEN;
49 case F2FS_XATTR_INDEX_SECURITY:
50 prefix = XATTR_SECURITY_PREFIX;
51 prefix_len = XATTR_SECURITY_PREFIX_LEN;
57 total_len = prefix_len + len + 1;
58 if (list && total_len <= list_size) {
59 memcpy(list, prefix, prefix_len);
60 memcpy(list + prefix_len, name, len);
61 list[prefix_len + len] = '\0';
66 static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
67 struct dentry *dentry, const char *name, void *buffer,
70 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
72 switch (handler->flags) {
73 case F2FS_XATTR_INDEX_USER:
74 if (!test_opt(sbi, XATTR_USER))
77 case F2FS_XATTR_INDEX_TRUSTED:
78 if (!capable(CAP_SYS_ADMIN))
81 case F2FS_XATTR_INDEX_SECURITY:
86 if (strcmp(name, "") == 0)
88 return f2fs_getxattr(d_inode(dentry), handler->flags, name,
92 static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
93 struct dentry *dentry, const char *name, const void *value,
94 size_t size, int flags)
96 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
98 switch (handler->flags) {
99 case F2FS_XATTR_INDEX_USER:
100 if (!test_opt(sbi, XATTR_USER))
103 case F2FS_XATTR_INDEX_TRUSTED:
104 if (!capable(CAP_SYS_ADMIN))
107 case F2FS_XATTR_INDEX_SECURITY:
112 if (strcmp(name, "") == 0)
115 return f2fs_setxattr(d_inode(dentry), handler->flags, name,
116 value, size, NULL, flags);
119 static size_t f2fs_xattr_advise_list(const struct xattr_handler *handler,
120 struct dentry *dentry, char *list, size_t list_size,
121 const char *name, size_t len)
123 const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
126 if (handler->flags != F2FS_XATTR_INDEX_ADVISE)
129 size = strlen(xname) + 1;
130 if (list && size <= list_size)
131 memcpy(list, xname, size);
135 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
136 struct dentry *dentry, const char *name, void *buffer,
139 struct inode *inode = d_inode(dentry);
141 if (strcmp(name, "") != 0)
145 *((char *)buffer) = F2FS_I(inode)->i_advise;
149 static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
150 struct dentry *dentry, const char *name, const void *value,
151 size_t size, int flags)
153 struct inode *inode = d_inode(dentry);
155 if (strcmp(name, "") != 0)
157 if (!inode_owner_or_capable(inode))
162 F2FS_I(inode)->i_advise |= *(char *)value;
163 mark_inode_dirty(inode);
167 #ifdef CONFIG_F2FS_FS_SECURITY
168 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
171 const struct xattr *xattr;
174 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
175 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
176 xattr->name, xattr->value,
177 xattr->value_len, (struct page *)page, 0);
184 int f2fs_init_security(struct inode *inode, struct inode *dir,
185 const struct qstr *qstr, struct page *ipage)
187 return security_inode_init_security(inode, dir, qstr,
188 &f2fs_initxattrs, ipage);
192 const struct xattr_handler f2fs_xattr_user_handler = {
193 .prefix = XATTR_USER_PREFIX,
194 .flags = F2FS_XATTR_INDEX_USER,
195 .list = f2fs_xattr_generic_list,
196 .get = f2fs_xattr_generic_get,
197 .set = f2fs_xattr_generic_set,
200 const struct xattr_handler f2fs_xattr_trusted_handler = {
201 .prefix = XATTR_TRUSTED_PREFIX,
202 .flags = F2FS_XATTR_INDEX_TRUSTED,
203 .list = f2fs_xattr_generic_list,
204 .get = f2fs_xattr_generic_get,
205 .set = f2fs_xattr_generic_set,
208 const struct xattr_handler f2fs_xattr_advise_handler = {
209 .prefix = F2FS_SYSTEM_ADVISE_PREFIX,
210 .flags = F2FS_XATTR_INDEX_ADVISE,
211 .list = f2fs_xattr_advise_list,
212 .get = f2fs_xattr_advise_get,
213 .set = f2fs_xattr_advise_set,
216 const struct xattr_handler f2fs_xattr_security_handler = {
217 .prefix = XATTR_SECURITY_PREFIX,
218 .flags = F2FS_XATTR_INDEX_SECURITY,
219 .list = f2fs_xattr_generic_list,
220 .get = f2fs_xattr_generic_get,
221 .set = f2fs_xattr_generic_set,
224 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
225 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
226 #ifdef CONFIG_F2FS_FS_POSIX_ACL
227 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
228 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
230 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
231 #ifdef CONFIG_F2FS_FS_SECURITY
232 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
234 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
237 const struct xattr_handler *f2fs_xattr_handlers[] = {
238 &f2fs_xattr_user_handler,
239 #ifdef CONFIG_F2FS_FS_POSIX_ACL
240 &posix_acl_access_xattr_handler,
241 &posix_acl_default_xattr_handler,
243 &f2fs_xattr_trusted_handler,
244 #ifdef CONFIG_F2FS_FS_SECURITY
245 &f2fs_xattr_security_handler,
247 &f2fs_xattr_advise_handler,
251 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
253 const struct xattr_handler *handler = NULL;
255 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
256 handler = f2fs_xattr_handler_map[index];
260 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
261 size_t len, const char *name)
263 struct f2fs_xattr_entry *entry;
265 list_for_each_xattr(entry, base_addr) {
266 if (entry->e_name_index != index)
268 if (entry->e_name_len != len)
270 if (!memcmp(entry->e_name, name, len))
276 static void *read_all_xattrs(struct inode *inode, struct page *ipage)
278 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
279 struct f2fs_xattr_header *header;
280 size_t size = PAGE_SIZE, inline_size = 0;
283 inline_size = inline_xattr_size(inode);
285 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
289 /* read from inline xattr */
291 struct page *page = NULL;
295 inline_addr = inline_xattr_addr(ipage);
297 page = get_node_page(sbi, inode->i_ino);
300 inline_addr = inline_xattr_addr(page);
302 memcpy(txattr_addr, inline_addr, inline_size);
303 f2fs_put_page(page, 1);
306 /* read from xattr node block */
307 if (F2FS_I(inode)->i_xattr_nid) {
311 /* The inode already has an extended attribute block. */
312 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
316 xattr_addr = page_address(xpage);
317 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
318 f2fs_put_page(xpage, 1);
321 header = XATTR_HDR(txattr_addr);
323 /* never been allocated xattrs */
324 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
325 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
326 header->h_refcount = cpu_to_le32(1);
334 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
335 void *txattr_addr, struct page *ipage)
337 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
338 size_t inline_size = 0;
344 inline_size = inline_xattr_size(inode);
346 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
347 if (!alloc_nid(sbi, &new_nid))
350 /* write to inline xattr */
352 struct page *page = NULL;
356 inline_addr = inline_xattr_addr(ipage);
357 f2fs_wait_on_page_writeback(ipage, NODE);
359 page = get_node_page(sbi, inode->i_ino);
361 alloc_nid_failed(sbi, new_nid);
362 return PTR_ERR(page);
364 inline_addr = inline_xattr_addr(page);
365 f2fs_wait_on_page_writeback(page, NODE);
367 memcpy(inline_addr, txattr_addr, inline_size);
368 f2fs_put_page(page, 1);
370 /* no need to use xattr node block */
371 if (hsize <= inline_size) {
372 err = truncate_xattr_node(inode, ipage);
373 alloc_nid_failed(sbi, new_nid);
378 /* write to xattr node block */
379 if (F2FS_I(inode)->i_xattr_nid) {
380 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
382 alloc_nid_failed(sbi, new_nid);
383 return PTR_ERR(xpage);
385 f2fs_bug_on(sbi, new_nid);
386 f2fs_wait_on_page_writeback(xpage, NODE);
388 struct dnode_of_data dn;
389 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
390 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
392 alloc_nid_failed(sbi, new_nid);
393 return PTR_ERR(xpage);
395 alloc_nid_done(sbi, new_nid);
398 xattr_addr = page_address(xpage);
399 memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
400 sizeof(struct node_footer));
401 set_page_dirty(xpage);
402 f2fs_put_page(xpage, 1);
404 /* need to checkpoint during fsync */
405 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
409 int f2fs_getxattr(struct inode *inode, int index, const char *name,
410 void *buffer, size_t buffer_size, struct page *ipage)
412 struct f2fs_xattr_entry *entry;
421 if (len > F2FS_NAME_LEN)
424 base_addr = read_all_xattrs(inode, ipage);
428 entry = __find_xattr(base_addr, index, len, name);
429 if (IS_XATTR_LAST_ENTRY(entry)) {
434 size = le16_to_cpu(entry->e_value_size);
436 if (buffer && size > buffer_size) {
442 char *pval = entry->e_name + entry->e_name_len;
443 memcpy(buffer, pval, size);
452 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
454 struct inode *inode = d_inode(dentry);
455 struct f2fs_xattr_entry *entry;
458 size_t rest = buffer_size;
460 base_addr = read_all_xattrs(inode, NULL);
464 list_for_each_xattr(entry, base_addr) {
465 const struct xattr_handler *handler =
466 f2fs_xattr_handler(entry->e_name_index);
472 size = handler->list(handler, dentry, buffer, rest,
473 entry->e_name, entry->e_name_len);
474 if (buffer && size > rest) {
483 error = buffer_size - rest;
489 static int __f2fs_setxattr(struct inode *inode, int index,
490 const char *name, const void *value, size_t size,
491 struct page *ipage, int flags)
493 struct f2fs_inode_info *fi = F2FS_I(inode);
494 struct f2fs_xattr_entry *here, *last;
509 if (len > F2FS_NAME_LEN)
512 if (size > MAX_VALUE_LEN(inode))
515 base_addr = read_all_xattrs(inode, ipage);
519 /* find entry with wanted name. */
520 here = __find_xattr(base_addr, index, len, name);
522 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
524 if ((flags & XATTR_REPLACE) && !found) {
527 } else if ((flags & XATTR_CREATE) && found) {
533 while (!IS_XATTR_LAST_ENTRY(last))
534 last = XATTR_NEXT_ENTRY(last);
536 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
542 * If value is NULL, it is remove operation.
543 * In case of update operation, we calculate free.
545 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
547 free = free + ENTRY_SIZE(here);
549 if (unlikely(free < newsize)) {
555 /* 2. Remove old entry */
558 * If entry is found, remove old entry.
559 * If not found, remove operation is not needed.
561 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
562 int oldsize = ENTRY_SIZE(here);
564 memmove(here, next, (char *)last - (char *)next);
565 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
566 memset(last, 0, oldsize);
569 new_hsize = (char *)last - (char *)base_addr;
571 /* 3. Write new entry */
575 * Before we come here, old entry is removed.
576 * We just write new entry.
578 memset(last, 0, newsize);
579 last->e_name_index = index;
580 last->e_name_len = len;
581 memcpy(last->e_name, name, len);
582 pval = last->e_name + len;
583 memcpy(pval, value, size);
584 last->e_value_size = cpu_to_le16(size);
585 new_hsize += newsize;
588 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
592 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
593 inode->i_mode = fi->i_acl_mode;
594 inode->i_ctime = CURRENT_TIME;
595 clear_inode_flag(fi, FI_ACL_MODE);
597 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
598 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
599 f2fs_set_encrypted_inode(inode);
602 update_inode(inode, ipage);
604 update_inode_page(inode);
610 int f2fs_setxattr(struct inode *inode, int index, const char *name,
611 const void *value, size_t size,
612 struct page *ipage, int flags)
614 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
617 /* this case is only from init_inode_metadata */
619 return __f2fs_setxattr(inode, index, name, value,
621 f2fs_balance_fs(sbi);
624 /* protect xattr_ver */
625 down_write(&F2FS_I(inode)->i_sem);
626 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
627 up_write(&F2FS_I(inode)->i_sem);