io.c 3.3 KB

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