io.c 3.5 KB

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