Commit | Line | Data |
---|---|---|
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 | ||
38 | STATIC struct posix_acl * | |
0a8aa193 DC |
39 | xfs_acl_from_disk( |
40 | struct xfs_acl *aclp, | |
41 | int max_entries) | |
ef14f0c1 CH |
42 | { |
43 | struct posix_acl_entry *acl_e; | |
44 | struct posix_acl *acl; | |
45 | struct xfs_acl_entry *ace; | |
093019cf | 46 | unsigned int count, i; |
ef14f0c1 CH |
47 | |
48 | count = be32_to_cpu(aclp->acl_cnt); | |
0a8aa193 | 49 | if (count > max_entries) |
fa8b18ed | 50 | return ERR_PTR(-EFSCORRUPTED); |
ef14f0c1 CH |
51 | |
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: | |
288bbe0e DE |
71 | acl_e->e_uid = xfs_uid_to_kuid(be32_to_cpu(ace->ae_id)); |
72 | break; | |
ef14f0c1 | 73 | case ACL_GROUP: |
288bbe0e | 74 | acl_e->e_gid = xfs_gid_to_kgid(be32_to_cpu(ace->ae_id)); |
ef14f0c1 CH |
75 | break; |
76 | case ACL_USER_OBJ: | |
77 | case ACL_GROUP_OBJ: | |
78 | case ACL_MASK: | |
79 | case ACL_OTHER: | |
ef14f0c1 CH |
80 | break; |
81 | default: | |
82 | goto fail; | |
83 | } | |
84 | } | |
85 | return acl; | |
86 | ||
87 | fail: | |
88 | posix_acl_release(acl); | |
89 | return ERR_PTR(-EINVAL); | |
90 | } | |
91 | ||
92 | STATIC void | |
93 | xfs_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); | |
288bbe0e DE |
105 | 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 | ||
ef14f0c1 CH |
117 | ace->ae_perm = cpu_to_be16(acl_e->e_perm); |
118 | } | |
119 | } | |
120 | ||
ef14f0c1 CH |
121 | struct posix_acl * |
122 | xfs_get_acl(struct inode *inode, int type) | |
123 | { | |
124 | struct xfs_inode *ip = XFS_I(inode); | |
2401dc29 | 125 | struct posix_acl *acl = NULL; |
ef14f0c1 | 126 | struct xfs_acl *xfs_acl; |
a9273ca5 | 127 | unsigned char *ea_name; |
ef14f0c1 | 128 | int error; |
0a8aa193 | 129 | int len; |
ef14f0c1 | 130 | |
4e34e719 CH |
131 | trace_xfs_get_acl(ip); |
132 | ||
ef14f0c1 CH |
133 | switch (type) { |
134 | case ACL_TYPE_ACCESS: | |
135 | ea_name = SGI_ACL_FILE; | |
ef14f0c1 CH |
136 | break; |
137 | case ACL_TYPE_DEFAULT: | |
138 | ea_name = SGI_ACL_DEFAULT; | |
ef14f0c1 CH |
139 | break; |
140 | default: | |
1cbd20d8 | 141 | BUG(); |
ef14f0c1 CH |
142 | } |
143 | ||
ef14f0c1 CH |
144 | /* |
145 | * If we have a cached ACLs value just return it, not need to | |
146 | * go out to the disk. | |
147 | */ | |
0a8aa193 | 148 | len = XFS_ACL_MAX_SIZE(ip->i_mount); |
fdd3ccee DC |
149 | xfs_acl = kmem_zalloc_large(len, KM_SLEEP); |
150 | if (!xfs_acl) | |
151 | return ERR_PTR(-ENOMEM); | |
ef14f0c1 | 152 | |
2451337d | 153 | error = xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl, |
a9273ca5 | 154 | &len, ATTR_ROOT); |
ef14f0c1 CH |
155 | 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 | |
1cbd20d8 | 159 | * leave the cache entry as ACL_NOT_CACHED. |
ef14f0c1 | 160 | */ |
2401dc29 | 161 | if (error == -ENOATTR) |
ef14f0c1 | 162 | goto out_update_cache; |
ef14f0c1 CH |
163 | goto out; |
164 | } | |
165 | ||
0a8aa193 | 166 | acl = xfs_acl_from_disk(xfs_acl, XFS_ACL_MAX_ENTRIES(ip->i_mount)); |
ef14f0c1 CH |
167 | if (IS_ERR(acl)) |
168 | goto out; | |
169 | ||
2dc164f2 | 170 | out_update_cache: |
1cbd20d8 | 171 | set_cached_acl(inode, type, acl); |
2dc164f2 | 172 | out: |
fdd3ccee | 173 | kmem_free(xfs_acl); |
ef14f0c1 CH |
174 | return acl; |
175 | } | |
176 | ||
177 | STATIC int | |
2401dc29 | 178 | __xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl) |
ef14f0c1 CH |
179 | { |
180 | struct xfs_inode *ip = XFS_I(inode); | |
a9273ca5 | 181 | unsigned char *ea_name; |
ef14f0c1 CH |
182 | int error; |
183 | ||
ef14f0c1 CH |
184 | switch (type) { |
185 | case ACL_TYPE_ACCESS: | |
186 | ea_name = SGI_ACL_FILE; | |
ef14f0c1 CH |
187 | break; |
188 | case ACL_TYPE_DEFAULT: | |
189 | if (!S_ISDIR(inode->i_mode)) | |
190 | return acl ? -EACCES : 0; | |
191 | ea_name = SGI_ACL_DEFAULT; | |
ef14f0c1 CH |
192 | break; |
193 | default: | |
194 | return -EINVAL; | |
195 | } | |
196 | ||
197 | if (acl) { | |
198 | struct xfs_acl *xfs_acl; | |
0a8aa193 | 199 | int len = XFS_ACL_MAX_SIZE(ip->i_mount); |
ef14f0c1 | 200 | |
fdd3ccee DC |
201 | xfs_acl = kmem_zalloc_large(len, KM_SLEEP); |
202 | if (!xfs_acl) | |
203 | return -ENOMEM; | |
ef14f0c1 CH |
204 | |
205 | xfs_acl_to_disk(xfs_acl, acl); | |
0a8aa193 DC |
206 | |
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); | |
ef14f0c1 | 210 | |
2451337d | 211 | error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl, |
ef14f0c1 CH |
212 | len, ATTR_ROOT); |
213 | ||
fdd3ccee | 214 | kmem_free(xfs_acl); |
ef14f0c1 CH |
215 | } else { |
216 | /* | |
217 | * A NULL ACL argument means we want to remove the ACL. | |
218 | */ | |
2451337d | 219 | error = xfs_attr_remove(ip, ea_name, ATTR_ROOT); |
ef14f0c1 CH |
220 | |
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) | |
1cbd20d8 | 229 | set_cached_acl(inode, type, acl); |
ef14f0c1 CH |
230 | return error; |
231 | } | |
232 | ||
ef14f0c1 | 233 | static int |
d3fb6120 | 234 | xfs_set_mode(struct inode *inode, umode_t mode) |
ef14f0c1 CH |
235 | { |
236 | int error = 0; | |
237 | ||
238 | if (mode != inode->i_mode) { | |
239 | struct iattr iattr; | |
240 | ||
d6d59bad | 241 | iattr.ia_valid = ATTR_MODE | ATTR_CTIME; |
ef14f0c1 | 242 | iattr.ia_mode = mode; |
d6d59bad | 243 | iattr.ia_ctime = current_fs_time(inode->i_sb); |
ef14f0c1 | 244 | |
2451337d | 245 | error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL); |
ef14f0c1 CH |
246 | } |
247 | ||
248 | return error; | |
249 | } | |
250 | ||
251 | static int | |
a9273ca5 | 252 | xfs_acl_exists(struct inode *inode, unsigned char *name) |
ef14f0c1 | 253 | { |
0a8aa193 | 254 | int len = XFS_ACL_MAX_SIZE(XFS_M(inode->i_sb)); |
ef14f0c1 CH |
255 | |
256 | return (xfs_attr_get(XFS_I(inode), name, NULL, &len, | |
257 | ATTR_ROOT|ATTR_KERNOVAL) == 0); | |
258 | } | |
259 | ||
260 | int | |
261 | posix_acl_access_exists(struct inode *inode) | |
262 | { | |
263 | return xfs_acl_exists(inode, SGI_ACL_FILE); | |
264 | } | |
265 | ||
266 | int | |
267 | posix_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 | ||
ef14f0c1 | 274 | int |
2401dc29 | 275 | xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type) |
ef14f0c1 | 276 | { |
431547b3 | 277 | int error = 0; |
ef14f0c1 | 278 | |
2401dc29 | 279 | if (!acl) |
ef14f0c1 CH |
280 | goto set_acl; |
281 | ||
4ae69fea | 282 | error = -E2BIG; |
0a8aa193 | 283 | if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb))) |
2401dc29 | 284 | return error; |
ef14f0c1 CH |
285 | |
286 | if (type == ACL_TYPE_ACCESS) { | |
d6952123 | 287 | umode_t mode = inode->i_mode; |
ef14f0c1 CH |
288 | error = posix_acl_equiv_mode(acl, &mode); |
289 | ||
290 | if (error <= 0) { | |
ef14f0c1 CH |
291 | acl = NULL; |
292 | ||
293 | if (error < 0) | |
294 | return error; | |
295 | } | |
296 | ||
297 | error = xfs_set_mode(inode, mode); | |
298 | if (error) | |
2401dc29 | 299 | return error; |
ef14f0c1 CH |
300 | } |
301 | ||
302 | set_acl: | |
2401dc29 | 303 | return __xfs_set_acl(inode, type, acl); |
ef14f0c1 | 304 | } |