]>
Commit | Line | Data |
---|---|---|
cafe5635 KO |
1 | /* |
2 | * Main bcache entry point - handle a read or a write request and decide what to | |
3 | * do with it; the make_request functions are called by the block layer. | |
4 | * | |
5 | * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> | |
6 | * Copyright 2012 Google, Inc. | |
7 | */ | |
8 | ||
9 | #include "bcache.h" | |
10 | #include "btree.h" | |
11 | #include "debug.h" | |
12 | #include "request.h" | |
279afbad | 13 | #include "writeback.h" |
cafe5635 KO |
14 | |
15 | #include <linux/cgroup.h> | |
16 | #include <linux/module.h> | |
17 | #include <linux/hash.h> | |
18 | #include <linux/random.h> | |
19 | #include "blk-cgroup.h" | |
20 | ||
21 | #include <trace/events/bcache.h> | |
22 | ||
23 | #define CUTOFF_CACHE_ADD 95 | |
24 | #define CUTOFF_CACHE_READA 90 | |
cafe5635 KO |
25 | |
26 | struct kmem_cache *bch_search_cache; | |
27 | ||
a34a8bfd KO |
28 | static void bch_data_insert_start(struct closure *); |
29 | ||
cafe5635 KO |
30 | /* Cgroup interface */ |
31 | ||
32 | #ifdef CONFIG_CGROUP_BCACHE | |
33 | static struct bch_cgroup bcache_default_cgroup = { .cache_mode = -1 }; | |
34 | ||
35 | static struct bch_cgroup *cgroup_to_bcache(struct cgroup *cgroup) | |
36 | { | |
37 | struct cgroup_subsys_state *css; | |
38 | return cgroup && | |
39 | (css = cgroup_subsys_state(cgroup, bcache_subsys_id)) | |
40 | ? container_of(css, struct bch_cgroup, css) | |
41 | : &bcache_default_cgroup; | |
42 | } | |
43 | ||
44 | struct bch_cgroup *bch_bio_to_cgroup(struct bio *bio) | |
45 | { | |
46 | struct cgroup_subsys_state *css = bio->bi_css | |
47 | ? cgroup_subsys_state(bio->bi_css->cgroup, bcache_subsys_id) | |
48 | : task_subsys_state(current, bcache_subsys_id); | |
49 | ||
50 | return css | |
51 | ? container_of(css, struct bch_cgroup, css) | |
52 | : &bcache_default_cgroup; | |
53 | } | |
54 | ||
55 | static ssize_t cache_mode_read(struct cgroup *cgrp, struct cftype *cft, | |
56 | struct file *file, | |
57 | char __user *buf, size_t nbytes, loff_t *ppos) | |
58 | { | |
59 | char tmp[1024]; | |
169ef1cf KO |
60 | int len = bch_snprint_string_list(tmp, PAGE_SIZE, bch_cache_modes, |
61 | cgroup_to_bcache(cgrp)->cache_mode + 1); | |
cafe5635 KO |
62 | |
63 | if (len < 0) | |
64 | return len; | |
65 | ||
66 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); | |
67 | } | |
68 | ||
69 | static int cache_mode_write(struct cgroup *cgrp, struct cftype *cft, | |
70 | const char *buf) | |
71 | { | |
169ef1cf | 72 | int v = bch_read_string_list(buf, bch_cache_modes); |
cafe5635 KO |
73 | if (v < 0) |
74 | return v; | |
75 | ||
76 | cgroup_to_bcache(cgrp)->cache_mode = v - 1; | |
77 | return 0; | |
78 | } | |
79 | ||
80 | static u64 bch_verify_read(struct cgroup *cgrp, struct cftype *cft) | |
81 | { | |
82 | return cgroup_to_bcache(cgrp)->verify; | |
83 | } | |
84 | ||
85 | static int bch_verify_write(struct cgroup *cgrp, struct cftype *cft, u64 val) | |
86 | { | |
87 | cgroup_to_bcache(cgrp)->verify = val; | |
88 | return 0; | |
89 | } | |
90 | ||
91 | static u64 bch_cache_hits_read(struct cgroup *cgrp, struct cftype *cft) | |
92 | { | |
93 | struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); | |
94 | return atomic_read(&bcachecg->stats.cache_hits); | |
95 | } | |
96 | ||
97 | static u64 bch_cache_misses_read(struct cgroup *cgrp, struct cftype *cft) | |
98 | { | |
99 | struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); | |
100 | return atomic_read(&bcachecg->stats.cache_misses); | |
101 | } | |
102 | ||
103 | static u64 bch_cache_bypass_hits_read(struct cgroup *cgrp, | |
104 | struct cftype *cft) | |
105 | { | |
106 | struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); | |
107 | return atomic_read(&bcachecg->stats.cache_bypass_hits); | |
108 | } | |
109 | ||
110 | static u64 bch_cache_bypass_misses_read(struct cgroup *cgrp, | |
111 | struct cftype *cft) | |
112 | { | |
113 | struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); | |
114 | return atomic_read(&bcachecg->stats.cache_bypass_misses); | |
115 | } | |
116 | ||
117 | static struct cftype bch_files[] = { | |
118 | { | |
119 | .name = "cache_mode", | |
120 | .read = cache_mode_read, | |
121 | .write_string = cache_mode_write, | |
122 | }, | |
123 | { | |
124 | .name = "verify", | |
125 | .read_u64 = bch_verify_read, | |
126 | .write_u64 = bch_verify_write, | |
127 | }, | |
128 | { | |
129 | .name = "cache_hits", | |
130 | .read_u64 = bch_cache_hits_read, | |
131 | }, | |
132 | { | |
133 | .name = "cache_misses", | |
134 | .read_u64 = bch_cache_misses_read, | |
135 | }, | |
136 | { | |
137 | .name = "cache_bypass_hits", | |
138 | .read_u64 = bch_cache_bypass_hits_read, | |
139 | }, | |
140 | { | |
141 | .name = "cache_bypass_misses", | |
142 | .read_u64 = bch_cache_bypass_misses_read, | |
143 | }, | |
144 | { } /* terminate */ | |
145 | }; | |
146 | ||
147 | static void init_bch_cgroup(struct bch_cgroup *cg) | |
148 | { | |
149 | cg->cache_mode = -1; | |
150 | } | |
151 | ||
152 | static struct cgroup_subsys_state *bcachecg_create(struct cgroup *cgroup) | |
153 | { | |
154 | struct bch_cgroup *cg; | |
155 | ||
156 | cg = kzalloc(sizeof(*cg), GFP_KERNEL); | |
157 | if (!cg) | |
158 | return ERR_PTR(-ENOMEM); | |
159 | init_bch_cgroup(cg); | |
160 | return &cg->css; | |
161 | } | |
162 | ||
163 | static void bcachecg_destroy(struct cgroup *cgroup) | |
164 | { | |
165 | struct bch_cgroup *cg = cgroup_to_bcache(cgroup); | |
166 | free_css_id(&bcache_subsys, &cg->css); | |
167 | kfree(cg); | |
168 | } | |
169 | ||
170 | struct cgroup_subsys bcache_subsys = { | |
171 | .create = bcachecg_create, | |
172 | .destroy = bcachecg_destroy, | |
173 | .subsys_id = bcache_subsys_id, | |
174 | .name = "bcache", | |
175 | .module = THIS_MODULE, | |
176 | }; | |
177 | EXPORT_SYMBOL_GPL(bcache_subsys); | |
178 | #endif | |
179 | ||
180 | static unsigned cache_mode(struct cached_dev *dc, struct bio *bio) | |
181 | { | |
182 | #ifdef CONFIG_CGROUP_BCACHE | |
183 | int r = bch_bio_to_cgroup(bio)->cache_mode; | |
184 | if (r >= 0) | |
185 | return r; | |
186 | #endif | |
187 | return BDEV_CACHE_MODE(&dc->sb); | |
188 | } | |
189 | ||
190 | static bool verify(struct cached_dev *dc, struct bio *bio) | |
191 | { | |
192 | #ifdef CONFIG_CGROUP_BCACHE | |
193 | if (bch_bio_to_cgroup(bio)->verify) | |
194 | return true; | |
195 | #endif | |
196 | return dc->verify; | |
197 | } | |
198 | ||
199 | static void bio_csum(struct bio *bio, struct bkey *k) | |
200 | { | |
7988613b KO |
201 | struct bio_vec bv; |
202 | struct bvec_iter iter; | |
cafe5635 | 203 | uint64_t csum = 0; |
cafe5635 | 204 | |
7988613b KO |
205 | bio_for_each_segment(bv, bio, iter) { |
206 | void *d = kmap(bv.bv_page) + bv.bv_offset; | |
207 | csum = bch_crc64_update(csum, d, bv.bv_len); | |
208 | kunmap(bv.bv_page); | |
cafe5635 KO |
209 | } |
210 | ||
211 | k->ptr[KEY_PTRS(k)] = csum & (~0ULL >> 1); | |
212 | } | |
213 | ||
214 | /* Insert data into cache */ | |
215 | ||
a34a8bfd | 216 | static void bch_data_insert_keys(struct closure *cl) |
cafe5635 | 217 | { |
220bb38c | 218 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
c18536a7 | 219 | atomic_t *journal_ref = NULL; |
220bb38c | 220 | struct bkey *replace_key = op->replace ? &op->replace_key : NULL; |
6054c6d4 | 221 | int ret; |
cafe5635 | 222 | |
a34a8bfd KO |
223 | /* |
224 | * If we're looping, might already be waiting on | |
225 | * another journal write - can't wait on more than one journal write at | |
226 | * a time | |
227 | * | |
228 | * XXX: this looks wrong | |
229 | */ | |
230 | #if 0 | |
231 | while (atomic_read(&s->cl.remaining) & CLOSURE_WAITING) | |
232 | closure_sync(&s->cl); | |
233 | #endif | |
cafe5635 | 234 | |
220bb38c KO |
235 | if (!op->replace) |
236 | journal_ref = bch_journal(op->c, &op->insert_keys, | |
237 | op->flush_journal ? cl : NULL); | |
cafe5635 | 238 | |
220bb38c | 239 | ret = bch_btree_insert(op->c, &op->insert_keys, |
6054c6d4 KO |
240 | journal_ref, replace_key); |
241 | if (ret == -ESRCH) { | |
220bb38c | 242 | op->replace_collision = true; |
6054c6d4 | 243 | } else if (ret) { |
220bb38c KO |
244 | op->error = -ENOMEM; |
245 | op->insert_data_done = true; | |
a34a8bfd | 246 | } |
cafe5635 | 247 | |
c18536a7 KO |
248 | if (journal_ref) |
249 | atomic_dec_bug(journal_ref); | |
cafe5635 | 250 | |
220bb38c | 251 | if (!op->insert_data_done) |
a34a8bfd | 252 | continue_at(cl, bch_data_insert_start, bcache_wq); |
cafe5635 | 253 | |
220bb38c | 254 | bch_keylist_free(&op->insert_keys); |
a34a8bfd | 255 | closure_return(cl); |
cafe5635 KO |
256 | } |
257 | ||
a34a8bfd KO |
258 | static void bch_data_invalidate(struct closure *cl) |
259 | { | |
220bb38c KO |
260 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
261 | struct bio *bio = op->bio; | |
a34a8bfd KO |
262 | |
263 | pr_debug("invalidating %i sectors from %llu", | |
4f024f37 | 264 | bio_sectors(bio), (uint64_t) bio->bi_iter.bi_sector); |
a34a8bfd KO |
265 | |
266 | while (bio_sectors(bio)) { | |
81ab4190 KO |
267 | unsigned sectors = min(bio_sectors(bio), |
268 | 1U << (KEY_SIZE_BITS - 1)); | |
a34a8bfd | 269 | |
220bb38c | 270 | if (bch_keylist_realloc(&op->insert_keys, 0, op->c)) |
a34a8bfd KO |
271 | goto out; |
272 | ||
4f024f37 KO |
273 | bio->bi_iter.bi_sector += sectors; |
274 | bio->bi_iter.bi_size -= sectors << 9; | |
a34a8bfd | 275 | |
220bb38c | 276 | bch_keylist_add(&op->insert_keys, |
4f024f37 | 277 | &KEY(op->inode, bio->bi_iter.bi_sector, sectors)); |
a34a8bfd KO |
278 | } |
279 | ||
220bb38c | 280 | op->insert_data_done = true; |
a34a8bfd KO |
281 | bio_put(bio); |
282 | out: | |
283 | continue_at(cl, bch_data_insert_keys, bcache_wq); | |
284 | } | |
285 | ||
286 | static void bch_data_insert_error(struct closure *cl) | |
cafe5635 | 287 | { |
220bb38c | 288 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
cafe5635 KO |
289 | |
290 | /* | |
291 | * Our data write just errored, which means we've got a bunch of keys to | |
292 | * insert that point to data that wasn't succesfully written. | |
293 | * | |
294 | * We don't have to insert those keys but we still have to invalidate | |
295 | * that region of the cache - so, if we just strip off all the pointers | |
296 | * from the keys we'll accomplish just that. | |
297 | */ | |
298 | ||
220bb38c | 299 | struct bkey *src = op->insert_keys.keys, *dst = op->insert_keys.keys; |
cafe5635 | 300 | |
220bb38c | 301 | while (src != op->insert_keys.top) { |
cafe5635 KO |
302 | struct bkey *n = bkey_next(src); |
303 | ||
304 | SET_KEY_PTRS(src, 0); | |
c2f95ae2 | 305 | memmove(dst, src, bkey_bytes(src)); |
cafe5635 KO |
306 | |
307 | dst = bkey_next(dst); | |
308 | src = n; | |
309 | } | |
310 | ||
220bb38c | 311 | op->insert_keys.top = dst; |
cafe5635 | 312 | |
a34a8bfd | 313 | bch_data_insert_keys(cl); |
cafe5635 KO |
314 | } |
315 | ||
a34a8bfd | 316 | static void bch_data_insert_endio(struct bio *bio, int error) |
cafe5635 KO |
317 | { |
318 | struct closure *cl = bio->bi_private; | |
220bb38c | 319 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
cafe5635 KO |
320 | |
321 | if (error) { | |
322 | /* TODO: We could try to recover from this. */ | |
220bb38c KO |
323 | if (op->writeback) |
324 | op->error = error; | |
325 | else if (!op->replace) | |
a34a8bfd | 326 | set_closure_fn(cl, bch_data_insert_error, bcache_wq); |
cafe5635 KO |
327 | else |
328 | set_closure_fn(cl, NULL, NULL); | |
329 | } | |
330 | ||
220bb38c | 331 | bch_bbio_endio(op->c, bio, error, "writing data to cache"); |
cafe5635 KO |
332 | } |
333 | ||
a34a8bfd | 334 | static void bch_data_insert_start(struct closure *cl) |
cafe5635 | 335 | { |
220bb38c KO |
336 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
337 | struct bio *bio = op->bio, *n; | |
cafe5635 | 338 | |
220bb38c | 339 | if (op->bypass) |
a34a8bfd | 340 | return bch_data_invalidate(cl); |
cafe5635 | 341 | |
220bb38c KO |
342 | if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0) { |
343 | set_gc_sectors(op->c); | |
344 | wake_up_gc(op->c); | |
cafe5635 KO |
345 | } |
346 | ||
54d12f2b KO |
347 | /* |
348 | * Journal writes are marked REQ_FLUSH; if the original write was a | |
349 | * flush, it'll wait on the journal write. | |
350 | */ | |
351 | bio->bi_rw &= ~(REQ_FLUSH|REQ_FUA); | |
352 | ||
cafe5635 KO |
353 | do { |
354 | unsigned i; | |
355 | struct bkey *k; | |
220bb38c | 356 | struct bio_set *split = op->c->bio_split; |
cafe5635 KO |
357 | |
358 | /* 1 for the device pointer and 1 for the chksum */ | |
220bb38c KO |
359 | if (bch_keylist_realloc(&op->insert_keys, |
360 | 1 + (op->csum ? 1 : 0), | |
361 | op->c)) | |
a34a8bfd | 362 | continue_at(cl, bch_data_insert_keys, bcache_wq); |
cafe5635 | 363 | |
220bb38c | 364 | k = op->insert_keys.top; |
cafe5635 | 365 | bkey_init(k); |
220bb38c | 366 | SET_KEY_INODE(k, op->inode); |
4f024f37 | 367 | SET_KEY_OFFSET(k, bio->bi_iter.bi_sector); |
cafe5635 | 368 | |
2599b53b KO |
369 | if (!bch_alloc_sectors(op->c, k, bio_sectors(bio), |
370 | op->write_point, op->write_prio, | |
371 | op->writeback)) | |
cafe5635 KO |
372 | goto err; |
373 | ||
20d0189b | 374 | n = bio_next_split(bio, KEY_SIZE(k), GFP_NOIO, split); |
cafe5635 | 375 | |
a34a8bfd | 376 | n->bi_end_io = bch_data_insert_endio; |
cafe5635 KO |
377 | n->bi_private = cl; |
378 | ||
220bb38c | 379 | if (op->writeback) { |
cafe5635 KO |
380 | SET_KEY_DIRTY(k, true); |
381 | ||
382 | for (i = 0; i < KEY_PTRS(k); i++) | |
220bb38c | 383 | SET_GC_MARK(PTR_BUCKET(op->c, k, i), |
cafe5635 KO |
384 | GC_MARK_DIRTY); |
385 | } | |
386 | ||
220bb38c | 387 | SET_KEY_CSUM(k, op->csum); |
cafe5635 KO |
388 | if (KEY_CSUM(k)) |
389 | bio_csum(n, k); | |
390 | ||
c37511b8 | 391 | trace_bcache_cache_insert(k); |
220bb38c | 392 | bch_keylist_push(&op->insert_keys); |
cafe5635 | 393 | |
cafe5635 | 394 | n->bi_rw |= REQ_WRITE; |
220bb38c | 395 | bch_submit_bbio(n, op->c, k, 0); |
cafe5635 KO |
396 | } while (n != bio); |
397 | ||
220bb38c | 398 | op->insert_data_done = true; |
a34a8bfd | 399 | continue_at(cl, bch_data_insert_keys, bcache_wq); |
cafe5635 KO |
400 | err: |
401 | /* bch_alloc_sectors() blocks if s->writeback = true */ | |
220bb38c | 402 | BUG_ON(op->writeback); |
cafe5635 KO |
403 | |
404 | /* | |
405 | * But if it's not a writeback write we'd rather just bail out if | |
406 | * there aren't any buckets ready to write to - it might take awhile and | |
407 | * we might be starving btree writes for gc or something. | |
408 | */ | |
409 | ||
220bb38c | 410 | if (!op->replace) { |
cafe5635 KO |
411 | /* |
412 | * Writethrough write: We can't complete the write until we've | |
413 | * updated the index. But we don't want to delay the write while | |
414 | * we wait for buckets to be freed up, so just invalidate the | |
415 | * rest of the write. | |
416 | */ | |
220bb38c | 417 | op->bypass = true; |
a34a8bfd | 418 | return bch_data_invalidate(cl); |
cafe5635 KO |
419 | } else { |
420 | /* | |
421 | * From a cache miss, we can just insert the keys for the data | |
422 | * we have written or bail out if we didn't do anything. | |
423 | */ | |
220bb38c | 424 | op->insert_data_done = true; |
cafe5635 KO |
425 | bio_put(bio); |
426 | ||
220bb38c | 427 | if (!bch_keylist_empty(&op->insert_keys)) |
a34a8bfd | 428 | continue_at(cl, bch_data_insert_keys, bcache_wq); |
cafe5635 KO |
429 | else |
430 | closure_return(cl); | |
431 | } | |
432 | } | |
433 | ||
434 | /** | |
a34a8bfd | 435 | * bch_data_insert - stick some data in the cache |
cafe5635 KO |
436 | * |
437 | * This is the starting point for any data to end up in a cache device; it could | |
438 | * be from a normal write, or a writeback write, or a write to a flash only | |
439 | * volume - it's also used by the moving garbage collector to compact data in | |
440 | * mostly empty buckets. | |
441 | * | |
442 | * It first writes the data to the cache, creating a list of keys to be inserted | |
443 | * (if the data had to be fragmented there will be multiple keys); after the | |
444 | * data is written it calls bch_journal, and after the keys have been added to | |
445 | * the next journal write they're inserted into the btree. | |
446 | * | |
c18536a7 | 447 | * It inserts the data in s->cache_bio; bi_sector is used for the key offset, |
cafe5635 KO |
448 | * and op->inode is used for the key inode. |
449 | * | |
c18536a7 KO |
450 | * If s->bypass is true, instead of inserting the data it invalidates the |
451 | * region of the cache represented by s->cache_bio and op->inode. | |
cafe5635 | 452 | */ |
a34a8bfd | 453 | void bch_data_insert(struct closure *cl) |
cafe5635 | 454 | { |
220bb38c | 455 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
cafe5635 | 456 | |
220bb38c KO |
457 | trace_bcache_write(op->bio, op->writeback, op->bypass); |
458 | ||
459 | bch_keylist_init(&op->insert_keys); | |
460 | bio_get(op->bio); | |
a34a8bfd | 461 | bch_data_insert_start(cl); |
cafe5635 KO |
462 | } |
463 | ||
220bb38c KO |
464 | /* Congested? */ |
465 | ||
466 | unsigned bch_get_congested(struct cache_set *c) | |
467 | { | |
468 | int i; | |
469 | long rand; | |
470 | ||
471 | if (!c->congested_read_threshold_us && | |
472 | !c->congested_write_threshold_us) | |
473 | return 0; | |
474 | ||
475 | i = (local_clock_us() - c->congested_last_us) / 1024; | |
476 | if (i < 0) | |
477 | return 0; | |
478 | ||
479 | i += atomic_read(&c->congested); | |
480 | if (i >= 0) | |
481 | return 0; | |
482 | ||
483 | i += CONGESTED_MAX; | |
484 | ||
485 | if (i > 0) | |
486 | i = fract_exp_two(i, 6); | |
487 | ||
488 | rand = get_random_int(); | |
489 | i -= bitmap_weight(&rand, BITS_PER_LONG); | |
490 | ||
491 | return i > 0 ? i : 1; | |
492 | } | |
493 | ||
494 | static void add_sequential(struct task_struct *t) | |
495 | { | |
496 | ewma_add(t->sequential_io_avg, | |
497 | t->sequential_io, 8, 0); | |
498 | ||
499 | t->sequential_io = 0; | |
500 | } | |
501 | ||
502 | static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k) | |
503 | { | |
504 | return &dc->io_hash[hash_64(k, RECENT_IO_BITS)]; | |
505 | } | |
506 | ||
507 | static bool check_should_bypass(struct cached_dev *dc, struct bio *bio) | |
508 | { | |
509 | struct cache_set *c = dc->disk.c; | |
510 | unsigned mode = cache_mode(dc, bio); | |
511 | unsigned sectors, congested = bch_get_congested(c); | |
512 | struct task_struct *task = current; | |
8aee1220 | 513 | struct io *i; |
220bb38c | 514 | |
c4d951dd | 515 | if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) || |
220bb38c KO |
516 | c->gc_stats.in_use > CUTOFF_CACHE_ADD || |
517 | (bio->bi_rw & REQ_DISCARD)) | |
518 | goto skip; | |
519 | ||
520 | if (mode == CACHE_MODE_NONE || | |
521 | (mode == CACHE_MODE_WRITEAROUND && | |
522 | (bio->bi_rw & REQ_WRITE))) | |
523 | goto skip; | |
524 | ||
4f024f37 | 525 | if (bio->bi_iter.bi_sector & (c->sb.block_size - 1) || |
220bb38c KO |
526 | bio_sectors(bio) & (c->sb.block_size - 1)) { |
527 | pr_debug("skipping unaligned io"); | |
528 | goto skip; | |
529 | } | |
530 | ||
5ceaaad7 KO |
531 | if (bypass_torture_test(dc)) { |
532 | if ((get_random_int() & 3) == 3) | |
533 | goto skip; | |
534 | else | |
535 | goto rescale; | |
536 | } | |
537 | ||
220bb38c KO |
538 | if (!congested && !dc->sequential_cutoff) |
539 | goto rescale; | |
540 | ||
541 | if (!congested && | |
542 | mode == CACHE_MODE_WRITEBACK && | |
543 | (bio->bi_rw & REQ_WRITE) && | |
544 | (bio->bi_rw & REQ_SYNC)) | |
545 | goto rescale; | |
546 | ||
8aee1220 | 547 | spin_lock(&dc->io_lock); |
220bb38c | 548 | |
4f024f37 KO |
549 | hlist_for_each_entry(i, iohash(dc, bio->bi_iter.bi_sector), hash) |
550 | if (i->last == bio->bi_iter.bi_sector && | |
8aee1220 KO |
551 | time_before(jiffies, i->jiffies)) |
552 | goto found; | |
220bb38c | 553 | |
8aee1220 | 554 | i = list_first_entry(&dc->io_lru, struct io, lru); |
220bb38c | 555 | |
8aee1220 KO |
556 | add_sequential(task); |
557 | i->sequential = 0; | |
220bb38c | 558 | found: |
4f024f37 KO |
559 | if (i->sequential + bio->bi_iter.bi_size > i->sequential) |
560 | i->sequential += bio->bi_iter.bi_size; | |
220bb38c | 561 | |
8aee1220 KO |
562 | i->last = bio_end_sector(bio); |
563 | i->jiffies = jiffies + msecs_to_jiffies(5000); | |
564 | task->sequential_io = i->sequential; | |
220bb38c | 565 | |
8aee1220 KO |
566 | hlist_del(&i->hash); |
567 | hlist_add_head(&i->hash, iohash(dc, i->last)); | |
568 | list_move_tail(&i->lru, &dc->io_lru); | |
220bb38c | 569 | |
8aee1220 | 570 | spin_unlock(&dc->io_lock); |
220bb38c KO |
571 | |
572 | sectors = max(task->sequential_io, | |
573 | task->sequential_io_avg) >> 9; | |
574 | ||
575 | if (dc->sequential_cutoff && | |
576 | sectors >= dc->sequential_cutoff >> 9) { | |
577 | trace_bcache_bypass_sequential(bio); | |
578 | goto skip; | |
579 | } | |
580 | ||
581 | if (congested && sectors >= congested) { | |
582 | trace_bcache_bypass_congested(bio); | |
583 | goto skip; | |
584 | } | |
585 | ||
586 | rescale: | |
587 | bch_rescale_priorities(c, bio_sectors(bio)); | |
588 | return false; | |
589 | skip: | |
590 | bch_mark_sectors_bypassed(c, dc, bio_sectors(bio)); | |
591 | return true; | |
592 | } | |
593 | ||
2c1953e2 | 594 | /* Cache lookup */ |
cafe5635 | 595 | |
220bb38c KO |
596 | struct search { |
597 | /* Stack frame for bio_complete */ | |
598 | struct closure cl; | |
599 | ||
220bb38c KO |
600 | struct bbio bio; |
601 | struct bio *orig_bio; | |
602 | struct bio *cache_miss; | |
a5ae4300 | 603 | struct bcache_device *d; |
220bb38c KO |
604 | |
605 | unsigned insert_bio_sectors; | |
220bb38c | 606 | unsigned recoverable:1; |
220bb38c | 607 | unsigned write:1; |
5ceaaad7 | 608 | unsigned read_dirty_data:1; |
220bb38c KO |
609 | |
610 | unsigned long start_time; | |
611 | ||
612 | struct btree_op op; | |
613 | struct data_insert_op iop; | |
614 | }; | |
615 | ||
2c1953e2 | 616 | static void bch_cache_read_endio(struct bio *bio, int error) |
cafe5635 KO |
617 | { |
618 | struct bbio *b = container_of(bio, struct bbio, bio); | |
619 | struct closure *cl = bio->bi_private; | |
620 | struct search *s = container_of(cl, struct search, cl); | |
621 | ||
622 | /* | |
623 | * If the bucket was reused while our bio was in flight, we might have | |
624 | * read the wrong data. Set s->error but not error so it doesn't get | |
625 | * counted against the cache device, but we'll still reread the data | |
626 | * from the backing device. | |
627 | */ | |
628 | ||
629 | if (error) | |
220bb38c | 630 | s->iop.error = error; |
d56d000a KO |
631 | else if (!KEY_DIRTY(&b->key) && |
632 | ptr_stale(s->iop.c, &b->key, 0)) { | |
220bb38c KO |
633 | atomic_long_inc(&s->iop.c->cache_read_races); |
634 | s->iop.error = -EINTR; | |
cafe5635 KO |
635 | } |
636 | ||
220bb38c | 637 | bch_bbio_endio(s->iop.c, bio, error, "reading from cache"); |
cafe5635 KO |
638 | } |
639 | ||
2c1953e2 KO |
640 | /* |
641 | * Read from a single key, handling the initial cache miss if the key starts in | |
642 | * the middle of the bio | |
643 | */ | |
cc231966 | 644 | static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k) |
2c1953e2 KO |
645 | { |
646 | struct search *s = container_of(op, struct search, op); | |
cc231966 KO |
647 | struct bio *n, *bio = &s->bio.bio; |
648 | struct bkey *bio_key; | |
2c1953e2 | 649 | unsigned ptr; |
2c1953e2 | 650 | |
4f024f37 | 651 | if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0)) <= 0) |
cc231966 KO |
652 | return MAP_CONTINUE; |
653 | ||
220bb38c | 654 | if (KEY_INODE(k) != s->iop.inode || |
4f024f37 | 655 | KEY_START(k) > bio->bi_iter.bi_sector) { |
cc231966 | 656 | unsigned bio_sectors = bio_sectors(bio); |
220bb38c | 657 | unsigned sectors = KEY_INODE(k) == s->iop.inode |
cc231966 | 658 | ? min_t(uint64_t, INT_MAX, |
4f024f37 | 659 | KEY_START(k) - bio->bi_iter.bi_sector) |
cc231966 KO |
660 | : INT_MAX; |
661 | ||
662 | int ret = s->d->cache_miss(b, s, bio, sectors); | |
663 | if (ret != MAP_CONTINUE) | |
664 | return ret; | |
665 | ||
666 | /* if this was a complete miss we shouldn't get here */ | |
667 | BUG_ON(bio_sectors <= sectors); | |
668 | } | |
669 | ||
670 | if (!KEY_SIZE(k)) | |
671 | return MAP_CONTINUE; | |
2c1953e2 KO |
672 | |
673 | /* XXX: figure out best pointer - for multiple cache devices */ | |
674 | ptr = 0; | |
675 | ||
676 | PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO; | |
677 | ||
5ceaaad7 KO |
678 | if (KEY_DIRTY(k)) |
679 | s->read_dirty_data = true; | |
680 | ||
20d0189b KO |
681 | n = bio_next_split(bio, min_t(uint64_t, INT_MAX, |
682 | KEY_OFFSET(k) - bio->bi_iter.bi_sector), | |
683 | GFP_NOIO, s->d->bio_split); | |
2c1953e2 | 684 | |
cc231966 KO |
685 | bio_key = &container_of(n, struct bbio, bio)->key; |
686 | bch_bkey_copy_single_ptr(bio_key, k, ptr); | |
2c1953e2 | 687 | |
4f024f37 | 688 | bch_cut_front(&KEY(s->iop.inode, n->bi_iter.bi_sector, 0), bio_key); |
220bb38c | 689 | bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key); |
2c1953e2 | 690 | |
cc231966 KO |
691 | n->bi_end_io = bch_cache_read_endio; |
692 | n->bi_private = &s->cl; | |
2c1953e2 | 693 | |
cc231966 KO |
694 | /* |
695 | * The bucket we're reading from might be reused while our bio | |
696 | * is in flight, and we could then end up reading the wrong | |
697 | * data. | |
698 | * | |
699 | * We guard against this by checking (in cache_read_endio()) if | |
700 | * the pointer is stale again; if so, we treat it as an error | |
701 | * and reread from the backing device (but we don't pass that | |
702 | * error up anywhere). | |
703 | */ | |
2c1953e2 | 704 | |
cc231966 KO |
705 | __bch_submit_bbio(n, b->c); |
706 | return n == bio ? MAP_DONE : MAP_CONTINUE; | |
2c1953e2 KO |
707 | } |
708 | ||
709 | static void cache_lookup(struct closure *cl) | |
710 | { | |
220bb38c | 711 | struct search *s = container_of(cl, struct search, iop.cl); |
2c1953e2 | 712 | struct bio *bio = &s->bio.bio; |
a5ae4300 | 713 | int ret; |
2c1953e2 | 714 | |
a5ae4300 KO |
715 | bch_btree_op_init(&s->op, -1); |
716 | ||
717 | ret = bch_btree_map_keys(&s->op, s->iop.c, | |
718 | &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0), | |
719 | cache_lookup_fn, MAP_END_KEY); | |
2c1953e2 KO |
720 | if (ret == -EAGAIN) |
721 | continue_at(cl, cache_lookup, bcache_wq); | |
722 | ||
723 | closure_return(cl); | |
724 | } | |
725 | ||
726 | /* Common code for the make_request functions */ | |
727 | ||
728 | static void request_endio(struct bio *bio, int error) | |
729 | { | |
730 | struct closure *cl = bio->bi_private; | |
731 | ||
732 | if (error) { | |
733 | struct search *s = container_of(cl, struct search, cl); | |
220bb38c | 734 | s->iop.error = error; |
2c1953e2 KO |
735 | /* Only cache read errors are recoverable */ |
736 | s->recoverable = false; | |
737 | } | |
738 | ||
739 | bio_put(bio); | |
740 | closure_put(cl); | |
741 | } | |
742 | ||
cafe5635 KO |
743 | static void bio_complete(struct search *s) |
744 | { | |
745 | if (s->orig_bio) { | |
746 | int cpu, rw = bio_data_dir(s->orig_bio); | |
747 | unsigned long duration = jiffies - s->start_time; | |
748 | ||
749 | cpu = part_stat_lock(); | |
750 | part_round_stats(cpu, &s->d->disk->part0); | |
751 | part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration); | |
752 | part_stat_unlock(); | |
753 | ||
220bb38c KO |
754 | trace_bcache_request_end(s->d, s->orig_bio); |
755 | bio_endio(s->orig_bio, s->iop.error); | |
cafe5635 KO |
756 | s->orig_bio = NULL; |
757 | } | |
758 | } | |
759 | ||
a5ae4300 | 760 | static void do_bio_hook(struct search *s, struct bio *orig_bio) |
cafe5635 KO |
761 | { |
762 | struct bio *bio = &s->bio.bio; | |
cafe5635 | 763 | |
ed9c47be | 764 | bio_init(bio); |
a5ae4300 | 765 | __bio_clone_fast(bio, orig_bio); |
cafe5635 KO |
766 | bio->bi_end_io = request_endio; |
767 | bio->bi_private = &s->cl; | |
ed9c47be | 768 | |
cafe5635 KO |
769 | atomic_set(&bio->bi_cnt, 3); |
770 | } | |
771 | ||
772 | static void search_free(struct closure *cl) | |
773 | { | |
774 | struct search *s = container_of(cl, struct search, cl); | |
775 | bio_complete(s); | |
776 | ||
220bb38c KO |
777 | if (s->iop.bio) |
778 | bio_put(s->iop.bio); | |
cafe5635 | 779 | |
cafe5635 KO |
780 | closure_debug_destroy(cl); |
781 | mempool_free(s, s->d->c->search); | |
782 | } | |
783 | ||
a5ae4300 KO |
784 | static inline struct search *search_alloc(struct bio *bio, |
785 | struct bcache_device *d) | |
cafe5635 | 786 | { |
0b93207a | 787 | struct search *s; |
0b93207a KO |
788 | |
789 | s = mempool_alloc(d->c->search, GFP_NOIO); | |
cafe5635 | 790 | |
a5ae4300 KO |
791 | closure_init(&s->cl, NULL); |
792 | do_bio_hook(s, bio); | |
cafe5635 | 793 | |
cafe5635 | 794 | s->orig_bio = bio; |
a5ae4300 KO |
795 | s->cache_miss = NULL; |
796 | s->d = d; | |
cafe5635 | 797 | s->recoverable = 1; |
a5ae4300 KO |
798 | s->write = (bio->bi_rw & REQ_WRITE) != 0; |
799 | s->read_dirty_data = 0; | |
cafe5635 | 800 | s->start_time = jiffies; |
a5ae4300 KO |
801 | |
802 | s->iop.c = d->c; | |
803 | s->iop.bio = NULL; | |
804 | s->iop.inode = d->id; | |
805 | s->iop.write_point = hash_long((unsigned long) current, 16); | |
806 | s->iop.write_prio = 0; | |
807 | s->iop.error = 0; | |
808 | s->iop.flags = 0; | |
809 | s->iop.flush_journal = (bio->bi_rw & (REQ_FLUSH|REQ_FUA)) != 0; | |
cafe5635 | 810 | |
cafe5635 KO |
811 | return s; |
812 | } | |
813 | ||
cafe5635 KO |
814 | /* Cached devices */ |
815 | ||
816 | static void cached_dev_bio_complete(struct closure *cl) | |
817 | { | |
818 | struct search *s = container_of(cl, struct search, cl); | |
819 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); | |
820 | ||
821 | search_free(cl); | |
822 | cached_dev_put(dc); | |
823 | } | |
824 | ||
825 | /* Process reads */ | |
826 | ||
cdd972b1 | 827 | static void cached_dev_cache_miss_done(struct closure *cl) |
cafe5635 KO |
828 | { |
829 | struct search *s = container_of(cl, struct search, cl); | |
830 | ||
220bb38c KO |
831 | if (s->iop.replace_collision) |
832 | bch_mark_cache_miss_collision(s->iop.c, s->d); | |
cafe5635 | 833 | |
220bb38c | 834 | if (s->iop.bio) { |
cafe5635 KO |
835 | int i; |
836 | struct bio_vec *bv; | |
837 | ||
220bb38c | 838 | bio_for_each_segment_all(bv, s->iop.bio, i) |
cafe5635 KO |
839 | __free_page(bv->bv_page); |
840 | } | |
841 | ||
842 | cached_dev_bio_complete(cl); | |
843 | } | |
844 | ||
cdd972b1 | 845 | static void cached_dev_read_error(struct closure *cl) |
cafe5635 KO |
846 | { |
847 | struct search *s = container_of(cl, struct search, cl); | |
cdd972b1 | 848 | struct bio *bio = &s->bio.bio; |
cafe5635 KO |
849 | |
850 | if (s->recoverable) { | |
c37511b8 KO |
851 | /* Retry from the backing device: */ |
852 | trace_bcache_read_retry(s->orig_bio); | |
cafe5635 | 853 | |
220bb38c | 854 | s->iop.error = 0; |
a5ae4300 | 855 | do_bio_hook(s, s->orig_bio); |
cafe5635 KO |
856 | |
857 | /* XXX: invalidate cache */ | |
858 | ||
cdd972b1 | 859 | closure_bio_submit(bio, cl, s->d); |
cafe5635 KO |
860 | } |
861 | ||
cdd972b1 | 862 | continue_at(cl, cached_dev_cache_miss_done, NULL); |
cafe5635 KO |
863 | } |
864 | ||
cdd972b1 | 865 | static void cached_dev_read_done(struct closure *cl) |
cafe5635 KO |
866 | { |
867 | struct search *s = container_of(cl, struct search, cl); | |
868 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); | |
869 | ||
870 | /* | |
cdd972b1 KO |
871 | * We had a cache miss; cache_bio now contains data ready to be inserted |
872 | * into the cache. | |
cafe5635 KO |
873 | * |
874 | * First, we copy the data we just read from cache_bio's bounce buffers | |
875 | * to the buffers the original bio pointed to: | |
876 | */ | |
877 | ||
220bb38c KO |
878 | if (s->iop.bio) { |
879 | bio_reset(s->iop.bio); | |
4f024f37 | 880 | s->iop.bio->bi_iter.bi_sector = s->cache_miss->bi_iter.bi_sector; |
220bb38c | 881 | s->iop.bio->bi_bdev = s->cache_miss->bi_bdev; |
4f024f37 | 882 | s->iop.bio->bi_iter.bi_size = s->insert_bio_sectors << 9; |
220bb38c | 883 | bch_bio_map(s->iop.bio, NULL); |
cafe5635 | 884 | |
220bb38c | 885 | bio_copy_data(s->cache_miss, s->iop.bio); |
cafe5635 KO |
886 | |
887 | bio_put(s->cache_miss); | |
888 | s->cache_miss = NULL; | |
889 | } | |
890 | ||
ed9c47be | 891 | if (verify(dc, &s->bio.bio) && s->recoverable && !s->read_dirty_data) |
220bb38c | 892 | bch_data_verify(dc, s->orig_bio); |
cafe5635 KO |
893 | |
894 | bio_complete(s); | |
895 | ||
220bb38c KO |
896 | if (s->iop.bio && |
897 | !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) { | |
898 | BUG_ON(!s->iop.replace); | |
899 | closure_call(&s->iop.cl, bch_data_insert, NULL, cl); | |
cafe5635 KO |
900 | } |
901 | ||
cdd972b1 | 902 | continue_at(cl, cached_dev_cache_miss_done, NULL); |
cafe5635 KO |
903 | } |
904 | ||
cdd972b1 | 905 | static void cached_dev_read_done_bh(struct closure *cl) |
cafe5635 KO |
906 | { |
907 | struct search *s = container_of(cl, struct search, cl); | |
908 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); | |
909 | ||
220bb38c KO |
910 | bch_mark_cache_accounting(s->iop.c, s->d, |
911 | !s->cache_miss, s->iop.bypass); | |
912 | trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass); | |
cafe5635 | 913 | |
220bb38c | 914 | if (s->iop.error) |
cdd972b1 | 915 | continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq); |
220bb38c | 916 | else if (s->iop.bio || verify(dc, &s->bio.bio)) |
cdd972b1 | 917 | continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq); |
cafe5635 | 918 | else |
cdd972b1 | 919 | continue_at_nobarrier(cl, cached_dev_bio_complete, NULL); |
cafe5635 KO |
920 | } |
921 | ||
922 | static int cached_dev_cache_miss(struct btree *b, struct search *s, | |
923 | struct bio *bio, unsigned sectors) | |
924 | { | |
2c1953e2 | 925 | int ret = MAP_CONTINUE; |
e7c590eb | 926 | unsigned reada = 0; |
cafe5635 | 927 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); |
cdd972b1 | 928 | struct bio *miss, *cache_bio; |
cafe5635 | 929 | |
220bb38c | 930 | if (s->cache_miss || s->iop.bypass) { |
20d0189b | 931 | miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split); |
2c1953e2 | 932 | ret = miss == bio ? MAP_DONE : MAP_CONTINUE; |
e7c590eb KO |
933 | goto out_submit; |
934 | } | |
cafe5635 | 935 | |
e7c590eb KO |
936 | if (!(bio->bi_rw & REQ_RAHEAD) && |
937 | !(bio->bi_rw & REQ_META) && | |
220bb38c | 938 | s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA) |
e7c590eb KO |
939 | reada = min_t(sector_t, dc->readahead >> 9, |
940 | bdev_sectors(bio->bi_bdev) - bio_end_sector(bio)); | |
cafe5635 | 941 | |
220bb38c | 942 | s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada); |
cafe5635 | 943 | |
220bb38c | 944 | s->iop.replace_key = KEY(s->iop.inode, |
4f024f37 | 945 | bio->bi_iter.bi_sector + s->insert_bio_sectors, |
220bb38c | 946 | s->insert_bio_sectors); |
e7c590eb | 947 | |
220bb38c | 948 | ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key); |
e7c590eb KO |
949 | if (ret) |
950 | return ret; | |
951 | ||
220bb38c | 952 | s->iop.replace = true; |
1b207d80 | 953 | |
20d0189b | 954 | miss = bio_next_split(bio, sectors, GFP_NOIO, s->d->bio_split); |
2c1953e2 KO |
955 | |
956 | /* btree_search_recurse()'s btree iterator is no good anymore */ | |
957 | ret = miss == bio ? MAP_DONE : -EINTR; | |
cafe5635 | 958 | |
cdd972b1 | 959 | cache_bio = bio_alloc_bioset(GFP_NOWAIT, |
220bb38c | 960 | DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS), |
cafe5635 | 961 | dc->disk.bio_split); |
cdd972b1 | 962 | if (!cache_bio) |
cafe5635 KO |
963 | goto out_submit; |
964 | ||
4f024f37 KO |
965 | cache_bio->bi_iter.bi_sector = miss->bi_iter.bi_sector; |
966 | cache_bio->bi_bdev = miss->bi_bdev; | |
967 | cache_bio->bi_iter.bi_size = s->insert_bio_sectors << 9; | |
cafe5635 | 968 | |
cdd972b1 KO |
969 | cache_bio->bi_end_io = request_endio; |
970 | cache_bio->bi_private = &s->cl; | |
cafe5635 | 971 | |
cdd972b1 KO |
972 | bch_bio_map(cache_bio, NULL); |
973 | if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO)) | |
cafe5635 KO |
974 | goto out_put; |
975 | ||
220bb38c KO |
976 | if (reada) |
977 | bch_mark_cache_readahead(s->iop.c, s->d); | |
978 | ||
cdd972b1 | 979 | s->cache_miss = miss; |
220bb38c | 980 | s->iop.bio = cache_bio; |
cdd972b1 KO |
981 | bio_get(cache_bio); |
982 | closure_bio_submit(cache_bio, &s->cl, s->d); | |
cafe5635 KO |
983 | |
984 | return ret; | |
985 | out_put: | |
cdd972b1 | 986 | bio_put(cache_bio); |
cafe5635 | 987 | out_submit: |
e7c590eb KO |
988 | miss->bi_end_io = request_endio; |
989 | miss->bi_private = &s->cl; | |
cafe5635 KO |
990 | closure_bio_submit(miss, &s->cl, s->d); |
991 | return ret; | |
992 | } | |
993 | ||
cdd972b1 | 994 | static void cached_dev_read(struct cached_dev *dc, struct search *s) |
cafe5635 KO |
995 | { |
996 | struct closure *cl = &s->cl; | |
997 | ||
220bb38c | 998 | closure_call(&s->iop.cl, cache_lookup, NULL, cl); |
cdd972b1 | 999 | continue_at(cl, cached_dev_read_done_bh, NULL); |
cafe5635 KO |
1000 | } |
1001 | ||
1002 | /* Process writes */ | |
1003 | ||
1004 | static void cached_dev_write_complete(struct closure *cl) | |
1005 | { | |
1006 | struct search *s = container_of(cl, struct search, cl); | |
1007 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); | |
1008 | ||
1009 | up_read_non_owner(&dc->writeback_lock); | |
1010 | cached_dev_bio_complete(cl); | |
1011 | } | |
1012 | ||
cdd972b1 | 1013 | static void cached_dev_write(struct cached_dev *dc, struct search *s) |
cafe5635 KO |
1014 | { |
1015 | struct closure *cl = &s->cl; | |
1016 | struct bio *bio = &s->bio.bio; | |
4f024f37 | 1017 | struct bkey start = KEY(dc->disk.id, bio->bi_iter.bi_sector, 0); |
84f0db03 | 1018 | struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0); |
cafe5635 | 1019 | |
220bb38c | 1020 | bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end); |
cafe5635 | 1021 | |
cafe5635 | 1022 | down_read_non_owner(&dc->writeback_lock); |
cafe5635 | 1023 | if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) { |
84f0db03 KO |
1024 | /* |
1025 | * We overlap with some dirty data undergoing background | |
1026 | * writeback, force this write to writeback | |
1027 | */ | |
220bb38c KO |
1028 | s->iop.bypass = false; |
1029 | s->iop.writeback = true; | |
cafe5635 KO |
1030 | } |
1031 | ||
84f0db03 KO |
1032 | /* |
1033 | * Discards aren't _required_ to do anything, so skipping if | |
1034 | * check_overlapping returned true is ok | |
1035 | * | |
1036 | * But check_overlapping drops dirty keys for which io hasn't started, | |
1037 | * so we still want to call it. | |
1038 | */ | |
cafe5635 | 1039 | if (bio->bi_rw & REQ_DISCARD) |
220bb38c | 1040 | s->iop.bypass = true; |
cafe5635 | 1041 | |
72c27061 KO |
1042 | if (should_writeback(dc, s->orig_bio, |
1043 | cache_mode(dc, bio), | |
220bb38c KO |
1044 | s->iop.bypass)) { |
1045 | s->iop.bypass = false; | |
1046 | s->iop.writeback = true; | |
72c27061 KO |
1047 | } |
1048 | ||
220bb38c KO |
1049 | if (s->iop.bypass) { |
1050 | s->iop.bio = s->orig_bio; | |
1051 | bio_get(s->iop.bio); | |
cafe5635 | 1052 | |
84f0db03 KO |
1053 | if (!(bio->bi_rw & REQ_DISCARD) || |
1054 | blk_queue_discard(bdev_get_queue(dc->bdev))) | |
1055 | closure_bio_submit(bio, cl, s->d); | |
220bb38c | 1056 | } else if (s->iop.writeback) { |
279afbad | 1057 | bch_writeback_add(dc); |
220bb38c | 1058 | s->iop.bio = bio; |
e49c7c37 | 1059 | |
c0f04d88 | 1060 | if (bio->bi_rw & REQ_FLUSH) { |
e49c7c37 | 1061 | /* Also need to send a flush to the backing device */ |
d4eddd42 | 1062 | struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0, |
c0f04d88 | 1063 | dc->disk.bio_split); |
e49c7c37 | 1064 | |
c0f04d88 KO |
1065 | flush->bi_rw = WRITE_FLUSH; |
1066 | flush->bi_bdev = bio->bi_bdev; | |
1067 | flush->bi_end_io = request_endio; | |
1068 | flush->bi_private = cl; | |
1069 | ||
1070 | closure_bio_submit(flush, cl, s->d); | |
e49c7c37 | 1071 | } |
84f0db03 | 1072 | } else { |
59d276fe | 1073 | s->iop.bio = bio_clone_fast(bio, GFP_NOIO, dc->disk.bio_split); |
84f0db03 KO |
1074 | |
1075 | closure_bio_submit(bio, cl, s->d); | |
cafe5635 | 1076 | } |
84f0db03 | 1077 | |
220bb38c | 1078 | closure_call(&s->iop.cl, bch_data_insert, NULL, cl); |
cafe5635 | 1079 | continue_at(cl, cached_dev_write_complete, NULL); |
cafe5635 KO |
1080 | } |
1081 | ||
a34a8bfd | 1082 | static void cached_dev_nodata(struct closure *cl) |
cafe5635 | 1083 | { |
a34a8bfd | 1084 | struct search *s = container_of(cl, struct search, cl); |
cafe5635 KO |
1085 | struct bio *bio = &s->bio.bio; |
1086 | ||
220bb38c KO |
1087 | if (s->iop.flush_journal) |
1088 | bch_journal_meta(s->iop.c, cl); | |
cafe5635 | 1089 | |
84f0db03 | 1090 | /* If it's a flush, we send the flush to the backing device too */ |
cafe5635 KO |
1091 | closure_bio_submit(bio, cl, s->d); |
1092 | ||
1093 | continue_at(cl, cached_dev_bio_complete, NULL); | |
1094 | } | |
1095 | ||
1096 | /* Cached devices - read & write stuff */ | |
1097 | ||
cafe5635 KO |
1098 | static void cached_dev_make_request(struct request_queue *q, struct bio *bio) |
1099 | { | |
1100 | struct search *s; | |
1101 | struct bcache_device *d = bio->bi_bdev->bd_disk->private_data; | |
1102 | struct cached_dev *dc = container_of(d, struct cached_dev, disk); | |
1103 | int cpu, rw = bio_data_dir(bio); | |
1104 | ||
1105 | cpu = part_stat_lock(); | |
1106 | part_stat_inc(cpu, &d->disk->part0, ios[rw]); | |
1107 | part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio)); | |
1108 | part_stat_unlock(); | |
1109 | ||
1110 | bio->bi_bdev = dc->bdev; | |
4f024f37 | 1111 | bio->bi_iter.bi_sector += dc->sb.data_offset; |
cafe5635 KO |
1112 | |
1113 | if (cached_dev_get(dc)) { | |
1114 | s = search_alloc(bio, d); | |
220bb38c | 1115 | trace_bcache_request_start(s->d, bio); |
cafe5635 | 1116 | |
4f024f37 | 1117 | if (!bio->bi_iter.bi_size) { |
a34a8bfd KO |
1118 | /* |
1119 | * can't call bch_journal_meta from under | |
1120 | * generic_make_request | |
1121 | */ | |
1122 | continue_at_nobarrier(&s->cl, | |
1123 | cached_dev_nodata, | |
1124 | bcache_wq); | |
1125 | } else { | |
220bb38c | 1126 | s->iop.bypass = check_should_bypass(dc, bio); |
84f0db03 KO |
1127 | |
1128 | if (rw) | |
cdd972b1 | 1129 | cached_dev_write(dc, s); |
84f0db03 | 1130 | else |
cdd972b1 | 1131 | cached_dev_read(dc, s); |
84f0db03 | 1132 | } |
cafe5635 KO |
1133 | } else { |
1134 | if ((bio->bi_rw & REQ_DISCARD) && | |
1135 | !blk_queue_discard(bdev_get_queue(dc->bdev))) | |
1136 | bio_endio(bio, 0); | |
1137 | else | |
1138 | bch_generic_make_request(bio, &d->bio_split_hook); | |
1139 | } | |
1140 | } | |
1141 | ||
1142 | static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode, | |
1143 | unsigned int cmd, unsigned long arg) | |
1144 | { | |
1145 | struct cached_dev *dc = container_of(d, struct cached_dev, disk); | |
1146 | return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg); | |
1147 | } | |
1148 | ||
1149 | static int cached_dev_congested(void *data, int bits) | |
1150 | { | |
1151 | struct bcache_device *d = data; | |
1152 | struct cached_dev *dc = container_of(d, struct cached_dev, disk); | |
1153 | struct request_queue *q = bdev_get_queue(dc->bdev); | |
1154 | int ret = 0; | |
1155 | ||
1156 | if (bdi_congested(&q->backing_dev_info, bits)) | |
1157 | return 1; | |
1158 | ||
1159 | if (cached_dev_get(dc)) { | |
1160 | unsigned i; | |
1161 | struct cache *ca; | |
1162 | ||
1163 | for_each_cache(ca, d->c, i) { | |
1164 | q = bdev_get_queue(ca->bdev); | |
1165 | ret |= bdi_congested(&q->backing_dev_info, bits); | |
1166 | } | |
1167 | ||
1168 | cached_dev_put(dc); | |
1169 | } | |
1170 | ||
1171 | return ret; | |
1172 | } | |
1173 | ||
1174 | void bch_cached_dev_request_init(struct cached_dev *dc) | |
1175 | { | |
1176 | struct gendisk *g = dc->disk.disk; | |
1177 | ||
1178 | g->queue->make_request_fn = cached_dev_make_request; | |
1179 | g->queue->backing_dev_info.congested_fn = cached_dev_congested; | |
1180 | dc->disk.cache_miss = cached_dev_cache_miss; | |
1181 | dc->disk.ioctl = cached_dev_ioctl; | |
1182 | } | |
1183 | ||
1184 | /* Flash backed devices */ | |
1185 | ||
1186 | static int flash_dev_cache_miss(struct btree *b, struct search *s, | |
1187 | struct bio *bio, unsigned sectors) | |
1188 | { | |
7988613b KO |
1189 | struct bio_vec bv; |
1190 | struct bvec_iter iter; | |
8e51e414 | 1191 | |
cafe5635 KO |
1192 | /* Zero fill bio */ |
1193 | ||
7988613b KO |
1194 | bio_for_each_segment(bv, bio, iter) { |
1195 | unsigned j = min(bv.bv_len >> 9, sectors); | |
cafe5635 | 1196 | |
7988613b KO |
1197 | void *p = kmap(bv.bv_page); |
1198 | memset(p + bv.bv_offset, 0, j << 9); | |
1199 | kunmap(bv.bv_page); | |
cafe5635 | 1200 | |
8e51e414 | 1201 | sectors -= j; |
cafe5635 KO |
1202 | } |
1203 | ||
4f024f37 | 1204 | bio_advance(bio, min(sectors << 9, bio->bi_iter.bi_size)); |
8e51e414 | 1205 | |
4f024f37 | 1206 | if (!bio->bi_iter.bi_size) |
2c1953e2 | 1207 | return MAP_DONE; |
cafe5635 | 1208 | |
2c1953e2 | 1209 | return MAP_CONTINUE; |
cafe5635 KO |
1210 | } |
1211 | ||
a34a8bfd KO |
1212 | static void flash_dev_nodata(struct closure *cl) |
1213 | { | |
1214 | struct search *s = container_of(cl, struct search, cl); | |
1215 | ||
220bb38c KO |
1216 | if (s->iop.flush_journal) |
1217 | bch_journal_meta(s->iop.c, cl); | |
a34a8bfd KO |
1218 | |
1219 | continue_at(cl, search_free, NULL); | |
1220 | } | |
1221 | ||
cafe5635 KO |
1222 | static void flash_dev_make_request(struct request_queue *q, struct bio *bio) |
1223 | { | |
1224 | struct search *s; | |
1225 | struct closure *cl; | |
1226 | struct bcache_device *d = bio->bi_bdev->bd_disk->private_data; | |
1227 | int cpu, rw = bio_data_dir(bio); | |
1228 | ||
1229 | cpu = part_stat_lock(); | |
1230 | part_stat_inc(cpu, &d->disk->part0, ios[rw]); | |
1231 | part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio)); | |
1232 | part_stat_unlock(); | |
1233 | ||
1234 | s = search_alloc(bio, d); | |
1235 | cl = &s->cl; | |
1236 | bio = &s->bio.bio; | |
1237 | ||
220bb38c | 1238 | trace_bcache_request_start(s->d, bio); |
cafe5635 | 1239 | |
4f024f37 | 1240 | if (!bio->bi_iter.bi_size) { |
a34a8bfd KO |
1241 | /* |
1242 | * can't call bch_journal_meta from under | |
1243 | * generic_make_request | |
1244 | */ | |
1245 | continue_at_nobarrier(&s->cl, | |
1246 | flash_dev_nodata, | |
1247 | bcache_wq); | |
84f0db03 | 1248 | } else if (rw) { |
220bb38c | 1249 | bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, |
4f024f37 | 1250 | &KEY(d->id, bio->bi_iter.bi_sector, 0), |
8e51e414 | 1251 | &KEY(d->id, bio_end_sector(bio), 0)); |
cafe5635 | 1252 | |
220bb38c KO |
1253 | s->iop.bypass = (bio->bi_rw & REQ_DISCARD) != 0; |
1254 | s->iop.writeback = true; | |
1255 | s->iop.bio = bio; | |
cafe5635 | 1256 | |
220bb38c | 1257 | closure_call(&s->iop.cl, bch_data_insert, NULL, cl); |
cafe5635 | 1258 | } else { |
220bb38c | 1259 | closure_call(&s->iop.cl, cache_lookup, NULL, cl); |
cafe5635 KO |
1260 | } |
1261 | ||
1262 | continue_at(cl, search_free, NULL); | |
1263 | } | |
1264 | ||
1265 | static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode, | |
1266 | unsigned int cmd, unsigned long arg) | |
1267 | { | |
1268 | return -ENOTTY; | |
1269 | } | |
1270 | ||
1271 | static int flash_dev_congested(void *data, int bits) | |
1272 | { | |
1273 | struct bcache_device *d = data; | |
1274 | struct request_queue *q; | |
1275 | struct cache *ca; | |
1276 | unsigned i; | |
1277 | int ret = 0; | |
1278 | ||
1279 | for_each_cache(ca, d->c, i) { | |
1280 | q = bdev_get_queue(ca->bdev); | |
1281 | ret |= bdi_congested(&q->backing_dev_info, bits); | |
1282 | } | |
1283 | ||
1284 | return ret; | |
1285 | } | |
1286 | ||
1287 | void bch_flash_dev_request_init(struct bcache_device *d) | |
1288 | { | |
1289 | struct gendisk *g = d->disk; | |
1290 | ||
1291 | g->queue->make_request_fn = flash_dev_make_request; | |
1292 | g->queue->backing_dev_info.congested_fn = flash_dev_congested; | |
1293 | d->cache_miss = flash_dev_cache_miss; | |
1294 | d->ioctl = flash_dev_ioctl; | |
1295 | } | |
1296 | ||
1297 | void bch_request_exit(void) | |
1298 | { | |
1299 | #ifdef CONFIG_CGROUP_BCACHE | |
1300 | cgroup_unload_subsys(&bcache_subsys); | |
1301 | #endif | |
1302 | if (bch_search_cache) | |
1303 | kmem_cache_destroy(bch_search_cache); | |
1304 | } | |
1305 | ||
1306 | int __init bch_request_init(void) | |
1307 | { | |
1308 | bch_search_cache = KMEM_CACHE(search, 0); | |
1309 | if (!bch_search_cache) | |
1310 | return -ENOMEM; | |
1311 | ||
1312 | #ifdef CONFIG_CGROUP_BCACHE | |
1313 | cgroup_load_subsys(&bcache_subsys); | |
1314 | init_bch_cgroup(&bcache_default_cgroup); | |
1315 | ||
1316 | cgroup_add_cftypes(&bcache_subsys, bch_files); | |
1317 | #endif | |
1318 | return 0; | |
1319 | } |