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/vs_base.h>
18 * Convert from filesystem to in-memory representation.
20 static struct posix_acl *
21 ext2_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(ext2_acl_header))
30 return ERR_PTR(-EINVAL);
31 if (((ext2_acl_header *)value)->a_version !=
32 cpu_to_le32(EXT2_ACL_VERSION))
33 return ERR_PTR(-EINVAL);
34 value = (char *)value + sizeof(ext2_acl_header);
35 count = ext2_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 ext2_acl_entry *entry =
45 (ext2_acl_entry *)value;
46 if ((char *)value + sizeof(ext2_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(ext2_acl_entry_short);
57 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
62 value = (char *)value + sizeof(ext2_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 ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
88 ext2_acl_header *ext_acl;
92 *size = ext2_acl_size(acl->a_count);
93 ext_acl = (ext2_acl_header *)kmalloc(sizeof(ext2_acl_header) +
94 acl->a_count * sizeof(ext2_acl_entry), GFP_KERNEL);
96 return ERR_PTR(-ENOMEM);
97 ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
98 e = (char *)ext_acl + sizeof(ext2_acl_header);
99 for (n=0; n < acl->a_count; n++) {
100 ext2_acl_entry *entry = (ext2_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(ext2_acl_entry);
115 e += sizeof(ext2_acl_entry_short);
122 return (char *)ext_acl;
126 return ERR_PTR(-EINVAL);
129 static inline struct posix_acl *
130 ext2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
132 struct posix_acl *acl = EXT2_ACL_NOT_CACHED;
134 spin_lock(&inode->i_lock);
135 if (*i_acl != EXT2_ACL_NOT_CACHED)
136 acl = posix_acl_dup(*i_acl);
137 spin_unlock(&inode->i_lock);
143 ext2_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 != EXT2_ACL_NOT_CACHED)
148 posix_acl_release(*i_acl);
149 *i_acl = posix_acl_dup(acl);
150 spin_unlock(&inode->i_lock);
154 * inode->i_sem: don't care
156 static struct posix_acl *
157 ext2_get_acl(struct inode *inode, int type)
159 struct ext2_inode_info *ei = EXT2_I(inode);
162 struct posix_acl *acl;
165 if (!test_opt(inode->i_sb, POSIX_ACL))
169 case ACL_TYPE_ACCESS:
170 acl = ext2_iget_acl(inode, &ei->i_acl);
171 if (acl != EXT2_ACL_NOT_CACHED)
173 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
176 case ACL_TYPE_DEFAULT:
177 acl = ext2_iget_acl(inode, &ei->i_default_acl);
178 if (acl != EXT2_ACL_NOT_CACHED)
180 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
184 return ERR_PTR(-EINVAL);
186 retval = ext2_xattr_get(inode, name_index, "", NULL, 0);
188 value = kmalloc(retval, GFP_KERNEL);
190 return ERR_PTR(-ENOMEM);
191 retval = ext2_xattr_get(inode, name_index, "", value, retval);
194 acl = ext2_acl_from_disk(value, retval);
195 else if (retval == -ENODATA || retval == -ENOSYS)
198 acl = ERR_PTR(retval);
204 case ACL_TYPE_ACCESS:
205 ext2_iset_acl(inode, &ei->i_acl, acl);
208 case ACL_TYPE_DEFAULT:
209 ext2_iset_acl(inode, &ei->i_default_acl, acl);
220 ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
222 struct ext2_inode_info *ei = EXT2_I(inode);
228 if (S_ISLNK(inode->i_mode))
230 if (!test_opt(inode->i_sb, POSIX_ACL))
234 case ACL_TYPE_ACCESS:
235 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
237 mode_t mode = inode->i_mode;
238 error = posix_acl_equiv_mode(acl, &mode);
242 inode->i_mode = mode;
243 mark_inode_dirty(inode);
250 case ACL_TYPE_DEFAULT:
251 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
252 if (!S_ISDIR(inode->i_mode))
253 return acl ? -EACCES : 0;
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 ext2_check_acl(struct inode *inode, int mask)
286 struct posix_acl *acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
291 int error = posix_acl_permission(inode, acl, mask);
292 posix_acl_release(acl);
300 ext2_permission(struct inode *inode, int mask, struct nameidata *nd)
302 return generic_permission(inode, mask, ext2_check_acl);
306 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
309 * inode->i_sem: up (access to inode is still exclusive)
312 ext2_init_acl(struct inode *inode, struct inode *dir)
314 struct posix_acl *acl = NULL;
317 if (!S_ISLNK(inode->i_mode)) {
318 if (test_opt(dir->i_sb, POSIX_ACL)) {
319 acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
324 inode->i_mode &= ~current->fs->umask;
326 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
327 struct posix_acl *clone;
330 if (S_ISDIR(inode->i_mode)) {
331 error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
335 clone = posix_acl_clone(acl, GFP_KERNEL);
339 mode = inode->i_mode;
340 error = posix_acl_create_masq(clone, &mode);
342 inode->i_mode = mode;
344 /* This is an extended ACL */
345 error = ext2_set_acl(inode,
346 ACL_TYPE_ACCESS, clone);
349 posix_acl_release(clone);
352 posix_acl_release(acl);
357 * Does chmod for an inode that may have an Access Control List. The
358 * inode->i_mode field must be updated to the desired value by the caller
359 * before calling this function.
360 * Returns 0 on success, or a negative error number.
362 * We change the ACL rather than storing some ACL entries in the file
363 * mode permission bits (which would be more efficient), because that
364 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
365 * for directories) are added. There are no more bits available in the
371 ext2_acl_chmod(struct inode *inode)
373 struct posix_acl *acl, *clone;
376 if (!test_opt(inode->i_sb, POSIX_ACL))
378 if (S_ISLNK(inode->i_mode))
380 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
381 if (IS_ERR(acl) || !acl)
383 clone = posix_acl_clone(acl, GFP_KERNEL);
384 posix_acl_release(acl);
387 error = posix_acl_chmod_masq(clone, inode->i_mode);
389 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
390 posix_acl_release(clone);
395 * Extended attribut handlers
398 ext2_xattr_list_acl_access(struct inode *inode, char *list, size_t list_size,
399 const char *name, size_t name_len)
401 const size_t size = sizeof(XATTR_NAME_ACL_ACCESS);
403 if (!test_opt(inode->i_sb, POSIX_ACL))
405 if (list && size <= list_size)
406 memcpy(list, XATTR_NAME_ACL_ACCESS, size);
411 ext2_xattr_list_acl_default(struct inode *inode, char *list, size_t list_size,
412 const char *name, size_t name_len)
414 const size_t size = sizeof(XATTR_NAME_ACL_DEFAULT);
416 if (!test_opt(inode->i_sb, POSIX_ACL))
418 if (list && size <= list_size)
419 memcpy(list, XATTR_NAME_ACL_DEFAULT, size);
424 ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
426 struct posix_acl *acl;
429 if (!test_opt(inode->i_sb, POSIX_ACL))
432 acl = ext2_get_acl(inode, type);
437 error = posix_acl_to_xattr(acl, buffer, size);
438 posix_acl_release(acl);
444 ext2_xattr_get_acl_access(struct inode *inode, const char *name,
445 void *buffer, size_t size)
447 if (strcmp(name, "") != 0)
449 return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
453 ext2_xattr_get_acl_default(struct inode *inode, const char *name,
454 void *buffer, size_t size)
456 if (strcmp(name, "") != 0)
458 return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
462 ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
465 struct posix_acl *acl;
468 if (!test_opt(inode->i_sb, POSIX_ACL))
470 if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
474 acl = posix_acl_from_xattr(value, size);
478 error = posix_acl_valid(acl);
480 goto release_and_out;
485 error = ext2_set_acl(inode, type, acl);
488 posix_acl_release(acl);
493 ext2_xattr_set_acl_access(struct inode *inode, const char *name,
494 const void *value, size_t size, int flags)
496 if (strcmp(name, "") != 0)
498 return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
502 ext2_xattr_set_acl_default(struct inode *inode, const char *name,
503 const void *value, size_t size, int flags)
505 if (strcmp(name, "") != 0)
507 return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
510 struct xattr_handler ext2_xattr_acl_access_handler = {
511 .prefix = XATTR_NAME_ACL_ACCESS,
512 .list = ext2_xattr_list_acl_access,
513 .get = ext2_xattr_get_acl_access,
514 .set = ext2_xattr_set_acl_access,
517 struct xattr_handler ext2_xattr_acl_default_handler = {
518 .prefix = XATTR_NAME_ACL_DEFAULT,
519 .list = ext2_xattr_list_acl_default,
520 .get = ext2_xattr_get_acl_default,
521 .set = ext2_xattr_set_acl_default,