blob: 8c2d6cf1ce5969b3804e842c3c6841b9c250289c [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
Byongho Leeee221842015-12-15 01:42:10 +09009#define BTRFS_SUPER_INFO_OFFSET SZ_64K
Chris Masonf2984462008-04-10 16:19:33 -040010#define BTRFS_SUPER_INFO_SIZE 4096
Yan Zhenga512bbf2008-12-08 16:46:26 -050011
12#define BTRFS_SUPER_MIRROR_MAX 3
13#define BTRFS_SUPER_MIRROR_SHIFT 12
14
David Sterba9f6d2512017-06-16 01:48:05 +020015/*
16 * Fixed blocksize for all devices, applies to specific ways of reading
17 * metadata like superblock. Must meet the set_blocksize requirements.
18 *
19 * Do not change.
20 */
21#define BTRFS_BDEV_BLOCKSIZE (4096)
22
David Sterbabfebd8b2014-07-30 00:25:45 +020023enum btrfs_wq_endio_type {
David Sterbabbe339c2018-11-27 15:25:13 +010024 BTRFS_WQ_ENDIO_DATA,
25 BTRFS_WQ_ENDIO_METADATA,
26 BTRFS_WQ_ENDIO_FREE_SPACE,
27 BTRFS_WQ_ENDIO_RAID56,
28 BTRFS_WQ_ENDIO_DIO_REPAIR,
David Woodhouse53b381b2013-01-29 18:40:14 -050029};
30
Yan Zhenga512bbf2008-12-08 16:46:26 -050031static inline u64 btrfs_sb_offset(int mirror)
32{
Byongho Leeee221842015-12-15 01:42:10 +090033 u64 start = SZ_16K;
Yan Zhenga512bbf2008-12-08 16:46:26 -050034 if (mirror)
35 return start << (BTRFS_SUPER_MIRROR_SHIFT * mirror);
36 return BTRFS_SUPER_INFO_OFFSET;
37}
38
Chris Mason0b86a832008-03-24 15:01:56 -040039struct btrfs_device;
Chris Mason8a4b83c2008-03-24 15:02:07 -040040struct btrfs_fs_devices;
Chris Masoneb60cea2007-02-02 09:18:22 -050041
David Sterbae064d5e2019-03-20 14:58:13 +010042int btrfs_verify_level_key(struct extent_buffer *eb, int level,
Qu Wenruo448de472019-03-12 17:10:40 +080043 struct btrfs_key *first_key, u64 parent_transid);
Qu Wenruo581c1762018-03-29 09:08:11 +080044struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
45 u64 parent_transid, int level,
46 struct btrfs_key *first_key);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040047void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040048struct extent_buffer *btrfs_find_create_tree_block(
49 struct btrfs_fs_info *fs_info,
50 u64 bytenr);
David Sterba6a884d7d2019-03-20 14:30:02 +010051void btrfs_clean_tree_block(struct extent_buffer *buf);
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);
Yan Zhenga512bbf2008-12-08 16:46:26 -050057struct buffer_head *btrfs_read_dev_super(struct block_device *bdev);
Anand Jain29c36d72015-08-14 18:32:58 +080058int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
59 struct buffer_head **bh_ret);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -040060int btrfs_commit_super(struct btrfs_fs_info *fs_info);
Miao Xiecb517ea2013-05-15 07:48:19 +000061struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,
62 struct btrfs_key *location);
63int btrfs_init_fs_root(struct btrfs_root *root);
Jeff Mahoney35bbb972016-08-17 21:58:33 -040064struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
65 u64 root_id);
Miao Xiecb517ea2013-05-15 07:48:19 +000066int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
67 struct btrfs_root *root);
Josef Bacikfaa2dbf2014-05-07 17:06:09 -040068void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info);
Miao Xiec00869f2013-09-25 21:47:44 +080069
70struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
71 struct btrfs_key *key,
72 bool check_ref);
73static inline struct btrfs_root *
74btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
75 struct btrfs_key *location)
76{
77 return btrfs_get_fs_root(fs_info, location, true);
78}
79
Yan Zhengc146afa2008-11-12 14:34:12 -050080int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040081void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info);
82void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info);
Miao Xiecb517ea2013-05-15 07:48:19 +000083void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
84 struct btrfs_root *root);
85void btrfs_free_fs_root(struct btrfs_root *root);
Miao Xieb0feb9d2013-05-15 07:48:20 +000086
Josef Bacik06ea65a2013-09-19 16:07:01 -040087#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
Jeff Mahoneyda170662016-06-15 09:22:56 -040088struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info);
Josef Bacik06ea65a2013-09-19 16:07:01 -040089#endif
90
Miao Xieb0feb9d2013-05-15 07:48:20 +000091/*
92 * This function is used to grab the root, and avoid it is freed when we
93 * access it. But it doesn't ensure that the tree is not dropped.
94 *
95 * If you want to ensure the whole tree is safe, you should use
96 * fs_info->subvol_srcu
97 */
98static inline struct btrfs_root *btrfs_grab_fs_root(struct btrfs_root *root)
99{
Elena Reshetova0700cea2017-03-03 10:55:18 +0200100 if (refcount_inc_not_zero(&root->refs))
Miao Xieb0feb9d2013-05-15 07:48:20 +0000101 return root;
102 return NULL;
103}
104
105static inline void btrfs_put_fs_root(struct btrfs_root *root)
106{
Elena Reshetova0700cea2017-03-03 10:55:18 +0200107 if (refcount_dec_and_test(&root->refs))
Miao Xieb0feb9d2013-05-15 07:48:20 +0000108 kfree(root);
109}
110
Chris Mason5f39d392007-10-15 16:14:19 -0400111void btrfs_mark_buffer_dirty(struct extent_buffer *buf);
Chris Masonb9fab912012-05-06 07:23:47 -0400112int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
113 int atomic);
Qu Wenruo581c1762018-03-29 09:08:11 +0800114int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level,
115 struct btrfs_key *first_key);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200116blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
David Sterbabfebd8b2014-07-30 00:25:45 +0200117 enum btrfs_wq_endio_type metadata);
Linus Torvalds8c27cb32017-07-05 16:41:23 -0700118blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
Josef Bacikc6100a42017-05-05 11:57:13 -0400119 int mirror_num, unsigned long bio_flags,
120 u64 bio_offset, void *private_data,
David Sterbae288c082018-07-18 17:36:24 +0200121 extent_submit_bio_start_t *submit_bio_start);
122blk_status_t btrfs_submit_bio_done(void *private_data, struct bio *bio,
123 int mirror_num);
Chris Masone02119d2008-09-05 16:13:11 -0400124int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
125 struct btrfs_fs_info *fs_info);
Yan Zheng7237f182009-01-21 12:54:03 -0500126int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
127 struct btrfs_root *root);
Liu Boc79a1752016-07-20 17:44:12 -0700128void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400129 struct btrfs_fs_info *fs_info);
Jeff Mahoney49b25e02012-03-01 17:24:58 +0100130void btrfs_cleanup_one_transaction(struct btrfs_transaction *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400131 struct btrfs_fs_info *fs_info);
Arne Jansen20897f52011-09-13 12:44:20 +0200132struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
Arne Jansen20897f52011-09-13 12:44:20 +0200133 u64 objectid);
134int btree_lock_page_hook(struct page *page, void *data,
135 void (*flush_fn)(void *));
David Sterba6af49db2017-06-23 04:09:57 +0200136struct extent_map *btree_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -0800137 struct page *page, size_t pg_offset,
138 u64 start, u64 len);
Zhao Lei943c6e92015-08-19 15:54:15 +0800139int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags);
David Sterba97eb6b62014-07-30 00:55:42 +0200140int __init btrfs_end_io_wq_init(void);
David Sterbae67c7182018-02-19 17:24:18 +0100141void __cold btrfs_end_io_wq_exit(void);
Chris Mason4008c042009-02-12 14:09:45 -0500142
143#ifdef CONFIG_DEBUG_LOCK_ALLOC
Chris Mason85d4e462011-07-26 16:11:19 -0400144void btrfs_init_lockdep(void);
145void btrfs_set_buffer_lockdep_class(u64 objectid,
146 struct extent_buffer *eb, int level);
Chris Mason4008c042009-02-12 14:09:45 -0500147#else
Chris Mason85d4e462011-07-26 16:11:19 -0400148static inline void btrfs_init_lockdep(void)
149{ }
150static inline void btrfs_set_buffer_lockdep_class(u64 objectid,
151 struct extent_buffer *eb, int level)
Chris Mason4008c042009-02-12 14:09:45 -0500152{
153}
154#endif
David Sterba9888c342018-04-03 19:16:55 +0200155
Chris Masoneb60cea2007-02-02 09:18:22 -0500156#endif