1 /* SPDX-License-Identifier: GPL-2.0 */ 1 2 /* 3 * Copyright (C) 2007 Oracle. All rights rese 4 */ 5 6 #ifndef BTRFS_CTREE_H 7 #define BTRFS_CTREE_H 8 9 #include "linux/cleanup.h" 10 #include <linux/pagemap.h> 11 #include <linux/spinlock.h> 12 #include <linux/rbtree.h> 13 #include <linux/mutex.h> 14 #include <linux/wait.h> 15 #include <linux/list.h> 16 #include <linux/atomic.h> 17 #include <linux/xarray.h> 18 #include <linux/refcount.h> 19 #include <uapi/linux/btrfs_tree.h> 20 #include "locking.h" 21 #include "fs.h" 22 #include "accessors.h" 23 #include "extent-io-tree.h" 24 25 struct extent_buffer; 26 struct btrfs_block_rsv; 27 struct btrfs_trans_handle; 28 struct btrfs_block_group; 29 30 /* Read ahead values for struct btrfs_path.rea 31 enum { 32 READA_NONE, 33 READA_BACK, 34 READA_FORWARD, 35 /* 36 * Similar to READA_FORWARD but unlike 37 * 38 * 1) It will trigger readahead even f 39 * each other on disk; 40 * 2) It also triggers readahead for n 41 * 3) During a search, even when a nod 42 * will still trigger readahead for 43 * it. 44 * 45 * This is meant to be used only when 46 * entire tree or a very large part of 47 */ 48 READA_FORWARD_ALWAYS, 49 }; 50 51 /* 52 * btrfs_paths remember the path taken from th 53 * level 0 is always the leaf, and nodes[1...B 54 * to any other levels that are present. 55 * 56 * The slots array records the index of the it 57 * used while walking the tree. 58 */ 59 struct btrfs_path { 60 struct extent_buffer *nodes[BTRFS_MAX_ 61 int slots[BTRFS_MAX_LEVEL]; 62 /* if there is real range locking, thi 63 u8 locks[BTRFS_MAX_LEVEL]; 64 u8 reada; 65 /* keep some upper locks as we walk do 66 u8 lowest_level; 67 68 /* 69 * set by btrfs_split_item, tells sear 70 * and to force calls to keep space in 71 */ 72 unsigned int search_for_split:1; 73 unsigned int keep_locks:1; 74 unsigned int skip_locking:1; 75 unsigned int search_commit_root:1; 76 unsigned int need_commit_sem:1; 77 unsigned int skip_release_on_error:1; 78 /* 79 * Indicate that new item (btrfs_searc 80 * existing item and ins_len contains 81 * header (ie. sizeof(struct btrfs_ite 82 */ 83 unsigned int search_for_extension:1; 84 /* Stop search if any locks need to be 85 unsigned int nowait:1; 86 }; 87 88 #define BTRFS_PATH_AUTO_FREE(path_name) 89 struct btrfs_path *path_name __free(bt 90 91 /* 92 * The state of btrfs root 93 */ 94 enum { 95 /* 96 * btrfs_record_root_in_trans is a mul 97 * with the balancing code. But the 98 * first time the root is added to eac 99 * is used to tell us when more checks 100 */ 101 BTRFS_ROOT_IN_TRANS_SETUP, 102 103 /* 104 * Set if tree blocks of this root can 105 * Only subvolume trees and their relo 106 * Conflicts with TRACK_DIRTY bit. 107 * 108 * This affects two things: 109 * 110 * - How balance works 111 * For shareable roots, we need to u 112 * replacement for balance, and need 113 * snapshot creation to handle them. 114 * 115 * While for non-shareable trees, we 116 * with COW. 117 * 118 * - How dirty roots are tracked 119 * For shareable roots, btrfs_record 120 * track them, while non-subvolume r 121 * don't need to set this manually. 122 */ 123 BTRFS_ROOT_SHAREABLE, 124 BTRFS_ROOT_TRACK_DIRTY, 125 BTRFS_ROOT_IN_RADIX, 126 BTRFS_ROOT_ORPHAN_ITEM_INSERTED, 127 BTRFS_ROOT_DEFRAG_RUNNING, 128 BTRFS_ROOT_FORCE_COW, 129 BTRFS_ROOT_MULTI_LOG_TASKS, 130 BTRFS_ROOT_DIRTY, 131 BTRFS_ROOT_DELETING, 132 133 /* 134 * Reloc tree is orphan, only kept her 135 * 136 * Set for the subvolume tree owning t 137 */ 138 BTRFS_ROOT_DEAD_RELOC_TREE, 139 /* Mark dead root stored on device who 140 BTRFS_ROOT_DEAD_TREE, 141 /* The root has a log tree. Used for s 142 BTRFS_ROOT_HAS_LOG_TREE, 143 /* Qgroup flushing is in progress */ 144 BTRFS_ROOT_QGROUP_FLUSHING, 145 /* We started the orphan cleanup for t 146 BTRFS_ROOT_ORPHAN_CLEANUP, 147 /* This root has a drop operation that 148 BTRFS_ROOT_UNFINISHED_DROP, 149 /* This reloc root needs to have its b 150 BTRFS_ROOT_RESET_LOCKDEP_CLASS, 151 }; 152 153 /* 154 * Record swapped tree blocks of a subvolume t 155 * code. For detail check comment in fs/btrfs/ 156 */ 157 struct btrfs_qgroup_swapped_blocks { 158 spinlock_t lock; 159 /* RM_EMPTY_ROOT() of above blocks[] * 160 bool swapped; 161 struct rb_root blocks[BTRFS_MAX_LEVEL] 162 }; 163 164 /* 165 * in ram representation of the tree. extent_ 166 * and for the extent tree extent_root root. 167 */ 168 struct btrfs_root { 169 struct rb_node rb_node; 170 171 struct extent_buffer *node; 172 173 struct extent_buffer *commit_root; 174 struct btrfs_root *log_root; 175 struct btrfs_root *reloc_root; 176 177 unsigned long state; 178 struct btrfs_root_item root_item; 179 struct btrfs_key root_key; 180 struct btrfs_fs_info *fs_info; 181 struct extent_io_tree dirty_log_pages; 182 183 struct mutex objectid_mutex; 184 185 spinlock_t accounting_lock; 186 struct btrfs_block_rsv *block_rsv; 187 188 struct mutex log_mutex; 189 wait_queue_head_t log_writer_wait; 190 wait_queue_head_t log_commit_wait[2]; 191 struct list_head log_ctxs[2]; 192 /* Used only for log trees of subvolum 193 atomic_t log_writers; 194 atomic_t log_commit[2]; 195 /* Used only for log trees of subvolum 196 atomic_t log_batch; 197 /* 198 * Protected by the 'log_mutex' lock b 199 * that lock to avoid unnecessary lock 200 * should be read using btrfs_get_root 201 * log tree in which case it can be di 202 * field should always use btrfs_set_r 203 * trees where the field can be update 204 */ 205 int log_transid; 206 /* No matter the commit succeeds or no 207 int log_transid_committed; 208 /* 209 * Just be updated when the commit suc 210 * btrfs_get_root_last_log_commit() an 211 * to access this field. 212 */ 213 int last_log_commit; 214 pid_t log_start_pid; 215 216 u64 last_trans; 217 218 u64 free_objectid; 219 220 struct btrfs_key defrag_progress; 221 struct btrfs_key defrag_max; 222 223 /* The dirty list is only used by non- 224 struct list_head dirty_list; 225 226 struct list_head root_list; 227 228 /* 229 * Xarray that keeps track of in-memor 230 * @inode_lock. 231 */ 232 struct xarray inodes; 233 234 /* 235 * Xarray that keeps track of delayed 236 * by @inode_lock. 237 */ 238 struct xarray delayed_nodes; 239 /* 240 * right now this just gets used so th 241 * for stat. It may be used for more 242 */ 243 dev_t anon_dev; 244 245 spinlock_t root_item_lock; 246 refcount_t refs; 247 248 struct mutex delalloc_mutex; 249 spinlock_t delalloc_lock; 250 /* 251 * all of the inodes that have delallo 252 * this list to be empty even when the 253 * extents waiting to finish IO. 254 */ 255 struct list_head delalloc_inodes; 256 struct list_head delalloc_root; 257 u64 nr_delalloc_inodes; 258 259 struct mutex ordered_extent_mutex; 260 /* 261 * this is used by the balancing code 262 * ordered extents 263 */ 264 spinlock_t ordered_extent_lock; 265 266 /* 267 * all of the data=ordered extents pen 268 * these can span multiple transaction 269 * every dirty data page that isn't fr 270 */ 271 struct list_head ordered_extents; 272 struct list_head ordered_root; 273 u64 nr_ordered_extents; 274 275 /* 276 * Not empty if this subvolume root ha 277 * (relocation) 278 * 279 * Will be used by reloc_control::dirt 280 */ 281 struct list_head reloc_dirty_list; 282 283 /* 284 * Number of currently running SEND io 285 * manipulation with the read-only sta 286 */ 287 int send_in_progress; 288 /* 289 * Number of currently running dedupli 290 * destination inode belonging to this 291 * root_item_lock. 292 */ 293 int dedupe_in_progress; 294 /* For exclusion of snapshot creation 295 struct btrfs_drew_lock snapshot_lock; 296 297 atomic_t snapshot_force_cow; 298 299 /* For qgroup metadata reserved space 300 spinlock_t qgroup_meta_rsv_lock; 301 u64 qgroup_meta_rsv_pertrans; 302 u64 qgroup_meta_rsv_prealloc; 303 wait_queue_head_t qgroup_flush_wait; 304 305 /* Number of active swapfiles */ 306 atomic_t nr_swapfiles; 307 308 /* Record pairs of swapped blocks for 309 struct btrfs_qgroup_swapped_blocks swa 310 311 /* Used only by log trees, when loggin 312 struct extent_io_tree log_csum_range; 313 314 /* Used in simple quotas, track root d 315 u64 relocation_src_root; 316 317 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 318 u64 alloc_bytenr; 319 #endif 320 321 #ifdef CONFIG_BTRFS_DEBUG 322 struct list_head leak_list; 323 #endif 324 }; 325 326 static inline bool btrfs_root_readonly(const s 327 { 328 /* Byte-swap the constant at compile t 329 return (root->root_item.flags & cpu_to 330 } 331 332 static inline bool btrfs_root_dead(const struc 333 { 334 /* Byte-swap the constant at compile t 335 return (root->root_item.flags & cpu_to 336 } 337 338 static inline u64 btrfs_root_id(const struct b 339 { 340 return root->root_key.objectid; 341 } 342 343 static inline int btrfs_get_root_log_transid(c 344 { 345 return READ_ONCE(root->log_transid); 346 } 347 348 static inline void btrfs_set_root_log_transid( 349 { 350 WRITE_ONCE(root->log_transid, log_tran 351 } 352 353 static inline int btrfs_get_root_last_log_comm 354 { 355 return READ_ONCE(root->last_log_commit 356 } 357 358 static inline void btrfs_set_root_last_log_com 359 { 360 WRITE_ONCE(root->last_log_commit, comm 361 } 362 363 static inline u64 btrfs_get_root_last_trans(co 364 { 365 return READ_ONCE(root->last_trans); 366 } 367 368 static inline void btrfs_set_root_last_trans(s 369 { 370 WRITE_ONCE(root->last_trans, transid); 371 } 372 373 /* 374 * Structure that conveys information about an 375 * all the extents in a file range. 376 */ 377 struct btrfs_replace_extent_info { 378 u64 disk_offset; 379 u64 disk_len; 380 u64 data_offset; 381 u64 data_len; 382 u64 file_offset; 383 /* Pointer to a file extent item of ty 384 char *extent_buf; 385 /* 386 * Set to true when attempting to repl 387 * described by this structure, set to 388 * existing extent into a file range. 389 */ 390 bool is_new_extent; 391 /* Indicate if we should update the in 392 bool update_times; 393 /* Meaningful only if is_new_extent is 394 int qgroup_reserved; 395 /* 396 * Meaningful only if is_new_extent is 397 * Used to track how many extent items 398 * subvolume tree that refer to the ex 399 * so that we know when to create a ne 400 * one. 401 */ 402 int insertions; 403 }; 404 405 /* Arguments for btrfs_drop_extents() */ 406 struct btrfs_drop_extents_args { 407 /* Input parameters */ 408 409 /* 410 * If NULL, btrfs_drop_extents() will 411 * If 'replace_extent' is true, this m 412 * is always released except if 'repla 413 * btrfs_drop_extents() sets 'extent_i 414 * the path is kept locked. 415 */ 416 struct btrfs_path *path; 417 /* Start offset of the range to drop e 418 u64 start; 419 /* End (exclusive, last byte + 1) of t 420 u64 end; 421 /* If true drop all the extent maps in 422 bool drop_cache; 423 /* 424 * If true it means we want to insert 425 * the extents in the range. If this i 426 * parameter must be set as well and t 427 * be set to true by btrfs_drop_extent 428 * extent. 429 * Note: when this is set to true the 430 */ 431 bool replace_extent; 432 /* 433 * Used if 'replace_extent' is true. S 434 * insert after dropping all existing 435 */ 436 u32 extent_item_size; 437 438 /* Output parameters */ 439 440 /* 441 * Set to the minimum between the inpu 442 * (exclusive, last byte + 1) of the l 443 * set even if btrfs_drop_extents() re 444 */ 445 u64 drop_end; 446 /* 447 * The number of allocated bytes found 448 * than the range's length when there 449 */ 450 u64 bytes_found; 451 /* 452 * Only set if 'replace_extent' is tru 453 * to insert a replacement extent afte 454 * range, otherwise set to false by bt 455 * Also, if btrfs_drop_extents() has s 456 * returned with the path locked, othe 457 * false it has returned with the path 458 */ 459 bool extent_inserted; 460 }; 461 462 struct btrfs_file_private { 463 void *filldir_buf; 464 u64 last_index; 465 struct extent_state *llseek_cached_sta 466 /* Task that allocated this structure. 467 struct task_struct *owner_task; 468 }; 469 470 static inline u32 BTRFS_LEAF_DATA_SIZE(const s 471 { 472 return info->nodesize - sizeof(struct 473 } 474 475 static inline u32 BTRFS_MAX_ITEM_SIZE(const st 476 { 477 return BTRFS_LEAF_DATA_SIZE(info) - si 478 } 479 480 static inline u32 BTRFS_NODEPTRS_PER_BLOCK(con 481 { 482 return BTRFS_LEAF_DATA_SIZE(info) / si 483 } 484 485 static inline u32 BTRFS_MAX_XATTR_SIZE(const s 486 { 487 return BTRFS_MAX_ITEM_SIZE(info) - siz 488 } 489 490 #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \ 491 ((bytes) >> (f 492 493 static inline gfp_t btrfs_alloc_write_mask(str 494 { 495 return mapping_gfp_constraint(mapping, 496 } 497 498 void btrfs_error_unpin_extent_range(struct btr 499 int btrfs_discard_extent(struct btrfs_fs_info 500 u64 num_bytes, u64 *a 501 int btrfs_trim_fs(struct btrfs_fs_info *fs_inf 502 503 /* ctree.c */ 504 int __init btrfs_ctree_init(void); 505 void __cold btrfs_ctree_exit(void); 506 507 int btrfs_bin_search(struct extent_buffer *eb, 508 const struct btrfs_key *k 509 510 int __pure btrfs_comp_cpu_keys(const struct bt 511 512 #ifdef __LITTLE_ENDIAN 513 514 /* 515 * Compare two keys, on little-endian the disk 516 * we can avoid the conversion. 517 */ 518 static inline int btrfs_comp_keys(const struct 519 const struct 520 { 521 const struct btrfs_key *k1 = (const st 522 523 return btrfs_comp_cpu_keys(k1, k2); 524 } 525 526 #else 527 528 /* Compare two keys in a memcmp fashion. */ 529 static inline int btrfs_comp_keys(const struct 530 const struct 531 { 532 struct btrfs_key k1; 533 534 btrfs_disk_key_to_cpu(&k1, disk); 535 536 return btrfs_comp_cpu_keys(&k1, k2); 537 } 538 539 #endif 540 541 int btrfs_previous_item(struct btrfs_root *roo 542 struct btrfs_path *pat 543 int type); 544 int btrfs_previous_extent_item(struct btrfs_ro 545 struct btrfs_path *pat 546 void btrfs_set_item_key_safe(struct btrfs_tran 547 const struct btrf 548 const struct btrf 549 struct extent_buffer *btrfs_root_node(struct b 550 int btrfs_find_next_key(struct btrfs_root *roo 551 struct btrfs_key *key, 552 u64 min_trans); 553 int btrfs_search_forward(struct btrfs_root *ro 554 struct btrfs_path *pa 555 u64 min_trans); 556 struct extent_buffer *btrfs_read_node_slot(str 557 int 558 559 int btrfs_cow_block(struct btrfs_trans_handle 560 struct btrfs_root *root, s 561 struct extent_buffer *pare 562 struct extent_buffer **cow 563 enum btrfs_lock_nesting ne 564 int btrfs_force_cow_block(struct btrfs_trans_h 565 struct btrfs_root *r 566 struct extent_buffer 567 struct extent_buffer 568 struct extent_buffer 569 u64 search_start, u6 570 enum btrfs_lock_nest 571 int btrfs_copy_root(struct btrfs_trans_handle 572 struct btrfs_root *root, 573 struct extent_buffer *bu 574 struct extent_buffer **c 575 bool btrfs_block_can_be_shared(struct btrfs_tr 576 struct btrfs_ro 577 struct extent_b 578 int btrfs_del_ptr(struct btrfs_trans_handle *t 579 struct btrfs_path *path, int 580 void btrfs_extend_item(struct btrfs_trans_hand 581 const struct btrfs_path 582 void btrfs_truncate_item(struct btrfs_trans_ha 583 const struct btrfs_pa 584 int btrfs_split_item(struct btrfs_trans_handle 585 struct btrfs_root *root, 586 struct btrfs_path *path, 587 const struct btrfs_key *n 588 unsigned long split_offse 589 int btrfs_duplicate_item(struct btrfs_trans_ha 590 struct btrfs_root *ro 591 struct btrfs_path *pa 592 const struct btrfs_ke 593 int btrfs_find_item(struct btrfs_root *fs_root 594 u64 inum, u64 ioff, u8 key_typ 595 int btrfs_search_slot(struct btrfs_trans_handl 596 const struct btrfs_key * 597 int ins_len, int cow); 598 int btrfs_search_old_slot(struct btrfs_root *r 599 struct btrfs_path *p 600 int btrfs_search_slot_for_read(struct btrfs_ro 601 const struct bt 602 struct btrfs_pa 603 int return_any) 604 void btrfs_release_path(struct btrfs_path *p); 605 struct btrfs_path *btrfs_alloc_path(void); 606 void btrfs_free_path(struct btrfs_path *p); 607 DEFINE_FREE(btrfs_free_path, struct btrfs_path 608 609 int btrfs_del_items(struct btrfs_trans_handle 610 struct btrfs_path *path, in 611 static inline int btrfs_del_item(struct btrfs_ 612 struct btrfs_ 613 struct btrfs_ 614 { 615 return btrfs_del_items(trans, root, pa 616 } 617 618 /* 619 * Describes a batch of items to insert in a b 620 * btrfs_insert_empty_items(). 621 */ 622 struct btrfs_item_batch { 623 /* 624 * Pointer to an array containing the 625 * sorted order). 626 */ 627 const struct btrfs_key *keys; 628 /* Pointer to an array containing the 629 const u32 *data_sizes; 630 /* 631 * The sum of data sizes for all items 632 * setting up the data_sizes array, so 633 * than having btrfs_insert_empty_item 634 * doing it, as it would avoid an extr 635 * array, and in the case of setup_ite 636 * it while holding a write lock on a 637 * too, unnecessarily increasing the s 638 */ 639 u32 total_data_size; 640 /* Size of the keys and data_sizes arr 641 int nr; 642 }; 643 644 void btrfs_setup_item_for_insert(struct btrfs_ 645 struct btrfs_ 646 struct btrfs_ 647 const struct 648 u32 data_size 649 int btrfs_insert_item(struct btrfs_trans_handl 650 const struct btrfs_key * 651 int btrfs_insert_empty_items(struct btrfs_tran 652 struct btrfs_root 653 struct btrfs_path 654 const struct btrf 655 656 static inline int btrfs_insert_empty_item(stru 657 stru 658 stru 659 cons 660 u32 661 { 662 struct btrfs_item_batch batch; 663 664 batch.keys = key; 665 batch.data_sizes = &data_size; 666 batch.total_data_size = data_size; 667 batch.nr = 1; 668 669 return btrfs_insert_empty_items(trans, 670 } 671 672 int btrfs_next_old_leaf(struct btrfs_root *roo 673 u64 time_seq); 674 675 int btrfs_search_backwards(struct btrfs_root * 676 struct btrfs_path * 677 678 int btrfs_get_next_valid_item(struct btrfs_roo 679 struct btrfs_pat 680 681 /* 682 * Search in @root for a given @key, and store 683 * 684 * @root: The root node of the tree. 685 * @key: The key we are looking for. 686 * @found_key: Will hold the found item. 687 * @path: Holds the current slot/leaf. 688 * @iter_ret: Contains the value returned fr 689 * btrfs_get_next_valid_item, whi 690 * 691 * The @iter_ret is an output variable that wi 692 * btrfs_search_slot, if it encountered an err 693 * btrfs_get_next_valid_item otherwise. That r 694 * slot was found, 1 if there were no more lea 695 * 696 * It's recommended to use a separate variable 697 * set the function return value so there's no 698 * values stemming from btrfs_search_slot. 699 */ 700 #define btrfs_for_each_slot(root, key, found_k 701 for (iter_ret = btrfs_search_slot(NULL 702 (iter_ret) >= 0 && 703 (iter_ret = btrfs_get_next_val 704 (path)->slots[0]++ 705 ) 706 707 int btrfs_next_old_item(struct btrfs_root *roo 708 709 /* 710 * Search the tree again to find a leaf with g 711 * 712 * Returns 0 if it found something or 1 if the 713 * Returns < 0 on error. 714 */ 715 static inline int btrfs_next_leaf(struct btrfs 716 { 717 return btrfs_next_old_leaf(root, path, 718 } 719 720 static inline int btrfs_next_item(struct btrfs 721 { 722 return btrfs_next_old_item(root, p, 0) 723 } 724 int btrfs_leaf_free_space(const struct extent_ 725 726 static inline int is_fstree(u64 rootid) 727 { 728 if (rootid == BTRFS_FS_TREE_OBJECTID | 729 ((s64)rootid >= (s64)BTRFS_FIRST_F 730 !btrfs_qgroup_level(rootid))) 731 return 1; 732 return 0; 733 } 734 735 static inline bool btrfs_is_data_reloc_root(co 736 { 737 return root->root_key.objectid == BTRF 738 } 739 740 u16 btrfs_csum_type_size(u16 type); 741 int btrfs_super_csum_size(const struct btrfs_s 742 const char *btrfs_super_csum_name(u16 csum_typ 743 const char *btrfs_super_csum_driver(u16 csum_t 744 size_t __attribute_const__ btrfs_get_num_csums 745 746 /* 747 * We use page status Private2 to indicate the 748 * unfinished IO. 749 * 750 * Rename the Private2 accessors to Ordered, t 751 */ 752 #define PageOrdered(page) PagePr 753 #define SetPageOrdered(page) SetPag 754 #define ClearPageOrdered(page) ClearP 755 #define folio_test_ordered(folio) folio_ 756 #define folio_set_ordered(folio) folio_ 757 #define folio_clear_ordered(folio) folio_ 758 759 #endif 760
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.