blob: a65fa5dde6e9c514b0e5fbfeecd6d14a83e202bc [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 Chinner9356fe22013-08-12 20:49:55 +100022#include "xfs_ag.h"
Dave Chinner0a8aa192013-06-05 12:09:10 +100023#include "xfs_sb.h"
24#include "xfs_mount.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110025#include "xfs_inode.h"
26#include "xfs_acl.h"
27#include "xfs_attr.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000028#include "xfs_trace.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020030#include <linux/xattr.h>
31#include <linux/posix_acl_xattr.h>
32
33
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020034/*
35 * Locking scheme:
36 * - all ACL updates are protected by inode->i_mutex, which is taken before
37 * calling into this file.
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020038 */
39
40STATIC struct posix_acl *
Dave Chinner0a8aa192013-06-05 12:09:10 +100041xfs_acl_from_disk(
42 struct xfs_acl *aclp,
43 int max_entries)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020044{
45 struct posix_acl_entry *acl_e;
46 struct posix_acl *acl;
47 struct xfs_acl_entry *ace;
Xi Wang093019c2011-12-12 21:55:52 +000048 unsigned int count, i;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020049
50 count = be32_to_cpu(aclp->acl_cnt);
Dave Chinner0a8aa192013-06-05 12:09:10 +100051 if (count > max_entries)
Christoph Hellwigfa8b18e2011-11-20 15:35:32 +000052 return ERR_PTR(-EFSCORRUPTED);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020053
54 acl = posix_acl_alloc(count, GFP_KERNEL);
55 if (!acl)
56 return ERR_PTR(-ENOMEM);
57
58 for (i = 0; i < count; i++) {
59 acl_e = &acl->a_entries[i];
60 ace = &aclp->acl_entry[i];
61
62 /*
63 * The tag is 32 bits on disk and 16 bits in core.
64 *
65 * Because every access to it goes through the core
66 * format first this is not a problem.
67 */
68 acl_e->e_tag = be32_to_cpu(ace->ae_tag);
69 acl_e->e_perm = be16_to_cpu(ace->ae_perm);
70
71 switch (acl_e->e_tag) {
72 case ACL_USER:
Dwight Engen288bbe02013-08-15 14:07:59 -040073 acl_e->e_uid = xfs_uid_to_kuid(be32_to_cpu(ace->ae_id));
74 break;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020075 case ACL_GROUP:
Dwight Engen288bbe02013-08-15 14:07:59 -040076 acl_e->e_gid = xfs_gid_to_kgid(be32_to_cpu(ace->ae_id));
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020077 break;
78 case ACL_USER_OBJ:
79 case ACL_GROUP_OBJ:
80 case ACL_MASK:
81 case ACL_OTHER:
Christoph Hellwigef14f0c2009-06-10 17:07:47 +020082 break;
83 default:
84 goto fail;
85 }
86 }
87 return acl;
88
89fail:
90 posix_acl_release(acl);
91 return ERR_PTR(-EINVAL);
92}
93
94STATIC void
95xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
96{
97 const struct posix_acl_entry *acl_e;
98 struct xfs_acl_entry *ace;
99 int i;
100
101 aclp->acl_cnt = cpu_to_be32(acl->a_count);
102 for (i = 0; i < acl->a_count; i++) {
103 ace = &aclp->acl_entry[i];
104 acl_e = &acl->a_entries[i];
105
106 ace->ae_tag = cpu_to_be32(acl_e->e_tag);
Dwight Engen288bbe02013-08-15 14:07:59 -0400107 switch (acl_e->e_tag) {
108 case ACL_USER:
109 ace->ae_id = cpu_to_be32(xfs_kuid_to_uid(acl_e->e_uid));
110 break;
111 case ACL_GROUP:
112 ace->ae_id = cpu_to_be32(xfs_kgid_to_gid(acl_e->e_gid));
113 break;
114 default:
115 ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
116 break;
117 }
118
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200119 ace->ae_perm = cpu_to_be16(acl_e->e_perm);
120 }
121}
122
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200123struct posix_acl *
124xfs_get_acl(struct inode *inode, int type)
125{
126 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800127 struct posix_acl *acl = NULL;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200128 struct xfs_acl *xfs_acl;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100129 unsigned char *ea_name;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200130 int error;
Dave Chinner0a8aa192013-06-05 12:09:10 +1000131 int len;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200132
Christoph Hellwig4e34e712011-07-23 17:37:31 +0200133 trace_xfs_get_acl(ip);
134
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200135 switch (type) {
136 case ACL_TYPE_ACCESS:
137 ea_name = SGI_ACL_FILE;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200138 break;
139 case ACL_TYPE_DEFAULT:
140 ea_name = SGI_ACL_DEFAULT;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200141 break;
142 default:
Al Viro1cbd20d2009-06-09 13:29:39 -0400143 BUG();
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200144 }
145
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200146 /*
147 * If we have a cached ACLs value just return it, not need to
148 * go out to the disk.
149 */
Dave Chinner0a8aa192013-06-05 12:09:10 +1000150 len = XFS_ACL_MAX_SIZE(ip->i_mount);
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000151 xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
152 if (!xfs_acl)
153 return ERR_PTR(-ENOMEM);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200154
Dave Chinner24513372014-06-25 14:58:08 +1000155 error = xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
Dave Chinnera9273ca2010-01-20 10:47:48 +1100156 &len, ATTR_ROOT);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200157 if (error) {
158 /*
159 * If the attribute doesn't exist make sure we have a negative
160 * cache entry, for any other error assume it is transient and
Al Viro1cbd20d2009-06-09 13:29:39 -0400161 * leave the cache entry as ACL_NOT_CACHED.
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200162 */
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800163 if (error == -ENOATTR)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200164 goto out_update_cache;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200165 goto out;
166 }
167
Dave Chinner0a8aa192013-06-05 12:09:10 +1000168 acl = xfs_acl_from_disk(xfs_acl, XFS_ACL_MAX_ENTRIES(ip->i_mount));
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200169 if (IS_ERR(acl))
170 goto out;
171
Dave Chinner2dc164f2013-09-02 20:52:59 +1000172out_update_cache:
Al Viro1cbd20d2009-06-09 13:29:39 -0400173 set_cached_acl(inode, type, acl);
Dave Chinner2dc164f2013-09-02 20:52:59 +1000174out:
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000175 kmem_free(xfs_acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200176 return acl;
177}
178
179STATIC int
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800180__xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200181{
182 struct xfs_inode *ip = XFS_I(inode);
Dave Chinnera9273ca2010-01-20 10:47:48 +1100183 unsigned char *ea_name;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200184 int error;
185
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200186 switch (type) {
187 case ACL_TYPE_ACCESS:
188 ea_name = SGI_ACL_FILE;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200189 break;
190 case ACL_TYPE_DEFAULT:
191 if (!S_ISDIR(inode->i_mode))
192 return acl ? -EACCES : 0;
193 ea_name = SGI_ACL_DEFAULT;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200194 break;
195 default:
196 return -EINVAL;
197 }
198
199 if (acl) {
200 struct xfs_acl *xfs_acl;
Dave Chinner0a8aa192013-06-05 12:09:10 +1000201 int len = XFS_ACL_MAX_SIZE(ip->i_mount);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200202
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000203 xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
204 if (!xfs_acl)
205 return -ENOMEM;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200206
207 xfs_acl_to_disk(xfs_acl, acl);
Dave Chinner0a8aa192013-06-05 12:09:10 +1000208
209 /* subtract away the unused acl entries */
210 len -= sizeof(struct xfs_acl_entry) *
211 (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200212
Dave Chinner24513372014-06-25 14:58:08 +1000213 error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200214 len, ATTR_ROOT);
215
Dave Chinnerfdd3cce2013-09-02 20:53:00 +1000216 kmem_free(xfs_acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200217 } else {
218 /*
219 * A NULL ACL argument means we want to remove the ACL.
220 */
Dave Chinner24513372014-06-25 14:58:08 +1000221 error = xfs_attr_remove(ip, ea_name, ATTR_ROOT);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200222
223 /*
224 * If the attribute didn't exist to start with that's fine.
225 */
226 if (error == -ENOATTR)
227 error = 0;
228 }
229
230 if (!error)
Al Viro1cbd20d2009-06-09 13:29:39 -0400231 set_cached_acl(inode, type, acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200232 return error;
233}
234
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200235static int
Al Virod3fb6122011-07-23 18:37:50 -0400236xfs_set_mode(struct inode *inode, umode_t mode)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200237{
238 int error = 0;
239
240 if (mode != inode->i_mode) {
241 struct iattr iattr;
242
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000243 iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200244 iattr.ia_mode = mode;
Christoph Hellwigd6d59ba2009-12-23 16:09:13 +0000245 iattr.ia_ctime = current_fs_time(inode->i_sb);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200246
Dave Chinner24513372014-06-25 14:58:08 +1000247 error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200248 }
249
250 return error;
251}
252
253static int
Dave Chinnera9273ca2010-01-20 10:47:48 +1100254xfs_acl_exists(struct inode *inode, unsigned char *name)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200255{
Dave Chinner0a8aa192013-06-05 12:09:10 +1000256 int len = XFS_ACL_MAX_SIZE(XFS_M(inode->i_sb));
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200257
258 return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
259 ATTR_ROOT|ATTR_KERNOVAL) == 0);
260}
261
262int
263posix_acl_access_exists(struct inode *inode)
264{
265 return xfs_acl_exists(inode, SGI_ACL_FILE);
266}
267
268int
269posix_acl_default_exists(struct inode *inode)
270{
271 if (!S_ISDIR(inode->i_mode))
272 return 0;
273 return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
274}
275
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200276int
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800277xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200278{
Christoph Hellwig431547b2009-11-13 09:52:56 +0000279 int error = 0;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200280
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800281 if (!acl)
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200282 goto set_acl;
283
Jie Liu4ae69fe2014-02-07 15:26:11 +1100284 error = -E2BIG;
Dave Chinner0a8aa192013-06-05 12:09:10 +1000285 if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800286 return error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200287
288 if (type == ACL_TYPE_ACCESS) {
Al Virod6952122011-07-23 18:56:36 -0400289 umode_t mode = inode->i_mode;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200290 error = posix_acl_equiv_mode(acl, &mode);
291
292 if (error <= 0) {
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200293 acl = NULL;
294
295 if (error < 0)
296 return error;
297 }
298
299 error = xfs_set_mode(inode, mode);
300 if (error)
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800301 return error;
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200302 }
303
304 set_acl:
Christoph Hellwig2401dc22013-12-20 05:16:50 -0800305 return __xfs_set_acl(inode, type, acl);
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200306}