| 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 completion csum_done; |
| 62 | struct bvec_iter csum_saved_iter; |
| 63 | u64 orig_physical; |
| 64 | u64 orig_logical; |
| 65 | }; |
| 66 | |
| 67 | /* For metadata reads: parentness verification. */ |
| 68 | struct btrfs_tree_parent_check parent_check; |
| 69 | }; |
| 70 | |
| 71 | /* End I/O information supplied to btrfs_bio_alloc */ |
| 72 | btrfs_bio_end_io_t end_io; |
| 73 | void *private; |
| 74 | |
| 75 | /* For internal use in read end I/O handling */ |
| 76 | unsigned int mirror_num; |
| 77 | atomic_t pending_ios; |
| 78 | struct work_struct end_io_work; |
| 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; |
| 85 | |
| 86 | /* |
| 87 | * Since scrub will reuse btree inode, we need this flag to distinguish |
| 88 | * scrub bios. |
| 89 | */ |
| 90 | bool is_scrub; |
| 91 | |
| 92 | /* Whether the csum generation for data write is async. */ |
| 93 | bool async_csum; |
| 94 | |
| 95 | /* |
| 96 | * This member must come last, bio_alloc_bioset will allocate enough |
| 97 | * bytes for entire btrfs_bio but relies on bio being last. |
| 98 | */ |
| 99 | struct bio bio; |
| 100 | }; |
| 101 | |
| 102 | static inline struct btrfs_bio *btrfs_bio(struct bio *bio) |
| 103 | { |
| 104 | return container_of(bio, struct btrfs_bio, bio); |
| 105 | } |
| 106 | |
| 107 | int __init btrfs_bioset_init(void); |
| 108 | void __cold btrfs_bioset_exit(void); |
| 109 | |
| 110 | void btrfs_bio_init(struct btrfs_bio *bbio, struct btrfs_inode *inode, u64 file_offset, |
| 111 | btrfs_bio_end_io_t end_io, void *private); |
| 112 | struct btrfs_bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf, |
| 113 | struct btrfs_inode *inode, u64 file_offset, |
| 114 | btrfs_bio_end_io_t end_io, void *private); |
| 115 | void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status); |
| 116 | |
| 117 | /* Submit using blkcg_punt_bio_submit. */ |
| 118 | #define REQ_BTRFS_CGROUP_PUNT REQ_FS_PRIVATE |
| 119 | |
| 120 | void btrfs_submit_bbio(struct btrfs_bio *bbio, int mirror_num); |
| 121 | void btrfs_submit_repair_write(struct btrfs_bio *bbio, int mirror_num, bool dev_replace); |
| 122 | int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 fileoff, |
| 123 | u32 length, u64 logical, const phys_addr_t paddrs[], |
| 124 | unsigned int step, int mirror_num); |
| 125 | |
| 126 | #endif |
| 127 | |