1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 * Copyright (C) 2022 Christoph Hellwig.
10 #include <linux/bio.h>
11 #include <linux/workqueue.h>
12 #include "tree-checker.h"
17 #define BTRFS_BIO_INLINE_CSUM_SIZE 64
20 * Maximum number of sectors for a single bio to limit the size of the
21 * checksum array. This matches the number of bio_vecs per bio and thus the
22 * I/O size for buffered I/O.
24 #define BTRFS_MAX_BIO_SECTORS (256)
26 typedef void (*btrfs_bio_end_io_t)(struct btrfs_bio *bbio);
29 * Additional info to pass along bio.
31 * Mostly for btrfs specific features like csum and mirror_num.
34 unsigned int mirror_num:7;
37 * Extra indicator for metadata bios.
38 * For some btrfs bios they use pages without a mapping, thus
39 * we can not rely on page->mapping->host to determine if
40 * it's a metadata bio.
42 unsigned int is_metadata:1;
43 struct bvec_iter iter;
48 /* @device is for stripe IO submission. */
49 struct btrfs_device *device;
51 /* For data checksum verification. */
54 u8 csum_inline[BTRFS_BIO_INLINE_CSUM_SIZE];
57 /* For metadata parentness verification. */
58 struct btrfs_tree_parent_check parent_check;
61 /* End I/O information supplied to btrfs_bio_alloc */
62 btrfs_bio_end_io_t end_io;
65 /* For read end I/O handling */
66 struct work_struct end_io_work;
69 * This member must come last, bio_alloc_bioset will allocate enough
70 * bytes for entire btrfs_bio but relies on bio being last.
75 static inline struct btrfs_bio *btrfs_bio(struct bio *bio)
77 return container_of(bio, struct btrfs_bio, bio);
80 int __init btrfs_bioset_init(void);
81 void __cold btrfs_bioset_exit(void);
83 struct bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf,
84 btrfs_bio_end_io_t end_io, void *private);
85 struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size,
86 btrfs_bio_end_io_t end_io, void *private);
89 static inline void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)
91 bbio->bio.bi_status = status;
95 static inline void btrfs_bio_free_csum(struct btrfs_bio *bbio)
97 if (bbio->is_metadata)
99 if (bbio->csum != bbio->csum_inline) {
106 * Iterate through a btrfs_bio (@bbio) on a per-sector basis.
108 * bvl - struct bio_vec
109 * bbio - struct btrfs_bio
110 * iters - struct bvec_iter
111 * bio_offset - unsigned int
113 #define btrfs_bio_for_each_sector(fs_info, bvl, bbio, iter, bio_offset) \
114 for ((iter) = (bbio)->iter, (bio_offset) = 0; \
116 (((bvl) = bio_iter_iovec((&(bbio)->bio), (iter))), 1); \
117 (bio_offset) += fs_info->sectorsize, \
118 bio_advance_iter_single(&(bbio)->bio, &(iter), \
119 (fs_info)->sectorsize))
121 void btrfs_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
123 int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
124 u64 length, u64 logical, struct page *page,
125 unsigned int pg_offset, int mirror_num);