Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-next-2.6
[deliverable/linux.git] / fs / ext4 / acl.c
CommitLineData
ac27a0ec 1/*
617ba13b 2 * linux/fs/ext4/acl.c
ac27a0ec
DK
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>
10#include <linux/capability.h>
11#include <linux/fs.h>
3dcf5451
CH
12#include "ext4_jbd2.h"
13#include "ext4.h"
ac27a0ec
DK
14#include "xattr.h"
15#include "acl.h"
16
17/*
18 * Convert from filesystem to in-memory representation.
19 */
20static struct posix_acl *
617ba13b 21ext4_acl_from_disk(const void *value, size_t size)
ac27a0ec
DK
22{
23 const char *end = (char *)value + size;
24 int n, count;
25 struct posix_acl *acl;
26
27 if (!value)
28 return NULL;
617ba13b 29 if (size < sizeof(ext4_acl_header))
ac27a0ec 30 return ERR_PTR(-EINVAL);
617ba13b
MC
31 if (((ext4_acl_header *)value)->a_version !=
32 cpu_to_le32(EXT4_ACL_VERSION))
ac27a0ec 33 return ERR_PTR(-EINVAL);
617ba13b
MC
34 value = (char *)value + sizeof(ext4_acl_header);
35 count = ext4_acl_count(size);
ac27a0ec
DK
36 if (count < 0)
37 return ERR_PTR(-EINVAL);
38 if (count == 0)
39 return NULL;
216553c4 40 acl = posix_acl_alloc(count, GFP_NOFS);
ac27a0ec
DK
41 if (!acl)
42 return ERR_PTR(-ENOMEM);
2b2d6d01 43 for (n = 0; n < count; n++) {
617ba13b
MC
44 ext4_acl_entry *entry =
45 (ext4_acl_entry *)value;
46 if ((char *)value + sizeof(ext4_acl_entry_short) > end)
ac27a0ec
DK
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);
2b2d6d01
TT
50
51 switch (acl->a_entries[n].e_tag) {
52 case ACL_USER_OBJ:
53 case ACL_GROUP_OBJ:
54 case ACL_MASK:
55 case ACL_OTHER:
56 value = (char *)value +
57 sizeof(ext4_acl_entry_short);
58 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
59 break;
60
61 case ACL_USER:
62 case ACL_GROUP:
63 value = (char *)value + sizeof(ext4_acl_entry);
64 if ((char *)value > end)
ac27a0ec 65 goto fail;
2b2d6d01
TT
66 acl->a_entries[n].e_id =
67 le32_to_cpu(entry->e_id);
68 break;
69
70 default:
71 goto fail;
ac27a0ec
DK
72 }
73 }
74 if (value != end)
75 goto fail;
76 return acl;
77
78fail:
79 posix_acl_release(acl);
80 return ERR_PTR(-EINVAL);
81}
82
83/*
84 * Convert from in-memory to filesystem representation.
85 */
86static void *
617ba13b 87ext4_acl_to_disk(const struct posix_acl *acl, size_t *size)
ac27a0ec 88{
617ba13b 89 ext4_acl_header *ext_acl;
ac27a0ec
DK
90 char *e;
91 size_t n;
92
617ba13b
MC
93 *size = ext4_acl_size(acl->a_count);
94 ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count *
216553c4 95 sizeof(ext4_acl_entry), GFP_NOFS);
ac27a0ec
DK
96 if (!ext_acl)
97 return ERR_PTR(-ENOMEM);
617ba13b
MC
98 ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION);
99 e = (char *)ext_acl + sizeof(ext4_acl_header);
2b2d6d01 100 for (n = 0; n < acl->a_count; n++) {
617ba13b 101 ext4_acl_entry *entry = (ext4_acl_entry *)e;
ac27a0ec
DK
102 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
103 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
2b2d6d01
TT
104 switch (acl->a_entries[n].e_tag) {
105 case ACL_USER:
106 case ACL_GROUP:
107 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
108 e += sizeof(ext4_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(ext4_acl_entry_short);
116 break;
117
118 default:
119 goto fail;
ac27a0ec
DK
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 *
617ba13b 130ext4_iget_acl(struct inode *inode, struct posix_acl **i_acl)
ac27a0ec 131{
210ad6ae
TT
132 struct posix_acl *acl = ACCESS_ONCE(*i_acl);
133
134 if (acl) {
135 spin_lock(&inode->i_lock);
136 acl = *i_acl;
137 if (acl != EXT4_ACL_NOT_CACHED)
138 acl = posix_acl_dup(acl);
139 spin_unlock(&inode->i_lock);
140 }
ac27a0ec
DK
141
142 return acl;
143}
144
145static inline void
617ba13b 146ext4_iset_acl(struct inode *inode, struct posix_acl **i_acl,
63f57933 147 struct posix_acl *acl)
ac27a0ec
DK
148{
149 spin_lock(&inode->i_lock);
617ba13b 150 if (*i_acl != EXT4_ACL_NOT_CACHED)
ac27a0ec
DK
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 *
159 * inode->i_mutex: don't care
160 */
161static struct posix_acl *
617ba13b 162ext4_get_acl(struct inode *inode, int type)
ac27a0ec 163{
617ba13b 164 struct ext4_inode_info *ei = EXT4_I(inode);
ac27a0ec
DK
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
2b2d6d01
TT
173 switch (type) {
174 case ACL_TYPE_ACCESS:
175 acl = ext4_iget_acl(inode, &ei->i_acl);
176 if (acl != EXT4_ACL_NOT_CACHED)
177 return acl;
178 name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
179 break;
180
181 case ACL_TYPE_DEFAULT:
182 acl = ext4_iget_acl(inode, &ei->i_default_acl);
183 if (acl != EXT4_ACL_NOT_CACHED)
184 return acl;
185 name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
186 break;
187
188 default:
189 return ERR_PTR(-EINVAL);
ac27a0ec 190 }
617ba13b 191 retval = ext4_xattr_get(inode, name_index, "", NULL, 0);
ac27a0ec 192 if (retval > 0) {
216553c4 193 value = kmalloc(retval, GFP_NOFS);
ac27a0ec
DK
194 if (!value)
195 return ERR_PTR(-ENOMEM);
617ba13b 196 retval = ext4_xattr_get(inode, name_index, "", value, retval);
ac27a0ec
DK
197 }
198 if (retval > 0)
617ba13b 199 acl = ext4_acl_from_disk(value, retval);
ac27a0ec
DK
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)) {
2b2d6d01
TT
207 switch (type) {
208 case ACL_TYPE_ACCESS:
209 ext4_iset_acl(inode, &ei->i_acl, acl);
210 break;
211
212 case ACL_TYPE_DEFAULT:
213 ext4_iset_acl(inode, &ei->i_default_acl, acl);
214 break;
ac27a0ec
DK
215 }
216 }
217 return acl;
218}
219
220/*
221 * Set the access or default ACL of an inode.
222 *
617ba13b 223 * inode->i_mutex: down unless called from ext4_new_inode
ac27a0ec
DK
224 */
225static int
617ba13b 226ext4_set_acl(handle_t *handle, struct inode *inode, int type,
ac27a0ec
DK
227 struct posix_acl *acl)
228{
617ba13b 229 struct ext4_inode_info *ei = EXT4_I(inode);
ac27a0ec
DK
230 int name_index;
231 void *value = NULL;
232 size_t size = 0;
233 int error;
234
235 if (S_ISLNK(inode->i_mode))
236 return -EOPNOTSUPP;
237
2b2d6d01
TT
238 switch (type) {
239 case ACL_TYPE_ACCESS:
240 name_index = EXT4_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 ext4_mark_inode_dirty(handle, inode);
249 if (error == 0)
250 acl = NULL;
ac27a0ec 251 }
2b2d6d01
TT
252 }
253 break;
ac27a0ec 254
2b2d6d01
TT
255 case ACL_TYPE_DEFAULT:
256 name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
257 if (!S_ISDIR(inode->i_mode))
258 return acl ? -EACCES : 0;
259 break;
ac27a0ec 260
2b2d6d01
TT
261 default:
262 return -EINVAL;
ac27a0ec
DK
263 }
264 if (acl) {
617ba13b 265 value = ext4_acl_to_disk(acl, &size);
ac27a0ec
DK
266 if (IS_ERR(value))
267 return (int)PTR_ERR(value);
268 }
269
617ba13b 270 error = ext4_xattr_set_handle(handle, inode, name_index, "",
ac27a0ec
DK
271 value, size, 0);
272
273 kfree(value);
274 if (!error) {
2b2d6d01
TT
275 switch (type) {
276 case ACL_TYPE_ACCESS:
277 ext4_iset_acl(inode, &ei->i_acl, acl);
278 break;
279
280 case ACL_TYPE_DEFAULT:
281 ext4_iset_acl(inode, &ei->i_default_acl, acl);
282 break;
ac27a0ec
DK
283 }
284 }
285 return error;
286}
287
288static int
617ba13b 289ext4_check_acl(struct inode *inode, int mask)
ac27a0ec 290{
617ba13b 291 struct posix_acl *acl = ext4_get_acl(inode, ACL_TYPE_ACCESS);
ac27a0ec
DK
292
293 if (IS_ERR(acl))
294 return PTR_ERR(acl);
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 305ext4_permission(struct inode *inode, int mask)
ac27a0ec 306{
617ba13b 307 return generic_permission(inode, mask, ext4_check_acl);
ac27a0ec
DK
308}
309
310/*
617ba13b 311 * Initialize the ACLs of a new inode. Called from ext4_new_inode.
ac27a0ec
DK
312 *
313 * dir->i_mutex: down
314 * inode->i_mutex: up (access to inode is still exclusive)
315 */
316int
617ba13b 317ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
ac27a0ec
DK
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)) {
617ba13b 324 acl = ext4_get_acl(dir, ACL_TYPE_DEFAULT);
ac27a0ec
DK
325 if (IS_ERR(acl))
326 return PTR_ERR(acl);
327 }
328 if (!acl)
ce3b0f8d 329 inode->i_mode &= ~current_umask();
ac27a0ec
DK
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)) {
617ba13b 336 error = ext4_set_acl(handle, inode,
ac27a0ec
DK
337 ACL_TYPE_DEFAULT, acl);
338 if (error)
339 goto cleanup;
340 }
216553c4 341 clone = posix_acl_clone(acl, GFP_NOFS);
ac27a0ec
DK
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 */
617ba13b 352 error = ext4_set_acl(handle, inode,
ac27a0ec
DK
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 *
375 * inode->i_mutex: down
376 */
377int
617ba13b 378ext4_acl_chmod(struct inode *inode)
ac27a0ec
DK
379{
380 struct posix_acl *acl, *clone;
63f57933 381 int error;
ac27a0ec
DK
382
383 if (S_ISLNK(inode->i_mode))
384 return -EOPNOTSUPP;
385 if (!test_opt(inode->i_sb, POSIX_ACL))
386 return 0;
617ba13b 387 acl = ext4_get_acl(inode, ACL_TYPE_ACCESS);
ac27a0ec
DK
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:
617ba13b
MC
400 handle = ext4_journal_start(inode,
401 EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
ac27a0ec
DK
402 if (IS_ERR(handle)) {
403 error = PTR_ERR(handle);
617ba13b 404 ext4_std_error(inode->i_sb, error);
ac27a0ec
DK
405 goto out;
406 }
617ba13b
MC
407 error = ext4_set_acl(handle, inode, ACL_TYPE_ACCESS, clone);
408 ext4_journal_stop(handle);
ac27a0ec 409 if (error == -ENOSPC &&
617ba13b 410 ext4_should_retry_alloc(inode->i_sb, &retries))
ac27a0ec
DK
411 goto retry;
412 }
413out:
414 posix_acl_release(clone);
415 return error;
416}
417
418/*
419 * Extended attribute handlers
420 */
421static size_t
617ba13b 422ext4_xattr_list_acl_access(struct inode *inode, char *list, size_t list_len,
ac27a0ec
DK
423 const char *name, size_t name_len)
424{
425 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
426
427 if (!test_opt(inode->i_sb, POSIX_ACL))
428 return 0;
429 if (list && size <= list_len)
430 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
431 return size;
432}
433
434static size_t
617ba13b 435ext4_xattr_list_acl_default(struct inode *inode, char *list, size_t list_len,
ac27a0ec
DK
436 const char *name, size_t name_len)
437{
438 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
439
440 if (!test_opt(inode->i_sb, POSIX_ACL))
441 return 0;
442 if (list && size <= list_len)
443 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
444 return size;
445}
446
447static int
617ba13b 448ext4_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
ac27a0ec
DK
449{
450 struct posix_acl *acl;
451 int error;
452
453 if (!test_opt(inode->i_sb, POSIX_ACL))
454 return -EOPNOTSUPP;
455
617ba13b 456 acl = ext4_get_acl(inode, type);
ac27a0ec
DK
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
617ba13b 468ext4_xattr_get_acl_access(struct inode *inode, const char *name,
ac27a0ec
DK
469 void *buffer, size_t size)
470{
471 if (strcmp(name, "") != 0)
472 return -EINVAL;
617ba13b 473 return ext4_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
ac27a0ec
DK
474}
475
476static int
617ba13b 477ext4_xattr_get_acl_default(struct inode *inode, const char *name,
ac27a0ec
DK
478 void *buffer, size_t size)
479{
480 if (strcmp(name, "") != 0)
481 return -EINVAL;
617ba13b 482 return ext4_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
ac27a0ec
DK
483}
484
485static int
617ba13b 486ext4_xattr_set_acl(struct inode *inode, int type, const void *value,
ac27a0ec
DK
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))
ac27a0ec
DK
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:
617ba13b 511 handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
ac27a0ec
DK
512 if (IS_ERR(handle))
513 return PTR_ERR(handle);
617ba13b
MC
514 error = ext4_set_acl(handle, inode, type, acl);
515 ext4_journal_stop(handle);
516 if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
ac27a0ec
DK
517 goto retry;
518
519release_and_out:
520 posix_acl_release(acl);
521 return error;
522}
523
524static int
617ba13b 525ext4_xattr_set_acl_access(struct inode *inode, const char *name,
ac27a0ec
DK
526 const void *value, size_t size, int flags)
527{
528 if (strcmp(name, "") != 0)
529 return -EINVAL;
617ba13b 530 return ext4_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
ac27a0ec
DK
531}
532
533static int
617ba13b 534ext4_xattr_set_acl_default(struct inode *inode, const char *name,
ac27a0ec
DK
535 const void *value, size_t size, int flags)
536{
537 if (strcmp(name, "") != 0)
538 return -EINVAL;
617ba13b 539 return ext4_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
ac27a0ec
DK
540}
541
617ba13b 542struct xattr_handler ext4_xattr_acl_access_handler = {
ac27a0ec 543 .prefix = POSIX_ACL_XATTR_ACCESS,
617ba13b
MC
544 .list = ext4_xattr_list_acl_access,
545 .get = ext4_xattr_get_acl_access,
546 .set = ext4_xattr_set_acl_access,
ac27a0ec
DK
547};
548
617ba13b 549struct xattr_handler ext4_xattr_acl_default_handler = {
ac27a0ec 550 .prefix = POSIX_ACL_XATTR_DEFAULT,
617ba13b
MC
551 .list = ext4_xattr_list_acl_default,
552 .get = ext4_xattr_get_acl_default,
553 .set = ext4_xattr_set_acl_default,
ac27a0ec 554};
This page took 0.279177 seconds and 5 git commands to generate.