4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2011, Lawrence Livermore National Security, LLC.
24 * Extended attributes (xattr) on Solaris are implemented as files
25 * which exist in a hidden xattr directory. These extended attributes
26 * can be accessed using the attropen() system call which opens
27 * the extended attribute. It can then be manipulated just like
28 * a standard file descriptor. This has a couple advantages such
29 * as practically no size limit on the file, and the extended
30 * attributes permissions may differ from those of the parent file.
31 * This interface is really quite clever, but it's also completely
32 * different than what is supported on Linux. It also comes with a
33 * steep performance penalty when accessing small xattrs because they
34 * are not stored with the parent file.
36 * Under Linux extended attributes are manipulated by the system
37 * calls getxattr(2), setxattr(2), and listxattr(2). They consider
38 * extended attributes to be name/value pairs where the name is a
39 * NULL terminated string. The name must also include one of the
40 * following namespace prefixes:
42 * user - No restrictions and is available to user applications.
43 * trusted - Restricted to kernel and root (CAP_SYS_ADMIN) use.
44 * system - Used for access control lists (system.nfs4_acl, etc).
45 * security - Used by SELinux to store a files security context.
47 * The value under Linux to limited to 65536 bytes of binary data.
48 * In practice, individual xattrs tend to be much smaller than this
49 * and are typically less than 100 bytes. A good example of this
50 * are the security.selinux xattrs which are less than 100 bytes and
51 * exist for every file when xattr labeling is enabled.
53 * The Linux xattr implementation has been written to take advantage of
54 * this typical usage. When the dataset property 'xattr=sa' is set,
55 * then xattrs will be preferentially stored as System Attributes (SA).
56 * This allows tiny xattrs (~100 bytes) to be stored with the dnode and
57 * up to 64k of xattrs to be stored in the spill block. If additional
58 * xattr space is required, which is unlikely under Linux, they will
59 * be stored using the traditional directory approach.
61 * This optimization results in roughly a 3x performance improvement
62 * when accessing xattrs because it avoids the need to perform a seek
63 * for every xattr value. When multiple xattrs are stored per-file
64 * the performance improvements are even greater because all of the
65 * xattrs stored in the spill block will be cached.
67 * However, by default SA based xattrs are disabled in the Linux port
68 * to maximize compatibility with other implementations. If you do
69 * enable SA based xattrs then they will not be visible on platforms
70 * which do not support this feature.
72 * NOTE: One additional consequence of the xattr directory implementation
73 * is that when an extended attribute is manipulated an inode is created.
74 * This inode will exist in the Linux inode cache but there will be no
75 * associated entry in the dentry cache which references it. This is
76 * safe but it may result in some confusion. Enabling SA based xattrs
77 * largely avoids the issue except in the overflow case.
80 #include <sys/zfs_znode.h>
81 #include <sys/zfs_vfsops.h>
82 #include <sys/zfs_vnops.h>
87 typedef struct xattr_filldir
{
91 struct dentry
*dentry
;
94 static const struct xattr_handler
*zpl_xattr_handler(const char *);
97 zpl_xattr_permission(xattr_filldir_t
*xf
, const char *name
, int name_len
)
99 static const struct xattr_handler
*handler
;
100 struct dentry
*d
= xf
->dentry
;
102 handler
= zpl_xattr_handler(name
);
107 #if defined(HAVE_XATTR_LIST_SIMPLE)
108 if (!handler
->list(d
))
110 #elif defined(HAVE_XATTR_LIST_DENTRY)
111 if (!handler
->list(d
, NULL
, 0, name
, name_len
, 0))
113 #elif defined(HAVE_XATTR_LIST_HANDLER)
114 if (!handler
->list(handler
, d
, NULL
, 0, name
, name_len
))
123 * Determine is a given xattr name should be visible and if so copy it
124 * in to the provided buffer (xf->buf).
127 zpl_xattr_filldir(xattr_filldir_t
*xf
, const char *name
, int name_len
)
129 /* Check permissions using the per-namespace list xattr handler. */
130 if (!zpl_xattr_permission(xf
, name
, name_len
))
133 /* When xf->buf is NULL only calculate the required size. */
135 if (xf
->offset
+ name_len
+ 1 > xf
->size
)
138 memcpy(xf
->buf
+ xf
->offset
, name
, name_len
);
139 xf
->buf
[xf
->offset
+ name_len
] = '\0';
142 xf
->offset
+= (name_len
+ 1);
148 * Read as many directory entry names as will fit in to the provided buffer,
149 * or when no buffer is provided calculate the required buffer size.
152 zpl_xattr_readdir(struct inode
*dxip
, xattr_filldir_t
*xf
)
158 zap_cursor_init(&zc
, ITOZSB(dxip
)->z_os
, ITOZ(dxip
)->z_id
);
160 while ((error
= -zap_cursor_retrieve(&zc
, &zap
)) == 0) {
162 if (zap
.za_integer_length
!= 8 || zap
.za_num_integers
!= 1) {
167 error
= zpl_xattr_filldir(xf
, zap
.za_name
, strlen(zap
.za_name
));
171 zap_cursor_advance(&zc
);
174 zap_cursor_fini(&zc
);
176 if (error
== -ENOENT
)
183 zpl_xattr_list_dir(xattr_filldir_t
*xf
, cred_t
*cr
)
185 struct inode
*ip
= xf
->dentry
->d_inode
;
186 struct inode
*dxip
= NULL
;
190 /* Lookup the xattr directory */
191 error
= -zfs_lookup(ITOZ(ip
), NULL
, &dxzp
, LOOKUP_XATTR
,
194 if (error
== -ENOENT
)
201 error
= zpl_xattr_readdir(dxip
, xf
);
208 zpl_xattr_list_sa(xattr_filldir_t
*xf
)
210 znode_t
*zp
= ITOZ(xf
->dentry
->d_inode
);
211 nvpair_t
*nvp
= NULL
;
214 mutex_enter(&zp
->z_lock
);
215 if (zp
->z_xattr_cached
== NULL
)
216 error
= -zfs_sa_get_xattr(zp
);
217 mutex_exit(&zp
->z_lock
);
222 ASSERT(zp
->z_xattr_cached
);
224 while ((nvp
= nvlist_next_nvpair(zp
->z_xattr_cached
, nvp
)) != NULL
) {
225 ASSERT3U(nvpair_type(nvp
), ==, DATA_TYPE_BYTE_ARRAY
);
227 error
= zpl_xattr_filldir(xf
, nvpair_name(nvp
),
228 strlen(nvpair_name(nvp
)));
237 zpl_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
239 znode_t
*zp
= ITOZ(dentry
->d_inode
);
240 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
241 xattr_filldir_t xf
= { buffer_size
, 0, buffer
, dentry
};
243 fstrans_cookie_t cookie
;
247 cookie
= spl_fstrans_mark();
250 rw_enter(&zp
->z_xattr_lock
, RW_READER
);
252 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
) {
253 error
= zpl_xattr_list_sa(&xf
);
258 error
= zpl_xattr_list_dir(&xf
, cr
);
265 rw_exit(&zp
->z_xattr_lock
);
267 spl_fstrans_unmark(cookie
);
274 zpl_xattr_get_dir(struct inode
*ip
, const char *name
, void *value
,
275 size_t size
, cred_t
*cr
)
277 fstrans_cookie_t cookie
;
278 struct inode
*xip
= NULL
;
279 znode_t
*dxzp
= NULL
;
283 /* Lookup the xattr directory */
284 error
= -zfs_lookup(ITOZ(ip
), NULL
, &dxzp
, LOOKUP_XATTR
,
289 /* Lookup a specific xattr name in the directory */
290 error
= -zfs_lookup(dxzp
, (char *)name
, &xzp
, 0, cr
, NULL
, NULL
);
296 error
= i_size_read(xip
);
300 if (size
< i_size_read(xip
)) {
306 iov
.iov_base
= (void *)value
;
310 zfs_uio_iovec_init(&uio
, &iov
, 1, 0, UIO_SYSSPACE
, size
, 0);
312 cookie
= spl_fstrans_mark();
313 error
= -zfs_read(ITOZ(xip
), &uio
, 0, cr
);
314 spl_fstrans_unmark(cookie
);
317 error
= size
- zfs_uio_resid(&uio
);
329 zpl_xattr_get_sa(struct inode
*ip
, const char *name
, void *value
, size_t size
)
331 znode_t
*zp
= ITOZ(ip
);
336 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
338 mutex_enter(&zp
->z_lock
);
339 if (zp
->z_xattr_cached
== NULL
)
340 error
= -zfs_sa_get_xattr(zp
);
341 mutex_exit(&zp
->z_lock
);
346 ASSERT(zp
->z_xattr_cached
);
347 error
= -nvlist_lookup_byte_array(zp
->z_xattr_cached
, name
,
348 &nv_value
, &nv_size
);
352 if (size
== 0 || value
== NULL
)
358 memcpy(value
, nv_value
, nv_size
);
364 __zpl_xattr_get(struct inode
*ip
, const char *name
, void *value
, size_t size
,
367 znode_t
*zp
= ITOZ(ip
);
368 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
371 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
373 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
) {
374 error
= zpl_xattr_get_sa(ip
, name
, value
, size
);
375 if (error
!= -ENOENT
)
379 error
= zpl_xattr_get_dir(ip
, name
, value
, size
, cr
);
381 if (error
== -ENOENT
)
387 #define XATTR_NOENT 0x0
388 #define XATTR_IN_SA 0x1
389 #define XATTR_IN_DIR 0x2
390 /* check where the xattr resides */
392 __zpl_xattr_where(struct inode
*ip
, const char *name
, int *where
, cred_t
*cr
)
394 znode_t
*zp
= ITOZ(ip
);
395 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
399 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
401 *where
= XATTR_NOENT
;
402 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
) {
403 error
= zpl_xattr_get_sa(ip
, name
, NULL
, 0);
405 *where
|= XATTR_IN_SA
;
406 else if (error
!= -ENOENT
)
410 error
= zpl_xattr_get_dir(ip
, name
, NULL
, 0, cr
);
412 *where
|= XATTR_IN_DIR
;
413 else if (error
!= -ENOENT
)
416 if (*where
== (XATTR_IN_SA
|XATTR_IN_DIR
))
417 cmn_err(CE_WARN
, "ZFS: inode %p has xattr \"%s\""
418 " in both SA and dir", ip
, name
);
419 if (*where
== XATTR_NOENT
)
427 zpl_xattr_get(struct inode
*ip
, const char *name
, void *value
, size_t size
)
429 znode_t
*zp
= ITOZ(ip
);
430 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
432 fstrans_cookie_t cookie
;
436 cookie
= spl_fstrans_mark();
439 rw_enter(&zp
->z_xattr_lock
, RW_READER
);
440 error
= __zpl_xattr_get(ip
, name
, value
, size
, cr
);
441 rw_exit(&zp
->z_xattr_lock
);
443 spl_fstrans_unmark(cookie
);
450 zpl_xattr_set_dir(struct inode
*ip
, const char *name
, const void *value
,
451 size_t size
, int flags
, cred_t
*cr
)
453 znode_t
*dxzp
= NULL
;
456 int lookup_flags
, error
;
457 const int xattr_mode
= S_IFREG
| 0644;
461 * Lookup the xattr directory. When we're adding an entry pass
462 * CREATE_XATTR_DIR to ensure the xattr directory is created.
463 * When removing an entry this flag is not passed to avoid
464 * unnecessarily creating a new xattr directory.
466 lookup_flags
= LOOKUP_XATTR
;
468 lookup_flags
|= CREATE_XATTR_DIR
;
470 error
= -zfs_lookup(ITOZ(ip
), NULL
, &dxzp
, lookup_flags
,
475 /* Lookup a specific xattr name in the directory */
476 error
= -zfs_lookup(dxzp
, (char *)name
, &xzp
, 0, cr
, NULL
, NULL
);
477 if (error
&& (error
!= -ENOENT
))
482 /* Remove a specific name xattr when value is set to NULL. */
485 error
= -zfs_remove(dxzp
, (char *)name
, cr
, 0);
490 /* Lookup failed create a new xattr. */
492 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
493 vap
->va_mode
= xattr_mode
;
494 vap
->va_mask
= ATTR_MODE
;
495 vap
->va_uid
= crgetfsuid(cr
);
496 vap
->va_gid
= crgetfsgid(cr
);
498 error
= -zfs_create(dxzp
, (char *)name
, vap
, 0, 0644, &xzp
,
506 error
= -zfs_freesp(xzp
, 0, 0, xattr_mode
, TRUE
);
510 error
= -zfs_write_simple(xzp
, value
, size
, pos
, NULL
);
513 ip
->i_ctime
= current_time(ip
);
514 zfs_mark_inode_dirty(ip
);
518 kmem_free(vap
, sizeof (vattr_t
));
526 if (error
== -ENOENT
)
529 ASSERT3S(error
, <=, 0);
535 zpl_xattr_set_sa(struct inode
*ip
, const char *name
, const void *value
,
536 size_t size
, int flags
, cred_t
*cr
)
538 znode_t
*zp
= ITOZ(ip
);
543 mutex_enter(&zp
->z_lock
);
544 if (zp
->z_xattr_cached
== NULL
)
545 error
= -zfs_sa_get_xattr(zp
);
546 mutex_exit(&zp
->z_lock
);
551 ASSERT(zp
->z_xattr_cached
);
552 nvl
= zp
->z_xattr_cached
;
555 error
= -nvlist_remove(nvl
, name
, DATA_TYPE_BYTE_ARRAY
);
556 if (error
== -ENOENT
)
557 error
= zpl_xattr_set_dir(ip
, name
, NULL
, 0, flags
, cr
);
559 /* Limited to 32k to keep nvpair memory allocations small */
560 if (size
> DXATTR_MAX_ENTRY_SIZE
)
563 /* Prevent the DXATTR SA from consuming the entire SA region */
564 error
= -nvlist_size(nvl
, &sa_size
, NV_ENCODE_XDR
);
568 if (sa_size
> DXATTR_MAX_SA_SIZE
)
571 error
= -nvlist_add_byte_array(nvl
, name
,
572 (uchar_t
*)value
, size
);
576 * Update the SA for additions, modifications, and removals. On
577 * error drop the inconsistent cached version of the nvlist, it
578 * will be reconstructed from the ARC when next accessed.
581 error
= -zfs_sa_set_xattr(zp
);
585 zp
->z_xattr_cached
= NULL
;
588 ASSERT3S(error
, <=, 0);
594 zpl_xattr_set(struct inode
*ip
, const char *name
, const void *value
,
595 size_t size
, int flags
)
597 znode_t
*zp
= ITOZ(ip
);
598 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
600 fstrans_cookie_t cookie
;
605 cookie
= spl_fstrans_mark();
608 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
611 * Before setting the xattr check to see if it already exists.
612 * This is done to ensure the following optional flags are honored.
614 * XATTR_CREATE: fail if xattr already exists
615 * XATTR_REPLACE: fail if xattr does not exist
617 * We also want to know if it resides in sa or dir, so we can make
618 * sure we don't end up with duplicate in both places.
620 error
= __zpl_xattr_where(ip
, name
, &where
, cr
);
622 if (error
!= -ENODATA
)
624 if (flags
& XATTR_REPLACE
)
627 /* The xattr to be removed already doesn't exist */
633 if (flags
& XATTR_CREATE
)
637 /* Preferentially store the xattr as a SA for better performance */
638 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
&&
639 (zfsvfs
->z_xattr_sa
|| (value
== NULL
&& where
& XATTR_IN_SA
))) {
640 error
= zpl_xattr_set_sa(ip
, name
, value
, size
, flags
, cr
);
643 * Successfully put into SA, we need to clear the one
646 if (where
& XATTR_IN_DIR
)
647 zpl_xattr_set_dir(ip
, name
, NULL
, 0, 0, cr
);
652 error
= zpl_xattr_set_dir(ip
, name
, value
, size
, flags
, cr
);
654 * Successfully put into dir, we need to clear the one in SA.
656 if (error
== 0 && (where
& XATTR_IN_SA
))
657 zpl_xattr_set_sa(ip
, name
, NULL
, 0, 0, cr
);
659 rw_exit(&zp
->z_xattr_lock
);
661 spl_fstrans_unmark(cookie
);
663 ASSERT3S(error
, <=, 0);
669 * Extended user attributes
671 * "Extended user attributes may be assigned to files and directories for
672 * storing arbitrary additional information such as the mime type,
673 * character set or encoding of a file. The access permissions for user
674 * attributes are defined by the file permission bits: read permission
675 * is required to retrieve the attribute value, and writer permission is
676 * required to change it.
678 * The file permission bits of regular files and directories are
679 * interpreted differently from the file permission bits of special
680 * files and symbolic links. For regular files and directories the file
681 * permission bits define access to the file's contents, while for
682 * device special files they define access to the device described by
683 * the special file. The file permissions of symbolic links are not
684 * used in access checks. These differences would allow users to
685 * consume filesystem resources in a way not controllable by disk quotas
686 * for group or world writable special files and directories.
688 * For this reason, extended user attributes are allowed only for
689 * regular files and directories, and access to extended user attributes
690 * is restricted to the owner and to users with appropriate capabilities
691 * for directories with the sticky bit set (see the chmod(1) manual page
692 * for an explanation of the sticky bit)." - xattr(7)
694 * ZFS allows extended user attributes to be disabled administratively
695 * by setting the 'xattr=off' property on the dataset.
698 __zpl_xattr_user_list(struct inode
*ip
, char *list
, size_t list_size
,
699 const char *name
, size_t name_len
)
701 return (ITOZSB(ip
)->z_flags
& ZSB_XATTR
);
703 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_user_list
);
706 __zpl_xattr_user_get(struct inode
*ip
, const char *name
,
707 void *value
, size_t size
)
711 /* xattr_resolve_name will do this for us if this is defined */
712 #ifndef HAVE_XATTR_HANDLER_NAME
713 if (strcmp(name
, "") == 0)
716 if (!(ITOZSB(ip
)->z_flags
& ZSB_XATTR
))
717 return (-EOPNOTSUPP
);
719 xattr_name
= kmem_asprintf("%s%s", XATTR_USER_PREFIX
, name
);
720 error
= zpl_xattr_get(ip
, xattr_name
, value
, size
);
721 kmem_strfree(xattr_name
);
725 ZPL_XATTR_GET_WRAPPER(zpl_xattr_user_get
);
728 __zpl_xattr_user_set(struct inode
*ip
, const char *name
,
729 const void *value
, size_t size
, int flags
)
733 /* xattr_resolve_name will do this for us if this is defined */
734 #ifndef HAVE_XATTR_HANDLER_NAME
735 if (strcmp(name
, "") == 0)
738 if (!(ITOZSB(ip
)->z_flags
& ZSB_XATTR
))
739 return (-EOPNOTSUPP
);
741 xattr_name
= kmem_asprintf("%s%s", XATTR_USER_PREFIX
, name
);
742 error
= zpl_xattr_set(ip
, xattr_name
, value
, size
, flags
);
743 kmem_strfree(xattr_name
);
747 ZPL_XATTR_SET_WRAPPER(zpl_xattr_user_set
);
749 static xattr_handler_t zpl_xattr_user_handler
=
751 .prefix
= XATTR_USER_PREFIX
,
752 .list
= zpl_xattr_user_list
,
753 .get
= zpl_xattr_user_get
,
754 .set
= zpl_xattr_user_set
,
758 * Trusted extended attributes
760 * "Trusted extended attributes are visible and accessible only to
761 * processes that have the CAP_SYS_ADMIN capability. Attributes in this
762 * class are used to implement mechanisms in user space (i.e., outside
763 * the kernel) which keep information in extended attributes to which
764 * ordinary processes should not have access." - xattr(7)
767 __zpl_xattr_trusted_list(struct inode
*ip
, char *list
, size_t list_size
,
768 const char *name
, size_t name_len
)
770 return (capable(CAP_SYS_ADMIN
));
772 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_trusted_list
);
775 __zpl_xattr_trusted_get(struct inode
*ip
, const char *name
,
776 void *value
, size_t size
)
781 if (!capable(CAP_SYS_ADMIN
))
783 /* xattr_resolve_name will do this for us if this is defined */
784 #ifndef HAVE_XATTR_HANDLER_NAME
785 if (strcmp(name
, "") == 0)
788 xattr_name
= kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX
, name
);
789 error
= zpl_xattr_get(ip
, xattr_name
, value
, size
);
790 kmem_strfree(xattr_name
);
794 ZPL_XATTR_GET_WRAPPER(zpl_xattr_trusted_get
);
797 __zpl_xattr_trusted_set(struct inode
*ip
, const char *name
,
798 const void *value
, size_t size
, int flags
)
803 if (!capable(CAP_SYS_ADMIN
))
805 /* xattr_resolve_name will do this for us if this is defined */
806 #ifndef HAVE_XATTR_HANDLER_NAME
807 if (strcmp(name
, "") == 0)
810 xattr_name
= kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX
, name
);
811 error
= zpl_xattr_set(ip
, xattr_name
, value
, size
, flags
);
812 kmem_strfree(xattr_name
);
816 ZPL_XATTR_SET_WRAPPER(zpl_xattr_trusted_set
);
818 static xattr_handler_t zpl_xattr_trusted_handler
= {
819 .prefix
= XATTR_TRUSTED_PREFIX
,
820 .list
= zpl_xattr_trusted_list
,
821 .get
= zpl_xattr_trusted_get
,
822 .set
= zpl_xattr_trusted_set
,
826 * Extended security attributes
828 * "The security attribute namespace is used by kernel security modules,
829 * such as Security Enhanced Linux, and also to implement file
830 * capabilities (see capabilities(7)). Read and write access
831 * permissions to security attributes depend on the policy implemented
832 * for each security attribute by the security module. When no security
833 * module is loaded, all processes have read access to extended security
834 * attributes, and write access is limited to processes that have the
835 * CAP_SYS_ADMIN capability." - xattr(7)
838 __zpl_xattr_security_list(struct inode
*ip
, char *list
, size_t list_size
,
839 const char *name
, size_t name_len
)
843 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_security_list
);
846 __zpl_xattr_security_get(struct inode
*ip
, const char *name
,
847 void *value
, size_t size
)
851 /* xattr_resolve_name will do this for us if this is defined */
852 #ifndef HAVE_XATTR_HANDLER_NAME
853 if (strcmp(name
, "") == 0)
856 xattr_name
= kmem_asprintf("%s%s", XATTR_SECURITY_PREFIX
, name
);
857 error
= zpl_xattr_get(ip
, xattr_name
, value
, size
);
858 kmem_strfree(xattr_name
);
862 ZPL_XATTR_GET_WRAPPER(zpl_xattr_security_get
);
865 __zpl_xattr_security_set(struct inode
*ip
, const char *name
,
866 const void *value
, size_t size
, int flags
)
870 /* xattr_resolve_name will do this for us if this is defined */
871 #ifndef HAVE_XATTR_HANDLER_NAME
872 if (strcmp(name
, "") == 0)
875 xattr_name
= kmem_asprintf("%s%s", XATTR_SECURITY_PREFIX
, name
);
876 error
= zpl_xattr_set(ip
, xattr_name
, value
, size
, flags
);
877 kmem_strfree(xattr_name
);
881 ZPL_XATTR_SET_WRAPPER(zpl_xattr_security_set
);
884 zpl_xattr_security_init_impl(struct inode
*ip
, const struct xattr
*xattrs
,
887 const struct xattr
*xattr
;
890 for (xattr
= xattrs
; xattr
->name
!= NULL
; xattr
++) {
891 error
= __zpl_xattr_security_set(ip
,
892 xattr
->name
, xattr
->value
, xattr
->value_len
, 0);
902 zpl_xattr_security_init(struct inode
*ip
, struct inode
*dip
,
903 const struct qstr
*qstr
)
905 return security_inode_init_security(ip
, dip
, qstr
,
906 &zpl_xattr_security_init_impl
, NULL
);
910 * Security xattr namespace handlers.
912 static xattr_handler_t zpl_xattr_security_handler
= {
913 .prefix
= XATTR_SECURITY_PREFIX
,
914 .list
= zpl_xattr_security_list
,
915 .get
= zpl_xattr_security_get
,
916 .set
= zpl_xattr_security_set
,
920 * Extended system attributes
922 * "Extended system attributes are used by the kernel to store system
923 * objects such as Access Control Lists. Read and write access permissions
924 * to system attributes depend on the policy implemented for each system
925 * attribute implemented by filesystems in the kernel." - xattr(7)
927 #ifdef CONFIG_FS_POSIX_ACL
929 zpl_set_acl_impl(struct inode
*ip
, struct posix_acl
*acl
, int type
)
931 char *name
, *value
= NULL
;
935 if (S_ISLNK(ip
->i_mode
))
936 return (-EOPNOTSUPP
);
939 case ACL_TYPE_ACCESS
:
940 name
= XATTR_NAME_POSIX_ACL_ACCESS
;
942 umode_t mode
= ip
->i_mode
;
943 error
= posix_acl_equiv_mode(acl
, &mode
);
948 * The mode bits will have been set by
949 * ->zfs_setattr()->zfs_acl_chmod_setattr()
950 * using the ZFS ACL conversion. If they
951 * differ from the Posix ACL conversion dirty
952 * the inode to write the Posix mode bits.
954 if (ip
->i_mode
!= mode
) {
956 ip
->i_ctime
= current_time(ip
);
957 zfs_mark_inode_dirty(ip
);
966 case ACL_TYPE_DEFAULT
:
967 name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
968 if (!S_ISDIR(ip
->i_mode
))
969 return (acl
? -EACCES
: 0);
977 size
= posix_acl_xattr_size(acl
->a_count
);
978 value
= kmem_alloc(size
, KM_SLEEP
);
980 error
= zpl_acl_to_xattr(acl
, value
, size
);
982 kmem_free(value
, size
);
987 error
= zpl_xattr_set(ip
, name
, value
, size
, 0);
989 kmem_free(value
, size
);
993 zpl_set_cached_acl(ip
, type
, acl
);
995 zpl_forget_cached_acl(ip
, type
);
1003 #ifdef HAVE_SET_ACL_USERNS
1004 zpl_set_acl(struct user_namespace
*userns
, struct inode
*ip
,
1005 struct posix_acl
*acl
, int type
)
1007 zpl_set_acl(struct inode
*ip
, struct posix_acl
*acl
, int type
)
1008 #endif /* HAVE_SET_ACL_USERNS */
1010 return (zpl_set_acl_impl(ip
, acl
, type
));
1012 #endif /* HAVE_SET_ACL */
1014 static struct posix_acl
*
1015 zpl_get_acl_impl(struct inode
*ip
, int type
)
1017 struct posix_acl
*acl
;
1022 * As of Linux 3.14, the kernel get_acl will check this for us.
1023 * Also as of Linux 4.7, comparing against ACL_NOT_CACHED is wrong
1024 * as the kernel get_acl will set it to temporary sentinel value.
1026 #ifndef HAVE_KERNEL_GET_ACL_HANDLE_CACHE
1027 acl
= get_cached_acl(ip
, type
);
1028 if (acl
!= ACL_NOT_CACHED
)
1033 case ACL_TYPE_ACCESS
:
1034 name
= XATTR_NAME_POSIX_ACL_ACCESS
;
1036 case ACL_TYPE_DEFAULT
:
1037 name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
1040 return (ERR_PTR(-EINVAL
));
1043 int size
= zpl_xattr_get(ip
, name
, NULL
, 0);
1045 value
= kmem_alloc(size
, KM_SLEEP
);
1046 size
= zpl_xattr_get(ip
, name
, value
, size
);
1050 acl
= zpl_acl_from_xattr(value
, size
);
1051 } else if (size
== -ENODATA
|| size
== -ENOSYS
) {
1054 acl
= ERR_PTR(-EIO
);
1058 kmem_free(value
, size
);
1060 /* As of Linux 4.7, the kernel get_acl will set this for us */
1061 #ifndef HAVE_KERNEL_GET_ACL_HANDLE_CACHE
1063 zpl_set_cached_acl(ip
, type
, acl
);
1069 #if defined(HAVE_GET_ACL_RCU)
1071 zpl_get_acl(struct inode
*ip
, int type
, bool rcu
)
1074 return (ERR_PTR(-ECHILD
));
1076 return (zpl_get_acl_impl(ip
, type
));
1078 #elif defined(HAVE_GET_ACL)
1080 zpl_get_acl(struct inode
*ip
, int type
)
1082 return (zpl_get_acl_impl(ip
, type
));
1085 #error "Unsupported iops->get_acl() implementation"
1086 #endif /* HAVE_GET_ACL_RCU */
1089 zpl_init_acl(struct inode
*ip
, struct inode
*dir
)
1091 struct posix_acl
*acl
= NULL
;
1094 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1097 if (!S_ISLNK(ip
->i_mode
)) {
1098 acl
= zpl_get_acl_impl(dir
, ACL_TYPE_DEFAULT
);
1100 return (PTR_ERR(acl
));
1102 ip
->i_mode
&= ~current_umask();
1103 ip
->i_ctime
= current_time(ip
);
1104 zfs_mark_inode_dirty(ip
);
1112 if (S_ISDIR(ip
->i_mode
)) {
1113 error
= zpl_set_acl_impl(ip
, acl
, ACL_TYPE_DEFAULT
);
1119 error
= __posix_acl_create(&acl
, GFP_KERNEL
, &mode
);
1122 zfs_mark_inode_dirty(ip
);
1124 error
= zpl_set_acl_impl(ip
, acl
,
1130 zpl_posix_acl_release(acl
);
1136 zpl_chmod_acl(struct inode
*ip
)
1138 struct posix_acl
*acl
;
1141 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1144 if (S_ISLNK(ip
->i_mode
))
1145 return (-EOPNOTSUPP
);
1147 acl
= zpl_get_acl_impl(ip
, ACL_TYPE_ACCESS
);
1148 if (IS_ERR(acl
) || !acl
)
1149 return (PTR_ERR(acl
));
1151 error
= __posix_acl_chmod(&acl
, GFP_KERNEL
, ip
->i_mode
);
1153 error
= zpl_set_acl_impl(ip
, acl
, ACL_TYPE_ACCESS
);
1155 zpl_posix_acl_release(acl
);
1161 __zpl_xattr_acl_list_access(struct inode
*ip
, char *list
, size_t list_size
,
1162 const char *name
, size_t name_len
)
1164 char *xattr_name
= XATTR_NAME_POSIX_ACL_ACCESS
;
1165 size_t xattr_size
= sizeof (XATTR_NAME_POSIX_ACL_ACCESS
);
1167 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1170 if (list
&& xattr_size
<= list_size
)
1171 memcpy(list
, xattr_name
, xattr_size
);
1173 return (xattr_size
);
1175 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_acl_list_access
);
1178 __zpl_xattr_acl_list_default(struct inode
*ip
, char *list
, size_t list_size
,
1179 const char *name
, size_t name_len
)
1181 char *xattr_name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
1182 size_t xattr_size
= sizeof (XATTR_NAME_POSIX_ACL_DEFAULT
);
1184 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1187 if (list
&& xattr_size
<= list_size
)
1188 memcpy(list
, xattr_name
, xattr_size
);
1190 return (xattr_size
);
1192 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_acl_list_default
);
1195 __zpl_xattr_acl_get_access(struct inode
*ip
, const char *name
,
1196 void *buffer
, size_t size
)
1198 struct posix_acl
*acl
;
1199 int type
= ACL_TYPE_ACCESS
;
1201 /* xattr_resolve_name will do this for us if this is defined */
1202 #ifndef HAVE_XATTR_HANDLER_NAME
1203 if (strcmp(name
, "") != 0)
1206 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1207 return (-EOPNOTSUPP
);
1209 acl
= zpl_get_acl_impl(ip
, type
);
1211 return (PTR_ERR(acl
));
1215 error
= zpl_acl_to_xattr(acl
, buffer
, size
);
1216 zpl_posix_acl_release(acl
);
1220 ZPL_XATTR_GET_WRAPPER(zpl_xattr_acl_get_access
);
1223 __zpl_xattr_acl_get_default(struct inode
*ip
, const char *name
,
1224 void *buffer
, size_t size
)
1226 struct posix_acl
*acl
;
1227 int type
= ACL_TYPE_DEFAULT
;
1229 /* xattr_resolve_name will do this for us if this is defined */
1230 #ifndef HAVE_XATTR_HANDLER_NAME
1231 if (strcmp(name
, "") != 0)
1234 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1235 return (-EOPNOTSUPP
);
1237 acl
= zpl_get_acl_impl(ip
, type
);
1239 return (PTR_ERR(acl
));
1243 error
= zpl_acl_to_xattr(acl
, buffer
, size
);
1244 zpl_posix_acl_release(acl
);
1248 ZPL_XATTR_GET_WRAPPER(zpl_xattr_acl_get_default
);
1251 __zpl_xattr_acl_set_access(struct inode
*ip
, const char *name
,
1252 const void *value
, size_t size
, int flags
)
1254 struct posix_acl
*acl
;
1255 int type
= ACL_TYPE_ACCESS
;
1257 /* xattr_resolve_name will do this for us if this is defined */
1258 #ifndef HAVE_XATTR_HANDLER_NAME
1259 if (strcmp(name
, "") != 0)
1262 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1263 return (-EOPNOTSUPP
);
1265 if (!zpl_inode_owner_or_capable(kcred
->user_ns
, ip
))
1269 acl
= zpl_acl_from_xattr(value
, size
);
1271 return (PTR_ERR(acl
));
1273 error
= zpl_posix_acl_valid(ip
, acl
);
1275 zpl_posix_acl_release(acl
);
1282 error
= zpl_set_acl_impl(ip
, acl
, type
);
1283 zpl_posix_acl_release(acl
);
1287 ZPL_XATTR_SET_WRAPPER(zpl_xattr_acl_set_access
);
1290 __zpl_xattr_acl_set_default(struct inode
*ip
, const char *name
,
1291 const void *value
, size_t size
, int flags
)
1293 struct posix_acl
*acl
;
1294 int type
= ACL_TYPE_DEFAULT
;
1296 /* xattr_resolve_name will do this for us if this is defined */
1297 #ifndef HAVE_XATTR_HANDLER_NAME
1298 if (strcmp(name
, "") != 0)
1301 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1302 return (-EOPNOTSUPP
);
1304 if (!zpl_inode_owner_or_capable(kcred
->user_ns
, ip
))
1308 acl
= zpl_acl_from_xattr(value
, size
);
1310 return (PTR_ERR(acl
));
1312 error
= zpl_posix_acl_valid(ip
, acl
);
1314 zpl_posix_acl_release(acl
);
1322 error
= zpl_set_acl_impl(ip
, acl
, type
);
1323 zpl_posix_acl_release(acl
);
1327 ZPL_XATTR_SET_WRAPPER(zpl_xattr_acl_set_default
);
1330 * ACL access xattr namespace handlers.
1332 * Use .name instead of .prefix when available. xattr_resolve_name will match
1333 * whole name and reject anything that has .name only as prefix.
1335 static xattr_handler_t zpl_xattr_acl_access_handler
= {
1336 #ifdef HAVE_XATTR_HANDLER_NAME
1337 .name
= XATTR_NAME_POSIX_ACL_ACCESS
,
1339 .prefix
= XATTR_NAME_POSIX_ACL_ACCESS
,
1341 .list
= zpl_xattr_acl_list_access
,
1342 .get
= zpl_xattr_acl_get_access
,
1343 .set
= zpl_xattr_acl_set_access
,
1344 #if defined(HAVE_XATTR_LIST_SIMPLE) || \
1345 defined(HAVE_XATTR_LIST_DENTRY) || \
1346 defined(HAVE_XATTR_LIST_HANDLER)
1347 .flags
= ACL_TYPE_ACCESS
,
1352 * ACL default xattr namespace handlers.
1354 * Use .name instead of .prefix when available. xattr_resolve_name will match
1355 * whole name and reject anything that has .name only as prefix.
1357 static xattr_handler_t zpl_xattr_acl_default_handler
= {
1358 #ifdef HAVE_XATTR_HANDLER_NAME
1359 .name
= XATTR_NAME_POSIX_ACL_DEFAULT
,
1361 .prefix
= XATTR_NAME_POSIX_ACL_DEFAULT
,
1363 .list
= zpl_xattr_acl_list_default
,
1364 .get
= zpl_xattr_acl_get_default
,
1365 .set
= zpl_xattr_acl_set_default
,
1366 #if defined(HAVE_XATTR_LIST_SIMPLE) || \
1367 defined(HAVE_XATTR_LIST_DENTRY) || \
1368 defined(HAVE_XATTR_LIST_HANDLER)
1369 .flags
= ACL_TYPE_DEFAULT
,
1373 #endif /* CONFIG_FS_POSIX_ACL */
1375 xattr_handler_t
*zpl_xattr_handlers
[] = {
1376 &zpl_xattr_security_handler
,
1377 &zpl_xattr_trusted_handler
,
1378 &zpl_xattr_user_handler
,
1379 #ifdef CONFIG_FS_POSIX_ACL
1380 &zpl_xattr_acl_access_handler
,
1381 &zpl_xattr_acl_default_handler
,
1382 #endif /* CONFIG_FS_POSIX_ACL */
1386 static const struct xattr_handler
*
1387 zpl_xattr_handler(const char *name
)
1389 if (strncmp(name
, XATTR_USER_PREFIX
,
1390 XATTR_USER_PREFIX_LEN
) == 0)
1391 return (&zpl_xattr_user_handler
);
1393 if (strncmp(name
, XATTR_TRUSTED_PREFIX
,
1394 XATTR_TRUSTED_PREFIX_LEN
) == 0)
1395 return (&zpl_xattr_trusted_handler
);
1397 if (strncmp(name
, XATTR_SECURITY_PREFIX
,
1398 XATTR_SECURITY_PREFIX_LEN
) == 0)
1399 return (&zpl_xattr_security_handler
);
1401 #ifdef CONFIG_FS_POSIX_ACL
1402 if (strncmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
,
1403 sizeof (XATTR_NAME_POSIX_ACL_ACCESS
)) == 0)
1404 return (&zpl_xattr_acl_access_handler
);
1406 if (strncmp(name
, XATTR_NAME_POSIX_ACL_DEFAULT
,
1407 sizeof (XATTR_NAME_POSIX_ACL_DEFAULT
)) == 0)
1408 return (&zpl_xattr_acl_default_handler
);
1409 #endif /* CONFIG_FS_POSIX_ACL */
1414 #if !defined(HAVE_POSIX_ACL_RELEASE) || defined(HAVE_POSIX_ACL_RELEASE_GPL_ONLY)
1415 struct acl_rel_struct
{
1416 struct acl_rel_struct
*next
;
1417 struct posix_acl
*acl
;
1421 #define ACL_REL_GRACE (60*HZ)
1422 #define ACL_REL_WINDOW (1*HZ)
1423 #define ACL_REL_SCHED (ACL_REL_GRACE+ACL_REL_WINDOW)
1426 * Lockless multi-producer single-consumer fifo list.
1427 * Nodes are added to tail and removed from head. Tail pointer is our
1428 * synchronization point. It always points to the next pointer of the last
1429 * node, or head if list is empty.
1431 static struct acl_rel_struct
*acl_rel_head
= NULL
;
1432 static struct acl_rel_struct
**acl_rel_tail
= &acl_rel_head
;
1435 zpl_posix_acl_free(void *arg
)
1437 struct acl_rel_struct
*freelist
= NULL
;
1438 struct acl_rel_struct
*a
;
1440 boolean_t refire
= B_FALSE
;
1442 ASSERT3P(acl_rel_head
, !=, NULL
);
1443 while (acl_rel_head
) {
1445 if (ddi_get_lbolt() - a
->time
>= ACL_REL_GRACE
) {
1447 * If a is the last node we need to reset tail, but we
1448 * need to use cmpxchg to make sure it is still the
1451 if (acl_rel_tail
== &a
->next
) {
1452 acl_rel_head
= NULL
;
1453 if (cmpxchg(&acl_rel_tail
, &a
->next
,
1454 &acl_rel_head
) == &a
->next
) {
1455 ASSERT3P(a
->next
, ==, NULL
);
1462 * a is not last node, make sure next pointer is set
1463 * by the adder and advance the head.
1465 while (READ_ONCE(a
->next
) == NULL
)
1467 acl_rel_head
= a
->next
;
1472 * a is still in grace period. We are responsible to
1473 * reschedule the free task, since adder will only do
1474 * so if list is empty.
1476 new_time
= a
->time
+ ACL_REL_SCHED
;
1483 taskq_dispatch_delay(system_delay_taskq
, zpl_posix_acl_free
,
1484 NULL
, TQ_SLEEP
, new_time
);
1490 kmem_free(a
, sizeof (struct acl_rel_struct
));
1495 zpl_posix_acl_release_impl(struct posix_acl
*acl
)
1497 struct acl_rel_struct
*a
, **prev
;
1499 a
= kmem_alloc(sizeof (struct acl_rel_struct
), KM_SLEEP
);
1502 a
->time
= ddi_get_lbolt();
1503 /* atomically points tail to us and get the previous tail */
1504 prev
= xchg(&acl_rel_tail
, &a
->next
);
1505 ASSERT3P(*prev
, ==, NULL
);
1507 /* if it was empty before, schedule the free task */
1508 if (prev
== &acl_rel_head
)
1509 taskq_dispatch_delay(system_delay_taskq
, zpl_posix_acl_free
,
1510 NULL
, TQ_SLEEP
, ddi_get_lbolt() + ACL_REL_SCHED
);