xfs: pass an initialized xfs_da_args structure to xfs_attr_set
[linux-block.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{
129 struct xfs_inode *ip = XFS_I(inode);
2401dc29 130 struct posix_acl *acl = NULL;
ddbca70c 131 struct xfs_acl *xfs_acl = NULL;
a9273ca5 132 unsigned char *ea_name;
ef14f0c1 133 int error;
0a8aa193 134 int len;
ef14f0c1 135
4e34e719
CH
136 trace_xfs_get_acl(ip);
137
ef14f0c1
CH
138 switch (type) {
139 case ACL_TYPE_ACCESS:
140 ea_name = SGI_ACL_FILE;
ef14f0c1
CH
141 break;
142 case ACL_TYPE_DEFAULT:
143 ea_name = SGI_ACL_DEFAULT;
ef14f0c1
CH
144 break;
145 default:
1cbd20d8 146 BUG();
ef14f0c1
CH
147 }
148
ef14f0c1
CH
149 /*
150 * If we have a cached ACLs value just return it, not need to
151 * go out to the disk.
152 */
0a8aa193 153 len = XFS_ACL_MAX_SIZE(ip->i_mount);
d29f781c
AH
154 error = xfs_attr_get(ip, ea_name, strlen(ea_name),
155 (unsigned char **)&xfs_acl, &len,
ddbca70c 156 ATTR_ALLOC | ATTR_ROOT);
ef14f0c1
CH
157 if (error) {
158 /*
159 * If the attribute doesn't exist make sure we have a negative
b8a7a3a6 160 * cache entry, for any other error assume it is transient.
ef14f0c1 161 */
b8a7a3a6
AG
162 if (error != -ENOATTR)
163 acl = ERR_PTR(error);
164 } else {
a5155b87 165 acl = xfs_acl_from_disk(ip->i_mount, xfs_acl, len,
b8a7a3a6 166 XFS_ACL_MAX_ENTRIES(ip->i_mount));
ddbca70c 167 kmem_free(xfs_acl);
ef14f0c1 168 }
ef14f0c1
CH
169 return acl;
170}
171
8ba35875
JK
172int
173__xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
ef14f0c1 174{
a2544622
CH
175 struct xfs_inode *ip = XFS_I(inode);
176 struct xfs_da_args args = {
177 .dp = ip,
178 .flags = ATTR_ROOT,
179 };
180 int error;
ef14f0c1 181
ef14f0c1
CH
182 switch (type) {
183 case ACL_TYPE_ACCESS:
a2544622 184 args.name = SGI_ACL_FILE;
ef14f0c1
CH
185 break;
186 case ACL_TYPE_DEFAULT:
187 if (!S_ISDIR(inode->i_mode))
188 return acl ? -EACCES : 0;
a2544622 189 args.name = SGI_ACL_DEFAULT;
ef14f0c1
CH
190 break;
191 default:
192 return -EINVAL;
193 }
a2544622 194 args.namelen = strlen(args.name);
ef14f0c1
CH
195
196 if (acl) {
a2544622
CH
197 args.valuelen = XFS_ACL_MAX_SIZE(ip->i_mount);
198 args.value = kmem_zalloc_large(args.valuelen, 0);
199 if (!args.value)
fdd3ccee 200 return -ENOMEM;
ef14f0c1 201
a2544622 202 xfs_acl_to_disk(args.value, acl);
0a8aa193
DC
203
204 /* subtract away the unused acl entries */
a2544622 205 args.valuelen -= sizeof(struct xfs_acl_entry) *
0a8aa193 206 (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
ef14f0c1
CH
207 }
208
a2544622
CH
209 error = xfs_attr_set(&args);
210 kmem_free(args.value);
0eb81a5f
CH
211
212 /*
213 * If the attribute didn't exist to start with that's fine.
214 */
215 if (!acl && error == -ENOATTR)
216 error = 0;
ef14f0c1 217 if (!error)
1cbd20d8 218 set_cached_acl(inode, type, acl);
ef14f0c1
CH
219 return error;
220}
221
ef14f0c1 222static int
d3fb6120 223xfs_set_mode(struct inode *inode, umode_t mode)
ef14f0c1
CH
224{
225 int error = 0;
226
227 if (mode != inode->i_mode) {
228 struct iattr iattr;
229
d6d59bad 230 iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
ef14f0c1 231 iattr.ia_mode = mode;
c2050a45 232 iattr.ia_ctime = current_time(inode);
ef14f0c1 233
2451337d 234 error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
ef14f0c1
CH
235 }
236
237 return error;
238}
239
ef14f0c1 240int
2401dc29 241xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
ef14f0c1 242{
67f2ffe3
DC
243 umode_t mode;
244 bool set_mode = false;
431547b3 245 int error = 0;
ef14f0c1 246
2401dc29 247 if (!acl)
ef14f0c1
CH
248 goto set_acl;
249
4ae69fea 250 error = -E2BIG;
0a8aa193 251 if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
2401dc29 252 return error;
ef14f0c1
CH
253
254 if (type == ACL_TYPE_ACCESS) {
07393101
JK
255 error = posix_acl_update_mode(inode, &mode, &acl);
256 if (error)
257 return error;
67f2ffe3 258 set_mode = true;
ef14f0c1
CH
259 }
260
261 set_acl:
67f2ffe3
DC
262 error = __xfs_set_acl(inode, acl, type);
263 if (error)
264 return error;
265
266 /*
267 * We set the mode after successfully updating the ACL xattr because the
268 * xattr update can fail at ENOSPC and we don't want to change the mode
269 * if the ACL update hasn't been applied.
270 */
271 if (set_mode)
272 error = xfs_set_mode(inode, mode);
273
274 return error;
ef14f0c1 275}