4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
7 #include <linux/init.h>
8 #include <linux/sched.h>
9 #include <linux/slab.h>
11 #include <linux/namei.h>
12 #include <linux/ext3_jbd.h>
13 #include <linux/ext3_fs.h>
18 * Convert from filesystem to in-memory representation.
20 static struct posix_acl *
21 ext3_acl_from_disk(const void *value, size_t size)
23 const char *end = (char *)value + size;
25 struct posix_acl *acl;
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);
37 return ERR_PTR(-EINVAL);
40 acl = posix_acl_alloc(count, GFP_KERNEL);
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)
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) {
55 value = (char *)value +
56 sizeof(ext3_acl_entry_short);
57 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
62 value = (char *)value + sizeof(ext3_acl_entry);
63 if ((char *)value > end)
65 acl->a_entries[n].e_id =
66 le32_to_cpu(entry->e_id);
78 posix_acl_release(acl);
79 return ERR_PTR(-EINVAL);
83 * Convert from in-memory to filesystem representation.
86 ext3_acl_to_disk(const struct posix_acl *acl, size_t *size)
88 ext3_acl_header *ext_acl;
92 *size = ext3_acl_size(acl->a_count);
93 ext_acl = (ext3_acl_header *)kmalloc(sizeof(ext3_acl_header) +
94 acl->a_count * sizeof(ext3_acl_entry), GFP_KERNEL);
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) {
107 cpu_to_le32(acl->a_entries[n].e_id);
108 e += sizeof(ext3_acl_entry);
115 e += sizeof(ext3_acl_entry_short);
122 return (char *)ext_acl;
126 return ERR_PTR(-EINVAL);
129 static inline struct posix_acl *
130 ext3_iget_acl(struct inode *inode, struct posix_acl **i_acl)
132 struct posix_acl *acl = EXT3_ACL_NOT_CACHED;
134 spin_lock(&inode->i_lock);
135 if (*i_acl != EXT3_ACL_NOT_CACHED)
136 acl = posix_acl_dup(*i_acl);
137 spin_unlock(&inode->i_lock);
143 ext3_iset_acl(struct inode *inode, struct posix_acl **i_acl,
144 struct posix_acl *acl)
146 spin_lock(&inode->i_lock);
147 if (*i_acl != EXT3_ACL_NOT_CACHED)
148 posix_acl_release(*i_acl);
149 *i_acl = posix_acl_dup(acl);
150 spin_unlock(&inode->i_lock);
154 * Inode operation get_posix_acl().
156 * inode->i_sem: don't care
158 static struct posix_acl *
159 ext3_get_acl(struct inode *inode, int type)
161 struct ext3_inode_info *ei = EXT3_I(inode);
164 struct posix_acl *acl;
167 if (!test_opt(inode->i_sb, POSIX_ACL))
171 case ACL_TYPE_ACCESS:
172 acl = ext3_iget_acl(inode, &ei->i_acl);
173 if (acl != EXT3_ACL_NOT_CACHED)
175 name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
178 case ACL_TYPE_DEFAULT:
179 acl = ext3_iget_acl(inode, &ei->i_default_acl);
180 if (acl != EXT3_ACL_NOT_CACHED)
182 name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
186 return ERR_PTR(-EINVAL);
188 retval = ext3_xattr_get(inode, name_index, "", NULL, 0);
190 value = kmalloc(retval, GFP_KERNEL);
192 return ERR_PTR(-ENOMEM);
193 retval = ext3_xattr_get(inode, name_index, "", value, retval);
196 acl = ext3_acl_from_disk(value, retval);
197 else if (retval == -ENODATA || retval == -ENOSYS)
200 acl = ERR_PTR(retval);
206 case ACL_TYPE_ACCESS:
207 ext3_iset_acl(inode, &ei->i_acl, acl);
210 case ACL_TYPE_DEFAULT:
211 ext3_iset_acl(inode, &ei->i_default_acl, acl);
219 * Set the access or default ACL of an inode.
221 * inode->i_sem: down unless called from ext3_new_inode
224 ext3_set_acl(handle_t *handle, struct inode *inode, int type,
225 struct posix_acl *acl)
227 struct ext3_inode_info *ei = EXT3_I(inode);
233 if (S_ISLNK(inode->i_mode))
237 case ACL_TYPE_ACCESS:
238 name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
240 mode_t mode = inode->i_mode;
241 error = posix_acl_equiv_mode(acl, &mode);
245 inode->i_mode = mode;
246 ext3_mark_inode_dirty(handle, inode);
253 case ACL_TYPE_DEFAULT:
254 name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
255 if (!S_ISDIR(inode->i_mode))
256 return acl ? -EACCES : 0;
263 if (acl->a_count > EXT3_ACL_MAX_ENTRIES)
265 value = ext3_acl_to_disk(acl, &size);
267 return (int)PTR_ERR(value);
270 error = ext3_xattr_set_handle(handle, inode, name_index, "",
277 case ACL_TYPE_ACCESS:
278 ext3_iset_acl(inode, &ei->i_acl, acl);
281 case ACL_TYPE_DEFAULT:
282 ext3_iset_acl(inode, &ei->i_default_acl, acl);
290 * Inode operation permission().
292 * inode->i_sem: don't care
295 ext3_permission(struct inode *inode, int mask, struct nameidata *nd)
297 int mode = inode->i_mode;
299 /* Nobody gets write access to a read-only fs */
300 if ((mask & MAY_WRITE) && (IS_RDONLY(inode) ||
301 (nd && nd->mnt && MNT_IS_RDONLY(nd->mnt))) &&
302 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
304 /* Nobody gets write access to an immutable file */
305 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
307 if (current->fsuid == inode->i_uid) {
309 } else if (test_opt(inode->i_sb, POSIX_ACL)) {
310 struct posix_acl *acl;
312 /* The access ACL cannot grant access if the group class
313 permission bits don't contain all requested permissions. */
314 if (((mode >> 3) & mask & S_IRWXO) != mask)
316 acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
318 int error = posix_acl_permission(inode, acl, mask);
319 posix_acl_release(acl);
320 if (error == -EACCES)
321 goto check_capabilities;
327 if (in_group_p(inode->i_gid))
330 if ((mode & mask & S_IRWXO) == mask)
334 /* Allowed to override Discretionary Access Control? */
335 if (!(mask & MAY_EXEC) ||
336 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
337 if (capable(CAP_DAC_OVERRIDE))
339 /* Read and search granted if capable(CAP_DAC_READ_SEARCH) */
340 if (capable(CAP_DAC_READ_SEARCH) && ((mask == MAY_READ) ||
341 (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))))
347 * Initialize the ACLs of a new inode. Called from ext3_new_inode.
350 * inode->i_sem: up (access to inode is still exclusive)
353 ext3_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
355 struct posix_acl *acl = NULL;
358 if (!S_ISLNK(inode->i_mode)) {
359 if (test_opt(dir->i_sb, POSIX_ACL)) {
360 acl = ext3_get_acl(dir, ACL_TYPE_DEFAULT);
365 inode->i_mode &= ~current->fs->umask;
367 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
368 struct posix_acl *clone;
371 if (S_ISDIR(inode->i_mode)) {
372 error = ext3_set_acl(handle, inode,
373 ACL_TYPE_DEFAULT, acl);
377 clone = posix_acl_clone(acl, GFP_KERNEL);
382 mode = inode->i_mode;
383 error = posix_acl_create_masq(clone, &mode);
385 inode->i_mode = mode;
387 /* This is an extended ACL */
388 error = ext3_set_acl(handle, inode,
389 ACL_TYPE_ACCESS, clone);
392 posix_acl_release(clone);
395 posix_acl_release(acl);
400 * Does chmod for an inode that may have an Access Control List. The
401 * inode->i_mode field must be updated to the desired value by the caller
402 * before calling this function.
403 * Returns 0 on success, or a negative error number.
405 * We change the ACL rather than storing some ACL entries in the file
406 * mode permission bits (which would be more efficient), because that
407 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
408 * for directories) are added. There are no more bits available in the
414 ext3_acl_chmod(struct inode *inode)
416 struct posix_acl *acl, *clone;
419 if (S_ISLNK(inode->i_mode))
421 if (!test_opt(inode->i_sb, POSIX_ACL))
423 acl = ext3_get_acl(inode, ACL_TYPE_ACCESS);
424 if (IS_ERR(acl) || !acl)
426 clone = posix_acl_clone(acl, GFP_KERNEL);
427 posix_acl_release(acl);
430 error = posix_acl_chmod_masq(clone, inode->i_mode);
436 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS);
437 if (IS_ERR(handle)) {
438 error = PTR_ERR(handle);
439 ext3_std_error(inode->i_sb, error);
442 error = ext3_set_acl(handle, inode, ACL_TYPE_ACCESS, clone);
443 ext3_journal_stop(handle);
444 if (error == -ENOSPC &&
445 ext3_should_retry_alloc(inode->i_sb, &retries))
449 posix_acl_release(clone);
454 * Extended attribute handlers
457 ext3_xattr_list_acl_access(char *list, struct inode *inode,
458 const char *name, int name_len)
460 const size_t size = sizeof(XATTR_NAME_ACL_ACCESS);
462 if (!test_opt(inode->i_sb, POSIX_ACL))
465 memcpy(list, XATTR_NAME_ACL_ACCESS, size);
470 ext3_xattr_list_acl_default(char *list, struct inode *inode,
471 const char *name, int name_len)
473 const size_t size = sizeof(XATTR_NAME_ACL_DEFAULT);
475 if (!test_opt(inode->i_sb, POSIX_ACL))
478 memcpy(list, XATTR_NAME_ACL_DEFAULT, size);
483 ext3_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
485 struct posix_acl *acl;
488 if (!test_opt(inode->i_sb, POSIX_ACL))
491 acl = ext3_get_acl(inode, type);
496 error = posix_acl_to_xattr(acl, buffer, size);
497 posix_acl_release(acl);
503 ext3_xattr_get_acl_access(struct inode *inode, const char *name,
504 void *buffer, size_t size)
506 if (strcmp(name, "") != 0)
508 return ext3_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
512 ext3_xattr_get_acl_default(struct inode *inode, const char *name,
513 void *buffer, size_t size)
515 if (strcmp(name, "") != 0)
517 return ext3_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
521 ext3_xattr_set_acl(struct inode *inode, int type, const void *value,
525 struct posix_acl *acl;
526 int error, retries = 0;
528 if (!test_opt(inode->i_sb, POSIX_ACL))
530 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
534 acl = posix_acl_from_xattr(value, size);
538 error = posix_acl_valid(acl);
540 goto release_and_out;
546 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS);
548 return PTR_ERR(handle);
549 error = ext3_set_acl(handle, inode, type, acl);
550 ext3_journal_stop(handle);
551 if (error == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
555 posix_acl_release(acl);
560 ext3_xattr_set_acl_access(struct inode *inode, const char *name,
561 const void *value, size_t size, int flags)
563 if (strcmp(name, "") != 0)
565 return ext3_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
569 ext3_xattr_set_acl_default(struct inode *inode, const char *name,
570 const void *value, size_t size, int flags)
572 if (strcmp(name, "") != 0)
574 return ext3_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
577 struct ext3_xattr_handler ext3_xattr_acl_access_handler = {
578 .prefix = XATTR_NAME_ACL_ACCESS,
579 .list = ext3_xattr_list_acl_access,
580 .get = ext3_xattr_get_acl_access,
581 .set = ext3_xattr_set_acl_access,
584 struct ext3_xattr_handler ext3_xattr_acl_default_handler = {
585 .prefix = XATTR_NAME_ACL_DEFAULT,
586 .list = ext3_xattr_list_acl_default,
587 .get = ext3_xattr_get_acl_default,
588 .set = ext3_xattr_set_acl_default,
594 ext3_xattr_unregister(EXT3_XATTR_INDEX_POSIX_ACL_ACCESS,
595 &ext3_xattr_acl_access_handler);
596 ext3_xattr_unregister(EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT,
597 &ext3_xattr_acl_default_handler);
605 error = ext3_xattr_register(EXT3_XATTR_INDEX_POSIX_ACL_ACCESS,
606 &ext3_xattr_acl_access_handler);
609 error = ext3_xattr_register(EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT,
610 &ext3_xattr_acl_default_handler);