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