blob: dc44f0689eb70044c8afe2fd4b81ba62020bac61 [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
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
8#include "bcache.h"
9#include "bset.h"
10#include "debug.h"
11
Kent Overstreetc37511b2013-04-26 15:39:55 -070012#include <linux/blkdev.h>
13
Kent Overstreetcafe5632013-03-23 16:11:31 -070014static void bch_bi_idx_hack_endio(struct bio *bio, int error)
15{
16 struct bio *p = bio->bi_private;
17
18 bio_endio(p, error);
19 bio_put(bio);
20}
21
22static void bch_generic_make_request_hack(struct bio *bio)
23{
Kent Overstreet4f024f32013-10-11 15:44:27 -070024 if (bio->bi_iter.bi_idx) {
Kent Overstreeta4ad39b12013-08-07 14:24:32 -070025 int i;
26 struct bio_vec *bv;
Kent Overstreetcafe5632013-03-23 16:11:31 -070027 struct bio *clone = bio_alloc(GFP_NOIO, bio_segments(bio));
28
Kent Overstreeta4ad39b12013-08-07 14:24:32 -070029 bio_for_each_segment(bv, bio, i)
30 clone->bi_io_vec[clone->bi_vcnt++] = *bv;
Kent Overstreetcafe5632013-03-23 16:11:31 -070031
Kent Overstreet4f024f32013-10-11 15:44:27 -070032 clone->bi_iter.bi_sector = bio->bi_iter.bi_sector;
Kent Overstreetcafe5632013-03-23 16:11:31 -070033 clone->bi_bdev = bio->bi_bdev;
34 clone->bi_rw = bio->bi_rw;
35 clone->bi_vcnt = bio_segments(bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -070036 clone->bi_iter.bi_size = bio->bi_iter.bi_size;
Kent Overstreetcafe5632013-03-23 16:11:31 -070037
38 clone->bi_private = bio;
39 clone->bi_end_io = bch_bi_idx_hack_endio;
40
41 bio = clone;
42 }
43
Kent Overstreetbca97ad2013-04-20 15:26:31 -070044 /*
45 * Hack, since drivers that clone bios clone up to bi_max_vecs, but our
46 * bios might have had more than that (before we split them per device
47 * limitations).
48 *
49 * To be taken out once immutable bvec stuff is in.
50 */
51 bio->bi_max_vecs = bio->bi_vcnt;
52
Kent Overstreetcafe5632013-03-23 16:11:31 -070053 generic_make_request(bio);
54}
55
56/**
57 * bch_bio_split - split a bio
58 * @bio: bio to split
59 * @sectors: number of sectors to split from the front of @bio
60 * @gfp: gfp mask
61 * @bs: bio set to allocate from
62 *
63 * Allocates and returns a new bio which represents @sectors from the start of
64 * @bio, and updates @bio to represent the remaining sectors.
65 *
66 * If bio_sectors(@bio) was less than or equal to @sectors, returns @bio
67 * unchanged.
68 *
69 * The newly allocated bio will point to @bio's bi_io_vec, if the split was on a
70 * bvec boundry; it is the caller's responsibility to ensure that @bio is not
71 * freed before the split.
Kent Overstreetcafe5632013-03-23 16:11:31 -070072 */
73struct bio *bch_bio_split(struct bio *bio, int sectors,
74 gfp_t gfp, struct bio_set *bs)
75{
Kent Overstreet4f024f32013-10-11 15:44:27 -070076 unsigned idx = bio->bi_iter.bi_idx, vcnt = 0, nbytes = sectors << 9;
Kent Overstreetcafe5632013-03-23 16:11:31 -070077 struct bio_vec *bv;
78 struct bio *ret = NULL;
79
80 BUG_ON(sectors <= 0);
81
Kent Overstreetcafe5632013-03-23 16:11:31 -070082 if (sectors >= bio_sectors(bio))
83 return bio;
84
85 if (bio->bi_rw & REQ_DISCARD) {
86 ret = bio_alloc_bioset(gfp, 1, bs);
Kumar Amit Mehta5c694122013-05-28 00:31:15 -070087 if (!ret)
88 return NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -070089 idx = 0;
90 goto out;
91 }
92
93 bio_for_each_segment(bv, bio, idx) {
Kent Overstreet4f024f32013-10-11 15:44:27 -070094 vcnt = idx - bio->bi_iter.bi_idx;
Kent Overstreetcafe5632013-03-23 16:11:31 -070095
96 if (!nbytes) {
97 ret = bio_alloc_bioset(gfp, vcnt, bs);
98 if (!ret)
99 return NULL;
100
Kent Overstreeta4ad39b12013-08-07 14:24:32 -0700101 memcpy(ret->bi_io_vec, __bio_iovec(bio),
Kent Overstreetcafe5632013-03-23 16:11:31 -0700102 sizeof(struct bio_vec) * vcnt);
103
104 break;
105 } else if (nbytes < bv->bv_len) {
106 ret = bio_alloc_bioset(gfp, ++vcnt, bs);
107 if (!ret)
108 return NULL;
109
Kent Overstreeta4ad39b12013-08-07 14:24:32 -0700110 memcpy(ret->bi_io_vec, __bio_iovec(bio),
Kent Overstreetcafe5632013-03-23 16:11:31 -0700111 sizeof(struct bio_vec) * vcnt);
112
113 ret->bi_io_vec[vcnt - 1].bv_len = nbytes;
114 bv->bv_offset += nbytes;
115 bv->bv_len -= nbytes;
116 break;
117 }
118
119 nbytes -= bv->bv_len;
120 }
121out:
122 ret->bi_bdev = bio->bi_bdev;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700123 ret->bi_iter.bi_sector = bio->bi_iter.bi_sector;
124 ret->bi_iter.bi_size = sectors << 9;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700125 ret->bi_rw = bio->bi_rw;
126 ret->bi_vcnt = vcnt;
127 ret->bi_max_vecs = vcnt;
128
Kent Overstreet4f024f32013-10-11 15:44:27 -0700129 bio->bi_iter.bi_sector += sectors;
130 bio->bi_iter.bi_size -= sectors << 9;
131 bio->bi_iter.bi_idx = idx;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700132
133 if (bio_integrity(bio)) {
134 if (bio_integrity_clone(ret, bio, gfp)) {
135 bio_put(ret);
136 return NULL;
137 }
138
139 bio_integrity_trim(ret, 0, bio_sectors(ret));
140 bio_integrity_trim(bio, bio_sectors(ret), bio_sectors(bio));
141 }
142
143 return ret;
144}
145
146static unsigned bch_bio_max_sectors(struct bio *bio)
147{
148 unsigned ret = bio_sectors(bio);
149 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Kent Overstreet1545f132013-04-10 15:50:57 -0700150 unsigned max_segments = min_t(unsigned, BIO_MAX_PAGES,
151 queue_max_segments(q));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700152
Kent Overstreetcafe5632013-03-23 16:11:31 -0700153 if (bio->bi_rw & REQ_DISCARD)
154 return min(ret, q->limits.max_discard_sectors);
155
Kent Overstreet1545f132013-04-10 15:50:57 -0700156 if (bio_segments(bio) > max_segments ||
Kent Overstreetcafe5632013-03-23 16:11:31 -0700157 q->merge_bvec_fn) {
Kent Overstreet8e51e412013-06-06 18:15:57 -0700158 struct bio_vec *bv;
159 int i, seg = 0;
160
Kent Overstreetcafe5632013-03-23 16:11:31 -0700161 ret = 0;
162
Kent Overstreet8e51e412013-06-06 18:15:57 -0700163 bio_for_each_segment(bv, bio, i) {
Kent Overstreeta09ded82013-04-22 14:44:24 -0700164 struct bvec_merge_data bvm = {
165 .bi_bdev = bio->bi_bdev,
Kent Overstreet4f024f32013-10-11 15:44:27 -0700166 .bi_sector = bio->bi_iter.bi_sector,
Kent Overstreeta09ded82013-04-22 14:44:24 -0700167 .bi_size = ret << 9,
168 .bi_rw = bio->bi_rw,
169 };
170
Kent Overstreet8e51e412013-06-06 18:15:57 -0700171 if (seg == max_segments)
172 break;
173
Kent Overstreetcafe5632013-03-23 16:11:31 -0700174 if (q->merge_bvec_fn &&
175 q->merge_bvec_fn(q, &bvm, bv) < (int) bv->bv_len)
176 break;
177
Kent Overstreet8e51e412013-06-06 18:15:57 -0700178 seg++;
Kent Overstreeta09ded82013-04-22 14:44:24 -0700179 ret += bv->bv_len >> 9;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700180 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700181 }
182
183 ret = min(ret, queue_max_sectors(q));
184
185 WARN_ON(!ret);
Kent Overstreeta4ad39b12013-08-07 14:24:32 -0700186 ret = max_t(int, ret, bio_iovec(bio).bv_len >> 9);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700187
188 return ret;
189}
190
191static void bch_bio_submit_split_done(struct closure *cl)
192{
193 struct bio_split_hook *s = container_of(cl, struct bio_split_hook, cl);
194
195 s->bio->bi_end_io = s->bi_end_io;
196 s->bio->bi_private = s->bi_private;
197 bio_endio(s->bio, 0);
198
199 closure_debug_destroy(&s->cl);
200 mempool_free(s, s->p->bio_split_hook);
201}
202
203static void bch_bio_submit_split_endio(struct bio *bio, int error)
204{
205 struct closure *cl = bio->bi_private;
206 struct bio_split_hook *s = container_of(cl, struct bio_split_hook, cl);
207
208 if (error)
209 clear_bit(BIO_UPTODATE, &s->bio->bi_flags);
210
211 bio_put(bio);
212 closure_put(cl);
213}
214
Kent Overstreetcafe5632013-03-23 16:11:31 -0700215void bch_generic_make_request(struct bio *bio, struct bio_split_pool *p)
216{
217 struct bio_split_hook *s;
Kent Overstreet8e51e412013-06-06 18:15:57 -0700218 struct bio *n;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700219
220 if (!bio_has_data(bio) && !(bio->bi_rw & REQ_DISCARD))
221 goto submit;
222
223 if (bio_sectors(bio) <= bch_bio_max_sectors(bio))
224 goto submit;
225
226 s = mempool_alloc(p->bio_split_hook, GFP_NOIO);
Kent Overstreet8e51e412013-06-06 18:15:57 -0700227 closure_init(&s->cl, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700228
229 s->bio = bio;
230 s->p = p;
231 s->bi_end_io = bio->bi_end_io;
232 s->bi_private = bio->bi_private;
233 bio_get(bio);
234
Kent Overstreet8e51e412013-06-06 18:15:57 -0700235 do {
236 n = bch_bio_split(bio, bch_bio_max_sectors(bio),
237 GFP_NOIO, s->p->bio_split);
238
239 n->bi_end_io = bch_bio_submit_split_endio;
240 n->bi_private = &s->cl;
241
242 closure_get(&s->cl);
243 bch_generic_make_request_hack(n);
244 } while (n != bio);
245
246 continue_at(&s->cl, bch_bio_submit_split_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700247submit:
248 bch_generic_make_request_hack(bio);
249}
250
251/* Bios with headers */
252
253void bch_bbio_free(struct bio *bio, struct cache_set *c)
254{
255 struct bbio *b = container_of(bio, struct bbio, bio);
256 mempool_free(b, c->bio_meta);
257}
258
259struct bio *bch_bbio_alloc(struct cache_set *c)
260{
261 struct bbio *b = mempool_alloc(c->bio_meta, GFP_NOIO);
262 struct bio *bio = &b->bio;
263
264 bio_init(bio);
265 bio->bi_flags |= BIO_POOL_NONE << BIO_POOL_OFFSET;
266 bio->bi_max_vecs = bucket_pages(c);
267 bio->bi_io_vec = bio->bi_inline_vecs;
268
269 return bio;
270}
271
272void __bch_submit_bbio(struct bio *bio, struct cache_set *c)
273{
274 struct bbio *b = container_of(bio, struct bbio, bio);
275
Kent Overstreet4f024f32013-10-11 15:44:27 -0700276 bio->bi_iter.bi_sector = PTR_OFFSET(&b->key, 0);
277 bio->bi_bdev = PTR_CACHE(c, &b->key, 0)->bdev;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700278
279 b->submit_time_us = local_clock_us();
280 closure_bio_submit(bio, bio->bi_private, PTR_CACHE(c, &b->key, 0));
281}
282
283void bch_submit_bbio(struct bio *bio, struct cache_set *c,
284 struct bkey *k, unsigned ptr)
285{
286 struct bbio *b = container_of(bio, struct bbio, bio);
287 bch_bkey_copy_single_ptr(&b->key, k, ptr);
288 __bch_submit_bbio(bio, c);
289}
290
291/* IO errors */
292
293void bch_count_io_errors(struct cache *ca, int error, const char *m)
294{
295 /*
296 * The halflife of an error is:
297 * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh
298 */
299
300 if (ca->set->error_decay) {
301 unsigned count = atomic_inc_return(&ca->io_count);
302
303 while (count > ca->set->error_decay) {
304 unsigned errors;
305 unsigned old = count;
306 unsigned new = count - ca->set->error_decay;
307
308 /*
309 * First we subtract refresh from count; each time we
310 * succesfully do so, we rescale the errors once:
311 */
312
313 count = atomic_cmpxchg(&ca->io_count, old, new);
314
315 if (count == old) {
316 count = new;
317
318 errors = atomic_read(&ca->io_errors);
319 do {
320 old = errors;
321 new = ((uint64_t) errors * 127) / 128;
322 errors = atomic_cmpxchg(&ca->io_errors,
323 old, new);
324 } while (old != errors);
325 }
326 }
327 }
328
329 if (error) {
330 char buf[BDEVNAME_SIZE];
331 unsigned errors = atomic_add_return(1 << IO_ERROR_SHIFT,
332 &ca->io_errors);
333 errors >>= IO_ERROR_SHIFT;
334
335 if (errors < ca->set->error_limit)
336 pr_err("%s: IO error on %s, recovering",
337 bdevname(ca->bdev, buf), m);
338 else
339 bch_cache_set_error(ca->set,
340 "%s: too many IO errors %s",
341 bdevname(ca->bdev, buf), m);
342 }
343}
344
345void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
346 int error, const char *m)
347{
348 struct bbio *b = container_of(bio, struct bbio, bio);
349 struct cache *ca = PTR_CACHE(c, &b->key, 0);
350
351 unsigned threshold = bio->bi_rw & REQ_WRITE
352 ? c->congested_write_threshold_us
353 : c->congested_read_threshold_us;
354
355 if (threshold) {
356 unsigned t = local_clock_us();
357
358 int us = t - b->submit_time_us;
359 int congested = atomic_read(&c->congested);
360
361 if (us > (int) threshold) {
362 int ms = us / 1024;
363 c->congested_last_us = t;
364
365 ms = min(ms, CONGESTED_MAX + congested);
366 atomic_sub(ms, &c->congested);
367 } else if (congested < 0)
368 atomic_inc(&c->congested);
369 }
370
371 bch_count_io_errors(ca, error, m);
372}
373
374void bch_bbio_endio(struct cache_set *c, struct bio *bio,
375 int error, const char *m)
376{
377 struct closure *cl = bio->bi_private;
378
379 bch_bbio_count_io_errors(c, bio, error, m);
380 bio_put(bio);
381 closure_put(cl);
382}