1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (C) 2007 Oracle. All rights reserved. 4 */ 5 6 #ifndef BTRFS_DISK_IO_H 7 #define BTRFS_DISK_IO_H 8 9 #define BTRFS_SUPER_MIRROR_MAX 3 10 #define BTRFS_SUPER_MIRROR_SHIFT 12 11 12 /* 13 * Fixed blocksize for all devices, applies to specific ways of reading 14 * metadata like superblock. Must meet the set_blocksize requirements. 15 * 16 * Do not change. 17 */ 18 #define BTRFS_BDEV_BLOCKSIZE (4096) 19 20 static inline u64 btrfs_sb_offset(int mirror) 21 { 22 u64 start = SZ_16K; 23 if (mirror) 24 return start << (BTRFS_SUPER_MIRROR_SHIFT * mirror); 25 return BTRFS_SUPER_INFO_OFFSET; 26 } 27 28 /* All the extra info needed to verify the parentness of a tree block. */ 29 struct btrfs_tree_parent_check { 30 /* 31 * The owner check against the tree block. 32 * 33 * Can be 0 to skip the owner check. 34 */ 35 u64 owner_root; 36 37 /* 38 * Expected transid, can be 0 to skip the check, but such skip 39 * should only be utlized for backref walk related code. 40 */ 41 u64 transid; 42 43 /* 44 * The expected first key. 45 * 46 * This check can be skipped if @has_first_key is false, such skip 47 * can happen for case where we don't have the parent node key, 48 * e.g. reading the tree root, doing backref walk. 49 */ 50 struct btrfs_key first_key; 51 bool has_first_key; 52 53 /* The expected level. Should always be set. */ 54 u8 level; 55 }; 56 57 struct btrfs_device; 58 struct btrfs_fs_devices; 59 60 void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info); 61 void btrfs_init_fs_info(struct btrfs_fs_info *fs_info); 62 int btrfs_verify_level_key(struct extent_buffer *eb, int level, 63 struct btrfs_key *first_key, u64 parent_transid); 64 struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr, 65 struct btrfs_tree_parent_check *check); 66 struct extent_buffer *btrfs_find_create_tree_block( 67 struct btrfs_fs_info *fs_info, 68 u64 bytenr, u64 owner_root, 69 int level); 70 void btrfs_clean_tree_block(struct extent_buffer *buf); 71 void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info); 72 int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info); 73 int btrfs_check_super_csum(struct btrfs_fs_info *fs_info, 74 const struct btrfs_super_block *disk_sb); 75 int __cold open_ctree(struct super_block *sb, 76 struct btrfs_fs_devices *fs_devices, 77 char *options); 78 void __cold close_ctree(struct btrfs_fs_info *fs_info); 79 int btrfs_validate_super(struct btrfs_fs_info *fs_info, 80 struct btrfs_super_block *sb, int mirror_num); 81 int btrfs_check_features(struct btrfs_fs_info *fs_info, struct super_block *sb); 82 int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors); 83 struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev); 84 struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev, 85 int copy_num, bool drop_cache); 86 int btrfs_commit_super(struct btrfs_fs_info *fs_info); 87 struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root, 88 struct btrfs_key *key); 89 int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info, 90 struct btrfs_root *root); 91 void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info); 92 93 struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info, 94 u64 objectid, bool check_ref); 95 struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info, 96 u64 objectid, dev_t anon_dev); 97 struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info, 98 struct btrfs_path *path, 99 u64 objectid); 100 int btrfs_global_root_insert(struct btrfs_root *root); 101 void btrfs_global_root_delete(struct btrfs_root *root); 102 struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info, 103 struct btrfs_key *key); 104 struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr); 105 struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr); 106 struct btrfs_root *btrfs_block_group_root(struct btrfs_fs_info *fs_info); 107 108 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info); 109 int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info); 110 void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info); 111 void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info); 112 void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 113 struct btrfs_root *root); 114 int btrfs_validate_metadata_buffer(struct btrfs_bio *bbio, 115 struct page *page, u64 start, u64 end, 116 int mirror); 117 void btrfs_submit_metadata_bio(struct btrfs_inode *inode, struct bio *bio, int mirror_num); 118 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 119 struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info); 120 #endif 121 122 /* 123 * This function is used to grab the root, and avoid it is freed when we 124 * access it. But it doesn't ensure that the tree is not dropped. 125 * 126 * If you want to ensure the whole tree is safe, you should use 127 * fs_info->subvol_srcu 128 */ 129 static inline struct btrfs_root *btrfs_grab_root(struct btrfs_root *root) 130 { 131 if (!root) 132 return NULL; 133 if (refcount_inc_not_zero(&root->refs)) 134 return root; 135 return NULL; 136 } 137 138 void btrfs_put_root(struct btrfs_root *root); 139 void btrfs_mark_buffer_dirty(struct extent_buffer *buf); 140 int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 141 int atomic); 142 int btrfs_read_extent_buffer(struct extent_buffer *buf, 143 struct btrfs_tree_parent_check *check); 144 145 enum btrfs_wq_submit_cmd { 146 WQ_SUBMIT_METADATA, 147 WQ_SUBMIT_DATA, 148 WQ_SUBMIT_DATA_DIO, 149 }; 150 151 bool btrfs_wq_submit_bio(struct btrfs_inode *inode, struct bio *bio, int mirror_num, 152 u64 dio_file_offset, enum btrfs_wq_submit_cmd cmd); 153 blk_status_t btree_submit_bio_start(struct bio *bio); 154 int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans, 155 struct btrfs_root *root); 156 int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans, 157 struct btrfs_fs_info *fs_info); 158 int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 159 struct btrfs_root *root); 160 void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *trans, 161 struct btrfs_fs_info *fs_info); 162 void btrfs_cleanup_one_transaction(struct btrfs_transaction *trans, 163 struct btrfs_fs_info *fs_info); 164 struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 165 u64 objectid); 166 int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags); 167 int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid); 168 int btrfs_init_root_free_objectid(struct btrfs_root *root); 169 170 #endif 171