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 https://opensource.org/licenses/CDDL-1.0.
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>
86 #include <linux/vfs_compat.h>
88 enum xattr_permission
{
94 typedef struct xattr_filldir
{
98 struct dentry
*dentry
;
101 static enum xattr_permission
zpl_xattr_permission(xattr_filldir_t
*,
104 static int zfs_xattr_compat
= 0;
107 * Determine is a given xattr name should be visible and if so copy it
108 * in to the provided buffer (xf->buf).
111 zpl_xattr_filldir(xattr_filldir_t
*xf
, const char *name
, int name_len
)
113 enum xattr_permission perm
;
115 /* Check permissions using the per-namespace list xattr handler. */
116 perm
= zpl_xattr_permission(xf
, name
, name_len
);
117 if (perm
== XAPERM_DENY
)
120 /* Prefix the name with "user." if it does not have a namespace. */
121 if (perm
== XAPERM_COMPAT
) {
123 if (xf
->offset
+ XATTR_USER_PREFIX_LEN
+ 1 > xf
->size
)
126 memcpy(xf
->buf
+ xf
->offset
, XATTR_USER_PREFIX
,
127 XATTR_USER_PREFIX_LEN
);
128 xf
->buf
[xf
->offset
+ XATTR_USER_PREFIX_LEN
] = '\0';
131 xf
->offset
+= XATTR_USER_PREFIX_LEN
;
134 /* When xf->buf is NULL only calculate the required size. */
136 if (xf
->offset
+ name_len
+ 1 > xf
->size
)
139 memcpy(xf
->buf
+ xf
->offset
, name
, name_len
);
140 xf
->buf
[xf
->offset
+ name_len
] = '\0';
143 xf
->offset
+= (name_len
+ 1);
149 * Read as many directory entry names as will fit in to the provided buffer,
150 * or when no buffer is provided calculate the required buffer size.
153 zpl_xattr_readdir(struct inode
*dxip
, xattr_filldir_t
*xf
)
159 zap_cursor_init(&zc
, ITOZSB(dxip
)->z_os
, ITOZ(dxip
)->z_id
);
161 while ((error
= -zap_cursor_retrieve(&zc
, &zap
)) == 0) {
163 if (zap
.za_integer_length
!= 8 || zap
.za_num_integers
!= 1) {
168 error
= zpl_xattr_filldir(xf
, zap
.za_name
, strlen(zap
.za_name
));
172 zap_cursor_advance(&zc
);
175 zap_cursor_fini(&zc
);
177 if (error
== -ENOENT
)
184 zpl_xattr_list_dir(xattr_filldir_t
*xf
, cred_t
*cr
)
186 struct inode
*ip
= xf
->dentry
->d_inode
;
187 struct inode
*dxip
= NULL
;
191 /* Lookup the xattr directory */
192 error
= -zfs_lookup(ITOZ(ip
), NULL
, &dxzp
, LOOKUP_XATTR
,
195 if (error
== -ENOENT
)
202 error
= zpl_xattr_readdir(dxip
, xf
);
209 zpl_xattr_list_sa(xattr_filldir_t
*xf
)
211 znode_t
*zp
= ITOZ(xf
->dentry
->d_inode
);
212 nvpair_t
*nvp
= NULL
;
215 mutex_enter(&zp
->z_lock
);
216 if (zp
->z_xattr_cached
== NULL
)
217 error
= -zfs_sa_get_xattr(zp
);
218 mutex_exit(&zp
->z_lock
);
223 ASSERT(zp
->z_xattr_cached
);
225 while ((nvp
= nvlist_next_nvpair(zp
->z_xattr_cached
, nvp
)) != NULL
) {
226 ASSERT3U(nvpair_type(nvp
), ==, DATA_TYPE_BYTE_ARRAY
);
228 error
= zpl_xattr_filldir(xf
, nvpair_name(nvp
),
229 strlen(nvpair_name(nvp
)));
238 zpl_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
240 znode_t
*zp
= ITOZ(dentry
->d_inode
);
241 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
242 xattr_filldir_t xf
= { buffer_size
, 0, buffer
, dentry
};
244 fstrans_cookie_t cookie
;
248 cookie
= spl_fstrans_mark();
249 if ((error
= zpl_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
251 rw_enter(&zp
->z_xattr_lock
, RW_READER
);
253 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
) {
254 error
= zpl_xattr_list_sa(&xf
);
259 error
= zpl_xattr_list_dir(&xf
, cr
);
266 rw_exit(&zp
->z_xattr_lock
);
267 zpl_exit(zfsvfs
, FTAG
);
269 spl_fstrans_unmark(cookie
);
276 zpl_xattr_get_dir(struct inode
*ip
, const char *name
, void *value
,
277 size_t size
, cred_t
*cr
)
279 fstrans_cookie_t cookie
;
280 struct inode
*xip
= NULL
;
281 znode_t
*dxzp
= NULL
;
285 /* Lookup the xattr directory */
286 error
= -zfs_lookup(ITOZ(ip
), NULL
, &dxzp
, LOOKUP_XATTR
,
291 /* Lookup a specific xattr name in the directory */
292 error
= -zfs_lookup(dxzp
, (char *)name
, &xzp
, 0, cr
, NULL
, NULL
);
298 error
= i_size_read(xip
);
302 if (size
< i_size_read(xip
)) {
308 iov
.iov_base
= (void *)value
;
312 zfs_uio_iovec_init(&uio
, &iov
, 1, 0, UIO_SYSSPACE
, size
, 0);
314 cookie
= spl_fstrans_mark();
315 error
= -zfs_read(ITOZ(xip
), &uio
, 0, cr
);
316 spl_fstrans_unmark(cookie
);
319 error
= size
- zfs_uio_resid(&uio
);
331 zpl_xattr_get_sa(struct inode
*ip
, const char *name
, void *value
, size_t size
)
333 znode_t
*zp
= ITOZ(ip
);
338 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
340 mutex_enter(&zp
->z_lock
);
341 if (zp
->z_xattr_cached
== NULL
)
342 error
= -zfs_sa_get_xattr(zp
);
343 mutex_exit(&zp
->z_lock
);
348 ASSERT(zp
->z_xattr_cached
);
349 error
= -nvlist_lookup_byte_array(zp
->z_xattr_cached
, name
,
350 &nv_value
, &nv_size
);
354 if (size
== 0 || value
== NULL
)
360 memcpy(value
, nv_value
, nv_size
);
366 __zpl_xattr_get(struct inode
*ip
, const char *name
, void *value
, size_t size
,
369 znode_t
*zp
= ITOZ(ip
);
370 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
373 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
375 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
) {
376 error
= zpl_xattr_get_sa(ip
, name
, value
, size
);
377 if (error
!= -ENOENT
)
381 error
= zpl_xattr_get_dir(ip
, name
, value
, size
, cr
);
383 if (error
== -ENOENT
)
389 #define XATTR_NOENT 0x0
390 #define XATTR_IN_SA 0x1
391 #define XATTR_IN_DIR 0x2
392 /* check where the xattr resides */
394 __zpl_xattr_where(struct inode
*ip
, const char *name
, int *where
, cred_t
*cr
)
396 znode_t
*zp
= ITOZ(ip
);
397 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
401 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
403 *where
= XATTR_NOENT
;
404 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
) {
405 error
= zpl_xattr_get_sa(ip
, name
, NULL
, 0);
407 *where
|= XATTR_IN_SA
;
408 else if (error
!= -ENOENT
)
412 error
= zpl_xattr_get_dir(ip
, name
, NULL
, 0, cr
);
414 *where
|= XATTR_IN_DIR
;
415 else if (error
!= -ENOENT
)
418 if (*where
== (XATTR_IN_SA
|XATTR_IN_DIR
))
419 cmn_err(CE_WARN
, "ZFS: inode %p has xattr \"%s\""
420 " in both SA and dir", ip
, name
);
421 if (*where
== XATTR_NOENT
)
429 zpl_xattr_get(struct inode
*ip
, const char *name
, void *value
, size_t size
)
431 znode_t
*zp
= ITOZ(ip
);
432 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
434 fstrans_cookie_t cookie
;
438 cookie
= spl_fstrans_mark();
439 if ((error
= zpl_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
441 rw_enter(&zp
->z_xattr_lock
, RW_READER
);
442 error
= __zpl_xattr_get(ip
, name
, value
, size
, cr
);
443 rw_exit(&zp
->z_xattr_lock
);
444 zpl_exit(zfsvfs
, FTAG
);
446 spl_fstrans_unmark(cookie
);
453 zpl_xattr_set_dir(struct inode
*ip
, const char *name
, const void *value
,
454 size_t size
, int flags
, cred_t
*cr
)
456 znode_t
*dxzp
= NULL
;
459 int lookup_flags
, error
;
460 const int xattr_mode
= S_IFREG
| 0644;
464 * Lookup the xattr directory. When we're adding an entry pass
465 * CREATE_XATTR_DIR to ensure the xattr directory is created.
466 * When removing an entry this flag is not passed to avoid
467 * unnecessarily creating a new xattr directory.
469 lookup_flags
= LOOKUP_XATTR
;
471 lookup_flags
|= CREATE_XATTR_DIR
;
473 error
= -zfs_lookup(ITOZ(ip
), NULL
, &dxzp
, lookup_flags
,
478 /* Lookup a specific xattr name in the directory */
479 error
= -zfs_lookup(dxzp
, (char *)name
, &xzp
, 0, cr
, NULL
, NULL
);
480 if (error
&& (error
!= -ENOENT
))
485 /* Remove a specific name xattr when value is set to NULL. */
488 error
= -zfs_remove(dxzp
, (char *)name
, cr
, 0);
493 /* Lookup failed create a new xattr. */
495 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
496 vap
->va_mode
= xattr_mode
;
497 vap
->va_mask
= ATTR_MODE
;
498 vap
->va_uid
= crgetuid(cr
);
499 vap
->va_gid
= crgetgid(cr
);
501 error
= -zfs_create(dxzp
, (char *)name
, vap
, 0, 0644, &xzp
,
509 error
= -zfs_freesp(xzp
, 0, 0, xattr_mode
, TRUE
);
513 error
= -zfs_write_simple(xzp
, value
, size
, pos
, NULL
);
516 ip
->i_ctime
= current_time(ip
);
517 zfs_mark_inode_dirty(ip
);
521 kmem_free(vap
, sizeof (vattr_t
));
529 if (error
== -ENOENT
)
532 ASSERT3S(error
, <=, 0);
538 zpl_xattr_set_sa(struct inode
*ip
, const char *name
, const void *value
,
539 size_t size
, int flags
, cred_t
*cr
)
541 znode_t
*zp
= ITOZ(ip
);
546 mutex_enter(&zp
->z_lock
);
547 if (zp
->z_xattr_cached
== NULL
)
548 error
= -zfs_sa_get_xattr(zp
);
549 mutex_exit(&zp
->z_lock
);
554 ASSERT(zp
->z_xattr_cached
);
555 nvl
= zp
->z_xattr_cached
;
558 error
= -nvlist_remove(nvl
, name
, DATA_TYPE_BYTE_ARRAY
);
559 if (error
== -ENOENT
)
560 error
= zpl_xattr_set_dir(ip
, name
, NULL
, 0, flags
, cr
);
562 /* Limited to 32k to keep nvpair memory allocations small */
563 if (size
> DXATTR_MAX_ENTRY_SIZE
)
566 /* Prevent the DXATTR SA from consuming the entire SA region */
567 error
= -nvlist_size(nvl
, &sa_size
, NV_ENCODE_XDR
);
571 if (sa_size
> DXATTR_MAX_SA_SIZE
)
574 error
= -nvlist_add_byte_array(nvl
, name
,
575 (uchar_t
*)value
, size
);
579 * Update the SA for additions, modifications, and removals. On
580 * error drop the inconsistent cached version of the nvlist, it
581 * will be reconstructed from the ARC when next accessed.
584 error
= -zfs_sa_set_xattr(zp
, name
, value
, size
);
588 zp
->z_xattr_cached
= NULL
;
591 ASSERT3S(error
, <=, 0);
597 zpl_xattr_set(struct inode
*ip
, const char *name
, const void *value
,
598 size_t size
, int flags
)
600 znode_t
*zp
= ITOZ(ip
);
601 zfsvfs_t
*zfsvfs
= ZTOZSB(zp
);
603 fstrans_cookie_t cookie
;
608 cookie
= spl_fstrans_mark();
609 if ((error
= zpl_enter_verify_zp(zfsvfs
, zp
, FTAG
)) != 0)
611 rw_enter(&zp
->z_xattr_lock
, RW_WRITER
);
614 * Before setting the xattr check to see if it already exists.
615 * This is done to ensure the following optional flags are honored.
617 * XATTR_CREATE: fail if xattr already exists
618 * XATTR_REPLACE: fail if xattr does not exist
620 * We also want to know if it resides in sa or dir, so we can make
621 * sure we don't end up with duplicate in both places.
623 error
= __zpl_xattr_where(ip
, name
, &where
, cr
);
625 if (error
!= -ENODATA
)
627 if (flags
& XATTR_REPLACE
)
630 /* The xattr to be removed already doesn't exist */
636 if (flags
& XATTR_CREATE
)
640 /* Preferentially store the xattr as a SA for better performance */
641 if (zfsvfs
->z_use_sa
&& zp
->z_is_sa
&&
642 (zfsvfs
->z_xattr_sa
|| (value
== NULL
&& where
& XATTR_IN_SA
))) {
643 error
= zpl_xattr_set_sa(ip
, name
, value
, size
, flags
, cr
);
646 * Successfully put into SA, we need to clear the one
649 if (where
& XATTR_IN_DIR
)
650 zpl_xattr_set_dir(ip
, name
, NULL
, 0, 0, cr
);
655 error
= zpl_xattr_set_dir(ip
, name
, value
, size
, flags
, cr
);
657 * Successfully put into dir, we need to clear the one in SA.
659 if (error
== 0 && (where
& XATTR_IN_SA
))
660 zpl_xattr_set_sa(ip
, name
, NULL
, 0, 0, cr
);
662 rw_exit(&zp
->z_xattr_lock
);
663 zpl_exit(zfsvfs
, FTAG
);
665 spl_fstrans_unmark(cookie
);
667 ASSERT3S(error
, <=, 0);
673 * Extended user attributes
675 * "Extended user attributes may be assigned to files and directories for
676 * storing arbitrary additional information such as the mime type,
677 * character set or encoding of a file. The access permissions for user
678 * attributes are defined by the file permission bits: read permission
679 * is required to retrieve the attribute value, and writer permission is
680 * required to change it.
682 * The file permission bits of regular files and directories are
683 * interpreted differently from the file permission bits of special
684 * files and symbolic links. For regular files and directories the file
685 * permission bits define access to the file's contents, while for
686 * device special files they define access to the device described by
687 * the special file. The file permissions of symbolic links are not
688 * used in access checks. These differences would allow users to
689 * consume filesystem resources in a way not controllable by disk quotas
690 * for group or world writable special files and directories.
692 * For this reason, extended user attributes are allowed only for
693 * regular files and directories, and access to extended user attributes
694 * is restricted to the owner and to users with appropriate capabilities
695 * for directories with the sticky bit set (see the chmod(1) manual page
696 * for an explanation of the sticky bit)." - xattr(7)
698 * ZFS allows extended user attributes to be disabled administratively
699 * by setting the 'xattr=off' property on the dataset.
702 __zpl_xattr_user_list(struct inode
*ip
, char *list
, size_t list_size
,
703 const char *name
, size_t name_len
)
705 return (ITOZSB(ip
)->z_flags
& ZSB_XATTR
);
707 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_user_list
);
710 __zpl_xattr_user_get(struct inode
*ip
, const char *name
,
711 void *value
, size_t size
)
714 /* xattr_resolve_name will do this for us if this is defined */
715 #ifndef HAVE_XATTR_HANDLER_NAME
716 if (strcmp(name
, "") == 0)
719 if (ZFS_XA_NS_PREFIX_FORBIDDEN(name
))
721 if (!(ITOZSB(ip
)->z_flags
& ZSB_XATTR
))
722 return (-EOPNOTSUPP
);
725 * Try to look up the name with the namespace prefix first for
726 * compatibility with xattrs from this platform. If that fails,
727 * try again without the namespace prefix for compatibility with
730 char *xattr_name
= kmem_asprintf("%s%s", XATTR_USER_PREFIX
, name
);
731 error
= zpl_xattr_get(ip
, xattr_name
, value
, size
);
732 kmem_strfree(xattr_name
);
733 if (error
== -ENODATA
)
734 error
= zpl_xattr_get(ip
, name
, value
, size
);
738 ZPL_XATTR_GET_WRAPPER(zpl_xattr_user_get
);
741 __zpl_xattr_user_set(struct inode
*ip
, const char *name
,
742 const void *value
, size_t size
, int flags
)
745 /* xattr_resolve_name will do this for us if this is defined */
746 #ifndef HAVE_XATTR_HANDLER_NAME
747 if (strcmp(name
, "") == 0)
750 if (ZFS_XA_NS_PREFIX_FORBIDDEN(name
))
752 if (!(ITOZSB(ip
)->z_flags
& ZSB_XATTR
))
753 return (-EOPNOTSUPP
);
756 * Remove alternate compat version of the xattr so we only set the
757 * version specified by the zfs_xattr_compat tunable.
759 * The following flags must be handled correctly:
761 * XATTR_CREATE: fail if xattr already exists
762 * XATTR_REPLACE: fail if xattr does not exist
764 char *prefixed_name
= kmem_asprintf("%s%s", XATTR_USER_PREFIX
, name
);
765 const char *clear_name
, *set_name
;
766 if (zfs_xattr_compat
) {
767 clear_name
= prefixed_name
;
771 set_name
= prefixed_name
;
774 * Clear the old value with the alternative name format, if it exists.
776 error
= zpl_xattr_set(ip
, clear_name
, NULL
, 0, flags
);
778 * XATTR_CREATE was specified and we failed to clear the xattr
779 * because it already exists. Stop here.
781 if (error
== -EEXIST
)
784 * If XATTR_REPLACE was specified and we succeeded to clear
785 * an xattr, we don't need to replace anything when setting
786 * the new value. If we failed with -ENODATA that's fine,
787 * there was nothing to be cleared and we can ignore the error.
790 flags
&= ~XATTR_REPLACE
;
792 * Set the new value with the configured name format.
794 error
= zpl_xattr_set(ip
, set_name
, value
, size
, flags
);
796 kmem_strfree(prefixed_name
);
799 ZPL_XATTR_SET_WRAPPER(zpl_xattr_user_set
);
801 static xattr_handler_t zpl_xattr_user_handler
=
803 .prefix
= XATTR_USER_PREFIX
,
804 .list
= zpl_xattr_user_list
,
805 .get
= zpl_xattr_user_get
,
806 .set
= zpl_xattr_user_set
,
810 * Trusted extended attributes
812 * "Trusted extended attributes are visible and accessible only to
813 * processes that have the CAP_SYS_ADMIN capability. Attributes in this
814 * class are used to implement mechanisms in user space (i.e., outside
815 * the kernel) which keep information in extended attributes to which
816 * ordinary processes should not have access." - xattr(7)
819 __zpl_xattr_trusted_list(struct inode
*ip
, char *list
, size_t list_size
,
820 const char *name
, size_t name_len
)
822 return (capable(CAP_SYS_ADMIN
));
824 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_trusted_list
);
827 __zpl_xattr_trusted_get(struct inode
*ip
, const char *name
,
828 void *value
, size_t size
)
833 if (!capable(CAP_SYS_ADMIN
))
835 /* xattr_resolve_name will do this for us if this is defined */
836 #ifndef HAVE_XATTR_HANDLER_NAME
837 if (strcmp(name
, "") == 0)
840 xattr_name
= kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX
, name
);
841 error
= zpl_xattr_get(ip
, xattr_name
, value
, size
);
842 kmem_strfree(xattr_name
);
846 ZPL_XATTR_GET_WRAPPER(zpl_xattr_trusted_get
);
849 __zpl_xattr_trusted_set(struct inode
*ip
, const char *name
,
850 const void *value
, size_t size
, int flags
)
855 if (!capable(CAP_SYS_ADMIN
))
857 /* xattr_resolve_name will do this for us if this is defined */
858 #ifndef HAVE_XATTR_HANDLER_NAME
859 if (strcmp(name
, "") == 0)
862 xattr_name
= kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX
, name
);
863 error
= zpl_xattr_set(ip
, xattr_name
, value
, size
, flags
);
864 kmem_strfree(xattr_name
);
868 ZPL_XATTR_SET_WRAPPER(zpl_xattr_trusted_set
);
870 static xattr_handler_t zpl_xattr_trusted_handler
= {
871 .prefix
= XATTR_TRUSTED_PREFIX
,
872 .list
= zpl_xattr_trusted_list
,
873 .get
= zpl_xattr_trusted_get
,
874 .set
= zpl_xattr_trusted_set
,
878 * Extended security attributes
880 * "The security attribute namespace is used by kernel security modules,
881 * such as Security Enhanced Linux, and also to implement file
882 * capabilities (see capabilities(7)). Read and write access
883 * permissions to security attributes depend on the policy implemented
884 * for each security attribute by the security module. When no security
885 * module is loaded, all processes have read access to extended security
886 * attributes, and write access is limited to processes that have the
887 * CAP_SYS_ADMIN capability." - xattr(7)
890 __zpl_xattr_security_list(struct inode
*ip
, char *list
, size_t list_size
,
891 const char *name
, size_t name_len
)
895 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_security_list
);
898 __zpl_xattr_security_get(struct inode
*ip
, const char *name
,
899 void *value
, size_t size
)
903 /* xattr_resolve_name will do this for us if this is defined */
904 #ifndef HAVE_XATTR_HANDLER_NAME
905 if (strcmp(name
, "") == 0)
908 xattr_name
= kmem_asprintf("%s%s", XATTR_SECURITY_PREFIX
, name
);
909 error
= zpl_xattr_get(ip
, xattr_name
, value
, size
);
910 kmem_strfree(xattr_name
);
914 ZPL_XATTR_GET_WRAPPER(zpl_xattr_security_get
);
917 __zpl_xattr_security_set(struct inode
*ip
, const char *name
,
918 const void *value
, size_t size
, int flags
)
922 /* xattr_resolve_name will do this for us if this is defined */
923 #ifndef HAVE_XATTR_HANDLER_NAME
924 if (strcmp(name
, "") == 0)
927 xattr_name
= kmem_asprintf("%s%s", XATTR_SECURITY_PREFIX
, name
);
928 error
= zpl_xattr_set(ip
, xattr_name
, value
, size
, flags
);
929 kmem_strfree(xattr_name
);
933 ZPL_XATTR_SET_WRAPPER(zpl_xattr_security_set
);
936 zpl_xattr_security_init_impl(struct inode
*ip
, const struct xattr
*xattrs
,
939 const struct xattr
*xattr
;
942 for (xattr
= xattrs
; xattr
->name
!= NULL
; xattr
++) {
943 error
= __zpl_xattr_security_set(ip
,
944 xattr
->name
, xattr
->value
, xattr
->value_len
, 0);
954 zpl_xattr_security_init(struct inode
*ip
, struct inode
*dip
,
955 const struct qstr
*qstr
)
957 return security_inode_init_security(ip
, dip
, qstr
,
958 &zpl_xattr_security_init_impl
, NULL
);
962 * Security xattr namespace handlers.
964 static xattr_handler_t zpl_xattr_security_handler
= {
965 .prefix
= XATTR_SECURITY_PREFIX
,
966 .list
= zpl_xattr_security_list
,
967 .get
= zpl_xattr_security_get
,
968 .set
= zpl_xattr_security_set
,
972 * Extended system attributes
974 * "Extended system attributes are used by the kernel to store system
975 * objects such as Access Control Lists. Read and write access permissions
976 * to system attributes depend on the policy implemented for each system
977 * attribute implemented by filesystems in the kernel." - xattr(7)
979 #ifdef CONFIG_FS_POSIX_ACL
981 zpl_set_acl_impl(struct inode
*ip
, struct posix_acl
*acl
, int type
)
983 char *name
, *value
= NULL
;
987 if (S_ISLNK(ip
->i_mode
))
988 return (-EOPNOTSUPP
);
991 case ACL_TYPE_ACCESS
:
992 name
= XATTR_NAME_POSIX_ACL_ACCESS
;
994 umode_t mode
= ip
->i_mode
;
995 error
= posix_acl_equiv_mode(acl
, &mode
);
1000 * The mode bits will have been set by
1001 * ->zfs_setattr()->zfs_acl_chmod_setattr()
1002 * using the ZFS ACL conversion. If they
1003 * differ from the Posix ACL conversion dirty
1004 * the inode to write the Posix mode bits.
1006 if (ip
->i_mode
!= mode
) {
1007 ip
->i_mode
= ITOZ(ip
)->z_mode
= mode
;
1008 ip
->i_ctime
= current_time(ip
);
1009 zfs_mark_inode_dirty(ip
);
1018 case ACL_TYPE_DEFAULT
:
1019 name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
1020 if (!S_ISDIR(ip
->i_mode
))
1021 return (acl
? -EACCES
: 0);
1029 size
= posix_acl_xattr_size(acl
->a_count
);
1030 value
= kmem_alloc(size
, KM_SLEEP
);
1032 error
= zpl_acl_to_xattr(acl
, value
, size
);
1034 kmem_free(value
, size
);
1039 error
= zpl_xattr_set(ip
, name
, value
, size
, 0);
1041 kmem_free(value
, size
);
1045 zpl_set_cached_acl(ip
, type
, acl
);
1047 zpl_forget_cached_acl(ip
, type
);
1055 #ifdef HAVE_SET_ACL_USERNS
1056 zpl_set_acl(struct user_namespace
*userns
, struct inode
*ip
,
1057 struct posix_acl
*acl
, int type
)
1059 zpl_set_acl(struct inode
*ip
, struct posix_acl
*acl
, int type
)
1060 #endif /* HAVE_SET_ACL_USERNS */
1062 return (zpl_set_acl_impl(ip
, acl
, type
));
1064 #endif /* HAVE_SET_ACL */
1066 static struct posix_acl
*
1067 zpl_get_acl_impl(struct inode
*ip
, int type
)
1069 struct posix_acl
*acl
;
1074 * As of Linux 3.14, the kernel get_acl will check this for us.
1075 * Also as of Linux 4.7, comparing against ACL_NOT_CACHED is wrong
1076 * as the kernel get_acl will set it to temporary sentinel value.
1078 #ifndef HAVE_KERNEL_GET_ACL_HANDLE_CACHE
1079 acl
= get_cached_acl(ip
, type
);
1080 if (acl
!= ACL_NOT_CACHED
)
1085 case ACL_TYPE_ACCESS
:
1086 name
= XATTR_NAME_POSIX_ACL_ACCESS
;
1088 case ACL_TYPE_DEFAULT
:
1089 name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
1092 return (ERR_PTR(-EINVAL
));
1095 int size
= zpl_xattr_get(ip
, name
, NULL
, 0);
1097 value
= kmem_alloc(size
, KM_SLEEP
);
1098 size
= zpl_xattr_get(ip
, name
, value
, size
);
1102 acl
= zpl_acl_from_xattr(value
, size
);
1103 } else if (size
== -ENODATA
|| size
== -ENOSYS
) {
1106 acl
= ERR_PTR(-EIO
);
1110 kmem_free(value
, size
);
1112 /* As of Linux 4.7, the kernel get_acl will set this for us */
1113 #ifndef HAVE_KERNEL_GET_ACL_HANDLE_CACHE
1115 zpl_set_cached_acl(ip
, type
, acl
);
1121 #if defined(HAVE_GET_ACL_RCU)
1123 zpl_get_acl(struct inode
*ip
, int type
, bool rcu
)
1126 return (ERR_PTR(-ECHILD
));
1128 return (zpl_get_acl_impl(ip
, type
));
1130 #elif defined(HAVE_GET_ACL)
1132 zpl_get_acl(struct inode
*ip
, int type
)
1134 return (zpl_get_acl_impl(ip
, type
));
1137 #error "Unsupported iops->get_acl() implementation"
1138 #endif /* HAVE_GET_ACL_RCU */
1141 zpl_init_acl(struct inode
*ip
, struct inode
*dir
)
1143 struct posix_acl
*acl
= NULL
;
1146 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1149 if (!S_ISLNK(ip
->i_mode
)) {
1150 acl
= zpl_get_acl_impl(dir
, ACL_TYPE_DEFAULT
);
1152 return (PTR_ERR(acl
));
1154 ITOZ(ip
)->z_mode
= (ip
->i_mode
&= ~current_umask());
1155 ip
->i_ctime
= current_time(ip
);
1156 zfs_mark_inode_dirty(ip
);
1164 if (S_ISDIR(ip
->i_mode
)) {
1165 error
= zpl_set_acl_impl(ip
, acl
, ACL_TYPE_DEFAULT
);
1171 error
= __posix_acl_create(&acl
, GFP_KERNEL
, &mode
);
1173 ip
->i_mode
= ITOZ(ip
)->z_mode
= mode
;
1174 zfs_mark_inode_dirty(ip
);
1176 error
= zpl_set_acl_impl(ip
, acl
,
1182 zpl_posix_acl_release(acl
);
1188 zpl_chmod_acl(struct inode
*ip
)
1190 struct posix_acl
*acl
;
1193 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1196 if (S_ISLNK(ip
->i_mode
))
1197 return (-EOPNOTSUPP
);
1199 acl
= zpl_get_acl_impl(ip
, ACL_TYPE_ACCESS
);
1200 if (IS_ERR(acl
) || !acl
)
1201 return (PTR_ERR(acl
));
1203 error
= __posix_acl_chmod(&acl
, GFP_KERNEL
, ip
->i_mode
);
1205 error
= zpl_set_acl_impl(ip
, acl
, ACL_TYPE_ACCESS
);
1207 zpl_posix_acl_release(acl
);
1213 __zpl_xattr_acl_list_access(struct inode
*ip
, char *list
, size_t list_size
,
1214 const char *name
, size_t name_len
)
1216 char *xattr_name
= XATTR_NAME_POSIX_ACL_ACCESS
;
1217 size_t xattr_size
= sizeof (XATTR_NAME_POSIX_ACL_ACCESS
);
1219 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1222 if (list
&& xattr_size
<= list_size
)
1223 memcpy(list
, xattr_name
, xattr_size
);
1225 return (xattr_size
);
1227 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_acl_list_access
);
1230 __zpl_xattr_acl_list_default(struct inode
*ip
, char *list
, size_t list_size
,
1231 const char *name
, size_t name_len
)
1233 char *xattr_name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
1234 size_t xattr_size
= sizeof (XATTR_NAME_POSIX_ACL_DEFAULT
);
1236 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1239 if (list
&& xattr_size
<= list_size
)
1240 memcpy(list
, xattr_name
, xattr_size
);
1242 return (xattr_size
);
1244 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_acl_list_default
);
1247 __zpl_xattr_acl_get_access(struct inode
*ip
, const char *name
,
1248 void *buffer
, size_t size
)
1250 struct posix_acl
*acl
;
1251 int type
= ACL_TYPE_ACCESS
;
1253 /* xattr_resolve_name will do this for us if this is defined */
1254 #ifndef HAVE_XATTR_HANDLER_NAME
1255 if (strcmp(name
, "") != 0)
1258 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1259 return (-EOPNOTSUPP
);
1261 acl
= zpl_get_acl_impl(ip
, type
);
1263 return (PTR_ERR(acl
));
1267 error
= zpl_acl_to_xattr(acl
, buffer
, size
);
1268 zpl_posix_acl_release(acl
);
1272 ZPL_XATTR_GET_WRAPPER(zpl_xattr_acl_get_access
);
1275 __zpl_xattr_acl_get_default(struct inode
*ip
, const char *name
,
1276 void *buffer
, size_t size
)
1278 struct posix_acl
*acl
;
1279 int type
= ACL_TYPE_DEFAULT
;
1281 /* xattr_resolve_name will do this for us if this is defined */
1282 #ifndef HAVE_XATTR_HANDLER_NAME
1283 if (strcmp(name
, "") != 0)
1286 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1287 return (-EOPNOTSUPP
);
1289 acl
= zpl_get_acl_impl(ip
, type
);
1291 return (PTR_ERR(acl
));
1295 error
= zpl_acl_to_xattr(acl
, buffer
, size
);
1296 zpl_posix_acl_release(acl
);
1300 ZPL_XATTR_GET_WRAPPER(zpl_xattr_acl_get_default
);
1303 __zpl_xattr_acl_set_access(struct inode
*ip
, const char *name
,
1304 const void *value
, size_t size
, int flags
)
1306 struct posix_acl
*acl
;
1307 int type
= ACL_TYPE_ACCESS
;
1309 /* xattr_resolve_name will do this for us if this is defined */
1310 #ifndef HAVE_XATTR_HANDLER_NAME
1311 if (strcmp(name
, "") != 0)
1314 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1315 return (-EOPNOTSUPP
);
1317 if (!zpl_inode_owner_or_capable(kcred
->user_ns
, ip
))
1321 acl
= zpl_acl_from_xattr(value
, size
);
1323 return (PTR_ERR(acl
));
1325 error
= zpl_posix_acl_valid(ip
, acl
);
1327 zpl_posix_acl_release(acl
);
1334 error
= zpl_set_acl_impl(ip
, acl
, type
);
1335 zpl_posix_acl_release(acl
);
1339 ZPL_XATTR_SET_WRAPPER(zpl_xattr_acl_set_access
);
1342 __zpl_xattr_acl_set_default(struct inode
*ip
, const char *name
,
1343 const void *value
, size_t size
, int flags
)
1345 struct posix_acl
*acl
;
1346 int type
= ACL_TYPE_DEFAULT
;
1348 /* xattr_resolve_name will do this for us if this is defined */
1349 #ifndef HAVE_XATTR_HANDLER_NAME
1350 if (strcmp(name
, "") != 0)
1353 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIX
)
1354 return (-EOPNOTSUPP
);
1356 if (!zpl_inode_owner_or_capable(kcred
->user_ns
, ip
))
1360 acl
= zpl_acl_from_xattr(value
, size
);
1362 return (PTR_ERR(acl
));
1364 error
= zpl_posix_acl_valid(ip
, acl
);
1366 zpl_posix_acl_release(acl
);
1374 error
= zpl_set_acl_impl(ip
, acl
, type
);
1375 zpl_posix_acl_release(acl
);
1379 ZPL_XATTR_SET_WRAPPER(zpl_xattr_acl_set_default
);
1382 * ACL access xattr namespace handlers.
1384 * Use .name instead of .prefix when available. xattr_resolve_name will match
1385 * whole name and reject anything that has .name only as prefix.
1387 static xattr_handler_t zpl_xattr_acl_access_handler
= {
1388 #ifdef HAVE_XATTR_HANDLER_NAME
1389 .name
= XATTR_NAME_POSIX_ACL_ACCESS
,
1391 .prefix
= XATTR_NAME_POSIX_ACL_ACCESS
,
1393 .list
= zpl_xattr_acl_list_access
,
1394 .get
= zpl_xattr_acl_get_access
,
1395 .set
= zpl_xattr_acl_set_access
,
1396 #if defined(HAVE_XATTR_LIST_SIMPLE) || \
1397 defined(HAVE_XATTR_LIST_DENTRY) || \
1398 defined(HAVE_XATTR_LIST_HANDLER)
1399 .flags
= ACL_TYPE_ACCESS
,
1404 * ACL default xattr namespace handlers.
1406 * Use .name instead of .prefix when available. xattr_resolve_name will match
1407 * whole name and reject anything that has .name only as prefix.
1409 static xattr_handler_t zpl_xattr_acl_default_handler
= {
1410 #ifdef HAVE_XATTR_HANDLER_NAME
1411 .name
= XATTR_NAME_POSIX_ACL_DEFAULT
,
1413 .prefix
= XATTR_NAME_POSIX_ACL_DEFAULT
,
1415 .list
= zpl_xattr_acl_list_default
,
1416 .get
= zpl_xattr_acl_get_default
,
1417 .set
= zpl_xattr_acl_set_default
,
1418 #if defined(HAVE_XATTR_LIST_SIMPLE) || \
1419 defined(HAVE_XATTR_LIST_DENTRY) || \
1420 defined(HAVE_XATTR_LIST_HANDLER)
1421 .flags
= ACL_TYPE_DEFAULT
,
1425 #endif /* CONFIG_FS_POSIX_ACL */
1427 xattr_handler_t
*zpl_xattr_handlers
[] = {
1428 &zpl_xattr_security_handler
,
1429 &zpl_xattr_trusted_handler
,
1430 &zpl_xattr_user_handler
,
1431 #ifdef CONFIG_FS_POSIX_ACL
1432 &zpl_xattr_acl_access_handler
,
1433 &zpl_xattr_acl_default_handler
,
1434 #endif /* CONFIG_FS_POSIX_ACL */
1438 static const struct xattr_handler
*
1439 zpl_xattr_handler(const char *name
)
1441 if (strncmp(name
, XATTR_USER_PREFIX
,
1442 XATTR_USER_PREFIX_LEN
) == 0)
1443 return (&zpl_xattr_user_handler
);
1445 if (strncmp(name
, XATTR_TRUSTED_PREFIX
,
1446 XATTR_TRUSTED_PREFIX_LEN
) == 0)
1447 return (&zpl_xattr_trusted_handler
);
1449 if (strncmp(name
, XATTR_SECURITY_PREFIX
,
1450 XATTR_SECURITY_PREFIX_LEN
) == 0)
1451 return (&zpl_xattr_security_handler
);
1453 #ifdef CONFIG_FS_POSIX_ACL
1454 if (strncmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
,
1455 sizeof (XATTR_NAME_POSIX_ACL_ACCESS
)) == 0)
1456 return (&zpl_xattr_acl_access_handler
);
1458 if (strncmp(name
, XATTR_NAME_POSIX_ACL_DEFAULT
,
1459 sizeof (XATTR_NAME_POSIX_ACL_DEFAULT
)) == 0)
1460 return (&zpl_xattr_acl_default_handler
);
1461 #endif /* CONFIG_FS_POSIX_ACL */
1466 static enum xattr_permission
1467 zpl_xattr_permission(xattr_filldir_t
*xf
, const char *name
, int name_len
)
1469 const struct xattr_handler
*handler
;
1470 struct dentry
*d __maybe_unused
= xf
->dentry
;
1471 enum xattr_permission perm
= XAPERM_ALLOW
;
1473 handler
= zpl_xattr_handler(name
);
1474 if (handler
== NULL
) {
1475 /* Do not expose FreeBSD system namespace xattrs. */
1476 if (ZFS_XA_NS_PREFIX_MATCH(FREEBSD
, name
))
1477 return (XAPERM_DENY
);
1479 * Anything that doesn't match a known namespace gets put in the
1480 * user namespace for compatibility with other platforms.
1482 perm
= XAPERM_COMPAT
;
1483 handler
= &zpl_xattr_user_handler
;
1486 if (handler
->list
) {
1487 #if defined(HAVE_XATTR_LIST_SIMPLE)
1488 if (!handler
->list(d
))
1489 return (XAPERM_DENY
);
1490 #elif defined(HAVE_XATTR_LIST_DENTRY)
1491 if (!handler
->list(d
, NULL
, 0, name
, name_len
, 0))
1492 return (XAPERM_DENY
);
1493 #elif defined(HAVE_XATTR_LIST_HANDLER)
1494 if (!handler
->list(handler
, d
, NULL
, 0, name
, name_len
))
1495 return (XAPERM_DENY
);
1502 #if defined(CONFIG_FS_POSIX_ACL) && \
1503 (!defined(HAVE_POSIX_ACL_RELEASE) || \
1504 defined(HAVE_POSIX_ACL_RELEASE_GPL_ONLY))
1505 struct acl_rel_struct
{
1506 struct acl_rel_struct
*next
;
1507 struct posix_acl
*acl
;
1511 #define ACL_REL_GRACE (60*HZ)
1512 #define ACL_REL_WINDOW (1*HZ)
1513 #define ACL_REL_SCHED (ACL_REL_GRACE+ACL_REL_WINDOW)
1516 * Lockless multi-producer single-consumer fifo list.
1517 * Nodes are added to tail and removed from head. Tail pointer is our
1518 * synchronization point. It always points to the next pointer of the last
1519 * node, or head if list is empty.
1521 static struct acl_rel_struct
*acl_rel_head
= NULL
;
1522 static struct acl_rel_struct
**acl_rel_tail
= &acl_rel_head
;
1525 zpl_posix_acl_free(void *arg
)
1527 struct acl_rel_struct
*freelist
= NULL
;
1528 struct acl_rel_struct
*a
;
1530 boolean_t refire
= B_FALSE
;
1532 ASSERT3P(acl_rel_head
, !=, NULL
);
1533 while (acl_rel_head
) {
1535 if (ddi_get_lbolt() - a
->time
>= ACL_REL_GRACE
) {
1537 * If a is the last node we need to reset tail, but we
1538 * need to use cmpxchg to make sure it is still the
1541 if (acl_rel_tail
== &a
->next
) {
1542 acl_rel_head
= NULL
;
1543 if (cmpxchg(&acl_rel_tail
, &a
->next
,
1544 &acl_rel_head
) == &a
->next
) {
1545 ASSERT3P(a
->next
, ==, NULL
);
1552 * a is not last node, make sure next pointer is set
1553 * by the adder and advance the head.
1555 while (READ_ONCE(a
->next
) == NULL
)
1557 acl_rel_head
= a
->next
;
1562 * a is still in grace period. We are responsible to
1563 * reschedule the free task, since adder will only do
1564 * so if list is empty.
1566 new_time
= a
->time
+ ACL_REL_SCHED
;
1573 taskq_dispatch_delay(system_delay_taskq
, zpl_posix_acl_free
,
1574 NULL
, TQ_SLEEP
, new_time
);
1580 kmem_free(a
, sizeof (struct acl_rel_struct
));
1585 zpl_posix_acl_release_impl(struct posix_acl
*acl
)
1587 struct acl_rel_struct
*a
, **prev
;
1589 a
= kmem_alloc(sizeof (struct acl_rel_struct
), KM_SLEEP
);
1592 a
->time
= ddi_get_lbolt();
1593 /* atomically points tail to us and get the previous tail */
1594 prev
= xchg(&acl_rel_tail
, &a
->next
);
1595 ASSERT3P(*prev
, ==, NULL
);
1597 /* if it was empty before, schedule the free task */
1598 if (prev
== &acl_rel_head
)
1599 taskq_dispatch_delay(system_delay_taskq
, zpl_posix_acl_free
,
1600 NULL
, TQ_SLEEP
, ddi_get_lbolt() + ACL_REL_SCHED
);
1604 ZFS_MODULE_PARAM(zfs
, zfs_
, xattr_compat
, INT
, ZMOD_RW
,
1605 "Use legacy ZFS xattr naming for writing new user namespace xattrs");