Lines Matching refs:b

104 #define insert_lock(s, b)	((b)->level <= (s)->lock)  argument
107 static inline struct bset *write_block(struct btree *b) in write_block() argument
109 return ((void *) btree_bset_first(b)) + b->written * block_bytes(b->c->cache); in write_block()
112 static void bch_btree_init_next(struct btree *b) in bch_btree_init_next() argument
115 if (b->level && b->keys.nsets) in bch_btree_init_next()
116 bch_btree_sort(&b->keys, &b->c->sort); in bch_btree_init_next()
118 bch_btree_sort_lazy(&b->keys, &b->c->sort); in bch_btree_init_next()
120 if (b->written < btree_blocks(b)) in bch_btree_init_next()
121 bch_bset_init_next(&b->keys, write_block(b), in bch_btree_init_next()
122 bset_magic(&b->c->cache->sb)); in bch_btree_init_next()
139 static uint64_t btree_csum_set(struct btree *b, struct bset *i) in btree_csum_set() argument
141 uint64_t crc = b->key.ptr[0]; in btree_csum_set()
148 void bch_btree_node_read_done(struct btree *b) in bch_btree_node_read_done() argument
151 struct bset *i = btree_bset_first(b); in bch_btree_node_read_done()
159 iter.heap.data = mempool_alloc(&b->c->fill_iter, GFP_NOIO); in bch_btree_node_read_done()
160 iter.heap.size = b->c->cache->sb.bucket_size / b->c->cache->sb.block_size; in bch_btree_node_read_done()
164 iter.b = &b->keys; in bch_btree_node_read_done()
171 b->written < btree_blocks(b) && i->seq == b->keys.set[0].data->seq; in bch_btree_node_read_done()
172 i = write_block(b)) { in bch_btree_node_read_done()
178 if (b->written + set_blocks(i, block_bytes(b->c->cache)) > in bch_btree_node_read_done()
179 btree_blocks(b)) in bch_btree_node_read_done()
183 if (i->magic != bset_magic(&b->c->cache->sb)) in bch_btree_node_read_done()
193 if (i->csum != btree_csum_set(b, i)) in bch_btree_node_read_done()
199 if (i != b->keys.set[0].data && !i->keys) in bch_btree_node_read_done()
204 b->written += set_blocks(i, block_bytes(b->c->cache)); in bch_btree_node_read_done()
208 for (i = write_block(b); in bch_btree_node_read_done()
209 bset_sector_offset(&b->keys, i) < KEY_SIZE(&b->key); in bch_btree_node_read_done()
210 i = ((void *) i) + block_bytes(b->c->cache)) in bch_btree_node_read_done()
211 if (i->seq == b->keys.set[0].data->seq) in bch_btree_node_read_done()
214 bch_btree_sort_and_fix_extents(&b->keys, &iter, &b->c->sort); in bch_btree_node_read_done()
216 i = b->keys.set[0].data; in bch_btree_node_read_done()
218 if (b->keys.set[0].size && in bch_btree_node_read_done()
219 bkey_cmp(&b->key, &b->keys.set[0].end) < 0) in bch_btree_node_read_done()
222 if (b->written < btree_blocks(b)) in bch_btree_node_read_done()
223 bch_bset_init_next(&b->keys, write_block(b), in bch_btree_node_read_done()
224 bset_magic(&b->c->cache->sb)); in bch_btree_node_read_done()
226 mempool_free(iter.heap.data, &b->c->fill_iter); in bch_btree_node_read_done()
229 set_btree_node_io_error(b); in bch_btree_node_read_done()
230 bch_cache_set_error(b->c, "%s at bucket %zu, block %u, %u keys", in bch_btree_node_read_done()
231 err, PTR_BUCKET_NR(b->c, &b->key, 0), in bch_btree_node_read_done()
232 bset_block_offset(b, i), i->keys); in bch_btree_node_read_done()
243 static void bch_btree_node_read(struct btree *b) in bch_btree_node_read() argument
249 trace_bcache_btree_read(b); in bch_btree_node_read()
253 bio = bch_bbio_alloc(b->c); in bch_btree_node_read()
254 bio->bi_iter.bi_size = KEY_SIZE(&b->key) << 9; in bch_btree_node_read()
259 bch_bio_map(bio, b->keys.set[0].data); in bch_btree_node_read()
261 bch_submit_bbio(bio, b->c, &b->key, 0); in bch_btree_node_read()
265 set_btree_node_io_error(b); in bch_btree_node_read()
267 bch_bbio_free(bio, b->c); in bch_btree_node_read()
269 if (btree_node_io_error(b)) in bch_btree_node_read()
272 bch_btree_node_read_done(b); in bch_btree_node_read()
273 bch_time_stats_update(&b->c->btree_read_time, start_time); in bch_btree_node_read()
277 bch_cache_set_error(b->c, "io error reading bucket %zu", in bch_btree_node_read()
278 PTR_BUCKET_NR(b->c, &b->key, 0)); in bch_btree_node_read()
281 static void btree_complete_write(struct btree *b, struct btree_write *w) in btree_complete_write() argument
284 !atomic_sub_return(w->prio_blocked, &b->c->prio_blocked)) in btree_complete_write()
285 wake_up_allocators(b->c); in btree_complete_write()
289 __closure_wake_up(&b->c->journal.wait); in btree_complete_write()
298 closure_type(b, struct btree, io); in CLOSURE_CALLBACK()
300 up(&b->io_mutex); in CLOSURE_CALLBACK()
305 closure_type(b, struct btree, io); in CLOSURE_CALLBACK()
306 struct btree_write *w = btree_prev_write(b); in CLOSURE_CALLBACK()
308 bch_bbio_free(b->bio, b->c); in CLOSURE_CALLBACK()
309 b->bio = NULL; in CLOSURE_CALLBACK()
310 btree_complete_write(b, w); in CLOSURE_CALLBACK()
312 if (btree_node_dirty(b)) in CLOSURE_CALLBACK()
313 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ); in CLOSURE_CALLBACK()
320 closure_type(b, struct btree, io); in CLOSURE_CALLBACK()
322 bio_free_pages(b->bio); in CLOSURE_CALLBACK()
329 struct btree *b = container_of(cl, struct btree, io); in btree_node_write_endio() local
332 set_btree_node_io_error(b); in btree_node_write_endio()
334 bch_bbio_count_io_errors(b->c, bio, bio->bi_status, "writing btree"); in btree_node_write_endio()
338 static void do_btree_node_write(struct btree *b) in do_btree_node_write() argument
340 struct closure *cl = &b->io; in do_btree_node_write()
341 struct bset *i = btree_bset_last(b); in do_btree_node_write()
345 i->csum = btree_csum_set(b, i); in do_btree_node_write()
347 BUG_ON(b->bio); in do_btree_node_write()
348 b->bio = bch_bbio_alloc(b->c); in do_btree_node_write()
350 b->bio->bi_end_io = btree_node_write_endio; in do_btree_node_write()
351 b->bio->bi_private = cl; in do_btree_node_write()
352 b->bio->bi_iter.bi_size = roundup(set_bytes(i), block_bytes(b->c->cache)); in do_btree_node_write()
353 b->bio->bi_opf = REQ_OP_WRITE | REQ_META | REQ_FUA; in do_btree_node_write()
354 bch_bio_map(b->bio, i); in do_btree_node_write()
371 bkey_copy(&k.key, &b->key); in do_btree_node_write()
373 bset_sector_offset(&b->keys, i)); in do_btree_node_write()
375 if (!bch_bio_alloc_pages(b->bio, __GFP_NOWARN|GFP_NOWAIT)) { in do_btree_node_write()
380 bio_for_each_segment_all(bv, b->bio, iter_all) { in do_btree_node_write()
385 bch_submit_bbio(b->bio, b->c, &k.key, 0); in do_btree_node_write()
393 b->bio->bi_vcnt = 0; in do_btree_node_write()
394 bch_bio_map(b->bio, i); in do_btree_node_write()
396 bch_submit_bbio(b->bio, b->c, &k.key, 0); in do_btree_node_write()
403 void __bch_btree_node_write(struct btree *b, struct closure *parent) in __bch_btree_node_write() argument
405 struct bset *i = btree_bset_last(b); in __bch_btree_node_write()
407 lockdep_assert_held(&b->write_lock); in __bch_btree_node_write()
409 trace_bcache_btree_write(b); in __bch_btree_node_write()
412 BUG_ON(b->written >= btree_blocks(b)); in __bch_btree_node_write()
413 BUG_ON(b->written && !i->keys); in __bch_btree_node_write()
414 BUG_ON(btree_bset_first(b)->seq != i->seq); in __bch_btree_node_write()
415 bch_check_keys(&b->keys, "writing"); in __bch_btree_node_write()
417 cancel_delayed_work(&b->work); in __bch_btree_node_write()
420 down(&b->io_mutex); in __bch_btree_node_write()
421 closure_init(&b->io, parent ?: &b->c->cl); in __bch_btree_node_write()
423 clear_bit(BTREE_NODE_dirty, &b->flags); in __bch_btree_node_write()
424 change_bit(BTREE_NODE_write_idx, &b->flags); in __bch_btree_node_write()
426 do_btree_node_write(b); in __bch_btree_node_write()
428 atomic_long_add(set_blocks(i, block_bytes(b->c->cache)) * b->c->cache->sb.block_size, in __bch_btree_node_write()
429 &b->c->cache->btree_sectors_written); in __bch_btree_node_write()
431 b->written += set_blocks(i, block_bytes(b->c->cache)); in __bch_btree_node_write()
434 void bch_btree_node_write(struct btree *b, struct closure *parent) in bch_btree_node_write() argument
436 unsigned int nsets = b->keys.nsets; in bch_btree_node_write()
438 lockdep_assert_held(&b->lock); in bch_btree_node_write()
440 __bch_btree_node_write(b, parent); in bch_btree_node_write()
446 if (nsets && !b->keys.nsets) in bch_btree_node_write()
447 bch_btree_verify(b); in bch_btree_node_write()
449 bch_btree_init_next(b); in bch_btree_node_write()
452 static void bch_btree_node_write_sync(struct btree *b) in bch_btree_node_write_sync() argument
458 mutex_lock(&b->write_lock); in bch_btree_node_write_sync()
459 bch_btree_node_write(b, &cl); in bch_btree_node_write_sync()
460 mutex_unlock(&b->write_lock); in bch_btree_node_write_sync()
467 struct btree *b = container_of(to_delayed_work(w), struct btree, work); in btree_node_write_work() local
469 mutex_lock(&b->write_lock); in btree_node_write_work()
470 if (btree_node_dirty(b)) in btree_node_write_work()
471 __bch_btree_node_write(b, NULL); in btree_node_write_work()
472 mutex_unlock(&b->write_lock); in btree_node_write_work()
475 static void bch_btree_leaf_dirty(struct btree *b, atomic_t *journal_ref) in bch_btree_leaf_dirty() argument
477 struct bset *i = btree_bset_last(b); in bch_btree_leaf_dirty()
478 struct btree_write *w = btree_current_write(b); in bch_btree_leaf_dirty()
480 lockdep_assert_held(&b->write_lock); in bch_btree_leaf_dirty()
482 BUG_ON(!b->written); in bch_btree_leaf_dirty()
485 if (!btree_node_dirty(b)) in bch_btree_leaf_dirty()
486 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ); in bch_btree_leaf_dirty()
488 set_btree_node_dirty(b); in bch_btree_leaf_dirty()
497 journal_pin_cmp(b->c, w->journal, journal_ref)) { in bch_btree_leaf_dirty()
511 bch_btree_node_write(b, NULL); in bch_btree_leaf_dirty()
524 static void mca_data_free(struct btree *b) in mca_data_free() argument
526 BUG_ON(b->io_mutex.count != 1); in mca_data_free()
528 bch_btree_keys_free(&b->keys); in mca_data_free()
530 b->c->btree_cache_used--; in mca_data_free()
531 list_move(&b->list, &b->c->btree_cache_freed); in mca_data_free()
534 static void mca_bucket_free(struct btree *b) in mca_bucket_free() argument
536 BUG_ON(btree_node_dirty(b)); in mca_bucket_free()
538 b->key.ptr[0] = 0; in mca_bucket_free()
539 hlist_del_init_rcu(&b->hash); in mca_bucket_free()
540 list_move(&b->list, &b->c->btree_cache_freeable); in mca_bucket_free()
548 static void mca_data_alloc(struct btree *b, struct bkey *k, gfp_t gfp) in mca_data_alloc() argument
550 if (!bch_btree_keys_alloc(&b->keys, in mca_data_alloc()
552 ilog2(b->c->btree_pages), in mca_data_alloc()
555 b->c->btree_cache_used++; in mca_data_alloc()
556 list_move(&b->list, &b->c->btree_cache); in mca_data_alloc()
558 list_move(&b->list, &b->c->btree_cache_freed); in mca_data_alloc()
569 const struct btree *b = container_of(_b, struct btree, lock.dep_map); in btree_lock_cmp_fn() local
571 return -cmp_int(a->level, b->level) ?: bkey_cmp(&a->key, &b->key); in btree_lock_cmp_fn()
576 const struct btree *b = container_of(map, struct btree, lock.dep_map); in btree_lock_print_fn() local
578 printk(KERN_CONT " l=%u %llu:%llu", b->level, in btree_lock_print_fn()
579 KEY_INODE(&b->key), KEY_OFFSET(&b->key)); in btree_lock_print_fn()
590 struct btree *b = kzalloc(sizeof(struct btree), gfp); in mca_bucket_alloc() local
592 if (!b) in mca_bucket_alloc()
595 init_rwsem(&b->lock); in mca_bucket_alloc()
596 lock_set_cmp_fn(&b->lock, btree_lock_cmp_fn, btree_lock_print_fn); in mca_bucket_alloc()
597 mutex_init(&b->write_lock); in mca_bucket_alloc()
598 lockdep_set_novalidate_class(&b->write_lock); in mca_bucket_alloc()
599 INIT_LIST_HEAD(&b->list); in mca_bucket_alloc()
600 INIT_DELAYED_WORK(&b->work, btree_node_write_work); in mca_bucket_alloc()
601 b->c = c; in mca_bucket_alloc()
602 sema_init(&b->io_mutex, 1); in mca_bucket_alloc()
604 mca_data_alloc(b, k, gfp); in mca_bucket_alloc()
605 return b; in mca_bucket_alloc()
608 static int mca_reap(struct btree *b, unsigned int min_order, bool flush) in mca_reap() argument
613 lockdep_assert_held(&b->c->bucket_lock); in mca_reap()
615 if (!down_write_trylock(&b->lock)) in mca_reap()
618 BUG_ON(btree_node_dirty(b) && !b->keys.set[0].data); in mca_reap()
620 if (b->keys.page_order < min_order) in mca_reap()
624 if (btree_node_dirty(b)) in mca_reap()
627 if (down_trylock(&b->io_mutex)) in mca_reap()
629 up(&b->io_mutex); in mca_reap()
638 mutex_lock(&b->write_lock); in mca_reap()
644 if (btree_node_journal_flush(b)) { in mca_reap()
645 pr_debug("bnode %p is flushing by journal, retry\n", b); in mca_reap()
646 mutex_unlock(&b->write_lock); in mca_reap()
651 if (btree_node_dirty(b)) in mca_reap()
652 __bch_btree_node_write(b, &cl); in mca_reap()
653 mutex_unlock(&b->write_lock); in mca_reap()
658 down(&b->io_mutex); in mca_reap()
659 up(&b->io_mutex); in mca_reap()
663 rw_unlock(true, b); in mca_reap()
671 struct btree *b, *t; in bch_mca_scan() local
702 list_for_each_entry_safe_reverse(b, t, &c->btree_cache_freeable, list) { in bch_mca_scan()
706 if (!mca_reap(b, 0, false)) { in bch_mca_scan()
707 mca_data_free(b); in bch_mca_scan()
708 rw_unlock(true, b); in bch_mca_scan()
715 list_for_each_entry_safe_reverse(b, t, &c->btree_cache, list) { in bch_mca_scan()
719 if (!mca_reap(b, 0, false)) { in bch_mca_scan()
720 mca_bucket_free(b); in bch_mca_scan()
721 mca_data_free(b); in bch_mca_scan()
722 rw_unlock(true, b); in bch_mca_scan()
750 struct btree *b; in bch_btree_cache_free() local
771 b = list_first_entry(&c->btree_cache, struct btree, list); in bch_btree_cache_free()
778 if (btree_node_dirty(b)) { in bch_btree_cache_free()
779 btree_complete_write(b, btree_current_write(b)); in bch_btree_cache_free()
780 clear_bit(BTREE_NODE_dirty, &b->flags); in bch_btree_cache_free()
782 mca_data_free(b); in bch_btree_cache_free()
786 b = list_first_entry(&c->btree_cache_freed, in bch_btree_cache_free()
788 list_del(&b->list); in bch_btree_cache_free()
789 cancel_delayed_work_sync(&b->work); in bch_btree_cache_free()
790 kfree(b); in bch_btree_cache_free()
857 struct btree *b; in mca_find() local
860 hlist_for_each_entry_rcu(b, mca_hash(c, k), hash) in mca_find()
861 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k)) in mca_find()
863 b = NULL; in mca_find()
866 return b; in mca_find()
889 struct btree *b; in mca_cannibalize() local
896 list_for_each_entry_reverse(b, &c->btree_cache, list) in mca_cannibalize()
897 if (!mca_reap(b, btree_order(k), false)) in mca_cannibalize()
898 return b; in mca_cannibalize()
900 list_for_each_entry_reverse(b, &c->btree_cache, list) in mca_cannibalize()
901 if (!mca_reap(b, btree_order(k), true)) in mca_cannibalize()
902 return b; in mca_cannibalize()
927 struct btree *b; in mca_alloc() local
939 list_for_each_entry(b, &c->btree_cache_freeable, list) in mca_alloc()
940 if (!mca_reap(b, btree_order(k), false)) in mca_alloc()
946 list_for_each_entry(b, &c->btree_cache_freed, list) in mca_alloc()
947 if (!mca_reap(b, 0, false)) { in mca_alloc()
948 mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO); in mca_alloc()
949 if (!b->keys.set[0].data) in mca_alloc()
955 b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO); in mca_alloc()
956 if (!b) in mca_alloc()
959 BUG_ON(!down_write_trylock(&b->lock)); in mca_alloc()
960 if (!b->keys.set->data) in mca_alloc()
963 BUG_ON(b->io_mutex.count != 1); in mca_alloc()
965 bkey_copy(&b->key, k); in mca_alloc()
966 list_move(&b->list, &c->btree_cache); in mca_alloc()
967 hlist_del_init_rcu(&b->hash); in mca_alloc()
968 hlist_add_head_rcu(&b->hash, mca_hash(c, k)); in mca_alloc()
970 lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_); in mca_alloc()
971 b->parent = (void *) ~0UL; in mca_alloc()
972 b->flags = 0; in mca_alloc()
973 b->written = 0; in mca_alloc()
974 b->level = level; in mca_alloc()
976 if (!b->level) in mca_alloc()
977 bch_btree_keys_init(&b->keys, &bch_extent_keys_ops, in mca_alloc()
978 &b->c->expensive_debug_checks); in mca_alloc()
980 bch_btree_keys_init(&b->keys, &bch_btree_keys_ops, in mca_alloc()
981 &b->c->expensive_debug_checks); in mca_alloc()
983 return b; in mca_alloc()
985 if (b) in mca_alloc()
986 rw_unlock(true, b); in mca_alloc()
988 b = mca_cannibalize(c, op, k); in mca_alloc()
989 if (!IS_ERR(b)) in mca_alloc()
992 return b; in mca_alloc()
1012 struct btree *b; in bch_btree_node_get() local
1016 b = mca_find(c, k); in bch_btree_node_get()
1018 if (!b) { in bch_btree_node_get()
1023 b = mca_alloc(c, op, k, level); in bch_btree_node_get()
1026 if (!b) in bch_btree_node_get()
1028 if (IS_ERR(b)) in bch_btree_node_get()
1029 return b; in bch_btree_node_get()
1031 bch_btree_node_read(b); in bch_btree_node_get()
1034 downgrade_write(&b->lock); in bch_btree_node_get()
1036 rw_lock(write, b, level); in bch_btree_node_get()
1037 if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) { in bch_btree_node_get()
1038 rw_unlock(write, b); in bch_btree_node_get()
1041 BUG_ON(b->level != level); in bch_btree_node_get()
1044 if (btree_node_io_error(b)) { in bch_btree_node_get()
1045 rw_unlock(write, b); in bch_btree_node_get()
1049 BUG_ON(!b->written); in bch_btree_node_get()
1051 b->parent = parent; in bch_btree_node_get()
1053 for (; i <= b->keys.nsets && b->keys.set[i].size; i++) { in bch_btree_node_get()
1054 prefetch(b->keys.set[i].tree); in bch_btree_node_get()
1055 prefetch(b->keys.set[i].data); in bch_btree_node_get()
1058 for (; i <= b->keys.nsets; i++) in bch_btree_node_get()
1059 prefetch(b->keys.set[i].data); in bch_btree_node_get()
1061 return b; in bch_btree_node_get()
1066 struct btree *b; in btree_node_prefetch() local
1069 b = mca_alloc(parent->c, NULL, k, parent->level - 1); in btree_node_prefetch()
1072 if (!IS_ERR_OR_NULL(b)) { in btree_node_prefetch()
1073 b->parent = parent; in btree_node_prefetch()
1074 bch_btree_node_read(b); in btree_node_prefetch()
1075 rw_unlock(true, b); in btree_node_prefetch()
1081 static void btree_node_free(struct btree *b) in btree_node_free() argument
1083 trace_bcache_btree_node_free(b); in btree_node_free()
1085 BUG_ON(b == b->c->root); in btree_node_free()
1088 mutex_lock(&b->write_lock); in btree_node_free()
1095 if (btree_node_journal_flush(b)) { in btree_node_free()
1096 mutex_unlock(&b->write_lock); in btree_node_free()
1097 pr_debug("bnode %p journal_flush set, retry\n", b); in btree_node_free()
1102 if (btree_node_dirty(b)) { in btree_node_free()
1103 btree_complete_write(b, btree_current_write(b)); in btree_node_free()
1104 clear_bit(BTREE_NODE_dirty, &b->flags); in btree_node_free()
1107 mutex_unlock(&b->write_lock); in btree_node_free()
1109 cancel_delayed_work(&b->work); in btree_node_free()
1111 mutex_lock(&b->c->bucket_lock); in btree_node_free()
1112 bch_bucket_free(b->c, &b->key); in btree_node_free()
1113 mca_bucket_free(b); in btree_node_free()
1114 mutex_unlock(&b->c->bucket_lock); in btree_node_free()
1126 struct btree *b; in __bch_btree_node_alloc() local
1131 b = ERR_PTR(-EAGAIN); in __bch_btree_node_alloc()
1138 b = mca_alloc(c, op, &k.key, level); in __bch_btree_node_alloc()
1139 if (IS_ERR(b)) in __bch_btree_node_alloc()
1142 if (!b) { in __bch_btree_node_alloc()
1148 b->parent = parent; in __bch_btree_node_alloc()
1149 bch_bset_init_next(&b->keys, b->keys.set->data, bset_magic(&b->c->cache->sb)); in __bch_btree_node_alloc()
1153 trace_bcache_btree_node_alloc(b); in __bch_btree_node_alloc()
1154 return b; in __bch_btree_node_alloc()
1161 return b; in __bch_btree_node_alloc()
1171 static struct btree *btree_node_alloc_replacement(struct btree *b, in btree_node_alloc_replacement() argument
1174 struct btree *n = bch_btree_node_alloc(b->c, op, b->level, b->parent); in btree_node_alloc_replacement()
1178 bch_btree_sort_into(&b->keys, &n->keys, &b->c->sort); in btree_node_alloc_replacement()
1179 bkey_copy_key(&n->key, &b->key); in btree_node_alloc_replacement()
1186 static void make_btree_freeing_key(struct btree *b, struct bkey *k) in make_btree_freeing_key() argument
1190 mutex_lock(&b->c->bucket_lock); in make_btree_freeing_key()
1192 atomic_inc(&b->c->prio_blocked); in make_btree_freeing_key()
1194 bkey_copy(k, &b->key); in make_btree_freeing_key()
1199 bch_inc_gen(b->c->cache, in make_btree_freeing_key()
1200 PTR_BUCKET(b->c, &b->key, i))); in make_btree_freeing_key()
1202 mutex_unlock(&b->c->bucket_lock); in make_btree_freeing_key()
1205 static int btree_check_reserve(struct btree *b, struct btree_op *op) in btree_check_reserve() argument
1207 struct cache_set *c = b->c; in btree_check_reserve()
1209 unsigned int reserve = (c->root->level - b->level) * 2 + 1; in btree_check_reserve()
1223 return mca_cannibalize_lock(b->c, op); in btree_check_reserve()
1280 #define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k) argument
1289 struct bucket *b = PTR_BUCKET(c, k, i); in bch_initial_mark_key() local
1291 b->gen = PTR_GEN(k, i); in bch_initial_mark_key()
1294 b->prio = BTREE_PRIO; in bch_initial_mark_key()
1295 else if (!level && b->prio == BTREE_PRIO) in bch_initial_mark_key()
1296 b->prio = INITIAL_PRIO; in bch_initial_mark_key()
1307 static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc) in btree_gc_mark_node() argument
1319 for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) { in btree_gc_mark_node()
1320 stale = max(stale, btree_mark_key(b, k)); in btree_gc_mark_node()
1323 if (bch_ptr_bad(&b->keys, k)) in btree_gc_mark_node()
1333 for (t = b->keys.set; t <= &b->keys.set[b->keys.nsets]; t++) in btree_gc_mark_node()
1335 bset_written(&b->keys, t) && in btree_gc_mark_node()
1336 bkey_cmp(&b->key, &t->end) < 0, in btree_gc_mark_node()
1337 b, "found short btree key in gc"); in btree_gc_mark_node()
1339 if (b->c->gc_always_rewrite) in btree_gc_mark_node()
1354 struct btree *b; member
1358 static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
1363 static int btree_gc_coalesce(struct btree *b, struct btree_op *op, in btree_gc_coalesce() argument
1374 if (btree_check_reserve(b, NULL)) in btree_gc_coalesce()
1380 while (nodes < GC_MERGE_NODES && !IS_ERR_OR_NULL(r[nodes].b)) in btree_gc_coalesce()
1383 blocks = btree_default_blocks(b->c) * 2 / 3; in btree_gc_coalesce()
1386 __set_blocks(b->keys.set[0].data, keys, in btree_gc_coalesce()
1387 block_bytes(b->c->cache)) > blocks * (nodes - 1)) in btree_gc_coalesce()
1391 new_nodes[i] = btree_node_alloc_replacement(r[i].b, NULL); in btree_gc_coalesce()
1402 if (btree_check_reserve(b, NULL)) in btree_gc_coalesce()
1421 block_bytes(b->c->cache)) > blocks) in btree_gc_coalesce()
1437 block_bytes(b->c->cache)) > in btree_gc_coalesce()
1443 last = &r->b->key; in btree_gc_coalesce()
1446 BUG_ON(__set_blocks(n1, n1->keys + keys, block_bytes(b->c->cache)) > in btree_gc_coalesce()
1486 if (__bch_keylist_realloc(&keylist, bkey_u64s(&r[i].b->key))) in btree_gc_coalesce()
1489 make_btree_freeing_key(r[i].b, keylist.top); in btree_gc_coalesce()
1493 bch_btree_insert_node(b, op, &keylist, NULL, NULL); in btree_gc_coalesce()
1497 btree_node_free(r[i].b); in btree_gc_coalesce()
1498 rw_unlock(true, r[i].b); in btree_gc_coalesce()
1500 r[i].b = new_nodes[i]; in btree_gc_coalesce()
1504 r[nodes - 1].b = ERR_PTR(-EINTR); in btree_gc_coalesce()
1523 atomic_dec(&b->c->prio_blocked); in btree_gc_coalesce()
1534 static int btree_gc_rewrite_node(struct btree *b, struct btree_op *op, in btree_gc_rewrite_node() argument
1540 if (btree_check_reserve(b, NULL)) in btree_gc_rewrite_node()
1548 if (btree_check_reserve(b, NULL)) { in btree_gc_rewrite_node()
1562 bch_btree_insert_node(b, op, &keys, NULL, NULL); in btree_gc_rewrite_node()
1572 static unsigned int btree_gc_count_keys(struct btree *b) in btree_gc_count_keys() argument
1580 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad) in btree_gc_count_keys()
1612 static int btree_gc_recurse(struct btree *b, struct btree_op *op, in btree_gc_recurse() argument
1623 bch_btree_iter_init(&b->keys, &iter, &b->c->gc_done); in btree_gc_recurse()
1626 i->b = ERR_PTR(-EINTR); in btree_gc_recurse()
1629 k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad); in btree_gc_recurse()
1631 r->b = bch_btree_node_get(b->c, op, k, b->level - 1, in btree_gc_recurse()
1632 true, b); in btree_gc_recurse()
1633 if (IS_ERR(r->b)) { in btree_gc_recurse()
1634 ret = PTR_ERR(r->b); in btree_gc_recurse()
1638 r->keys = btree_gc_count_keys(r->b); in btree_gc_recurse()
1640 ret = btree_gc_coalesce(b, op, gc, r); in btree_gc_recurse()
1645 if (!last->b) in btree_gc_recurse()
1648 if (!IS_ERR(last->b)) { in btree_gc_recurse()
1649 should_rewrite = btree_gc_mark_node(last->b, gc); in btree_gc_recurse()
1651 ret = btree_gc_rewrite_node(b, op, last->b); in btree_gc_recurse()
1656 if (last->b->level) { in btree_gc_recurse()
1657 ret = btree_gc_recurse(last->b, op, writes, gc); in btree_gc_recurse()
1662 bkey_copy_key(&b->c->gc_done, &last->b->key); in btree_gc_recurse()
1668 mutex_lock(&last->b->write_lock); in btree_gc_recurse()
1669 if (btree_node_dirty(last->b)) in btree_gc_recurse()
1670 bch_btree_node_write(last->b, writes); in btree_gc_recurse()
1671 mutex_unlock(&last->b->write_lock); in btree_gc_recurse()
1672 rw_unlock(true, last->b); in btree_gc_recurse()
1676 r->b = NULL; in btree_gc_recurse()
1678 if (atomic_read(&b->c->search_inflight) && in btree_gc_recurse()
1679 gc->nodes >= gc->nodes_pre + btree_gc_min_nodes(b->c)) { in btree_gc_recurse()
1692 if (!IS_ERR_OR_NULL(i->b)) { in btree_gc_recurse()
1693 mutex_lock(&i->b->write_lock); in btree_gc_recurse()
1694 if (btree_node_dirty(i->b)) in btree_gc_recurse()
1695 bch_btree_node_write(i->b, writes); in btree_gc_recurse()
1696 mutex_unlock(&i->b->write_lock); in btree_gc_recurse()
1697 rw_unlock(true, i->b); in btree_gc_recurse()
1703 static int bch_btree_gc_root(struct btree *b, struct btree_op *op, in bch_btree_gc_root() argument
1710 should_rewrite = btree_gc_mark_node(b, gc); in bch_btree_gc_root()
1712 n = btree_node_alloc_replacement(b, NULL); in bch_btree_gc_root()
1718 btree_node_free(b); in bch_btree_gc_root()
1725 __bch_btree_mark_key(b->c, b->level + 1, &b->key); in bch_btree_gc_root()
1727 if (b->level) { in bch_btree_gc_root()
1728 ret = btree_gc_recurse(b, op, writes, gc); in bch_btree_gc_root()
1733 bkey_copy_key(&b->c->gc_done, &b->key); in bch_btree_gc_root()
1741 struct bucket *b; in btree_gc_start() local
1751 for_each_bucket(b, ca) { in btree_gc_start()
1752 b->last_gc = b->gen; in btree_gc_start()
1753 if (bch_can_invalidate_bucket(ca, b)) in btree_gc_start()
1754 b->reclaimable_in_gc = 1; in btree_gc_start()
1755 if (!atomic_read(&b->pin)) { in btree_gc_start()
1756 SET_GC_MARK(b, 0); in btree_gc_start()
1757 SET_GC_SECTORS_USED(b, 0); in btree_gc_start()
1767 struct bucket *b; in bch_btree_gc_finish() local
1815 for_each_bucket(b, ca) { in bch_btree_gc_finish()
1816 c->need_gc = max(c->need_gc, bucket_gc_gen(b)); in bch_btree_gc_finish()
1818 if (b->reclaimable_in_gc) in bch_btree_gc_finish()
1819 b->reclaimable_in_gc = 0; in bch_btree_gc_finish()
1821 if (atomic_read(&b->pin)) in bch_btree_gc_finish()
1824 BUG_ON(!GC_MARK(b) && GC_SECTORS_USED(b)); in bch_btree_gc_finish()
1826 if (!GC_MARK(b) || GC_MARK(b) == GC_MARK_RECLAIMABLE) in bch_btree_gc_finish()
1920 static int bch_btree_check_recurse(struct btree *b, struct btree_op *op) in bch_btree_check_recurse() argument
1928 for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) in bch_btree_check_recurse()
1929 bch_initial_mark_key(b->c, b->level, k); in bch_btree_check_recurse()
1931 bch_initial_mark_key(b->c, b->level + 1, &b->key); in bch_btree_check_recurse()
1933 if (b->level) { in bch_btree_check_recurse()
1934 bch_btree_iter_init(&b->keys, &iter, NULL); in bch_btree_check_recurse()
1937 k = bch_btree_iter_next_filter(&iter, &b->keys, in bch_btree_check_recurse()
1940 btree_node_prefetch(b, k); in bch_btree_check_recurse()
1945 b->c->gc_stats.nodes++; in bch_btree_check_recurse()
1949 ret = bcache_btree(check_recurse, p, b, op); in bch_btree_check_recurse()
2141 struct bucket *b; in bch_initial_gc_finish() local
2156 for_each_bucket(b, ca) { in bch_initial_gc_finish()
2161 if (bch_can_invalidate_bucket(ca, b) && in bch_initial_gc_finish()
2162 !GC_MARK(b)) { in bch_initial_gc_finish()
2163 __bch_invalidate_one_bucket(ca, b); in bch_initial_gc_finish()
2165 b - ca->buckets)) in bch_initial_gc_finish()
2167 b - ca->buckets); in bch_initial_gc_finish()
2176 static bool btree_insert_key(struct btree *b, struct bkey *k, in btree_insert_key() argument
2181 BUG_ON(bkey_cmp(k, &b->key) > 0); in btree_insert_key()
2183 status = bch_btree_insert_key(&b->keys, k, replace_key); in btree_insert_key()
2185 bch_check_keys(&b->keys, "%u for %s", status, in btree_insert_key()
2188 trace_bcache_btree_insert_key(b, k, replace_key != NULL, in btree_insert_key()
2195 static size_t insert_u64s_remaining(struct btree *b) in insert_u64s_remaining() argument
2197 long ret = bch_btree_keys_u64s_remaining(&b->keys); in insert_u64s_remaining()
2202 if (b->keys.ops->is_extents) in insert_u64s_remaining()
2208 static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op, in bch_btree_insert_keys() argument
2213 int oldsize = bch_count_data(&b->keys); in bch_btree_insert_keys()
2218 if (bkey_u64s(k) > insert_u64s_remaining(b)) in bch_btree_insert_keys()
2221 if (bkey_cmp(k, &b->key) <= 0) { in bch_btree_insert_keys()
2222 if (!b->level) in bch_btree_insert_keys()
2223 bkey_put(b->c, k); in bch_btree_insert_keys()
2225 ret |= btree_insert_key(b, k, replace_key); in bch_btree_insert_keys()
2227 } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) { in bch_btree_insert_keys()
2231 bch_cut_back(&b->key, &temp.key); in bch_btree_insert_keys()
2232 bch_cut_front(&b->key, insert_keys->keys); in bch_btree_insert_keys()
2234 ret |= btree_insert_key(b, &temp.key, replace_key); in bch_btree_insert_keys()
2244 BUG_ON(!bch_keylist_empty(insert_keys) && b->level); in bch_btree_insert_keys()
2246 BUG_ON(bch_count_data(&b->keys) < oldsize); in bch_btree_insert_keys()
2250 static int btree_split(struct btree *b, struct btree_op *op, in btree_split() argument
2263 if (btree_check_reserve(b, op)) { in btree_split()
2264 if (!b->level) in btree_split()
2270 n1 = btree_node_alloc_replacement(b, op); in btree_split()
2275 block_bytes(n1->c->cache)) > (btree_blocks(b) * 4) / 5; in btree_split()
2280 trace_bcache_btree_node_split(b, btree_bset_first(n1)->keys); in btree_split()
2282 n2 = bch_btree_node_alloc(b->c, op, b->level, b->parent); in btree_split()
2286 if (!b->parent) { in btree_split()
2287 n3 = bch_btree_node_alloc(b->c, op, b->level + 1, NULL); in btree_split()
2317 bkey_copy_key(&n2->key, &b->key); in btree_split()
2324 trace_bcache_btree_node_compact(b, btree_bset_first(n1)->keys); in btree_split()
2345 } else if (!b->parent) { in btree_split()
2352 make_btree_freeing_key(b, parent_keys.top); in btree_split()
2355 bch_btree_insert_node(b->parent, op, &parent_keys, NULL, NULL); in btree_split()
2359 btree_node_free(b); in btree_split()
2362 bch_time_stats_update(&b->c->btree_split_time, start_time); in btree_split()
2366 bkey_put(b->c, &n2->key); in btree_split()
2370 bkey_put(b->c, &n1->key); in btree_split()
2374 WARN(1, "bcache: btree split failed (level %u)", b->level); in btree_split()
2384 static int bch_btree_insert_node(struct btree *b, struct btree_op *op, in bch_btree_insert_node() argument
2391 BUG_ON(b->level && replace_key); in bch_btree_insert_node()
2395 mutex_lock(&b->write_lock); in bch_btree_insert_node()
2397 if (write_block(b) != btree_bset_last(b) && in bch_btree_insert_node()
2398 b->keys.last_set_unwritten) in bch_btree_insert_node()
2399 bch_btree_init_next(b); /* just wrote a set */ in bch_btree_insert_node()
2401 if (bch_keylist_nkeys(insert_keys) > insert_u64s_remaining(b)) { in bch_btree_insert_node()
2402 mutex_unlock(&b->write_lock); in bch_btree_insert_node()
2406 BUG_ON(write_block(b) != btree_bset_last(b)); in bch_btree_insert_node()
2408 if (bch_btree_insert_keys(b, op, insert_keys, replace_key)) { in bch_btree_insert_node()
2409 if (!b->level) in bch_btree_insert_node()
2410 bch_btree_leaf_dirty(b, journal_ref); in bch_btree_insert_node()
2412 bch_btree_node_write(b, &cl); in bch_btree_insert_node()
2415 mutex_unlock(&b->write_lock); in bch_btree_insert_node()
2423 op->lock = b->c->root->level + 1; in bch_btree_insert_node()
2425 } else if (op->lock <= b->c->root->level) { in bch_btree_insert_node()
2426 op->lock = b->c->root->level + 1; in bch_btree_insert_node()
2430 int ret = btree_split(b, op, insert_keys, replace_key); in bch_btree_insert_node()
2440 int bch_btree_insert_check_key(struct btree *b, struct btree_op *op, in bch_btree_insert_check_key() argument
2444 uint64_t btree_ptr = b->key.ptr[0]; in bch_btree_insert_check_key()
2445 unsigned long seq = b->seq; in bch_btree_insert_check_key()
2452 rw_unlock(false, b); in bch_btree_insert_check_key()
2453 rw_lock(true, b, b->level); in bch_btree_insert_check_key()
2455 if (b->key.ptr[0] != btree_ptr || in bch_btree_insert_check_key()
2456 b->seq != seq + 1) { in bch_btree_insert_check_key()
2457 op->lock = b->level; in bch_btree_insert_check_key()
2469 ret = bch_btree_insert_node(b, op, &insert, NULL, NULL); in bch_btree_insert_check_key()
2474 downgrade_write(&b->lock); in bch_btree_insert_check_key()
2485 static int btree_insert_fn(struct btree_op *b_op, struct btree *b) in btree_insert_fn() argument
2490 int ret = bch_btree_insert_node(b, &op->op, op->keys, in btree_insert_fn()
2532 void bch_btree_set_root(struct btree *b) in bch_btree_set_root() argument
2539 trace_bcache_btree_set_root(b); in bch_btree_set_root()
2541 BUG_ON(!b->written); in bch_btree_set_root()
2543 for (i = 0; i < KEY_PTRS(&b->key); i++) in bch_btree_set_root()
2544 BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO); in bch_btree_set_root()
2546 mutex_lock(&b->c->bucket_lock); in bch_btree_set_root()
2547 list_del_init(&b->list); in bch_btree_set_root()
2548 mutex_unlock(&b->c->bucket_lock); in bch_btree_set_root()
2550 b->c->root = b; in bch_btree_set_root()
2552 bch_journal_meta(b->c, &cl); in bch_btree_set_root()
2558 static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op, in bch_btree_map_nodes_recurse() argument
2564 if (b->level) { in bch_btree_map_nodes_recurse()
2569 bch_btree_iter_init(&b->keys, &iter, from); in bch_btree_map_nodes_recurse()
2571 while ((k = bch_btree_iter_next_filter(&iter, &b->keys, in bch_btree_map_nodes_recurse()
2573 ret = bcache_btree(map_nodes_recurse, k, b, in bch_btree_map_nodes_recurse()
2582 if (!b->level || flags == MAP_ALL_NODES) in bch_btree_map_nodes_recurse()
2583 ret = fn(op, b); in bch_btree_map_nodes_recurse()
2594 int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op, in bch_btree_map_keys_recurse() argument
2603 bch_btree_iter_init(&b->keys, &iter, from); in bch_btree_map_keys_recurse()
2605 while ((k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad))) { in bch_btree_map_keys_recurse()
2606 ret = !b->level in bch_btree_map_keys_recurse()
2607 ? fn(op, b, k) in bch_btree_map_keys_recurse()
2609 b, op, from, fn, flags); in bch_btree_map_keys_recurse()
2616 if (!b->level && (flags & MAP_END_KEY)) in bch_btree_map_keys_recurse()
2617 ret = fn(op, b, &KEY(KEY_INODE(&b->key), in bch_btree_map_keys_recurse()
2618 KEY_OFFSET(&b->key), 0)); in bch_btree_map_keys_recurse()
2655 static int refill_keybuf_fn(struct btree_op *op, struct btree *b, in refill_keybuf_fn() argument