2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* linux/fs/ext3/acl.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
|
|
|
|
*/
|
|
|
|
|
2012-03-30 02:30:07 +00:00
|
|
|
#include "ext3.h"
|
2005-04-16 22:20:36 +00:00
|
|
|
#include "xattr.h"
|
|
|
|
#include "acl.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert from filesystem to in-memory representation.
|
|
|
|
*/
|
|
|
|
static struct posix_acl *
|
|
|
|
ext3_acl_from_disk(const void *value, size_t size)
|
|
|
|
{
|
|
|
|
const char *end = (char *)value + size;
|
|
|
|
int n, count;
|
|
|
|
struct posix_acl *acl;
|
|
|
|
|
|
|
|
if (!value)
|
|
|
|
return NULL;
|
|
|
|
if (size < sizeof(ext3_acl_header))
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
if (((ext3_acl_header *)value)->a_version !=
|
|
|
|
cpu_to_le32(EXT3_ACL_VERSION))
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
value = (char *)value + sizeof(ext3_acl_header);
|
|
|
|
count = ext3_acl_count(size);
|
|
|
|
if (count < 0)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
if (count == 0)
|
|
|
|
return NULL;
|
2008-03-20 00:00:49 +00:00
|
|
|
acl = posix_acl_alloc(count, GFP_NOFS);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!acl)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
for (n=0; n < count; n++) {
|
|
|
|
ext3_acl_entry *entry =
|
|
|
|
(ext3_acl_entry *)value;
|
|
|
|
if ((char *)value + sizeof(ext3_acl_entry_short) > end)
|
|
|
|
goto fail;
|
|
|
|
acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
|
|
|
|
acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
|
|
|
|
switch(acl->a_entries[n].e_tag) {
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
case ACL_GROUP_OBJ:
|
|
|
|
case ACL_MASK:
|
|
|
|
case ACL_OTHER:
|
|
|
|
value = (char *)value +
|
|
|
|
sizeof(ext3_acl_entry_short);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_USER:
|
2012-09-11 03:44:54 +00:00
|
|
|
value = (char *)value + sizeof(ext3_acl_entry);
|
|
|
|
if ((char *)value > end)
|
|
|
|
goto fail;
|
|
|
|
acl->a_entries[n].e_uid =
|
|
|
|
make_kuid(&init_user_ns,
|
|
|
|
le32_to_cpu(entry->e_id));
|
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
case ACL_GROUP:
|
|
|
|
value = (char *)value + sizeof(ext3_acl_entry);
|
|
|
|
if ((char *)value > end)
|
|
|
|
goto fail;
|
2012-09-11 03:44:54 +00:00
|
|
|
acl->a_entries[n].e_gid =
|
|
|
|
make_kgid(&init_user_ns,
|
|
|
|
le32_to_cpu(entry->e_id));
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value != end)
|
|
|
|
goto fail;
|
|
|
|
return acl;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
posix_acl_release(acl);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert from in-memory to filesystem representation.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
ext3_acl_to_disk(const struct posix_acl *acl, size_t *size)
|
|
|
|
{
|
|
|
|
ext3_acl_header *ext_acl;
|
|
|
|
char *e;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
*size = ext3_acl_size(acl->a_count);
|
2006-09-27 08:49:39 +00:00
|
|
|
ext_acl = kmalloc(sizeof(ext3_acl_header) + acl->a_count *
|
2008-03-20 00:00:49 +00:00
|
|
|
sizeof(ext3_acl_entry), GFP_NOFS);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!ext_acl)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
ext_acl->a_version = cpu_to_le32(EXT3_ACL_VERSION);
|
|
|
|
e = (char *)ext_acl + sizeof(ext3_acl_header);
|
|
|
|
for (n=0; n < acl->a_count; n++) {
|
2012-09-11 03:44:54 +00:00
|
|
|
const struct posix_acl_entry *acl_e = &acl->a_entries[n];
|
2005-04-16 22:20:36 +00:00
|
|
|
ext3_acl_entry *entry = (ext3_acl_entry *)e;
|
2012-09-11 03:44:54 +00:00
|
|
|
entry->e_tag = cpu_to_le16(acl_e->e_tag);
|
|
|
|
entry->e_perm = cpu_to_le16(acl_e->e_perm);
|
|
|
|
switch(acl_e->e_tag) {
|
2005-04-16 22:20:36 +00:00
|
|
|
case ACL_USER:
|
2012-09-11 03:44:54 +00:00
|
|
|
entry->e_id = cpu_to_le32(
|
|
|
|
from_kuid(&init_user_ns, acl_e->e_uid));
|
|
|
|
e += sizeof(ext3_acl_entry);
|
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
case ACL_GROUP:
|
2012-09-11 03:44:54 +00:00
|
|
|
entry->e_id = cpu_to_le32(
|
|
|
|
from_kgid(&init_user_ns, acl_e->e_gid));
|
2005-04-16 22:20:36 +00:00
|
|
|
e += sizeof(ext3_acl_entry);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
case ACL_GROUP_OBJ:
|
|
|
|
case ACL_MASK:
|
|
|
|
case ACL_OTHER:
|
|
|
|
e += sizeof(ext3_acl_entry_short);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (char *)ext_acl;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
kfree(ext_acl);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inode operation get_posix_acl().
|
|
|
|
*
|
2006-01-09 23:59:24 +00:00
|
|
|
* inode->i_mutex: don't care
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2011-07-23 15:37:31 +00:00
|
|
|
struct posix_acl *
|
2005-04-16 22:20:36 +00:00
|
|
|
ext3_get_acl(struct inode *inode, int type)
|
|
|
|
{
|
|
|
|
int name_index;
|
|
|
|
char *value = NULL;
|
|
|
|
struct posix_acl *acl;
|
|
|
|
int retval;
|
|
|
|
|
2009-06-09 16:11:54 +00:00
|
|
|
switch (type) {
|
|
|
|
case ACL_TYPE_ACCESS:
|
|
|
|
name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
|
|
|
|
break;
|
|
|
|
case ACL_TYPE_DEFAULT:
|
|
|
|
name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
BUG();
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2009-06-09 16:11:54 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
retval = ext3_xattr_get(inode, name_index, "", NULL, 0);
|
|
|
|
if (retval > 0) {
|
2008-03-20 00:00:49 +00:00
|
|
|
value = kmalloc(retval, GFP_NOFS);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!value)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
retval = ext3_xattr_get(inode, name_index, "", value, retval);
|
|
|
|
}
|
|
|
|
if (retval > 0)
|
|
|
|
acl = ext3_acl_from_disk(value, retval);
|
|
|
|
else if (retval == -ENODATA || retval == -ENOSYS)
|
|
|
|
acl = NULL;
|
|
|
|
else
|
|
|
|
acl = ERR_PTR(retval);
|
|
|
|
kfree(value);
|
|
|
|
|
2009-06-09 16:11:54 +00:00
|
|
|
if (!IS_ERR(acl))
|
|
|
|
set_cached_acl(inode, type, acl);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
return acl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the access or default ACL of an inode.
|
|
|
|
*
|
2006-01-09 23:59:24 +00:00
|
|
|
* inode->i_mutex: down unless called from ext3_new_inode
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
static int
|
2013-12-20 13:16:44 +00:00
|
|
|
__ext3_set_acl(handle_t *handle, struct inode *inode, int type,
|
2005-04-16 22:20:36 +00:00
|
|
|
struct posix_acl *acl)
|
|
|
|
{
|
|
|
|
int name_index;
|
|
|
|
void *value = NULL;
|
2006-02-03 11:04:13 +00:00
|
|
|
size_t size = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case ACL_TYPE_ACCESS:
|
|
|
|
name_index = EXT3_XATTR_INDEX_POSIX_ACL_ACCESS;
|
|
|
|
if (acl) {
|
2011-07-23 22:56:36 +00:00
|
|
|
error = posix_acl_equiv_mode(acl, &inode->i_mode);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (error < 0)
|
|
|
|
return error;
|
|
|
|
else {
|
2010-05-27 08:28:40 +00:00
|
|
|
inode->i_ctime = CURRENT_TIME_SEC;
|
2005-04-16 22:20:36 +00:00
|
|
|
ext3_mark_inode_dirty(handle, inode);
|
|
|
|
if (error == 0)
|
|
|
|
acl = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_TYPE_DEFAULT:
|
|
|
|
name_index = EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT;
|
|
|
|
if (!S_ISDIR(inode->i_mode))
|
|
|
|
return acl ? -EACCES : 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2006-09-27 08:49:35 +00:00
|
|
|
if (acl) {
|
2005-04-16 22:20:36 +00:00
|
|
|
value = ext3_acl_to_disk(acl, &size);
|
|
|
|
if (IS_ERR(value))
|
|
|
|
return (int)PTR_ERR(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = ext3_xattr_set_handle(handle, inode, name_index, "",
|
|
|
|
value, size, 0);
|
|
|
|
|
|
|
|
kfree(value);
|
|
|
|
|
2009-06-09 16:11:54 +00:00
|
|
|
if (!error)
|
|
|
|
set_cached_acl(inode, type, acl);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-12-20 13:16:44 +00:00
|
|
|
ext3_set_acl(struct inode *inode, struct posix_acl *acl, int type)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2011-07-23 04:18:02 +00:00
|
|
|
handle_t *handle;
|
2013-12-20 13:16:44 +00:00
|
|
|
int error, retries = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-07-23 04:18:02 +00:00
|
|
|
retry:
|
2013-12-20 13:16:44 +00:00
|
|
|
handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
|
|
|
|
if (IS_ERR(handle))
|
|
|
|
return PTR_ERR(handle);
|
|
|
|
error = __ext3_set_acl(handle, inode, type, acl);
|
2011-07-23 04:18:02 +00:00
|
|
|
ext3_journal_stop(handle);
|
2013-12-20 13:16:44 +00:00
|
|
|
if (error == -ENOSPC && ext3_should_retry_alloc(inode->i_sb, &retries))
|
2011-07-23 04:18:02 +00:00
|
|
|
goto retry;
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-12-20 13:16:44 +00:00
|
|
|
* Initialize the ACLs of a new inode. Called from ext3_new_inode.
|
|
|
|
*
|
|
|
|
* dir->i_mutex: down
|
|
|
|
* inode->i_mutex: up (access to inode is still exclusive)
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2013-12-20 13:16:44 +00:00
|
|
|
int
|
|
|
|
ext3_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-12-20 13:16:44 +00:00
|
|
|
struct posix_acl *default_acl, *acl;
|
2005-04-16 22:20:36 +00:00
|
|
|
int error;
|
|
|
|
|
2013-12-20 13:16:44 +00:00
|
|
|
error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-12-20 13:16:44 +00:00
|
|
|
if (default_acl) {
|
|
|
|
error = __ext3_set_acl(handle, inode, ACL_TYPE_DEFAULT,
|
|
|
|
default_acl);
|
|
|
|
posix_acl_release(default_acl);
|
|
|
|
}
|
|
|
|
if (acl) {
|
|
|
|
if (!error)
|
|
|
|
error = __ext3_set_acl(handle, inode, ACL_TYPE_ACCESS,
|
|
|
|
acl);
|
|
|
|
posix_acl_release(acl);
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
return error;
|
|
|
|
}
|