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