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 implemenation 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_vfsops.h>
81 #include <sys/zfs_vnops.h>
82 #include <sys/zfs_znode.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
))
116 #elif defined(HAVE_XATTR_LIST_INODE)
117 if (!handler
->list(d
->d_inode
, NULL
, 0, name
, name_len
))
126 * Determine is a given xattr name should be visible and if so copy it
127 * in to the provided buffer (xf->buf).
130 zpl_xattr_filldir(xattr_filldir_t
*xf
, const char *name
, int name_len
)
132 /* Check permissions using the per-namespace list xattr handler. */
133 if (!zpl_xattr_permission(xf
, name
, name_len
))
136 /* When xf->buf is NULL only calculate the required size. */
138 if (xf
->offset
+ name_len
+ 1 > xf
->size
)
141 memcpy(xf
->buf
+ xf
->offset
, name
, name_len
);
142 xf
->buf
[xf
->offset
+ name_len
] = '\0';
145 xf
->offset
+= (name_len
+ 1);
151 * Read as many directory entry names as will fit in to the provided buffer,
152 * or when no buffer is provided calculate the required buffer size.
155 zpl_xattr_readdir(struct inode
*dxip
, xattr_filldir_t
*xf
)
161 zap_cursor_init(&zc
, ITOZSB(dxip
)->z_os
, ITOZ(dxip
)->z_id
);
163 while ((error
= -zap_cursor_retrieve(&zc
, &zap
)) == 0) {
165 if (zap
.za_integer_length
!= 8 || zap
.za_num_integers
!= 1) {
170 error
= zpl_xattr_filldir(xf
, zap
.za_name
, strlen(zap
.za_name
));
174 zap_cursor_advance(&zc
);
177 zap_cursor_fini(&zc
);
179 if (error
== -ENOENT
)
186 zpl_xattr_list_dir(xattr_filldir_t
*xf
, cred_t
*cr
)
188 struct inode
*ip
= xf
->dentry
->d_inode
;
189 struct inode
*dxip
= NULL
;
192 /* Lookup the xattr directory */
193 error
= -zfs_lookup(ip
, NULL
, &dxip
, LOOKUP_XATTR
, cr
, NULL
, NULL
);
195 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 zfs_sb_t
*zsb
= ZTOZSB(zp
);
241 xattr_filldir_t xf
= { buffer_size
, 0, buffer
, dentry
};
243 fstrans_cookie_t cookie
;
247 cookie
= spl_fstrans_mark();
248 rrm_enter_read(&(zsb
)->z_teardown_lock
, FTAG
);
249 rw_enter(&zp
->z_xattr_lock
, RW_READER
);
251 if (zsb
->z_use_sa
&& zp
->z_is_sa
) {
252 error
= zpl_xattr_list_sa(&xf
);
257 error
= zpl_xattr_list_dir(&xf
, cr
);
264 rw_exit(&zp
->z_xattr_lock
);
265 rrm_exit(&(zsb
)->z_teardown_lock
, FTAG
);
266 spl_fstrans_unmark(cookie
);
273 zpl_xattr_get_dir(struct inode
*ip
, const char *name
, void *value
,
274 size_t size
, cred_t
*cr
)
276 struct inode
*dxip
= NULL
;
277 struct inode
*xip
= NULL
;
281 /* Lookup the xattr directory */
282 error
= -zfs_lookup(ip
, NULL
, &dxip
, LOOKUP_XATTR
, cr
, NULL
, NULL
);
286 /* Lookup a specific xattr name in the directory */
287 error
= -zfs_lookup(dxip
, (char *)name
, &xip
, 0, cr
, NULL
, NULL
);
292 error
= i_size_read(xip
);
296 if (size
< i_size_read(xip
)) {
301 error
= zpl_read_common(xip
, value
, size
, &pos
, UIO_SYSSPACE
, 0, cr
);
313 zpl_xattr_get_sa(struct inode
*ip
, const char *name
, void *value
, size_t size
)
315 znode_t
*zp
= ITOZ(ip
);
320 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
322 mutex_enter(&zp
->z_lock
);
323 if (zp
->z_xattr_cached
== NULL
)
324 error
= -zfs_sa_get_xattr(zp
);
325 mutex_exit(&zp
->z_lock
);
330 ASSERT(zp
->z_xattr_cached
);
331 error
= -nvlist_lookup_byte_array(zp
->z_xattr_cached
, name
,
332 &nv_value
, &nv_size
);
342 memcpy(value
, nv_value
, nv_size
);
348 __zpl_xattr_get(struct inode
*ip
, const char *name
, void *value
, size_t size
,
351 znode_t
*zp
= ITOZ(ip
);
352 zfs_sb_t
*zsb
= ZTOZSB(zp
);
355 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
357 if (zsb
->z_use_sa
&& zp
->z_is_sa
) {
358 error
= zpl_xattr_get_sa(ip
, name
, value
, size
);
359 if (error
!= -ENOENT
)
363 error
= zpl_xattr_get_dir(ip
, name
, value
, size
, cr
);
365 if (error
== -ENOENT
)
371 #define XATTR_NOENT 0x0
372 #define XATTR_IN_SA 0x1
373 #define XATTR_IN_DIR 0x2
374 /* check where the xattr resides */
376 __zpl_xattr_where(struct inode
*ip
, const char *name
, int *where
, cred_t
*cr
)
378 znode_t
*zp
= ITOZ(ip
);
379 zfs_sb_t
*zsb
= ZTOZSB(zp
);
383 ASSERT(RW_LOCK_HELD(&zp
->z_xattr_lock
));
385 *where
= XATTR_NOENT
;
386 if (zsb
->z_use_sa
&& zp
->z_is_sa
) {
387 error
= zpl_xattr_get_sa(ip
, name
, NULL
, 0);
389 *where
|= XATTR_IN_SA
;
390 else if (error
!= -ENOENT
)
394 error
= zpl_xattr_get_dir(ip
, name
, NULL
, 0, cr
);
396 *where
|= XATTR_IN_DIR
;
397 else if (error
!= -ENOENT
)
400 if (*where
== (XATTR_IN_SA
|XATTR_IN_DIR
))
401 cmn_err(CE_WARN
, "ZFS: inode %p has xattr \"%s\""
402 " in both SA and dir", ip
, name
);
403 if (*where
== XATTR_NOENT
)
411 zpl_xattr_get(struct inode
*ip
, const char *name
, void *value
, size_t size
)
413 znode_t
*zp
= ITOZ(ip
);
414 zfs_sb_t
*zsb
= ZTOZSB(zp
);
416 fstrans_cookie_t cookie
;
420 cookie
= spl_fstrans_mark();
421 rrm_enter_read(&(zsb
)->z_teardown_lock
, FTAG
);
422 rw_enter(&zp
->z_xattr_lock
, RW_READER
);
423 error
= __zpl_xattr_get(ip
, name
, value
, size
, cr
);
424 rw_exit(&zp
->z_xattr_lock
);
425 rrm_exit(&(zsb
)->z_teardown_lock
, FTAG
);
426 spl_fstrans_unmark(cookie
);
433 zpl_xattr_set_dir(struct inode
*ip
, const char *name
, const void *value
,
434 size_t size
, int flags
, cred_t
*cr
)
436 struct inode
*dxip
= NULL
;
437 struct inode
*xip
= NULL
;
440 int lookup_flags
, error
;
441 const int xattr_mode
= S_IFREG
| 0644;
445 * Lookup the xattr directory. When we're adding an entry pass
446 * CREATE_XATTR_DIR to ensure the xattr directory is created.
447 * When removing an entry this flag is not passed to avoid
448 * unnecessarily creating a new xattr directory.
450 lookup_flags
= LOOKUP_XATTR
;
452 lookup_flags
|= CREATE_XATTR_DIR
;
454 error
= -zfs_lookup(ip
, NULL
, &dxip
, lookup_flags
, cr
, NULL
, NULL
);
458 /* Lookup a specific xattr name in the directory */
459 error
= -zfs_lookup(dxip
, (char *)name
, &xip
, 0, cr
, NULL
, NULL
);
460 if (error
&& (error
!= -ENOENT
))
465 /* Remove a specific name xattr when value is set to NULL. */
468 error
= -zfs_remove(dxip
, (char *)name
, cr
);
473 /* Lookup failed create a new xattr. */
475 vap
= kmem_zalloc(sizeof (vattr_t
), KM_SLEEP
);
476 vap
->va_mode
= xattr_mode
;
477 vap
->va_mask
= ATTR_MODE
;
478 vap
->va_uid
= crgetfsuid(cr
);
479 vap
->va_gid
= crgetfsgid(cr
);
481 error
= -zfs_create(dxip
, (char *)name
, vap
, 0, 0644, &xip
,
489 error
= -zfs_freesp(ITOZ(xip
), 0, 0, xattr_mode
, TRUE
);
493 wrote
= zpl_write_common(xip
, value
, size
, &pos
, UIO_SYSSPACE
, 0, cr
);
499 kmem_free(vap
, sizeof (vattr_t
));
507 if (error
== -ENOENT
)
510 ASSERT3S(error
, <=, 0);
516 zpl_xattr_set_sa(struct inode
*ip
, const char *name
, const void *value
,
517 size_t size
, int flags
, cred_t
*cr
)
519 znode_t
*zp
= ITOZ(ip
);
524 mutex_enter(&zp
->z_lock
);
525 if (zp
->z_xattr_cached
== NULL
)
526 error
= -zfs_sa_get_xattr(zp
);
527 mutex_exit(&zp
->z_lock
);
532 ASSERT(zp
->z_xattr_cached
);
533 nvl
= zp
->z_xattr_cached
;
536 error
= -nvlist_remove(nvl
, name
, DATA_TYPE_BYTE_ARRAY
);
537 if (error
== -ENOENT
)
538 error
= zpl_xattr_set_dir(ip
, name
, NULL
, 0, flags
, cr
);
540 /* Limited to 32k to keep nvpair memory allocations small */
541 if (size
> DXATTR_MAX_ENTRY_SIZE
)
544 /* Prevent the DXATTR SA from consuming the entire SA region */
545 error
= -nvlist_size(nvl
, &sa_size
, NV_ENCODE_XDR
);
549 if (sa_size
> DXATTR_MAX_SA_SIZE
)
552 error
= -nvlist_add_byte_array(nvl
, name
,
553 (uchar_t
*)value
, size
);
557 * Update the SA for additions, modifications, and removals. On
558 * error drop the inconsistent cached version of the nvlist, it
559 * will be reconstructed from the ARC when next accessed.
562 error
= -zfs_sa_set_xattr(zp
);
566 zp
->z_xattr_cached
= NULL
;
569 ASSERT3S(error
, <=, 0);
575 zpl_xattr_set(struct inode
*ip
, const char *name
, const void *value
,
576 size_t size
, int flags
)
578 znode_t
*zp
= ITOZ(ip
);
579 zfs_sb_t
*zsb
= ZTOZSB(zp
);
581 fstrans_cookie_t cookie
;
586 cookie
= spl_fstrans_mark();
587 rrm_enter_read(&(zsb
)->z_teardown_lock
, FTAG
);
588 rw_enter(&ITOZ(ip
)->z_xattr_lock
, RW_WRITER
);
591 * Before setting the xattr check to see if it already exists.
592 * This is done to ensure the following optional flags are honored.
594 * XATTR_CREATE: fail if xattr already exists
595 * XATTR_REPLACE: fail if xattr does not exist
597 * We also want to know if it resides in sa or dir, so we can make
598 * sure we don't end up with duplicate in both places.
600 error
= __zpl_xattr_where(ip
, name
, &where
, cr
);
602 if (error
!= -ENODATA
)
604 if (flags
& XATTR_REPLACE
)
607 /* The xattr to be removed already doesn't exist */
613 if (flags
& XATTR_CREATE
)
617 /* Preferentially store the xattr as a SA for better performance */
618 if (zsb
->z_use_sa
&& zp
->z_is_sa
&&
619 (zsb
->z_xattr_sa
|| (value
== NULL
&& where
& XATTR_IN_SA
))) {
620 error
= zpl_xattr_set_sa(ip
, name
, value
, size
, flags
, cr
);
623 * Successfully put into SA, we need to clear the one
626 if (where
& XATTR_IN_DIR
)
627 zpl_xattr_set_dir(ip
, name
, NULL
, 0, 0, cr
);
632 error
= zpl_xattr_set_dir(ip
, name
, value
, size
, flags
, cr
);
634 * Successfully put into dir, we need to clear the one in SA.
636 if (error
== 0 && (where
& XATTR_IN_SA
))
637 zpl_xattr_set_sa(ip
, name
, NULL
, 0, 0, cr
);
639 rw_exit(&ITOZ(ip
)->z_xattr_lock
);
640 rrm_exit(&(zsb
)->z_teardown_lock
, FTAG
);
641 spl_fstrans_unmark(cookie
);
643 ASSERT3S(error
, <=, 0);
649 * Extended user attributes
651 * "Extended user attributes may be assigned to files and directories for
652 * storing arbitrary additional information such as the mime type,
653 * character set or encoding of a file. The access permissions for user
654 * attributes are defined by the file permission bits: read permission
655 * is required to retrieve the attribute value, and writer permission is
656 * required to change it.
658 * The file permission bits of regular files and directories are
659 * interpreted differently from the file permission bits of special
660 * files and symbolic links. For regular files and directories the file
661 * permission bits define access to the file's contents, while for
662 * device special files they define access to the device described by
663 * the special file. The file permissions of symbolic links are not
664 * used in access checks. These differences would allow users to
665 * consume filesystem resources in a way not controllable by disk quotas
666 * for group or world writable special files and directories.
668 * For this reason, extended user attributes are allowed only for
669 * regular files and directories, and access to extended user attributes
670 * is restricted to the owner and to users with appropriate capabilities
671 * for directories with the sticky bit set (see the chmod(1) manual page
672 * for an explanation of the sticky bit)." - xattr(7)
674 * ZFS allows extended user attributes to be disabled administratively
675 * by setting the 'xattr=off' property on the dataset.
678 __zpl_xattr_user_list(struct inode
*ip
, char *list
, size_t list_size
,
679 const char *name
, size_t name_len
)
681 return (ITOZSB(ip
)->z_flags
& ZSB_XATTR
);
683 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_user_list
);
686 __zpl_xattr_user_get(struct inode
*ip
, const char *name
,
687 void *value
, size_t size
)
691 /* xattr_resolve_name will do this for us if this is defined */
692 #ifndef HAVE_XATTR_HANDLER_NAME
693 if (strcmp(name
, "") == 0)
696 if (!(ITOZSB(ip
)->z_flags
& ZSB_XATTR
))
697 return (-EOPNOTSUPP
);
699 xattr_name
= kmem_asprintf("%s%s", XATTR_USER_PREFIX
, name
);
700 error
= zpl_xattr_get(ip
, xattr_name
, value
, size
);
705 ZPL_XATTR_GET_WRAPPER(zpl_xattr_user_get
);
708 __zpl_xattr_user_set(struct inode
*ip
, const char *name
,
709 const void *value
, size_t size
, int flags
)
713 /* xattr_resolve_name will do this for us if this is defined */
714 #ifndef HAVE_XATTR_HANDLER_NAME
715 if (strcmp(name
, "") == 0)
718 if (!(ITOZSB(ip
)->z_flags
& ZSB_XATTR
))
719 return (-EOPNOTSUPP
);
721 xattr_name
= kmem_asprintf("%s%s", XATTR_USER_PREFIX
, name
);
722 error
= zpl_xattr_set(ip
, xattr_name
, value
, size
, flags
);
727 ZPL_XATTR_SET_WRAPPER(zpl_xattr_user_set
);
729 xattr_handler_t zpl_xattr_user_handler
=
731 .prefix
= XATTR_USER_PREFIX
,
732 .list
= zpl_xattr_user_list
,
733 .get
= zpl_xattr_user_get
,
734 .set
= zpl_xattr_user_set
,
738 * Trusted extended attributes
740 * "Trusted extended attributes are visible and accessible only to
741 * processes that have the CAP_SYS_ADMIN capability. Attributes in this
742 * class are used to implement mechanisms in user space (i.e., outside
743 * the kernel) which keep information in extended attributes to which
744 * ordinary processes should not have access." - xattr(7)
747 __zpl_xattr_trusted_list(struct inode
*ip
, char *list
, size_t list_size
,
748 const char *name
, size_t name_len
)
750 return (capable(CAP_SYS_ADMIN
));
752 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_trusted_list
);
755 __zpl_xattr_trusted_get(struct inode
*ip
, const char *name
,
756 void *value
, size_t size
)
761 if (!capable(CAP_SYS_ADMIN
))
763 /* xattr_resolve_name will do this for us if this is defined */
764 #ifndef HAVE_XATTR_HANDLER_NAME
765 if (strcmp(name
, "") == 0)
768 xattr_name
= kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX
, name
);
769 error
= zpl_xattr_get(ip
, xattr_name
, value
, size
);
774 ZPL_XATTR_GET_WRAPPER(zpl_xattr_trusted_get
);
777 __zpl_xattr_trusted_set(struct inode
*ip
, const char *name
,
778 const void *value
, size_t size
, int flags
)
783 if (!capable(CAP_SYS_ADMIN
))
785 /* xattr_resolve_name will do this for us if this is defined */
786 #ifndef HAVE_XATTR_HANDLER_NAME
787 if (strcmp(name
, "") == 0)
790 xattr_name
= kmem_asprintf("%s%s", XATTR_TRUSTED_PREFIX
, name
);
791 error
= zpl_xattr_set(ip
, xattr_name
, value
, size
, flags
);
796 ZPL_XATTR_SET_WRAPPER(zpl_xattr_trusted_set
);
798 xattr_handler_t zpl_xattr_trusted_handler
=
800 .prefix
= XATTR_TRUSTED_PREFIX
,
801 .list
= zpl_xattr_trusted_list
,
802 .get
= zpl_xattr_trusted_get
,
803 .set
= zpl_xattr_trusted_set
,
807 * Extended security attributes
809 * "The security attribute namespace is used by kernel security modules,
810 * such as Security Enhanced Linux, and also to implement file
811 * capabilities (see capabilities(7)). Read and write access
812 * permissions to security attributes depend on the policy implemented
813 * for each security attribute by the security module. When no security
814 * module is loaded, all processes have read access to extended security
815 * attributes, and write access is limited to processes that have the
816 * CAP_SYS_ADMIN capability." - xattr(7)
819 __zpl_xattr_security_list(struct inode
*ip
, char *list
, size_t list_size
,
820 const char *name
, size_t name_len
)
824 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_security_list
);
827 __zpl_xattr_security_get(struct inode
*ip
, const char *name
,
828 void *value
, size_t size
)
832 /* xattr_resolve_name will do this for us if this is defined */
833 #ifndef HAVE_XATTR_HANDLER_NAME
834 if (strcmp(name
, "") == 0)
837 xattr_name
= kmem_asprintf("%s%s", XATTR_SECURITY_PREFIX
, name
);
838 error
= zpl_xattr_get(ip
, xattr_name
, value
, size
);
843 ZPL_XATTR_GET_WRAPPER(zpl_xattr_security_get
);
846 __zpl_xattr_security_set(struct inode
*ip
, const char *name
,
847 const void *value
, size_t size
, int flags
)
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_set(ip
, xattr_name
, value
, size
, flags
);
862 ZPL_XATTR_SET_WRAPPER(zpl_xattr_security_set
);
864 #ifdef HAVE_CALLBACK_SECURITY_INODE_INIT_SECURITY
866 __zpl_xattr_security_init(struct inode
*ip
, const struct xattr
*xattrs
,
869 const struct xattr
*xattr
;
872 for (xattr
= xattrs
; xattr
->name
!= NULL
; xattr
++) {
873 error
= __zpl_xattr_security_set(ip
,
874 xattr
->name
, xattr
->value
, xattr
->value_len
, 0);
884 zpl_xattr_security_init(struct inode
*ip
, struct inode
*dip
,
885 const struct qstr
*qstr
)
887 return security_inode_init_security(ip
, dip
, qstr
,
888 &__zpl_xattr_security_init
, NULL
);
893 zpl_xattr_security_init(struct inode
*ip
, struct inode
*dip
,
894 const struct qstr
*qstr
)
901 error
= zpl_security_inode_init_security(ip
, dip
, qstr
,
902 &name
, &value
, &len
);
904 if (error
== -EOPNOTSUPP
)
910 error
= __zpl_xattr_security_set(ip
, name
, value
, len
, 0);
917 #endif /* HAVE_CALLBACK_SECURITY_INODE_INIT_SECURITY */
920 * Security xattr namespace handlers.
922 xattr_handler_t zpl_xattr_security_handler
= {
923 .prefix
= XATTR_SECURITY_PREFIX
,
924 .list
= zpl_xattr_security_list
,
925 .get
= zpl_xattr_security_get
,
926 .set
= zpl_xattr_security_set
,
930 * Extended system attributes
932 * "Extended system attributes are used by the kernel to store system
933 * objects such as Access Control Lists. Read and write access permissions
934 * to system attributes depend on the policy implemented for each system
935 * attribute implemented by filesystems in the kernel." - xattr(7)
937 #ifdef CONFIG_FS_POSIX_ACL
939 zpl_set_acl(struct inode
*ip
, struct posix_acl
*acl
, int type
)
941 struct super_block
*sb
= ITOZSB(ip
)->z_sb
;
942 char *name
, *value
= NULL
;
946 if (S_ISLNK(ip
->i_mode
))
947 return (-EOPNOTSUPP
);
950 case ACL_TYPE_ACCESS
:
951 name
= XATTR_NAME_POSIX_ACL_ACCESS
;
953 zpl_equivmode_t mode
= ip
->i_mode
;
954 error
= posix_acl_equiv_mode(acl
, &mode
);
959 * The mode bits will have been set by
960 * ->zfs_setattr()->zfs_acl_chmod_setattr()
961 * using the ZFS ACL conversion. If they
962 * differ from the Posix ACL conversion dirty
963 * the inode to write the Posix mode bits.
965 if (ip
->i_mode
!= mode
) {
967 ip
->i_ctime
= current_fs_time(sb
);
968 zfs_mark_inode_dirty(ip
);
977 case ACL_TYPE_DEFAULT
:
978 name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
979 if (!S_ISDIR(ip
->i_mode
))
980 return (acl
? -EACCES
: 0);
988 size
= posix_acl_xattr_size(acl
->a_count
);
989 value
= kmem_alloc(size
, KM_SLEEP
);
991 error
= zpl_acl_to_xattr(acl
, value
, size
);
993 kmem_free(value
, size
);
998 error
= zpl_xattr_set(ip
, name
, value
, size
, 0);
1000 kmem_free(value
, size
);
1004 zpl_set_cached_acl(ip
, type
, acl
);
1006 zpl_forget_cached_acl(ip
, type
);
1013 zpl_get_acl(struct inode
*ip
, int type
)
1015 struct posix_acl
*acl
;
1021 * As of Linux 3.14, the kernel get_acl will check this for us.
1022 * Also as of Linux 4.7, comparing against ACL_NOT_CACHED is wrong
1023 * as the kernel get_acl will set it to temporary sentinel value.
1025 #ifndef HAVE_KERNEL_GET_ACL_HANDLE_CACHE
1026 acl
= get_cached_acl(ip
, type
);
1027 if (acl
!= ACL_NOT_CACHED
)
1032 case ACL_TYPE_ACCESS
:
1033 name
= XATTR_NAME_POSIX_ACL_ACCESS
;
1035 case ACL_TYPE_DEFAULT
:
1036 name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
1039 return (ERR_PTR(-EINVAL
));
1042 size
= zpl_xattr_get(ip
, name
, NULL
, 0);
1044 value
= kmem_alloc(size
, KM_SLEEP
);
1045 size
= zpl_xattr_get(ip
, name
, value
, size
);
1049 acl
= zpl_acl_from_xattr(value
, size
);
1050 } else if (size
== -ENODATA
|| size
== -ENOSYS
) {
1053 acl
= ERR_PTR(-EIO
);
1057 kmem_free(value
, size
);
1059 /* As of Linux 4.7, the kernel get_acl will set this for us */
1060 #ifndef HAVE_KERNEL_GET_ACL_HANDLE_CACHE
1062 zpl_set_cached_acl(ip
, type
, acl
);
1068 #if !defined(HAVE_GET_ACL)
1070 __zpl_check_acl(struct inode
*ip
, int mask
)
1072 struct posix_acl
*acl
;
1075 acl
= zpl_get_acl(ip
, ACL_TYPE_ACCESS
);
1077 return (PTR_ERR(acl
));
1080 error
= posix_acl_permission(ip
, acl
, mask
);
1081 zpl_posix_acl_release(acl
);
1088 #if defined(HAVE_CHECK_ACL_WITH_FLAGS)
1090 zpl_check_acl(struct inode
*ip
, int mask
, unsigned int flags
)
1092 return (__zpl_check_acl(ip
, mask
));
1094 #elif defined(HAVE_CHECK_ACL)
1096 zpl_check_acl(struct inode
*ip
, int mask
)
1098 return (__zpl_check_acl(ip
, mask
));
1100 #elif defined(HAVE_PERMISSION_WITH_NAMEIDATA)
1102 zpl_permission(struct inode
*ip
, int mask
, struct nameidata
*nd
)
1104 return (generic_permission(ip
, mask
, __zpl_check_acl
));
1106 #elif defined(HAVE_PERMISSION)
1108 zpl_permission(struct inode
*ip
, int mask
)
1110 return (generic_permission(ip
, mask
, __zpl_check_acl
));
1112 #endif /* HAVE_CHECK_ACL | HAVE_PERMISSION */
1113 #endif /* !HAVE_GET_ACL */
1116 zpl_init_acl(struct inode
*ip
, struct inode
*dir
)
1118 struct posix_acl
*acl
= NULL
;
1121 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIXACL
)
1124 if (!S_ISLNK(ip
->i_mode
)) {
1125 if (ITOZSB(ip
)->z_acl_type
== ZFS_ACLTYPE_POSIXACL
) {
1126 acl
= zpl_get_acl(dir
, ACL_TYPE_DEFAULT
);
1128 return (PTR_ERR(acl
));
1132 ip
->i_mode
&= ~current_umask();
1133 ip
->i_ctime
= current_fs_time(ITOZSB(ip
)->z_sb
);
1134 zfs_mark_inode_dirty(ip
);
1139 if ((ITOZSB(ip
)->z_acl_type
== ZFS_ACLTYPE_POSIXACL
) && acl
) {
1142 if (S_ISDIR(ip
->i_mode
)) {
1143 error
= zpl_set_acl(ip
, acl
, ACL_TYPE_DEFAULT
);
1149 error
= __posix_acl_create(&acl
, GFP_KERNEL
, &mode
);
1152 zfs_mark_inode_dirty(ip
);
1154 error
= zpl_set_acl(ip
, acl
, ACL_TYPE_ACCESS
);
1158 zpl_posix_acl_release(acl
);
1164 zpl_chmod_acl(struct inode
*ip
)
1166 struct posix_acl
*acl
;
1169 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIXACL
)
1172 if (S_ISLNK(ip
->i_mode
))
1173 return (-EOPNOTSUPP
);
1175 acl
= zpl_get_acl(ip
, ACL_TYPE_ACCESS
);
1176 if (IS_ERR(acl
) || !acl
)
1177 return (PTR_ERR(acl
));
1179 error
= __posix_acl_chmod(&acl
, GFP_KERNEL
, ip
->i_mode
);
1181 error
= zpl_set_acl(ip
, acl
, ACL_TYPE_ACCESS
);
1183 zpl_posix_acl_release(acl
);
1189 __zpl_xattr_acl_list_access(struct inode
*ip
, char *list
, size_t list_size
,
1190 const char *name
, size_t name_len
)
1192 char *xattr_name
= XATTR_NAME_POSIX_ACL_ACCESS
;
1193 size_t xattr_size
= sizeof (XATTR_NAME_POSIX_ACL_ACCESS
);
1195 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIXACL
)
1198 if (list
&& xattr_size
<= list_size
)
1199 memcpy(list
, xattr_name
, xattr_size
);
1201 return (xattr_size
);
1203 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_acl_list_access
);
1206 __zpl_xattr_acl_list_default(struct inode
*ip
, char *list
, size_t list_size
,
1207 const char *name
, size_t name_len
)
1209 char *xattr_name
= XATTR_NAME_POSIX_ACL_DEFAULT
;
1210 size_t xattr_size
= sizeof (XATTR_NAME_POSIX_ACL_DEFAULT
);
1212 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIXACL
)
1215 if (list
&& xattr_size
<= list_size
)
1216 memcpy(list
, xattr_name
, xattr_size
);
1218 return (xattr_size
);
1220 ZPL_XATTR_LIST_WRAPPER(zpl_xattr_acl_list_default
);
1223 __zpl_xattr_acl_get_access(struct inode
*ip
, const char *name
,
1224 void *buffer
, size_t size
)
1226 struct posix_acl
*acl
;
1227 int type
= ACL_TYPE_ACCESS
;
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_POSIXACL
)
1235 return (-EOPNOTSUPP
);
1237 acl
= zpl_get_acl(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_access
);
1251 __zpl_xattr_acl_get_default(struct inode
*ip
, const char *name
,
1252 void *buffer
, size_t size
)
1254 struct posix_acl
*acl
;
1255 int type
= ACL_TYPE_DEFAULT
;
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_POSIXACL
)
1263 return (-EOPNOTSUPP
);
1265 acl
= zpl_get_acl(ip
, type
);
1267 return (PTR_ERR(acl
));
1271 error
= zpl_acl_to_xattr(acl
, buffer
, size
);
1272 zpl_posix_acl_release(acl
);
1276 ZPL_XATTR_GET_WRAPPER(zpl_xattr_acl_get_default
);
1279 __zpl_xattr_acl_set_access(struct inode
*ip
, const char *name
,
1280 const void *value
, size_t size
, int flags
)
1282 struct posix_acl
*acl
;
1283 int type
= ACL_TYPE_ACCESS
;
1285 /* xattr_resolve_name will do this for us if this is defined */
1286 #ifndef HAVE_XATTR_HANDLER_NAME
1287 if (strcmp(name
, "") != 0)
1290 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIXACL
)
1291 return (-EOPNOTSUPP
);
1293 if (!zpl_inode_owner_or_capable(ip
))
1297 acl
= zpl_acl_from_xattr(value
, size
);
1299 return (PTR_ERR(acl
));
1301 error
= zpl_posix_acl_valid(ip
, acl
);
1303 zpl_posix_acl_release(acl
);
1311 error
= zpl_set_acl(ip
, acl
, type
);
1312 zpl_posix_acl_release(acl
);
1316 ZPL_XATTR_SET_WRAPPER(zpl_xattr_acl_set_access
);
1319 __zpl_xattr_acl_set_default(struct inode
*ip
, const char *name
,
1320 const void *value
, size_t size
, int flags
)
1322 struct posix_acl
*acl
;
1323 int type
= ACL_TYPE_DEFAULT
;
1325 /* xattr_resolve_name will do this for us if this is defined */
1326 #ifndef HAVE_XATTR_HANDLER_NAME
1327 if (strcmp(name
, "") != 0)
1330 if (ITOZSB(ip
)->z_acl_type
!= ZFS_ACLTYPE_POSIXACL
)
1331 return (-EOPNOTSUPP
);
1333 if (!zpl_inode_owner_or_capable(ip
))
1337 acl
= zpl_acl_from_xattr(value
, size
);
1339 return (PTR_ERR(acl
));
1341 error
= zpl_posix_acl_valid(ip
, acl
);
1343 zpl_posix_acl_release(acl
);
1351 error
= zpl_set_acl(ip
, acl
, type
);
1352 zpl_posix_acl_release(acl
);
1356 ZPL_XATTR_SET_WRAPPER(zpl_xattr_acl_set_default
);
1359 * ACL access xattr namespace handlers.
1361 * Use .name instead of .prefix when available. xattr_resolve_name will match
1362 * whole name and reject anything that has .name only as prefix.
1364 xattr_handler_t zpl_xattr_acl_access_handler
=
1366 #ifdef HAVE_XATTR_HANDLER_NAME
1367 .name
= XATTR_NAME_POSIX_ACL_ACCESS
,
1369 .prefix
= XATTR_NAME_POSIX_ACL_ACCESS
,
1371 .list
= zpl_xattr_acl_list_access
,
1372 .get
= zpl_xattr_acl_get_access
,
1373 .set
= zpl_xattr_acl_set_access
,
1374 #if defined(HAVE_XATTR_LIST_SIMPLE) || \
1375 defined(HAVE_XATTR_LIST_DENTRY) || \
1376 defined(HAVE_XATTR_LIST_HANDLER)
1377 .flags
= ACL_TYPE_ACCESS
,
1382 * ACL default 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 xattr_handler_t zpl_xattr_acl_default_handler
=
1389 #ifdef HAVE_XATTR_HANDLER_NAME
1390 .name
= XATTR_NAME_POSIX_ACL_DEFAULT
,
1392 .prefix
= XATTR_NAME_POSIX_ACL_DEFAULT
,
1394 .list
= zpl_xattr_acl_list_default
,
1395 .get
= zpl_xattr_acl_get_default
,
1396 .set
= zpl_xattr_acl_set_default
,
1397 #if defined(HAVE_XATTR_LIST_SIMPLE) || \
1398 defined(HAVE_XATTR_LIST_DENTRY) || \
1399 defined(HAVE_XATTR_LIST_HANDLER)
1400 .flags
= ACL_TYPE_DEFAULT
,
1404 #endif /* CONFIG_FS_POSIX_ACL */
1406 xattr_handler_t
*zpl_xattr_handlers
[] = {
1407 &zpl_xattr_security_handler
,
1408 &zpl_xattr_trusted_handler
,
1409 &zpl_xattr_user_handler
,
1410 #ifdef CONFIG_FS_POSIX_ACL
1411 &zpl_xattr_acl_access_handler
,
1412 &zpl_xattr_acl_default_handler
,
1413 #endif /* CONFIG_FS_POSIX_ACL */
1417 static const struct xattr_handler
*
1418 zpl_xattr_handler(const char *name
)
1420 if (strncmp(name
, XATTR_USER_PREFIX
,
1421 XATTR_USER_PREFIX_LEN
) == 0)
1422 return (&zpl_xattr_user_handler
);
1424 if (strncmp(name
, XATTR_TRUSTED_PREFIX
,
1425 XATTR_TRUSTED_PREFIX_LEN
) == 0)
1426 return (&zpl_xattr_trusted_handler
);
1428 if (strncmp(name
, XATTR_SECURITY_PREFIX
,
1429 XATTR_SECURITY_PREFIX_LEN
) == 0)
1430 return (&zpl_xattr_security_handler
);
1432 #ifdef CONFIG_FS_POSIX_ACL
1433 if (strncmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
,
1434 sizeof (XATTR_NAME_POSIX_ACL_ACCESS
)) == 0)
1435 return (&zpl_xattr_acl_access_handler
);
1437 if (strncmp(name
, XATTR_NAME_POSIX_ACL_DEFAULT
,
1438 sizeof (XATTR_NAME_POSIX_ACL_DEFAULT
)) == 0)
1439 return (&zpl_xattr_acl_default_handler
);
1440 #endif /* CONFIG_FS_POSIX_ACL */
1445 #if !defined(HAVE_POSIX_ACL_RELEASE) || defined(HAVE_POSIX_ACL_RELEASE_GPL_ONLY)
1446 struct acl_rel_struct
{
1447 struct acl_rel_struct
*next
;
1448 struct posix_acl
*acl
;
1452 #define ACL_REL_GRACE (60*HZ)
1453 #define ACL_REL_WINDOW (1*HZ)
1454 #define ACL_REL_SCHED (ACL_REL_GRACE+ACL_REL_WINDOW)
1457 * Lockless multi-producer single-consumer fifo list.
1458 * Nodes are added to tail and removed from head. Tail pointer is our
1459 * synchronization point. It always points to the next pointer of the last
1460 * node, or head if list is empty.
1462 static struct acl_rel_struct
*acl_rel_head
= NULL
;
1463 static struct acl_rel_struct
**acl_rel_tail
= &acl_rel_head
;
1466 zpl_posix_acl_free(void *arg
)
1468 struct acl_rel_struct
*freelist
= NULL
;
1469 struct acl_rel_struct
*a
;
1471 boolean_t refire
= B_FALSE
;
1473 ASSERT3P(acl_rel_head
, !=, NULL
);
1474 while (acl_rel_head
) {
1476 if (ddi_get_lbolt() - a
->time
>= ACL_REL_GRACE
) {
1478 * If a is the last node we need to reset tail, but we
1479 * need to use cmpxchg to make sure it is still the
1482 if (acl_rel_tail
== &a
->next
) {
1483 acl_rel_head
= NULL
;
1484 if (cmpxchg(&acl_rel_tail
, &a
->next
,
1485 &acl_rel_head
) == &a
->next
) {
1486 ASSERT3P(a
->next
, ==, NULL
);
1493 * a is not last node, make sure next pointer is set
1494 * by the adder and advance the head.
1496 while (ACCESS_ONCE(a
->next
) == NULL
)
1498 acl_rel_head
= a
->next
;
1503 * a is still in grace period. We are responsible to
1504 * reschedule the free task, since adder will only do
1505 * so if list is empty.
1507 new_time
= a
->time
+ ACL_REL_SCHED
;
1514 taskq_dispatch_delay(system_taskq
, zpl_posix_acl_free
, NULL
,
1515 TQ_SLEEP
, new_time
);
1521 kmem_free(a
, sizeof (struct acl_rel_struct
));
1526 zpl_posix_acl_release_impl(struct posix_acl
*acl
)
1528 struct acl_rel_struct
*a
, **prev
;
1530 a
= kmem_alloc(sizeof (struct acl_rel_struct
), KM_SLEEP
);
1533 a
->time
= ddi_get_lbolt();
1534 /* atomically points tail to us and get the previous tail */
1535 prev
= xchg(&acl_rel_tail
, &a
->next
);
1536 ASSERT3P(*prev
, ==, NULL
);
1538 /* if it was empty before, schedule the free task */
1539 if (prev
== &acl_rel_head
)
1540 taskq_dispatch_delay(system_taskq
, zpl_posix_acl_free
, NULL
,
1541 TQ_SLEEP
, ddi_get_lbolt() + ACL_REL_SCHED
);