1cafe5635SKent Overstreet /* 2cafe5635SKent Overstreet * Main bcache entry point - handle a read or a write request and decide what to 3cafe5635SKent Overstreet * do with it; the make_request functions are called by the block layer. 4cafe5635SKent Overstreet * 5cafe5635SKent Overstreet * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> 6cafe5635SKent Overstreet * Copyright 2012 Google, Inc. 7cafe5635SKent Overstreet */ 8cafe5635SKent Overstreet 9cafe5635SKent Overstreet #include "bcache.h" 10cafe5635SKent Overstreet #include "btree.h" 11cafe5635SKent Overstreet #include "debug.h" 12cafe5635SKent Overstreet #include "request.h" 13279afbadSKent Overstreet #include "writeback.h" 14cafe5635SKent Overstreet 15cafe5635SKent Overstreet #include <linux/cgroup.h> 16cafe5635SKent Overstreet #include <linux/module.h> 17cafe5635SKent Overstreet #include <linux/hash.h> 18cafe5635SKent Overstreet #include <linux/random.h> 19cafe5635SKent Overstreet #include "blk-cgroup.h" 20cafe5635SKent Overstreet 21cafe5635SKent Overstreet #include <trace/events/bcache.h> 22cafe5635SKent Overstreet 23cafe5635SKent Overstreet #define CUTOFF_CACHE_ADD 95 24cafe5635SKent Overstreet #define CUTOFF_CACHE_READA 90 25cafe5635SKent Overstreet 26cafe5635SKent Overstreet struct kmem_cache *bch_search_cache; 27cafe5635SKent Overstreet 28a34a8bfdSKent Overstreet static void bch_data_insert_start(struct closure *); 29a34a8bfdSKent Overstreet 30cafe5635SKent Overstreet /* Cgroup interface */ 31cafe5635SKent Overstreet 32cafe5635SKent Overstreet #ifdef CONFIG_CGROUP_BCACHE 33cafe5635SKent Overstreet static struct bch_cgroup bcache_default_cgroup = { .cache_mode = -1 }; 34cafe5635SKent Overstreet 35cafe5635SKent Overstreet static struct bch_cgroup *cgroup_to_bcache(struct cgroup *cgroup) 36cafe5635SKent Overstreet { 37cafe5635SKent Overstreet struct cgroup_subsys_state *css; 38cafe5635SKent Overstreet return cgroup && 39cafe5635SKent Overstreet (css = cgroup_subsys_state(cgroup, bcache_subsys_id)) 40cafe5635SKent Overstreet ? container_of(css, struct bch_cgroup, css) 41cafe5635SKent Overstreet : &bcache_default_cgroup; 42cafe5635SKent Overstreet } 43cafe5635SKent Overstreet 44cafe5635SKent Overstreet struct bch_cgroup *bch_bio_to_cgroup(struct bio *bio) 45cafe5635SKent Overstreet { 46cafe5635SKent Overstreet struct cgroup_subsys_state *css = bio->bi_css 47cafe5635SKent Overstreet ? cgroup_subsys_state(bio->bi_css->cgroup, bcache_subsys_id) 48cafe5635SKent Overstreet : task_subsys_state(current, bcache_subsys_id); 49cafe5635SKent Overstreet 50cafe5635SKent Overstreet return css 51cafe5635SKent Overstreet ? container_of(css, struct bch_cgroup, css) 52cafe5635SKent Overstreet : &bcache_default_cgroup; 53cafe5635SKent Overstreet } 54cafe5635SKent Overstreet 55cafe5635SKent Overstreet static ssize_t cache_mode_read(struct cgroup *cgrp, struct cftype *cft, 56cafe5635SKent Overstreet struct file *file, 57cafe5635SKent Overstreet char __user *buf, size_t nbytes, loff_t *ppos) 58cafe5635SKent Overstreet { 59cafe5635SKent Overstreet char tmp[1024]; 60169ef1cfSKent Overstreet int len = bch_snprint_string_list(tmp, PAGE_SIZE, bch_cache_modes, 61cafe5635SKent Overstreet cgroup_to_bcache(cgrp)->cache_mode + 1); 62cafe5635SKent Overstreet 63cafe5635SKent Overstreet if (len < 0) 64cafe5635SKent Overstreet return len; 65cafe5635SKent Overstreet 66cafe5635SKent Overstreet return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); 67cafe5635SKent Overstreet } 68cafe5635SKent Overstreet 69cafe5635SKent Overstreet static int cache_mode_write(struct cgroup *cgrp, struct cftype *cft, 70cafe5635SKent Overstreet const char *buf) 71cafe5635SKent Overstreet { 72169ef1cfSKent Overstreet int v = bch_read_string_list(buf, bch_cache_modes); 73cafe5635SKent Overstreet if (v < 0) 74cafe5635SKent Overstreet return v; 75cafe5635SKent Overstreet 76cafe5635SKent Overstreet cgroup_to_bcache(cgrp)->cache_mode = v - 1; 77cafe5635SKent Overstreet return 0; 78cafe5635SKent Overstreet } 79cafe5635SKent Overstreet 80cafe5635SKent Overstreet static u64 bch_verify_read(struct cgroup *cgrp, struct cftype *cft) 81cafe5635SKent Overstreet { 82cafe5635SKent Overstreet return cgroup_to_bcache(cgrp)->verify; 83cafe5635SKent Overstreet } 84cafe5635SKent Overstreet 85cafe5635SKent Overstreet static int bch_verify_write(struct cgroup *cgrp, struct cftype *cft, u64 val) 86cafe5635SKent Overstreet { 87cafe5635SKent Overstreet cgroup_to_bcache(cgrp)->verify = val; 88cafe5635SKent Overstreet return 0; 89cafe5635SKent Overstreet } 90cafe5635SKent Overstreet 91cafe5635SKent Overstreet static u64 bch_cache_hits_read(struct cgroup *cgrp, struct cftype *cft) 92cafe5635SKent Overstreet { 93cafe5635SKent Overstreet struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); 94cafe5635SKent Overstreet return atomic_read(&bcachecg->stats.cache_hits); 95cafe5635SKent Overstreet } 96cafe5635SKent Overstreet 97cafe5635SKent Overstreet static u64 bch_cache_misses_read(struct cgroup *cgrp, struct cftype *cft) 98cafe5635SKent Overstreet { 99cafe5635SKent Overstreet struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); 100cafe5635SKent Overstreet return atomic_read(&bcachecg->stats.cache_misses); 101cafe5635SKent Overstreet } 102cafe5635SKent Overstreet 103cafe5635SKent Overstreet static u64 bch_cache_bypass_hits_read(struct cgroup *cgrp, 104cafe5635SKent Overstreet struct cftype *cft) 105cafe5635SKent Overstreet { 106cafe5635SKent Overstreet struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); 107cafe5635SKent Overstreet return atomic_read(&bcachecg->stats.cache_bypass_hits); 108cafe5635SKent Overstreet } 109cafe5635SKent Overstreet 110cafe5635SKent Overstreet static u64 bch_cache_bypass_misses_read(struct cgroup *cgrp, 111cafe5635SKent Overstreet struct cftype *cft) 112cafe5635SKent Overstreet { 113cafe5635SKent Overstreet struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); 114cafe5635SKent Overstreet return atomic_read(&bcachecg->stats.cache_bypass_misses); 115cafe5635SKent Overstreet } 116cafe5635SKent Overstreet 117cafe5635SKent Overstreet static struct cftype bch_files[] = { 118cafe5635SKent Overstreet { 119cafe5635SKent Overstreet .name = "cache_mode", 120cafe5635SKent Overstreet .read = cache_mode_read, 121cafe5635SKent Overstreet .write_string = cache_mode_write, 122cafe5635SKent Overstreet }, 123cafe5635SKent Overstreet { 124cafe5635SKent Overstreet .name = "verify", 125cafe5635SKent Overstreet .read_u64 = bch_verify_read, 126cafe5635SKent Overstreet .write_u64 = bch_verify_write, 127cafe5635SKent Overstreet }, 128cafe5635SKent Overstreet { 129cafe5635SKent Overstreet .name = "cache_hits", 130cafe5635SKent Overstreet .read_u64 = bch_cache_hits_read, 131cafe5635SKent Overstreet }, 132cafe5635SKent Overstreet { 133cafe5635SKent Overstreet .name = "cache_misses", 134cafe5635SKent Overstreet .read_u64 = bch_cache_misses_read, 135cafe5635SKent Overstreet }, 136cafe5635SKent Overstreet { 137cafe5635SKent Overstreet .name = "cache_bypass_hits", 138cafe5635SKent Overstreet .read_u64 = bch_cache_bypass_hits_read, 139cafe5635SKent Overstreet }, 140cafe5635SKent Overstreet { 141cafe5635SKent Overstreet .name = "cache_bypass_misses", 142cafe5635SKent Overstreet .read_u64 = bch_cache_bypass_misses_read, 143cafe5635SKent Overstreet }, 144cafe5635SKent Overstreet { } /* terminate */ 145cafe5635SKent Overstreet }; 146cafe5635SKent Overstreet 147cafe5635SKent Overstreet static void init_bch_cgroup(struct bch_cgroup *cg) 148cafe5635SKent Overstreet { 149cafe5635SKent Overstreet cg->cache_mode = -1; 150cafe5635SKent Overstreet } 151cafe5635SKent Overstreet 152cafe5635SKent Overstreet static struct cgroup_subsys_state *bcachecg_create(struct cgroup *cgroup) 153cafe5635SKent Overstreet { 154cafe5635SKent Overstreet struct bch_cgroup *cg; 155cafe5635SKent Overstreet 156cafe5635SKent Overstreet cg = kzalloc(sizeof(*cg), GFP_KERNEL); 157cafe5635SKent Overstreet if (!cg) 158cafe5635SKent Overstreet return ERR_PTR(-ENOMEM); 159cafe5635SKent Overstreet init_bch_cgroup(cg); 160cafe5635SKent Overstreet return &cg->css; 161cafe5635SKent Overstreet } 162cafe5635SKent Overstreet 163cafe5635SKent Overstreet static void bcachecg_destroy(struct cgroup *cgroup) 164cafe5635SKent Overstreet { 165cafe5635SKent Overstreet struct bch_cgroup *cg = cgroup_to_bcache(cgroup); 166cafe5635SKent Overstreet free_css_id(&bcache_subsys, &cg->css); 167cafe5635SKent Overstreet kfree(cg); 168cafe5635SKent Overstreet } 169cafe5635SKent Overstreet 170cafe5635SKent Overstreet struct cgroup_subsys bcache_subsys = { 171cafe5635SKent Overstreet .create = bcachecg_create, 172cafe5635SKent Overstreet .destroy = bcachecg_destroy, 173cafe5635SKent Overstreet .subsys_id = bcache_subsys_id, 174cafe5635SKent Overstreet .name = "bcache", 175cafe5635SKent Overstreet .module = THIS_MODULE, 176cafe5635SKent Overstreet }; 177cafe5635SKent Overstreet EXPORT_SYMBOL_GPL(bcache_subsys); 178cafe5635SKent Overstreet #endif 179cafe5635SKent Overstreet 180cafe5635SKent Overstreet static unsigned cache_mode(struct cached_dev *dc, struct bio *bio) 181cafe5635SKent Overstreet { 182cafe5635SKent Overstreet #ifdef CONFIG_CGROUP_BCACHE 183cafe5635SKent Overstreet int r = bch_bio_to_cgroup(bio)->cache_mode; 184cafe5635SKent Overstreet if (r >= 0) 185cafe5635SKent Overstreet return r; 186cafe5635SKent Overstreet #endif 187cafe5635SKent Overstreet return BDEV_CACHE_MODE(&dc->sb); 188cafe5635SKent Overstreet } 189cafe5635SKent Overstreet 190cafe5635SKent Overstreet static bool verify(struct cached_dev *dc, struct bio *bio) 191cafe5635SKent Overstreet { 192cafe5635SKent Overstreet #ifdef CONFIG_CGROUP_BCACHE 193cafe5635SKent Overstreet if (bch_bio_to_cgroup(bio)->verify) 194cafe5635SKent Overstreet return true; 195cafe5635SKent Overstreet #endif 196cafe5635SKent Overstreet return dc->verify; 197cafe5635SKent Overstreet } 198cafe5635SKent Overstreet 199cafe5635SKent Overstreet static void bio_csum(struct bio *bio, struct bkey *k) 200cafe5635SKent Overstreet { 201cafe5635SKent Overstreet struct bio_vec *bv; 202cafe5635SKent Overstreet uint64_t csum = 0; 203cafe5635SKent Overstreet int i; 204cafe5635SKent Overstreet 205cafe5635SKent Overstreet bio_for_each_segment(bv, bio, i) { 206cafe5635SKent Overstreet void *d = kmap(bv->bv_page) + bv->bv_offset; 207169ef1cfSKent Overstreet csum = bch_crc64_update(csum, d, bv->bv_len); 208cafe5635SKent Overstreet kunmap(bv->bv_page); 209cafe5635SKent Overstreet } 210cafe5635SKent Overstreet 211cafe5635SKent Overstreet k->ptr[KEY_PTRS(k)] = csum & (~0ULL >> 1); 212cafe5635SKent Overstreet } 213cafe5635SKent Overstreet 214cafe5635SKent Overstreet /* Insert data into cache */ 215cafe5635SKent Overstreet 216a34a8bfdSKent Overstreet static void bch_data_insert_keys(struct closure *cl) 217cafe5635SKent Overstreet { 218220bb38cSKent Overstreet struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 219c18536a7SKent Overstreet atomic_t *journal_ref = NULL; 220220bb38cSKent Overstreet struct bkey *replace_key = op->replace ? &op->replace_key : NULL; 2216054c6d4SKent Overstreet int ret; 222cafe5635SKent Overstreet 223a34a8bfdSKent Overstreet /* 224a34a8bfdSKent Overstreet * If we're looping, might already be waiting on 225a34a8bfdSKent Overstreet * another journal write - can't wait on more than one journal write at 226a34a8bfdSKent Overstreet * a time 227a34a8bfdSKent Overstreet * 228a34a8bfdSKent Overstreet * XXX: this looks wrong 229a34a8bfdSKent Overstreet */ 230a34a8bfdSKent Overstreet #if 0 231a34a8bfdSKent Overstreet while (atomic_read(&s->cl.remaining) & CLOSURE_WAITING) 232a34a8bfdSKent Overstreet closure_sync(&s->cl); 233a34a8bfdSKent Overstreet #endif 234cafe5635SKent Overstreet 235220bb38cSKent Overstreet if (!op->replace) 236220bb38cSKent Overstreet journal_ref = bch_journal(op->c, &op->insert_keys, 237220bb38cSKent Overstreet op->flush_journal ? cl : NULL); 238cafe5635SKent Overstreet 239220bb38cSKent Overstreet ret = bch_btree_insert(op->c, &op->insert_keys, 2406054c6d4SKent Overstreet journal_ref, replace_key); 2416054c6d4SKent Overstreet if (ret == -ESRCH) { 242220bb38cSKent Overstreet op->replace_collision = true; 2436054c6d4SKent Overstreet } else if (ret) { 244220bb38cSKent Overstreet op->error = -ENOMEM; 245220bb38cSKent Overstreet op->insert_data_done = true; 246cafe5635SKent Overstreet } 247cafe5635SKent Overstreet 248c18536a7SKent Overstreet if (journal_ref) 249c18536a7SKent Overstreet atomic_dec_bug(journal_ref); 250a34a8bfdSKent Overstreet 251220bb38cSKent Overstreet if (!op->insert_data_done) 252a34a8bfdSKent Overstreet continue_at(cl, bch_data_insert_start, bcache_wq); 253a34a8bfdSKent Overstreet 254220bb38cSKent Overstreet bch_keylist_free(&op->insert_keys); 255a34a8bfdSKent Overstreet closure_return(cl); 256cafe5635SKent Overstreet } 257cafe5635SKent Overstreet 258a34a8bfdSKent Overstreet static void bch_data_invalidate(struct closure *cl) 259a34a8bfdSKent Overstreet { 260220bb38cSKent Overstreet struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 261220bb38cSKent Overstreet struct bio *bio = op->bio; 262a34a8bfdSKent Overstreet 263a34a8bfdSKent Overstreet pr_debug("invalidating %i sectors from %llu", 264a34a8bfdSKent Overstreet bio_sectors(bio), (uint64_t) bio->bi_sector); 265a34a8bfdSKent Overstreet 266a34a8bfdSKent Overstreet while (bio_sectors(bio)) { 26781ab4190SKent Overstreet unsigned sectors = min(bio_sectors(bio), 26881ab4190SKent Overstreet 1U << (KEY_SIZE_BITS - 1)); 269a34a8bfdSKent Overstreet 270220bb38cSKent Overstreet if (bch_keylist_realloc(&op->insert_keys, 0, op->c)) 271a34a8bfdSKent Overstreet goto out; 272a34a8bfdSKent Overstreet 27381ab4190SKent Overstreet bio->bi_sector += sectors; 27481ab4190SKent Overstreet bio->bi_size -= sectors << 9; 275a34a8bfdSKent Overstreet 276220bb38cSKent Overstreet bch_keylist_add(&op->insert_keys, 27781ab4190SKent Overstreet &KEY(op->inode, bio->bi_sector, sectors)); 278a34a8bfdSKent Overstreet } 279a34a8bfdSKent Overstreet 280220bb38cSKent Overstreet op->insert_data_done = true; 281a34a8bfdSKent Overstreet bio_put(bio); 282a34a8bfdSKent Overstreet out: 283a34a8bfdSKent Overstreet continue_at(cl, bch_data_insert_keys, bcache_wq); 284a34a8bfdSKent Overstreet } 285a34a8bfdSKent Overstreet 286a34a8bfdSKent Overstreet static void bch_data_insert_error(struct closure *cl) 287cafe5635SKent Overstreet { 288220bb38cSKent Overstreet struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 289cafe5635SKent Overstreet 290cafe5635SKent Overstreet /* 291cafe5635SKent Overstreet * Our data write just errored, which means we've got a bunch of keys to 292cafe5635SKent Overstreet * insert that point to data that wasn't succesfully written. 293cafe5635SKent Overstreet * 294cafe5635SKent Overstreet * We don't have to insert those keys but we still have to invalidate 295cafe5635SKent Overstreet * that region of the cache - so, if we just strip off all the pointers 296cafe5635SKent Overstreet * from the keys we'll accomplish just that. 297cafe5635SKent Overstreet */ 298cafe5635SKent Overstreet 299220bb38cSKent Overstreet struct bkey *src = op->insert_keys.keys, *dst = op->insert_keys.keys; 300cafe5635SKent Overstreet 301220bb38cSKent Overstreet while (src != op->insert_keys.top) { 302cafe5635SKent Overstreet struct bkey *n = bkey_next(src); 303cafe5635SKent Overstreet 304cafe5635SKent Overstreet SET_KEY_PTRS(src, 0); 305c2f95ae2SKent Overstreet memmove(dst, src, bkey_bytes(src)); 306cafe5635SKent Overstreet 307cafe5635SKent Overstreet dst = bkey_next(dst); 308cafe5635SKent Overstreet src = n; 309cafe5635SKent Overstreet } 310cafe5635SKent Overstreet 311220bb38cSKent Overstreet op->insert_keys.top = dst; 312cafe5635SKent Overstreet 313a34a8bfdSKent Overstreet bch_data_insert_keys(cl); 314cafe5635SKent Overstreet } 315cafe5635SKent Overstreet 316a34a8bfdSKent Overstreet static void bch_data_insert_endio(struct bio *bio, int error) 317cafe5635SKent Overstreet { 318cafe5635SKent Overstreet struct closure *cl = bio->bi_private; 319220bb38cSKent Overstreet struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 320cafe5635SKent Overstreet 321cafe5635SKent Overstreet if (error) { 322cafe5635SKent Overstreet /* TODO: We could try to recover from this. */ 323220bb38cSKent Overstreet if (op->writeback) 324220bb38cSKent Overstreet op->error = error; 325220bb38cSKent Overstreet else if (!op->replace) 326a34a8bfdSKent Overstreet set_closure_fn(cl, bch_data_insert_error, bcache_wq); 327cafe5635SKent Overstreet else 328cafe5635SKent Overstreet set_closure_fn(cl, NULL, NULL); 329cafe5635SKent Overstreet } 330cafe5635SKent Overstreet 331220bb38cSKent Overstreet bch_bbio_endio(op->c, bio, error, "writing data to cache"); 332cafe5635SKent Overstreet } 333cafe5635SKent Overstreet 334a34a8bfdSKent Overstreet static void bch_data_insert_start(struct closure *cl) 335cafe5635SKent Overstreet { 336220bb38cSKent Overstreet struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 337220bb38cSKent Overstreet struct bio *bio = op->bio, *n; 338cafe5635SKent Overstreet 339220bb38cSKent Overstreet if (op->bypass) 340a34a8bfdSKent Overstreet return bch_data_invalidate(cl); 341cafe5635SKent Overstreet 342220bb38cSKent Overstreet if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0) { 343220bb38cSKent Overstreet set_gc_sectors(op->c); 344220bb38cSKent Overstreet wake_up_gc(op->c); 345cafe5635SKent Overstreet } 346cafe5635SKent Overstreet 34754d12f2bSKent Overstreet /* 34854d12f2bSKent Overstreet * Journal writes are marked REQ_FLUSH; if the original write was a 34954d12f2bSKent Overstreet * flush, it'll wait on the journal write. 35054d12f2bSKent Overstreet */ 35154d12f2bSKent Overstreet bio->bi_rw &= ~(REQ_FLUSH|REQ_FUA); 35254d12f2bSKent Overstreet 353cafe5635SKent Overstreet do { 354cafe5635SKent Overstreet unsigned i; 355cafe5635SKent Overstreet struct bkey *k; 356220bb38cSKent Overstreet struct bio_set *split = op->c->bio_split; 357cafe5635SKent Overstreet 358cafe5635SKent Overstreet /* 1 for the device pointer and 1 for the chksum */ 359220bb38cSKent Overstreet if (bch_keylist_realloc(&op->insert_keys, 360220bb38cSKent Overstreet 1 + (op->csum ? 1 : 0), 361220bb38cSKent Overstreet op->c)) 362a34a8bfdSKent Overstreet continue_at(cl, bch_data_insert_keys, bcache_wq); 363cafe5635SKent Overstreet 364220bb38cSKent Overstreet k = op->insert_keys.top; 365cafe5635SKent Overstreet bkey_init(k); 366220bb38cSKent Overstreet SET_KEY_INODE(k, op->inode); 367cafe5635SKent Overstreet SET_KEY_OFFSET(k, bio->bi_sector); 368cafe5635SKent Overstreet 3692599b53bSKent Overstreet if (!bch_alloc_sectors(op->c, k, bio_sectors(bio), 3702599b53bSKent Overstreet op->write_point, op->write_prio, 3712599b53bSKent Overstreet op->writeback)) 372cafe5635SKent Overstreet goto err; 373cafe5635SKent Overstreet 374cafe5635SKent Overstreet n = bch_bio_split(bio, KEY_SIZE(k), GFP_NOIO, split); 375cafe5635SKent Overstreet 376a34a8bfdSKent Overstreet n->bi_end_io = bch_data_insert_endio; 377cafe5635SKent Overstreet n->bi_private = cl; 378cafe5635SKent Overstreet 379220bb38cSKent Overstreet if (op->writeback) { 380cafe5635SKent Overstreet SET_KEY_DIRTY(k, true); 381cafe5635SKent Overstreet 382cafe5635SKent Overstreet for (i = 0; i < KEY_PTRS(k); i++) 383220bb38cSKent Overstreet SET_GC_MARK(PTR_BUCKET(op->c, k, i), 384cafe5635SKent Overstreet GC_MARK_DIRTY); 385cafe5635SKent Overstreet } 386cafe5635SKent Overstreet 387220bb38cSKent Overstreet SET_KEY_CSUM(k, op->csum); 388cafe5635SKent Overstreet if (KEY_CSUM(k)) 389cafe5635SKent Overstreet bio_csum(n, k); 390cafe5635SKent Overstreet 391c37511b8SKent Overstreet trace_bcache_cache_insert(k); 392220bb38cSKent Overstreet bch_keylist_push(&op->insert_keys); 393cafe5635SKent Overstreet 394cafe5635SKent Overstreet n->bi_rw |= REQ_WRITE; 395220bb38cSKent Overstreet bch_submit_bbio(n, op->c, k, 0); 396cafe5635SKent Overstreet } while (n != bio); 397cafe5635SKent Overstreet 398220bb38cSKent Overstreet op->insert_data_done = true; 399a34a8bfdSKent Overstreet continue_at(cl, bch_data_insert_keys, bcache_wq); 400cafe5635SKent Overstreet err: 401cafe5635SKent Overstreet /* bch_alloc_sectors() blocks if s->writeback = true */ 402220bb38cSKent Overstreet BUG_ON(op->writeback); 403cafe5635SKent Overstreet 404cafe5635SKent Overstreet /* 405cafe5635SKent Overstreet * But if it's not a writeback write we'd rather just bail out if 406cafe5635SKent Overstreet * there aren't any buckets ready to write to - it might take awhile and 407cafe5635SKent Overstreet * we might be starving btree writes for gc or something. 408cafe5635SKent Overstreet */ 409cafe5635SKent Overstreet 410220bb38cSKent Overstreet if (!op->replace) { 411cafe5635SKent Overstreet /* 412cafe5635SKent Overstreet * Writethrough write: We can't complete the write until we've 413cafe5635SKent Overstreet * updated the index. But we don't want to delay the write while 414cafe5635SKent Overstreet * we wait for buckets to be freed up, so just invalidate the 415cafe5635SKent Overstreet * rest of the write. 416cafe5635SKent Overstreet */ 417220bb38cSKent Overstreet op->bypass = true; 418a34a8bfdSKent Overstreet return bch_data_invalidate(cl); 419cafe5635SKent Overstreet } else { 420cafe5635SKent Overstreet /* 421cafe5635SKent Overstreet * From a cache miss, we can just insert the keys for the data 422cafe5635SKent Overstreet * we have written or bail out if we didn't do anything. 423cafe5635SKent Overstreet */ 424220bb38cSKent Overstreet op->insert_data_done = true; 425cafe5635SKent Overstreet bio_put(bio); 426cafe5635SKent Overstreet 427220bb38cSKent Overstreet if (!bch_keylist_empty(&op->insert_keys)) 428a34a8bfdSKent Overstreet continue_at(cl, bch_data_insert_keys, bcache_wq); 429cafe5635SKent Overstreet else 430cafe5635SKent Overstreet closure_return(cl); 431cafe5635SKent Overstreet } 432cafe5635SKent Overstreet } 433cafe5635SKent Overstreet 434cafe5635SKent Overstreet /** 435a34a8bfdSKent Overstreet * bch_data_insert - stick some data in the cache 436cafe5635SKent Overstreet * 437cafe5635SKent Overstreet * This is the starting point for any data to end up in a cache device; it could 438cafe5635SKent Overstreet * be from a normal write, or a writeback write, or a write to a flash only 439cafe5635SKent Overstreet * volume - it's also used by the moving garbage collector to compact data in 440cafe5635SKent Overstreet * mostly empty buckets. 441cafe5635SKent Overstreet * 442cafe5635SKent Overstreet * It first writes the data to the cache, creating a list of keys to be inserted 443cafe5635SKent Overstreet * (if the data had to be fragmented there will be multiple keys); after the 444cafe5635SKent Overstreet * data is written it calls bch_journal, and after the keys have been added to 445cafe5635SKent Overstreet * the next journal write they're inserted into the btree. 446cafe5635SKent Overstreet * 447c18536a7SKent Overstreet * It inserts the data in s->cache_bio; bi_sector is used for the key offset, 448cafe5635SKent Overstreet * and op->inode is used for the key inode. 449cafe5635SKent Overstreet * 450c18536a7SKent Overstreet * If s->bypass is true, instead of inserting the data it invalidates the 451c18536a7SKent Overstreet * region of the cache represented by s->cache_bio and op->inode. 452cafe5635SKent Overstreet */ 453a34a8bfdSKent Overstreet void bch_data_insert(struct closure *cl) 454cafe5635SKent Overstreet { 455220bb38cSKent Overstreet struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); 456cafe5635SKent Overstreet 457220bb38cSKent Overstreet trace_bcache_write(op->bio, op->writeback, op->bypass); 458220bb38cSKent Overstreet 459220bb38cSKent Overstreet bch_keylist_init(&op->insert_keys); 460220bb38cSKent Overstreet bio_get(op->bio); 461a34a8bfdSKent Overstreet bch_data_insert_start(cl); 462cafe5635SKent Overstreet } 463cafe5635SKent Overstreet 464220bb38cSKent Overstreet /* Congested? */ 465cafe5635SKent Overstreet 46684f0db03SKent Overstreet unsigned bch_get_congested(struct cache_set *c) 46784f0db03SKent Overstreet { 46884f0db03SKent Overstreet int i; 46984f0db03SKent Overstreet long rand; 47084f0db03SKent Overstreet 47184f0db03SKent Overstreet if (!c->congested_read_threshold_us && 47284f0db03SKent Overstreet !c->congested_write_threshold_us) 47384f0db03SKent Overstreet return 0; 47484f0db03SKent Overstreet 47584f0db03SKent Overstreet i = (local_clock_us() - c->congested_last_us) / 1024; 47684f0db03SKent Overstreet if (i < 0) 47784f0db03SKent Overstreet return 0; 47884f0db03SKent Overstreet 47984f0db03SKent Overstreet i += atomic_read(&c->congested); 48084f0db03SKent Overstreet if (i >= 0) 48184f0db03SKent Overstreet return 0; 48284f0db03SKent Overstreet 48384f0db03SKent Overstreet i += CONGESTED_MAX; 48484f0db03SKent Overstreet 48584f0db03SKent Overstreet if (i > 0) 48684f0db03SKent Overstreet i = fract_exp_two(i, 6); 48784f0db03SKent Overstreet 48884f0db03SKent Overstreet rand = get_random_int(); 48984f0db03SKent Overstreet i -= bitmap_weight(&rand, BITS_PER_LONG); 49084f0db03SKent Overstreet 49184f0db03SKent Overstreet return i > 0 ? i : 1; 49284f0db03SKent Overstreet } 49384f0db03SKent Overstreet 49484f0db03SKent Overstreet static void add_sequential(struct task_struct *t) 49584f0db03SKent Overstreet { 49684f0db03SKent Overstreet ewma_add(t->sequential_io_avg, 49784f0db03SKent Overstreet t->sequential_io, 8, 0); 49884f0db03SKent Overstreet 49984f0db03SKent Overstreet t->sequential_io = 0; 50084f0db03SKent Overstreet } 50184f0db03SKent Overstreet 50284f0db03SKent Overstreet static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k) 50384f0db03SKent Overstreet { 50484f0db03SKent Overstreet return &dc->io_hash[hash_64(k, RECENT_IO_BITS)]; 50584f0db03SKent Overstreet } 50684f0db03SKent Overstreet 507220bb38cSKent Overstreet static bool check_should_bypass(struct cached_dev *dc, struct bio *bio) 50884f0db03SKent Overstreet { 509220bb38cSKent Overstreet struct cache_set *c = dc->disk.c; 51084f0db03SKent Overstreet unsigned mode = cache_mode(dc, bio); 51184f0db03SKent Overstreet unsigned sectors, congested = bch_get_congested(c); 512220bb38cSKent Overstreet struct task_struct *task = current; 5138aee1220SKent Overstreet struct io *i; 51484f0db03SKent Overstreet 51584f0db03SKent Overstreet if (atomic_read(&dc->disk.detaching) || 51684f0db03SKent Overstreet c->gc_stats.in_use > CUTOFF_CACHE_ADD || 51784f0db03SKent Overstreet (bio->bi_rw & REQ_DISCARD)) 51884f0db03SKent Overstreet goto skip; 51984f0db03SKent Overstreet 52084f0db03SKent Overstreet if (mode == CACHE_MODE_NONE || 52184f0db03SKent Overstreet (mode == CACHE_MODE_WRITEAROUND && 52284f0db03SKent Overstreet (bio->bi_rw & REQ_WRITE))) 52384f0db03SKent Overstreet goto skip; 52484f0db03SKent Overstreet 52584f0db03SKent Overstreet if (bio->bi_sector & (c->sb.block_size - 1) || 52684f0db03SKent Overstreet bio_sectors(bio) & (c->sb.block_size - 1)) { 52784f0db03SKent Overstreet pr_debug("skipping unaligned io"); 52884f0db03SKent Overstreet goto skip; 52984f0db03SKent Overstreet } 53084f0db03SKent Overstreet 53184f0db03SKent Overstreet if (!congested && !dc->sequential_cutoff) 53284f0db03SKent Overstreet goto rescale; 53384f0db03SKent Overstreet 53484f0db03SKent Overstreet if (!congested && 53584f0db03SKent Overstreet mode == CACHE_MODE_WRITEBACK && 53684f0db03SKent Overstreet (bio->bi_rw & REQ_WRITE) && 53784f0db03SKent Overstreet (bio->bi_rw & REQ_SYNC)) 53884f0db03SKent Overstreet goto rescale; 53984f0db03SKent Overstreet 54084f0db03SKent Overstreet spin_lock(&dc->io_lock); 54184f0db03SKent Overstreet 54284f0db03SKent Overstreet hlist_for_each_entry(i, iohash(dc, bio->bi_sector), hash) 54384f0db03SKent Overstreet if (i->last == bio->bi_sector && 54484f0db03SKent Overstreet time_before(jiffies, i->jiffies)) 54584f0db03SKent Overstreet goto found; 54684f0db03SKent Overstreet 54784f0db03SKent Overstreet i = list_first_entry(&dc->io_lru, struct io, lru); 54884f0db03SKent Overstreet 549220bb38cSKent Overstreet add_sequential(task); 55084f0db03SKent Overstreet i->sequential = 0; 55184f0db03SKent Overstreet found: 55284f0db03SKent Overstreet if (i->sequential + bio->bi_size > i->sequential) 55384f0db03SKent Overstreet i->sequential += bio->bi_size; 55484f0db03SKent Overstreet 55584f0db03SKent Overstreet i->last = bio_end_sector(bio); 55684f0db03SKent Overstreet i->jiffies = jiffies + msecs_to_jiffies(5000); 557220bb38cSKent Overstreet task->sequential_io = i->sequential; 55884f0db03SKent Overstreet 55984f0db03SKent Overstreet hlist_del(&i->hash); 56084f0db03SKent Overstreet hlist_add_head(&i->hash, iohash(dc, i->last)); 56184f0db03SKent Overstreet list_move_tail(&i->lru, &dc->io_lru); 56284f0db03SKent Overstreet 56384f0db03SKent Overstreet spin_unlock(&dc->io_lock); 56484f0db03SKent Overstreet 565220bb38cSKent Overstreet sectors = max(task->sequential_io, 566220bb38cSKent Overstreet task->sequential_io_avg) >> 9; 56784f0db03SKent Overstreet 56884f0db03SKent Overstreet if (dc->sequential_cutoff && 56984f0db03SKent Overstreet sectors >= dc->sequential_cutoff >> 9) { 570220bb38cSKent Overstreet trace_bcache_bypass_sequential(bio); 57184f0db03SKent Overstreet goto skip; 57284f0db03SKent Overstreet } 57384f0db03SKent Overstreet 57484f0db03SKent Overstreet if (congested && sectors >= congested) { 575220bb38cSKent Overstreet trace_bcache_bypass_congested(bio); 57684f0db03SKent Overstreet goto skip; 57784f0db03SKent Overstreet } 57884f0db03SKent Overstreet 57984f0db03SKent Overstreet rescale: 58084f0db03SKent Overstreet bch_rescale_priorities(c, bio_sectors(bio)); 58184f0db03SKent Overstreet return false; 58284f0db03SKent Overstreet skip: 583220bb38cSKent Overstreet bch_mark_sectors_bypassed(c, dc, bio_sectors(bio)); 58484f0db03SKent Overstreet return true; 58584f0db03SKent Overstreet } 58684f0db03SKent Overstreet 587220bb38cSKent Overstreet /* Cache lookup */ 588220bb38cSKent Overstreet 589220bb38cSKent Overstreet struct search { 590220bb38cSKent Overstreet /* Stack frame for bio_complete */ 591220bb38cSKent Overstreet struct closure cl; 592220bb38cSKent Overstreet 593220bb38cSKent Overstreet struct bcache_device *d; 594220bb38cSKent Overstreet 595220bb38cSKent Overstreet struct bbio bio; 596220bb38cSKent Overstreet struct bio *orig_bio; 597220bb38cSKent Overstreet struct bio *cache_miss; 598220bb38cSKent Overstreet 599220bb38cSKent Overstreet unsigned insert_bio_sectors; 600220bb38cSKent Overstreet 601220bb38cSKent Overstreet unsigned recoverable:1; 602220bb38cSKent Overstreet unsigned unaligned_bvec:1; 603220bb38cSKent Overstreet unsigned write:1; 604220bb38cSKent Overstreet 605220bb38cSKent Overstreet unsigned long start_time; 606220bb38cSKent Overstreet 607220bb38cSKent Overstreet struct btree_op op; 608220bb38cSKent Overstreet struct data_insert_op iop; 609220bb38cSKent Overstreet }; 610220bb38cSKent Overstreet 611220bb38cSKent Overstreet static void bch_cache_read_endio(struct bio *bio, int error) 612220bb38cSKent Overstreet { 613220bb38cSKent Overstreet struct bbio *b = container_of(bio, struct bbio, bio); 614220bb38cSKent Overstreet struct closure *cl = bio->bi_private; 615220bb38cSKent Overstreet struct search *s = container_of(cl, struct search, cl); 616220bb38cSKent Overstreet 617220bb38cSKent Overstreet /* 618220bb38cSKent Overstreet * If the bucket was reused while our bio was in flight, we might have 619220bb38cSKent Overstreet * read the wrong data. Set s->error but not error so it doesn't get 620220bb38cSKent Overstreet * counted against the cache device, but we'll still reread the data 621220bb38cSKent Overstreet * from the backing device. 622220bb38cSKent Overstreet */ 623220bb38cSKent Overstreet 624220bb38cSKent Overstreet if (error) 625220bb38cSKent Overstreet s->iop.error = error; 626220bb38cSKent Overstreet else if (ptr_stale(s->iop.c, &b->key, 0)) { 627220bb38cSKent Overstreet atomic_long_inc(&s->iop.c->cache_read_races); 628220bb38cSKent Overstreet s->iop.error = -EINTR; 629220bb38cSKent Overstreet } 630220bb38cSKent Overstreet 631220bb38cSKent Overstreet bch_bbio_endio(s->iop.c, bio, error, "reading from cache"); 632220bb38cSKent Overstreet } 633220bb38cSKent Overstreet 634220bb38cSKent Overstreet /* 635220bb38cSKent Overstreet * Read from a single key, handling the initial cache miss if the key starts in 636220bb38cSKent Overstreet * the middle of the bio 637220bb38cSKent Overstreet */ 638220bb38cSKent Overstreet static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k) 639220bb38cSKent Overstreet { 640220bb38cSKent Overstreet struct search *s = container_of(op, struct search, op); 641220bb38cSKent Overstreet struct bio *n, *bio = &s->bio.bio; 642220bb38cSKent Overstreet struct bkey *bio_key; 643220bb38cSKent Overstreet unsigned ptr; 644220bb38cSKent Overstreet 645220bb38cSKent Overstreet if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_sector, 0)) <= 0) 646220bb38cSKent Overstreet return MAP_CONTINUE; 647220bb38cSKent Overstreet 648220bb38cSKent Overstreet if (KEY_INODE(k) != s->iop.inode || 649220bb38cSKent Overstreet KEY_START(k) > bio->bi_sector) { 650220bb38cSKent Overstreet unsigned bio_sectors = bio_sectors(bio); 651220bb38cSKent Overstreet unsigned sectors = KEY_INODE(k) == s->iop.inode 652220bb38cSKent Overstreet ? min_t(uint64_t, INT_MAX, 653220bb38cSKent Overstreet KEY_START(k) - bio->bi_sector) 654220bb38cSKent Overstreet : INT_MAX; 655220bb38cSKent Overstreet 656220bb38cSKent Overstreet int ret = s->d->cache_miss(b, s, bio, sectors); 657220bb38cSKent Overstreet if (ret != MAP_CONTINUE) 658220bb38cSKent Overstreet return ret; 659220bb38cSKent Overstreet 660220bb38cSKent Overstreet /* if this was a complete miss we shouldn't get here */ 661220bb38cSKent Overstreet BUG_ON(bio_sectors <= sectors); 662220bb38cSKent Overstreet } 663220bb38cSKent Overstreet 664220bb38cSKent Overstreet if (!KEY_SIZE(k)) 665220bb38cSKent Overstreet return MAP_CONTINUE; 666220bb38cSKent Overstreet 667220bb38cSKent Overstreet /* XXX: figure out best pointer - for multiple cache devices */ 668220bb38cSKent Overstreet ptr = 0; 669220bb38cSKent Overstreet 670220bb38cSKent Overstreet PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO; 671220bb38cSKent Overstreet 672220bb38cSKent Overstreet n = bch_bio_split(bio, min_t(uint64_t, INT_MAX, 673220bb38cSKent Overstreet KEY_OFFSET(k) - bio->bi_sector), 674220bb38cSKent Overstreet GFP_NOIO, s->d->bio_split); 675220bb38cSKent Overstreet 676220bb38cSKent Overstreet bio_key = &container_of(n, struct bbio, bio)->key; 677220bb38cSKent Overstreet bch_bkey_copy_single_ptr(bio_key, k, ptr); 678220bb38cSKent Overstreet 679220bb38cSKent Overstreet bch_cut_front(&KEY(s->iop.inode, n->bi_sector, 0), bio_key); 680220bb38cSKent Overstreet bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key); 681220bb38cSKent Overstreet 682220bb38cSKent Overstreet n->bi_end_io = bch_cache_read_endio; 683220bb38cSKent Overstreet n->bi_private = &s->cl; 684220bb38cSKent Overstreet 685220bb38cSKent Overstreet /* 686220bb38cSKent Overstreet * The bucket we're reading from might be reused while our bio 687220bb38cSKent Overstreet * is in flight, and we could then end up reading the wrong 688220bb38cSKent Overstreet * data. 689220bb38cSKent Overstreet * 690220bb38cSKent Overstreet * We guard against this by checking (in cache_read_endio()) if 691220bb38cSKent Overstreet * the pointer is stale again; if so, we treat it as an error 692220bb38cSKent Overstreet * and reread from the backing device (but we don't pass that 693220bb38cSKent Overstreet * error up anywhere). 694220bb38cSKent Overstreet */ 695220bb38cSKent Overstreet 696220bb38cSKent Overstreet __bch_submit_bbio(n, b->c); 697220bb38cSKent Overstreet return n == bio ? MAP_DONE : MAP_CONTINUE; 698220bb38cSKent Overstreet } 699220bb38cSKent Overstreet 700220bb38cSKent Overstreet static void cache_lookup(struct closure *cl) 701220bb38cSKent Overstreet { 702220bb38cSKent Overstreet struct search *s = container_of(cl, struct search, iop.cl); 703220bb38cSKent Overstreet struct bio *bio = &s->bio.bio; 704220bb38cSKent Overstreet 705220bb38cSKent Overstreet int ret = bch_btree_map_keys(&s->op, s->iop.c, 706220bb38cSKent Overstreet &KEY(s->iop.inode, bio->bi_sector, 0), 707220bb38cSKent Overstreet cache_lookup_fn, MAP_END_KEY); 708220bb38cSKent Overstreet if (ret == -EAGAIN) 709220bb38cSKent Overstreet continue_at(cl, cache_lookup, bcache_wq); 710220bb38cSKent Overstreet 711220bb38cSKent Overstreet closure_return(cl); 712220bb38cSKent Overstreet } 713220bb38cSKent Overstreet 714220bb38cSKent Overstreet /* Common code for the make_request functions */ 715220bb38cSKent Overstreet 716220bb38cSKent Overstreet static void request_endio(struct bio *bio, int error) 717220bb38cSKent Overstreet { 718220bb38cSKent Overstreet struct closure *cl = bio->bi_private; 719220bb38cSKent Overstreet 720220bb38cSKent Overstreet if (error) { 721220bb38cSKent Overstreet struct search *s = container_of(cl, struct search, cl); 722220bb38cSKent Overstreet s->iop.error = error; 723220bb38cSKent Overstreet /* Only cache read errors are recoverable */ 724220bb38cSKent Overstreet s->recoverable = false; 725220bb38cSKent Overstreet } 726220bb38cSKent Overstreet 727220bb38cSKent Overstreet bio_put(bio); 728220bb38cSKent Overstreet closure_put(cl); 729220bb38cSKent Overstreet } 730220bb38cSKent Overstreet 731220bb38cSKent Overstreet static void bio_complete(struct search *s) 732220bb38cSKent Overstreet { 733220bb38cSKent Overstreet if (s->orig_bio) { 734220bb38cSKent Overstreet int cpu, rw = bio_data_dir(s->orig_bio); 735220bb38cSKent Overstreet unsigned long duration = jiffies - s->start_time; 736220bb38cSKent Overstreet 737220bb38cSKent Overstreet cpu = part_stat_lock(); 738220bb38cSKent Overstreet part_round_stats(cpu, &s->d->disk->part0); 739220bb38cSKent Overstreet part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration); 740220bb38cSKent Overstreet part_stat_unlock(); 741220bb38cSKent Overstreet 742220bb38cSKent Overstreet trace_bcache_request_end(s->d, s->orig_bio); 743220bb38cSKent Overstreet bio_endio(s->orig_bio, s->iop.error); 744220bb38cSKent Overstreet s->orig_bio = NULL; 745220bb38cSKent Overstreet } 746220bb38cSKent Overstreet } 747220bb38cSKent Overstreet 748220bb38cSKent Overstreet static void do_bio_hook(struct search *s) 749220bb38cSKent Overstreet { 750220bb38cSKent Overstreet struct bio *bio = &s->bio.bio; 751220bb38cSKent Overstreet memcpy(bio, s->orig_bio, sizeof(struct bio)); 752220bb38cSKent Overstreet 753220bb38cSKent Overstreet bio->bi_end_io = request_endio; 754220bb38cSKent Overstreet bio->bi_private = &s->cl; 755220bb38cSKent Overstreet atomic_set(&bio->bi_cnt, 3); 756220bb38cSKent Overstreet } 757220bb38cSKent Overstreet 758220bb38cSKent Overstreet static void search_free(struct closure *cl) 759220bb38cSKent Overstreet { 760220bb38cSKent Overstreet struct search *s = container_of(cl, struct search, cl); 761220bb38cSKent Overstreet bio_complete(s); 762220bb38cSKent Overstreet 763220bb38cSKent Overstreet if (s->iop.bio) 764220bb38cSKent Overstreet bio_put(s->iop.bio); 765220bb38cSKent Overstreet 766220bb38cSKent Overstreet if (s->unaligned_bvec) 767220bb38cSKent Overstreet mempool_free(s->bio.bio.bi_io_vec, s->d->unaligned_bvec); 768220bb38cSKent Overstreet 769220bb38cSKent Overstreet closure_debug_destroy(cl); 770220bb38cSKent Overstreet mempool_free(s, s->d->c->search); 771220bb38cSKent Overstreet } 772220bb38cSKent Overstreet 773220bb38cSKent Overstreet static struct search *search_alloc(struct bio *bio, struct bcache_device *d) 774220bb38cSKent Overstreet { 775220bb38cSKent Overstreet struct search *s; 776220bb38cSKent Overstreet struct bio_vec *bv; 777220bb38cSKent Overstreet 778220bb38cSKent Overstreet s = mempool_alloc(d->c->search, GFP_NOIO); 779220bb38cSKent Overstreet memset(s, 0, offsetof(struct search, iop.insert_keys)); 780220bb38cSKent Overstreet 781220bb38cSKent Overstreet __closure_init(&s->cl, NULL); 782220bb38cSKent Overstreet 783220bb38cSKent Overstreet s->iop.inode = d->id; 784220bb38cSKent Overstreet s->iop.c = d->c; 785220bb38cSKent Overstreet s->d = d; 786220bb38cSKent Overstreet s->op.lock = -1; 7872599b53bSKent Overstreet s->iop.write_point = hash_long((unsigned long) current, 16); 788220bb38cSKent Overstreet s->orig_bio = bio; 789220bb38cSKent Overstreet s->write = (bio->bi_rw & REQ_WRITE) != 0; 790220bb38cSKent Overstreet s->iop.flush_journal = (bio->bi_rw & (REQ_FLUSH|REQ_FUA)) != 0; 791220bb38cSKent Overstreet s->recoverable = 1; 792220bb38cSKent Overstreet s->start_time = jiffies; 793220bb38cSKent Overstreet do_bio_hook(s); 794220bb38cSKent Overstreet 795220bb38cSKent Overstreet if (bio->bi_size != bio_segments(bio) * PAGE_SIZE) { 796220bb38cSKent Overstreet bv = mempool_alloc(d->unaligned_bvec, GFP_NOIO); 797220bb38cSKent Overstreet memcpy(bv, bio_iovec(bio), 798220bb38cSKent Overstreet sizeof(struct bio_vec) * bio_segments(bio)); 799220bb38cSKent Overstreet 800220bb38cSKent Overstreet s->bio.bio.bi_io_vec = bv; 801220bb38cSKent Overstreet s->unaligned_bvec = 1; 802220bb38cSKent Overstreet } 803220bb38cSKent Overstreet 804220bb38cSKent Overstreet return s; 805220bb38cSKent Overstreet } 806220bb38cSKent Overstreet 807220bb38cSKent Overstreet /* Cached devices */ 808220bb38cSKent Overstreet 809220bb38cSKent Overstreet static void cached_dev_bio_complete(struct closure *cl) 810220bb38cSKent Overstreet { 811220bb38cSKent Overstreet struct search *s = container_of(cl, struct search, cl); 812220bb38cSKent Overstreet struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 813220bb38cSKent Overstreet 814220bb38cSKent Overstreet search_free(cl); 815220bb38cSKent Overstreet cached_dev_put(dc); 816220bb38cSKent Overstreet } 817220bb38cSKent Overstreet 818cafe5635SKent Overstreet /* Process reads */ 819cafe5635SKent Overstreet 820cdd972b1SKent Overstreet static void cached_dev_cache_miss_done(struct closure *cl) 821cafe5635SKent Overstreet { 822cafe5635SKent Overstreet struct search *s = container_of(cl, struct search, cl); 823cafe5635SKent Overstreet 824220bb38cSKent Overstreet if (s->iop.replace_collision) 825220bb38cSKent Overstreet bch_mark_cache_miss_collision(s->iop.c, s->d); 826cafe5635SKent Overstreet 827220bb38cSKent Overstreet if (s->iop.bio) { 828cafe5635SKent Overstreet int i; 829cafe5635SKent Overstreet struct bio_vec *bv; 830cafe5635SKent Overstreet 831220bb38cSKent Overstreet bio_for_each_segment_all(bv, s->iop.bio, i) 832cafe5635SKent Overstreet __free_page(bv->bv_page); 833cafe5635SKent Overstreet } 834cafe5635SKent Overstreet 835cafe5635SKent Overstreet cached_dev_bio_complete(cl); 836cafe5635SKent Overstreet } 837cafe5635SKent Overstreet 838cdd972b1SKent Overstreet static void cached_dev_read_error(struct closure *cl) 839cafe5635SKent Overstreet { 840cafe5635SKent Overstreet struct search *s = container_of(cl, struct search, cl); 841cdd972b1SKent Overstreet struct bio *bio = &s->bio.bio; 842cafe5635SKent Overstreet struct bio_vec *bv; 843cafe5635SKent Overstreet int i; 844cafe5635SKent Overstreet 845cafe5635SKent Overstreet if (s->recoverable) { 846c37511b8SKent Overstreet /* Retry from the backing device: */ 847c37511b8SKent Overstreet trace_bcache_read_retry(s->orig_bio); 848cafe5635SKent Overstreet 849220bb38cSKent Overstreet s->iop.error = 0; 850cafe5635SKent Overstreet bv = s->bio.bio.bi_io_vec; 851cafe5635SKent Overstreet do_bio_hook(s); 852cafe5635SKent Overstreet s->bio.bio.bi_io_vec = bv; 853cafe5635SKent Overstreet 854cafe5635SKent Overstreet if (!s->unaligned_bvec) 855cafe5635SKent Overstreet bio_for_each_segment(bv, s->orig_bio, i) 856cafe5635SKent Overstreet bv->bv_offset = 0, bv->bv_len = PAGE_SIZE; 857cafe5635SKent Overstreet else 858cafe5635SKent Overstreet memcpy(s->bio.bio.bi_io_vec, 859cafe5635SKent Overstreet bio_iovec(s->orig_bio), 860cafe5635SKent Overstreet sizeof(struct bio_vec) * 861cafe5635SKent Overstreet bio_segments(s->orig_bio)); 862cafe5635SKent Overstreet 863cafe5635SKent Overstreet /* XXX: invalidate cache */ 864cafe5635SKent Overstreet 865cdd972b1SKent Overstreet closure_bio_submit(bio, cl, s->d); 866cafe5635SKent Overstreet } 867cafe5635SKent Overstreet 868cdd972b1SKent Overstreet continue_at(cl, cached_dev_cache_miss_done, NULL); 869cafe5635SKent Overstreet } 870cafe5635SKent Overstreet 871cdd972b1SKent Overstreet static void cached_dev_read_done(struct closure *cl) 872cafe5635SKent Overstreet { 873cafe5635SKent Overstreet struct search *s = container_of(cl, struct search, cl); 874cafe5635SKent Overstreet struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 875cafe5635SKent Overstreet 876cafe5635SKent Overstreet /* 877cdd972b1SKent Overstreet * We had a cache miss; cache_bio now contains data ready to be inserted 878cdd972b1SKent Overstreet * into the cache. 879cafe5635SKent Overstreet * 880cafe5635SKent Overstreet * First, we copy the data we just read from cache_bio's bounce buffers 881cafe5635SKent Overstreet * to the buffers the original bio pointed to: 882cafe5635SKent Overstreet */ 883cafe5635SKent Overstreet 884220bb38cSKent Overstreet if (s->iop.bio) { 885220bb38cSKent Overstreet bio_reset(s->iop.bio); 886220bb38cSKent Overstreet s->iop.bio->bi_sector = s->cache_miss->bi_sector; 887220bb38cSKent Overstreet s->iop.bio->bi_bdev = s->cache_miss->bi_bdev; 888220bb38cSKent Overstreet s->iop.bio->bi_size = s->insert_bio_sectors << 9; 889220bb38cSKent Overstreet bch_bio_map(s->iop.bio, NULL); 890cafe5635SKent Overstreet 891220bb38cSKent Overstreet bio_copy_data(s->cache_miss, s->iop.bio); 892cafe5635SKent Overstreet 893cafe5635SKent Overstreet bio_put(s->cache_miss); 894cafe5635SKent Overstreet s->cache_miss = NULL; 895cafe5635SKent Overstreet } 896cafe5635SKent Overstreet 897220bb38cSKent Overstreet if (verify(dc, &s->bio.bio) && s->recoverable && !s->unaligned_bvec) 898220bb38cSKent Overstreet bch_data_verify(dc, s->orig_bio); 899cafe5635SKent Overstreet 900cafe5635SKent Overstreet bio_complete(s); 901cafe5635SKent Overstreet 902220bb38cSKent Overstreet if (s->iop.bio && 903220bb38cSKent Overstreet !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) { 904220bb38cSKent Overstreet BUG_ON(!s->iop.replace); 905220bb38cSKent Overstreet closure_call(&s->iop.cl, bch_data_insert, NULL, cl); 906cafe5635SKent Overstreet } 907cafe5635SKent Overstreet 908cdd972b1SKent Overstreet continue_at(cl, cached_dev_cache_miss_done, NULL); 909cafe5635SKent Overstreet } 910cafe5635SKent Overstreet 911cdd972b1SKent Overstreet static void cached_dev_read_done_bh(struct closure *cl) 912cafe5635SKent Overstreet { 913cafe5635SKent Overstreet struct search *s = container_of(cl, struct search, cl); 914cafe5635SKent Overstreet struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 915cafe5635SKent Overstreet 916220bb38cSKent Overstreet bch_mark_cache_accounting(s->iop.c, s->d, 917220bb38cSKent Overstreet !s->cache_miss, s->iop.bypass); 918220bb38cSKent Overstreet trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass); 919cafe5635SKent Overstreet 920220bb38cSKent Overstreet if (s->iop.error) 921cdd972b1SKent Overstreet continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq); 922220bb38cSKent Overstreet else if (s->iop.bio || verify(dc, &s->bio.bio)) 923cdd972b1SKent Overstreet continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq); 924cafe5635SKent Overstreet else 925cdd972b1SKent Overstreet continue_at_nobarrier(cl, cached_dev_bio_complete, NULL); 926cafe5635SKent Overstreet } 927cafe5635SKent Overstreet 928cafe5635SKent Overstreet static int cached_dev_cache_miss(struct btree *b, struct search *s, 929cafe5635SKent Overstreet struct bio *bio, unsigned sectors) 930cafe5635SKent Overstreet { 9312c1953e2SKent Overstreet int ret = MAP_CONTINUE; 932e7c590ebSKent Overstreet unsigned reada = 0; 933cafe5635SKent Overstreet struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 934cdd972b1SKent Overstreet struct bio *miss, *cache_bio; 935cafe5635SKent Overstreet 936220bb38cSKent Overstreet if (s->cache_miss || s->iop.bypass) { 937e7c590ebSKent Overstreet miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split); 9382c1953e2SKent Overstreet ret = miss == bio ? MAP_DONE : MAP_CONTINUE; 939e7c590ebSKent Overstreet goto out_submit; 940e7c590ebSKent Overstreet } 941e7c590ebSKent Overstreet 942e7c590ebSKent Overstreet if (!(bio->bi_rw & REQ_RAHEAD) && 943e7c590ebSKent Overstreet !(bio->bi_rw & REQ_META) && 944220bb38cSKent Overstreet s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA) 945e7c590ebSKent Overstreet reada = min_t(sector_t, dc->readahead >> 9, 946e7c590ebSKent Overstreet bdev_sectors(bio->bi_bdev) - bio_end_sector(bio)); 947e7c590ebSKent Overstreet 948220bb38cSKent Overstreet s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada); 949e7c590ebSKent Overstreet 950220bb38cSKent Overstreet s->iop.replace_key = KEY(s->iop.inode, 951220bb38cSKent Overstreet bio->bi_sector + s->insert_bio_sectors, 952220bb38cSKent Overstreet s->insert_bio_sectors); 953e7c590ebSKent Overstreet 954220bb38cSKent Overstreet ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key); 955e7c590ebSKent Overstreet if (ret) 956e7c590ebSKent Overstreet return ret; 957e7c590ebSKent Overstreet 958220bb38cSKent Overstreet s->iop.replace = true; 9591b207d80SKent Overstreet 960cafe5635SKent Overstreet miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split); 9612c1953e2SKent Overstreet 962e7c590ebSKent Overstreet /* btree_search_recurse()'s btree iterator is no good anymore */ 9632c1953e2SKent Overstreet ret = miss == bio ? MAP_DONE : -EINTR; 964cafe5635SKent Overstreet 965cdd972b1SKent Overstreet cache_bio = bio_alloc_bioset(GFP_NOWAIT, 966220bb38cSKent Overstreet DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS), 967cafe5635SKent Overstreet dc->disk.bio_split); 968cdd972b1SKent Overstreet if (!cache_bio) 969cafe5635SKent Overstreet goto out_submit; 970cafe5635SKent Overstreet 971cdd972b1SKent Overstreet cache_bio->bi_sector = miss->bi_sector; 972cdd972b1SKent Overstreet cache_bio->bi_bdev = miss->bi_bdev; 973220bb38cSKent Overstreet cache_bio->bi_size = s->insert_bio_sectors << 9; 974cafe5635SKent Overstreet 975cdd972b1SKent Overstreet cache_bio->bi_end_io = request_endio; 976cdd972b1SKent Overstreet cache_bio->bi_private = &s->cl; 977cafe5635SKent Overstreet 978cdd972b1SKent Overstreet bch_bio_map(cache_bio, NULL); 979cdd972b1SKent Overstreet if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO)) 980cafe5635SKent Overstreet goto out_put; 981cafe5635SKent Overstreet 982220bb38cSKent Overstreet if (reada) 983220bb38cSKent Overstreet bch_mark_cache_readahead(s->iop.c, s->d); 984220bb38cSKent Overstreet 985cafe5635SKent Overstreet s->cache_miss = miss; 986220bb38cSKent Overstreet s->iop.bio = cache_bio; 987cdd972b1SKent Overstreet bio_get(cache_bio); 988cdd972b1SKent Overstreet closure_bio_submit(cache_bio, &s->cl, s->d); 989cafe5635SKent Overstreet 990cafe5635SKent Overstreet return ret; 991cafe5635SKent Overstreet out_put: 992cdd972b1SKent Overstreet bio_put(cache_bio); 993cafe5635SKent Overstreet out_submit: 994e7c590ebSKent Overstreet miss->bi_end_io = request_endio; 995e7c590ebSKent Overstreet miss->bi_private = &s->cl; 996cafe5635SKent Overstreet closure_bio_submit(miss, &s->cl, s->d); 997cafe5635SKent Overstreet return ret; 998cafe5635SKent Overstreet } 999cafe5635SKent Overstreet 1000cdd972b1SKent Overstreet static void cached_dev_read(struct cached_dev *dc, struct search *s) 1001cafe5635SKent Overstreet { 1002cafe5635SKent Overstreet struct closure *cl = &s->cl; 1003cafe5635SKent Overstreet 1004220bb38cSKent Overstreet closure_call(&s->iop.cl, cache_lookup, NULL, cl); 1005cdd972b1SKent Overstreet continue_at(cl, cached_dev_read_done_bh, NULL); 1006cafe5635SKent Overstreet } 1007cafe5635SKent Overstreet 1008cafe5635SKent Overstreet /* Process writes */ 1009cafe5635SKent Overstreet 1010cafe5635SKent Overstreet static void cached_dev_write_complete(struct closure *cl) 1011cafe5635SKent Overstreet { 1012cafe5635SKent Overstreet struct search *s = container_of(cl, struct search, cl); 1013cafe5635SKent Overstreet struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); 1014cafe5635SKent Overstreet 1015cafe5635SKent Overstreet up_read_non_owner(&dc->writeback_lock); 1016cafe5635SKent Overstreet cached_dev_bio_complete(cl); 1017cafe5635SKent Overstreet } 1018cafe5635SKent Overstreet 1019cdd972b1SKent Overstreet static void cached_dev_write(struct cached_dev *dc, struct search *s) 1020cafe5635SKent Overstreet { 1021cafe5635SKent Overstreet struct closure *cl = &s->cl; 1022cafe5635SKent Overstreet struct bio *bio = &s->bio.bio; 102384f0db03SKent Overstreet struct bkey start = KEY(dc->disk.id, bio->bi_sector, 0); 102484f0db03SKent Overstreet struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0); 1025cafe5635SKent Overstreet 1026220bb38cSKent Overstreet bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end); 1027cafe5635SKent Overstreet 1028cafe5635SKent Overstreet down_read_non_owner(&dc->writeback_lock); 1029cafe5635SKent Overstreet if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) { 103084f0db03SKent Overstreet /* 103184f0db03SKent Overstreet * We overlap with some dirty data undergoing background 103284f0db03SKent Overstreet * writeback, force this write to writeback 103384f0db03SKent Overstreet */ 1034220bb38cSKent Overstreet s->iop.bypass = false; 1035220bb38cSKent Overstreet s->iop.writeback = true; 1036cafe5635SKent Overstreet } 1037cafe5635SKent Overstreet 103884f0db03SKent Overstreet /* 103984f0db03SKent Overstreet * Discards aren't _required_ to do anything, so skipping if 104084f0db03SKent Overstreet * check_overlapping returned true is ok 104184f0db03SKent Overstreet * 104284f0db03SKent Overstreet * But check_overlapping drops dirty keys for which io hasn't started, 104384f0db03SKent Overstreet * so we still want to call it. 104484f0db03SKent Overstreet */ 1045cafe5635SKent Overstreet if (bio->bi_rw & REQ_DISCARD) 1046220bb38cSKent Overstreet s->iop.bypass = true; 1047cafe5635SKent Overstreet 104872c27061SKent Overstreet if (should_writeback(dc, s->orig_bio, 104972c27061SKent Overstreet cache_mode(dc, bio), 1050220bb38cSKent Overstreet s->iop.bypass)) { 1051220bb38cSKent Overstreet s->iop.bypass = false; 1052220bb38cSKent Overstreet s->iop.writeback = true; 105372c27061SKent Overstreet } 105472c27061SKent Overstreet 1055220bb38cSKent Overstreet if (s->iop.bypass) { 1056220bb38cSKent Overstreet s->iop.bio = s->orig_bio; 1057220bb38cSKent Overstreet bio_get(s->iop.bio); 1058c37511b8SKent Overstreet 105984f0db03SKent Overstreet if (!(bio->bi_rw & REQ_DISCARD) || 106084f0db03SKent Overstreet blk_queue_discard(bdev_get_queue(dc->bdev))) 1061cafe5635SKent Overstreet closure_bio_submit(bio, cl, s->d); 1062220bb38cSKent Overstreet } else if (s->iop.writeback) { 1063279afbadSKent Overstreet bch_writeback_add(dc); 1064220bb38cSKent Overstreet s->iop.bio = bio; 1065e49c7c37SKent Overstreet 1066c0f04d88SKent Overstreet if (bio->bi_rw & REQ_FLUSH) { 1067e49c7c37SKent Overstreet /* Also need to send a flush to the backing device */ 1068d4eddd42SKent Overstreet struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0, 1069e49c7c37SKent Overstreet dc->disk.bio_split); 1070e49c7c37SKent Overstreet 1071c0f04d88SKent Overstreet flush->bi_rw = WRITE_FLUSH; 1072c0f04d88SKent Overstreet flush->bi_bdev = bio->bi_bdev; 1073c0f04d88SKent Overstreet flush->bi_end_io = request_endio; 1074c0f04d88SKent Overstreet flush->bi_private = cl; 1075c0f04d88SKent Overstreet 1076c0f04d88SKent Overstreet closure_bio_submit(flush, cl, s->d); 1077e49c7c37SKent Overstreet } 107884f0db03SKent Overstreet } else { 1079220bb38cSKent Overstreet s->iop.bio = bio_clone_bioset(bio, GFP_NOIO, 108084f0db03SKent Overstreet dc->disk.bio_split); 1081cafe5635SKent Overstreet 1082cafe5635SKent Overstreet closure_bio_submit(bio, cl, s->d); 108384f0db03SKent Overstreet } 108484f0db03SKent Overstreet 1085220bb38cSKent Overstreet closure_call(&s->iop.cl, bch_data_insert, NULL, cl); 108684f0db03SKent Overstreet continue_at(cl, cached_dev_write_complete, NULL); 1087cafe5635SKent Overstreet } 1088cafe5635SKent Overstreet 1089a34a8bfdSKent Overstreet static void cached_dev_nodata(struct closure *cl) 1090cafe5635SKent Overstreet { 1091a34a8bfdSKent Overstreet struct search *s = container_of(cl, struct search, cl); 1092cafe5635SKent Overstreet struct bio *bio = &s->bio.bio; 1093cafe5635SKent Overstreet 1094220bb38cSKent Overstreet if (s->iop.flush_journal) 1095220bb38cSKent Overstreet bch_journal_meta(s->iop.c, cl); 1096cafe5635SKent Overstreet 109784f0db03SKent Overstreet /* If it's a flush, we send the flush to the backing device too */ 1098cafe5635SKent Overstreet closure_bio_submit(bio, cl, s->d); 1099cafe5635SKent Overstreet 1100cafe5635SKent Overstreet continue_at(cl, cached_dev_bio_complete, NULL); 1101cafe5635SKent Overstreet } 1102cafe5635SKent Overstreet 1103cafe5635SKent Overstreet /* Cached devices - read & write stuff */ 1104cafe5635SKent Overstreet 1105cafe5635SKent Overstreet static void cached_dev_make_request(struct request_queue *q, struct bio *bio) 1106cafe5635SKent Overstreet { 1107cafe5635SKent Overstreet struct search *s; 1108cafe5635SKent Overstreet struct bcache_device *d = bio->bi_bdev->bd_disk->private_data; 1109cafe5635SKent Overstreet struct cached_dev *dc = container_of(d, struct cached_dev, disk); 1110cafe5635SKent Overstreet int cpu, rw = bio_data_dir(bio); 1111cafe5635SKent Overstreet 1112cafe5635SKent Overstreet cpu = part_stat_lock(); 1113cafe5635SKent Overstreet part_stat_inc(cpu, &d->disk->part0, ios[rw]); 1114cafe5635SKent Overstreet part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio)); 1115cafe5635SKent Overstreet part_stat_unlock(); 1116cafe5635SKent Overstreet 1117cafe5635SKent Overstreet bio->bi_bdev = dc->bdev; 11182903381fSKent Overstreet bio->bi_sector += dc->sb.data_offset; 1119cafe5635SKent Overstreet 1120cafe5635SKent Overstreet if (cached_dev_get(dc)) { 1121cafe5635SKent Overstreet s = search_alloc(bio, d); 1122220bb38cSKent Overstreet trace_bcache_request_start(s->d, bio); 1123cafe5635SKent Overstreet 1124a34a8bfdSKent Overstreet if (!bio->bi_size) { 1125a34a8bfdSKent Overstreet /* 1126a34a8bfdSKent Overstreet * can't call bch_journal_meta from under 1127a34a8bfdSKent Overstreet * generic_make_request 1128a34a8bfdSKent Overstreet */ 1129a34a8bfdSKent Overstreet continue_at_nobarrier(&s->cl, 1130a34a8bfdSKent Overstreet cached_dev_nodata, 1131a34a8bfdSKent Overstreet bcache_wq); 1132a34a8bfdSKent Overstreet } else { 1133220bb38cSKent Overstreet s->iop.bypass = check_should_bypass(dc, bio); 113484f0db03SKent Overstreet 113584f0db03SKent Overstreet if (rw) 1136cdd972b1SKent Overstreet cached_dev_write(dc, s); 1137cafe5635SKent Overstreet else 1138cdd972b1SKent Overstreet cached_dev_read(dc, s); 113984f0db03SKent Overstreet } 1140cafe5635SKent Overstreet } else { 1141cafe5635SKent Overstreet if ((bio->bi_rw & REQ_DISCARD) && 1142cafe5635SKent Overstreet !blk_queue_discard(bdev_get_queue(dc->bdev))) 1143cafe5635SKent Overstreet bio_endio(bio, 0); 1144cafe5635SKent Overstreet else 1145cafe5635SKent Overstreet bch_generic_make_request(bio, &d->bio_split_hook); 1146cafe5635SKent Overstreet } 1147cafe5635SKent Overstreet } 1148cafe5635SKent Overstreet 1149cafe5635SKent Overstreet static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode, 1150cafe5635SKent Overstreet unsigned int cmd, unsigned long arg) 1151cafe5635SKent Overstreet { 1152cafe5635SKent Overstreet struct cached_dev *dc = container_of(d, struct cached_dev, disk); 1153cafe5635SKent Overstreet return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg); 1154cafe5635SKent Overstreet } 1155cafe5635SKent Overstreet 1156cafe5635SKent Overstreet static int cached_dev_congested(void *data, int bits) 1157cafe5635SKent Overstreet { 1158cafe5635SKent Overstreet struct bcache_device *d = data; 1159cafe5635SKent Overstreet struct cached_dev *dc = container_of(d, struct cached_dev, disk); 1160cafe5635SKent Overstreet struct request_queue *q = bdev_get_queue(dc->bdev); 1161cafe5635SKent Overstreet int ret = 0; 1162cafe5635SKent Overstreet 1163cafe5635SKent Overstreet if (bdi_congested(&q->backing_dev_info, bits)) 1164cafe5635SKent Overstreet return 1; 1165cafe5635SKent Overstreet 1166cafe5635SKent Overstreet if (cached_dev_get(dc)) { 1167cafe5635SKent Overstreet unsigned i; 1168cafe5635SKent Overstreet struct cache *ca; 1169cafe5635SKent Overstreet 1170cafe5635SKent Overstreet for_each_cache(ca, d->c, i) { 1171cafe5635SKent Overstreet q = bdev_get_queue(ca->bdev); 1172cafe5635SKent Overstreet ret |= bdi_congested(&q->backing_dev_info, bits); 1173cafe5635SKent Overstreet } 1174cafe5635SKent Overstreet 1175cafe5635SKent Overstreet cached_dev_put(dc); 1176cafe5635SKent Overstreet } 1177cafe5635SKent Overstreet 1178cafe5635SKent Overstreet return ret; 1179cafe5635SKent Overstreet } 1180cafe5635SKent Overstreet 1181cafe5635SKent Overstreet void bch_cached_dev_request_init(struct cached_dev *dc) 1182cafe5635SKent Overstreet { 1183cafe5635SKent Overstreet struct gendisk *g = dc->disk.disk; 1184cafe5635SKent Overstreet 1185cafe5635SKent Overstreet g->queue->make_request_fn = cached_dev_make_request; 1186cafe5635SKent Overstreet g->queue->backing_dev_info.congested_fn = cached_dev_congested; 1187cafe5635SKent Overstreet dc->disk.cache_miss = cached_dev_cache_miss; 1188cafe5635SKent Overstreet dc->disk.ioctl = cached_dev_ioctl; 1189cafe5635SKent Overstreet } 1190cafe5635SKent Overstreet 1191cafe5635SKent Overstreet /* Flash backed devices */ 1192cafe5635SKent Overstreet 1193cafe5635SKent Overstreet static int flash_dev_cache_miss(struct btree *b, struct search *s, 1194cafe5635SKent Overstreet struct bio *bio, unsigned sectors) 1195cafe5635SKent Overstreet { 11968e51e414SKent Overstreet struct bio_vec *bv; 11978e51e414SKent Overstreet int i; 11988e51e414SKent Overstreet 1199cafe5635SKent Overstreet /* Zero fill bio */ 1200cafe5635SKent Overstreet 12018e51e414SKent Overstreet bio_for_each_segment(bv, bio, i) { 1202cafe5635SKent Overstreet unsigned j = min(bv->bv_len >> 9, sectors); 1203cafe5635SKent Overstreet 1204cafe5635SKent Overstreet void *p = kmap(bv->bv_page); 1205cafe5635SKent Overstreet memset(p + bv->bv_offset, 0, j << 9); 1206cafe5635SKent Overstreet kunmap(bv->bv_page); 1207cafe5635SKent Overstreet 1208cafe5635SKent Overstreet sectors -= j; 1209cafe5635SKent Overstreet } 1210cafe5635SKent Overstreet 12118e51e414SKent Overstreet bio_advance(bio, min(sectors << 9, bio->bi_size)); 12128e51e414SKent Overstreet 12138e51e414SKent Overstreet if (!bio->bi_size) 12142c1953e2SKent Overstreet return MAP_DONE; 1215cafe5635SKent Overstreet 12162c1953e2SKent Overstreet return MAP_CONTINUE; 1217cafe5635SKent Overstreet } 1218cafe5635SKent Overstreet 1219a34a8bfdSKent Overstreet static void flash_dev_nodata(struct closure *cl) 1220a34a8bfdSKent Overstreet { 1221a34a8bfdSKent Overstreet struct search *s = container_of(cl, struct search, cl); 1222a34a8bfdSKent Overstreet 1223220bb38cSKent Overstreet if (s->iop.flush_journal) 1224220bb38cSKent Overstreet bch_journal_meta(s->iop.c, cl); 1225a34a8bfdSKent Overstreet 1226a34a8bfdSKent Overstreet continue_at(cl, search_free, NULL); 1227a34a8bfdSKent Overstreet } 1228a34a8bfdSKent Overstreet 1229cafe5635SKent Overstreet static void flash_dev_make_request(struct request_queue *q, struct bio *bio) 1230cafe5635SKent Overstreet { 1231cafe5635SKent Overstreet struct search *s; 1232cafe5635SKent Overstreet struct closure *cl; 1233cafe5635SKent Overstreet struct bcache_device *d = bio->bi_bdev->bd_disk->private_data; 1234cafe5635SKent Overstreet int cpu, rw = bio_data_dir(bio); 1235cafe5635SKent Overstreet 1236cafe5635SKent Overstreet cpu = part_stat_lock(); 1237cafe5635SKent Overstreet part_stat_inc(cpu, &d->disk->part0, ios[rw]); 1238cafe5635SKent Overstreet part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio)); 1239cafe5635SKent Overstreet part_stat_unlock(); 1240cafe5635SKent Overstreet 1241cafe5635SKent Overstreet s = search_alloc(bio, d); 1242cafe5635SKent Overstreet cl = &s->cl; 1243cafe5635SKent Overstreet bio = &s->bio.bio; 1244cafe5635SKent Overstreet 1245220bb38cSKent Overstreet trace_bcache_request_start(s->d, bio); 1246cafe5635SKent Overstreet 124784f0db03SKent Overstreet if (!bio->bi_size) { 1248a34a8bfdSKent Overstreet /* 1249a34a8bfdSKent Overstreet * can't call bch_journal_meta from under 1250a34a8bfdSKent Overstreet * generic_make_request 1251a34a8bfdSKent Overstreet */ 1252a34a8bfdSKent Overstreet continue_at_nobarrier(&s->cl, 1253a34a8bfdSKent Overstreet flash_dev_nodata, 1254a34a8bfdSKent Overstreet bcache_wq); 125584f0db03SKent Overstreet } else if (rw) { 1256220bb38cSKent Overstreet bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, 1257cafe5635SKent Overstreet &KEY(d->id, bio->bi_sector, 0), 12588e51e414SKent Overstreet &KEY(d->id, bio_end_sector(bio), 0)); 1259cafe5635SKent Overstreet 1260220bb38cSKent Overstreet s->iop.bypass = (bio->bi_rw & REQ_DISCARD) != 0; 1261220bb38cSKent Overstreet s->iop.writeback = true; 1262220bb38cSKent Overstreet s->iop.bio = bio; 1263cafe5635SKent Overstreet 1264220bb38cSKent Overstreet closure_call(&s->iop.cl, bch_data_insert, NULL, cl); 1265cafe5635SKent Overstreet } else { 1266220bb38cSKent Overstreet closure_call(&s->iop.cl, cache_lookup, NULL, cl); 1267cafe5635SKent Overstreet } 1268cafe5635SKent Overstreet 1269cafe5635SKent Overstreet continue_at(cl, search_free, NULL); 1270cafe5635SKent Overstreet } 1271cafe5635SKent Overstreet 1272cafe5635SKent Overstreet static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode, 1273cafe5635SKent Overstreet unsigned int cmd, unsigned long arg) 1274cafe5635SKent Overstreet { 1275cafe5635SKent Overstreet return -ENOTTY; 1276cafe5635SKent Overstreet } 1277cafe5635SKent Overstreet 1278cafe5635SKent Overstreet static int flash_dev_congested(void *data, int bits) 1279cafe5635SKent Overstreet { 1280cafe5635SKent Overstreet struct bcache_device *d = data; 1281cafe5635SKent Overstreet struct request_queue *q; 1282cafe5635SKent Overstreet struct cache *ca; 1283cafe5635SKent Overstreet unsigned i; 1284cafe5635SKent Overstreet int ret = 0; 1285cafe5635SKent Overstreet 1286cafe5635SKent Overstreet for_each_cache(ca, d->c, i) { 1287cafe5635SKent Overstreet q = bdev_get_queue(ca->bdev); 1288cafe5635SKent Overstreet ret |= bdi_congested(&q->backing_dev_info, bits); 1289cafe5635SKent Overstreet } 1290cafe5635SKent Overstreet 1291cafe5635SKent Overstreet return ret; 1292cafe5635SKent Overstreet } 1293cafe5635SKent Overstreet 1294cafe5635SKent Overstreet void bch_flash_dev_request_init(struct bcache_device *d) 1295cafe5635SKent Overstreet { 1296cafe5635SKent Overstreet struct gendisk *g = d->disk; 1297cafe5635SKent Overstreet 1298cafe5635SKent Overstreet g->queue->make_request_fn = flash_dev_make_request; 1299cafe5635SKent Overstreet g->queue->backing_dev_info.congested_fn = flash_dev_congested; 1300cafe5635SKent Overstreet d->cache_miss = flash_dev_cache_miss; 1301cafe5635SKent Overstreet d->ioctl = flash_dev_ioctl; 1302cafe5635SKent Overstreet } 1303cafe5635SKent Overstreet 1304cafe5635SKent Overstreet void bch_request_exit(void) 1305cafe5635SKent Overstreet { 1306cafe5635SKent Overstreet #ifdef CONFIG_CGROUP_BCACHE 1307cafe5635SKent Overstreet cgroup_unload_subsys(&bcache_subsys); 1308cafe5635SKent Overstreet #endif 1309cafe5635SKent Overstreet if (bch_search_cache) 1310cafe5635SKent Overstreet kmem_cache_destroy(bch_search_cache); 1311cafe5635SKent Overstreet } 1312cafe5635SKent Overstreet 1313cafe5635SKent Overstreet int __init bch_request_init(void) 1314cafe5635SKent Overstreet { 1315cafe5635SKent Overstreet bch_search_cache = KMEM_CACHE(search, 0); 1316cafe5635SKent Overstreet if (!bch_search_cache) 1317cafe5635SKent Overstreet return -ENOMEM; 1318cafe5635SKent Overstreet 1319cafe5635SKent Overstreet #ifdef CONFIG_CGROUP_BCACHE 1320cafe5635SKent Overstreet cgroup_load_subsys(&bcache_subsys); 1321cafe5635SKent Overstreet init_bch_cgroup(&bcache_default_cgroup); 1322cafe5635SKent Overstreet 1323cafe5635SKent Overstreet cgroup_add_cftypes(&bcache_subsys, bch_files); 1324cafe5635SKent Overstreet #endif 1325cafe5635SKent Overstreet return 0; 1326cafe5635SKent Overstreet } 1327