1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/types.h>
7 #include "btrfs_inode.h"
8 #include "print-tree.h"
11 #define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \
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)
17 static int btrfs_encode_fh(struct inode
*inode
, u32
*fh
, int *max_len
,
20 struct btrfs_fid
*fid
= (struct btrfs_fid
*)fh
;
24 if (parent
&& (len
< BTRFS_FID_SIZE_CONNECTABLE
)) {
25 *max_len
= BTRFS_FID_SIZE_CONNECTABLE
;
26 return FILEID_INVALID
;
27 } else if (len
< BTRFS_FID_SIZE_NON_CONNECTABLE
) {
28 *max_len
= BTRFS_FID_SIZE_NON_CONNECTABLE
;
29 return FILEID_INVALID
;
32 len
= BTRFS_FID_SIZE_NON_CONNECTABLE
;
33 type
= FILEID_BTRFS_WITHOUT_PARENT
;
35 fid
->objectid
= btrfs_ino(BTRFS_I(inode
));
36 fid
->root_objectid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
37 fid
->gen
= inode
->i_generation
;
42 fid
->parent_objectid
= BTRFS_I(parent
)->location
.objectid
;
43 fid
->parent_gen
= parent
->i_generation
;
44 parent_root_id
= BTRFS_I(parent
)->root
->root_key
.objectid
;
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
;
51 len
= BTRFS_FID_SIZE_CONNECTABLE
;
52 type
= FILEID_BTRFS_WITH_PARENT
;
60 struct dentry
*btrfs_get_dentry(struct super_block
*sb
, u64 objectid
,
61 u64 root_objectid
, u32 generation
,
64 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
65 struct btrfs_root
*root
;
69 if (objectid
< BTRFS_FIRST_FREE_OBJECTID
)
70 return ERR_PTR(-ESTALE
);
72 key
.objectid
= root_objectid
;
73 key
.type
= BTRFS_ROOT_ITEM_KEY
;
76 root
= btrfs_get_fs_root(fs_info
, &key
, true);
78 return ERR_CAST(root
);
80 key
.objectid
= objectid
;
81 key
.type
= BTRFS_INODE_ITEM_KEY
;
84 inode
= btrfs_iget(sb
, &key
, root
);
87 return ERR_CAST(inode
);
89 if (check_generation
&& generation
!= inode
->i_generation
) {
91 return ERR_PTR(-ESTALE
);
94 return d_obtain_alias(inode
);
97 static struct dentry
*btrfs_fh_to_parent(struct super_block
*sb
, struct fid
*fh
,
98 int fh_len
, int fh_type
)
100 struct btrfs_fid
*fid
= (struct btrfs_fid
*) fh
;
101 u64 objectid
, root_objectid
;
104 if (fh_type
== FILEID_BTRFS_WITH_PARENT
) {
105 if (fh_len
< BTRFS_FID_SIZE_CONNECTABLE
)
107 root_objectid
= fid
->root_objectid
;
108 } else if (fh_type
== FILEID_BTRFS_WITH_PARENT_ROOT
) {
109 if (fh_len
< BTRFS_FID_SIZE_CONNECTABLE_ROOT
)
111 root_objectid
= fid
->parent_root_objectid
;
115 objectid
= fid
->parent_objectid
;
116 generation
= fid
->parent_gen
;
118 return btrfs_get_dentry(sb
, objectid
, root_objectid
, generation
, 1);
121 static struct dentry
*btrfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
122 int fh_len
, int fh_type
)
124 struct btrfs_fid
*fid
= (struct btrfs_fid
*) fh
;
125 u64 objectid
, root_objectid
;
128 if ((fh_type
!= FILEID_BTRFS_WITH_PARENT
||
129 fh_len
< BTRFS_FID_SIZE_CONNECTABLE
) &&
130 (fh_type
!= FILEID_BTRFS_WITH_PARENT_ROOT
||
131 fh_len
< BTRFS_FID_SIZE_CONNECTABLE_ROOT
) &&
132 (fh_type
!= FILEID_BTRFS_WITHOUT_PARENT
||
133 fh_len
< BTRFS_FID_SIZE_NON_CONNECTABLE
))
136 objectid
= fid
->objectid
;
137 root_objectid
= fid
->root_objectid
;
138 generation
= fid
->gen
;
140 return btrfs_get_dentry(sb
, objectid
, root_objectid
, generation
, 1);
143 struct dentry
*btrfs_get_parent(struct dentry
*child
)
145 struct inode
*dir
= d_inode(child
);
146 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
147 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
148 struct btrfs_path
*path
;
149 struct extent_buffer
*leaf
;
150 struct btrfs_root_ref
*ref
;
151 struct btrfs_key key
;
152 struct btrfs_key found_key
;
155 path
= btrfs_alloc_path();
157 return ERR_PTR(-ENOMEM
);
159 if (btrfs_ino(BTRFS_I(dir
)) == BTRFS_FIRST_FREE_OBJECTID
) {
160 key
.objectid
= root
->root_key
.objectid
;
161 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
162 key
.offset
= (u64
)-1;
163 root
= fs_info
->tree_root
;
165 key
.objectid
= btrfs_ino(BTRFS_I(dir
));
166 key
.type
= BTRFS_INODE_REF_KEY
;
167 key
.offset
= (u64
)-1;
170 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
174 BUG_ON(ret
== 0); /* Key with offset of -1 found */
175 if (path
->slots
[0] == 0) {
181 leaf
= path
->nodes
[0];
183 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
184 if (found_key
.objectid
!= key
.objectid
|| found_key
.type
!= key
.type
) {
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
);
194 key
.objectid
= found_key
.offset
;
196 btrfs_free_path(path
);
198 if (found_key
.type
== BTRFS_ROOT_BACKREF_KEY
) {
199 return btrfs_get_dentry(fs_info
->sb
, key
.objectid
,
200 found_key
.offset
, 0, 0);
203 key
.type
= BTRFS_INODE_ITEM_KEY
;
205 return d_obtain_alias(btrfs_iget(fs_info
->sb
, &key
, root
));
207 btrfs_free_path(path
);
211 static int btrfs_get_name(struct dentry
*parent
, char *name
,
212 struct dentry
*child
)
214 struct inode
*inode
= d_inode(child
);
215 struct inode
*dir
= d_inode(parent
);
216 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
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
;
228 if (!S_ISDIR(dir
->i_mode
))
231 ino
= btrfs_ino(BTRFS_I(inode
));
233 path
= btrfs_alloc_path();
236 path
->leave_spinning
= 1;
238 if (ino
== BTRFS_FIRST_FREE_OBJECTID
) {
239 key
.objectid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
240 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
241 key
.offset
= (u64
)-1;
242 root
= fs_info
->tree_root
;
245 key
.offset
= btrfs_ino(BTRFS_I(dir
));
246 key
.type
= BTRFS_INODE_REF_KEY
;
249 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
251 btrfs_free_path(path
);
253 } else if (ret
> 0) {
254 if (ino
== BTRFS_FIRST_FREE_OBJECTID
) {
257 btrfs_free_path(path
);
261 leaf
= path
->nodes
[0];
263 if (ino
== BTRFS_FIRST_FREE_OBJECTID
) {
264 rref
= btrfs_item_ptr(leaf
, path
->slots
[0],
265 struct btrfs_root_ref
);
266 name_ptr
= (unsigned long)(rref
+ 1);
267 name_len
= btrfs_root_ref_name_len(leaf
, rref
);
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
);
275 read_extent_buffer(leaf
, name
, name_ptr
, name_len
);
276 btrfs_free_path(path
);
279 * have to add the null termination to make sure that reconnect_path
280 * gets the right len for strlen
282 name
[name_len
] = '\0';
287 const 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
,
292 .get_name
= btrfs_get_name
,