io.c 3.4 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_flags |= BIO_POOL_NONE << BIO_POOL_OFFSET;
  23. bio->bi_max_vecs = bucket_pages(c);
  24. bio->bi_io_vec = bio->bi_inline_vecs;
  25. return bio;
  26. }
  27. void __bch_submit_bbio(struct bio *bio, struct cache_set *c)
  28. {
  29. struct bbio *b = container_of(bio, struct bbio, bio);
  30. bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
  31. bio->bi_bdev = PTR_CACHE(c, &b->key, 0)->bdev;
  32. b->submit_time_us = local_clock_us();
  33. closure_bio_submit(bio, bio->bi_private);
  34. }
  35. void bch_submit_bbio(struct bio *bio, struct cache_set *c,
  36. struct bkey *k, unsigned ptr)
  37. {
  38. struct bbio *b = container_of(bio, struct bbio, bio);
  39. bch_bkey_copy_single_ptr(&b->key, k, ptr);
  40. __bch_submit_bbio(bio, c);
  41. }
  42. /* IO errors */
  43. void bch_count_io_errors(struct cache *ca, int error, const char *m)
  44. {
  45. /*
  46. * The halflife of an error is:
  47. * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh
  48. */
  49. if (ca->set->error_decay) {
  50. unsigned count = atomic_inc_return(&ca->io_count);
  51. while (count > ca->set->error_decay) {
  52. unsigned errors;
  53. unsigned old = count;
  54. unsigned new = count - ca->set->error_decay;
  55. /*
  56. * First we subtract refresh from count; each time we
  57. * succesfully do so, we rescale the errors once:
  58. */
  59. count = atomic_cmpxchg(&ca->io_count, old, new);
  60. if (count == old) {
  61. count = new;
  62. errors = atomic_read(&ca->io_errors);
  63. do {
  64. old = errors;
  65. new = ((uint64_t) errors * 127) / 128;
  66. errors = atomic_cmpxchg(&ca->io_errors,
  67. old, new);
  68. } while (old != errors);
  69. }
  70. }
  71. }
  72. if (error) {
  73. char buf[BDEVNAME_SIZE];
  74. unsigned errors = atomic_add_return(1 << IO_ERROR_SHIFT,
  75. &ca->io_errors);
  76. errors >>= IO_ERROR_SHIFT;
  77. if (errors < ca->set->error_limit)
  78. pr_err("%s: IO error on %s, recovering",
  79. bdevname(ca->bdev, buf), m);
  80. else
  81. bch_cache_set_error(ca->set,
  82. "%s: too many IO errors %s",
  83. bdevname(ca->bdev, buf), m);
  84. }
  85. }
  86. void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
  87. int error, const char *m)
  88. {
  89. struct bbio *b = container_of(bio, struct bbio, bio);
  90. struct cache *ca = PTR_CACHE(c, &b->key, 0);
  91. unsigned threshold = bio->bi_rw & REQ_WRITE
  92. ? c->congested_write_threshold_us
  93. : c->congested_read_threshold_us;
  94. if (threshold) {
  95. unsigned t = local_clock_us();
  96. int us = t - b->submit_time_us;
  97. int congested = atomic_read(&c->congested);
  98. if (us > (int) threshold) {
  99. int ms = us / 1024;
  100. c->congested_last_us = t;
  101. ms = min(ms, CONGESTED_MAX + congested);
  102. atomic_sub(ms, &c->congested);
  103. } else if (congested < 0)
  104. atomic_inc(&c->congested);
  105. }
  106. bch_count_io_errors(ca, error, m);
  107. }
  108. void bch_bbio_endio(struct cache_set *c, struct bio *bio,
  109. int error, const char *m)
  110. {
  111. struct closure *cl = bio->bi_private;
  112. bch_bbio_count_io_errors(c, bio, error, m);
  113. bio_put(bio);
  114. closure_put(cl);
  115. }