dm writecache: correct uncommitted_block when discarding uncommitted entry
[linux/fpc-iii.git] / fs / btrfs / export.c
blob2bb25d2dc44bdfb26dae60e7ac4d27b7001b0297
1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/fs.h>
4 #include <linux/types.h>
5 #include "ctree.h"
6 #include "disk-io.h"
7 #include "btrfs_inode.h"
8 #include "print-tree.h"
9 #include "export.h"
11 #define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \
12 parent_objectid) / 4)
13 #define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, \
14 parent_root_objectid) / 4)
15 #define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid) / 4)
17 static int btrfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
18 struct inode *parent)
20 struct btrfs_fid *fid = (struct btrfs_fid *)fh;
21 int len = *max_len;
22 int type;
24 if (parent && (len < BTRFS_FID_SIZE_CONNECTABLE)) {
25 *max_len = BTRFS_FID_SIZE_CONNECTABLE;
26 return FILEID_INVALID;
27 } else if (len < BTRFS_FID_SIZE_NON_CONNECTABLE) {
28 *max_len = BTRFS_FID_SIZE_NON_CONNECTABLE;
29 return FILEID_INVALID;
32 len = BTRFS_FID_SIZE_NON_CONNECTABLE;
33 type = FILEID_BTRFS_WITHOUT_PARENT;
35 fid->objectid = btrfs_ino(BTRFS_I(inode));
36 fid->root_objectid = BTRFS_I(inode)->root->root_key.objectid;
37 fid->gen = inode->i_generation;
39 if (parent) {
40 u64 parent_root_id;
42 fid->parent_objectid = BTRFS_I(parent)->location.objectid;
43 fid->parent_gen = parent->i_generation;
44 parent_root_id = BTRFS_I(parent)->root->root_key.objectid;
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;
50 } else {
51 len = BTRFS_FID_SIZE_CONNECTABLE;
52 type = FILEID_BTRFS_WITH_PARENT;
56 *max_len = len;
57 return type;
60 struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
61 u64 root_objectid, u32 generation,
62 int check_generation)
64 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
65 struct btrfs_root *root;
66 struct inode *inode;
67 struct btrfs_key key;
69 if (objectid < BTRFS_FIRST_FREE_OBJECTID)
70 return ERR_PTR(-ESTALE);
72 key.objectid = root_objectid;
73 key.type = BTRFS_ROOT_ITEM_KEY;
74 key.offset = (u64)-1;
76 root = btrfs_get_fs_root(fs_info, &key, true);
77 if (IS_ERR(root))
78 return ERR_CAST(root);
80 key.objectid = objectid;
81 key.type = BTRFS_INODE_ITEM_KEY;
82 key.offset = 0;
84 inode = btrfs_iget(sb, &key, root);
85 btrfs_put_root(root);
86 if (IS_ERR(inode))
87 return ERR_CAST(inode);
89 if (check_generation && generation != inode->i_generation) {
90 iput(inode);
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;
102 u32 generation;
104 if (fh_type == FILEID_BTRFS_WITH_PARENT) {
105 if (fh_len < BTRFS_FID_SIZE_CONNECTABLE)
106 return NULL;
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)
110 return NULL;
111 root_objectid = fid->parent_root_objectid;
112 } else
113 return NULL;
115 objectid = fid->parent_objectid;
116 generation = fid->parent_gen;
118 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
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;
126 u32 generation;
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))
134 return NULL;
136 objectid = fid->objectid;
137 root_objectid = fid->root_objectid;
138 generation = fid->gen;
140 return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
143 struct dentry *btrfs_get_parent(struct dentry *child)
145 struct inode *dir = d_inode(child);
146 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
147 struct btrfs_root *root = BTRFS_I(dir)->root;
148 struct btrfs_path *path;
149 struct extent_buffer *leaf;
150 struct btrfs_root_ref *ref;
151 struct btrfs_key key;
152 struct btrfs_key found_key;
153 int ret;
155 path = btrfs_alloc_path();
156 if (!path)
157 return ERR_PTR(-ENOMEM);
159 if (btrfs_ino(BTRFS_I(dir)) == BTRFS_FIRST_FREE_OBJECTID) {
160 key.objectid = root->root_key.objectid;
161 key.type = BTRFS_ROOT_BACKREF_KEY;
162 key.offset = (u64)-1;
163 root = fs_info->tree_root;
164 } else {
165 key.objectid = btrfs_ino(BTRFS_I(dir));
166 key.type = BTRFS_INODE_REF_KEY;
167 key.offset = (u64)-1;
170 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
171 if (ret < 0)
172 goto fail;
174 BUG_ON(ret == 0); /* Key with offset of -1 found */
175 if (path->slots[0] == 0) {
176 ret = -ENOENT;
177 goto fail;
180 path->slots[0]--;
181 leaf = path->nodes[0];
183 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
184 if (found_key.objectid != key.objectid || found_key.type != key.type) {
185 ret = -ENOENT;
186 goto fail;
189 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
190 ref = btrfs_item_ptr(leaf, path->slots[0],
191 struct btrfs_root_ref);
192 key.objectid = btrfs_root_ref_dirid(leaf, ref);
193 } else {
194 key.objectid = found_key.offset;
196 btrfs_free_path(path);
198 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
199 return btrfs_get_dentry(fs_info->sb, key.objectid,
200 found_key.offset, 0, 0);
203 key.type = BTRFS_INODE_ITEM_KEY;
204 key.offset = 0;
205 return d_obtain_alias(btrfs_iget(fs_info->sb, &key, root));
206 fail:
207 btrfs_free_path(path);
208 return ERR_PTR(ret);
211 static int btrfs_get_name(struct dentry *parent, char *name,
212 struct dentry *child)
214 struct inode *inode = d_inode(child);
215 struct inode *dir = d_inode(parent);
216 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
217 struct btrfs_path *path;
218 struct btrfs_root *root = BTRFS_I(dir)->root;
219 struct btrfs_inode_ref *iref;
220 struct btrfs_root_ref *rref;
221 struct extent_buffer *leaf;
222 unsigned long name_ptr;
223 struct btrfs_key key;
224 int name_len;
225 int ret;
226 u64 ino;
228 if (!S_ISDIR(dir->i_mode))
229 return -EINVAL;
231 ino = btrfs_ino(BTRFS_I(inode));
233 path = btrfs_alloc_path();
234 if (!path)
235 return -ENOMEM;
236 path->leave_spinning = 1;
238 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
239 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
240 key.type = BTRFS_ROOT_BACKREF_KEY;
241 key.offset = (u64)-1;
242 root = fs_info->tree_root;
243 } else {
244 key.objectid = ino;
245 key.offset = btrfs_ino(BTRFS_I(dir));
246 key.type = BTRFS_INODE_REF_KEY;
249 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
250 if (ret < 0) {
251 btrfs_free_path(path);
252 return ret;
253 } else if (ret > 0) {
254 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
255 path->slots[0]--;
256 } else {
257 btrfs_free_path(path);
258 return -ENOENT;
261 leaf = path->nodes[0];
263 if (ino == BTRFS_FIRST_FREE_OBJECTID) {
264 rref = btrfs_item_ptr(leaf, path->slots[0],
265 struct btrfs_root_ref);
266 name_ptr = (unsigned long)(rref + 1);
267 name_len = btrfs_root_ref_name_len(leaf, rref);
268 } else {
269 iref = btrfs_item_ptr(leaf, path->slots[0],
270 struct btrfs_inode_ref);
271 name_ptr = (unsigned long)(iref + 1);
272 name_len = btrfs_inode_ref_name_len(leaf, iref);
275 read_extent_buffer(leaf, name, name_ptr, name_len);
276 btrfs_free_path(path);
279 * have to add the null termination to make sure that reconnect_path
280 * gets the right len for strlen
282 name[name_len] = '\0';
284 return 0;
287 const struct export_operations btrfs_export_ops = {
288 .encode_fh = btrfs_encode_fh,
289 .fh_to_dentry = btrfs_fh_to_dentry,
290 .fh_to_parent = btrfs_fh_to_parent,
291 .get_parent = btrfs_get_parent,
292 .get_name = btrfs_get_name,