blob: 5e8bef4b7563a7c676165487e73a04550591f389 [file] [log] [blame]
David Sterba9888c342018-04-03 19:16:55 +02001/* SPDX-License-Identifier: GPL-2.0 */
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
David Sterba9888c342018-04-03 19:16:55 +02006#ifndef BTRFS_DISK_IO_H
7#define BTRFS_DISK_IO_H
Chris Masoneb60cea2007-02-02 09:18:22 -05008
Yan Zhenga512bbf2008-12-08 16:46:26 -05009#define BTRFS_SUPER_MIRROR_MAX 3
10#define BTRFS_SUPER_MIRROR_SHIFT 12
11
David Sterba9f6d2512017-06-16 01:48:05 +020012/*
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
David Sterbabfebd8b2014-07-30 00:25:45 +020020enum btrfs_wq_endio_type {
David Sterbabbe339c2018-11-27 15:25:13 +010021 BTRFS_WQ_ENDIO_DATA,
22 BTRFS_WQ_ENDIO_METADATA,
23 BTRFS_WQ_ENDIO_FREE_SPACE,
24 BTRFS_WQ_ENDIO_RAID56,
David Woodhouse53b381b2013-01-29 18:40:14 -050025};
26
Yan Zhenga512bbf2008-12-08 16:46:26 -050027static inline u64 btrfs_sb_offset(int mirror)
28{
Byongho Leeee221842015-12-15 01:42:10 +090029 u64 start = SZ_16K;
Yan Zhenga512bbf2008-12-08 16:46:26 -050030 if (mirror)
31 return start << (BTRFS_SUPER_MIRROR_SHIFT * mirror);
32 return BTRFS_SUPER_INFO_OFFSET;
33}
34
Chris Mason0b86a832008-03-24 15:01:56 -040035struct btrfs_device;
Chris Mason8a4b83c2008-03-24 15:02:07 -040036struct btrfs_fs_devices;
Chris Masoneb60cea2007-02-02 09:18:22 -050037
Josef Bacikbd647ce2020-01-24 09:33:00 -050038void btrfs_check_leaked_roots(struct btrfs_fs_info *fs_info);
Josef Bacik8260edba2020-01-24 09:32:59 -050039void btrfs_init_fs_info(struct btrfs_fs_info *fs_info);
David Sterbae064d5e2019-03-20 14:58:13 +010040int btrfs_verify_level_key(struct extent_buffer *eb, int level,
Qu Wenruo448de472019-03-12 17:10:40 +080041 struct btrfs_key *first_key, u64 parent_transid);
Qu Wenruo581c1762018-03-29 09:08:11 +080042struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
Josef Bacik1b7ec852020-11-05 10:45:18 -050043 u64 owner_root, u64 parent_transid,
44 int level, struct btrfs_key *first_key);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040045struct extent_buffer *btrfs_find_create_tree_block(
46 struct btrfs_fs_info *fs_info,
Josef Bacik3fbaf252020-11-05 10:45:20 -050047 u64 bytenr, u64 owner_root,
48 int level);
David Sterba6a884d7d2019-03-20 14:30:02 +010049void btrfs_clean_tree_block(struct extent_buffer *buf);
Boris Burkov8cd29082020-11-18 15:06:20 -080050void btrfs_clear_oneshot_options(struct btrfs_fs_info *fs_info);
Boris Burkov44c0ca22020-11-18 15:06:16 -080051int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info);
David Sterbab105e922019-10-01 19:57:35 +020052int __cold open_ctree(struct super_block *sb,
Al Viroad2b2c82011-11-17 01:10:02 -050053 struct btrfs_fs_devices *fs_devices,
54 char *options);
David Sterbab105e922019-10-01 19:57:35 +020055void __cold close_ctree(struct btrfs_fs_info *fs_info);
David Sterbaeece6a92017-02-10 19:04:32 +010056int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors);
Johannes Thumshirn8f323802020-02-14 00:24:32 +090057struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev);
58struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev,
59 int copy_num);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040060int btrfs_commit_super(struct btrfs_fs_info *fs_info);
Josef Bacik62a2c732020-01-24 09:32:21 -050061struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
62 struct btrfs_key *key);
Miao Xiecb517ea2013-05-15 07:48:19 +000063int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
64 struct btrfs_root *root);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040065void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info);
Miao Xiec00869f2013-09-25 21:47:44 +080066
67struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
David Sterba56e93572020-05-15 19:35:55 +020068 u64 objectid, bool check_ref);
Qu Wenruo2dfb1e42020-06-16 10:17:36 +080069struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info,
70 u64 objectid, dev_t anon_dev);
Josef Bacik49d11be2020-10-19 16:02:31 -040071struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info,
72 struct btrfs_path *path,
73 u64 objectid);
Josef Bacikabed4aa2021-11-05 16:45:51 -040074int btrfs_global_root_insert(struct btrfs_root *root);
75void btrfs_global_root_delete(struct btrfs_root *root);
76struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info,
77 struct btrfs_key *key);
78struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr);
79struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr);
Miao Xiec00869f2013-09-25 21:47:44 +080080
Josef Bacik0d4b0462020-01-24 09:32:53 -050081void btrfs_free_fs_info(struct btrfs_fs_info *fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -050082int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040083void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info);
84void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info);
Miao Xiecb517ea2013-05-15 07:48:19 +000085void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
86 struct btrfs_root *root);
Qu Wenruoc3a3b192021-09-15 15:17:18 +080087int btrfs_validate_metadata_buffer(struct btrfs_bio *bbio,
Nikolay Borisov9a446d62020-09-18 16:34:33 +030088 struct page *page, u64 start, u64 end,
89 int mirror);
Nikolay Borisov1b362942020-09-18 16:34:38 +030090blk_status_t btrfs_submit_metadata_bio(struct inode *inode, struct bio *bio,
91 int mirror_num, unsigned long bio_flags);
Josef Bacik06ea65a2013-09-19 16:07:01 -040092#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoneyda170662016-06-15 09:22:56 -040093struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info);
Josef Bacik06ea65a2013-09-19 16:07:01 -040094#endif
95
Miao Xieb0feb9d2013-05-15 07:48:20 +000096/*
97 * This function is used to grab the root, and avoid it is freed when we
98 * access it. But it doesn't ensure that the tree is not dropped.
99 *
100 * If you want to ensure the whole tree is safe, you should use
101 * fs_info->subvol_srcu
102 */
Josef Bacik00246522020-01-24 09:33:01 -0500103static inline struct btrfs_root *btrfs_grab_root(struct btrfs_root *root)
Miao Xieb0feb9d2013-05-15 07:48:20 +0000104{
Josef Bacik4cdfd932020-01-24 09:32:26 -0500105 if (!root)
106 return NULL;
Elena Reshetova0700cea2017-03-03 10:55:18 +0200107 if (refcount_inc_not_zero(&root->refs))
Miao Xieb0feb9d2013-05-15 07:48:20 +0000108 return root;
109 return NULL;
110}
111
Josef Bacikdfe8aec2021-11-05 16:45:36 -0400112static inline struct btrfs_root *btrfs_block_group_root(struct btrfs_fs_info *fs_info)
113{
Josef Bacik29cbcf42021-11-05 16:45:45 -0400114 return btrfs_extent_root(fs_info, 0);
Josef Bacikdfe8aec2021-11-05 16:45:36 -0400115}
116
Josef Bacik8c389382020-02-14 16:11:42 -0500117void btrfs_put_root(struct btrfs_root *root);
Chris Mason5f39d392007-10-15 16:14:19 -0400118void btrfs_mark_buffer_dirty(struct extent_buffer *buf);
Chris Masonb9fab912012-05-06 07:23:47 -0400119int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
120 int atomic);
Qu Wenruo581c1762018-03-29 09:08:11 +0800121int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level,
122 struct btrfs_key *first_key);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200123blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
David Sterbabfebd8b2014-07-30 00:25:45 +0200124 enum btrfs_wq_endio_type metadata);
Qu Wenruo8896a082020-10-21 14:24:53 +0800125blk_status_t btrfs_wq_submit_bio(struct inode *inode, struct bio *bio,
126 int mirror_num, unsigned long bio_flags,
Qu Wenruo1941b642020-12-02 14:47:57 +0800127 u64 dio_file_offset,
Qu Wenruo8896a082020-10-21 14:24:53 +0800128 extent_submit_bio_start_t *submit_bio_start);
David Sterbae288c082018-07-18 17:36:24 +0200129blk_status_t btrfs_submit_bio_done(void *private_data, struct bio *bio,
130 int mirror_num);
Naohiro Aota6ab6ebb2021-02-04 19:22:17 +0900131int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans,
132 struct btrfs_root *root);
Chris Masone02119d2008-09-05 16:13:11 -0400133int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
134 struct btrfs_fs_info *fs_info);
Yan Zheng7237f182009-01-21 12:54:03 -0500135int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
136 struct btrfs_root *root);
Liu Boc79a1752016-07-20 17:44:12 -0700137void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400138 struct btrfs_fs_info *fs_info);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100139void btrfs_cleanup_one_transaction(struct btrfs_transaction *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400140 struct btrfs_fs_info *fs_info);
Arne Jansen20897f52011-09-13 12:44:20 +0200141struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
Arne Jansen20897f52011-09-13 12:44:20 +0200142 u64 objectid);
143int btree_lock_page_hook(struct page *page, void *data,
144 void (*flush_fn)(void *));
Zhao Lei943c6e92015-08-19 15:54:15 +0800145int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags);
Nikolay Borisov543068a2020-12-07 17:32:33 +0200146int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid);
Nikolay Borisov453e4872020-12-07 17:32:32 +0200147int btrfs_init_root_free_objectid(struct btrfs_root *root);
David Sterba97eb6b62014-07-30 00:55:42 +0200148int __init btrfs_end_io_wq_init(void);
David Sterbae67c7182018-02-19 17:24:18 +0100149void __cold btrfs_end_io_wq_exit(void);
Chris Mason4008c042009-02-12 14:09:45 -0500150
151#ifdef CONFIG_DEBUG_LOCK_ALLOC
Chris Mason85d4e462011-07-26 16:11:19 -0400152void btrfs_set_buffer_lockdep_class(u64 objectid,
153 struct extent_buffer *eb, int level);
Chris Mason4008c042009-02-12 14:09:45 -0500154#else
Chris Mason85d4e462011-07-26 16:11:19 -0400155static inline void btrfs_set_buffer_lockdep_class(u64 objectid,
156 struct extent_buffer *eb, int level)
Chris Mason4008c042009-02-12 14:09:45 -0500157{
158}
159#endif
David Sterba9888c342018-04-03 19:16:55 +0200160
Chris Masoneb60cea2007-02-02 09:18:22 -0500161#endif