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>
16 * Convert from filesystem to in-memory representation.
18 static struct posix_acl *
19 ext2_acl_from_disk(const void *value, size_t size)
21 const char *end = (char *)value + size;
23 struct posix_acl *acl;
27 if (size < sizeof(ext2_acl_header))
28 return ERR_PTR(-EINVAL);
29 if (((ext2_acl_header *)value)->a_version !=
30 cpu_to_le32(EXT2_ACL_VERSION))
31 return ERR_PTR(-EINVAL);
32 value = (char *)value + sizeof(ext2_acl_header);
33 count = ext2_acl_count(size);
35 return ERR_PTR(-EINVAL);
38 acl = posix_acl_alloc(count, GFP_KERNEL);
40 return ERR_PTR(-ENOMEM);
41 for (n=0; n < count; n++) {
42 ext2_acl_entry *entry =
43 (ext2_acl_entry *)value;
44 if ((char *)value + sizeof(ext2_acl_entry_short) > end)
46 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
47 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
48 switch(acl->a_entries[n].e_tag) {
53 value = (char *)value +
54 sizeof(ext2_acl_entry_short);
55 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
60 value = (char *)value + sizeof(ext2_acl_entry);
61 if ((char *)value > end)
63 acl->a_entries[n].e_id =
64 le32_to_cpu(entry->e_id);
76 posix_acl_release(acl);
77 return ERR_PTR(-EINVAL);
81 * Convert from in-memory to filesystem representation.
84 ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
86 ext2_acl_header *ext_acl;
90 *size = ext2_acl_size(acl->a_count);
91 ext_acl = (ext2_acl_header *)kmalloc(sizeof(ext2_acl_header) +
92 acl->a_count * sizeof(ext2_acl_entry), GFP_KERNEL);
94 return ERR_PTR(-ENOMEM);
95 ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
96 e = (char *)ext_acl + sizeof(ext2_acl_header);
97 for (n=0; n < acl->a_count; n++) {
98 ext2_acl_entry *entry = (ext2_acl_entry *)e;
99 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
100 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
101 switch(acl->a_entries[n].e_tag) {
105 cpu_to_le32(acl->a_entries[n].e_id);
106 e += sizeof(ext2_acl_entry);
113 e += sizeof(ext2_acl_entry_short);
120 return (char *)ext_acl;
124 return ERR_PTR(-EINVAL);
127 static inline struct posix_acl *
128 ext2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
130 struct posix_acl *acl = EXT2_ACL_NOT_CACHED;
132 spin_lock(&inode->i_lock);
133 if (*i_acl != EXT2_ACL_NOT_CACHED)
134 acl = posix_acl_dup(*i_acl);
135 spin_unlock(&inode->i_lock);
141 ext2_iset_acl(struct inode *inode, struct posix_acl **i_acl,
142 struct posix_acl *acl)
144 spin_lock(&inode->i_lock);
145 if (*i_acl != EXT2_ACL_NOT_CACHED)
146 posix_acl_release(*i_acl);
147 *i_acl = posix_acl_dup(acl);
148 spin_unlock(&inode->i_lock);
152 * inode->i_sem: don't care
154 static struct posix_acl *
155 ext2_get_acl(struct inode *inode, int type)
157 struct ext2_inode_info *ei = EXT2_I(inode);
160 struct posix_acl *acl;
163 if (!test_opt(inode->i_sb, POSIX_ACL))
167 case ACL_TYPE_ACCESS:
168 acl = ext2_iget_acl(inode, &ei->i_acl);
169 if (acl != EXT2_ACL_NOT_CACHED)
171 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
174 case ACL_TYPE_DEFAULT:
175 acl = ext2_iget_acl(inode, &ei->i_default_acl);
176 if (acl != EXT2_ACL_NOT_CACHED)
178 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
182 return ERR_PTR(-EINVAL);
184 retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
186 value = kmalloc(retval, GFP_KERNEL);
188 return ERR_PTR(-ENOMEM);
189 retval = ext2_xattr_get(inode, name_index, "", value, retval);
192 acl = ext2_acl_from_disk(value, retval);
193 else if (retval == -ENODATA || retval == -ENOSYS)
196 acl = ERR_PTR(retval);
202 case ACL_TYPE_ACCESS:
203 ext2_iset_acl(inode, &ei->i_acl, acl);
206 case ACL_TYPE_DEFAULT:
207 ext2_iset_acl(inode, &ei->i_default_acl, acl);
218 ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
220 struct ext2_inode_info *ei = EXT2_I(inode);
226 if (S_ISLNK(inode->i_mode))
228 if (!test_opt(inode->i_sb, POSIX_ACL))
232 case ACL_TYPE_ACCESS:
233 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
235 mode_t mode = inode->i_mode;
236 error = posix_acl_equiv_mode(acl, &mode);
240 inode->i_mode = mode;
241 mark_inode_dirty(inode);
248 case ACL_TYPE_DEFAULT:
249 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
250 if (!S_ISDIR(inode->i_mode))
251 return acl ? -EACCES : 0;
258 if (acl->a_count > EXT2_ACL_MAX_ENTRIES)
260 value = ext2_acl_to_disk(acl, &size);
262 return (int)PTR_ERR(value);
265 error = ext2_xattr_set(inode, name_index, "", value, size, 0);
271 case ACL_TYPE_ACCESS:
272 ext2_iset_acl(inode, &ei->i_acl, acl);
275 case ACL_TYPE_DEFAULT:
276 ext2_iset_acl(inode, &ei->i_default_acl, acl);
284 * Inode operation permission().
286 * inode->i_sem: don't care
289 ext2_permission(struct inode *inode, int mask, struct nameidata *nd)
291 int mode = inode->i_mode;
293 /* Nobody gets write access to a read-only fs */
294 if ((mask & MAY_WRITE) && IS_RDONLY(inode) &&
295 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
297 /* Nobody gets write access to an immutable file */
298 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
300 if (current->fsuid == inode->i_uid) {
302 } else if (test_opt(inode->i_sb, POSIX_ACL)) {
303 struct posix_acl *acl;
305 /* The access ACL cannot grant access if the group class
306 permission bits don't contain all requested permissions. */
307 if (((mode >> 3) & mask & S_IRWXO) != mask)
309 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
311 int error = posix_acl_permission(inode, acl, mask);
312 posix_acl_release(acl);
313 if (error == -EACCES)
314 goto check_capabilities;
320 if (in_group_p(inode->i_gid))
323 if ((mode & mask & S_IRWXO) == mask)
327 /* Allowed to override Discretionary Access Control? */
328 if (!(mask & MAY_EXEC) ||
329 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
330 if (capable(CAP_DAC_OVERRIDE))
332 /* Read and search granted if capable(CAP_DAC_READ_SEARCH) */
333 if (capable(CAP_DAC_READ_SEARCH) && ((mask == MAY_READ) ||
334 (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))))
340 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
343 * inode->i_sem: up (access to inode is still exclusive)
346 ext2_init_acl(struct inode *inode, struct inode *dir)
348 struct posix_acl *acl = NULL;
351 if (!S_ISLNK(inode->i_mode)) {
352 if (test_opt(dir->i_sb, POSIX_ACL)) {
353 acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
358 inode->i_mode &= ~current->fs->umask;
360 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
361 struct posix_acl *clone;
364 if (S_ISDIR(inode->i_mode)) {
365 error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
369 clone = posix_acl_clone(acl, GFP_KERNEL);
373 mode = inode->i_mode;
374 error = posix_acl_create_masq(clone, &mode);
376 inode->i_mode = mode;
378 /* This is an extended ACL */
379 error = ext2_set_acl(inode,
380 ACL_TYPE_ACCESS, clone);
383 posix_acl_release(clone);
386 posix_acl_release(acl);
391 * Does chmod for an inode that may have an Access Control List. The
392 * inode->i_mode field must be updated to the desired value by the caller
393 * before calling this function.
394 * Returns 0 on success, or a negative error number.
396 * We change the ACL rather than storing some ACL entries in the file
397 * mode permission bits (which would be more efficient), because that
398 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
399 * for directories) are added. There are no more bits available in the
405 ext2_acl_chmod(struct inode *inode)
407 struct posix_acl *acl, *clone;
410 if (!test_opt(inode->i_sb, POSIX_ACL))
412 if (S_ISLNK(inode->i_mode))
414 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
415 if (IS_ERR(acl) || !acl)
417 clone = posix_acl_clone(acl, GFP_KERNEL);
418 posix_acl_release(acl);
421 error = posix_acl_chmod_masq(clone, inode->i_mode);
423 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
424 posix_acl_release(clone);
429 * Extended attribut handlers
432 ext2_xattr_list_acl_access(char *list, struct inode *inode,
433 const char *name, int name_len)
435 const size_t size = sizeof(XATTR_NAME_ACL_ACCESS);
437 if (!test_opt(inode->i_sb, POSIX_ACL))
440 memcpy(list, XATTR_NAME_ACL_ACCESS, size);
445 ext2_xattr_list_acl_default(char *list, struct inode *inode,
446 const char *name, int name_len)
448 const size_t size = sizeof(XATTR_NAME_ACL_DEFAULT);
450 if (!test_opt(inode->i_sb, POSIX_ACL))
453 memcpy(list, XATTR_NAME_ACL_DEFAULT, size);
458 ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
460 struct posix_acl *acl;
463 if (!test_opt(inode->i_sb, POSIX_ACL))
466 acl = ext2_get_acl(inode, type);
471 error = posix_acl_to_xattr(acl, buffer, size);
472 posix_acl_release(acl);
478 ext2_xattr_get_acl_access(struct inode *inode, const char *name,
479 void *buffer, size_t size)
481 if (strcmp(name, "") != 0)
483 return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
487 ext2_xattr_get_acl_default(struct inode *inode, const char *name,
488 void *buffer, size_t size)
490 if (strcmp(name, "") != 0)
492 return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
496 ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
499 struct posix_acl *acl;
502 if (!test_opt(inode->i_sb, POSIX_ACL))
504 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
508 acl = posix_acl_from_xattr(value, size);
512 error = posix_acl_valid(acl);
514 goto release_and_out;
519 error = ext2_set_acl(inode, type, acl);
522 posix_acl_release(acl);
527 ext2_xattr_set_acl_access(struct inode *inode, const char *name,
528 const void *value, size_t size, int flags)
530 if (strcmp(name, "") != 0)
532 return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
536 ext2_xattr_set_acl_default(struct inode *inode, const char *name,
537 const void *value, size_t size, int flags)
539 if (strcmp(name, "") != 0)
541 return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
544 struct ext2_xattr_handler ext2_xattr_acl_access_handler = {
545 .prefix = XATTR_NAME_ACL_ACCESS,
546 .list = ext2_xattr_list_acl_access,
547 .get = ext2_xattr_get_acl_access,
548 .set = ext2_xattr_set_acl_access,
551 struct ext2_xattr_handler ext2_xattr_acl_default_handler = {
552 .prefix = XATTR_NAME_ACL_DEFAULT,
553 .list = ext2_xattr_list_acl_default,
554 .get = ext2_xattr_get_acl_default,
555 .set = ext2_xattr_set_acl_default,
561 ext2_xattr_unregister(EXT2_XATTR_INDEX_POSIX_ACL_ACCESS,
562 &ext2_xattr_acl_access_handler);
563 ext2_xattr_unregister(EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT,
564 &ext2_xattr_acl_default_handler);
572 error = ext2_xattr_register(EXT2_XATTR_INDEX_POSIX_ACL_ACCESS,
573 &ext2_xattr_acl_access_handler);
576 error = ext2_xattr_register(EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT,
577 &ext2_xattr_acl_default_handler);