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 */ 31 typedef 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 */ 39 struct 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 * Context object to pass to the @cache_lookup, @cache_store and 113 * @indirect_ref_iterator callbacks. 114 */ 115 void *user_ctx; 116 }; 117 118 struct inode_fs_paths { 119 struct btrfs_path *btrfs_path; 120 struct btrfs_root *fs_root; 121 struct btrfs_data_container *fspath; 122 }; 123 124 struct btrfs_backref_shared_cache_entry { 125 u64 bytenr; 126 u64 gen; 127 bool is_shared; 128 }; 129 130 #define BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE 8 131 132 struct btrfs_backref_share_check_ctx { 133 /* Ulists used during backref walking. */ 134 struct ulist refs; 135 /* 136 * The current leaf the caller of btrfs_is_data_extent_shared() is at. 137 * Typically the caller (at the moment only fiemap) tries to determine 138 * the sharedness of data extents point by file extent items from entire 139 * leaves. 140 */ 141 u64 curr_leaf_bytenr; 142 /* 143 * The previous leaf the caller was at in the previous call to 144 * btrfs_is_data_extent_shared(). This may be the same as the current 145 * leaf. On the first call it must be 0. 146 */ 147 u64 prev_leaf_bytenr; 148 /* 149 * A path from a root to a leaf that has a file extent item pointing to 150 * a given data extent should never exceed the maximum b+tree height. 151 */ 152 struct btrfs_backref_shared_cache_entry path_cache_entries[BTRFS_MAX_LEVEL]; 153 bool use_path_cache; 154 /* 155 * Cache the sharedness result for the last few extents we have found, 156 * but only for extents for which we have multiple file extent items 157 * that point to them. 158 * It's very common to have several file extent items that point to the 159 * same extent (bytenr) but with different offsets and lengths. This 160 * typically happens for COW writes, partial writes into prealloc 161 * extents, NOCOW writes after snapshoting a root, hole punching or 162 * reflinking within the same file (less common perhaps). 163 * So keep a small cache with the lookup results for the extent pointed 164 * by the last few file extent items. This cache is checked, with a 165 * linear scan, whenever btrfs_is_data_extent_shared() is called, so 166 * it must be small so that it does not negatively affect performance in 167 * case we don't have multiple file extent items that point to the same 168 * data extent. 169 */ 170 struct { 171 u64 bytenr; 172 bool is_shared; 173 } prev_extents_cache[BTRFS_BACKREF_CTX_PREV_EXTENTS_SIZE]; 174 /* 175 * The slot in the prev_extents_cache array that will be used for 176 * storing the sharedness result of a new data extent. 177 */ 178 int prev_extents_cache_slot; 179 }; 180 181 struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void); 182 void btrfs_free_backref_share_ctx(struct btrfs_backref_share_check_ctx *ctx); 183 184 int extent_from_logical(struct btrfs_fs_info *fs_info, u64 logical, 185 struct btrfs_path *path, struct btrfs_key *found_key, 186 u64 *flags); 187 188 int tree_backref_for_extent(unsigned long *ptr, struct extent_buffer *eb, 189 struct btrfs_key *key, struct btrfs_extent_item *ei, 190 u32 item_size, u64 *out_root, u8 *out_level); 191 192 int iterate_extent_inodes(struct btrfs_backref_walk_ctx *ctx, 193 bool search_commit_root, 194 iterate_extent_inodes_t *iterate, void *user_ctx); 195 196 int iterate_inodes_from_logical(u64 logical, struct btrfs_fs_info *fs_info, 197 struct btrfs_path *path, void *ctx, 198 bool ignore_offset); 199 200 int paths_from_inode(u64 inum, struct inode_fs_paths *ipath); 201 202 int btrfs_find_all_leafs(struct btrfs_backref_walk_ctx *ctx); 203 int btrfs_find_all_roots(struct btrfs_backref_walk_ctx *ctx, 204 bool skip_commit_root_sem); 205 char *btrfs_ref_to_path(struct btrfs_root *fs_root, struct btrfs_path *path, 206 u32 name_len, unsigned long name_off, 207 struct extent_buffer *eb_in, u64 parent, 208 char *dest, u32 size); 209 210 struct btrfs_data_container *init_data_container(u32 total_bytes); 211 struct inode_fs_paths *init_ipath(s32 total_bytes, struct btrfs_root *fs_root, 212 struct btrfs_path *path); 213 void free_ipath(struct inode_fs_paths *ipath); 214 215 int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid, 216 u64 start_off, struct btrfs_path *path, 217 struct btrfs_inode_extref **ret_extref, 218 u64 *found_off); 219 int btrfs_is_data_extent_shared(struct btrfs_inode *inode, u64 bytenr, 220 u64 extent_gen, 221 struct btrfs_backref_share_check_ctx *ctx); 222 223 int __init btrfs_prelim_ref_init(void); 224 void __cold btrfs_prelim_ref_exit(void); 225 226 struct prelim_ref { 227 struct rb_node rbnode; 228 u64 root_id; 229 struct btrfs_key key_for_search; 230 int level; 231 int count; 232 struct extent_inode_elem *inode_list; 233 u64 parent; 234 u64 wanted_disk_byte; 235 }; 236 237 /* 238 * Iterate backrefs of one extent. 239 * 240 * Now it only supports iteration of tree block in commit root. 241 */ 242 struct btrfs_backref_iter { 243 u64 bytenr; 244 struct btrfs_path *path; 245 struct btrfs_fs_info *fs_info; 246 struct btrfs_key cur_key; 247 u32 item_ptr; 248 u32 cur_ptr; 249 u32 end_ptr; 250 }; 251 252 struct btrfs_backref_iter *btrfs_backref_iter_alloc(struct btrfs_fs_info *fs_info); 253 254 static inline void btrfs_backref_iter_free(struct btrfs_backref_iter *iter) 255 { 256 if (!iter) 257 return; 258 btrfs_free_path(iter->path); 259 kfree(iter); 260 } 261 262 static inline struct extent_buffer *btrfs_backref_get_eb( 263 struct btrfs_backref_iter *iter) 264 { 265 if (!iter) 266 return NULL; 267 return iter->path->nodes[0]; 268 } 269 270 /* 271 * For metadata with EXTENT_ITEM key (non-skinny) case, the first inline data 272 * is btrfs_tree_block_info, without a btrfs_extent_inline_ref header. 273 * 274 * This helper determines if that's the case. 275 */ 276 static inline bool btrfs_backref_has_tree_block_info( 277 struct btrfs_backref_iter *iter) 278 { 279 if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY && 280 iter->cur_ptr - iter->item_ptr == sizeof(struct btrfs_extent_item)) 281 return true; 282 return false; 283 } 284 285 int btrfs_backref_iter_start(struct btrfs_backref_iter *iter, u64 bytenr); 286 287 int btrfs_backref_iter_next(struct btrfs_backref_iter *iter); 288 289 static inline bool btrfs_backref_iter_is_inline_ref( 290 struct btrfs_backref_iter *iter) 291 { 292 if (iter->cur_key.type == BTRFS_EXTENT_ITEM_KEY || 293 iter->cur_key.type == BTRFS_METADATA_ITEM_KEY) 294 return true; 295 return false; 296 } 297 298 static inline void btrfs_backref_iter_release(struct btrfs_backref_iter *iter) 299 { 300 iter->bytenr = 0; 301 iter->item_ptr = 0; 302 iter->cur_ptr = 0; 303 iter->end_ptr = 0; 304 btrfs_release_path(iter->path); 305 memset(&iter->cur_key, 0, sizeof(iter->cur_key)); 306 } 307 308 /* 309 * Backref cache related structures 310 * 311 * The whole objective of backref_cache is to build a bi-directional map 312 * of tree blocks (represented by backref_node) and all their parents. 313 */ 314 315 /* 316 * Represent a tree block in the backref cache 317 */ 318 struct btrfs_backref_node { 319 struct { 320 struct rb_node rb_node; 321 u64 bytenr; 322 }; /* Use rb_simple_node for search/insert */ 323 324 u64 new_bytenr; 325 /* Objectid of tree block owner, can be not uptodate */ 326 u64 owner; 327 /* Link to pending, changed or detached list */ 328 struct list_head list; 329 330 /* List of upper level edges, which link this node to its parents */ 331 struct list_head upper; 332 /* List of lower level edges, which link this node to its children */ 333 struct list_head lower; 334 335 /* NULL if this node is not tree root */ 336 struct btrfs_root *root; 337 /* Extent buffer got by COWing the block */ 338 struct extent_buffer *eb; 339 /* Level of the tree block */ 340 unsigned int level:8; 341 /* Is the block in a non-shareable tree */ 342 unsigned int cowonly:1; 343 /* 1 if no child node is in the cache */ 344 unsigned int lowest:1; 345 /* Is the extent buffer locked */ 346 unsigned int locked:1; 347 /* Has the block been processed */ 348 unsigned int processed:1; 349 /* Have backrefs of this block been checked */ 350 unsigned int checked:1; 351 /* 352 * 1 if corresponding block has been COWed but some upper level block 353 * pointers may not point to the new location 354 */ 355 unsigned int pending:1; 356 /* 1 if the backref node isn't connected to any other backref node */ 357 unsigned int detached:1; 358 359 /* 360 * For generic purpose backref cache, where we only care if it's a reloc 361 * root, doesn't care the source subvolid. 362 */ 363 unsigned int is_reloc_root:1; 364 }; 365 366 #define LOWER 0 367 #define UPPER 1 368 369 /* 370 * Represent an edge connecting upper and lower backref nodes. 371 */ 372 struct btrfs_backref_edge { 373 /* 374 * list[LOWER] is linked to btrfs_backref_node::upper of lower level 375 * node, and list[UPPER] is linked to btrfs_backref_node::lower of 376 * upper level node. 377 * 378 * Also, build_backref_tree() uses list[UPPER] for pending edges, before 379 * linking list[UPPER] to its upper level nodes. 380 */ 381 struct list_head list[2]; 382 383 /* Two related nodes */ 384 struct btrfs_backref_node *node[2]; 385 }; 386 387 struct btrfs_backref_cache { 388 /* Red black tree of all backref nodes in the cache */ 389 struct rb_root rb_root; 390 /* For passing backref nodes to btrfs_reloc_cow_block */ 391 struct btrfs_backref_node *path[BTRFS_MAX_LEVEL]; 392 /* 393 * List of blocks that have been COWed but some block pointers in upper 394 * level blocks may not reflect the new location 395 */ 396 struct list_head pending[BTRFS_MAX_LEVEL]; 397 /* List of backref nodes with no child node */ 398 struct list_head leaves; 399 /* List of blocks that have been COWed in current transaction */ 400 struct list_head changed; 401 /* List of detached backref node. */ 402 struct list_head detached; 403 404 u64 last_trans; 405 406 int nr_nodes; 407 int nr_edges; 408 409 /* List of unchecked backref edges during backref cache build */ 410 struct list_head pending_edge; 411 412 /* List of useless backref nodes during backref cache build */ 413 struct list_head useless_node; 414 415 struct btrfs_fs_info *fs_info; 416 417 /* 418 * Whether this cache is for relocation 419 * 420 * Reloction backref cache require more info for reloc root compared 421 * to generic backref cache. 422 */ 423 unsigned int is_reloc; 424 }; 425 426 void btrfs_backref_init_cache(struct btrfs_fs_info *fs_info, 427 struct btrfs_backref_cache *cache, int is_reloc); 428 struct btrfs_backref_node *btrfs_backref_alloc_node( 429 struct btrfs_backref_cache *cache, u64 bytenr, int level); 430 struct btrfs_backref_edge *btrfs_backref_alloc_edge( 431 struct btrfs_backref_cache *cache); 432 433 #define LINK_LOWER (1 << 0) 434 #define LINK_UPPER (1 << 1) 435 static inline void btrfs_backref_link_edge(struct btrfs_backref_edge *edge, 436 struct btrfs_backref_node *lower, 437 struct btrfs_backref_node *upper, 438 int link_which) 439 { 440 ASSERT(upper && lower && upper->level == lower->level + 1); 441 edge->node[LOWER] = lower; 442 edge->node[UPPER] = upper; 443 if (link_which & LINK_LOWER) 444 list_add_tail(&edge->list[LOWER], &lower->upper); 445 if (link_which & LINK_UPPER) 446 list_add_tail(&edge->list[UPPER], &upper->lower); 447 } 448 449 static inline void btrfs_backref_free_node(struct btrfs_backref_cache *cache, 450 struct btrfs_backref_node *node) 451 { 452 if (node) { 453 ASSERT(list_empty(&node->list)); 454 ASSERT(list_empty(&node->lower)); 455 ASSERT(node->eb == NULL); 456 cache->nr_nodes--; 457 btrfs_put_root(node->root); 458 kfree(node); 459 } 460 } 461 462 static inline void btrfs_backref_free_edge(struct btrfs_backref_cache *cache, 463 struct btrfs_backref_edge *edge) 464 { 465 if (edge) { 466 cache->nr_edges--; 467 kfree(edge); 468 } 469 } 470 471 static inline void btrfs_backref_unlock_node_buffer( 472 struct btrfs_backref_node *node) 473 { 474 if (node->locked) { 475 btrfs_tree_unlock(node->eb); 476 node->locked = 0; 477 } 478 } 479 480 static inline void btrfs_backref_drop_node_buffer( 481 struct btrfs_backref_node *node) 482 { 483 if (node->eb) { 484 btrfs_backref_unlock_node_buffer(node); 485 free_extent_buffer(node->eb); 486 node->eb = NULL; 487 } 488 } 489 490 /* 491 * Drop the backref node from cache without cleaning up its children 492 * edges. 493 * 494 * This can only be called on node without parent edges. 495 * The children edges are still kept as is. 496 */ 497 static inline void btrfs_backref_drop_node(struct btrfs_backref_cache *tree, 498 struct btrfs_backref_node *node) 499 { 500 ASSERT(list_empty(&node->upper)); 501 502 btrfs_backref_drop_node_buffer(node); 503 list_del_init(&node->list); 504 list_del_init(&node->lower); 505 if (!RB_EMPTY_NODE(&node->rb_node)) 506 rb_erase(&node->rb_node, &tree->rb_root); 507 btrfs_backref_free_node(tree, node); 508 } 509 510 void btrfs_backref_cleanup_node(struct btrfs_backref_cache *cache, 511 struct btrfs_backref_node *node); 512 513 void btrfs_backref_release_cache(struct btrfs_backref_cache *cache); 514 515 static inline void btrfs_backref_panic(struct btrfs_fs_info *fs_info, 516 u64 bytenr, int errno) 517 { 518 btrfs_panic(fs_info, errno, 519 "Inconsistency in backref cache found at offset %llu", 520 bytenr); 521 } 522 523 int btrfs_backref_add_tree_node(struct btrfs_backref_cache *cache, 524 struct btrfs_path *path, 525 struct btrfs_backref_iter *iter, 526 struct btrfs_key *node_key, 527 struct btrfs_backref_node *cur); 528 529 int btrfs_backref_finish_upper_links(struct btrfs_backref_cache *cache, 530 struct btrfs_backref_node *start); 531 532 void btrfs_backref_error_cleanup(struct btrfs_backref_cache *cache, 533 struct btrfs_backref_node *node); 534 535 #endif 536