blob: fac97ec2d0e2b3924f9b153cdff89ab58147f893 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Kent Overstreetcafe5632013-03-23 16:11:31 -07002/*
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
9#include "bcache.h"
10#include "bset.h"
11#include "debug.h"
12
Kent Overstreetc37511b2013-04-26 15:39:55 -070013#include <linux/blkdev.h>
14
Kent Overstreetcafe5632013-03-23 16:11:31 -070015/* Bios with headers */
16
17void bch_bbio_free(struct bio *bio, struct cache_set *c)
18{
19 struct bbio *b = container_of(bio, struct bbio, bio);
20 mempool_free(b, c->bio_meta);
21}
22
23struct bio *bch_bbio_alloc(struct cache_set *c)
24{
25 struct bbio *b = mempool_alloc(c->bio_meta, GFP_NOIO);
26 struct bio *bio = &b->bio;
27
Ming Lei3a83f462016-11-22 08:57:21 -070028 bio_init(bio, bio->bi_inline_vecs, bucket_pages(c));
Kent Overstreetcafe5632013-03-23 16:11:31 -070029
30 return bio;
31}
32
33void __bch_submit_bbio(struct bio *bio, struct cache_set *c)
34{
35 struct bbio *b = container_of(bio, struct bbio, bio);
36
Kent Overstreet4f024f32013-10-11 15:44:27 -070037 bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
Christoph Hellwig74d46992017-08-23 19:10:32 +020038 bio_set_dev(bio, PTR_CACHE(c, &b->key, 0)->bdev);
Kent Overstreetcafe5632013-03-23 16:11:31 -070039
40 b->submit_time_us = local_clock_us();
Kent Overstreet749b61d2013-11-23 23:11:25 -080041 closure_bio_submit(bio, bio->bi_private);
Kent Overstreetcafe5632013-03-23 16:11:31 -070042}
43
44void bch_submit_bbio(struct bio *bio, struct cache_set *c,
45 struct bkey *k, unsigned ptr)
46{
47 struct bbio *b = container_of(bio, struct bbio, bio);
48 bch_bkey_copy_single_ptr(&b->key, k, ptr);
49 __bch_submit_bbio(bio, c);
50}
51
52/* IO errors */
53
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020054void bch_count_io_errors(struct cache *ca, blk_status_t error, const char *m)
Kent Overstreetcafe5632013-03-23 16:11:31 -070055{
56 /*
57 * The halflife of an error is:
58 * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh
59 */
60
61 if (ca->set->error_decay) {
62 unsigned count = atomic_inc_return(&ca->io_count);
63
64 while (count > ca->set->error_decay) {
65 unsigned errors;
66 unsigned old = count;
67 unsigned new = count - ca->set->error_decay;
68
69 /*
70 * First we subtract refresh from count; each time we
71 * succesfully do so, we rescale the errors once:
72 */
73
74 count = atomic_cmpxchg(&ca->io_count, old, new);
75
76 if (count == old) {
77 count = new;
78
79 errors = atomic_read(&ca->io_errors);
80 do {
81 old = errors;
82 new = ((uint64_t) errors * 127) / 128;
83 errors = atomic_cmpxchg(&ca->io_errors,
84 old, new);
85 } while (old != errors);
86 }
87 }
88 }
89
90 if (error) {
91 char buf[BDEVNAME_SIZE];
92 unsigned errors = atomic_add_return(1 << IO_ERROR_SHIFT,
93 &ca->io_errors);
94 errors >>= IO_ERROR_SHIFT;
95
96 if (errors < ca->set->error_limit)
97 pr_err("%s: IO error on %s, recovering",
98 bdevname(ca->bdev, buf), m);
99 else
100 bch_cache_set_error(ca->set,
101 "%s: too many IO errors %s",
102 bdevname(ca->bdev, buf), m);
103 }
104}
105
106void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200107 blk_status_t error, const char *m)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700108{
109 struct bbio *b = container_of(bio, struct bbio, bio);
110 struct cache *ca = PTR_CACHE(c, &b->key, 0);
111
Mike Christiec8d93242016-06-05 14:31:47 -0500112 unsigned threshold = op_is_write(bio_op(bio))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700113 ? c->congested_write_threshold_us
114 : c->congested_read_threshold_us;
115
116 if (threshold) {
117 unsigned t = local_clock_us();
118
119 int us = t - b->submit_time_us;
120 int congested = atomic_read(&c->congested);
121
122 if (us > (int) threshold) {
123 int ms = us / 1024;
124 c->congested_last_us = t;
125
126 ms = min(ms, CONGESTED_MAX + congested);
127 atomic_sub(ms, &c->congested);
128 } else if (congested < 0)
129 atomic_inc(&c->congested);
130 }
131
132 bch_count_io_errors(ca, error, m);
133}
134
135void bch_bbio_endio(struct cache_set *c, struct bio *bio,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200136 blk_status_t error, const char *m)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700137{
138 struct closure *cl = bio->bi_private;
139
140 bch_bbio_count_io_errors(c, bio, error, m);
141 bio_put(bio);
142 closure_put(cl);
143}