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