2 * Some low level IO code, and hacks for various block layer limitations
4 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
5 * Copyright 2012 Google, Inc.
11 #include "btree_update.h"
27 #include <linux/blkdev.h>
28 #include <linux/random.h>
30 #include <trace/events/bcache.h>
32 static inline void __bio_inc_remaining(struct bio *bio)
34 bio_set_flag(bio, BIO_CHAIN);
35 smp_mb__before_atomic();
36 atomic_inc(&bio->__bi_remaining);
39 void bch_generic_make_request(struct bio *bio, struct cache_set *c)
41 if (current->bio_list) {
42 spin_lock(&c->bio_submit_lock);
43 bio_list_add(&c->bio_submit_list, bio);
44 spin_unlock(&c->bio_submit_lock);
45 queue_work(bcache_io_wq, &c->bio_submit_work);
47 generic_make_request(bio);
51 void bch_bio_submit_work(struct work_struct *work)
53 struct cache_set *c = container_of(work, struct cache_set,
58 spin_lock(&c->bio_submit_lock);
59 bl = c->bio_submit_list;
60 bio_list_init(&c->bio_submit_list);
61 spin_unlock(&c->bio_submit_lock);
63 while ((bio = bio_list_pop(&bl)))
64 generic_make_request(bio);
67 /* Allocate, free from mempool: */
69 void bch_bio_free_pages_pool(struct cache_set *c, struct bio *bio)
74 bio_for_each_segment_all(bv, bio, i)
75 if (bv->bv_page != ZERO_PAGE(0))
76 mempool_free(bv->bv_page, &c->bio_bounce_pages);
80 static void bch_bio_alloc_page_pool(struct cache_set *c, struct bio *bio,
83 struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt++];
85 if (likely(!*using_mempool)) {
86 bv->bv_page = alloc_page(GFP_NOIO);
87 if (unlikely(!bv->bv_page)) {
88 mutex_lock(&c->bio_bounce_pages_lock);
89 *using_mempool = true;
95 bv->bv_page = mempool_alloc(&c->bio_bounce_pages, GFP_NOIO);
98 bv->bv_len = PAGE_SIZE;
102 void bch_bio_alloc_pages_pool(struct cache_set *c, struct bio *bio,
105 bool using_mempool = false;
107 bio->bi_iter.bi_size = bytes;
109 while (bio->bi_vcnt < DIV_ROUND_UP(bytes, PAGE_SIZE))
110 bch_bio_alloc_page_pool(c, bio, &using_mempool);
113 mutex_unlock(&c->bio_bounce_pages_lock);
116 /* Bios with headers */
118 static void bch_submit_wbio(struct cache_set *c, struct bch_write_bio *wbio,
119 struct cache *ca, const struct bch_extent_ptr *ptr,
123 wbio->submit_time_us = local_clock_us();
124 wbio->bio.bi_iter.bi_sector = ptr->offset;
125 wbio->bio.bi_bdev = ca ? ca->disk_sb.bdev : NULL;
128 bcache_io_error(c, &wbio->bio, "device has been removed");
130 bch_generic_make_request(&wbio->bio, c);
132 generic_make_request(&wbio->bio);
135 void bch_submit_wbio_replicas(struct bch_write_bio *wbio, struct cache_set *c,
136 const struct bkey_i *k, bool punt)
138 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(k);
139 const struct bch_extent_ptr *ptr;
140 struct bch_write_bio *n;
146 extent_for_each_ptr(e, ptr) {
148 ca = PTR_CACHE(c, ptr);
150 percpu_ref_get(&ca->ref);
154 bch_submit_wbio(c, wbio, ca, ptr, punt);
158 if (ptr + 1 < &extent_entry_last(e)->ptr) {
159 n = to_wbio(bio_clone_fast(&wbio->bio, GFP_NOIO,
162 n->bio.bi_end_io = wbio->bio.bi_end_io;
163 n->bio.bi_private = wbio->bio.bi_private;
165 n->orig = &wbio->bio;
169 n->bio.bi_opf = wbio->bio.bi_opf;
170 __bio_inc_remaining(n->orig);
175 if (!journal_flushes_device(ca))
176 n->bio.bi_opf |= REQ_FUA;
178 bch_submit_wbio(c, n, ca, ptr, punt);
186 static struct workqueue_struct *index_update_wq(struct bch_write_op *op)
188 return op->alloc_reserve == RESERVE_MOVINGGC
193 static void __bch_write(struct closure *);
195 static void bch_write_done(struct closure *cl)
197 struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
199 BUG_ON(!(op->flags & BCH_WRITE_DONE));
201 if (!op->error && (op->flags & BCH_WRITE_FLUSH))
202 op->error = bch_journal_error(&op->c->journal);
204 bch_disk_reservation_put(op->c, &op->res);
205 percpu_ref_put(&op->c->writes);
206 bch_keylist_free(&op->insert_keys, op->inline_keys);
210 static u64 keylist_sectors(struct keylist *keys)
215 for_each_keylist_key(keys, k)
221 static int bch_write_index_default(struct bch_write_op *op)
223 struct keylist *keys = &op->insert_keys;
224 struct btree_iter iter;
227 bch_btree_iter_init_intent(&iter, op->c, BTREE_ID_EXTENTS,
228 bkey_start_pos(&bch_keylist_front(keys)->k));
230 ret = bch_btree_insert_list_at(&iter, keys, &op->res,
231 NULL, op_journal_seq(op),
232 BTREE_INSERT_NOFAIL);
233 bch_btree_iter_unlock(&iter);
239 * bch_write_index - after a write, update index to point to new data
241 static void bch_write_index(struct closure *cl)
243 struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
244 struct cache_set *c = op->c;
245 struct keylist *keys = &op->insert_keys;
248 op->flags |= BCH_WRITE_LOOPED;
250 if (!bch_keylist_empty(keys)) {
251 u64 sectors_start = keylist_sectors(keys);
252 int ret = op->index_update_fn(op);
254 BUG_ON(keylist_sectors(keys) && !ret);
256 op->written += sectors_start - keylist_sectors(keys);
259 __bcache_io_error(c, "btree IO error %i", ret);
264 for (i = 0; i < ARRAY_SIZE(op->open_buckets); i++)
265 if (op->open_buckets[i]) {
266 bch_open_bucket_put(c,
268 op->open_buckets[i]);
269 op->open_buckets[i] = 0;
272 if (!(op->flags & BCH_WRITE_DONE))
273 continue_at(cl, __bch_write, op->io_wq);
275 if (!op->error && (op->flags & BCH_WRITE_FLUSH)) {
276 bch_journal_flush_seq_async(&c->journal,
279 continue_at(cl, bch_write_done, index_update_wq(op));
281 continue_at_nobarrier(cl, bch_write_done, NULL);
286 * bch_write_discard - discard range of keys
288 * Used to implement discard, and to handle when writethrough write hits
289 * a write error on the cache device.
291 static void bch_write_discard(struct closure *cl)
293 struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
294 struct bio *bio = &op->bio->bio;
295 struct bpos end = op->pos;
297 end.offset += bio_sectors(bio);
299 op->error = bch_discard(op->c, op->pos, end, op->version,
300 &op->res, NULL, NULL);
304 * Convert extents to be inserted to discards after an error:
306 static void bch_write_io_error(struct closure *cl)
308 struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
310 if (op->flags & BCH_WRITE_DISCARD_ON_ERROR) {
311 struct bkey_i *src = bch_keylist_front(&op->insert_keys);
312 struct bkey_i *dst = bch_keylist_front(&op->insert_keys);
315 * Our data write just errored, which means we've got a bunch
316 * of keys to insert that point to data that wasn't
317 * successfully written.
319 * We don't have to insert those keys but we still have to
320 * invalidate that region of the cache - so, if we just strip
321 * off all the pointers from the keys we'll accomplish just
325 while (src != op->insert_keys.top) {
326 struct bkey_i *n = bkey_next(src);
328 set_bkey_val_u64s(&src->k, 0);
329 src->k.type = KEY_TYPE_DISCARD;
332 dst = bkey_next(dst);
336 op->insert_keys.top = dst;
337 op->flags |= BCH_WRITE_DISCARD;
339 /* TODO: We could try to recover from this. */
340 while (!bch_keylist_empty(&op->insert_keys))
341 bch_keylist_pop_front(&op->insert_keys);
344 op->flags |= BCH_WRITE_DONE;
350 static void bch_write_endio(struct bio *bio)
352 struct closure *cl = bio->bi_private;
353 struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
354 struct bch_write_bio *wbio = to_wbio(bio);
355 struct cache_set *c = wbio->c;
356 struct bio *orig = wbio->orig;
357 struct cache *ca = wbio->ca;
359 if (cache_nonfatal_io_err_on(bio->bi_error, ca,
361 set_closure_fn(cl, bch_write_io_error, index_update_wq(op));
363 bch_account_io_completion_time(ca, wbio->submit_time_us,
366 percpu_ref_put(&ca->ref);
368 if (bio->bi_error && orig)
369 orig->bi_error = bio->bi_error;
372 bch_bio_free_pages_pool(c, bio);
383 static void init_append_extent(struct bch_write_op *op,
384 unsigned compressed_size,
385 unsigned uncompressed_size,
386 unsigned compression_type,
387 u64 csum, unsigned csum_type,
388 struct open_bucket *ob)
390 struct bkey_i_extent *e = bkey_extent_init(op->insert_keys.top);
392 op->pos.offset += uncompressed_size;
394 e->k.size = uncompressed_size;
396 bch_extent_crc_append(e, compressed_size,
401 bch_alloc_sectors_append_ptrs(op->c, e, op->nr_replicas,
402 ob, compressed_size);
404 bkey_extent_set_cached(&e->k, (op->flags & BCH_WRITE_CACHED));
405 bch_keylist_push(&op->insert_keys);
408 static int bch_write_extent(struct bch_write_op *op,
409 struct open_bucket *ob,
412 struct cache_set *c = op->c;
414 struct bch_write_bio *wbio;
415 unsigned key_to_write_offset = op->insert_keys.top_p -
416 op->insert_keys.keys_p;
417 struct bkey_i *key_to_write;
418 unsigned csum_type = c->opts.data_checksum;
419 unsigned compression_type = op->compression_type;
422 /* don't refetch csum type/compression type */
425 /* Need to decompress data? */
426 if ((op->flags & BCH_WRITE_DATA_COMPRESSED) &&
427 (op->crc.uncompressed_size != op->size ||
428 op->crc.compressed_size > ob->sectors_free)) {
431 ret = bch_bio_uncompress_inplace(c, orig, op->size, op->crc);
435 op->flags &= ~BCH_WRITE_DATA_COMPRESSED;
438 if (op->flags & BCH_WRITE_DATA_COMPRESSED) {
439 init_append_extent(op,
440 op->crc.compressed_size,
441 op->crc.uncompressed_size,
442 op->crc.compression_type,
450 wbio->bounce = false;
451 wbio->put_bio = false;
453 } else if (csum_type != BCH_CSUM_NONE ||
454 compression_type != BCH_COMPRESSION_NONE) {
455 /* all units here in bytes */
456 unsigned total_output = 0, output_available =
457 min(ob->sectors_free << 9, orig->bi_iter.bi_size);
460 bio = bio_alloc_bioset(GFP_NOIO,
461 DIV_ROUND_UP(output_available, PAGE_SIZE),
464 * XXX: can't use mempool for more than
465 * BCH_COMPRESSED_EXTENT_MAX worth of pages
467 bch_bio_alloc_pages_pool(c, bio, output_available);
469 /* copy WRITE_SYNC flag */
470 bio->bi_opf = orig->bi_opf;
474 wbio->put_bio = true;
477 unsigned fragment_compression_type = compression_type;
478 size_t dst_len, src_len;
480 bch_bio_compress(c, bio, &dst_len,
482 &fragment_compression_type);
484 BUG_ON(!dst_len || dst_len > bio->bi_iter.bi_size);
485 BUG_ON(!src_len || src_len > orig->bi_iter.bi_size);
486 BUG_ON(dst_len & (block_bytes(c) - 1));
487 BUG_ON(src_len & (block_bytes(c) - 1));
489 swap(bio->bi_iter.bi_size, dst_len);
490 csum = bch_checksum_bio(bio, csum_type);
491 swap(bio->bi_iter.bi_size, dst_len);
493 init_append_extent(op,
494 dst_len >> 9, src_len >> 9,
495 fragment_compression_type,
496 csum, csum_type, ob);
498 total_output += dst_len;
499 bio_advance(bio, dst_len);
500 bio_advance(orig, src_len);
501 } while (bio->bi_iter.bi_size &&
502 orig->bi_iter.bi_size &&
503 !bch_keylist_realloc(&op->insert_keys,
505 ARRAY_SIZE(op->inline_keys),
506 BKEY_EXTENT_U64s_MAX));
508 BUG_ON(total_output > output_available);
510 memset(&bio->bi_iter, 0, sizeof(bio->bi_iter));
511 bio->bi_iter.bi_size = total_output;
514 * Free unneeded pages after compressing:
516 while (bio->bi_vcnt * PAGE_SIZE >
517 round_up(bio->bi_iter.bi_size, PAGE_SIZE))
518 mempool_free(bio->bi_io_vec[--bio->bi_vcnt].bv_page,
519 &c->bio_bounce_pages);
521 ret = orig->bi_iter.bi_size != 0;
523 bio = bio_next_split(orig, ob->sectors_free, GFP_NOIO,
528 wbio->bounce = false;
529 wbio->put_bio = bio != orig;
531 init_append_extent(op, bio_sectors(bio), bio_sectors(bio),
532 compression_type, 0, csum_type, ob);
537 bio->bi_end_io = bch_write_endio;
538 bio->bi_private = &op->cl;
539 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
541 closure_get(bio->bi_private);
543 /* might have done a realloc... */
545 key_to_write = (void *) (op->insert_keys.keys_p + key_to_write_offset);
547 if (!(op->flags & BCH_WRITE_CACHED))
548 bch_check_mark_super(c, key_to_write, false);
550 #ifndef CONFIG_BCACHE_NO_IO
551 bch_submit_wbio_replicas(to_wbio(bio), c, key_to_write, false);
553 to_wbio(bio)->ca = NULL;
559 static void __bch_write(struct closure *cl)
561 struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
562 struct cache_set *c = op->c;
563 struct bio *bio = &op->bio->bio;
564 unsigned open_bucket_nr = 0;
565 struct open_bucket *b;
568 memset(op->open_buckets, 0, sizeof(op->open_buckets));
570 if (op->flags & BCH_WRITE_DISCARD) {
571 op->flags |= BCH_WRITE_DONE;
572 bch_write_discard(cl);
574 continue_at(cl, bch_write_done, index_update_wq(op));
578 * Journal writes are marked REQ_PREFLUSH; if the original write was a
579 * flush, it'll wait on the journal write.
581 bio->bi_opf &= ~(REQ_PREFLUSH|REQ_FUA);
584 EBUG_ON(bio->bi_iter.bi_sector != op->pos.offset);
585 EBUG_ON(!bio_sectors(bio));
587 if (open_bucket_nr == ARRAY_SIZE(op->open_buckets))
588 continue_at(cl, bch_write_index, index_update_wq(op));
590 /* for the device pointers and 1 for the chksum */
591 if (bch_keylist_realloc(&op->insert_keys,
593 ARRAY_SIZE(op->inline_keys),
594 BKEY_EXTENT_U64s_MAX))
595 continue_at(cl, bch_write_index, index_update_wq(op));
597 b = bch_alloc_sectors_start(c, op->wp, op->nr_replicas,
599 (op->flags & BCH_WRITE_ALLOC_NOWAIT) ? NULL : cl);
602 if (unlikely(IS_ERR(b))) {
603 if (unlikely(PTR_ERR(b) != -EAGAIN)) {
609 * If we already have some keys, must insert them first
610 * before allocating another open bucket. We only hit
611 * this case if open_bucket_nr > 1.
613 if (!bch_keylist_empty(&op->insert_keys))
614 continue_at(cl, bch_write_index,
615 index_update_wq(op));
618 * If we've looped, we're running out of a workqueue -
619 * not the bch_write() caller's context - and we don't
620 * want to block the workqueue:
622 if (op->flags & BCH_WRITE_LOOPED)
623 continue_at(cl, __bch_write, op->io_wq);
626 * Otherwise, we do want to block the caller on alloc
627 * failure instead of letting it queue up more and more
629 * XXX: this technically needs a try_to_freeze() -
630 * except that that's not safe because caller may have
631 * issued other IO... hmm..
637 BUG_ON(b - c->open_buckets == 0 ||
638 b - c->open_buckets > U8_MAX);
639 op->open_buckets[open_bucket_nr++] = b - c->open_buckets;
641 ret = bch_write_extent(op, b, bio);
643 bch_alloc_sectors_done(c, op->wp, b);
649 op->flags |= BCH_WRITE_DONE;
650 continue_at(cl, bch_write_index, index_update_wq(op));
652 if (op->flags & BCH_WRITE_DISCARD_ON_ERROR) {
654 * If we were writing cached data, not doing the write is fine
655 * so long as we discard whatever would have been overwritten -
656 * then it's equivalent to doing the write and immediately
660 bch_write_discard(cl);
663 * Right now we can only error here if we went RO - the
664 * allocation failed, but we already checked for -ENOSPC when we
665 * got our reservation.
667 * XXX capacity might have changed, but we don't check for that
673 op->flags |= BCH_WRITE_DONE;
676 * No reason not to insert keys for whatever data was successfully
677 * written (especially for a cmpxchg operation that's moving data
680 continue_at(cl, !bch_keylist_empty(&op->insert_keys)
682 : bch_write_done, index_update_wq(op));
685 void bch_wake_delayed_writes(unsigned long data)
687 struct cache_set *c = (void *) data;
688 struct bch_write_op *op;
691 spin_lock_irqsave(&c->foreground_write_pd_lock, flags);
693 while ((op = c->write_wait_head)) {
694 if (!test_bit(CACHE_SET_RO, &c->flags) &&
695 !test_bit(CACHE_SET_STOPPING, &c->flags) &&
696 time_after(op->expires, jiffies)) {
697 mod_timer(&c->foreground_write_wakeup, op->expires);
701 c->write_wait_head = op->next;
702 if (!c->write_wait_head)
703 c->write_wait_tail = NULL;
705 closure_put(&op->cl);
708 spin_unlock_irqrestore(&c->foreground_write_pd_lock, flags);
712 * bch_write - handle a write to a cache device or flash only volume
714 * This is the starting point for any data to end up in a cache device; it could
715 * be from a normal write, or a writeback write, or a write to a flash only
716 * volume - it's also used by the moving garbage collector to compact data in
717 * mostly empty buckets.
719 * It first writes the data to the cache, creating a list of keys to be inserted
720 * (if the data won't fit in a single open bucket, there will be multiple keys);
721 * after the data is written it calls bch_journal, and after the keys have been
722 * added to the next journal write they're inserted into the btree.
724 * It inserts the data in op->bio; bi_sector is used for the key offset, and
725 * op->inode is used for the key inode.
727 * If op->discard is true, instead of inserting the data it invalidates the
728 * region of the cache represented by op->bio and op->inode.
730 void bch_write(struct closure *cl)
732 struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
733 struct bio *bio = &op->bio->bio;
734 struct cache_set *c = op->c;
735 u64 inode = op->pos.inode;
737 trace_bcache_write(c, inode, bio,
738 !(op->flags & BCH_WRITE_CACHED),
739 op->flags & BCH_WRITE_DISCARD);
741 if (!percpu_ref_tryget(&c->writes)) {
742 __bcache_io_error(c, "read only");
744 bch_disk_reservation_put(c, &op->res);
748 if (!(op->flags & BCH_WRITE_DISCARD))
749 bch_increment_clock(c, bio_sectors(bio), WRITE);
751 if (!(op->flags & BCH_WRITE_DISCARD))
752 bch_mark_foreground_write(c, bio_sectors(bio));
754 bch_mark_discard(c, bio_sectors(bio));
756 /* Don't call bch_next_delay() if rate is >= 1 GB/sec */
758 if (c->foreground_write_ratelimit_enabled &&
759 c->foreground_write_pd.rate.rate < (1 << 30) &&
760 !(op->flags & BCH_WRITE_DISCARD) && op->wp->throttle) {
764 spin_lock_irqsave(&c->foreground_write_pd_lock, flags);
765 bch_ratelimit_increment(&c->foreground_write_pd.rate,
766 bio->bi_iter.bi_size);
768 delay = bch_ratelimit_delay(&c->foreground_write_pd.rate);
770 if (delay >= HZ / 100) {
771 trace_bcache_write_throttle(c, inode, bio, delay);
773 closure_get(&op->cl); /* list takes a ref */
775 op->expires = jiffies + delay;
778 if (c->write_wait_tail)
779 c->write_wait_tail->next = op;
781 c->write_wait_head = op;
782 c->write_wait_tail = op;
784 if (!timer_pending(&c->foreground_write_wakeup))
785 mod_timer(&c->foreground_write_wakeup,
788 spin_unlock_irqrestore(&c->foreground_write_pd_lock,
790 continue_at(cl, __bch_write, index_update_wq(op));
793 spin_unlock_irqrestore(&c->foreground_write_pd_lock, flags);
796 continue_at_nobarrier(cl, __bch_write, NULL);
799 void bch_write_op_init(struct bch_write_op *op, struct cache_set *c,
800 struct bch_write_bio *bio, struct disk_reservation res,
801 struct write_point *wp, struct bpos pos,
802 u64 *journal_seq, unsigned flags)
805 op->io_wq = index_update_wq(op);
810 op->compression_type = c->opts.compression;
811 op->nr_replicas = res.nr_replicas;
812 op->alloc_reserve = RESERVE_NONE;
819 op->journal_seq_p = journal_seq;
820 op->flags |= BCH_WRITE_JOURNAL_SEQ_PTR;
825 op->index_update_fn = bch_write_index_default;
827 bch_keylist_init(&op->insert_keys,
829 ARRAY_SIZE(op->inline_keys));
831 if (version_stress_test(c))
832 get_random_bytes(&op->version, sizeof(op->version));
837 /* bch_discard - discard a range of keys from start_key to end_key.
839 * @start_key pointer to start location
840 * NOTE: discard starts at bkey_start_offset(start_key)
841 * @end_key pointer to end location
842 * NOTE: discard ends at KEY_OFFSET(end_key)
843 * @version version of discard (0ULL if none)
849 * XXX: this needs to be refactored with inode_truncate, or more
850 * appropriately inode_truncate should call this
852 int bch_discard(struct cache_set *c, struct bpos start,
853 struct bpos end, u64 version,
854 struct disk_reservation *disk_res,
855 struct extent_insert_hook *hook,
858 return bch_btree_delete_range(c, BTREE_ID_EXTENTS, start, end, version,
859 disk_res, hook, journal_seq);
862 /* Cache promotion on read */
864 struct cache_promote_op {
866 struct migrate_write write;
867 struct bio_vec bi_inline_vecs[0]; /* must be last */
872 static int bio_checksum_uncompress(struct cache_set *c,
873 struct bch_read_bio *rbio)
875 struct bio *src = &rbio->bio;
876 struct bio *dst = &bch_rbio_parent(rbio)->bio;
877 struct bvec_iter dst_iter = rbio->parent_iter;
882 * reset iterator for checksumming and copying bounced data: here we've
883 * set rbio->compressed_size to the amount of data we actually read,
884 * which was not necessarily the full extent if we were only bouncing
885 * in order to promote
888 src->bi_iter.bi_size = rbio->crc.compressed_size << 9;
889 src->bi_iter.bi_idx = 0;
890 src->bi_iter.bi_bvec_done = 0;
892 src->bi_iter = rbio->parent_iter;
895 csum = bch_checksum_bio(src, rbio->crc.csum_type);
896 if (cache_nonfatal_io_err_on(rbio->crc.csum != csum, rbio->ca,
897 "data checksum error, inode %llu offset %llu: expected %0llx got %0llx (type %u)",
898 rbio->inode, (u64) rbio->parent_iter.bi_sector << 9,
899 rbio->crc.csum, csum, rbio->crc.csum_type))
903 * If there was a checksum error, still copy the data back - unless it
904 * was compressed, we don't want to decompress bad data:
906 if (rbio->crc.compression_type != BCH_COMPRESSION_NONE) {
908 ret = bch_bio_uncompress(c, src, dst,
909 dst_iter, rbio->crc);
911 __bcache_io_error(c, "decompression error");
913 } else if (rbio->bounce) {
914 bio_advance(src, rbio->crc.offset << 9);
915 bio_copy_data_iter(dst, dst_iter,
922 static void bch_rbio_free(struct cache_set *c, struct bch_read_bio *rbio)
924 struct bio *bio = &rbio->bio;
927 BUG_ON(!rbio->split);
930 kfree(rbio->promote);
932 bch_bio_free_pages_pool(c, bio);
937 static void bch_rbio_done(struct cache_set *c, struct bch_read_bio *rbio)
939 struct bio *orig = &bch_rbio_parent(rbio)->bio;
941 percpu_ref_put(&rbio->ca->ref);
945 if (rbio->bio.bi_error)
946 orig->bi_error = rbio->bio.bi_error;
949 bch_rbio_free(c, rbio);
952 kfree(rbio->promote);
954 orig->bi_end_io = rbio->orig_bi_end_io;
955 bio_endio_nodec(orig);
960 * Decide if we want to retry the read - returns true if read is being retried,
961 * false if caller should pass error on up
963 static void bch_read_error_maybe_retry(struct cache_set *c,
964 struct bch_read_bio *rbio,
969 if ((error == -EINTR) &&
970 (rbio->flags & BCH_READ_RETRY_IF_STALE)) {
971 atomic_long_inc(&c->cache_read_races);
976 /* io error - do we have another replica? */
979 bch_rbio_parent(rbio)->bio.bi_error = error;
980 bch_rbio_done(c, rbio);
983 percpu_ref_put(&rbio->ca->ref);
986 spin_lock_irqsave(&c->read_retry_lock, flags);
987 bio_list_add(&c->read_retry_list, &rbio->bio);
988 spin_unlock_irqrestore(&c->read_retry_lock, flags);
989 queue_work(c->wq, &c->read_retry_work);
992 static void cache_promote_done(struct closure *cl)
994 struct cache_promote_op *op =
995 container_of(cl, struct cache_promote_op, cl);
997 bch_bio_free_pages_pool(op->write.op.c, &op->write.wbio.bio);
1001 /* Inner part that may run in process context */
1002 static void __bch_read_endio(struct cache_set *c, struct bch_read_bio *rbio)
1006 ret = bio_checksum_uncompress(c, rbio);
1008 bch_read_error_maybe_retry(c, rbio, ret);
1012 if (rbio->promote &&
1013 !test_bit(CACHE_SET_RO, &c->flags) &&
1014 !test_bit(CACHE_SET_STOPPING, &c->flags)) {
1015 struct cache_promote_op *promote = rbio->promote;
1016 struct closure *cl = &promote->cl;
1018 BUG_ON(!rbio->split || !rbio->bounce);
1020 /* we now own pages: */
1021 swap(promote->write.wbio.bio.bi_vcnt, rbio->bio.bi_vcnt);
1022 rbio->promote = NULL;
1024 bch_rbio_done(c, rbio);
1026 closure_init(cl, &c->cl);
1027 closure_call(&promote->write.op.cl, bch_write, c->wq, cl);
1028 closure_return_with_destructor(cl, cache_promote_done);
1030 bch_rbio_done(c, rbio);
1034 void bch_bio_decompress_work(struct work_struct *work)
1036 struct bio_decompress_worker *d =
1037 container_of(work, struct bio_decompress_worker, work);
1038 struct llist_node *list, *next;
1039 struct bch_read_bio *rbio;
1041 while ((list = llist_del_all(&d->bio_list)))
1042 for (list = llist_reverse_order(list);
1045 next = llist_next(list);
1046 rbio = container_of(list, struct bch_read_bio, list);
1048 __bch_read_endio(d->c, rbio);
1052 static void bch_read_endio(struct bio *bio)
1054 struct bch_read_bio *rbio =
1055 container_of(bio, struct bch_read_bio, bio);
1056 struct cache_set *c = rbio->ca->set;
1057 int stale = ((rbio->flags & BCH_READ_RETRY_IF_STALE) && race_fault()) ||
1058 ptr_stale(rbio->ca, &rbio->ptr) ? -EINTR : 0;
1059 int error = bio->bi_error ?: stale;
1061 bch_account_io_completion_time(rbio->ca, rbio->submit_time_us, REQ_OP_READ);
1063 cache_nonfatal_io_err_on(bio->bi_error, rbio->ca, "data read");
1066 bch_read_error_maybe_retry(c, rbio, error);
1070 if (rbio->crc.compression_type != BCH_COMPRESSION_NONE) {
1071 struct bio_decompress_worker *d;
1074 d = this_cpu_ptr(c->bio_decompress_worker);
1075 llist_add(&rbio->list, &d->bio_list);
1076 queue_work(system_unbound_wq, &d->work);
1079 __bch_read_endio(c, rbio);
1083 void bch_read_extent_iter(struct cache_set *c, struct bch_read_bio *orig,
1084 struct bvec_iter iter, struct bkey_s_c k,
1085 struct extent_pick_ptr *pick, unsigned flags)
1087 struct bch_read_bio *rbio;
1088 struct cache_promote_op *promote_op = NULL;
1089 unsigned skip = iter.bi_sector - bkey_start_offset(k.k);
1090 bool bounce = false, split, read_full = false;
1092 EBUG_ON(bkey_start_offset(k.k) > iter.bi_sector ||
1093 k.k->p.offset < bvec_iter_end_sector(iter));
1095 /* only promote if we're not reading from the fastest tier: */
1098 * XXX: multiple promotes can race with each other, wastefully. Keep a
1099 * list of outstanding promotes?
1101 if ((flags & BCH_READ_PROMOTE) && pick->ca->mi.tier) {
1103 * biovec needs to be big enough to hold decompressed data, if
1104 * the bch_write_extent() has to decompress/recompress it:
1107 max_t(unsigned, k.k->size,
1108 pick->crc.uncompressed_size);
1109 unsigned pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
1111 promote_op = kmalloc(sizeof(*promote_op) +
1112 sizeof(struct bio_vec) * pages, GFP_NOIO);
1114 struct bio *promote_bio = &promote_op->write.wbio.bio;
1116 bio_init(promote_bio);
1117 promote_bio->bi_max_vecs = pages;
1118 promote_bio->bi_io_vec = promote_bio->bi_inline_vecs;
1120 /* could also set read_full */
1125 * note: if compression_type and crc_type both == none, then
1126 * compressed/uncompressed size is zero
1128 if (pick->crc.compression_type != BCH_COMPRESSION_NONE ||
1129 (pick->crc.csum_type != BCH_CSUM_NONE &&
1130 (bvec_iter_sectors(iter) != pick->crc.uncompressed_size ||
1131 (flags & BCH_READ_FORCE_BOUNCE)))) {
1137 unsigned sectors = read_full
1138 ? (pick->crc.compressed_size ?: k.k->size)
1139 : bvec_iter_sectors(iter);
1141 rbio = container_of(bio_alloc_bioset(GFP_NOIO,
1142 DIV_ROUND_UP(sectors, PAGE_SECTORS),
1143 &c->bio_read_split),
1144 struct bch_read_bio, bio);
1146 bch_bio_alloc_pages_pool(c, &rbio->bio, sectors << 9);
1148 } else if (!(flags & BCH_READ_MAY_REUSE_BIO) ||
1149 !(flags & BCH_READ_IS_LAST)) {
1151 * Have to clone if there were any splits, due to error
1152 * reporting issues (if a split errored, and retrying didn't
1153 * work, when it reports the error to its parent (us) we don't
1154 * know if the error was from our bio, and we should retry, or
1155 * from the whole bio, in which case we don't want to retry and
1158 rbio = container_of(bio_clone_fast(&orig->bio,
1159 GFP_NOIO, &c->bio_read_split),
1160 struct bch_read_bio, bio);
1161 rbio->bio.bi_iter = iter;
1165 rbio->bio.bi_iter = iter;
1167 BUG_ON(bio_flagged(&rbio->bio, BIO_CHAIN));
1170 if (!(flags & BCH_READ_IS_LAST))
1171 __bio_inc_remaining(&orig->bio);
1174 rbio->parent = orig;
1176 rbio->orig_bi_end_io = orig->bio.bi_end_io;
1177 rbio->parent_iter = iter;
1179 rbio->inode = k.k->p.inode;
1180 rbio->flags = flags;
1181 rbio->bounce = bounce;
1182 rbio->split = split;
1183 rbio->crc = pick->crc;
1185 * crc.compressed_size will be 0 if there wasn't any checksum
1186 * information, also we need to stash the original size of the bio if we
1187 * bounced (which isn't necessarily the original key size, if we bounced
1188 * only for promoting)
1190 rbio->crc.compressed_size = bio_sectors(&rbio->bio);
1191 rbio->ptr = pick->ptr;
1192 rbio->ca = pick->ca;
1193 rbio->promote = promote_op;
1195 rbio->bio.bi_bdev = pick->ca->disk_sb.bdev;
1196 rbio->bio.bi_opf = orig->bio.bi_opf;
1197 rbio->bio.bi_iter.bi_sector = pick->ptr.offset;
1198 rbio->bio.bi_end_io = bch_read_endio;
1201 struct bio *promote_bio = &promote_op->write.wbio.bio;
1203 promote_bio->bi_iter = rbio->bio.bi_iter;
1204 memcpy(promote_bio->bi_io_vec, rbio->bio.bi_io_vec,
1205 sizeof(struct bio_vec) * rbio->bio.bi_vcnt);
1207 bch_migrate_write_init(c, &promote_op->write,
1208 &c->promote_write_point,
1210 BCH_WRITE_ALLOC_NOWAIT);
1211 promote_op->write.promote = true;
1213 if (rbio->crc.compression_type) {
1214 promote_op->write.op.flags |= BCH_WRITE_DATA_COMPRESSED;
1215 promote_op->write.op.crc = rbio->crc;
1216 promote_op->write.op.size = k.k->size;
1217 } else if (read_full) {
1219 * Adjust bio to correspond to _live_ portion of @k -
1220 * which might be less than what we're actually reading:
1222 bio_advance(promote_bio, rbio->crc.offset << 9);
1223 BUG_ON(bio_sectors(promote_bio) < k.k->size);
1224 promote_bio->bi_iter.bi_size = k.k->size << 9;
1227 * Set insert pos to correspond to what we're actually
1230 promote_op->write.op.pos.offset = iter.bi_sector;
1233 promote_bio->bi_iter.bi_sector =
1234 promote_op->write.op.pos.offset;
1237 /* _after_ promete stuff has looked at rbio->crc.offset */
1239 rbio->crc.offset += skip;
1241 rbio->bio.bi_iter.bi_sector += skip;
1243 rbio->submit_time_us = local_clock_us();
1245 #ifndef CONFIG_BCACHE_NO_IO
1246 generic_make_request(&rbio->bio);
1248 bio_endio(&rbio->bio);
1252 static void bch_read_iter(struct cache_set *c, struct bch_read_bio *rbio,
1253 struct bvec_iter bvec_iter, u64 inode,
1256 struct bio *bio = &rbio->bio;
1257 struct btree_iter iter;
1261 for_each_btree_key_with_holes(&iter, c, BTREE_ID_EXTENTS,
1262 POS(inode, bvec_iter.bi_sector), k) {
1264 struct extent_pick_ptr pick;
1265 unsigned bytes, sectors;
1269 * Unlock the iterator while the btree node's lock is still in
1270 * cache, before doing the IO:
1272 bkey_reassemble(&tmp.k, k);
1273 k = bkey_i_to_s_c(&tmp.k);
1274 bch_btree_iter_unlock(&iter);
1276 bch_extent_pick_ptr(c, k, &pick);
1277 if (IS_ERR(pick.ca)) {
1278 bcache_io_error(c, bio, "no device to read from");
1283 sectors = min_t(u64, k.k->p.offset,
1284 bvec_iter_end_sector(bvec_iter)) -
1285 bvec_iter.bi_sector;
1286 bytes = sectors << 9;
1287 is_last = bytes == bvec_iter.bi_size;
1288 swap(bvec_iter.bi_size, bytes);
1291 flags |= BCH_READ_IS_LAST;
1294 PTR_BUCKET(pick.ca, &pick.ptr)->read_prio =
1295 c->prio_clock[READ].hand;
1297 bch_read_extent_iter(c, rbio, bvec_iter,
1300 flags &= ~BCH_READ_MAY_REUSE_BIO;
1302 zero_fill_bio_iter(bio, bvec_iter);
1311 swap(bvec_iter.bi_size, bytes);
1312 bio_advance_iter(bio, &bvec_iter, bytes);
1316 * If we get here, it better have been because there was an error
1317 * reading a btree node
1319 ret = bch_btree_iter_unlock(&iter);
1321 bcache_io_error(c, bio, "btree IO error %i", ret);
1325 void bch_read(struct cache_set *c, struct bch_read_bio *bio, u64 inode)
1327 bch_increment_clock(c, bio_sectors(&bio->bio), READ);
1329 bch_read_iter(c, bio, bio->bio.bi_iter, inode,
1330 BCH_READ_FORCE_BOUNCE|
1331 BCH_READ_RETRY_IF_STALE|
1333 BCH_READ_MAY_REUSE_BIO);
1335 EXPORT_SYMBOL(bch_read);
1338 * bch_read_retry - re-submit a bio originally from bch_read()
1340 static void bch_read_retry(struct cache_set *c, struct bch_read_bio *rbio)
1342 struct bch_read_bio *parent = bch_rbio_parent(rbio);
1343 struct bvec_iter iter = rbio->parent_iter;
1344 u64 inode = rbio->inode;
1346 trace_bcache_read_retry(&rbio->bio);
1349 bch_rbio_free(c, rbio);
1351 rbio->bio.bi_end_io = rbio->orig_bi_end_io;
1353 bch_read_iter(c, parent, iter, inode,
1354 BCH_READ_FORCE_BOUNCE|
1355 BCH_READ_RETRY_IF_STALE|
1359 void bch_read_retry_work(struct work_struct *work)
1361 struct cache_set *c = container_of(work, struct cache_set,
1363 struct bch_read_bio *rbio;
1365 unsigned long flags;
1368 spin_lock_irqsave(&c->read_retry_lock, flags);
1369 bio = bio_list_pop(&c->read_retry_list);
1370 spin_unlock_irqrestore(&c->read_retry_lock, flags);
1375 rbio = container_of(bio, struct bch_read_bio, bio);
1376 bch_read_retry(c, rbio);