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