xattr handlers: Pass handler to operations instead of flags
[firefly-linux-kernel-4.4.55.git] / fs / f2fs / xattr.c
1 /*
2  * fs/f2fs/xattr.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5  *             http://www.samsung.com/
6  *
7  * Portions of this code from linux/fs/ext2/xattr.c
8  *
9  * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
10  *
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.
16  *
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.
20  */
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"
27
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)
31 {
32         struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
33         int total_len, prefix_len = 0;
34         const char *prefix = NULL;
35
36         switch (handler->flags) {
37         case F2FS_XATTR_INDEX_USER:
38                 if (!test_opt(sbi, XATTR_USER))
39                         return -EOPNOTSUPP;
40                 prefix = XATTR_USER_PREFIX;
41                 prefix_len = XATTR_USER_PREFIX_LEN;
42                 break;
43         case F2FS_XATTR_INDEX_TRUSTED:
44                 if (!capable(CAP_SYS_ADMIN))
45                         return -EPERM;
46                 prefix = XATTR_TRUSTED_PREFIX;
47                 prefix_len = XATTR_TRUSTED_PREFIX_LEN;
48                 break;
49         case F2FS_XATTR_INDEX_SECURITY:
50                 prefix = XATTR_SECURITY_PREFIX;
51                 prefix_len = XATTR_SECURITY_PREFIX_LEN;
52                 break;
53         default:
54                 return -EINVAL;
55         }
56
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';
62         }
63         return total_len;
64 }
65
66 static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
67                 struct dentry *dentry, const char *name, void *buffer,
68                 size_t size)
69 {
70         struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
71
72         switch (handler->flags) {
73         case F2FS_XATTR_INDEX_USER:
74                 if (!test_opt(sbi, XATTR_USER))
75                         return -EOPNOTSUPP;
76                 break;
77         case F2FS_XATTR_INDEX_TRUSTED:
78                 if (!capable(CAP_SYS_ADMIN))
79                         return -EPERM;
80                 break;
81         case F2FS_XATTR_INDEX_SECURITY:
82                 break;
83         default:
84                 return -EINVAL;
85         }
86         if (strcmp(name, "") == 0)
87                 return -EINVAL;
88         return f2fs_getxattr(d_inode(dentry), handler->flags, name,
89                              buffer, size, NULL);
90 }
91
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)
95 {
96         struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
97
98         switch (handler->flags) {
99         case F2FS_XATTR_INDEX_USER:
100                 if (!test_opt(sbi, XATTR_USER))
101                         return -EOPNOTSUPP;
102                 break;
103         case F2FS_XATTR_INDEX_TRUSTED:
104                 if (!capable(CAP_SYS_ADMIN))
105                         return -EPERM;
106                 break;
107         case F2FS_XATTR_INDEX_SECURITY:
108                 break;
109         default:
110                 return -EINVAL;
111         }
112         if (strcmp(name, "") == 0)
113                 return -EINVAL;
114
115         return f2fs_setxattr(d_inode(dentry), handler->flags, name,
116                                         value, size, NULL, flags);
117 }
118
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)
122 {
123         const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
124         size_t size;
125
126         if (handler->flags != F2FS_XATTR_INDEX_ADVISE)
127                 return 0;
128
129         size = strlen(xname) + 1;
130         if (list && size <= list_size)
131                 memcpy(list, xname, size);
132         return size;
133 }
134
135 static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
136                 struct dentry *dentry, const char *name, void *buffer,
137                 size_t size)
138 {
139         struct inode *inode = d_inode(dentry);
140
141         if (strcmp(name, "") != 0)
142                 return -EINVAL;
143
144         if (buffer)
145                 *((char *)buffer) = F2FS_I(inode)->i_advise;
146         return sizeof(char);
147 }
148
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)
152 {
153         struct inode *inode = d_inode(dentry);
154
155         if (strcmp(name, "") != 0)
156                 return -EINVAL;
157         if (!inode_owner_or_capable(inode))
158                 return -EPERM;
159         if (value == NULL)
160                 return -EINVAL;
161
162         F2FS_I(inode)->i_advise |= *(char *)value;
163         mark_inode_dirty(inode);
164         return 0;
165 }
166
167 #ifdef CONFIG_F2FS_FS_SECURITY
168 static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
169                 void *page)
170 {
171         const struct xattr *xattr;
172         int err = 0;
173
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);
178                 if (err < 0)
179                         break;
180         }
181         return err;
182 }
183
184 int f2fs_init_security(struct inode *inode, struct inode *dir,
185                                 const struct qstr *qstr, struct page *ipage)
186 {
187         return security_inode_init_security(inode, dir, qstr,
188                                 &f2fs_initxattrs, ipage);
189 }
190 #endif
191
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,
198 };
199
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,
206 };
207
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,
214 };
215
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,
222 };
223
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,
229 #endif
230         [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
231 #ifdef CONFIG_F2FS_FS_SECURITY
232         [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
233 #endif
234         [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
235 };
236
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,
242 #endif
243         &f2fs_xattr_trusted_handler,
244 #ifdef CONFIG_F2FS_FS_SECURITY
245         &f2fs_xattr_security_handler,
246 #endif
247         &f2fs_xattr_advise_handler,
248         NULL,
249 };
250
251 static inline const struct xattr_handler *f2fs_xattr_handler(int index)
252 {
253         const struct xattr_handler *handler = NULL;
254
255         if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
256                 handler = f2fs_xattr_handler_map[index];
257         return handler;
258 }
259
260 static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
261                                         size_t len, const char *name)
262 {
263         struct f2fs_xattr_entry *entry;
264
265         list_for_each_xattr(entry, base_addr) {
266                 if (entry->e_name_index != index)
267                         continue;
268                 if (entry->e_name_len != len)
269                         continue;
270                 if (!memcmp(entry->e_name, name, len))
271                         break;
272         }
273         return entry;
274 }
275
276 static void *read_all_xattrs(struct inode *inode, struct page *ipage)
277 {
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;
281         void *txattr_addr;
282
283         inline_size = inline_xattr_size(inode);
284
285         txattr_addr = kzalloc(inline_size + size, GFP_F2FS_ZERO);
286         if (!txattr_addr)
287                 return NULL;
288
289         /* read from inline xattr */
290         if (inline_size) {
291                 struct page *page = NULL;
292                 void *inline_addr;
293
294                 if (ipage) {
295                         inline_addr = inline_xattr_addr(ipage);
296                 } else {
297                         page = get_node_page(sbi, inode->i_ino);
298                         if (IS_ERR(page))
299                                 goto fail;
300                         inline_addr = inline_xattr_addr(page);
301                 }
302                 memcpy(txattr_addr, inline_addr, inline_size);
303                 f2fs_put_page(page, 1);
304         }
305
306         /* read from xattr node block */
307         if (F2FS_I(inode)->i_xattr_nid) {
308                 struct page *xpage;
309                 void *xattr_addr;
310
311                 /* The inode already has an extended attribute block. */
312                 xpage = get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
313                 if (IS_ERR(xpage))
314                         goto fail;
315
316                 xattr_addr = page_address(xpage);
317                 memcpy(txattr_addr + inline_size, xattr_addr, PAGE_SIZE);
318                 f2fs_put_page(xpage, 1);
319         }
320
321         header = XATTR_HDR(txattr_addr);
322
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);
327         }
328         return txattr_addr;
329 fail:
330         kzfree(txattr_addr);
331         return NULL;
332 }
333
334 static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
335                                 void *txattr_addr, struct page *ipage)
336 {
337         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
338         size_t inline_size = 0;
339         void *xattr_addr;
340         struct page *xpage;
341         nid_t new_nid = 0;
342         int err;
343
344         inline_size = inline_xattr_size(inode);
345
346         if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
347                 if (!alloc_nid(sbi, &new_nid))
348                         return -ENOSPC;
349
350         /* write to inline xattr */
351         if (inline_size) {
352                 struct page *page = NULL;
353                 void *inline_addr;
354
355                 if (ipage) {
356                         inline_addr = inline_xattr_addr(ipage);
357                         f2fs_wait_on_page_writeback(ipage, NODE);
358                 } else {
359                         page = get_node_page(sbi, inode->i_ino);
360                         if (IS_ERR(page)) {
361                                 alloc_nid_failed(sbi, new_nid);
362                                 return PTR_ERR(page);
363                         }
364                         inline_addr = inline_xattr_addr(page);
365                         f2fs_wait_on_page_writeback(page, NODE);
366                 }
367                 memcpy(inline_addr, txattr_addr, inline_size);
368                 f2fs_put_page(page, 1);
369
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);
374                         return err;
375                 }
376         }
377
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);
381                 if (IS_ERR(xpage)) {
382                         alloc_nid_failed(sbi, new_nid);
383                         return PTR_ERR(xpage);
384                 }
385                 f2fs_bug_on(sbi, new_nid);
386                 f2fs_wait_on_page_writeback(xpage, NODE);
387         } else {
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);
391                 if (IS_ERR(xpage)) {
392                         alloc_nid_failed(sbi, new_nid);
393                         return PTR_ERR(xpage);
394                 }
395                 alloc_nid_done(sbi, new_nid);
396         }
397
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);
403
404         /* need to checkpoint during fsync */
405         F2FS_I(inode)->xattr_ver = cur_cp_version(F2FS_CKPT(sbi));
406         return 0;
407 }
408
409 int f2fs_getxattr(struct inode *inode, int index, const char *name,
410                 void *buffer, size_t buffer_size, struct page *ipage)
411 {
412         struct f2fs_xattr_entry *entry;
413         void *base_addr;
414         int error = 0;
415         size_t size, len;
416
417         if (name == NULL)
418                 return -EINVAL;
419
420         len = strlen(name);
421         if (len > F2FS_NAME_LEN)
422                 return -ERANGE;
423
424         base_addr = read_all_xattrs(inode, ipage);
425         if (!base_addr)
426                 return -ENOMEM;
427
428         entry = __find_xattr(base_addr, index, len, name);
429         if (IS_XATTR_LAST_ENTRY(entry)) {
430                 error = -ENODATA;
431                 goto cleanup;
432         }
433
434         size = le16_to_cpu(entry->e_value_size);
435
436         if (buffer && size > buffer_size) {
437                 error = -ERANGE;
438                 goto cleanup;
439         }
440
441         if (buffer) {
442                 char *pval = entry->e_name + entry->e_name_len;
443                 memcpy(buffer, pval, size);
444         }
445         error = size;
446
447 cleanup:
448         kzfree(base_addr);
449         return error;
450 }
451
452 ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
453 {
454         struct inode *inode = d_inode(dentry);
455         struct f2fs_xattr_entry *entry;
456         void *base_addr;
457         int error = 0;
458         size_t rest = buffer_size;
459
460         base_addr = read_all_xattrs(inode, NULL);
461         if (!base_addr)
462                 return -ENOMEM;
463
464         list_for_each_xattr(entry, base_addr) {
465                 const struct xattr_handler *handler =
466                         f2fs_xattr_handler(entry->e_name_index);
467                 size_t size;
468
469                 if (!handler)
470                         continue;
471
472                 size = handler->list(handler, dentry, buffer, rest,
473                                      entry->e_name, entry->e_name_len);
474                 if (buffer && size > rest) {
475                         error = -ERANGE;
476                         goto cleanup;
477                 }
478
479                 if (buffer)
480                         buffer += size;
481                 rest -= size;
482         }
483         error = buffer_size - rest;
484 cleanup:
485         kzfree(base_addr);
486         return error;
487 }
488
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)
492 {
493         struct f2fs_inode_info *fi = F2FS_I(inode);
494         struct f2fs_xattr_entry *here, *last;
495         void *base_addr;
496         int found, newsize;
497         size_t len;
498         __u32 new_hsize;
499         int error = -ENOMEM;
500
501         if (name == NULL)
502                 return -EINVAL;
503
504         if (value == NULL)
505                 size = 0;
506
507         len = strlen(name);
508
509         if (len > F2FS_NAME_LEN)
510                 return -ERANGE;
511
512         if (size > MAX_VALUE_LEN(inode))
513                 return -E2BIG;
514
515         base_addr = read_all_xattrs(inode, ipage);
516         if (!base_addr)
517                 goto exit;
518
519         /* find entry with wanted name. */
520         here = __find_xattr(base_addr, index, len, name);
521
522         found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
523
524         if ((flags & XATTR_REPLACE) && !found) {
525                 error = -ENODATA;
526                 goto exit;
527         } else if ((flags & XATTR_CREATE) && found) {
528                 error = -EEXIST;
529                 goto exit;
530         }
531
532         last = here;
533         while (!IS_XATTR_LAST_ENTRY(last))
534                 last = XATTR_NEXT_ENTRY(last);
535
536         newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
537
538         /* 1. Check space */
539         if (value) {
540                 int free;
541                 /*
542                  * If value is NULL, it is remove operation.
543                  * In case of update operation, we calculate free.
544                  */
545                 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
546                 if (found)
547                         free = free + ENTRY_SIZE(here);
548
549                 if (unlikely(free < newsize)) {
550                         error = -ENOSPC;
551                         goto exit;
552                 }
553         }
554
555         /* 2. Remove old entry */
556         if (found) {
557                 /*
558                  * If entry is found, remove old entry.
559                  * If not found, remove operation is not needed.
560                  */
561                 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
562                 int oldsize = ENTRY_SIZE(here);
563
564                 memmove(here, next, (char *)last - (char *)next);
565                 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
566                 memset(last, 0, oldsize);
567         }
568
569         new_hsize = (char *)last - (char *)base_addr;
570
571         /* 3. Write new entry */
572         if (value) {
573                 char *pval;
574                 /*
575                  * Before we come here, old entry is removed.
576                  * We just write new entry.
577                  */
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;
586         }
587
588         error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
589         if (error)
590                 goto exit;
591
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);
596         }
597         if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
598                         !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
599                 f2fs_set_encrypted_inode(inode);
600
601         if (ipage)
602                 update_inode(inode, ipage);
603         else
604                 update_inode_page(inode);
605 exit:
606         kzfree(base_addr);
607         return error;
608 }
609
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)
613 {
614         struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
615         int err;
616
617         /* this case is only from init_inode_metadata */
618         if (ipage)
619                 return __f2fs_setxattr(inode, index, name, value,
620                                                 size, ipage, flags);
621         f2fs_balance_fs(sbi);
622
623         f2fs_lock_op(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);
628         f2fs_unlock_op(sbi);
629
630         return err;
631 }