generic_acl: no need to clone acl just to push it to set_cached_acl()
[linux-2.6-block.git] / fs / ext2 / acl.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/ext2/acl.c
3 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 */
6
16f7e0fe 7#include <linux/capability.h>
1da177e4
LT
8#include <linux/init.h>
9#include <linux/sched.h>
10#include <linux/slab.h>
11#include <linux/fs.h>
12#include "ext2.h"
13#include "xattr.h"
14#include "acl.h"
15
16/*
17 * Convert from filesystem to in-memory representation.
18 */
19static struct posix_acl *
20ext2_acl_from_disk(const void *value, size_t size)
21{
22 const char *end = (char *)value + size;
23 int n, count;
24 struct posix_acl *acl;
25
26 if (!value)
27 return NULL;
28 if (size < sizeof(ext2_acl_header))
29 return ERR_PTR(-EINVAL);
30 if (((ext2_acl_header *)value)->a_version !=
31 cpu_to_le32(EXT2_ACL_VERSION))
32 return ERR_PTR(-EINVAL);
33 value = (char *)value + sizeof(ext2_acl_header);
34 count = ext2_acl_count(size);
35 if (count < 0)
36 return ERR_PTR(-EINVAL);
37 if (count == 0)
38 return NULL;
39 acl = posix_acl_alloc(count, GFP_KERNEL);
40 if (!acl)
41 return ERR_PTR(-ENOMEM);
42 for (n=0; n < count; n++) {
43 ext2_acl_entry *entry =
44 (ext2_acl_entry *)value;
45 if ((char *)value + sizeof(ext2_acl_entry_short) > end)
46 goto fail;
47 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
48 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
49 switch(acl->a_entries[n].e_tag) {
50 case ACL_USER_OBJ:
51 case ACL_GROUP_OBJ:
52 case ACL_MASK:
53 case ACL_OTHER:
54 value = (char *)value +
55 sizeof(ext2_acl_entry_short);
56 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
57 break;
58
59 case ACL_USER:
60 case ACL_GROUP:
61 value = (char *)value + sizeof(ext2_acl_entry);
62 if ((char *)value > end)
63 goto fail;
64 acl->a_entries[n].e_id =
65 le32_to_cpu(entry->e_id);
66 break;
67
68 default:
69 goto fail;
70 }
71 }
72 if (value != end)
73 goto fail;
74 return acl;
75
76fail:
77 posix_acl_release(acl);
78 return ERR_PTR(-EINVAL);
79}
80
81/*
82 * Convert from in-memory to filesystem representation.
83 */
84static void *
85ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
86{
87 ext2_acl_header *ext_acl;
88 char *e;
89 size_t n;
90
91 *size = ext2_acl_size(acl->a_count);
f52720ca
PI
92 ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
93 sizeof(ext2_acl_entry), GFP_KERNEL);
1da177e4
LT
94 if (!ext_acl)
95 return ERR_PTR(-ENOMEM);
96 ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
97 e = (char *)ext_acl + sizeof(ext2_acl_header);
98 for (n=0; n < acl->a_count; n++) {
99 ext2_acl_entry *entry = (ext2_acl_entry *)e;
100 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
101 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
102 switch(acl->a_entries[n].e_tag) {
103 case ACL_USER:
104 case ACL_GROUP:
105 entry->e_id =
106 cpu_to_le32(acl->a_entries[n].e_id);
107 e += sizeof(ext2_acl_entry);
108 break;
109
110 case ACL_USER_OBJ:
111 case ACL_GROUP_OBJ:
112 case ACL_MASK:
113 case ACL_OTHER:
114 e += sizeof(ext2_acl_entry_short);
115 break;
116
117 default:
118 goto fail;
119 }
120 }
121 return (char *)ext_acl;
122
123fail:
124 kfree(ext_acl);
125 return ERR_PTR(-EINVAL);
126}
127
1da177e4 128/*
1b1dcc1b 129 * inode->i_mutex: don't care
1da177e4
LT
130 */
131static struct posix_acl *
132ext2_get_acl(struct inode *inode, int type)
133{
1da177e4
LT
134 int name_index;
135 char *value = NULL;
136 struct posix_acl *acl;
137 int retval;
138
139 if (!test_opt(inode->i_sb, POSIX_ACL))
140 return NULL;
141
073aaa1b
AV
142 acl = get_cached_acl(inode, type);
143 if (acl != ACL_NOT_CACHED)
144 return acl;
145
146 switch (type) {
147 case ACL_TYPE_ACCESS:
148 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
149 break;
150 case ACL_TYPE_DEFAULT:
151 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
152 break;
153 default:
154 BUG();
1da177e4
LT
155 }
156 retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
157 if (retval > 0) {
158 value = kmalloc(retval, GFP_KERNEL);
159 if (!value)
160 return ERR_PTR(-ENOMEM);
161 retval = ext2_xattr_get(inode, name_index, "", value, retval);
162 }
163 if (retval > 0)
164 acl = ext2_acl_from_disk(value, retval);
165 else if (retval == -ENODATA || retval == -ENOSYS)
166 acl = NULL;
167 else
168 acl = ERR_PTR(retval);
f99d49ad 169 kfree(value);
1da177e4 170
073aaa1b
AV
171 if (!IS_ERR(acl))
172 set_cached_acl(inode, type, acl);
1da177e4 173
1da177e4
LT
174 return acl;
175}
176
177/*
1b1dcc1b 178 * inode->i_mutex: down
1da177e4
LT
179 */
180static int
181ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
182{
1da177e4
LT
183 int name_index;
184 void *value = NULL;
dfa08592 185 size_t size = 0;
1da177e4
LT
186 int error;
187
188 if (S_ISLNK(inode->i_mode))
189 return -EOPNOTSUPP;
190 if (!test_opt(inode->i_sb, POSIX_ACL))
191 return 0;
192
193 switch(type) {
194 case ACL_TYPE_ACCESS:
195 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
196 if (acl) {
197 mode_t mode = inode->i_mode;
198 error = posix_acl_equiv_mode(acl, &mode);
199 if (error < 0)
200 return error;
201 else {
202 inode->i_mode = mode;
523825bc 203 inode->i_ctime = CURRENT_TIME_SEC;
1da177e4
LT
204 mark_inode_dirty(inode);
205 if (error == 0)
206 acl = NULL;
207 }
208 }
209 break;
210
211 case ACL_TYPE_DEFAULT:
212 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
213 if (!S_ISDIR(inode->i_mode))
214 return acl ? -EACCES : 0;
215 break;
216
217 default:
218 return -EINVAL;
219 }
220 if (acl) {
221 value = ext2_acl_to_disk(acl, &size);
222 if (IS_ERR(value))
223 return (int)PTR_ERR(value);
224 }
225
226 error = ext2_xattr_set(inode, name_index, "", value, size, 0);
227
f99d49ad 228 kfree(value);
073aaa1b
AV
229 if (!error)
230 set_cached_acl(inode, type, acl);
1da177e4
LT
231 return error;
232}
233
1d5ccd1c 234int
7e40145e 235ext2_check_acl(struct inode *inode, int mask)
1da177e4 236{
b74c79e9
NP
237 struct posix_acl *acl;
238
b74c79e9 239 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
e493073d 240 if (IS_ERR(acl))
241 return PTR_ERR(acl);
1da177e4
LT
242 if (acl) {
243 int error = posix_acl_permission(inode, acl, mask);
244 posix_acl_release(acl);
245 return error;
246 }
247
248 return -EAGAIN;
249}
250
1da177e4
LT
251/*
252 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
253 *
1b1dcc1b
JS
254 * dir->i_mutex: down
255 * inode->i_mutex: up (access to inode is still exclusive)
1da177e4
LT
256 */
257int
258ext2_init_acl(struct inode *inode, struct inode *dir)
259{
260 struct posix_acl *acl = NULL;
261 int error = 0;
262
263 if (!S_ISLNK(inode->i_mode)) {
264 if (test_opt(dir->i_sb, POSIX_ACL)) {
265 acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
266 if (IS_ERR(acl))
267 return PTR_ERR(acl);
268 }
269 if (!acl)
ce3b0f8d 270 inode->i_mode &= ~current_umask();
1da177e4
LT
271 }
272 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
273 struct posix_acl *clone;
274 mode_t mode;
275
276 if (S_ISDIR(inode->i_mode)) {
277 error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
278 if (error)
279 goto cleanup;
280 }
281 clone = posix_acl_clone(acl, GFP_KERNEL);
282 error = -ENOMEM;
283 if (!clone)
284 goto cleanup;
285 mode = inode->i_mode;
286 error = posix_acl_create_masq(clone, &mode);
287 if (error >= 0) {
288 inode->i_mode = mode;
289 if (error > 0) {
290 /* This is an extended ACL */
291 error = ext2_set_acl(inode,
292 ACL_TYPE_ACCESS, clone);
293 }
294 }
295 posix_acl_release(clone);
296 }
297cleanup:
298 posix_acl_release(acl);
299 return error;
300}
301
302/*
303 * Does chmod for an inode that may have an Access Control List. The
304 * inode->i_mode field must be updated to the desired value by the caller
305 * before calling this function.
306 * Returns 0 on success, or a negative error number.
307 *
308 * We change the ACL rather than storing some ACL entries in the file
309 * mode permission bits (which would be more efficient), because that
310 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
311 * for directories) are added. There are no more bits available in the
312 * file mode.
313 *
1b1dcc1b 314 * inode->i_mutex: down
1da177e4
LT
315 */
316int
317ext2_acl_chmod(struct inode *inode)
318{
bc26ab5f 319 struct posix_acl *acl;
1da177e4
LT
320 int error;
321
322 if (!test_opt(inode->i_sb, POSIX_ACL))
323 return 0;
324 if (S_ISLNK(inode->i_mode))
325 return -EOPNOTSUPP;
326 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
327 if (IS_ERR(acl) || !acl)
328 return PTR_ERR(acl);
bc26ab5f
AV
329 error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
330 if (error)
331 return error;
332 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, acl);
1da177e4 333 posix_acl_release(acl);
1da177e4
LT
334 return error;
335}
336
337/*
338 * Extended attribut handlers
339 */
340static size_t
431547b3
CH
341ext2_xattr_list_acl_access(struct dentry *dentry, char *list, size_t list_size,
342 const char *name, size_t name_len, int type)
1da177e4 343{
9a59f452 344 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
1da177e4 345
431547b3 346 if (!test_opt(dentry->d_sb, POSIX_ACL))
1da177e4
LT
347 return 0;
348 if (list && size <= list_size)
9a59f452 349 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
1da177e4
LT
350 return size;
351}
352
353static size_t
431547b3
CH
354ext2_xattr_list_acl_default(struct dentry *dentry, char *list, size_t list_size,
355 const char *name, size_t name_len, int type)
1da177e4 356{
9a59f452 357 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
1da177e4 358
431547b3 359 if (!test_opt(dentry->d_sb, POSIX_ACL))
1da177e4
LT
360 return 0;
361 if (list && size <= list_size)
9a59f452 362 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
1da177e4
LT
363 return size;
364}
365
366static int
431547b3
CH
367ext2_xattr_get_acl(struct dentry *dentry, const char *name, void *buffer,
368 size_t size, int type)
1da177e4
LT
369{
370 struct posix_acl *acl;
371 int error;
372
431547b3
CH
373 if (strcmp(name, "") != 0)
374 return -EINVAL;
375 if (!test_opt(dentry->d_sb, POSIX_ACL))
1da177e4
LT
376 return -EOPNOTSUPP;
377
431547b3 378 acl = ext2_get_acl(dentry->d_inode, type);
1da177e4
LT
379 if (IS_ERR(acl))
380 return PTR_ERR(acl);
381 if (acl == NULL)
382 return -ENODATA;
383 error = posix_acl_to_xattr(acl, buffer, size);
384 posix_acl_release(acl);
385
386 return error;
387}
388
389static int
431547b3
CH
390ext2_xattr_set_acl(struct dentry *dentry, const char *name, const void *value,
391 size_t size, int flags, int type)
1da177e4
LT
392{
393 struct posix_acl *acl;
394 int error;
395
431547b3
CH
396 if (strcmp(name, "") != 0)
397 return -EINVAL;
398 if (!test_opt(dentry->d_sb, POSIX_ACL))
1da177e4 399 return -EOPNOTSUPP;
2e149670 400 if (!inode_owner_or_capable(dentry->d_inode))
1da177e4
LT
401 return -EPERM;
402
403 if (value) {
404 acl = posix_acl_from_xattr(value, size);
405 if (IS_ERR(acl))
406 return PTR_ERR(acl);
407 else if (acl) {
408 error = posix_acl_valid(acl);
409 if (error)
410 goto release_and_out;
411 }
412 } else
413 acl = NULL;
414
431547b3 415 error = ext2_set_acl(dentry->d_inode, type, acl);
1da177e4
LT
416
417release_and_out:
418 posix_acl_release(acl);
419 return error;
420}
421
749c72ef 422const struct xattr_handler ext2_xattr_acl_access_handler = {
9a59f452 423 .prefix = POSIX_ACL_XATTR_ACCESS,
431547b3 424 .flags = ACL_TYPE_ACCESS,
1da177e4 425 .list = ext2_xattr_list_acl_access,
431547b3
CH
426 .get = ext2_xattr_get_acl,
427 .set = ext2_xattr_set_acl,
1da177e4
LT
428};
429
749c72ef 430const struct xattr_handler ext2_xattr_acl_default_handler = {
9a59f452 431 .prefix = POSIX_ACL_XATTR_DEFAULT,
431547b3 432 .flags = ACL_TYPE_DEFAULT,
1da177e4 433 .list = ext2_xattr_list_acl_default,
431547b3
CH
434 .get = ext2_xattr_get_acl,
435 .set = ext2_xattr_set_acl,
1da177e4 436};