• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2011 STRATO.  All rights reserved.
4  */
5 
6 #ifndef BTRFS_BACKREF_H
7 #define BTRFS_BACKREF_H
8 
9 #include <linux/btrfs.h>
10 #include "ulist.h"
11 #include "disk-io.h"
12 #include "extent_io.h"
13 
14 struct inode_fs_paths {
15 	struct btrfs_path		*btrfs_path;
16 	struct btrfs_root		*fs_root;
17 	struct btrfs_data_container	*fspath;
18 };
19 
20 struct btrfs_backref_shared_cache_entry {
21 	u64 bytenr;
22 	u64 gen;
23 	bool is_shared;
24 };
25 
26 struct btrfs_backref_shared_cache {
27 	/*
28 	 * A path from a root to a leaf that has a file extent item pointing to
29 	 * a given data extent should never exceed the maximum b+tree height.
30 	 */
31 	struct btrfs_backref_shared_cache_entry entries[BTRFS_MAX_LEVEL];
32 	bool use_cache;
33 };
34 
35 typedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 root,
36 		void *ctx);
37 
38 int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
39 			struct btrfs_path *path, struct btrfs_key *found_key,
40 			u64 *flags);
41 
42 int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
43 			    struct btrfs_key *key, struct btrfs_extent_item *ei,
44 			    u32 item_size, u64 *out_root, u8 *out_level);
45 
46 int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
47 				u64 extent_item_objectid,
48 				u64 extent_offset, int search_commit_root,
49 				iterate_extent_inodes_t *iterate, void *ctx,
50 				bool ignore_offset);
51 
52 int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
53 				struct btrfs_path *path, void *ctx,
54 				bool ignore_offset);
55 
56 int paths_from_inode(u64 inum, struct inode_fs_paths *ipath);
57 
58 int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
59 			 struct btrfs_fs_info *fs_info, u64 bytenr,
60 			 u64 time_seq, struct ulist **leafs,
61 			 const u64 *extent_item_pos, bool ignore_offset);
62 int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
63 			 struct btrfs_fs_info *fs_info, u64 bytenr,
64 			 u64 time_seq, struct ulist **roots,
65 			 bool skip_commit_root_sem);
66 char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
67 			u32 name_len, unsigned long name_off,
68 			struct extent_buffer *eb_in, u64 parent,
69 			char *dest, u32 size);
70 
71 struct btrfs_data_container *init_data_container(u32 total_bytes);
72 struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
73 					struct btrfs_path *path);
74 void free_ipath(struct inode_fs_paths *ipath);
75 
76 int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
77 			  u64 start_off, struct btrfs_path *path,
78 			  struct btrfs_inode_extref **ret_extref,
79 			  u64 *found_off);
80 int btrfs_is_data_extent_shared(struct btrfs_root *root, u64 inum, u64 bytenr,
81 				u64 extent_gen,
82 				struct ulist *roots, struct ulist *tmp,
83 				struct btrfs_backref_shared_cache *cache);
84 
85 int __init btrfs_prelim_ref_init(void);
86 void __cold btrfs_prelim_ref_exit(void);
87 
88 struct prelim_ref {
89 	struct rb_node rbnode;
90 	u64 root_id;
91 	struct btrfs_key key_for_search;
92 	int level;
93 	int count;
94 	struct extent_inode_elem *inode_list;
95 	u64 parent;
96 	u64 wanted_disk_byte;
97 };
98 
99 /*
100  * Iterate backrefs of one extent.
101  *
102  * Now it only supports iteration of tree block in commit root.
103  */
104 struct btrfs_backref_iter {
105 	u64 bytenr;
106 	struct btrfs_path *path;
107 	struct btrfs_fs_info *fs_info;
108 	struct btrfs_key cur_key;
109 	u32 item_ptr;
110 	u32 cur_ptr;
111 	u32 end_ptr;
112 };
113 
114 struct btrfs_backref_iter *btrfs_backref_iter_alloc(
115 		struct btrfs_fs_info *fs_info, gfp_t gfp_flag);
116 
btrfs_backref_iter_free(struct btrfs_backref_iter * iter)117 static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter)
118 {
119 	if (!iter)
120 		return;
121 	btrfs_free_path(iter->path);
122 	kfree(iter);
123 }
124 
btrfs_backref_get_eb(struct btrfs_backref_iter * iter)125 static inline struct extent_buffer *btrfs_backref_get_eb(
126 		struct btrfs_backref_iter *iter)
127 {
128 	if (!iter)
129 		return NULL;
130 	return iter->path->nodes[0];
131 }
132 
133 /*
134  * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data
135  * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header.
136  *
137  * This helper determines if that's the case.
138  */
btrfs_backref_has_tree_block_info(struct btrfs_backref_iter * iter)139 static inline bool btrfs_backref_has_tree_block_info(
140 		struct btrfs_backref_iter *iter)
141 {
142 	if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY &&
143 	    iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item))
144 		return true;
145 	return false;
146 }
147 
148 int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr);
149 
150 int btrfs_backref_iter_next(struct btrfs_backref_iter *iter);
151 
btrfs_backref_iter_is_inline_ref(struct btrfs_backref_iter * iter)152 static inline bool btrfs_backref_iter_is_inline_ref(
153 		struct btrfs_backref_iter *iter)
154 {
155 	if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY ||
156 	    iter->cur_key.type == BTRFS_METADATA_ITEM_KEY)
157 		return true;
158 	return false;
159 }
160 
btrfs_backref_iter_release(struct btrfs_backref_iter * iter)161 static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter)
162 {
163 	iter->bytenr = 0;
164 	iter->item_ptr = 0;
165 	iter->cur_ptr = 0;
166 	iter->end_ptr = 0;
167 	btrfs_release_path(iter->path);
168 	memset(&iter->cur_key, 0, sizeof(iter->cur_key));
169 }
170 
171 /*
172  * Backref cache related structures
173  *
174  * The whole objective of backref_cache is to build a bi-directional map
175  * of tree blocks (represented by backref_node) and all their parents.
176  */
177 
178 /*
179  * Represent a tree block in the backref cache
180  */
181 struct btrfs_backref_node {
182 	struct {
183 		struct rb_node rb_node;
184 		u64 bytenr;
185 	}; /* Use rb_simple_node for search/insert */
186 
187 	u64 new_bytenr;
188 	/* Objectid of tree block owner, can be not uptodate */
189 	u64 owner;
190 	/* Link to pending, changed or detached list */
191 	struct list_head list;
192 
193 	/* List of upper level edges, which link this node to its parents */
194 	struct list_head upper;
195 	/* List of lower level edges, which link this node to its children */
196 	struct list_head lower;
197 
198 	/* NULL if this node is not tree root */
199 	struct btrfs_root *root;
200 	/* Extent buffer got by COWing the block */
201 	struct extent_buffer *eb;
202 	/* Level of the tree block */
203 	unsigned int level:8;
204 	/* Is the block in a non-shareable tree */
205 	unsigned int cowonly:1;
206 	/* 1 if no child node is in the cache */
207 	unsigned int lowest:1;
208 	/* Is the extent buffer locked */
209 	unsigned int locked:1;
210 	/* Has the block been processed */
211 	unsigned int processed:1;
212 	/* Have backrefs of this block been checked */
213 	unsigned int checked:1;
214 	/*
215 	 * 1 if corresponding block has been COWed but some upper level block
216 	 * pointers may not point to the new location
217 	 */
218 	unsigned int pending:1;
219 	/* 1 if the backref node isn't connected to any other backref node */
220 	unsigned int detached:1;
221 
222 	/*
223 	 * For generic purpose backref cache, where we only care if it's a reloc
224 	 * root, doesn't care the source subvolid.
225 	 */
226 	unsigned int is_reloc_root:1;
227 };
228 
229 #define LOWER	0
230 #define UPPER	1
231 
232 /*
233  * Represent an edge connecting upper and lower backref nodes.
234  */
235 struct btrfs_backref_edge {
236 	/*
237 	 * list[LOWER] is linked to btrfs_backref_node::upper of lower level
238 	 * node, and list[UPPER] is linked to btrfs_backref_node::lower of
239 	 * upper level node.
240 	 *
241 	 * Also, build_backref_tree() uses list[UPPER] for pending edges, before
242 	 * linking list[UPPER] to its upper level nodes.
243 	 */
244 	struct list_head list[2];
245 
246 	/* Two related nodes */
247 	struct btrfs_backref_node *node[2];
248 };
249 
250 struct btrfs_backref_cache {
251 	/* Red black tree of all backref nodes in the cache */
252 	struct rb_root rb_root;
253 	/* For passing backref nodes to btrfs_reloc_cow_block */
254 	struct btrfs_backref_node *path[BTRFS_MAX_LEVEL];
255 	/*
256 	 * List of blocks that have been COWed but some block pointers in upper
257 	 * level blocks may not reflect the new location
258 	 */
259 	struct list_head pending[BTRFS_MAX_LEVEL];
260 	/* List of backref nodes with no child node */
261 	struct list_head leaves;
262 	/* List of blocks that have been COWed in current transaction */
263 	struct list_head changed;
264 	/* List of detached backref node. */
265 	struct list_head detached;
266 
267 	u64 last_trans;
268 
269 	int nr_nodes;
270 	int nr_edges;
271 
272 	/* List of unchecked backref edges during backref cache build */
273 	struct list_head pending_edge;
274 
275 	/* List of useless backref nodes during backref cache build */
276 	struct list_head useless_node;
277 
278 	struct btrfs_fs_info *fs_info;
279 
280 	/*
281 	 * Whether this cache is for relocation
282 	 *
283 	 * Reloction backref cache require more info for reloc root compared
284 	 * to generic backref cache.
285 	 */
286 	unsigned int is_reloc;
287 };
288 
289 void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
290 			      struct btrfs_backref_cache *cache, int is_reloc);
291 struct btrfs_backref_node *btrfs_backref_alloc_node(
292 		struct btrfs_backref_cache *cache, u64 bytenr, int level);
293 struct btrfs_backref_edge *btrfs_backref_alloc_edge(
294 		struct btrfs_backref_cache *cache);
295 
296 #define		LINK_LOWER	(1 << 0)
297 #define		LINK_UPPER	(1 << 1)
btrfs_backref_link_edge(struct btrfs_backref_edge * edge,struct btrfs_backref_node * lower,struct btrfs_backref_node * upper,int link_which)298 static inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge,
299 					   struct btrfs_backref_node *lower,
300 					   struct btrfs_backref_node *upper,
301 					   int link_which)
302 {
303 	ASSERT(upper && lower && upper->level == lower->level + 1);
304 	edge->node[LOWER] = lower;
305 	edge->node[UPPER] = upper;
306 	if (link_which & LINK_LOWER)
307 		list_add_tail(&edge->list[LOWER], &lower->upper);
308 	if (link_which & LINK_UPPER)
309 		list_add_tail(&edge->list[UPPER], &upper->lower);
310 }
311 
btrfs_backref_free_node(struct btrfs_backref_cache * cache,struct btrfs_backref_node * node)312 static inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache,
313 					   struct btrfs_backref_node *node)
314 {
315 	if (node) {
316 		ASSERT(list_empty(&node->list));
317 		ASSERT(list_empty(&node->lower));
318 		ASSERT(node->eb == NULL);
319 		cache->nr_nodes--;
320 		btrfs_put_root(node->root);
321 		kfree(node);
322 	}
323 }
324 
btrfs_backref_free_edge(struct btrfs_backref_cache * cache,struct btrfs_backref_edge * edge)325 static inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache,
326 					   struct btrfs_backref_edge *edge)
327 {
328 	if (edge) {
329 		cache->nr_edges--;
330 		kfree(edge);
331 	}
332 }
333 
btrfs_backref_unlock_node_buffer(struct btrfs_backref_node * node)334 static inline void btrfs_backref_unlock_node_buffer(
335 		struct btrfs_backref_node *node)
336 {
337 	if (node->locked) {
338 		btrfs_tree_unlock(node->eb);
339 		node->locked = 0;
340 	}
341 }
342 
btrfs_backref_drop_node_buffer(struct btrfs_backref_node * node)343 static inline void btrfs_backref_drop_node_buffer(
344 		struct btrfs_backref_node *node)
345 {
346 	if (node->eb) {
347 		btrfs_backref_unlock_node_buffer(node);
348 		free_extent_buffer(node->eb);
349 		node->eb = NULL;
350 	}
351 }
352 
353 /*
354  * Drop the backref node from cache without cleaning up its children
355  * edges.
356  *
357  * This can only be called on node without parent edges.
358  * The children edges are still kept as is.
359  */
btrfs_backref_drop_node(struct btrfs_backref_cache * tree,struct btrfs_backref_node * node)360 static inline void btrfs_backref_drop_node(struct btrfs_backref_cache *tree,
361 					   struct btrfs_backref_node *node)
362 {
363 	ASSERT(list_empty(&node->upper));
364 
365 	btrfs_backref_drop_node_buffer(node);
366 	list_del_init(&node->list);
367 	list_del_init(&node->lower);
368 	if (!RB_EMPTY_NODE(&node->rb_node))
369 		rb_erase(&node->rb_node, &tree->rb_root);
370 	btrfs_backref_free_node(tree, node);
371 }
372 
373 void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
374 				struct btrfs_backref_node *node);
375 
376 void btrfs_backref_release_cache(struct btrfs_backref_cache *cache);
377 
btrfs_backref_panic(struct btrfs_fs_info * fs_info,u64 bytenr,int errno)378 static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,
379 				       u64 bytenr, int errno)
380 {
381 	btrfs_panic(fs_info, errno,
382 		    "Inconsistency in backref cache found at offset %llu",
383 		    bytenr);
384 }
385 
386 int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache,
387 				struct btrfs_path *path,
388 				struct btrfs_backref_iter *iter,
389 				struct btrfs_key *node_key,
390 				struct btrfs_backref_node *cur);
391 
392 int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
393 				     struct btrfs_backref_node *start);
394 
395 void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,
396 				 struct btrfs_backref_node *node);
397 
398 #endif
399