diff options
Diffstat (limited to 'fs/btrfs/export.c')
-rw-r--r-- | fs/btrfs/export.c | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/fs/btrfs/export.c b/fs/btrfs/export.c new file mode 100644 index 000000000000..48b82cd7583c --- /dev/null +++ b/fs/btrfs/export.c | |||
@@ -0,0 +1,201 @@ | |||
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 | #define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, parent_objectid)/4) | ||
11 | #define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, parent_root_objectid)/4) | ||
12 | #define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid)/4) | ||
13 | |||
14 | static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len, | ||
15 | int connectable) | ||
16 | { | ||
17 | struct btrfs_fid *fid = (struct btrfs_fid *)fh; | ||
18 | struct inode *inode = dentry->d_inode; | ||
19 | int len = *max_len; | ||
20 | int type; | ||
21 | |||
22 | if ((len < BTRFS_FID_SIZE_NON_CONNECTABLE) || | ||
23 | (connectable && len < BTRFS_FID_SIZE_CONNECTABLE)) | ||
24 | return 255; | ||
25 | |||
26 | len = BTRFS_FID_SIZE_NON_CONNECTABLE; | ||
27 | type = FILEID_BTRFS_WITHOUT_PARENT; | ||
28 | |||
29 | fid->objectid = BTRFS_I(inode)->location.objectid; | ||
30 | fid->root_objectid = BTRFS_I(inode)->root->objectid; | ||
31 | fid->gen = inode->i_generation; | ||
32 | |||
33 | if (connectable && !S_ISDIR(inode->i_mode)) { | ||
34 | struct inode *parent; | ||
35 | u64 parent_root_id; | ||
36 | |||
37 | spin_lock(&dentry->d_lock); | ||
38 | |||
39 | parent = dentry->d_parent->d_inode; | ||
40 | fid->parent_objectid = BTRFS_I(parent)->location.objectid; | ||
41 | fid->parent_gen = parent->i_generation; | ||
42 | parent_root_id = BTRFS_I(parent)->root->objectid; | ||
43 | |||
44 | spin_unlock(&dentry->d_lock); | ||
45 | |||
46 | if (parent_root_id != fid->root_objectid) { | ||
47 | fid->parent_root_objectid = parent_root_id; | ||
48 | len = BTRFS_FID_SIZE_CONNECTABLE_ROOT; | ||
49 | type = FILEID_BTRFS_WITH_PARENT_ROOT; | ||
50 | } else { | ||
51 | len = BTRFS_FID_SIZE_CONNECTABLE; | ||
52 | type = FILEID_BTRFS_WITH_PARENT; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | *max_len = len; | ||
57 | return type; | ||
58 | } | ||
59 | |||
60 | static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid, | ||
61 | u64 root_objectid, u32 generation) | ||
62 | { | ||
63 | struct btrfs_root *root; | ||
64 | struct inode *inode; | ||
65 | struct btrfs_key key; | ||
66 | |||
67 | key.objectid = root_objectid; | ||
68 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | ||
69 | key.offset = (u64)-1; | ||
70 | |||
71 | root = btrfs_read_fs_root_no_name(btrfs_sb(sb)->fs_info, &key); | ||
72 | if (IS_ERR(root)) | ||
73 | return ERR_CAST(root); | ||
74 | |||
75 | key.objectid = objectid; | ||
76 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | ||
77 | key.offset = 0; | ||
78 | |||
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 | return d_obtain_alias(inode); | ||
89 | } | ||
90 | |||
91 | static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh, | ||
92 | int fh_len, int fh_type) | ||
93 | { | ||
94 | struct btrfs_fid *fid = (struct btrfs_fid *) fh; | ||
95 | u64 objectid, root_objectid; | ||
96 | u32 generation; | ||
97 | |||
98 | if (fh_type == FILEID_BTRFS_WITH_PARENT) { | ||
99 | if (fh_len != BTRFS_FID_SIZE_CONNECTABLE) | ||
100 | return NULL; | ||
101 | root_objectid = fid->root_objectid; | ||
102 | } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) { | ||
103 | if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) | ||
104 | return NULL; | ||
105 | root_objectid = fid->parent_root_objectid; | ||
106 | } else | ||
107 | return NULL; | ||
108 | |||
109 | objectid = fid->parent_objectid; | ||
110 | generation = fid->parent_gen; | ||
111 | |||
112 | return btrfs_get_dentry(sb, objectid, root_objectid, generation); | ||
113 | } | ||
114 | |||
115 | static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh, | ||
116 | int fh_len, int fh_type) | ||
117 | { | ||
118 | struct btrfs_fid *fid = (struct btrfs_fid *) fh; | ||
119 | u64 objectid, root_objectid; | ||
120 | u32 generation; | ||
121 | |||
122 | if ((fh_type != FILEID_BTRFS_WITH_PARENT || | ||
123 | fh_len != BTRFS_FID_SIZE_CONNECTABLE) && | ||
124 | (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT || | ||
125 | fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) && | ||
126 | (fh_type != FILEID_BTRFS_WITHOUT_PARENT || | ||
127 | fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE)) | ||
128 | return NULL; | ||
129 | |||
130 | objectid = fid->objectid; | ||
131 | root_objectid = fid->root_objectid; | ||
132 | generation = fid->gen; | ||
133 | |||
134 | return btrfs_get_dentry(sb, objectid, root_objectid, generation); | ||
135 | } | ||
136 | |||
137 | static struct dentry *btrfs_get_parent(struct dentry *child) | ||
138 | { | ||
139 | struct inode *dir = child->d_inode; | ||
140 | struct btrfs_root *root = BTRFS_I(dir)->root; | ||
141 | struct btrfs_key key; | ||
142 | struct btrfs_path *path; | ||
143 | struct extent_buffer *leaf; | ||
144 | int slot; | ||
145 | u64 objectid; | ||
146 | int ret; | ||
147 | |||
148 | path = btrfs_alloc_path(); | ||
149 | |||
150 | key.objectid = dir->i_ino; | ||
151 | btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); | ||
152 | key.offset = (u64)-1; | ||
153 | |||
154 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | ||
155 | if (ret < 0) { | ||
156 | /* Error */ | ||
157 | btrfs_free_path(path); | ||
158 | return ERR_PTR(ret); | ||
159 | } | ||
160 | leaf = path->nodes[0]; | ||
161 | slot = path->slots[0]; | ||
162 | if (ret) { | ||
163 | /* btrfs_search_slot() returns the slot where we'd want to | ||
164 | insert a backref for parent inode #0xFFFFFFFFFFFFFFFF. | ||
165 | The _real_ backref, telling us what the parent inode | ||
166 | _actually_ is, will be in the slot _before_ the one | ||
167 | that btrfs_search_slot() returns. */ | ||
168 | if (!slot) { | ||
169 | /* Unless there is _no_ key in the tree before... */ | ||
170 | btrfs_free_path(path); | ||
171 | return ERR_PTR(-EIO); | ||
172 | } | ||
173 | slot--; | ||
174 | } | ||
175 | |||
176 | btrfs_item_key_to_cpu(leaf, &key, slot); | ||
177 | btrfs_free_path(path); | ||
178 | |||
179 | if (key.objectid != dir->i_ino || key.type != BTRFS_INODE_REF_KEY) | ||
180 | return ERR_PTR(-EINVAL); | ||
181 | |||
182 | objectid = key.offset; | ||
183 | |||
184 | /* If we are already at the root of a subvol, return the real root */ | ||
185 | if (objectid == dir->i_ino) | ||
186 | return dget(dir->i_sb->s_root); | ||
187 | |||
188 | /* Build a new key for the inode item */ | ||
189 | key.objectid = objectid; | ||
190 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | ||
191 | key.offset = 0; | ||
192 | |||
193 | return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL)); | ||
194 | } | ||
195 | |||
196 | const struct export_operations btrfs_export_ops = { | ||
197 | .encode_fh = btrfs_encode_fh, | ||
198 | .fh_to_dentry = btrfs_fh_to_dentry, | ||
199 | .fh_to_parent = btrfs_fh_to_parent, | ||
200 | .get_parent = btrfs_get_parent, | ||
201 | }; | ||