4 Extended attribute handling.
6 Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
7 Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>
8 Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
11 #include <linux/slab.h>
12 #include <linux/file.h>
13 #include <linux/xattr.h>
14 #include <linux/mount.h>
15 #include <linux/namei.h>
16 #include <linux/security.h>
17 #include <linux/evm.h>
18 #include <linux/syscalls.h>
19 #include <linux/export.h>
20 #include <linux/fsnotify.h>
21 #include <linux/audit.h>
22 #include <linux/vmalloc.h>
23 #include <linux/posix_acl_xattr.h>
25 #include <asm/uaccess.h>
28 strcmp_prefix(const char *a
, const char *a_prefix
)
30 while (*a_prefix
&& *a
== *a_prefix
) {
34 return *a_prefix
? NULL
: a
;
38 * In order to implement different sets of xattr operations for each xattr
39 * prefix with the generic xattr API, a filesystem should create a
40 * null-terminated array of struct xattr_handler (one for each prefix) and
41 * hang a pointer to it off of the s_xattr field of the superblock.
43 * The generic_fooxattr() functions will use this list to dispatch xattr
44 * operations to the correct xattr_handler.
46 #define for_each_xattr_handler(handlers, handler) \
48 for ((handler) = *(handlers)++; \
50 (handler) = *(handlers)++)
53 * Find the xattr_handler with the matching prefix.
55 static const struct xattr_handler
*
56 xattr_resolve_name(struct inode
*inode
, const char **name
)
58 const struct xattr_handler
**handlers
= inode
->i_sb
->s_xattr
;
59 const struct xattr_handler
*handler
;
61 if (!(inode
->i_opflags
& IOP_XATTR
)) {
62 if (unlikely(is_bad_inode(inode
)))
64 return ERR_PTR(-EOPNOTSUPP
);
66 for_each_xattr_handler(handlers
, handler
) {
69 n
= strcmp_prefix(*name
, xattr_prefix(handler
));
71 if (!handler
->prefix
^ !*n
) {
74 return ERR_PTR(-EINVAL
);
80 return ERR_PTR(-EOPNOTSUPP
);
84 * Check permissions for extended attribute access. This is a bit complicated
85 * because different namespaces have very different rules.
88 xattr_permission(struct inode
*inode
, const char *name
, int mask
)
91 * We can never set or remove an extended attribute on a read-only
92 * filesystem or on an immutable / append-only inode.
94 if (mask
& MAY_WRITE
) {
95 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
98 * Updating an xattr will likely cause i_uid and i_gid
99 * to be writen back improperly if their true value is
100 * unknown to the vfs.
102 if (HAS_UNMAPPED_ID(inode
))
107 * No restriction for security.* and system.* from the VFS. Decision
108 * on these is left to the underlying filesystem / security module.
110 if (!strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) ||
111 !strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
115 * The trusted.* namespace can only be accessed by privileged users.
117 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
)) {
118 if (!capable(CAP_SYS_ADMIN
))
119 return (mask
& MAY_WRITE
) ? -EPERM
: -ENODATA
;
124 * In the user.* namespace, only regular files and directories can have
125 * extended attributes. For sticky directories, only the owner and
126 * privileged users can write attributes.
128 if (!strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
)) {
129 if (!S_ISREG(inode
->i_mode
) && !S_ISDIR(inode
->i_mode
))
130 return (mask
& MAY_WRITE
) ? -EPERM
: -ENODATA
;
131 if (S_ISDIR(inode
->i_mode
) && (inode
->i_mode
& S_ISVTX
) &&
132 (mask
& MAY_WRITE
) && !inode_owner_or_capable(inode
))
136 return inode_permission(inode
, mask
);
140 __vfs_setxattr(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
141 const void *value
, size_t size
, int flags
)
143 if (!inode
->i_op
->setxattr
)
145 return inode
->i_op
->setxattr(dentry
, inode
, name
, value
, size
, flags
);
147 EXPORT_SYMBOL(__vfs_setxattr
);
150 * __vfs_setxattr_noperm - perform setxattr operation without performing
153 * @dentry - object to perform setxattr on
154 * @name - xattr name to set
155 * @value - value to set @name to
156 * @size - size of @value
157 * @flags - flags to pass into filesystem operations
159 * returns the result of the internal setxattr or setsecurity operations.
161 * This function requires the caller to lock the inode's i_mutex before it
162 * is executed. It also assumes that the caller will make the appropriate
165 int __vfs_setxattr_noperm(struct dentry
*dentry
, const char *name
,
166 const void *value
, size_t size
, int flags
)
168 struct inode
*inode
= dentry
->d_inode
;
169 int error
= -EOPNOTSUPP
;
170 int issec
= !strncmp(name
, XATTR_SECURITY_PREFIX
,
171 XATTR_SECURITY_PREFIX_LEN
);
174 inode
->i_flags
&= ~S_NOSEC
;
175 if (inode
->i_op
->setxattr
) {
176 error
= __vfs_setxattr(dentry
, inode
, name
, value
, size
, flags
);
178 fsnotify_xattr(dentry
);
179 security_inode_post_setxattr(dentry
, name
, value
,
183 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
185 if (unlikely(is_bad_inode(inode
)))
187 error
= security_inode_setsecurity(inode
, suffix
, value
,
190 fsnotify_xattr(dentry
);
198 vfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
199 size_t size
, int flags
)
201 struct inode
*inode
= dentry
->d_inode
;
204 error
= xattr_permission(inode
, name
, MAY_WRITE
);
209 error
= security_inode_setxattr(dentry
, name
, value
, size
, flags
);
213 error
= __vfs_setxattr_noperm(dentry
, name
, value
, size
, flags
);
219 EXPORT_SYMBOL_GPL(vfs_setxattr
);
222 xattr_getsecurity(struct inode
*inode
, const char *name
, void *value
,
228 if (!value
|| !size
) {
229 len
= security_inode_getsecurity(inode
, name
, &buffer
, false);
233 len
= security_inode_getsecurity(inode
, name
, &buffer
, true);
240 memcpy(value
, buffer
, len
);
242 security_release_secctx(buffer
, len
);
246 EXPORT_SYMBOL_GPL(xattr_getsecurity
);
249 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
251 * Allocate memory, if not already allocated, or re-allocate correct size,
252 * before retrieving the extended attribute.
254 * Returns the result of alloc, if failed, or the getxattr operation.
257 vfs_getxattr_alloc(struct dentry
*dentry
, const char *name
, char **xattr_value
,
258 size_t xattr_size
, gfp_t flags
)
260 struct inode
*inode
= dentry
->d_inode
;
261 char *value
= *xattr_value
;
264 error
= xattr_permission(inode
, name
, MAY_READ
);
268 if (!inode
->i_op
->getxattr
)
271 error
= inode
->i_op
->getxattr(dentry
, inode
, name
, NULL
, 0);
275 if (!value
|| (error
> xattr_size
)) {
276 value
= krealloc(*xattr_value
, error
+ 1, flags
);
279 memset(value
, 0, error
+ 1);
282 error
= inode
->i_op
->getxattr(dentry
, inode
, name
, value
, error
);
283 *xattr_value
= value
;
288 __vfs_getxattr(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
289 void *value
, size_t size
)
291 if (!inode
->i_op
->getxattr
)
293 return inode
->i_op
->getxattr(dentry
, inode
, name
, value
, size
);
295 EXPORT_SYMBOL(__vfs_getxattr
);
298 vfs_getxattr(struct dentry
*dentry
, const char *name
, void *value
, size_t size
)
300 struct inode
*inode
= dentry
->d_inode
;
303 error
= xattr_permission(inode
, name
, MAY_READ
);
307 error
= security_inode_getxattr(dentry
, name
);
311 if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
312 XATTR_SECURITY_PREFIX_LEN
)) {
313 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
314 int ret
= xattr_getsecurity(inode
, suffix
, value
, size
);
316 * Only overwrite the return value if a security module
317 * is actually active.
319 if (ret
== -EOPNOTSUPP
)
324 return __vfs_getxattr(dentry
, inode
, name
, value
, size
);
326 EXPORT_SYMBOL_GPL(vfs_getxattr
);
329 vfs_listxattr(struct dentry
*d
, char *list
, size_t size
)
333 error
= security_inode_listxattr(d
);
337 if (d
->d_inode
->i_op
->listxattr
) {
338 error
= d
->d_inode
->i_op
->listxattr(d
, list
, size
);
340 error
= security_inode_listsecurity(d
->d_inode
, list
, size
);
341 if (size
&& error
> size
)
346 EXPORT_SYMBOL_GPL(vfs_listxattr
);
349 __vfs_removexattr(struct dentry
*dentry
, const char *name
)
351 struct inode
*inode
= dentry
->d_inode
;
353 if (!inode
->i_op
->removexattr
)
355 return inode
->i_op
->removexattr(dentry
, name
);
357 EXPORT_SYMBOL(__vfs_removexattr
);
360 vfs_removexattr(struct dentry
*dentry
, const char *name
)
362 struct inode
*inode
= dentry
->d_inode
;
365 error
= xattr_permission(inode
, name
, MAY_WRITE
);
370 error
= security_inode_removexattr(dentry
, name
);
374 error
= __vfs_removexattr(dentry
, name
);
377 fsnotify_xattr(dentry
);
378 evm_inode_post_removexattr(dentry
, name
);
385 EXPORT_SYMBOL_GPL(vfs_removexattr
);
389 * Extended attribute SET operations
392 setxattr(struct dentry
*d
, const char __user
*name
, const void __user
*value
,
393 size_t size
, int flags
)
397 char kname
[XATTR_NAME_MAX
+ 1];
399 if (flags
& ~(XATTR_CREATE
|XATTR_REPLACE
))
402 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
403 if (error
== 0 || error
== sizeof(kname
))
409 if (size
> XATTR_SIZE_MAX
)
411 kvalue
= kmalloc(size
, GFP_KERNEL
| __GFP_NOWARN
);
413 kvalue
= vmalloc(size
);
417 if (copy_from_user(kvalue
, value
, size
)) {
421 if ((strcmp(kname
, XATTR_NAME_POSIX_ACL_ACCESS
) == 0) ||
422 (strcmp(kname
, XATTR_NAME_POSIX_ACL_DEFAULT
) == 0))
423 posix_acl_fix_xattr_from_user(kvalue
, size
);
426 error
= vfs_setxattr(d
, kname
, kvalue
, size
, flags
);
433 static int path_setxattr(const char __user
*pathname
,
434 const char __user
*name
, const void __user
*value
,
435 size_t size
, int flags
, unsigned int lookup_flags
)
440 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
443 error
= mnt_want_write(path
.mnt
);
445 error
= setxattr(path
.dentry
, name
, value
, size
, flags
);
446 mnt_drop_write(path
.mnt
);
449 if (retry_estale(error
, lookup_flags
)) {
450 lookup_flags
|= LOOKUP_REVAL
;
456 SYSCALL_DEFINE5(setxattr
, const char __user
*, pathname
,
457 const char __user
*, name
, const void __user
*, value
,
458 size_t, size
, int, flags
)
460 return path_setxattr(pathname
, name
, value
, size
, flags
, LOOKUP_FOLLOW
);
463 SYSCALL_DEFINE5(lsetxattr
, const char __user
*, pathname
,
464 const char __user
*, name
, const void __user
*, value
,
465 size_t, size
, int, flags
)
467 return path_setxattr(pathname
, name
, value
, size
, flags
, 0);
470 SYSCALL_DEFINE5(fsetxattr
, int, fd
, const char __user
*, name
,
471 const void __user
*,value
, size_t, size
, int, flags
)
473 struct fd f
= fdget(fd
);
479 error
= mnt_want_write_file(f
.file
);
481 error
= setxattr(f
.file
->f_path
.dentry
, name
, value
, size
, flags
);
482 mnt_drop_write_file(f
.file
);
489 * Extended attribute GET operations
492 getxattr(struct dentry
*d
, const char __user
*name
, void __user
*value
,
497 char kname
[XATTR_NAME_MAX
+ 1];
499 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
500 if (error
== 0 || error
== sizeof(kname
))
506 if (size
> XATTR_SIZE_MAX
)
507 size
= XATTR_SIZE_MAX
;
508 kvalue
= kzalloc(size
, GFP_KERNEL
| __GFP_NOWARN
);
510 kvalue
= vmalloc(size
);
516 error
= vfs_getxattr(d
, kname
, kvalue
, size
);
518 if ((strcmp(kname
, XATTR_NAME_POSIX_ACL_ACCESS
) == 0) ||
519 (strcmp(kname
, XATTR_NAME_POSIX_ACL_DEFAULT
) == 0))
520 posix_acl_fix_xattr_to_user(kvalue
, size
);
521 if (size
&& copy_to_user(value
, kvalue
, error
))
523 } else if (error
== -ERANGE
&& size
>= XATTR_SIZE_MAX
) {
524 /* The file system tried to returned a value bigger
525 than XATTR_SIZE_MAX bytes. Not possible. */
534 static ssize_t
path_getxattr(const char __user
*pathname
,
535 const char __user
*name
, void __user
*value
,
536 size_t size
, unsigned int lookup_flags
)
541 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
544 error
= getxattr(path
.dentry
, name
, value
, size
);
546 if (retry_estale(error
, lookup_flags
)) {
547 lookup_flags
|= LOOKUP_REVAL
;
553 SYSCALL_DEFINE4(getxattr
, const char __user
*, pathname
,
554 const char __user
*, name
, void __user
*, value
, size_t, size
)
556 return path_getxattr(pathname
, name
, value
, size
, LOOKUP_FOLLOW
);
559 SYSCALL_DEFINE4(lgetxattr
, const char __user
*, pathname
,
560 const char __user
*, name
, void __user
*, value
, size_t, size
)
562 return path_getxattr(pathname
, name
, value
, size
, 0);
565 SYSCALL_DEFINE4(fgetxattr
, int, fd
, const char __user
*, name
,
566 void __user
*, value
, size_t, size
)
568 struct fd f
= fdget(fd
);
569 ssize_t error
= -EBADF
;
574 error
= getxattr(f
.file
->f_path
.dentry
, name
, value
, size
);
580 * Extended attribute LIST operations
583 listxattr(struct dentry
*d
, char __user
*list
, size_t size
)
589 if (size
> XATTR_LIST_MAX
)
590 size
= XATTR_LIST_MAX
;
591 klist
= kmalloc(size
, __GFP_NOWARN
| GFP_KERNEL
);
593 klist
= vmalloc(size
);
599 error
= vfs_listxattr(d
, klist
, size
);
601 if (size
&& copy_to_user(list
, klist
, error
))
603 } else if (error
== -ERANGE
&& size
>= XATTR_LIST_MAX
) {
604 /* The file system tried to returned a list bigger
605 than XATTR_LIST_MAX bytes. Not possible. */
614 static ssize_t
path_listxattr(const char __user
*pathname
, char __user
*list
,
615 size_t size
, unsigned int lookup_flags
)
620 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
623 error
= listxattr(path
.dentry
, list
, size
);
625 if (retry_estale(error
, lookup_flags
)) {
626 lookup_flags
|= LOOKUP_REVAL
;
632 SYSCALL_DEFINE3(listxattr
, const char __user
*, pathname
, char __user
*, list
,
635 return path_listxattr(pathname
, list
, size
, LOOKUP_FOLLOW
);
638 SYSCALL_DEFINE3(llistxattr
, const char __user
*, pathname
, char __user
*, list
,
641 return path_listxattr(pathname
, list
, size
, 0);
644 SYSCALL_DEFINE3(flistxattr
, int, fd
, char __user
*, list
, size_t, size
)
646 struct fd f
= fdget(fd
);
647 ssize_t error
= -EBADF
;
652 error
= listxattr(f
.file
->f_path
.dentry
, list
, size
);
658 * Extended attribute REMOVE operations
661 removexattr(struct dentry
*d
, const char __user
*name
)
664 char kname
[XATTR_NAME_MAX
+ 1];
666 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
667 if (error
== 0 || error
== sizeof(kname
))
672 return vfs_removexattr(d
, kname
);
675 static int path_removexattr(const char __user
*pathname
,
676 const char __user
*name
, unsigned int lookup_flags
)
681 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
684 error
= mnt_want_write(path
.mnt
);
686 error
= removexattr(path
.dentry
, name
);
687 mnt_drop_write(path
.mnt
);
690 if (retry_estale(error
, lookup_flags
)) {
691 lookup_flags
|= LOOKUP_REVAL
;
697 SYSCALL_DEFINE2(removexattr
, const char __user
*, pathname
,
698 const char __user
*, name
)
700 return path_removexattr(pathname
, name
, LOOKUP_FOLLOW
);
703 SYSCALL_DEFINE2(lremovexattr
, const char __user
*, pathname
,
704 const char __user
*, name
)
706 return path_removexattr(pathname
, name
, 0);
709 SYSCALL_DEFINE2(fremovexattr
, int, fd
, const char __user
*, name
)
711 struct fd f
= fdget(fd
);
717 error
= mnt_want_write_file(f
.file
);
719 error
= removexattr(f
.file
->f_path
.dentry
, name
);
720 mnt_drop_write_file(f
.file
);
727 * Find the handler for the prefix and dispatch its get() operation.
730 generic_getxattr(struct dentry
*dentry
, struct inode
*inode
,
731 const char *name
, void *buffer
, size_t size
)
733 const struct xattr_handler
*handler
;
735 handler
= xattr_resolve_name(inode
, &name
);
737 return PTR_ERR(handler
);
738 return handler
->get(handler
, dentry
, inode
,
743 * Combine the results of the list() operation from every xattr_handler in the
747 generic_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
749 const struct xattr_handler
*handler
, **handlers
= dentry
->d_sb
->s_xattr
;
750 unsigned int size
= 0;
753 for_each_xattr_handler(handlers
, handler
) {
754 if (!handler
->name
||
755 (handler
->list
&& !handler
->list(dentry
)))
757 size
+= strlen(handler
->name
) + 1;
763 for_each_xattr_handler(handlers
, handler
) {
764 if (!handler
->name
||
765 (handler
->list
&& !handler
->list(dentry
)))
767 len
= strlen(handler
->name
);
768 if (len
+ 1 > buffer_size
)
770 memcpy(buf
, handler
->name
, len
+ 1);
772 buffer_size
-= len
+ 1;
780 * Find the handler for the prefix and dispatch its set() operation.
783 generic_setxattr(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
784 const void *value
, size_t size
, int flags
)
786 const struct xattr_handler
*handler
;
789 value
= ""; /* empty EA, do not remove */
790 handler
= xattr_resolve_name(inode
, &name
);
792 return PTR_ERR(handler
);
793 return handler
->set(handler
, dentry
, inode
, name
, value
, size
, flags
);
797 * Find the handler for the prefix and dispatch its set() operation to remove
798 * any associated extended attribute.
801 generic_removexattr(struct dentry
*dentry
, const char *name
)
803 const struct xattr_handler
*handler
;
805 handler
= xattr_resolve_name(d_inode(dentry
), &name
);
807 return PTR_ERR(handler
);
808 return handler
->set(handler
, dentry
, d_inode(dentry
), name
, NULL
,
812 EXPORT_SYMBOL(generic_getxattr
);
813 EXPORT_SYMBOL(generic_listxattr
);
814 EXPORT_SYMBOL(generic_setxattr
);
815 EXPORT_SYMBOL(generic_removexattr
);
818 * xattr_full_name - Compute full attribute name from suffix
820 * @handler: handler of the xattr_handler operation
821 * @name: name passed to the xattr_handler operation
823 * The get and set xattr handler operations are called with the remainder of
824 * the attribute name after skipping the handler's prefix: for example, "foo"
825 * is passed to the get operation of a handler with prefix "user." to get
826 * attribute "user.foo". The full name is still "there" in the name though.
828 * Note: the list xattr handler operation when called from the vfs is passed a
829 * NULL name; some file systems use this operation internally, with varying
832 const char *xattr_full_name(const struct xattr_handler
*handler
,
835 size_t prefix_len
= strlen(xattr_prefix(handler
));
837 return name
- prefix_len
;
839 EXPORT_SYMBOL(xattr_full_name
);
842 * Allocate new xattr and copy in the value; but leave the name to callers.
844 struct simple_xattr
*simple_xattr_alloc(const void *value
, size_t size
)
846 struct simple_xattr
*new_xattr
;
850 len
= sizeof(*new_xattr
) + size
;
851 if (len
< sizeof(*new_xattr
))
854 new_xattr
= kmalloc(len
, GFP_KERNEL
);
858 new_xattr
->size
= size
;
859 memcpy(new_xattr
->value
, value
, size
);
864 * xattr GET operation for in-memory/pseudo filesystems
866 int simple_xattr_get(struct simple_xattrs
*xattrs
, const char *name
,
867 void *buffer
, size_t size
)
869 struct simple_xattr
*xattr
;
872 spin_lock(&xattrs
->lock
);
873 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
874 if (strcmp(name
, xattr
->name
))
879 if (size
< xattr
->size
)
882 memcpy(buffer
, xattr
->value
, xattr
->size
);
886 spin_unlock(&xattrs
->lock
);
891 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
892 * @xattrs: target simple_xattr list
893 * @name: name of the extended attribute
894 * @value: value of the xattr. If %NULL, will remove the attribute.
895 * @size: size of the new xattr
896 * @flags: %XATTR_{CREATE|REPLACE}
898 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
899 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
900 * otherwise, fails with -ENODATA.
902 * Returns 0 on success, -errno on failure.
904 int simple_xattr_set(struct simple_xattrs
*xattrs
, const char *name
,
905 const void *value
, size_t size
, int flags
)
907 struct simple_xattr
*xattr
;
908 struct simple_xattr
*new_xattr
= NULL
;
911 /* value == NULL means remove */
913 new_xattr
= simple_xattr_alloc(value
, size
);
917 new_xattr
->name
= kstrdup(name
, GFP_KERNEL
);
918 if (!new_xattr
->name
) {
924 spin_lock(&xattrs
->lock
);
925 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
926 if (!strcmp(name
, xattr
->name
)) {
927 if (flags
& XATTR_CREATE
) {
930 } else if (new_xattr
) {
931 list_replace(&xattr
->list
, &new_xattr
->list
);
933 list_del(&xattr
->list
);
938 if (flags
& XATTR_REPLACE
) {
942 list_add(&new_xattr
->list
, &xattrs
->head
);
946 spin_unlock(&xattrs
->lock
);
955 static bool xattr_is_trusted(const char *name
)
957 return !strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
);
960 static int xattr_list_one(char **buffer
, ssize_t
*remaining_size
,
963 size_t len
= strlen(name
) + 1;
965 if (*remaining_size
< len
)
967 memcpy(*buffer
, name
, len
);
970 *remaining_size
-= len
;
975 * xattr LIST operation for in-memory/pseudo filesystems
977 ssize_t
simple_xattr_list(struct inode
*inode
, struct simple_xattrs
*xattrs
,
978 char *buffer
, size_t size
)
980 bool trusted
= capable(CAP_SYS_ADMIN
);
981 struct simple_xattr
*xattr
;
982 ssize_t remaining_size
= size
;
985 #ifdef CONFIG_FS_POSIX_ACL
987 err
= xattr_list_one(&buffer
, &remaining_size
,
988 XATTR_NAME_POSIX_ACL_ACCESS
);
992 if (inode
->i_default_acl
) {
993 err
= xattr_list_one(&buffer
, &remaining_size
,
994 XATTR_NAME_POSIX_ACL_DEFAULT
);
1000 spin_lock(&xattrs
->lock
);
1001 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
1002 /* skip "trusted." attributes for unprivileged callers */
1003 if (!trusted
&& xattr_is_trusted(xattr
->name
))
1006 err
= xattr_list_one(&buffer
, &remaining_size
, xattr
->name
);
1010 spin_unlock(&xattrs
->lock
);
1012 return err
? err
: size
- remaining_size
;
1016 * Adds an extended attribute to the list
1018 void simple_xattr_list_add(struct simple_xattrs
*xattrs
,
1019 struct simple_xattr
*new_xattr
)
1021 spin_lock(&xattrs
->lock
);
1022 list_add(&new_xattr
->list
, &xattrs
->head
);
1023 spin_unlock(&xattrs
->lock
);