Lines Matching +full:- +full:s
1 // SPDX-License-Identifier: GPL-2.0
3 * Main bcache entry point - handle a read or a write request and decide what to
19 #include <linux/backing-dev.h>
32 return BDEV_CACHE_MODE(&dc->sb); in cache_mode()
37 return dc->verify; in verify()
53 k->ptr[KEY_PTRS(k)] = csum & (~0ULL >> 1); in bio_csum()
62 struct bkey *replace_key = op->replace ? &op->replace_key : NULL; in CLOSURE_CALLBACK()
65 if (!op->replace) in CLOSURE_CALLBACK()
66 journal_ref = bch_journal(op->c, &op->insert_keys, in CLOSURE_CALLBACK()
67 op->flush_journal ? cl : NULL); in CLOSURE_CALLBACK()
69 ret = bch_btree_insert(op->c, &op->insert_keys, in CLOSURE_CALLBACK()
71 if (ret == -ESRCH) { in CLOSURE_CALLBACK()
72 op->replace_collision = true; in CLOSURE_CALLBACK()
74 op->status = BLK_STS_RESOURCE; in CLOSURE_CALLBACK()
75 op->insert_data_done = true; in CLOSURE_CALLBACK()
81 if (!op->insert_data_done) { in CLOSURE_CALLBACK()
82 continue_at(cl, bch_data_insert_start, op->wq); in CLOSURE_CALLBACK()
86 bch_keylist_free(&op->insert_keys); in CLOSURE_CALLBACK()
98 * is bigger than an empty write: If we just return -ENOMEM here, in bch_keylist_realloc()
102 if (newsize * sizeof(uint64_t) > block_bytes(c->cache) - sizeof(struct jset)) in bch_keylist_realloc()
103 return -ENOMEM; in bch_keylist_realloc()
111 struct bio *bio = op->bio; in bch_data_invalidate()
114 bio_sectors(bio), (uint64_t) bio->bi_iter.bi_sector); in bch_data_invalidate()
118 1U << (KEY_SIZE_BITS - 1)); in bch_data_invalidate()
120 if (bch_keylist_realloc(&op->insert_keys, 2, op->c)) in bch_data_invalidate()
123 bio->bi_iter.bi_sector += sectors; in bch_data_invalidate()
124 bio->bi_iter.bi_size -= sectors << 9; in bch_data_invalidate()
126 bch_keylist_add(&op->insert_keys, in bch_data_invalidate()
127 &KEY(op->inode, in bch_data_invalidate()
128 bio->bi_iter.bi_sector, in bch_data_invalidate()
132 op->insert_data_done = true; in bch_data_invalidate()
136 continue_at(cl, bch_data_insert_keys, op->wq); in bch_data_invalidate()
148 * that region of the cache - so, if we just strip off all the pointers in CLOSURE_CALLBACK()
152 struct bkey *src = op->insert_keys.keys, *dst = op->insert_keys.keys; in CLOSURE_CALLBACK()
154 while (src != op->insert_keys.top) { in CLOSURE_CALLBACK()
164 op->insert_keys.top = dst; in CLOSURE_CALLBACK()
166 bch_data_insert_keys(&cl->work); in CLOSURE_CALLBACK()
171 struct closure *cl = bio->bi_private; in bch_data_insert_endio()
174 if (bio->bi_status) { in bch_data_insert_endio()
176 if (op->writeback) in bch_data_insert_endio()
177 op->status = bio->bi_status; in bch_data_insert_endio()
178 else if (!op->replace) in bch_data_insert_endio()
179 set_closure_fn(cl, bch_data_insert_error, op->wq); in bch_data_insert_endio()
184 bch_bbio_endio(op->c, bio, bio->bi_status, "writing data to cache"); in bch_data_insert_endio()
190 struct bio *bio = op->bio, *n; in CLOSURE_CALLBACK()
192 if (op->bypass) in CLOSURE_CALLBACK()
195 if (atomic_sub_return(bio_sectors(bio), &op->c->sectors_to_gc) < 0) in CLOSURE_CALLBACK()
196 wake_up_gc(op->c); in CLOSURE_CALLBACK()
202 bio->bi_opf &= ~(REQ_PREFLUSH|REQ_FUA); in CLOSURE_CALLBACK()
207 struct bio_set *split = &op->c->bio_split; in CLOSURE_CALLBACK()
210 if (bch_keylist_realloc(&op->insert_keys, in CLOSURE_CALLBACK()
211 3 + (op->csum ? 1 : 0), in CLOSURE_CALLBACK()
212 op->c)) { in CLOSURE_CALLBACK()
213 continue_at(cl, bch_data_insert_keys, op->wq); in CLOSURE_CALLBACK()
217 k = op->insert_keys.top; in CLOSURE_CALLBACK()
219 SET_KEY_INODE(k, op->inode); in CLOSURE_CALLBACK()
220 SET_KEY_OFFSET(k, bio->bi_iter.bi_sector); in CLOSURE_CALLBACK()
222 if (!bch_alloc_sectors(op->c, k, bio_sectors(bio), in CLOSURE_CALLBACK()
223 op->write_point, op->write_prio, in CLOSURE_CALLBACK()
224 op->writeback)) in CLOSURE_CALLBACK()
229 n->bi_end_io = bch_data_insert_endio; in CLOSURE_CALLBACK()
230 n->bi_private = cl; in CLOSURE_CALLBACK()
232 if (op->writeback) { in CLOSURE_CALLBACK()
236 SET_GC_MARK(PTR_BUCKET(op->c, k, i), in CLOSURE_CALLBACK()
240 SET_KEY_CSUM(k, op->csum); in CLOSURE_CALLBACK()
245 bch_keylist_push(&op->insert_keys); in CLOSURE_CALLBACK()
247 n->bi_opf = REQ_OP_WRITE; in CLOSURE_CALLBACK()
248 bch_submit_bbio(n, op->c, k, 0); in CLOSURE_CALLBACK()
251 op->insert_data_done = true; in CLOSURE_CALLBACK()
252 continue_at(cl, bch_data_insert_keys, op->wq); in CLOSURE_CALLBACK()
255 /* bch_alloc_sectors() blocks if s->writeback = true */ in CLOSURE_CALLBACK()
256 BUG_ON(op->writeback); in CLOSURE_CALLBACK()
259 * But if it's not a writeback write we'd rather just bail out if in CLOSURE_CALLBACK()
260 * there aren't any buckets ready to write to - it might take awhile and in CLOSURE_CALLBACK()
264 if (!op->replace) { in CLOSURE_CALLBACK()
271 op->bypass = true; in CLOSURE_CALLBACK()
278 op->insert_data_done = true; in CLOSURE_CALLBACK()
281 if (!bch_keylist_empty(&op->insert_keys)) in CLOSURE_CALLBACK()
282 continue_at(cl, bch_data_insert_keys, op->wq); in CLOSURE_CALLBACK()
289 * bch_data_insert - stick some data in the cache
294 * volume - it's also used by the moving garbage collector to compact data in
302 * It inserts the data in op->bio; bi_sector is used for the key offset,
303 * and op->inode is used for the key inode.
305 * If op->bypass is true, instead of inserting the data it invalidates the
306 * region of the cache represented by op->bio and op->inode.
312 trace_bcache_write(op->c, op->inode, op->bio, in CLOSURE_CALLBACK()
313 op->writeback, op->bypass); in CLOSURE_CALLBACK()
315 bch_keylist_init(&op->insert_keys); in CLOSURE_CALLBACK()
316 bio_get(op->bio); in CLOSURE_CALLBACK()
317 bch_data_insert_start(&cl->work); in CLOSURE_CALLBACK()
328 if (!c->congested_read_threshold_us && in bch_get_congested()
329 !c->congested_write_threshold_us) in bch_get_congested()
332 i = (local_clock_us() - c->congested_last_us) / 1024; in bch_get_congested()
336 i += atomic_read(&c->congested); in bch_get_congested()
345 i -= hweight32(get_random_u32()); in bch_get_congested()
352 ewma_add(t->sequential_io_avg, in add_sequential()
353 t->sequential_io, 8, 0); in add_sequential()
355 t->sequential_io = 0; in add_sequential()
360 return &dc->io_hash[hash_64(k, RECENT_IO_BITS)]; in iohash()
365 struct cache_set *c = dc->disk.c; in check_should_bypass()
371 if (test_bit(BCACHE_DEV_DETACHING, &dc->disk.flags) || in check_should_bypass()
375 if (c->gc_stats.in_use > CUTOFF_CACHE_ADD) { in check_should_bypass()
379 * Then c->sectors_to_gc has no chance to be negative, and in check_should_bypass()
383 if (BDEV_STATE(&dc->sb) == BDEV_STATE_CLEAN && in check_should_bypass()
384 c->gc_mark_valid) in check_should_bypass()
396 * If the bio is for read-ahead or background IO, bypass it or in check_should_bypass()
398 * - If the IO is for meta data, always cache it and no bypass in check_should_bypass()
399 * - If the IO is not meta data, check dc->cache_reada_policy, in check_should_bypass()
402 * That is, read-ahead request for metadata always get cached in check_should_bypass()
405 if ((bio->bi_opf & (REQ_RAHEAD|REQ_BACKGROUND))) { in check_should_bypass()
406 if (!(bio->bi_opf & (REQ_META|REQ_PRIO)) && in check_should_bypass()
407 (dc->cache_readahead_policy != BCH_CACHE_READA_ALL)) in check_should_bypass()
411 if (bio->bi_iter.bi_sector & (c->cache->sb.block_size - 1) || in check_should_bypass()
412 bio_sectors(bio) & (c->cache->sb.block_size - 1)) { in check_should_bypass()
425 if (!congested && !dc->sequential_cutoff) in check_should_bypass()
428 spin_lock(&dc->io_lock); in check_should_bypass()
430 hlist_for_each_entry(i, iohash(dc, bio->bi_iter.bi_sector), hash) in check_should_bypass()
431 if (i->last == bio->bi_iter.bi_sector && in check_should_bypass()
432 time_before(jiffies, i->jiffies)) in check_should_bypass()
435 i = list_first_entry(&dc->io_lru, struct io, lru); in check_should_bypass()
438 i->sequential = 0; in check_should_bypass()
440 if (i->sequential + bio->bi_iter.bi_size > i->sequential) in check_should_bypass()
441 i->sequential += bio->bi_iter.bi_size; in check_should_bypass()
443 i->last = bio_end_sector(bio); in check_should_bypass()
444 i->jiffies = jiffies + msecs_to_jiffies(5000); in check_should_bypass()
445 task->sequential_io = i->sequential; in check_should_bypass()
447 hlist_del(&i->hash); in check_should_bypass()
448 hlist_add_head(&i->hash, iohash(dc, i->last)); in check_should_bypass()
449 list_move_tail(&i->lru, &dc->io_lru); in check_should_bypass()
451 spin_unlock(&dc->io_lock); in check_should_bypass()
453 sectors = max(task->sequential_io, in check_should_bypass()
454 task->sequential_io_avg) >> 9; in check_should_bypass()
456 if (dc->sequential_cutoff && in check_should_bypass()
457 sectors >= dc->sequential_cutoff >> 9) { in check_should_bypass()
502 struct closure *cl = bio->bi_private; in bch_cache_read_endio()
503 struct search *s = container_of(cl, struct search, cl); in bch_cache_read_endio() local
507 * read the wrong data. Set s->error but not error so it doesn't get in bch_cache_read_endio()
512 if (bio->bi_status) in bch_cache_read_endio()
513 s->iop.status = bio->bi_status; in bch_cache_read_endio()
514 else if (!KEY_DIRTY(&b->key) && in bch_cache_read_endio()
515 ptr_stale(s->iop.c, &b->key, 0)) { in bch_cache_read_endio()
516 atomic_long_inc(&s->iop.c->cache_read_races); in bch_cache_read_endio()
517 s->iop.status = BLK_STS_IOERR; in bch_cache_read_endio()
520 bch_bbio_endio(s->iop.c, bio, bio->bi_status, "reading from cache"); in bch_cache_read_endio()
529 struct search *s = container_of(op, struct search, op); in cache_lookup_fn() local
530 struct bio *n, *bio = &s->bio.bio; in cache_lookup_fn()
534 if (bkey_cmp(k, &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0)) <= 0) in cache_lookup_fn()
537 if (KEY_INODE(k) != s->iop.inode || in cache_lookup_fn()
538 KEY_START(k) > bio->bi_iter.bi_sector) { in cache_lookup_fn()
540 unsigned int sectors = KEY_INODE(k) == s->iop.inode in cache_lookup_fn()
542 KEY_START(k) - bio->bi_iter.bi_sector) in cache_lookup_fn()
544 int ret = s->d->cache_miss(b, s, bio, sectors); in cache_lookup_fn()
556 /* XXX: figure out best pointer - for multiple cache devices */ in cache_lookup_fn()
559 PTR_BUCKET(b->c, k, ptr)->prio = INITIAL_PRIO; in cache_lookup_fn()
562 s->read_dirty_data = true; in cache_lookup_fn()
565 KEY_OFFSET(k) - bio->bi_iter.bi_sector), in cache_lookup_fn()
566 GFP_NOIO, &s->d->bio_split); in cache_lookup_fn()
568 bio_key = &container_of(n, struct bbio, bio)->key; in cache_lookup_fn()
571 bch_cut_front(&KEY(s->iop.inode, n->bi_iter.bi_sector, 0), bio_key); in cache_lookup_fn()
572 bch_cut_back(&KEY(s->iop.inode, bio_end_sector(n), 0), bio_key); in cache_lookup_fn()
574 n->bi_end_io = bch_cache_read_endio; in cache_lookup_fn()
575 n->bi_private = &s->cl; in cache_lookup_fn()
588 __bch_submit_bbio(n, b->c); in cache_lookup_fn()
594 closure_type(s, struct search, iop.cl); in CLOSURE_CALLBACK()
595 struct bio *bio = &s->bio.bio; in CLOSURE_CALLBACK()
599 bch_btree_op_init(&s->op, -1); in CLOSURE_CALLBACK()
601 ret = bch_btree_map_keys(&s->op, s->iop.c, in CLOSURE_CALLBACK()
602 &KEY(s->iop.inode, bio->bi_iter.bi_sector, 0), in CLOSURE_CALLBACK()
604 if (ret == -EAGAIN) { in CLOSURE_CALLBACK()
615 * And after that happened, s->iop.status is still its initial value in CLOSURE_CALLBACK()
616 * before we submit s->bio.bio in CLOSURE_CALLBACK()
619 BUG_ON(ret == -EINTR); in CLOSURE_CALLBACK()
620 if (s->d && s->d->c && in CLOSURE_CALLBACK()
621 !UUID_FLASH_ONLY(&s->d->c->uuids[s->d->id])) { in CLOSURE_CALLBACK()
622 dc = container_of(s->d, struct cached_dev, disk); in CLOSURE_CALLBACK()
623 if (dc && atomic_read(&dc->has_dirty)) in CLOSURE_CALLBACK()
624 s->recoverable = false; in CLOSURE_CALLBACK()
626 if (!s->iop.status) in CLOSURE_CALLBACK()
627 s->iop.status = BLK_STS_IOERR; in CLOSURE_CALLBACK()
637 struct closure *cl = bio->bi_private; in request_endio()
639 if (bio->bi_status) { in request_endio()
640 struct search *s = container_of(cl, struct search, cl); in request_endio() local
642 s->iop.status = bio->bi_status; in request_endio()
644 s->recoverable = false; in request_endio()
653 struct closure *cl = bio->bi_private; in backing_request_endio()
655 if (bio->bi_status) { in backing_request_endio()
656 struct search *s = container_of(cl, struct search, cl); in backing_request_endio() local
657 struct cached_dev *dc = container_of(s->d, in backing_request_endio()
661 * speically assembled in cached_dev_write() for a non-zero in backing_request_endio()
663 * s->iop.status by this failure, the status will be decided in backing_request_endio()
666 if (unlikely(s->iop.writeback && in backing_request_endio()
667 bio->bi_opf & REQ_PREFLUSH)) { in backing_request_endio()
669 dc->bdev, bio->bi_status); in backing_request_endio()
671 /* set to orig_bio->bi_status in bio_complete() */ in backing_request_endio()
672 s->iop.status = bio->bi_status; in backing_request_endio()
674 s->recoverable = false; in backing_request_endio()
683 static void bio_complete(struct search *s) in bio_complete() argument
685 if (s->orig_bio) { in bio_complete()
687 bio_end_io_acct_remapped(s->orig_bio, s->start_time, in bio_complete()
688 s->orig_bdev); in bio_complete()
689 trace_bcache_request_end(s->d, s->orig_bio); in bio_complete()
690 s->orig_bio->bi_status = s->iop.status; in bio_complete()
691 bio_endio(s->orig_bio); in bio_complete()
692 s->orig_bio = NULL; in bio_complete()
696 static void do_bio_hook(struct search *s, in do_bio_hook() argument
700 struct bio *bio = &s->bio.bio; in do_bio_hook()
702 bio_init_clone(orig_bio->bi_bdev, bio, orig_bio, GFP_NOIO); in do_bio_hook()
706 * - cache_bio->bi_end_io from cached_dev_cache_miss() in do_bio_hook()
707 * - n->bi_end_io from cache_lookup_fn() in do_bio_hook()
709 bio->bi_end_io = end_io_fn; in do_bio_hook()
710 bio->bi_private = &s->cl; in do_bio_hook()
717 closure_type(s, struct search, cl); in CLOSURE_CALLBACK()
719 atomic_dec(&s->iop.c->search_inflight); in CLOSURE_CALLBACK()
721 if (s->iop.bio) in CLOSURE_CALLBACK()
722 bio_put(s->iop.bio); in CLOSURE_CALLBACK()
724 bio_complete(s); in CLOSURE_CALLBACK()
726 mempool_free(s, &s->iop.c->search); in CLOSURE_CALLBACK()
733 struct search *s; in search_alloc() local
735 s = mempool_alloc(&d->c->search, GFP_NOIO); in search_alloc()
737 closure_init(&s->cl, NULL); in search_alloc()
738 do_bio_hook(s, bio, request_endio); in search_alloc()
739 atomic_inc(&d->c->search_inflight); in search_alloc()
741 s->orig_bio = bio; in search_alloc()
742 s->cache_miss = NULL; in search_alloc()
743 s->cache_missed = 0; in search_alloc()
744 s->d = d; in search_alloc()
745 s->recoverable = 1; in search_alloc()
746 s->write = op_is_write(bio_op(bio)); in search_alloc()
747 s->read_dirty_data = 0; in search_alloc()
749 s->orig_bdev = orig_bdev; in search_alloc()
750 s->start_time = start_time; in search_alloc()
751 s->iop.c = d->c; in search_alloc()
752 s->iop.bio = NULL; in search_alloc()
753 s->iop.inode = d->id; in search_alloc()
754 s->iop.write_point = hash_long((unsigned long) current, 16); in search_alloc()
755 s->iop.write_prio = 0; in search_alloc()
756 s->iop.status = 0; in search_alloc()
757 s->iop.flags = 0; in search_alloc()
758 s->iop.flush_journal = op_is_flush(bio->bi_opf); in search_alloc()
759 s->iop.wq = bcache_wq; in search_alloc()
761 return s; in search_alloc()
768 closure_type(s, struct search, cl); in CLOSURE_CALLBACK()
769 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); in CLOSURE_CALLBACK()
772 search_free(&cl->work); in CLOSURE_CALLBACK()
779 closure_type(s, struct search, cl); in CLOSURE_CALLBACK()
781 if (s->iop.replace_collision) in CLOSURE_CALLBACK()
782 bch_mark_cache_miss_collision(s->iop.c, s->d); in CLOSURE_CALLBACK()
784 if (s->iop.bio) in CLOSURE_CALLBACK()
785 bio_free_pages(s->iop.bio); in CLOSURE_CALLBACK()
787 cached_dev_bio_complete(&cl->work); in CLOSURE_CALLBACK()
792 closure_type(s, struct search, cl); in CLOSURE_CALLBACK()
793 struct bio *bio = &s->bio.bio; in CLOSURE_CALLBACK()
796 * If read request hit dirty data (s->read_dirty_data is true), in CLOSURE_CALLBACK()
802 if (s->recoverable && !s->read_dirty_data) { in CLOSURE_CALLBACK()
804 trace_bcache_read_retry(s->orig_bio); in CLOSURE_CALLBACK()
806 s->iop.status = 0; in CLOSURE_CALLBACK()
807 do_bio_hook(s, s->orig_bio, backing_request_endio); in CLOSURE_CALLBACK()
812 closure_bio_submit(s->iop.c, bio, cl); in CLOSURE_CALLBACK()
820 closure_type(s, struct search, cl); in CLOSURE_CALLBACK()
821 struct bcache_device *d = s->d; in CLOSURE_CALLBACK()
823 if (s->iop.replace_collision) in CLOSURE_CALLBACK()
824 bch_mark_cache_miss_collision(s->iop.c, s->d); in CLOSURE_CALLBACK()
826 if (s->iop.bio) in CLOSURE_CALLBACK()
827 bio_free_pages(s->iop.bio); in CLOSURE_CALLBACK()
829 cached_dev_bio_complete(&cl->work); in CLOSURE_CALLBACK()
830 closure_put(&d->cl); in CLOSURE_CALLBACK()
835 closure_type(s, struct search, cl); in CLOSURE_CALLBACK()
836 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); in CLOSURE_CALLBACK()
842 * First, we copy the data we just read from cache_bio's bounce buffers in CLOSURE_CALLBACK()
846 if (s->iop.bio) { in CLOSURE_CALLBACK()
847 bio_reset(s->iop.bio, s->cache_miss->bi_bdev, REQ_OP_READ); in CLOSURE_CALLBACK()
848 s->iop.bio->bi_iter.bi_sector = in CLOSURE_CALLBACK()
849 s->cache_miss->bi_iter.bi_sector; in CLOSURE_CALLBACK()
850 s->iop.bio->bi_iter.bi_size = s->insert_bio_sectors << 9; in CLOSURE_CALLBACK()
851 bio_clone_blkg_association(s->iop.bio, s->cache_miss); in CLOSURE_CALLBACK()
852 bch_bio_map(s->iop.bio, NULL); in CLOSURE_CALLBACK()
854 bio_copy_data(s->cache_miss, s->iop.bio); in CLOSURE_CALLBACK()
856 bio_put(s->cache_miss); in CLOSURE_CALLBACK()
857 s->cache_miss = NULL; in CLOSURE_CALLBACK()
860 if (verify(dc) && s->recoverable && !s->read_dirty_data) in CLOSURE_CALLBACK()
861 bch_data_verify(dc, s->orig_bio); in CLOSURE_CALLBACK()
863 closure_get(&dc->disk.cl); in CLOSURE_CALLBACK()
864 bio_complete(s); in CLOSURE_CALLBACK()
866 if (s->iop.bio && in CLOSURE_CALLBACK()
867 !test_bit(CACHE_SET_STOPPING, &s->iop.c->flags)) { in CLOSURE_CALLBACK()
868 BUG_ON(!s->iop.replace); in CLOSURE_CALLBACK()
869 closure_call(&s->iop.cl, bch_data_insert, NULL, cl); in CLOSURE_CALLBACK()
877 closure_type(s, struct search, cl); in CLOSURE_CALLBACK()
878 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); in CLOSURE_CALLBACK()
880 bch_mark_cache_accounting(s->iop.c, s->d, in CLOSURE_CALLBACK()
881 !s->cache_missed, s->iop.bypass); in CLOSURE_CALLBACK()
882 trace_bcache_read(s->orig_bio, !s->cache_missed, s->iop.bypass); in CLOSURE_CALLBACK()
884 if (s->iop.status) in CLOSURE_CALLBACK()
886 else if (s->iop.bio || verify(dc)) in CLOSURE_CALLBACK()
892 static int cached_dev_cache_miss(struct btree *b, struct search *s, in cached_dev_cache_miss() argument
896 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); in cached_dev_cache_miss()
900 s->cache_missed = 1; in cached_dev_cache_miss()
902 if (s->cache_miss || s->iop.bypass) { in cached_dev_cache_miss()
903 miss = bio_next_split(bio, sectors, GFP_NOIO, &s->d->bio_split); in cached_dev_cache_miss()
910 (1 << KEY_SIZE_BITS) - 1); in cached_dev_cache_miss()
911 s->insert_bio_sectors = min3(size_limit, sectors, bio_sectors(bio)); in cached_dev_cache_miss()
913 s->iop.replace_key = KEY(s->iop.inode, in cached_dev_cache_miss()
914 bio->bi_iter.bi_sector + s->insert_bio_sectors, in cached_dev_cache_miss()
915 s->insert_bio_sectors); in cached_dev_cache_miss()
917 ret = bch_btree_insert_check_key(b, &s->op, &s->iop.replace_key); in cached_dev_cache_miss()
921 s->iop.replace = true; in cached_dev_cache_miss()
923 miss = bio_next_split(bio, s->insert_bio_sectors, GFP_NOIO, in cached_dev_cache_miss()
924 &s->d->bio_split); in cached_dev_cache_miss()
926 /* btree_search_recurse()'s btree iterator is no good anymore */ in cached_dev_cache_miss()
927 ret = miss == bio ? MAP_DONE : -EINTR; in cached_dev_cache_miss()
929 cache_bio = bio_alloc_bioset(miss->bi_bdev, in cached_dev_cache_miss()
930 DIV_ROUND_UP(s->insert_bio_sectors, PAGE_SECTORS), in cached_dev_cache_miss()
931 0, GFP_NOWAIT, &dc->disk.bio_split); in cached_dev_cache_miss()
935 cache_bio->bi_iter.bi_sector = miss->bi_iter.bi_sector; in cached_dev_cache_miss()
936 cache_bio->bi_iter.bi_size = s->insert_bio_sectors << 9; in cached_dev_cache_miss()
938 cache_bio->bi_end_io = backing_request_endio; in cached_dev_cache_miss()
939 cache_bio->bi_private = &s->cl; in cached_dev_cache_miss()
945 s->cache_miss = miss; in cached_dev_cache_miss()
946 s->iop.bio = cache_bio; in cached_dev_cache_miss()
949 closure_bio_submit(s->iop.c, cache_bio, &s->cl); in cached_dev_cache_miss()
955 miss->bi_end_io = backing_request_endio; in cached_dev_cache_miss()
956 miss->bi_private = &s->cl; in cached_dev_cache_miss()
958 closure_bio_submit(s->iop.c, miss, &s->cl); in cached_dev_cache_miss()
962 static void cached_dev_read(struct cached_dev *dc, struct search *s) in cached_dev_read() argument
964 struct closure *cl = &s->cl; in cached_dev_read()
966 closure_call(&s->iop.cl, cache_lookup, NULL, cl); in cached_dev_read()
974 closure_type(s, struct search, cl); in CLOSURE_CALLBACK()
975 struct cached_dev *dc = container_of(s->d, struct cached_dev, disk); in CLOSURE_CALLBACK()
977 up_read_non_owner(&dc->writeback_lock); in CLOSURE_CALLBACK()
978 cached_dev_bio_complete(&cl->work); in CLOSURE_CALLBACK()
981 static void cached_dev_write(struct cached_dev *dc, struct search *s) in cached_dev_write() argument
983 struct closure *cl = &s->cl; in cached_dev_write()
984 struct bio *bio = &s->bio.bio; in cached_dev_write()
985 struct bkey start = KEY(dc->disk.id, bio->bi_iter.bi_sector, 0); in cached_dev_write()
986 struct bkey end = KEY(dc->disk.id, bio_end_sector(bio), 0); in cached_dev_write()
988 bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, &start, &end); in cached_dev_write()
990 down_read_non_owner(&dc->writeback_lock); in cached_dev_write()
991 if (bch_keybuf_check_overlapping(&dc->writeback_keys, &start, &end)) { in cached_dev_write()
996 s->iop.bypass = false; in cached_dev_write()
997 s->iop.writeback = true; in cached_dev_write()
1008 s->iop.bypass = true; in cached_dev_write()
1010 if (should_writeback(dc, s->orig_bio, in cached_dev_write()
1012 s->iop.bypass)) { in cached_dev_write()
1013 s->iop.bypass = false; in cached_dev_write()
1014 s->iop.writeback = true; in cached_dev_write()
1017 if (s->iop.bypass) { in cached_dev_write()
1018 s->iop.bio = s->orig_bio; in cached_dev_write()
1019 bio_get(s->iop.bio); in cached_dev_write()
1022 !bdev_max_discard_sectors(dc->bdev)) in cached_dev_write()
1026 bio->bi_end_io = backing_request_endio; in cached_dev_write()
1027 closure_bio_submit(s->iop.c, bio, cl); in cached_dev_write()
1029 } else if (s->iop.writeback) { in cached_dev_write()
1031 s->iop.bio = bio; in cached_dev_write()
1033 if (bio->bi_opf & REQ_PREFLUSH) { in cached_dev_write()
1040 flush = bio_alloc_bioset(bio->bi_bdev, 0, in cached_dev_write()
1042 GFP_NOIO, &dc->disk.bio_split); in cached_dev_write()
1044 s->iop.status = BLK_STS_RESOURCE; in cached_dev_write()
1047 flush->bi_end_io = backing_request_endio; in cached_dev_write()
1048 flush->bi_private = cl; in cached_dev_write()
1050 closure_bio_submit(s->iop.c, flush, cl); in cached_dev_write()
1053 s->iop.bio = bio_alloc_clone(bio->bi_bdev, bio, GFP_NOIO, in cached_dev_write()
1054 &dc->disk.bio_split); in cached_dev_write()
1056 bio->bi_end_io = backing_request_endio; in cached_dev_write()
1057 closure_bio_submit(s->iop.c, bio, cl); in cached_dev_write()
1061 closure_call(&s->iop.cl, bch_data_insert, NULL, cl); in cached_dev_write()
1067 closure_type(s, struct search, cl); in CLOSURE_CALLBACK()
1068 struct bio *bio = &s->bio.bio; in CLOSURE_CALLBACK()
1070 if (s->iop.flush_journal) in CLOSURE_CALLBACK()
1071 bch_journal_meta(s->iop.c, cl); in CLOSURE_CALLBACK()
1073 /* If it's a flush, we send the flush to the backing device too */ in CLOSURE_CALLBACK()
1074 bio->bi_end_io = backing_request_endio; in CLOSURE_CALLBACK()
1075 closure_bio_submit(s->iop.c, bio, cl); in CLOSURE_CALLBACK()
1084 struct bio *orig_bio = ddip->orig_bio; in detached_dev_end_io()
1087 bio_end_io_acct(orig_bio, ddip->start_time); in detached_dev_end_io()
1089 if (bio->bi_status) { in detached_dev_end_io()
1090 struct cached_dev *dc = bio->bi_private; in detached_dev_end_io()
1094 orig_bio->bi_status = bio->bi_status; in detached_dev_end_io()
1109 !bdev_max_discard_sectors(dc->bdev)) { in detached_dev_do_request()
1115 clone_bio = bio_alloc_clone(dc->bdev, orig_bio, GFP_NOIO, in detached_dev_do_request()
1116 &d->bio_detached); in detached_dev_do_request()
1120 ddip->d = d; in detached_dev_do_request()
1121 ddip->start_time = start_time; in detached_dev_do_request()
1122 ddip->orig_bio = orig_bio; in detached_dev_do_request()
1124 clone_bio->bi_end_io = detached_dev_end_io; in detached_dev_do_request()
1125 clone_bio->bi_private = dc; in detached_dev_do_request()
1144 * c->idle_counter is 0 already). in quit_max_writeback_rate()
1147 for (i = 0; i < c->devices_max_used; i++) { in quit_max_writeback_rate()
1148 if (!c->devices[i]) in quit_max_writeback_rate()
1151 if (UUID_FLASH_ONLY(&c->uuids[i])) in quit_max_writeback_rate()
1154 d = c->devices[i]; in quit_max_writeback_rate()
1161 atomic_long_set(&dc->writeback_rate.rate, 1); in quit_max_writeback_rate()
1165 atomic_long_set(&this_dc->writeback_rate.rate, 1); in quit_max_writeback_rate()
1168 /* Cached devices - read & write stuff */
1172 struct search *s; in cached_dev_submit_bio() local
1173 struct block_device *orig_bdev = bio->bi_bdev; in cached_dev_submit_bio()
1174 struct bcache_device *d = orig_bdev->bd_disk->private_data; in cached_dev_submit_bio()
1179 if (unlikely((d->c && test_bit(CACHE_SET_IO_DISABLE, &d->c->flags)) || in cached_dev_submit_bio()
1180 dc->io_disable)) { in cached_dev_submit_bio()
1181 bio->bi_status = BLK_STS_IOERR; in cached_dev_submit_bio()
1186 if (likely(d->c)) { in cached_dev_submit_bio()
1187 if (atomic_read(&d->c->idle_counter)) in cached_dev_submit_bio()
1188 atomic_set(&d->c->idle_counter, 0); in cached_dev_submit_bio()
1195 if (unlikely(atomic_read(&d->c->at_max_writeback_rate) == 1)) { in cached_dev_submit_bio()
1196 atomic_set(&d->c->at_max_writeback_rate, 0); in cached_dev_submit_bio()
1197 quit_max_writeback_rate(d->c, dc); in cached_dev_submit_bio()
1203 bio->bi_iter.bi_sector += dc->sb.data_offset; in cached_dev_submit_bio()
1206 bio_set_dev(bio, dc->bdev); in cached_dev_submit_bio()
1207 s = search_alloc(bio, d, orig_bdev, start_time); in cached_dev_submit_bio()
1208 trace_bcache_request_start(s->d, bio); in cached_dev_submit_bio()
1210 if (!bio->bi_iter.bi_size) { in cached_dev_submit_bio()
1215 continue_at_nobarrier(&s->cl, in cached_dev_submit_bio()
1219 s->iop.bypass = check_should_bypass(dc, bio); in cached_dev_submit_bio()
1222 cached_dev_write(dc, s); in cached_dev_submit_bio()
1224 cached_dev_read(dc, s); in cached_dev_submit_bio()
1237 if (dc->io_disable) in cached_dev_ioctl()
1238 return -EIO; in cached_dev_ioctl()
1239 if (!dc->bdev->bd_disk->fops->ioctl) in cached_dev_ioctl()
1240 return -ENOTTY; in cached_dev_ioctl()
1241 return dc->bdev->bd_disk->fops->ioctl(dc->bdev, mode, cmd, arg); in cached_dev_ioctl()
1246 dc->disk.cache_miss = cached_dev_cache_miss; in bch_cached_dev_request_init()
1247 dc->disk.ioctl = cached_dev_ioctl; in bch_cached_dev_request_init()
1252 static int flash_dev_cache_miss(struct btree *b, struct search *s, in flash_dev_cache_miss() argument
1257 swap(bio->bi_iter.bi_size, bytes); in flash_dev_cache_miss()
1259 swap(bio->bi_iter.bi_size, bytes); in flash_dev_cache_miss()
1263 if (!bio->bi_iter.bi_size) in flash_dev_cache_miss()
1271 closure_type(s, struct search, cl); in CLOSURE_CALLBACK()
1273 if (s->iop.flush_journal) in CLOSURE_CALLBACK()
1274 bch_journal_meta(s->iop.c, cl); in CLOSURE_CALLBACK()
1281 struct search *s; in flash_dev_submit_bio() local
1283 struct bcache_device *d = bio->bi_bdev->bd_disk->private_data; in flash_dev_submit_bio()
1285 if (unlikely(d->c && test_bit(CACHE_SET_IO_DISABLE, &d->c->flags))) { in flash_dev_submit_bio()
1286 bio->bi_status = BLK_STS_IOERR; in flash_dev_submit_bio()
1291 s = search_alloc(bio, d, bio->bi_bdev, bio_start_io_acct(bio)); in flash_dev_submit_bio()
1292 cl = &s->cl; in flash_dev_submit_bio()
1293 bio = &s->bio.bio; in flash_dev_submit_bio()
1295 trace_bcache_request_start(s->d, bio); in flash_dev_submit_bio()
1297 if (!bio->bi_iter.bi_size) { in flash_dev_submit_bio()
1301 continue_at_nobarrier(&s->cl, in flash_dev_submit_bio()
1306 bch_keybuf_check_overlapping(&s->iop.c->moving_gc_keys, in flash_dev_submit_bio()
1307 &KEY(d->id, bio->bi_iter.bi_sector, 0), in flash_dev_submit_bio()
1308 &KEY(d->id, bio_end_sector(bio), 0)); in flash_dev_submit_bio()
1310 s->iop.bypass = (bio_op(bio) == REQ_OP_DISCARD) != 0; in flash_dev_submit_bio()
1311 s->iop.writeback = true; in flash_dev_submit_bio()
1312 s->iop.bio = bio; in flash_dev_submit_bio()
1314 closure_call(&s->iop.cl, bch_data_insert, NULL, cl); in flash_dev_submit_bio()
1316 closure_call(&s->iop.cl, cache_lookup, NULL, cl); in flash_dev_submit_bio()
1325 return -ENOTTY; in flash_dev_ioctl()
1330 d->cache_miss = flash_dev_cache_miss; in bch_flash_dev_request_init()
1331 d->ioctl = flash_dev_ioctl; in bch_flash_dev_request_init()
1343 return -ENOMEM; in bch_request_init()