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>,
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>
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
)
32 struct f2fs_sb_info
*sbi
= F2FS_SB(dentry
->d_sb
);
34 int total_len
, prefix_len
;
36 switch (handler
->flags
) {
37 case F2FS_XATTR_INDEX_USER
:
38 if (!test_opt(sbi
, XATTR_USER
))
41 case F2FS_XATTR_INDEX_TRUSTED
:
42 if (!capable(CAP_SYS_ADMIN
))
45 case F2FS_XATTR_INDEX_SECURITY
:
51 prefix
= xattr_prefix(handler
);
52 prefix_len
= strlen(prefix
);
53 total_len
= prefix_len
+ len
+ 1;
54 if (list
&& total_len
<= list_size
) {
55 memcpy(list
, prefix
, prefix_len
);
56 memcpy(list
+ prefix_len
, name
, len
);
57 list
[prefix_len
+ len
] = '\0';
62 static int f2fs_xattr_generic_get(const struct xattr_handler
*handler
,
63 struct dentry
*dentry
, const char *name
, void *buffer
,
66 struct f2fs_sb_info
*sbi
= F2FS_SB(dentry
->d_sb
);
68 switch (handler
->flags
) {
69 case F2FS_XATTR_INDEX_USER
:
70 if (!test_opt(sbi
, XATTR_USER
))
73 case F2FS_XATTR_INDEX_TRUSTED
:
74 if (!capable(CAP_SYS_ADMIN
))
77 case F2FS_XATTR_INDEX_SECURITY
:
82 return f2fs_getxattr(d_inode(dentry
), handler
->flags
, name
,
86 static int f2fs_xattr_generic_set(const struct xattr_handler
*handler
,
87 struct dentry
*dentry
, const char *name
, const void *value
,
88 size_t size
, int flags
)
90 struct f2fs_sb_info
*sbi
= F2FS_SB(dentry
->d_sb
);
92 switch (handler
->flags
) {
93 case F2FS_XATTR_INDEX_USER
:
94 if (!test_opt(sbi
, XATTR_USER
))
97 case F2FS_XATTR_INDEX_TRUSTED
:
98 if (!capable(CAP_SYS_ADMIN
))
101 case F2FS_XATTR_INDEX_SECURITY
:
106 return f2fs_setxattr(d_inode(dentry
), handler
->flags
, name
,
107 value
, size
, NULL
, flags
);
110 static size_t f2fs_xattr_advise_list(const struct xattr_handler
*handler
,
111 struct dentry
*dentry
, char *list
, size_t list_size
,
112 const char *name
, size_t len
)
114 const char *xname
= F2FS_SYSTEM_ADVISE_NAME
;
117 size
= strlen(xname
) + 1;
118 if (list
&& size
<= list_size
)
119 memcpy(list
, xname
, size
);
123 static int f2fs_xattr_advise_get(const struct xattr_handler
*handler
,
124 struct dentry
*dentry
, const char *name
, void *buffer
,
127 struct inode
*inode
= d_inode(dentry
);
130 *((char *)buffer
) = F2FS_I(inode
)->i_advise
;
134 static int f2fs_xattr_advise_set(const struct xattr_handler
*handler
,
135 struct dentry
*dentry
, const char *name
, const void *value
,
136 size_t size
, int flags
)
138 struct inode
*inode
= d_inode(dentry
);
140 if (!inode_owner_or_capable(inode
))
145 F2FS_I(inode
)->i_advise
|= *(char *)value
;
146 mark_inode_dirty(inode
);
150 #ifdef CONFIG_F2FS_FS_SECURITY
151 static int f2fs_initxattrs(struct inode
*inode
, const struct xattr
*xattr_array
,
154 const struct xattr
*xattr
;
157 for (xattr
= xattr_array
; xattr
->name
!= NULL
; xattr
++) {
158 err
= f2fs_setxattr(inode
, F2FS_XATTR_INDEX_SECURITY
,
159 xattr
->name
, xattr
->value
,
160 xattr
->value_len
, (struct page
*)page
, 0);
167 int f2fs_init_security(struct inode
*inode
, struct inode
*dir
,
168 const struct qstr
*qstr
, struct page
*ipage
)
170 return security_inode_init_security(inode
, dir
, qstr
,
171 &f2fs_initxattrs
, ipage
);
175 const struct xattr_handler f2fs_xattr_user_handler
= {
176 .prefix
= XATTR_USER_PREFIX
,
177 .flags
= F2FS_XATTR_INDEX_USER
,
178 .list
= f2fs_xattr_generic_list
,
179 .get
= f2fs_xattr_generic_get
,
180 .set
= f2fs_xattr_generic_set
,
183 const struct xattr_handler f2fs_xattr_trusted_handler
= {
184 .prefix
= XATTR_TRUSTED_PREFIX
,
185 .flags
= F2FS_XATTR_INDEX_TRUSTED
,
186 .list
= f2fs_xattr_generic_list
,
187 .get
= f2fs_xattr_generic_get
,
188 .set
= f2fs_xattr_generic_set
,
191 const struct xattr_handler f2fs_xattr_advise_handler
= {
192 .name
= F2FS_SYSTEM_ADVISE_NAME
,
193 .flags
= F2FS_XATTR_INDEX_ADVISE
,
194 .list
= f2fs_xattr_advise_list
,
195 .get
= f2fs_xattr_advise_get
,
196 .set
= f2fs_xattr_advise_set
,
199 const struct xattr_handler f2fs_xattr_security_handler
= {
200 .prefix
= XATTR_SECURITY_PREFIX
,
201 .flags
= F2FS_XATTR_INDEX_SECURITY
,
202 .list
= f2fs_xattr_generic_list
,
203 .get
= f2fs_xattr_generic_get
,
204 .set
= f2fs_xattr_generic_set
,
207 static const struct xattr_handler
*f2fs_xattr_handler_map
[] = {
208 [F2FS_XATTR_INDEX_USER
] = &f2fs_xattr_user_handler
,
209 #ifdef CONFIG_F2FS_FS_POSIX_ACL
210 [F2FS_XATTR_INDEX_POSIX_ACL_ACCESS
] = &posix_acl_access_xattr_handler
,
211 [F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &posix_acl_default_xattr_handler
,
213 [F2FS_XATTR_INDEX_TRUSTED
] = &f2fs_xattr_trusted_handler
,
214 #ifdef CONFIG_F2FS_FS_SECURITY
215 [F2FS_XATTR_INDEX_SECURITY
] = &f2fs_xattr_security_handler
,
217 [F2FS_XATTR_INDEX_ADVISE
] = &f2fs_xattr_advise_handler
,
220 const struct xattr_handler
*f2fs_xattr_handlers
[] = {
221 &f2fs_xattr_user_handler
,
222 #ifdef CONFIG_F2FS_FS_POSIX_ACL
223 &posix_acl_access_xattr_handler
,
224 &posix_acl_default_xattr_handler
,
226 &f2fs_xattr_trusted_handler
,
227 #ifdef CONFIG_F2FS_FS_SECURITY
228 &f2fs_xattr_security_handler
,
230 &f2fs_xattr_advise_handler
,
234 static inline const struct xattr_handler
*f2fs_xattr_handler(int index
)
236 const struct xattr_handler
*handler
= NULL
;
238 if (index
> 0 && index
< ARRAY_SIZE(f2fs_xattr_handler_map
))
239 handler
= f2fs_xattr_handler_map
[index
];
243 static struct f2fs_xattr_entry
*__find_xattr(void *base_addr
, int index
,
244 size_t len
, const char *name
)
246 struct f2fs_xattr_entry
*entry
;
248 list_for_each_xattr(entry
, base_addr
) {
249 if (entry
->e_name_index
!= index
)
251 if (entry
->e_name_len
!= len
)
253 if (!memcmp(entry
->e_name
, name
, len
))
259 static void *read_all_xattrs(struct inode
*inode
, struct page
*ipage
)
261 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
262 struct f2fs_xattr_header
*header
;
263 size_t size
= PAGE_SIZE
, inline_size
= 0;
266 inline_size
= inline_xattr_size(inode
);
268 txattr_addr
= kzalloc(inline_size
+ size
, GFP_F2FS_ZERO
);
272 /* read from inline xattr */
274 struct page
*page
= NULL
;
278 inline_addr
= inline_xattr_addr(ipage
);
280 page
= get_node_page(sbi
, inode
->i_ino
);
283 inline_addr
= inline_xattr_addr(page
);
285 memcpy(txattr_addr
, inline_addr
, inline_size
);
286 f2fs_put_page(page
, 1);
289 /* read from xattr node block */
290 if (F2FS_I(inode
)->i_xattr_nid
) {
294 /* The inode already has an extended attribute block. */
295 xpage
= get_node_page(sbi
, F2FS_I(inode
)->i_xattr_nid
);
299 xattr_addr
= page_address(xpage
);
300 memcpy(txattr_addr
+ inline_size
, xattr_addr
, PAGE_SIZE
);
301 f2fs_put_page(xpage
, 1);
304 header
= XATTR_HDR(txattr_addr
);
306 /* never been allocated xattrs */
307 if (le32_to_cpu(header
->h_magic
) != F2FS_XATTR_MAGIC
) {
308 header
->h_magic
= cpu_to_le32(F2FS_XATTR_MAGIC
);
309 header
->h_refcount
= cpu_to_le32(1);
317 static inline int write_all_xattrs(struct inode
*inode
, __u32 hsize
,
318 void *txattr_addr
, struct page
*ipage
)
320 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
321 size_t inline_size
= 0;
327 inline_size
= inline_xattr_size(inode
);
329 if (hsize
> inline_size
&& !F2FS_I(inode
)->i_xattr_nid
)
330 if (!alloc_nid(sbi
, &new_nid
))
333 /* write to inline xattr */
335 struct page
*page
= NULL
;
339 inline_addr
= inline_xattr_addr(ipage
);
340 f2fs_wait_on_page_writeback(ipage
, NODE
);
342 page
= get_node_page(sbi
, inode
->i_ino
);
344 alloc_nid_failed(sbi
, new_nid
);
345 return PTR_ERR(page
);
347 inline_addr
= inline_xattr_addr(page
);
348 f2fs_wait_on_page_writeback(page
, NODE
);
350 memcpy(inline_addr
, txattr_addr
, inline_size
);
351 f2fs_put_page(page
, 1);
353 /* no need to use xattr node block */
354 if (hsize
<= inline_size
) {
355 err
= truncate_xattr_node(inode
, ipage
);
356 alloc_nid_failed(sbi
, new_nid
);
361 /* write to xattr node block */
362 if (F2FS_I(inode
)->i_xattr_nid
) {
363 xpage
= get_node_page(sbi
, F2FS_I(inode
)->i_xattr_nid
);
365 alloc_nid_failed(sbi
, new_nid
);
366 return PTR_ERR(xpage
);
368 f2fs_bug_on(sbi
, new_nid
);
369 f2fs_wait_on_page_writeback(xpage
, NODE
);
371 struct dnode_of_data dn
;
372 set_new_dnode(&dn
, inode
, NULL
, NULL
, new_nid
);
373 xpage
= new_node_page(&dn
, XATTR_NODE_OFFSET
, ipage
);
375 alloc_nid_failed(sbi
, new_nid
);
376 return PTR_ERR(xpage
);
378 alloc_nid_done(sbi
, new_nid
);
381 xattr_addr
= page_address(xpage
);
382 memcpy(xattr_addr
, txattr_addr
+ inline_size
, PAGE_SIZE
-
383 sizeof(struct node_footer
));
384 set_page_dirty(xpage
);
385 f2fs_put_page(xpage
, 1);
387 /* need to checkpoint during fsync */
388 F2FS_I(inode
)->xattr_ver
= cur_cp_version(F2FS_CKPT(sbi
));
392 int f2fs_getxattr(struct inode
*inode
, int index
, const char *name
,
393 void *buffer
, size_t buffer_size
, struct page
*ipage
)
395 struct f2fs_xattr_entry
*entry
;
404 if (len
> F2FS_NAME_LEN
)
407 base_addr
= read_all_xattrs(inode
, ipage
);
411 entry
= __find_xattr(base_addr
, index
, len
, name
);
412 if (IS_XATTR_LAST_ENTRY(entry
)) {
417 size
= le16_to_cpu(entry
->e_value_size
);
419 if (buffer
&& size
> buffer_size
) {
425 char *pval
= entry
->e_name
+ entry
->e_name_len
;
426 memcpy(buffer
, pval
, size
);
435 ssize_t
f2fs_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
437 struct inode
*inode
= d_inode(dentry
);
438 struct f2fs_xattr_entry
*entry
;
441 size_t rest
= buffer_size
;
443 base_addr
= read_all_xattrs(inode
, NULL
);
447 list_for_each_xattr(entry
, base_addr
) {
448 const struct xattr_handler
*handler
=
449 f2fs_xattr_handler(entry
->e_name_index
);
455 size
= handler
->list(handler
, dentry
, buffer
, rest
,
456 entry
->e_name
, entry
->e_name_len
);
457 if (buffer
&& size
> rest
) {
466 error
= buffer_size
- rest
;
472 static int __f2fs_setxattr(struct inode
*inode
, int index
,
473 const char *name
, const void *value
, size_t size
,
474 struct page
*ipage
, int flags
)
476 struct f2fs_inode_info
*fi
= F2FS_I(inode
);
477 struct f2fs_xattr_entry
*here
, *last
;
492 if (len
> F2FS_NAME_LEN
)
495 if (size
> MAX_VALUE_LEN(inode
))
498 base_addr
= read_all_xattrs(inode
, ipage
);
502 /* find entry with wanted name. */
503 here
= __find_xattr(base_addr
, index
, len
, name
);
505 found
= IS_XATTR_LAST_ENTRY(here
) ? 0 : 1;
507 if ((flags
& XATTR_REPLACE
) && !found
) {
510 } else if ((flags
& XATTR_CREATE
) && found
) {
516 while (!IS_XATTR_LAST_ENTRY(last
))
517 last
= XATTR_NEXT_ENTRY(last
);
519 newsize
= XATTR_ALIGN(sizeof(struct f2fs_xattr_entry
) + len
+ size
);
525 * If value is NULL, it is remove operation.
526 * In case of update operation, we calculate free.
528 free
= MIN_OFFSET(inode
) - ((char *)last
- (char *)base_addr
);
530 free
= free
+ ENTRY_SIZE(here
);
532 if (unlikely(free
< newsize
)) {
538 /* 2. Remove old entry */
541 * If entry is found, remove old entry.
542 * If not found, remove operation is not needed.
544 struct f2fs_xattr_entry
*next
= XATTR_NEXT_ENTRY(here
);
545 int oldsize
= ENTRY_SIZE(here
);
547 memmove(here
, next
, (char *)last
- (char *)next
);
548 last
= (struct f2fs_xattr_entry
*)((char *)last
- oldsize
);
549 memset(last
, 0, oldsize
);
552 new_hsize
= (char *)last
- (char *)base_addr
;
554 /* 3. Write new entry */
558 * Before we come here, old entry is removed.
559 * We just write new entry.
561 memset(last
, 0, newsize
);
562 last
->e_name_index
= index
;
563 last
->e_name_len
= len
;
564 memcpy(last
->e_name
, name
, len
);
565 pval
= last
->e_name
+ len
;
566 memcpy(pval
, value
, size
);
567 last
->e_value_size
= cpu_to_le16(size
);
568 new_hsize
+= newsize
;
571 error
= write_all_xattrs(inode
, new_hsize
, base_addr
, ipage
);
575 if (is_inode_flag_set(fi
, FI_ACL_MODE
)) {
576 inode
->i_mode
= fi
->i_acl_mode
;
577 inode
->i_ctime
= CURRENT_TIME
;
578 clear_inode_flag(fi
, FI_ACL_MODE
);
580 if (index
== F2FS_XATTR_INDEX_ENCRYPTION
&&
581 !strcmp(name
, F2FS_XATTR_NAME_ENCRYPTION_CONTEXT
))
582 f2fs_set_encrypted_inode(inode
);
585 update_inode(inode
, ipage
);
587 update_inode_page(inode
);
593 int f2fs_setxattr(struct inode
*inode
, int index
, const char *name
,
594 const void *value
, size_t size
,
595 struct page
*ipage
, int flags
)
597 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
600 /* this case is only from init_inode_metadata */
602 return __f2fs_setxattr(inode
, index
, name
, value
,
604 f2fs_balance_fs(sbi
);
607 /* protect xattr_ver */
608 down_write(&F2FS_I(inode
)->i_sem
);
609 err
= __f2fs_setxattr(inode
, index
, name
, value
, size
, ipage
, flags
);
610 up_write(&F2FS_I(inode
)->i_sem
);