aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs
diff options
context:
space:
mode:
authorBalaji Rao <balajirrao@gmail.com>2008-07-20 16:31:56 -0400
committerChris Mason <chris.mason@oracle.com>2008-09-25 11:04:06 -0400
commitbe6e8dc0ba84029997075a1ec77b4ddb863cbe15 (patch)
treeba65e49acd8bc55bfd2719a9d6ce65a81ee037c6 /fs/btrfs
parent1a54ef8c11a0eaef59ff418089f109c27f8bd48d (diff)
NFS support for btrfs - v3
Date: Mon, 21 Jul 2008 02:01:56 +0530 Here's an implementation of NFS support for btrfs. It relies on the fixes which are going in to 2.6.28 for the NFS readdir/lookup deadlock. This uses the btrfs_iget helper introduced previously. [dwmw2: Tidy up a little, switch to d_obtain_alias() w/compat routine, change fh_type, store parent's root object ID where needed, fix some get_parent() and fs_to_dentry() bugs] Signed-off-by: Balaji Rao <balajirrao@gmail.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com> 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/compat.h17
-rw-r--r--fs/btrfs/export.c208
-rw-r--r--fs/btrfs/export.h19
-rw-r--r--fs/btrfs/super.c2
5 files changed, 247 insertions, 1 deletions
diff --git a/fs/btrfs/Makefile b/fs/btrfs/Makefile
index a4b38177abdb..75f8818cbfee 100644
--- a/fs/btrfs/Makefile
+++ b/fs/btrfs/Makefile
@@ -7,7 +7,7 @@ btrfs-y := super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.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 xattr.o ordered-data.o \ 8 extent_map.o sysfs.o struct-funcs.o xattr.o ordered-data.o \
9 extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \ 9 extent_io.o volumes.o async-thread.o ioctl.o locking.o orphan.o \
10 ref-cache.o 10 ref-cache.o export.o
11 11
12btrfs-$(CONFIG_FS_POSIX_ACL) += acl.o 12btrfs-$(CONFIG_FS_POSIX_ACL) += acl.o
13else 13else
diff --git a/fs/btrfs/compat.h b/fs/btrfs/compat.h
index b3349a66999d..d45fb37887bc 100644
--- a/fs/btrfs/compat.h
+++ b/fs/btrfs/compat.h
@@ -5,6 +5,23 @@
5#define trylock_page(page) (!TestSetPageLocked(page)) 5#define trylock_page(page) (!TestSetPageLocked(page))
6#endif 6#endif
7 7
8#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,27)
9static inline struct dentry *d_obtain_alias(struct inode *inode)
10{
11 struct dentry *d;
12
13 if (!inode)
14 return NULL;
15 if (IS_ERR(inode))
16 return ERR_CAST(inode);
17
18 d = d_alloc_anon(inode);
19 if (!d)
20 iput(inode);
21 return d;
22}
23#endif
24
8/* 25/*
9 * Even if AppArmor isn't enabled, it still has different prototypes. 26 * Even if AppArmor isn't enabled, it still has different prototypes.
10 * Add more distro/version pairs here to declare which has AppArmor applied. 27 * Add more distro/version pairs here to declare which has AppArmor applied.
diff --git a/fs/btrfs/export.c b/fs/btrfs/export.c
new file mode 100644
index 000000000000..797b4cbc3786
--- /dev/null
+++ b/fs/btrfs/export.c
@@ -0,0 +1,208 @@
1#include <linux/fs.h>
2#include <linux/types.h>
3#include "ctree.h"
4#include "disk-io.h"
5#include "btrfs_inode.h"
6#include "print-tree.h"
7#include "export.h"
8#include "compat.h"
9
10#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
11#define FILEID_BTRFS_WITHOUT_PARENT 0x4d
12#define FILEID_BTRFS_WITH_PARENT 0x4e
13#define FILEID_BTRFS_WITH_PARENT_ROOT 0x4f
14#endif
15
16#define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, parent_objectid)/4)
17#define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, parent_root_objectid)/4)
18#define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid)/4)
19
20static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
21 int connectable)
22{
23 struct btrfs_fid *fid = (struct btrfs_fid *)fh;
24 struct inode *inode = dentry->d_inode;
25 int len = *max_len;
26 int type;
27
28 if ((len < BTRFS_FID_SIZE_NON_CONNECTABLE) ||
29 (connectable && len < BTRFS_FID_SIZE_CONNECTABLE))
30 return 255;
31
32 len = BTRFS_FID_SIZE_NON_CONNECTABLE;
33 type = FILEID_BTRFS_WITHOUT_PARENT;
34
35 fid->objectid = BTRFS_I(inode)->location.objectid;
36 fid->root_objectid = BTRFS_I(inode)->root->objectid;
37 fid->gen = inode->i_generation;
38
39 if (connectable && !S_ISDIR(inode->i_mode)) {
40 struct inode *parent;
41 u64 parent_root_id;
42
43 spin_lock(&dentry->d_lock);
44
45 parent = dentry->d_parent->d_inode;
46 fid->parent_objectid = BTRFS_I(parent)->location.objectid;
47 fid->parent_gen = parent->i_generation;
48 parent_root_id = BTRFS_I(parent)->root->objectid;
49
50 spin_unlock(&dentry->d_lock);
51
52 if (parent_root_id != fid->root_objectid) {
53 fid->parent_root_objectid = parent_root_id;
54 len = BTRFS_FID_SIZE_CONNECTABLE_ROOT;
55 type = FILEID_BTRFS_WITH_PARENT_ROOT;
56 } else {
57 len = BTRFS_FID_SIZE_CONNECTABLE;
58 type = FILEID_BTRFS_WITH_PARENT;
59 }
60 }
61
62 *max_len = len;
63 return type;
64}
65
66static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
67 u64 root_objectid, u32 generation)
68{
69 struct btrfs_root *root;
70 struct inode *inode;
71 struct dentry *result;
72 struct btrfs_key key;
73
74 key.objectid = objectid;
75 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
76 key.offset = 0;
77
78 root = btrfs_lookup_fs_root(btrfs_sb(sb)->fs_info, root_objectid);
79 inode = btrfs_iget(sb, &key, root, NULL);
80 if (IS_ERR(inode))
81 return (void *)inode;
82
83 if (generation != inode->i_generation) {
84 iput(inode);
85 return ERR_PTR(-ESTALE);
86 }
87
88 result = d_obtain_alias(inode);
89 if (!result)
90 return ERR_PTR(-ENOMEM);
91
92 return result;
93}
94
95static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
96 int fh_len, int fh_type)
97{
98 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
99 u64 objectid, root_objectid;
100 u32 generation;
101
102 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
103 if (fh_len != BTRFS_FID_SIZE_CONNECTABLE)
104 return NULL;
105 root_objectid = fid->root_objectid;
106 } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
107 if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT)
108 return NULL;
109 root_objectid = fid->parent_root_objectid;
110 } else
111 return NULL;
112
113 objectid = fid->parent_objectid;
114 generation = fid->parent_gen;
115
116 return btrfs_get_dentry(sb, objectid, root_objectid, generation);
117}
118
119static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
120 int fh_len, int fh_type)
121{
122 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
123 u64 objectid, root_objectid;
124 u32 generation;
125
126 if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
127 fh_len != BTRFS_FID_SIZE_CONNECTABLE) &&
128 (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
129 fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
130 (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
131 fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE))
132 return NULL;
133
134 objectid = fid->objectid;
135 root_objectid = fid->root_objectid;
136 generation = fid->gen;
137
138 return btrfs_get_dentry(sb, objectid, root_objectid, generation);
139}
140
141static struct dentry *btrfs_get_parent(struct dentry *child)
142{
143 struct inode *dir = child->d_inode;
144 struct inode *inode;
145 struct dentry *parent;
146 struct btrfs_root *root = BTRFS_I(dir)->root;
147 struct btrfs_key key;
148 struct btrfs_path *path;
149 struct extent_buffer *leaf;
150 u32 nritems;
151 int slot;
152 u64 objectid;
153 int ret;
154
155 path = btrfs_alloc_path();
156
157 key.objectid = dir->i_ino;
158 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
159 key.offset = 0;
160 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
161 BUG_ON(ret == 0);
162 ret = 0;
163
164 leaf = path->nodes[0];
165 slot = path->slots[0];
166 nritems = btrfs_header_nritems(leaf);
167 if (slot >= nritems) {
168 ret = btrfs_next_leaf(root, path);
169 if (ret) {
170 btrfs_free_path(path);
171 goto out;
172 }
173 leaf = path->nodes[0];
174 slot = path->slots[0];
175 }
176
177 btrfs_free_path(path);
178
179 btrfs_item_key_to_cpu(leaf, &key, slot);
180 if (key.objectid != dir->i_ino || key.type != BTRFS_INODE_REF_KEY)
181 goto out;
182
183 objectid = key.offset;
184
185 /* Build a new key for the inode item */
186 key.objectid = objectid;
187 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
188 key.offset = 0;
189
190 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
191
192 parent = d_obtain_alias(inode);
193 if (!parent)
194 parent = ERR_PTR(-ENOMEM);
195
196 return parent;
197
198out:
199 btrfs_free_path(path);
200 return ERR_PTR(-EINVAL);
201}
202
203const struct export_operations btrfs_export_ops = {
204 .encode_fh = btrfs_encode_fh,
205 .fh_to_dentry = btrfs_fh_to_dentry,
206 .fh_to_parent = btrfs_fh_to_parent,
207 .get_parent = btrfs_get_parent,
208};
diff --git a/fs/btrfs/export.h b/fs/btrfs/export.h
new file mode 100644
index 000000000000..074348a95841
--- /dev/null
+++ b/fs/btrfs/export.h
@@ -0,0 +1,19 @@
1#ifndef BTRFS_EXPORT_H
2#define BTRFS_EXPORT_H
3
4#include <linux/exportfs.h>
5
6extern const struct export_operations btrfs_export_ops;
7
8struct btrfs_fid {
9 u64 objectid;
10 u64 root_objectid;
11 u32 gen;
12
13 u64 parent_objectid;
14 u32 parent_gen;
15
16 u64 parent_root_objectid;
17} __attribute__ ((packed));
18
19#endif
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index eb4b357d05e1..e830e0ed409a 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -46,6 +46,7 @@
46#include "xattr.h" 46#include "xattr.h"
47#include "volumes.h" 47#include "volumes.h"
48#include "version.h" 48#include "version.h"
49#include "export.h"
49 50
50#define BTRFS_SUPER_MAGIC 0x9123683E 51#define BTRFS_SUPER_MAGIC 0x9123683E
51 52
@@ -303,6 +304,7 @@ static int btrfs_fill_super(struct super_block * sb,
303 sb->s_maxbytes = MAX_LFS_FILESIZE; 304 sb->s_maxbytes = MAX_LFS_FILESIZE;
304 sb->s_magic = BTRFS_SUPER_MAGIC; 305 sb->s_magic = BTRFS_SUPER_MAGIC;
305 sb->s_op = &btrfs_super_ops; 306 sb->s_op = &btrfs_super_ops;
307 sb->s_export_op = &btrfs_export_ops;
306 sb->s_xattr = btrfs_xattr_handlers; 308 sb->s_xattr = btrfs_xattr_handlers;
307 sb->s_time_gran = 1; 309 sb->s_time_gran = 1;
308 sb->s_flags |= MS_POSIXACL; 310 sb->s_flags |= MS_POSIXACL;