Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / fs / f2fs / xattr.c
blob06a72dc0191a022049bbf48ad26e127bb6211c02
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 *dentry, const char *name, void *buffer,
30 size_t size)
32 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_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(d_inode(dentry), handler->flags, name,
49 buffer, size, NULL);
52 static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
53 struct dentry *dentry, const char *name, const void *value,
54 size_t size, int flags)
56 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
58 switch (handler->flags) {
59 case F2FS_XATTR_INDEX_USER:
60 if (!test_opt(sbi, XATTR_USER))
61 return -EOPNOTSUPP;
62 break;
63 case F2FS_XATTR_INDEX_TRUSTED:
64 if (!capable(CAP_SYS_ADMIN))
65 return -EPERM;
66 break;
67 case F2FS_XATTR_INDEX_SECURITY:
68 break;
69 default:
70 return -EINVAL;
72 return f2fs_setxattr(d_inode(dentry), handler->flags, name,
73 value, size, NULL, flags);
76 static bool f2fs_xattr_user_list(struct dentry *dentry)
78 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
80 return test_opt(sbi, XATTR_USER);
83 static bool f2fs_xattr_trusted_list(struct dentry *dentry)
85 return capable(CAP_SYS_ADMIN);
88 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
89 struct dentry *dentry, const char *name, void *buffer,
90 size_t size)
92 struct inode *inode = d_inode(dentry);
94 if (buffer)
95 *((char *)buffer) = F2FS_I(inode)->i_advise;
96 return sizeof(char);
99 static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
100 struct dentry *dentry, const char *name, const void *value,
101 size_t size, int flags)
103 struct inode *inode = d_inode(dentry);
105 if (!inode_owner_or_capable(inode))
106 return -EPERM;
107 if (value == NULL)
108 return -EINVAL;
110 F2FS_I(inode)->i_advise |= *(char *)value;
111 mark_inode_dirty(inode);
112 return 0;
115 #ifdef CONFIG_F2FS_FS_SECURITY
116 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
117 void *page)
119 const struct xattr *xattr;
120 int err = 0;
122 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
123 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
124 xattr->name, xattr->value,
125 xattr->value_len, (struct page *)page, 0);
126 if (err < 0)
127 break;
129 return err;
132 int f2fs_init_security(struct inode *inode, struct inode *dir,
133 const struct qstr *qstr, struct page *ipage)
135 return security_inode_init_security(inode, dir, qstr,
136 &f2fs_initxattrs, ipage);
138 #endif
140 const struct xattr_handler f2fs_xattr_user_handler = {
141 .prefix = XATTR_USER_PREFIX,
142 .flags = F2FS_XATTR_INDEX_USER,
143 .list = f2fs_xattr_user_list,
144 .get = f2fs_xattr_generic_get,
145 .set = f2fs_xattr_generic_set,
148 const struct xattr_handler f2fs_xattr_trusted_handler = {
149 .prefix = XATTR_TRUSTED_PREFIX,
150 .flags = F2FS_XATTR_INDEX_TRUSTED,
151 .list = f2fs_xattr_trusted_list,
152 .get = f2fs_xattr_generic_get,
153 .set = f2fs_xattr_generic_set,
156 const struct xattr_handler f2fs_xattr_advise_handler = {
157 .name = F2FS_SYSTEM_ADVISE_NAME,
158 .flags = F2FS_XATTR_INDEX_ADVISE,
159 .get = f2fs_xattr_advise_get,
160 .set = f2fs_xattr_advise_set,
163 const struct xattr_handler f2fs_xattr_security_handler = {
164 .prefix = XATTR_SECURITY_PREFIX,
165 .flags = F2FS_XATTR_INDEX_SECURITY,
166 .get = f2fs_xattr_generic_get,
167 .set = f2fs_xattr_generic_set,
170 static const struct xattr_handler *f2fs_xattr_handler_map[] = {
171 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
172 #ifdef CONFIG_F2FS_FS_POSIX_ACL
173 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
174 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
175 #endif
176 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
177 #ifdef CONFIG_F2FS_FS_SECURITY
178 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
179 #endif
180 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
183 const struct xattr_handler *f2fs_xattr_handlers[] = {
184 &f2fs_xattr_user_handler,
185 #ifdef CONFIG_F2FS_FS_POSIX_ACL
186 &posix_acl_access_xattr_handler,
187 &posix_acl_default_xattr_handler,
188 #endif
189 &f2fs_xattr_trusted_handler,
190 #ifdef CONFIG_F2FS_FS_SECURITY
191 &f2fs_xattr_security_handler,
192 #endif
193 &f2fs_xattr_advise_handler,
194 NULL,
197 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
199 const struct xattr_handler *handler = NULL;
201 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
202 handler = f2fs_xattr_handler_map[index];
203 return handler;
206 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
207 size_t len, const char *name)
209 struct f2fs_xattr_entry *entry;
211 list_for_each_xattr(entry, base_addr) {
212 if (entry->e_name_index != index)
213 continue;
214 if (entry->e_name_len != len)
215 continue;
216 if (!memcmp(entry->e_name, name, len))
217 break;
219 return entry;
222 static void *read_all_xattrs(struct inode *inode, struct page *ipage)
224 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
225 struct f2fs_xattr_header *header;
226 size_t size = PAGE_SIZE, inline_size = 0;
227 void *txattr_addr;
229 inline_size = inline_xattr_size(inode);
231 txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
232 if (!txattr_addr)
233 return NULL;
235 /* read from inline xattr */
236 if (inline_size) {
237 struct page *page = NULL;
238 void *inline_addr;
240 if (ipage) {
241 inline_addr = inline_xattr_addr(ipage);
242 } else {
243 page = get_node_page(sbi, inode->i_ino);
244 if (IS_ERR(page))
245 goto fail;
246 inline_addr = inline_xattr_addr(page);
248 memcpy(txattr_addr, inline_addr, inline_size);
249 f2fs_put_page(page, 1);
252 /* read from xattr node block */
253 if (F2FS_I(inode)->i_xattr_nid) {
254 struct page *xpage;
255 void *xattr_addr;
257 /* The inode already has an extended attribute block. */
258 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
259 if (IS_ERR(xpage))
260 goto fail;
262 xattr_addr = page_address(xpage);
263 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
264 f2fs_put_page(xpage, 1);
267 header = XATTR_HDR(txattr_addr);
269 /* never been allocated xattrs */
270 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
271 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
272 header->h_refcount = cpu_to_le32(1);
274 return txattr_addr;
275 fail:
276 kzfree(txattr_addr);
277 return NULL;
280 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
281 void *txattr_addr, struct page *ipage)
283 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
284 size_t inline_size = 0;
285 void *xattr_addr;
286 struct page *xpage;
287 nid_t new_nid = 0;
288 int err;
290 inline_size = inline_xattr_size(inode);
292 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
293 if (!alloc_nid(sbi, &new_nid))
294 return -ENOSPC;
296 /* write to inline xattr */
297 if (inline_size) {
298 struct page *page = NULL;
299 void *inline_addr;
301 if (ipage) {
302 inline_addr = inline_xattr_addr(ipage);
303 f2fs_wait_on_page_writeback(ipage, NODE, true);
304 } else {
305 page = get_node_page(sbi, inode->i_ino);
306 if (IS_ERR(page)) {
307 alloc_nid_failed(sbi, new_nid);
308 return PTR_ERR(page);
310 inline_addr = inline_xattr_addr(page);
311 f2fs_wait_on_page_writeback(page, NODE, true);
313 memcpy(inline_addr, txattr_addr, inline_size);
314 f2fs_put_page(page, 1);
316 /* no need to use xattr node block */
317 if (hsize <= inline_size) {
318 err = truncate_xattr_node(inode, ipage);
319 alloc_nid_failed(sbi, new_nid);
320 return err;
324 /* write to xattr node block */
325 if (F2FS_I(inode)->i_xattr_nid) {
326 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
327 if (IS_ERR(xpage)) {
328 alloc_nid_failed(sbi, new_nid);
329 return PTR_ERR(xpage);
331 f2fs_bug_on(sbi, new_nid);
332 f2fs_wait_on_page_writeback(xpage, NODE, true);
333 } else {
334 struct dnode_of_data dn;
335 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
336 xpage = new_node_page(&dn, XATTR_NODE_OFFSET, ipage);
337 if (IS_ERR(xpage)) {
338 alloc_nid_failed(sbi, new_nid);
339 return PTR_ERR(xpage);
341 alloc_nid_done(sbi, new_nid);
344 xattr_addr = page_address(xpage);
345 memcpy(xattr_addr, txattr_addr + inline_size, PAGE_SIZE -
346 sizeof(struct node_footer));
347 set_page_dirty(xpage);
348 f2fs_put_page(xpage, 1);
350 /* need to checkpoint during fsync */
351 F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
352 return 0;
355 int f2fs_getxattr(struct inode *inode, int index, const char *name,
356 void *buffer, size_t buffer_size, struct page *ipage)
358 struct f2fs_xattr_entry *entry;
359 void *base_addr;
360 int error = 0;
361 size_t size, len;
363 if (name == NULL)
364 return -EINVAL;
366 len = strlen(name);
367 if (len > F2FS_NAME_LEN)
368 return -ERANGE;
370 base_addr = read_all_xattrs(inode, ipage);
371 if (!base_addr)
372 return -ENOMEM;
374 entry = __find_xattr(base_addr, index, len, name);
375 if (IS_XATTR_LAST_ENTRY(entry)) {
376 error = -ENODATA;
377 goto cleanup;
380 size = le16_to_cpu(entry->e_value_size);
382 if (buffer && size > buffer_size) {
383 error = -ERANGE;
384 goto cleanup;
387 if (buffer) {
388 char *pval = entry->e_name + entry->e_name_len;
389 memcpy(buffer, pval, size);
391 error = size;
393 cleanup:
394 kzfree(base_addr);
395 return error;
398 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
400 struct inode *inode = d_inode(dentry);
401 struct f2fs_xattr_entry *entry;
402 void *base_addr;
403 int error = 0;
404 size_t rest = buffer_size;
406 base_addr = read_all_xattrs(inode, NULL);
407 if (!base_addr)
408 return -ENOMEM;
410 list_for_each_xattr(entry, base_addr) {
411 const struct xattr_handler *handler =
412 f2fs_xattr_handler(entry->e_name_index);
413 const char *prefix;
414 size_t prefix_len;
415 size_t size;
417 if (!handler || (handler->list && !handler->list(dentry)))
418 continue;
420 prefix = handler->prefix ?: handler->name;
421 prefix_len = strlen(prefix);
422 size = prefix_len + entry->e_name_len + 1;
423 if (buffer) {
424 if (size > rest) {
425 error = -ERANGE;
426 goto cleanup;
428 memcpy(buffer, prefix, prefix_len);
429 buffer += prefix_len;
430 memcpy(buffer, entry->e_name, entry->e_name_len);
431 buffer += entry->e_name_len;
432 *buffer++ = 0;
434 rest -= size;
436 error = buffer_size - rest;
437 cleanup:
438 kzfree(base_addr);
439 return error;
442 static int __f2fs_setxattr(struct inode *inode, int index,
443 const char *name, const void *value, size_t size,
444 struct page *ipage, int flags)
446 struct f2fs_inode_info *fi = F2FS_I(inode);
447 struct f2fs_xattr_entry *here, *last;
448 void *base_addr;
449 int found, newsize;
450 size_t len;
451 __u32 new_hsize;
452 int error = -ENOMEM;
454 if (name == NULL)
455 return -EINVAL;
457 if (value == NULL)
458 size = 0;
460 len = strlen(name);
462 if (len > F2FS_NAME_LEN)
463 return -ERANGE;
465 if (size > MAX_VALUE_LEN(inode))
466 return -E2BIG;
468 base_addr = read_all_xattrs(inode, ipage);
469 if (!base_addr)
470 goto exit;
472 /* find entry with wanted name. */
473 here = __find_xattr(base_addr, index, len, name);
475 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
477 if ((flags & XATTR_REPLACE) && !found) {
478 error = -ENODATA;
479 goto exit;
480 } else if ((flags & XATTR_CREATE) && found) {
481 error = -EEXIST;
482 goto exit;
485 last = here;
486 while (!IS_XATTR_LAST_ENTRY(last))
487 last = XATTR_NEXT_ENTRY(last);
489 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
491 /* 1. Check space */
492 if (value) {
493 int free;
495 * If value is NULL, it is remove operation.
496 * In case of update operation, we calculate free.
498 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
499 if (found)
500 free = free + ENTRY_SIZE(here);
502 if (unlikely(free < newsize)) {
503 error = -ENOSPC;
504 goto exit;
508 /* 2. Remove old entry */
509 if (found) {
511 * If entry is found, remove old entry.
512 * If not found, remove operation is not needed.
514 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
515 int oldsize = ENTRY_SIZE(here);
517 memmove(here, next, (char *)last - (char *)next);
518 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
519 memset(last, 0, oldsize);
522 new_hsize = (char *)last - (char *)base_addr;
524 /* 3. Write new entry */
525 if (value) {
526 char *pval;
528 * Before we come here, old entry is removed.
529 * We just write new entry.
531 memset(last, 0, newsize);
532 last->e_name_index = index;
533 last->e_name_len = len;
534 memcpy(last->e_name, name, len);
535 pval = last->e_name + len;
536 memcpy(pval, value, size);
537 last->e_value_size = cpu_to_le16(size);
538 new_hsize += newsize;
541 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
542 if (error)
543 goto exit;
545 if (is_inode_flag_set(fi, FI_ACL_MODE)) {
546 inode->i_mode = fi->i_acl_mode;
547 inode->i_ctime = CURRENT_TIME;
548 clear_inode_flag(fi, FI_ACL_MODE);
550 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
551 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
552 f2fs_set_encrypted_inode(inode);
554 if (ipage)
555 update_inode(inode, ipage);
556 else
557 update_inode_page(inode);
558 exit:
559 kzfree(base_addr);
560 return error;
563 int f2fs_setxattr(struct inode *inode, int index, const char *name,
564 const void *value, size_t size,
565 struct page *ipage, int flags)
567 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
568 int err;
570 /* this case is only from init_inode_metadata */
571 if (ipage)
572 return __f2fs_setxattr(inode, index, name, value,
573 size, ipage, flags);
574 f2fs_balance_fs(sbi, true);
576 f2fs_lock_op(sbi);
577 /* protect xattr_ver */
578 down_write(&F2FS_I(inode)->i_sem);
579 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
580 up_write(&F2FS_I(inode)->i_sem);
581 f2fs_unlock_op(sbi);
583 f2fs_update_time(sbi, REQ_TIME);
584 return err;