1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
8 * Portions of this code from linux/fs/ext2/xattr.c
10 * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
12 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
13 * Extended attributes for symlinks and special files added per
14 * suggestion of Luka Renko <luka.renko@hermes.si>.
15 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
18 #include <linux/rwsem.h>
19 #include <linux/f2fs_fs.h>
20 #include <linux/security.h>
21 #include <linux/posix_acl_xattr.h>
26 static void *xattr_alloc(struct f2fs_sb_info
*sbi
, int size
, bool *is_inline
)
28 if (likely(size
== sbi
->inline_xattr_slab_size
)) {
30 return kmem_cache_zalloc(sbi
->inline_xattr_slab
, GFP_NOFS
);
33 return f2fs_kzalloc(sbi
, size
, GFP_NOFS
);
36 static void xattr_free(struct f2fs_sb_info
*sbi
, void *xattr_addr
,
40 kmem_cache_free(sbi
->inline_xattr_slab
, xattr_addr
);
45 static int f2fs_xattr_generic_get(const struct xattr_handler
*handler
,
46 struct dentry
*unused
, struct inode
*inode
,
47 const char *name
, void *buffer
, size_t size
)
49 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
51 switch (handler
->flags
) {
52 case F2FS_XATTR_INDEX_USER
:
53 if (!test_opt(sbi
, XATTR_USER
))
56 case F2FS_XATTR_INDEX_TRUSTED
:
57 case F2FS_XATTR_INDEX_SECURITY
:
62 return f2fs_getxattr(inode
, handler
->flags
, name
,
66 static int f2fs_xattr_generic_set(const struct xattr_handler
*handler
,
67 struct dentry
*unused
, struct inode
*inode
,
68 const char *name
, const void *value
,
69 size_t size
, int flags
)
71 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
73 switch (handler
->flags
) {
74 case F2FS_XATTR_INDEX_USER
:
75 if (!test_opt(sbi
, XATTR_USER
))
78 case F2FS_XATTR_INDEX_TRUSTED
:
79 case F2FS_XATTR_INDEX_SECURITY
:
84 return f2fs_setxattr(inode
, handler
->flags
, name
,
85 value
, size
, NULL
, flags
);
88 static bool f2fs_xattr_user_list(struct dentry
*dentry
)
90 struct f2fs_sb_info
*sbi
= F2FS_SB(dentry
->d_sb
);
92 return test_opt(sbi
, XATTR_USER
);
95 static bool f2fs_xattr_trusted_list(struct dentry
*dentry
)
97 return capable(CAP_SYS_ADMIN
);
100 static int f2fs_xattr_advise_get(const struct xattr_handler
*handler
,
101 struct dentry
*unused
, struct inode
*inode
,
102 const char *name
, void *buffer
, size_t size
)
105 *((char *)buffer
) = F2FS_I(inode
)->i_advise
;
109 static int f2fs_xattr_advise_set(const struct xattr_handler
*handler
,
110 struct dentry
*unused
, struct inode
*inode
,
111 const char *name
, const void *value
,
112 size_t size
, int flags
)
114 unsigned char old_advise
= F2FS_I(inode
)->i_advise
;
115 unsigned char new_advise
;
117 if (!inode_owner_or_capable(inode
))
122 new_advise
= *(char *)value
;
123 if (new_advise
& ~FADVISE_MODIFIABLE_BITS
)
126 new_advise
= new_advise
& FADVISE_MODIFIABLE_BITS
;
127 new_advise
|= old_advise
& ~FADVISE_MODIFIABLE_BITS
;
129 F2FS_I(inode
)->i_advise
= new_advise
;
130 f2fs_mark_inode_dirty_sync(inode
, true);
134 #ifdef CONFIG_F2FS_FS_SECURITY
135 static int f2fs_initxattrs(struct inode
*inode
, const struct xattr
*xattr_array
,
138 const struct xattr
*xattr
;
141 for (xattr
= xattr_array
; xattr
->name
!= NULL
; xattr
++) {
142 err
= f2fs_setxattr(inode
, F2FS_XATTR_INDEX_SECURITY
,
143 xattr
->name
, xattr
->value
,
144 xattr
->value_len
, (struct page
*)page
, 0);
151 int f2fs_init_security(struct inode
*inode
, struct inode
*dir
,
152 const struct qstr
*qstr
, struct page
*ipage
)
154 return security_inode_init_security(inode
, dir
, qstr
,
155 &f2fs_initxattrs
, ipage
);
159 const struct xattr_handler f2fs_xattr_user_handler
= {
160 .prefix
= XATTR_USER_PREFIX
,
161 .flags
= F2FS_XATTR_INDEX_USER
,
162 .list
= f2fs_xattr_user_list
,
163 .get
= f2fs_xattr_generic_get
,
164 .set
= f2fs_xattr_generic_set
,
167 const struct xattr_handler f2fs_xattr_trusted_handler
= {
168 .prefix
= XATTR_TRUSTED_PREFIX
,
169 .flags
= F2FS_XATTR_INDEX_TRUSTED
,
170 .list
= f2fs_xattr_trusted_list
,
171 .get
= f2fs_xattr_generic_get
,
172 .set
= f2fs_xattr_generic_set
,
175 const struct xattr_handler f2fs_xattr_advise_handler
= {
176 .name
= F2FS_SYSTEM_ADVISE_NAME
,
177 .flags
= F2FS_XATTR_INDEX_ADVISE
,
178 .get
= f2fs_xattr_advise_get
,
179 .set
= f2fs_xattr_advise_set
,
182 const struct xattr_handler f2fs_xattr_security_handler
= {
183 .prefix
= XATTR_SECURITY_PREFIX
,
184 .flags
= F2FS_XATTR_INDEX_SECURITY
,
185 .get
= f2fs_xattr_generic_get
,
186 .set
= f2fs_xattr_generic_set
,
189 static const struct xattr_handler
*f2fs_xattr_handler_map
[] = {
190 [F2FS_XATTR_INDEX_USER
] = &f2fs_xattr_user_handler
,
191 #ifdef CONFIG_F2FS_FS_POSIX_ACL
192 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS
] = &posix_acl_access_xattr_handler
,
193 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &posix_acl_default_xattr_handler
,
195 [F2FS_XATTR_INDEX_TRUSTED
] = &f2fs_xattr_trusted_handler
,
196 #ifdef CONFIG_F2FS_FS_SECURITY
197 [F2FS_XATTR_INDEX_SECURITY
] = &f2fs_xattr_security_handler
,
199 [F2FS_XATTR_INDEX_ADVISE
] = &f2fs_xattr_advise_handler
,
202 const struct xattr_handler
*f2fs_xattr_handlers
[] = {
203 &f2fs_xattr_user_handler
,
204 #ifdef CONFIG_F2FS_FS_POSIX_ACL
205 &posix_acl_access_xattr_handler
,
206 &posix_acl_default_xattr_handler
,
208 &f2fs_xattr_trusted_handler
,
209 #ifdef CONFIG_F2FS_FS_SECURITY
210 &f2fs_xattr_security_handler
,
212 &f2fs_xattr_advise_handler
,
216 static inline const struct xattr_handler
*f2fs_xattr_handler(int index
)
218 const struct xattr_handler
*handler
= NULL
;
220 if (index
> 0 && index
< ARRAY_SIZE(f2fs_xattr_handler_map
))
221 handler
= f2fs_xattr_handler_map
[index
];
225 static struct f2fs_xattr_entry
*__find_xattr(void *base_addr
,
226 void *last_base_addr
, int index
,
227 size_t len
, const char *name
)
229 struct f2fs_xattr_entry
*entry
;
231 list_for_each_xattr(entry
, base_addr
) {
232 if ((void *)(entry
) + sizeof(__u32
) > last_base_addr
||
233 (void *)XATTR_NEXT_ENTRY(entry
) > last_base_addr
)
236 if (entry
->e_name_index
!= index
)
238 if (entry
->e_name_len
!= len
)
240 if (!memcmp(entry
->e_name
, name
, len
))
246 static struct f2fs_xattr_entry
*__find_inline_xattr(struct inode
*inode
,
247 void *base_addr
, void **last_addr
, int index
,
248 size_t len
, const char *name
)
250 struct f2fs_xattr_entry
*entry
;
251 unsigned int inline_size
= inline_xattr_size(inode
);
252 void *max_addr
= base_addr
+ inline_size
;
254 list_for_each_xattr(entry
, base_addr
) {
255 if ((void *)entry
+ sizeof(__u32
) > max_addr
||
256 (void *)XATTR_NEXT_ENTRY(entry
) > max_addr
) {
260 if (entry
->e_name_index
!= index
)
262 if (entry
->e_name_len
!= len
)
264 if (!memcmp(entry
->e_name
, name
, len
))
268 /* inline xattr header or entry across max inline xattr size */
269 if (IS_XATTR_LAST_ENTRY(entry
) &&
270 (void *)entry
+ sizeof(__u32
) > max_addr
) {
277 static int read_inline_xattr(struct inode
*inode
, struct page
*ipage
,
280 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
281 unsigned int inline_size
= inline_xattr_size(inode
);
282 struct page
*page
= NULL
;
286 inline_addr
= inline_xattr_addr(inode
, ipage
);
288 page
= f2fs_get_node_page(sbi
, inode
->i_ino
);
290 return PTR_ERR(page
);
292 inline_addr
= inline_xattr_addr(inode
, page
);
294 memcpy(txattr_addr
, inline_addr
, inline_size
);
295 f2fs_put_page(page
, 1);
300 static int read_xattr_block(struct inode
*inode
, void *txattr_addr
)
302 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
303 nid_t xnid
= F2FS_I(inode
)->i_xattr_nid
;
304 unsigned int inline_size
= inline_xattr_size(inode
);
308 /* The inode already has an extended attribute block. */
309 xpage
= f2fs_get_node_page(sbi
, xnid
);
311 return PTR_ERR(xpage
);
313 xattr_addr
= page_address(xpage
);
314 memcpy(txattr_addr
+ inline_size
, xattr_addr
, VALID_XATTR_BLOCK_SIZE
);
315 f2fs_put_page(xpage
, 1);
320 static int lookup_all_xattrs(struct inode
*inode
, struct page
*ipage
,
321 unsigned int index
, unsigned int len
,
322 const char *name
, struct f2fs_xattr_entry
**xe
,
323 void **base_addr
, int *base_size
,
326 void *cur_addr
, *txattr_addr
, *last_txattr_addr
;
327 void *last_addr
= NULL
;
328 nid_t xnid
= F2FS_I(inode
)->i_xattr_nid
;
329 unsigned int inline_size
= inline_xattr_size(inode
);
332 if (!xnid
&& !inline_size
)
335 *base_size
= XATTR_SIZE(inode
) + XATTR_PADDING_SIZE
;
336 txattr_addr
= xattr_alloc(F2FS_I_SB(inode
), *base_size
, is_inline
);
340 last_txattr_addr
= (void *)txattr_addr
+ XATTR_SIZE(inode
);
342 /* read from inline xattr */
344 err
= read_inline_xattr(inode
, ipage
, txattr_addr
);
348 *xe
= __find_inline_xattr(inode
, txattr_addr
, &last_addr
,
351 *base_size
= inline_size
;
356 /* read from xattr node block */
358 err
= read_xattr_block(inode
, txattr_addr
);
364 cur_addr
= XATTR_HDR(last_addr
) - 1;
366 cur_addr
= txattr_addr
;
368 *xe
= __find_xattr(cur_addr
, last_txattr_addr
, index
, len
, name
);
370 f2fs_err(F2FS_I_SB(inode
), "inode (%lu) has corrupted xattr",
372 set_sbi_flag(F2FS_I_SB(inode
), SBI_NEED_FSCK
);
377 if (IS_XATTR_LAST_ENTRY(*xe
)) {
382 *base_addr
= txattr_addr
;
385 xattr_free(F2FS_I_SB(inode
), txattr_addr
, *is_inline
);
389 static int read_all_xattrs(struct inode
*inode
, struct page
*ipage
,
392 struct f2fs_xattr_header
*header
;
393 nid_t xnid
= F2FS_I(inode
)->i_xattr_nid
;
394 unsigned int size
= VALID_XATTR_BLOCK_SIZE
;
395 unsigned int inline_size
= inline_xattr_size(inode
);
399 txattr_addr
= f2fs_kzalloc(F2FS_I_SB(inode
),
400 inline_size
+ size
+ XATTR_PADDING_SIZE
, GFP_NOFS
);
404 /* read from inline xattr */
406 err
= read_inline_xattr(inode
, ipage
, txattr_addr
);
411 /* read from xattr node block */
413 err
= read_xattr_block(inode
, txattr_addr
);
418 header
= XATTR_HDR(txattr_addr
);
420 /* never been allocated xattrs */
421 if (le32_to_cpu(header
->h_magic
) != F2FS_XATTR_MAGIC
) {
422 header
->h_magic
= cpu_to_le32(F2FS_XATTR_MAGIC
);
423 header
->h_refcount
= cpu_to_le32(1);
425 *base_addr
= txattr_addr
;
432 static inline int write_all_xattrs(struct inode
*inode
, __u32 hsize
,
433 void *txattr_addr
, struct page
*ipage
)
435 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
436 size_t inline_size
= inline_xattr_size(inode
);
437 struct page
*in_page
= NULL
;
439 void *inline_addr
= NULL
;
444 if (hsize
> inline_size
&& !F2FS_I(inode
)->i_xattr_nid
)
445 if (!f2fs_alloc_nid(sbi
, &new_nid
))
448 /* write to inline xattr */
451 inline_addr
= inline_xattr_addr(inode
, ipage
);
453 in_page
= f2fs_get_node_page(sbi
, inode
->i_ino
);
454 if (IS_ERR(in_page
)) {
455 f2fs_alloc_nid_failed(sbi
, new_nid
);
456 return PTR_ERR(in_page
);
458 inline_addr
= inline_xattr_addr(inode
, in_page
);
461 f2fs_wait_on_page_writeback(ipage
? ipage
: in_page
,
463 /* no need to use xattr node block */
464 if (hsize
<= inline_size
) {
465 err
= f2fs_truncate_xattr_node(inode
);
466 f2fs_alloc_nid_failed(sbi
, new_nid
);
468 f2fs_put_page(in_page
, 1);
471 memcpy(inline_addr
, txattr_addr
, inline_size
);
472 set_page_dirty(ipage
? ipage
: in_page
);
477 /* write to xattr node block */
478 if (F2FS_I(inode
)->i_xattr_nid
) {
479 xpage
= f2fs_get_node_page(sbi
, F2FS_I(inode
)->i_xattr_nid
);
481 err
= PTR_ERR(xpage
);
482 f2fs_alloc_nid_failed(sbi
, new_nid
);
485 f2fs_bug_on(sbi
, new_nid
);
486 f2fs_wait_on_page_writeback(xpage
, NODE
, true, true);
488 struct dnode_of_data dn
;
489 set_new_dnode(&dn
, inode
, NULL
, NULL
, new_nid
);
490 xpage
= f2fs_new_node_page(&dn
, XATTR_NODE_OFFSET
);
492 err
= PTR_ERR(xpage
);
493 f2fs_alloc_nid_failed(sbi
, new_nid
);
496 f2fs_alloc_nid_done(sbi
, new_nid
);
498 xattr_addr
= page_address(xpage
);
501 memcpy(inline_addr
, txattr_addr
, inline_size
);
502 memcpy(xattr_addr
, txattr_addr
+ inline_size
, VALID_XATTR_BLOCK_SIZE
);
505 set_page_dirty(ipage
? ipage
: in_page
);
506 set_page_dirty(xpage
);
508 f2fs_put_page(xpage
, 1);
510 f2fs_put_page(in_page
, 1);
514 int f2fs_getxattr(struct inode
*inode
, int index
, const char *name
,
515 void *buffer
, size_t buffer_size
, struct page
*ipage
)
517 struct f2fs_xattr_entry
*entry
= NULL
;
519 unsigned int size
, len
;
520 void *base_addr
= NULL
;
528 if (len
> F2FS_NAME_LEN
)
531 down_read(&F2FS_I(inode
)->i_xattr_sem
);
532 error
= lookup_all_xattrs(inode
, ipage
, index
, len
, name
,
533 &entry
, &base_addr
, &base_size
, &is_inline
);
534 up_read(&F2FS_I(inode
)->i_xattr_sem
);
538 size
= le16_to_cpu(entry
->e_value_size
);
540 if (buffer
&& size
> buffer_size
) {
546 char *pval
= entry
->e_name
+ entry
->e_name_len
;
548 if (base_size
- (pval
- (char *)base_addr
) < size
) {
552 memcpy(buffer
, pval
, size
);
556 xattr_free(F2FS_I_SB(inode
), base_addr
, is_inline
);
560 ssize_t
f2fs_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
562 struct inode
*inode
= d_inode(dentry
);
563 struct f2fs_xattr_entry
*entry
;
564 void *base_addr
, *last_base_addr
;
566 size_t rest
= buffer_size
;
568 down_read(&F2FS_I(inode
)->i_xattr_sem
);
569 error
= read_all_xattrs(inode
, NULL
, &base_addr
);
570 up_read(&F2FS_I(inode
)->i_xattr_sem
);
574 last_base_addr
= (void *)base_addr
+ XATTR_SIZE(inode
);
576 list_for_each_xattr(entry
, base_addr
) {
577 const struct xattr_handler
*handler
=
578 f2fs_xattr_handler(entry
->e_name_index
);
583 if ((void *)(entry
) + sizeof(__u32
) > last_base_addr
||
584 (void *)XATTR_NEXT_ENTRY(entry
) > last_base_addr
) {
585 f2fs_err(F2FS_I_SB(inode
), "inode (%lu) has corrupted xattr",
587 set_sbi_flag(F2FS_I_SB(inode
), SBI_NEED_FSCK
);
588 error
= -EFSCORRUPTED
;
592 if (!handler
|| (handler
->list
&& !handler
->list(dentry
)))
595 prefix
= xattr_prefix(handler
);
596 prefix_len
= strlen(prefix
);
597 size
= prefix_len
+ entry
->e_name_len
+ 1;
603 memcpy(buffer
, prefix
, prefix_len
);
604 buffer
+= prefix_len
;
605 memcpy(buffer
, entry
->e_name
, entry
->e_name_len
);
606 buffer
+= entry
->e_name_len
;
611 error
= buffer_size
- rest
;
617 static bool f2fs_xattr_value_same(struct f2fs_xattr_entry
*entry
,
618 const void *value
, size_t size
)
620 void *pval
= entry
->e_name
+ entry
->e_name_len
;
622 return (le16_to_cpu(entry
->e_value_size
) == size
) &&
623 !memcmp(pval
, value
, size
);
626 static int __f2fs_setxattr(struct inode
*inode
, int index
,
627 const char *name
, const void *value
, size_t size
,
628 struct page
*ipage
, int flags
)
630 struct f2fs_xattr_entry
*here
, *last
;
631 void *base_addr
, *last_base_addr
;
645 if (len
> F2FS_NAME_LEN
)
648 if (size
> MAX_VALUE_LEN(inode
))
651 error
= read_all_xattrs(inode
, ipage
, &base_addr
);
655 last_base_addr
= (void *)base_addr
+ XATTR_SIZE(inode
);
657 /* find entry with wanted name. */
658 here
= __find_xattr(base_addr
, last_base_addr
, index
, len
, name
);
660 f2fs_err(F2FS_I_SB(inode
), "inode (%lu) has corrupted xattr",
662 set_sbi_flag(F2FS_I_SB(inode
), SBI_NEED_FSCK
);
663 error
= -EFSCORRUPTED
;
667 found
= IS_XATTR_LAST_ENTRY(here
) ? 0 : 1;
670 if ((flags
& XATTR_CREATE
)) {
675 if (value
&& f2fs_xattr_value_same(here
, value
, size
))
677 } else if ((flags
& XATTR_REPLACE
)) {
683 while (!IS_XATTR_LAST_ENTRY(last
))
684 last
= XATTR_NEXT_ENTRY(last
);
686 newsize
= XATTR_ALIGN(sizeof(struct f2fs_xattr_entry
) + len
+ size
);
692 * If value is NULL, it is remove operation.
693 * In case of update operation, we calculate free.
695 free
= MIN_OFFSET(inode
) - ((char *)last
- (char *)base_addr
);
697 free
= free
+ ENTRY_SIZE(here
);
699 if (unlikely(free
< newsize
)) {
705 /* 2. Remove old entry */
708 * If entry is found, remove old entry.
709 * If not found, remove operation is not needed.
711 struct f2fs_xattr_entry
*next
= XATTR_NEXT_ENTRY(here
);
712 int oldsize
= ENTRY_SIZE(here
);
714 memmove(here
, next
, (char *)last
- (char *)next
);
715 last
= (struct f2fs_xattr_entry
*)((char *)last
- oldsize
);
716 memset(last
, 0, oldsize
);
719 new_hsize
= (char *)last
- (char *)base_addr
;
721 /* 3. Write new entry */
725 * Before we come here, old entry is removed.
726 * We just write new entry.
728 last
->e_name_index
= index
;
729 last
->e_name_len
= len
;
730 memcpy(last
->e_name
, name
, len
);
731 pval
= last
->e_name
+ len
;
732 memcpy(pval
, value
, size
);
733 last
->e_value_size
= cpu_to_le16(size
);
734 new_hsize
+= newsize
;
737 error
= write_all_xattrs(inode
, new_hsize
, base_addr
, ipage
);
741 if (is_inode_flag_set(inode
, FI_ACL_MODE
)) {
742 inode
->i_mode
= F2FS_I(inode
)->i_acl_mode
;
743 inode
->i_ctime
= current_time(inode
);
744 clear_inode_flag(inode
, FI_ACL_MODE
);
746 if (index
== F2FS_XATTR_INDEX_ENCRYPTION
&&
747 !strcmp(name
, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT
))
748 f2fs_set_encrypted_inode(inode
);
749 f2fs_mark_inode_dirty_sync(inode
, true);
750 if (!error
&& S_ISDIR(inode
->i_mode
))
751 set_sbi_flag(F2FS_I_SB(inode
), SBI_NEED_CP
);
757 int f2fs_setxattr(struct inode
*inode
, int index
, const char *name
,
758 const void *value
, size_t size
,
759 struct page
*ipage
, int flags
)
761 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
764 if (unlikely(f2fs_cp_error(sbi
)))
766 if (!f2fs_is_checkpoint_ready(sbi
))
769 err
= dquot_initialize(inode
);
773 /* this case is only from f2fs_init_inode_metadata */
775 return __f2fs_setxattr(inode
, index
, name
, value
,
777 f2fs_balance_fs(sbi
, true);
780 down_write(&F2FS_I(inode
)->i_xattr_sem
);
781 err
= __f2fs_setxattr(inode
, index
, name
, value
, size
, ipage
, flags
);
782 up_write(&F2FS_I(inode
)->i_xattr_sem
);
785 f2fs_update_time(sbi
, REQ_TIME
);
789 int f2fs_init_xattr_caches(struct f2fs_sb_info
*sbi
)
791 dev_t dev
= sbi
->sb
->s_bdev
->bd_dev
;
794 sprintf(slab_name
, "f2fs_xattr_entry-%u:%u", MAJOR(dev
), MINOR(dev
));
796 sbi
->inline_xattr_slab_size
= F2FS_OPTION(sbi
).inline_xattr_size
*
797 sizeof(__le32
) + XATTR_PADDING_SIZE
;
799 sbi
->inline_xattr_slab
= f2fs_kmem_cache_create(slab_name
,
800 sbi
->inline_xattr_slab_size
);
801 if (!sbi
->inline_xattr_slab
)
807 void f2fs_destroy_xattr_caches(struct f2fs_sb_info
*sbi
)
809 kmem_cache_destroy(sbi
->inline_xattr_slab
);