4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
7 #include <linux/capability.h>
8 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
17 * Convert from filesystem to in-memory representation.
19 static struct posix_acl *
20 ext2_acl_from_disk(const void *value, size_t size)
22 const char *end = (char *)value + size;
24 struct posix_acl *acl;
28 if (size < sizeof(ext2_acl_header))
29 return ERR_PTR(-EINVAL);
30 if (((ext2_acl_header *)value)->a_version !=
31 cpu_to_le32(EXT2_ACL_VERSION))
32 return ERR_PTR(-EINVAL);
33 value = (char *)value + sizeof(ext2_acl_header);
34 count = ext2_acl_count(size);
36 return ERR_PTR(-EINVAL);
39 acl = posix_acl_alloc(count, GFP_KERNEL);
41 return ERR_PTR(-ENOMEM);
42 for (n=0; n < count; n++) {
43 ext2_acl_entry *entry =
44 (ext2_acl_entry *)value;
45 if ((char *)value + sizeof(ext2_acl_entry_short) > end)
47 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
48 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
49 switch(acl->a_entries[n].e_tag) {
54 value = (char *)value +
55 sizeof(ext2_acl_entry_short);
56 acl->a_entries[n].e_id = ACL_UNDEFINED_ID;
61 value = (char *)value + sizeof(ext2_acl_entry);
62 if ((char *)value > end)
64 acl->a_entries[n].e_id =
65 le32_to_cpu(entry->e_id);
77 posix_acl_release(acl);
78 return ERR_PTR(-EINVAL);
82 * Convert from in-memory to filesystem representation.
85 ext2_acl_to_disk(const struct posix_acl *acl, size_t *size)
87 ext2_acl_header *ext_acl;
91 *size = ext2_acl_size(acl->a_count);
92 ext_acl = kmalloc(sizeof(ext2_acl_header) + acl->a_count *
93 sizeof(ext2_acl_entry), GFP_KERNEL);
95 return ERR_PTR(-ENOMEM);
96 ext_acl->a_version = cpu_to_le32(EXT2_ACL_VERSION);
97 e = (char *)ext_acl + sizeof(ext2_acl_header);
98 for (n=0; n < acl->a_count; n++) {
99 ext2_acl_entry *entry = (ext2_acl_entry *)e;
100 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
101 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
102 switch(acl->a_entries[n].e_tag) {
106 cpu_to_le32(acl->a_entries[n].e_id);
107 e += sizeof(ext2_acl_entry);
114 e += sizeof(ext2_acl_entry_short);
121 return (char *)ext_acl;
125 return ERR_PTR(-EINVAL);
128 static inline struct posix_acl *
129 ext2_iget_acl(struct inode *inode, struct posix_acl **i_acl)
131 struct posix_acl *acl = ACL_NOT_CACHED;
133 spin_lock(&inode->i_lock);
134 if (*i_acl != ACL_NOT_CACHED)
135 acl = posix_acl_dup(*i_acl);
136 spin_unlock(&inode->i_lock);
142 ext2_iset_acl(struct inode *inode, struct posix_acl **i_acl,
143 struct posix_acl *acl)
145 spin_lock(&inode->i_lock);
146 if (*i_acl != ACL_NOT_CACHED)
147 posix_acl_release(*i_acl);
148 *i_acl = posix_acl_dup(acl);
149 spin_unlock(&inode->i_lock);
153 * inode->i_mutex: don't care
155 static struct posix_acl *
156 ext2_get_acl(struct inode *inode, int type)
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, &inode->i_acl);
169 if (acl != ACL_NOT_CACHED)
171 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
174 case ACL_TYPE_DEFAULT:
175 acl = ext2_iget_acl(inode, &inode->i_default_acl);
176 if (acl != 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);
201 case ACL_TYPE_ACCESS:
202 ext2_iset_acl(inode, &inode->i_acl, acl);
205 case ACL_TYPE_DEFAULT:
206 ext2_iset_acl(inode, &inode->i_default_acl, acl);
214 * inode->i_mutex: down
217 ext2_set_acl(struct inode *inode, int type, struct posix_acl *acl)
224 if (S_ISLNK(inode->i_mode))
226 if (!test_opt(inode->i_sb, POSIX_ACL))
230 case ACL_TYPE_ACCESS:
231 name_index = EXT2_XATTR_INDEX_POSIX_ACL_ACCESS;
233 mode_t mode = inode->i_mode;
234 error = posix_acl_equiv_mode(acl, &mode);
238 inode->i_mode = mode;
239 mark_inode_dirty(inode);
246 case ACL_TYPE_DEFAULT:
247 name_index = EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT;
248 if (!S_ISDIR(inode->i_mode))
249 return acl ? -EACCES : 0;
256 value = ext2_acl_to_disk(acl, &size);
258 return (int)PTR_ERR(value);
261 error = ext2_xattr_set(inode, name_index, "", value, size, 0);
266 case ACL_TYPE_ACCESS:
267 ext2_iset_acl(inode, &inode->i_acl, acl);
270 case ACL_TYPE_DEFAULT:
271 ext2_iset_acl(inode, &inode->i_default_acl, acl);
279 ext2_check_acl(struct inode *inode, int mask)
281 struct posix_acl *acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
286 int error = posix_acl_permission(inode, acl, mask);
287 posix_acl_release(acl);
295 ext2_permission(struct inode *inode, int mask)
297 return generic_permission(inode, mask, ext2_check_acl);
301 * Initialize the ACLs of a new inode. Called from ext2_new_inode.
304 * inode->i_mutex: up (access to inode is still exclusive)
307 ext2_init_acl(struct inode *inode, struct inode *dir)
309 struct posix_acl *acl = NULL;
312 if (!S_ISLNK(inode->i_mode)) {
313 if (test_opt(dir->i_sb, POSIX_ACL)) {
314 acl = ext2_get_acl(dir, ACL_TYPE_DEFAULT);
319 inode->i_mode &= ~current_umask();
321 if (test_opt(inode->i_sb, POSIX_ACL) && acl) {
322 struct posix_acl *clone;
325 if (S_ISDIR(inode->i_mode)) {
326 error = ext2_set_acl(inode, ACL_TYPE_DEFAULT, acl);
330 clone = posix_acl_clone(acl, GFP_KERNEL);
334 mode = inode->i_mode;
335 error = posix_acl_create_masq(clone, &mode);
337 inode->i_mode = mode;
339 /* This is an extended ACL */
340 error = ext2_set_acl(inode,
341 ACL_TYPE_ACCESS, clone);
344 posix_acl_release(clone);
347 posix_acl_release(acl);
352 * Does chmod for an inode that may have an Access Control List. The
353 * inode->i_mode field must be updated to the desired value by the caller
354 * before calling this function.
355 * Returns 0 on success, or a negative error number.
357 * We change the ACL rather than storing some ACL entries in the file
358 * mode permission bits (which would be more efficient), because that
359 * would break once additional permissions (like ACL_APPEND, ACL_DELETE
360 * for directories) are added. There are no more bits available in the
363 * inode->i_mutex: down
366 ext2_acl_chmod(struct inode *inode)
368 struct posix_acl *acl, *clone;
371 if (!test_opt(inode->i_sb, POSIX_ACL))
373 if (S_ISLNK(inode->i_mode))
375 acl = ext2_get_acl(inode, ACL_TYPE_ACCESS);
376 if (IS_ERR(acl) || !acl)
378 clone = posix_acl_clone(acl, GFP_KERNEL);
379 posix_acl_release(acl);
382 error = posix_acl_chmod_masq(clone, inode->i_mode);
384 error = ext2_set_acl(inode, ACL_TYPE_ACCESS, clone);
385 posix_acl_release(clone);
390 * Extended attribut handlers
393 ext2_xattr_list_acl_access(struct inode *inode, char *list, size_t list_size,
394 const char *name, size_t name_len)
396 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
398 if (!test_opt(inode->i_sb, POSIX_ACL))
400 if (list && size <= list_size)
401 memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
406 ext2_xattr_list_acl_default(struct inode *inode, char *list, size_t list_size,
407 const char *name, size_t name_len)
409 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
411 if (!test_opt(inode->i_sb, POSIX_ACL))
413 if (list && size <= list_size)
414 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
419 ext2_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size)
421 struct posix_acl *acl;
424 if (!test_opt(inode->i_sb, POSIX_ACL))
427 acl = ext2_get_acl(inode, type);
432 error = posix_acl_to_xattr(acl, buffer, size);
433 posix_acl_release(acl);
439 ext2_xattr_get_acl_access(struct inode *inode, const char *name,
440 void *buffer, size_t size)
442 if (strcmp(name, "") != 0)
444 return ext2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
448 ext2_xattr_get_acl_default(struct inode *inode, const char *name,
449 void *buffer, size_t size)
451 if (strcmp(name, "") != 0)
453 return ext2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
457 ext2_xattr_set_acl(struct inode *inode, int type, const void *value,
460 struct posix_acl *acl;
463 if (!test_opt(inode->i_sb, POSIX_ACL))
465 if (!is_owner_or_cap(inode))
469 acl = posix_acl_from_xattr(value, size);
473 error = posix_acl_valid(acl);
475 goto release_and_out;
480 error = ext2_set_acl(inode, type, acl);
483 posix_acl_release(acl);
488 ext2_xattr_set_acl_access(struct inode *inode, const char *name,
489 const void *value, size_t size, int flags)
491 if (strcmp(name, "") != 0)
493 return ext2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
497 ext2_xattr_set_acl_default(struct inode *inode, const char *name,
498 const void *value, size_t size, int flags)
500 if (strcmp(name, "") != 0)
502 return ext2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
505 struct xattr_handler ext2_xattr_acl_access_handler = {
506 .prefix = POSIX_ACL_XATTR_ACCESS,
507 .list = ext2_xattr_list_acl_access,
508 .get = ext2_xattr_get_acl_access,
509 .set = ext2_xattr_set_acl_access,
512 struct xattr_handler ext2_xattr_acl_default_handler = {
513 .prefix = POSIX_ACL_XATTR_DEFAULT,
514 .list = ext2_xattr_list_acl_default,
515 .get = ext2_xattr_get_acl_default,
516 .set = ext2_xattr_set_acl_default,