mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-11 12:28:41 +08:00
f2fs: add xattr and acl functionalities
This implements xattr and acl functionalities. - F2FS uses a node page to contain use extended attributes. Signed-off-by: Changman Lee <cm224.lee@samsung.com> Signed-off-by: Jaegeuk Kim <jaegeuk.kim@samsung.com>
This commit is contained in:
parent
6b4ea0160a
commit
af48b85b8c
465
fs/f2fs/acl.c
Normal file
465
fs/f2fs/acl.c
Normal file
@ -0,0 +1,465 @@
|
||||
/**
|
||||
* 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"
|
||||
|
||||
#define get_inode_mode(i) ((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
|
||||
(F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
|
||||
|
||||
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:
|
||||
acl->a_entries[i].e_id = ACL_UNDEFINED_ID;
|
||||
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)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
|
||||
int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
|
||||
void *value = NULL;
|
||||
struct posix_acl *acl;
|
||||
int retval;
|
||||
|
||||
if (!test_opt(sbi, POSIX_ACL))
|
||||
return NULL;
|
||||
|
||||
acl = get_cached_acl(inode, type);
|
||||
if (acl != ACL_NOT_CACHED)
|
||||
return acl;
|
||||
|
||||
if (type == ACL_TYPE_ACCESS)
|
||||
name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
|
||||
|
||||
retval = f2fs_getxattr(inode, name_index, "", NULL, 0);
|
||||
if (retval > 0) {
|
||||
value = kmalloc(retval, GFP_KERNEL);
|
||||
if (!value)
|
||||
return ERR_PTR(-ENOMEM);
|
||||
retval = f2fs_getxattr(inode, name_index, "", value, retval);
|
||||
}
|
||||
|
||||
if (retval < 0) {
|
||||
if (retval == -ENODATA)
|
||||
acl = NULL;
|
||||
else
|
||||
acl = ERR_PTR(retval);
|
||||
} else {
|
||||
acl = f2fs_acl_from_disk(value, retval);
|
||||
}
|
||||
kfree(value);
|
||||
if (!IS_ERR(acl))
|
||||
set_cached_acl(inode, type, acl);
|
||||
|
||||
return acl;
|
||||
}
|
||||
|
||||
static int f2fs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
|
||||
struct f2fs_inode_info *fi = F2FS_I(inode);
|
||||
int name_index;
|
||||
void *value = NULL;
|
||||
size_t size = 0;
|
||||
int error;
|
||||
|
||||
if (!test_opt(sbi, POSIX_ACL))
|
||||
return 0;
|
||||
if (S_ISLNK(inode->i_mode))
|
||||
return -EOPNOTSUPP;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
error = f2fs_setxattr(inode, name_index, "", value, size);
|
||||
|
||||
kfree(value);
|
||||
if (!error)
|
||||
set_cached_acl(inode, type, acl);
|
||||
|
||||
cond_clear_inode_flag(fi, FI_ACL_MODE);
|
||||
return error;
|
||||
}
|
||||
|
||||
int f2fs_init_acl(struct inode *inode, struct inode *dir)
|
||||
{
|
||||
struct posix_acl *acl = NULL;
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
|
||||
int error = 0;
|
||||
|
||||
if (!S_ISLNK(inode->i_mode)) {
|
||||
if (test_opt(sbi, POSIX_ACL)) {
|
||||
acl = f2fs_get_acl(dir, ACL_TYPE_DEFAULT);
|
||||
if (IS_ERR(acl))
|
||||
return PTR_ERR(acl);
|
||||
}
|
||||
if (!acl)
|
||||
inode->i_mode &= ~current_umask();
|
||||
}
|
||||
|
||||
if (test_opt(sbi, POSIX_ACL) && acl) {
|
||||
|
||||
if (S_ISDIR(inode->i_mode)) {
|
||||
error = f2fs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
|
||||
if (error)
|
||||
goto cleanup;
|
||||
}
|
||||
error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
|
||||
if (error < 0)
|
||||
return error;
|
||||
if (error > 0)
|
||||
error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl);
|
||||
}
|
||||
cleanup:
|
||||
posix_acl_release(acl);
|
||||
return error;
|
||||
}
|
||||
|
||||
int f2fs_acl_chmod(struct inode *inode)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
|
||||
struct posix_acl *acl;
|
||||
int error;
|
||||
mode_t mode = get_inode_mode(inode);
|
||||
|
||||
if (!test_opt(sbi, POSIX_ACL))
|
||||
return 0;
|
||||
if (S_ISLNK(mode))
|
||||
return -EOPNOTSUPP;
|
||||
|
||||
acl = f2fs_get_acl(inode, ACL_TYPE_ACCESS);
|
||||
if (IS_ERR(acl) || !acl)
|
||||
return PTR_ERR(acl);
|
||||
|
||||
error = posix_acl_chmod(&acl, GFP_KERNEL, mode);
|
||||
if (error)
|
||||
return error;
|
||||
error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl);
|
||||
posix_acl_release(acl);
|
||||
return error;
|
||||
}
|
||||
|
||||
static size_t f2fs_xattr_list_acl(struct dentry *dentry, char *list,
|
||||
size_t list_size, const char *name, size_t name_len, int type)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
|
||||
const char *xname = POSIX_ACL_XATTR_DEFAULT;
|
||||
size_t size;
|
||||
|
||||
if (!test_opt(sbi, POSIX_ACL))
|
||||
return 0;
|
||||
|
||||
if (type == ACL_TYPE_ACCESS)
|
||||
xname = POSIX_ACL_XATTR_ACCESS;
|
||||
|
||||
size = strlen(xname) + 1;
|
||||
if (list && size <= list_size)
|
||||
memcpy(list, xname, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
static int f2fs_xattr_get_acl(struct dentry *dentry, const char *name,
|
||||
void *buffer, size_t size, int type)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
|
||||
struct posix_acl *acl;
|
||||
int error;
|
||||
|
||||
if (strcmp(name, "") != 0)
|
||||
return -EINVAL;
|
||||
if (!test_opt(sbi, POSIX_ACL))
|
||||
return -EOPNOTSUPP;
|
||||
|
||||
acl = f2fs_get_acl(dentry->d_inode, type);
|
||||
if (IS_ERR(acl))
|
||||
return PTR_ERR(acl);
|
||||
if (!acl)
|
||||
return -ENODATA;
|
||||
error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
|
||||
posix_acl_release(acl);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
static int f2fs_xattr_set_acl(struct dentry *dentry, const char *name,
|
||||
const void *value, size_t size, int flags, int type)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
|
||||
struct inode *inode = dentry->d_inode;
|
||||
struct posix_acl *acl = NULL;
|
||||
int error;
|
||||
|
||||
if (strcmp(name, "") != 0)
|
||||
return -EINVAL;
|
||||
if (!test_opt(sbi, POSIX_ACL))
|
||||
return -EOPNOTSUPP;
|
||||
if (!inode_owner_or_capable(inode))
|
||||
return -EPERM;
|
||||
|
||||
if (value) {
|
||||
acl = posix_acl_from_xattr(&init_user_ns, value, size);
|
||||
if (IS_ERR(acl))
|
||||
return PTR_ERR(acl);
|
||||
if (acl) {
|
||||
error = posix_acl_valid(acl);
|
||||
if (error)
|
||||
goto release_and_out;
|
||||
}
|
||||
} else {
|
||||
acl = NULL;
|
||||
}
|
||||
|
||||
error = f2fs_set_acl(inode, type, acl);
|
||||
|
||||
release_and_out:
|
||||
posix_acl_release(acl);
|
||||
return error;
|
||||
}
|
||||
|
||||
const struct xattr_handler f2fs_xattr_acl_default_handler = {
|
||||
.prefix = POSIX_ACL_XATTR_DEFAULT,
|
||||
.flags = ACL_TYPE_DEFAULT,
|
||||
.list = f2fs_xattr_list_acl,
|
||||
.get = f2fs_xattr_get_acl,
|
||||
.set = f2fs_xattr_set_acl,
|
||||
};
|
||||
|
||||
const struct xattr_handler f2fs_xattr_acl_access_handler = {
|
||||
.prefix = POSIX_ACL_XATTR_ACCESS,
|
||||
.flags = ACL_TYPE_ACCESS,
|
||||
.list = f2fs_xattr_list_acl,
|
||||
.get = f2fs_xattr_get_acl,
|
||||
.set = f2fs_xattr_set_acl,
|
||||
};
|
||||
|
||||
static size_t f2fs_xattr_advise_list(struct dentry *dentry, char *list,
|
||||
size_t list_size, const char *name, size_t name_len, int type)
|
||||
{
|
||||
const char *xname = F2FS_SYSTEM_ADVISE_PREFIX;
|
||||
size_t size;
|
||||
|
||||
if (type != F2FS_XATTR_INDEX_ADVISE)
|
||||
return 0;
|
||||
|
||||
size = strlen(xname) + 1;
|
||||
if (list && size <= list_size)
|
||||
memcpy(list, xname, size);
|
||||
return size;
|
||||
}
|
||||
|
||||
static int f2fs_xattr_advise_get(struct dentry *dentry, const char *name,
|
||||
void *buffer, size_t size, int type)
|
||||
{
|
||||
struct inode *inode = dentry->d_inode;
|
||||
|
||||
if (strcmp(name, "") != 0)
|
||||
return -EINVAL;
|
||||
|
||||
*((char *)buffer) = F2FS_I(inode)->i_advise;
|
||||
return sizeof(char);
|
||||
}
|
||||
|
||||
static int f2fs_xattr_advise_set(struct dentry *dentry, const char *name,
|
||||
const void *value, size_t size, int flags, int type)
|
||||
{
|
||||
struct inode *inode = dentry->d_inode;
|
||||
|
||||
if (strcmp(name, "") != 0)
|
||||
return -EINVAL;
|
||||
if (!inode_owner_or_capable(inode))
|
||||
return -EPERM;
|
||||
if (value == NULL)
|
||||
return -EINVAL;
|
||||
|
||||
F2FS_I(inode)->i_advise |= *(char *)value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const struct xattr_handler f2fs_xattr_advise_handler = {
|
||||
.prefix = F2FS_SYSTEM_ADVISE_PREFIX,
|
||||
.flags = F2FS_XATTR_INDEX_ADVISE,
|
||||
.list = f2fs_xattr_advise_list,
|
||||
.get = f2fs_xattr_advise_get,
|
||||
.set = f2fs_xattr_advise_set,
|
||||
};
|
57
fs/f2fs/acl.h
Normal file
57
fs/f2fs/acl.h
Normal file
@ -0,0 +1,57 @@
|
||||
/**
|
||||
* fs/f2fs/acl.h
|
||||
*
|
||||
* Copyright (c) 2012 Samsung Electronics Co., Ltd.
|
||||
* http://www.samsung.com/
|
||||
*
|
||||
* Portions of this code from linux/fs/ext2/acl.h
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef __F2FS_ACL_H__
|
||||
#define __F2FS_ACL_H__
|
||||
|
||||
#include <linux/posix_acl_xattr.h>
|
||||
|
||||
#define F2FS_ACL_VERSION 0x0001
|
||||
|
||||
struct f2fs_acl_entry {
|
||||
__le16 e_tag;
|
||||
__le16 e_perm;
|
||||
__le32 e_id;
|
||||
};
|
||||
|
||||
struct f2fs_acl_entry_short {
|
||||
__le16 e_tag;
|
||||
__le16 e_perm;
|
||||
};
|
||||
|
||||
struct f2fs_acl_header {
|
||||
__le32 a_version;
|
||||
};
|
||||
|
||||
#ifdef CONFIG_F2FS_FS_POSIX_ACL
|
||||
|
||||
extern struct posix_acl *f2fs_get_acl(struct inode *inode, int type);
|
||||
extern int f2fs_acl_chmod(struct inode *inode);
|
||||
extern int f2fs_init_acl(struct inode *inode, struct inode *dir);
|
||||
#else
|
||||
#define f2fs_check_acl NULL
|
||||
#define f2fs_get_acl NULL
|
||||
#define f2fs_set_acl NULL
|
||||
|
||||
static inline int f2fs_acl_chmod(struct inode *inode)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int f2fs_init_acl(struct inode *inode, struct inode *dir)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#endif /* __F2FS_ACL_H__ */
|
389
fs/f2fs/xattr.c
Normal file
389
fs/f2fs/xattr.c
Normal file
@ -0,0 +1,389 @@
|
||||
/**
|
||||
* fs/f2fs/xattr.c
|
||||
*
|
||||
* Copyright (c) 2012 Samsung Electronics Co., Ltd.
|
||||
* http://www.samsung.com/
|
||||
*
|
||||
* Portions of this code from linux/fs/ext2/xattr.c
|
||||
*
|
||||
* Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
|
||||
*
|
||||
* Fix by Harrison Xing <harrison@mountainviewdata.com>.
|
||||
* Extended attributes for symlinks and special files added per
|
||||
* suggestion of Luka Renko <luka.renko@hermes.si>.
|
||||
* xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
|
||||
* Red Hat Inc.
|
||||
*
|
||||
* 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/rwsem.h>
|
||||
#include <linux/f2fs_fs.h>
|
||||
#include "f2fs.h"
|
||||
#include "xattr.h"
|
||||
|
||||
static size_t f2fs_xattr_generic_list(struct dentry *dentry, char *list,
|
||||
size_t list_size, const char *name, size_t name_len, int type)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
|
||||
int total_len, prefix_len = 0;
|
||||
const char *prefix = NULL;
|
||||
|
||||
switch (type) {
|
||||
case F2FS_XATTR_INDEX_USER:
|
||||
if (!test_opt(sbi, XATTR_USER))
|
||||
return -EOPNOTSUPP;
|
||||
prefix = XATTR_USER_PREFIX;
|
||||
prefix_len = XATTR_USER_PREFIX_LEN;
|
||||
break;
|
||||
case F2FS_XATTR_INDEX_TRUSTED:
|
||||
if (!capable(CAP_SYS_ADMIN))
|
||||
return -EPERM;
|
||||
prefix = XATTR_TRUSTED_PREFIX;
|
||||
prefix_len = XATTR_TRUSTED_PREFIX_LEN;
|
||||
break;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
total_len = prefix_len + name_len + 1;
|
||||
if (list && total_len <= list_size) {
|
||||
memcpy(list, prefix, prefix_len);
|
||||
memcpy(list+prefix_len, name, name_len);
|
||||
list[prefix_len + name_len] = '\0';
|
||||
}
|
||||
return total_len;
|
||||
}
|
||||
|
||||
static int f2fs_xattr_generic_get(struct dentry *dentry, const char *name,
|
||||
void *buffer, size_t size, int type)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
|
||||
|
||||
switch (type) {
|
||||
case F2FS_XATTR_INDEX_USER:
|
||||
if (!test_opt(sbi, XATTR_USER))
|
||||
return -EOPNOTSUPP;
|
||||
break;
|
||||
case F2FS_XATTR_INDEX_TRUSTED:
|
||||
if (!capable(CAP_SYS_ADMIN))
|
||||
return -EPERM;
|
||||
break;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
if (strcmp(name, "") == 0)
|
||||
return -EINVAL;
|
||||
return f2fs_getxattr(dentry->d_inode, type, name,
|
||||
buffer, size);
|
||||
}
|
||||
|
||||
static int f2fs_xattr_generic_set(struct dentry *dentry, const char *name,
|
||||
const void *value, size_t size, int flags, int type)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
|
||||
|
||||
switch (type) {
|
||||
case F2FS_XATTR_INDEX_USER:
|
||||
if (!test_opt(sbi, XATTR_USER))
|
||||
return -EOPNOTSUPP;
|
||||
break;
|
||||
case F2FS_XATTR_INDEX_TRUSTED:
|
||||
if (!capable(CAP_SYS_ADMIN))
|
||||
return -EPERM;
|
||||
break;
|
||||
default:
|
||||
return -EINVAL;
|
||||
}
|
||||
if (strcmp(name, "") == 0)
|
||||
return -EINVAL;
|
||||
|
||||
return f2fs_setxattr(dentry->d_inode, type, name, value, size);
|
||||
}
|
||||
|
||||
const struct xattr_handler f2fs_xattr_user_handler = {
|
||||
.prefix = XATTR_USER_PREFIX,
|
||||
.flags = F2FS_XATTR_INDEX_USER,
|
||||
.list = f2fs_xattr_generic_list,
|
||||
.get = f2fs_xattr_generic_get,
|
||||
.set = f2fs_xattr_generic_set,
|
||||
};
|
||||
|
||||
const struct xattr_handler f2fs_xattr_trusted_handler = {
|
||||
.prefix = XATTR_TRUSTED_PREFIX,
|
||||
.flags = F2FS_XATTR_INDEX_TRUSTED,
|
||||
.list = f2fs_xattr_generic_list,
|
||||
.get = f2fs_xattr_generic_get,
|
||||
.set = f2fs_xattr_generic_set,
|
||||
};
|
||||
|
||||
static const struct xattr_handler *f2fs_xattr_handler_map[] = {
|
||||
[F2FS_XATTR_INDEX_USER] = &f2fs_xattr_user_handler,
|
||||
#ifdef CONFIG_F2FS_FS_POSIX_ACL
|
||||
[F2FS_XATTR_INDEX_POSIX_ACL_ACCESS] = &f2fs_xattr_acl_access_handler,
|
||||
[F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &f2fs_xattr_acl_default_handler,
|
||||
#endif
|
||||
[F2FS_XATTR_INDEX_TRUSTED] = &f2fs_xattr_trusted_handler,
|
||||
[F2FS_XATTR_INDEX_ADVISE] = &f2fs_xattr_advise_handler,
|
||||
};
|
||||
|
||||
const struct xattr_handler *f2fs_xattr_handlers[] = {
|
||||
&f2fs_xattr_user_handler,
|
||||
#ifdef CONFIG_F2FS_FS_POSIX_ACL
|
||||
&f2fs_xattr_acl_access_handler,
|
||||
&f2fs_xattr_acl_default_handler,
|
||||
#endif
|
||||
&f2fs_xattr_trusted_handler,
|
||||
&f2fs_xattr_advise_handler,
|
||||
NULL,
|
||||
};
|
||||
|
||||
static inline const struct xattr_handler *f2fs_xattr_handler(int name_index)
|
||||
{
|
||||
const struct xattr_handler *handler = NULL;
|
||||
|
||||
if (name_index > 0 && name_index < ARRAY_SIZE(f2fs_xattr_handler_map))
|
||||
handler = f2fs_xattr_handler_map[name_index];
|
||||
return handler;
|
||||
}
|
||||
|
||||
int f2fs_getxattr(struct inode *inode, int name_index, const char *name,
|
||||
void *buffer, size_t buffer_size)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
|
||||
struct f2fs_inode_info *fi = F2FS_I(inode);
|
||||
struct f2fs_xattr_entry *entry;
|
||||
struct page *page;
|
||||
void *base_addr;
|
||||
int error = 0, found = 0;
|
||||
int value_len, name_len;
|
||||
|
||||
if (name == NULL)
|
||||
return -EINVAL;
|
||||
name_len = strlen(name);
|
||||
|
||||
if (!fi->i_xattr_nid)
|
||||
return -ENODATA;
|
||||
|
||||
page = get_node_page(sbi, fi->i_xattr_nid);
|
||||
base_addr = page_address(page);
|
||||
|
||||
list_for_each_xattr(entry, base_addr) {
|
||||
if (entry->e_name_index != name_index)
|
||||
continue;
|
||||
if (entry->e_name_len != name_len)
|
||||
continue;
|
||||
if (!memcmp(entry->e_name, name, name_len)) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
error = -ENODATA;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
value_len = le16_to_cpu(entry->e_value_size);
|
||||
|
||||
if (buffer && value_len > buffer_size) {
|
||||
error = -ERANGE;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (buffer) {
|
||||
char *pval = entry->e_name + entry->e_name_len;
|
||||
memcpy(buffer, pval, value_len);
|
||||
}
|
||||
error = value_len;
|
||||
|
||||
cleanup:
|
||||
f2fs_put_page(page, 1);
|
||||
return error;
|
||||
}
|
||||
|
||||
ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
|
||||
{
|
||||
struct inode *inode = dentry->d_inode;
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
|
||||
struct f2fs_inode_info *fi = F2FS_I(inode);
|
||||
struct f2fs_xattr_entry *entry;
|
||||
struct page *page;
|
||||
void *base_addr;
|
||||
int error = 0;
|
||||
size_t rest = buffer_size;
|
||||
|
||||
if (!fi->i_xattr_nid)
|
||||
return 0;
|
||||
|
||||
page = get_node_page(sbi, fi->i_xattr_nid);
|
||||
base_addr = page_address(page);
|
||||
|
||||
list_for_each_xattr(entry, base_addr) {
|
||||
const struct xattr_handler *handler =
|
||||
f2fs_xattr_handler(entry->e_name_index);
|
||||
size_t size;
|
||||
|
||||
if (!handler)
|
||||
continue;
|
||||
|
||||
size = handler->list(dentry, buffer, rest, entry->e_name,
|
||||
entry->e_name_len, handler->flags);
|
||||
if (buffer && size > rest) {
|
||||
error = -ERANGE;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (buffer)
|
||||
buffer += size;
|
||||
rest -= size;
|
||||
}
|
||||
error = buffer_size - rest;
|
||||
cleanup:
|
||||
f2fs_put_page(page, 1);
|
||||
return error;
|
||||
}
|
||||
|
||||
int f2fs_setxattr(struct inode *inode, int name_index, const char *name,
|
||||
const void *value, size_t value_len)
|
||||
{
|
||||
struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
|
||||
struct f2fs_inode_info *fi = F2FS_I(inode);
|
||||
struct f2fs_xattr_header *header = NULL;
|
||||
struct f2fs_xattr_entry *here, *last;
|
||||
struct page *page;
|
||||
void *base_addr;
|
||||
int error, found, free, name_len, newsize;
|
||||
char *pval;
|
||||
|
||||
if (name == NULL)
|
||||
return -EINVAL;
|
||||
name_len = strlen(name);
|
||||
|
||||
if (value == NULL)
|
||||
value_len = 0;
|
||||
|
||||
if (name_len > 255 || value_len > MAX_VALUE_LEN)
|
||||
return -ERANGE;
|
||||
|
||||
mutex_lock_op(sbi, NODE_NEW);
|
||||
if (!fi->i_xattr_nid) {
|
||||
/* Allocate new attribute block */
|
||||
struct dnode_of_data dn;
|
||||
|
||||
if (!alloc_nid(sbi, &fi->i_xattr_nid)) {
|
||||
mutex_unlock_op(sbi, NODE_NEW);
|
||||
return -ENOSPC;
|
||||
}
|
||||
set_new_dnode(&dn, inode, NULL, NULL, fi->i_xattr_nid);
|
||||
mark_inode_dirty(inode);
|
||||
|
||||
page = new_node_page(&dn, XATTR_NODE_OFFSET);
|
||||
if (IS_ERR(page)) {
|
||||
alloc_nid_failed(sbi, fi->i_xattr_nid);
|
||||
fi->i_xattr_nid = 0;
|
||||
mutex_unlock_op(sbi, NODE_NEW);
|
||||
return PTR_ERR(page);
|
||||
}
|
||||
|
||||
alloc_nid_done(sbi, fi->i_xattr_nid);
|
||||
base_addr = page_address(page);
|
||||
header = XATTR_HDR(base_addr);
|
||||
header->h_magic = cpu_to_le32(F2FS_XATTR_MAGIC);
|
||||
header->h_refcount = cpu_to_le32(1);
|
||||
} else {
|
||||
/* The inode already has an extended attribute block. */
|
||||
page = get_node_page(sbi, fi->i_xattr_nid);
|
||||
if (IS_ERR(page)) {
|
||||
mutex_unlock_op(sbi, NODE_NEW);
|
||||
return PTR_ERR(page);
|
||||
}
|
||||
|
||||
base_addr = page_address(page);
|
||||
header = XATTR_HDR(base_addr);
|
||||
}
|
||||
|
||||
if (le32_to_cpu(header->h_magic) != F2FS_XATTR_MAGIC) {
|
||||
error = -EIO;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* find entry with wanted name. */
|
||||
found = 0;
|
||||
list_for_each_xattr(here, base_addr) {
|
||||
if (here->e_name_index != name_index)
|
||||
continue;
|
||||
if (here->e_name_len != name_len)
|
||||
continue;
|
||||
if (!memcmp(here->e_name, name, name_len)) {
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
last = here;
|
||||
|
||||
while (!IS_XATTR_LAST_ENTRY(last))
|
||||
last = XATTR_NEXT_ENTRY(last);
|
||||
|
||||
newsize = XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) +
|
||||
name_len + value_len);
|
||||
|
||||
/* 1. Check space */
|
||||
if (value) {
|
||||
/* If value is NULL, it is remove operation.
|
||||
* In case of update operation, we caculate free.
|
||||
*/
|
||||
free = MIN_OFFSET - ((char *)last - (char *)header);
|
||||
if (found)
|
||||
free = free - ENTRY_SIZE(here);
|
||||
|
||||
if (free < newsize) {
|
||||
error = -ENOSPC;
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
/* 2. Remove old entry */
|
||||
if (found) {
|
||||
/* If entry is found, remove old entry.
|
||||
* If not found, remove operation is not needed.
|
||||
*/
|
||||
struct f2fs_xattr_entry *next = XATTR_NEXT_ENTRY(here);
|
||||
int oldsize = ENTRY_SIZE(here);
|
||||
|
||||
memmove(here, next, (char *)last - (char *)next);
|
||||
last = (struct f2fs_xattr_entry *)((char *)last - oldsize);
|
||||
memset(last, 0, oldsize);
|
||||
}
|
||||
|
||||
/* 3. Write new entry */
|
||||
if (value) {
|
||||
/* Before we come here, old entry is removed.
|
||||
* We just write new entry. */
|
||||
memset(last, 0, newsize);
|
||||
last->e_name_index = name_index;
|
||||
last->e_name_len = name_len;
|
||||
memcpy(last->e_name, name, name_len);
|
||||
pval = last->e_name + name_len;
|
||||
memcpy(pval, value, value_len);
|
||||
last->e_value_size = cpu_to_le16(value_len);
|
||||
}
|
||||
|
||||
set_page_dirty(page);
|
||||
f2fs_put_page(page, 1);
|
||||
|
||||
if (is_inode_flag_set(fi, FI_ACL_MODE)) {
|
||||
inode->i_mode = fi->i_acl_mode;
|
||||
inode->i_ctime = CURRENT_TIME;
|
||||
clear_inode_flag(fi, FI_ACL_MODE);
|
||||
}
|
||||
f2fs_write_inode(inode, NULL);
|
||||
mutex_unlock_op(sbi, NODE_NEW);
|
||||
|
||||
return 0;
|
||||
cleanup:
|
||||
f2fs_put_page(page, 1);
|
||||
mutex_unlock_op(sbi, NODE_NEW);
|
||||
return error;
|
||||
}
|
145
fs/f2fs/xattr.h
Normal file
145
fs/f2fs/xattr.h
Normal file
@ -0,0 +1,145 @@
|
||||
/**
|
||||
* fs/f2fs/xattr.h
|
||||
*
|
||||
* Copyright (c) 2012 Samsung Electronics Co., Ltd.
|
||||
* http://www.samsung.com/
|
||||
*
|
||||
* Portions of this code from linux/fs/ext2/xattr.h
|
||||
*
|
||||
* On-disk format of extended attributes for the ext2 filesystem.
|
||||
*
|
||||
* (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef __F2FS_XATTR_H__
|
||||
#define __F2FS_XATTR_H__
|
||||
|
||||
#include <linux/init.h>
|
||||
#include <linux/xattr.h>
|
||||
|
||||
/* Magic value in attribute blocks */
|
||||
#define F2FS_XATTR_MAGIC 0xF2F52011
|
||||
|
||||
/* Maximum number of references to one attribute block */
|
||||
#define F2FS_XATTR_REFCOUNT_MAX 1024
|
||||
|
||||
/* Name indexes */
|
||||
#define F2FS_SYSTEM_ADVISE_PREFIX "system.advise"
|
||||
#define F2FS_XATTR_INDEX_USER 1
|
||||
#define F2FS_XATTR_INDEX_POSIX_ACL_ACCESS 2
|
||||
#define F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
|
||||
#define F2FS_XATTR_INDEX_TRUSTED 4
|
||||
#define F2FS_XATTR_INDEX_LUSTRE 5
|
||||
#define F2FS_XATTR_INDEX_SECURITY 6
|
||||
#define F2FS_XATTR_INDEX_ADVISE 7
|
||||
|
||||
struct f2fs_xattr_header {
|
||||
__le32 h_magic; /* magic number for identification */
|
||||
__le32 h_refcount; /* reference count */
|
||||
__u32 h_reserved[4]; /* zero right now */
|
||||
};
|
||||
|
||||
struct f2fs_xattr_entry {
|
||||
__u8 e_name_index;
|
||||
__u8 e_name_len;
|
||||
__le16 e_value_size; /* size of attribute value */
|
||||
char e_name[0]; /* attribute name */
|
||||
};
|
||||
|
||||
#define XATTR_HDR(ptr) ((struct f2fs_xattr_header *)(ptr))
|
||||
#define XATTR_ENTRY(ptr) ((struct f2fs_xattr_entry *)(ptr))
|
||||
#define XATTR_FIRST_ENTRY(ptr) (XATTR_ENTRY(XATTR_HDR(ptr)+1))
|
||||
#define XATTR_ROUND (3)
|
||||
|
||||
#define XATTR_ALIGN(size) ((size + XATTR_ROUND) & ~XATTR_ROUND)
|
||||
|
||||
#define ENTRY_SIZE(entry) (XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + \
|
||||
entry->e_name_len + le16_to_cpu(entry->e_value_size)))
|
||||
|
||||
#define XATTR_NEXT_ENTRY(entry) ((struct f2fs_xattr_entry *)((char *)(entry) +\
|
||||
ENTRY_SIZE(entry)))
|
||||
|
||||
#define IS_XATTR_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
|
||||
|
||||
#define list_for_each_xattr(entry, addr) \
|
||||
for (entry = XATTR_FIRST_ENTRY(addr);\
|
||||
!IS_XATTR_LAST_ENTRY(entry);\
|
||||
entry = XATTR_NEXT_ENTRY(entry))
|
||||
|
||||
|
||||
#define MIN_OFFSET XATTR_ALIGN(PAGE_SIZE - \
|
||||
sizeof(struct node_footer) - \
|
||||
sizeof(__u32))
|
||||
|
||||
#define MAX_VALUE_LEN (MIN_OFFSET - sizeof(struct f2fs_xattr_header) - \
|
||||
sizeof(struct f2fs_xattr_entry))
|
||||
|
||||
/**
|
||||
* On-disk structure of f2fs_xattr
|
||||
* We use only 1 block for xattr.
|
||||
*
|
||||
* +--------------------+
|
||||
* | f2fs_xattr_header |
|
||||
* | |
|
||||
* +--------------------+
|
||||
* | f2fs_xattr_entry |
|
||||
* | .e_name_index = 1 |
|
||||
* | .e_name_len = 3 |
|
||||
* | .e_value_size = 14 |
|
||||
* | .e_name = "foo" |
|
||||
* | "value_of_xattr" |<- value_offs = e_name + e_name_len
|
||||
* +--------------------+
|
||||
* | f2fs_xattr_entry |
|
||||
* | .e_name_index = 4 |
|
||||
* | .e_name = "bar" |
|
||||
* +--------------------+
|
||||
* | |
|
||||
* | Free |
|
||||
* | |
|
||||
* +--------------------+<- MIN_OFFSET
|
||||
* | node_footer |
|
||||
* | (nid, ino, offset) |
|
||||
* +--------------------+
|
||||
*
|
||||
**/
|
||||
|
||||
#ifdef CONFIG_F2FS_FS_XATTR
|
||||
extern const struct xattr_handler f2fs_xattr_user_handler;
|
||||
extern const struct xattr_handler f2fs_xattr_trusted_handler;
|
||||
extern const struct xattr_handler f2fs_xattr_acl_access_handler;
|
||||
extern const struct xattr_handler f2fs_xattr_acl_default_handler;
|
||||
extern const struct xattr_handler f2fs_xattr_advise_handler;
|
||||
|
||||
extern const struct xattr_handler *f2fs_xattr_handlers[];
|
||||
|
||||
extern int f2fs_setxattr(struct inode *inode, int name_index, const char *name,
|
||||
const void *value, size_t value_len);
|
||||
extern int f2fs_getxattr(struct inode *inode, int name_index, const char *name,
|
||||
void *buffer, size_t buffer_size);
|
||||
extern ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer,
|
||||
size_t buffer_size);
|
||||
|
||||
#else
|
||||
|
||||
#define f2fs_xattr_handlers NULL
|
||||
static inline int f2fs_setxattr(struct inode *inode, int name_index,
|
||||
const char *name, const void *value, size_t value_len)
|
||||
{
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
static inline int f2fs_getxattr(struct inode *inode, int name_index,
|
||||
const char *name, void *buffer, size_t buffer_size)
|
||||
{
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
static inline ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer,
|
||||
size_t buffer_size)
|
||||
{
|
||||
return -EOPNOTSUPP;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __F2FS_XATTR_H__ */
|
Loading…
Reference in New Issue
Block a user