blob: 4b641676f258d7ac76c4462a562cc4406b89d882 [file] [log] [blame]
Christoph Hellwigef14f0c2009-06-10 17:07:47 +02001/*
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"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110019#include "xfs_format.h"
Dave Chinner69432832013-08-12 20:49:23 +100020#include "xfs_log_format.h"
Dave Chinner7fd36c42013-08-12 20:49:32 +100021#include "xfs_trans_resv.h"
Dave Chinner0a8aa192013-06-05 12:09:10 +100022#include "xfs_mount.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110023#include "xfs_inode.h"
24#include "xfs_acl.h"
25#include "xfs_attr.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000026#include "xfs_trace.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020028#include <linux/xattr.h>
29#include <linux/posix_acl_xattr.h>
30
31
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020032/*
33 * Locking scheme:
34 * - all ACL updates are protected by inode->i_mutex, which is taken before
35 * calling into this file.
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020036 */
37
38STATIC struct posix_acl *
Dave Chinner0a8aa192013-06-05 12:09:10 +100039xfs_acl_from_disk(
40 struct xfs_acl *aclp,
41 int max_entries)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020042{
43 struct posix_acl_entry *acl_e;
44 struct posix_acl *acl;
45 struct xfs_acl_entry *ace;
Xi Wang093019c2011-12-12 21:55:52 +000046 unsigned int count, i;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020047
48 count = be32_to_cpu(aclp->acl_cnt);
Dave Chinner0a8aa192013-06-05 12:09:10 +100049 if (count > max_entries)
Christoph Hellwigfa8b18e2011-11-20 15:35:32 +000050 return ERR_PTR(-EFSCORRUPTED);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020051
52 acl = posix_acl_alloc(count, GFP_KERNEL);
53 if (!acl)
54 return ERR_PTR(-ENOMEM);
55
56 for (i = 0; i < count; i++) {
57 acl_e = &acl->a_entries[i];
58 ace = &aclp->acl_entry[i];
59
60 /*
61 * The tag is 32 bits on disk and 16 bits in core.
62 *
63 * Because every access to it goes through the core
64 * format first this is not a problem.
65 */
66 acl_e->e_tag = be32_to_cpu(ace->ae_tag);
67 acl_e->e_perm = be16_to_cpu(ace->ae_perm);
68
69 switch (acl_e->e_tag) {
70 case ACL_USER:
Dwight Engen288bbe02013-08-15 14:07:59 -040071 acl_e->e_uid = xfs_uid_to_kuid(be32_to_cpu(ace->ae_id));
72 break;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020073 case ACL_GROUP:
Dwight Engen288bbe02013-08-15 14:07:59 -040074 acl_e->e_gid = xfs_gid_to_kgid(be32_to_cpu(ace->ae_id));
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020075 break;
76 case ACL_USER_OBJ:
77 case ACL_GROUP_OBJ:
78 case ACL_MASK:
79 case ACL_OTHER:
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020080 break;
81 default:
82 goto fail;
83 }
84 }
85 return acl;
86
87fail:
88 posix_acl_release(acl);
89 return ERR_PTR(-EINVAL);
90}
91
92STATIC void
93xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
94{
95 const struct posix_acl_entry *acl_e;
96 struct xfs_acl_entry *ace;
97 int i;
98
99 aclp->acl_cnt = cpu_to_be32(acl->a_count);
100 for (i = 0; i < acl->a_count; i++) {
101 ace = &aclp->acl_entry[i];
102 acl_e = &acl->a_entries[i];
103
104 ace->ae_tag = cpu_to_be32(acl_e->e_tag);
Dwight Engen288bbe02013-08-15 14:07:59 -0400105 switch (acl_e->e_tag) {
106 case ACL_USER:
107 ace->ae_id = cpu_to_be32(xfs_kuid_to_uid(acl_e->e_uid));
108 break;
109 case ACL_GROUP:
110 ace->ae_id = cpu_to_be32(xfs_kgid_to_gid(acl_e->e_gid));
111 break;
112 default:
113 ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
114 break;
115 }
116
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200117 ace->ae_perm = cpu_to_be16(acl_e->e_perm);
118 }
119}
120
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200121struct posix_acl *
122xfs_get_acl(struct inode *inode, int type)
123{
124 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800125 struct posix_acl *acl = NULL;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200126 struct xfs_acl *xfs_acl;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100127 unsigned char *ea_name;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200128 int error;
Dave Chinner0a8aa192013-06-05 12:09:10 +1000129 int len;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200130
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200131 trace_xfs_get_acl(ip);
132
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200133 switch (type) {
134 case ACL_TYPE_ACCESS:
135 ea_name = SGI_ACL_FILE;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200136 break;
137 case ACL_TYPE_DEFAULT:
138 ea_name = SGI_ACL_DEFAULT;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200139 break;
140 default:
Al Viro1cbd20d2009-06-09 13:29:39 -0400141 BUG();
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200142 }
143
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200144 /*
145 * If we have a cached ACLs value just return it, not need to
146 * go out to the disk.
147 */
Dave Chinner0a8aa192013-06-05 12:09:10 +1000148 len = XFS_ACL_MAX_SIZE(ip->i_mount);
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000149 xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
150 if (!xfs_acl)
151 return ERR_PTR(-ENOMEM);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200152
Dave Chinner24513372014-06-25 14:58:08 +1000153 error = xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100154 &len, ATTR_ROOT);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200155 if (error) {
156 /*
157 * If the attribute doesn't exist make sure we have a negative
158 * cache entry, for any other error assume it is transient and
Al Viro1cbd20d2009-06-09 13:29:39 -0400159 * leave the cache entry as ACL_NOT_CACHED.
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200160 */
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800161 if (error == -ENOATTR)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200162 goto out_update_cache;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200163 goto out;
164 }
165
Dave Chinner0a8aa192013-06-05 12:09:10 +1000166 acl = xfs_acl_from_disk(xfs_acl, XFS_ACL_MAX_ENTRIES(ip->i_mount));
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200167 if (IS_ERR(acl))
168 goto out;
169
Dave Chinner2dc164f2013-09-02 20:52:59 +1000170out_update_cache:
Al Viro1cbd20d2009-06-09 13:29:39 -0400171 set_cached_acl(inode, type, acl);
Dave Chinner2dc164f2013-09-02 20:52:59 +1000172out:
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000173 kmem_free(xfs_acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200174 return acl;
175}
176
177STATIC int
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800178__xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200179{
180 struct xfs_inode *ip = XFS_I(inode);
Dave Chinnera9273ca2010-01-20 10:47:48 +1100181 unsigned char *ea_name;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200182 int error;
183
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200184 switch (type) {
185 case ACL_TYPE_ACCESS:
186 ea_name = SGI_ACL_FILE;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200187 break;
188 case ACL_TYPE_DEFAULT:
189 if (!S_ISDIR(inode->i_mode))
190 return acl ? -EACCES : 0;
191 ea_name = SGI_ACL_DEFAULT;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200192 break;
193 default:
194 return -EINVAL;
195 }
196
197 if (acl) {
198 struct xfs_acl *xfs_acl;
Dave Chinner0a8aa192013-06-05 12:09:10 +1000199 int len = XFS_ACL_MAX_SIZE(ip->i_mount);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200200
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000201 xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
202 if (!xfs_acl)
203 return -ENOMEM;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200204
205 xfs_acl_to_disk(xfs_acl, acl);
Dave Chinner0a8aa192013-06-05 12:09:10 +1000206
207 /* subtract away the unused acl entries */
208 len -= sizeof(struct xfs_acl_entry) *
209 (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200210
Dave Chinner24513372014-06-25 14:58:08 +1000211 error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200212 len, ATTR_ROOT);
213
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000214 kmem_free(xfs_acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200215 } else {
216 /*
217 * A NULL ACL argument means we want to remove the ACL.
218 */
Dave Chinner24513372014-06-25 14:58:08 +1000219 error = xfs_attr_remove(ip, ea_name, ATTR_ROOT);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200220
221 /*
222 * If the attribute didn't exist to start with that's fine.
223 */
224 if (error == -ENOATTR)
225 error = 0;
226 }
227
228 if (!error)
Al Viro1cbd20d2009-06-09 13:29:39 -0400229 set_cached_acl(inode, type, acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200230 return error;
231}
232
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200233static int
Al Virod3fb6122011-07-23 18:37:50 -0400234xfs_set_mode(struct inode *inode, umode_t mode)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200235{
236 int error = 0;
237
238 if (mode != inode->i_mode) {
239 struct iattr iattr;
240
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000241 iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200242 iattr.ia_mode = mode;
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000243 iattr.ia_ctime = current_fs_time(inode->i_sb);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200244
Dave Chinner24513372014-06-25 14:58:08 +1000245 error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200246 }
247
248 return error;
249}
250
251static int
Dave Chinnera9273ca2010-01-20 10:47:48 +1100252xfs_acl_exists(struct inode *inode, unsigned char *name)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200253{
Dave Chinner0a8aa192013-06-05 12:09:10 +1000254 int len = XFS_ACL_MAX_SIZE(XFS_M(inode->i_sb));
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200255
256 return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
257 ATTR_ROOT|ATTR_KERNOVAL) == 0);
258}
259
260int
261posix_acl_access_exists(struct inode *inode)
262{
263 return xfs_acl_exists(inode, SGI_ACL_FILE);
264}
265
266int
267posix_acl_default_exists(struct inode *inode)
268{
269 if (!S_ISDIR(inode->i_mode))
270 return 0;
271 return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
272}
273
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200274int
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800275xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200276{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000277 int error = 0;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200278
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800279 if (!acl)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200280 goto set_acl;
281
Jie Liu4ae69fe2014-02-07 15:26:11 +1100282 error = -E2BIG;
Dave Chinner0a8aa192013-06-05 12:09:10 +1000283 if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800284 return error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200285
286 if (type == ACL_TYPE_ACCESS) {
Al Virod6952122011-07-23 18:56:36 -0400287 umode_t mode = inode->i_mode;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200288 error = posix_acl_equiv_mode(acl, &mode);
289
290 if (error <= 0) {
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200291 acl = NULL;
292
293 if (error < 0)
294 return error;
295 }
296
297 error = xfs_set_mode(inode, mode);
298 if (error)
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800299 return error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200300 }
301
302 set_acl:
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800303 return __xfs_set_acl(inode, type, acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200304}