1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
4 * Copyright (C) 2022 Christoph Hellwig.
12 #include "async-thread.h"
13 #include "check-integrity.h"
14 #include "dev-replace.h"
15 #include "rcu-string.h"
18 static struct bio_set btrfs_bioset;
21 * Initialize a btrfs_bio structure. This skips the embedded bio itself as it
22 * is already initialized by the block layer.
24 static inline void btrfs_bio_init(struct btrfs_bio *bbio,
25 btrfs_bio_end_io_t end_io, void *private)
27 memset(bbio, 0, offsetof(struct btrfs_bio, bio));
28 bbio->end_io = end_io;
29 bbio->private = private;
33 * Allocate a btrfs_bio structure. The btrfs_bio is the main I/O container for
34 * btrfs, and is used for all I/O submitted through btrfs_submit_bio.
36 * Just like the underlying bio_alloc_bioset it will not fail as it is backed by
39 struct bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf,
40 btrfs_bio_end_io_t end_io, void *private)
44 bio = bio_alloc_bioset(NULL, nr_vecs, opf, GFP_NOFS, &btrfs_bioset);
45 btrfs_bio_init(btrfs_bio(bio), end_io, private);
49 struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size,
50 btrfs_bio_end_io_t end_io, void *private)
53 struct btrfs_bio *bbio;
55 ASSERT(offset <= UINT_MAX && size <= UINT_MAX);
57 bio = bio_alloc_clone(orig->bi_bdev, orig, GFP_NOFS, &btrfs_bioset);
58 bbio = btrfs_bio(bio);
59 btrfs_bio_init(bbio, end_io, private);
61 bio_trim(bio, offset >> 9, size >> 9);
62 bbio->iter = bio->bi_iter;
66 static void btrfs_log_dev_io_error(struct bio *bio, struct btrfs_device *dev)
68 if (!dev || !dev->bdev)
70 if (bio->bi_status != BLK_STS_IOERR && bio->bi_status != BLK_STS_TARGET)
73 if (btrfs_op(bio) == BTRFS_MAP_WRITE)
74 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
75 if (!(bio->bi_opf & REQ_RAHEAD))
76 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
77 if (bio->bi_opf & REQ_PREFLUSH)
78 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_FLUSH_ERRS);
81 static struct workqueue_struct *btrfs_end_io_wq(struct btrfs_fs_info *fs_info,
84 if (bio->bi_opf & REQ_META)
85 return fs_info->endio_meta_workers;
86 return fs_info->endio_workers;
89 static void btrfs_end_bio_work(struct work_struct *work)
91 struct btrfs_bio *bbio = container_of(work, struct btrfs_bio, end_io_work);
96 static void btrfs_simple_end_io(struct bio *bio)
98 struct btrfs_fs_info *fs_info = bio->bi_private;
99 struct btrfs_bio *bbio = btrfs_bio(bio);
101 btrfs_bio_counter_dec(fs_info);
104 btrfs_log_dev_io_error(bio, bbio->device);
106 if (bio_op(bio) == REQ_OP_READ) {
107 INIT_WORK(&bbio->end_io_work, btrfs_end_bio_work);
108 queue_work(btrfs_end_io_wq(fs_info, bio), &bbio->end_io_work);
114 static void btrfs_raid56_end_io(struct bio *bio)
116 struct btrfs_io_context *bioc = bio->bi_private;
117 struct btrfs_bio *bbio = btrfs_bio(bio);
119 btrfs_bio_counter_dec(bioc->fs_info);
120 bbio->mirror_num = bioc->mirror_num;
123 btrfs_put_bioc(bioc);
126 static void btrfs_orig_write_end_io(struct bio *bio)
128 struct btrfs_io_stripe *stripe = bio->bi_private;
129 struct btrfs_io_context *bioc = stripe->bioc;
130 struct btrfs_bio *bbio = btrfs_bio(bio);
132 btrfs_bio_counter_dec(bioc->fs_info);
134 if (bio->bi_status) {
135 atomic_inc(&bioc->error);
136 btrfs_log_dev_io_error(bio, stripe->dev);
140 * Only send an error to the higher layers if it is beyond the tolerance
143 if (atomic_read(&bioc->error) > bioc->max_errors)
144 bio->bi_status = BLK_STS_IOERR;
146 bio->bi_status = BLK_STS_OK;
149 btrfs_put_bioc(bioc);
152 static void btrfs_clone_write_end_io(struct bio *bio)
154 struct btrfs_io_stripe *stripe = bio->bi_private;
156 if (bio->bi_status) {
157 atomic_inc(&stripe->bioc->error);
158 btrfs_log_dev_io_error(bio, stripe->dev);
161 /* Pass on control to the original bio this one was cloned from */
162 bio_endio(stripe->bioc->orig_bio);
166 static void btrfs_submit_dev_bio(struct btrfs_device *dev, struct bio *bio)
168 if (!dev || !dev->bdev ||
169 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
170 (btrfs_op(bio) == BTRFS_MAP_WRITE &&
171 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
176 bio_set_dev(bio, dev->bdev);
179 * For zone append writing, bi_sector must point the beginning of the
182 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
183 u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
185 if (btrfs_dev_is_sequential(dev, physical)) {
186 u64 zone_start = round_down(physical,
187 dev->fs_info->zone_size);
189 bio->bi_iter.bi_sector = zone_start >> SECTOR_SHIFT;
191 bio->bi_opf &= ~REQ_OP_ZONE_APPEND;
192 bio->bi_opf |= REQ_OP_WRITE;
195 btrfs_debug_in_rcu(dev->fs_info,
196 "%s: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
197 __func__, bio_op(bio), bio->bi_opf, bio->bi_iter.bi_sector,
198 (unsigned long)dev->bdev->bd_dev, btrfs_dev_name(dev),
199 dev->devid, bio->bi_iter.bi_size);
201 btrfsic_check_bio(bio);
205 static void btrfs_submit_mirrored_bio(struct btrfs_io_context *bioc, int dev_nr)
207 struct bio *orig_bio = bioc->orig_bio, *bio;
209 ASSERT(bio_op(orig_bio) != REQ_OP_READ);
211 /* Reuse the bio embedded into the btrfs_bio for the last mirror */
212 if (dev_nr == bioc->num_stripes - 1) {
214 bio->bi_end_io = btrfs_orig_write_end_io;
216 bio = bio_alloc_clone(NULL, orig_bio, GFP_NOFS, &fs_bio_set);
217 bio_inc_remaining(orig_bio);
218 bio->bi_end_io = btrfs_clone_write_end_io;
221 bio->bi_private = &bioc->stripes[dev_nr];
222 bio->bi_iter.bi_sector = bioc->stripes[dev_nr].physical >> SECTOR_SHIFT;
223 bioc->stripes[dev_nr].bioc = bioc;
224 btrfs_submit_dev_bio(bioc->stripes[dev_nr].dev, bio);
227 void btrfs_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, int mirror_num)
229 u64 logical = bio->bi_iter.bi_sector << 9;
230 u64 length = bio->bi_iter.bi_size;
231 u64 map_length = length;
232 struct btrfs_io_context *bioc = NULL;
233 struct btrfs_io_stripe smap;
236 btrfs_bio_counter_inc_blocked(fs_info);
237 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
238 &bioc, &smap, &mirror_num, 1);
240 btrfs_bio_counter_dec(fs_info);
241 btrfs_bio_end_io(btrfs_bio(bio), errno_to_blk_status(ret));
245 if (map_length < length) {
247 "mapping failed logical %llu bio len %llu len %llu",
248 logical, length, map_length);
253 /* Single mirror read/write fast path */
254 btrfs_bio(bio)->mirror_num = mirror_num;
255 btrfs_bio(bio)->device = smap.dev;
256 bio->bi_iter.bi_sector = smap.physical >> SECTOR_SHIFT;
257 bio->bi_private = fs_info;
258 bio->bi_end_io = btrfs_simple_end_io;
259 btrfs_submit_dev_bio(smap.dev, bio);
260 } else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
261 /* Parity RAID write or read recovery */
262 bio->bi_private = bioc;
263 bio->bi_end_io = btrfs_raid56_end_io;
264 if (bio_op(bio) == REQ_OP_READ)
265 raid56_parity_recover(bio, bioc, mirror_num);
267 raid56_parity_write(bio, bioc);
269 /* Write to multiple mirrors */
270 int total_devs = bioc->num_stripes;
273 bioc->orig_bio = bio;
274 for (dev_nr = 0; dev_nr < total_devs; dev_nr++)
275 btrfs_submit_mirrored_bio(bioc, dev_nr);
280 * Submit a repair write.
282 * This bypasses btrfs_submit_bio deliberately, as that writes all copies in a
283 * RAID setup. Here we only want to write the one bad copy, so we do the
284 * mapping ourselves and submit the bio directly.
286 * The I/O is issued sychronously to block the repair read completion from
289 int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
290 u64 length, u64 logical, struct page *page,
291 unsigned int pg_offset, int mirror_num)
293 struct btrfs_device *dev;
298 struct btrfs_io_context *bioc = NULL;
301 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
304 if (btrfs_repair_one_zone(fs_info, logical))
310 * Avoid races with device replace and make sure our bioc has devices
311 * associated to its stripes that don't go away while we are doing the
312 * read repair operation.
314 btrfs_bio_counter_inc_blocked(fs_info);
315 if (btrfs_is_parity_mirror(fs_info, logical, length)) {
317 * Note that we don't use BTRFS_MAP_WRITE because it's supposed
318 * to update all raid stripes, but here we just want to correct
319 * bad stripe, thus BTRFS_MAP_READ is abused to only get the bad
320 * stripe's dev and sector.
322 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ, logical,
323 &map_length, &bioc, 0);
325 goto out_counter_dec;
326 ASSERT(bioc->mirror_num == 1);
328 ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, logical,
329 &map_length, &bioc, mirror_num);
331 goto out_counter_dec;
333 * This happens when dev-replace is also running, and the
334 * mirror_num indicates the dev-replace target.
336 * In this case, we don't need to do anything, as the read
337 * error just means the replace progress hasn't reached our
338 * read range, and later replace routine would handle it well.
340 if (mirror_num != bioc->mirror_num)
341 goto out_counter_dec;
344 sector = bioc->stripes[bioc->mirror_num - 1].physical >> 9;
345 dev = bioc->stripes[bioc->mirror_num - 1].dev;
346 btrfs_put_bioc(bioc);
348 if (!dev || !dev->bdev ||
349 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state)) {
351 goto out_counter_dec;
354 bio_init(&bio, dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC);
355 bio.bi_iter.bi_sector = sector;
356 __bio_add_page(&bio, page, length, pg_offset);
358 btrfsic_check_bio(&bio);
359 ret = submit_bio_wait(&bio);
361 /* try to remap that extent elsewhere? */
362 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
366 btrfs_info_rl_in_rcu(fs_info,
367 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
368 ino, start, btrfs_dev_name(dev), sector);
374 btrfs_bio_counter_dec(fs_info);
378 int __init btrfs_bioset_init(void)
380 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
381 offsetof(struct btrfs_bio, bio),
387 void __cold btrfs_bioset_exit(void)
389 bioset_exit(&btrfs_bioset);