Merge tag 'tag-chrome-platform-for-v5.7' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / fs / btrfs / export.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
c1d7c514 2
be6e8dc0
BR
3#include <linux/fs.h>
4#include <linux/types.h>
5#include "ctree.h"
6#include "disk-io.h"
7#include "btrfs_inode.h"
8#include "print-tree.h"
9#include "export.h"
be6e8dc0 10
d397712b
CM
11#define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \
12 parent_objectid) / 4)
13#define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, \
14 parent_root_objectid) / 4)
15#define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid) / 4)
be6e8dc0 16
b0b0382b
AV
17static int btrfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
18 struct inode *parent)
be6e8dc0
BR
19{
20 struct btrfs_fid *fid = (struct btrfs_fid *)fh;
be6e8dc0
BR
21 int len = *max_len;
22 int type;
23
b0b0382b 24 if (parent && (len < BTRFS_FID_SIZE_CONNECTABLE)) {
5fe0c237 25 *max_len = BTRFS_FID_SIZE_CONNECTABLE;
94e07a75 26 return FILEID_INVALID;
5fe0c237
AK
27 } else if (len < BTRFS_FID_SIZE_NON_CONNECTABLE) {
28 *max_len = BTRFS_FID_SIZE_NON_CONNECTABLE;
94e07a75 29 return FILEID_INVALID;
5fe0c237 30 }
be6e8dc0
BR
31
32 len = BTRFS_FID_SIZE_NON_CONNECTABLE;
33 type = FILEID_BTRFS_WITHOUT_PARENT;
34
4a0cc7ca 35 fid->objectid = btrfs_ino(BTRFS_I(inode));
4fd786e6 36 fid->root_objectid = BTRFS_I(inode)->root->root_key.objectid;
be6e8dc0
BR
37 fid->gen = inode->i_generation;
38
b0b0382b 39 if (parent) {
be6e8dc0
BR
40 u64 parent_root_id;
41
be6e8dc0
BR
42 fid->parent_objectid = BTRFS_I(parent)->location.objectid;
43 fid->parent_gen = parent->i_generation;
4fd786e6 44 parent_root_id = BTRFS_I(parent)->root->root_key.objectid;
be6e8dc0 45
be6e8dc0
BR
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
c0c907a4
MPS
60struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
61 u64 root_objectid, u32 generation,
62 int check_generation)
be6e8dc0 63{
815745cf 64 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
be6e8dc0
BR
65 struct btrfs_root *root;
66 struct inode *inode;
be6e8dc0 67 struct btrfs_key key;
76dda93c
YZ
68
69 if (objectid < BTRFS_FIRST_FREE_OBJECTID)
70 return ERR_PTR(-ESTALE);
be6e8dc0 71
2d4d9fbd 72 key.objectid = root_objectid;
962a298f 73 key.type = BTRFS_ROOT_ITEM_KEY;
2d4d9fbd
DW
74 key.offset = (u64)-1;
75
3619c94f 76 root = btrfs_get_fs_root(fs_info, &key, true);
c75e8394
JB
77 if (IS_ERR(root))
78 return ERR_CAST(root);
76dda93c 79
be6e8dc0 80 key.objectid = objectid;
962a298f 81 key.type = BTRFS_INODE_ITEM_KEY;
be6e8dc0
BR
82 key.offset = 0;
83
4c66e0d4 84 inode = btrfs_iget(sb, &key, root);
00246528 85 btrfs_put_root(root);
c75e8394
JB
86 if (IS_ERR(inode))
87 return ERR_CAST(inode);
be6e8dc0 88
76dda93c 89 if (check_generation && generation != inode->i_generation) {
be6e8dc0
BR
90 iput(inode);
91 return ERR_PTR(-ESTALE);
92 }
93
af53d29a 94 return d_obtain_alias(inode);
be6e8dc0
BR
95}
96
97static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
98 int fh_len, int fh_type)
99{
100 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
101 u64 objectid, root_objectid;
102 u32 generation;
103
104 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
7d35199e 105 if (fh_len < BTRFS_FID_SIZE_CONNECTABLE)
be6e8dc0
BR
106 return NULL;
107 root_objectid = fid->root_objectid;
108 } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
7d35199e 109 if (fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT)
be6e8dc0
BR
110 return NULL;
111 root_objectid = fid->parent_root_objectid;
112 } else
113 return NULL;
114
115 objectid = fid->parent_objectid;
116 generation = fid->parent_gen;
117
76dda93c 118 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
be6e8dc0
BR
119}
120
121static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
122 int fh_len, int fh_type)
123{
124 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
125 u64 objectid, root_objectid;
126 u32 generation;
127
128 if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
7d35199e 129 fh_len < BTRFS_FID_SIZE_CONNECTABLE) &&
be6e8dc0 130 (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
7d35199e 131 fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
be6e8dc0 132 (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
7d35199e 133 fh_len < BTRFS_FID_SIZE_NON_CONNECTABLE))
be6e8dc0
BR
134 return NULL;
135
136 objectid = fid->objectid;
137 root_objectid = fid->root_objectid;
138 generation = fid->gen;
139
76dda93c 140 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
be6e8dc0
BR
141}
142
c0c907a4 143struct dentry *btrfs_get_parent(struct dentry *child)
be6e8dc0 144{
2b0143b5 145 struct inode *dir = d_inode(child);
0b246afa 146 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
be6e8dc0 147 struct btrfs_root *root = BTRFS_I(dir)->root;
be6e8dc0
BR
148 struct btrfs_path *path;
149 struct extent_buffer *leaf;
76dda93c
YZ
150 struct btrfs_root_ref *ref;
151 struct btrfs_key key;
152 struct btrfs_key found_key;
be6e8dc0
BR
153 int ret;
154
155 path = btrfs_alloc_path();
2a29edc6 156 if (!path)
157 return ERR_PTR(-ENOMEM);
be6e8dc0 158
4a0cc7ca 159 if (btrfs_ino(BTRFS_I(dir)) == BTRFS_FIRST_FREE_OBJECTID) {
76dda93c
YZ
160 key.objectid = root->root_key.objectid;
161 key.type = BTRFS_ROOT_BACKREF_KEY;
162 key.offset = (u64)-1;
0b246afa 163 root = fs_info->tree_root;
76dda93c 164 } else {
4a0cc7ca 165 key.objectid = btrfs_ino(BTRFS_I(dir));
76dda93c
YZ
166 key.type = BTRFS_INODE_REF_KEY;
167 key.offset = (u64)-1;
168 }
be6e8dc0 169
87acb4ef 170 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
76dda93c
YZ
171 if (ret < 0)
172 goto fail;
173
79787eaa 174 BUG_ON(ret == 0); /* Key with offset of -1 found */
76dda93c
YZ
175 if (path->slots[0] == 0) {
176 ret = -ENOENT;
177 goto fail;
d54a8390 178 }
76dda93c
YZ
179
180 path->slots[0]--;
be6e8dc0 181 leaf = path->nodes[0];
76dda93c
YZ
182
183 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
184 if (found_key.objectid != key.objectid || found_key.type != key.type) {
185 ret = -ENOENT;
186 goto fail;
be6e8dc0
BR
187 }
188
76dda93c
YZ
189 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
190 ref = btrfs_item_ptr(leaf, path->slots[0],
191 struct btrfs_root_ref);
192 key.objectid = btrfs_root_ref_dirid(leaf, ref);
193 } else {
194 key.objectid = found_key.offset;
195 }
be6e8dc0
BR
196 btrfs_free_path(path);
197
76dda93c 198 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
0b246afa 199 return btrfs_get_dentry(fs_info->sb, key.objectid,
76dda93c
YZ
200 found_key.offset, 0, 0);
201 }
2d4d9fbd 202
76dda93c 203 key.type = BTRFS_INODE_ITEM_KEY;
be6e8dc0 204 key.offset = 0;
4c66e0d4 205 return d_obtain_alias(btrfs_iget(fs_info->sb, &key, root));
76dda93c
YZ
206fail:
207 btrfs_free_path(path);
208 return ERR_PTR(ret);
be6e8dc0
BR
209}
210
2ede0daf
JB
211static int btrfs_get_name(struct dentry *parent, char *name,
212 struct dentry *child)
213{
2b0143b5
DH
214 struct inode *inode = d_inode(child);
215 struct inode *dir = d_inode(parent);
0b246afa 216 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2ede0daf
JB
217 struct btrfs_path *path;
218 struct btrfs_root *root = BTRFS_I(dir)->root;
219 struct btrfs_inode_ref *iref;
220 struct btrfs_root_ref *rref;
221 struct extent_buffer *leaf;
222 unsigned long name_ptr;
223 struct btrfs_key key;
224 int name_len;
225 int ret;
33345d01 226 u64 ino;
2ede0daf 227
2ede0daf
JB
228 if (!S_ISDIR(dir->i_mode))
229 return -EINVAL;
230
4a0cc7ca 231 ino = btrfs_ino(BTRFS_I(inode));
33345d01 232
2ede0daf
JB
233 path = btrfs_alloc_path();
234 if (!path)
235 return -ENOMEM;
236 path->leave_spinning = 1;
237
33345d01 238 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
2ede0daf
JB
239 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
240 key.type = BTRFS_ROOT_BACKREF_KEY;
241 key.offset = (u64)-1;
0b246afa 242 root = fs_info->tree_root;
2ede0daf 243 } else {
33345d01 244 key.objectid = ino;
4a0cc7ca 245 key.offset = btrfs_ino(BTRFS_I(dir));
2ede0daf
JB
246 key.type = BTRFS_INODE_REF_KEY;
247 }
248
249 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
250 if (ret < 0) {
251 btrfs_free_path(path);
252 return ret;
253 } else if (ret > 0) {
33345d01 254 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
2ede0daf
JB
255 path->slots[0]--;
256 } else {
257 btrfs_free_path(path);
258 return -ENOENT;
259 }
260 }
261 leaf = path->nodes[0];
262
33345d01
LZ
263 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
264 rref = btrfs_item_ptr(leaf, path->slots[0],
2ede0daf 265 struct btrfs_root_ref);
33345d01
LZ
266 name_ptr = (unsigned long)(rref + 1);
267 name_len = btrfs_root_ref_name_len(leaf, rref);
2ede0daf
JB
268 } else {
269 iref = btrfs_item_ptr(leaf, path->slots[0],
270 struct btrfs_inode_ref);
271 name_ptr = (unsigned long)(iref + 1);
272 name_len = btrfs_inode_ref_name_len(leaf, iref);
273 }
274
275 read_extent_buffer(leaf, name, name_ptr, name_len);
276 btrfs_free_path(path);
277
278 /*
279 * have to add the null termination to make sure that reconnect_path
280 * gets the right len for strlen
281 */
282 name[name_len] = '\0';
283
284 return 0;
285}
286
be6e8dc0
BR
287const struct export_operations btrfs_export_ops = {
288 .encode_fh = btrfs_encode_fh,
289 .fh_to_dentry = btrfs_fh_to_dentry,
290 .fh_to_parent = btrfs_fh_to_parent,
291 .get_parent = btrfs_get_parent,
2ede0daf 292 .get_name = btrfs_get_name,
be6e8dc0 293};