summaryrefslogtreecommitdiff
path: root/fs/btrfs
diff options
context:
space:
mode:
authorJosef Bacik <jbacik@redhat.com>2007-11-16 19:45:54 +0300
committerChris Mason <chris.mason@oracle.com>2008-09-25 19:03:57 +0400
commit5103e947b9b7ac18ddb21a04ee3486e94c6504d7 (patch)
tree5d7c0c9ce1d57b988165f87c7bae304cb339589c /fs/btrfs
parent0591fb56fb732c7bf500d1446d0bf927046d7002 (diff)
downloadlinux-5103e947b9b7ac18ddb21a04ee3486e94c6504d7.tar.xz
xattr support for btrfs
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs')
-rw-r--r--fs/btrfs/Makefile2
-rw-r--r--fs/btrfs/acl.c66
-rw-r--r--fs/btrfs/ctree.h22
-rw-r--r--fs/btrfs/dir-item.c100
-rw-r--r--fs/btrfs/inode.c15
-rw-r--r--fs/btrfs/print-tree.c3
-rw-r--r--fs/btrfs/super.c2
-rw-r--r--fs/btrfs/xattr.c497
-rw-r--r--fs/btrfs/xattr.h58
9 files changed, 751 insertions, 14 deletions
diff --git a/fs/btrfs/Makefile b/fs/btrfs/Makefile
index 551743be5f03..d5804c5ca075 100644
--- a/fs/btrfs/Makefile
+++ b/fs/btrfs/Makefile
@@ -5,7 +5,7 @@ obj-m := btrfs.o
btrfs-y := super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \
hash.o file-item.o inode-item.o inode-map.o disk-io.o \
transaction.o bit-radix.o inode.o file.o tree-defrag.o \
- extent_map.o sysfs.o struct-funcs.o
+ extent_map.o sysfs.o struct-funcs.o xattr.o acl.o
#btrfs-y := ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
# root-tree.o dir-item.o hash.o file-item.o inode-item.o \
diff --git a/fs/btrfs/acl.c b/fs/btrfs/acl.c
new file mode 100644
index 000000000000..aee9f0657c35
--- /dev/null
+++ b/fs/btrfs/acl.c
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2007 Red Hat. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <linux/fs.h>
+#include <linux/string.h>
+#include <linux/xattr.h>
+#include <linux/posix_acl_xattr.h>
+#include "ctree.h"
+#include "xattr.h"
+
+static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
+ void *value, size_t size)
+{
+ return btrfs_xattr_get(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS, name,
+ value, size);
+}
+
+static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
+ const void *value, size_t size, int flags)
+{
+ return btrfs_xattr_set(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS, name,
+ value, size, flags);
+}
+
+static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
+ void *value, size_t size)
+{
+ return btrfs_xattr_get(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT,
+ name, value, size);
+}
+
+static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
+ const void *value, size_t size, int flags)
+{
+ return btrfs_xattr_set(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT,
+ name, value, size, flags);
+}
+
+struct xattr_handler btrfs_xattr_acl_default_handler = {
+ .prefix = POSIX_ACL_XATTR_DEFAULT,
+ .list = btrfs_xattr_generic_list,
+ .get = btrfs_xattr_acl_default_get,
+ .set = btrfs_xattr_acl_default_set,
+};
+
+struct xattr_handler btrfs_xattr_acl_access_handler = {
+ .prefix = POSIX_ACL_XATTR_ACCESS,
+ .list = btrfs_xattr_generic_list,
+ .get = btrfs_xattr_acl_access_get,
+ .set = btrfs_xattr_acl_access_set,
+};
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 030d21d7f98c..27cadae1af63 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -63,7 +63,8 @@ extern struct kmem_cache *btrfs_path_cachep;
#define BTRFS_FT_FIFO 5
#define BTRFS_FT_SOCK 6
#define BTRFS_FT_SYMLINK 7
-#define BTRFS_FT_MAX 8
+#define BTRFS_FT_XATTR 8
+#define BTRFS_FT_MAX 9
/*
* the key defines the order in the tree, and so it also defines (optimal)
@@ -226,7 +227,7 @@ struct btrfs_inode_item {
struct btrfs_dir_item {
struct btrfs_disk_key location;
- __le16 flags;
+ __le16 data_len;
__le16 name_len;
u8 type;
} __attribute__ ((__packed__));
@@ -367,7 +368,7 @@ struct btrfs_root {
* the FS
*/
#define BTRFS_INODE_ITEM_KEY 1
-
+#define BTRFS_XATTR_ITEM_KEY 2
/* reserve 2-15 close to the inode for later flexibility */
/*
@@ -621,7 +622,7 @@ static inline void btrfs_set_item_key(struct extent_buffer *eb,
}
/* struct btrfs_dir_item */
-BTRFS_SETGET_FUNCS(dir_flags, struct btrfs_dir_item, flags, 16);
+BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
@@ -962,6 +963,15 @@ int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
struct btrfs_root *root,
struct btrfs_path *path,
struct btrfs_dir_item *di);
+int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root, const char *name,
+ u16 name_len, const void *data, u16 data_len,
+ u64 dir);
+struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root,
+ struct btrfs_path *path, u64 dir,
+ const char *name, u16 name_len,
+ int mod);
/* inode-map.c */
int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
struct btrfs_root *fs_root,
@@ -1039,4 +1049,8 @@ int btrfs_sysfs_add_root(struct btrfs_root *root);
void btrfs_sysfs_del_root(struct btrfs_root *root);
void btrfs_sysfs_del_super(struct btrfs_fs_info *root);
+/* xattr.c */
+ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
+int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root, struct inode *inode);
#endif
diff --git a/fs/btrfs/dir-item.c b/fs/btrfs/dir-item.c
index 514a1dc337a8..ddbe12ae0d63 100644
--- a/fs/btrfs/dir-item.c
+++ b/fs/btrfs/dir-item.c
@@ -43,8 +43,6 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
return ERR_PTR(-EEXIST);
ret = btrfs_extend_item(trans, root, path, data_size);
WARN_ON(ret > 0);
- if (ret)
- return ERR_PTR(ret);
}
if (ret < 0)
return ERR_PTR(ret);
@@ -57,6 +55,57 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
return (struct btrfs_dir_item *)ptr;
}
+int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root, const char *name,
+ u16 name_len, const void *data, u16 data_len,
+ u64 dir)
+{
+ int ret = 0;
+ struct btrfs_path *path;
+ struct btrfs_dir_item *dir_item;
+ unsigned long name_ptr, data_ptr;
+ struct btrfs_key key, location;
+ struct btrfs_disk_key disk_key;
+ struct extent_buffer *leaf;
+ u32 data_size;
+
+ key.objectid = dir;
+ btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
+ ret = btrfs_name_hash(name, name_len, &key.offset);
+ BUG_ON(ret);
+ path = btrfs_alloc_path();
+ if (!path)
+ return -ENOMEM;
+
+ data_size = sizeof(*dir_item) + name_len + data_len;
+ dir_item = insert_with_overflow(trans, root, path, &key, data_size,
+ name, name_len);
+ /*
+ * FIXME: at some point we should handle xattr's that are larger than
+ * what we can fit in our leaf. We set location to NULL b/c we arent
+ * pointing at anything else, that will change if we store the xattr
+ * data in a separate inode.
+ */
+ BUG_ON(IS_ERR(dir_item));
+ memset(&location, 0, sizeof(location));
+
+ leaf = path->nodes[0];
+ btrfs_cpu_key_to_disk(&disk_key, &location);
+ btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
+ btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
+ btrfs_set_dir_name_len(leaf, dir_item, name_len);
+ btrfs_set_dir_data_len(leaf, dir_item, data_len);
+ name_ptr = (unsigned long)(dir_item + 1);
+ data_ptr = (unsigned long)((char *)name_ptr + name_len);
+
+ write_extent_buffer(leaf, name, name_ptr, name_len);
+ write_extent_buffer(leaf, data, data_ptr, data_len);
+ btrfs_mark_buffer_dirty(path->nodes[0]);
+
+ btrfs_free_path(path);
+ return ret;
+}
+
int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
*root, const char *name, int name_len, u64 dir,
struct btrfs_key *location, u8 type)
@@ -90,7 +139,7 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
btrfs_cpu_key_to_disk(&disk_key, location);
btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
btrfs_set_dir_type(leaf, dir_item, type);
- btrfs_set_dir_flags(leaf, dir_item, 0);
+ btrfs_set_dir_data_len(leaf, dir_item, 0);
btrfs_set_dir_name_len(leaf, dir_item, name_len);
name_ptr = (unsigned long)(dir_item + 1);
@@ -117,7 +166,7 @@ second_insert:
btrfs_cpu_key_to_disk(&disk_key, location);
btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
btrfs_set_dir_type(leaf, dir_item, type);
- btrfs_set_dir_flags(leaf, dir_item, 0);
+ btrfs_set_dir_data_len(leaf, dir_item, 0);
btrfs_set_dir_name_len(leaf, dir_item, name_len);
name_ptr = (unsigned long)(dir_item + 1);
write_extent_buffer(leaf, name, name_ptr, name_len);
@@ -194,6 +243,43 @@ btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
return btrfs_match_dir_item_name(root, path, name, name_len);
}
+struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root,
+ struct btrfs_path *path, u64 dir,
+ const char *name, u16 name_len,
+ int mod)
+{
+ int ret;
+ struct btrfs_key key;
+ int ins_len = mod < 0 ? -1 : 0;
+ int cow = mod != 0;
+ struct btrfs_key found_key;
+ struct extent_buffer *leaf;
+
+ key.objectid = dir;
+ btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
+ ret = btrfs_name_hash(name, name_len, &key.offset);
+ BUG_ON(ret);
+ ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
+ if (ret < 0)
+ return ERR_PTR(ret);
+ if (ret > 0) {
+ if (path->slots[0] == 0)
+ return NULL;
+ path->slots[0]--;
+ }
+
+ leaf = path->nodes[0];
+ btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+ if (found_key.objectid != dir ||
+ btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY ||
+ found_key.offset != key.offset)
+ return NULL;
+
+ return btrfs_match_dir_item_name(root, path, name, name_len);
+}
+
struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
struct btrfs_path *path,
const char *name, int name_len)
@@ -210,7 +296,8 @@ struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
total_len = btrfs_item_size_nr(leaf, path->slots[0]);
while(cur < total_len) {
this_len = sizeof(*dir_item) +
- btrfs_dir_name_len(leaf, dir_item);
+ btrfs_dir_name_len(leaf, dir_item) +
+ btrfs_dir_data_len(leaf, dir_item);
name_ptr = (unsigned long)(dir_item + 1);
if (btrfs_dir_name_len(leaf, dir_item) == name_len &&
@@ -236,7 +323,8 @@ int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
int ret = 0;
leaf = path->nodes[0];
- sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di);
+ sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
+ btrfs_dir_data_len(leaf, di);
item_len = btrfs_item_size_nr(leaf, path->slots[0]);
if (sub_item_len == item_len) {
ret = btrfs_del_item(trans, root, path);
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index d6d32465bc9a..37f8e0321af8 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -32,6 +32,7 @@
#include <linux/compat.h>
#include <linux/bit_spinlock.h>
#include <linux/version.h>
+#include <linux/xattr.h>
#include "ctree.h"
#include "disk-io.h"
#include "transaction.h"
@@ -841,6 +842,9 @@ void btrfs_delete_inode(struct inode *inode)
ret = btrfs_truncate_in_trans(trans, root, inode);
if (ret)
goto no_delete_lock;
+ ret = btrfs_delete_xattrs(trans, root, inode);
+ if (ret)
+ goto no_delete_lock;
ret = btrfs_free_inode(trans, root, inode);
if (ret)
goto no_delete_lock;
@@ -1110,7 +1114,8 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
if (over)
goto nopos;
- di_len = btrfs_dir_name_len(leaf, di) + sizeof(*di);
+ di_len = btrfs_dir_name_len(leaf, di) +
+ btrfs_dir_data_len(leaf, di) +sizeof(*di);
di_cur += di_len;
di = (struct btrfs_dir_item *)((char *)di + di_len);
}
@@ -2519,6 +2524,10 @@ static struct inode_operations btrfs_dir_inode_operations = {
.symlink = btrfs_symlink,
.setattr = btrfs_setattr,
.mknod = btrfs_mknod,
+ .setxattr = generic_setxattr,
+ .getxattr = generic_getxattr,
+ .listxattr = btrfs_listxattr,
+ .removexattr = generic_removexattr,
};
static struct inode_operations btrfs_dir_ro_inode_operations = {
@@ -2567,6 +2576,10 @@ static struct inode_operations btrfs_file_inode_operations = {
.truncate = btrfs_truncate,
.getattr = btrfs_getattr,
.setattr = btrfs_setattr,
+ .setxattr = generic_setxattr,
+ .getxattr = generic_getxattr,
+ .listxattr = btrfs_listxattr,
+ .removexattr = generic_removexattr,
};
static struct inode_operations btrfs_special_inode_operations = {
diff --git a/fs/btrfs/print-tree.c b/fs/btrfs/print-tree.c
index 9f8696c8a8e8..030324febf6c 100644
--- a/fs/btrfs/print-tree.c
+++ b/fs/btrfs/print-tree.c
@@ -58,9 +58,8 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
case BTRFS_DIR_ITEM_KEY:
di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
btrfs_dir_item_key_to_cpu(l, di, &found_key);
- printk("\t\tdir oid %llu flags %u type %u\n",
+ printk("\t\tdir oid %llu type %u\n",
(unsigned long long)found_key.objectid,
- btrfs_dir_flags(l, di),
btrfs_dir_type(l, di));
break;
case BTRFS_ROOT_ITEM_KEY:
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index f94aa1f97a0a..c46bc3911798 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -40,6 +40,7 @@
#include "btrfs_inode.h"
#include "ioctl.h"
#include "print-tree.h"
+#include "xattr.h"
#define BTRFS_SUPER_MAGIC 0x9123683E
@@ -106,6 +107,7 @@ static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
sb->s_maxbytes = MAX_LFS_FILESIZE;
sb->s_magic = BTRFS_SUPER_MAGIC;
sb->s_op = &btrfs_super_ops;
+ sb->s_xattr = btrfs_xattr_handlers;
sb->s_time_gran = 1;
tree_root = open_ctree(sb);
diff --git a/fs/btrfs/xattr.c b/fs/btrfs/xattr.c
new file mode 100644
index 000000000000..f4ac5e0bbad1
--- /dev/null
+++ b/fs/btrfs/xattr.c
@@ -0,0 +1,497 @@
+/*
+ * Copyright (C) 2007 Red Hat. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/slab.h>
+#include <linux/rwsem.h>
+#include <linux/xattr.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,
+ [BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS] = &btrfs_xattr_acl_access_handler,
+ [BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &btrfs_xattr_acl_default_handler,
+ [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,
+ &btrfs_xattr_acl_access_handler,
+ &btrfs_xattr_acl_default_handler,
+ &btrfs_xattr_trusted_handler,
+ &btrfs_xattr_security_handler,
+ &btrfs_xattr_system_handler,
+ NULL,
+};
+
+/*
+ * @param name - the xattr name
+ * @return - the xattr_handler for the xattr, NULL if its not found
+ *
+ * use this with listxattr where we don't already know the type of xattr we
+ * have
+ */
+static struct xattr_handler *find_btrfs_xattr_handler(struct extent_buffer *l,
+ unsigned long name_ptr,
+ u16 name_len)
+{
+ struct xattr_handler *handler = NULL;
+ int i = 0;
+
+ for (handler = btrfs_xattr_handlers[i]; handler != NULL; i++,
+ handler = btrfs_xattr_handlers[i]) {
+ u16 prefix_len = strlen(handler->prefix);
+
+ if (name_len < prefix_len)
+ continue;
+
+ if (memcmp_extent_buffer(l, handler->prefix, name_ptr,
+ prefix_len) == 0)
+ break;
+ }
+
+ return handler;
+}
+
+/*
+ * @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;
+}
+
+size_t btrfs_xattr_generic_list(struct inode *inode, char *list,
+ size_t list_size, const char *name,
+ size_t name_len)
+{
+ if (list && (name_len+1) <= list_size) {
+ memcpy(list, name, name_len);
+ list[name_len] = '\0';
+ } else
+ return -ERANGE;
+
+ return name_len+1;
+}
+
+ssize_t btrfs_xattr_get(struct inode *inode, int name_index,
+ const char *attr_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;
+
+ /* just in case... */
+ if (*attr_name == '\0')
+ return -EINVAL;
+
+ name = get_name(attr_name, name_index);
+ if (!name)
+ return -ENOMEM;
+
+ path = btrfs_alloc_path();
+ if (!path) {
+ kfree(name);
+ return -ENOMEM;
+ }
+
+ mutex_lock(&root->fs_info->fs_mutex);
+ /* lookup the xattr by name */
+ di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
+ strlen(name), 0);
+ if (!di || IS_ERR(di)) {
+ ret = -ENODATA;
+ goto out;
+ }
+
+ leaf = path->nodes[0];
+ /* if size is 0, that means we want the size of the attr */
+ if (!size) {
+ ret = btrfs_dir_data_len(leaf, di);
+ goto out;
+ }
+
+ /* now get the data out of our dir_item */
+ if (btrfs_dir_data_len(leaf, di) > size) {
+ ret = -ERANGE;
+ goto out;
+ }
+ data_ptr = (unsigned long)((char *)(di + 1) +
+ btrfs_dir_name_len(leaf, di));
+ read_extent_buffer(leaf, buffer, data_ptr,
+ btrfs_dir_name_len(leaf, di));
+ ret = btrfs_dir_data_len(leaf, di);
+
+out:
+ mutex_unlock(&root->fs_info->fs_mutex);
+ 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)
+{
+ 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;
+
+ /* just in case... */
+ if (*attr_name == '\0')
+ return -EINVAL;
+
+ name = get_name(attr_name, name_index);
+ if (!name)
+ return -ENOMEM;
+
+ path = btrfs_alloc_path();
+ if (!path) {
+ kfree(name);
+ return -ENOMEM;
+ }
+
+ mutex_lock(&root->fs_info->fs_mutex);
+ 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,
+ strlen(name), -1);
+ if (IS_ERR(di)) {
+ ret = PTR_ERR(di);
+ goto out;
+ }
+
+ /* ok we already have this xattr, lets remove it */
+ if (di) {
+ /* if we want create only exit */
+ if (flags & XATTR_CREATE) {
+ ret = -EEXIST;
+ goto out;
+ }
+
+ ret = btrfs_delete_one_dir_name(trans, root, path, di);
+ if (ret)
+ goto out;
+ btrfs_release_path(root, path);
+
+ /* if we don't have a value then we are removing the xattr */
+ if (!value) {
+ mod = 1;
+ goto out;
+ }
+ } else if (flags & XATTR_REPLACE) {
+ /* we couldn't find the attr to replace, so error out */
+ ret = -ENODATA;
+ goto out;
+ }
+
+ /* ok we have to create a completely new xattr */
+ ret = btrfs_insert_xattr_item(trans, root, name, strlen(name),
+ value, size, inode->i_ino);
+ if (ret)
+ goto out;
+ mod = 1;
+
+out:
+ if (mod) {
+ inode->i_ctime = CURRENT_TIME;
+ ret = btrfs_update_inode(trans, root, inode);
+ }
+
+ btrfs_end_transaction(trans, root);
+ mutex_unlock(&root->fs_info->fs_mutex);
+ kfree(name);
+ btrfs_free_path(path);
+
+ return ret;
+}
+
+ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
+{
+ struct btrfs_key key, found_key;
+ struct inode *inode = dentry->d_inode;
+ struct btrfs_root *root = BTRFS_I(inode)->root;
+ struct btrfs_path *path;
+ struct btrfs_item *item;
+ struct extent_buffer *leaf;
+ struct btrfs_dir_item *di;
+ struct xattr_handler *handler;
+ int ret = 0, slot, advance;
+ size_t total_size = 0, size_left = size, written;
+ unsigned long name_ptr;
+ char *name;
+ u32 nritems;
+
+ /*
+ * ok we want all objects associated with this id.
+ * NOTE: we set key.offset = 0; because we want to start with the
+ * first xattr that we find and walk forward
+ */
+ key.objectid = inode->i_ino;
+ btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
+ key.offset = 0;
+
+ path = btrfs_alloc_path();
+ path->reada = 2;
+ if (!path)
+ return -ENOMEM;
+
+ mutex_lock(&root->fs_info->fs_mutex);
+
+ /* search for our xattrs */
+ 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];
+ nritems = btrfs_header_nritems(leaf);
+ slot = path->slots[0];
+
+ /* this is where we start walking through the path */
+ if (advance || slot >= nritems) {
+ /*
+ * if we've reached the last slot in this leaf we need
+ * to go to the next leaf and reset everything
+ */
+ if (slot >= nritems-1) {
+ ret = btrfs_next_leaf(root, path);
+ if (ret)
+ break;
+ leaf = path->nodes[0];
+ nritems = btrfs_header_nritems(leaf);
+ slot = path->slots[0];
+ } else {
+ /*
+ * just walking through the slots on this leaf
+ */
+ slot++;
+ path->slots[0]++;
+ }
+ }
+ advance = 1;
+
+ item = btrfs_item_nr(leaf, slot);
+ btrfs_item_key_to_cpu(leaf, &found_key, slot);
+
+ /* check to make sure this item is what we want */
+ if (found_key.objectid != key.objectid)
+ break;
+ if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
+ break;
+
+ di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
+
+ total_size += btrfs_dir_name_len(leaf, di)+1;
+
+ /* we are just looking for how big our buffer needs to be */
+ if (!size)
+ continue;
+
+ /* find our handler for this xattr */
+ name_ptr = (unsigned long)(di + 1);
+ handler = find_btrfs_xattr_handler(leaf, name_ptr,
+ btrfs_dir_name_len(leaf, di));
+ if (!handler) {
+ printk(KERN_ERR "btrfs: unsupported xattr found\n");
+ continue;
+ }
+
+ name = kmalloc(btrfs_dir_name_len(leaf, di), GFP_KERNEL);
+ read_extent_buffer(leaf, name, name_ptr,
+ btrfs_dir_name_len(leaf, di));
+
+ /* call the list function associated with this xattr */
+ written = handler->list(inode, buffer, size_left, name,
+ btrfs_dir_name_len(leaf, di));
+ kfree(name);
+
+ if (written < 0) {
+ ret = -ERANGE;
+ break;
+ }
+
+ size_left -= written;
+ buffer += written;
+ }
+ ret = total_size;
+
+err:
+ mutex_unlock(&root->fs_info->fs_mutex);
+ btrfs_free_path(path);
+
+ return ret;
+}
+
+/*
+ * delete all the xattrs associated with the inode. fs_mutex should be
+ * held when we come into here
+ */
+int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root, struct inode *inode)
+{
+ struct btrfs_path *path;
+ struct btrfs_key key, found_key;
+ struct btrfs_item *item;
+ struct extent_buffer *leaf;
+ int ret;
+
+ path = btrfs_alloc_path();
+ if (!path)
+ return -ENOMEM;
+
+ key.objectid = inode->i_ino;
+ btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
+ key.offset = (u64)-1;
+
+ while(1) {
+ /* look for our next xattr */
+ ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
+ if (ret < 0)
+ goto out;
+ BUG_ON(ret == 0);
+
+ if (path->slots[0] == 0)
+ break;
+
+ path->slots[0]--;
+ leaf = path->nodes[0];
+ item = btrfs_item_nr(leaf, path->slots[0]);
+ btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
+
+ if (found_key.objectid != key.objectid)
+ break;
+ if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
+ break;
+
+ ret = btrfs_del_item(trans, root, path);
+ BUG_ON(ret);
+ btrfs_release_path(root, path);
+ }
+ ret = 0;
+out:
+ btrfs_free_path(path);
+
+ return ret;
+}
+
+/*
+ * Handler functions
+ */
+#define BTRFS_XATTR_SETGET_FUNCS(name, index) \
+static int btrfs_xattr_##name##_get(struct inode *inode, \
+ const char *name, void *value, \
+ size_t size) \
+{ \
+ 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) \
+{ \
+ return btrfs_xattr_set(inode, index, name, value, size, flags); \
+} \
+
+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);
+
+struct xattr_handler btrfs_xattr_security_handler = {
+ .prefix = XATTR_SECURITY_PREFIX,
+ .list = btrfs_xattr_generic_list,
+ .get = btrfs_xattr_security_get,
+ .set = btrfs_xattr_security_set,
+};
+
+struct xattr_handler btrfs_xattr_system_handler = {
+ .prefix = XATTR_SYSTEM_PREFIX,
+ .list = btrfs_xattr_generic_list,
+ .get = btrfs_xattr_system_get,
+ .set = btrfs_xattr_system_set,
+};
+
+struct xattr_handler btrfs_xattr_user_handler = {
+ .prefix = XATTR_USER_PREFIX,
+ .list = btrfs_xattr_generic_list,
+ .get = btrfs_xattr_user_get,
+ .set = btrfs_xattr_user_set,
+};
+
+struct xattr_handler btrfs_xattr_trusted_handler = {
+ .prefix = XATTR_USER_PREFIX,
+ .list = btrfs_xattr_generic_list,
+ .get = btrfs_xattr_trusted_get,
+ .set = btrfs_xattr_trusted_set,
+};
diff --git a/fs/btrfs/xattr.h b/fs/btrfs/xattr.h
new file mode 100644
index 000000000000..b2e47e3f2442
--- /dev/null
+++ b/fs/btrfs/xattr.h
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2007 Red Hat. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#ifndef __XATTR__
+#define __XATTR__
+
+#include <linux/xattr.h>
+#include "ctree.h"
+
+/* Name indexes */
+enum {
+ BTRFS_XATTR_INDEX_USER,
+ BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS,
+ BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT,
+ BTRFS_XATTR_INDEX_TRUSTED,
+ BTRFS_XATTR_INDEX_SECURITY,
+ BTRFS_XATTR_INDEX_SYSTEM,
+ BTRFS_XATTR_INDEX_END,
+};
+
+extern struct xattr_handler btrfs_xattr_user_handler;
+extern struct xattr_handler btrfs_xattr_trusted_handler;
+extern struct xattr_handler btrfs_xattr_acl_access_handler;
+extern struct xattr_handler btrfs_xattr_acl_default_handler;
+extern struct xattr_handler btrfs_xattr_security_handler;
+extern struct xattr_handler btrfs_xattr_system_handler;
+
+extern struct xattr_handler *btrfs_xattr_handlers[];
+
+ssize_t btrfs_xattr_get(struct inode *inode, int name_index, const char *name,
+ void *buffer, size_t size);
+int btrfs_xattr_set(struct inode *inode, int name_index, const char *name,
+ const void *value, size_t size, int flags);
+
+/*
+ * the only reason this is public is for acl.c. There may be a point where
+ * acl.c doesn't need it, and if thats the case we need to remove it and make
+ * it static in xattr.c
+ */
+size_t btrfs_xattr_generic_list(struct inode *inode, char *list,
+ size_t list_size, const char *name,
+ size_t name_len);
+#endif /* __XATTR__ */