1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (C) 2007 Oracle. All rights reserved. 4 * Copyright (C) 2022 Christoph Hellwig. 5 */ 6 7 #ifndef BTRFS_BIO_H 8 #define BTRFS_BIO_H 9 10 #include <linux/types.h> 11 #include <linux/bio.h> 12 #include <linux/workqueue.h> 13 #include "tree-checker.h" 14 15 struct btrfs_bio; 16 struct btrfs_fs_info; 17 struct btrfs_inode; 18 19 #define BTRFS_BIO_INLINE_CSUM_SIZE 64 20 21 typedef void (*btrfs_bio_end_io_t)(struct btrfs_bio *bbio); 22 23 /* 24 * Highlevel btrfs I/O structure. It is allocated by btrfs_bio_alloc and 25 * passed to btrfs_submit_bbio() for mapping to the physical devices. 26 */ 27 struct btrfs_bio { 28 /* 29 * Inode and offset into it that this I/O operates on. 30 * 31 * If the inode is a data one, csum verification and read-repair 32 * will be done automatically. 33 * If the inode is a metadata one, everything is handled by the caller. 34 */ 35 struct btrfs_inode *inode; 36 u64 file_offset; 37 38 union { 39 /* 40 * For data reads: checksumming and original I/O information. 41 * (for internal use in the btrfs_submit_bbio() machinery only) 42 */ 43 struct { 44 u8 *csum; 45 u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE]; 46 struct bvec_iter saved_iter; 47 }; 48 49 /* 50 * For data writes: 51 * - ordered extent covering the bio 52 * - pointer to the checksums for this bio 53 * - original physical address from the allocator 54 * (for zone append only) 55 * - original logical address, used for checksumming fscrypt bios 56 */ 57 struct { 58 struct btrfs_ordered_extent *ordered; 59 struct btrfs_ordered_sum *sums; 60 struct work_struct csum_work; 61 struct bvec_iter csum_saved_iter; 62 u64 orig_physical; 63 u64 orig_logical; 64 }; 65 66 /* For metadata reads: parentness verification. */ 67 struct btrfs_tree_parent_check parent_check; 68 }; 69 70 /* For internal use in read end I/O handling */ 71 struct work_struct end_io_work; 72 73 /* End I/O information supplied to btrfs_bio_alloc */ 74 btrfs_bio_end_io_t end_io; 75 void *private; 76 77 atomic_t pending_ios; 78 u16 mirror_num; 79 80 /* Save the first error status of split bio. */ 81 blk_status_t status; 82 83 /* Use the commit root to look up csums (data read bio only). */ 84 bool csum_search_commit_root:1; 85 86 /* 87 * Since scrub will reuse btree inode, we need this flag to distinguish 88 * scrub bios. 89 */ 90 bool is_scrub:1; 91 92 /* Whether the bio is coming from copy_remapped_data_io(). */ 93 bool is_remap:1; 94 95 /* Whether the bio is written using zone append. */ 96 bool can_use_append:1; 97 98 /* 99 * This member must come last, bio_alloc_bioset will allocate enough 100 * bytes for entire btrfs_bio but relies on bio being last. 101 */ 102 struct bio bio; 103 }; 104 105 static inline struct btrfs_bio *btrfs_bio(struct bio *bio) 106 { 107 return container_of(bio, struct btrfs_bio, bio); 108 } 109 110 int __init btrfs_bioset_init(void); 111 void __cold btrfs_bioset_exit(void); 112 113 void btrfs_bio_init(struct btrfs_bio *bbio, struct btrfs_inode *inode, u64 file_offset, 114 btrfs_bio_end_io_t end_io, void *private); 115 struct btrfs_bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf, 116 struct btrfs_inode *inode, u64 file_offset, 117 btrfs_bio_end_io_t end_io, void *private); 118 void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status); 119 120 /* Submit using blkcg_punt_bio_submit. */ 121 #define REQ_BTRFS_CGROUP_PUNT REQ_FS_PRIVATE 122 123 void btrfs_submit_bbio(struct btrfs_bio *bbio, int mirror_num); 124 void btrfs_submit_repair_write(struct btrfs_bio *bbio, int mirror_num, bool dev_replace); 125 int btrfs_repair_bbio_failure(struct btrfs_bio *bbio, const struct bvec_iter *orig_iter, 126 u32 length, int mirror_num); 127 128 #endif 129