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;
73 key.objectid = root_objectid;
74 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
77 root = btrfs_read_fs_root_no_name(btrfs_sb(sb)->fs_info, &key);
79 return ERR_CAST(root);
81 key.objectid = objectid;
82 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
85 inode = btrfs_iget(sb, &key, root, NULL);
89 if (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);
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);
143 static struct dentry *btrfs_get_parent(struct dentry *child)
145 struct inode *dir = child->d_inode;
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);
163 btrfs_free_path(path);
166 leaf = path->nodes[0];
167 slot = path->slots[0];
169 /* btrfs_search_slot() returns the slot where we'd want to
170 insert a backref for parent inode #0xFFFFFFFFFFFFFFFF.
171 The _real_ backref, telling us what the parent inode
172 _actually_ is, will be in the slot _before_ the one
173 that btrfs_search_slot() returns. */
175 /* Unless there is _no_ key in the tree before... */
176 btrfs_free_path(path);
177 return ERR_PTR(-EIO);
182 btrfs_item_key_to_cpu(leaf, &key, slot);
183 btrfs_free_path(path);
185 if (key.objectid != dir->i_ino || key.type != BTRFS_INODE_REF_KEY)
186 return ERR_PTR(-EINVAL);
188 objectid = key.offset;
190 /* If we are already at the root of a subvol, return the real root */
191 if (objectid == dir->i_ino)
192 return dget(dir->i_sb->s_root);
194 /* Build a new key for the inode item */
195 key.objectid = objectid;
196 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
199 return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL));
202 const struct export_operations btrfs_export_ops = {
203 .encode_fh = btrfs_encode_fh,
204 .fh_to_dentry = btrfs_fh_to_dentry,
205 .fh_to_parent = btrfs_fh_to_parent,
206 .get_parent = btrfs_get_parent,