Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
cafe5635 KO |
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 | ||
9 | #include "bcache.h" | |
10 | #include "bset.h" | |
11 | #include "debug.h" | |
12 | ||
c37511b8 KO |
13 | #include <linux/blkdev.h> |
14 | ||
cafe5635 KO |
15 | /* Bios with headers */ |
16 | ||
17 | void bch_bbio_free(struct bio *bio, struct cache_set *c) | |
18 | { | |
19 | struct bbio *b = container_of(bio, struct bbio, bio); | |
1fae7cf0 | 20 | |
d19936a2 | 21 | mempool_free(b, &c->bio_meta); |
cafe5635 KO |
22 | } |
23 | ||
24 | struct bio *bch_bbio_alloc(struct cache_set *c) | |
25 | { | |
d19936a2 | 26 | struct bbio *b = mempool_alloc(&c->bio_meta, GFP_NOIO); |
cafe5635 KO |
27 | struct bio *bio = &b->bio; |
28 | ||
3a83f467 | 29 | bio_init(bio, bio->bi_inline_vecs, bucket_pages(c)); |
cafe5635 KO |
30 | |
31 | return bio; | |
32 | } | |
33 | ||
34 | void __bch_submit_bbio(struct bio *bio, struct cache_set *c) | |
35 | { | |
36 | struct bbio *b = container_of(bio, struct bbio, bio); | |
37 | ||
4f024f37 | 38 | bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0); |
74d46992 | 39 | bio_set_dev(bio, PTR_CACHE(c, &b->key, 0)->bdev); |
cafe5635 KO |
40 | |
41 | b->submit_time_us = local_clock_us(); | |
771f393e | 42 | closure_bio_submit(c, bio, bio->bi_private); |
cafe5635 KO |
43 | } |
44 | ||
45 | void bch_submit_bbio(struct bio *bio, struct cache_set *c, | |
6f10f7d1 | 46 | struct bkey *k, unsigned int ptr) |
cafe5635 KO |
47 | { |
48 | struct bbio *b = container_of(bio, struct bbio, bio); | |
1fae7cf0 | 49 | |
cafe5635 KO |
50 | bch_bkey_copy_single_ptr(&b->key, k, ptr); |
51 | __bch_submit_bbio(bio, c); | |
52 | } | |
53 | ||
54 | /* IO errors */ | |
c7b7bd07 CL |
55 | void bch_count_backing_io_errors(struct cached_dev *dc, struct bio *bio) |
56 | { | |
6f10f7d1 | 57 | unsigned int errors; |
c7b7bd07 CL |
58 | |
59 | WARN_ONCE(!dc, "NULL pointer of struct cached_dev"); | |
60 | ||
61 | errors = atomic_add_return(1, &dc->io_errors); | |
62 | if (errors < dc->error_limit) | |
63 | pr_err("%s: IO error on backing device, unrecoverable", | |
6e916a7e | 64 | dc->backing_dev_name); |
c7b7bd07 CL |
65 | else |
66 | bch_cached_dev_error(dc); | |
67 | } | |
cafe5635 | 68 | |
5138ac67 CL |
69 | void bch_count_io_errors(struct cache *ca, |
70 | blk_status_t error, | |
71 | int is_read, | |
72 | const char *m) | |
cafe5635 KO |
73 | { |
74 | /* | |
75 | * The halflife of an error is: | |
76 | * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh | |
77 | */ | |
78 | ||
79 | if (ca->set->error_decay) { | |
6f10f7d1 | 80 | unsigned int count = atomic_inc_return(&ca->io_count); |
cafe5635 KO |
81 | |
82 | while (count > ca->set->error_decay) { | |
6f10f7d1 CL |
83 | unsigned int errors; |
84 | unsigned int old = count; | |
85 | unsigned int new = count - ca->set->error_decay; | |
cafe5635 KO |
86 | |
87 | /* | |
88 | * First we subtract refresh from count; each time we | |
89 | * succesfully do so, we rescale the errors once: | |
90 | */ | |
91 | ||
92 | count = atomic_cmpxchg(&ca->io_count, old, new); | |
93 | ||
94 | if (count == old) { | |
95 | count = new; | |
96 | ||
97 | errors = atomic_read(&ca->io_errors); | |
98 | do { | |
99 | old = errors; | |
100 | new = ((uint64_t) errors * 127) / 128; | |
101 | errors = atomic_cmpxchg(&ca->io_errors, | |
102 | old, new); | |
103 | } while (old != errors); | |
104 | } | |
105 | } | |
106 | } | |
107 | ||
108 | if (error) { | |
6f10f7d1 | 109 | unsigned int errors = atomic_add_return(1 << IO_ERROR_SHIFT, |
cafe5635 KO |
110 | &ca->io_errors); |
111 | errors >>= IO_ERROR_SHIFT; | |
112 | ||
113 | if (errors < ca->set->error_limit) | |
5138ac67 | 114 | pr_err("%s: IO error on %s%s", |
6e916a7e | 115 | ca->cache_dev_name, m, |
5138ac67 | 116 | is_read ? ", recovering." : "."); |
cafe5635 KO |
117 | else |
118 | bch_cache_set_error(ca->set, | |
119 | "%s: too many IO errors %s", | |
6e916a7e | 120 | ca->cache_dev_name, m); |
cafe5635 KO |
121 | } |
122 | } | |
123 | ||
124 | void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio, | |
4e4cbee9 | 125 | blk_status_t error, const char *m) |
cafe5635 KO |
126 | { |
127 | struct bbio *b = container_of(bio, struct bbio, bio); | |
128 | struct cache *ca = PTR_CACHE(c, &b->key, 0); | |
5138ac67 | 129 | int is_read = (bio_data_dir(bio) == READ ? 1 : 0); |
cafe5635 | 130 | |
6f10f7d1 | 131 | unsigned int threshold = op_is_write(bio_op(bio)) |
cafe5635 KO |
132 | ? c->congested_write_threshold_us |
133 | : c->congested_read_threshold_us; | |
134 | ||
135 | if (threshold) { | |
6f10f7d1 | 136 | unsigned int t = local_clock_us(); |
cafe5635 KO |
137 | int us = t - b->submit_time_us; |
138 | int congested = atomic_read(&c->congested); | |
139 | ||
140 | if (us > (int) threshold) { | |
141 | int ms = us / 1024; | |
1fae7cf0 | 142 | |
cafe5635 KO |
143 | c->congested_last_us = t; |
144 | ||
145 | ms = min(ms, CONGESTED_MAX + congested); | |
146 | atomic_sub(ms, &c->congested); | |
147 | } else if (congested < 0) | |
148 | atomic_inc(&c->congested); | |
149 | } | |
150 | ||
5138ac67 | 151 | bch_count_io_errors(ca, error, is_read, m); |
cafe5635 KO |
152 | } |
153 | ||
154 | void bch_bbio_endio(struct cache_set *c, struct bio *bio, | |
4e4cbee9 | 155 | blk_status_t error, const char *m) |
cafe5635 KO |
156 | { |
157 | struct closure *cl = bio->bi_private; | |
158 | ||
159 | bch_bbio_count_io_errors(c, bio, error, m); | |
160 | bio_put(bio); | |
161 | closure_put(cl); | |
162 | } |