1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/rwsem.h>
19#include <linux/f2fs_fs.h>
20#include <linux/security.h>
21#include <linux/posix_acl_xattr.h>
22#include "f2fs.h"
23#include "xattr.h"
24
25static int f2fs_xattr_generic_get(const struct xattr_handler *handler,
26 struct dentry *unused, struct inode *inode,
27 const char *name, void *buffer, size_t size)
28{
29 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
30
31 switch (handler->flags) {
32 case F2FS_XATTR_INDEX_USER:
33 if (!test_opt(sbi, XATTR_USER))
34 return -EOPNOTSUPP;
35 break;
36 case F2FS_XATTR_INDEX_TRUSTED:
37 case F2FS_XATTR_INDEX_SECURITY:
38 break;
39 default:
40 return -EINVAL;
41 }
42 return f2fs_getxattr(inode, handler->flags, name,
43 buffer, size, NULL);
44}
45
46static int f2fs_xattr_generic_set(const struct xattr_handler *handler,
47 struct dentry *unused, struct inode *inode,
48 const char *name, const void *value,
49 size_t size, int flags)
50{
51 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
52
53 switch (handler->flags) {
54 case F2FS_XATTR_INDEX_USER:
55 if (!test_opt(sbi, XATTR_USER))
56 return -EOPNOTSUPP;
57 break;
58 case F2FS_XATTR_INDEX_TRUSTED:
59 case F2FS_XATTR_INDEX_SECURITY:
60 break;
61 default:
62 return -EINVAL;
63 }
64 return f2fs_setxattr(inode, handler->flags, name,
65 value, size, NULL, flags);
66}
67
68static bool f2fs_xattr_user_list(struct dentry *dentry)
69{
70 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
71
72 return test_opt(sbi, XATTR_USER);
73}
74
75static bool f2fs_xattr_trusted_list(struct dentry *dentry)
76{
77 return capable(CAP_SYS_ADMIN);
78}
79
80static int f2fs_xattr_advise_get(const struct xattr_handler *handler,
81 struct dentry *unused, struct inode *inode,
82 const char *name, void *buffer, size_t size)
83{
84 if (buffer)
85 *((char *)buffer) = F2FS_I(inode)->i_advise;
86 return sizeof(char);
87}
88
89static int f2fs_xattr_advise_set(const struct xattr_handler *handler,
90 struct dentry *unused, struct inode *inode,
91 const char *name, const void *value,
92 size_t size, int flags)
93{
94 unsigned char old_advise = F2FS_I(inode)->i_advise;
95 unsigned char new_advise;
96
97 if (!inode_owner_or_capable(inode))
98 return -EPERM;
99 if (value == NULL)
100 return -EINVAL;
101
102 new_advise = *(char *)value;
103 if (new_advise & ~FADVISE_MODIFIABLE_BITS)
104 return -EINVAL;
105
106 new_advise = new_advise & FADVISE_MODIFIABLE_BITS;
107 new_advise |= old_advise & ~FADVISE_MODIFIABLE_BITS;
108
109 F2FS_I(inode)->i_advise = new_advise;
110 f2fs_mark_inode_dirty_sync(inode, true);
111 return 0;
112}
113
114#ifdef CONFIG_F2FS_FS_SECURITY
115static int f2fs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
116 void *page)
117{
118 const struct xattr *xattr;
119 int err = 0;
120
121 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
122 err = f2fs_setxattr(inode, F2FS_XATTR_INDEX_SECURITY,
123 xattr->name, xattr->value,
124 xattr->value_len, (struct page *)page, 0);
125 if (err < 0)
126 break;
127 }
128 return err;
129}
130
131int f2fs_init_security(struct inode *inode, struct inode *dir,
132 const struct qstr *qstr, struct page *ipage)
133{
134 return security_inode_init_security(inode, dir, qstr,
135 &f2fs_initxattrs, ipage);
136}
137#endif
138
139const struct xattr_handler f2fs_xattr_user_handler = {
140 .prefix = XATTR_USER_PREFIX,
141 .flags = F2FS_XATTR_INDEX_USER,
142 .list = f2fs_xattr_user_list,
143 .get = f2fs_xattr_generic_get,
144 .set = f2fs_xattr_generic_set,
145};
146
147const struct xattr_handler f2fs_xattr_trusted_handler = {
148 .prefix = XATTR_TRUSTED_PREFIX,
149 .flags = F2FS_XATTR_INDEX_TRUSTED,
150 .list = f2fs_xattr_trusted_list,
151 .get = f2fs_xattr_generic_get,
152 .set = f2fs_xattr_generic_set,
153};
154
155const struct xattr_handler f2fs_xattr_advise_handler = {
156 .name = F2FS_SYSTEM_ADVISE_NAME,
157 .flags = F2FS_XATTR_INDEX_ADVISE,
158 .get = f2fs_xattr_advise_get,
159 .set = f2fs_xattr_advise_set,
160};
161
162const struct xattr_handler f2fs_xattr_security_handler = {
163 .prefix = XATTR_SECURITY_PREFIX,
164 .flags = F2FS_XATTR_INDEX_SECURITY,
165 .get = f2fs_xattr_generic_get,
166 .set = f2fs_xattr_generic_set,
167};
168
169static const struct xattr_handler *f2fs_xattr_handler_map[] = {
170 [F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
171#ifdef CONFIG_F2FS_FS_POSIX_ACL
172 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
173 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
174#endif
175 [F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
176#ifdef CONFIG_F2FS_FS_SECURITY
177 [F2FS_XATTR_INDEX_SECURITY] = &f2fs_xattr_security_handler,
178#endif
179 [F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
180};
181
182const struct xattr_handler *f2fs_xattr_handlers[] = {
183 &f2fs_xattr_user_handler,
184#ifdef CONFIG_F2FS_FS_POSIX_ACL
185 &posix_acl_access_xattr_handler,
186 &posix_acl_default_xattr_handler,
187#endif
188 &f2fs_xattr_trusted_handler,
189#ifdef CONFIG_F2FS_FS_SECURITY
190 &f2fs_xattr_security_handler,
191#endif
192 &f2fs_xattr_advise_handler,
193 NULL,
194};
195
196static inline const struct xattr_handler *f2fs_xattr_handler(int index)
197{
198 const struct xattr_handler *handler = NULL;
199
200 if (index > 0 && index < ARRAY_SIZE(f2fs_xattr_handler_map))
201 handler = f2fs_xattr_handler_map[index];
202 return handler;
203}
204
205static struct f2fs_xattr_entry *__find_xattr(void *base_addr, int index,
206 size_t len, const char *name)
207{
208 struct f2fs_xattr_entry *entry;
209
210 list_for_each_xattr(entry, base_addr) {
211 if (entry->e_name_index != index)
212 continue;
213 if (entry->e_name_len != len)
214 continue;
215 if (!memcmp(entry->e_name, name, len))
216 break;
217 }
218 return entry;
219}
220
221static struct f2fs_xattr_entry *__find_inline_xattr(struct inode *inode,
222 void *base_addr, void **last_addr, int index,
223 size_t len, const char *name)
224{
225 struct f2fs_xattr_entry *entry;
226 unsigned int inline_size = inline_xattr_size(inode);
227
228 list_for_each_xattr(entry, base_addr) {
229 if ((void *)entry + sizeof(__u32) > base_addr + inline_size ||
230 (void *)XATTR_NEXT_ENTRY(entry) + sizeof(__u32) >
231 base_addr + inline_size) {
232 *last_addr = entry;
233 return NULL;
234 }
235 if (entry->e_name_index != index)
236 continue;
237 if (entry->e_name_len != len)
238 continue;
239 if (!memcmp(entry->e_name, name, len))
240 break;
241 }
242 return entry;
243}
244
245static int read_inline_xattr(struct inode *inode, struct page *ipage,
246 void *txattr_addr)
247{
248 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
249 unsigned int inline_size = inline_xattr_size(inode);
250 struct page *page = NULL;
251 void *inline_addr;
252
253 if (ipage) {
254 inline_addr = inline_xattr_addr(inode, ipage);
255 } else {
256 page = f2fs_get_node_page(sbi, inode->i_ino);
257 if (IS_ERR(page))
258 return PTR_ERR(page);
259
260 inline_addr = inline_xattr_addr(inode, page);
261 }
262 memcpy(txattr_addr, inline_addr, inline_size);
263 f2fs_put_page(page, 1);
264
265 return 0;
266}
267
268static int read_xattr_block(struct inode *inode, void *txattr_addr)
269{
270 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
271 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
272 unsigned int inline_size = inline_xattr_size(inode);
273 struct page *xpage;
274 void *xattr_addr;
275
276
277 xpage = f2fs_get_node_page(sbi, xnid);
278 if (IS_ERR(xpage))
279 return PTR_ERR(xpage);
280
281 xattr_addr = page_address(xpage);
282 memcpy(txattr_addr + inline_size, xattr_addr, VALID_XATTR_BLOCK_SIZE);
283 f2fs_put_page(xpage, 1);
284
285 return 0;
286}
287
288static int lookup_all_xattrs(struct inode *inode, struct page *ipage,
289 unsigned int index, unsigned int len,
290 const char *name, struct f2fs_xattr_entry **xe,
291 void **base_addr)
292{
293 void *cur_addr, *txattr_addr, *last_addr = NULL;
294 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
295 unsigned int size = xnid ? VALID_XATTR_BLOCK_SIZE : 0;
296 unsigned int inline_size = inline_xattr_size(inode);
297 int err = 0;
298
299 if (!size && !inline_size)
300 return -ENODATA;
301
302 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
303 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
304 if (!txattr_addr)
305 return -ENOMEM;
306
307
308 if (inline_size) {
309 err = read_inline_xattr(inode, ipage, txattr_addr);
310 if (err)
311 goto out;
312
313 *xe = __find_inline_xattr(inode, txattr_addr, &last_addr,
314 index, len, name);
315 if (*xe)
316 goto check;
317 }
318
319
320 if (xnid) {
321 err = read_xattr_block(inode, txattr_addr);
322 if (err)
323 goto out;
324 }
325
326 if (last_addr)
327 cur_addr = XATTR_HDR(last_addr) - 1;
328 else
329 cur_addr = txattr_addr;
330
331 *xe = __find_xattr(cur_addr, index, len, name);
332check:
333 if (IS_XATTR_LAST_ENTRY(*xe)) {
334 err = -ENODATA;
335 goto out;
336 }
337
338 *base_addr = txattr_addr;
339 return 0;
340out:
341 kzfree(txattr_addr);
342 return err;
343}
344
345static int read_all_xattrs(struct inode *inode, struct page *ipage,
346 void **base_addr)
347{
348 struct f2fs_xattr_header *header;
349 nid_t xnid = F2FS_I(inode)->i_xattr_nid;
350 unsigned int size = VALID_XATTR_BLOCK_SIZE;
351 unsigned int inline_size = inline_xattr_size(inode);
352 void *txattr_addr;
353 int err;
354
355 txattr_addr = f2fs_kzalloc(F2FS_I_SB(inode),
356 inline_size + size + XATTR_PADDING_SIZE, GFP_NOFS);
357 if (!txattr_addr)
358 return -ENOMEM;
359
360
361 if (inline_size) {
362 err = read_inline_xattr(inode, ipage, txattr_addr);
363 if (err)
364 goto fail;
365 }
366
367
368 if (xnid) {
369 err = read_xattr_block(inode, txattr_addr);
370 if (err)
371 goto fail;
372 }
373
374 header = XATTR_HDR(txattr_addr);
375
376
377 if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
378 header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
379 header->h_refcount = cpu_to_le32(1);
380 }
381 *base_addr = txattr_addr;
382 return 0;
383fail:
384 kzfree(txattr_addr);
385 return err;
386}
387
388static inline int write_all_xattrs(struct inode *inode, __u32 hsize,
389 void *txattr_addr, struct page *ipage)
390{
391 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
392 size_t inline_size = inline_xattr_size(inode);
393 struct page *in_page = NULL;
394 void *xattr_addr;
395 void *inline_addr = NULL;
396 struct page *xpage;
397 nid_t new_nid = 0;
398 int err = 0;
399
400 if (hsize > inline_size && !F2FS_I(inode)->i_xattr_nid)
401 if (!f2fs_alloc_nid(sbi, &new_nid))
402 return -ENOSPC;
403
404
405 if (inline_size) {
406 if (ipage) {
407 inline_addr = inline_xattr_addr(inode, ipage);
408 } else {
409 in_page = f2fs_get_node_page(sbi, inode->i_ino);
410 if (IS_ERR(in_page)) {
411 f2fs_alloc_nid_failed(sbi, new_nid);
412 return PTR_ERR(in_page);
413 }
414 inline_addr = inline_xattr_addr(inode, in_page);
415 }
416
417 f2fs_wait_on_page_writeback(ipage ? ipage : in_page,
418 NODE, true);
419
420 if (hsize <= inline_size) {
421 err = f2fs_truncate_xattr_node(inode);
422 f2fs_alloc_nid_failed(sbi, new_nid);
423 if (err) {
424 f2fs_put_page(in_page, 1);
425 return err;
426 }
427 memcpy(inline_addr, txattr_addr, inline_size);
428 set_page_dirty(ipage ? ipage : in_page);
429 goto in_page_out;
430 }
431 }
432
433
434 if (F2FS_I(inode)->i_xattr_nid) {
435 xpage = f2fs_get_node_page(sbi, F2FS_I(inode)->i_xattr_nid);
436 if (IS_ERR(xpage)) {
437 err = PTR_ERR(xpage);
438 f2fs_alloc_nid_failed(sbi, new_nid);
439 goto in_page_out;
440 }
441 f2fs_bug_on(sbi, new_nid);
442 f2fs_wait_on_page_writeback(xpage, NODE, true);
443 } else {
444 struct dnode_of_data dn;
445 set_new_dnode(&dn, inode, NULL, NULL, new_nid);
446 xpage = f2fs_new_node_page(&dn, XATTR_NODE_OFFSET);
447 if (IS_ERR(xpage)) {
448 err = PTR_ERR(xpage);
449 f2fs_alloc_nid_failed(sbi, new_nid);
450 goto in_page_out;
451 }
452 f2fs_alloc_nid_done(sbi, new_nid);
453 }
454 xattr_addr = page_address(xpage);
455
456 if (inline_size)
457 memcpy(inline_addr, txattr_addr, inline_size);
458 memcpy(xattr_addr, txattr_addr + inline_size, VALID_XATTR_BLOCK_SIZE);
459
460 if (inline_size)
461 set_page_dirty(ipage ? ipage : in_page);
462 set_page_dirty(xpage);
463
464 f2fs_put_page(xpage, 1);
465in_page_out:
466 f2fs_put_page(in_page, 1);
467 return err;
468}
469
470int f2fs_getxattr(struct inode *inode, int index, const char *name,
471 void *buffer, size_t buffer_size, struct page *ipage)
472{
473 struct f2fs_xattr_entry *entry = NULL;
474 int error = 0;
475 unsigned int size, len;
476 void *base_addr = NULL;
477
478 if (name == NULL)
479 return -EINVAL;
480
481 len = strlen(name);
482 if (len > F2FS_NAME_LEN)
483 return -ERANGE;
484
485 down_read(&F2FS_I(inode)->i_xattr_sem);
486 error = lookup_all_xattrs(inode, ipage, index, len, name,
487 &entry, &base_addr);
488 up_read(&F2FS_I(inode)->i_xattr_sem);
489 if (error)
490 return error;
491
492 size = le16_to_cpu(entry->e_value_size);
493
494 if (buffer && size > buffer_size) {
495 error = -ERANGE;
496 goto out;
497 }
498
499 if (buffer) {
500 char *pval = entry->e_name + entry->e_name_len;
501 memcpy(buffer, pval, size);
502 }
503 error = size;
504out:
505 kzfree(base_addr);
506 return error;
507}
508
509ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
510{
511 struct inode *inode = d_inode(dentry);
512 struct f2fs_xattr_entry *entry;
513 void *base_addr;
514 int error = 0;
515 size_t rest = buffer_size;
516
517 down_read(&F2FS_I(inode)->i_xattr_sem);
518 error = read_all_xattrs(inode, NULL, &base_addr);
519 up_read(&F2FS_I(inode)->i_xattr_sem);
520 if (error)
521 return error;
522
523 list_for_each_xattr(entry, base_addr) {
524 const struct xattr_handler *handler =
525 f2fs_xattr_handler(entry->e_name_index);
526 const char *prefix;
527 size_t prefix_len;
528 size_t size;
529
530 if (!handler || (handler->list && !handler->list(dentry)))
531 continue;
532
533 prefix = handler->prefix ?: handler->name;
534 prefix_len = strlen(prefix);
535 size = prefix_len + entry->e_name_len + 1;
536 if (buffer) {
537 if (size > rest) {
538 error = -ERANGE;
539 goto cleanup;
540 }
541 memcpy(buffer, prefix, prefix_len);
542 buffer += prefix_len;
543 memcpy(buffer, entry->e_name, entry->e_name_len);
544 buffer += entry->e_name_len;
545 *buffer++ = 0;
546 }
547 rest -= size;
548 }
549 error = buffer_size - rest;
550cleanup:
551 kzfree(base_addr);
552 return error;
553}
554
555static bool f2fs_xattr_value_same(struct f2fs_xattr_entry *entry,
556 const void *value, size_t size)
557{
558 void *pval = entry->e_name + entry->e_name_len;
559
560 return (le16_to_cpu(entry->e_value_size) == size) &&
561 !memcmp(pval, value, size);
562}
563
564static int __f2fs_setxattr(struct inode *inode, int index,
565 const char *name, const void *value, size_t size,
566 struct page *ipage, int flags)
567{
568 struct f2fs_xattr_entry *here, *last;
569 void *base_addr;
570 int found, newsize;
571 size_t len;
572 __u32 new_hsize;
573 int error = 0;
574
575 if (name == NULL)
576 return -EINVAL;
577
578 if (value == NULL)
579 size = 0;
580
581 len = strlen(name);
582
583 if (len > F2FS_NAME_LEN)
584 return -ERANGE;
585
586 if (size > MAX_VALUE_LEN(inode))
587 return -E2BIG;
588
589 error = read_all_xattrs(inode, ipage, &base_addr);
590 if (error)
591 return error;
592
593
594 here = __find_xattr(base_addr, index, len, name);
595
596 found = IS_XATTR_LAST_ENTRY(here) ? 0 : 1;
597
598 if (found) {
599 if ((flags & XATTR_CREATE)) {
600 error = -EEXIST;
601 goto exit;
602 }
603
604 if (value && f2fs_xattr_value_same(here, value, size))
605 goto exit;
606 } else if ((flags & XATTR_REPLACE)) {
607 error = -ENODATA;
608 goto exit;
609 }
610
611 last = here;
612 while (!IS_XATTR_LAST_ENTRY(last))
613 last = XATTR_NEXT_ENTRY(last);
614
615 newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + len + size);
616
617
618 if (value) {
619 int free;
620
621
622
623
624 free = MIN_OFFSET(inode) - ((char *)last - (char *)base_addr);
625 if (found)
626 free = free + ENTRY_SIZE(here);
627
628 if (unlikely(free < newsize)) {
629 error = -E2BIG;
630 goto exit;
631 }
632 }
633
634
635 if (found) {
636
637
638
639
640 struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
641 int oldsize = ENTRY_SIZE(here);
642
643 memmove(here, next, (char *)last - (char *)next);
644 last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
645 memset(last, 0, oldsize);
646 }
647
648 new_hsize = (char *)last - (char *)base_addr;
649
650
651 if (value) {
652 char *pval;
653
654
655
656
657 last->e_name_index = index;
658 last->e_name_len = len;
659 memcpy(last->e_name, name, len);
660 pval = last->e_name + len;
661 memcpy(pval, value, size);
662 last->e_value_size = cpu_to_le16(size);
663 new_hsize += newsize;
664 }
665
666 error = write_all_xattrs(inode, new_hsize, base_addr, ipage);
667 if (error)
668 goto exit;
669
670 if (is_inode_flag_set(inode, FI_ACL_MODE)) {
671 inode->i_mode = F2FS_I(inode)->i_acl_mode;
672 inode->i_ctime = current_time(inode);
673 clear_inode_flag(inode, FI_ACL_MODE);
674 }
675 if (index == F2FS_XATTR_INDEX_ENCRYPTION &&
676 !strcmp(name, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT))
677 f2fs_set_encrypted_inode(inode);
678 f2fs_mark_inode_dirty_sync(inode, true);
679 if (!error && S_ISDIR(inode->i_mode))
680 set_sbi_flag(F2FS_I_SB(inode), SBI_NEED_CP);
681exit:
682 kzfree(base_addr);
683 return error;
684}
685
686int f2fs_setxattr(struct inode *inode, int index, const char *name,
687 const void *value, size_t size,
688 struct page *ipage, int flags)
689{
690 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
691 int err;
692
693 err = dquot_initialize(inode);
694 if (err)
695 return err;
696
697
698 if (ipage)
699 return __f2fs_setxattr(inode, index, name, value,
700 size, ipage, flags);
701 f2fs_balance_fs(sbi, true);
702
703 f2fs_lock_op(sbi);
704
705 down_write(&F2FS_I(inode)->i_sem);
706 down_write(&F2FS_I(inode)->i_xattr_sem);
707 err = __f2fs_setxattr(inode, index, name, value, size, ipage, flags);
708 up_write(&F2FS_I(inode)->i_xattr_sem);
709 up_write(&F2FS_I(inode)->i_sem);
710 f2fs_unlock_op(sbi);
711
712 f2fs_update_time(sbi, REQ_TIME);
713 return err;
714}
715