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 #include <linux/bio.h>
8 #include "bio.h"
9 #include "ctree.h"
10 #include "volumes.h"
11 #include "raid56.h"
12 #include "async-thread.h"
13 #include "dev-replace.h"
14 #include "zoned.h"
15 #include "file-item.h"
16 #include "raid-stripe-tree.h"
17
18 static struct bio_set btrfs_bioset;
19 static struct bio_set btrfs_clone_bioset;
20 static struct bio_set btrfs_repair_bioset;
21 static mempool_t btrfs_failed_bio_pool;
22
23 struct btrfs_failed_bio {
24 struct btrfs_bio *bbio;
25 int num_copies;
26 atomic_t repair_count;
27 };
28
29 /* Is this a data path I/O that needs storage layer checksum and repair? */
is_data_bbio(struct btrfs_bio * bbio)30 static inline bool is_data_bbio(struct btrfs_bio *bbio)
31 {
32 return bbio->inode && is_data_inode(bbio->inode);
33 }
34
bbio_has_ordered_extent(struct btrfs_bio * bbio)35 static bool bbio_has_ordered_extent(struct btrfs_bio *bbio)
36 {
37 return is_data_bbio(bbio) && btrfs_op(&bbio->bio) == BTRFS_MAP_WRITE;
38 }
39
40 /*
41 * Initialize a btrfs_bio structure. This skips the embedded bio itself as it
42 * is already initialized by the block layer.
43 */
btrfs_bio_init(struct btrfs_bio * bbio,struct btrfs_fs_info * fs_info,btrfs_bio_end_io_t end_io,void * private)44 void btrfs_bio_init(struct btrfs_bio *bbio, struct btrfs_fs_info *fs_info,
45 btrfs_bio_end_io_t end_io, void *private)
46 {
47 memset(bbio, 0, offsetof(struct btrfs_bio, bio));
48 bbio->fs_info = fs_info;
49 bbio->end_io = end_io;
50 bbio->private = private;
51 atomic_set(&bbio->pending_ios, 1);
52 WRITE_ONCE(bbio->status, BLK_STS_OK);
53 }
54
55 /*
56 * Allocate a btrfs_bio structure. The btrfs_bio is the main I/O container for
57 * btrfs, and is used for all I/O submitted through btrfs_submit_bbio().
58 *
59 * Just like the underlying bio_alloc_bioset it will not fail as it is backed by
60 * a mempool.
61 */
btrfs_bio_alloc(unsigned int nr_vecs,blk_opf_t opf,struct btrfs_fs_info * fs_info,btrfs_bio_end_io_t end_io,void * private)62 struct btrfs_bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf,
63 struct btrfs_fs_info *fs_info,
64 btrfs_bio_end_io_t end_io, void *private)
65 {
66 struct btrfs_bio *bbio;
67 struct bio *bio;
68
69 bio = bio_alloc_bioset(NULL, nr_vecs, opf, GFP_NOFS, &btrfs_bioset);
70 bbio = btrfs_bio(bio);
71 btrfs_bio_init(bbio, fs_info, end_io, private);
72 return bbio;
73 }
74
btrfs_split_bio(struct btrfs_fs_info * fs_info,struct btrfs_bio * orig_bbio,u64 map_length)75 static struct btrfs_bio *btrfs_split_bio(struct btrfs_fs_info *fs_info,
76 struct btrfs_bio *orig_bbio,
77 u64 map_length)
78 {
79 struct btrfs_bio *bbio;
80 struct bio *bio;
81
82 bio = bio_split(&orig_bbio->bio, map_length >> SECTOR_SHIFT, GFP_NOFS,
83 &btrfs_clone_bioset);
84 if (IS_ERR(bio))
85 return ERR_CAST(bio);
86
87 bbio = btrfs_bio(bio);
88 btrfs_bio_init(bbio, fs_info, NULL, orig_bbio);
89 bbio->inode = orig_bbio->inode;
90 bbio->file_offset = orig_bbio->file_offset;
91 orig_bbio->file_offset += map_length;
92 if (bbio_has_ordered_extent(bbio)) {
93 refcount_inc(&orig_bbio->ordered->refs);
94 bbio->ordered = orig_bbio->ordered;
95 }
96 atomic_inc(&orig_bbio->pending_ios);
97 return bbio;
98 }
99
100 /* Free a bio that was never submitted to the underlying device. */
btrfs_cleanup_bio(struct btrfs_bio * bbio)101 static void btrfs_cleanup_bio(struct btrfs_bio *bbio)
102 {
103 if (bbio_has_ordered_extent(bbio))
104 btrfs_put_ordered_extent(bbio->ordered);
105 bio_put(&bbio->bio);
106 }
107
__btrfs_bio_end_io(struct btrfs_bio * bbio)108 static void __btrfs_bio_end_io(struct btrfs_bio *bbio)
109 {
110 if (bbio_has_ordered_extent(bbio)) {
111 struct btrfs_ordered_extent *ordered = bbio->ordered;
112
113 bbio->end_io(bbio);
114 btrfs_put_ordered_extent(ordered);
115 } else {
116 bbio->end_io(bbio);
117 }
118 }
119
btrfs_bio_end_io(struct btrfs_bio * bbio,blk_status_t status)120 void btrfs_bio_end_io(struct btrfs_bio *bbio, blk_status_t status)
121 {
122 bbio->bio.bi_status = status;
123 if (bbio->bio.bi_pool == &btrfs_clone_bioset) {
124 struct btrfs_bio *orig_bbio = bbio->private;
125
126 btrfs_cleanup_bio(bbio);
127 bbio = orig_bbio;
128 }
129
130 /*
131 * At this point, bbio always points to the original btrfs_bio. Save
132 * the first error in it.
133 */
134 if (status != BLK_STS_OK)
135 cmpxchg(&bbio->status, BLK_STS_OK, status);
136
137 if (atomic_dec_and_test(&bbio->pending_ios)) {
138 /* Load split bio's error which might be set above. */
139 if (status == BLK_STS_OK)
140 bbio->bio.bi_status = READ_ONCE(bbio->status);
141 __btrfs_bio_end_io(bbio);
142 }
143 }
144
next_repair_mirror(struct btrfs_failed_bio * fbio,int cur_mirror)145 static int next_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror)
146 {
147 if (cur_mirror == fbio->num_copies)
148 return cur_mirror + 1 - fbio->num_copies;
149 return cur_mirror + 1;
150 }
151
prev_repair_mirror(struct btrfs_failed_bio * fbio,int cur_mirror)152 static int prev_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror)
153 {
154 if (cur_mirror == 1)
155 return fbio->num_copies;
156 return cur_mirror - 1;
157 }
158
btrfs_repair_done(struct btrfs_failed_bio * fbio)159 static void btrfs_repair_done(struct btrfs_failed_bio *fbio)
160 {
161 if (atomic_dec_and_test(&fbio->repair_count)) {
162 btrfs_bio_end_io(fbio->bbio, fbio->bbio->bio.bi_status);
163 mempool_free(fbio, &btrfs_failed_bio_pool);
164 }
165 }
166
btrfs_end_repair_bio(struct btrfs_bio * repair_bbio,struct btrfs_device * dev)167 static void btrfs_end_repair_bio(struct btrfs_bio *repair_bbio,
168 struct btrfs_device *dev)
169 {
170 struct btrfs_failed_bio *fbio = repair_bbio->private;
171 struct btrfs_inode *inode = repair_bbio->inode;
172 struct btrfs_fs_info *fs_info = inode->root->fs_info;
173 struct bio_vec *bv = bio_first_bvec_all(&repair_bbio->bio);
174 int mirror = repair_bbio->mirror_num;
175
176 /*
177 * We can only trigger this for data bio, which doesn't support larger
178 * folios yet.
179 */
180 ASSERT(folio_order(page_folio(bv->bv_page)) == 0);
181
182 if (repair_bbio->bio.bi_status ||
183 !btrfs_data_csum_ok(repair_bbio, dev, 0, bv)) {
184 bio_reset(&repair_bbio->bio, NULL, REQ_OP_READ);
185 repair_bbio->bio.bi_iter = repair_bbio->saved_iter;
186
187 mirror = next_repair_mirror(fbio, mirror);
188 if (mirror == fbio->bbio->mirror_num) {
189 btrfs_debug(fs_info, "no mirror left");
190 fbio->bbio->bio.bi_status = BLK_STS_IOERR;
191 goto done;
192 }
193
194 btrfs_submit_bbio(repair_bbio, mirror);
195 return;
196 }
197
198 do {
199 mirror = prev_repair_mirror(fbio, mirror);
200 btrfs_repair_io_failure(fs_info, btrfs_ino(inode),
201 repair_bbio->file_offset, fs_info->sectorsize,
202 repair_bbio->saved_iter.bi_sector << SECTOR_SHIFT,
203 page_folio(bv->bv_page), bv->bv_offset, mirror);
204 } while (mirror != fbio->bbio->mirror_num);
205
206 done:
207 btrfs_repair_done(fbio);
208 bio_put(&repair_bbio->bio);
209 }
210
211 /*
212 * Try to kick off a repair read to the next available mirror for a bad sector.
213 *
214 * This primarily tries to recover good data to serve the actual read request,
215 * but also tries to write the good data back to the bad mirror(s) when a
216 * read succeeded to restore the redundancy.
217 */
repair_one_sector(struct btrfs_bio * failed_bbio,u32 bio_offset,struct bio_vec * bv,struct btrfs_failed_bio * fbio)218 static struct btrfs_failed_bio *repair_one_sector(struct btrfs_bio *failed_bbio,
219 u32 bio_offset,
220 struct bio_vec *bv,
221 struct btrfs_failed_bio *fbio)
222 {
223 struct btrfs_inode *inode = failed_bbio->inode;
224 struct btrfs_fs_info *fs_info = inode->root->fs_info;
225 const u32 sectorsize = fs_info->sectorsize;
226 const u64 logical = (failed_bbio->saved_iter.bi_sector << SECTOR_SHIFT);
227 struct btrfs_bio *repair_bbio;
228 struct bio *repair_bio;
229 int num_copies;
230 int mirror;
231
232 btrfs_debug(fs_info, "repair read error: read error at %llu",
233 failed_bbio->file_offset + bio_offset);
234
235 num_copies = btrfs_num_copies(fs_info, logical, sectorsize);
236 if (num_copies == 1) {
237 btrfs_debug(fs_info, "no copy to repair from");
238 failed_bbio->bio.bi_status = BLK_STS_IOERR;
239 return fbio;
240 }
241
242 if (!fbio) {
243 fbio = mempool_alloc(&btrfs_failed_bio_pool, GFP_NOFS);
244 fbio->bbio = failed_bbio;
245 fbio->num_copies = num_copies;
246 atomic_set(&fbio->repair_count, 1);
247 }
248
249 atomic_inc(&fbio->repair_count);
250
251 repair_bio = bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS,
252 &btrfs_repair_bioset);
253 repair_bio->bi_iter.bi_sector = failed_bbio->saved_iter.bi_sector;
254 __bio_add_page(repair_bio, bv->bv_page, bv->bv_len, bv->bv_offset);
255
256 repair_bbio = btrfs_bio(repair_bio);
257 btrfs_bio_init(repair_bbio, fs_info, NULL, fbio);
258 repair_bbio->inode = failed_bbio->inode;
259 repair_bbio->file_offset = failed_bbio->file_offset + bio_offset;
260
261 mirror = next_repair_mirror(fbio, failed_bbio->mirror_num);
262 btrfs_debug(fs_info, "submitting repair read to mirror %d", mirror);
263 btrfs_submit_bbio(repair_bbio, mirror);
264 return fbio;
265 }
266
btrfs_check_read_bio(struct btrfs_bio * bbio,struct btrfs_device * dev)267 static void btrfs_check_read_bio(struct btrfs_bio *bbio, struct btrfs_device *dev)
268 {
269 struct btrfs_inode *inode = bbio->inode;
270 struct btrfs_fs_info *fs_info = inode->root->fs_info;
271 u32 sectorsize = fs_info->sectorsize;
272 struct bvec_iter *iter = &bbio->saved_iter;
273 blk_status_t status = bbio->bio.bi_status;
274 struct btrfs_failed_bio *fbio = NULL;
275 u32 offset = 0;
276
277 /* Read-repair requires the inode field to be set by the submitter. */
278 ASSERT(inode);
279
280 /*
281 * Hand off repair bios to the repair code as there is no upper level
282 * submitter for them.
283 */
284 if (bbio->bio.bi_pool == &btrfs_repair_bioset) {
285 btrfs_end_repair_bio(bbio, dev);
286 return;
287 }
288
289 /* Clear the I/O error. A failed repair will reset it. */
290 bbio->bio.bi_status = BLK_STS_OK;
291
292 while (iter->bi_size) {
293 struct bio_vec bv = bio_iter_iovec(&bbio->bio, *iter);
294
295 bv.bv_len = min(bv.bv_len, sectorsize);
296 if (status || !btrfs_data_csum_ok(bbio, dev, offset, &bv))
297 fbio = repair_one_sector(bbio, offset, &bv, fbio);
298
299 bio_advance_iter_single(&bbio->bio, iter, sectorsize);
300 offset += sectorsize;
301 }
302
303 if (bbio->csum != bbio->csum_inline)
304 kfree(bbio->csum);
305
306 if (fbio)
307 btrfs_repair_done(fbio);
308 else
309 btrfs_bio_end_io(bbio, bbio->bio.bi_status);
310 }
311
btrfs_log_dev_io_error(struct bio * bio,struct btrfs_device * dev)312 static void btrfs_log_dev_io_error(struct bio *bio, struct btrfs_device *dev)
313 {
314 if (!dev || !dev->bdev)
315 return;
316 if (bio->bi_status != BLK_STS_IOERR && bio->bi_status != BLK_STS_TARGET)
317 return;
318
319 if (btrfs_op(bio) == BTRFS_MAP_WRITE)
320 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
321 else if (!(bio->bi_opf & REQ_RAHEAD))
322 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_READ_ERRS);
323 if (bio->bi_opf & REQ_PREFLUSH)
324 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_FLUSH_ERRS);
325 }
326
btrfs_end_io_wq(struct btrfs_fs_info * fs_info,struct bio * bio)327 static struct workqueue_struct *btrfs_end_io_wq(struct btrfs_fs_info *fs_info,
328 struct bio *bio)
329 {
330 if (bio->bi_opf & REQ_META)
331 return fs_info->endio_meta_workers;
332 return fs_info->endio_workers;
333 }
334
btrfs_end_bio_work(struct work_struct * work)335 static void btrfs_end_bio_work(struct work_struct *work)
336 {
337 struct btrfs_bio *bbio = container_of(work, struct btrfs_bio, end_io_work);
338
339 /* Metadata reads are checked and repaired by the submitter. */
340 if (is_data_bbio(bbio))
341 btrfs_check_read_bio(bbio, bbio->bio.bi_private);
342 else
343 btrfs_bio_end_io(bbio, bbio->bio.bi_status);
344 }
345
btrfs_simple_end_io(struct bio * bio)346 static void btrfs_simple_end_io(struct bio *bio)
347 {
348 struct btrfs_bio *bbio = btrfs_bio(bio);
349 struct btrfs_device *dev = bio->bi_private;
350 struct btrfs_fs_info *fs_info = bbio->fs_info;
351
352 btrfs_bio_counter_dec(fs_info);
353
354 if (bio->bi_status)
355 btrfs_log_dev_io_error(bio, dev);
356
357 if (bio_op(bio) == REQ_OP_READ) {
358 INIT_WORK(&bbio->end_io_work, btrfs_end_bio_work);
359 queue_work(btrfs_end_io_wq(fs_info, bio), &bbio->end_io_work);
360 } else {
361 if (bio_is_zone_append(bio) && !bio->bi_status)
362 btrfs_record_physical_zoned(bbio);
363 btrfs_bio_end_io(bbio, bbio->bio.bi_status);
364 }
365 }
366
btrfs_raid56_end_io(struct bio * bio)367 static void btrfs_raid56_end_io(struct bio *bio)
368 {
369 struct btrfs_io_context *bioc = bio->bi_private;
370 struct btrfs_bio *bbio = btrfs_bio(bio);
371
372 btrfs_bio_counter_dec(bioc->fs_info);
373 bbio->mirror_num = bioc->mirror_num;
374 if (bio_op(bio) == REQ_OP_READ && is_data_bbio(bbio))
375 btrfs_check_read_bio(bbio, NULL);
376 else
377 btrfs_bio_end_io(bbio, bbio->bio.bi_status);
378
379 btrfs_put_bioc(bioc);
380 }
381
btrfs_orig_write_end_io(struct bio * bio)382 static void btrfs_orig_write_end_io(struct bio *bio)
383 {
384 struct btrfs_io_stripe *stripe = bio->bi_private;
385 struct btrfs_io_context *bioc = stripe->bioc;
386 struct btrfs_bio *bbio = btrfs_bio(bio);
387
388 btrfs_bio_counter_dec(bioc->fs_info);
389
390 if (bio->bi_status) {
391 atomic_inc(&bioc->error);
392 btrfs_log_dev_io_error(bio, stripe->dev);
393 }
394
395 /*
396 * Only send an error to the higher layers if it is beyond the tolerance
397 * threshold.
398 */
399 if (atomic_read(&bioc->error) > bioc->max_errors)
400 bio->bi_status = BLK_STS_IOERR;
401 else
402 bio->bi_status = BLK_STS_OK;
403
404 if (bio_is_zone_append(bio) && !bio->bi_status)
405 stripe->physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
406
407 btrfs_bio_end_io(bbio, bbio->bio.bi_status);
408 btrfs_put_bioc(bioc);
409 }
410
btrfs_clone_write_end_io(struct bio * bio)411 static void btrfs_clone_write_end_io(struct bio *bio)
412 {
413 struct btrfs_io_stripe *stripe = bio->bi_private;
414
415 if (bio->bi_status) {
416 atomic_inc(&stripe->bioc->error);
417 btrfs_log_dev_io_error(bio, stripe->dev);
418 } else if (bio_is_zone_append(bio)) {
419 stripe->physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
420 }
421
422 /* Pass on control to the original bio this one was cloned from */
423 bio_endio(stripe->bioc->orig_bio);
424 bio_put(bio);
425 }
426
btrfs_submit_dev_bio(struct btrfs_device * dev,struct bio * bio)427 static void btrfs_submit_dev_bio(struct btrfs_device *dev, struct bio *bio)
428 {
429 if (!dev || !dev->bdev ||
430 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
431 (btrfs_op(bio) == BTRFS_MAP_WRITE &&
432 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
433 bio_io_error(bio);
434 return;
435 }
436
437 bio_set_dev(bio, dev->bdev);
438
439 /*
440 * For zone append writing, bi_sector must point the beginning of the
441 * zone
442 */
443 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
444 u64 physical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
445 u64 zone_start = round_down(physical, dev->fs_info->zone_size);
446
447 ASSERT(btrfs_dev_is_sequential(dev, physical));
448 bio->bi_iter.bi_sector = zone_start >> SECTOR_SHIFT;
449 }
450 btrfs_debug_in_rcu(dev->fs_info,
451 "%s: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
452 __func__, bio_op(bio), bio->bi_opf, bio->bi_iter.bi_sector,
453 (unsigned long)dev->bdev->bd_dev, btrfs_dev_name(dev),
454 dev->devid, bio->bi_iter.bi_size);
455
456 /*
457 * Track reads if tracking is enabled; ignore I/O operations before the
458 * filesystem is fully initialized.
459 */
460 if (dev->fs_devices->collect_fs_stats && bio_op(bio) == REQ_OP_READ && dev->fs_info)
461 percpu_counter_add(&dev->fs_info->stats_read_blocks,
462 bio->bi_iter.bi_size >> dev->fs_info->sectorsize_bits);
463
464 if (bio->bi_opf & REQ_BTRFS_CGROUP_PUNT)
465 blkcg_punt_bio_submit(bio);
466 else
467 submit_bio(bio);
468 }
469
btrfs_submit_mirrored_bio(struct btrfs_io_context * bioc,int dev_nr)470 static void btrfs_submit_mirrored_bio(struct btrfs_io_context *bioc, int dev_nr)
471 {
472 struct bio *orig_bio = bioc->orig_bio, *bio;
473
474 ASSERT(bio_op(orig_bio) != REQ_OP_READ);
475
476 /* Reuse the bio embedded into the btrfs_bio for the last mirror */
477 if (dev_nr == bioc->num_stripes - 1) {
478 bio = orig_bio;
479 bio->bi_end_io = btrfs_orig_write_end_io;
480 } else {
481 bio = bio_alloc_clone(NULL, orig_bio, GFP_NOFS, &fs_bio_set);
482 bio_inc_remaining(orig_bio);
483 bio->bi_end_io = btrfs_clone_write_end_io;
484 }
485
486 bio->bi_private = &bioc->stripes[dev_nr];
487 bio->bi_iter.bi_sector = bioc->stripes[dev_nr].physical >> SECTOR_SHIFT;
488 bioc->stripes[dev_nr].bioc = bioc;
489 bioc->size = bio->bi_iter.bi_size;
490 btrfs_submit_dev_bio(bioc->stripes[dev_nr].dev, bio);
491 }
492
btrfs_submit_bio(struct bio * bio,struct btrfs_io_context * bioc,struct btrfs_io_stripe * smap,int mirror_num)493 static void btrfs_submit_bio(struct bio *bio, struct btrfs_io_context *bioc,
494 struct btrfs_io_stripe *smap, int mirror_num)
495 {
496 if (!bioc) {
497 /* Single mirror read/write fast path. */
498 btrfs_bio(bio)->mirror_num = mirror_num;
499 bio->bi_iter.bi_sector = smap->physical >> SECTOR_SHIFT;
500 if (bio_op(bio) != REQ_OP_READ)
501 btrfs_bio(bio)->orig_physical = smap->physical;
502 bio->bi_private = smap->dev;
503 bio->bi_end_io = btrfs_simple_end_io;
504 btrfs_submit_dev_bio(smap->dev, bio);
505 } else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
506 /* Parity RAID write or read recovery. */
507 bio->bi_private = bioc;
508 bio->bi_end_io = btrfs_raid56_end_io;
509 if (bio_op(bio) == REQ_OP_READ)
510 raid56_parity_recover(bio, bioc, mirror_num);
511 else
512 raid56_parity_write(bio, bioc);
513 } else {
514 /* Write to multiple mirrors. */
515 int total_devs = bioc->num_stripes;
516
517 bioc->orig_bio = bio;
518 for (int dev_nr = 0; dev_nr < total_devs; dev_nr++)
519 btrfs_submit_mirrored_bio(bioc, dev_nr);
520 }
521 }
522
btrfs_bio_csum(struct btrfs_bio * bbio)523 static blk_status_t btrfs_bio_csum(struct btrfs_bio *bbio)
524 {
525 if (bbio->bio.bi_opf & REQ_META)
526 return btree_csum_one_bio(bbio);
527 return btrfs_csum_one_bio(bbio);
528 }
529
530 /*
531 * Async submit bios are used to offload expensive checksumming onto the worker
532 * threads.
533 */
534 struct async_submit_bio {
535 struct btrfs_bio *bbio;
536 struct btrfs_io_context *bioc;
537 struct btrfs_io_stripe smap;
538 int mirror_num;
539 struct btrfs_work work;
540 };
541
542 /*
543 * In order to insert checksums into the metadata in large chunks, we wait
544 * until bio submission time. All the pages in the bio are checksummed and
545 * sums are attached onto the ordered extent record.
546 *
547 * At IO completion time the csums attached on the ordered extent record are
548 * inserted into the btree.
549 */
run_one_async_start(struct btrfs_work * work)550 static void run_one_async_start(struct btrfs_work *work)
551 {
552 struct async_submit_bio *async =
553 container_of(work, struct async_submit_bio, work);
554 blk_status_t ret;
555
556 ret = btrfs_bio_csum(async->bbio);
557 if (ret)
558 async->bbio->bio.bi_status = ret;
559 }
560
561 /*
562 * In order to insert checksums into the metadata in large chunks, we wait
563 * until bio submission time. All the pages in the bio are checksummed and
564 * sums are attached onto the ordered extent record.
565 *
566 * At IO completion time the csums attached on the ordered extent record are
567 * inserted into the tree.
568 *
569 * If called with @do_free == true, then it will free the work struct.
570 */
run_one_async_done(struct btrfs_work * work,bool do_free)571 static void run_one_async_done(struct btrfs_work *work, bool do_free)
572 {
573 struct async_submit_bio *async =
574 container_of(work, struct async_submit_bio, work);
575 struct bio *bio = &async->bbio->bio;
576
577 if (do_free) {
578 kfree(container_of(work, struct async_submit_bio, work));
579 return;
580 }
581
582 /* If an error occurred we just want to clean up the bio and move on. */
583 if (bio->bi_status) {
584 btrfs_bio_end_io(async->bbio, async->bbio->bio.bi_status);
585 return;
586 }
587
588 /*
589 * All of the bios that pass through here are from async helpers.
590 * Use REQ_BTRFS_CGROUP_PUNT to issue them from the owning cgroup's
591 * context. This changes nothing when cgroups aren't in use.
592 */
593 bio->bi_opf |= REQ_BTRFS_CGROUP_PUNT;
594 btrfs_submit_bio(bio, async->bioc, &async->smap, async->mirror_num);
595 }
596
should_async_write(struct btrfs_bio * bbio)597 static bool should_async_write(struct btrfs_bio *bbio)
598 {
599 bool auto_csum_mode = true;
600
601 #ifdef CONFIG_BTRFS_EXPERIMENTAL
602 struct btrfs_fs_devices *fs_devices = bbio->fs_info->fs_devices;
603 enum btrfs_offload_csum_mode csum_mode = READ_ONCE(fs_devices->offload_csum_mode);
604
605 if (csum_mode == BTRFS_OFFLOAD_CSUM_FORCE_OFF)
606 return false;
607
608 auto_csum_mode = (csum_mode == BTRFS_OFFLOAD_CSUM_AUTO);
609 #endif
610
611 /* Submit synchronously if the checksum implementation is fast. */
612 if (auto_csum_mode && test_bit(BTRFS_FS_CSUM_IMPL_FAST, &bbio->fs_info->flags))
613 return false;
614
615 /*
616 * Try to defer the submission to a workqueue to parallelize the
617 * checksum calculation unless the I/O is issued synchronously.
618 */
619 if (op_is_sync(bbio->bio.bi_opf))
620 return false;
621
622 /* Zoned devices require I/O to be submitted in order. */
623 if ((bbio->bio.bi_opf & REQ_META) && btrfs_is_zoned(bbio->fs_info))
624 return false;
625
626 return true;
627 }
628
629 /*
630 * Submit bio to an async queue.
631 *
632 * Return true if the work has been successfully submitted, else false.
633 */
btrfs_wq_submit_bio(struct btrfs_bio * bbio,struct btrfs_io_context * bioc,struct btrfs_io_stripe * smap,int mirror_num)634 static bool btrfs_wq_submit_bio(struct btrfs_bio *bbio,
635 struct btrfs_io_context *bioc,
636 struct btrfs_io_stripe *smap, int mirror_num)
637 {
638 struct btrfs_fs_info *fs_info = bbio->fs_info;
639 struct async_submit_bio *async;
640
641 async = kmalloc(sizeof(*async), GFP_NOFS);
642 if (!async)
643 return false;
644
645 async->bbio = bbio;
646 async->bioc = bioc;
647 async->smap = *smap;
648 async->mirror_num = mirror_num;
649
650 btrfs_init_work(&async->work, run_one_async_start, run_one_async_done);
651 btrfs_queue_work(fs_info->workers, &async->work);
652 return true;
653 }
654
btrfs_append_map_length(struct btrfs_bio * bbio,u64 map_length)655 static u64 btrfs_append_map_length(struct btrfs_bio *bbio, u64 map_length)
656 {
657 unsigned int nr_segs;
658 int sector_offset;
659
660 map_length = min(map_length, bbio->fs_info->max_zone_append_size);
661 sector_offset = bio_split_rw_at(&bbio->bio, &bbio->fs_info->limits,
662 &nr_segs, map_length);
663 if (sector_offset) {
664 /*
665 * bio_split_rw_at() could split at a size smaller than our
666 * sectorsize and thus cause unaligned I/Os. Fix that by
667 * always rounding down to the nearest boundary.
668 */
669 return ALIGN_DOWN(sector_offset << SECTOR_SHIFT, bbio->fs_info->sectorsize);
670 }
671 return map_length;
672 }
673
btrfs_submit_chunk(struct btrfs_bio * bbio,int mirror_num)674 static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num)
675 {
676 struct btrfs_inode *inode = bbio->inode;
677 struct btrfs_fs_info *fs_info = bbio->fs_info;
678 struct bio *bio = &bbio->bio;
679 u64 logical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
680 u64 length = bio->bi_iter.bi_size;
681 u64 map_length = length;
682 bool use_append = btrfs_use_zone_append(bbio);
683 struct btrfs_io_context *bioc = NULL;
684 struct btrfs_io_stripe smap;
685 blk_status_t ret;
686 int error;
687
688 if (!bbio->inode || btrfs_is_data_reloc_root(inode->root))
689 smap.rst_search_commit_root = true;
690 else
691 smap.rst_search_commit_root = false;
692
693 btrfs_bio_counter_inc_blocked(fs_info);
694 error = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
695 &bioc, &smap, &mirror_num);
696 if (error) {
697 ret = errno_to_blk_status(error);
698 btrfs_bio_counter_dec(fs_info);
699 goto end_bbio;
700 }
701
702 map_length = min(map_length, length);
703 if (use_append)
704 map_length = btrfs_append_map_length(bbio, map_length);
705
706 if (map_length < length) {
707 struct btrfs_bio *split;
708
709 split = btrfs_split_bio(fs_info, bbio, map_length);
710 if (IS_ERR(split)) {
711 ret = errno_to_blk_status(PTR_ERR(split));
712 btrfs_bio_counter_dec(fs_info);
713 goto end_bbio;
714 }
715 bbio = split;
716 bio = &bbio->bio;
717 }
718
719 /*
720 * Save the iter for the end_io handler and preload the checksums for
721 * data reads.
722 */
723 if (bio_op(bio) == REQ_OP_READ && is_data_bbio(bbio)) {
724 bbio->saved_iter = bio->bi_iter;
725 ret = btrfs_lookup_bio_sums(bbio);
726 if (ret)
727 goto fail;
728 }
729
730 if (btrfs_op(bio) == BTRFS_MAP_WRITE) {
731 if (use_append) {
732 bio->bi_opf &= ~REQ_OP_WRITE;
733 bio->bi_opf |= REQ_OP_ZONE_APPEND;
734 }
735
736 if (is_data_bbio(bbio) && bioc && bioc->use_rst) {
737 /*
738 * No locking for the list update, as we only add to
739 * the list in the I/O submission path, and list
740 * iteration only happens in the completion path, which
741 * can't happen until after the last submission.
742 */
743 btrfs_get_bioc(bioc);
744 list_add_tail(&bioc->rst_ordered_entry, &bbio->ordered->bioc_list);
745 }
746
747 /*
748 * Csum items for reloc roots have already been cloned at this
749 * point, so they are handled as part of the no-checksum case.
750 */
751 if (inode && !(inode->flags & BTRFS_INODE_NODATASUM) &&
752 !test_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state) &&
753 !btrfs_is_data_reloc_root(inode->root)) {
754 if (should_async_write(bbio) &&
755 btrfs_wq_submit_bio(bbio, bioc, &smap, mirror_num))
756 goto done;
757
758 ret = btrfs_bio_csum(bbio);
759 if (ret)
760 goto fail;
761 } else if (use_append ||
762 (btrfs_is_zoned(fs_info) && inode &&
763 inode->flags & BTRFS_INODE_NODATASUM)) {
764 ret = btrfs_alloc_dummy_sum(bbio);
765 if (ret)
766 goto fail;
767 }
768 }
769
770 btrfs_submit_bio(bio, bioc, &smap, mirror_num);
771 done:
772 return map_length == length;
773
774 fail:
775 btrfs_bio_counter_dec(fs_info);
776 /*
777 * We have split the original bbio, now we have to end both the current
778 * @bbio and remaining one, as the remaining one will never be submitted.
779 */
780 if (map_length < length) {
781 struct btrfs_bio *remaining = bbio->private;
782
783 ASSERT(bbio->bio.bi_pool == &btrfs_clone_bioset);
784 ASSERT(remaining);
785
786 btrfs_bio_end_io(remaining, ret);
787 }
788 end_bbio:
789 btrfs_bio_end_io(bbio, ret);
790 /* Do not submit another chunk */
791 return true;
792 }
793
btrfs_submit_bbio(struct btrfs_bio * bbio,int mirror_num)794 void btrfs_submit_bbio(struct btrfs_bio *bbio, int mirror_num)
795 {
796 /* If bbio->inode is not populated, its file_offset must be 0. */
797 ASSERT(bbio->inode || bbio->file_offset == 0);
798
799 while (!btrfs_submit_chunk(bbio, mirror_num))
800 ;
801 }
802
803 /*
804 * Submit a repair write.
805 *
806 * This bypasses btrfs_submit_bbio() deliberately, as that writes all copies in a
807 * RAID setup. Here we only want to write the one bad copy, so we do the
808 * mapping ourselves and submit the bio directly.
809 *
810 * The I/O is issued synchronously to block the repair read completion from
811 * freeing the bio.
812 */
btrfs_repair_io_failure(struct btrfs_fs_info * fs_info,u64 ino,u64 start,u64 length,u64 logical,struct folio * folio,unsigned int folio_offset,int mirror_num)813 int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
814 u64 length, u64 logical, struct folio *folio,
815 unsigned int folio_offset, int mirror_num)
816 {
817 struct btrfs_io_stripe smap = { 0 };
818 struct bio_vec bvec;
819 struct bio bio;
820 int ret = 0;
821
822 ASSERT(!(fs_info->sb->s_flags & SB_RDONLY));
823 BUG_ON(!mirror_num);
824
825 if (btrfs_repair_one_zone(fs_info, logical))
826 return 0;
827
828 /*
829 * Avoid races with device replace and make sure our bioc has devices
830 * associated to its stripes that don't go away while we are doing the
831 * read repair operation.
832 */
833 btrfs_bio_counter_inc_blocked(fs_info);
834 ret = btrfs_map_repair_block(fs_info, &smap, logical, length, mirror_num);
835 if (ret < 0)
836 goto out_counter_dec;
837
838 if (!smap.dev->bdev ||
839 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &smap.dev->dev_state)) {
840 ret = -EIO;
841 goto out_counter_dec;
842 }
843
844 bio_init(&bio, smap.dev->bdev, &bvec, 1, REQ_OP_WRITE | REQ_SYNC);
845 bio.bi_iter.bi_sector = smap.physical >> SECTOR_SHIFT;
846 ret = bio_add_folio(&bio, folio, length, folio_offset);
847 ASSERT(ret);
848 ret = submit_bio_wait(&bio);
849 if (ret) {
850 /* try to remap that extent elsewhere? */
851 btrfs_dev_stat_inc_and_print(smap.dev, BTRFS_DEV_STAT_WRITE_ERRS);
852 goto out_bio_uninit;
853 }
854
855 btrfs_info_rl_in_rcu(fs_info,
856 "read error corrected: ino %llu off %llu (dev %s sector %llu)",
857 ino, start, btrfs_dev_name(smap.dev),
858 smap.physical >> SECTOR_SHIFT);
859 ret = 0;
860
861 out_bio_uninit:
862 bio_uninit(&bio);
863 out_counter_dec:
864 btrfs_bio_counter_dec(fs_info);
865 return ret;
866 }
867
868 /*
869 * Submit a btrfs_bio based repair write.
870 *
871 * If @dev_replace is true, the write would be submitted to dev-replace target.
872 */
btrfs_submit_repair_write(struct btrfs_bio * bbio,int mirror_num,bool dev_replace)873 void btrfs_submit_repair_write(struct btrfs_bio *bbio, int mirror_num, bool dev_replace)
874 {
875 struct btrfs_fs_info *fs_info = bbio->fs_info;
876 u64 logical = bbio->bio.bi_iter.bi_sector << SECTOR_SHIFT;
877 u64 length = bbio->bio.bi_iter.bi_size;
878 struct btrfs_io_stripe smap = { 0 };
879 int ret;
880
881 ASSERT(fs_info);
882 ASSERT(mirror_num > 0);
883 ASSERT(btrfs_op(&bbio->bio) == BTRFS_MAP_WRITE);
884 ASSERT(!bbio->inode);
885
886 btrfs_bio_counter_inc_blocked(fs_info);
887 ret = btrfs_map_repair_block(fs_info, &smap, logical, length, mirror_num);
888 if (ret < 0)
889 goto fail;
890
891 if (dev_replace) {
892 ASSERT(smap.dev == fs_info->dev_replace.srcdev);
893 smap.dev = fs_info->dev_replace.tgtdev;
894 }
895 btrfs_submit_bio(&bbio->bio, NULL, &smap, mirror_num);
896 return;
897
898 fail:
899 btrfs_bio_counter_dec(fs_info);
900 btrfs_bio_end_io(bbio, errno_to_blk_status(ret));
901 }
902
btrfs_bioset_init(void)903 int __init btrfs_bioset_init(void)
904 {
905 if (bioset_init(&btrfs_bioset, BIO_POOL_SIZE,
906 offsetof(struct btrfs_bio, bio),
907 BIOSET_NEED_BVECS))
908 return -ENOMEM;
909 if (bioset_init(&btrfs_clone_bioset, BIO_POOL_SIZE,
910 offsetof(struct btrfs_bio, bio), 0))
911 goto out_free_bioset;
912 if (bioset_init(&btrfs_repair_bioset, BIO_POOL_SIZE,
913 offsetof(struct btrfs_bio, bio),
914 BIOSET_NEED_BVECS))
915 goto out_free_clone_bioset;
916 if (mempool_init_kmalloc_pool(&btrfs_failed_bio_pool, BIO_POOL_SIZE,
917 sizeof(struct btrfs_failed_bio)))
918 goto out_free_repair_bioset;
919 return 0;
920
921 out_free_repair_bioset:
922 bioset_exit(&btrfs_repair_bioset);
923 out_free_clone_bioset:
924 bioset_exit(&btrfs_clone_bioset);
925 out_free_bioset:
926 bioset_exit(&btrfs_bioset);
927 return -ENOMEM;
928 }
929
btrfs_bioset_exit(void)930 void __cold btrfs_bioset_exit(void)
931 {
932 mempool_exit(&btrfs_failed_bio_pool);
933 bioset_exit(&btrfs_repair_bioset);
934 bioset_exit(&btrfs_clone_bioset);
935 bioset_exit(&btrfs_bioset);
936 }
937