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