1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2008, Christoph Hellwig
7 #include "xfs_format.h"
8 #include "xfs_log_format.h"
9 #include "xfs_trans_resv.h"
10 #include "xfs_mount.h"
11 #include "xfs_inode.h"
14 #include "xfs_trace.h"
15 #include <linux/slab.h>
16 #include <linux/xattr.h>
17 #include <linux/posix_acl_xattr.h>
22 * - all ACL updates are protected by inode->i_mutex, which is taken before
23 * calling into this file.
26 STATIC struct posix_acl *
28 const struct xfs_acl *aclp,
32 struct posix_acl_entry *acl_e;
33 struct posix_acl *acl;
34 const struct xfs_acl_entry *ace;
35 unsigned int count, i;
37 if (len < sizeof(*aclp))
38 return ERR_PTR(-EFSCORRUPTED);
39 count = be32_to_cpu(aclp->acl_cnt);
40 if (count > max_entries || XFS_ACL_SIZE(count) != len)
41 return ERR_PTR(-EFSCORRUPTED);
43 acl = posix_acl_alloc(count, GFP_KERNEL);
45 return ERR_PTR(-ENOMEM);
47 for (i = 0; i < count; i++) {
48 acl_e = &acl->a_entries[i];
49 ace = &aclp->acl_entry[i];
52 * The tag is 32 bits on disk and 16 bits in core.
54 * Because every access to it goes through the core
55 * format first this is not a problem.
57 acl_e->e_tag = be32_to_cpu(ace->ae_tag);
58 acl_e->e_perm = be16_to_cpu(ace->ae_perm);
60 switch (acl_e->e_tag) {
62 acl_e->e_uid = xfs_uid_to_kuid(be32_to_cpu(ace->ae_id));
65 acl_e->e_gid = xfs_gid_to_kgid(be32_to_cpu(ace->ae_id));
79 posix_acl_release(acl);
80 return ERR_PTR(-EINVAL);
84 xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
86 const struct posix_acl_entry *acl_e;
87 struct xfs_acl_entry *ace;
90 aclp->acl_cnt = cpu_to_be32(acl->a_count);
91 for (i = 0; i < acl->a_count; i++) {
92 ace = &aclp->acl_entry[i];
93 acl_e = &acl->a_entries[i];
95 ace->ae_tag = cpu_to_be32(acl_e->e_tag);
96 switch (acl_e->e_tag) {
98 ace->ae_id = cpu_to_be32(xfs_kuid_to_uid(acl_e->e_uid));
101 ace->ae_id = cpu_to_be32(xfs_kgid_to_gid(acl_e->e_gid));
104 ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
108 ace->ae_perm = cpu_to_be16(acl_e->e_perm);
113 xfs_get_acl(struct inode *inode, int type)
115 struct xfs_inode *ip = XFS_I(inode);
116 struct posix_acl *acl = NULL;
117 struct xfs_acl *xfs_acl;
118 unsigned char *ea_name;
122 trace_xfs_get_acl(ip);
125 case ACL_TYPE_ACCESS:
126 ea_name = SGI_ACL_FILE;
128 case ACL_TYPE_DEFAULT:
129 ea_name = SGI_ACL_DEFAULT;
136 * If we have a cached ACLs value just return it, not need to
137 * go out to the disk.
139 len = XFS_ACL_MAX_SIZE(ip->i_mount);
140 xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
142 return ERR_PTR(-ENOMEM);
144 error = xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
148 * If the attribute doesn't exist make sure we have a negative
149 * cache entry, for any other error assume it is transient.
151 if (error != -ENOATTR)
152 acl = ERR_PTR(error);
154 acl = xfs_acl_from_disk(xfs_acl, len,
155 XFS_ACL_MAX_ENTRIES(ip->i_mount));
162 __xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
164 struct xfs_inode *ip = XFS_I(inode);
165 unsigned char *ea_name;
169 case ACL_TYPE_ACCESS:
170 ea_name = SGI_ACL_FILE;
172 case ACL_TYPE_DEFAULT:
173 if (!S_ISDIR(inode->i_mode))
174 return acl ? -EACCES : 0;
175 ea_name = SGI_ACL_DEFAULT;
182 struct xfs_acl *xfs_acl;
183 int len = XFS_ACL_MAX_SIZE(ip->i_mount);
185 xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
189 xfs_acl_to_disk(xfs_acl, acl);
191 /* subtract away the unused acl entries */
192 len -= sizeof(struct xfs_acl_entry) *
193 (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
195 error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
201 * A NULL ACL argument means we want to remove the ACL.
203 error = xfs_attr_remove(ip, ea_name, ATTR_ROOT);
206 * If the attribute didn't exist to start with that's fine.
208 if (error == -ENOATTR)
213 set_cached_acl(inode, type, acl);
218 xfs_set_mode(struct inode *inode, umode_t mode)
222 if (mode != inode->i_mode) {
225 iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
226 iattr.ia_mode = mode;
227 iattr.ia_ctime = current_time(inode);
229 error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
236 xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
239 bool set_mode = false;
246 if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
249 if (type == ACL_TYPE_ACCESS) {
250 error = posix_acl_update_mode(inode, &mode, &acl);
257 error = __xfs_set_acl(inode, acl, type);
262 * We set the mode after successfully updating the ACL xattr because the
263 * xattr update can fail at ENOSPC and we don't want to change the mode
264 * if the ACL update hasn't been applied.
267 error = xfs_set_mode(inode, mode);