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
;
68 struct dentry
*dentry
;
74 if (objectid
< BTRFS_FIRST_FREE_OBJECTID
)
75 return ERR_PTR(-ESTALE
);
77 key
.objectid
= root_objectid
;
78 btrfs_set_key_type(&key
, BTRFS_ROOT_ITEM_KEY
);
81 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
83 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
89 if (btrfs_root_refs(&root
->root_item
) == 0) {
94 key
.objectid
= objectid
;
95 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
98 inode
= btrfs_iget(sb
, &key
, root
, NULL
);
100 err
= PTR_ERR(inode
);
104 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
106 if (check_generation
&& generation
!= inode
->i_generation
) {
108 return ERR_PTR(-ESTALE
);
111 dentry
= d_obtain_alias(inode
);
113 dentry
->d_op
= &btrfs_dentry_operations
;
116 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
120 static struct dentry
*btrfs_fh_to_parent(struct super_block
*sb
, struct fid
*fh
,
121 int fh_len
, int fh_type
)
123 struct btrfs_fid
*fid
= (struct btrfs_fid
*) fh
;
124 u64 objectid
, root_objectid
;
127 if (fh_type
== FILEID_BTRFS_WITH_PARENT
) {
128 if (fh_len
!= BTRFS_FID_SIZE_CONNECTABLE
)
130 root_objectid
= fid
->root_objectid
;
131 } else if (fh_type
== FILEID_BTRFS_WITH_PARENT_ROOT
) {
132 if (fh_len
!= BTRFS_FID_SIZE_CONNECTABLE_ROOT
)
134 root_objectid
= fid
->parent_root_objectid
;
138 objectid
= fid
->parent_objectid
;
139 generation
= fid
->parent_gen
;
141 return btrfs_get_dentry(sb
, objectid
, root_objectid
, generation
, 1);
144 static struct dentry
*btrfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
145 int fh_len
, int fh_type
)
147 struct btrfs_fid
*fid
= (struct btrfs_fid
*) fh
;
148 u64 objectid
, root_objectid
;
151 if ((fh_type
!= FILEID_BTRFS_WITH_PARENT
||
152 fh_len
!= BTRFS_FID_SIZE_CONNECTABLE
) &&
153 (fh_type
!= FILEID_BTRFS_WITH_PARENT_ROOT
||
154 fh_len
!= BTRFS_FID_SIZE_CONNECTABLE_ROOT
) &&
155 (fh_type
!= FILEID_BTRFS_WITHOUT_PARENT
||
156 fh_len
!= BTRFS_FID_SIZE_NON_CONNECTABLE
))
159 objectid
= fid
->objectid
;
160 root_objectid
= fid
->root_objectid
;
161 generation
= fid
->gen
;
163 return btrfs_get_dentry(sb
, objectid
, root_objectid
, generation
, 1);
166 static struct dentry
*btrfs_get_parent(struct dentry
*child
)
168 struct inode
*dir
= child
->d_inode
;
169 static struct dentry
*dentry
;
170 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
171 struct btrfs_path
*path
;
172 struct extent_buffer
*leaf
;
173 struct btrfs_root_ref
*ref
;
174 struct btrfs_key key
;
175 struct btrfs_key found_key
;
178 path
= btrfs_alloc_path();
180 return ERR_PTR(-ENOMEM
);
182 if (dir
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
183 key
.objectid
= root
->root_key
.objectid
;
184 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
185 key
.offset
= (u64
)-1;
186 root
= root
->fs_info
->tree_root
;
188 key
.objectid
= dir
->i_ino
;
189 key
.type
= BTRFS_INODE_REF_KEY
;
190 key
.offset
= (u64
)-1;
193 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
198 if (path
->slots
[0] == 0) {
204 leaf
= path
->nodes
[0];
206 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
207 if (found_key
.objectid
!= key
.objectid
|| found_key
.type
!= key
.type
) {
212 if (found_key
.type
== BTRFS_ROOT_BACKREF_KEY
) {
213 ref
= btrfs_item_ptr(leaf
, path
->slots
[0],
214 struct btrfs_root_ref
);
215 key
.objectid
= btrfs_root_ref_dirid(leaf
, ref
);
217 key
.objectid
= found_key
.offset
;
219 btrfs_free_path(path
);
221 if (found_key
.type
== BTRFS_ROOT_BACKREF_KEY
) {
222 return btrfs_get_dentry(root
->fs_info
->sb
, key
.objectid
,
223 found_key
.offset
, 0, 0);
226 key
.type
= BTRFS_INODE_ITEM_KEY
;
228 dentry
= d_obtain_alias(btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
));
230 dentry
->d_op
= &btrfs_dentry_operations
;
233 btrfs_free_path(path
);
237 static int btrfs_get_name(struct dentry
*parent
, char *name
,
238 struct dentry
*child
)
240 struct inode
*inode
= child
->d_inode
;
241 struct inode
*dir
= parent
->d_inode
;
242 struct btrfs_path
*path
;
243 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
244 struct btrfs_inode_ref
*iref
;
245 struct btrfs_root_ref
*rref
;
246 struct extent_buffer
*leaf
;
247 unsigned long name_ptr
;
248 struct btrfs_key key
;
255 if (!S_ISDIR(dir
->i_mode
))
258 path
= btrfs_alloc_path();
261 path
->leave_spinning
= 1;
263 if (inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
264 key
.objectid
= BTRFS_I(inode
)->root
->root_key
.objectid
;
265 key
.type
= BTRFS_ROOT_BACKREF_KEY
;
266 key
.offset
= (u64
)-1;
267 root
= root
->fs_info
->tree_root
;
269 key
.objectid
= inode
->i_ino
;
270 key
.offset
= dir
->i_ino
;
271 key
.type
= BTRFS_INODE_REF_KEY
;
274 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
276 btrfs_free_path(path
);
278 } else if (ret
> 0) {
279 if (inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
282 btrfs_free_path(path
);
286 leaf
= path
->nodes
[0];
288 if (inode
->i_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
289 rref
= btrfs_item_ptr(leaf
, path
->slots
[0],
290 struct btrfs_root_ref
);
291 name_ptr
= (unsigned long)(rref
+ 1);
292 name_len
= btrfs_root_ref_name_len(leaf
, rref
);
294 iref
= btrfs_item_ptr(leaf
, path
->slots
[0],
295 struct btrfs_inode_ref
);
296 name_ptr
= (unsigned long)(iref
+ 1);
297 name_len
= btrfs_inode_ref_name_len(leaf
, iref
);
300 read_extent_buffer(leaf
, name
, name_ptr
, name_len
);
301 btrfs_free_path(path
);
304 * have to add the null termination to make sure that reconnect_path
305 * gets the right len for strlen
307 name
[name_len
] = '\0';
312 const struct export_operations btrfs_export_ops
= {
313 .encode_fh
= btrfs_encode_fh
,
314 .fh_to_dentry
= btrfs_fh_to_dentry
,
315 .fh_to_parent
= btrfs_fh_to_parent
,
316 .get_parent
= btrfs_get_parent
,
317 .get_name
= btrfs_get_name
,