2 #include <linux/types.h>
5 #include "btrfs_inode.h"
6 #include "print-tree.h"
10 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
11 #define FILEID_BTRFS_WITHOUT_PARENT 0x4d
12 #define FILEID_BTRFS_WITH_PARENT 0x4e
13 #define FILEID_BTRFS_WITH_PARENT_ROOT 0x4f
16 #define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, parent_objectid)/4)
17 #define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, parent_root_objectid)/4)
18 #define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid)/4)
20 static int btrfs_encode_fh(struct dentry
*dentry
, u32
*fh
, int *max_len
,
23 struct btrfs_fid
*fid
= (struct btrfs_fid
*)fh
;
24 struct inode
*inode
= dentry
->d_inode
;
28 if ((len
< BTRFS_FID_SIZE_NON_CONNECTABLE
) ||
29 (connectable
&& len
< BTRFS_FID_SIZE_CONNECTABLE
))
32 len
= BTRFS_FID_SIZE_NON_CONNECTABLE
;
33 type
= FILEID_BTRFS_WITHOUT_PARENT
;
35 fid
->objectid
= BTRFS_I(inode
)->location
.objectid
;
36 fid
->root_objectid
= BTRFS_I(inode
)->root
->objectid
;
37 fid
->gen
= inode
->i_generation
;
39 if (connectable
&& !S_ISDIR(inode
->i_mode
)) {
43 spin_lock(&dentry
->d_lock
);
45 parent
= dentry
->d_parent
->d_inode
;
46 fid
->parent_objectid
= BTRFS_I(parent
)->location
.objectid
;
47 fid
->parent_gen
= parent
->i_generation
;
48 parent_root_id
= BTRFS_I(parent
)->root
->objectid
;
50 spin_unlock(&dentry
->d_lock
);
52 if (parent_root_id
!= fid
->root_objectid
) {
53 fid
->parent_root_objectid
= parent_root_id
;
54 len
= BTRFS_FID_SIZE_CONNECTABLE_ROOT
;
55 type
= FILEID_BTRFS_WITH_PARENT_ROOT
;
57 len
= BTRFS_FID_SIZE_CONNECTABLE
;
58 type
= FILEID_BTRFS_WITH_PARENT
;
66 static struct dentry
*btrfs_get_dentry(struct super_block
*sb
, u64 objectid
,
67 u64 root_objectid
, u32 generation
)
69 struct btrfs_root
*root
;
71 struct dentry
*result
;
74 key
.objectid
= objectid
;
75 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
78 root
= btrfs_lookup_fs_root(btrfs_sb(sb
)->fs_info
, root_objectid
);
79 inode
= btrfs_iget(sb
, &key
, root
, NULL
);
83 if (generation
!= inode
->i_generation
) {
85 return ERR_PTR(-ESTALE
);
88 result
= d_obtain_alias(inode
);
90 return ERR_PTR(-ENOMEM
);
95 static struct dentry
*btrfs_fh_to_parent(struct super_block
*sb
, struct fid
*fh
,
96 int fh_len
, int fh_type
)
98 struct btrfs_fid
*fid
= (struct btrfs_fid
*) fh
;
99 u64 objectid
, root_objectid
;
102 if (fh_type
== FILEID_BTRFS_WITH_PARENT
) {
103 if (fh_len
!= BTRFS_FID_SIZE_CONNECTABLE
)
105 root_objectid
= fid
->root_objectid
;
106 } else if (fh_type
== FILEID_BTRFS_WITH_PARENT_ROOT
) {
107 if (fh_len
!= BTRFS_FID_SIZE_CONNECTABLE_ROOT
)
109 root_objectid
= fid
->parent_root_objectid
;
113 objectid
= fid
->parent_objectid
;
114 generation
= fid
->parent_gen
;
116 return btrfs_get_dentry(sb
, objectid
, root_objectid
, generation
);
119 static struct dentry
*btrfs_fh_to_dentry(struct super_block
*sb
, struct fid
*fh
,
120 int fh_len
, int fh_type
)
122 struct btrfs_fid
*fid
= (struct btrfs_fid
*) fh
;
123 u64 objectid
, root_objectid
;
126 if ((fh_type
!= FILEID_BTRFS_WITH_PARENT
||
127 fh_len
!= BTRFS_FID_SIZE_CONNECTABLE
) &&
128 (fh_type
!= FILEID_BTRFS_WITH_PARENT_ROOT
||
129 fh_len
!= BTRFS_FID_SIZE_CONNECTABLE_ROOT
) &&
130 (fh_type
!= FILEID_BTRFS_WITHOUT_PARENT
||
131 fh_len
!= BTRFS_FID_SIZE_NON_CONNECTABLE
))
134 objectid
= fid
->objectid
;
135 root_objectid
= fid
->root_objectid
;
136 generation
= fid
->gen
;
138 return btrfs_get_dentry(sb
, objectid
, root_objectid
, generation
);
141 static struct dentry
*btrfs_get_parent(struct dentry
*child
)
143 struct inode
*dir
= child
->d_inode
;
145 struct dentry
*parent
;
146 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
147 struct btrfs_key key
;
148 struct btrfs_path
*path
;
149 struct extent_buffer
*leaf
;
154 path
= btrfs_alloc_path();
156 key
.objectid
= dir
->i_ino
;
157 btrfs_set_key_type(&key
, BTRFS_INODE_REF_KEY
);
158 key
.offset
= (u64
)-1;
160 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
161 leaf
= path
->nodes
[0];
162 slot
= path
->slots
[0];
163 if (ret
< 0 || slot
== 0) {
164 btrfs_free_path(path
);
167 /* btrfs_search_slot() returns the slot where we'd want to insert
168 an INODE_REF_KEY for parent inode #0xFFFFFFFFFFFFFFFF. The _real_
169 one, telling us what the parent inode _actually_ is, will be in
170 the slot _before_ the one that btrfs_search_slot() returns. */
173 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
174 btrfs_free_path(path
);
176 if (key
.objectid
!= dir
->i_ino
|| key
.type
!= BTRFS_INODE_REF_KEY
)
179 objectid
= key
.offset
;
181 /* Build a new key for the inode item */
182 key
.objectid
= objectid
;
183 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
186 inode
= btrfs_iget(root
->fs_info
->sb
, &key
, root
, NULL
);
188 parent
= d_obtain_alias(inode
);
190 parent
= ERR_PTR(-ENOMEM
);
195 btrfs_free_path(path
);
196 return ERR_PTR(-EINVAL
);
199 const struct export_operations btrfs_export_ops
= {
200 .encode_fh
= btrfs_encode_fh
,
201 .fh_to_dentry
= btrfs_fh_to_dentry
,
202 .fh_to_parent
= btrfs_fh_to_parent
,
203 .get_parent
= btrfs_get_parent
,