xfs: fix type usage
[linux/fpc-iii.git] / fs / f2fs / xattr.c
blob7c65540148f8b1b787c54e8eaff3a6813f4a5f9e
1 /*
2 * fs/f2fs/xattr.c
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>,
15 * Red Hat Inc.
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>
25 #include "f2fs.h"
26 #include "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))
37 return -EOPNOTSUPP;
38 break;
39 case F2FS_XATTR_INDEX_TRUSTED:
40 if (!capable(CAP_SYS_ADMIN))
41 return -EPERM;
42 break;
43 case F2FS_XATTR_INDEX_SECURITY:
44 break;
45 default:
46 return -EINVAL;
48 return f2fs_getxattr(inode, handler->flags, name,
49 buffer, size, NULL);
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))
62 return -EOPNOTSUPP;
63 break;
64 case F2FS_XATTR_INDEX_TRUSTED:
65 if (!capable(CAP_SYS_ADMIN))
66 return -EPERM;
67 break;
68 case F2FS_XATTR_INDEX_SECURITY:
69 break;
70 default:
71 return -EINVAL;
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)
93 if (buffer)
94 *((char *)buffer) = F2FS_I(inode)->i_advise;
95 return sizeof(char);
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))
104 return -EPERM;
105 if (value == NULL)
106 return -EINVAL;
108 F2FS_I(inode)->i_advise |= *(char *)value;
109 f2fs_mark_inode_dirty_sync(inode, true);
110 return 0;
113 #ifdef CONFIG_F2FS_FS_SECURITY
114 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
115 void *page)
117 const struct xattr *xattr;
118 int err = 0;
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);
124 if (err < 0)
125 break;
127 return err;
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);
136 #endif
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,
173 #endif
174 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
175 #ifdef CONFIG_F2FS_FS_SECURITY
176 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
177 #endif
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,
186 #endif
187 &f2fs_xattr_trusted_handler,
188 #ifdef CONFIG_F2FS_FS_SECURITY
189 &f2fs_xattr_security_handler,
190 #endif
191 &f2fs_xattr_advise_handler,
192 NULL,
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];
201 return handler;
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)
211 continue;
212 if (entry->e_name_len != len)
213 continue;
214 if (!memcmp(entry->e_name, name, len))
215 break;
217 return entry;
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) {
231 *last_addr = entry;
232 return NULL;
234 if (entry->e_name_index != index)
235 continue;
236 if (entry->e_name_len != len)
237 continue;
238 if (!memcmp(entry->e_name, name, len))
239 break;
241 return entry;
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,
247 void **base_addr)
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 = inline_xattr_size(inode);
254 int err = 0;
256 if (!size && !inline_size)
257 return -ENODATA;
259 txattr_addr = kzalloc(inline_size + size + XATTR_PADDING_SIZE,
260 GFP_F2FS_ZERO);
261 if (!txattr_addr)
262 return -ENOMEM;
264 /* read from inline xattr */
265 if (inline_size) {
266 struct page *page = NULL;
267 void *inline_addr;
269 if (ipage) {
270 inline_addr = inline_xattr_addr(ipage);
271 } else {
272 page = get_node_page(sbi, inode->i_ino);
273 if (IS_ERR(page)) {
274 err = PTR_ERR(page);
275 goto out;
277 inline_addr = inline_xattr_addr(page);
279 memcpy(txattr_addr, inline_addr, inline_size);
280 f2fs_put_page(page, 1);
282 *xe = __find_inline_xattr(txattr_addr, &last_addr,
283 index, len, name);
284 if (*xe)
285 goto check;
288 /* read from xattr node block */
289 if (xnid) {
290 struct page *xpage;
291 void *xattr_addr;
293 /* The inode already has an extended attribute block. */
294 xpage = get_node_page(sbi, xnid);
295 if (IS_ERR(xpage)) {
296 err = PTR_ERR(xpage);
297 goto out;
300 xattr_addr = page_address(xpage);
301 memcpy(txattr_addr + inline_size, xattr_addr, size);
302 f2fs_put_page(xpage, 1);
305 if (last_addr)
306 cur_addr = XATTR_HDR(last_addr) - 1;
307 else
308 cur_addr = txattr_addr;
310 *xe = __find_xattr(cur_addr, index, len, name);
311 check:
312 if (IS_XATTR_LAST_ENTRY(*xe)) {
313 err = -ENODATA;
314 goto out;
317 *base_addr = txattr_addr;
318 return 0;
319 out:
320 kzfree(txattr_addr);
321 return err;
324 static int read_all_xattrs(struct inode *inode, struct page *ipage,
325 void **base_addr)
327 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
328 struct f2fs_xattr_header *header;
329 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
330 unsigned int size = VALID_XATTR_BLOCK_SIZE;
331 unsigned int inline_size = inline_xattr_size(inode);
332 void *txattr_addr;
333 int err;
335 txattr_addr = kzalloc(inline_size + size + XATTR_PADDING_SIZE,
336 GFP_F2FS_ZERO);
337 if (!txattr_addr)
338 return -ENOMEM;
340 /* read from inline xattr */
341 if (inline_size) {
342 struct page *page = NULL;
343 void *inline_addr;
345 if (ipage) {
346 inline_addr = inline_xattr_addr(ipage);
347 } else {
348 page = get_node_page(sbi, inode->i_ino);
349 if (IS_ERR(page)) {
350 err = PTR_ERR(page);
351 goto fail;
353 inline_addr = inline_xattr_addr(page);
355 memcpy(txattr_addr, inline_addr, inline_size);
356 f2fs_put_page(page, 1);
359 /* read from xattr node block */
360 if (xnid) {
361 struct page *xpage;
362 void *xattr_addr;
364 /* The inode already has an extended attribute block. */
365 xpage = get_node_page(sbi, xnid);
366 if (IS_ERR(xpage)) {
367 err = PTR_ERR(xpage);
368 goto fail;
371 xattr_addr = page_address(xpage);
372 memcpy(txattr_addr + inline_size, xattr_addr, size);
373 f2fs_put_page(xpage, 1);
376 header = XATTR_HDR(txattr_addr);
378 /* never been allocated xattrs */
379 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
380 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
381 header->h_refcount = cpu_to_le32(1);
383 *base_addr = txattr_addr;
384 return 0;
385 fail:
386 kzfree(txattr_addr);
387 return err;
390 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
391 void *txattr_addr, struct page *ipage)
393 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
394 size_t inline_size = inline_xattr_size(inode);
395 void *xattr_addr;
396 struct page *xpage;
397 nid_t new_nid = 0;
398 int err;
400 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
401 if (!alloc_nid(sbi, &new_nid))
402 return -ENOSPC;
404 /* write to inline xattr */
405 if (inline_size) {
406 struct page *page = NULL;
407 void *inline_addr;
409 if (ipage) {
410 inline_addr = inline_xattr_addr(ipage);
411 f2fs_wait_on_page_writeback(ipage, NODE, true);
412 set_page_dirty(ipage);
413 } else {
414 page = get_node_page(sbi, inode->i_ino);
415 if (IS_ERR(page)) {
416 alloc_nid_failed(sbi, new_nid);
417 return PTR_ERR(page);
419 inline_addr = inline_xattr_addr(page);
420 f2fs_wait_on_page_writeback(page, NODE, true);
422 memcpy(inline_addr, txattr_addr, inline_size);
423 f2fs_put_page(page, 1);
425 /* no need to use xattr node block */
426 if (hsize <= inline_size) {
427 err = truncate_xattr_node(inode, ipage);
428 alloc_nid_failed(sbi, new_nid);
429 return err;
433 /* write to xattr node block */
434 if (F2FS_I(inode)->i_xattr_nid) {
435 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
436 if (IS_ERR(xpage)) {
437 alloc_nid_failed(sbi, new_nid);
438 return PTR_ERR(xpage);
440 f2fs_bug_on(sbi, new_nid);
441 f2fs_wait_on_page_writeback(xpage, NODE, true);
442 } else {
443 struct dnode_of_data dn;
444 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
445 xpage = new_node_page(&dn, XATTR_NODE_OFFSET);
446 if (IS_ERR(xpage)) {
447 alloc_nid_failed(sbi, new_nid);
448 return PTR_ERR(xpage);
450 alloc_nid_done(sbi, new_nid);
453 xattr_addr = page_address(xpage);
454 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
455 set_page_dirty(xpage);
456 f2fs_put_page(xpage, 1);
458 return 0;
461 int f2fs_getxattr(struct inode *inode, int index, const char *name,
462 void *buffer, size_t buffer_size, struct page *ipage)
464 struct f2fs_xattr_entry *entry = NULL;
465 int error = 0;
466 unsigned int size, len;
467 void *base_addr = NULL;
469 if (name == NULL)
470 return -EINVAL;
472 len = strlen(name);
473 if (len > F2FS_NAME_LEN)
474 return -ERANGE;
476 down_read(&F2FS_I(inode)->i_xattr_sem);
477 error = lookup_all_xattrs(inode, ipage, index, len, name,
478 &entry, &base_addr);
479 up_read(&F2FS_I(inode)->i_xattr_sem);
480 if (error)
481 return error;
483 size = le16_to_cpu(entry->e_value_size);
485 if (buffer && size > buffer_size) {
486 error = -ERANGE;
487 goto out;
490 if (buffer) {
491 char *pval = entry->e_name + entry->e_name_len;
492 memcpy(buffer, pval, size);
494 error = size;
495 out:
496 kzfree(base_addr);
497 return error;
500 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
502 struct inode *inode = d_inode(dentry);
503 struct f2fs_xattr_entry *entry;
504 void *base_addr;
505 int error = 0;
506 size_t rest = buffer_size;
508 down_read(&F2FS_I(inode)->i_xattr_sem);
509 error = read_all_xattrs(inode, NULL, &base_addr);
510 up_read(&F2FS_I(inode)->i_xattr_sem);
511 if (error)
512 return error;
514 list_for_each_xattr(entry, base_addr) {
515 const struct xattr_handler *handler =
516 f2fs_xattr_handler(entry->e_name_index);
517 const char *prefix;
518 size_t prefix_len;
519 size_t size;
521 if (!handler || (handler->list && !handler->list(dentry)))
522 continue;
524 prefix = handler->prefix ?: handler->name;
525 prefix_len = strlen(prefix);
526 size = prefix_len + entry->e_name_len + 1;
527 if (buffer) {
528 if (size > rest) {
529 error = -ERANGE;
530 goto cleanup;
532 memcpy(buffer, prefix, prefix_len);
533 buffer += prefix_len;
534 memcpy(buffer, entry->e_name, entry->e_name_len);
535 buffer += entry->e_name_len;
536 *buffer++ = 0;
538 rest -= size;
540 error = buffer_size - rest;
541 cleanup:
542 kzfree(base_addr);
543 return error;
546 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
547 const void *value, size_t size)
549 void *pval = entry->e_name + entry->e_name_len;
551 return (le16_to_cpu(entry->e_value_size) == size) &&
552 !memcmp(pval, value, size);
555 static int __f2fs_setxattr(struct inode *inode, int index,
556 const char *name, const void *value, size_t size,
557 struct page *ipage, int flags)
559 struct f2fs_xattr_entry *here, *last;
560 void *base_addr;
561 int found, newsize;
562 size_t len;
563 __u32 new_hsize;
564 int error = 0;
566 if (name == NULL)
567 return -EINVAL;
569 if (value == NULL)
570 size = 0;
572 len = strlen(name);
574 if (len > F2FS_NAME_LEN)
575 return -ERANGE;
577 if (size > MAX_VALUE_LEN(inode))
578 return -E2BIG;
580 error = read_all_xattrs(inode, ipage, &base_addr);
581 if (error)
582 return error;
584 /* find entry with wanted name. */
585 here = __find_xattr(base_addr, index, len, name);
587 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
589 if (found) {
590 if ((flags & XATTR_CREATE)) {
591 error = -EEXIST;
592 goto exit;
595 if (f2fs_xattr_value_same(here, value, size))
596 goto exit;
597 } else if ((flags & XATTR_REPLACE)) {
598 error = -ENODATA;
599 goto exit;
602 last = here;
603 while (!IS_XATTR_LAST_ENTRY(last))
604 last = XATTR_NEXT_ENTRY(last);
606 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
608 /* 1. Check space */
609 if (value) {
610 int free;
612 * If value is NULL, it is remove operation.
613 * In case of update operation, we calculate free.
615 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
616 if (found)
617 free = free + ENTRY_SIZE(here);
619 if (unlikely(free < newsize)) {
620 error = -E2BIG;
621 goto exit;
625 /* 2. Remove old entry */
626 if (found) {
628 * If entry is found, remove old entry.
629 * If not found, remove operation is not needed.
631 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
632 int oldsize = ENTRY_SIZE(here);
634 memmove(here, next, (char *)last - (char *)next);
635 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
636 memset(last, 0, oldsize);
639 new_hsize = (char *)last - (char *)base_addr;
641 /* 3. Write new entry */
642 if (value) {
643 char *pval;
645 * Before we come here, old entry is removed.
646 * We just write new entry.
648 last->e_name_index = index;
649 last->e_name_len = len;
650 memcpy(last->e_name, name, len);
651 pval = last->e_name + len;
652 memcpy(pval, value, size);
653 last->e_value_size = cpu_to_le16(size);
654 new_hsize += newsize;
657 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
658 if (error)
659 goto exit;
661 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
662 inode->i_mode = F2FS_I(inode)->i_acl_mode;
663 inode->i_ctime = current_time(inode);
664 clear_inode_flag(inode, FI_ACL_MODE);
666 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
667 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
668 f2fs_set_encrypted_inode(inode);
669 f2fs_mark_inode_dirty_sync(inode, true);
670 if (!error && S_ISDIR(inode->i_mode))
671 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
672 exit:
673 kzfree(base_addr);
674 return error;
677 int f2fs_setxattr(struct inode *inode, int index, const char *name,
678 const void *value, size_t size,
679 struct page *ipage, int flags)
681 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
682 int err;
684 /* this case is only from init_inode_metadata */
685 if (ipage)
686 return __f2fs_setxattr(inode, index, name, value,
687 size, ipage, flags);
688 f2fs_balance_fs(sbi, true);
690 f2fs_lock_op(sbi);
691 /* protect xattr_ver */
692 down_write(&F2FS_I(inode)->i_sem);
693 down_write(&F2FS_I(inode)->i_xattr_sem);
694 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
695 up_write(&F2FS_I(inode)->i_xattr_sem);
696 up_write(&F2FS_I(inode)->i_sem);
697 f2fs_unlock_op(sbi);
699 f2fs_update_time(sbi, REQ_TIME);
700 return err;