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