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, parent_objectid)/4)
11 #define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, parent_root_objectid)/4)
12 #define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid)/4)
14 static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
17 struct btrfs_fid *fid = (struct btrfs_fid *)fh;
18 struct inode *inode = dentry->d_inode;
22 if ((len < BTRFS_FID_SIZE_NON_CONNECTABLE) ||
23 (connectable && len < BTRFS_FID_SIZE_CONNECTABLE))
26 len = BTRFS_FID_SIZE_NON_CONNECTABLE;
27 type = FILEID_BTRFS_WITHOUT_PARENT;
29 fid->objectid = BTRFS_I(inode)->location.objectid;
30 fid->root_objectid = BTRFS_I(inode)->root->objectid;
31 fid->gen = inode->i_generation;
33 if (connectable && !S_ISDIR(inode->i_mode)) {
37 spin_lock(&dentry->d_lock);
39 parent = dentry->d_parent->d_inode;
40 fid->parent_objectid = BTRFS_I(parent)->location.objectid;
41 fid->parent_gen = parent->i_generation;
42 parent_root_id = BTRFS_I(parent)->root->objectid;
44 spin_unlock(&dentry->d_lock);
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 static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
61 u64 root_objectid, u32 generation)
63 struct btrfs_root *root;
67 key.objectid = root_objectid;
68 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
71 root = btrfs_read_fs_root_no_name(btrfs_sb(sb)->fs_info, &key);
73 return ERR_CAST(root);
75 key.objectid = objectid;
76 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
79 inode = btrfs_iget(sb, &key, root, NULL);
83 if (generation != inode->i_generation) {
85 return ERR_PTR(-ESTALE);
88 return d_obtain_alias(inode);
91 static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
92 int fh_len, int fh_type)
94 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
95 u64 objectid, root_objectid;
98 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
99 if (fh_len != BTRFS_FID_SIZE_CONNECTABLE)
101 root_objectid = fid->root_objectid;
102 } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
103 if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT)
105 root_objectid = fid->parent_root_objectid;
109 objectid = fid->parent_objectid;
110 generation = fid->parent_gen;
112 return btrfs_get_dentry(sb, objectid, root_objectid, generation);
115 static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
116 int fh_len, int fh_type)
118 struct btrfs_fid *fid = (struct btrfs_fid *) fh;
119 u64 objectid, root_objectid;
122 if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
123 fh_len != BTRFS_FID_SIZE_CONNECTABLE) &&
124 (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
125 fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
126 (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
127 fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE))
130 objectid = fid->objectid;
131 root_objectid = fid->root_objectid;
132 generation = fid->gen;
134 return btrfs_get_dentry(sb, objectid, root_objectid, generation);
137 static struct dentry *btrfs_get_parent(struct dentry *child)
139 struct inode *dir = child->d_inode;
140 struct btrfs_root *root = BTRFS_I(dir)->root;
141 struct btrfs_key key;
142 struct btrfs_path *path;
143 struct extent_buffer *leaf;
148 path = btrfs_alloc_path();
150 key.objectid = dir->i_ino;
151 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
152 key.offset = (u64)-1;
154 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
157 btrfs_free_path(path);
160 leaf = path->nodes[0];
161 slot = path->slots[0];
163 /* btrfs_search_slot() returns the slot where we'd want to
164 insert a backref for parent inode #0xFFFFFFFFFFFFFFFF.
165 The _real_ backref, telling us what the parent inode
166 _actually_ is, will be in the slot _before_ the one
167 that btrfs_search_slot() returns. */
169 /* Unless there is _no_ key in the tree before... */
170 btrfs_free_path(path);
171 return ERR_PTR(-EIO);
176 btrfs_item_key_to_cpu(leaf, &key, slot);
177 btrfs_free_path(path);
179 if (key.objectid != dir->i_ino || key.type != BTRFS_INODE_REF_KEY)
180 return ERR_PTR(-EINVAL);
182 objectid = key.offset;
184 /* If we are already at the root of a subvol, return the real root */
185 if (objectid == dir->i_ino)
186 return dget(dir->i_sb->s_root);
188 /* Build a new key for the inode item */
189 key.objectid = objectid;
190 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
193 return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL));
196 const struct export_operations btrfs_export_ops = {
197 .encode_fh = btrfs_encode_fh,
198 .fh_to_dentry = btrfs_fh_to_dentry,
199 .fh_to_parent = btrfs_fh_to_parent,
200 .get_parent = btrfs_get_parent,