io.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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, meta_bucket_pages(&c->cache->sb));
  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 int 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 int errors;
  44. WARN_ONCE(!dc, "NULL pointer of struct cached_dev");
  45. /*
  46. * Read-ahead requests on a degrading and recovering md raid
  47. * (e.g. raid6) device might be failured immediately by md
  48. * raid code, which is not a real hardware media failure. So
  49. * we shouldn't count failed REQ_RAHEAD bio to dc->io_errors.
  50. */
  51. if (bio->bi_opf & REQ_RAHEAD) {
  52. pr_warn_ratelimited("%s: Read-ahead I/O failed on backing device, ignore\n",
  53. dc->backing_dev_name);
  54. return;
  55. }
  56. errors = atomic_add_return(1, &dc->io_errors);
  57. if (errors < dc->error_limit)
  58. pr_err("%s: IO error on backing device, unrecoverable\n",
  59. dc->backing_dev_name);
  60. else
  61. bch_cached_dev_error(dc);
  62. }
  63. void bch_count_io_errors(struct cache *ca,
  64. blk_status_t error,
  65. int is_read,
  66. const char *m)
  67. {
  68. /*
  69. * The halflife of an error is:
  70. * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh
  71. */
  72. if (ca->set->error_decay) {
  73. unsigned int count = atomic_inc_return(&ca->io_count);
  74. while (count > ca->set->error_decay) {
  75. unsigned int errors;
  76. unsigned int old = count;
  77. unsigned int new = count - ca->set->error_decay;
  78. /*
  79. * First we subtract refresh from count; each time we
  80. * successfully do so, we rescale the errors once:
  81. */
  82. count = atomic_cmpxchg(&ca->io_count, old, new);
  83. if (count == old) {
  84. count = new;
  85. errors = atomic_read(&ca->io_errors);
  86. do {
  87. old = errors;
  88. new = ((uint64_t) errors * 127) / 128;
  89. errors = atomic_cmpxchg(&ca->io_errors,
  90. old, new);
  91. } while (old != errors);
  92. }
  93. }
  94. }
  95. if (error) {
  96. unsigned int errors = atomic_add_return(1 << IO_ERROR_SHIFT,
  97. &ca->io_errors);
  98. errors >>= IO_ERROR_SHIFT;
  99. if (errors < ca->set->error_limit)
  100. pr_err("%s: IO error on %s%s\n",
  101. ca->cache_dev_name, m,
  102. is_read ? ", recovering." : ".");
  103. else
  104. bch_cache_set_error(ca->set,
  105. "%s: too many IO errors %s\n",
  106. ca->cache_dev_name, m);
  107. }
  108. }
  109. void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
  110. blk_status_t error, const char *m)
  111. {
  112. struct bbio *b = container_of(bio, struct bbio, bio);
  113. struct cache *ca = PTR_CACHE(c, &b->key, 0);
  114. int is_read = (bio_data_dir(bio) == READ ? 1 : 0);
  115. unsigned int threshold = op_is_write(bio_op(bio))
  116. ? c->congested_write_threshold_us
  117. : c->congested_read_threshold_us;
  118. if (threshold) {
  119. unsigned int t = local_clock_us();
  120. int us = t - b->submit_time_us;
  121. int congested = atomic_read(&c->congested);
  122. if (us > (int) threshold) {
  123. int ms = us / 1024;
  124. c->congested_last_us = t;
  125. ms = min(ms, CONGESTED_MAX + congested);
  126. atomic_sub(ms, &c->congested);
  127. } else if (congested < 0)
  128. atomic_inc(&c->congested);
  129. }
  130. bch_count_io_errors(ca, error, is_read, m);
  131. }
  132. void bch_bbio_endio(struct cache_set *c, struct bio *bio,
  133. blk_status_t error, const char *m)
  134. {
  135. struct closure *cl = bio->bi_private;
  136. bch_bbio_count_io_errors(c, bio, error, m);
  137. bio_put(bio);
  138. closure_put(cl);
  139. }