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