extent_io.h (10119B)
1/* SPDX-License-Identifier: GPL-2.0 */ 2 3#ifndef BTRFS_EXTENT_IO_H 4#define BTRFS_EXTENT_IO_H 5 6#include <linux/rbtree.h> 7#include <linux/refcount.h> 8#include <linux/fiemap.h> 9#include <linux/btrfs_tree.h> 10#include "compression.h" 11#include "ulist.h" 12 13enum { 14 EXTENT_BUFFER_UPTODATE, 15 EXTENT_BUFFER_DIRTY, 16 EXTENT_BUFFER_CORRUPT, 17 /* this got triggered by readahead */ 18 EXTENT_BUFFER_READAHEAD, 19 EXTENT_BUFFER_TREE_REF, 20 EXTENT_BUFFER_STALE, 21 EXTENT_BUFFER_WRITEBACK, 22 /* read IO error */ 23 EXTENT_BUFFER_READ_ERR, 24 EXTENT_BUFFER_UNMAPPED, 25 EXTENT_BUFFER_IN_TREE, 26 /* write IO error */ 27 EXTENT_BUFFER_WRITE_ERR, 28 EXTENT_BUFFER_NO_CHECK, 29}; 30 31/* these are flags for __process_pages_contig */ 32#define PAGE_UNLOCK (1 << 0) 33/* Page starts writeback, clear dirty bit and set writeback bit */ 34#define PAGE_START_WRITEBACK (1 << 1) 35#define PAGE_END_WRITEBACK (1 << 2) 36#define PAGE_SET_ORDERED (1 << 3) 37#define PAGE_SET_ERROR (1 << 4) 38#define PAGE_LOCK (1 << 5) 39 40/* 41 * page->private values. Every page that is controlled by the extent 42 * map has page->private set to one. 43 */ 44#define EXTENT_PAGE_PRIVATE 1 45 46/* 47 * The extent buffer bitmap operations are done with byte granularity instead of 48 * word granularity for two reasons: 49 * 1. The bitmaps must be little-endian on disk. 50 * 2. Bitmap items are not guaranteed to be aligned to a word and therefore a 51 * single word in a bitmap may straddle two pages in the extent buffer. 52 */ 53#define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE) 54#define BYTE_MASK ((1 << BITS_PER_BYTE) - 1) 55#define BITMAP_FIRST_BYTE_MASK(start) \ 56 ((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK) 57#define BITMAP_LAST_BYTE_MASK(nbits) \ 58 (BYTE_MASK >> (-(nbits) & (BITS_PER_BYTE - 1))) 59 60struct btrfs_root; 61struct btrfs_inode; 62struct btrfs_io_bio; 63struct btrfs_fs_info; 64struct io_failure_record; 65struct extent_io_tree; 66 67typedef void (submit_bio_hook_t)(struct inode *inode, struct bio *bio, 68 int mirror_num, 69 enum btrfs_compression_type compress_type); 70 71typedef blk_status_t (extent_submit_bio_start_t)(struct inode *inode, 72 struct bio *bio, u64 dio_file_offset); 73 74#define INLINE_EXTENT_BUFFER_PAGES (BTRFS_MAX_METADATA_BLOCKSIZE / PAGE_SIZE) 75struct extent_buffer { 76 u64 start; 77 unsigned long len; 78 unsigned long bflags; 79 struct btrfs_fs_info *fs_info; 80 spinlock_t refs_lock; 81 atomic_t refs; 82 atomic_t io_pages; 83 int read_mirror; 84 struct rcu_head rcu_head; 85 pid_t lock_owner; 86 /* >= 0 if eb belongs to a log tree, -1 otherwise */ 87 s8 log_index; 88 89 struct rw_semaphore lock; 90 91 struct page *pages[INLINE_EXTENT_BUFFER_PAGES]; 92 struct list_head release_list; 93#ifdef CONFIG_BTRFS_DEBUG 94 struct list_head leak_list; 95#endif 96}; 97 98/* 99 * Structure to record how many bytes and which ranges are set/cleared 100 */ 101struct extent_changeset { 102 /* How many bytes are set/cleared in this operation */ 103 u64 bytes_changed; 104 105 /* Changed ranges */ 106 struct ulist range_changed; 107}; 108 109static inline void extent_changeset_init(struct extent_changeset *changeset) 110{ 111 changeset->bytes_changed = 0; 112 ulist_init(&changeset->range_changed); 113} 114 115static inline struct extent_changeset *extent_changeset_alloc(void) 116{ 117 struct extent_changeset *ret; 118 119 ret = kmalloc(sizeof(*ret), GFP_KERNEL); 120 if (!ret) 121 return NULL; 122 123 extent_changeset_init(ret); 124 return ret; 125} 126 127static inline void extent_changeset_release(struct extent_changeset *changeset) 128{ 129 if (!changeset) 130 return; 131 changeset->bytes_changed = 0; 132 ulist_release(&changeset->range_changed); 133} 134 135static inline void extent_changeset_free(struct extent_changeset *changeset) 136{ 137 if (!changeset) 138 return; 139 extent_changeset_release(changeset); 140 kfree(changeset); 141} 142 143struct extent_map_tree; 144 145typedef struct extent_map *(get_extent_t)(struct btrfs_inode *inode, 146 struct page *page, size_t pg_offset, 147 u64 start, u64 len); 148 149int try_release_extent_mapping(struct page *page, gfp_t mask); 150int try_release_extent_buffer(struct page *page); 151 152int btrfs_read_folio(struct file *file, struct folio *folio); 153int extent_write_full_page(struct page *page, struct writeback_control *wbc); 154int extent_write_locked_range(struct inode *inode, u64 start, u64 end); 155int extent_writepages(struct address_space *mapping, 156 struct writeback_control *wbc); 157int btree_write_cache_pages(struct address_space *mapping, 158 struct writeback_control *wbc); 159void extent_readahead(struct readahead_control *rac); 160int extent_fiemap(struct btrfs_inode *inode, struct fiemap_extent_info *fieinfo, 161 u64 start, u64 len); 162int set_page_extent_mapped(struct page *page); 163void clear_page_extent_mapped(struct page *page); 164 165struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info, 166 u64 start, u64 owner_root, int level); 167struct extent_buffer *__alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, 168 u64 start, unsigned long len); 169struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info, 170 u64 start); 171struct extent_buffer *btrfs_clone_extent_buffer(const struct extent_buffer *src); 172struct extent_buffer *find_extent_buffer(struct btrfs_fs_info *fs_info, 173 u64 start); 174void free_extent_buffer(struct extent_buffer *eb); 175void free_extent_buffer_stale(struct extent_buffer *eb); 176#define WAIT_NONE 0 177#define WAIT_COMPLETE 1 178#define WAIT_PAGE_LOCK 2 179int read_extent_buffer_pages(struct extent_buffer *eb, int wait, 180 int mirror_num); 181void wait_on_extent_buffer_writeback(struct extent_buffer *eb); 182void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info, 183 u64 bytenr, u64 owner_root, u64 gen, int level); 184void btrfs_readahead_node_child(struct extent_buffer *node, int slot); 185 186static inline int num_extent_pages(const struct extent_buffer *eb) 187{ 188 /* 189 * For sectorsize == PAGE_SIZE case, since nodesize is always aligned to 190 * sectorsize, it's just eb->len >> PAGE_SHIFT. 191 * 192 * For sectorsize < PAGE_SIZE case, we could have nodesize < PAGE_SIZE, 193 * thus have to ensure we get at least one page. 194 */ 195 return (eb->len >> PAGE_SHIFT) ?: 1; 196} 197 198static inline int extent_buffer_uptodate(const struct extent_buffer *eb) 199{ 200 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 201} 202 203int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv, 204 unsigned long start, unsigned long len); 205void read_extent_buffer(const struct extent_buffer *eb, void *dst, 206 unsigned long start, 207 unsigned long len); 208int read_extent_buffer_to_user_nofault(const struct extent_buffer *eb, 209 void __user *dst, unsigned long start, 210 unsigned long len); 211void write_extent_buffer_fsid(const struct extent_buffer *eb, const void *src); 212void write_extent_buffer_chunk_tree_uuid(const struct extent_buffer *eb, 213 const void *src); 214void write_extent_buffer(const struct extent_buffer *eb, const void *src, 215 unsigned long start, unsigned long len); 216void copy_extent_buffer_full(const struct extent_buffer *dst, 217 const struct extent_buffer *src); 218void copy_extent_buffer(const struct extent_buffer *dst, 219 const struct extent_buffer *src, 220 unsigned long dst_offset, unsigned long src_offset, 221 unsigned long len); 222void memcpy_extent_buffer(const struct extent_buffer *dst, 223 unsigned long dst_offset, unsigned long src_offset, 224 unsigned long len); 225void memmove_extent_buffer(const struct extent_buffer *dst, 226 unsigned long dst_offset, unsigned long src_offset, 227 unsigned long len); 228void memzero_extent_buffer(const struct extent_buffer *eb, unsigned long start, 229 unsigned long len); 230int extent_buffer_test_bit(const struct extent_buffer *eb, unsigned long start, 231 unsigned long pos); 232void extent_buffer_bitmap_set(const struct extent_buffer *eb, unsigned long start, 233 unsigned long pos, unsigned long len); 234void extent_buffer_bitmap_clear(const struct extent_buffer *eb, 235 unsigned long start, unsigned long pos, 236 unsigned long len); 237void clear_extent_buffer_dirty(const struct extent_buffer *eb); 238bool set_extent_buffer_dirty(struct extent_buffer *eb); 239void set_extent_buffer_uptodate(struct extent_buffer *eb); 240void clear_extent_buffer_uptodate(struct extent_buffer *eb); 241int extent_buffer_under_io(const struct extent_buffer *eb); 242void extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end); 243void extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end); 244void extent_clear_unlock_delalloc(struct btrfs_inode *inode, u64 start, u64 end, 245 struct page *locked_page, 246 u32 bits_to_clear, unsigned long page_ops); 247 248int btrfs_alloc_page_array(unsigned int nr_pages, struct page **page_array); 249struct bio *btrfs_bio_alloc(unsigned int nr_iovecs); 250struct bio *btrfs_bio_clone(struct block_device *bdev, struct bio *bio); 251struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size); 252 253void end_extent_writepage(struct page *page, int err, u64 start, u64 end); 254int btrfs_repair_eb_io_failure(const struct extent_buffer *eb, int mirror_num); 255 256/* 257 * When IO fails, either with EIO or csum verification fails, we 258 * try other mirrors that might have a good copy of the data. This 259 * io_failure_record is used to record state as we go through all the 260 * mirrors. If another mirror has good data, the sector is set up to date 261 * and things continue. If a good mirror can't be found, the original 262 * bio end_io callback is called to indicate things have failed. 263 */ 264struct io_failure_record { 265 struct page *page; 266 u64 start; 267 u64 len; 268 u64 logical; 269 enum btrfs_compression_type compress_type; 270 int this_mirror; 271 int failed_mirror; 272}; 273 274int btrfs_repair_one_sector(struct inode *inode, 275 struct bio *failed_bio, u32 bio_offset, 276 struct page *page, unsigned int pgoff, 277 u64 start, int failed_mirror, 278 submit_bio_hook_t *submit_bio_hook); 279 280#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 281bool find_lock_delalloc_range(struct inode *inode, 282 struct page *locked_page, u64 *start, 283 u64 *end); 284#endif 285struct extent_buffer *alloc_test_extent_buffer(struct btrfs_fs_info *fs_info, 286 u64 start); 287 288#ifdef CONFIG_BTRFS_DEBUG 289void btrfs_extent_buffer_leak_debug_check(struct btrfs_fs_info *fs_info); 290#else 291#define btrfs_extent_buffer_leak_debug_check(fs_info) do {} while (0) 292#endif 293 294#endif