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>
27 static size_t f2fs_xattr_generic_list(struct dentry *dentry, char *list,
28 size_t list_size, const char *name, size_t name_len, int type)
30 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
31 int total_len, prefix_len = 0;
32 const char *prefix = NULL;
35 case F2FS_XATTR_INDEX_USER:
36 if (!test_opt(sbi, XATTR_USER))
38 prefix = XATTR_USER_PREFIX;
39 prefix_len = XATTR_USER_PREFIX_LEN;
41 case F2FS_XATTR_INDEX_TRUSTED:
42 if (!capable(CAP_SYS_ADMIN))
44 prefix = XATTR_TRUSTED_PREFIX;
45 prefix_len = XATTR_TRUSTED_PREFIX_LEN;
47 case F2FS_XATTR_INDEX_SECURITY:
48 prefix = XATTR_SECURITY_PREFIX;
49 prefix_len = XATTR_SECURITY_PREFIX_LEN;
55 total_len = prefix_len + name_len + 1;
56 if (list && total_len <= list_size) {
57 memcpy(list, prefix, prefix_len);
58 memcpy(list + prefix_len, name, name_len);
59 list[prefix_len + name_len] = '\0';
64 static int f2fs_xattr_generic_get(struct dentry *dentry, const char *name,
65 void *buffer, size_t size, int type)
67 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
70 case F2FS_XATTR_INDEX_USER:
71 if (!test_opt(sbi, XATTR_USER))
74 case F2FS_XATTR_INDEX_TRUSTED:
75 if (!capable(CAP_SYS_ADMIN))
78 case F2FS_XATTR_INDEX_SECURITY:
83 if (strcmp(name, "") == 0)
85 return f2fs_getxattr(dentry->d_inode, type, name, buffer, size);
88 static int f2fs_xattr_generic_set(struct dentry *dentry, const char *name,
89 const void *value, size_t size, int flags, int type)
91 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
94 case F2FS_XATTR_INDEX_USER:
95 if (!test_opt(sbi, XATTR_USER))
98 case F2FS_XATTR_INDEX_TRUSTED:
99 if (!capable(CAP_SYS_ADMIN))
102 case F2FS_XATTR_INDEX_SECURITY:
107 if (strcmp(name, "") == 0)
110 return f2fs_setxattr(dentry->d_inode, type, name, value, size, NULL);
113 static size_t f2fs_xattr_advise_list(struct dentry *dentry, char *list,
114 size_t list_size, const char *name, size_t name_len, int type)
116 const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
119 if (type != F2FS_XATTR_INDEX_ADVISE)
122 size = strlen(xname) + 1;
123 if (list && size <= list_size)
124 memcpy(list, xname, size);
128 static int f2fs_xattr_advise_get(struct dentry *dentry, const char *name,
129 void *buffer, size_t size, int type)
131 struct inode *inode = dentry->d_inode;
133 if (strcmp(name, "") != 0)
136 *((char *)buffer) = F2FS_I(inode)->i_advise;
140 static int f2fs_xattr_advise_set(struct dentry *dentry, const char *name,
141 const void *value, size_t size, int flags, int type)
143 struct inode *inode = dentry->d_inode;
145 if (strcmp(name, "") != 0)
147 if (!inode_owner_or_capable(inode))
152 F2FS_I(inode)->i_advise |= *(char *)value;
156 #ifdef CONFIG_F2FS_FS_SECURITY
157 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
160 const struct xattr *xattr;
163 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
164 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
165 xattr->name, xattr->value,
166 xattr->value_len, (struct page *)page);
173 int f2fs_init_security(struct inode *inode, struct inode *dir,
174 const struct qstr *qstr, struct page *ipage)
176 return security_inode_init_security(inode, dir, qstr,
177 &f2fs_initxattrs, ipage);
181 const struct xattr_handler f2fs_xattr_user_handler = {
182 .prefix = XATTR_USER_PREFIX,
183 .flags = F2FS_XATTR_INDEX_USER,
184 .list = f2fs_xattr_generic_list,
185 .get = f2fs_xattr_generic_get,
186 .set = f2fs_xattr_generic_set,
189 const struct xattr_handler f2fs_xattr_trusted_handler = {
190 .prefix = XATTR_TRUSTED_PREFIX,
191 .flags = F2FS_XATTR_INDEX_TRUSTED,
192 .list = f2fs_xattr_generic_list,
193 .get = f2fs_xattr_generic_get,
194 .set = f2fs_xattr_generic_set,
197 const struct xattr_handler f2fs_xattr_advise_handler = {
198 .prefix = F2FS_SYSTEM_ADVISE_PREFIX,
199 .flags = F2FS_XATTR_INDEX_ADVISE,
200 .list = f2fs_xattr_advise_list,
201 .get = f2fs_xattr_advise_get,
202 .set = f2fs_xattr_advise_set,
205 const struct xattr_handler f2fs_xattr_security_handler = {
206 .prefix = XATTR_SECURITY_PREFIX,
207 .flags = F2FS_XATTR_INDEX_SECURITY,
208 .list = f2fs_xattr_generic_list,
209 .get = f2fs_xattr_generic_get,
210 .set = f2fs_xattr_generic_set,
213 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
214 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
215 #ifdef CONFIG_F2FS_FS_POSIX_ACL
216 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &f2fs_xattr_acl_access_handler,
217 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &f2fs_xattr_acl_default_handler,
219 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
220 #ifdef CONFIG_F2FS_FS_SECURITY
221 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
223 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
226 const struct xattr_handler *f2fs_xattr_handlers[] = {
227 &f2fs_xattr_user_handler,
228 #ifdef CONFIG_F2FS_FS_POSIX_ACL
229 &f2fs_xattr_acl_access_handler,
230 &f2fs_xattr_acl_default_handler,
232 &f2fs_xattr_trusted_handler,
233 #ifdef CONFIG_F2FS_FS_SECURITY
234 &f2fs_xattr_security_handler,
236 &f2fs_xattr_advise_handler,
240 static inline const struct xattr_handler *f2fs_xattr_handler(int name_index)
242 const struct xattr_handler *handler = NULL;
244 if (name_index > 0 && name_index < ARRAY_SIZE(f2fs_xattr_handler_map))
245 handler = f2fs_xattr_handler_map[name_index];
249 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int name_index,
250 size_t name_len, const char *name)
252 struct f2fs_xattr_entry *entry;
254 list_for_each_xattr(entry, base_addr) {
255 if (entry->e_name_index != name_index)
257 if (entry->e_name_len != name_len)
259 if (!memcmp(entry->e_name, name, name_len))
265 int f2fs_getxattr(struct inode *inode, int name_index, const char *name,
266 void *buffer, size_t buffer_size)
268 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
269 struct f2fs_inode_info *fi = F2FS_I(inode);
270 struct f2fs_xattr_entry *entry;
273 size_t value_len, name_len;
277 name_len = strlen(name);
279 if (!fi->i_xattr_nid)
282 page = get_node_page(sbi, fi->i_xattr_nid);
284 return PTR_ERR(page);
286 entry = __find_xattr(page_address(page), name_index, name_len, name);
287 if (IS_XATTR_LAST_ENTRY(entry)) {
292 value_len = le16_to_cpu(entry->e_value_size);
294 if (buffer && value_len > buffer_size) {
300 char *pval = entry->e_name + entry->e_name_len;
301 memcpy(buffer, pval, value_len);
306 f2fs_put_page(page, 1);
310 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
312 struct inode *inode = dentry->d_inode;
313 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
314 struct f2fs_inode_info *fi = F2FS_I(inode);
315 struct f2fs_xattr_entry *entry;
319 size_t rest = buffer_size;
321 if (!fi->i_xattr_nid)
324 page = get_node_page(sbi, fi->i_xattr_nid);
326 return PTR_ERR(page);
327 base_addr = page_address(page);
329 list_for_each_xattr(entry, base_addr) {
330 const struct xattr_handler *handler =
331 f2fs_xattr_handler(entry->e_name_index);
337 size = handler->list(dentry, buffer, rest, entry->e_name,
338 entry->e_name_len, handler->flags);
339 if (buffer && size > rest) {
348 error = buffer_size - rest;
350 f2fs_put_page(page, 1);
354 int f2fs_setxattr(struct inode *inode, int name_index, const char *name,
355 const void *value, size_t value_len, struct page *ipage)
357 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
358 struct f2fs_inode_info *fi = F2FS_I(inode);
359 struct f2fs_xattr_header *header = NULL;
360 struct f2fs_xattr_entry *here, *last;
363 int error, found, free, newsize;
374 name_len = strlen(name);
376 if (name_len > F2FS_NAME_LEN || value_len > MAX_VALUE_LEN)
379 f2fs_balance_fs(sbi);
381 ilock = mutex_lock_op(sbi);
383 if (!fi->i_xattr_nid) {
384 /* Allocate new attribute block */
385 struct dnode_of_data dn;
388 if (!alloc_nid(sbi, &new_nid)) {
392 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
393 mark_inode_dirty(inode);
395 page = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
397 alloc_nid_failed(sbi, new_nid);
398 error = PTR_ERR(page);
402 alloc_nid_done(sbi, new_nid);
403 base_addr = page_address(page);
404 header = XATTR_HDR(base_addr);
405 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
406 header->h_refcount = cpu_to_le32(1);
408 /* The inode already has an extended attribute block. */
409 page = get_node_page(sbi, fi->i_xattr_nid);
411 error = PTR_ERR(page);
415 base_addr = page_address(page);
416 header = XATTR_HDR(base_addr);
419 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
424 /* find entry with wanted name. */
425 here = __find_xattr(base_addr, name_index, name_len, name);
427 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
430 while (!IS_XATTR_LAST_ENTRY(last))
431 last = XATTR_NEXT_ENTRY(last);
433 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) +
434 name_len + value_len);
438 /* If value is NULL, it is remove operation.
439 * In case of update operation, we caculate free.
441 free = MIN_OFFSET - ((char *)last - (char *)header);
443 free = free - ENTRY_SIZE(here);
445 if (free < newsize) {
451 /* 2. Remove old entry */
453 /* If entry is found, remove old entry.
454 * If not found, remove operation is not needed.
456 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
457 int oldsize = ENTRY_SIZE(here);
459 memmove(here, next, (char *)last - (char *)next);
460 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
461 memset(last, 0, oldsize);
464 /* 3. Write new entry */
466 /* Before we come here, old entry is removed.
467 * We just write new entry. */
468 memset(last, 0, newsize);
469 last->e_name_index = name_index;
470 last->e_name_len = name_len;
471 memcpy(last->e_name, name, name_len);
472 pval = last->e_name + name_len;
473 memcpy(pval, value, value_len);
474 last->e_value_size = cpu_to_le16(value_len);
477 set_page_dirty(page);
478 f2fs_put_page(page, 1);
480 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
481 inode->i_mode = fi->i_acl_mode;
482 inode->i_ctime = CURRENT_TIME;
483 clear_inode_flag(fi, FI_ACL_MODE);
486 /* store checkpoint version for conducting checkpoint during fsync */
487 fi->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
490 update_inode(inode, ipage);
492 update_inode_page(inode);
493 mutex_unlock_op(sbi, ilock);
497 f2fs_put_page(page, 1);
499 mutex_unlock_op(sbi, ilock);