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