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