#include <linux/slab.h>
#include <linux/rwsem.h>
#include <linux/xattr.h>
+#include <linux/security.h>
#include "ctree.h"
#include "btrfs_inode.h"
#include "transaction.h"
#include "xattr.h"
#include "disk-io.h"
-static struct xattr_handler *btrfs_xattr_handler_map[] = {
- [BTRFS_XATTR_INDEX_USER] = &btrfs_xattr_user_handler,
-#ifdef CONFIG_FS_POSIX_ACL
- [BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS] = &btrfs_xattr_acl_access_handler,
- [BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &btrfs_xattr_acl_default_handler,
-#endif
- [BTRFS_XATTR_INDEX_TRUSTED] = &btrfs_xattr_trusted_handler,
- [BTRFS_XATTR_INDEX_SECURITY] = &btrfs_xattr_security_handler,
- [BTRFS_XATTR_INDEX_SYSTEM] = &btrfs_xattr_system_handler,
-};
-
-struct xattr_handler *btrfs_xattr_handlers[] = {
- &btrfs_xattr_user_handler,
-#ifdef CONFIG_FS_POSIX_ACL
- &btrfs_xattr_acl_access_handler,
- &btrfs_xattr_acl_default_handler,
-#endif
- &btrfs_xattr_trusted_handler,
- &btrfs_xattr_security_handler,
- &btrfs_xattr_system_handler,
- NULL,
-};
-
-/*
- * @param name_index - the index for the xattr handler
- * @return the xattr_handler if we found it, NULL otherwise
- *
- * use this if we know the type of the xattr already
- */
-static struct xattr_handler *btrfs_xattr_handler(int name_index)
-{
- struct xattr_handler *handler = NULL;
-
- if (name_index >= 0 &&
- name_index < ARRAY_SIZE(btrfs_xattr_handler_map))
- handler = btrfs_xattr_handler_map[name_index];
- return handler;
-}
-
-static inline char *get_name(const char *name, int name_index)
-{
- char *ret = NULL;
- struct xattr_handler *handler = btrfs_xattr_handler(name_index);
- int prefix_len;
-
- if (!handler)
- return ret;
-
- prefix_len = strlen(handler->prefix);
-
- ret = kmalloc(strlen(name) + prefix_len + 1, GFP_KERNEL);
- if (!ret)
- return ret;
-
- memcpy(ret, handler->prefix, prefix_len);
- memcpy(ret+prefix_len, name, strlen(name));
- ret[prefix_len + strlen(name)] = '\0';
-
- return ret;
-}
-
-ssize_t btrfs_xattr_get(struct inode *inode, int name_index,
- const char *attr_name, void *buffer, size_t size)
+ssize_t __btrfs_getxattr(struct inode *inode, const char *name,
+ void *buffer, size_t size)
{
struct btrfs_dir_item *di;
struct btrfs_root *root = BTRFS_I(inode)->root;
struct btrfs_path *path;
struct extent_buffer *leaf;
- struct xattr_handler *handler = btrfs_xattr_handler(name_index);
int ret = 0;
unsigned long data_ptr;
- char *name;
-
- if (!handler)
- return -EOPNOTSUPP;
- name = get_name(attr_name, name_index);
- if (!name)
- return -ENOMEM;
path = btrfs_alloc_path();
- if (!path) {
- kfree(name);
+ if (!path)
return -ENOMEM;
- }
/* lookup the xattr by name */
di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
strlen(name), 0);
- if (!di || IS_ERR(di)) {
+ if (!di) {
ret = -ENODATA;
goto out;
+ } else if (IS_ERR(di)) {
+ ret = PTR_ERR(di);
+ goto out;
}
leaf = path->nodes[0];
ret = -ERANGE;
goto out;
}
+
+ /*
+ * The way things are packed into the leaf is like this
+ * |struct btrfs_dir_item|name|data|
+ * where name is the xattr name, so security.foo, and data is the
+ * content of the xattr. data_ptr points to the location in memory
+ * where the data starts in the in memory leaf
+ */
data_ptr = (unsigned long)((char *)(di + 1) +
btrfs_dir_name_len(leaf, di));
read_extent_buffer(leaf, buffer, data_ptr,
ret = btrfs_dir_data_len(leaf, di);
out:
- kfree(name);
btrfs_free_path(path);
return ret;
}
-int btrfs_xattr_set(struct inode *inode, int name_index,
- const char *attr_name, const void *value, size_t size,
- int flags)
+static int do_setxattr(struct btrfs_trans_handle *trans,
+ struct inode *inode, const char *name,
+ const void *value, size_t size, int flags)
{
struct btrfs_dir_item *di;
struct btrfs_root *root = BTRFS_I(inode)->root;
- struct btrfs_trans_handle *trans;
struct btrfs_path *path;
- struct xattr_handler *handler = btrfs_xattr_handler(name_index);
- char *name;
- int ret = 0, mod = 0;
- if (!handler)
- return -EOPNOTSUPP;
- name = get_name(attr_name, name_index);
- if (!name)
- return -ENOMEM;
+ size_t name_len = strlen(name);
+ int ret = 0;
+
+ if (name_len + size > BTRFS_MAX_XATTR_SIZE(root))
+ return -ENOSPC;
path = btrfs_alloc_path();
- if (!path) {
- kfree(name);
+ if (!path)
return -ENOMEM;
- }
-
- trans = btrfs_start_transaction(root, 1);
- btrfs_set_trans_block_group(trans, inode);
/* first lets see if we already have this xattr */
di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
}
ret = btrfs_delete_one_dir_name(trans, root, path, di);
- if (ret)
- goto out;
+ BUG_ON(ret);
btrfs_release_path(root, path);
/* if we don't have a value then we are removing the xattr */
- if (!value) {
- mod = 1;
+ if (!value)
goto out;
- }
} else {
btrfs_release_path(root, path);
}
/* ok we have to create a completely new xattr */
- ret = btrfs_insert_xattr_item(trans, root, name, strlen(name),
- value, size, inode->i_ino);
+ ret = btrfs_insert_xattr_item(trans, root, path, inode->i_ino,
+ name, name_len, value, size);
+ BUG_ON(ret);
+out:
+ btrfs_free_path(path);
+ return ret;
+}
+
+int __btrfs_setxattr(struct btrfs_trans_handle *trans,
+ struct inode *inode, const char *name,
+ const void *value, size_t size, int flags)
+{
+ struct btrfs_root *root = BTRFS_I(inode)->root;
+ int ret;
+
+ if (trans)
+ return do_setxattr(trans, inode, name, value, size, flags);
+
+ ret = btrfs_reserve_metadata_space(root, 2);
if (ret)
- goto out;
- mod = 1;
+ return ret;
-out:
- if (mod) {
- inode->i_ctime = CURRENT_TIME;
- ret = btrfs_update_inode(trans, root, inode);
+ trans = btrfs_start_transaction(root, 1);
+ if (!trans) {
+ ret = -ENOMEM;
+ goto out;
}
+ btrfs_set_trans_block_group(trans, inode);
- btrfs_end_transaction(trans, root);
- kfree(name);
- btrfs_free_path(path);
+ ret = do_setxattr(trans, inode, name, value, size, flags);
+ if (ret)
+ goto out;
+ inode->i_ctime = CURRENT_TIME;
+ ret = btrfs_update_inode(trans, root, inode);
+ BUG_ON(ret);
+out:
+ btrfs_end_transaction_throttle(trans, root);
+ btrfs_unreserve_metadata_space(root, 2);
return ret;
}
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
goto err;
- ret = 0;
advance = 0;
while (1) {
leaf = path->nodes[0];
if (!buffer || (name_len + 1) > size_left) {
ret = -ERANGE;
- break;
+ goto err;
}
name_ptr = (unsigned long)(di + 1);
}
/*
- * Handler functions
+ * List of handlers for synthetic system.* attributes. All real ondisk
+ * attributes are handled directly.
+ */
+struct xattr_handler *btrfs_xattr_handlers[] = {
+#ifdef CONFIG_BTRFS_FS_POSIX_ACL
+ &btrfs_xattr_acl_access_handler,
+ &btrfs_xattr_acl_default_handler,
+#endif
+ NULL,
+};
+
+/*
+ * Check if the attribute is in a supported namespace.
+ *
+ * This applied after the check for the synthetic attributes in the system
+ * namespace.
*/
-#define BTRFS_XATTR_SETGET_FUNCS(name, index) \
-static int btrfs_xattr_##name##_get(struct inode *inode, \
- const char *name, void *value, \
- size_t size) \
-{ \
- if (*name == '\0') \
- return -EINVAL; \
- return btrfs_xattr_get(inode, index, name, value, size); \
-} \
-static int btrfs_xattr_##name##_set(struct inode *inode, \
- const char *name, const void *value,\
- size_t size, int flags) \
-{ \
- if (*name == '\0') \
- return -EINVAL; \
- return btrfs_xattr_set(inode, index, name, value, size, flags); \
+static bool btrfs_is_valid_xattr(const char *name)
+{
+ return !strncmp(name, XATTR_SECURITY_PREFIX,
+ XATTR_SECURITY_PREFIX_LEN) ||
+ !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) ||
+ !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
+ !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
}
-BTRFS_XATTR_SETGET_FUNCS(security, BTRFS_XATTR_INDEX_SECURITY);
-BTRFS_XATTR_SETGET_FUNCS(system, BTRFS_XATTR_INDEX_SYSTEM);
-BTRFS_XATTR_SETGET_FUNCS(user, BTRFS_XATTR_INDEX_USER);
-BTRFS_XATTR_SETGET_FUNCS(trusted, BTRFS_XATTR_INDEX_TRUSTED);
+ssize_t btrfs_getxattr(struct dentry *dentry, const char *name,
+ void *buffer, size_t size)
+{
+ /*
+ * If this is a request for a synthetic attribute in the system.*
+ * namespace use the generic infrastructure to resolve a handler
+ * for it via sb->s_xattr.
+ */
+ if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
+ return generic_getxattr(dentry, name, buffer, size);
-struct xattr_handler btrfs_xattr_security_handler = {
- .prefix = XATTR_SECURITY_PREFIX,
- .get = btrfs_xattr_security_get,
- .set = btrfs_xattr_security_set,
-};
+ if (!btrfs_is_valid_xattr(name))
+ return -EOPNOTSUPP;
+ return __btrfs_getxattr(dentry->d_inode, name, buffer, size);
+}
-struct xattr_handler btrfs_xattr_system_handler = {
- .prefix = XATTR_SYSTEM_PREFIX,
- .get = btrfs_xattr_system_get,
- .set = btrfs_xattr_system_set,
-};
+int btrfs_setxattr(struct dentry *dentry, const char *name, const void *value,
+ size_t size, int flags)
+{
+ /*
+ * If this is a request for a synthetic attribute in the system.*
+ * namespace use the generic infrastructure to resolve a handler
+ * for it via sb->s_xattr.
+ */
+ if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
+ return generic_setxattr(dentry, name, value, size, flags);
-struct xattr_handler btrfs_xattr_user_handler = {
- .prefix = XATTR_USER_PREFIX,
- .get = btrfs_xattr_user_get,
- .set = btrfs_xattr_user_set,
-};
+ if (!btrfs_is_valid_xattr(name))
+ return -EOPNOTSUPP;
-struct xattr_handler btrfs_xattr_trusted_handler = {
- .prefix = XATTR_TRUSTED_PREFIX,
- .get = btrfs_xattr_trusted_get,
- .set = btrfs_xattr_trusted_set,
-};
+ if (size == 0)
+ value = ""; /* empty EA, do not remove */
+
+ return __btrfs_setxattr(NULL, dentry->d_inode, name, value, size,
+ flags);
+}
+
+int btrfs_removexattr(struct dentry *dentry, const char *name)
+{
+ /*
+ * If this is a request for a synthetic attribute in the system.*
+ * namespace use the generic infrastructure to resolve a handler
+ * for it via sb->s_xattr.
+ */
+ if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
+ return generic_removexattr(dentry, name);
+
+ if (!btrfs_is_valid_xattr(name))
+ return -EOPNOTSUPP;
+
+ return __btrfs_setxattr(NULL, dentry->d_inode, name, NULL, 0,
+ XATTR_REPLACE);
+}
+
+int btrfs_xattr_security_init(struct btrfs_trans_handle *trans,
+ struct inode *inode, struct inode *dir)
+{
+ int err;
+ size_t len;
+ void *value;
+ char *suffix;
+ char *name;
+
+ err = security_inode_init_security(inode, dir, &suffix, &value, &len);
+ if (err) {
+ if (err == -EOPNOTSUPP)
+ return 0;
+ return err;
+ }
+
+ name = kmalloc(XATTR_SECURITY_PREFIX_LEN + strlen(suffix) + 1,
+ GFP_NOFS);
+ if (!name) {
+ err = -ENOMEM;
+ } else {
+ strcpy(name, XATTR_SECURITY_PREFIX);
+ strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
+ err = __btrfs_setxattr(trans, inode, name, value, len, 0);
+ kfree(name);
+ }
+
+ kfree(suffix);
+ kfree(value);
+ return err;
+}