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.
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
13 #include "writeback.h"
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"
21 #include <trace/events/bcache.h>
23 #define CUTOFF_CACHE_ADD 95
24 #define CUTOFF_CACHE_READA 90
26 struct kmem_cache
*bch_search_cache
;
28 static void bch_data_insert_start(struct closure
*);
30 /* Cgroup interface */
32 #ifdef CONFIG_CGROUP_BCACHE
33 static struct bch_cgroup bcache_default_cgroup
= { .cache_mode
= -1 };
35 static struct bch_cgroup
*cgroup_to_bcache(struct cgroup
*cgroup
)
37 struct cgroup_subsys_state
*css
;
39 (css
= cgroup_subsys_state(cgroup
, bcache_subsys_id
))
40 ? container_of(css
, struct bch_cgroup
, css
)
41 : &bcache_default_cgroup
;
44 struct bch_cgroup
*bch_bio_to_cgroup(struct bio
*bio
)
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
);
51 ? container_of(css
, struct bch_cgroup
, css
)
52 : &bcache_default_cgroup
;
55 static ssize_t
cache_mode_read(struct cgroup
*cgrp
, struct cftype
*cft
,
57 char __user
*buf
, size_t nbytes
, loff_t
*ppos
)
60 int len
= bch_snprint_string_list(tmp
, PAGE_SIZE
, bch_cache_modes
,
61 cgroup_to_bcache(cgrp
)->cache_mode
+ 1);
66 return simple_read_from_buffer(buf
, nbytes
, ppos
, tmp
, len
);
69 static int cache_mode_write(struct cgroup
*cgrp
, struct cftype
*cft
,
72 int v
= bch_read_string_list(buf
, bch_cache_modes
);
76 cgroup_to_bcache(cgrp
)->cache_mode
= v
- 1;
80 static u64
bch_verify_read(struct cgroup
*cgrp
, struct cftype
*cft
)
82 return cgroup_to_bcache(cgrp
)->verify
;
85 static int bch_verify_write(struct cgroup
*cgrp
, struct cftype
*cft
, u64 val
)
87 cgroup_to_bcache(cgrp
)->verify
= val
;
91 static u64
bch_cache_hits_read(struct cgroup
*cgrp
, struct cftype
*cft
)
93 struct bch_cgroup
*bcachecg
= cgroup_to_bcache(cgrp
);
94 return atomic_read(&bcachecg
->stats
.cache_hits
);
97 static u64
bch_cache_misses_read(struct cgroup
*cgrp
, struct cftype
*cft
)
99 struct bch_cgroup
*bcachecg
= cgroup_to_bcache(cgrp
);
100 return atomic_read(&bcachecg
->stats
.cache_misses
);
103 static u64
bch_cache_bypass_hits_read(struct cgroup
*cgrp
,
106 struct bch_cgroup
*bcachecg
= cgroup_to_bcache(cgrp
);
107 return atomic_read(&bcachecg
->stats
.cache_bypass_hits
);
110 static u64
bch_cache_bypass_misses_read(struct cgroup
*cgrp
,
113 struct bch_cgroup
*bcachecg
= cgroup_to_bcache(cgrp
);
114 return atomic_read(&bcachecg
->stats
.cache_bypass_misses
);
117 static struct cftype bch_files
[] = {
119 .name
= "cache_mode",
120 .read
= cache_mode_read
,
121 .write_string
= cache_mode_write
,
125 .read_u64
= bch_verify_read
,
126 .write_u64
= bch_verify_write
,
129 .name
= "cache_hits",
130 .read_u64
= bch_cache_hits_read
,
133 .name
= "cache_misses",
134 .read_u64
= bch_cache_misses_read
,
137 .name
= "cache_bypass_hits",
138 .read_u64
= bch_cache_bypass_hits_read
,
141 .name
= "cache_bypass_misses",
142 .read_u64
= bch_cache_bypass_misses_read
,
147 static void init_bch_cgroup(struct bch_cgroup
*cg
)
152 static struct cgroup_subsys_state
*bcachecg_create(struct cgroup
*cgroup
)
154 struct bch_cgroup
*cg
;
156 cg
= kzalloc(sizeof(*cg
), GFP_KERNEL
);
158 return ERR_PTR(-ENOMEM
);
163 static void bcachecg_destroy(struct cgroup
*cgroup
)
165 struct bch_cgroup
*cg
= cgroup_to_bcache(cgroup
);
166 free_css_id(&bcache_subsys
, &cg
->css
);
170 struct cgroup_subsys bcache_subsys
= {
171 .create
= bcachecg_create
,
172 .destroy
= bcachecg_destroy
,
173 .subsys_id
= bcache_subsys_id
,
175 .module
= THIS_MODULE
,
177 EXPORT_SYMBOL_GPL(bcache_subsys
);
180 static unsigned cache_mode(struct cached_dev
*dc
, struct bio
*bio
)
182 #ifdef CONFIG_CGROUP_BCACHE
183 int r
= bch_bio_to_cgroup(bio
)->cache_mode
;
187 return BDEV_CACHE_MODE(&dc
->sb
);
190 static bool verify(struct cached_dev
*dc
, struct bio
*bio
)
192 #ifdef CONFIG_CGROUP_BCACHE
193 if (bch_bio_to_cgroup(bio
)->verify
)
199 static void bio_csum(struct bio
*bio
, struct bkey
*k
)
205 bio_for_each_segment(bv
, bio
, i
) {
206 void *d
= kmap(bv
->bv_page
) + bv
->bv_offset
;
207 csum
= bch_crc64_update(csum
, d
, bv
->bv_len
);
211 k
->ptr
[KEY_PTRS(k
)] = csum
& (~0ULL >> 1);
214 /* Insert data into cache */
216 static void bch_data_insert_keys(struct closure
*cl
)
218 struct data_insert_op
*op
= container_of(cl
, struct data_insert_op
, cl
);
219 atomic_t
*journal_ref
= NULL
;
220 struct bkey
*replace_key
= op
->replace
? &op
->replace_key
: NULL
;
224 * If we're looping, might already be waiting on
225 * another journal write - can't wait on more than one journal write at
228 * XXX: this looks wrong
231 while (atomic_read(&s
->cl
.remaining
) & CLOSURE_WAITING
)
232 closure_sync(&s
->cl
);
236 journal_ref
= bch_journal(op
->c
, &op
->insert_keys
,
237 op
->flush_journal
? cl
: NULL
);
239 ret
= bch_btree_insert(op
->c
, &op
->insert_keys
,
240 journal_ref
, replace_key
);
242 op
->replace_collision
= true;
245 op
->insert_data_done
= true;
249 atomic_dec_bug(journal_ref
);
251 if (!op
->insert_data_done
)
252 continue_at(cl
, bch_data_insert_start
, bcache_wq
);
254 bch_keylist_free(&op
->insert_keys
);
258 static void bch_data_invalidate(struct closure
*cl
)
260 struct data_insert_op
*op
= container_of(cl
, struct data_insert_op
, cl
);
261 struct bio
*bio
= op
->bio
;
263 pr_debug("invalidating %i sectors from %llu",
264 bio_sectors(bio
), (uint64_t) bio
->bi_sector
);
266 while (bio_sectors(bio
)) {
267 unsigned len
= min(bio_sectors(bio
), 1U << 14);
269 if (bch_keylist_realloc(&op
->insert_keys
, 0, op
->c
))
272 bio
->bi_sector
+= len
;
273 bio
->bi_size
-= len
<< 9;
275 bch_keylist_add(&op
->insert_keys
,
276 &KEY(op
->inode
, bio
->bi_sector
, len
));
279 op
->insert_data_done
= true;
282 continue_at(cl
, bch_data_insert_keys
, bcache_wq
);
285 static void bch_data_insert_error(struct closure
*cl
)
287 struct data_insert_op
*op
= container_of(cl
, struct data_insert_op
, cl
);
290 * Our data write just errored, which means we've got a bunch of keys to
291 * insert that point to data that wasn't succesfully written.
293 * We don't have to insert those keys but we still have to invalidate
294 * that region of the cache - so, if we just strip off all the pointers
295 * from the keys we'll accomplish just that.
298 struct bkey
*src
= op
->insert_keys
.keys
, *dst
= op
->insert_keys
.keys
;
300 while (src
!= op
->insert_keys
.top
) {
301 struct bkey
*n
= bkey_next(src
);
303 SET_KEY_PTRS(src
, 0);
304 memmove(dst
, src
, bkey_bytes(src
));
306 dst
= bkey_next(dst
);
310 op
->insert_keys
.top
= dst
;
312 bch_data_insert_keys(cl
);
315 static void bch_data_insert_endio(struct bio
*bio
, int error
)
317 struct closure
*cl
= bio
->bi_private
;
318 struct data_insert_op
*op
= container_of(cl
, struct data_insert_op
, cl
);
321 /* TODO: We could try to recover from this. */
324 else if (!op
->replace
)
325 set_closure_fn(cl
, bch_data_insert_error
, bcache_wq
);
327 set_closure_fn(cl
, NULL
, NULL
);
330 bch_bbio_endio(op
->c
, bio
, error
, "writing data to cache");
333 static void bch_data_insert_start(struct closure
*cl
)
335 struct data_insert_op
*op
= container_of(cl
, struct data_insert_op
, cl
);
336 struct bio
*bio
= op
->bio
, *n
;
339 return bch_data_invalidate(cl
);
341 if (atomic_sub_return(bio_sectors(bio
), &op
->c
->sectors_to_gc
) < 0) {
342 set_gc_sectors(op
->c
);
347 * Journal writes are marked REQ_FLUSH; if the original write was a
348 * flush, it'll wait on the journal write.
350 bio
->bi_rw
&= ~(REQ_FLUSH
|REQ_FUA
);
355 struct bio_set
*split
= op
->c
->bio_split
;
357 /* 1 for the device pointer and 1 for the chksum */
358 if (bch_keylist_realloc(&op
->insert_keys
,
359 1 + (op
->csum
? 1 : 0),
361 continue_at(cl
, bch_data_insert_keys
, bcache_wq
);
363 k
= op
->insert_keys
.top
;
365 SET_KEY_INODE(k
, op
->inode
);
366 SET_KEY_OFFSET(k
, bio
->bi_sector
);
368 if (!bch_alloc_sectors(op
->c
, k
, bio_sectors(bio
),
369 op
->write_point
, op
->write_prio
,
373 n
= bch_bio_split(bio
, KEY_SIZE(k
), GFP_NOIO
, split
);
375 n
->bi_end_io
= bch_data_insert_endio
;
379 SET_KEY_DIRTY(k
, true);
381 for (i
= 0; i
< KEY_PTRS(k
); i
++)
382 SET_GC_MARK(PTR_BUCKET(op
->c
, k
, i
),
386 SET_KEY_CSUM(k
, op
->csum
);
390 trace_bcache_cache_insert(k
);
391 bch_keylist_push(&op
->insert_keys
);
393 n
->bi_rw
|= REQ_WRITE
;
394 bch_submit_bbio(n
, op
->c
, k
, 0);
397 op
->insert_data_done
= true;
398 continue_at(cl
, bch_data_insert_keys
, bcache_wq
);
400 /* bch_alloc_sectors() blocks if s->writeback = true */
401 BUG_ON(op
->writeback
);
404 * But if it's not a writeback write we'd rather just bail out if
405 * there aren't any buckets ready to write to - it might take awhile and
406 * we might be starving btree writes for gc or something.
411 * Writethrough write: We can't complete the write until we've
412 * updated the index. But we don't want to delay the write while
413 * we wait for buckets to be freed up, so just invalidate the
417 return bch_data_invalidate(cl
);
420 * From a cache miss, we can just insert the keys for the data
421 * we have written or bail out if we didn't do anything.
423 op
->insert_data_done
= true;
426 if (!bch_keylist_empty(&op
->insert_keys
))
427 continue_at(cl
, bch_data_insert_keys
, bcache_wq
);
434 * bch_data_insert - stick some data in the cache
436 * This is the starting point for any data to end up in a cache device; it could
437 * be from a normal write, or a writeback write, or a write to a flash only
438 * volume - it's also used by the moving garbage collector to compact data in
439 * mostly empty buckets.
441 * It first writes the data to the cache, creating a list of keys to be inserted
442 * (if the data had to be fragmented there will be multiple keys); after the
443 * data is written it calls bch_journal, and after the keys have been added to
444 * the next journal write they're inserted into the btree.
446 * It inserts the data in s->cache_bio; bi_sector is used for the key offset,
447 * and op->inode is used for the key inode.
449 * If s->bypass is true, instead of inserting the data it invalidates the
450 * region of the cache represented by s->cache_bio and op->inode.
452 void bch_data_insert(struct closure
*cl
)
454 struct data_insert_op
*op
= container_of(cl
, struct data_insert_op
, cl
);
456 trace_bcache_write(op
->bio
, op
->writeback
, op
->bypass
);
458 bch_keylist_init(&op
->insert_keys
);
460 bch_data_insert_start(cl
);
465 unsigned bch_get_congested(struct cache_set
*c
)
470 if (!c
->congested_read_threshold_us
&&
471 !c
->congested_write_threshold_us
)
474 i
= (local_clock_us() - c
->congested_last_us
) / 1024;
478 i
+= atomic_read(&c
->congested
);
485 i
= fract_exp_two(i
, 6);
487 rand
= get_random_int();
488 i
-= bitmap_weight(&rand
, BITS_PER_LONG
);
490 return i
> 0 ? i
: 1;
493 static void add_sequential(struct task_struct
*t
)
495 ewma_add(t
->sequential_io_avg
,
496 t
->sequential_io
, 8, 0);
498 t
->sequential_io
= 0;
501 static struct hlist_head
*iohash(struct cached_dev
*dc
, uint64_t k
)
503 return &dc
->io_hash
[hash_64(k
, RECENT_IO_BITS
)];
506 static bool check_should_bypass(struct cached_dev
*dc
, struct bio
*bio
)
508 struct cache_set
*c
= dc
->disk
.c
;
509 unsigned mode
= cache_mode(dc
, bio
);
510 unsigned sectors
, congested
= bch_get_congested(c
);
511 struct task_struct
*task
= current
;
513 if (atomic_read(&dc
->disk
.detaching
) ||
514 c
->gc_stats
.in_use
> CUTOFF_CACHE_ADD
||
515 (bio
->bi_rw
& REQ_DISCARD
))
518 if (mode
== CACHE_MODE_NONE
||
519 (mode
== CACHE_MODE_WRITEAROUND
&&
520 (bio
->bi_rw
& REQ_WRITE
)))
523 if (bio
->bi_sector
& (c
->sb
.block_size
- 1) ||
524 bio_sectors(bio
) & (c
->sb
.block_size
- 1)) {
525 pr_debug("skipping unaligned io");
529 if (!congested
&& !dc
->sequential_cutoff
)
533 mode
== CACHE_MODE_WRITEBACK
&&
534 (bio
->bi_rw
& REQ_WRITE
) &&
535 (bio
->bi_rw
& REQ_SYNC
))
538 if (dc
->sequential_merge
) {
541 spin_lock(&dc
->io_lock
);
543 hlist_for_each_entry(i
, iohash(dc
, bio
->bi_sector
), hash
)
544 if (i
->last
== bio
->bi_sector
&&
545 time_before(jiffies
, i
->jiffies
))
548 i
= list_first_entry(&dc
->io_lru
, struct io
, lru
);
550 add_sequential(task
);
553 if (i
->sequential
+ bio
->bi_size
> i
->sequential
)
554 i
->sequential
+= bio
->bi_size
;
556 i
->last
= bio_end_sector(bio
);
557 i
->jiffies
= jiffies
+ msecs_to_jiffies(5000);
558 task
->sequential_io
= i
->sequential
;
561 hlist_add_head(&i
->hash
, iohash(dc
, i
->last
));
562 list_move_tail(&i
->lru
, &dc
->io_lru
);
564 spin_unlock(&dc
->io_lock
);
566 task
->sequential_io
= bio
->bi_size
;
568 add_sequential(task
);
571 sectors
= max(task
->sequential_io
,
572 task
->sequential_io_avg
) >> 9;
574 if (dc
->sequential_cutoff
&&
575 sectors
>= dc
->sequential_cutoff
>> 9) {
576 trace_bcache_bypass_sequential(bio
);
580 if (congested
&& sectors
>= congested
) {
581 trace_bcache_bypass_congested(bio
);
586 bch_rescale_priorities(c
, bio_sectors(bio
));
589 bch_mark_sectors_bypassed(c
, dc
, bio_sectors(bio
));
596 /* Stack frame for bio_complete */
599 struct bcache_device
*d
;
602 struct bio
*orig_bio
;
603 struct bio
*cache_miss
;
605 unsigned insert_bio_sectors
;
607 unsigned recoverable
:1;
608 unsigned unaligned_bvec
:1;
611 unsigned long start_time
;
614 struct data_insert_op iop
;
617 static void bch_cache_read_endio(struct bio
*bio
, int error
)
619 struct bbio
*b
= container_of(bio
, struct bbio
, bio
);
620 struct closure
*cl
= bio
->bi_private
;
621 struct search
*s
= container_of(cl
, struct search
, cl
);
624 * If the bucket was reused while our bio was in flight, we might have
625 * read the wrong data. Set s->error but not error so it doesn't get
626 * counted against the cache device, but we'll still reread the data
627 * from the backing device.
631 s
->iop
.error
= error
;
632 else if (ptr_stale(s
->iop
.c
, &b
->key
, 0)) {
633 atomic_long_inc(&s
->iop
.c
->cache_read_races
);
634 s
->iop
.error
= -EINTR
;
637 bch_bbio_endio(s
->iop
.c
, bio
, error
, "reading from cache");
641 * Read from a single key, handling the initial cache miss if the key starts in
642 * the middle of the bio
644 static int cache_lookup_fn(struct btree_op
*op
, struct btree
*b
, struct bkey
*k
)
646 struct search
*s
= container_of(op
, struct search
, op
);
647 struct bio
*n
, *bio
= &s
->bio
.bio
;
648 struct bkey
*bio_key
;
651 if (bkey_cmp(k
, &KEY(s
->iop
.inode
, bio
->bi_sector
, 0)) <= 0)
654 if (KEY_INODE(k
) != s
->iop
.inode
||
655 KEY_START(k
) > bio
->bi_sector
) {
656 unsigned bio_sectors
= bio_sectors(bio
);
657 unsigned sectors
= KEY_INODE(k
) == s
->iop
.inode
658 ? min_t(uint64_t, INT_MAX
,
659 KEY_START(k
) - bio
->bi_sector
)
662 int ret
= s
->d
->cache_miss(b
, s
, bio
, sectors
);
663 if (ret
!= MAP_CONTINUE
)
666 /* if this was a complete miss we shouldn't get here */
667 BUG_ON(bio_sectors
<= sectors
);
673 /* XXX: figure out best pointer - for multiple cache devices */
676 PTR_BUCKET(b
->c
, k
, ptr
)->prio
= INITIAL_PRIO
;
678 n
= bch_bio_split(bio
, min_t(uint64_t, INT_MAX
,
679 KEY_OFFSET(k
) - bio
->bi_sector
),
680 GFP_NOIO
, s
->d
->bio_split
);
682 bio_key
= &container_of(n
, struct bbio
, bio
)->key
;
683 bch_bkey_copy_single_ptr(bio_key
, k
, ptr
);
685 bch_cut_front(&KEY(s
->iop
.inode
, n
->bi_sector
, 0), bio_key
);
686 bch_cut_back(&KEY(s
->iop
.inode
, bio_end_sector(n
), 0), bio_key
);
688 n
->bi_end_io
= bch_cache_read_endio
;
689 n
->bi_private
= &s
->cl
;
692 * The bucket we're reading from might be reused while our bio
693 * is in flight, and we could then end up reading the wrong
696 * We guard against this by checking (in cache_read_endio()) if
697 * the pointer is stale again; if so, we treat it as an error
698 * and reread from the backing device (but we don't pass that
699 * error up anywhere).
702 __bch_submit_bbio(n
, b
->c
);
703 return n
== bio
? MAP_DONE
: MAP_CONTINUE
;
706 static void cache_lookup(struct closure
*cl
)
708 struct search
*s
= container_of(cl
, struct search
, iop
.cl
);
709 struct bio
*bio
= &s
->bio
.bio
;
711 int ret
= bch_btree_map_keys(&s
->op
, s
->iop
.c
,
712 &KEY(s
->iop
.inode
, bio
->bi_sector
, 0),
713 cache_lookup_fn
, MAP_END_KEY
);
715 continue_at(cl
, cache_lookup
, bcache_wq
);
720 /* Common code for the make_request functions */
722 static void request_endio(struct bio
*bio
, int error
)
724 struct closure
*cl
= bio
->bi_private
;
727 struct search
*s
= container_of(cl
, struct search
, cl
);
728 s
->iop
.error
= error
;
729 /* Only cache read errors are recoverable */
730 s
->recoverable
= false;
737 static void bio_complete(struct search
*s
)
740 int cpu
, rw
= bio_data_dir(s
->orig_bio
);
741 unsigned long duration
= jiffies
- s
->start_time
;
743 cpu
= part_stat_lock();
744 part_round_stats(cpu
, &s
->d
->disk
->part0
);
745 part_stat_add(cpu
, &s
->d
->disk
->part0
, ticks
[rw
], duration
);
748 trace_bcache_request_end(s
->d
, s
->orig_bio
);
749 bio_endio(s
->orig_bio
, s
->iop
.error
);
754 static void do_bio_hook(struct search
*s
)
756 struct bio
*bio
= &s
->bio
.bio
;
757 memcpy(bio
, s
->orig_bio
, sizeof(struct bio
));
759 bio
->bi_end_io
= request_endio
;
760 bio
->bi_private
= &s
->cl
;
761 atomic_set(&bio
->bi_cnt
, 3);
764 static void search_free(struct closure
*cl
)
766 struct search
*s
= container_of(cl
, struct search
, cl
);
772 if (s
->unaligned_bvec
)
773 mempool_free(s
->bio
.bio
.bi_io_vec
, s
->d
->unaligned_bvec
);
775 closure_debug_destroy(cl
);
776 mempool_free(s
, s
->d
->c
->search
);
779 static struct search
*search_alloc(struct bio
*bio
, struct bcache_device
*d
)
784 s
= mempool_alloc(d
->c
->search
, GFP_NOIO
);
785 memset(s
, 0, offsetof(struct search
, iop
.insert_keys
));
787 __closure_init(&s
->cl
, NULL
);
789 s
->iop
.inode
= d
->id
;
793 s
->iop
.write_point
= hash_long((unsigned long) current
, 16);
795 s
->write
= (bio
->bi_rw
& REQ_WRITE
) != 0;
796 s
->iop
.flush_journal
= (bio
->bi_rw
& (REQ_FLUSH
|REQ_FUA
)) != 0;
798 s
->start_time
= jiffies
;
801 if (bio
->bi_size
!= bio_segments(bio
) * PAGE_SIZE
) {
802 bv
= mempool_alloc(d
->unaligned_bvec
, GFP_NOIO
);
803 memcpy(bv
, bio_iovec(bio
),
804 sizeof(struct bio_vec
) * bio_segments(bio
));
806 s
->bio
.bio
.bi_io_vec
= bv
;
807 s
->unaligned_bvec
= 1;
815 static void cached_dev_bio_complete(struct closure
*cl
)
817 struct search
*s
= container_of(cl
, struct search
, cl
);
818 struct cached_dev
*dc
= container_of(s
->d
, struct cached_dev
, disk
);
826 static void cached_dev_cache_miss_done(struct closure
*cl
)
828 struct search
*s
= container_of(cl
, struct search
, cl
);
830 if (s
->iop
.replace_collision
)
831 bch_mark_cache_miss_collision(s
->iop
.c
, s
->d
);
837 bio_for_each_segment_all(bv
, s
->iop
.bio
, i
)
838 __free_page(bv
->bv_page
);
841 cached_dev_bio_complete(cl
);
844 static void cached_dev_read_error(struct closure
*cl
)
846 struct search
*s
= container_of(cl
, struct search
, cl
);
847 struct bio
*bio
= &s
->bio
.bio
;
851 if (s
->recoverable
) {
852 /* Retry from the backing device: */
853 trace_bcache_read_retry(s
->orig_bio
);
856 bv
= s
->bio
.bio
.bi_io_vec
;
858 s
->bio
.bio
.bi_io_vec
= bv
;
860 if (!s
->unaligned_bvec
)
861 bio_for_each_segment(bv
, s
->orig_bio
, i
)
862 bv
->bv_offset
= 0, bv
->bv_len
= PAGE_SIZE
;
864 memcpy(s
->bio
.bio
.bi_io_vec
,
865 bio_iovec(s
->orig_bio
),
866 sizeof(struct bio_vec
) *
867 bio_segments(s
->orig_bio
));
869 /* XXX: invalidate cache */
871 closure_bio_submit(bio
, cl
, s
->d
);
874 continue_at(cl
, cached_dev_cache_miss_done
, NULL
);
877 static void cached_dev_read_done(struct closure
*cl
)
879 struct search
*s
= container_of(cl
, struct search
, cl
);
880 struct cached_dev
*dc
= container_of(s
->d
, struct cached_dev
, disk
);
883 * We had a cache miss; cache_bio now contains data ready to be inserted
886 * First, we copy the data we just read from cache_bio's bounce buffers
887 * to the buffers the original bio pointed to:
891 bio_reset(s
->iop
.bio
);
892 s
->iop
.bio
->bi_sector
= s
->cache_miss
->bi_sector
;
893 s
->iop
.bio
->bi_bdev
= s
->cache_miss
->bi_bdev
;
894 s
->iop
.bio
->bi_size
= s
->insert_bio_sectors
<< 9;
895 bch_bio_map(s
->iop
.bio
, NULL
);
897 bio_copy_data(s
->cache_miss
, s
->iop
.bio
);
899 bio_put(s
->cache_miss
);
900 s
->cache_miss
= NULL
;
903 if (verify(dc
, &s
->bio
.bio
) && s
->recoverable
&& !s
->unaligned_bvec
)
904 bch_data_verify(dc
, s
->orig_bio
);
909 !test_bit(CACHE_SET_STOPPING
, &s
->iop
.c
->flags
)) {
910 BUG_ON(!s
->iop
.replace
);
911 closure_call(&s
->iop
.cl
, bch_data_insert
, NULL
, cl
);
914 continue_at(cl
, cached_dev_cache_miss_done
, NULL
);
917 static void cached_dev_read_done_bh(struct closure
*cl
)
919 struct search
*s
= container_of(cl
, struct search
, cl
);
920 struct cached_dev
*dc
= container_of(s
->d
, struct cached_dev
, disk
);
922 bch_mark_cache_accounting(s
->iop
.c
, s
->d
,
923 !s
->cache_miss
, s
->iop
.bypass
);
924 trace_bcache_read(s
->orig_bio
, !s
->cache_miss
, s
->iop
.bypass
);
927 continue_at_nobarrier(cl
, cached_dev_read_error
, bcache_wq
);
928 else if (s
->iop
.bio
|| verify(dc
, &s
->bio
.bio
))
929 continue_at_nobarrier(cl
, cached_dev_read_done
, bcache_wq
);
931 continue_at_nobarrier(cl
, cached_dev_bio_complete
, NULL
);
934 static int cached_dev_cache_miss(struct btree
*b
, struct search
*s
,
935 struct bio
*bio
, unsigned sectors
)
937 int ret
= MAP_CONTINUE
;
939 struct cached_dev
*dc
= container_of(s
->d
, struct cached_dev
, disk
);
940 struct bio
*miss
, *cache_bio
;
942 if (s
->cache_miss
|| s
->iop
.bypass
) {
943 miss
= bch_bio_split(bio
, sectors
, GFP_NOIO
, s
->d
->bio_split
);
944 ret
= miss
== bio
? MAP_DONE
: MAP_CONTINUE
;
948 if (!(bio
->bi_rw
& REQ_RAHEAD
) &&
949 !(bio
->bi_rw
& REQ_META
) &&
950 s
->iop
.c
->gc_stats
.in_use
< CUTOFF_CACHE_READA
)
951 reada
= min_t(sector_t
, dc
->readahead
>> 9,
952 bdev_sectors(bio
->bi_bdev
) - bio_end_sector(bio
));
954 s
->insert_bio_sectors
= min(sectors
, bio_sectors(bio
) + reada
);
956 s
->iop
.replace_key
= KEY(s
->iop
.inode
,
957 bio
->bi_sector
+ s
->insert_bio_sectors
,
958 s
->insert_bio_sectors
);
960 ret
= bch_btree_insert_check_key(b
, &s
->op
, &s
->iop
.replace_key
);
964 s
->iop
.replace
= true;
966 miss
= bch_bio_split(bio
, sectors
, GFP_NOIO
, s
->d
->bio_split
);
968 /* btree_search_recurse()'s btree iterator is no good anymore */
969 ret
= miss
== bio
? MAP_DONE
: -EINTR
;
971 cache_bio
= bio_alloc_bioset(GFP_NOWAIT
,
972 DIV_ROUND_UP(s
->insert_bio_sectors
, PAGE_SECTORS
),
977 cache_bio
->bi_sector
= miss
->bi_sector
;
978 cache_bio
->bi_bdev
= miss
->bi_bdev
;
979 cache_bio
->bi_size
= s
->insert_bio_sectors
<< 9;
981 cache_bio
->bi_end_io
= request_endio
;
982 cache_bio
->bi_private
= &s
->cl
;
984 bch_bio_map(cache_bio
, NULL
);
985 if (bio_alloc_pages(cache_bio
, __GFP_NOWARN
|GFP_NOIO
))
989 bch_mark_cache_readahead(s
->iop
.c
, s
->d
);
991 s
->cache_miss
= miss
;
992 s
->iop
.bio
= cache_bio
;
994 closure_bio_submit(cache_bio
, &s
->cl
, s
->d
);
1000 miss
->bi_end_io
= request_endio
;
1001 miss
->bi_private
= &s
->cl
;
1002 closure_bio_submit(miss
, &s
->cl
, s
->d
);
1006 static void cached_dev_read(struct cached_dev
*dc
, struct search
*s
)
1008 struct closure
*cl
= &s
->cl
;
1010 closure_call(&s
->iop
.cl
, cache_lookup
, NULL
, cl
);
1011 continue_at(cl
, cached_dev_read_done_bh
, NULL
);
1014 /* Process writes */
1016 static void cached_dev_write_complete(struct closure
*cl
)
1018 struct search
*s
= container_of(cl
, struct search
, cl
);
1019 struct cached_dev
*dc
= container_of(s
->d
, struct cached_dev
, disk
);
1021 up_read_non_owner(&dc
->writeback_lock
);
1022 cached_dev_bio_complete(cl
);
1025 static void cached_dev_write(struct cached_dev
*dc
, struct search
*s
)
1027 struct closure
*cl
= &s
->cl
;
1028 struct bio
*bio
= &s
->bio
.bio
;
1029 struct bkey start
= KEY(dc
->disk
.id
, bio
->bi_sector
, 0);
1030 struct bkey end
= KEY(dc
->disk
.id
, bio_end_sector(bio
), 0);
1032 bch_keybuf_check_overlapping(&s
->iop
.c
->moving_gc_keys
, &start
, &end
);
1034 down_read_non_owner(&dc
->writeback_lock
);
1035 if (bch_keybuf_check_overlapping(&dc
->writeback_keys
, &start
, &end
)) {
1037 * We overlap with some dirty data undergoing background
1038 * writeback, force this write to writeback
1040 s
->iop
.bypass
= false;
1041 s
->iop
.writeback
= true;
1045 * Discards aren't _required_ to do anything, so skipping if
1046 * check_overlapping returned true is ok
1048 * But check_overlapping drops dirty keys for which io hasn't started,
1049 * so we still want to call it.
1051 if (bio
->bi_rw
& REQ_DISCARD
)
1052 s
->iop
.bypass
= true;
1054 if (should_writeback(dc
, s
->orig_bio
,
1055 cache_mode(dc
, bio
),
1057 s
->iop
.bypass
= false;
1058 s
->iop
.writeback
= true;
1061 if (s
->iop
.bypass
) {
1062 s
->iop
.bio
= s
->orig_bio
;
1063 bio_get(s
->iop
.bio
);
1065 if (!(bio
->bi_rw
& REQ_DISCARD
) ||
1066 blk_queue_discard(bdev_get_queue(dc
->bdev
)))
1067 closure_bio_submit(bio
, cl
, s
->d
);
1068 } else if (s
->iop
.writeback
) {
1069 bch_writeback_add(dc
);
1072 if (bio
->bi_rw
& REQ_FLUSH
) {
1073 /* Also need to send a flush to the backing device */
1074 struct bio
*flush
= bio_alloc_bioset(GFP_NOIO
, 0,
1075 dc
->disk
.bio_split
);
1077 flush
->bi_rw
= WRITE_FLUSH
;
1078 flush
->bi_bdev
= bio
->bi_bdev
;
1079 flush
->bi_end_io
= request_endio
;
1080 flush
->bi_private
= cl
;
1082 closure_bio_submit(flush
, cl
, s
->d
);
1085 s
->iop
.bio
= bio_clone_bioset(bio
, GFP_NOIO
,
1086 dc
->disk
.bio_split
);
1088 closure_bio_submit(bio
, cl
, s
->d
);
1091 closure_call(&s
->iop
.cl
, bch_data_insert
, NULL
, cl
);
1092 continue_at(cl
, cached_dev_write_complete
, NULL
);
1095 static void cached_dev_nodata(struct closure
*cl
)
1097 struct search
*s
= container_of(cl
, struct search
, cl
);
1098 struct bio
*bio
= &s
->bio
.bio
;
1100 if (s
->iop
.flush_journal
)
1101 bch_journal_meta(s
->iop
.c
, cl
);
1103 /* If it's a flush, we send the flush to the backing device too */
1104 closure_bio_submit(bio
, cl
, s
->d
);
1106 continue_at(cl
, cached_dev_bio_complete
, NULL
);
1109 /* Cached devices - read & write stuff */
1111 static void cached_dev_make_request(struct request_queue
*q
, struct bio
*bio
)
1114 struct bcache_device
*d
= bio
->bi_bdev
->bd_disk
->private_data
;
1115 struct cached_dev
*dc
= container_of(d
, struct cached_dev
, disk
);
1116 int cpu
, rw
= bio_data_dir(bio
);
1118 cpu
= part_stat_lock();
1119 part_stat_inc(cpu
, &d
->disk
->part0
, ios
[rw
]);
1120 part_stat_add(cpu
, &d
->disk
->part0
, sectors
[rw
], bio_sectors(bio
));
1123 bio
->bi_bdev
= dc
->bdev
;
1124 bio
->bi_sector
+= dc
->sb
.data_offset
;
1126 if (cached_dev_get(dc
)) {
1127 s
= search_alloc(bio
, d
);
1128 trace_bcache_request_start(s
->d
, bio
);
1130 if (!bio
->bi_size
) {
1132 * can't call bch_journal_meta from under
1133 * generic_make_request
1135 continue_at_nobarrier(&s
->cl
,
1139 s
->iop
.bypass
= check_should_bypass(dc
, bio
);
1142 cached_dev_write(dc
, s
);
1144 cached_dev_read(dc
, s
);
1147 if ((bio
->bi_rw
& REQ_DISCARD
) &&
1148 !blk_queue_discard(bdev_get_queue(dc
->bdev
)))
1151 bch_generic_make_request(bio
, &d
->bio_split_hook
);
1155 static int cached_dev_ioctl(struct bcache_device
*d
, fmode_t mode
,
1156 unsigned int cmd
, unsigned long arg
)
1158 struct cached_dev
*dc
= container_of(d
, struct cached_dev
, disk
);
1159 return __blkdev_driver_ioctl(dc
->bdev
, mode
, cmd
, arg
);
1162 static int cached_dev_congested(void *data
, int bits
)
1164 struct bcache_device
*d
= data
;
1165 struct cached_dev
*dc
= container_of(d
, struct cached_dev
, disk
);
1166 struct request_queue
*q
= bdev_get_queue(dc
->bdev
);
1169 if (bdi_congested(&q
->backing_dev_info
, bits
))
1172 if (cached_dev_get(dc
)) {
1176 for_each_cache(ca
, d
->c
, i
) {
1177 q
= bdev_get_queue(ca
->bdev
);
1178 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
1187 void bch_cached_dev_request_init(struct cached_dev
*dc
)
1189 struct gendisk
*g
= dc
->disk
.disk
;
1191 g
->queue
->make_request_fn
= cached_dev_make_request
;
1192 g
->queue
->backing_dev_info
.congested_fn
= cached_dev_congested
;
1193 dc
->disk
.cache_miss
= cached_dev_cache_miss
;
1194 dc
->disk
.ioctl
= cached_dev_ioctl
;
1197 /* Flash backed devices */
1199 static int flash_dev_cache_miss(struct btree
*b
, struct search
*s
,
1200 struct bio
*bio
, unsigned sectors
)
1207 bio_for_each_segment(bv
, bio
, i
) {
1208 unsigned j
= min(bv
->bv_len
>> 9, sectors
);
1210 void *p
= kmap(bv
->bv_page
);
1211 memset(p
+ bv
->bv_offset
, 0, j
<< 9);
1212 kunmap(bv
->bv_page
);
1217 bio_advance(bio
, min(sectors
<< 9, bio
->bi_size
));
1222 return MAP_CONTINUE
;
1225 static void flash_dev_nodata(struct closure
*cl
)
1227 struct search
*s
= container_of(cl
, struct search
, cl
);
1229 if (s
->iop
.flush_journal
)
1230 bch_journal_meta(s
->iop
.c
, cl
);
1232 continue_at(cl
, search_free
, NULL
);
1235 static void flash_dev_make_request(struct request_queue
*q
, struct bio
*bio
)
1239 struct bcache_device
*d
= bio
->bi_bdev
->bd_disk
->private_data
;
1240 int cpu
, rw
= bio_data_dir(bio
);
1242 cpu
= part_stat_lock();
1243 part_stat_inc(cpu
, &d
->disk
->part0
, ios
[rw
]);
1244 part_stat_add(cpu
, &d
->disk
->part0
, sectors
[rw
], bio_sectors(bio
));
1247 s
= search_alloc(bio
, d
);
1251 trace_bcache_request_start(s
->d
, bio
);
1253 if (!bio
->bi_size
) {
1255 * can't call bch_journal_meta from under
1256 * generic_make_request
1258 continue_at_nobarrier(&s
->cl
,
1262 bch_keybuf_check_overlapping(&s
->iop
.c
->moving_gc_keys
,
1263 &KEY(d
->id
, bio
->bi_sector
, 0),
1264 &KEY(d
->id
, bio_end_sector(bio
), 0));
1266 s
->iop
.bypass
= (bio
->bi_rw
& REQ_DISCARD
) != 0;
1267 s
->iop
.writeback
= true;
1270 closure_call(&s
->iop
.cl
, bch_data_insert
, NULL
, cl
);
1272 closure_call(&s
->iop
.cl
, cache_lookup
, NULL
, cl
);
1275 continue_at(cl
, search_free
, NULL
);
1278 static int flash_dev_ioctl(struct bcache_device
*d
, fmode_t mode
,
1279 unsigned int cmd
, unsigned long arg
)
1284 static int flash_dev_congested(void *data
, int bits
)
1286 struct bcache_device
*d
= data
;
1287 struct request_queue
*q
;
1292 for_each_cache(ca
, d
->c
, i
) {
1293 q
= bdev_get_queue(ca
->bdev
);
1294 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
1300 void bch_flash_dev_request_init(struct bcache_device
*d
)
1302 struct gendisk
*g
= d
->disk
;
1304 g
->queue
->make_request_fn
= flash_dev_make_request
;
1305 g
->queue
->backing_dev_info
.congested_fn
= flash_dev_congested
;
1306 d
->cache_miss
= flash_dev_cache_miss
;
1307 d
->ioctl
= flash_dev_ioctl
;
1310 void bch_request_exit(void)
1312 #ifdef CONFIG_CGROUP_BCACHE
1313 cgroup_unload_subsys(&bcache_subsys
);
1315 if (bch_search_cache
)
1316 kmem_cache_destroy(bch_search_cache
);
1319 int __init
bch_request_init(void)
1321 bch_search_cache
= KMEM_CACHE(search
, 0);
1322 if (!bch_search_cache
)
1325 #ifdef CONFIG_CGROUP_BCACHE
1326 cgroup_load_subsys(&bcache_subsys
);
1327 init_bch_cgroup(&bcache_default_cgroup
);
1329 cgroup_add_cftypes(&bcache_subsys
, bch_files
);