2 #include <linux/types.h>
5 #include "btrfs_inode.h"
6 #include "print-tree.h"
10 #define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \
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)
16 static int btrfs_encode_fh(struct dentry
*dentry
, u32
*fh
, int *max_len
,
19 struct btrfs_fid
*fid
= (struct btrfs_fid
*)fh
;
20 struct inode
*inode
= dentry
->d_inode
;
24 if ((len
< BTRFS_FID_SIZE_NON_CONNECTABLE
) ||
25 (connectable
&& len
< BTRFS_FID_SIZE_CONNECTABLE
))
28 len
= BTRFS_FID_SIZE_NON_CONNECTABLE
;
29 type
= FILEID_BTRFS_WITHOUT_PARENT
;
31 fid
->objectid
= inode
->i_ino
;
32 fid
->root_objectid
= BTRFS_I(inode
)->root
->objectid
;
33 fid
->gen
= inode
->i_generation
;
35 if (connectable
&& !S_ISDIR(inode
->i_mode
)) {
39 spin_lock(&dentry
->d_lock
);
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
;
46 spin_unlock(&dentry
->d_lock
);
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
;
53 len
= BTRFS_FID_SIZE_CONNECTABLE
;
54 type
= FILEID_BTRFS_WITH_PARENT
;
62 static struct dentry
*btrfs_get_dentry(struct super_block
*sb
, u64 objectid
,
63 u64 root_objectid
, u32 generation
,
66 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
)->fs_info
;
67 struct btrfs_root
*root
;
73 if (objectid
< BTRFS_FIRST_FREE_OBJECTID
)
74 return ERR_PTR(-ESTALE
);
76 key
.objectid
= root_objectid
;
77 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
80 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
82 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
88 if (btrfs_root_refs(&root
->root_item
) == 0) {
93 key
.objectid
= objectid
;
94 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
97 inode
= btrfs_iget(sb
, &key
, root
, NULL
);
103 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
105 if (check_generation
&& generation
!= inode
->i_generation
) {
107 return ERR_PTR(-ESTALE
);
110 return d_obtain_alias(inode
);
112 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
116 static struct dentry
*btrfs_fh_to_parent(struct super_block
*sb
, struct fid
*fh
,
117 int fh_len
, int fh_type
)
119 struct btrfs_fid
*fid
= (struct btrfs_fid
*) fh
;
120 u64 objectid
, root_objectid
;
123 if (fh_type
== FILEID_BTRFS_WITH_PARENT
) {
124 if (fh_len
!= BTRFS_FID_SIZE_CONNECTABLE
)
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
)
130 root_objectid
= fid
->parent_root_objectid
;
134 objectid
= fid
->parent_objectid
;
135 generation
= fid
->parent_gen
;
137 return btrfs_get_dentry(sb
, objectid
, root_objectid
, generation
, 1);
140 static struct dentry
*btrfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
141 int fh_len
, int fh_type
)
143 struct btrfs_fid
*fid
= (struct btrfs_fid
*) fh
;
144 u64 objectid
, root_objectid
;
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
))
155 objectid
= fid
->objectid
;
156 root_objectid
= fid
->root_objectid
;
157 generation
= fid
->gen
;
159 return btrfs_get_dentry(sb
, objectid
, root_objectid
, generation
, 1);
162 static struct dentry
*btrfs_get_parent(struct dentry
*child
)
164 struct inode
*dir
= child
->d_inode
;
165 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
166 struct btrfs_path
*path
;
167 struct extent_buffer
*leaf
;
168 struct btrfs_root_ref
*ref
;
169 struct btrfs_key key
;
170 struct btrfs_key found_key
;
173 path
= btrfs_alloc_path();
175 if (dir
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
176 key
.objectid
= root
->root_key
.objectid
;
177 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
178 key
.offset
= (u64
)-1;
179 root
= root
->fs_info
->tree_root
;
181 key
.objectid
= dir
->i_ino
;
182 key
.type
= BTRFS_INODE_REF_KEY
;
183 key
.offset
= (u64
)-1;
186 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
191 if (path
->slots
[0] == 0) {
197 leaf
= path
->nodes
[0];
199 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
200 if (found_key
.objectid
!= key
.objectid
|| found_key
.type
!= key
.type
) {
205 if (found_key
.type
== BTRFS_ROOT_BACKREF_KEY
) {
206 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
207 struct btrfs_root_ref
);
208 key
.objectid
= btrfs_root_ref_dirid(leaf
, ref
);
210 key
.objectid
= found_key
.offset
;
212 btrfs_free_path(path
);
214 if (found_key
.type
== BTRFS_ROOT_BACKREF_KEY
) {
215 return btrfs_get_dentry(root
->fs_info
->sb
, key
.objectid
,
216 found_key
.offset
, 0, 0);
219 key
.type
= BTRFS_INODE_ITEM_KEY
;
221 return d_obtain_alias(btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
));
223 btrfs_free_path(path
);
227 static int btrfs_get_name(struct dentry
*parent
, char *name
,
228 struct dentry
*child
)
230 struct inode
*inode
= child
->d_inode
;
231 struct inode
*dir
= parent
->d_inode
;
232 struct btrfs_path
*path
;
233 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
234 struct btrfs_inode_ref
*iref
;
235 struct btrfs_root_ref
*rref
;
236 struct extent_buffer
*leaf
;
237 unsigned long name_ptr
;
238 struct btrfs_key key
;
245 if (!S_ISDIR(dir
->i_mode
))
248 path
= btrfs_alloc_path();
251 path
->leave_spinning
= 1;
253 if (inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
254 key
.objectid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
255 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
256 key
.offset
= (u64
)-1;
257 root
= root
->fs_info
->tree_root
;
259 key
.objectid
= inode
->i_ino
;
260 key
.offset
= dir
->i_ino
;
261 key
.type
= BTRFS_INODE_REF_KEY
;
264 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
266 btrfs_free_path(path
);
268 } else if (ret
> 0) {
269 if (inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
272 btrfs_free_path(path
);
276 leaf
= path
->nodes
[0];
278 if (inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
279 rref
= btrfs_item_ptr(leaf
, path
->slots
[0],
280 struct btrfs_root_ref
);
281 name_ptr
= (unsigned long)(rref
+ 1);
282 name_len
= btrfs_root_ref_name_len(leaf
, rref
);
284 iref
= btrfs_item_ptr(leaf
, path
->slots
[0],
285 struct btrfs_inode_ref
);
286 name_ptr
= (unsigned long)(iref
+ 1);
287 name_len
= btrfs_inode_ref_name_len(leaf
, iref
);
290 read_extent_buffer(leaf
, name
, name_ptr
, name_len
);
291 btrfs_free_path(path
);
294 * have to add the null termination to make sure that reconnect_path
295 * gets the right len for strlen
297 name
[name_len
] = '\0';
302 const struct export_operations btrfs_export_ops
= {
303 .encode_fh
= btrfs_encode_fh
,
304 .fh_to_dentry
= btrfs_fh_to_dentry
,
305 .fh_to_parent
= btrfs_fh_to_parent
,
306 .get_parent
= btrfs_get_parent
,
307 .get_name
= btrfs_get_name
,