io.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. static unsigned bch_bio_max_sectors(struct bio *bio)
  12. {
  13. struct request_queue *q = bdev_get_queue(bio->bi_bdev);
  14. struct bio_vec bv;
  15. struct bvec_iter iter;
  16. unsigned ret = 0, seg = 0;
  17. if (bio->bi_rw & REQ_DISCARD)
  18. return min(bio_sectors(bio), q->limits.max_discard_sectors);
  19. bio_for_each_segment(bv, bio, iter) {
  20. struct bvec_merge_data bvm = {
  21. .bi_bdev = bio->bi_bdev,
  22. .bi_sector = bio->bi_iter.bi_sector,
  23. .bi_size = ret << 9,
  24. .bi_rw = bio->bi_rw,
  25. };
  26. if (seg == min_t(unsigned, BIO_MAX_PAGES,
  27. queue_max_segments(q)))
  28. break;
  29. if (q->merge_bvec_fn &&
  30. q->merge_bvec_fn(q, &bvm, &bv) < (int) bv.bv_len)
  31. break;
  32. seg++;
  33. ret += bv.bv_len >> 9;
  34. }
  35. ret = min(ret, queue_max_sectors(q));
  36. WARN_ON(!ret);
  37. ret = max_t(int, ret, bio_iovec(bio).bv_len >> 9);
  38. return ret;
  39. }
  40. static void bch_bio_submit_split_done(struct closure *cl)
  41. {
  42. struct bio_split_hook *s = container_of(cl, struct bio_split_hook, cl);
  43. s->bio->bi_end_io = s->bi_end_io;
  44. s->bio->bi_private = s->bi_private;
  45. bio_endio(s->bio, 0);
  46. closure_debug_destroy(&s->cl);
  47. mempool_free(s, s->p->bio_split_hook);
  48. }
  49. static void bch_bio_submit_split_endio(struct bio *bio, int error)
  50. {
  51. struct closure *cl = bio->bi_private;
  52. struct bio_split_hook *s = container_of(cl, struct bio_split_hook, cl);
  53. if (error)
  54. clear_bit(BIO_UPTODATE, &s->bio->bi_flags);
  55. bio_put(bio);
  56. closure_put(cl);
  57. }
  58. void bch_generic_make_request(struct bio *bio, struct bio_split_pool *p)
  59. {
  60. struct bio_split_hook *s;
  61. struct bio *n;
  62. if (!bio_has_data(bio) && !(bio->bi_rw & REQ_DISCARD))
  63. goto submit;
  64. if (bio_sectors(bio) <= bch_bio_max_sectors(bio))
  65. goto submit;
  66. s = mempool_alloc(p->bio_split_hook, GFP_NOIO);
  67. closure_init(&s->cl, NULL);
  68. s->bio = bio;
  69. s->p = p;
  70. s->bi_end_io = bio->bi_end_io;
  71. s->bi_private = bio->bi_private;
  72. bio_get(bio);
  73. do {
  74. n = bio_next_split(bio, bch_bio_max_sectors(bio),
  75. GFP_NOIO, s->p->bio_split);
  76. n->bi_end_io = bch_bio_submit_split_endio;
  77. n->bi_private = &s->cl;
  78. closure_get(&s->cl);
  79. generic_make_request(n);
  80. } while (n != bio);
  81. continue_at(&s->cl, bch_bio_submit_split_done, NULL);
  82. submit:
  83. generic_make_request(bio);
  84. }
  85. /* Bios with headers */
  86. void bch_bbio_free(struct bio *bio, struct cache_set *c)
  87. {
  88. struct bbio *b = container_of(bio, struct bbio, bio);
  89. mempool_free(b, c->bio_meta);
  90. }
  91. struct bio *bch_bbio_alloc(struct cache_set *c)
  92. {
  93. struct bbio *b = mempool_alloc(c->bio_meta, GFP_NOIO);
  94. struct bio *bio = &b->bio;
  95. bio_init(bio);
  96. bio->bi_flags |= BIO_POOL_NONE << BIO_POOL_OFFSET;
  97. bio->bi_max_vecs = bucket_pages(c);
  98. bio->bi_io_vec = bio->bi_inline_vecs;
  99. return bio;
  100. }
  101. void __bch_submit_bbio(struct bio *bio, struct cache_set *c)
  102. {
  103. struct bbio *b = container_of(bio, struct bbio, bio);
  104. bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
  105. bio->bi_bdev = PTR_CACHE(c, &b->key, 0)->bdev;
  106. b->submit_time_us = local_clock_us();
  107. closure_bio_submit(bio, bio->bi_private, PTR_CACHE(c, &b->key, 0));
  108. }
  109. void bch_submit_bbio(struct bio *bio, struct cache_set *c,
  110. struct bkey *k, unsigned ptr)
  111. {
  112. struct bbio *b = container_of(bio, struct bbio, bio);
  113. bch_bkey_copy_single_ptr(&b->key, k, ptr);
  114. __bch_submit_bbio(bio, c);
  115. }
  116. /* IO errors */
  117. void bch_count_io_errors(struct cache *ca, int error, const char *m)
  118. {
  119. /*
  120. * The halflife of an error is:
  121. * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh
  122. */
  123. if (ca->set->error_decay) {
  124. unsigned count = atomic_inc_return(&ca->io_count);
  125. while (count > ca->set->error_decay) {
  126. unsigned errors;
  127. unsigned old = count;
  128. unsigned new = count - ca->set->error_decay;
  129. /*
  130. * First we subtract refresh from count; each time we
  131. * succesfully do so, we rescale the errors once:
  132. */
  133. count = atomic_cmpxchg(&ca->io_count, old, new);
  134. if (count == old) {
  135. count = new;
  136. errors = atomic_read(&ca->io_errors);
  137. do {
  138. old = errors;
  139. new = ((uint64_t) errors * 127) / 128;
  140. errors = atomic_cmpxchg(&ca->io_errors,
  141. old, new);
  142. } while (old != errors);
  143. }
  144. }
  145. }
  146. if (error) {
  147. char buf[BDEVNAME_SIZE];
  148. unsigned errors = atomic_add_return(1 << IO_ERROR_SHIFT,
  149. &ca->io_errors);
  150. errors >>= IO_ERROR_SHIFT;
  151. if (errors < ca->set->error_limit)
  152. pr_err("%s: IO error on %s, recovering",
  153. bdevname(ca->bdev, buf), m);
  154. else
  155. bch_cache_set_error(ca->set,
  156. "%s: too many IO errors %s",
  157. bdevname(ca->bdev, buf), m);
  158. }
  159. }
  160. void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
  161. int error, const char *m)
  162. {
  163. struct bbio *b = container_of(bio, struct bbio, bio);
  164. struct cache *ca = PTR_CACHE(c, &b->key, 0);
  165. unsigned threshold = bio->bi_rw & REQ_WRITE
  166. ? c->congested_write_threshold_us
  167. : c->congested_read_threshold_us;
  168. if (threshold) {
  169. unsigned t = local_clock_us();
  170. int us = t - b->submit_time_us;
  171. int congested = atomic_read(&c->congested);
  172. if (us > (int) threshold) {
  173. int ms = us / 1024;
  174. c->congested_last_us = t;
  175. ms = min(ms, CONGESTED_MAX + congested);
  176. atomic_sub(ms, &c->congested);
  177. } else if (congested < 0)
  178. atomic_inc(&c->congested);
  179. }
  180. bch_count_io_errors(ca, error, m);
  181. }
  182. void bch_bbio_endio(struct cache_set *c, struct bio *bio,
  183. int error, const char *m)
  184. {
  185. struct closure *cl = bio->bi_private;
  186. bch_bbio_count_io_errors(c, bio, error, m);
  187. bio_put(bio);
  188. closure_put(cl);
  189. }