2012-11-29 11:28:09 +07:00
|
|
|
/*
|
2012-11-02 15:12:17 +07:00
|
|
|
* fs/f2fs/acl.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Samsung Electronics Co., Ltd.
|
|
|
|
* http://www.samsung.com/
|
|
|
|
*
|
|
|
|
* Portions of this code from linux/fs/ext2/acl.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
#include <linux/f2fs_fs.h>
|
|
|
|
#include "f2fs.h"
|
|
|
|
#include "xattr.h"
|
|
|
|
#include "acl.h"
|
|
|
|
|
|
|
|
static inline size_t f2fs_acl_size(int count)
|
|
|
|
{
|
|
|
|
if (count <= 4) {
|
|
|
|
return sizeof(struct f2fs_acl_header) +
|
|
|
|
count * sizeof(struct f2fs_acl_entry_short);
|
|
|
|
} else {
|
|
|
|
return sizeof(struct f2fs_acl_header) +
|
|
|
|
4 * sizeof(struct f2fs_acl_entry_short) +
|
|
|
|
(count - 4) * sizeof(struct f2fs_acl_entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int f2fs_acl_count(size_t size)
|
|
|
|
{
|
|
|
|
ssize_t s;
|
|
|
|
size -= sizeof(struct f2fs_acl_header);
|
|
|
|
s = size - 4 * sizeof(struct f2fs_acl_entry_short);
|
|
|
|
if (s < 0) {
|
|
|
|
if (size % sizeof(struct f2fs_acl_entry_short))
|
|
|
|
return -1;
|
|
|
|
return size / sizeof(struct f2fs_acl_entry_short);
|
|
|
|
} else {
|
|
|
|
if (s % sizeof(struct f2fs_acl_entry))
|
|
|
|
return -1;
|
|
|
|
return s / sizeof(struct f2fs_acl_entry) + 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size)
|
|
|
|
{
|
|
|
|
int i, count;
|
|
|
|
struct posix_acl *acl;
|
|
|
|
struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
|
|
|
|
struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
|
|
|
|
const char *end = value + size;
|
|
|
|
|
|
|
|
if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION))
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
|
|
|
|
count = f2fs_acl_count(size);
|
|
|
|
if (count < 0)
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
if (count == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
acl = posix_acl_alloc(count, GFP_KERNEL);
|
|
|
|
if (!acl)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
|
|
|
|
if ((char *)entry > end)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
acl->a_entries[i].e_tag = le16_to_cpu(entry->e_tag);
|
|
|
|
acl->a_entries[i].e_perm = le16_to_cpu(entry->e_perm);
|
|
|
|
|
|
|
|
switch (acl->a_entries[i].e_tag) {
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
case ACL_GROUP_OBJ:
|
|
|
|
case ACL_MASK:
|
|
|
|
case ACL_OTHER:
|
|
|
|
entry = (struct f2fs_acl_entry *)((char *)entry +
|
|
|
|
sizeof(struct f2fs_acl_entry_short));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_USER:
|
|
|
|
acl->a_entries[i].e_uid =
|
|
|
|
make_kuid(&init_user_ns,
|
|
|
|
le32_to_cpu(entry->e_id));
|
|
|
|
entry = (struct f2fs_acl_entry *)((char *)entry +
|
|
|
|
sizeof(struct f2fs_acl_entry));
|
|
|
|
break;
|
|
|
|
case ACL_GROUP:
|
|
|
|
acl->a_entries[i].e_gid =
|
|
|
|
make_kgid(&init_user_ns,
|
|
|
|
le32_to_cpu(entry->e_id));
|
|
|
|
entry = (struct f2fs_acl_entry *)((char *)entry +
|
|
|
|
sizeof(struct f2fs_acl_entry));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((char *)entry != end)
|
|
|
|
goto fail;
|
|
|
|
return acl;
|
|
|
|
fail:
|
|
|
|
posix_acl_release(acl);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *f2fs_acl_to_disk(const struct posix_acl *acl, size_t *size)
|
|
|
|
{
|
|
|
|
struct f2fs_acl_header *f2fs_acl;
|
|
|
|
struct f2fs_acl_entry *entry;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
f2fs_acl = kmalloc(sizeof(struct f2fs_acl_header) + acl->a_count *
|
|
|
|
sizeof(struct f2fs_acl_entry), GFP_KERNEL);
|
|
|
|
if (!f2fs_acl)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
|
|
|
|
f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
|
|
|
|
entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
|
|
|
|
|
|
|
|
for (i = 0; i < acl->a_count; i++) {
|
|
|
|
|
|
|
|
entry->e_tag = cpu_to_le16(acl->a_entries[i].e_tag);
|
|
|
|
entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
|
|
|
|
|
|
|
|
switch (acl->a_entries[i].e_tag) {
|
|
|
|
case ACL_USER:
|
|
|
|
entry->e_id = cpu_to_le32(
|
|
|
|
from_kuid(&init_user_ns,
|
|
|
|
acl->a_entries[i].e_uid));
|
|
|
|
entry = (struct f2fs_acl_entry *)((char *)entry +
|
|
|
|
sizeof(struct f2fs_acl_entry));
|
|
|
|
break;
|
|
|
|
case ACL_GROUP:
|
|
|
|
entry->e_id = cpu_to_le32(
|
|
|
|
from_kgid(&init_user_ns,
|
|
|
|
acl->a_entries[i].e_gid));
|
|
|
|
entry = (struct f2fs_acl_entry *)((char *)entry +
|
|
|
|
sizeof(struct f2fs_acl_entry));
|
|
|
|
break;
|
|
|
|
case ACL_USER_OBJ:
|
|
|
|
case ACL_GROUP_OBJ:
|
|
|
|
case ACL_MASK:
|
|
|
|
case ACL_OTHER:
|
|
|
|
entry = (struct f2fs_acl_entry *)((char *)entry +
|
|
|
|
sizeof(struct f2fs_acl_entry_short));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*size = f2fs_acl_size(acl->a_count);
|
|
|
|
return (void *)f2fs_acl;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
kfree(f2fs_acl);
|
|
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct posix_acl *f2fs_get_acl(struct inode *inode, int type)
|
|
|
|
{
|
|
|
|
int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
|
|
|
|
void *value = NULL;
|
|
|
|
struct posix_acl *acl;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (type == ACL_TYPE_ACCESS)
|
|
|
|
name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
|
|
|
|
|
|
|
|
retval = f2fs_getxattr(inode, name_index, "", NULL, 0);
|
|
|
|
if (retval > 0) {
|
2014-03-20 20:21:08 +07:00
|
|
|
value = kmalloc(retval, GFP_F2FS_ZERO);
|
2012-11-02 15:12:17 +07:00
|
|
|
if (!value)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
retval = f2fs_getxattr(inode, name_index, "", value, retval);
|
|
|
|
}
|
|
|
|
|
2013-01-03 07:24:28 +07:00
|
|
|
if (retval > 0)
|
2012-11-02 15:12:17 +07:00
|
|
|
acl = f2fs_acl_from_disk(value, retval);
|
2013-01-03 07:24:28 +07:00
|
|
|
else if (retval == -ENODATA)
|
|
|
|
acl = NULL;
|
|
|
|
else
|
|
|
|
acl = ERR_PTR(retval);
|
2012-11-02 15:12:17 +07:00
|
|
|
kfree(value);
|
2013-01-03 07:24:28 +07:00
|
|
|
|
2012-11-02 15:12:17 +07:00
|
|
|
if (!IS_ERR(acl))
|
|
|
|
set_cached_acl(inode, type, acl);
|
|
|
|
|
|
|
|
return acl;
|
|
|
|
}
|
|
|
|
|
2013-12-20 20:16:45 +07:00
|
|
|
static int __f2fs_set_acl(struct inode *inode, int type,
|
2013-10-28 11:17:54 +07:00
|
|
|
struct posix_acl *acl, struct page *ipage)
|
2012-11-02 15:12:17 +07:00
|
|
|
{
|
|
|
|
struct f2fs_inode_info *fi = F2FS_I(inode);
|
|
|
|
int name_index;
|
|
|
|
void *value = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
int error;
|
|
|
|
|
2014-04-04 08:47:16 +07:00
|
|
|
if (acl) {
|
|
|
|
error = posix_acl_valid(acl);
|
|
|
|
if (error < 0)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2012-11-02 15:12:17 +07:00
|
|
|
switch (type) {
|
|
|
|
case ACL_TYPE_ACCESS:
|
|
|
|
name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
|
|
|
|
if (acl) {
|
|
|
|
error = posix_acl_equiv_mode(acl, &inode->i_mode);
|
|
|
|
if (error < 0)
|
|
|
|
return error;
|
|
|
|
set_acl_inode(fi, inode->i_mode);
|
|
|
|
if (error == 0)
|
|
|
|
acl = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACL_TYPE_DEFAULT:
|
|
|
|
name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
|
|
|
|
if (!S_ISDIR(inode->i_mode))
|
|
|
|
return acl ? -EACCES : 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acl) {
|
|
|
|
value = f2fs_acl_to_disk(acl, &size);
|
|
|
|
if (IS_ERR(value)) {
|
|
|
|
cond_clear_inode_flag(fi, FI_ACL_MODE);
|
|
|
|
return (int)PTR_ERR(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-23 10:23:14 +07:00
|
|
|
error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0);
|
2012-11-02 15:12:17 +07:00
|
|
|
|
|
|
|
kfree(value);
|
|
|
|
if (!error)
|
|
|
|
set_cached_acl(inode, type, acl);
|
|
|
|
|
|
|
|
cond_clear_inode_flag(fi, FI_ACL_MODE);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2013-12-20 20:16:45 +07:00
|
|
|
int f2fs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
|
2012-11-02 15:12:17 +07:00
|
|
|
{
|
2013-12-20 20:16:45 +07:00
|
|
|
return __f2fs_set_acl(inode, type, acl, NULL);
|
2012-11-02 15:12:17 +07:00
|
|
|
}
|
|
|
|
|
2013-12-20 20:16:45 +07:00
|
|
|
int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage)
|
2012-11-02 15:12:17 +07:00
|
|
|
{
|
2013-12-20 20:16:45 +07:00
|
|
|
struct posix_acl *default_acl, *acl;
|
|
|
|
int error = 0;
|
2012-11-02 15:12:17 +07:00
|
|
|
|
2013-12-20 20:16:45 +07:00
|
|
|
error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
|
2012-11-02 15:12:17 +07:00
|
|
|
if (error)
|
|
|
|
return error;
|
2013-10-28 11:12:09 +07:00
|
|
|
|
2013-12-20 20:16:45 +07:00
|
|
|
if (default_acl) {
|
|
|
|
error = __f2fs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl,
|
|
|
|
ipage);
|
|
|
|
posix_acl_release(default_acl);
|
|
|
|
}
|
|
|
|
if (acl) {
|
|
|
|
if (error)
|
|
|
|
error = __f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl,
|
|
|
|
ipage);
|
|
|
|
posix_acl_release(acl);
|
2012-11-02 15:12:17 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|