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 /* For internal use in read end I/O handling */
72 struct work_struct end_io_work;
73
74 /* End I/O information supplied to btrfs_bio_alloc */
75 btrfs_bio_end_io_t end_io;
76 void *private;
77
78 atomic_t pending_ios;
79 u16 mirror_num;
80
81 /* Save the first error status of split bio. */
82 blk_status_t status;
83
84 /* Use the commit root to look up csums (data read bio only). */
85 bool csum_search_commit_root:1;
86
87 /*
88 * Since scrub will reuse btree inode, we need this flag to distinguish
89 * scrub bios.
90 */
91 bool is_scrub:1;
92
93 /* Whether the bio is coming from copy_remapped_data_io(). */
94 bool is_remap:1;
95
96 /* Whether the csum generation for data write is async. */
97 bool async_csum:1;
98
99 /* Whether the bio is written using zone append. */
100 bool can_use_append:1;
101
102 /*
103 * This member must come last, bio_alloc_bioset will allocate enough
104 * bytes for entire btrfs_bio but relies on bio being last.
105 */
106 struct bio bio;
107 };
108
btrfs_bio(struct bio * bio)109 static inline struct btrfs_bio *btrfs_bio(struct bio *bio)
110 {
111 return container_of(bio, struct btrfs_bio, bio);
112 }
113
114 int __init btrfs_bioset_init(void);
115 void __cold btrfs_bioset_exit(void);
116
117 void btrfs_bio_init(struct btrfs_bio *bbio, struct btrfs_inode *inode, u64 file_offset,
118 btrfs_bio_end_io_t end_io, void *private);
119 struct btrfs_bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf,
120 struct btrfs_inode *inode, u64 file_offset,
121 btrfs_bio_end_io_t end_io, void *private);
122 void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status);
123
124 /* Submit using blkcg_punt_bio_submit. */
125 #define REQ_BTRFS_CGROUP_PUNT REQ_FS_PRIVATE
126
127 void btrfs_submit_bbio(struct btrfs_bio *bbio, int mirror_num);
128 void btrfs_submit_repair_write(struct btrfs_bio *bbio, int mirror_num, bool dev_replace);
129 int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 fileoff,
130 u32 length, u64 logical, const phys_addr_t paddrs[],
131 unsigned int step, int mirror_num);
132
133 #endif
134