aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
Diffstat (limited to 'fs')
-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
5btrfs-y := super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \ 5btrfs-y := super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \
6 hash.o file-item.o inode-item.o inode-map.o disk-io.o \ 6 hash.o file-item.o inode-item.o inode-map.o disk-io.o \
7 transaction.o bit-radix.o inode.o file.o tree-defrag.o \ 7 transaction.o bit-radix.o inode.o file.o tree-defrag.o \
8 extent_map.o sysfs.o struct-funcs.o 8 extent_map.o sysfs.o struct-funcs.o xattr.o acl.o
9 9
10#btrfs-y := ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \ 10#btrfs-y := ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \
11# root-tree.o dir-item.o hash.o file-item.o inode-item.o \ 11# 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 @@
1/*
2 * Copyright (C) 2007 Red Hat. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/fs.h>
20#include <linux/string.h>
21#include <linux/xattr.h>
22#include <linux/posix_acl_xattr.h>
23#include "ctree.h"
24#include "xattr.h"
25
26static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
27 void *value, size_t size)
28{
29 return btrfs_xattr_get(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS, name,
30 value, size);
31}
32
33static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
34 const void *value, size_t size, int flags)
35{
36 return btrfs_xattr_set(inode, BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS, name,
37 value, size, flags);
38}
39
40static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
41 void *value, size_t size)
42{
43 return btrfs_xattr_get(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT,
44 name, value, size);
45}
46
47static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
48 const void *value, size_t size, int flags)
49{
50 return btrfs_xattr_set(inode, BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT,
51 name, value, size, flags);
52}
53
54struct xattr_handler btrfs_xattr_acl_default_handler = {
55 .prefix = POSIX_ACL_XATTR_DEFAULT,
56 .list = btrfs_xattr_generic_list,
57 .get = btrfs_xattr_acl_default_get,
58 .set = btrfs_xattr_acl_default_set,
59};
60
61struct xattr_handler btrfs_xattr_acl_access_handler = {
62 .prefix = POSIX_ACL_XATTR_ACCESS,
63 .list = btrfs_xattr_generic_list,
64 .get = btrfs_xattr_acl_access_get,
65 .set = btrfs_xattr_acl_access_set,
66};
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;
63#define BTRFS_FT_FIFO 5 63#define BTRFS_FT_FIFO 5
64#define BTRFS_FT_SOCK 6 64#define BTRFS_FT_SOCK 6
65#define BTRFS_FT_SYMLINK 7 65#define BTRFS_FT_SYMLINK 7
66#define BTRFS_FT_MAX 8 66#define BTRFS_FT_XATTR 8
67#define BTRFS_FT_MAX 9
67 68
68/* 69/*
69 * the key defines the order in the tree, and so it also defines (optimal) 70 * the key defines the order in the tree, and so it also defines (optimal)
@@ -226,7 +227,7 @@ struct btrfs_inode_item {
226 227
227struct btrfs_dir_item { 228struct btrfs_dir_item {
228 struct btrfs_disk_key location; 229 struct btrfs_disk_key location;
229 __le16 flags; 230 __le16 data_len;
230 __le16 name_len; 231 __le16 name_len;
231 u8 type; 232 u8 type;
232} __attribute__ ((__packed__)); 233} __attribute__ ((__packed__));
@@ -367,7 +368,7 @@ struct btrfs_root {
367 * the FS 368 * the FS
368 */ 369 */
369#define BTRFS_INODE_ITEM_KEY 1 370#define BTRFS_INODE_ITEM_KEY 1
370 371#define BTRFS_XATTR_ITEM_KEY 2
371/* reserve 2-15 close to the inode for later flexibility */ 372/* reserve 2-15 close to the inode for later flexibility */
372 373
373/* 374/*
@@ -621,7 +622,7 @@ static inline void btrfs_set_item_key(struct extent_buffer *eb,
621} 622}
622 623
623/* struct btrfs_dir_item */ 624/* struct btrfs_dir_item */
624BTRFS_SETGET_FUNCS(dir_flags, struct btrfs_dir_item, flags, 16); 625BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
625BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8); 626BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
626BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16); 627BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
627 628
@@ -962,6 +963,15 @@ int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
962 struct btrfs_root *root, 963 struct btrfs_root *root,
963 struct btrfs_path *path, 964 struct btrfs_path *path,
964 struct btrfs_dir_item *di); 965 struct btrfs_dir_item *di);
966int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
967 struct btrfs_root *root, const char *name,
968 u16 name_len, const void *data, u16 data_len,
969 u64 dir);
970struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
971 struct btrfs_root *root,
972 struct btrfs_path *path, u64 dir,
973 const char *name, u16 name_len,
974 int mod);
965/* inode-map.c */ 975/* inode-map.c */
966int btrfs_find_free_objectid(struct btrfs_trans_handle *trans, 976int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
967 struct btrfs_root *fs_root, 977 struct btrfs_root *fs_root,
@@ -1039,4 +1049,8 @@ int btrfs_sysfs_add_root(struct btrfs_root *root);
1039void btrfs_sysfs_del_root(struct btrfs_root *root); 1049void btrfs_sysfs_del_root(struct btrfs_root *root);
1040void btrfs_sysfs_del_super(struct btrfs_fs_info *root); 1050void btrfs_sysfs_del_super(struct btrfs_fs_info *root);
1041 1051
1052/* xattr.c */
1053ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
1054int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
1055 struct btrfs_root *root, struct inode *inode);
1042#endif 1056#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
43 return ERR_PTR(-EEXIST); 43 return ERR_PTR(-EEXIST);
44 ret = btrfs_extend_item(trans, root, path, data_size); 44 ret = btrfs_extend_item(trans, root, path, data_size);
45 WARN_ON(ret > 0); 45 WARN_ON(ret > 0);
46 if (ret)
47 return ERR_PTR(ret);
48 } 46 }
49 if (ret < 0) 47 if (ret < 0)
50 return ERR_PTR(ret); 48 return ERR_PTR(ret);
@@ -57,6 +55,57 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
57 return (struct btrfs_dir_item *)ptr; 55 return (struct btrfs_dir_item *)ptr;
58} 56}
59 57
58int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
59 struct btrfs_root *root, const char *name,
60 u16 name_len, const void *data, u16 data_len,
61 u64 dir)
62{
63 int ret = 0;
64 struct btrfs_path *path;
65 struct btrfs_dir_item *dir_item;
66 unsigned long name_ptr, data_ptr;
67 struct btrfs_key key, location;
68 struct btrfs_disk_key disk_key;
69 struct extent_buffer *leaf;
70 u32 data_size;
71
72 key.objectid = dir;
73 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
74 ret = btrfs_name_hash(name, name_len, &key.offset);
75 BUG_ON(ret);
76 path = btrfs_alloc_path();
77 if (!path)
78 return -ENOMEM;
79
80 data_size = sizeof(*dir_item) + name_len + data_len;
81 dir_item = insert_with_overflow(trans, root, path, &key, data_size,
82 name, name_len);
83 /*
84 * FIXME: at some point we should handle xattr's that are larger than
85 * what we can fit in our leaf. We set location to NULL b/c we arent
86 * pointing at anything else, that will change if we store the xattr
87 * data in a separate inode.
88 */
89 BUG_ON(IS_ERR(dir_item));
90 memset(&location, 0, sizeof(location));
91
92 leaf = path->nodes[0];
93 btrfs_cpu_key_to_disk(&disk_key, &location);
94 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
95 btrfs_set_dir_type(leaf, dir_item, BTRFS_FT_XATTR);
96 btrfs_set_dir_name_len(leaf, dir_item, name_len);
97 btrfs_set_dir_data_len(leaf, dir_item, data_len);
98 name_ptr = (unsigned long)(dir_item + 1);
99 data_ptr = (unsigned long)((char *)name_ptr + name_len);
100
101 write_extent_buffer(leaf, name, name_ptr, name_len);
102 write_extent_buffer(leaf, data, data_ptr, data_len);
103 btrfs_mark_buffer_dirty(path->nodes[0]);
104
105 btrfs_free_path(path);
106 return ret;
107}
108
60int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root 109int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
61 *root, const char *name, int name_len, u64 dir, 110 *root, const char *name, int name_len, u64 dir,
62 struct btrfs_key *location, u8 type) 111 struct btrfs_key *location, u8 type)
@@ -90,7 +139,7 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
90 btrfs_cpu_key_to_disk(&disk_key, location); 139 btrfs_cpu_key_to_disk(&disk_key, location);
91 btrfs_set_dir_item_key(leaf, dir_item, &disk_key); 140 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
92 btrfs_set_dir_type(leaf, dir_item, type); 141 btrfs_set_dir_type(leaf, dir_item, type);
93 btrfs_set_dir_flags(leaf, dir_item, 0); 142 btrfs_set_dir_data_len(leaf, dir_item, 0);
94 btrfs_set_dir_name_len(leaf, dir_item, name_len); 143 btrfs_set_dir_name_len(leaf, dir_item, name_len);
95 name_ptr = (unsigned long)(dir_item + 1); 144 name_ptr = (unsigned long)(dir_item + 1);
96 145
@@ -117,7 +166,7 @@ second_insert:
117 btrfs_cpu_key_to_disk(&disk_key, location); 166 btrfs_cpu_key_to_disk(&disk_key, location);
118 btrfs_set_dir_item_key(leaf, dir_item, &disk_key); 167 btrfs_set_dir_item_key(leaf, dir_item, &disk_key);
119 btrfs_set_dir_type(leaf, dir_item, type); 168 btrfs_set_dir_type(leaf, dir_item, type);
120 btrfs_set_dir_flags(leaf, dir_item, 0); 169 btrfs_set_dir_data_len(leaf, dir_item, 0);
121 btrfs_set_dir_name_len(leaf, dir_item, name_len); 170 btrfs_set_dir_name_len(leaf, dir_item, name_len);
122 name_ptr = (unsigned long)(dir_item + 1); 171 name_ptr = (unsigned long)(dir_item + 1);
123 write_extent_buffer(leaf, name, name_ptr, name_len); 172 write_extent_buffer(leaf, name, name_ptr, name_len);
@@ -194,6 +243,43 @@ btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
194 return btrfs_match_dir_item_name(root, path, name, name_len); 243 return btrfs_match_dir_item_name(root, path, name, name_len);
195} 244}
196 245
246struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root,
248 struct btrfs_path *path, u64 dir,
249 const char *name, u16 name_len,
250 int mod)
251{
252 int ret;
253 struct btrfs_key key;
254 int ins_len = mod < 0 ? -1 : 0;
255 int cow = mod != 0;
256 struct btrfs_key found_key;
257 struct extent_buffer *leaf;
258
259 key.objectid = dir;
260 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
261 ret = btrfs_name_hash(name, name_len, &key.offset);
262 BUG_ON(ret);
263 ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
264 if (ret < 0)
265 return ERR_PTR(ret);
266 if (ret > 0) {
267 if (path->slots[0] == 0)
268 return NULL;
269 path->slots[0]--;
270 }
271
272 leaf = path->nodes[0];
273 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
274
275 if (found_key.objectid != dir ||
276 btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY ||
277 found_key.offset != key.offset)
278 return NULL;
279
280 return btrfs_match_dir_item_name(root, path, name, name_len);
281}
282
197struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root, 283struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
198 struct btrfs_path *path, 284 struct btrfs_path *path,
199 const char *name, int name_len) 285 const char *name, int name_len)
@@ -210,7 +296,8 @@ struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
210 total_len = btrfs_item_size_nr(leaf, path->slots[0]); 296 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
211 while(cur < total_len) { 297 while(cur < total_len) {
212 this_len = sizeof(*dir_item) + 298 this_len = sizeof(*dir_item) +
213 btrfs_dir_name_len(leaf, dir_item); 299 btrfs_dir_name_len(leaf, dir_item) +
300 btrfs_dir_data_len(leaf, dir_item);
214 name_ptr = (unsigned long)(dir_item + 1); 301 name_ptr = (unsigned long)(dir_item + 1);
215 302
216 if (btrfs_dir_name_len(leaf, dir_item) == name_len && 303 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,
236 int ret = 0; 323 int ret = 0;
237 324
238 leaf = path->nodes[0]; 325 leaf = path->nodes[0];
239 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di); 326 sub_item_len = sizeof(*di) + btrfs_dir_name_len(leaf, di) +
327 btrfs_dir_data_len(leaf, di);
240 item_len = btrfs_item_size_nr(leaf, path->slots[0]); 328 item_len = btrfs_item_size_nr(leaf, path->slots[0]);
241 if (sub_item_len == item_len) { 329 if (sub_item_len == item_len) {
242 ret = btrfs_del_item(trans, root, path); 330 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 @@
32#include <linux/compat.h> 32#include <linux/compat.h>
33#include <linux/bit_spinlock.h> 33#include <linux/bit_spinlock.h>
34#include <linux/version.h> 34#include <linux/version.h>
35#include <linux/xattr.h>
35#include "ctree.h" 36#include "ctree.h"
36#include "disk-io.h" 37#include "disk-io.h"
37#include "transaction.h" 38#include "transaction.h"
@@ -841,6 +842,9 @@ void btrfs_delete_inode(struct inode *inode)
841 ret = btrfs_truncate_in_trans(trans, root, inode); 842 ret = btrfs_truncate_in_trans(trans, root, inode);
842 if (ret) 843 if (ret)
843 goto no_delete_lock; 844 goto no_delete_lock;
845 ret = btrfs_delete_xattrs(trans, root, inode);
846 if (ret)
847 goto no_delete_lock;
844 ret = btrfs_free_inode(trans, root, inode); 848 ret = btrfs_free_inode(trans, root, inode);
845 if (ret) 849 if (ret)
846 goto no_delete_lock; 850 goto no_delete_lock;
@@ -1110,7 +1114,8 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1110 1114
1111 if (over) 1115 if (over)
1112 goto nopos; 1116 goto nopos;
1113 di_len = btrfs_dir_name_len(leaf, di) + sizeof(*di); 1117 di_len = btrfs_dir_name_len(leaf, di) +
1118 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1114 di_cur += di_len; 1119 di_cur += di_len;
1115 di = (struct btrfs_dir_item *)((char *)di + di_len); 1120 di = (struct btrfs_dir_item *)((char *)di + di_len);
1116 } 1121 }
@@ -2519,6 +2524,10 @@ static struct inode_operations btrfs_dir_inode_operations = {
2519 .symlink = btrfs_symlink, 2524 .symlink = btrfs_symlink,
2520 .setattr = btrfs_setattr, 2525 .setattr = btrfs_setattr,
2521 .mknod = btrfs_mknod, 2526 .mknod = btrfs_mknod,
2527 .setxattr = generic_setxattr,
2528 .getxattr = generic_getxattr,
2529 .listxattr = btrfs_listxattr,
2530 .removexattr = generic_removexattr,
2522}; 2531};
2523 2532
2524static struct inode_operations btrfs_dir_ro_inode_operations = { 2533static struct inode_operations btrfs_dir_ro_inode_operations = {
@@ -2567,6 +2576,10 @@ static struct inode_operations btrfs_file_inode_operations = {
2567 .truncate = btrfs_truncate, 2576 .truncate = btrfs_truncate,
2568 .getattr = btrfs_getattr, 2577 .getattr = btrfs_getattr,
2569 .setattr = btrfs_setattr, 2578 .setattr = btrfs_setattr,
2579 .setxattr = generic_setxattr,
2580 .getxattr = generic_getxattr,
2581 .listxattr = btrfs_listxattr,
2582 .removexattr = generic_removexattr,
2570}; 2583};
2571 2584
2572static struct inode_operations btrfs_special_inode_operations = { 2585static 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)
58 case BTRFS_DIR_ITEM_KEY: 58 case BTRFS_DIR_ITEM_KEY:
59 di = btrfs_item_ptr(l, i, struct btrfs_dir_item); 59 di = btrfs_item_ptr(l, i, struct btrfs_dir_item);
60 btrfs_dir_item_key_to_cpu(l, di, &found_key); 60 btrfs_dir_item_key_to_cpu(l, di, &found_key);
61 printk("\t\tdir oid %llu flags %u type %u\n", 61 printk("\t\tdir oid %llu type %u\n",
62 (unsigned long long)found_key.objectid, 62 (unsigned long long)found_key.objectid,
63 btrfs_dir_flags(l, di),
64 btrfs_dir_type(l, di)); 63 btrfs_dir_type(l, di));
65 break; 64 break;
66 case BTRFS_ROOT_ITEM_KEY: 65 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 @@
40#include "btrfs_inode.h" 40#include "btrfs_inode.h"
41#include "ioctl.h" 41#include "ioctl.h"
42#include "print-tree.h" 42#include "print-tree.h"
43#include "xattr.h"
43 44
44#define BTRFS_SUPER_MAGIC 0x9123683E 45#define BTRFS_SUPER_MAGIC 0x9123683E
45 46
@@ -106,6 +107,7 @@ static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
106 sb->s_maxbytes = MAX_LFS_FILESIZE; 107 sb->s_maxbytes = MAX_LFS_FILESIZE;
107 sb->s_magic = BTRFS_SUPER_MAGIC; 108 sb->s_magic = BTRFS_SUPER_MAGIC;
108 sb->s_op = &btrfs_super_ops; 109 sb->s_op = &btrfs_super_ops;
110 sb->s_xattr = btrfs_xattr_handlers;
109 sb->s_time_gran = 1; 111 sb->s_time_gran = 1;
110 112
111 tree_root = open_ctree(sb); 113 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 @@
1/*
2 * Copyright (C) 2007 Red Hat. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/init.h>
20#include <linux/fs.h>
21#include <linux/slab.h>
22#include <linux/rwsem.h>
23#include <linux/xattr.h>
24#include "ctree.h"
25#include "btrfs_inode.h"
26#include "transaction.h"
27#include "xattr.h"
28#include "disk-io.h"
29
30static struct xattr_handler *btrfs_xattr_handler_map[] = {
31 [BTRFS_XATTR_INDEX_USER] = &btrfs_xattr_user_handler,
32 [BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS] = &btrfs_xattr_acl_access_handler,
33 [BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &btrfs_xattr_acl_default_handler,
34 [BTRFS_XATTR_INDEX_TRUSTED] = &btrfs_xattr_trusted_handler,
35 [BTRFS_XATTR_INDEX_SECURITY] = &btrfs_xattr_security_handler,
36 [BTRFS_XATTR_INDEX_SYSTEM] = &btrfs_xattr_system_handler,
37};
38
39struct xattr_handler *btrfs_xattr_handlers[] = {
40 &btrfs_xattr_user_handler,
41 &btrfs_xattr_acl_access_handler,
42 &btrfs_xattr_acl_default_handler,
43 &btrfs_xattr_trusted_handler,
44 &btrfs_xattr_security_handler,
45 &btrfs_xattr_system_handler,
46 NULL,
47};
48
49/*
50 * @param name - the xattr name
51 * @return - the xattr_handler for the xattr, NULL if its not found
52 *
53 * use this with listxattr where we don't already know the type of xattr we
54 * have
55 */
56static struct xattr_handler *find_btrfs_xattr_handler(struct extent_buffer *l,
57 unsigned long name_ptr,
58 u16 name_len)
59{
60 struct xattr_handler *handler = NULL;
61 int i = 0;
62
63 for (handler = btrfs_xattr_handlers[i]; handler != NULL; i++,
64 handler = btrfs_xattr_handlers[i]) {
65 u16 prefix_len = strlen(handler->prefix);
66
67 if (name_len < prefix_len)
68 continue;
69
70 if (memcmp_extent_buffer(l, handler->prefix, name_ptr,
71 prefix_len) == 0)
72 break;
73 }
74
75 return handler;
76}
77
78/*
79 * @param name_index - the index for the xattr handler
80 * @return the xattr_handler if we found it, NULL otherwise
81 *
82 * use this if we know the type of the xattr already
83 */
84static struct xattr_handler *btrfs_xattr_handler(int name_index)
85{
86 struct xattr_handler *handler = NULL;
87
88 if (name_index >= 0 &&
89 name_index < ARRAY_SIZE(btrfs_xattr_handler_map))
90 handler = btrfs_xattr_handler_map[name_index];
91
92 return handler;
93}
94
95static inline char *get_name(const char *name, int name_index)
96{
97 char *ret = NULL;
98 struct xattr_handler *handler = btrfs_xattr_handler(name_index);
99 int prefix_len;
100
101 if (!handler)
102 return ret;
103
104 prefix_len = strlen(handler->prefix);
105
106 ret = kmalloc(strlen(name) + prefix_len + 1, GFP_KERNEL);
107 if (!ret)
108 return ret;
109
110 memcpy(ret, handler->prefix, prefix_len);
111 memcpy(ret+prefix_len, name, strlen(name));
112 ret[prefix_len + strlen(name)] = '\0';
113
114 return ret;
115}
116
117size_t btrfs_xattr_generic_list(struct inode *inode, char *list,
118 size_t list_size, const char *name,
119 size_t name_len)
120{
121 if (list && (name_len+1) <= list_size) {
122 memcpy(list, name, name_len);
123 list[name_len] = '\0';
124 } else
125 return -ERANGE;
126
127 return name_len+1;
128}
129
130ssize_t btrfs_xattr_get(struct inode *inode, int name_index,
131 const char *attr_name, void *buffer, size_t size)
132{
133 struct btrfs_dir_item *di;
134 struct btrfs_root *root = BTRFS_I(inode)->root;
135 struct btrfs_path *path;
136 struct extent_buffer *leaf;
137 struct xattr_handler *handler = btrfs_xattr_handler(name_index);
138 int ret = 0;
139 unsigned long data_ptr;
140 char *name;
141
142 if (!handler)
143 return -EOPNOTSUPP;
144
145 /* just in case... */
146 if (*attr_name == '\0')
147 return -EINVAL;
148
149 name = get_name(attr_name, name_index);
150 if (!name)
151 return -ENOMEM;
152
153 path = btrfs_alloc_path();
154 if (!path) {
155 kfree(name);
156 return -ENOMEM;
157 }
158
159 mutex_lock(&root->fs_info->fs_mutex);
160 /* lookup the xattr by name */
161 di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
162 strlen(name), 0);
163 if (!di || IS_ERR(di)) {
164 ret = -ENODATA;
165 goto out;
166 }
167
168 leaf = path->nodes[0];
169 /* if size is 0, that means we want the size of the attr */
170 if (!size) {
171 ret = btrfs_dir_data_len(leaf, di);
172 goto out;
173 }
174
175 /* now get the data out of our dir_item */
176 if (btrfs_dir_data_len(leaf, di) > size) {
177 ret = -ERANGE;
178 goto out;
179 }
180 data_ptr = (unsigned long)((char *)(di + 1) +
181 btrfs_dir_name_len(leaf, di));
182 read_extent_buffer(leaf, buffer, data_ptr,
183 btrfs_dir_name_len(leaf, di));
184 ret = btrfs_dir_data_len(leaf, di);
185
186out:
187 mutex_unlock(&root->fs_info->fs_mutex);
188 kfree(name);
189 btrfs_free_path(path);
190 return ret;
191}
192
193int btrfs_xattr_set(struct inode *inode, int name_index,
194 const char *attr_name, const void *value, size_t size,
195 int flags)
196{
197 struct btrfs_dir_item *di;
198 struct btrfs_root *root = BTRFS_I(inode)->root;
199 struct btrfs_trans_handle *trans;
200 struct btrfs_path *path;
201 struct xattr_handler *handler = btrfs_xattr_handler(name_index);
202 char *name;
203 int ret = 0, mod = 0;
204
205 if (!handler)
206 return -EOPNOTSUPP;
207
208 /* just in case... */
209 if (*attr_name == '\0')
210 return -EINVAL;
211
212 name = get_name(attr_name, name_index);
213 if (!name)
214 return -ENOMEM;
215
216 path = btrfs_alloc_path();
217 if (!path) {
218 kfree(name);
219 return -ENOMEM;
220 }
221
222 mutex_lock(&root->fs_info->fs_mutex);
223 trans = btrfs_start_transaction(root, 1);
224 btrfs_set_trans_block_group(trans, inode);
225
226 /* first lets see if we already have this xattr */
227 di = btrfs_lookup_xattr(trans, root, path, inode->i_ino, name,
228 strlen(name), -1);
229 if (IS_ERR(di)) {
230 ret = PTR_ERR(di);
231 goto out;
232 }
233
234 /* ok we already have this xattr, lets remove it */
235 if (di) {
236 /* if we want create only exit */
237 if (flags & XATTR_CREATE) {
238 ret = -EEXIST;
239 goto out;
240 }
241
242 ret = btrfs_delete_one_dir_name(trans, root, path, di);
243 if (ret)
244 goto out;
245 btrfs_release_path(root, path);
246
247 /* if we don't have a value then we are removing the xattr */
248 if (!value) {
249 mod = 1;
250 goto out;
251 }
252 } else if (flags & XATTR_REPLACE) {
253 /* we couldn't find the attr to replace, so error out */
254 ret = -ENODATA;
255 goto out;
256 }
257
258 /* ok we have to create a completely new xattr */
259 ret = btrfs_insert_xattr_item(trans, root, name, strlen(name),
260 value, size, inode->i_ino);
261 if (ret)
262 goto out;
263 mod = 1;
264
265out:
266 if (mod) {
267 inode->i_ctime = CURRENT_TIME;
268 ret = btrfs_update_inode(trans, root, inode);
269 }
270
271 btrfs_end_transaction(trans, root);
272 mutex_unlock(&root->fs_info->fs_mutex);
273 kfree(name);
274 btrfs_free_path(path);
275
276 return ret;
277}
278
279ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
280{
281 struct btrfs_key key, found_key;
282 struct inode *inode = dentry->d_inode;
283 struct btrfs_root *root = BTRFS_I(inode)->root;
284 struct btrfs_path *path;
285 struct btrfs_item *item;
286 struct extent_buffer *leaf;
287 struct btrfs_dir_item *di;
288 struct xattr_handler *handler;
289 int ret = 0, slot, advance;
290 size_t total_size = 0, size_left = size, written;
291 unsigned long name_ptr;
292 char *name;
293 u32 nritems;
294
295 /*
296 * ok we want all objects associated with this id.
297 * NOTE: we set key.offset = 0; because we want to start with the
298 * first xattr that we find and walk forward
299 */
300 key.objectid = inode->i_ino;
301 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
302 key.offset = 0;
303
304 path = btrfs_alloc_path();
305 path->reada = 2;
306 if (!path)
307 return -ENOMEM;
308
309 mutex_lock(&root->fs_info->fs_mutex);
310
311 /* search for our xattrs */
312 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
313 if (ret < 0)
314 goto err;
315 ret = 0;
316 advance = 0;
317 while (1) {
318 leaf = path->nodes[0];
319 nritems = btrfs_header_nritems(leaf);
320 slot = path->slots[0];
321
322 /* this is where we start walking through the path */
323 if (advance || slot >= nritems) {
324 /*
325 * if we've reached the last slot in this leaf we need
326 * to go to the next leaf and reset everything
327 */
328 if (slot >= nritems-1) {
329 ret = btrfs_next_leaf(root, path);
330 if (ret)
331 break;
332 leaf = path->nodes[0];
333 nritems = btrfs_header_nritems(leaf);
334 slot = path->slots[0];
335 } else {
336 /*
337 * just walking through the slots on this leaf
338 */
339 slot++;
340 path->slots[0]++;
341 }
342 }
343 advance = 1;
344
345 item = btrfs_item_nr(leaf, slot);
346 btrfs_item_key_to_cpu(leaf, &found_key, slot);
347
348 /* check to make sure this item is what we want */
349 if (found_key.objectid != key.objectid)
350 break;
351 if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
352 break;
353
354 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
355
356 total_size += btrfs_dir_name_len(leaf, di)+1;
357
358 /* we are just looking for how big our buffer needs to be */
359 if (!size)
360 continue;
361
362 /* find our handler for this xattr */
363 name_ptr = (unsigned long)(di + 1);
364 handler = find_btrfs_xattr_handler(leaf, name_ptr,
365 btrfs_dir_name_len(leaf, di));
366 if (!handler) {
367 printk(KERN_ERR "btrfs: unsupported xattr found\n");
368 continue;
369 }
370
371 name = kmalloc(btrfs_dir_name_len(leaf, di), GFP_KERNEL);
372 read_extent_buffer(leaf, name, name_ptr,
373 btrfs_dir_name_len(leaf, di));
374
375 /* call the list function associated with this xattr */
376 written = handler->list(inode, buffer, size_left, name,
377 btrfs_dir_name_len(leaf, di));
378 kfree(name);
379
380 if (written < 0) {
381 ret = -ERANGE;
382 break;
383 }
384
385 size_left -= written;
386 buffer += written;
387 }
388 ret = total_size;
389
390err:
391 mutex_unlock(&root->fs_info->fs_mutex);
392 btrfs_free_path(path);
393
394 return ret;
395}
396
397/*
398 * delete all the xattrs associated with the inode. fs_mutex should be
399 * held when we come into here
400 */
401int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
402 struct btrfs_root *root, struct inode *inode)
403{
404 struct btrfs_path *path;
405 struct btrfs_key key, found_key;
406 struct btrfs_item *item;
407 struct extent_buffer *leaf;
408 int ret;
409
410 path = btrfs_alloc_path();
411 if (!path)
412 return -ENOMEM;
413
414 key.objectid = inode->i_ino;
415 btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
416 key.offset = (u64)-1;
417
418 while(1) {
419 /* look for our next xattr */
420 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
421 if (ret < 0)
422 goto out;
423 BUG_ON(ret == 0);
424
425 if (path->slots[0] == 0)
426 break;
427
428 path->slots[0]--;
429 leaf = path->nodes[0];
430 item = btrfs_item_nr(leaf, path->slots[0]);
431 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
432
433 if (found_key.objectid != key.objectid)
434 break;
435 if (btrfs_key_type(&found_key) != BTRFS_XATTR_ITEM_KEY)
436 break;
437
438 ret = btrfs_del_item(trans, root, path);
439 BUG_ON(ret);
440 btrfs_release_path(root, path);
441 }
442 ret = 0;
443out:
444 btrfs_free_path(path);
445
446 return ret;
447}
448
449/*
450 * Handler functions
451 */
452#define BTRFS_XATTR_SETGET_FUNCS(name, index) \
453static int btrfs_xattr_##name##_get(struct inode *inode, \
454 const char *name, void *value, \
455 size_t size) \
456{ \
457 return btrfs_xattr_get(inode, index, name, value, size); \
458} \
459static int btrfs_xattr_##name##_set(struct inode *inode, \
460 const char *name, const void *value,\
461 size_t size, int flags) \
462{ \
463 return btrfs_xattr_set(inode, index, name, value, size, flags); \
464} \
465
466BTRFS_XATTR_SETGET_FUNCS(security, BTRFS_XATTR_INDEX_SECURITY);
467BTRFS_XATTR_SETGET_FUNCS(system, BTRFS_XATTR_INDEX_SYSTEM);
468BTRFS_XATTR_SETGET_FUNCS(user, BTRFS_XATTR_INDEX_USER);
469BTRFS_XATTR_SETGET_FUNCS(trusted, BTRFS_XATTR_INDEX_TRUSTED);
470
471struct xattr_handler btrfs_xattr_security_handler = {
472 .prefix = XATTR_SECURITY_PREFIX,
473 .list = btrfs_xattr_generic_list,
474 .get = btrfs_xattr_security_get,
475 .set = btrfs_xattr_security_set,
476};
477
478struct xattr_handler btrfs_xattr_system_handler = {
479 .prefix = XATTR_SYSTEM_PREFIX,
480 .list = btrfs_xattr_generic_list,
481 .get = btrfs_xattr_system_get,
482 .set = btrfs_xattr_system_set,
483};
484
485struct xattr_handler btrfs_xattr_user_handler = {
486 .prefix = XATTR_USER_PREFIX,
487 .list = btrfs_xattr_generic_list,
488 .get = btrfs_xattr_user_get,
489 .set = btrfs_xattr_user_set,
490};
491
492struct xattr_handler btrfs_xattr_trusted_handler = {
493 .prefix = XATTR_USER_PREFIX,
494 .list = btrfs_xattr_generic_list,
495 .get = btrfs_xattr_trusted_get,
496 .set = btrfs_xattr_trusted_set,
497};
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 @@
1/*
2 * Copyright (C) 2007 Red Hat. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#ifndef __XATTR__
20#define __XATTR__
21
22#include <linux/xattr.h>
23#include "ctree.h"
24
25/* Name indexes */
26enum {
27 BTRFS_XATTR_INDEX_USER,
28 BTRFS_XATTR_INDEX_POSIX_ACL_ACCESS,
29 BTRFS_XATTR_INDEX_POSIX_ACL_DEFAULT,
30 BTRFS_XATTR_INDEX_TRUSTED,
31 BTRFS_XATTR_INDEX_SECURITY,
32 BTRFS_XATTR_INDEX_SYSTEM,
33 BTRFS_XATTR_INDEX_END,
34};
35
36extern struct xattr_handler btrfs_xattr_user_handler;
37extern struct xattr_handler btrfs_xattr_trusted_handler;
38extern struct xattr_handler btrfs_xattr_acl_access_handler;
39extern struct xattr_handler btrfs_xattr_acl_default_handler;
40extern struct xattr_handler btrfs_xattr_security_handler;
41extern struct xattr_handler btrfs_xattr_system_handler;
42
43extern struct xattr_handler *btrfs_xattr_handlers[];
44
45ssize_t btrfs_xattr_get(struct inode *inode, int name_index, const char *name,
46 void *buffer, size_t size);
47int btrfs_xattr_set(struct inode *inode, int name_index, const char *name,
48 const void *value, size_t size, int flags);
49
50/*
51 * the only reason this is public is for acl.c. There may be a point where
52 * acl.c doesn't need it, and if thats the case we need to remove it and make
53 * it static in xattr.c
54 */
55size_t btrfs_xattr_generic_list(struct inode *inode, char *list,
56 size_t list_size, const char *name,
57 size_t name_len);
58#endif /* __XATTR__ */