Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / fs / ext3 / acl.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/ext3/acl.c
3 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 */
6
7#include <linux/init.h>
8#include <linux/sched.h>
9#include <linux/slab.h>
16f7e0fe 10#include <linux/capability.h>
1da177e4
LT
11#include <linux/fs.h>
12#include <linux/ext3_jbd.h>
13#include <linux/ext3_fs.h>
14#include "xattr.h"
15#include "acl.h"
16
17/*
18 * Convert from filesystem to in-memory representation.
19 */
20static struct posix_acl *
21ext3_acl_from_disk(const void *value, size_t size)
22{
23 const char *end = (char *)value + size;
24 int n, count;
25 struct posix_acl *acl;
26
27 if (!value)
28 return NULL;
29 if (size < sizeof(ext3_acl_header))
30 return ERR_PTR(-EINVAL);
31 if (((ext3_acl_header *)value)->a_version !=
32 cpu_to_le32(EXT3_ACL_VERSION))
33 return ERR_PTR(-EINVAL);
34 value = (char *)value + sizeof(ext3_acl_header);
35 count = ext3_acl_count(size);
36 if (count < 0)
37 return ERR_PTR(-EINVAL);
38 if (count == 0)
39 return NULL;
c587f0c0 40 acl = posix_acl_alloc(count, GFP_NOFS);
1da177e4
LT
41 if (!acl)
42 return ERR_PTR(-ENOMEM);
43 for (n=0; n < count; n++) {
44 ext3_acl_entry *entry =
45 (ext3_acl_entry *)value;
46 if ((char *)value + sizeof(ext3_acl_entry_short) > end)
47 goto fail;
48 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
49 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
50 switch(acl->a_entries[n].e_tag) {
51 case ACL_USER_OBJ:
52 case ACL_GROUP_OBJ:
53 case ACL_MASK:
54 case ACL_OTHER:
55 value = (char *)value +
56 sizeof(ext3_acl_entry_short);
57 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
58 break;
59
60 case ACL_USER:
61 case ACL_GROUP:
62 value = (char *)value + sizeof(ext3_acl_entry);
63 if ((char *)value > end)
64 goto fail;
65 acl->a_entries[n].e_id =
66 le32_to_cpu(entry->e_id);
67 break;
68
69 default:
70 goto fail;
71 }
72 }
73 if (value != end)
74 goto fail;
75 return acl;
76
77fail:
78 posix_acl_release(acl);
79 return ERR_PTR(-EINVAL);
80}
81
82/*
83 * Convert from in-memory to filesystem representation.
84 */
85static void *
86ext3_acl_to_disk(const struct posix_acl *acl, size_t *size)
87{
88 ext3_acl_header *ext_acl;
89 char *e;
90 size_t n;
91
92 *size = ext3_acl_size(acl->a_count);
f52720ca 93 ext_acl = kmalloc(sizeof(ext3_acl_header) + acl->a_count *
c587f0c0 94 sizeof(ext3_acl_entry), GFP_NOFS);
1da177e4
LT
95 if (!ext_acl)
96 return ERR_PTR(-ENOMEM);
97 ext_acl->a_version = cpu_to_le32(EXT3_ACL_VERSION);
98 e = (char *)ext_acl + sizeof(ext3_acl_header);
99 for (n=0; n < acl->a_count; n++) {
100 ext3_acl_entry *entry = (ext3_acl_entry *)e;
101 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
102 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
103 switch(acl->a_entries[n].e_tag) {
104 case ACL_USER:
105 case ACL_GROUP:
106 entry->e_id =
107 cpu_to_le32(acl->a_entries[n].e_id);
108 e += sizeof(ext3_acl_entry);
109 break;
110
111 case ACL_USER_OBJ:
112 case ACL_GROUP_OBJ:
113 case ACL_MASK:
114 case ACL_OTHER:
115 e += sizeof(ext3_acl_entry_short);
116 break;
117
118 default:
119 goto fail;
120 }
121 }
122 return (char *)ext_acl;
123
124fail:
125 kfree(ext_acl);
126 return ERR_PTR(-EINVAL);
127}
128
129static inline struct posix_acl *
130ext3_iget_acl(struct inode *inode, struct posix_acl **i_acl)
131{
9c64daff 132 struct posix_acl *acl = ACCESS_ONCE(*i_acl);
1da177e4 133
9c64daff
LT
134 if (acl) {
135 spin_lock(&inode->i_lock);
136 acl = *i_acl;
137 if (acl != EXT3_ACL_NOT_CACHED)
138 acl = posix_acl_dup(acl);
139 spin_unlock(&inode->i_lock);
140 }
1da177e4
LT
141
142 return acl;
143}
144
145static inline void
146ext3_iset_acl(struct inode *inode, struct posix_acl **i_acl,
147 struct posix_acl *acl)
148{
149 spin_lock(&inode->i_lock);
150 if (*i_acl != EXT3_ACL_NOT_CACHED)
151 posix_acl_release(*i_acl);
152 *i_acl = posix_acl_dup(acl);
153 spin_unlock(&inode->i_lock);
154}
155
156/*
157 * Inode operation get_posix_acl().
158 *
1b1dcc1b 159 * inode->i_mutex: don't care
1da177e4
LT
160 */
161static struct posix_acl *
162ext3_get_acl(struct inode *inode, int type)
163{
164 struct ext3_inode_info *ei = EXT3_I(inode);
165 int name_index;
166 char *value = NULL;
167 struct posix_acl *acl;
168 int retval;
169
170 if (!test_opt(inode->i_sb, POSIX_ACL))
171 return NULL;
172
173 switch(type) {
174 case ACL_TYPE_ACCESS:
175 acl = ext3_iget_acl(inode, &ei->i_acl);
176 if (acl != EXT3_ACL_NOT_CACHED)
177 return acl;
178 name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
179 break;
180
181 case ACL_TYPE_DEFAULT:
182 acl = ext3_iget_acl(inode, &ei->i_default_acl);
183 if (acl != EXT3_ACL_NOT_CACHED)
184 return acl;
185 name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
186 break;
187
188 default:
189 return ERR_PTR(-EINVAL);
190 }
191 retval = ext3_xattr_get(inode, name_index, "", NULL, 0);
192 if (retval > 0) {
c587f0c0 193 value = kmalloc(retval, GFP_NOFS);
1da177e4
LT
194 if (!value)
195 return ERR_PTR(-ENOMEM);
196 retval = ext3_xattr_get(inode, name_index, "", value, retval);
197 }
198 if (retval > 0)
199 acl = ext3_acl_from_disk(value, retval);
200 else if (retval == -ENODATA || retval == -ENOSYS)
201 acl = NULL;
202 else
203 acl = ERR_PTR(retval);
204 kfree(value);
205
206 if (!IS_ERR(acl)) {
207 switch(type) {
208 case ACL_TYPE_ACCESS:
209 ext3_iset_acl(inode, &ei->i_acl, acl);
210 break;
211
212 case ACL_TYPE_DEFAULT:
213 ext3_iset_acl(inode, &ei->i_default_acl, acl);
214 break;
215 }
216 }
217 return acl;
218}
219
220/*
221 * Set the access or default ACL of an inode.
222 *
1b1dcc1b 223 * inode->i_mutex: down unless called from ext3_new_inode
1da177e4
LT
224 */
225static int
226ext3_set_acl(handle_t *handle, struct inode *inode, int type,
227 struct posix_acl *acl)
228{
229 struct ext3_inode_info *ei = EXT3_I(inode);
230 int name_index;
231 void *value = NULL;
dfa08592 232 size_t size = 0;
1da177e4
LT
233 int error;
234
235 if (S_ISLNK(inode->i_mode))
236 return -EOPNOTSUPP;
237
238 switch(type) {
239 case ACL_TYPE_ACCESS:
240 name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
241 if (acl) {
242 mode_t mode = inode->i_mode;
243 error = posix_acl_equiv_mode(acl, &mode);
244 if (error < 0)
245 return error;
246 else {
247 inode->i_mode = mode;
248 ext3_mark_inode_dirty(handle, inode);
249 if (error == 0)
250 acl = NULL;
251 }
252 }
253 break;
254
255 case ACL_TYPE_DEFAULT:
256 name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
257 if (!S_ISDIR(inode->i_mode))
258 return acl ? -EACCES : 0;
259 break;
260
261 default:
262 return -EINVAL;
263 }
e9ad5620 264 if (acl) {
1da177e4
LT
265 value = ext3_acl_to_disk(acl, &size);
266 if (IS_ERR(value))
267 return (int)PTR_ERR(value);
268 }
269
270 error = ext3_xattr_set_handle(handle, inode, name_index, "",
271 value, size, 0);
272
273 kfree(value);
274 if (!error) {
275 switch(type) {
276 case ACL_TYPE_ACCESS:
277 ext3_iset_acl(inode, &ei->i_acl, acl);
278 break;
279
280 case ACL_TYPE_DEFAULT:
281 ext3_iset_acl(inode, &ei->i_default_acl, acl);
282 break;
283 }
284 }
285 return error;
286}
287
288static int
289ext3_check_acl(struct inode *inode, int mask)
290{
291 struct posix_acl *acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
292
e493073d 293 if (IS_ERR(acl))
294 return PTR_ERR(acl);
1da177e4
LT
295 if (acl) {
296 int error = posix_acl_permission(inode, acl, mask);
297 posix_acl_release(acl);
298 return error;
299 }
300
301 return -EAGAIN;
302}
303
304int
e6305c43 305ext3_permission(struct inode *inode, int mask)
1da177e4
LT
306{
307 return generic_permission(inode, mask, ext3_check_acl);
308}
309
310/*
311 * Initialize the ACLs of a new inode. Called from ext3_new_inode.
312 *
1b1dcc1b
JS
313 * dir->i_mutex: down
314 * inode->i_mutex: up (access to inode is still exclusive)
1da177e4
LT
315 */
316int
317ext3_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
318{
319 struct posix_acl *acl = NULL;
320 int error = 0;
321
322 if (!S_ISLNK(inode->i_mode)) {
323 if (test_opt(dir->i_sb, POSIX_ACL)) {
324 acl = ext3_get_acl(dir, ACL_TYPE_DEFAULT);
325 if (IS_ERR(acl))
326 return PTR_ERR(acl);
327 }
328 if (!acl)
ce3b0f8d 329 inode->i_mode &= ~current_umask();
1da177e4
LT
330 }
331 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
332 struct posix_acl *clone;
333 mode_t mode;
334
335 if (S_ISDIR(inode->i_mode)) {
336 error = ext3_set_acl(handle, inode,
337 ACL_TYPE_DEFAULT, acl);
338 if (error)
339 goto cleanup;
340 }
c587f0c0 341 clone = posix_acl_clone(acl, GFP_NOFS);
1da177e4
LT
342 error = -ENOMEM;
343 if (!clone)
344 goto cleanup;
345
346 mode = inode->i_mode;
347 error = posix_acl_create_masq(clone, &mode);
348 if (error >= 0) {
349 inode->i_mode = mode;
350 if (error > 0) {
351 /* This is an extended ACL */
352 error = ext3_set_acl(handle, inode,
353 ACL_TYPE_ACCESS, clone);
354 }
355 }
356 posix_acl_release(clone);
357 }
358cleanup:
359 posix_acl_release(acl);
360 return error;
361}
362
363/*
364 * Does chmod for an inode that may have an Access Control List. The
365 * inode->i_mode field must be updated to the desired value by the caller
366 * before calling this function.
367 * Returns 0 on success, or a negative error number.
368 *
369 * We change the ACL rather than storing some ACL entries in the file
370 * mode permission bits (which would be more efficient), because that
371 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
372 * for directories) are added. There are no more bits available in the
373 * file mode.
374 *
1b1dcc1b 375 * inode->i_mutex: down
1da177e4
LT
376 */
377int
378ext3_acl_chmod(struct inode *inode)
379{
380 struct posix_acl *acl, *clone;
381 int error;
382
383 if (S_ISLNK(inode->i_mode))
384 return -EOPNOTSUPP;
385 if (!test_opt(inode->i_sb, POSIX_ACL))
386 return 0;
387 acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
388 if (IS_ERR(acl) || !acl)
389 return PTR_ERR(acl);
390 clone = posix_acl_clone(acl, GFP_KERNEL);
391 posix_acl_release(acl);
392 if (!clone)
393 return -ENOMEM;
394 error = posix_acl_chmod_masq(clone, inode->i_mode);
395 if (!error) {
396 handle_t *handle;
397 int retries = 0;
398
399 retry:
1f54587b
JK
400 handle = ext3_journal_start(inode,
401 EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
1da177e4
LT
402 if (IS_ERR(handle)) {
403 error = PTR_ERR(handle);
404 ext3_std_error(inode->i_sb, error);
405 goto out;
406 }
407 error = ext3_set_acl(handle, inode, ACL_TYPE_ACCESS, clone);
408 ext3_journal_stop(handle);
409 if (error == -ENOSPC &&
410 ext3_should_retry_alloc(inode->i_sb, &retries))
411 goto retry;
412 }
413out:
414 posix_acl_release(clone);
415 return error;
416}
417
418/*
419 * Extended attribute handlers
420 */
421static size_t
422ext3_xattr_list_acl_access(struct inode *inode, char *list, size_t list_len,
423 const char *name, size_t name_len)
424{
9a59f452 425 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
1da177e4
LT
426
427 if (!test_opt(inode->i_sb, POSIX_ACL))
428 return 0;
429 if (list && size <= list_len)
9a59f452 430 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
1da177e4
LT
431 return size;
432}
433
434static size_t
435ext3_xattr_list_acl_default(struct inode *inode, char *list, size_t list_len,
436 const char *name, size_t name_len)
437{
9a59f452 438 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
1da177e4
LT
439
440 if (!test_opt(inode->i_sb, POSIX_ACL))
441 return 0;
442 if (list && size <= list_len)
9a59f452 443 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
1da177e4
LT
444 return size;
445}
446
447static int
448ext3_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
449{
450 struct posix_acl *acl;
451 int error;
452
453 if (!test_opt(inode->i_sb, POSIX_ACL))
454 return -EOPNOTSUPP;
455
456 acl = ext3_get_acl(inode, type);
457 if (IS_ERR(acl))
458 return PTR_ERR(acl);
459 if (acl == NULL)
460 return -ENODATA;
461 error = posix_acl_to_xattr(acl, buffer, size);
462 posix_acl_release(acl);
463
464 return error;
465}
466
467static int
468ext3_xattr_get_acl_access(struct inode *inode, const char *name,
469 void *buffer, size_t size)
470{
471 if (strcmp(name, "") != 0)
472 return -EINVAL;
473 return ext3_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
474}
475
476static int
477ext3_xattr_get_acl_default(struct inode *inode, const char *name,
478 void *buffer, size_t size)
479{
480 if (strcmp(name, "") != 0)
481 return -EINVAL;
482 return ext3_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
483}
484
485static int
486ext3_xattr_set_acl(struct inode *inode, int type, const void *value,
487 size_t size)
488{
489 handle_t *handle;
490 struct posix_acl *acl;
491 int error, retries = 0;
492
493 if (!test_opt(inode->i_sb, POSIX_ACL))
494 return -EOPNOTSUPP;
3bd858ab 495 if (!is_owner_or_cap(inode))
1da177e4
LT
496 return -EPERM;
497
498 if (value) {
499 acl = posix_acl_from_xattr(value, size);
500 if (IS_ERR(acl))
501 return PTR_ERR(acl);
502 else if (acl) {
503 error = posix_acl_valid(acl);
504 if (error)
505 goto release_and_out;
506 }
507 } else
508 acl = NULL;
509
510retry:
1f54587b 511 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
1da177e4
LT
512 if (IS_ERR(handle))
513 return PTR_ERR(handle);
514 error = ext3_set_acl(handle, inode, type, acl);
515 ext3_journal_stop(handle);
516 if (error == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
517 goto retry;
518
519release_and_out:
520 posix_acl_release(acl);
521 return error;
522}
523
524static int
525ext3_xattr_set_acl_access(struct inode *inode, const char *name,
526 const void *value, size_t size, int flags)
527{
528 if (strcmp(name, "") != 0)
529 return -EINVAL;
530 return ext3_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
531}
532
533static int
534ext3_xattr_set_acl_default(struct inode *inode, const char *name,
535 const void *value, size_t size, int flags)
536{
537 if (strcmp(name, "") != 0)
538 return -EINVAL;
539 return ext3_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
540}
541
542struct xattr_handler ext3_xattr_acl_access_handler = {
9a59f452 543 .prefix = POSIX_ACL_XATTR_ACCESS,
1da177e4
LT
544 .list = ext3_xattr_list_acl_access,
545 .get = ext3_xattr_get_acl_access,
546 .set = ext3_xattr_set_acl_access,
547};
548
549struct xattr_handler ext3_xattr_acl_default_handler = {
9a59f452 550 .prefix = POSIX_ACL_XATTR_DEFAULT,
1da177e4
LT
551 .list = ext3_xattr_list_acl_default,
552 .get = ext3_xattr_get_acl_default,
553 .set = ext3_xattr_set_acl_default,
554};