]> Git Repo - linux.git/blame - fs/btrfs/export.c
btrfs: make btrfs_cleanup_fs_roots use the radix tree lock
[linux.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 int index;
69 int err = 0;
70
71 if (objectid < BTRFS_FIRST_FREE_OBJECTID)
72 return ERR_PTR(-ESTALE);
be6e8dc0 73
2d4d9fbd 74 key.objectid = root_objectid;
962a298f 75 key.type = BTRFS_ROOT_ITEM_KEY;
2d4d9fbd
DW
76 key.offset = (u64)-1;
77
76dda93c
YZ
78 index = srcu_read_lock(&fs_info->subvol_srcu);
79
3619c94f 80 root = btrfs_get_fs_root(fs_info, &key, true);
76dda93c
YZ
81 if (IS_ERR(root)) {
82 err = PTR_ERR(root);
83 goto fail;
84 }
85
be6e8dc0 86 key.objectid = objectid;
962a298f 87 key.type = BTRFS_INODE_ITEM_KEY;
be6e8dc0
BR
88 key.offset = 0;
89
4c66e0d4 90 inode = btrfs_iget(sb, &key, root);
00246528 91 btrfs_put_root(root);
76dda93c
YZ
92 if (IS_ERR(inode)) {
93 err = PTR_ERR(inode);
94 goto fail;
95 }
96
97 srcu_read_unlock(&fs_info->subvol_srcu, index);
be6e8dc0 98
76dda93c 99 if (check_generation && generation != inode->i_generation) {
be6e8dc0
BR
100 iput(inode);
101 return ERR_PTR(-ESTALE);
102 }
103
af53d29a 104 return d_obtain_alias(inode);
76dda93c
YZ
105fail:
106 srcu_read_unlock(&fs_info->subvol_srcu, index);
107 return ERR_PTR(err);
be6e8dc0
BR
108}
109
110static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
111 int fh_len, int fh_type)
112{
113 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
114 u64 objectid, root_objectid;
115 u32 generation;
116
117 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
7d35199e 118 if (fh_len < BTRFS_FID_SIZE_CONNECTABLE)
be6e8dc0
BR
119 return NULL;
120 root_objectid = fid->root_objectid;
121 } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
7d35199e 122 if (fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT)
be6e8dc0
BR
123 return NULL;
124 root_objectid = fid->parent_root_objectid;
125 } else
126 return NULL;
127
128 objectid = fid->parent_objectid;
129 generation = fid->parent_gen;
130
76dda93c 131 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
be6e8dc0
BR
132}
133
134static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
135 int fh_len, int fh_type)
136{
137 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
138 u64 objectid, root_objectid;
139 u32 generation;
140
141 if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
7d35199e 142 fh_len < BTRFS_FID_SIZE_CONNECTABLE) &&
be6e8dc0 143 (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
7d35199e 144 fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
be6e8dc0 145 (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
7d35199e 146 fh_len < BTRFS_FID_SIZE_NON_CONNECTABLE))
be6e8dc0
BR
147 return NULL;
148
149 objectid = fid->objectid;
150 root_objectid = fid->root_objectid;
151 generation = fid->gen;
152
76dda93c 153 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
be6e8dc0
BR
154}
155
c0c907a4 156struct dentry *btrfs_get_parent(struct dentry *child)
be6e8dc0 157{
2b0143b5 158 struct inode *dir = d_inode(child);
0b246afa 159 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
be6e8dc0 160 struct btrfs_root *root = BTRFS_I(dir)->root;
be6e8dc0
BR
161 struct btrfs_path *path;
162 struct extent_buffer *leaf;
76dda93c
YZ
163 struct btrfs_root_ref *ref;
164 struct btrfs_key key;
165 struct btrfs_key found_key;
be6e8dc0
BR
166 int ret;
167
168 path = btrfs_alloc_path();
2a29edc6 169 if (!path)
170 return ERR_PTR(-ENOMEM);
be6e8dc0 171
4a0cc7ca 172 if (btrfs_ino(BTRFS_I(dir)) == BTRFS_FIRST_FREE_OBJECTID) {
76dda93c
YZ
173 key.objectid = root->root_key.objectid;
174 key.type = BTRFS_ROOT_BACKREF_KEY;
175 key.offset = (u64)-1;
0b246afa 176 root = fs_info->tree_root;
76dda93c 177 } else {
4a0cc7ca 178 key.objectid = btrfs_ino(BTRFS_I(dir));
76dda93c
YZ
179 key.type = BTRFS_INODE_REF_KEY;
180 key.offset = (u64)-1;
181 }
be6e8dc0 182
87acb4ef 183 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
76dda93c
YZ
184 if (ret < 0)
185 goto fail;
186
79787eaa 187 BUG_ON(ret == 0); /* Key with offset of -1 found */
76dda93c
YZ
188 if (path->slots[0] == 0) {
189 ret = -ENOENT;
190 goto fail;
d54a8390 191 }
76dda93c
YZ
192
193 path->slots[0]--;
be6e8dc0 194 leaf = path->nodes[0];
76dda93c
YZ
195
196 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
197 if (found_key.objectid != key.objectid || found_key.type != key.type) {
198 ret = -ENOENT;
199 goto fail;
be6e8dc0
BR
200 }
201
76dda93c
YZ
202 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
203 ref = btrfs_item_ptr(leaf, path->slots[0],
204 struct btrfs_root_ref);
205 key.objectid = btrfs_root_ref_dirid(leaf, ref);
206 } else {
207 key.objectid = found_key.offset;
208 }
be6e8dc0
BR
209 btrfs_free_path(path);
210
76dda93c 211 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
0b246afa 212 return btrfs_get_dentry(fs_info->sb, key.objectid,
76dda93c
YZ
213 found_key.offset, 0, 0);
214 }
2d4d9fbd 215
76dda93c 216 key.type = BTRFS_INODE_ITEM_KEY;
be6e8dc0 217 key.offset = 0;
4c66e0d4 218 return d_obtain_alias(btrfs_iget(fs_info->sb, &key, root));
76dda93c
YZ
219fail:
220 btrfs_free_path(path);
221 return ERR_PTR(ret);
be6e8dc0
BR
222}
223
2ede0daf
JB
224static int btrfs_get_name(struct dentry *parent, char *name,
225 struct dentry *child)
226{
2b0143b5
DH
227 struct inode *inode = d_inode(child);
228 struct inode *dir = d_inode(parent);
0b246afa 229 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2ede0daf
JB
230 struct btrfs_path *path;
231 struct btrfs_root *root = BTRFS_I(dir)->root;
232 struct btrfs_inode_ref *iref;
233 struct btrfs_root_ref *rref;
234 struct extent_buffer *leaf;
235 unsigned long name_ptr;
236 struct btrfs_key key;
237 int name_len;
238 int ret;
33345d01 239 u64 ino;
2ede0daf 240
2ede0daf
JB
241 if (!S_ISDIR(dir->i_mode))
242 return -EINVAL;
243
4a0cc7ca 244 ino = btrfs_ino(BTRFS_I(inode));
33345d01 245
2ede0daf
JB
246 path = btrfs_alloc_path();
247 if (!path)
248 return -ENOMEM;
249 path->leave_spinning = 1;
250
33345d01 251 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
2ede0daf
JB
252 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
253 key.type = BTRFS_ROOT_BACKREF_KEY;
254 key.offset = (u64)-1;
0b246afa 255 root = fs_info->tree_root;
2ede0daf 256 } else {
33345d01 257 key.objectid = ino;
4a0cc7ca 258 key.offset = btrfs_ino(BTRFS_I(dir));
2ede0daf
JB
259 key.type = BTRFS_INODE_REF_KEY;
260 }
261
262 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
263 if (ret < 0) {
264 btrfs_free_path(path);
265 return ret;
266 } else if (ret > 0) {
33345d01 267 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
2ede0daf
JB
268 path->slots[0]--;
269 } else {
270 btrfs_free_path(path);
271 return -ENOENT;
272 }
273 }
274 leaf = path->nodes[0];
275
33345d01
LZ
276 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
277 rref = btrfs_item_ptr(leaf, path->slots[0],
2ede0daf 278 struct btrfs_root_ref);
33345d01
LZ
279 name_ptr = (unsigned long)(rref + 1);
280 name_len = btrfs_root_ref_name_len(leaf, rref);
2ede0daf
JB
281 } else {
282 iref = btrfs_item_ptr(leaf, path->slots[0],
283 struct btrfs_inode_ref);
284 name_ptr = (unsigned long)(iref + 1);
285 name_len = btrfs_inode_ref_name_len(leaf, iref);
286 }
287
288 read_extent_buffer(leaf, name, name_ptr, name_len);
289 btrfs_free_path(path);
290
291 /*
292 * have to add the null termination to make sure that reconnect_path
293 * gets the right len for strlen
294 */
295 name[name_len] = '\0';
296
297 return 0;
298}
299
be6e8dc0
BR
300const struct export_operations btrfs_export_ops = {
301 .encode_fh = btrfs_encode_fh,
302 .fh_to_dentry = btrfs_fh_to_dentry,
303 .fh_to_parent = btrfs_fh_to_parent,
304 .get_parent = btrfs_get_parent,
2ede0daf 305 .get_name = btrfs_get_name,
be6e8dc0 306};
This page took 0.706151 seconds and 4 git commands to generate.