Btrfs: Always use 64bit inode number
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / btrfs / export.c
CommitLineData
be6e8dc0
BR
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
d397712b
CM
10#define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \
11 parent_objectid) / 4)
12#define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, \
13 parent_root_objectid) / 4)
14#define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid) / 4)
be6e8dc0
BR
15
16static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
17 int connectable)
18{
19 struct btrfs_fid *fid = (struct btrfs_fid *)fh;
20 struct inode *inode = dentry->d_inode;
21 int len = *max_len;
22 int type;
23
24 if ((len < BTRFS_FID_SIZE_NON_CONNECTABLE) ||
25 (connectable && len < BTRFS_FID_SIZE_CONNECTABLE))
26 return 255;
27
28 len = BTRFS_FID_SIZE_NON_CONNECTABLE;
29 type = FILEID_BTRFS_WITHOUT_PARENT;
30
33345d01 31 fid->objectid = btrfs_ino(inode);
be6e8dc0
BR
32 fid->root_objectid = BTRFS_I(inode)->root->objectid;
33 fid->gen = inode->i_generation;
34
35 if (connectable && !S_ISDIR(inode->i_mode)) {
36 struct inode *parent;
37 u64 parent_root_id;
38
39 spin_lock(&dentry->d_lock);
40
41 parent = dentry->d_parent->d_inode;
42 fid->parent_objectid = BTRFS_I(parent)->location.objectid;
43 fid->parent_gen = parent->i_generation;
44 parent_root_id = BTRFS_I(parent)->root->objectid;
45
46 spin_unlock(&dentry->d_lock);
47
48 if (parent_root_id != fid->root_objectid) {
49 fid->parent_root_objectid = parent_root_id;
50 len = BTRFS_FID_SIZE_CONNECTABLE_ROOT;
51 type = FILEID_BTRFS_WITH_PARENT_ROOT;
52 } else {
53 len = BTRFS_FID_SIZE_CONNECTABLE;
54 type = FILEID_BTRFS_WITH_PARENT;
55 }
56 }
57
58 *max_len = len;
59 return type;
60}
61
62static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
76dda93c
YZ
63 u64 root_objectid, u32 generation,
64 int check_generation)
be6e8dc0 65{
76dda93c 66 struct btrfs_fs_info *fs_info = btrfs_sb(sb)->fs_info;
be6e8dc0
BR
67 struct btrfs_root *root;
68 struct inode *inode;
be6e8dc0 69 struct btrfs_key key;
76dda93c
YZ
70 int index;
71 int err = 0;
72
73 if (objectid < BTRFS_FIRST_FREE_OBJECTID)
74 return ERR_PTR(-ESTALE);
be6e8dc0 75
2d4d9fbd
DW
76 key.objectid = root_objectid;
77 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
78 key.offset = (u64)-1;
79
76dda93c
YZ
80 index = srcu_read_lock(&fs_info->subvol_srcu);
81
82 root = btrfs_read_fs_root_no_name(fs_info, &key);
83 if (IS_ERR(root)) {
84 err = PTR_ERR(root);
85 goto fail;
86 }
87
88 if (btrfs_root_refs(&root->root_item) == 0) {
89 err = -ENOENT;
90 goto fail;
91 }
2d4d9fbd 92
be6e8dc0
BR
93 key.objectid = objectid;
94 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
95 key.offset = 0;
96
73f73415 97 inode = btrfs_iget(sb, &key, root, NULL);
76dda93c
YZ
98 if (IS_ERR(inode)) {
99 err = PTR_ERR(inode);
100 goto fail;
101 }
102
103 srcu_read_unlock(&fs_info->subvol_srcu, index);
be6e8dc0 104
76dda93c 105 if (check_generation && generation != inode->i_generation) {
be6e8dc0
BR
106 iput(inode);
107 return ERR_PTR(-ESTALE);
108 }
109
af53d29a 110 return d_obtain_alias(inode);
76dda93c
YZ
111fail:
112 srcu_read_unlock(&fs_info->subvol_srcu, index);
113 return ERR_PTR(err);
be6e8dc0
BR
114}
115
116static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
117 int fh_len, int fh_type)
118{
119 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
120 u64 objectid, root_objectid;
121 u32 generation;
122
123 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
124 if (fh_len != BTRFS_FID_SIZE_CONNECTABLE)
125 return NULL;
126 root_objectid = fid->root_objectid;
127 } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
128 if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT)
129 return NULL;
130 root_objectid = fid->parent_root_objectid;
131 } else
132 return NULL;
133
134 objectid = fid->parent_objectid;
135 generation = fid->parent_gen;
136
76dda93c 137 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
be6e8dc0
BR
138}
139
140static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
141 int fh_len, int fh_type)
142{
143 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
144 u64 objectid, root_objectid;
145 u32 generation;
146
147 if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
148 fh_len != BTRFS_FID_SIZE_CONNECTABLE) &&
149 (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
150 fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
151 (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
152 fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE))
153 return NULL;
154
155 objectid = fid->objectid;
156 root_objectid = fid->root_objectid;
157 generation = fid->gen;
158
76dda93c 159 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
be6e8dc0
BR
160}
161
162static struct dentry *btrfs_get_parent(struct dentry *child)
163{
164 struct inode *dir = child->d_inode;
be6e8dc0 165 struct btrfs_root *root = BTRFS_I(dir)->root;
be6e8dc0
BR
166 struct btrfs_path *path;
167 struct extent_buffer *leaf;
76dda93c
YZ
168 struct btrfs_root_ref *ref;
169 struct btrfs_key key;
170 struct btrfs_key found_key;
be6e8dc0
BR
171 int ret;
172
173 path = btrfs_alloc_path();
2a29edc6 174 if (!path)
175 return ERR_PTR(-ENOMEM);
be6e8dc0 176
33345d01 177 if (btrfs_ino(dir) == BTRFS_FIRST_FREE_OBJECTID) {
76dda93c
YZ
178 key.objectid = root->root_key.objectid;
179 key.type = BTRFS_ROOT_BACKREF_KEY;
180 key.offset = (u64)-1;
181 root = root->fs_info->tree_root;
182 } else {
33345d01 183 key.objectid = btrfs_ino(dir);
76dda93c
YZ
184 key.type = BTRFS_INODE_REF_KEY;
185 key.offset = (u64)-1;
186 }
be6e8dc0 187
87acb4ef 188 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
76dda93c
YZ
189 if (ret < 0)
190 goto fail;
191
192 BUG_ON(ret == 0);
193 if (path->slots[0] == 0) {
194 ret = -ENOENT;
195 goto fail;
d54a8390 196 }
76dda93c
YZ
197
198 path->slots[0]--;
be6e8dc0 199 leaf = path->nodes[0];
76dda93c
YZ
200
201 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
202 if (found_key.objectid != key.objectid || found_key.type != key.type) {
203 ret = -ENOENT;
204 goto fail;
be6e8dc0
BR
205 }
206
76dda93c
YZ
207 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
208 ref = btrfs_item_ptr(leaf, path->slots[0],
209 struct btrfs_root_ref);
210 key.objectid = btrfs_root_ref_dirid(leaf, ref);
211 } else {
212 key.objectid = found_key.offset;
213 }
be6e8dc0
BR
214 btrfs_free_path(path);
215
76dda93c
YZ
216 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
217 return btrfs_get_dentry(root->fs_info->sb, key.objectid,
218 found_key.offset, 0, 0);
219 }
2d4d9fbd 220
76dda93c 221 key.type = BTRFS_INODE_ITEM_KEY;
be6e8dc0 222 key.offset = 0;
af53d29a 223 return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL));
76dda93c
YZ
224fail:
225 btrfs_free_path(path);
226 return ERR_PTR(ret);
be6e8dc0
BR
227}
228
2ede0daf
JB
229static int btrfs_get_name(struct dentry *parent, char *name,
230 struct dentry *child)
231{
232 struct inode *inode = child->d_inode;
233 struct inode *dir = parent->d_inode;
234 struct btrfs_path *path;
235 struct btrfs_root *root = BTRFS_I(dir)->root;
236 struct btrfs_inode_ref *iref;
237 struct btrfs_root_ref *rref;
238 struct extent_buffer *leaf;
239 unsigned long name_ptr;
240 struct btrfs_key key;
241 int name_len;
242 int ret;
33345d01 243 u64 ino;
2ede0daf
JB
244
245 if (!dir || !inode)
246 return -EINVAL;
247
248 if (!S_ISDIR(dir->i_mode))
249 return -EINVAL;
250
33345d01
LZ
251 ino = btrfs_ino(inode);
252
2ede0daf
JB
253 path = btrfs_alloc_path();
254 if (!path)
255 return -ENOMEM;
256 path->leave_spinning = 1;
257
33345d01 258 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
2ede0daf
JB
259 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
260 key.type = BTRFS_ROOT_BACKREF_KEY;
261 key.offset = (u64)-1;
262 root = root->fs_info->tree_root;
263 } else {
33345d01
LZ
264 key.objectid = ino;
265 key.offset = btrfs_ino(dir);
2ede0daf
JB
266 key.type = BTRFS_INODE_REF_KEY;
267 }
268
269 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
270 if (ret < 0) {
271 btrfs_free_path(path);
272 return ret;
273 } else if (ret > 0) {
33345d01 274 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
2ede0daf
JB
275 path->slots[0]--;
276 } else {
277 btrfs_free_path(path);
278 return -ENOENT;
279 }
280 }
281 leaf = path->nodes[0];
282
33345d01
LZ
283 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
284 rref = btrfs_item_ptr(leaf, path->slots[0],
2ede0daf 285 struct btrfs_root_ref);
33345d01
LZ
286 name_ptr = (unsigned long)(rref + 1);
287 name_len = btrfs_root_ref_name_len(leaf, rref);
2ede0daf
JB
288 } else {
289 iref = btrfs_item_ptr(leaf, path->slots[0],
290 struct btrfs_inode_ref);
291 name_ptr = (unsigned long)(iref + 1);
292 name_len = btrfs_inode_ref_name_len(leaf, iref);
293 }
294
295 read_extent_buffer(leaf, name, name_ptr, name_len);
296 btrfs_free_path(path);
297
298 /*
299 * have to add the null termination to make sure that reconnect_path
300 * gets the right len for strlen
301 */
302 name[name_len] = '\0';
303
304 return 0;
305}
306
be6e8dc0
BR
307const struct export_operations btrfs_export_ops = {
308 .encode_fh = btrfs_encode_fh,
309 .fh_to_dentry = btrfs_fh_to_dentry,
310 .fh_to_parent = btrfs_fh_to_parent,
311 .get_parent = btrfs_get_parent,
2ede0daf 312 .get_name = btrfs_get_name,
be6e8dc0 313};