bio.c 26 KB

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