]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/xfs/xfs_acl.c
Merge branches 'for-4.4/upstream-fixes', 'for-4.5/async-suspend', 'for-4.5/container...
[mirror_ubuntu-artful-kernel.git] / fs / xfs / xfs_acl.c
CommitLineData
ef14f0c1
CH
1/*
2 * Copyright (c) 2008, Christoph Hellwig
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18#include "xfs.h"
a4fbe6ab 19#include "xfs_format.h"
69432832 20#include "xfs_log_format.h"
7fd36c44 21#include "xfs_trans_resv.h"
0a8aa193 22#include "xfs_mount.h"
a4fbe6ab
DC
23#include "xfs_inode.h"
24#include "xfs_acl.h"
25#include "xfs_attr.h"
0b1b213f 26#include "xfs_trace.h"
5a0e3ad6 27#include <linux/slab.h>
ef14f0c1
CH
28#include <linux/xattr.h>
29#include <linux/posix_acl_xattr.h>
30
31
ef14f0c1
CH
32/*
33 * Locking scheme:
34 * - all ACL updates are protected by inode->i_mutex, which is taken before
35 * calling into this file.
ef14f0c1
CH
36 */
37
38STATIC struct posix_acl *
0a8aa193 39xfs_acl_from_disk(
86a21c79
AG
40 const struct xfs_acl *aclp,
41 int len,
42 int max_entries)
ef14f0c1
CH
43{
44 struct posix_acl_entry *acl_e;
45 struct posix_acl *acl;
86a21c79 46 const struct xfs_acl_entry *ace;
093019cf 47 unsigned int count, i;
ef14f0c1 48
86a21c79
AG
49 if (len < sizeof(*aclp))
50 return ERR_PTR(-EFSCORRUPTED);
ef14f0c1 51 count = be32_to_cpu(aclp->acl_cnt);
86a21c79 52 if (count > max_entries || XFS_ACL_SIZE(count) != len)
fa8b18ed 53 return ERR_PTR(-EFSCORRUPTED);
ef14f0c1
CH
54
55 acl = posix_acl_alloc(count, GFP_KERNEL);
56 if (!acl)
57 return ERR_PTR(-ENOMEM);
58
59 for (i = 0; i < count; i++) {
60 acl_e = &acl->a_entries[i];
61 ace = &aclp->acl_entry[i];
62
63 /*
64 * The tag is 32 bits on disk and 16 bits in core.
65 *
66 * Because every access to it goes through the core
67 * format first this is not a problem.
68 */
69 acl_e->e_tag = be32_to_cpu(ace->ae_tag);
70 acl_e->e_perm = be16_to_cpu(ace->ae_perm);
71
72 switch (acl_e->e_tag) {
73 case ACL_USER:
288bbe0e
DE
74 acl_e->e_uid = xfs_uid_to_kuid(be32_to_cpu(ace->ae_id));
75 break;
ef14f0c1 76 case ACL_GROUP:
288bbe0e 77 acl_e->e_gid = xfs_gid_to_kgid(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:
110 ace->ae_id = cpu_to_be32(xfs_kuid_to_uid(acl_e->e_uid));
111 break;
112 case ACL_GROUP:
113 ace->ae_id = cpu_to_be32(xfs_kgid_to_gid(acl_e->e_gid));
114 break;
115 default:
116 ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
117 break;
118 }
119
ef14f0c1
CH
120 ace->ae_perm = cpu_to_be16(acl_e->e_perm);
121 }
122}
123
ef14f0c1
CH
124struct posix_acl *
125xfs_get_acl(struct inode *inode, int type)
126{
127 struct xfs_inode *ip = XFS_I(inode);
2401dc29 128 struct posix_acl *acl = NULL;
ef14f0c1 129 struct xfs_acl *xfs_acl;
a9273ca5 130 unsigned char *ea_name;
ef14f0c1 131 int error;
0a8aa193 132 int len;
ef14f0c1 133
4e34e719
CH
134 trace_xfs_get_acl(ip);
135
ef14f0c1
CH
136 switch (type) {
137 case ACL_TYPE_ACCESS:
138 ea_name = SGI_ACL_FILE;
ef14f0c1
CH
139 break;
140 case ACL_TYPE_DEFAULT:
141 ea_name = SGI_ACL_DEFAULT;
ef14f0c1
CH
142 break;
143 default:
1cbd20d8 144 BUG();
ef14f0c1
CH
145 }
146
ef14f0c1
CH
147 /*
148 * If we have a cached ACLs value just return it, not need to
149 * go out to the disk.
150 */
0a8aa193 151 len = XFS_ACL_MAX_SIZE(ip->i_mount);
fdd3ccee
DC
152 xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
153 if (!xfs_acl)
154 return ERR_PTR(-ENOMEM);
ef14f0c1 155
2451337d 156 error = xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
a9273ca5 157 &len, ATTR_ROOT);
ef14f0c1
CH
158 if (error) {
159 /*
160 * If the attribute doesn't exist make sure we have a negative
161 * cache entry, for any other error assume it is transient and
1cbd20d8 162 * leave the cache entry as ACL_NOT_CACHED.
ef14f0c1 163 */
2401dc29 164 if (error == -ENOATTR)
ef14f0c1 165 goto out_update_cache;
edfb8ebc 166 acl = ERR_PTR(error);
ef14f0c1
CH
167 goto out;
168 }
169
86a21c79 170 acl = xfs_acl_from_disk(xfs_acl, len, XFS_ACL_MAX_ENTRIES(ip->i_mount));
ef14f0c1
CH
171 if (IS_ERR(acl))
172 goto out;
173
2dc164f2 174out_update_cache:
1cbd20d8 175 set_cached_acl(inode, type, acl);
2dc164f2 176out:
fdd3ccee 177 kmem_free(xfs_acl);
ef14f0c1
CH
178 return acl;
179}
180
181STATIC int
2401dc29 182__xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
ef14f0c1
CH
183{
184 struct xfs_inode *ip = XFS_I(inode);
a9273ca5 185 unsigned char *ea_name;
ef14f0c1
CH
186 int error;
187
ef14f0c1
CH
188 switch (type) {
189 case ACL_TYPE_ACCESS:
190 ea_name = SGI_ACL_FILE;
ef14f0c1
CH
191 break;
192 case ACL_TYPE_DEFAULT:
193 if (!S_ISDIR(inode->i_mode))
194 return acl ? -EACCES : 0;
195 ea_name = SGI_ACL_DEFAULT;
ef14f0c1
CH
196 break;
197 default:
198 return -EINVAL;
199 }
200
201 if (acl) {
202 struct xfs_acl *xfs_acl;
0a8aa193 203 int len = XFS_ACL_MAX_SIZE(ip->i_mount);
ef14f0c1 204
fdd3ccee
DC
205 xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
206 if (!xfs_acl)
207 return -ENOMEM;
ef14f0c1
CH
208
209 xfs_acl_to_disk(xfs_acl, acl);
0a8aa193
DC
210
211 /* subtract away the unused acl entries */
212 len -= sizeof(struct xfs_acl_entry) *
213 (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
ef14f0c1 214
2451337d 215 error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
ef14f0c1
CH
216 len, ATTR_ROOT);
217
fdd3ccee 218 kmem_free(xfs_acl);
ef14f0c1
CH
219 } else {
220 /*
221 * A NULL ACL argument means we want to remove the ACL.
222 */
2451337d 223 error = xfs_attr_remove(ip, ea_name, ATTR_ROOT);
ef14f0c1
CH
224
225 /*
226 * If the attribute didn't exist to start with that's fine.
227 */
228 if (error == -ENOATTR)
229 error = 0;
230 }
231
232 if (!error)
1cbd20d8 233 set_cached_acl(inode, type, acl);
ef14f0c1
CH
234 return error;
235}
236
ef14f0c1 237static int
d3fb6120 238xfs_set_mode(struct inode *inode, umode_t mode)
ef14f0c1
CH
239{
240 int error = 0;
241
242 if (mode != inode->i_mode) {
243 struct iattr iattr;
244
d6d59bad 245 iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
ef14f0c1 246 iattr.ia_mode = mode;
d6d59bad 247 iattr.ia_ctime = current_fs_time(inode->i_sb);
ef14f0c1 248
2451337d 249 error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
ef14f0c1
CH
250 }
251
252 return error;
253}
254
255static int
a9273ca5 256xfs_acl_exists(struct inode *inode, unsigned char *name)
ef14f0c1 257{
0a8aa193 258 int len = XFS_ACL_MAX_SIZE(XFS_M(inode->i_sb));
ef14f0c1
CH
259
260 return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
261 ATTR_ROOT|ATTR_KERNOVAL) == 0);
262}
263
264int
265posix_acl_access_exists(struct inode *inode)
266{
267 return xfs_acl_exists(inode, SGI_ACL_FILE);
268}
269
270int
271posix_acl_default_exists(struct inode *inode)
272{
273 if (!S_ISDIR(inode->i_mode))
274 return 0;
275 return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
276}
277
ef14f0c1 278int
2401dc29 279xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
ef14f0c1 280{
431547b3 281 int error = 0;
ef14f0c1 282
2401dc29 283 if (!acl)
ef14f0c1
CH
284 goto set_acl;
285
4ae69fea 286 error = -E2BIG;
0a8aa193 287 if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
2401dc29 288 return error;
ef14f0c1
CH
289
290 if (type == ACL_TYPE_ACCESS) {
d6952123 291 umode_t mode = inode->i_mode;
ef14f0c1
CH
292 error = posix_acl_equiv_mode(acl, &mode);
293
294 if (error <= 0) {
ef14f0c1
CH
295 acl = NULL;
296
297 if (error < 0)
298 return error;
299 }
300
301 error = xfs_set_mode(inode, mode);
302 if (error)
2401dc29 303 return error;
ef14f0c1
CH
304 }
305
306 set_acl:
2401dc29 307 return __xfs_set_acl(inode, type, acl);
ef14f0c1 308}