blob: cf7850a7592c6940c948f07ffd6dcda53fb55b6e [file] [log] [blame]
Kent Overstreetcafe5632013-03-23 16:11:31 -07001/*
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 Overstreet279afba2013-06-05 06:21:07 -070013#include "writeback.h"
Kent Overstreetcafe5632013-03-23 16:11:31 -070014
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 Overstreetcafe5632013-03-23 16:11:31 -070025
26struct kmem_cache *bch_search_cache;
27
Kent Overstreeta34a8bf2013-10-24 17:07:04 -070028static void bch_data_insert_start(struct closure *);
29
Kent Overstreetcafe5632013-03-23 16:11:31 -070030/* Cgroup interface */
31
32#ifdef CONFIG_CGROUP_BCACHE
33static struct bch_cgroup bcache_default_cgroup = { .cache_mode = -1 };
34
35static struct bch_cgroup *cgroup_to_bcache(struct cgroup *cgroup)
36{
37 struct cgroup_subsys_state *css;
38 return cgroup &&
39 (css = cgroup_subsys_state(cgroup, bcache_subsys_id))
40 ? container_of(css, struct bch_cgroup, css)
41 : &bcache_default_cgroup;
42}
43
44struct bch_cgroup *bch_bio_to_cgroup(struct bio *bio)
45{
46 struct cgroup_subsys_state *css = bio->bi_css
47 ? cgroup_subsys_state(bio->bi_css->cgroup, bcache_subsys_id)
48 : task_subsys_state(current, bcache_subsys_id);
49
50 return css
51 ? container_of(css, struct bch_cgroup, css)
52 : &bcache_default_cgroup;
53}
54
55static ssize_t cache_mode_read(struct cgroup *cgrp, struct cftype *cft,
56 struct file *file,
57 char __user *buf, size_t nbytes, loff_t *ppos)
58{
59 char tmp[1024];
Kent Overstreet169ef1c2013-03-28 12:50:55 -060060 int len = bch_snprint_string_list(tmp, PAGE_SIZE, bch_cache_modes,
61 cgroup_to_bcache(cgrp)->cache_mode + 1);
Kent Overstreetcafe5632013-03-23 16:11:31 -070062
63 if (len < 0)
64 return len;
65
66 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
67}
68
69static int cache_mode_write(struct cgroup *cgrp, struct cftype *cft,
70 const char *buf)
71{
Kent Overstreet169ef1c2013-03-28 12:50:55 -060072 int v = bch_read_string_list(buf, bch_cache_modes);
Kent Overstreetcafe5632013-03-23 16:11:31 -070073 if (v < 0)
74 return v;
75
76 cgroup_to_bcache(cgrp)->cache_mode = v - 1;
77 return 0;
78}
79
80static u64 bch_verify_read(struct cgroup *cgrp, struct cftype *cft)
81{
82 return cgroup_to_bcache(cgrp)->verify;
83}
84
85static int bch_verify_write(struct cgroup *cgrp, struct cftype *cft, u64 val)
86{
87 cgroup_to_bcache(cgrp)->verify = val;
88 return 0;
89}
90
91static u64 bch_cache_hits_read(struct cgroup *cgrp, struct cftype *cft)
92{
93 struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
94 return atomic_read(&bcachecg->stats.cache_hits);
95}
96
97static u64 bch_cache_misses_read(struct cgroup *cgrp, struct cftype *cft)
98{
99 struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
100 return atomic_read(&bcachecg->stats.cache_misses);
101}
102
103static u64 bch_cache_bypass_hits_read(struct cgroup *cgrp,
104 struct cftype *cft)
105{
106 struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
107 return atomic_read(&bcachecg->stats.cache_bypass_hits);
108}
109
110static u64 bch_cache_bypass_misses_read(struct cgroup *cgrp,
111 struct cftype *cft)
112{
113 struct bch_cgroup *bcachecg = cgroup_to_bcache(cgrp);
114 return atomic_read(&bcachecg->stats.cache_bypass_misses);
115}
116
117static struct cftype bch_files[] = {
118 {
119 .name = "cache_mode",
120 .read = cache_mode_read,
121 .write_string = cache_mode_write,
122 },
123 {
124 .name = "verify",
125 .read_u64 = bch_verify_read,
126 .write_u64 = bch_verify_write,
127 },
128 {
129 .name = "cache_hits",
130 .read_u64 = bch_cache_hits_read,
131 },
132 {
133 .name = "cache_misses",
134 .read_u64 = bch_cache_misses_read,
135 },
136 {
137 .name = "cache_bypass_hits",
138 .read_u64 = bch_cache_bypass_hits_read,
139 },
140 {
141 .name = "cache_bypass_misses",
142 .read_u64 = bch_cache_bypass_misses_read,
143 },
144 { } /* terminate */
145};
146
147static void init_bch_cgroup(struct bch_cgroup *cg)
148{
149 cg->cache_mode = -1;
150}
151
152static struct cgroup_subsys_state *bcachecg_create(struct cgroup *cgroup)
153{
154 struct bch_cgroup *cg;
155
156 cg = kzalloc(sizeof(*cg), GFP_KERNEL);
157 if (!cg)
158 return ERR_PTR(-ENOMEM);
159 init_bch_cgroup(cg);
160 return &cg->css;
161}
162
163static void bcachecg_destroy(struct cgroup *cgroup)
164{
165 struct bch_cgroup *cg = cgroup_to_bcache(cgroup);
166 free_css_id(&bcache_subsys, &cg->css);
167 kfree(cg);
168}
169
170struct cgroup_subsys bcache_subsys = {
171 .create = bcachecg_create,
172 .destroy = bcachecg_destroy,
173 .subsys_id = bcache_subsys_id,
174 .name = "bcache",
175 .module = THIS_MODULE,
176};
177EXPORT_SYMBOL_GPL(bcache_subsys);
178#endif
179
180static unsigned cache_mode(struct cached_dev *dc, struct bio *bio)
181{
182#ifdef CONFIG_CGROUP_BCACHE
183 int r = bch_bio_to_cgroup(bio)->cache_mode;
184 if (r >= 0)
185 return r;
186#endif
187 return BDEV_CACHE_MODE(&dc->sb);
188}
189
190static bool verify(struct cached_dev *dc, struct bio *bio)
191{
192#ifdef CONFIG_CGROUP_BCACHE
193 if (bch_bio_to_cgroup(bio)->verify)
194 return true;
195#endif
196 return dc->verify;
197}
198
199static void bio_csum(struct bio *bio, struct bkey *k)
200{
201 struct bio_vec *bv;
202 uint64_t csum = 0;
203 int i;
204
205 bio_for_each_segment(bv, bio, i) {
206 void *d = kmap(bv->bv_page) + bv->bv_offset;
Kent Overstreet169ef1c2013-03-28 12:50:55 -0600207 csum = bch_crc64_update(csum, d, bv->bv_len);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700208 kunmap(bv->bv_page);
209 }
210
211 k->ptr[KEY_PTRS(k)] = csum & (~0ULL >> 1);
212}
213
214/* Insert data into cache */
215
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700216static void bch_data_insert_keys(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700217{
Kent Overstreet220bb382013-09-10 19:02:45 -0700218 struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
Kent Overstreetc18536a2013-07-24 17:44:17 -0700219 atomic_t *journal_ref = NULL;
Kent Overstreet220bb382013-09-10 19:02:45 -0700220 struct bkey *replace_key = op->replace ? &op->replace_key : NULL;
Kent Overstreet6054c6d2013-07-24 18:06:22 -0700221 int ret;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700222
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700223 /*
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 Overstreetcafe5632013-03-23 16:11:31 -0700234
Kent Overstreet220bb382013-09-10 19:02:45 -0700235 if (!op->replace)
236 journal_ref = bch_journal(op->c, &op->insert_keys,
237 op->flush_journal ? cl : NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700238
Kent Overstreet220bb382013-09-10 19:02:45 -0700239 ret = bch_btree_insert(op->c, &op->insert_keys,
Kent Overstreet6054c6d2013-07-24 18:06:22 -0700240 journal_ref, replace_key);
241 if (ret == -ESRCH) {
Kent Overstreet220bb382013-09-10 19:02:45 -0700242 op->replace_collision = true;
Kent Overstreet6054c6d2013-07-24 18:06:22 -0700243 } else if (ret) {
Kent Overstreet220bb382013-09-10 19:02:45 -0700244 op->error = -ENOMEM;
245 op->insert_data_done = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700246 }
247
Kent Overstreetc18536a2013-07-24 17:44:17 -0700248 if (journal_ref)
249 atomic_dec_bug(journal_ref);
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700250
Kent Overstreet220bb382013-09-10 19:02:45 -0700251 if (!op->insert_data_done)
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700252 continue_at(cl, bch_data_insert_start, bcache_wq);
253
Kent Overstreet220bb382013-09-10 19:02:45 -0700254 bch_keylist_free(&op->insert_keys);
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700255 closure_return(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700256}
257
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700258static void bch_data_invalidate(struct closure *cl)
259{
Kent Overstreet220bb382013-09-10 19:02:45 -0700260 struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
261 struct bio *bio = op->bio;
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700262
263 pr_debug("invalidating %i sectors from %llu",
264 bio_sectors(bio), (uint64_t) bio->bi_sector);
265
266 while (bio_sectors(bio)) {
267 unsigned len = min(bio_sectors(bio), 1U << 14);
268
Kent Overstreet220bb382013-09-10 19:02:45 -0700269 if (bch_keylist_realloc(&op->insert_keys, 0, op->c))
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700270 goto out;
271
272 bio->bi_sector += len;
273 bio->bi_size -= len << 9;
274
Kent Overstreet220bb382013-09-10 19:02:45 -0700275 bch_keylist_add(&op->insert_keys,
276 &KEY(op->inode, bio->bi_sector, len));
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700277 }
278
Kent Overstreet220bb382013-09-10 19:02:45 -0700279 op->insert_data_done = true;
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700280 bio_put(bio);
281out:
282 continue_at(cl, bch_data_insert_keys, bcache_wq);
283}
284
285static void bch_data_insert_error(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700286{
Kent Overstreet220bb382013-09-10 19:02:45 -0700287 struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700288
289 /*
290 * Our data write just errored, which means we've got a bunch of keys to
291 * insert that point to data that wasn't succesfully written.
292 *
293 * We don't have to insert those keys but we still have to invalidate
294 * that region of the cache - so, if we just strip off all the pointers
295 * from the keys we'll accomplish just that.
296 */
297
Kent Overstreet220bb382013-09-10 19:02:45 -0700298 struct bkey *src = op->insert_keys.keys, *dst = op->insert_keys.keys;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700299
Kent Overstreet220bb382013-09-10 19:02:45 -0700300 while (src != op->insert_keys.top) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700301 struct bkey *n = bkey_next(src);
302
303 SET_KEY_PTRS(src, 0);
Kent Overstreetc2f95ae2013-07-24 17:24:25 -0700304 memmove(dst, src, bkey_bytes(src));
Kent Overstreetcafe5632013-03-23 16:11:31 -0700305
306 dst = bkey_next(dst);
307 src = n;
308 }
309
Kent Overstreet220bb382013-09-10 19:02:45 -0700310 op->insert_keys.top = dst;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700311
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700312 bch_data_insert_keys(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700313}
314
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700315static void bch_data_insert_endio(struct bio *bio, int error)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700316{
317 struct closure *cl = bio->bi_private;
Kent Overstreet220bb382013-09-10 19:02:45 -0700318 struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700319
320 if (error) {
321 /* TODO: We could try to recover from this. */
Kent Overstreet220bb382013-09-10 19:02:45 -0700322 if (op->writeback)
323 op->error = error;
324 else if (!op->replace)
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700325 set_closure_fn(cl, bch_data_insert_error, bcache_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700326 else
327 set_closure_fn(cl, NULL, NULL);
328 }
329
Kent Overstreet220bb382013-09-10 19:02:45 -0700330 bch_bbio_endio(op->c, bio, error, "writing data to cache");
Kent Overstreetcafe5632013-03-23 16:11:31 -0700331}
332
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700333static void bch_data_insert_start(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700334{
Kent Overstreet220bb382013-09-10 19:02:45 -0700335 struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
336 struct bio *bio = op->bio, *n;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700337
Kent Overstreet220bb382013-09-10 19:02:45 -0700338 if (op->bypass)
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700339 return bch_data_invalidate(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700340
Kent Overstreet220bb382013-09-10 19:02:45 -0700341 if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0) {
342 set_gc_sectors(op->c);
343 wake_up_gc(op->c);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700344 }
345
Kent Overstreet54d12f22013-07-10 18:44:40 -0700346 /*
347 * Journal writes are marked REQ_FLUSH; if the original write was a
348 * flush, it'll wait on the journal write.
349 */
350 bio->bi_rw &= ~(REQ_FLUSH|REQ_FUA);
351
Kent Overstreetcafe5632013-03-23 16:11:31 -0700352 do {
353 unsigned i;
354 struct bkey *k;
Kent Overstreet220bb382013-09-10 19:02:45 -0700355 struct bio_set *split = op->c->bio_split;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700356
357 /* 1 for the device pointer and 1 for the chksum */
Kent Overstreet220bb382013-09-10 19:02:45 -0700358 if (bch_keylist_realloc(&op->insert_keys,
359 1 + (op->csum ? 1 : 0),
360 op->c))
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700361 continue_at(cl, bch_data_insert_keys, bcache_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700362
Kent Overstreet220bb382013-09-10 19:02:45 -0700363 k = op->insert_keys.top;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700364 bkey_init(k);
Kent Overstreet220bb382013-09-10 19:02:45 -0700365 SET_KEY_INODE(k, op->inode);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700366 SET_KEY_OFFSET(k, bio->bi_sector);
367
Kent Overstreet2599b532013-07-24 18:11:11 -0700368 if (!bch_alloc_sectors(op->c, k, bio_sectors(bio),
369 op->write_point, op->write_prio,
370 op->writeback))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700371 goto err;
372
373 n = bch_bio_split(bio, KEY_SIZE(k), GFP_NOIO, split);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700374
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700375 n->bi_end_io = bch_data_insert_endio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700376 n->bi_private = cl;
377
Kent Overstreet220bb382013-09-10 19:02:45 -0700378 if (op->writeback) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700379 SET_KEY_DIRTY(k, true);
380
381 for (i = 0; i < KEY_PTRS(k); i++)
Kent Overstreet220bb382013-09-10 19:02:45 -0700382 SET_GC_MARK(PTR_BUCKET(op->c, k, i),
Kent Overstreetcafe5632013-03-23 16:11:31 -0700383 GC_MARK_DIRTY);
384 }
385
Kent Overstreet220bb382013-09-10 19:02:45 -0700386 SET_KEY_CSUM(k, op->csum);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700387 if (KEY_CSUM(k))
388 bio_csum(n, k);
389
Kent Overstreetc37511b2013-04-26 15:39:55 -0700390 trace_bcache_cache_insert(k);
Kent Overstreet220bb382013-09-10 19:02:45 -0700391 bch_keylist_push(&op->insert_keys);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700392
Kent Overstreetcafe5632013-03-23 16:11:31 -0700393 n->bi_rw |= REQ_WRITE;
Kent Overstreet220bb382013-09-10 19:02:45 -0700394 bch_submit_bbio(n, op->c, k, 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700395 } while (n != bio);
396
Kent Overstreet220bb382013-09-10 19:02:45 -0700397 op->insert_data_done = true;
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700398 continue_at(cl, bch_data_insert_keys, bcache_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700399err:
400 /* bch_alloc_sectors() blocks if s->writeback = true */
Kent Overstreet220bb382013-09-10 19:02:45 -0700401 BUG_ON(op->writeback);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700402
403 /*
404 * But if it's not a writeback write we'd rather just bail out if
405 * there aren't any buckets ready to write to - it might take awhile and
406 * we might be starving btree writes for gc or something.
407 */
408
Kent Overstreet220bb382013-09-10 19:02:45 -0700409 if (!op->replace) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700410 /*
411 * Writethrough write: We can't complete the write until we've
412 * updated the index. But we don't want to delay the write while
413 * we wait for buckets to be freed up, so just invalidate the
414 * rest of the write.
415 */
Kent Overstreet220bb382013-09-10 19:02:45 -0700416 op->bypass = true;
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700417 return bch_data_invalidate(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700418 } else {
419 /*
420 * From a cache miss, we can just insert the keys for the data
421 * we have written or bail out if we didn't do anything.
422 */
Kent Overstreet220bb382013-09-10 19:02:45 -0700423 op->insert_data_done = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700424 bio_put(bio);
425
Kent Overstreet220bb382013-09-10 19:02:45 -0700426 if (!bch_keylist_empty(&op->insert_keys))
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700427 continue_at(cl, bch_data_insert_keys, bcache_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700428 else
429 closure_return(cl);
430 }
431}
432
433/**
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700434 * bch_data_insert - stick some data in the cache
Kent Overstreetcafe5632013-03-23 16:11:31 -0700435 *
436 * This is the starting point for any data to end up in a cache device; it could
437 * be from a normal write, or a writeback write, or a write to a flash only
438 * volume - it's also used by the moving garbage collector to compact data in
439 * mostly empty buckets.
440 *
441 * It first writes the data to the cache, creating a list of keys to be inserted
442 * (if the data had to be fragmented there will be multiple keys); after the
443 * data is written it calls bch_journal, and after the keys have been added to
444 * the next journal write they're inserted into the btree.
445 *
Kent Overstreetc18536a2013-07-24 17:44:17 -0700446 * It inserts the data in s->cache_bio; bi_sector is used for the key offset,
Kent Overstreetcafe5632013-03-23 16:11:31 -0700447 * and op->inode is used for the key inode.
448 *
Kent Overstreetc18536a2013-07-24 17:44:17 -0700449 * If s->bypass is true, instead of inserting the data it invalidates the
450 * region of the cache represented by s->cache_bio and op->inode.
Kent Overstreetcafe5632013-03-23 16:11:31 -0700451 */
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700452void bch_data_insert(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700453{
Kent Overstreet220bb382013-09-10 19:02:45 -0700454 struct data_insert_op *op = container_of(cl, struct data_insert_op, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700455
Kent Overstreet220bb382013-09-10 19:02:45 -0700456 trace_bcache_write(op->bio, op->writeback, op->bypass);
457
458 bch_keylist_init(&op->insert_keys);
459 bio_get(op->bio);
Kent Overstreeta34a8bf2013-10-24 17:07:04 -0700460 bch_data_insert_start(cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700461}
462
Kent Overstreet220bb382013-09-10 19:02:45 -0700463/* Congested? */
Kent Overstreetcafe5632013-03-23 16:11:31 -0700464
Kent Overstreet84f0db02013-07-24 17:24:52 -0700465unsigned bch_get_congested(struct cache_set *c)
466{
467 int i;
468 long rand;
469
470 if (!c->congested_read_threshold_us &&
471 !c->congested_write_threshold_us)
472 return 0;
473
474 i = (local_clock_us() - c->congested_last_us) / 1024;
475 if (i < 0)
476 return 0;
477
478 i += atomic_read(&c->congested);
479 if (i >= 0)
480 return 0;
481
482 i += CONGESTED_MAX;
483
484 if (i > 0)
485 i = fract_exp_two(i, 6);
486
487 rand = get_random_int();
488 i -= bitmap_weight(&rand, BITS_PER_LONG);
489
490 return i > 0 ? i : 1;
491}
492
493static void add_sequential(struct task_struct *t)
494{
495 ewma_add(t->sequential_io_avg,
496 t->sequential_io, 8, 0);
497
498 t->sequential_io = 0;
499}
500
501static struct hlist_head *iohash(struct cached_dev *dc, uint64_t k)
502{
503 return &dc->io_hash[hash_64(k, RECENT_IO_BITS)];
504}
505
Kent Overstreet220bb382013-09-10 19:02:45 -0700506static bool check_should_bypass(struct cached_dev *dc, struct bio *bio)
Kent Overstreet84f0db02013-07-24 17:24:52 -0700507{
Kent Overstreet220bb382013-09-10 19:02:45 -0700508 struct cache_set *c = dc->disk.c;
Kent Overstreet84f0db02013-07-24 17:24:52 -0700509 unsigned mode = cache_mode(dc, bio);
510 unsigned sectors, congested = bch_get_congested(c);
Kent Overstreet220bb382013-09-10 19:02:45 -0700511 struct task_struct *task = current;
Kent Overstreet84f0db02013-07-24 17:24:52 -0700512
513 if (atomic_read(&dc->disk.detaching) ||
514 c->gc_stats.in_use > CUTOFF_CACHE_ADD ||
515 (bio->bi_rw & REQ_DISCARD))
516 goto skip;
517
518 if (mode == CACHE_MODE_NONE ||
519 (mode == CACHE_MODE_WRITEAROUND &&
520 (bio->bi_rw & REQ_WRITE)))
521 goto skip;
522
523 if (bio->bi_sector & (c->sb.block_size - 1) ||
524 bio_sectors(bio) & (c->sb.block_size - 1)) {
525 pr_debug("skipping unaligned io");
526 goto skip;
527 }
528
529 if (!congested && !dc->sequential_cutoff)
530 goto rescale;
531
532 if (!congested &&
533 mode == CACHE_MODE_WRITEBACK &&
534 (bio->bi_rw & REQ_WRITE) &&
535 (bio->bi_rw & REQ_SYNC))
536 goto rescale;
537
538 if (dc->sequential_merge) {
539 struct io *i;
540
541 spin_lock(&dc->io_lock);
542
543 hlist_for_each_entry(i, iohash(dc, bio->bi_sector), hash)
544 if (i->last == bio->bi_sector &&
545 time_before(jiffies, i->jiffies))
546 goto found;
547
548 i = list_first_entry(&dc->io_lru, struct io, lru);
549
Kent Overstreet220bb382013-09-10 19:02:45 -0700550 add_sequential(task);
Kent Overstreet84f0db02013-07-24 17:24:52 -0700551 i->sequential = 0;
552found:
553 if (i->sequential + bio->bi_size > i->sequential)
554 i->sequential += bio->bi_size;
555
556 i->last = bio_end_sector(bio);
557 i->jiffies = jiffies + msecs_to_jiffies(5000);
Kent Overstreet220bb382013-09-10 19:02:45 -0700558 task->sequential_io = i->sequential;
Kent Overstreet84f0db02013-07-24 17:24:52 -0700559
560 hlist_del(&i->hash);
561 hlist_add_head(&i->hash, iohash(dc, i->last));
562 list_move_tail(&i->lru, &dc->io_lru);
563
564 spin_unlock(&dc->io_lock);
565 } else {
Kent Overstreet220bb382013-09-10 19:02:45 -0700566 task->sequential_io = bio->bi_size;
Kent Overstreet84f0db02013-07-24 17:24:52 -0700567
Kent Overstreet220bb382013-09-10 19:02:45 -0700568 add_sequential(task);
Kent Overstreet84f0db02013-07-24 17:24:52 -0700569 }
570
Kent Overstreet220bb382013-09-10 19:02:45 -0700571 sectors = max(task->sequential_io,
572 task->sequential_io_avg) >> 9;
Kent Overstreet84f0db02013-07-24 17:24:52 -0700573
574 if (dc->sequential_cutoff &&
575 sectors >= dc->sequential_cutoff >> 9) {
Kent Overstreet220bb382013-09-10 19:02:45 -0700576 trace_bcache_bypass_sequential(bio);
Kent Overstreet84f0db02013-07-24 17:24:52 -0700577 goto skip;
578 }
579
580 if (congested && sectors >= congested) {
Kent Overstreet220bb382013-09-10 19:02:45 -0700581 trace_bcache_bypass_congested(bio);
Kent Overstreet84f0db02013-07-24 17:24:52 -0700582 goto skip;
583 }
584
585rescale:
586 bch_rescale_priorities(c, bio_sectors(bio));
587 return false;
588skip:
Kent Overstreet220bb382013-09-10 19:02:45 -0700589 bch_mark_sectors_bypassed(c, dc, bio_sectors(bio));
Kent Overstreet84f0db02013-07-24 17:24:52 -0700590 return true;
591}
592
Kent Overstreet220bb382013-09-10 19:02:45 -0700593/* Cache lookup */
594
595struct search {
596 /* Stack frame for bio_complete */
597 struct closure cl;
598
599 struct bcache_device *d;
600
601 struct bbio bio;
602 struct bio *orig_bio;
603 struct bio *cache_miss;
604
605 unsigned insert_bio_sectors;
606
607 unsigned recoverable:1;
608 unsigned unaligned_bvec:1;
609 unsigned write:1;
610
611 unsigned long start_time;
612
613 struct btree_op op;
614 struct data_insert_op iop;
615};
616
617static void bch_cache_read_endio(struct bio *bio, int error)
618{
619 struct bbio *b = container_of(bio, struct bbio, bio);
620 struct closure *cl = bio->bi_private;
621 struct search *s = container_of(cl, struct search, cl);
622
623 /*
624 * If the bucket was reused while our bio was in flight, we might have
625 * read the wrong data. Set s->error but not error so it doesn't get
626 * counted against the cache device, but we'll still reread the data
627 * from the backing device.
628 */
629
630 if (error)
631 s->iop.error = error;
632 else if (ptr_stale(s->iop.c, &b->key, 0)) {
633 atomic_long_inc(&s->iop.c->cache_read_races);
634 s->iop.error = -EINTR;
635 }
636
637 bch_bbio_endio(s->iop.c, bio, error, "reading from cache");
638}
639
640/*
641 * Read from a single key, handling the initial cache miss if the key starts in
642 * the middle of the bio
643 */
644static int cache_lookup_fn(struct btree_op *op, struct btree *b, struct bkey *k)
645{
646 struct search *s = container_of(op, struct search, op);
647 struct bio *n, *bio = &s->bio.bio;
648 struct bkey *bio_key;
649 unsigned ptr;
650
651 if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_sector, 0)) <= 0)
652 return MAP_CONTINUE;
653
654 if (KEY_INODE(k) != s->iop.inode ||
655 KEY_START(k) > bio->bi_sector) {
656 unsigned bio_sectors = bio_sectors(bio);
657 unsigned sectors = KEY_INODE(k) == s->iop.inode
658 ? min_t(uint64_t, INT_MAX,
659 KEY_START(k) - bio->bi_sector)
660 : INT_MAX;
661
662 int ret = s->d->cache_miss(b, s, bio, sectors);
663 if (ret != MAP_CONTINUE)
664 return ret;
665
666 /* if this was a complete miss we shouldn't get here */
667 BUG_ON(bio_sectors <= sectors);
668 }
669
670 if (!KEY_SIZE(k))
671 return MAP_CONTINUE;
672
673 /* XXX: figure out best pointer - for multiple cache devices */
674 ptr = 0;
675
676 PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO;
677
678 n = bch_bio_split(bio, min_t(uint64_t, INT_MAX,
679 KEY_OFFSET(k) - bio->bi_sector),
680 GFP_NOIO, s->d->bio_split);
681
682 bio_key = &container_of(n, struct bbio, bio)->key;
683 bch_bkey_copy_single_ptr(bio_key, k, ptr);
684
685 bch_cut_front(&KEY(s->iop.inode, n->bi_sector, 0), bio_key);
686 bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key);
687
688 n->bi_end_io = bch_cache_read_endio;
689 n->bi_private = &s->cl;
690
691 /*
692 * The bucket we're reading from might be reused while our bio
693 * is in flight, and we could then end up reading the wrong
694 * data.
695 *
696 * We guard against this by checking (in cache_read_endio()) if
697 * the pointer is stale again; if so, we treat it as an error
698 * and reread from the backing device (but we don't pass that
699 * error up anywhere).
700 */
701
702 __bch_submit_bbio(n, b->c);
703 return n == bio ? MAP_DONE : MAP_CONTINUE;
704}
705
706static void cache_lookup(struct closure *cl)
707{
708 struct search *s = container_of(cl, struct search, iop.cl);
709 struct bio *bio = &s->bio.bio;
710
711 int ret = bch_btree_map_keys(&s->op, s->iop.c,
712 &KEY(s->iop.inode, bio->bi_sector, 0),
713 cache_lookup_fn, MAP_END_KEY);
714 if (ret == -EAGAIN)
715 continue_at(cl, cache_lookup, bcache_wq);
716
717 closure_return(cl);
718}
719
720/* Common code for the make_request functions */
721
722static void request_endio(struct bio *bio, int error)
723{
724 struct closure *cl = bio->bi_private;
725
726 if (error) {
727 struct search *s = container_of(cl, struct search, cl);
728 s->iop.error = error;
729 /* Only cache read errors are recoverable */
730 s->recoverable = false;
731 }
732
733 bio_put(bio);
734 closure_put(cl);
735}
736
737static void bio_complete(struct search *s)
738{
739 if (s->orig_bio) {
740 int cpu, rw = bio_data_dir(s->orig_bio);
741 unsigned long duration = jiffies - s->start_time;
742
743 cpu = part_stat_lock();
744 part_round_stats(cpu, &s->d->disk->part0);
745 part_stat_add(cpu, &s->d->disk->part0, ticks[rw], duration);
746 part_stat_unlock();
747
748 trace_bcache_request_end(s->d, s->orig_bio);
749 bio_endio(s->orig_bio, s->iop.error);
750 s->orig_bio = NULL;
751 }
752}
753
754static void do_bio_hook(struct search *s)
755{
756 struct bio *bio = &s->bio.bio;
757 memcpy(bio, s->orig_bio, sizeof(struct bio));
758
759 bio->bi_end_io = request_endio;
760 bio->bi_private = &s->cl;
761 atomic_set(&bio->bi_cnt, 3);
762}
763
764static void search_free(struct closure *cl)
765{
766 struct search *s = container_of(cl, struct search, cl);
767 bio_complete(s);
768
769 if (s->iop.bio)
770 bio_put(s->iop.bio);
771
772 if (s->unaligned_bvec)
773 mempool_free(s->bio.bio.bi_io_vec, s->d->unaligned_bvec);
774
775 closure_debug_destroy(cl);
776 mempool_free(s, s->d->c->search);
777}
778
779static struct search *search_alloc(struct bio *bio, struct bcache_device *d)
780{
781 struct search *s;
782 struct bio_vec *bv;
783
784 s = mempool_alloc(d->c->search, GFP_NOIO);
785 memset(s, 0, offsetof(struct search, iop.insert_keys));
786
787 __closure_init(&s->cl, NULL);
788
789 s->iop.inode = d->id;
790 s->iop.c = d->c;
791 s->d = d;
792 s->op.lock = -1;
Kent Overstreet2599b532013-07-24 18:11:11 -0700793 s->iop.write_point = hash_long((unsigned long) current, 16);
Kent Overstreet220bb382013-09-10 19:02:45 -0700794 s->orig_bio = bio;
795 s->write = (bio->bi_rw & REQ_WRITE) != 0;
796 s->iop.flush_journal = (bio->bi_rw & (REQ_FLUSH|REQ_FUA)) != 0;
797 s->recoverable = 1;
798 s->start_time = jiffies;
799 do_bio_hook(s);
800
801 if (bio->bi_size != bio_segments(bio) * PAGE_SIZE) {
802 bv = mempool_alloc(d->unaligned_bvec, GFP_NOIO);
803 memcpy(bv, bio_iovec(bio),
804 sizeof(struct bio_vec) * bio_segments(bio));
805
806 s->bio.bio.bi_io_vec = bv;
807 s->unaligned_bvec = 1;
808 }
809
810 return s;
811}
812
813/* Cached devices */
814
815static void cached_dev_bio_complete(struct closure *cl)
816{
817 struct search *s = container_of(cl, struct search, cl);
818 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
819
820 search_free(cl);
821 cached_dev_put(dc);
822}
823
Kent Overstreetcafe5632013-03-23 16:11:31 -0700824/* Process reads */
825
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700826static void cached_dev_cache_miss_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700827{
828 struct search *s = container_of(cl, struct search, cl);
829
Kent Overstreet220bb382013-09-10 19:02:45 -0700830 if (s->iop.replace_collision)
831 bch_mark_cache_miss_collision(s->iop.c, s->d);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700832
Kent Overstreet220bb382013-09-10 19:02:45 -0700833 if (s->iop.bio) {
Kent Overstreetcafe5632013-03-23 16:11:31 -0700834 int i;
835 struct bio_vec *bv;
836
Kent Overstreet220bb382013-09-10 19:02:45 -0700837 bio_for_each_segment_all(bv, s->iop.bio, i)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700838 __free_page(bv->bv_page);
839 }
840
841 cached_dev_bio_complete(cl);
842}
843
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700844static void cached_dev_read_error(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700845{
846 struct search *s = container_of(cl, struct search, cl);
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700847 struct bio *bio = &s->bio.bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700848 struct bio_vec *bv;
849 int i;
850
851 if (s->recoverable) {
Kent Overstreetc37511b2013-04-26 15:39:55 -0700852 /* Retry from the backing device: */
853 trace_bcache_read_retry(s->orig_bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700854
Kent Overstreet220bb382013-09-10 19:02:45 -0700855 s->iop.error = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700856 bv = s->bio.bio.bi_io_vec;
857 do_bio_hook(s);
858 s->bio.bio.bi_io_vec = bv;
859
860 if (!s->unaligned_bvec)
861 bio_for_each_segment(bv, s->orig_bio, i)
862 bv->bv_offset = 0, bv->bv_len = PAGE_SIZE;
863 else
864 memcpy(s->bio.bio.bi_io_vec,
865 bio_iovec(s->orig_bio),
866 sizeof(struct bio_vec) *
867 bio_segments(s->orig_bio));
868
869 /* XXX: invalidate cache */
870
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700871 closure_bio_submit(bio, cl, s->d);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700872 }
873
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700874 continue_at(cl, cached_dev_cache_miss_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700875}
876
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700877static void cached_dev_read_done(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700878{
879 struct search *s = container_of(cl, struct search, cl);
880 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
881
882 /*
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700883 * We had a cache miss; cache_bio now contains data ready to be inserted
884 * into the cache.
Kent Overstreetcafe5632013-03-23 16:11:31 -0700885 *
886 * First, we copy the data we just read from cache_bio's bounce buffers
887 * to the buffers the original bio pointed to:
888 */
889
Kent Overstreet220bb382013-09-10 19:02:45 -0700890 if (s->iop.bio) {
891 bio_reset(s->iop.bio);
892 s->iop.bio->bi_sector = s->cache_miss->bi_sector;
893 s->iop.bio->bi_bdev = s->cache_miss->bi_bdev;
894 s->iop.bio->bi_size = s->insert_bio_sectors << 9;
895 bch_bio_map(s->iop.bio, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700896
Kent Overstreet220bb382013-09-10 19:02:45 -0700897 bio_copy_data(s->cache_miss, s->iop.bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700898
899 bio_put(s->cache_miss);
900 s->cache_miss = NULL;
901 }
902
Kent Overstreet220bb382013-09-10 19:02:45 -0700903 if (verify(dc, &s->bio.bio) && s->recoverable && !s->unaligned_bvec)
904 bch_data_verify(dc, s->orig_bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700905
906 bio_complete(s);
907
Kent Overstreet220bb382013-09-10 19:02:45 -0700908 if (s->iop.bio &&
909 !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) {
910 BUG_ON(!s->iop.replace);
911 closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700912 }
913
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700914 continue_at(cl, cached_dev_cache_miss_done, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700915}
916
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700917static void cached_dev_read_done_bh(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700918{
919 struct search *s = container_of(cl, struct search, cl);
920 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
921
Kent Overstreet220bb382013-09-10 19:02:45 -0700922 bch_mark_cache_accounting(s->iop.c, s->d,
923 !s->cache_miss, s->iop.bypass);
924 trace_bcache_read(s->orig_bio, !s->cache_miss, s->iop.bypass);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700925
Kent Overstreet220bb382013-09-10 19:02:45 -0700926 if (s->iop.error)
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700927 continue_at_nobarrier(cl, cached_dev_read_error, bcache_wq);
Kent Overstreet220bb382013-09-10 19:02:45 -0700928 else if (s->iop.bio || verify(dc, &s->bio.bio))
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700929 continue_at_nobarrier(cl, cached_dev_read_done, bcache_wq);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700930 else
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700931 continue_at_nobarrier(cl, cached_dev_bio_complete, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700932}
933
934static int cached_dev_cache_miss(struct btree *b, struct search *s,
935 struct bio *bio, unsigned sectors)
936{
Kent Overstreet2c1953e2013-07-24 17:41:08 -0700937 int ret = MAP_CONTINUE;
Kent Overstreete7c590e2013-09-10 18:39:16 -0700938 unsigned reada = 0;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700939 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700940 struct bio *miss, *cache_bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700941
Kent Overstreet220bb382013-09-10 19:02:45 -0700942 if (s->cache_miss || s->iop.bypass) {
Kent Overstreete7c590e2013-09-10 18:39:16 -0700943 miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split);
Kent Overstreet2c1953e2013-07-24 17:41:08 -0700944 ret = miss == bio ? MAP_DONE : MAP_CONTINUE;
Kent Overstreete7c590e2013-09-10 18:39:16 -0700945 goto out_submit;
946 }
947
948 if (!(bio->bi_rw & REQ_RAHEAD) &&
949 !(bio->bi_rw & REQ_META) &&
Kent Overstreet220bb382013-09-10 19:02:45 -0700950 s->iop.c->gc_stats.in_use < CUTOFF_CACHE_READA)
Kent Overstreete7c590e2013-09-10 18:39:16 -0700951 reada = min_t(sector_t, dc->readahead >> 9,
952 bdev_sectors(bio->bi_bdev) - bio_end_sector(bio));
953
Kent Overstreet220bb382013-09-10 19:02:45 -0700954 s->insert_bio_sectors = min(sectors, bio_sectors(bio) + reada);
Kent Overstreete7c590e2013-09-10 18:39:16 -0700955
Kent Overstreet220bb382013-09-10 19:02:45 -0700956 s->iop.replace_key = KEY(s->iop.inode,
957 bio->bi_sector + s->insert_bio_sectors,
958 s->insert_bio_sectors);
Kent Overstreete7c590e2013-09-10 18:39:16 -0700959
Kent Overstreet220bb382013-09-10 19:02:45 -0700960 ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key);
Kent Overstreete7c590e2013-09-10 18:39:16 -0700961 if (ret)
962 return ret;
963
Kent Overstreet220bb382013-09-10 19:02:45 -0700964 s->iop.replace = true;
Kent Overstreet1b207d82013-09-10 18:52:54 -0700965
Kent Overstreetcafe5632013-03-23 16:11:31 -0700966 miss = bch_bio_split(bio, sectors, GFP_NOIO, s->d->bio_split);
Kent Overstreet2c1953e2013-07-24 17:41:08 -0700967
968 /* btree_search_recurse()'s btree iterator is no good anymore */
969 ret = miss == bio ? MAP_DONE : -EINTR;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700970
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700971 cache_bio = bio_alloc_bioset(GFP_NOWAIT,
Kent Overstreet220bb382013-09-10 19:02:45 -0700972 DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS),
Kent Overstreetcafe5632013-03-23 16:11:31 -0700973 dc->disk.bio_split);
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700974 if (!cache_bio)
Kent Overstreetcafe5632013-03-23 16:11:31 -0700975 goto out_submit;
976
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700977 cache_bio->bi_sector = miss->bi_sector;
978 cache_bio->bi_bdev = miss->bi_bdev;
Kent Overstreet220bb382013-09-10 19:02:45 -0700979 cache_bio->bi_size = s->insert_bio_sectors << 9;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700980
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700981 cache_bio->bi_end_io = request_endio;
982 cache_bio->bi_private = &s->cl;
Kent Overstreetcafe5632013-03-23 16:11:31 -0700983
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700984 bch_bio_map(cache_bio, NULL);
985 if (bio_alloc_pages(cache_bio, __GFP_NOWARN|GFP_NOIO))
Kent Overstreetcafe5632013-03-23 16:11:31 -0700986 goto out_put;
987
Kent Overstreet220bb382013-09-10 19:02:45 -0700988 if (reada)
989 bch_mark_cache_readahead(s->iop.c, s->d);
990
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700991 s->cache_miss = miss;
Kent Overstreet220bb382013-09-10 19:02:45 -0700992 s->iop.bio = cache_bio;
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700993 bio_get(cache_bio);
994 closure_bio_submit(cache_bio, &s->cl, s->d);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700995
996 return ret;
997out_put:
Kent Overstreetcdd972b2013-09-10 17:06:17 -0700998 bio_put(cache_bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -0700999out_submit:
Kent Overstreete7c590e2013-09-10 18:39:16 -07001000 miss->bi_end_io = request_endio;
1001 miss->bi_private = &s->cl;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001002 closure_bio_submit(miss, &s->cl, s->d);
1003 return ret;
1004}
1005
Kent Overstreetcdd972b2013-09-10 17:06:17 -07001006static void cached_dev_read(struct cached_dev *dc, struct search *s)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001007{
1008 struct closure *cl = &s->cl;
1009
Kent Overstreet220bb382013-09-10 19:02:45 -07001010 closure_call(&s->iop.cl, cache_lookup, NULL, cl);
Kent Overstreetcdd972b2013-09-10 17:06:17 -07001011 continue_at(cl, cached_dev_read_done_bh, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001012}
1013
1014/* Process writes */
1015
1016static void cached_dev_write_complete(struct closure *cl)
1017{
1018 struct search *s = container_of(cl, struct search, cl);
1019 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk);
1020
1021 up_read_non_owner(&dc->writeback_lock);
1022 cached_dev_bio_complete(cl);
1023}
1024
Kent Overstreetcdd972b2013-09-10 17:06:17 -07001025static void cached_dev_write(struct cached_dev *dc, struct search *s)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001026{
1027 struct closure *cl = &s->cl;
1028 struct bio *bio = &s->bio.bio;
Kent Overstreet84f0db02013-07-24 17:24:52 -07001029 struct bkey start = KEY(dc->disk.id, bio->bi_sector, 0);
1030 struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001031
Kent Overstreet220bb382013-09-10 19:02:45 -07001032 bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001033
Kent Overstreetcafe5632013-03-23 16:11:31 -07001034 down_read_non_owner(&dc->writeback_lock);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001035 if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) {
Kent Overstreet84f0db02013-07-24 17:24:52 -07001036 /*
1037 * We overlap with some dirty data undergoing background
1038 * writeback, force this write to writeback
1039 */
Kent Overstreet220bb382013-09-10 19:02:45 -07001040 s->iop.bypass = false;
1041 s->iop.writeback = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001042 }
1043
Kent Overstreet84f0db02013-07-24 17:24:52 -07001044 /*
1045 * Discards aren't _required_ to do anything, so skipping if
1046 * check_overlapping returned true is ok
1047 *
1048 * But check_overlapping drops dirty keys for which io hasn't started,
1049 * so we still want to call it.
1050 */
Kent Overstreetcafe5632013-03-23 16:11:31 -07001051 if (bio->bi_rw & REQ_DISCARD)
Kent Overstreet220bb382013-09-10 19:02:45 -07001052 s->iop.bypass = true;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001053
Kent Overstreet72c27062013-06-05 06:24:39 -07001054 if (should_writeback(dc, s->orig_bio,
1055 cache_mode(dc, bio),
Kent Overstreet220bb382013-09-10 19:02:45 -07001056 s->iop.bypass)) {
1057 s->iop.bypass = false;
1058 s->iop.writeback = true;
Kent Overstreet72c27062013-06-05 06:24:39 -07001059 }
1060
Kent Overstreet220bb382013-09-10 19:02:45 -07001061 if (s->iop.bypass) {
1062 s->iop.bio = s->orig_bio;
1063 bio_get(s->iop.bio);
Kent Overstreetc37511b2013-04-26 15:39:55 -07001064
Kent Overstreet84f0db02013-07-24 17:24:52 -07001065 if (!(bio->bi_rw & REQ_DISCARD) ||
1066 blk_queue_discard(bdev_get_queue(dc->bdev)))
1067 closure_bio_submit(bio, cl, s->d);
Kent Overstreet220bb382013-09-10 19:02:45 -07001068 } else if (s->iop.writeback) {
Kent Overstreet279afba2013-06-05 06:21:07 -07001069 bch_writeback_add(dc);
Kent Overstreet220bb382013-09-10 19:02:45 -07001070 s->iop.bio = bio;
Kent Overstreete49c7c32013-06-26 17:25:38 -07001071
Kent Overstreetc0f04d82013-09-23 23:17:36 -07001072 if (bio->bi_rw & REQ_FLUSH) {
Kent Overstreete49c7c32013-06-26 17:25:38 -07001073 /* Also need to send a flush to the backing device */
Kent Overstreetd4eddd42013-10-22 15:35:50 -07001074 struct bio *flush = bio_alloc_bioset(GFP_NOIO, 0,
Kent Overstreetc0f04d82013-09-23 23:17:36 -07001075 dc->disk.bio_split);
Kent Overstreete49c7c32013-06-26 17:25:38 -07001076
Kent Overstreetc0f04d82013-09-23 23:17:36 -07001077 flush->bi_rw = WRITE_FLUSH;
1078 flush->bi_bdev = bio->bi_bdev;
1079 flush->bi_end_io = request_endio;
1080 flush->bi_private = cl;
1081
1082 closure_bio_submit(flush, cl, s->d);
Kent Overstreete49c7c32013-06-26 17:25:38 -07001083 }
Kent Overstreet84f0db02013-07-24 17:24:52 -07001084 } else {
Kent Overstreet220bb382013-09-10 19:02:45 -07001085 s->iop.bio = bio_clone_bioset(bio, GFP_NOIO,
1086 dc->disk.bio_split);
Kent Overstreet84f0db02013-07-24 17:24:52 -07001087
1088 closure_bio_submit(bio, cl, s->d);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001089 }
Kent Overstreet84f0db02013-07-24 17:24:52 -07001090
Kent Overstreet220bb382013-09-10 19:02:45 -07001091 closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001092 continue_at(cl, cached_dev_write_complete, NULL);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001093}
1094
Kent Overstreeta34a8bf2013-10-24 17:07:04 -07001095static void cached_dev_nodata(struct closure *cl)
Kent Overstreetcafe5632013-03-23 16:11:31 -07001096{
Kent Overstreeta34a8bf2013-10-24 17:07:04 -07001097 struct search *s = container_of(cl, struct search, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001098 struct bio *bio = &s->bio.bio;
1099
Kent Overstreet220bb382013-09-10 19:02:45 -07001100 if (s->iop.flush_journal)
1101 bch_journal_meta(s->iop.c, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001102
Kent Overstreet84f0db02013-07-24 17:24:52 -07001103 /* If it's a flush, we send the flush to the backing device too */
Kent Overstreetcafe5632013-03-23 16:11:31 -07001104 closure_bio_submit(bio, cl, s->d);
1105
1106 continue_at(cl, cached_dev_bio_complete, NULL);
1107}
1108
1109/* Cached devices - read & write stuff */
1110
Kent Overstreetcafe5632013-03-23 16:11:31 -07001111static void cached_dev_make_request(struct request_queue *q, struct bio *bio)
1112{
1113 struct search *s;
1114 struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
1115 struct cached_dev *dc = container_of(d, struct cached_dev, disk);
1116 int cpu, rw = bio_data_dir(bio);
1117
1118 cpu = part_stat_lock();
1119 part_stat_inc(cpu, &d->disk->part0, ios[rw]);
1120 part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
1121 part_stat_unlock();
1122
1123 bio->bi_bdev = dc->bdev;
Kent Overstreet29033812013-04-11 15:14:35 -07001124 bio->bi_sector += dc->sb.data_offset;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001125
1126 if (cached_dev_get(dc)) {
1127 s = search_alloc(bio, d);
Kent Overstreet220bb382013-09-10 19:02:45 -07001128 trace_bcache_request_start(s->d, bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001129
Kent Overstreeta34a8bf2013-10-24 17:07:04 -07001130 if (!bio->bi_size) {
1131 /*
1132 * can't call bch_journal_meta from under
1133 * generic_make_request
1134 */
1135 continue_at_nobarrier(&s->cl,
1136 cached_dev_nodata,
1137 bcache_wq);
1138 } else {
Kent Overstreet220bb382013-09-10 19:02:45 -07001139 s->iop.bypass = check_should_bypass(dc, bio);
Kent Overstreet84f0db02013-07-24 17:24:52 -07001140
1141 if (rw)
Kent Overstreetcdd972b2013-09-10 17:06:17 -07001142 cached_dev_write(dc, s);
Kent Overstreet84f0db02013-07-24 17:24:52 -07001143 else
Kent Overstreetcdd972b2013-09-10 17:06:17 -07001144 cached_dev_read(dc, s);
Kent Overstreet84f0db02013-07-24 17:24:52 -07001145 }
Kent Overstreetcafe5632013-03-23 16:11:31 -07001146 } else {
1147 if ((bio->bi_rw & REQ_DISCARD) &&
1148 !blk_queue_discard(bdev_get_queue(dc->bdev)))
1149 bio_endio(bio, 0);
1150 else
1151 bch_generic_make_request(bio, &d->bio_split_hook);
1152 }
1153}
1154
1155static int cached_dev_ioctl(struct bcache_device *d, fmode_t mode,
1156 unsigned int cmd, unsigned long arg)
1157{
1158 struct cached_dev *dc = container_of(d, struct cached_dev, disk);
1159 return __blkdev_driver_ioctl(dc->bdev, mode, cmd, arg);
1160}
1161
1162static int cached_dev_congested(void *data, int bits)
1163{
1164 struct bcache_device *d = data;
1165 struct cached_dev *dc = container_of(d, struct cached_dev, disk);
1166 struct request_queue *q = bdev_get_queue(dc->bdev);
1167 int ret = 0;
1168
1169 if (bdi_congested(&q->backing_dev_info, bits))
1170 return 1;
1171
1172 if (cached_dev_get(dc)) {
1173 unsigned i;
1174 struct cache *ca;
1175
1176 for_each_cache(ca, d->c, i) {
1177 q = bdev_get_queue(ca->bdev);
1178 ret |= bdi_congested(&q->backing_dev_info, bits);
1179 }
1180
1181 cached_dev_put(dc);
1182 }
1183
1184 return ret;
1185}
1186
1187void bch_cached_dev_request_init(struct cached_dev *dc)
1188{
1189 struct gendisk *g = dc->disk.disk;
1190
1191 g->queue->make_request_fn = cached_dev_make_request;
1192 g->queue->backing_dev_info.congested_fn = cached_dev_congested;
1193 dc->disk.cache_miss = cached_dev_cache_miss;
1194 dc->disk.ioctl = cached_dev_ioctl;
1195}
1196
1197/* Flash backed devices */
1198
1199static int flash_dev_cache_miss(struct btree *b, struct search *s,
1200 struct bio *bio, unsigned sectors)
1201{
Kent Overstreet8e51e412013-06-06 18:15:57 -07001202 struct bio_vec *bv;
1203 int i;
1204
Kent Overstreetcafe5632013-03-23 16:11:31 -07001205 /* Zero fill bio */
1206
Kent Overstreet8e51e412013-06-06 18:15:57 -07001207 bio_for_each_segment(bv, bio, i) {
Kent Overstreetcafe5632013-03-23 16:11:31 -07001208 unsigned j = min(bv->bv_len >> 9, sectors);
1209
1210 void *p = kmap(bv->bv_page);
1211 memset(p + bv->bv_offset, 0, j << 9);
1212 kunmap(bv->bv_page);
1213
Kent Overstreet8e51e412013-06-06 18:15:57 -07001214 sectors -= j;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001215 }
1216
Kent Overstreet8e51e412013-06-06 18:15:57 -07001217 bio_advance(bio, min(sectors << 9, bio->bi_size));
1218
1219 if (!bio->bi_size)
Kent Overstreet2c1953e2013-07-24 17:41:08 -07001220 return MAP_DONE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001221
Kent Overstreet2c1953e2013-07-24 17:41:08 -07001222 return MAP_CONTINUE;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001223}
1224
Kent Overstreeta34a8bf2013-10-24 17:07:04 -07001225static void flash_dev_nodata(struct closure *cl)
1226{
1227 struct search *s = container_of(cl, struct search, cl);
1228
Kent Overstreet220bb382013-09-10 19:02:45 -07001229 if (s->iop.flush_journal)
1230 bch_journal_meta(s->iop.c, cl);
Kent Overstreeta34a8bf2013-10-24 17:07:04 -07001231
1232 continue_at(cl, search_free, NULL);
1233}
1234
Kent Overstreetcafe5632013-03-23 16:11:31 -07001235static void flash_dev_make_request(struct request_queue *q, struct bio *bio)
1236{
1237 struct search *s;
1238 struct closure *cl;
1239 struct bcache_device *d = bio->bi_bdev->bd_disk->private_data;
1240 int cpu, rw = bio_data_dir(bio);
1241
1242 cpu = part_stat_lock();
1243 part_stat_inc(cpu, &d->disk->part0, ios[rw]);
1244 part_stat_add(cpu, &d->disk->part0, sectors[rw], bio_sectors(bio));
1245 part_stat_unlock();
1246
1247 s = search_alloc(bio, d);
1248 cl = &s->cl;
1249 bio = &s->bio.bio;
1250
Kent Overstreet220bb382013-09-10 19:02:45 -07001251 trace_bcache_request_start(s->d, bio);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001252
Kent Overstreet84f0db02013-07-24 17:24:52 -07001253 if (!bio->bi_size) {
Kent Overstreeta34a8bf2013-10-24 17:07:04 -07001254 /*
1255 * can't call bch_journal_meta from under
1256 * generic_make_request
1257 */
1258 continue_at_nobarrier(&s->cl,
1259 flash_dev_nodata,
1260 bcache_wq);
Kent Overstreet84f0db02013-07-24 17:24:52 -07001261 } else if (rw) {
Kent Overstreet220bb382013-09-10 19:02:45 -07001262 bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys,
Kent Overstreet8e51e412013-06-06 18:15:57 -07001263 &KEY(d->id, bio->bi_sector, 0),
1264 &KEY(d->id, bio_end_sector(bio), 0));
Kent Overstreetcafe5632013-03-23 16:11:31 -07001265
Kent Overstreet220bb382013-09-10 19:02:45 -07001266 s->iop.bypass = (bio->bi_rw & REQ_DISCARD) != 0;
1267 s->iop.writeback = true;
1268 s->iop.bio = bio;
Kent Overstreetcafe5632013-03-23 16:11:31 -07001269
Kent Overstreet220bb382013-09-10 19:02:45 -07001270 closure_call(&s->iop.cl, bch_data_insert, NULL, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001271 } else {
Kent Overstreet220bb382013-09-10 19:02:45 -07001272 closure_call(&s->iop.cl, cache_lookup, NULL, cl);
Kent Overstreetcafe5632013-03-23 16:11:31 -07001273 }
1274
1275 continue_at(cl, search_free, NULL);
1276}
1277
1278static int flash_dev_ioctl(struct bcache_device *d, fmode_t mode,
1279 unsigned int cmd, unsigned long arg)
1280{
1281 return -ENOTTY;
1282}
1283
1284static int flash_dev_congested(void *data, int bits)
1285{
1286 struct bcache_device *d = data;
1287 struct request_queue *q;
1288 struct cache *ca;
1289 unsigned i;
1290 int ret = 0;
1291
1292 for_each_cache(ca, d->c, i) {
1293 q = bdev_get_queue(ca->bdev);
1294 ret |= bdi_congested(&q->backing_dev_info, bits);
1295 }
1296
1297 return ret;
1298}
1299
1300void bch_flash_dev_request_init(struct bcache_device *d)
1301{
1302 struct gendisk *g = d->disk;
1303
1304 g->queue->make_request_fn = flash_dev_make_request;
1305 g->queue->backing_dev_info.congested_fn = flash_dev_congested;
1306 d->cache_miss = flash_dev_cache_miss;
1307 d->ioctl = flash_dev_ioctl;
1308}
1309
1310void bch_request_exit(void)
1311{
1312#ifdef CONFIG_CGROUP_BCACHE
1313 cgroup_unload_subsys(&bcache_subsys);
1314#endif
1315 if (bch_search_cache)
1316 kmem_cache_destroy(bch_search_cache);
1317}
1318
1319int __init bch_request_init(void)
1320{
1321 bch_search_cache = KMEM_CACHE(search, 0);
1322 if (!bch_search_cache)
1323 return -ENOMEM;
1324
1325#ifdef CONFIG_CGROUP_BCACHE
1326 cgroup_load_subsys(&bcache_subsys);
1327 init_bch_cgroup(&bcache_default_cgroup);
1328
1329 cgroup_add_cftypes(&bcache_subsys, bch_files);
1330#endif
1331 return 0;
1332}