]>
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 | { | |
201 | struct bio_vec *bv; | |
202 | uint64_t csum = 0; | |
203 | int i; | |
204 | ||
205 | bio_for_each_segment(bv, bio, i) { | |
206 | void *d = kmap(bv->bv_page) + bv->bv_offset; | |
169ef1cf | 207 | csum = bch_crc64_update(csum, d, bv->bv_len); |
cafe5635 KO |
208 | kunmap(bv->bv_page); |
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 | { |
b54d6934 | 218 | struct search *s = container_of(cl, struct search, btree); |
c18536a7 | 219 | atomic_t *journal_ref = NULL; |
1b207d80 | 220 | struct bkey *replace_key = s->replace ? &s->replace_key : NULL; |
cafe5635 | 221 | |
a34a8bfd KO |
222 | /* |
223 | * If we're looping, might already be waiting on | |
224 | * another journal write - can't wait on more than one journal write at | |
225 | * a time | |
226 | * | |
227 | * XXX: this looks wrong | |
228 | */ | |
229 | #if 0 | |
230 | while (atomic_read(&s->cl.remaining) & CLOSURE_WAITING) | |
231 | closure_sync(&s->cl); | |
232 | #endif | |
cafe5635 | 233 | |
a34a8bfd | 234 | if (s->write) |
c18536a7 KO |
235 | journal_ref = bch_journal(s->c, &s->insert_keys, |
236 | s->flush_journal | |
a34a8bfd | 237 | ? &s->cl : NULL); |
cafe5635 | 238 | |
1b207d80 KO |
239 | if (bch_btree_insert(&s->op, s->c, &s->insert_keys, |
240 | journal_ref, replace_key)) { | |
a34a8bfd | 241 | s->error = -ENOMEM; |
c18536a7 | 242 | s->insert_data_done = true; |
a34a8bfd | 243 | } |
cafe5635 | 244 | |
c18536a7 KO |
245 | if (journal_ref) |
246 | atomic_dec_bug(journal_ref); | |
cafe5635 | 247 | |
c18536a7 | 248 | if (!s->insert_data_done) |
a34a8bfd | 249 | continue_at(cl, bch_data_insert_start, bcache_wq); |
cafe5635 | 250 | |
0b93207a | 251 | bch_keylist_free(&s->insert_keys); |
a34a8bfd | 252 | closure_return(cl); |
cafe5635 KO |
253 | } |
254 | ||
255 | struct open_bucket { | |
256 | struct list_head list; | |
257 | struct task_struct *last; | |
258 | unsigned sectors_free; | |
259 | BKEY_PADDED(key); | |
260 | }; | |
261 | ||
262 | void bch_open_buckets_free(struct cache_set *c) | |
263 | { | |
264 | struct open_bucket *b; | |
265 | ||
266 | while (!list_empty(&c->data_buckets)) { | |
267 | b = list_first_entry(&c->data_buckets, | |
268 | struct open_bucket, list); | |
269 | list_del(&b->list); | |
270 | kfree(b); | |
271 | } | |
272 | } | |
273 | ||
274 | int bch_open_buckets_alloc(struct cache_set *c) | |
275 | { | |
276 | int i; | |
277 | ||
278 | spin_lock_init(&c->data_bucket_lock); | |
279 | ||
280 | for (i = 0; i < 6; i++) { | |
281 | struct open_bucket *b = kzalloc(sizeof(*b), GFP_KERNEL); | |
282 | if (!b) | |
283 | return -ENOMEM; | |
284 | ||
285 | list_add(&b->list, &c->data_buckets); | |
286 | } | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
291 | /* | |
292 | * We keep multiple buckets open for writes, and try to segregate different | |
293 | * write streams for better cache utilization: first we look for a bucket where | |
294 | * the last write to it was sequential with the current write, and failing that | |
295 | * we look for a bucket that was last used by the same task. | |
296 | * | |
297 | * The ideas is if you've got multiple tasks pulling data into the cache at the | |
298 | * same time, you'll get better cache utilization if you try to segregate their | |
299 | * data and preserve locality. | |
300 | * | |
301 | * For example, say you've starting Firefox at the same time you're copying a | |
302 | * bunch of files. Firefox will likely end up being fairly hot and stay in the | |
303 | * cache awhile, but the data you copied might not be; if you wrote all that | |
304 | * data to the same buckets it'd get invalidated at the same time. | |
305 | * | |
306 | * Both of those tasks will be doing fairly random IO so we can't rely on | |
307 | * detecting sequential IO to segregate their data, but going off of the task | |
308 | * should be a sane heuristic. | |
309 | */ | |
310 | static struct open_bucket *pick_data_bucket(struct cache_set *c, | |
311 | const struct bkey *search, | |
312 | struct task_struct *task, | |
313 | struct bkey *alloc) | |
314 | { | |
315 | struct open_bucket *ret, *ret_task = NULL; | |
316 | ||
317 | list_for_each_entry_reverse(ret, &c->data_buckets, list) | |
318 | if (!bkey_cmp(&ret->key, search)) | |
319 | goto found; | |
320 | else if (ret->last == task) | |
321 | ret_task = ret; | |
322 | ||
323 | ret = ret_task ?: list_first_entry(&c->data_buckets, | |
324 | struct open_bucket, list); | |
325 | found: | |
326 | if (!ret->sectors_free && KEY_PTRS(alloc)) { | |
327 | ret->sectors_free = c->sb.bucket_size; | |
328 | bkey_copy(&ret->key, alloc); | |
329 | bkey_init(alloc); | |
330 | } | |
331 | ||
332 | if (!ret->sectors_free) | |
333 | ret = NULL; | |
334 | ||
335 | return ret; | |
336 | } | |
337 | ||
338 | /* | |
339 | * Allocates some space in the cache to write to, and k to point to the newly | |
340 | * allocated space, and updates KEY_SIZE(k) and KEY_OFFSET(k) (to point to the | |
341 | * end of the newly allocated space). | |
342 | * | |
343 | * May allocate fewer sectors than @sectors, KEY_SIZE(k) indicates how many | |
344 | * sectors were actually allocated. | |
345 | * | |
346 | * If s->writeback is true, will not fail. | |
347 | */ | |
348 | static bool bch_alloc_sectors(struct bkey *k, unsigned sectors, | |
349 | struct search *s) | |
350 | { | |
c18536a7 | 351 | struct cache_set *c = s->c; |
cafe5635 KO |
352 | struct open_bucket *b; |
353 | BKEY_PADDED(key) alloc; | |
cafe5635 KO |
354 | unsigned i; |
355 | ||
cafe5635 KO |
356 | /* |
357 | * We might have to allocate a new bucket, which we can't do with a | |
358 | * spinlock held. So if we have to allocate, we drop the lock, allocate | |
359 | * and then retry. KEY_PTRS() indicates whether alloc points to | |
360 | * allocated bucket(s). | |
361 | */ | |
362 | ||
363 | bkey_init(&alloc.key); | |
364 | spin_lock(&c->data_bucket_lock); | |
365 | ||
366 | while (!(b = pick_data_bucket(c, k, s->task, &alloc.key))) { | |
c18536a7 | 367 | unsigned watermark = s->write_prio |
cafe5635 KO |
368 | ? WATERMARK_MOVINGGC |
369 | : WATERMARK_NONE; | |
370 | ||
371 | spin_unlock(&c->data_bucket_lock); | |
372 | ||
35fcd848 KO |
373 | if (bch_bucket_alloc_set(c, watermark, &alloc.key, |
374 | 1, s->writeback)) | |
cafe5635 KO |
375 | return false; |
376 | ||
377 | spin_lock(&c->data_bucket_lock); | |
378 | } | |
379 | ||
380 | /* | |
381 | * If we had to allocate, we might race and not need to allocate the | |
382 | * second time we call find_data_bucket(). If we allocated a bucket but | |
383 | * didn't use it, drop the refcount bch_bucket_alloc_set() took: | |
384 | */ | |
385 | if (KEY_PTRS(&alloc.key)) | |
386 | __bkey_put(c, &alloc.key); | |
387 | ||
388 | for (i = 0; i < KEY_PTRS(&b->key); i++) | |
389 | EBUG_ON(ptr_stale(c, &b->key, i)); | |
390 | ||
391 | /* Set up the pointer to the space we're allocating: */ | |
392 | ||
393 | for (i = 0; i < KEY_PTRS(&b->key); i++) | |
394 | k->ptr[i] = b->key.ptr[i]; | |
395 | ||
396 | sectors = min(sectors, b->sectors_free); | |
397 | ||
398 | SET_KEY_OFFSET(k, KEY_OFFSET(k) + sectors); | |
399 | SET_KEY_SIZE(k, sectors); | |
400 | SET_KEY_PTRS(k, KEY_PTRS(&b->key)); | |
401 | ||
402 | /* | |
403 | * Move b to the end of the lru, and keep track of what this bucket was | |
404 | * last used for: | |
405 | */ | |
406 | list_move_tail(&b->list, &c->data_buckets); | |
407 | bkey_copy_key(&b->key, k); | |
408 | b->last = s->task; | |
409 | ||
410 | b->sectors_free -= sectors; | |
411 | ||
412 | for (i = 0; i < KEY_PTRS(&b->key); i++) { | |
413 | SET_PTR_OFFSET(&b->key, i, PTR_OFFSET(&b->key, i) + sectors); | |
414 | ||
415 | atomic_long_add(sectors, | |
416 | &PTR_CACHE(c, &b->key, i)->sectors_written); | |
417 | } | |
418 | ||
419 | if (b->sectors_free < c->sb.block_size) | |
420 | b->sectors_free = 0; | |
421 | ||
422 | /* | |
423 | * k takes refcounts on the buckets it points to until it's inserted | |
424 | * into the btree, but if we're done with this bucket we just transfer | |
425 | * get_data_bucket()'s refcount. | |
426 | */ | |
427 | if (b->sectors_free) | |
428 | for (i = 0; i < KEY_PTRS(&b->key); i++) | |
429 | atomic_inc(&PTR_BUCKET(c, &b->key, i)->pin); | |
430 | ||
431 | spin_unlock(&c->data_bucket_lock); | |
432 | return true; | |
433 | } | |
434 | ||
a34a8bfd KO |
435 | static void bch_data_invalidate(struct closure *cl) |
436 | { | |
b54d6934 | 437 | struct search *s = container_of(cl, struct search, btree); |
c18536a7 | 438 | struct bio *bio = s->cache_bio; |
a34a8bfd KO |
439 | |
440 | pr_debug("invalidating %i sectors from %llu", | |
441 | bio_sectors(bio), (uint64_t) bio->bi_sector); | |
442 | ||
443 | while (bio_sectors(bio)) { | |
444 | unsigned len = min(bio_sectors(bio), 1U << 14); | |
445 | ||
c18536a7 | 446 | if (bch_keylist_realloc(&s->insert_keys, 0, s->c)) |
a34a8bfd KO |
447 | goto out; |
448 | ||
449 | bio->bi_sector += len; | |
450 | bio->bi_size -= len << 9; | |
451 | ||
0b93207a | 452 | bch_keylist_add(&s->insert_keys, |
c18536a7 | 453 | &KEY(s->inode, bio->bi_sector, len)); |
a34a8bfd KO |
454 | } |
455 | ||
c18536a7 | 456 | s->insert_data_done = true; |
a34a8bfd KO |
457 | bio_put(bio); |
458 | out: | |
459 | continue_at(cl, bch_data_insert_keys, bcache_wq); | |
460 | } | |
461 | ||
462 | static void bch_data_insert_error(struct closure *cl) | |
cafe5635 | 463 | { |
b54d6934 | 464 | struct search *s = container_of(cl, struct search, btree); |
cafe5635 KO |
465 | |
466 | /* | |
467 | * Our data write just errored, which means we've got a bunch of keys to | |
468 | * insert that point to data that wasn't succesfully written. | |
469 | * | |
470 | * We don't have to insert those keys but we still have to invalidate | |
471 | * that region of the cache - so, if we just strip off all the pointers | |
472 | * from the keys we'll accomplish just that. | |
473 | */ | |
474 | ||
0b93207a | 475 | struct bkey *src = s->insert_keys.keys, *dst = s->insert_keys.keys; |
cafe5635 | 476 | |
0b93207a | 477 | while (src != s->insert_keys.top) { |
cafe5635 KO |
478 | struct bkey *n = bkey_next(src); |
479 | ||
480 | SET_KEY_PTRS(src, 0); | |
c2f95ae2 | 481 | memmove(dst, src, bkey_bytes(src)); |
cafe5635 KO |
482 | |
483 | dst = bkey_next(dst); | |
484 | src = n; | |
485 | } | |
486 | ||
0b93207a | 487 | s->insert_keys.top = dst; |
cafe5635 | 488 | |
a34a8bfd | 489 | bch_data_insert_keys(cl); |
cafe5635 KO |
490 | } |
491 | ||
a34a8bfd | 492 | static void bch_data_insert_endio(struct bio *bio, int error) |
cafe5635 KO |
493 | { |
494 | struct closure *cl = bio->bi_private; | |
b54d6934 | 495 | struct search *s = container_of(cl, struct search, btree); |
cafe5635 KO |
496 | |
497 | if (error) { | |
498 | /* TODO: We could try to recover from this. */ | |
499 | if (s->writeback) | |
500 | s->error = error; | |
501 | else if (s->write) | |
a34a8bfd | 502 | set_closure_fn(cl, bch_data_insert_error, bcache_wq); |
cafe5635 KO |
503 | else |
504 | set_closure_fn(cl, NULL, NULL); | |
505 | } | |
506 | ||
c18536a7 | 507 | bch_bbio_endio(s->c, bio, error, "writing data to cache"); |
cafe5635 KO |
508 | } |
509 | ||
a34a8bfd | 510 | static void bch_data_insert_start(struct closure *cl) |
cafe5635 | 511 | { |
b54d6934 | 512 | struct search *s = container_of(cl, struct search, btree); |
c18536a7 | 513 | struct bio *bio = s->cache_bio, *n; |
cafe5635 | 514 | |
c18536a7 | 515 | if (s->bypass) |
a34a8bfd | 516 | return bch_data_invalidate(cl); |
cafe5635 | 517 | |
c18536a7 KO |
518 | if (atomic_sub_return(bio_sectors(bio), &s->c->sectors_to_gc) < 0) { |
519 | set_gc_sectors(s->c); | |
520 | wake_up_gc(s->c); | |
cafe5635 KO |
521 | } |
522 | ||
54d12f2b KO |
523 | /* |
524 | * Journal writes are marked REQ_FLUSH; if the original write was a | |
525 | * flush, it'll wait on the journal write. | |
526 | */ | |
527 | bio->bi_rw &= ~(REQ_FLUSH|REQ_FUA); | |
528 | ||
cafe5635 KO |
529 | do { |
530 | unsigned i; | |
531 | struct bkey *k; | |
532 | struct bio_set *split = s->d | |
c18536a7 | 533 | ? s->d->bio_split : s->c->bio_split; |
cafe5635 KO |
534 | |
535 | /* 1 for the device pointer and 1 for the chksum */ | |
0b93207a | 536 | if (bch_keylist_realloc(&s->insert_keys, |
c18536a7 KO |
537 | 1 + (s->csum ? 1 : 0), |
538 | s->c)) | |
a34a8bfd | 539 | continue_at(cl, bch_data_insert_keys, bcache_wq); |
cafe5635 | 540 | |
0b93207a | 541 | k = s->insert_keys.top; |
cafe5635 | 542 | bkey_init(k); |
c18536a7 | 543 | SET_KEY_INODE(k, s->inode); |
cafe5635 KO |
544 | SET_KEY_OFFSET(k, bio->bi_sector); |
545 | ||
546 | if (!bch_alloc_sectors(k, bio_sectors(bio), s)) | |
547 | goto err; | |
548 | ||
549 | n = bch_bio_split(bio, KEY_SIZE(k), GFP_NOIO, split); | |
cafe5635 | 550 | |
a34a8bfd | 551 | n->bi_end_io = bch_data_insert_endio; |
cafe5635 KO |
552 | n->bi_private = cl; |
553 | ||
554 | if (s->writeback) { | |
555 | SET_KEY_DIRTY(k, true); | |
556 | ||
557 | for (i = 0; i < KEY_PTRS(k); i++) | |
c18536a7 | 558 | SET_GC_MARK(PTR_BUCKET(s->c, k, i), |
cafe5635 KO |
559 | GC_MARK_DIRTY); |
560 | } | |
561 | ||
c18536a7 | 562 | SET_KEY_CSUM(k, s->csum); |
cafe5635 KO |
563 | if (KEY_CSUM(k)) |
564 | bio_csum(n, k); | |
565 | ||
c37511b8 | 566 | trace_bcache_cache_insert(k); |
0b93207a | 567 | bch_keylist_push(&s->insert_keys); |
cafe5635 | 568 | |
cafe5635 | 569 | n->bi_rw |= REQ_WRITE; |
c18536a7 | 570 | bch_submit_bbio(n, s->c, k, 0); |
cafe5635 KO |
571 | } while (n != bio); |
572 | ||
c18536a7 | 573 | s->insert_data_done = true; |
a34a8bfd | 574 | continue_at(cl, bch_data_insert_keys, bcache_wq); |
cafe5635 KO |
575 | err: |
576 | /* bch_alloc_sectors() blocks if s->writeback = true */ | |
577 | BUG_ON(s->writeback); | |
578 | ||
579 | /* | |
580 | * But if it's not a writeback write we'd rather just bail out if | |
581 | * there aren't any buckets ready to write to - it might take awhile and | |
582 | * we might be starving btree writes for gc or something. | |
583 | */ | |
584 | ||
585 | if (s->write) { | |
586 | /* | |
587 | * Writethrough write: We can't complete the write until we've | |
588 | * updated the index. But we don't want to delay the write while | |
589 | * we wait for buckets to be freed up, so just invalidate the | |
590 | * rest of the write. | |
591 | */ | |
c18536a7 | 592 | s->bypass = true; |
a34a8bfd | 593 | return bch_data_invalidate(cl); |
cafe5635 KO |
594 | } else { |
595 | /* | |
596 | * From a cache miss, we can just insert the keys for the data | |
597 | * we have written or bail out if we didn't do anything. | |
598 | */ | |
c18536a7 | 599 | s->insert_data_done = true; |
cafe5635 KO |
600 | bio_put(bio); |
601 | ||
0b93207a | 602 | if (!bch_keylist_empty(&s->insert_keys)) |
a34a8bfd | 603 | continue_at(cl, bch_data_insert_keys, bcache_wq); |
cafe5635 KO |
604 | else |
605 | closure_return(cl); | |
606 | } | |
607 | } | |
608 | ||
609 | /** | |
a34a8bfd | 610 | * bch_data_insert - stick some data in the cache |
cafe5635 KO |
611 | * |
612 | * This is the starting point for any data to end up in a cache device; it could | |
613 | * be from a normal write, or a writeback write, or a write to a flash only | |
614 | * volume - it's also used by the moving garbage collector to compact data in | |
615 | * mostly empty buckets. | |
616 | * | |
617 | * It first writes the data to the cache, creating a list of keys to be inserted | |
618 | * (if the data had to be fragmented there will be multiple keys); after the | |
619 | * data is written it calls bch_journal, and after the keys have been added to | |
620 | * the next journal write they're inserted into the btree. | |
621 | * | |
c18536a7 | 622 | * It inserts the data in s->cache_bio; bi_sector is used for the key offset, |
cafe5635 KO |
623 | * and op->inode is used for the key inode. |
624 | * | |
c18536a7 KO |
625 | * If s->bypass is true, instead of inserting the data it invalidates the |
626 | * region of the cache represented by s->cache_bio and op->inode. | |
cafe5635 | 627 | */ |
a34a8bfd | 628 | void bch_data_insert(struct closure *cl) |
cafe5635 | 629 | { |
b54d6934 | 630 | struct search *s = container_of(cl, struct search, btree); |
cafe5635 | 631 | |
0b93207a | 632 | bch_keylist_init(&s->insert_keys); |
c18536a7 | 633 | bio_get(s->cache_bio); |
a34a8bfd | 634 | bch_data_insert_start(cl); |
cafe5635 KO |
635 | } |
636 | ||
2c1953e2 | 637 | /* Cache lookup */ |
cafe5635 | 638 | |
2c1953e2 | 639 | static void bch_cache_read_endio(struct bio *bio, int error) |
cafe5635 KO |
640 | { |
641 | struct bbio *b = container_of(bio, struct bbio, bio); | |
642 | struct closure *cl = bio->bi_private; | |
643 | struct search *s = container_of(cl, struct search, cl); | |
644 | ||
645 | /* | |
646 | * If the bucket was reused while our bio was in flight, we might have | |
647 | * read the wrong data. Set s->error but not error so it doesn't get | |
648 | * counted against the cache device, but we'll still reread the data | |
649 | * from the backing device. | |
650 | */ | |
651 | ||
652 | if (error) | |
653 | s->error = error; | |
c18536a7 KO |
654 | else if (ptr_stale(s->c, &b->key, 0)) { |
655 | atomic_long_inc(&s->c->cache_read_races); | |
cafe5635 KO |
656 | s->error = -EINTR; |
657 | } | |
658 | ||
c18536a7 | 659 | bch_bbio_endio(s->c, bio, error, "reading from cache"); |
cafe5635 KO |
660 | } |
661 | ||
2c1953e2 KO |
662 | /* |
663 | * Read from a single key, handling the initial cache miss if the key starts in | |
664 | * the middle of the bio | |
665 | */ | |
cc231966 | 666 | static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k) |
2c1953e2 KO |
667 | { |
668 | struct search *s = container_of(op, struct search, op); | |
cc231966 KO |
669 | struct bio *n, *bio = &s->bio.bio; |
670 | struct bkey *bio_key; | |
2c1953e2 | 671 | unsigned ptr; |
2c1953e2 | 672 | |
c18536a7 | 673 | if (bkey_cmp(k, &KEY(s->inode, bio->bi_sector, 0)) <= 0) |
cc231966 KO |
674 | return MAP_CONTINUE; |
675 | ||
c18536a7 | 676 | if (KEY_INODE(k) != s->inode || |
cc231966 KO |
677 | KEY_START(k) > bio->bi_sector) { |
678 | unsigned bio_sectors = bio_sectors(bio); | |
c18536a7 | 679 | unsigned sectors = KEY_INODE(k) == s->inode |
cc231966 KO |
680 | ? min_t(uint64_t, INT_MAX, |
681 | KEY_START(k) - bio->bi_sector) | |
682 | : INT_MAX; | |
683 | ||
684 | int ret = s->d->cache_miss(b, s, bio, sectors); | |
685 | if (ret != MAP_CONTINUE) | |
686 | return ret; | |
687 | ||
688 | /* if this was a complete miss we shouldn't get here */ | |
689 | BUG_ON(bio_sectors <= sectors); | |
690 | } | |
691 | ||
692 | if (!KEY_SIZE(k)) | |
693 | return MAP_CONTINUE; | |
2c1953e2 KO |
694 | |
695 | /* XXX: figure out best pointer - for multiple cache devices */ | |
696 | ptr = 0; | |
697 | ||
698 | PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO; | |
699 | ||
cc231966 KO |
700 | n = bch_bio_split(bio, min_t(uint64_t, INT_MAX, |
701 | KEY_OFFSET(k) - bio->bi_sector), | |
702 | GFP_NOIO, s->d->bio_split); | |
2c1953e2 | 703 | |
cc231966 KO |
704 | bio_key = &container_of(n, struct bbio, bio)->key; |
705 | bch_bkey_copy_single_ptr(bio_key, k, ptr); | |
2c1953e2 | 706 | |
c18536a7 KO |
707 | bch_cut_front(&KEY(s->inode, n->bi_sector, 0), bio_key); |
708 | bch_cut_back(&KEY(s->inode, bio_end_sector(n), 0), bio_key); | |
2c1953e2 | 709 | |
cc231966 KO |
710 | n->bi_end_io = bch_cache_read_endio; |
711 | n->bi_private = &s->cl; | |
2c1953e2 | 712 | |
cc231966 KO |
713 | /* |
714 | * The bucket we're reading from might be reused while our bio | |
715 | * is in flight, and we could then end up reading the wrong | |
716 | * data. | |
717 | * | |
718 | * We guard against this by checking (in cache_read_endio()) if | |
719 | * the pointer is stale again; if so, we treat it as an error | |
720 | * and reread from the backing device (but we don't pass that | |
721 | * error up anywhere). | |
722 | */ | |
2c1953e2 | 723 | |
cc231966 KO |
724 | __bch_submit_bbio(n, b->c); |
725 | return n == bio ? MAP_DONE : MAP_CONTINUE; | |
2c1953e2 KO |
726 | } |
727 | ||
728 | static void cache_lookup(struct closure *cl) | |
729 | { | |
b54d6934 | 730 | struct search *s = container_of(cl, struct search, btree); |
2c1953e2 KO |
731 | struct bio *bio = &s->bio.bio; |
732 | ||
b54d6934 | 733 | int ret = bch_btree_map_keys(&s->op, s->c, |
c18536a7 | 734 | &KEY(s->inode, bio->bi_sector, 0), |
cc231966 | 735 | cache_lookup_fn, MAP_END_KEY); |
2c1953e2 KO |
736 | if (ret == -EAGAIN) |
737 | continue_at(cl, cache_lookup, bcache_wq); | |
738 | ||
739 | closure_return(cl); | |
740 | } | |
741 | ||
742 | /* Common code for the make_request functions */ | |
743 | ||
744 | static void request_endio(struct bio *bio, int error) | |
745 | { | |
746 | struct closure *cl = bio->bi_private; | |
747 | ||
748 | if (error) { | |
749 | struct search *s = container_of(cl, struct search, cl); | |
750 | s->error = error; | |
751 | /* Only cache read errors are recoverable */ | |
752 | s->recoverable = false; | |
753 | } | |
754 | ||
755 | bio_put(bio); | |
756 | closure_put(cl); | |
757 | } | |
758 | ||
cafe5635 KO |
759 | static void bio_complete(struct search *s) |
760 | { | |
761 | if (s->orig_bio) { | |
762 | int cpu, rw = bio_data_dir(s->orig_bio); | |
763 | unsigned long duration = jiffies - s->start_time; | |
764 | ||
765 | cpu = part_stat_lock(); | |
766 | part_round_stats(cpu, &s->d->disk->part0); | |
767 | part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration); | |
768 | part_stat_unlock(); | |
769 | ||
770 | trace_bcache_request_end(s, s->orig_bio); | |
771 | bio_endio(s->orig_bio, s->error); | |
772 | s->orig_bio = NULL; | |
773 | } | |
774 | } | |
775 | ||
776 | static void do_bio_hook(struct search *s) | |
777 | { | |
778 | struct bio *bio = &s->bio.bio; | |
779 | memcpy(bio, s->orig_bio, sizeof(struct bio)); | |
780 | ||
781 | bio->bi_end_io = request_endio; | |
782 | bio->bi_private = &s->cl; | |
783 | atomic_set(&bio->bi_cnt, 3); | |
784 | } | |
785 | ||
786 | static void search_free(struct closure *cl) | |
787 | { | |
788 | struct search *s = container_of(cl, struct search, cl); | |
789 | bio_complete(s); | |
790 | ||
c18536a7 KO |
791 | if (s->cache_bio) |
792 | bio_put(s->cache_bio); | |
cafe5635 KO |
793 | |
794 | if (s->unaligned_bvec) | |
795 | mempool_free(s->bio.bio.bi_io_vec, s->d->unaligned_bvec); | |
796 | ||
797 | closure_debug_destroy(cl); | |
798 | mempool_free(s, s->d->c->search); | |
799 | } | |
800 | ||
801 | static struct search *search_alloc(struct bio *bio, struct bcache_device *d) | |
802 | { | |
0b93207a | 803 | struct search *s; |
cafe5635 | 804 | struct bio_vec *bv; |
0b93207a KO |
805 | |
806 | s = mempool_alloc(d->c->search, GFP_NOIO); | |
807 | memset(s, 0, offsetof(struct search, insert_keys)); | |
cafe5635 KO |
808 | |
809 | __closure_init(&s->cl, NULL); | |
810 | ||
c18536a7 KO |
811 | s->inode = d->id; |
812 | s->c = d->c; | |
cafe5635 KO |
813 | s->d = d; |
814 | s->op.lock = -1; | |
815 | s->task = current; | |
816 | s->orig_bio = bio; | |
817 | s->write = (bio->bi_rw & REQ_WRITE) != 0; | |
c18536a7 | 818 | s->flush_journal = (bio->bi_rw & (REQ_FLUSH|REQ_FUA)) != 0; |
cafe5635 KO |
819 | s->recoverable = 1; |
820 | s->start_time = jiffies; | |
821 | do_bio_hook(s); | |
822 | ||
823 | if (bio->bi_size != bio_segments(bio) * PAGE_SIZE) { | |
824 | bv = mempool_alloc(d->unaligned_bvec, GFP_NOIO); | |
825 | memcpy(bv, bio_iovec(bio), | |
826 | sizeof(struct bio_vec) * bio_segments(bio)); | |
827 | ||
828 | s->bio.bio.bi_io_vec = bv; | |
829 | s->unaligned_bvec = 1; | |
830 | } | |
831 | ||
832 | return s; | |
833 | } | |
834 | ||
cafe5635 KO |
835 | /* Cached devices */ |
836 | ||
837 | static void cached_dev_bio_complete(struct closure *cl) | |
838 | { | |
839 | struct search *s = container_of(cl, struct search, cl); | |
840 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); | |
841 | ||
842 | search_free(cl); | |
843 | cached_dev_put(dc); | |
844 | } | |
845 | ||
84f0db03 KO |
846 | unsigned bch_get_congested(struct cache_set *c) |
847 | { | |
848 | int i; | |
849 | long rand; | |
850 | ||
851 | if (!c->congested_read_threshold_us && | |
852 | !c->congested_write_threshold_us) | |
853 | return 0; | |
854 | ||
855 | i = (local_clock_us() - c->congested_last_us) / 1024; | |
856 | if (i < 0) | |
857 | return 0; | |
858 | ||
859 | i += atomic_read(&c->congested); | |
860 | if (i >= 0) | |
861 | return 0; | |
862 | ||
863 | i += CONGESTED_MAX; | |
864 | ||
865 | if (i > 0) | |
866 | i = fract_exp_two(i, 6); | |
867 | ||
868 | rand = get_random_int(); | |
869 | i -= bitmap_weight(&rand, BITS_PER_LONG); | |
870 | ||
871 | return i > 0 ? i : 1; | |
872 | } | |
873 | ||
874 | static void add_sequential(struct task_struct *t) | |
875 | { | |
876 | ewma_add(t->sequential_io_avg, | |
877 | t->sequential_io, 8, 0); | |
878 | ||
879 | t->sequential_io = 0; | |
880 | } | |
881 | ||
882 | static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k) | |
883 | { | |
884 | return &dc->io_hash[hash_64(k, RECENT_IO_BITS)]; | |
885 | } | |
886 | ||
887 | static bool check_should_bypass(struct cached_dev *dc, struct search *s) | |
888 | { | |
c18536a7 | 889 | struct cache_set *c = s->c; |
84f0db03 KO |
890 | struct bio *bio = &s->bio.bio; |
891 | unsigned mode = cache_mode(dc, bio); | |
892 | unsigned sectors, congested = bch_get_congested(c); | |
893 | ||
894 | if (atomic_read(&dc->disk.detaching) || | |
895 | c->gc_stats.in_use > CUTOFF_CACHE_ADD || | |
896 | (bio->bi_rw & REQ_DISCARD)) | |
897 | goto skip; | |
898 | ||
899 | if (mode == CACHE_MODE_NONE || | |
900 | (mode == CACHE_MODE_WRITEAROUND && | |
901 | (bio->bi_rw & REQ_WRITE))) | |
902 | goto skip; | |
903 | ||
904 | if (bio->bi_sector & (c->sb.block_size - 1) || | |
905 | bio_sectors(bio) & (c->sb.block_size - 1)) { | |
906 | pr_debug("skipping unaligned io"); | |
907 | goto skip; | |
908 | } | |
909 | ||
910 | if (!congested && !dc->sequential_cutoff) | |
911 | goto rescale; | |
912 | ||
913 | if (!congested && | |
914 | mode == CACHE_MODE_WRITEBACK && | |
915 | (bio->bi_rw & REQ_WRITE) && | |
916 | (bio->bi_rw & REQ_SYNC)) | |
917 | goto rescale; | |
918 | ||
919 | if (dc->sequential_merge) { | |
920 | struct io *i; | |
921 | ||
922 | spin_lock(&dc->io_lock); | |
923 | ||
924 | hlist_for_each_entry(i, iohash(dc, bio->bi_sector), hash) | |
925 | if (i->last == bio->bi_sector && | |
926 | time_before(jiffies, i->jiffies)) | |
927 | goto found; | |
928 | ||
929 | i = list_first_entry(&dc->io_lru, struct io, lru); | |
930 | ||
931 | add_sequential(s->task); | |
932 | i->sequential = 0; | |
933 | found: | |
934 | if (i->sequential + bio->bi_size > i->sequential) | |
935 | i->sequential += bio->bi_size; | |
936 | ||
937 | i->last = bio_end_sector(bio); | |
938 | i->jiffies = jiffies + msecs_to_jiffies(5000); | |
939 | s->task->sequential_io = i->sequential; | |
940 | ||
941 | hlist_del(&i->hash); | |
942 | hlist_add_head(&i->hash, iohash(dc, i->last)); | |
943 | list_move_tail(&i->lru, &dc->io_lru); | |
944 | ||
945 | spin_unlock(&dc->io_lock); | |
946 | } else { | |
947 | s->task->sequential_io = bio->bi_size; | |
948 | ||
949 | add_sequential(s->task); | |
950 | } | |
951 | ||
952 | sectors = max(s->task->sequential_io, | |
953 | s->task->sequential_io_avg) >> 9; | |
954 | ||
955 | if (dc->sequential_cutoff && | |
956 | sectors >= dc->sequential_cutoff >> 9) { | |
957 | trace_bcache_bypass_sequential(s->orig_bio); | |
958 | goto skip; | |
959 | } | |
960 | ||
961 | if (congested && sectors >= congested) { | |
962 | trace_bcache_bypass_congested(s->orig_bio); | |
963 | goto skip; | |
964 | } | |
965 | ||
966 | rescale: | |
967 | bch_rescale_priorities(c, bio_sectors(bio)); | |
968 | return false; | |
969 | skip: | |
970 | bch_mark_sectors_bypassed(s, bio_sectors(bio)); | |
971 | return true; | |
972 | } | |
973 | ||
cafe5635 KO |
974 | /* Process reads */ |
975 | ||
cdd972b1 | 976 | static void cached_dev_cache_miss_done(struct closure *cl) |
cafe5635 KO |
977 | { |
978 | struct search *s = container_of(cl, struct search, cl); | |
979 | ||
980 | if (s->op.insert_collision) | |
981 | bch_mark_cache_miss_collision(s); | |
982 | ||
c18536a7 | 983 | if (s->cache_bio) { |
cafe5635 KO |
984 | int i; |
985 | struct bio_vec *bv; | |
986 | ||
c18536a7 | 987 | bio_for_each_segment_all(bv, s->cache_bio, i) |
cafe5635 KO |
988 | __free_page(bv->bv_page); |
989 | } | |
990 | ||
991 | cached_dev_bio_complete(cl); | |
992 | } | |
993 | ||
cdd972b1 | 994 | static void cached_dev_read_error(struct closure *cl) |
cafe5635 KO |
995 | { |
996 | struct search *s = container_of(cl, struct search, cl); | |
cdd972b1 | 997 | struct bio *bio = &s->bio.bio; |
cafe5635 KO |
998 | struct bio_vec *bv; |
999 | int i; | |
1000 | ||
1001 | if (s->recoverable) { | |
c37511b8 KO |
1002 | /* Retry from the backing device: */ |
1003 | trace_bcache_read_retry(s->orig_bio); | |
cafe5635 KO |
1004 | |
1005 | s->error = 0; | |
1006 | bv = s->bio.bio.bi_io_vec; | |
1007 | do_bio_hook(s); | |
1008 | s->bio.bio.bi_io_vec = bv; | |
1009 | ||
1010 | if (!s->unaligned_bvec) | |
1011 | bio_for_each_segment(bv, s->orig_bio, i) | |
1012 | bv->bv_offset = 0, bv->bv_len = PAGE_SIZE; | |
1013 | else | |
1014 | memcpy(s->bio.bio.bi_io_vec, | |
1015 | bio_iovec(s->orig_bio), | |
1016 | sizeof(struct bio_vec) * | |
1017 | bio_segments(s->orig_bio)); | |
1018 | ||
1019 | /* XXX: invalidate cache */ | |
1020 | ||
cdd972b1 | 1021 | closure_bio_submit(bio, cl, s->d); |
cafe5635 KO |
1022 | } |
1023 | ||
cdd972b1 | 1024 | continue_at(cl, cached_dev_cache_miss_done, NULL); |
cafe5635 KO |
1025 | } |
1026 | ||
cdd972b1 | 1027 | static void cached_dev_read_done(struct closure *cl) |
cafe5635 KO |
1028 | { |
1029 | struct search *s = container_of(cl, struct search, cl); | |
1030 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); | |
1031 | ||
1032 | /* | |
cdd972b1 KO |
1033 | * We had a cache miss; cache_bio now contains data ready to be inserted |
1034 | * into the cache. | |
cafe5635 KO |
1035 | * |
1036 | * First, we copy the data we just read from cache_bio's bounce buffers | |
1037 | * to the buffers the original bio pointed to: | |
1038 | */ | |
1039 | ||
c18536a7 KO |
1040 | if (s->cache_bio) { |
1041 | bio_reset(s->cache_bio); | |
1042 | s->cache_bio->bi_sector = | |
1043 | s->cache_miss->bi_sector; | |
1044 | s->cache_bio->bi_bdev = s->cache_miss->bi_bdev; | |
1045 | s->cache_bio->bi_size = s->cache_bio_sectors << 9; | |
1046 | bch_bio_map(s->cache_bio, NULL); | |
cafe5635 | 1047 | |
c18536a7 | 1048 | bio_copy_data(s->cache_miss, s->cache_bio); |
cafe5635 KO |
1049 | |
1050 | bio_put(s->cache_miss); | |
1051 | s->cache_miss = NULL; | |
1052 | } | |
1053 | ||
1054 | if (verify(dc, &s->bio.bio) && s->recoverable) | |
1055 | bch_data_verify(s); | |
1056 | ||
1057 | bio_complete(s); | |
1058 | ||
c18536a7 KO |
1059 | if (s->cache_bio && |
1060 | !test_bit(CACHE_SET_STOPPING, &s->c->flags)) { | |
1b207d80 | 1061 | BUG_ON(!s->replace); |
b54d6934 | 1062 | closure_call(&s->btree, bch_data_insert, NULL, cl); |
cafe5635 KO |
1063 | } |
1064 | ||
cdd972b1 | 1065 | continue_at(cl, cached_dev_cache_miss_done, NULL); |
cafe5635 KO |
1066 | } |
1067 | ||
cdd972b1 | 1068 | static void cached_dev_read_done_bh(struct closure *cl) |
cafe5635 KO |
1069 | { |
1070 | struct search *s = container_of(cl, struct search, cl); | |
1071 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); | |
1072 | ||
c18536a7 KO |
1073 | bch_mark_cache_accounting(s, !s->cache_miss, s->bypass); |
1074 | trace_bcache_read(s->orig_bio, !s->cache_miss, s->bypass); | |
cafe5635 KO |
1075 | |
1076 | if (s->error) | |
cdd972b1 | 1077 | continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq); |
c18536a7 | 1078 | else if (s->cache_bio || verify(dc, &s->bio.bio)) |
cdd972b1 | 1079 | continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq); |
cafe5635 | 1080 | else |
cdd972b1 | 1081 | continue_at_nobarrier(cl, cached_dev_bio_complete, NULL); |
cafe5635 KO |
1082 | } |
1083 | ||
1084 | static int cached_dev_cache_miss(struct btree *b, struct search *s, | |
1085 | struct bio *bio, unsigned sectors) | |
1086 | { | |
2c1953e2 | 1087 | int ret = MAP_CONTINUE; |
e7c590eb | 1088 | unsigned reada = 0; |
cafe5635 | 1089 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); |
cdd972b1 | 1090 | struct bio *miss, *cache_bio; |
cafe5635 | 1091 | |
c18536a7 | 1092 | if (s->cache_miss || s->bypass) { |
e7c590eb | 1093 | miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split); |
2c1953e2 | 1094 | ret = miss == bio ? MAP_DONE : MAP_CONTINUE; |
e7c590eb KO |
1095 | goto out_submit; |
1096 | } | |
cafe5635 | 1097 | |
e7c590eb KO |
1098 | if (!(bio->bi_rw & REQ_RAHEAD) && |
1099 | !(bio->bi_rw & REQ_META) && | |
c18536a7 | 1100 | s->c->gc_stats.in_use < CUTOFF_CACHE_READA) |
e7c590eb KO |
1101 | reada = min_t(sector_t, dc->readahead >> 9, |
1102 | bdev_sectors(bio->bi_bdev) - bio_end_sector(bio)); | |
cafe5635 | 1103 | |
e7c590eb | 1104 | s->cache_bio_sectors = min(sectors, bio_sectors(bio) + reada); |
cafe5635 | 1105 | |
1b207d80 KO |
1106 | s->replace_key = KEY(s->inode, bio->bi_sector + |
1107 | s->cache_bio_sectors, s->cache_bio_sectors); | |
e7c590eb | 1108 | |
1b207d80 | 1109 | ret = bch_btree_insert_check_key(b, &s->op, &s->replace_key); |
e7c590eb KO |
1110 | if (ret) |
1111 | return ret; | |
1112 | ||
1b207d80 KO |
1113 | s->replace = true; |
1114 | ||
e7c590eb | 1115 | miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split); |
2c1953e2 KO |
1116 | |
1117 | /* btree_search_recurse()'s btree iterator is no good anymore */ | |
1118 | ret = miss == bio ? MAP_DONE : -EINTR; | |
cafe5635 | 1119 | |
cdd972b1 | 1120 | cache_bio = bio_alloc_bioset(GFP_NOWAIT, |
cafe5635 KO |
1121 | DIV_ROUND_UP(s->cache_bio_sectors, PAGE_SECTORS), |
1122 | dc->disk.bio_split); | |
cdd972b1 | 1123 | if (!cache_bio) |
cafe5635 KO |
1124 | goto out_submit; |
1125 | ||
cdd972b1 KO |
1126 | cache_bio->bi_sector = miss->bi_sector; |
1127 | cache_bio->bi_bdev = miss->bi_bdev; | |
1128 | cache_bio->bi_size = s->cache_bio_sectors << 9; | |
cafe5635 | 1129 | |
cdd972b1 KO |
1130 | cache_bio->bi_end_io = request_endio; |
1131 | cache_bio->bi_private = &s->cl; | |
cafe5635 | 1132 | |
cdd972b1 KO |
1133 | bch_bio_map(cache_bio, NULL); |
1134 | if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO)) | |
cafe5635 KO |
1135 | goto out_put; |
1136 | ||
cdd972b1 | 1137 | s->cache_miss = miss; |
c18536a7 | 1138 | s->cache_bio = cache_bio; |
cdd972b1 KO |
1139 | bio_get(cache_bio); |
1140 | closure_bio_submit(cache_bio, &s->cl, s->d); | |
cafe5635 KO |
1141 | |
1142 | return ret; | |
1143 | out_put: | |
cdd972b1 | 1144 | bio_put(cache_bio); |
cafe5635 | 1145 | out_submit: |
e7c590eb KO |
1146 | miss->bi_end_io = request_endio; |
1147 | miss->bi_private = &s->cl; | |
cafe5635 KO |
1148 | closure_bio_submit(miss, &s->cl, s->d); |
1149 | return ret; | |
1150 | } | |
1151 | ||
cdd972b1 | 1152 | static void cached_dev_read(struct cached_dev *dc, struct search *s) |
cafe5635 KO |
1153 | { |
1154 | struct closure *cl = &s->cl; | |
1155 | ||
b54d6934 | 1156 | closure_call(&s->btree, cache_lookup, NULL, cl); |
cdd972b1 | 1157 | continue_at(cl, cached_dev_read_done_bh, NULL); |
cafe5635 KO |
1158 | } |
1159 | ||
1160 | /* Process writes */ | |
1161 | ||
1162 | static void cached_dev_write_complete(struct closure *cl) | |
1163 | { | |
1164 | struct search *s = container_of(cl, struct search, cl); | |
1165 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); | |
1166 | ||
1167 | up_read_non_owner(&dc->writeback_lock); | |
1168 | cached_dev_bio_complete(cl); | |
1169 | } | |
1170 | ||
cdd972b1 | 1171 | static void cached_dev_write(struct cached_dev *dc, struct search *s) |
cafe5635 KO |
1172 | { |
1173 | struct closure *cl = &s->cl; | |
1174 | struct bio *bio = &s->bio.bio; | |
84f0db03 KO |
1175 | struct bkey start = KEY(dc->disk.id, bio->bi_sector, 0); |
1176 | struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0); | |
cafe5635 | 1177 | |
c18536a7 | 1178 | bch_keybuf_check_overlapping(&s->c->moving_gc_keys, &start, &end); |
cafe5635 | 1179 | |
cafe5635 | 1180 | down_read_non_owner(&dc->writeback_lock); |
cafe5635 | 1181 | if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) { |
84f0db03 KO |
1182 | /* |
1183 | * We overlap with some dirty data undergoing background | |
1184 | * writeback, force this write to writeback | |
1185 | */ | |
c18536a7 | 1186 | s->bypass = false; |
cafe5635 KO |
1187 | s->writeback = true; |
1188 | } | |
1189 | ||
84f0db03 KO |
1190 | /* |
1191 | * Discards aren't _required_ to do anything, so skipping if | |
1192 | * check_overlapping returned true is ok | |
1193 | * | |
1194 | * But check_overlapping drops dirty keys for which io hasn't started, | |
1195 | * so we still want to call it. | |
1196 | */ | |
cafe5635 | 1197 | if (bio->bi_rw & REQ_DISCARD) |
c18536a7 | 1198 | s->bypass = true; |
cafe5635 | 1199 | |
72c27061 KO |
1200 | if (should_writeback(dc, s->orig_bio, |
1201 | cache_mode(dc, bio), | |
c18536a7 KO |
1202 | s->bypass)) { |
1203 | s->bypass = false; | |
72c27061 KO |
1204 | s->writeback = true; |
1205 | } | |
1206 | ||
c18536a7 | 1207 | trace_bcache_write(s->orig_bio, s->writeback, s->bypass); |
c37511b8 | 1208 | |
c18536a7 KO |
1209 | if (s->bypass) { |
1210 | s->cache_bio = s->orig_bio; | |
1211 | bio_get(s->cache_bio); | |
cafe5635 | 1212 | |
84f0db03 KO |
1213 | if (!(bio->bi_rw & REQ_DISCARD) || |
1214 | blk_queue_discard(bdev_get_queue(dc->bdev))) | |
1215 | closure_bio_submit(bio, cl, s->d); | |
1216 | } else if (s->writeback) { | |
279afbad | 1217 | bch_writeback_add(dc); |
c18536a7 | 1218 | s->cache_bio = bio; |
e49c7c37 | 1219 | |
c0f04d88 | 1220 | if (bio->bi_rw & REQ_FLUSH) { |
e49c7c37 | 1221 | /* Also need to send a flush to the backing device */ |
d4eddd42 | 1222 | struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0, |
c0f04d88 | 1223 | dc->disk.bio_split); |
e49c7c37 | 1224 | |
c0f04d88 KO |
1225 | flush->bi_rw = WRITE_FLUSH; |
1226 | flush->bi_bdev = bio->bi_bdev; | |
1227 | flush->bi_end_io = request_endio; | |
1228 | flush->bi_private = cl; | |
1229 | ||
1230 | closure_bio_submit(flush, cl, s->d); | |
e49c7c37 | 1231 | } |
84f0db03 | 1232 | } else { |
c18536a7 KO |
1233 | s->cache_bio = bio_clone_bioset(bio, GFP_NOIO, |
1234 | dc->disk.bio_split); | |
84f0db03 KO |
1235 | |
1236 | closure_bio_submit(bio, cl, s->d); | |
cafe5635 | 1237 | } |
84f0db03 | 1238 | |
b54d6934 | 1239 | closure_call(&s->btree, bch_data_insert, NULL, cl); |
cafe5635 | 1240 | continue_at(cl, cached_dev_write_complete, NULL); |
cafe5635 KO |
1241 | } |
1242 | ||
a34a8bfd | 1243 | static void cached_dev_nodata(struct closure *cl) |
cafe5635 | 1244 | { |
a34a8bfd | 1245 | struct search *s = container_of(cl, struct search, cl); |
cafe5635 KO |
1246 | struct bio *bio = &s->bio.bio; |
1247 | ||
c18536a7 KO |
1248 | if (s->flush_journal) |
1249 | bch_journal_meta(s->c, cl); | |
cafe5635 | 1250 | |
84f0db03 | 1251 | /* If it's a flush, we send the flush to the backing device too */ |
cafe5635 KO |
1252 | closure_bio_submit(bio, cl, s->d); |
1253 | ||
1254 | continue_at(cl, cached_dev_bio_complete, NULL); | |
1255 | } | |
1256 | ||
1257 | /* Cached devices - read & write stuff */ | |
1258 | ||
cafe5635 KO |
1259 | static void cached_dev_make_request(struct request_queue *q, struct bio *bio) |
1260 | { | |
1261 | struct search *s; | |
1262 | struct bcache_device *d = bio->bi_bdev->bd_disk->private_data; | |
1263 | struct cached_dev *dc = container_of(d, struct cached_dev, disk); | |
1264 | int cpu, rw = bio_data_dir(bio); | |
1265 | ||
1266 | cpu = part_stat_lock(); | |
1267 | part_stat_inc(cpu, &d->disk->part0, ios[rw]); | |
1268 | part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio)); | |
1269 | part_stat_unlock(); | |
1270 | ||
1271 | bio->bi_bdev = dc->bdev; | |
2903381f | 1272 | bio->bi_sector += dc->sb.data_offset; |
cafe5635 KO |
1273 | |
1274 | if (cached_dev_get(dc)) { | |
1275 | s = search_alloc(bio, d); | |
1276 | trace_bcache_request_start(s, bio); | |
1277 | ||
a34a8bfd KO |
1278 | if (!bio->bi_size) { |
1279 | /* | |
1280 | * can't call bch_journal_meta from under | |
1281 | * generic_make_request | |
1282 | */ | |
1283 | continue_at_nobarrier(&s->cl, | |
1284 | cached_dev_nodata, | |
1285 | bcache_wq); | |
1286 | } else { | |
c18536a7 | 1287 | s->bypass = check_should_bypass(dc, s); |
84f0db03 KO |
1288 | |
1289 | if (rw) | |
cdd972b1 | 1290 | cached_dev_write(dc, s); |
84f0db03 | 1291 | else |
cdd972b1 | 1292 | cached_dev_read(dc, s); |
84f0db03 | 1293 | } |
cafe5635 KO |
1294 | } else { |
1295 | if ((bio->bi_rw & REQ_DISCARD) && | |
1296 | !blk_queue_discard(bdev_get_queue(dc->bdev))) | |
1297 | bio_endio(bio, 0); | |
1298 | else | |
1299 | bch_generic_make_request(bio, &d->bio_split_hook); | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode, | |
1304 | unsigned int cmd, unsigned long arg) | |
1305 | { | |
1306 | struct cached_dev *dc = container_of(d, struct cached_dev, disk); | |
1307 | return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg); | |
1308 | } | |
1309 | ||
1310 | static int cached_dev_congested(void *data, int bits) | |
1311 | { | |
1312 | struct bcache_device *d = data; | |
1313 | struct cached_dev *dc = container_of(d, struct cached_dev, disk); | |
1314 | struct request_queue *q = bdev_get_queue(dc->bdev); | |
1315 | int ret = 0; | |
1316 | ||
1317 | if (bdi_congested(&q->backing_dev_info, bits)) | |
1318 | return 1; | |
1319 | ||
1320 | if (cached_dev_get(dc)) { | |
1321 | unsigned i; | |
1322 | struct cache *ca; | |
1323 | ||
1324 | for_each_cache(ca, d->c, i) { | |
1325 | q = bdev_get_queue(ca->bdev); | |
1326 | ret |= bdi_congested(&q->backing_dev_info, bits); | |
1327 | } | |
1328 | ||
1329 | cached_dev_put(dc); | |
1330 | } | |
1331 | ||
1332 | return ret; | |
1333 | } | |
1334 | ||
1335 | void bch_cached_dev_request_init(struct cached_dev *dc) | |
1336 | { | |
1337 | struct gendisk *g = dc->disk.disk; | |
1338 | ||
1339 | g->queue->make_request_fn = cached_dev_make_request; | |
1340 | g->queue->backing_dev_info.congested_fn = cached_dev_congested; | |
1341 | dc->disk.cache_miss = cached_dev_cache_miss; | |
1342 | dc->disk.ioctl = cached_dev_ioctl; | |
1343 | } | |
1344 | ||
1345 | /* Flash backed devices */ | |
1346 | ||
1347 | static int flash_dev_cache_miss(struct btree *b, struct search *s, | |
1348 | struct bio *bio, unsigned sectors) | |
1349 | { | |
8e51e414 KO |
1350 | struct bio_vec *bv; |
1351 | int i; | |
1352 | ||
cafe5635 KO |
1353 | /* Zero fill bio */ |
1354 | ||
8e51e414 | 1355 | bio_for_each_segment(bv, bio, i) { |
cafe5635 KO |
1356 | unsigned j = min(bv->bv_len >> 9, sectors); |
1357 | ||
1358 | void *p = kmap(bv->bv_page); | |
1359 | memset(p + bv->bv_offset, 0, j << 9); | |
1360 | kunmap(bv->bv_page); | |
1361 | ||
8e51e414 | 1362 | sectors -= j; |
cafe5635 KO |
1363 | } |
1364 | ||
8e51e414 KO |
1365 | bio_advance(bio, min(sectors << 9, bio->bi_size)); |
1366 | ||
1367 | if (!bio->bi_size) | |
2c1953e2 | 1368 | return MAP_DONE; |
cafe5635 | 1369 | |
2c1953e2 | 1370 | return MAP_CONTINUE; |
cafe5635 KO |
1371 | } |
1372 | ||
a34a8bfd KO |
1373 | static void flash_dev_nodata(struct closure *cl) |
1374 | { | |
1375 | struct search *s = container_of(cl, struct search, cl); | |
1376 | ||
c18536a7 KO |
1377 | if (s->flush_journal) |
1378 | bch_journal_meta(s->c, cl); | |
a34a8bfd KO |
1379 | |
1380 | continue_at(cl, search_free, NULL); | |
1381 | } | |
1382 | ||
cafe5635 KO |
1383 | static void flash_dev_make_request(struct request_queue *q, struct bio *bio) |
1384 | { | |
1385 | struct search *s; | |
1386 | struct closure *cl; | |
1387 | struct bcache_device *d = bio->bi_bdev->bd_disk->private_data; | |
1388 | int cpu, rw = bio_data_dir(bio); | |
1389 | ||
1390 | cpu = part_stat_lock(); | |
1391 | part_stat_inc(cpu, &d->disk->part0, ios[rw]); | |
1392 | part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio)); | |
1393 | part_stat_unlock(); | |
1394 | ||
1395 | s = search_alloc(bio, d); | |
1396 | cl = &s->cl; | |
1397 | bio = &s->bio.bio; | |
1398 | ||
1399 | trace_bcache_request_start(s, bio); | |
1400 | ||
84f0db03 | 1401 | if (!bio->bi_size) { |
a34a8bfd KO |
1402 | /* |
1403 | * can't call bch_journal_meta from under | |
1404 | * generic_make_request | |
1405 | */ | |
1406 | continue_at_nobarrier(&s->cl, | |
1407 | flash_dev_nodata, | |
1408 | bcache_wq); | |
84f0db03 | 1409 | } else if (rw) { |
c18536a7 | 1410 | bch_keybuf_check_overlapping(&s->c->moving_gc_keys, |
8e51e414 KO |
1411 | &KEY(d->id, bio->bi_sector, 0), |
1412 | &KEY(d->id, bio_end_sector(bio), 0)); | |
cafe5635 | 1413 | |
c18536a7 | 1414 | s->bypass = (bio->bi_rw & REQ_DISCARD) != 0; |
cafe5635 | 1415 | s->writeback = true; |
c18536a7 | 1416 | s->cache_bio = bio; |
cafe5635 | 1417 | |
b54d6934 | 1418 | closure_call(&s->btree, bch_data_insert, NULL, cl); |
cafe5635 | 1419 | } else { |
b54d6934 | 1420 | closure_call(&s->btree, cache_lookup, NULL, cl); |
cafe5635 KO |
1421 | } |
1422 | ||
1423 | continue_at(cl, search_free, NULL); | |
1424 | } | |
1425 | ||
1426 | static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode, | |
1427 | unsigned int cmd, unsigned long arg) | |
1428 | { | |
1429 | return -ENOTTY; | |
1430 | } | |
1431 | ||
1432 | static int flash_dev_congested(void *data, int bits) | |
1433 | { | |
1434 | struct bcache_device *d = data; | |
1435 | struct request_queue *q; | |
1436 | struct cache *ca; | |
1437 | unsigned i; | |
1438 | int ret = 0; | |
1439 | ||
1440 | for_each_cache(ca, d->c, i) { | |
1441 | q = bdev_get_queue(ca->bdev); | |
1442 | ret |= bdi_congested(&q->backing_dev_info, bits); | |
1443 | } | |
1444 | ||
1445 | return ret; | |
1446 | } | |
1447 | ||
1448 | void bch_flash_dev_request_init(struct bcache_device *d) | |
1449 | { | |
1450 | struct gendisk *g = d->disk; | |
1451 | ||
1452 | g->queue->make_request_fn = flash_dev_make_request; | |
1453 | g->queue->backing_dev_info.congested_fn = flash_dev_congested; | |
1454 | d->cache_miss = flash_dev_cache_miss; | |
1455 | d->ioctl = flash_dev_ioctl; | |
1456 | } | |
1457 | ||
1458 | void bch_request_exit(void) | |
1459 | { | |
1460 | #ifdef CONFIG_CGROUP_BCACHE | |
1461 | cgroup_unload_subsys(&bcache_subsys); | |
1462 | #endif | |
1463 | if (bch_search_cache) | |
1464 | kmem_cache_destroy(bch_search_cache); | |
1465 | } | |
1466 | ||
1467 | int __init bch_request_init(void) | |
1468 | { | |
1469 | bch_search_cache = KMEM_CACHE(search, 0); | |
1470 | if (!bch_search_cache) | |
1471 | return -ENOMEM; | |
1472 | ||
1473 | #ifdef CONFIG_CGROUP_BCACHE | |
1474 | cgroup_load_subsys(&bcache_subsys); | |
1475 | init_bch_cgroup(&bcache_default_cgroup); | |
1476 | ||
1477 | cgroup_add_cftypes(&bcache_subsys, bch_files); | |
1478 | #endif | |
1479 | return 0; | |
1480 | } |