Loading...
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 "messages.h"
11#include "ulist.h"
12#include "disk-io.h"
13#include "extent_io.h"
14
15/*
16 * Used by implementations of iterate_extent_inodes_t (see definition below) to
17 * signal that backref iteration can stop immediately and no error happened.
18 * The value must be non-negative and must not be 0, 1 (which is a common return
19 * value from things like btrfs_search_slot() and used internally in the backref
20 * walking code) and different from BACKREF_FOUND_SHARED and
21 * BACKREF_FOUND_NOT_SHARED
22 */
23#define BTRFS_ITERATE_EXTENT_INODES_STOP 5
24
25/*
26 * Should return 0 if no errors happened and iteration of backrefs should
27 * continue. Can return BTRFS_ITERATE_EXTENT_INODES_STOP or any other non-zero
28 * value to immediately stop iteration and possibly signal an error back to
29 * the caller.
30 */
31typedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 num_bytes,
32 u64 root, void *ctx);
33
34/*
35 * Context and arguments for backref walking functions. Some of the fields are
36 * to be filled by the caller of such functions while other are filled by the
37 * functions themselves, as described below.
38 */
39struct btrfs_backref_walk_ctx {
40 /*
41 * The address of the extent for which we are doing backref walking.
42 * Can be either a data extent or a metadata extent.
43 *
44 * Must always be set by the top level caller.
45 */
46 u64 bytenr;
47 /*
48 * Offset relative to the target extent. This is only used for data
49 * extents, and it's meaningful because we can have file extent items
50 * that point only to a section of a data extent ("bookend" extents),
51 * and we want to filter out any that don't point to a section of the
52 * data extent containing the given offset.
53 *
54 * Must always be set by the top level caller.
55 */
56 u64 extent_item_pos;
57 /*
58 * If true and bytenr corresponds to a data extent, then references from
59 * all file extent items that point to the data extent are considered,
60 * @extent_item_pos is ignored.
61 */
62 bool ignore_extent_item_pos;
63 /* A valid transaction handle or NULL. */
64 struct btrfs_trans_handle *trans;
65 /*
66 * The file system's info object, can not be NULL.
67 *
68 * Must always be set by the top level caller.
69 */
70 struct btrfs_fs_info *fs_info;
71 /*
72 * Time sequence acquired from btrfs_get_tree_mod_seq(), in case the
73 * caller joined the tree mod log to get a consistent view of b+trees
74 * while we do backref walking, or BTRFS_SEQ_LAST.
75 * When using BTRFS_SEQ_LAST, delayed refs are not checked and it uses
76 * commit roots when searching b+trees - this is a special case for
77 * qgroups used during a transaction commit.
78 */
79 u64 time_seq;
80 /*
81 * Used to collect the bytenr of metadata extents that point to the
82 * target extent.
83 */
84 struct ulist *refs;
85 /*
86 * List used to collect the IDs of the roots from which the target
87 * extent is accessible. Can be NULL in case the caller does not care
88 * about collecting root IDs.
89 */
90 struct ulist *roots;
91 /*
92 * Used by iterate_extent_inodes() and the main backref walk code
93 * (find_parent_nodes()). Lookup and store functions for an optional
94 * cache which maps the logical address (bytenr) of leaves to an array
95 * of root IDs.
96 */
97 bool (*cache_lookup)(u64 leaf_bytenr, void *user_ctx,
98 const u64 **root_ids_ret, int *root_count_ret);
99 void (*cache_store)(u64 leaf_bytenr, const struct ulist *root_ids,
100 void *user_ctx);
101 /*
102 * If this is not NULL, then the backref walking code will call this
103 * for each indirect data extent reference as soon as it finds one,
104 * before collecting all the remaining backrefs and before resolving
105 * indirect backrefs. This allows for the caller to terminate backref
106 * walking as soon as it finds one backref that matches some specific
107 * criteria. The @cache_lookup and @cache_store callbacks should not
108 * be NULL in order to use this callback.
109 */
110 iterate_extent_inodes_t *indirect_ref_iterator;
111 /*
112 * If this is not NULL, then the backref walking code will call this for
113 * each extent item it's meant to process before it actually starts
114 * processing it. If this returns anything other than 0, then it stops
115 * the backref walking code immediately.
116 */
117 int (*check_extent_item)(u64 bytenr, const struct btrfs_extent_item *ei,
118 const struct extent_buffer *leaf, void *user_ctx);
119 /*
120 * If this is not NULL, then the backref walking code will call this for
121 * each extent data ref it finds (BTRFS_EXTENT_DATA_REF_KEY keys) before
122 * processing that data ref. If this callback return false, then it will
123 * ignore this data ref and it will never resolve the indirect data ref,
124 * saving time searching for leaves in a fs tree with file extent items
125 * matching the data ref.
126 */
127 bool (*skip_data_ref)(u64 root, u64 ino, u64 offset, void *user_ctx);
128 /* Context object to pass to the callbacks defined above. */
129 void *user_ctx;
130};
131
132struct inode_fs_paths {
133 struct btrfs_path *btrfs_path;
134 struct btrfs_root *fs_root;
135 struct btrfs_data_container *fspath;
136};
137
138struct btrfs_backref_shared_cache_entry {
139 u64 bytenr;
140 u64 gen;
141 bool is_shared;
142};
143
144#define BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE 8
145
146struct btrfs_backref_share_check_ctx {
147 /* Ulists used during backref walking. */
148 struct ulist refs;
149 /*
150 * The current leaf the caller of btrfs_is_data_extent_shared() is at.
151 * Typically the caller (at the moment only fiemap) tries to determine
152 * the sharedness of data extents point by file extent items from entire
153 * leaves.
154 */
155 u64 curr_leaf_bytenr;
156 /*
157 * The previous leaf the caller was at in the previous call to
158 * btrfs_is_data_extent_shared(). This may be the same as the current
159 * leaf. On the first call it must be 0.
160 */
161 u64 prev_leaf_bytenr;
162 /*
163 * A path from a root to a leaf that has a file extent item pointing to
164 * a given data extent should never exceed the maximum b+tree height.
165 */
166 struct btrfs_backref_shared_cache_entry path_cache_entries[BTRFS_MAX_LEVEL];
167 bool use_path_cache;
168 /*
169 * Cache the sharedness result for the last few extents we have found,
170 * but only for extents for which we have multiple file extent items
171 * that point to them.
172 * It's very common to have several file extent items that point to the
173 * same extent (bytenr) but with different offsets and lengths. This
174 * typically happens for COW writes, partial writes into prealloc
175 * extents, NOCOW writes after snapshoting a root, hole punching or
176 * reflinking within the same file (less common perhaps).
177 * So keep a small cache with the lookup results for the extent pointed
178 * by the last few file extent items. This cache is checked, with a
179 * linear scan, whenever btrfs_is_data_extent_shared() is called, so
180 * it must be small so that it does not negatively affect performance in
181 * case we don't have multiple file extent items that point to the same
182 * data extent.
183 */
184 struct {
185 u64 bytenr;
186 bool is_shared;
187 } prev_extents_cache[BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE];
188 /*
189 * The slot in the prev_extents_cache array that will be used for
190 * storing the sharedness result of a new data extent.
191 */
192 int prev_extents_cache_slot;
193};
194
195struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void);
196void btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx);
197
198int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
199 struct btrfs_path *path, struct btrfs_key *found_key,
200 u64 *flags);
201
202int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
203 struct btrfs_key *key, struct btrfs_extent_item *ei,
204 u32 item_size, u64 *out_root, u8 *out_level);
205
206int iterate_extent_inodes(struct btrfs_backref_walk_ctx *ctx,
207 bool search_commit_root,
208 iterate_extent_inodes_t *iterate, void *user_ctx);
209
210int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
211 struct btrfs_path *path, void *ctx,
212 bool ignore_offset);
213
214int paths_from_inode(u64 inum, struct inode_fs_paths *ipath);
215
216int btrfs_find_all_leafs(struct btrfs_backref_walk_ctx *ctx);
217int btrfs_find_all_roots(struct btrfs_backref_walk_ctx *ctx,
218 bool skip_commit_root_sem);
219char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
220 u32 name_len, unsigned long name_off,
221 struct extent_buffer *eb_in, u64 parent,
222 char *dest, u32 size);
223
224struct btrfs_data_container *init_data_container(u32 total_bytes);
225struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
226 struct btrfs_path *path);
227void free_ipath(struct inode_fs_paths *ipath);
228
229int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
230 u64 start_off, struct btrfs_path *path,
231 struct btrfs_inode_extref **ret_extref,
232 u64 *found_off);
233int btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr,
234 u64 extent_gen,
235 struct btrfs_backref_share_check_ctx *ctx);
236
237int __init btrfs_prelim_ref_init(void);
238void __cold btrfs_prelim_ref_exit(void);
239
240struct prelim_ref {
241 struct rb_node rbnode;
242 u64 root_id;
243 struct btrfs_key key_for_search;
244 int level;
245 int count;
246 struct extent_inode_elem *inode_list;
247 u64 parent;
248 u64 wanted_disk_byte;
249};
250
251/*
252 * Iterate backrefs of one extent.
253 *
254 * Now it only supports iteration of tree block in commit root.
255 */
256struct btrfs_backref_iter {
257 u64 bytenr;
258 struct btrfs_path *path;
259 struct btrfs_fs_info *fs_info;
260 struct btrfs_key cur_key;
261 u32 item_ptr;
262 u32 cur_ptr;
263 u32 end_ptr;
264};
265
266struct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *fs_info);
267
268static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter)
269{
270 if (!iter)
271 return;
272 btrfs_free_path(iter->path);
273 kfree(iter);
274}
275
276static inline struct extent_buffer *btrfs_backref_get_eb(
277 struct btrfs_backref_iter *iter)
278{
279 if (!iter)
280 return NULL;
281 return iter->path->nodes[0];
282}
283
284/*
285 * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data
286 * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header.
287 *
288 * This helper determines if that's the case.
289 */
290static inline bool btrfs_backref_has_tree_block_info(
291 struct btrfs_backref_iter *iter)
292{
293 if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY &&
294 iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item))
295 return true;
296 return false;
297}
298
299int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr);
300
301int btrfs_backref_iter_next(struct btrfs_backref_iter *iter);
302
303static inline bool btrfs_backref_iter_is_inline_ref(
304 struct btrfs_backref_iter *iter)
305{
306 if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY ||
307 iter->cur_key.type == BTRFS_METADATA_ITEM_KEY)
308 return true;
309 return false;
310}
311
312static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter)
313{
314 iter->bytenr = 0;
315 iter->item_ptr = 0;
316 iter->cur_ptr = 0;
317 iter->end_ptr = 0;
318 btrfs_release_path(iter->path);
319 memset(&iter->cur_key, 0, sizeof(iter->cur_key));
320}
321
322/*
323 * Backref cache related structures
324 *
325 * The whole objective of backref_cache is to build a bi-directional map
326 * of tree blocks (represented by backref_node) and all their parents.
327 */
328
329/*
330 * Represent a tree block in the backref cache
331 */
332struct btrfs_backref_node {
333 struct {
334 struct rb_node rb_node;
335 u64 bytenr;
336 }; /* Use rb_simple_node for search/insert */
337
338 u64 new_bytenr;
339 /* Objectid of tree block owner, can be not uptodate */
340 u64 owner;
341 /* Link to pending, changed or detached list */
342 struct list_head list;
343
344 /* List of upper level edges, which link this node to its parents */
345 struct list_head upper;
346 /* List of lower level edges, which link this node to its children */
347 struct list_head lower;
348
349 /* NULL if this node is not tree root */
350 struct btrfs_root *root;
351 /* Extent buffer got by COWing the block */
352 struct extent_buffer *eb;
353 /* Level of the tree block */
354 unsigned int level:8;
355 /* Is the block in a non-shareable tree */
356 unsigned int cowonly:1;
357 /* 1 if no child node is in the cache */
358 unsigned int lowest:1;
359 /* Is the extent buffer locked */
360 unsigned int locked:1;
361 /* Has the block been processed */
362 unsigned int processed:1;
363 /* Have backrefs of this block been checked */
364 unsigned int checked:1;
365 /*
366 * 1 if corresponding block has been COWed but some upper level block
367 * pointers may not point to the new location
368 */
369 unsigned int pending:1;
370 /* 1 if the backref node isn't connected to any other backref node */
371 unsigned int detached:1;
372
373 /*
374 * For generic purpose backref cache, where we only care if it's a reloc
375 * root, doesn't care the source subvolid.
376 */
377 unsigned int is_reloc_root:1;
378};
379
380#define LOWER 0
381#define UPPER 1
382
383/*
384 * Represent an edge connecting upper and lower backref nodes.
385 */
386struct btrfs_backref_edge {
387 /*
388 * list[LOWER] is linked to btrfs_backref_node::upper of lower level
389 * node, and list[UPPER] is linked to btrfs_backref_node::lower of
390 * upper level node.
391 *
392 * Also, build_backref_tree() uses list[UPPER] for pending edges, before
393 * linking list[UPPER] to its upper level nodes.
394 */
395 struct list_head list[2];
396
397 /* Two related nodes */
398 struct btrfs_backref_node *node[2];
399};
400
401struct btrfs_backref_cache {
402 /* Red black tree of all backref nodes in the cache */
403 struct rb_root rb_root;
404 /* For passing backref nodes to btrfs_reloc_cow_block */
405 struct btrfs_backref_node *path[BTRFS_MAX_LEVEL];
406 /*
407 * List of blocks that have been COWed but some block pointers in upper
408 * level blocks may not reflect the new location
409 */
410 struct list_head pending[BTRFS_MAX_LEVEL];
411 /* List of backref nodes with no child node */
412 struct list_head leaves;
413 /* List of blocks that have been COWed in current transaction */
414 struct list_head changed;
415 /* List of detached backref node. */
416 struct list_head detached;
417
418 u64 last_trans;
419
420 int nr_nodes;
421 int nr_edges;
422
423 /* List of unchecked backref edges during backref cache build */
424 struct list_head pending_edge;
425
426 /* List of useless backref nodes during backref cache build */
427 struct list_head useless_node;
428
429 struct btrfs_fs_info *fs_info;
430
431 /*
432 * Whether this cache is for relocation
433 *
434 * Reloction backref cache require more info for reloc root compared
435 * to generic backref cache.
436 */
437 unsigned int is_reloc;
438};
439
440void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
441 struct btrfs_backref_cache *cache, int is_reloc);
442struct btrfs_backref_node *btrfs_backref_alloc_node(
443 struct btrfs_backref_cache *cache, u64 bytenr, int level);
444struct btrfs_backref_edge *btrfs_backref_alloc_edge(
445 struct btrfs_backref_cache *cache);
446
447#define LINK_LOWER (1 << 0)
448#define LINK_UPPER (1 << 1)
449static inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge,
450 struct btrfs_backref_node *lower,
451 struct btrfs_backref_node *upper,
452 int link_which)
453{
454 ASSERT(upper && lower && upper->level == lower->level + 1);
455 edge->node[LOWER] = lower;
456 edge->node[UPPER] = upper;
457 if (link_which & LINK_LOWER)
458 list_add_tail(&edge->list[LOWER], &lower->upper);
459 if (link_which & LINK_UPPER)
460 list_add_tail(&edge->list[UPPER], &upper->lower);
461}
462
463static inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache,
464 struct btrfs_backref_node *node)
465{
466 if (node) {
467 ASSERT(list_empty(&node->list));
468 ASSERT(list_empty(&node->lower));
469 ASSERT(node->eb == NULL);
470 cache->nr_nodes--;
471 btrfs_put_root(node->root);
472 kfree(node);
473 }
474}
475
476static inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache,
477 struct btrfs_backref_edge *edge)
478{
479 if (edge) {
480 cache->nr_edges--;
481 kfree(edge);
482 }
483}
484
485static inline void btrfs_backref_unlock_node_buffer(
486 struct btrfs_backref_node *node)
487{
488 if (node->locked) {
489 btrfs_tree_unlock(node->eb);
490 node->locked = 0;
491 }
492}
493
494static inline void btrfs_backref_drop_node_buffer(
495 struct btrfs_backref_node *node)
496{
497 if (node->eb) {
498 btrfs_backref_unlock_node_buffer(node);
499 free_extent_buffer(node->eb);
500 node->eb = NULL;
501 }
502}
503
504/*
505 * Drop the backref node from cache without cleaning up its children
506 * edges.
507 *
508 * This can only be called on node without parent edges.
509 * The children edges are still kept as is.
510 */
511static inline void btrfs_backref_drop_node(struct btrfs_backref_cache *tree,
512 struct btrfs_backref_node *node)
513{
514 ASSERT(list_empty(&node->upper));
515
516 btrfs_backref_drop_node_buffer(node);
517 list_del_init(&node->list);
518 list_del_init(&node->lower);
519 if (!RB_EMPTY_NODE(&node->rb_node))
520 rb_erase(&node->rb_node, &tree->rb_root);
521 btrfs_backref_free_node(tree, node);
522}
523
524void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
525 struct btrfs_backref_node *node);
526
527void btrfs_backref_release_cache(struct btrfs_backref_cache *cache);
528
529static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,
530 u64 bytenr, int errno)
531{
532 btrfs_panic(fs_info, errno,
533 "Inconsistency in backref cache found at offset %llu",
534 bytenr);
535}
536
537int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache,
538 struct btrfs_path *path,
539 struct btrfs_backref_iter *iter,
540 struct btrfs_key *node_key,
541 struct btrfs_backref_node *cur);
542
543int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
544 struct btrfs_backref_node *start);
545
546void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,
547 struct btrfs_backref_node *node);
548
549#endif
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
14struct inode_fs_paths {
15 struct btrfs_path *btrfs_path;
16 struct btrfs_root *fs_root;
17 struct btrfs_data_container *fspath;
18};
19
20typedef int (iterate_extent_inodes_t)(u64 inum, u64 offset, u64 root,
21 void *ctx);
22
23int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical,
24 struct btrfs_path *path, struct btrfs_key *found_key,
25 u64 *flags);
26
27int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb,
28 struct btrfs_key *key, struct btrfs_extent_item *ei,
29 u32 item_size, u64 *out_root, u8 *out_level);
30
31int iterate_extent_inodes(struct btrfs_fs_info *fs_info,
32 u64 extent_item_objectid,
33 u64 extent_offset, int search_commit_root,
34 iterate_extent_inodes_t *iterate, void *ctx,
35 bool ignore_offset);
36
37int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info,
38 struct btrfs_path *path,
39 iterate_extent_inodes_t *iterate, void *ctx,
40 bool ignore_offset);
41
42int paths_from_inode(u64 inum, struct inode_fs_paths *ipath);
43
44int btrfs_find_all_leafs(struct btrfs_trans_handle *trans,
45 struct btrfs_fs_info *fs_info, u64 bytenr,
46 u64 time_seq, struct ulist **leafs,
47 const u64 *extent_item_pos, bool ignore_offset);
48int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
49 struct btrfs_fs_info *fs_info, u64 bytenr,
50 u64 time_seq, struct ulist **roots, bool ignore_offset);
51char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path,
52 u32 name_len, unsigned long name_off,
53 struct extent_buffer *eb_in, u64 parent,
54 char *dest, u32 size);
55
56struct btrfs_data_container *init_data_container(u32 total_bytes);
57struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root,
58 struct btrfs_path *path);
59void free_ipath(struct inode_fs_paths *ipath);
60
61int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
62 u64 start_off, struct btrfs_path *path,
63 struct btrfs_inode_extref **ret_extref,
64 u64 *found_off);
65int btrfs_check_shared(struct btrfs_root *root, u64 inum, u64 bytenr,
66 struct ulist *roots, struct ulist *tmp_ulist);
67
68int __init btrfs_prelim_ref_init(void);
69void __cold btrfs_prelim_ref_exit(void);
70
71struct prelim_ref {
72 struct rb_node rbnode;
73 u64 root_id;
74 struct btrfs_key key_for_search;
75 int level;
76 int count;
77 struct extent_inode_elem *inode_list;
78 u64 parent;
79 u64 wanted_disk_byte;
80};
81
82/*
83 * Iterate backrefs of one extent.
84 *
85 * Now it only supports iteration of tree block in commit root.
86 */
87struct btrfs_backref_iter {
88 u64 bytenr;
89 struct btrfs_path *path;
90 struct btrfs_fs_info *fs_info;
91 struct btrfs_key cur_key;
92 u32 item_ptr;
93 u32 cur_ptr;
94 u32 end_ptr;
95};
96
97struct btrfs_backref_iter *btrfs_backref_iter_alloc(
98 struct btrfs_fs_info *fs_info, gfp_t gfp_flag);
99
100static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter)
101{
102 if (!iter)
103 return;
104 btrfs_free_path(iter->path);
105 kfree(iter);
106}
107
108static inline struct extent_buffer *btrfs_backref_get_eb(
109 struct btrfs_backref_iter *iter)
110{
111 if (!iter)
112 return NULL;
113 return iter->path->nodes[0];
114}
115
116/*
117 * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data
118 * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header.
119 *
120 * This helper determines if that's the case.
121 */
122static inline bool btrfs_backref_has_tree_block_info(
123 struct btrfs_backref_iter *iter)
124{
125 if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY &&
126 iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item))
127 return true;
128 return false;
129}
130
131int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr);
132
133int btrfs_backref_iter_next(struct btrfs_backref_iter *iter);
134
135static inline bool btrfs_backref_iter_is_inline_ref(
136 struct btrfs_backref_iter *iter)
137{
138 if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY ||
139 iter->cur_key.type == BTRFS_METADATA_ITEM_KEY)
140 return true;
141 return false;
142}
143
144static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter)
145{
146 iter->bytenr = 0;
147 iter->item_ptr = 0;
148 iter->cur_ptr = 0;
149 iter->end_ptr = 0;
150 btrfs_release_path(iter->path);
151 memset(&iter->cur_key, 0, sizeof(iter->cur_key));
152}
153
154/*
155 * Backref cache related structures
156 *
157 * The whole objective of backref_cache is to build a bi-directional map
158 * of tree blocks (represented by backref_node) and all their parents.
159 */
160
161/*
162 * Represent a tree block in the backref cache
163 */
164struct btrfs_backref_node {
165 struct {
166 struct rb_node rb_node;
167 u64 bytenr;
168 }; /* Use rb_simple_node for search/insert */
169
170 u64 new_bytenr;
171 /* Objectid of tree block owner, can be not uptodate */
172 u64 owner;
173 /* Link to pending, changed or detached list */
174 struct list_head list;
175
176 /* List of upper level edges, which link this node to its parents */
177 struct list_head upper;
178 /* List of lower level edges, which link this node to its children */
179 struct list_head lower;
180
181 /* NULL if this node is not tree root */
182 struct btrfs_root *root;
183 /* Extent buffer got by COWing the block */
184 struct extent_buffer *eb;
185 /* Level of the tree block */
186 unsigned int level:8;
187 /* Is the block in a non-shareable tree */
188 unsigned int cowonly:1;
189 /* 1 if no child node is in the cache */
190 unsigned int lowest:1;
191 /* Is the extent buffer locked */
192 unsigned int locked:1;
193 /* Has the block been processed */
194 unsigned int processed:1;
195 /* Have backrefs of this block been checked */
196 unsigned int checked:1;
197 /*
198 * 1 if corresponding block has been COWed but some upper level block
199 * pointers may not point to the new location
200 */
201 unsigned int pending:1;
202 /* 1 if the backref node isn't connected to any other backref node */
203 unsigned int detached:1;
204
205 /*
206 * For generic purpose backref cache, where we only care if it's a reloc
207 * root, doesn't care the source subvolid.
208 */
209 unsigned int is_reloc_root:1;
210};
211
212#define LOWER 0
213#define UPPER 1
214
215/*
216 * Represent an edge connecting upper and lower backref nodes.
217 */
218struct btrfs_backref_edge {
219 /*
220 * list[LOWER] is linked to btrfs_backref_node::upper of lower level
221 * node, and list[UPPER] is linked to btrfs_backref_node::lower of
222 * upper level node.
223 *
224 * Also, build_backref_tree() uses list[UPPER] for pending edges, before
225 * linking list[UPPER] to its upper level nodes.
226 */
227 struct list_head list[2];
228
229 /* Two related nodes */
230 struct btrfs_backref_node *node[2];
231};
232
233struct btrfs_backref_cache {
234 /* Red black tree of all backref nodes in the cache */
235 struct rb_root rb_root;
236 /* For passing backref nodes to btrfs_reloc_cow_block */
237 struct btrfs_backref_node *path[BTRFS_MAX_LEVEL];
238 /*
239 * List of blocks that have been COWed but some block pointers in upper
240 * level blocks may not reflect the new location
241 */
242 struct list_head pending[BTRFS_MAX_LEVEL];
243 /* List of backref nodes with no child node */
244 struct list_head leaves;
245 /* List of blocks that have been COWed in current transaction */
246 struct list_head changed;
247 /* List of detached backref node. */
248 struct list_head detached;
249
250 u64 last_trans;
251
252 int nr_nodes;
253 int nr_edges;
254
255 /* List of unchecked backref edges during backref cache build */
256 struct list_head pending_edge;
257
258 /* List of useless backref nodes during backref cache build */
259 struct list_head useless_node;
260
261 struct btrfs_fs_info *fs_info;
262
263 /*
264 * Whether this cache is for relocation
265 *
266 * Reloction backref cache require more info for reloc root compared
267 * to generic backref cache.
268 */
269 unsigned int is_reloc;
270};
271
272void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info,
273 struct btrfs_backref_cache *cache, int is_reloc);
274struct btrfs_backref_node *btrfs_backref_alloc_node(
275 struct btrfs_backref_cache *cache, u64 bytenr, int level);
276struct btrfs_backref_edge *btrfs_backref_alloc_edge(
277 struct btrfs_backref_cache *cache);
278
279#define LINK_LOWER (1 << 0)
280#define LINK_UPPER (1 << 1)
281static inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge,
282 struct btrfs_backref_node *lower,
283 struct btrfs_backref_node *upper,
284 int link_which)
285{
286 ASSERT(upper && lower && upper->level == lower->level + 1);
287 edge->node[LOWER] = lower;
288 edge->node[UPPER] = upper;
289 if (link_which & LINK_LOWER)
290 list_add_tail(&edge->list[LOWER], &lower->upper);
291 if (link_which & LINK_UPPER)
292 list_add_tail(&edge->list[UPPER], &upper->lower);
293}
294
295static inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache,
296 struct btrfs_backref_node *node)
297{
298 if (node) {
299 cache->nr_nodes--;
300 btrfs_put_root(node->root);
301 kfree(node);
302 }
303}
304
305static inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache,
306 struct btrfs_backref_edge *edge)
307{
308 if (edge) {
309 cache->nr_edges--;
310 kfree(edge);
311 }
312}
313
314static inline void btrfs_backref_unlock_node_buffer(
315 struct btrfs_backref_node *node)
316{
317 if (node->locked) {
318 btrfs_tree_unlock(node->eb);
319 node->locked = 0;
320 }
321}
322
323static inline void btrfs_backref_drop_node_buffer(
324 struct btrfs_backref_node *node)
325{
326 if (node->eb) {
327 btrfs_backref_unlock_node_buffer(node);
328 free_extent_buffer(node->eb);
329 node->eb = NULL;
330 }
331}
332
333/*
334 * Drop the backref node from cache without cleaning up its children
335 * edges.
336 *
337 * This can only be called on node without parent edges.
338 * The children edges are still kept as is.
339 */
340static inline void btrfs_backref_drop_node(struct btrfs_backref_cache *tree,
341 struct btrfs_backref_node *node)
342{
343 BUG_ON(!list_empty(&node->upper));
344
345 btrfs_backref_drop_node_buffer(node);
346 list_del(&node->list);
347 list_del(&node->lower);
348 if (!RB_EMPTY_NODE(&node->rb_node))
349 rb_erase(&node->rb_node, &tree->rb_root);
350 btrfs_backref_free_node(tree, node);
351}
352
353void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache,
354 struct btrfs_backref_node *node);
355
356void btrfs_backref_release_cache(struct btrfs_backref_cache *cache);
357
358static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info,
359 u64 bytenr, int errno)
360{
361 btrfs_panic(fs_info, errno,
362 "Inconsistency in backref cache found at offset %llu",
363 bytenr);
364}
365
366int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache,
367 struct btrfs_path *path,
368 struct btrfs_backref_iter *iter,
369 struct btrfs_key *node_key,
370 struct btrfs_backref_node *cur);
371
372int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache,
373 struct btrfs_backref_node *start);
374
375void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache,
376 struct btrfs_backref_node *node);
377
378#endif