io.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Some low level IO code, and hacks for various block layer limitations
  4. *
  5. * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
  6. * Copyright 2012 Google, Inc.
  7. */
  8. #include "bcache.h"
  9. #include "bset.h"
  10. #include "debug.h"
  11. #include <linux/blkdev.h>
  12. /* Bios with headers */
  13. void bch_bbio_free(struct bio *bio, struct cache_set *c)
  14. {
  15. struct bbio *b = container_of(bio, struct bbio, bio);
  16. mempool_free(b, c->bio_meta);
  17. }
  18. struct bio *bch_bbio_alloc(struct cache_set *c)
  19. {
  20. struct bbio *b = mempool_alloc(c->bio_meta, GFP_NOIO);
  21. struct bio *bio = &b->bio;
  22. bio_init(bio, bio->bi_inline_vecs, bucket_pages(c));
  23. return bio;
  24. }
  25. void __bch_submit_bbio(struct bio *bio, struct cache_set *c)
  26. {
  27. struct bbio *b = container_of(bio, struct bbio, bio);
  28. bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
  29. bio_set_dev(bio, PTR_CACHE(c, &b->key, 0)->bdev);
  30. b->submit_time_us = local_clock_us();
  31. closure_bio_submit(bio, bio->bi_private);
  32. }
  33. void bch_submit_bbio(struct bio *bio, struct cache_set *c,
  34. struct bkey *k, unsigned ptr)
  35. {
  36. struct bbio *b = container_of(bio, struct bbio, bio);
  37. bch_bkey_copy_single_ptr(&b->key, k, ptr);
  38. __bch_submit_bbio(bio, c);
  39. }
  40. /* IO errors */
  41. void bch_count_io_errors(struct cache *ca, blk_status_t error, const char *m)
  42. {
  43. /*
  44. * The halflife of an error is:
  45. * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh
  46. */
  47. if (ca->set->error_decay) {
  48. unsigned count = atomic_inc_return(&ca->io_count);
  49. while (count > ca->set->error_decay) {
  50. unsigned errors;
  51. unsigned old = count;
  52. unsigned new = count - ca->set->error_decay;
  53. /*
  54. * First we subtract refresh from count; each time we
  55. * succesfully do so, we rescale the errors once:
  56. */
  57. count = atomic_cmpxchg(&ca->io_count, old, new);
  58. if (count == old) {
  59. count = new;
  60. errors = atomic_read(&ca->io_errors);
  61. do {
  62. old = errors;
  63. new = ((uint64_t) errors * 127) / 128;
  64. errors = atomic_cmpxchg(&ca->io_errors,
  65. old, new);
  66. } while (old != errors);
  67. }
  68. }
  69. }
  70. if (error) {
  71. char buf[BDEVNAME_SIZE];
  72. unsigned errors = atomic_add_return(1 << IO_ERROR_SHIFT,
  73. &ca->io_errors);
  74. errors >>= IO_ERROR_SHIFT;
  75. if (errors < ca->set->error_limit)
  76. pr_err("%s: IO error on %s, recovering",
  77. bdevname(ca->bdev, buf), m);
  78. else
  79. bch_cache_set_error(ca->set,
  80. "%s: too many IO errors %s",
  81. bdevname(ca->bdev, buf), m);
  82. }
  83. }
  84. void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
  85. blk_status_t error, const char *m)
  86. {
  87. struct bbio *b = container_of(bio, struct bbio, bio);
  88. struct cache *ca = PTR_CACHE(c, &b->key, 0);
  89. unsigned threshold = op_is_write(bio_op(bio))
  90. ? c->congested_write_threshold_us
  91. : c->congested_read_threshold_us;
  92. if (threshold) {
  93. unsigned t = local_clock_us();
  94. int us = t - b->submit_time_us;
  95. int congested = atomic_read(&c->congested);
  96. if (us > (int) threshold) {
  97. int ms = us / 1024;
  98. c->congested_last_us = t;
  99. ms = min(ms, CONGESTED_MAX + congested);
  100. atomic_sub(ms, &c->congested);
  101. } else if (congested < 0)
  102. atomic_inc(&c->congested);
  103. }
  104. bch_count_io_errors(ca, error, m);
  105. }
  106. void bch_bbio_endio(struct cache_set *c, struct bio *bio,
  107. blk_status_t error, const char *m)
  108. {
  109. struct closure *cl = bio->bi_private;
  110. bch_bbio_count_io_errors(c, bio, error, m);
  111. bio_put(bio);
  112. closure_put(cl);
  113. }