Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Main bcache entry point - handle a read or a write request and decide what to |
| 3 | * do with it; the make_request functions are called by the block layer. |
| 4 | * |
| 5 | * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> |
| 6 | * Copyright 2012 Google, Inc. |
| 7 | */ |
| 8 | |
| 9 | #include "bcache.h" |
| 10 | #include "btree.h" |
| 11 | #include "debug.h" |
| 12 | #include "request.h" |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 13 | #include "writeback.h" |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 14 | |
| 15 | #include <linux/cgroup.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/hash.h> |
| 18 | #include <linux/random.h> |
| 19 | #include "blk-cgroup.h" |
| 20 | |
| 21 | #include <trace/events/bcache.h> |
| 22 | |
| 23 | #define CUTOFF_CACHE_ADD 95 |
| 24 | #define CUTOFF_CACHE_READA 90 |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 25 | |
| 26 | struct kmem_cache *bch_search_cache; |
| 27 | |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 28 | static void bch_data_insert_start(struct closure *); |
| 29 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 30 | /* Cgroup interface */ |
| 31 | |
| 32 | #ifdef CONFIG_CGROUP_BCACHE |
| 33 | static struct bch_cgroup bcache_default_cgroup = { .cache_mode = -1 }; |
| 34 | |
| 35 | static struct bch_cgroup *cgroup_to_bcache(struct cgroup *cgroup) |
| 36 | { |
| 37 | struct cgroup_subsys_state *css; |
| 38 | return cgroup && |
| 39 | (css = cgroup_subsys_state(cgroup, bcache_subsys_id)) |
| 40 | ? container_of(css, struct bch_cgroup, css) |
| 41 | : &bcache_default_cgroup; |
| 42 | } |
| 43 | |
| 44 | struct bch_cgroup *bch_bio_to_cgroup(struct bio *bio) |
| 45 | { |
| 46 | struct cgroup_subsys_state *css = bio->bi_css |
| 47 | ? cgroup_subsys_state(bio->bi_css->cgroup, bcache_subsys_id) |
| 48 | : task_subsys_state(current, bcache_subsys_id); |
| 49 | |
| 50 | return css |
| 51 | ? container_of(css, struct bch_cgroup, css) |
| 52 | : &bcache_default_cgroup; |
| 53 | } |
| 54 | |
| 55 | static ssize_t cache_mode_read(struct cgroup *cgrp, struct cftype *cft, |
| 56 | struct file *file, |
| 57 | char __user *buf, size_t nbytes, loff_t *ppos) |
| 58 | { |
| 59 | char tmp[1024]; |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 60 | int len = bch_snprint_string_list(tmp, PAGE_SIZE, bch_cache_modes, |
| 61 | cgroup_to_bcache(cgrp)->cache_mode + 1); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 62 | |
| 63 | if (len < 0) |
| 64 | return len; |
| 65 | |
| 66 | return simple_read_from_buffer(buf, nbytes, ppos, tmp, len); |
| 67 | } |
| 68 | |
| 69 | static int cache_mode_write(struct cgroup *cgrp, struct cftype *cft, |
| 70 | const char *buf) |
| 71 | { |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 72 | int v = bch_read_string_list(buf, bch_cache_modes); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 73 | if (v < 0) |
| 74 | return v; |
| 75 | |
| 76 | cgroup_to_bcache(cgrp)->cache_mode = v - 1; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static u64 bch_verify_read(struct cgroup *cgrp, struct cftype *cft) |
| 81 | { |
| 82 | return cgroup_to_bcache(cgrp)->verify; |
| 83 | } |
| 84 | |
| 85 | static int bch_verify_write(struct cgroup *cgrp, struct cftype *cft, u64 val) |
| 86 | { |
| 87 | cgroup_to_bcache(cgrp)->verify = val; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static u64 bch_cache_hits_read(struct cgroup *cgrp, struct cftype *cft) |
| 92 | { |
| 93 | struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); |
| 94 | return atomic_read(&bcachecg->stats.cache_hits); |
| 95 | } |
| 96 | |
| 97 | static u64 bch_cache_misses_read(struct cgroup *cgrp, struct cftype *cft) |
| 98 | { |
| 99 | struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); |
| 100 | return atomic_read(&bcachecg->stats.cache_misses); |
| 101 | } |
| 102 | |
| 103 | static u64 bch_cache_bypass_hits_read(struct cgroup *cgrp, |
| 104 | struct cftype *cft) |
| 105 | { |
| 106 | struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); |
| 107 | return atomic_read(&bcachecg->stats.cache_bypass_hits); |
| 108 | } |
| 109 | |
| 110 | static u64 bch_cache_bypass_misses_read(struct cgroup *cgrp, |
| 111 | struct cftype *cft) |
| 112 | { |
| 113 | struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp); |
| 114 | return atomic_read(&bcachecg->stats.cache_bypass_misses); |
| 115 | } |
| 116 | |
| 117 | static struct cftype bch_files[] = { |
| 118 | { |
| 119 | .name = "cache_mode", |
| 120 | .read = cache_mode_read, |
| 121 | .write_string = cache_mode_write, |
| 122 | }, |
| 123 | { |
| 124 | .name = "verify", |
| 125 | .read_u64 = bch_verify_read, |
| 126 | .write_u64 = bch_verify_write, |
| 127 | }, |
| 128 | { |
| 129 | .name = "cache_hits", |
| 130 | .read_u64 = bch_cache_hits_read, |
| 131 | }, |
| 132 | { |
| 133 | .name = "cache_misses", |
| 134 | .read_u64 = bch_cache_misses_read, |
| 135 | }, |
| 136 | { |
| 137 | .name = "cache_bypass_hits", |
| 138 | .read_u64 = bch_cache_bypass_hits_read, |
| 139 | }, |
| 140 | { |
| 141 | .name = "cache_bypass_misses", |
| 142 | .read_u64 = bch_cache_bypass_misses_read, |
| 143 | }, |
| 144 | { } /* terminate */ |
| 145 | }; |
| 146 | |
| 147 | static void init_bch_cgroup(struct bch_cgroup *cg) |
| 148 | { |
| 149 | cg->cache_mode = -1; |
| 150 | } |
| 151 | |
| 152 | static struct cgroup_subsys_state *bcachecg_create(struct cgroup *cgroup) |
| 153 | { |
| 154 | struct bch_cgroup *cg; |
| 155 | |
| 156 | cg = kzalloc(sizeof(*cg), GFP_KERNEL); |
| 157 | if (!cg) |
| 158 | return ERR_PTR(-ENOMEM); |
| 159 | init_bch_cgroup(cg); |
| 160 | return &cg->css; |
| 161 | } |
| 162 | |
| 163 | static void bcachecg_destroy(struct cgroup *cgroup) |
| 164 | { |
| 165 | struct bch_cgroup *cg = cgroup_to_bcache(cgroup); |
| 166 | free_css_id(&bcache_subsys, &cg->css); |
| 167 | kfree(cg); |
| 168 | } |
| 169 | |
| 170 | struct cgroup_subsys bcache_subsys = { |
| 171 | .create = bcachecg_create, |
| 172 | .destroy = bcachecg_destroy, |
| 173 | .subsys_id = bcache_subsys_id, |
| 174 | .name = "bcache", |
| 175 | .module = THIS_MODULE, |
| 176 | }; |
| 177 | EXPORT_SYMBOL_GPL(bcache_subsys); |
| 178 | #endif |
| 179 | |
| 180 | static unsigned cache_mode(struct cached_dev *dc, struct bio *bio) |
| 181 | { |
| 182 | #ifdef CONFIG_CGROUP_BCACHE |
| 183 | int r = bch_bio_to_cgroup(bio)->cache_mode; |
| 184 | if (r >= 0) |
| 185 | return r; |
| 186 | #endif |
| 187 | return BDEV_CACHE_MODE(&dc->sb); |
| 188 | } |
| 189 | |
| 190 | static bool verify(struct cached_dev *dc, struct bio *bio) |
| 191 | { |
| 192 | #ifdef CONFIG_CGROUP_BCACHE |
| 193 | if (bch_bio_to_cgroup(bio)->verify) |
| 194 | return true; |
| 195 | #endif |
| 196 | return dc->verify; |
| 197 | } |
| 198 | |
| 199 | static void bio_csum(struct bio *bio, struct bkey *k) |
| 200 | { |
| 201 | struct bio_vec *bv; |
| 202 | uint64_t csum = 0; |
| 203 | int i; |
| 204 | |
| 205 | bio_for_each_segment(bv, bio, i) { |
| 206 | void *d = kmap(bv->bv_page) + bv->bv_offset; |
Kent Overstreet | 169ef1c | 2013-03-28 12:50:55 -0600 | [diff] [blame] | 207 | csum = bch_crc64_update(csum, d, bv->bv_len); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 208 | kunmap(bv->bv_page); |
| 209 | } |
| 210 | |
| 211 | k->ptr[KEY_PTRS(k)] = csum & (~0ULL >> 1); |
| 212 | } |
| 213 | |
| 214 | /* Insert data into cache */ |
| 215 | |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 216 | static void bch_data_insert_keys(struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 217 | { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 218 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
Kent Overstreet | c18536a | 2013-07-24 17:44:17 -0700 | [diff] [blame] | 219 | atomic_t *journal_ref = NULL; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 220 | struct bkey *replace_key = op->replace ? &op->replace_key : NULL; |
Kent Overstreet | 6054c6d | 2013-07-24 18:06:22 -0700 | [diff] [blame] | 221 | int ret; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 222 | |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 223 | /* |
| 224 | * If we're looping, might already be waiting on |
| 225 | * another journal write - can't wait on more than one journal write at |
| 226 | * a time |
| 227 | * |
| 228 | * XXX: this looks wrong |
| 229 | */ |
| 230 | #if 0 |
| 231 | while (atomic_read(&s->cl.remaining) & CLOSURE_WAITING) |
| 232 | closure_sync(&s->cl); |
| 233 | #endif |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 234 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 235 | if (!op->replace) |
| 236 | journal_ref = bch_journal(op->c, &op->insert_keys, |
| 237 | op->flush_journal ? cl : NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 238 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 239 | ret = bch_btree_insert(op->c, &op->insert_keys, |
Kent Overstreet | 6054c6d | 2013-07-24 18:06:22 -0700 | [diff] [blame] | 240 | journal_ref, replace_key); |
| 241 | if (ret == -ESRCH) { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 242 | op->replace_collision = true; |
Kent Overstreet | 6054c6d | 2013-07-24 18:06:22 -0700 | [diff] [blame] | 243 | } else if (ret) { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 244 | op->error = -ENOMEM; |
| 245 | op->insert_data_done = true; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Kent Overstreet | c18536a | 2013-07-24 17:44:17 -0700 | [diff] [blame] | 248 | if (journal_ref) |
| 249 | atomic_dec_bug(journal_ref); |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 250 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 251 | if (!op->insert_data_done) |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 252 | continue_at(cl, bch_data_insert_start, bcache_wq); |
| 253 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 254 | bch_keylist_free(&op->insert_keys); |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 255 | closure_return(cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 258 | static void bch_data_invalidate(struct closure *cl) |
| 259 | { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 260 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
| 261 | struct bio *bio = op->bio; |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 262 | |
| 263 | pr_debug("invalidating %i sectors from %llu", |
| 264 | bio_sectors(bio), (uint64_t) bio->bi_sector); |
| 265 | |
| 266 | while (bio_sectors(bio)) { |
Kent Overstreet | 81ab419 | 2013-10-31 15:46:42 -0700 | [diff] [blame] | 267 | unsigned sectors = min(bio_sectors(bio), |
| 268 | 1U << (KEY_SIZE_BITS - 1)); |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 269 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 270 | if (bch_keylist_realloc(&op->insert_keys, 0, op->c)) |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 271 | goto out; |
| 272 | |
Kent Overstreet | 81ab419 | 2013-10-31 15:46:42 -0700 | [diff] [blame] | 273 | bio->bi_sector += sectors; |
| 274 | bio->bi_size -= sectors << 9; |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 275 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 276 | bch_keylist_add(&op->insert_keys, |
Kent Overstreet | 81ab419 | 2013-10-31 15:46:42 -0700 | [diff] [blame] | 277 | &KEY(op->inode, bio->bi_sector, sectors)); |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 280 | op->insert_data_done = true; |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 281 | bio_put(bio); |
| 282 | out: |
| 283 | continue_at(cl, bch_data_insert_keys, bcache_wq); |
| 284 | } |
| 285 | |
| 286 | static void bch_data_insert_error(struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 287 | { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 288 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 289 | |
| 290 | /* |
| 291 | * Our data write just errored, which means we've got a bunch of keys to |
| 292 | * insert that point to data that wasn't succesfully written. |
| 293 | * |
| 294 | * We don't have to insert those keys but we still have to invalidate |
| 295 | * that region of the cache - so, if we just strip off all the pointers |
| 296 | * from the keys we'll accomplish just that. |
| 297 | */ |
| 298 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 299 | struct bkey *src = op->insert_keys.keys, *dst = op->insert_keys.keys; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 300 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 301 | while (src != op->insert_keys.top) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 302 | struct bkey *n = bkey_next(src); |
| 303 | |
| 304 | SET_KEY_PTRS(src, 0); |
Kent Overstreet | c2f95ae | 2013-07-24 17:24:25 -0700 | [diff] [blame] | 305 | memmove(dst, src, bkey_bytes(src)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 306 | |
| 307 | dst = bkey_next(dst); |
| 308 | src = n; |
| 309 | } |
| 310 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 311 | op->insert_keys.top = dst; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 312 | |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 313 | bch_data_insert_keys(cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 316 | static void bch_data_insert_endio(struct bio *bio, int error) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 317 | { |
| 318 | struct closure *cl = bio->bi_private; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 319 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 320 | |
| 321 | if (error) { |
| 322 | /* TODO: We could try to recover from this. */ |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 323 | if (op->writeback) |
| 324 | op->error = error; |
| 325 | else if (!op->replace) |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 326 | set_closure_fn(cl, bch_data_insert_error, bcache_wq); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 327 | else |
| 328 | set_closure_fn(cl, NULL, NULL); |
| 329 | } |
| 330 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 331 | bch_bbio_endio(op->c, bio, error, "writing data to cache"); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 334 | static void bch_data_insert_start(struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 335 | { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 336 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
| 337 | struct bio *bio = op->bio, *n; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 338 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 339 | if (op->bypass) |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 340 | return bch_data_invalidate(cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 341 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 342 | if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0) { |
| 343 | set_gc_sectors(op->c); |
| 344 | wake_up_gc(op->c); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Kent Overstreet | 54d12f2 | 2013-07-10 18:44:40 -0700 | [diff] [blame] | 347 | /* |
| 348 | * Journal writes are marked REQ_FLUSH; if the original write was a |
| 349 | * flush, it'll wait on the journal write. |
| 350 | */ |
| 351 | bio->bi_rw &= ~(REQ_FLUSH|REQ_FUA); |
| 352 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 353 | do { |
| 354 | unsigned i; |
| 355 | struct bkey *k; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 356 | struct bio_set *split = op->c->bio_split; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 357 | |
| 358 | /* 1 for the device pointer and 1 for the chksum */ |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 359 | if (bch_keylist_realloc(&op->insert_keys, |
| 360 | 1 + (op->csum ? 1 : 0), |
| 361 | op->c)) |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 362 | continue_at(cl, bch_data_insert_keys, bcache_wq); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 363 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 364 | k = op->insert_keys.top; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 365 | bkey_init(k); |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 366 | SET_KEY_INODE(k, op->inode); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 367 | SET_KEY_OFFSET(k, bio->bi_sector); |
| 368 | |
Kent Overstreet | 2599b53 | 2013-07-24 18:11:11 -0700 | [diff] [blame] | 369 | if (!bch_alloc_sectors(op->c, k, bio_sectors(bio), |
| 370 | op->write_point, op->write_prio, |
| 371 | op->writeback)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 372 | goto err; |
| 373 | |
| 374 | n = bch_bio_split(bio, KEY_SIZE(k), GFP_NOIO, split); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 375 | |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 376 | n->bi_end_io = bch_data_insert_endio; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 377 | n->bi_private = cl; |
| 378 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 379 | if (op->writeback) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 380 | SET_KEY_DIRTY(k, true); |
| 381 | |
| 382 | for (i = 0; i < KEY_PTRS(k); i++) |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 383 | SET_GC_MARK(PTR_BUCKET(op->c, k, i), |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 384 | GC_MARK_DIRTY); |
| 385 | } |
| 386 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 387 | SET_KEY_CSUM(k, op->csum); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 388 | if (KEY_CSUM(k)) |
| 389 | bio_csum(n, k); |
| 390 | |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 391 | trace_bcache_cache_insert(k); |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 392 | bch_keylist_push(&op->insert_keys); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 393 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 394 | n->bi_rw |= REQ_WRITE; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 395 | bch_submit_bbio(n, op->c, k, 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 396 | } while (n != bio); |
| 397 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 398 | op->insert_data_done = true; |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 399 | continue_at(cl, bch_data_insert_keys, bcache_wq); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 400 | err: |
| 401 | /* bch_alloc_sectors() blocks if s->writeback = true */ |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 402 | BUG_ON(op->writeback); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 403 | |
| 404 | /* |
| 405 | * But if it's not a writeback write we'd rather just bail out if |
| 406 | * there aren't any buckets ready to write to - it might take awhile and |
| 407 | * we might be starving btree writes for gc or something. |
| 408 | */ |
| 409 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 410 | if (!op->replace) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 411 | /* |
| 412 | * Writethrough write: We can't complete the write until we've |
| 413 | * updated the index. But we don't want to delay the write while |
| 414 | * we wait for buckets to be freed up, so just invalidate the |
| 415 | * rest of the write. |
| 416 | */ |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 417 | op->bypass = true; |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 418 | return bch_data_invalidate(cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 419 | } else { |
| 420 | /* |
| 421 | * From a cache miss, we can just insert the keys for the data |
| 422 | * we have written or bail out if we didn't do anything. |
| 423 | */ |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 424 | op->insert_data_done = true; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 425 | bio_put(bio); |
| 426 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 427 | if (!bch_keylist_empty(&op->insert_keys)) |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 428 | continue_at(cl, bch_data_insert_keys, bcache_wq); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 429 | else |
| 430 | closure_return(cl); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /** |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 435 | * bch_data_insert - stick some data in the cache |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 436 | * |
| 437 | * This is the starting point for any data to end up in a cache device; it could |
| 438 | * be from a normal write, or a writeback write, or a write to a flash only |
| 439 | * volume - it's also used by the moving garbage collector to compact data in |
| 440 | * mostly empty buckets. |
| 441 | * |
| 442 | * It first writes the data to the cache, creating a list of keys to be inserted |
| 443 | * (if the data had to be fragmented there will be multiple keys); after the |
| 444 | * data is written it calls bch_journal, and after the keys have been added to |
| 445 | * the next journal write they're inserted into the btree. |
| 446 | * |
Kent Overstreet | c18536a | 2013-07-24 17:44:17 -0700 | [diff] [blame] | 447 | * It inserts the data in s->cache_bio; bi_sector is used for the key offset, |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 448 | * and op->inode is used for the key inode. |
| 449 | * |
Kent Overstreet | c18536a | 2013-07-24 17:44:17 -0700 | [diff] [blame] | 450 | * If s->bypass is true, instead of inserting the data it invalidates the |
| 451 | * region of the cache represented by s->cache_bio and op->inode. |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 452 | */ |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 453 | void bch_data_insert(struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 454 | { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 455 | struct data_insert_op *op = container_of(cl, struct data_insert_op, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 456 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 457 | trace_bcache_write(op->bio, op->writeback, op->bypass); |
| 458 | |
| 459 | bch_keylist_init(&op->insert_keys); |
| 460 | bio_get(op->bio); |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 461 | bch_data_insert_start(cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 464 | /* Congested? */ |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 465 | |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 466 | unsigned bch_get_congested(struct cache_set *c) |
| 467 | { |
| 468 | int i; |
| 469 | long rand; |
| 470 | |
| 471 | if (!c->congested_read_threshold_us && |
| 472 | !c->congested_write_threshold_us) |
| 473 | return 0; |
| 474 | |
| 475 | i = (local_clock_us() - c->congested_last_us) / 1024; |
| 476 | if (i < 0) |
| 477 | return 0; |
| 478 | |
| 479 | i += atomic_read(&c->congested); |
| 480 | if (i >= 0) |
| 481 | return 0; |
| 482 | |
| 483 | i += CONGESTED_MAX; |
| 484 | |
| 485 | if (i > 0) |
| 486 | i = fract_exp_two(i, 6); |
| 487 | |
| 488 | rand = get_random_int(); |
| 489 | i -= bitmap_weight(&rand, BITS_PER_LONG); |
| 490 | |
| 491 | return i > 0 ? i : 1; |
| 492 | } |
| 493 | |
| 494 | static void add_sequential(struct task_struct *t) |
| 495 | { |
| 496 | ewma_add(t->sequential_io_avg, |
| 497 | t->sequential_io, 8, 0); |
| 498 | |
| 499 | t->sequential_io = 0; |
| 500 | } |
| 501 | |
| 502 | static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k) |
| 503 | { |
| 504 | return &dc->io_hash[hash_64(k, RECENT_IO_BITS)]; |
| 505 | } |
| 506 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 507 | static bool check_should_bypass(struct cached_dev *dc, struct bio *bio) |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 508 | { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 509 | struct cache_set *c = dc->disk.c; |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 510 | unsigned mode = cache_mode(dc, bio); |
| 511 | unsigned sectors, congested = bch_get_congested(c); |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 512 | struct task_struct *task = current; |
Kent Overstreet | 8aee122 | 2013-07-30 22:34:40 -0700 | [diff] [blame] | 513 | struct io *i; |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 514 | |
Kent Overstreet | c4d951d | 2013-08-21 17:49:09 -0700 | [diff] [blame] | 515 | if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) || |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 516 | c->gc_stats.in_use > CUTOFF_CACHE_ADD || |
| 517 | (bio->bi_rw & REQ_DISCARD)) |
| 518 | goto skip; |
| 519 | |
| 520 | if (mode == CACHE_MODE_NONE || |
| 521 | (mode == CACHE_MODE_WRITEAROUND && |
| 522 | (bio->bi_rw & REQ_WRITE))) |
| 523 | goto skip; |
| 524 | |
| 525 | if (bio->bi_sector & (c->sb.block_size - 1) || |
| 526 | bio_sectors(bio) & (c->sb.block_size - 1)) { |
| 527 | pr_debug("skipping unaligned io"); |
| 528 | goto skip; |
| 529 | } |
| 530 | |
Kent Overstreet | 5ceaaad | 2013-09-10 14:27:42 -0700 | [diff] [blame] | 531 | if (bypass_torture_test(dc)) { |
| 532 | if ((get_random_int() & 3) == 3) |
| 533 | goto skip; |
| 534 | else |
| 535 | goto rescale; |
| 536 | } |
| 537 | |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 538 | if (!congested && !dc->sequential_cutoff) |
| 539 | goto rescale; |
| 540 | |
| 541 | if (!congested && |
| 542 | mode == CACHE_MODE_WRITEBACK && |
| 543 | (bio->bi_rw & REQ_WRITE) && |
| 544 | (bio->bi_rw & REQ_SYNC)) |
| 545 | goto rescale; |
| 546 | |
Kent Overstreet | 8aee122 | 2013-07-30 22:34:40 -0700 | [diff] [blame] | 547 | spin_lock(&dc->io_lock); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 548 | |
Kent Overstreet | 8aee122 | 2013-07-30 22:34:40 -0700 | [diff] [blame] | 549 | hlist_for_each_entry(i, iohash(dc, bio->bi_sector), hash) |
| 550 | if (i->last == bio->bi_sector && |
| 551 | time_before(jiffies, i->jiffies)) |
| 552 | goto found; |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 553 | |
Kent Overstreet | 8aee122 | 2013-07-30 22:34:40 -0700 | [diff] [blame] | 554 | i = list_first_entry(&dc->io_lru, struct io, lru); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 555 | |
Kent Overstreet | 8aee122 | 2013-07-30 22:34:40 -0700 | [diff] [blame] | 556 | add_sequential(task); |
| 557 | i->sequential = 0; |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 558 | found: |
Kent Overstreet | 8aee122 | 2013-07-30 22:34:40 -0700 | [diff] [blame] | 559 | if (i->sequential + bio->bi_size > i->sequential) |
| 560 | i->sequential += bio->bi_size; |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 561 | |
Kent Overstreet | 8aee122 | 2013-07-30 22:34:40 -0700 | [diff] [blame] | 562 | i->last = bio_end_sector(bio); |
| 563 | i->jiffies = jiffies + msecs_to_jiffies(5000); |
| 564 | task->sequential_io = i->sequential; |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 565 | |
Kent Overstreet | 8aee122 | 2013-07-30 22:34:40 -0700 | [diff] [blame] | 566 | hlist_del(&i->hash); |
| 567 | hlist_add_head(&i->hash, iohash(dc, i->last)); |
| 568 | list_move_tail(&i->lru, &dc->io_lru); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 569 | |
Kent Overstreet | 8aee122 | 2013-07-30 22:34:40 -0700 | [diff] [blame] | 570 | spin_unlock(&dc->io_lock); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 571 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 572 | sectors = max(task->sequential_io, |
| 573 | task->sequential_io_avg) >> 9; |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 574 | |
| 575 | if (dc->sequential_cutoff && |
| 576 | sectors >= dc->sequential_cutoff >> 9) { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 577 | trace_bcache_bypass_sequential(bio); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 578 | goto skip; |
| 579 | } |
| 580 | |
| 581 | if (congested && sectors >= congested) { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 582 | trace_bcache_bypass_congested(bio); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 583 | goto skip; |
| 584 | } |
| 585 | |
| 586 | rescale: |
| 587 | bch_rescale_priorities(c, bio_sectors(bio)); |
| 588 | return false; |
| 589 | skip: |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 590 | bch_mark_sectors_bypassed(c, dc, bio_sectors(bio)); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 591 | return true; |
| 592 | } |
| 593 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 594 | /* Cache lookup */ |
| 595 | |
| 596 | struct search { |
| 597 | /* Stack frame for bio_complete */ |
| 598 | struct closure cl; |
| 599 | |
| 600 | struct bcache_device *d; |
| 601 | |
| 602 | struct bbio bio; |
| 603 | struct bio *orig_bio; |
| 604 | struct bio *cache_miss; |
| 605 | |
| 606 | unsigned insert_bio_sectors; |
| 607 | |
| 608 | unsigned recoverable:1; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 609 | unsigned write:1; |
Kent Overstreet | 5ceaaad | 2013-09-10 14:27:42 -0700 | [diff] [blame] | 610 | unsigned read_dirty_data:1; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 611 | |
| 612 | unsigned long start_time; |
| 613 | |
| 614 | struct btree_op op; |
| 615 | struct data_insert_op iop; |
Kent Overstreet | ed9c47b | 2013-11-22 19:37:48 -0800 | [diff] [blame^] | 616 | struct bio_vec bv[BIO_MAX_PAGES]; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 617 | }; |
| 618 | |
| 619 | static void bch_cache_read_endio(struct bio *bio, int error) |
| 620 | { |
| 621 | struct bbio *b = container_of(bio, struct bbio, bio); |
| 622 | struct closure *cl = bio->bi_private; |
| 623 | struct search *s = container_of(cl, struct search, cl); |
| 624 | |
| 625 | /* |
| 626 | * If the bucket was reused while our bio was in flight, we might have |
| 627 | * read the wrong data. Set s->error but not error so it doesn't get |
| 628 | * counted against the cache device, but we'll still reread the data |
| 629 | * from the backing device. |
| 630 | */ |
| 631 | |
| 632 | if (error) |
| 633 | s->iop.error = error; |
| 634 | else if (ptr_stale(s->iop.c, &b->key, 0)) { |
| 635 | atomic_long_inc(&s->iop.c->cache_read_races); |
| 636 | s->iop.error = -EINTR; |
| 637 | } |
| 638 | |
| 639 | bch_bbio_endio(s->iop.c, bio, error, "reading from cache"); |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Read from a single key, handling the initial cache miss if the key starts in |
| 644 | * the middle of the bio |
| 645 | */ |
| 646 | static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k) |
| 647 | { |
| 648 | struct search *s = container_of(op, struct search, op); |
| 649 | struct bio *n, *bio = &s->bio.bio; |
| 650 | struct bkey *bio_key; |
| 651 | unsigned ptr; |
| 652 | |
| 653 | if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_sector, 0)) <= 0) |
| 654 | return MAP_CONTINUE; |
| 655 | |
| 656 | if (KEY_INODE(k) != s->iop.inode || |
| 657 | KEY_START(k) > bio->bi_sector) { |
| 658 | unsigned bio_sectors = bio_sectors(bio); |
| 659 | unsigned sectors = KEY_INODE(k) == s->iop.inode |
| 660 | ? min_t(uint64_t, INT_MAX, |
| 661 | KEY_START(k) - bio->bi_sector) |
| 662 | : INT_MAX; |
| 663 | |
| 664 | int ret = s->d->cache_miss(b, s, bio, sectors); |
| 665 | if (ret != MAP_CONTINUE) |
| 666 | return ret; |
| 667 | |
| 668 | /* if this was a complete miss we shouldn't get here */ |
| 669 | BUG_ON(bio_sectors <= sectors); |
| 670 | } |
| 671 | |
| 672 | if (!KEY_SIZE(k)) |
| 673 | return MAP_CONTINUE; |
| 674 | |
| 675 | /* XXX: figure out best pointer - for multiple cache devices */ |
| 676 | ptr = 0; |
| 677 | |
| 678 | PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO; |
| 679 | |
Kent Overstreet | 5ceaaad | 2013-09-10 14:27:42 -0700 | [diff] [blame] | 680 | if (KEY_DIRTY(k)) |
| 681 | s->read_dirty_data = true; |
| 682 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 683 | n = bch_bio_split(bio, min_t(uint64_t, INT_MAX, |
| 684 | KEY_OFFSET(k) - bio->bi_sector), |
| 685 | GFP_NOIO, s->d->bio_split); |
| 686 | |
| 687 | bio_key = &container_of(n, struct bbio, bio)->key; |
| 688 | bch_bkey_copy_single_ptr(bio_key, k, ptr); |
| 689 | |
| 690 | bch_cut_front(&KEY(s->iop.inode, n->bi_sector, 0), bio_key); |
| 691 | bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key); |
| 692 | |
| 693 | n->bi_end_io = bch_cache_read_endio; |
| 694 | n->bi_private = &s->cl; |
| 695 | |
| 696 | /* |
| 697 | * The bucket we're reading from might be reused while our bio |
| 698 | * is in flight, and we could then end up reading the wrong |
| 699 | * data. |
| 700 | * |
| 701 | * We guard against this by checking (in cache_read_endio()) if |
| 702 | * the pointer is stale again; if so, we treat it as an error |
| 703 | * and reread from the backing device (but we don't pass that |
| 704 | * error up anywhere). |
| 705 | */ |
| 706 | |
| 707 | __bch_submit_bbio(n, b->c); |
| 708 | return n == bio ? MAP_DONE : MAP_CONTINUE; |
| 709 | } |
| 710 | |
| 711 | static void cache_lookup(struct closure *cl) |
| 712 | { |
| 713 | struct search *s = container_of(cl, struct search, iop.cl); |
| 714 | struct bio *bio = &s->bio.bio; |
| 715 | |
| 716 | int ret = bch_btree_map_keys(&s->op, s->iop.c, |
| 717 | &KEY(s->iop.inode, bio->bi_sector, 0), |
| 718 | cache_lookup_fn, MAP_END_KEY); |
| 719 | if (ret == -EAGAIN) |
| 720 | continue_at(cl, cache_lookup, bcache_wq); |
| 721 | |
| 722 | closure_return(cl); |
| 723 | } |
| 724 | |
| 725 | /* Common code for the make_request functions */ |
| 726 | |
| 727 | static void request_endio(struct bio *bio, int error) |
| 728 | { |
| 729 | struct closure *cl = bio->bi_private; |
| 730 | |
| 731 | if (error) { |
| 732 | struct search *s = container_of(cl, struct search, cl); |
| 733 | s->iop.error = error; |
| 734 | /* Only cache read errors are recoverable */ |
| 735 | s->recoverable = false; |
| 736 | } |
| 737 | |
| 738 | bio_put(bio); |
| 739 | closure_put(cl); |
| 740 | } |
| 741 | |
| 742 | static void bio_complete(struct search *s) |
| 743 | { |
| 744 | if (s->orig_bio) { |
| 745 | int cpu, rw = bio_data_dir(s->orig_bio); |
| 746 | unsigned long duration = jiffies - s->start_time; |
| 747 | |
| 748 | cpu = part_stat_lock(); |
| 749 | part_round_stats(cpu, &s->d->disk->part0); |
| 750 | part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration); |
| 751 | part_stat_unlock(); |
| 752 | |
| 753 | trace_bcache_request_end(s->d, s->orig_bio); |
| 754 | bio_endio(s->orig_bio, s->iop.error); |
| 755 | s->orig_bio = NULL; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | static void do_bio_hook(struct search *s) |
| 760 | { |
| 761 | struct bio *bio = &s->bio.bio; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 762 | |
Kent Overstreet | ed9c47b | 2013-11-22 19:37:48 -0800 | [diff] [blame^] | 763 | bio_init(bio); |
| 764 | bio->bi_io_vec = s->bv; |
| 765 | bio->bi_max_vecs = BIO_MAX_PAGES; |
| 766 | __bio_clone(bio, s->orig_bio); |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 767 | bio->bi_end_io = request_endio; |
| 768 | bio->bi_private = &s->cl; |
Kent Overstreet | ed9c47b | 2013-11-22 19:37:48 -0800 | [diff] [blame^] | 769 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 770 | atomic_set(&bio->bi_cnt, 3); |
| 771 | } |
| 772 | |
| 773 | static void search_free(struct closure *cl) |
| 774 | { |
| 775 | struct search *s = container_of(cl, struct search, cl); |
| 776 | bio_complete(s); |
| 777 | |
| 778 | if (s->iop.bio) |
| 779 | bio_put(s->iop.bio); |
| 780 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 781 | closure_debug_destroy(cl); |
| 782 | mempool_free(s, s->d->c->search); |
| 783 | } |
| 784 | |
| 785 | static struct search *search_alloc(struct bio *bio, struct bcache_device *d) |
| 786 | { |
| 787 | struct search *s; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 788 | |
| 789 | s = mempool_alloc(d->c->search, GFP_NOIO); |
| 790 | memset(s, 0, offsetof(struct search, iop.insert_keys)); |
| 791 | |
| 792 | __closure_init(&s->cl, NULL); |
| 793 | |
| 794 | s->iop.inode = d->id; |
| 795 | s->iop.c = d->c; |
| 796 | s->d = d; |
| 797 | s->op.lock = -1; |
Kent Overstreet | 2599b53 | 2013-07-24 18:11:11 -0700 | [diff] [blame] | 798 | s->iop.write_point = hash_long((unsigned long) current, 16); |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 799 | s->orig_bio = bio; |
| 800 | s->write = (bio->bi_rw & REQ_WRITE) != 0; |
| 801 | s->iop.flush_journal = (bio->bi_rw & (REQ_FLUSH|REQ_FUA)) != 0; |
| 802 | s->recoverable = 1; |
| 803 | s->start_time = jiffies; |
| 804 | do_bio_hook(s); |
| 805 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 806 | return s; |
| 807 | } |
| 808 | |
| 809 | /* Cached devices */ |
| 810 | |
| 811 | static void cached_dev_bio_complete(struct closure *cl) |
| 812 | { |
| 813 | struct search *s = container_of(cl, struct search, cl); |
| 814 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); |
| 815 | |
| 816 | search_free(cl); |
| 817 | cached_dev_put(dc); |
| 818 | } |
| 819 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 820 | /* Process reads */ |
| 821 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 822 | static void cached_dev_cache_miss_done(struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 823 | { |
| 824 | struct search *s = container_of(cl, struct search, cl); |
| 825 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 826 | if (s->iop.replace_collision) |
| 827 | bch_mark_cache_miss_collision(s->iop.c, s->d); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 828 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 829 | if (s->iop.bio) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 830 | int i; |
| 831 | struct bio_vec *bv; |
| 832 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 833 | bio_for_each_segment_all(bv, s->iop.bio, i) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 834 | __free_page(bv->bv_page); |
| 835 | } |
| 836 | |
| 837 | cached_dev_bio_complete(cl); |
| 838 | } |
| 839 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 840 | static void cached_dev_read_error(struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 841 | { |
| 842 | struct search *s = container_of(cl, struct search, cl); |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 843 | struct bio *bio = &s->bio.bio; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 844 | |
| 845 | if (s->recoverable) { |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 846 | /* Retry from the backing device: */ |
| 847 | trace_bcache_read_retry(s->orig_bio); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 848 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 849 | s->iop.error = 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 850 | do_bio_hook(s); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 851 | |
| 852 | /* XXX: invalidate cache */ |
| 853 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 854 | closure_bio_submit(bio, cl, s->d); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 857 | continue_at(cl, cached_dev_cache_miss_done, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 860 | static void cached_dev_read_done(struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 861 | { |
| 862 | struct search *s = container_of(cl, struct search, cl); |
| 863 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); |
| 864 | |
| 865 | /* |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 866 | * We had a cache miss; cache_bio now contains data ready to be inserted |
| 867 | * into the cache. |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 868 | * |
| 869 | * First, we copy the data we just read from cache_bio's bounce buffers |
| 870 | * to the buffers the original bio pointed to: |
| 871 | */ |
| 872 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 873 | if (s->iop.bio) { |
| 874 | bio_reset(s->iop.bio); |
| 875 | s->iop.bio->bi_sector = s->cache_miss->bi_sector; |
| 876 | s->iop.bio->bi_bdev = s->cache_miss->bi_bdev; |
| 877 | s->iop.bio->bi_size = s->insert_bio_sectors << 9; |
| 878 | bch_bio_map(s->iop.bio, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 879 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 880 | bio_copy_data(s->cache_miss, s->iop.bio); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 881 | |
| 882 | bio_put(s->cache_miss); |
| 883 | s->cache_miss = NULL; |
| 884 | } |
| 885 | |
Kent Overstreet | ed9c47b | 2013-11-22 19:37:48 -0800 | [diff] [blame^] | 886 | if (verify(dc, &s->bio.bio) && s->recoverable && !s->read_dirty_data) |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 887 | bch_data_verify(dc, s->orig_bio); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 888 | |
| 889 | bio_complete(s); |
| 890 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 891 | if (s->iop.bio && |
| 892 | !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) { |
| 893 | BUG_ON(!s->iop.replace); |
| 894 | closure_call(&s->iop.cl, bch_data_insert, NULL, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 897 | continue_at(cl, cached_dev_cache_miss_done, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 898 | } |
| 899 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 900 | static void cached_dev_read_done_bh(struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 901 | { |
| 902 | struct search *s = container_of(cl, struct search, cl); |
| 903 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); |
| 904 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 905 | bch_mark_cache_accounting(s->iop.c, s->d, |
| 906 | !s->cache_miss, s->iop.bypass); |
| 907 | trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 908 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 909 | if (s->iop.error) |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 910 | continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq); |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 911 | else if (s->iop.bio || verify(dc, &s->bio.bio)) |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 912 | continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 913 | else |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 914 | continue_at_nobarrier(cl, cached_dev_bio_complete, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | static int cached_dev_cache_miss(struct btree *b, struct search *s, |
| 918 | struct bio *bio, unsigned sectors) |
| 919 | { |
Kent Overstreet | 2c1953e | 2013-07-24 17:41:08 -0700 | [diff] [blame] | 920 | int ret = MAP_CONTINUE; |
Kent Overstreet | e7c590e | 2013-09-10 18:39:16 -0700 | [diff] [blame] | 921 | unsigned reada = 0; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 922 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 923 | struct bio *miss, *cache_bio; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 924 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 925 | if (s->cache_miss || s->iop.bypass) { |
Kent Overstreet | e7c590e | 2013-09-10 18:39:16 -0700 | [diff] [blame] | 926 | miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split); |
Kent Overstreet | 2c1953e | 2013-07-24 17:41:08 -0700 | [diff] [blame] | 927 | ret = miss == bio ? MAP_DONE : MAP_CONTINUE; |
Kent Overstreet | e7c590e | 2013-09-10 18:39:16 -0700 | [diff] [blame] | 928 | goto out_submit; |
| 929 | } |
| 930 | |
| 931 | if (!(bio->bi_rw & REQ_RAHEAD) && |
| 932 | !(bio->bi_rw & REQ_META) && |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 933 | s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA) |
Kent Overstreet | e7c590e | 2013-09-10 18:39:16 -0700 | [diff] [blame] | 934 | reada = min_t(sector_t, dc->readahead >> 9, |
| 935 | bdev_sectors(bio->bi_bdev) - bio_end_sector(bio)); |
| 936 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 937 | s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada); |
Kent Overstreet | e7c590e | 2013-09-10 18:39:16 -0700 | [diff] [blame] | 938 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 939 | s->iop.replace_key = KEY(s->iop.inode, |
| 940 | bio->bi_sector + s->insert_bio_sectors, |
| 941 | s->insert_bio_sectors); |
Kent Overstreet | e7c590e | 2013-09-10 18:39:16 -0700 | [diff] [blame] | 942 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 943 | ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key); |
Kent Overstreet | e7c590e | 2013-09-10 18:39:16 -0700 | [diff] [blame] | 944 | if (ret) |
| 945 | return ret; |
| 946 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 947 | s->iop.replace = true; |
Kent Overstreet | 1b207d8 | 2013-09-10 18:52:54 -0700 | [diff] [blame] | 948 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 949 | miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split); |
Kent Overstreet | 2c1953e | 2013-07-24 17:41:08 -0700 | [diff] [blame] | 950 | |
| 951 | /* btree_search_recurse()'s btree iterator is no good anymore */ |
| 952 | ret = miss == bio ? MAP_DONE : -EINTR; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 953 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 954 | cache_bio = bio_alloc_bioset(GFP_NOWAIT, |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 955 | DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS), |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 956 | dc->disk.bio_split); |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 957 | if (!cache_bio) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 958 | goto out_submit; |
| 959 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 960 | cache_bio->bi_sector = miss->bi_sector; |
| 961 | cache_bio->bi_bdev = miss->bi_bdev; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 962 | cache_bio->bi_size = s->insert_bio_sectors << 9; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 963 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 964 | cache_bio->bi_end_io = request_endio; |
| 965 | cache_bio->bi_private = &s->cl; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 966 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 967 | bch_bio_map(cache_bio, NULL); |
| 968 | if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO)) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 969 | goto out_put; |
| 970 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 971 | if (reada) |
| 972 | bch_mark_cache_readahead(s->iop.c, s->d); |
| 973 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 974 | s->cache_miss = miss; |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 975 | s->iop.bio = cache_bio; |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 976 | bio_get(cache_bio); |
| 977 | closure_bio_submit(cache_bio, &s->cl, s->d); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 978 | |
| 979 | return ret; |
| 980 | out_put: |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 981 | bio_put(cache_bio); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 982 | out_submit: |
Kent Overstreet | e7c590e | 2013-09-10 18:39:16 -0700 | [diff] [blame] | 983 | miss->bi_end_io = request_endio; |
| 984 | miss->bi_private = &s->cl; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 985 | closure_bio_submit(miss, &s->cl, s->d); |
| 986 | return ret; |
| 987 | } |
| 988 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 989 | static void cached_dev_read(struct cached_dev *dc, struct search *s) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 990 | { |
| 991 | struct closure *cl = &s->cl; |
| 992 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 993 | closure_call(&s->iop.cl, cache_lookup, NULL, cl); |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 994 | continue_at(cl, cached_dev_read_done_bh, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | /* Process writes */ |
| 998 | |
| 999 | static void cached_dev_write_complete(struct closure *cl) |
| 1000 | { |
| 1001 | struct search *s = container_of(cl, struct search, cl); |
| 1002 | struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); |
| 1003 | |
| 1004 | up_read_non_owner(&dc->writeback_lock); |
| 1005 | cached_dev_bio_complete(cl); |
| 1006 | } |
| 1007 | |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 1008 | static void cached_dev_write(struct cached_dev *dc, struct search *s) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1009 | { |
| 1010 | struct closure *cl = &s->cl; |
| 1011 | struct bio *bio = &s->bio.bio; |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1012 | struct bkey start = KEY(dc->disk.id, bio->bi_sector, 0); |
| 1013 | struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1014 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1015 | bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1016 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1017 | down_read_non_owner(&dc->writeback_lock); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1018 | if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) { |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1019 | /* |
| 1020 | * We overlap with some dirty data undergoing background |
| 1021 | * writeback, force this write to writeback |
| 1022 | */ |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1023 | s->iop.bypass = false; |
| 1024 | s->iop.writeback = true; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1027 | /* |
| 1028 | * Discards aren't _required_ to do anything, so skipping if |
| 1029 | * check_overlapping returned true is ok |
| 1030 | * |
| 1031 | * But check_overlapping drops dirty keys for which io hasn't started, |
| 1032 | * so we still want to call it. |
| 1033 | */ |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1034 | if (bio->bi_rw & REQ_DISCARD) |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1035 | s->iop.bypass = true; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1036 | |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 1037 | if (should_writeback(dc, s->orig_bio, |
| 1038 | cache_mode(dc, bio), |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1039 | s->iop.bypass)) { |
| 1040 | s->iop.bypass = false; |
| 1041 | s->iop.writeback = true; |
Kent Overstreet | 72c2706 | 2013-06-05 06:24:39 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1044 | if (s->iop.bypass) { |
| 1045 | s->iop.bio = s->orig_bio; |
| 1046 | bio_get(s->iop.bio); |
Kent Overstreet | c37511b | 2013-04-26 15:39:55 -0700 | [diff] [blame] | 1047 | |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1048 | if (!(bio->bi_rw & REQ_DISCARD) || |
| 1049 | blk_queue_discard(bdev_get_queue(dc->bdev))) |
| 1050 | closure_bio_submit(bio, cl, s->d); |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1051 | } else if (s->iop.writeback) { |
Kent Overstreet | 279afba | 2013-06-05 06:21:07 -0700 | [diff] [blame] | 1052 | bch_writeback_add(dc); |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1053 | s->iop.bio = bio; |
Kent Overstreet | e49c7c3 | 2013-06-26 17:25:38 -0700 | [diff] [blame] | 1054 | |
Kent Overstreet | c0f04d8 | 2013-09-23 23:17:36 -0700 | [diff] [blame] | 1055 | if (bio->bi_rw & REQ_FLUSH) { |
Kent Overstreet | e49c7c3 | 2013-06-26 17:25:38 -0700 | [diff] [blame] | 1056 | /* Also need to send a flush to the backing device */ |
Kent Overstreet | d4eddd4 | 2013-10-22 15:35:50 -0700 | [diff] [blame] | 1057 | struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0, |
Kent Overstreet | c0f04d8 | 2013-09-23 23:17:36 -0700 | [diff] [blame] | 1058 | dc->disk.bio_split); |
Kent Overstreet | e49c7c3 | 2013-06-26 17:25:38 -0700 | [diff] [blame] | 1059 | |
Kent Overstreet | c0f04d8 | 2013-09-23 23:17:36 -0700 | [diff] [blame] | 1060 | flush->bi_rw = WRITE_FLUSH; |
| 1061 | flush->bi_bdev = bio->bi_bdev; |
| 1062 | flush->bi_end_io = request_endio; |
| 1063 | flush->bi_private = cl; |
| 1064 | |
| 1065 | closure_bio_submit(flush, cl, s->d); |
Kent Overstreet | e49c7c3 | 2013-06-26 17:25:38 -0700 | [diff] [blame] | 1066 | } |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1067 | } else { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1068 | s->iop.bio = bio_clone_bioset(bio, GFP_NOIO, |
| 1069 | dc->disk.bio_split); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1070 | |
| 1071 | closure_bio_submit(bio, cl, s->d); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1072 | } |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1073 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1074 | closure_call(&s->iop.cl, bch_data_insert, NULL, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1075 | continue_at(cl, cached_dev_write_complete, NULL); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 1078 | static void cached_dev_nodata(struct closure *cl) |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1079 | { |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 1080 | struct search *s = container_of(cl, struct search, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1081 | struct bio *bio = &s->bio.bio; |
| 1082 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1083 | if (s->iop.flush_journal) |
| 1084 | bch_journal_meta(s->iop.c, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1085 | |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1086 | /* If it's a flush, we send the flush to the backing device too */ |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1087 | closure_bio_submit(bio, cl, s->d); |
| 1088 | |
| 1089 | continue_at(cl, cached_dev_bio_complete, NULL); |
| 1090 | } |
| 1091 | |
| 1092 | /* Cached devices - read & write stuff */ |
| 1093 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1094 | static void cached_dev_make_request(struct request_queue *q, struct bio *bio) |
| 1095 | { |
| 1096 | struct search *s; |
| 1097 | struct bcache_device *d = bio->bi_bdev->bd_disk->private_data; |
| 1098 | struct cached_dev *dc = container_of(d, struct cached_dev, disk); |
| 1099 | int cpu, rw = bio_data_dir(bio); |
| 1100 | |
| 1101 | cpu = part_stat_lock(); |
| 1102 | part_stat_inc(cpu, &d->disk->part0, ios[rw]); |
| 1103 | part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio)); |
| 1104 | part_stat_unlock(); |
| 1105 | |
| 1106 | bio->bi_bdev = dc->bdev; |
Kent Overstreet | 2903381 | 2013-04-11 15:14:35 -0700 | [diff] [blame] | 1107 | bio->bi_sector += dc->sb.data_offset; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1108 | |
| 1109 | if (cached_dev_get(dc)) { |
| 1110 | s = search_alloc(bio, d); |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1111 | trace_bcache_request_start(s->d, bio); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1112 | |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 1113 | if (!bio->bi_size) { |
| 1114 | /* |
| 1115 | * can't call bch_journal_meta from under |
| 1116 | * generic_make_request |
| 1117 | */ |
| 1118 | continue_at_nobarrier(&s->cl, |
| 1119 | cached_dev_nodata, |
| 1120 | bcache_wq); |
| 1121 | } else { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1122 | s->iop.bypass = check_should_bypass(dc, bio); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1123 | |
| 1124 | if (rw) |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 1125 | cached_dev_write(dc, s); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1126 | else |
Kent Overstreet | cdd972b | 2013-09-10 17:06:17 -0700 | [diff] [blame] | 1127 | cached_dev_read(dc, s); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1128 | } |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1129 | } else { |
| 1130 | if ((bio->bi_rw & REQ_DISCARD) && |
| 1131 | !blk_queue_discard(bdev_get_queue(dc->bdev))) |
| 1132 | bio_endio(bio, 0); |
| 1133 | else |
| 1134 | bch_generic_make_request(bio, &d->bio_split_hook); |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode, |
| 1139 | unsigned int cmd, unsigned long arg) |
| 1140 | { |
| 1141 | struct cached_dev *dc = container_of(d, struct cached_dev, disk); |
| 1142 | return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg); |
| 1143 | } |
| 1144 | |
| 1145 | static int cached_dev_congested(void *data, int bits) |
| 1146 | { |
| 1147 | struct bcache_device *d = data; |
| 1148 | struct cached_dev *dc = container_of(d, struct cached_dev, disk); |
| 1149 | struct request_queue *q = bdev_get_queue(dc->bdev); |
| 1150 | int ret = 0; |
| 1151 | |
| 1152 | if (bdi_congested(&q->backing_dev_info, bits)) |
| 1153 | return 1; |
| 1154 | |
| 1155 | if (cached_dev_get(dc)) { |
| 1156 | unsigned i; |
| 1157 | struct cache *ca; |
| 1158 | |
| 1159 | for_each_cache(ca, d->c, i) { |
| 1160 | q = bdev_get_queue(ca->bdev); |
| 1161 | ret |= bdi_congested(&q->backing_dev_info, bits); |
| 1162 | } |
| 1163 | |
| 1164 | cached_dev_put(dc); |
| 1165 | } |
| 1166 | |
| 1167 | return ret; |
| 1168 | } |
| 1169 | |
| 1170 | void bch_cached_dev_request_init(struct cached_dev *dc) |
| 1171 | { |
| 1172 | struct gendisk *g = dc->disk.disk; |
| 1173 | |
| 1174 | g->queue->make_request_fn = cached_dev_make_request; |
| 1175 | g->queue->backing_dev_info.congested_fn = cached_dev_congested; |
| 1176 | dc->disk.cache_miss = cached_dev_cache_miss; |
| 1177 | dc->disk.ioctl = cached_dev_ioctl; |
| 1178 | } |
| 1179 | |
| 1180 | /* Flash backed devices */ |
| 1181 | |
| 1182 | static int flash_dev_cache_miss(struct btree *b, struct search *s, |
| 1183 | struct bio *bio, unsigned sectors) |
| 1184 | { |
Kent Overstreet | 8e51e41 | 2013-06-06 18:15:57 -0700 | [diff] [blame] | 1185 | struct bio_vec *bv; |
| 1186 | int i; |
| 1187 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1188 | /* Zero fill bio */ |
| 1189 | |
Kent Overstreet | 8e51e41 | 2013-06-06 18:15:57 -0700 | [diff] [blame] | 1190 | bio_for_each_segment(bv, bio, i) { |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1191 | unsigned j = min(bv->bv_len >> 9, sectors); |
| 1192 | |
| 1193 | void *p = kmap(bv->bv_page); |
| 1194 | memset(p + bv->bv_offset, 0, j << 9); |
| 1195 | kunmap(bv->bv_page); |
| 1196 | |
Kent Overstreet | 8e51e41 | 2013-06-06 18:15:57 -0700 | [diff] [blame] | 1197 | sectors -= j; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
Kent Overstreet | 8e51e41 | 2013-06-06 18:15:57 -0700 | [diff] [blame] | 1200 | bio_advance(bio, min(sectors << 9, bio->bi_size)); |
| 1201 | |
| 1202 | if (!bio->bi_size) |
Kent Overstreet | 2c1953e | 2013-07-24 17:41:08 -0700 | [diff] [blame] | 1203 | return MAP_DONE; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1204 | |
Kent Overstreet | 2c1953e | 2013-07-24 17:41:08 -0700 | [diff] [blame] | 1205 | return MAP_CONTINUE; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 1208 | static void flash_dev_nodata(struct closure *cl) |
| 1209 | { |
| 1210 | struct search *s = container_of(cl, struct search, cl); |
| 1211 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1212 | if (s->iop.flush_journal) |
| 1213 | bch_journal_meta(s->iop.c, cl); |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 1214 | |
| 1215 | continue_at(cl, search_free, NULL); |
| 1216 | } |
| 1217 | |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1218 | static void flash_dev_make_request(struct request_queue *q, struct bio *bio) |
| 1219 | { |
| 1220 | struct search *s; |
| 1221 | struct closure *cl; |
| 1222 | struct bcache_device *d = bio->bi_bdev->bd_disk->private_data; |
| 1223 | int cpu, rw = bio_data_dir(bio); |
| 1224 | |
| 1225 | cpu = part_stat_lock(); |
| 1226 | part_stat_inc(cpu, &d->disk->part0, ios[rw]); |
| 1227 | part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio)); |
| 1228 | part_stat_unlock(); |
| 1229 | |
| 1230 | s = search_alloc(bio, d); |
| 1231 | cl = &s->cl; |
| 1232 | bio = &s->bio.bio; |
| 1233 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1234 | trace_bcache_request_start(s->d, bio); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1235 | |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1236 | if (!bio->bi_size) { |
Kent Overstreet | a34a8bf | 2013-10-24 17:07:04 -0700 | [diff] [blame] | 1237 | /* |
| 1238 | * can't call bch_journal_meta from under |
| 1239 | * generic_make_request |
| 1240 | */ |
| 1241 | continue_at_nobarrier(&s->cl, |
| 1242 | flash_dev_nodata, |
| 1243 | bcache_wq); |
Kent Overstreet | 84f0db0 | 2013-07-24 17:24:52 -0700 | [diff] [blame] | 1244 | } else if (rw) { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1245 | bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, |
Kent Overstreet | 8e51e41 | 2013-06-06 18:15:57 -0700 | [diff] [blame] | 1246 | &KEY(d->id, bio->bi_sector, 0), |
| 1247 | &KEY(d->id, bio_end_sector(bio), 0)); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1248 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1249 | s->iop.bypass = (bio->bi_rw & REQ_DISCARD) != 0; |
| 1250 | s->iop.writeback = true; |
| 1251 | s->iop.bio = bio; |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1252 | |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1253 | closure_call(&s->iop.cl, bch_data_insert, NULL, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1254 | } else { |
Kent Overstreet | 220bb38 | 2013-09-10 19:02:45 -0700 | [diff] [blame] | 1255 | closure_call(&s->iop.cl, cache_lookup, NULL, cl); |
Kent Overstreet | cafe563 | 2013-03-23 16:11:31 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | continue_at(cl, search_free, NULL); |
| 1259 | } |
| 1260 | |
| 1261 | static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode, |
| 1262 | unsigned int cmd, unsigned long arg) |
| 1263 | { |
| 1264 | return -ENOTTY; |
| 1265 | } |
| 1266 | |
| 1267 | static int flash_dev_congested(void *data, int bits) |
| 1268 | { |
| 1269 | struct bcache_device *d = data; |
| 1270 | struct request_queue *q; |
| 1271 | struct cache *ca; |
| 1272 | unsigned i; |
| 1273 | int ret = 0; |
| 1274 | |
| 1275 | for_each_cache(ca, d->c, i) { |
| 1276 | q = bdev_get_queue(ca->bdev); |
| 1277 | ret |= bdi_congested(&q->backing_dev_info, bits); |
| 1278 | } |
| 1279 | |
| 1280 | return ret; |
| 1281 | } |
| 1282 | |
| 1283 | void bch_flash_dev_request_init(struct bcache_device *d) |
| 1284 | { |
| 1285 | struct gendisk *g = d->disk; |
| 1286 | |
| 1287 | g->queue->make_request_fn = flash_dev_make_request; |
| 1288 | g->queue->backing_dev_info.congested_fn = flash_dev_congested; |
| 1289 | d->cache_miss = flash_dev_cache_miss; |
| 1290 | d->ioctl = flash_dev_ioctl; |
| 1291 | } |
| 1292 | |
| 1293 | void bch_request_exit(void) |
| 1294 | { |
| 1295 | #ifdef CONFIG_CGROUP_BCACHE |
| 1296 | cgroup_unload_subsys(&bcache_subsys); |
| 1297 | #endif |
| 1298 | if (bch_search_cache) |
| 1299 | kmem_cache_destroy(bch_search_cache); |
| 1300 | } |
| 1301 | |
| 1302 | int __init bch_request_init(void) |
| 1303 | { |
| 1304 | bch_search_cache = KMEM_CACHE(search, 0); |
| 1305 | if (!bch_search_cache) |
| 1306 | return -ENOMEM; |
| 1307 | |
| 1308 | #ifdef CONFIG_CGROUP_BCACHE |
| 1309 | cgroup_load_subsys(&bcache_subsys); |
| 1310 | init_bch_cgroup(&bcache_default_cgroup); |
| 1311 | |
| 1312 | cgroup_add_cftypes(&bcache_subsys, bch_files); |
| 1313 | #endif |
| 1314 | return 0; |
| 1315 | } |