]> git.ipfire.org Git - people/ms/linux.git/blame - fs/xfs/xfs_acl.c
xfs: clean up bufsize alignment in xfs_ioc_attr_list
[people/ms/linux.git] / fs / xfs / xfs_acl.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
ef14f0c1
CH
2/*
3 * Copyright (c) 2008, Christoph Hellwig
4 * All Rights Reserved.
ef14f0c1
CH
5 */
6#include "xfs.h"
5467b34b 7#include "xfs_shared.h"
a4fbe6ab 8#include "xfs_format.h"
69432832 9#include "xfs_log_format.h"
7fd36c44 10#include "xfs_trans_resv.h"
0a8aa193 11#include "xfs_mount.h"
a4fbe6ab 12#include "xfs_inode.h"
a4fbe6ab 13#include "xfs_attr.h"
0b1b213f 14#include "xfs_trace.h"
a5155b87 15#include "xfs_error.h"
5f213ddb 16#include "xfs_acl.h"
a2544622
CH
17#include "xfs_da_format.h"
18#include "xfs_da_btree.h"
ef14f0c1 19
5f213ddb 20#include <linux/posix_acl_xattr.h>
ef14f0c1 21
ef14f0c1
CH
22/*
23 * Locking scheme:
24 * - all ACL updates are protected by inode->i_mutex, which is taken before
25 * calling into this file.
ef14f0c1
CH
26 */
27
28STATIC struct posix_acl *
0a8aa193 29xfs_acl_from_disk(
a5155b87 30 struct xfs_mount *mp,
86a21c79
AG
31 const struct xfs_acl *aclp,
32 int len,
33 int max_entries)
ef14f0c1
CH
34{
35 struct posix_acl_entry *acl_e;
36 struct posix_acl *acl;
86a21c79 37 const struct xfs_acl_entry *ace;
093019cf 38 unsigned int count, i;
ef14f0c1 39
a5155b87
DW
40 if (len < sizeof(*aclp)) {
41 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp,
42 len);
86a21c79 43 return ERR_PTR(-EFSCORRUPTED);
a5155b87
DW
44 }
45
ef14f0c1 46 count = be32_to_cpu(aclp->acl_cnt);
a5155b87
DW
47 if (count > max_entries || XFS_ACL_SIZE(count) != len) {
48 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp,
49 len);
fa8b18ed 50 return ERR_PTR(-EFSCORRUPTED);
a5155b87 51 }
ef14f0c1
CH
52
53 acl = posix_acl_alloc(count, GFP_KERNEL);
54 if (!acl)
55 return ERR_PTR(-ENOMEM);
56
57 for (i = 0; i < count; i++) {
58 acl_e = &acl->a_entries[i];
59 ace = &aclp->acl_entry[i];
60
61 /*
62 * The tag is 32 bits on disk and 16 bits in core.
63 *
64 * Because every access to it goes through the core
65 * format first this is not a problem.
66 */
67 acl_e->e_tag = be32_to_cpu(ace->ae_tag);
68 acl_e->e_perm = be16_to_cpu(ace->ae_perm);
69
70 switch (acl_e->e_tag) {
71 case ACL_USER:
ba8adad5
CH
72 acl_e->e_uid = make_kuid(&init_user_ns,
73 be32_to_cpu(ace->ae_id));
288bbe0e 74 break;
ef14f0c1 75 case ACL_GROUP:
ba8adad5
CH
76 acl_e->e_gid = make_kgid(&init_user_ns,
77 be32_to_cpu(ace->ae_id));
ef14f0c1
CH
78 break;
79 case ACL_USER_OBJ:
80 case ACL_GROUP_OBJ:
81 case ACL_MASK:
82 case ACL_OTHER:
ef14f0c1
CH
83 break;
84 default:
85 goto fail;
86 }
87 }
88 return acl;
89
90fail:
91 posix_acl_release(acl);
92 return ERR_PTR(-EINVAL);
93}
94
95STATIC void
96xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
97{
98 const struct posix_acl_entry *acl_e;
99 struct xfs_acl_entry *ace;
100 int i;
101
102 aclp->acl_cnt = cpu_to_be32(acl->a_count);
103 for (i = 0; i < acl->a_count; i++) {
104 ace = &aclp->acl_entry[i];
105 acl_e = &acl->a_entries[i];
106
107 ace->ae_tag = cpu_to_be32(acl_e->e_tag);
288bbe0e
DE
108 switch (acl_e->e_tag) {
109 case ACL_USER:
ba8adad5
CH
110 ace->ae_id = cpu_to_be32(
111 from_kuid(&init_user_ns, acl_e->e_uid));
288bbe0e
DE
112 break;
113 case ACL_GROUP:
ba8adad5
CH
114 ace->ae_id = cpu_to_be32(
115 from_kgid(&init_user_ns, acl_e->e_gid));
288bbe0e
DE
116 break;
117 default:
118 ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
119 break;
120 }
121
ef14f0c1
CH
122 ace->ae_perm = cpu_to_be16(acl_e->e_perm);
123 }
124}
125
ef14f0c1
CH
126struct posix_acl *
127xfs_get_acl(struct inode *inode, int type)
128{
e5171d7e
CH
129 struct xfs_inode *ip = XFS_I(inode);
130 struct xfs_mount *mp = ip->i_mount;
131 struct posix_acl *acl = NULL;
132 struct xfs_da_args args = {
133 .dp = ip,
d5f0f49a 134 .attr_filter = XFS_ATTR_ROOT,
e5171d7e
CH
135 .valuelen = XFS_ACL_MAX_SIZE(mp),
136 };
137 int error;
ef14f0c1 138
4e34e719
CH
139 trace_xfs_get_acl(ip);
140
ef14f0c1
CH
141 switch (type) {
142 case ACL_TYPE_ACCESS:
e5171d7e 143 args.name = SGI_ACL_FILE;
ef14f0c1
CH
144 break;
145 case ACL_TYPE_DEFAULT:
e5171d7e 146 args.name = SGI_ACL_DEFAULT;
ef14f0c1
CH
147 break;
148 default:
1cbd20d8 149 BUG();
ef14f0c1 150 }
e5171d7e 151 args.namelen = strlen(args.name);
ef14f0c1 152
d49db18b
CH
153 /*
154 * If the attribute doesn't exist make sure we have a negative cache
155 * entry, for any other error assume it is transient.
156 */
e5171d7e 157 error = xfs_attr_get(&args);
d49db18b 158 if (!error) {
e5171d7e
CH
159 acl = xfs_acl_from_disk(mp, args.value, args.valuelen,
160 XFS_ACL_MAX_ENTRIES(mp));
d49db18b
CH
161 } else if (error != -ENOATTR) {
162 acl = ERR_PTR(error);
ef14f0c1 163 }
d49db18b
CH
164
165 kmem_free(args.value);
ef14f0c1
CH
166 return acl;
167}
168
8ba35875
JK
169int
170__xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
ef14f0c1 171{
a2544622
CH
172 struct xfs_inode *ip = XFS_I(inode);
173 struct xfs_da_args args = {
174 .dp = ip,
d5f0f49a 175 .attr_filter = XFS_ATTR_ROOT,
a2544622
CH
176 };
177 int error;
ef14f0c1 178
ef14f0c1
CH
179 switch (type) {
180 case ACL_TYPE_ACCESS:
a2544622 181 args.name = SGI_ACL_FILE;
ef14f0c1
CH
182 break;
183 case ACL_TYPE_DEFAULT:
184 if (!S_ISDIR(inode->i_mode))
185 return acl ? -EACCES : 0;
a2544622 186 args.name = SGI_ACL_DEFAULT;
ef14f0c1
CH
187 break;
188 default:
189 return -EINVAL;
190 }
a2544622 191 args.namelen = strlen(args.name);
ef14f0c1
CH
192
193 if (acl) {
a2544622
CH
194 args.valuelen = XFS_ACL_MAX_SIZE(ip->i_mount);
195 args.value = kmem_zalloc_large(args.valuelen, 0);
196 if (!args.value)
fdd3ccee 197 return -ENOMEM;
ef14f0c1 198
a2544622 199 xfs_acl_to_disk(args.value, acl);
0a8aa193
DC
200
201 /* subtract away the unused acl entries */
a2544622 202 args.valuelen -= sizeof(struct xfs_acl_entry) *
0a8aa193 203 (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
ef14f0c1
CH
204 }
205
a2544622
CH
206 error = xfs_attr_set(&args);
207 kmem_free(args.value);
0eb81a5f
CH
208
209 /*
210 * If the attribute didn't exist to start with that's fine.
211 */
212 if (!acl && error == -ENOATTR)
213 error = 0;
ef14f0c1 214 if (!error)
1cbd20d8 215 set_cached_acl(inode, type, acl);
ef14f0c1
CH
216 return error;
217}
218
ef14f0c1 219static int
d3fb6120 220xfs_set_mode(struct inode *inode, umode_t mode)
ef14f0c1
CH
221{
222 int error = 0;
223
224 if (mode != inode->i_mode) {
225 struct iattr iattr;
226
d6d59bad 227 iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
ef14f0c1 228 iattr.ia_mode = mode;
c2050a45 229 iattr.ia_ctime = current_time(inode);
ef14f0c1 230
2451337d 231 error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
ef14f0c1
CH
232 }
233
234 return error;
235}
236
ef14f0c1 237int
2401dc29 238xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
ef14f0c1 239{
67f2ffe3
DC
240 umode_t mode;
241 bool set_mode = false;
431547b3 242 int error = 0;
ef14f0c1 243
2401dc29 244 if (!acl)
ef14f0c1
CH
245 goto set_acl;
246
4ae69fea 247 error = -E2BIG;
0a8aa193 248 if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
2401dc29 249 return error;
ef14f0c1
CH
250
251 if (type == ACL_TYPE_ACCESS) {
07393101
JK
252 error = posix_acl_update_mode(inode, &mode, &acl);
253 if (error)
254 return error;
67f2ffe3 255 set_mode = true;
ef14f0c1
CH
256 }
257
258 set_acl:
67f2ffe3
DC
259 error = __xfs_set_acl(inode, acl, type);
260 if (error)
261 return error;
262
263 /*
264 * We set the mode after successfully updating the ACL xattr because the
265 * xattr update can fail at ENOSPC and we don't want to change the mode
266 * if the ACL update hasn't been applied.
267 */
268 if (set_mode)
269 error = xfs_set_mode(inode, mode);
270
271 return error;
ef14f0c1 272}
5a3930e2
CH
273
274/*
275 * Invalidate any cached ACLs if the user has bypassed the ACL interface.
276 * We don't validate the content whatsoever so it is caller responsibility to
277 * provide data in valid format and ensure i_mode is consistent.
278 */
279void
280xfs_forget_acl(
281 struct inode *inode,
282 const char *name)
283{
284 if (!strcmp(name, SGI_ACL_FILE))
285 forget_cached_acl(inode, ACL_TYPE_ACCESS);
286 else if (!strcmp(name, SGI_ACL_DEFAULT))
287 forget_cached_acl(inode, ACL_TYPE_DEFAULT);
288}