]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/md/bcache/request.c
bcache: Move some stuff to btree.c
[mirror_ubuntu-artful-kernel.git] / drivers / md / bcache / request.c
CommitLineData
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
26struct kmem_cache *bch_search_cache;
27
a34a8bfd
KO
28static void bch_data_insert_start(struct closure *);
29
cafe5635
KO
30/* Cgroup interface */
31
32#ifdef CONFIG_CGROUP_BCACHE
33static struct bch_cgroup bcache_default_cgroup = { .cache_mode = -1 };
34
35static 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
44struct 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
55static 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
69static 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
80static u64 bch_verify_read(struct cgroup *cgrp, struct cftype *cft)
81{
82 return cgroup_to_bcache(cgrp)->verify;
83}
84
85static 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
91static 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
97static 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
103static 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
110static 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
117static 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
147static void init_bch_cgroup(struct bch_cgroup *cg)
148{
149 cg->cache_mode = -1;
150}
151
152static 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
163static 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
170struct 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};
177EXPORT_SYMBOL_GPL(bcache_subsys);
178#endif
179
180static 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
190static 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
199static 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 216static 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
254struct open_bucket {
255 struct list_head list;
256 struct task_struct *last;
257 unsigned sectors_free;
258 BKEY_PADDED(key);
259};
260
261void 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
273int 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 */
309static 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);
324found:
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 */
347static 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
434static 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);
458out:
459 continue_at(cl, bch_data_insert_keys, bcache_wq);
460}
461
462static 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 493static 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 512static 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
578err:
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 631void 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
641/* Common code for the make_request functions */
642
643static void request_endio(struct bio *bio, int error)
644{
645 struct closure *cl = bio->bi_private;
646
647 if (error) {
648 struct search *s = container_of(cl, struct search, cl);
649 s->error = error;
650 /* Only cache read errors are recoverable */
651 s->recoverable = false;
652 }
653
654 bio_put(bio);
655 closure_put(cl);
656}
657
658void bch_cache_read_endio(struct bio *bio, int error)
659{
660 struct bbio *b = container_of(bio, struct bbio, bio);
661 struct closure *cl = bio->bi_private;
662 struct search *s = container_of(cl, struct search, cl);
663
664 /*
665 * If the bucket was reused while our bio was in flight, we might have
666 * read the wrong data. Set s->error but not error so it doesn't get
667 * counted against the cache device, but we'll still reread the data
668 * from the backing device.
669 */
670
671 if (error)
672 s->error = error;
673 else if (ptr_stale(s->op.c, &b->key, 0)) {
674 atomic_long_inc(&s->op.c->cache_read_races);
675 s->error = -EINTR;
676 }
677
678 bch_bbio_endio(s->op.c, bio, error, "reading from cache");
679}
680
681static void bio_complete(struct search *s)
682{
683 if (s->orig_bio) {
684 int cpu, rw = bio_data_dir(s->orig_bio);
685 unsigned long duration = jiffies - s->start_time;
686
687 cpu = part_stat_lock();
688 part_round_stats(cpu, &s->d->disk->part0);
689 part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration);
690 part_stat_unlock();
691
692 trace_bcache_request_end(s, s->orig_bio);
693 bio_endio(s->orig_bio, s->error);
694 s->orig_bio = NULL;
695 }
696}
697
698static void do_bio_hook(struct search *s)
699{
700 struct bio *bio = &s->bio.bio;
701 memcpy(bio, s->orig_bio, sizeof(struct bio));
702
703 bio->bi_end_io = request_endio;
704 bio->bi_private = &s->cl;
705 atomic_set(&bio->bi_cnt, 3);
706}
707
708static void search_free(struct closure *cl)
709{
710 struct search *s = container_of(cl, struct search, cl);
711 bio_complete(s);
712
713 if (s->op.cache_bio)
714 bio_put(s->op.cache_bio);
715
716 if (s->unaligned_bvec)
717 mempool_free(s->bio.bio.bi_io_vec, s->d->unaligned_bvec);
718
719 closure_debug_destroy(cl);
720 mempool_free(s, s->d->c->search);
721}
722
723static struct search *search_alloc(struct bio *bio, struct bcache_device *d)
724{
0b93207a 725 struct search *s;
cafe5635 726 struct bio_vec *bv;
0b93207a
KO
727
728 s = mempool_alloc(d->c->search, GFP_NOIO);
729 memset(s, 0, offsetof(struct search, insert_keys));
cafe5635
KO
730
731 __closure_init(&s->cl, NULL);
732
733 s->op.inode = d->id;
734 s->op.c = d->c;
735 s->d = d;
736 s->op.lock = -1;
737 s->task = current;
738 s->orig_bio = bio;
739 s->write = (bio->bi_rw & REQ_WRITE) != 0;
54d12f2b 740 s->op.flush_journal = (bio->bi_rw & (REQ_FLUSH|REQ_FUA)) != 0;
cafe5635
KO
741 s->recoverable = 1;
742 s->start_time = jiffies;
743 do_bio_hook(s);
744
745 if (bio->bi_size != bio_segments(bio) * PAGE_SIZE) {
746 bv = mempool_alloc(d->unaligned_bvec, GFP_NOIO);
747 memcpy(bv, bio_iovec(bio),
748 sizeof(struct bio_vec) * bio_segments(bio));
749
750 s->bio.bio.bi_io_vec = bv;
751 s->unaligned_bvec = 1;
752 }
753
754 return s;
755}
756
cafe5635
KO
757/* Cached devices */
758
759static void cached_dev_bio_complete(struct closure *cl)
760{
761 struct search *s = container_of(cl, struct search, cl);
762 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
763
764 search_free(cl);
765 cached_dev_put(dc);
766}
767
84f0db03
KO
768unsigned bch_get_congested(struct cache_set *c)
769{
770 int i;
771 long rand;
772
773 if (!c->congested_read_threshold_us &&
774 !c->congested_write_threshold_us)
775 return 0;
776
777 i = (local_clock_us() - c->congested_last_us) / 1024;
778 if (i < 0)
779 return 0;
780
781 i += atomic_read(&c->congested);
782 if (i >= 0)
783 return 0;
784
785 i += CONGESTED_MAX;
786
787 if (i > 0)
788 i = fract_exp_two(i, 6);
789
790 rand = get_random_int();
791 i -= bitmap_weight(&rand, BITS_PER_LONG);
792
793 return i > 0 ? i : 1;
794}
795
796static void add_sequential(struct task_struct *t)
797{
798 ewma_add(t->sequential_io_avg,
799 t->sequential_io, 8, 0);
800
801 t->sequential_io = 0;
802}
803
804static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k)
805{
806 return &dc->io_hash[hash_64(k, RECENT_IO_BITS)];
807}
808
809static bool check_should_bypass(struct cached_dev *dc, struct search *s)
810{
811 struct cache_set *c = s->op.c;
812 struct bio *bio = &s->bio.bio;
813 unsigned mode = cache_mode(dc, bio);
814 unsigned sectors, congested = bch_get_congested(c);
815
816 if (atomic_read(&dc->disk.detaching) ||
817 c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
818 (bio->bi_rw & REQ_DISCARD))
819 goto skip;
820
821 if (mode == CACHE_MODE_NONE ||
822 (mode == CACHE_MODE_WRITEAROUND &&
823 (bio->bi_rw & REQ_WRITE)))
824 goto skip;
825
826 if (bio->bi_sector & (c->sb.block_size - 1) ||
827 bio_sectors(bio) & (c->sb.block_size - 1)) {
828 pr_debug("skipping unaligned io");
829 goto skip;
830 }
831
832 if (!congested && !dc->sequential_cutoff)
833 goto rescale;
834
835 if (!congested &&
836 mode == CACHE_MODE_WRITEBACK &&
837 (bio->bi_rw & REQ_WRITE) &&
838 (bio->bi_rw & REQ_SYNC))
839 goto rescale;
840
841 if (dc->sequential_merge) {
842 struct io *i;
843
844 spin_lock(&dc->io_lock);
845
846 hlist_for_each_entry(i, iohash(dc, bio->bi_sector), hash)
847 if (i->last == bio->bi_sector &&
848 time_before(jiffies, i->jiffies))
849 goto found;
850
851 i = list_first_entry(&dc->io_lru, struct io, lru);
852
853 add_sequential(s->task);
854 i->sequential = 0;
855found:
856 if (i->sequential + bio->bi_size > i->sequential)
857 i->sequential += bio->bi_size;
858
859 i->last = bio_end_sector(bio);
860 i->jiffies = jiffies + msecs_to_jiffies(5000);
861 s->task->sequential_io = i->sequential;
862
863 hlist_del(&i->hash);
864 hlist_add_head(&i->hash, iohash(dc, i->last));
865 list_move_tail(&i->lru, &dc->io_lru);
866
867 spin_unlock(&dc->io_lock);
868 } else {
869 s->task->sequential_io = bio->bi_size;
870
871 add_sequential(s->task);
872 }
873
874 sectors = max(s->task->sequential_io,
875 s->task->sequential_io_avg) >> 9;
876
877 if (dc->sequential_cutoff &&
878 sectors >= dc->sequential_cutoff >> 9) {
879 trace_bcache_bypass_sequential(s->orig_bio);
880 goto skip;
881 }
882
883 if (congested && sectors >= congested) {
884 trace_bcache_bypass_congested(s->orig_bio);
885 goto skip;
886 }
887
888rescale:
889 bch_rescale_priorities(c, bio_sectors(bio));
890 return false;
891skip:
892 bch_mark_sectors_bypassed(s, bio_sectors(bio));
893 return true;
894}
895
cafe5635
KO
896/* Process reads */
897
cdd972b1 898static void cached_dev_cache_miss_done(struct closure *cl)
cafe5635
KO
899{
900 struct search *s = container_of(cl, struct search, cl);
901
902 if (s->op.insert_collision)
903 bch_mark_cache_miss_collision(s);
904
905 if (s->op.cache_bio) {
906 int i;
907 struct bio_vec *bv;
908
909 __bio_for_each_segment(bv, s->op.cache_bio, i, 0)
910 __free_page(bv->bv_page);
911 }
912
913 cached_dev_bio_complete(cl);
914}
915
cdd972b1 916static void cached_dev_read_error(struct closure *cl)
cafe5635
KO
917{
918 struct search *s = container_of(cl, struct search, cl);
cdd972b1 919 struct bio *bio = &s->bio.bio;
cafe5635
KO
920 struct bio_vec *bv;
921 int i;
922
923 if (s->recoverable) {
c37511b8
KO
924 /* Retry from the backing device: */
925 trace_bcache_read_retry(s->orig_bio);
cafe5635
KO
926
927 s->error = 0;
928 bv = s->bio.bio.bi_io_vec;
929 do_bio_hook(s);
930 s->bio.bio.bi_io_vec = bv;
931
932 if (!s->unaligned_bvec)
933 bio_for_each_segment(bv, s->orig_bio, i)
934 bv->bv_offset = 0, bv->bv_len = PAGE_SIZE;
935 else
936 memcpy(s->bio.bio.bi_io_vec,
937 bio_iovec(s->orig_bio),
938 sizeof(struct bio_vec) *
939 bio_segments(s->orig_bio));
940
941 /* XXX: invalidate cache */
942
cdd972b1 943 closure_bio_submit(bio, cl, s->d);
cafe5635
KO
944 }
945
cdd972b1 946 continue_at(cl, cached_dev_cache_miss_done, NULL);
cafe5635
KO
947}
948
cdd972b1 949static void cached_dev_read_done(struct closure *cl)
cafe5635
KO
950{
951 struct search *s = container_of(cl, struct search, cl);
952 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
953
954 /*
cdd972b1
KO
955 * We had a cache miss; cache_bio now contains data ready to be inserted
956 * into the cache.
cafe5635
KO
957 *
958 * First, we copy the data we just read from cache_bio's bounce buffers
959 * to the buffers the original bio pointed to:
960 */
961
962 if (s->op.cache_bio) {
cafe5635
KO
963 bio_reset(s->op.cache_bio);
964 s->op.cache_bio->bi_sector = s->cache_miss->bi_sector;
965 s->op.cache_bio->bi_bdev = s->cache_miss->bi_bdev;
966 s->op.cache_bio->bi_size = s->cache_bio_sectors << 9;
169ef1cf 967 bch_bio_map(s->op.cache_bio, NULL);
cafe5635 968
8e51e414 969 bio_copy_data(s->cache_miss, s->op.cache_bio);
cafe5635
KO
970
971 bio_put(s->cache_miss);
972 s->cache_miss = NULL;
973 }
974
975 if (verify(dc, &s->bio.bio) && s->recoverable)
976 bch_data_verify(s);
977
978 bio_complete(s);
979
980 if (s->op.cache_bio &&
981 !test_bit(CACHE_SET_STOPPING, &s->op.c->flags)) {
982 s->op.type = BTREE_REPLACE;
a34a8bfd 983 closure_call(&s->op.cl, bch_data_insert, NULL, cl);
cafe5635
KO
984 }
985
cdd972b1 986 continue_at(cl, cached_dev_cache_miss_done, NULL);
cafe5635
KO
987}
988
cdd972b1 989static void cached_dev_read_done_bh(struct closure *cl)
cafe5635
KO
990{
991 struct search *s = container_of(cl, struct search, cl);
992 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
993
84f0db03
KO
994 bch_mark_cache_accounting(s, !s->cache_miss, s->op.bypass);
995 trace_bcache_read(s->orig_bio, !s->cache_miss, s->op.bypass);
cafe5635
KO
996
997 if (s->error)
cdd972b1 998 continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq);
cafe5635 999 else if (s->op.cache_bio || verify(dc, &s->bio.bio))
cdd972b1 1000 continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq);
cafe5635 1001 else
cdd972b1 1002 continue_at_nobarrier(cl, cached_dev_bio_complete, NULL);
cafe5635
KO
1003}
1004
1005static int cached_dev_cache_miss(struct btree *b, struct search *s,
1006 struct bio *bio, unsigned sectors)
1007{
1008 int ret = 0;
e7c590eb 1009 unsigned reada = 0;
cafe5635 1010 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
cdd972b1 1011 struct bio *miss, *cache_bio;
cafe5635 1012
84f0db03 1013 if (s->cache_miss || s->op.bypass) {
e7c590eb
KO
1014 miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split);
1015 if (miss == bio)
1016 s->op.lookup_done = true;
1017 goto out_submit;
1018 }
cafe5635 1019
e7c590eb
KO
1020 if (!(bio->bi_rw & REQ_RAHEAD) &&
1021 !(bio->bi_rw & REQ_META) &&
1022 s->op.c->gc_stats.in_use < CUTOFF_CACHE_READA)
1023 reada = min_t(sector_t, dc->readahead >> 9,
1024 bdev_sectors(bio->bi_bdev) - bio_end_sector(bio));
cafe5635 1025
e7c590eb 1026 s->cache_bio_sectors = min(sectors, bio_sectors(bio) + reada);
cafe5635 1027
e7c590eb
KO
1028 s->op.replace = KEY(s->op.inode, bio->bi_sector +
1029 s->cache_bio_sectors, s->cache_bio_sectors);
1030
1031 ret = bch_btree_insert_check_key(b, &s->op, &s->op.replace);
1032 if (ret)
1033 return ret;
1034
1035 miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split);
1036 if (miss == bio)
1037 s->op.lookup_done = true;
1038 else
1039 /* btree_search_recurse()'s btree iterator is no good anymore */
1040 ret = -EINTR;
cafe5635 1041
cdd972b1 1042 cache_bio = bio_alloc_bioset(GFP_NOWAIT,
cafe5635
KO
1043 DIV_ROUND_UP(s->cache_bio_sectors, PAGE_SECTORS),
1044 dc->disk.bio_split);
cdd972b1 1045 if (!cache_bio)
cafe5635
KO
1046 goto out_submit;
1047
cdd972b1
KO
1048 cache_bio->bi_sector = miss->bi_sector;
1049 cache_bio->bi_bdev = miss->bi_bdev;
1050 cache_bio->bi_size = s->cache_bio_sectors << 9;
cafe5635 1051
cdd972b1
KO
1052 cache_bio->bi_end_io = request_endio;
1053 cache_bio->bi_private = &s->cl;
cafe5635 1054
cdd972b1
KO
1055 bch_bio_map(cache_bio, NULL);
1056 if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO))
cafe5635
KO
1057 goto out_put;
1058
cdd972b1
KO
1059 s->cache_miss = miss;
1060 s->op.cache_bio = cache_bio;
1061 bio_get(cache_bio);
1062 closure_bio_submit(cache_bio, &s->cl, s->d);
cafe5635
KO
1063
1064 return ret;
1065out_put:
cdd972b1 1066 bio_put(cache_bio);
cafe5635 1067out_submit:
e7c590eb
KO
1068 miss->bi_end_io = request_endio;
1069 miss->bi_private = &s->cl;
cafe5635
KO
1070 closure_bio_submit(miss, &s->cl, s->d);
1071 return ret;
1072}
1073
cdd972b1 1074static void cached_dev_read(struct cached_dev *dc, struct search *s)
cafe5635
KO
1075{
1076 struct closure *cl = &s->cl;
1077
df8e8970 1078 closure_call(&s->op.cl, bch_btree_search_async, NULL, cl);
cdd972b1 1079 continue_at(cl, cached_dev_read_done_bh, NULL);
cafe5635
KO
1080}
1081
1082/* Process writes */
1083
1084static void cached_dev_write_complete(struct closure *cl)
1085{
1086 struct search *s = container_of(cl, struct search, cl);
1087 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
1088
1089 up_read_non_owner(&dc->writeback_lock);
1090 cached_dev_bio_complete(cl);
1091}
1092
cdd972b1 1093static void cached_dev_write(struct cached_dev *dc, struct search *s)
cafe5635
KO
1094{
1095 struct closure *cl = &s->cl;
1096 struct bio *bio = &s->bio.bio;
84f0db03
KO
1097 struct bkey start = KEY(dc->disk.id, bio->bi_sector, 0);
1098 struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0);
cafe5635
KO
1099
1100 bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys, &start, &end);
1101
cafe5635 1102 down_read_non_owner(&dc->writeback_lock);
cafe5635 1103 if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
84f0db03
KO
1104 /*
1105 * We overlap with some dirty data undergoing background
1106 * writeback, force this write to writeback
1107 */
1108 s->op.bypass = false;
cafe5635
KO
1109 s->writeback = true;
1110 }
1111
84f0db03
KO
1112 /*
1113 * Discards aren't _required_ to do anything, so skipping if
1114 * check_overlapping returned true is ok
1115 *
1116 * But check_overlapping drops dirty keys for which io hasn't started,
1117 * so we still want to call it.
1118 */
cafe5635 1119 if (bio->bi_rw & REQ_DISCARD)
84f0db03 1120 s->op.bypass = true;
cafe5635 1121
72c27061
KO
1122 if (should_writeback(dc, s->orig_bio,
1123 cache_mode(dc, bio),
84f0db03
KO
1124 s->op.bypass)) {
1125 s->op.bypass = false;
72c27061
KO
1126 s->writeback = true;
1127 }
1128
84f0db03 1129 trace_bcache_write(s->orig_bio, s->writeback, s->op.bypass);
c37511b8 1130
84f0db03
KO
1131 if (s->op.bypass) {
1132 s->op.cache_bio = s->orig_bio;
1133 bio_get(s->op.cache_bio);
cafe5635 1134
84f0db03
KO
1135 if (!(bio->bi_rw & REQ_DISCARD) ||
1136 blk_queue_discard(bdev_get_queue(dc->bdev)))
1137 closure_bio_submit(bio, cl, s->d);
1138 } else if (s->writeback) {
279afbad 1139 bch_writeback_add(dc);
2fe80d3b 1140 s->op.cache_bio = bio;
e49c7c37 1141
c0f04d88 1142 if (bio->bi_rw & REQ_FLUSH) {
e49c7c37 1143 /* Also need to send a flush to the backing device */
d4eddd42 1144 struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0,
c0f04d88 1145 dc->disk.bio_split);
e49c7c37 1146
c0f04d88
KO
1147 flush->bi_rw = WRITE_FLUSH;
1148 flush->bi_bdev = bio->bi_bdev;
1149 flush->bi_end_io = request_endio;
1150 flush->bi_private = cl;
1151
1152 closure_bio_submit(flush, cl, s->d);
e49c7c37 1153 }
84f0db03
KO
1154 } else {
1155 s->op.cache_bio = bio_clone_bioset(bio, GFP_NOIO,
1156 dc->disk.bio_split);
1157
1158 closure_bio_submit(bio, cl, s->d);
cafe5635 1159 }
84f0db03 1160
a34a8bfd 1161 closure_call(&s->op.cl, bch_data_insert, NULL, cl);
cafe5635 1162 continue_at(cl, cached_dev_write_complete, NULL);
cafe5635
KO
1163}
1164
a34a8bfd 1165static void cached_dev_nodata(struct closure *cl)
cafe5635 1166{
a34a8bfd 1167 struct search *s = container_of(cl, struct search, cl);
cafe5635
KO
1168 struct bio *bio = &s->bio.bio;
1169
cafe5635
KO
1170 if (s->op.flush_journal)
1171 bch_journal_meta(s->op.c, cl);
1172
84f0db03 1173 /* If it's a flush, we send the flush to the backing device too */
cafe5635
KO
1174 closure_bio_submit(bio, cl, s->d);
1175
1176 continue_at(cl, cached_dev_bio_complete, NULL);
1177}
1178
1179/* Cached devices - read & write stuff */
1180
cafe5635
KO
1181static void cached_dev_make_request(struct request_queue *q, struct bio *bio)
1182{
1183 struct search *s;
1184 struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
1185 struct cached_dev *dc = container_of(d, struct cached_dev, disk);
1186 int cpu, rw = bio_data_dir(bio);
1187
1188 cpu = part_stat_lock();
1189 part_stat_inc(cpu, &d->disk->part0, ios[rw]);
1190 part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
1191 part_stat_unlock();
1192
1193 bio->bi_bdev = dc->bdev;
2903381f 1194 bio->bi_sector += dc->sb.data_offset;
cafe5635
KO
1195
1196 if (cached_dev_get(dc)) {
1197 s = search_alloc(bio, d);
1198 trace_bcache_request_start(s, bio);
1199
a34a8bfd
KO
1200 if (!bio->bi_size) {
1201 /*
1202 * can't call bch_journal_meta from under
1203 * generic_make_request
1204 */
1205 continue_at_nobarrier(&s->cl,
1206 cached_dev_nodata,
1207 bcache_wq);
1208 } else {
84f0db03
KO
1209 s->op.bypass = check_should_bypass(dc, s);
1210
1211 if (rw)
cdd972b1 1212 cached_dev_write(dc, s);
84f0db03 1213 else
cdd972b1 1214 cached_dev_read(dc, s);
84f0db03 1215 }
cafe5635
KO
1216 } else {
1217 if ((bio->bi_rw & REQ_DISCARD) &&
1218 !blk_queue_discard(bdev_get_queue(dc->bdev)))
1219 bio_endio(bio, 0);
1220 else
1221 bch_generic_make_request(bio, &d->bio_split_hook);
1222 }
1223}
1224
1225static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
1226 unsigned int cmd, unsigned long arg)
1227{
1228 struct cached_dev *dc = container_of(d, struct cached_dev, disk);
1229 return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
1230}
1231
1232static int cached_dev_congested(void *data, int bits)
1233{
1234 struct bcache_device *d = data;
1235 struct cached_dev *dc = container_of(d, struct cached_dev, disk);
1236 struct request_queue *q = bdev_get_queue(dc->bdev);
1237 int ret = 0;
1238
1239 if (bdi_congested(&q->backing_dev_info, bits))
1240 return 1;
1241
1242 if (cached_dev_get(dc)) {
1243 unsigned i;
1244 struct cache *ca;
1245
1246 for_each_cache(ca, d->c, i) {
1247 q = bdev_get_queue(ca->bdev);
1248 ret |= bdi_congested(&q->backing_dev_info, bits);
1249 }
1250
1251 cached_dev_put(dc);
1252 }
1253
1254 return ret;
1255}
1256
1257void bch_cached_dev_request_init(struct cached_dev *dc)
1258{
1259 struct gendisk *g = dc->disk.disk;
1260
1261 g->queue->make_request_fn = cached_dev_make_request;
1262 g->queue->backing_dev_info.congested_fn = cached_dev_congested;
1263 dc->disk.cache_miss = cached_dev_cache_miss;
1264 dc->disk.ioctl = cached_dev_ioctl;
1265}
1266
1267/* Flash backed devices */
1268
1269static int flash_dev_cache_miss(struct btree *b, struct search *s,
1270 struct bio *bio, unsigned sectors)
1271{
8e51e414
KO
1272 struct bio_vec *bv;
1273 int i;
1274
cafe5635
KO
1275 /* Zero fill bio */
1276
8e51e414 1277 bio_for_each_segment(bv, bio, i) {
cafe5635
KO
1278 unsigned j = min(bv->bv_len >> 9, sectors);
1279
1280 void *p = kmap(bv->bv_page);
1281 memset(p + bv->bv_offset, 0, j << 9);
1282 kunmap(bv->bv_page);
1283
8e51e414 1284 sectors -= j;
cafe5635
KO
1285 }
1286
8e51e414
KO
1287 bio_advance(bio, min(sectors << 9, bio->bi_size));
1288
1289 if (!bio->bi_size)
1290 s->op.lookup_done = true;
cafe5635
KO
1291
1292 return 0;
1293}
1294
a34a8bfd
KO
1295static void flash_dev_nodata(struct closure *cl)
1296{
1297 struct search *s = container_of(cl, struct search, cl);
1298
1299 if (s->op.flush_journal)
1300 bch_journal_meta(s->op.c, cl);
1301
1302 continue_at(cl, search_free, NULL);
1303}
1304
cafe5635
KO
1305static void flash_dev_make_request(struct request_queue *q, struct bio *bio)
1306{
1307 struct search *s;
1308 struct closure *cl;
1309 struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
1310 int cpu, rw = bio_data_dir(bio);
1311
1312 cpu = part_stat_lock();
1313 part_stat_inc(cpu, &d->disk->part0, ios[rw]);
1314 part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
1315 part_stat_unlock();
1316
1317 s = search_alloc(bio, d);
1318 cl = &s->cl;
1319 bio = &s->bio.bio;
1320
1321 trace_bcache_request_start(s, bio);
1322
84f0db03 1323 if (!bio->bi_size) {
a34a8bfd
KO
1324 /*
1325 * can't call bch_journal_meta from under
1326 * generic_make_request
1327 */
1328 continue_at_nobarrier(&s->cl,
1329 flash_dev_nodata,
1330 bcache_wq);
84f0db03 1331 } else if (rw) {
cafe5635 1332 bch_keybuf_check_overlapping(&s->op.c->moving_gc_keys,
8e51e414
KO
1333 &KEY(d->id, bio->bi_sector, 0),
1334 &KEY(d->id, bio_end_sector(bio), 0));
cafe5635 1335
84f0db03 1336 s->op.bypass = (bio->bi_rw & REQ_DISCARD) != 0;
cafe5635
KO
1337 s->writeback = true;
1338 s->op.cache_bio = bio;
1339
a34a8bfd 1340 closure_call(&s->op.cl, bch_data_insert, NULL, cl);
cafe5635 1341 } else {
df8e8970 1342 closure_call(&s->op.cl, bch_btree_search_async, NULL, cl);
cafe5635
KO
1343 }
1344
1345 continue_at(cl, search_free, NULL);
1346}
1347
1348static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
1349 unsigned int cmd, unsigned long arg)
1350{
1351 return -ENOTTY;
1352}
1353
1354static int flash_dev_congested(void *data, int bits)
1355{
1356 struct bcache_device *d = data;
1357 struct request_queue *q;
1358 struct cache *ca;
1359 unsigned i;
1360 int ret = 0;
1361
1362 for_each_cache(ca, d->c, i) {
1363 q = bdev_get_queue(ca->bdev);
1364 ret |= bdi_congested(&q->backing_dev_info, bits);
1365 }
1366
1367 return ret;
1368}
1369
1370void bch_flash_dev_request_init(struct bcache_device *d)
1371{
1372 struct gendisk *g = d->disk;
1373
1374 g->queue->make_request_fn = flash_dev_make_request;
1375 g->queue->backing_dev_info.congested_fn = flash_dev_congested;
1376 d->cache_miss = flash_dev_cache_miss;
1377 d->ioctl = flash_dev_ioctl;
1378}
1379
1380void bch_request_exit(void)
1381{
1382#ifdef CONFIG_CGROUP_BCACHE
1383 cgroup_unload_subsys(&bcache_subsys);
1384#endif
1385 if (bch_search_cache)
1386 kmem_cache_destroy(bch_search_cache);
1387}
1388
1389int __init bch_request_init(void)
1390{
1391 bch_search_cache = KMEM_CACHE(search, 0);
1392 if (!bch_search_cache)
1393 return -ENOMEM;
1394
1395#ifdef CONFIG_CGROUP_BCACHE
1396 cgroup_load_subsys(&bcache_subsys);
1397 init_bch_cgroup(&bcache_default_cgroup);
1398
1399 cgroup_add_cftypes(&bcache_subsys, bch_files);
1400#endif
1401 return 0;
1402}