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(struct dentry *dentry, char *list,
29 size_t list_size, const char *name, size_t len, int type)
31 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
32 int total_len, prefix_len = 0;
33 const char *prefix = NULL;
36 case F2FS_XATTR_INDEX_USER:
37 if (!test_opt(sbi, XATTR_USER))
39 prefix = XATTR_USER_PREFIX;
40 prefix_len = XATTR_USER_PREFIX_LEN;
42 case F2FS_XATTR_INDEX_TRUSTED:
43 if (!capable(CAP_SYS_ADMIN))
45 prefix = XATTR_TRUSTED_PREFIX;
46 prefix_len = XATTR_TRUSTED_PREFIX_LEN;
48 case F2FS_XATTR_INDEX_SECURITY:
49 prefix = XATTR_SECURITY_PREFIX;
50 prefix_len = XATTR_SECURITY_PREFIX_LEN;
56 total_len = prefix_len + len + 1;
57 if (list && total_len <= list_size) {
58 memcpy(list, prefix, prefix_len);
59 memcpy(list + prefix_len, name, len);
60 list[prefix_len + len] = '\0';
65 static int f2fs_xattr_generic_get(struct dentry *dentry, const char *name,
66 void *buffer, size_t size, int type)
68 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
71 case F2FS_XATTR_INDEX_USER:
72 if (!test_opt(sbi, XATTR_USER))
75 case F2FS_XATTR_INDEX_TRUSTED:
76 if (!capable(CAP_SYS_ADMIN))
79 case F2FS_XATTR_INDEX_SECURITY:
84 if (strcmp(name, "") == 0)
86 return f2fs_getxattr(dentry->d_inode, type, name, buffer, size);
89 static int f2fs_xattr_generic_set(struct dentry *dentry, const char *name,
90 const void *value, size_t size, int flags, int type)
92 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
95 case F2FS_XATTR_INDEX_USER:
96 if (!test_opt(sbi, XATTR_USER))
99 case F2FS_XATTR_INDEX_TRUSTED:
100 if (!capable(CAP_SYS_ADMIN))
103 case F2FS_XATTR_INDEX_SECURITY:
108 if (strcmp(name, "") == 0)
111 return f2fs_setxattr(dentry->d_inode, type, name,
112 value, size, NULL, flags);
115 static size_t f2fs_xattr_advise_list(struct dentry *dentry, char *list,
116 size_t list_size, const char *name, size_t len, int type)
118 const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
121 if (type != F2FS_XATTR_INDEX_ADVISE)
124 size = strlen(xname) + 1;
125 if (list && size <= list_size)
126 memcpy(list, xname, size);
130 static int f2fs_xattr_advise_get(struct dentry *dentry, const char *name,
131 void *buffer, size_t size, int type)
133 struct inode *inode = dentry->d_inode;
135 if (strcmp(name, "") != 0)
138 *((char *)buffer) = F2FS_I(inode)->i_advise;
142 static int f2fs_xattr_advise_set(struct dentry *dentry, const char *name,
143 const void *value, size_t size, int flags, int type)
145 struct inode *inode = dentry->d_inode;
147 if (strcmp(name, "") != 0)
149 if (!inode_owner_or_capable(inode))
154 F2FS_I(inode)->i_advise |= *(char *)value;
158 #ifdef CONFIG_F2FS_FS_SECURITY
159 static int __f2fs_setxattr(struct inode *inode, int index,
160 const char *name, const void *value, size_t size,
161 struct page *ipage, int);
163 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
166 const struct xattr *xattr;
169 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
170 err = __f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
171 xattr->name, xattr->value,
172 xattr->value_len, (struct page *)page, 0);
179 int f2fs_init_security(struct inode *inode, struct inode *dir,
180 const struct qstr *qstr, struct page *ipage)
182 return security_inode_init_security(inode, dir, qstr,
183 &f2fs_initxattrs, ipage);
187 const struct xattr_handler f2fs_xattr_user_handler = {
188 .prefix = XATTR_USER_PREFIX,
189 .flags = F2FS_XATTR_INDEX_USER,
190 .list = f2fs_xattr_generic_list,
191 .get = f2fs_xattr_generic_get,
192 .set = f2fs_xattr_generic_set,
195 const struct xattr_handler f2fs_xattr_trusted_handler = {
196 .prefix = XATTR_TRUSTED_PREFIX,
197 .flags = F2FS_XATTR_INDEX_TRUSTED,
198 .list = f2fs_xattr_generic_list,
199 .get = f2fs_xattr_generic_get,
200 .set = f2fs_xattr_generic_set,
203 const struct xattr_handler f2fs_xattr_advise_handler = {
204 .prefix = F2FS_SYSTEM_ADVISE_PREFIX,
205 .flags = F2FS_XATTR_INDEX_ADVISE,
206 .list = f2fs_xattr_advise_list,
207 .get = f2fs_xattr_advise_get,
208 .set = f2fs_xattr_advise_set,
211 const struct xattr_handler f2fs_xattr_security_handler = {
212 .prefix = XATTR_SECURITY_PREFIX,
213 .flags = F2FS_XATTR_INDEX_SECURITY,
214 .list = f2fs_xattr_generic_list,
215 .get = f2fs_xattr_generic_get,
216 .set = f2fs_xattr_generic_set,
219 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
220 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
221 #ifdef CONFIG_F2FS_FS_POSIX_ACL
222 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
223 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
225 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
226 #ifdef CONFIG_F2FS_FS_SECURITY
227 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
229 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
232 const struct xattr_handler *f2fs_xattr_handlers[] = {
233 &f2fs_xattr_user_handler,
234 #ifdef CONFIG_F2FS_FS_POSIX_ACL
235 &posix_acl_access_xattr_handler,
236 &posix_acl_default_xattr_handler,
238 &f2fs_xattr_trusted_handler,
239 #ifdef CONFIG_F2FS_FS_SECURITY
240 &f2fs_xattr_security_handler,
242 &f2fs_xattr_advise_handler,
246 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
248 const struct xattr_handler *handler = NULL;
250 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
251 handler = f2fs_xattr_handler_map[index];
255 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
256 size_t len, const char *name)
258 struct f2fs_xattr_entry *entry;
260 list_for_each_xattr(entry, base_addr) {
261 if (entry->e_name_index != index)
263 if (entry->e_name_len != len)
265 if (!memcmp(entry->e_name, name, len))
271 static void *read_all_xattrs(struct inode *inode, struct page *ipage)
273 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
274 struct f2fs_xattr_header *header;
275 size_t size = PAGE_SIZE, inline_size = 0;
278 inline_size = inline_xattr_size(inode);
280 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
284 /* read from inline xattr */
286 struct page *page = NULL;
290 inline_addr = inline_xattr_addr(ipage);
292 page = get_node_page(sbi, inode->i_ino);
295 inline_addr = inline_xattr_addr(page);
297 memcpy(txattr_addr, inline_addr, inline_size);
298 f2fs_put_page(page, 1);
301 /* read from xattr node block */
302 if (F2FS_I(inode)->i_xattr_nid) {
306 /* The inode already has an extended attribute block. */
307 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
311 xattr_addr = page_address(xpage);
312 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
313 f2fs_put_page(xpage, 1);
316 header = XATTR_HDR(txattr_addr);
318 /* never been allocated xattrs */
319 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
320 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
321 header->h_refcount = cpu_to_le32(1);
329 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
330 void *txattr_addr, struct page *ipage)
332 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
333 size_t inline_size = 0;
339 inline_size = inline_xattr_size(inode);
341 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
342 if (!alloc_nid(sbi, &new_nid))
345 /* write to inline xattr */
347 struct page *page = NULL;
351 inline_addr = inline_xattr_addr(ipage);
353 page = get_node_page(sbi, inode->i_ino);
355 alloc_nid_failed(sbi, new_nid);
356 return PTR_ERR(page);
358 inline_addr = inline_xattr_addr(page);
360 memcpy(inline_addr, txattr_addr, inline_size);
361 f2fs_put_page(page, 1);
363 /* no need to use xattr node block */
364 if (hsize <= inline_size) {
365 err = truncate_xattr_node(inode, ipage);
366 alloc_nid_failed(sbi, new_nid);
371 /* write to xattr node block */
372 if (F2FS_I(inode)->i_xattr_nid) {
373 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
375 alloc_nid_failed(sbi, new_nid);
376 return PTR_ERR(xpage);
378 f2fs_bug_on(new_nid);
380 struct dnode_of_data dn;
381 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
382 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
384 alloc_nid_failed(sbi, new_nid);
385 return PTR_ERR(xpage);
387 alloc_nid_done(sbi, new_nid);
390 xattr_addr = page_address(xpage);
391 memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
392 sizeof(struct node_footer));
393 set_page_dirty(xpage);
394 f2fs_put_page(xpage, 1);
396 /* need to checkpoint during fsync */
397 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
401 int f2fs_getxattr(struct inode *inode, int index, const char *name,
402 void *buffer, size_t buffer_size)
404 struct f2fs_xattr_entry *entry;
413 if (len > F2FS_NAME_LEN)
416 base_addr = read_all_xattrs(inode, NULL);
420 entry = __find_xattr(base_addr, index, len, name);
421 if (IS_XATTR_LAST_ENTRY(entry)) {
426 size = le16_to_cpu(entry->e_value_size);
428 if (buffer && size > buffer_size) {
434 char *pval = entry->e_name + entry->e_name_len;
435 memcpy(buffer, pval, size);
444 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
446 struct inode *inode = dentry->d_inode;
447 struct f2fs_xattr_entry *entry;
450 size_t rest = buffer_size;
452 base_addr = read_all_xattrs(inode, NULL);
456 list_for_each_xattr(entry, base_addr) {
457 const struct xattr_handler *handler =
458 f2fs_xattr_handler(entry->e_name_index);
464 size = handler->list(dentry, buffer, rest, entry->e_name,
465 entry->e_name_len, handler->flags);
466 if (buffer && size > rest) {
475 error = buffer_size - rest;
481 static int __f2fs_setxattr(struct inode *inode, int index,
482 const char *name, const void *value, size_t size,
483 struct page *ipage, int flags)
485 struct f2fs_inode_info *fi = F2FS_I(inode);
486 struct f2fs_xattr_entry *here, *last;
501 if (len > F2FS_NAME_LEN || size > MAX_VALUE_LEN(inode))
504 base_addr = read_all_xattrs(inode, ipage);
508 /* find entry with wanted name. */
509 here = __find_xattr(base_addr, index, len, name);
511 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
513 if ((flags & XATTR_REPLACE) && !found) {
516 } else if ((flags & XATTR_CREATE) && found) {
522 while (!IS_XATTR_LAST_ENTRY(last))
523 last = XATTR_NEXT_ENTRY(last);
525 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
531 * If value is NULL, it is remove operation.
532 * In case of update operation, we caculate free.
534 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
536 free = free + ENTRY_SIZE(here);
538 if (unlikely(free < newsize)) {
544 /* 2. Remove old entry */
547 * If entry is found, remove old entry.
548 * If not found, remove operation is not needed.
550 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
551 int oldsize = ENTRY_SIZE(here);
553 memmove(here, next, (char *)last - (char *)next);
554 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
555 memset(last, 0, oldsize);
558 new_hsize = (char *)last - (char *)base_addr;
560 /* 3. Write new entry */
564 * Before we come here, old entry is removed.
565 * We just write new entry.
567 memset(last, 0, newsize);
568 last->e_name_index = index;
569 last->e_name_len = len;
570 memcpy(last->e_name, name, len);
571 pval = last->e_name + len;
572 memcpy(pval, value, size);
573 last->e_value_size = cpu_to_le16(size);
574 new_hsize += newsize;
577 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
581 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
582 inode->i_mode = fi->i_acl_mode;
583 inode->i_ctime = CURRENT_TIME;
584 clear_inode_flag(fi, FI_ACL_MODE);
588 update_inode(inode, ipage);
590 update_inode_page(inode);
596 int f2fs_setxattr(struct inode *inode, int index, const char *name,
597 const void *value, size_t size,
598 struct page *ipage, int flags)
600 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
603 f2fs_balance_fs(sbi);
606 /* protect xattr_ver */
607 down_write(&F2FS_I(inode)->i_sem);
608 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
609 up_write(&F2FS_I(inode)->i_sem);