Commit | Line | Data |
---|---|---|
be6e8dc0 BR |
1 | #include <linux/fs.h> |
2 | #include <linux/types.h> | |
3 | #include "ctree.h" | |
4 | #include "disk-io.h" | |
5 | #include "btrfs_inode.h" | |
6 | #include "print-tree.h" | |
7 | #include "export.h" | |
8 | #include "compat.h" | |
9 | ||
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 | |
14 | #endif | |
15 | ||
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) | |
19 | ||
20 | static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len, | |
21 | int connectable) | |
22 | { | |
23 | struct btrfs_fid *fid = (struct btrfs_fid *)fh; | |
24 | struct inode *inode = dentry->d_inode; | |
25 | int len = *max_len; | |
26 | int type; | |
27 | ||
28 | if ((len < BTRFS_FID_SIZE_NON_CONNECTABLE) || | |
29 | (connectable && len < BTRFS_FID_SIZE_CONNECTABLE)) | |
30 | return 255; | |
31 | ||
32 | len = BTRFS_FID_SIZE_NON_CONNECTABLE; | |
33 | type = FILEID_BTRFS_WITHOUT_PARENT; | |
34 | ||
35 | fid->objectid = BTRFS_I(inode)->location.objectid; | |
36 | fid->root_objectid = BTRFS_I(inode)->root->objectid; | |
37 | fid->gen = inode->i_generation; | |
38 | ||
39 | if (connectable && !S_ISDIR(inode->i_mode)) { | |
40 | struct inode *parent; | |
41 | u64 parent_root_id; | |
42 | ||
43 | spin_lock(&dentry->d_lock); | |
44 | ||
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; | |
49 | ||
50 | spin_unlock(&dentry->d_lock); | |
51 | ||
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; | |
56 | } else { | |
57 | len = BTRFS_FID_SIZE_CONNECTABLE; | |
58 | type = FILEID_BTRFS_WITH_PARENT; | |
59 | } | |
60 | } | |
61 | ||
62 | *max_len = len; | |
63 | return type; | |
64 | } | |
65 | ||
66 | static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid, | |
67 | u64 root_objectid, u32 generation) | |
68 | { | |
69 | struct btrfs_root *root; | |
70 | struct inode *inode; | |
71 | struct dentry *result; | |
72 | struct btrfs_key key; | |
73 | ||
2d4d9fbd DW |
74 | key.objectid = root_objectid; |
75 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | |
76 | key.offset = (u64)-1; | |
77 | ||
78 | root = btrfs_read_fs_root_no_name(btrfs_sb(sb)->fs_info, &key); | |
79 | if (IS_ERR(root)) | |
80 | return ERR_CAST(root); | |
81 | ||
be6e8dc0 BR |
82 | key.objectid = objectid; |
83 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | |
84 | key.offset = 0; | |
85 | ||
be6e8dc0 BR |
86 | inode = btrfs_iget(sb, &key, root, NULL); |
87 | if (IS_ERR(inode)) | |
88 | return (void *)inode; | |
89 | ||
90 | if (generation != inode->i_generation) { | |
91 | iput(inode); | |
92 | return ERR_PTR(-ESTALE); | |
93 | } | |
94 | ||
95 | result = d_obtain_alias(inode); | |
96 | if (!result) | |
97 | return ERR_PTR(-ENOMEM); | |
98 | ||
99 | return result; | |
100 | } | |
101 | ||
102 | static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh, | |
103 | int fh_len, int fh_type) | |
104 | { | |
105 | struct btrfs_fid *fid = (struct btrfs_fid *) fh; | |
106 | u64 objectid, root_objectid; | |
107 | u32 generation; | |
108 | ||
109 | if (fh_type == FILEID_BTRFS_WITH_PARENT) { | |
110 | if (fh_len != BTRFS_FID_SIZE_CONNECTABLE) | |
111 | return NULL; | |
112 | root_objectid = fid->root_objectid; | |
113 | } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) { | |
114 | if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) | |
115 | return NULL; | |
116 | root_objectid = fid->parent_root_objectid; | |
117 | } else | |
118 | return NULL; | |
119 | ||
120 | objectid = fid->parent_objectid; | |
121 | generation = fid->parent_gen; | |
122 | ||
123 | return btrfs_get_dentry(sb, objectid, root_objectid, generation); | |
124 | } | |
125 | ||
126 | static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh, | |
127 | int fh_len, int fh_type) | |
128 | { | |
129 | struct btrfs_fid *fid = (struct btrfs_fid *) fh; | |
130 | u64 objectid, root_objectid; | |
131 | u32 generation; | |
132 | ||
133 | if ((fh_type != FILEID_BTRFS_WITH_PARENT || | |
134 | fh_len != BTRFS_FID_SIZE_CONNECTABLE) && | |
135 | (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT || | |
136 | fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) && | |
137 | (fh_type != FILEID_BTRFS_WITHOUT_PARENT || | |
138 | fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE)) | |
139 | return NULL; | |
140 | ||
141 | objectid = fid->objectid; | |
142 | root_objectid = fid->root_objectid; | |
143 | generation = fid->gen; | |
144 | ||
145 | return btrfs_get_dentry(sb, objectid, root_objectid, generation); | |
146 | } | |
147 | ||
148 | static struct dentry *btrfs_get_parent(struct dentry *child) | |
149 | { | |
150 | struct inode *dir = child->d_inode; | |
151 | struct inode *inode; | |
152 | struct dentry *parent; | |
153 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
154 | struct btrfs_key key; | |
155 | struct btrfs_path *path; | |
156 | struct extent_buffer *leaf; | |
be6e8dc0 BR |
157 | int slot; |
158 | u64 objectid; | |
159 | int ret; | |
160 | ||
161 | path = btrfs_alloc_path(); | |
162 | ||
163 | key.objectid = dir->i_ino; | |
164 | btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY); | |
87acb4ef | 165 | key.offset = (u64)-1; |
be6e8dc0 | 166 | |
87acb4ef | 167 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
d54a8390 DW |
168 | if (ret < 0) { |
169 | /* Error */ | |
170 | btrfs_free_path(path); | |
171 | return ERR_PTR(ret); | |
172 | } | |
be6e8dc0 BR |
173 | leaf = path->nodes[0]; |
174 | slot = path->slots[0]; | |
d54a8390 DW |
175 | if (ret) { |
176 | /* btrfs_search_slot() returns the slot where we'd want to | |
177 | insert a backref for parent inode #0xFFFFFFFFFFFFFFFF. | |
178 | The _real_ backref, telling us what the parent inode | |
179 | _actually_ is, will be in the slot _before_ the one | |
180 | that btrfs_search_slot() returns. */ | |
181 | if (!slot) { | |
182 | /* Unless there is _no_ key in the tree before... */ | |
183 | btrfs_free_path(path); | |
184 | return ERR_PTR(-EIO); | |
185 | } | |
186 | slot--; | |
be6e8dc0 BR |
187 | } |
188 | ||
87acb4ef | 189 | btrfs_item_key_to_cpu(leaf, &key, slot); |
be6e8dc0 BR |
190 | btrfs_free_path(path); |
191 | ||
be6e8dc0 | 192 | if (key.objectid != dir->i_ino || key.type != BTRFS_INODE_REF_KEY) |
d54a8390 | 193 | return ERR_PTR(-EINVAL); |
be6e8dc0 BR |
194 | |
195 | objectid = key.offset; | |
196 | ||
2d4d9fbd DW |
197 | /* If we are already at the root of a subvol, return the real root */ |
198 | if (objectid == dir->i_ino) | |
199 | return dget(dir->i_sb->s_root); | |
200 | ||
be6e8dc0 BR |
201 | /* Build a new key for the inode item */ |
202 | key.objectid = objectid; | |
203 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); | |
204 | key.offset = 0; | |
205 | ||
206 | inode = btrfs_iget(root->fs_info->sb, &key, root, NULL); | |
207 | ||
208 | parent = d_obtain_alias(inode); | |
209 | if (!parent) | |
210 | parent = ERR_PTR(-ENOMEM); | |
211 | ||
212 | return parent; | |
be6e8dc0 BR |
213 | } |
214 | ||
215 | const struct export_operations btrfs_export_ops = { | |
216 | .encode_fh = btrfs_encode_fh, | |
217 | .fh_to_dentry = btrfs_fh_to_dentry, | |
218 | .fh_to_parent = btrfs_fh_to_parent, | |
219 | .get_parent = btrfs_get_parent, | |
220 | }; |