blob: 0f6d69658b61bd3f7a3259d133dec8325cedebb6 [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{
24 if (bio->bi_idx) {
25 struct bio *clone = bio_alloc(GFP_NOIO, bio_segments(bio));
26
27 memcpy(clone->bi_io_vec,
28 bio_iovec(bio),
29 bio_segments(bio) * sizeof(struct bio_vec));
30
31 clone->bi_sector = bio->bi_sector;
32 clone->bi_bdev = bio->bi_bdev;
33 clone->bi_rw = bio->bi_rw;
34 clone->bi_vcnt = bio_segments(bio);
35 clone->bi_size = bio->bi_size;
36
37 clone->bi_private = bio;
38 clone->bi_end_io = bch_bi_idx_hack_endio;
39
40 bio = clone;
41 }
42
Kent Overstreetbca97ad2013-04-20 15:26:31 -070043 /*
44 * Hack, since drivers that clone bios clone up to bi_max_vecs, but our
45 * bios might have had more than that (before we split them per device
46 * limitations).
47 *
48 * To be taken out once immutable bvec stuff is in.
49 */
50 bio->bi_max_vecs = bio->bi_vcnt;
51
Kent Overstreetcafe5632013-03-23 16:11:31 -070052 generic_make_request(bio);
53}
54
55/**
56 * bch_bio_split - split a bio
57 * @bio: bio to split
58 * @sectors: number of sectors to split from the front of @bio
59 * @gfp: gfp mask
60 * @bs: bio set to allocate from
61 *
62 * Allocates and returns a new bio which represents @sectors from the start of
63 * @bio, and updates @bio to represent the remaining sectors.
64 *
65 * If bio_sectors(@bio) was less than or equal to @sectors, returns @bio
66 * unchanged.
67 *
68 * The newly allocated bio will point to @bio's bi_io_vec, if the split was on a
69 * bvec boundry; it is the caller's responsibility to ensure that @bio is not
70 * freed before the split.
71 *
72 * If bch_bio_split() is running under generic_make_request(), it's not safe to
73 * allocate more than one bio from the same bio set. Therefore, if it is running
74 * under generic_make_request() it masks out __GFP_WAIT when doing the
75 * allocation. The caller must check for failure if there's any possibility of
76 * it being called from under generic_make_request(); it is then the caller's
77 * responsibility to retry from a safe context (by e.g. punting to workqueue).
78 */
79struct bio *bch_bio_split(struct bio *bio, int sectors,
80 gfp_t gfp, struct bio_set *bs)
81{
82 unsigned idx = bio->bi_idx, vcnt = 0, nbytes = sectors << 9;
83 struct bio_vec *bv;
84 struct bio *ret = NULL;
85
86 BUG_ON(sectors <= 0);
87
88 /*
89 * If we're being called from underneath generic_make_request() and we
90 * already allocated any bios from this bio set, we risk deadlock if we
91 * use the mempool. So instead, we possibly fail and let the caller punt
92 * to workqueue or somesuch and retry in a safe context.
93 */
94 if (current->bio_list)
95 gfp &= ~__GFP_WAIT;
96
97 if (sectors >= bio_sectors(bio))
98 return bio;
99
100 if (bio->bi_rw & REQ_DISCARD) {
101 ret = bio_alloc_bioset(gfp, 1, bs);
Kumar Amit Mehta5c694122013-05-28 00:31:15 -0700102 if (!ret)
103 return NULL;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700104 idx = 0;
105 goto out;
106 }
107
108 bio_for_each_segment(bv, bio, idx) {
109 vcnt = idx - bio->bi_idx;
110
111 if (!nbytes) {
112 ret = bio_alloc_bioset(gfp, vcnt, bs);
113 if (!ret)
114 return NULL;
115
116 memcpy(ret->bi_io_vec, bio_iovec(bio),
117 sizeof(struct bio_vec) * vcnt);
118
119 break;
120 } else if (nbytes < bv->bv_len) {
121 ret = bio_alloc_bioset(gfp, ++vcnt, bs);
122 if (!ret)
123 return NULL;
124
125 memcpy(ret->bi_io_vec, bio_iovec(bio),
126 sizeof(struct bio_vec) * vcnt);
127
128 ret->bi_io_vec[vcnt - 1].bv_len = nbytes;
129 bv->bv_offset += nbytes;
130 bv->bv_len -= nbytes;
131 break;
132 }
133
134 nbytes -= bv->bv_len;
135 }
136out:
137 ret->bi_bdev = bio->bi_bdev;
138 ret->bi_sector = bio->bi_sector;
139 ret->bi_size = sectors << 9;
140 ret->bi_rw = bio->bi_rw;
141 ret->bi_vcnt = vcnt;
142 ret->bi_max_vecs = vcnt;
143
144 bio->bi_sector += sectors;
145 bio->bi_size -= sectors << 9;
146 bio->bi_idx = idx;
147
148 if (bio_integrity(bio)) {
149 if (bio_integrity_clone(ret, bio, gfp)) {
150 bio_put(ret);
151 return NULL;
152 }
153
154 bio_integrity_trim(ret, 0, bio_sectors(ret));
155 bio_integrity_trim(bio, bio_sectors(ret), bio_sectors(bio));
156 }
157
158 return ret;
159}
160
161static unsigned bch_bio_max_sectors(struct bio *bio)
162{
163 unsigned ret = bio_sectors(bio);
164 struct request_queue *q = bdev_get_queue(bio->bi_bdev);
Kent Overstreet1545f132013-04-10 15:50:57 -0700165 unsigned max_segments = min_t(unsigned, BIO_MAX_PAGES,
166 queue_max_segments(q));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700167 struct bio_vec *bv, *end = bio_iovec(bio) +
Kent Overstreet1545f132013-04-10 15:50:57 -0700168 min_t(int, bio_segments(bio), max_segments);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700169
Kent Overstreetcafe5632013-03-23 16:11:31 -0700170 if (bio->bi_rw & REQ_DISCARD)
171 return min(ret, q->limits.max_discard_sectors);
172
Kent Overstreet1545f132013-04-10 15:50:57 -0700173 if (bio_segments(bio) > max_segments ||
Kent Overstreetcafe5632013-03-23 16:11:31 -0700174 q->merge_bvec_fn) {
175 ret = 0;
176
177 for (bv = bio_iovec(bio); bv < end; bv++) {
Kent Overstreeta09ded82013-04-22 14:44:24 -0700178 struct bvec_merge_data bvm = {
179 .bi_bdev = bio->bi_bdev,
180 .bi_sector = bio->bi_sector,
181 .bi_size = ret << 9,
182 .bi_rw = bio->bi_rw,
183 };
184
Kent Overstreetcafe5632013-03-23 16:11:31 -0700185 if (q->merge_bvec_fn &&
186 q->merge_bvec_fn(q, &bvm, bv) < (int) bv->bv_len)
187 break;
188
Kent Overstreeta09ded82013-04-22 14:44:24 -0700189 ret += bv->bv_len >> 9;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700190 }
Kent Overstreetcafe5632013-03-23 16:11:31 -0700191 }
192
193 ret = min(ret, queue_max_sectors(q));
194
195 WARN_ON(!ret);
196 ret = max_t(int, ret, bio_iovec(bio)->bv_len >> 9);
197
198 return ret;
199}
200
201static void bch_bio_submit_split_done(struct closure *cl)
202{
203 struct bio_split_hook *s = container_of(cl, struct bio_split_hook, cl);
204
205 s->bio->bi_end_io = s->bi_end_io;
206 s->bio->bi_private = s->bi_private;
207 bio_endio(s->bio, 0);
208
209 closure_debug_destroy(&s->cl);
210 mempool_free(s, s->p->bio_split_hook);
211}
212
213static void bch_bio_submit_split_endio(struct bio *bio, int error)
214{
215 struct closure *cl = bio->bi_private;
216 struct bio_split_hook *s = container_of(cl, struct bio_split_hook, cl);
217
218 if (error)
219 clear_bit(BIO_UPTODATE, &s->bio->bi_flags);
220
221 bio_put(bio);
222 closure_put(cl);
223}
224
225static void __bch_bio_submit_split(struct closure *cl)
226{
227 struct bio_split_hook *s = container_of(cl, struct bio_split_hook, cl);
228 struct bio *bio = s->bio, *n;
229
230 do {
231 n = bch_bio_split(bio, bch_bio_max_sectors(bio),
232 GFP_NOIO, s->p->bio_split);
233 if (!n)
234 continue_at(cl, __bch_bio_submit_split, system_wq);
235
236 n->bi_end_io = bch_bio_submit_split_endio;
237 n->bi_private = cl;
238
239 closure_get(cl);
240 bch_generic_make_request_hack(n);
241 } while (n != bio);
242
243 continue_at(cl, bch_bio_submit_split_done, NULL);
244}
245
246void bch_generic_make_request(struct bio *bio, struct bio_split_pool *p)
247{
248 struct bio_split_hook *s;
249
250 if (!bio_has_data(bio) && !(bio->bi_rw & REQ_DISCARD))
251 goto submit;
252
253 if (bio_sectors(bio) <= bch_bio_max_sectors(bio))
254 goto submit;
255
256 s = mempool_alloc(p->bio_split_hook, GFP_NOIO);
257
258 s->bio = bio;
259 s->p = p;
260 s->bi_end_io = bio->bi_end_io;
261 s->bi_private = bio->bi_private;
262 bio_get(bio);
263
264 closure_call(&s->cl, __bch_bio_submit_split, NULL, NULL);
265 return;
266submit:
267 bch_generic_make_request_hack(bio);
268}
269
270/* Bios with headers */
271
272void bch_bbio_free(struct bio *bio, struct cache_set *c)
273{
274 struct bbio *b = container_of(bio, struct bbio, bio);
275 mempool_free(b, c->bio_meta);
276}
277
278struct bio *bch_bbio_alloc(struct cache_set *c)
279{
280 struct bbio *b = mempool_alloc(c->bio_meta, GFP_NOIO);
281 struct bio *bio = &b->bio;
282
283 bio_init(bio);
284 bio->bi_flags |= BIO_POOL_NONE << BIO_POOL_OFFSET;
285 bio->bi_max_vecs = bucket_pages(c);
286 bio->bi_io_vec = bio->bi_inline_vecs;
287
288 return bio;
289}
290
291void __bch_submit_bbio(struct bio *bio, struct cache_set *c)
292{
293 struct bbio *b = container_of(bio, struct bbio, bio);
294
295 bio->bi_sector = PTR_OFFSET(&b->key, 0);
296 bio->bi_bdev = PTR_CACHE(c, &b->key, 0)->bdev;
297
298 b->submit_time_us = local_clock_us();
299 closure_bio_submit(bio, bio->bi_private, PTR_CACHE(c, &b->key, 0));
300}
301
302void bch_submit_bbio(struct bio *bio, struct cache_set *c,
303 struct bkey *k, unsigned ptr)
304{
305 struct bbio *b = container_of(bio, struct bbio, bio);
306 bch_bkey_copy_single_ptr(&b->key, k, ptr);
307 __bch_submit_bbio(bio, c);
308}
309
310/* IO errors */
311
312void bch_count_io_errors(struct cache *ca, int error, const char *m)
313{
314 /*
315 * The halflife of an error is:
316 * log2(1/2)/log2(127/128) * refresh ~= 88 * refresh
317 */
318
319 if (ca->set->error_decay) {
320 unsigned count = atomic_inc_return(&ca->io_count);
321
322 while (count > ca->set->error_decay) {
323 unsigned errors;
324 unsigned old = count;
325 unsigned new = count - ca->set->error_decay;
326
327 /*
328 * First we subtract refresh from count; each time we
329 * succesfully do so, we rescale the errors once:
330 */
331
332 count = atomic_cmpxchg(&ca->io_count, old, new);
333
334 if (count == old) {
335 count = new;
336
337 errors = atomic_read(&ca->io_errors);
338 do {
339 old = errors;
340 new = ((uint64_t) errors * 127) / 128;
341 errors = atomic_cmpxchg(&ca->io_errors,
342 old, new);
343 } while (old != errors);
344 }
345 }
346 }
347
348 if (error) {
349 char buf[BDEVNAME_SIZE];
350 unsigned errors = atomic_add_return(1 << IO_ERROR_SHIFT,
351 &ca->io_errors);
352 errors >>= IO_ERROR_SHIFT;
353
354 if (errors < ca->set->error_limit)
355 pr_err("%s: IO error on %s, recovering",
356 bdevname(ca->bdev, buf), m);
357 else
358 bch_cache_set_error(ca->set,
359 "%s: too many IO errors %s",
360 bdevname(ca->bdev, buf), m);
361 }
362}
363
364void bch_bbio_count_io_errors(struct cache_set *c, struct bio *bio,
365 int error, const char *m)
366{
367 struct bbio *b = container_of(bio, struct bbio, bio);
368 struct cache *ca = PTR_CACHE(c, &b->key, 0);
369
370 unsigned threshold = bio->bi_rw & REQ_WRITE
371 ? c->congested_write_threshold_us
372 : c->congested_read_threshold_us;
373
374 if (threshold) {
375 unsigned t = local_clock_us();
376
377 int us = t - b->submit_time_us;
378 int congested = atomic_read(&c->congested);
379
380 if (us > (int) threshold) {
381 int ms = us / 1024;
382 c->congested_last_us = t;
383
384 ms = min(ms, CONGESTED_MAX + congested);
385 atomic_sub(ms, &c->congested);
386 } else if (congested < 0)
387 atomic_inc(&c->congested);
388 }
389
390 bch_count_io_errors(ca, error, m);
391}
392
393void bch_bbio_endio(struct cache_set *c, struct bio *bio,
394 int error, const char *m)
395{
396 struct closure *cl = bio->bi_private;
397
398 bch_bbio_count_io_errors(c, bio, error, m);
399 bio_put(bio);
400 closure_put(cl);
401}