io.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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(c, 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_backing_io_errors(struct cached_dev *dc, struct bio *bio)
  42. {
  43. unsigned errors;
  44. WARN_ONCE(!dc, "NULL pointer of struct cached_dev");
  45. errors = atomic_add_return(1, &dc->io_errors);
  46. if (errors < dc->error_limit)
  47. pr_err("%s: IO error on backing device, unrecoverable",
  48. dc->backing_dev_name);
  49. else
  50. bch_cached_dev_error(dc);
  51. }
  52. void bch_count_io_errors(struct cache *ca,
  53. blk_status_t error,
  54. int is_read,
  55. const char *m)
  56. {
  57. /*
  58. * The halflife of an error is:
  59. * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh
  60. */
  61. if (ca->set->error_decay) {
  62. unsigned count = atomic_inc_return(&ca->io_count);
  63. while (count > ca->set->error_decay) {
  64. unsigned errors;
  65. unsigned old = count;
  66. unsigned new = count - ca->set->error_decay;
  67. /*
  68. * First we subtract refresh from count; each time we
  69. * succesfully do so, we rescale the errors once:
  70. */
  71. count = atomic_cmpxchg(&ca->io_count, old, new);
  72. if (count == old) {
  73. count = new;
  74. errors = atomic_read(&ca->io_errors);
  75. do {
  76. old = errors;
  77. new = ((uint64_t) errors * 127) / 128;
  78. errors = atomic_cmpxchg(&ca->io_errors,
  79. old, new);
  80. } while (old != errors);
  81. }
  82. }
  83. }
  84. if (error) {
  85. unsigned errors = atomic_add_return(1 << IO_ERROR_SHIFT,
  86. &ca->io_errors);
  87. errors >>= IO_ERROR_SHIFT;
  88. if (errors < ca->set->error_limit)
  89. pr_err("%s: IO error on %s%s",
  90. ca->cache_dev_name, m,
  91. is_read ? ", recovering." : ".");
  92. else
  93. bch_cache_set_error(ca->set,
  94. "%s: too many IO errors %s",
  95. ca->cache_dev_name, m);
  96. }
  97. }
  98. void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
  99. blk_status_t error, const char *m)
  100. {
  101. struct bbio *b = container_of(bio, struct bbio, bio);
  102. struct cache *ca = PTR_CACHE(c, &b->key, 0);
  103. int is_read = (bio_data_dir(bio) == READ ? 1 : 0);
  104. unsigned threshold = op_is_write(bio_op(bio))
  105. ? c->congested_write_threshold_us
  106. : c->congested_read_threshold_us;
  107. if (threshold) {
  108. unsigned t = local_clock_us();
  109. int us = t - b->submit_time_us;
  110. int congested = atomic_read(&c->congested);
  111. if (us > (int) threshold) {
  112. int ms = us / 1024;
  113. c->congested_last_us = t;
  114. ms = min(ms, CONGESTED_MAX + congested);
  115. atomic_sub(ms, &c->congested);
  116. } else if (congested < 0)
  117. atomic_inc(&c->congested);
  118. }
  119. bch_count_io_errors(ca, error, is_read, m);
  120. }
  121. void bch_bbio_endio(struct cache_set *c, struct bio *bio,
  122. blk_status_t error, const char *m)
  123. {
  124. struct closure *cl = bio->bi_private;
  125. bch_bbio_count_io_errors(c, bio, error, m);
  126. bio_put(bio);
  127. closure_put(cl);
  128. }