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 = mempool_alloc(&b->c->fill_iter, GFP_NOIO); in bch_btree_node_read_done()
160 iter->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, &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 struct btree *b = container_of(cl, struct btree, io); in btree_node_write_unlock() local
300 up(&b->io_mutex); in btree_node_write_unlock()
305 struct btree *b = container_of(cl, struct btree, io); in __btree_node_write_done() local
306 struct btree_write *w = btree_prev_write(b); in __btree_node_write_done()
308 bch_bbio_free(b->bio, b->c); in __btree_node_write_done()
309 b->bio = NULL; in __btree_node_write_done()
310 btree_complete_write(b, w); in __btree_node_write_done()
312 if (btree_node_dirty(b)) in __btree_node_write_done()
313 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ); in __btree_node_write_done()
320 struct btree *b = container_of(cl, struct btree, io); in btree_node_write_done() local
322 bio_free_pages(b->bio); in btree_node_write_done()
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()
852 struct btree *b; in mca_find() local
855 hlist_for_each_entry_rcu(b, mca_hash(c, k), hash) in mca_find()
856 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k)) in mca_find()
858 b = NULL; in mca_find()
861 return b; in mca_find()
884 struct btree *b; in mca_cannibalize() local
891 list_for_each_entry_reverse(b, &c->btree_cache, list) in mca_cannibalize()
892 if (!mca_reap(b, btree_order(k), false)) in mca_cannibalize()
893 return b; in mca_cannibalize()
895 list_for_each_entry_reverse(b, &c->btree_cache, list) in mca_cannibalize()
896 if (!mca_reap(b, btree_order(k), true)) in mca_cannibalize()
897 return b; in mca_cannibalize()
922 struct btree *b; in mca_alloc() local
934 list_for_each_entry(b, &c->btree_cache_freeable, list) in mca_alloc()
935 if (!mca_reap(b, btree_order(k), false)) in mca_alloc()
941 list_for_each_entry(b, &c->btree_cache_freed, list) in mca_alloc()
942 if (!mca_reap(b, 0, false)) { in mca_alloc()
943 mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO); in mca_alloc()
944 if (!b->keys.set[0].data) in mca_alloc()
950 b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO); in mca_alloc()
951 if (!b) in mca_alloc()
954 BUG_ON(!down_write_trylock(&b->lock)); in mca_alloc()
955 if (!b->keys.set->data) in mca_alloc()
958 BUG_ON(b->io_mutex.count != 1); in mca_alloc()
960 bkey_copy(&b->key, k); in mca_alloc()
961 list_move(&b->list, &c->btree_cache); in mca_alloc()
962 hlist_del_init_rcu(&b->hash); in mca_alloc()
963 hlist_add_head_rcu(&b->hash, mca_hash(c, k)); in mca_alloc()
965 lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_); in mca_alloc()
966 b->parent = (void *) ~0UL; in mca_alloc()
967 b->flags = 0; in mca_alloc()
968 b->written = 0; in mca_alloc()
969 b->level = level; in mca_alloc()
971 if (!b->level) in mca_alloc()
972 bch_btree_keys_init(&b->keys, &bch_extent_keys_ops, in mca_alloc()
973 &b->c->expensive_debug_checks); in mca_alloc()
975 bch_btree_keys_init(&b->keys, &bch_btree_keys_ops, in mca_alloc()
976 &b->c->expensive_debug_checks); in mca_alloc()
978 return b; in mca_alloc()
980 if (b) in mca_alloc()
981 rw_unlock(true, b); in mca_alloc()
983 b = mca_cannibalize(c, op, k); in mca_alloc()
984 if (!IS_ERR(b)) in mca_alloc()
987 return b; in mca_alloc()
1007 struct btree *b; in bch_btree_node_get() local
1011 b = mca_find(c, k); in bch_btree_node_get()
1013 if (!b) { in bch_btree_node_get()
1018 b = mca_alloc(c, op, k, level); in bch_btree_node_get()
1021 if (!b) in bch_btree_node_get()
1023 if (IS_ERR(b)) in bch_btree_node_get()
1024 return b; in bch_btree_node_get()
1026 bch_btree_node_read(b); in bch_btree_node_get()
1029 downgrade_write(&b->lock); in bch_btree_node_get()
1031 rw_lock(write, b, level); in bch_btree_node_get()
1032 if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) { in bch_btree_node_get()
1033 rw_unlock(write, b); in bch_btree_node_get()
1036 BUG_ON(b->level != level); in bch_btree_node_get()
1039 if (btree_node_io_error(b)) { in bch_btree_node_get()
1040 rw_unlock(write, b); in bch_btree_node_get()
1044 BUG_ON(!b->written); in bch_btree_node_get()
1046 b->parent = parent; in bch_btree_node_get()
1048 for (; i <= b->keys.nsets && b->keys.set[i].size; i++) { in bch_btree_node_get()
1049 prefetch(b->keys.set[i].tree); in bch_btree_node_get()
1050 prefetch(b->keys.set[i].data); in bch_btree_node_get()
1053 for (; i <= b->keys.nsets; i++) in bch_btree_node_get()
1054 prefetch(b->keys.set[i].data); in bch_btree_node_get()
1056 return b; in bch_btree_node_get()
1061 struct btree *b; in btree_node_prefetch() local
1064 b = mca_alloc(parent->c, NULL, k, parent->level - 1); in btree_node_prefetch()
1067 if (!IS_ERR_OR_NULL(b)) { in btree_node_prefetch()
1068 b->parent = parent; in btree_node_prefetch()
1069 bch_btree_node_read(b); in btree_node_prefetch()
1070 rw_unlock(true, b); in btree_node_prefetch()
1076 static void btree_node_free(struct btree *b) in btree_node_free() argument
1078 trace_bcache_btree_node_free(b); in btree_node_free()
1080 BUG_ON(b == b->c->root); in btree_node_free()
1083 mutex_lock(&b->write_lock); in btree_node_free()
1090 if (btree_node_journal_flush(b)) { in btree_node_free()
1091 mutex_unlock(&b->write_lock); in btree_node_free()
1092 pr_debug("bnode %p journal_flush set, retry\n", b); in btree_node_free()
1097 if (btree_node_dirty(b)) { in btree_node_free()
1098 btree_complete_write(b, btree_current_write(b)); in btree_node_free()
1099 clear_bit(BTREE_NODE_dirty, &b->flags); in btree_node_free()
1102 mutex_unlock(&b->write_lock); in btree_node_free()
1104 cancel_delayed_work(&b->work); in btree_node_free()
1106 mutex_lock(&b->c->bucket_lock); in btree_node_free()
1107 bch_bucket_free(b->c, &b->key); in btree_node_free()
1108 mca_bucket_free(b); in btree_node_free()
1109 mutex_unlock(&b->c->bucket_lock); in btree_node_free()
1121 struct btree *b; in __bch_btree_node_alloc() local
1126 b = ERR_PTR(-EAGAIN); in __bch_btree_node_alloc()
1133 b = mca_alloc(c, op, &k.key, level); in __bch_btree_node_alloc()
1134 if (IS_ERR(b)) in __bch_btree_node_alloc()
1137 if (!b) { in __bch_btree_node_alloc()
1143 b->parent = parent; in __bch_btree_node_alloc()
1144 bch_bset_init_next(&b->keys, b->keys.set->data, bset_magic(&b->c->cache->sb)); in __bch_btree_node_alloc()
1148 trace_bcache_btree_node_alloc(b); in __bch_btree_node_alloc()
1149 return b; in __bch_btree_node_alloc()
1156 return b; in __bch_btree_node_alloc()
1166 static struct btree *btree_node_alloc_replacement(struct btree *b, in btree_node_alloc_replacement() argument
1169 struct btree *n = bch_btree_node_alloc(b->c, op, b->level, b->parent); in btree_node_alloc_replacement()
1173 bch_btree_sort_into(&b->keys, &n->keys, &b->c->sort); in btree_node_alloc_replacement()
1174 bkey_copy_key(&n->key, &b->key); in btree_node_alloc_replacement()
1181 static void make_btree_freeing_key(struct btree *b, struct bkey *k) in make_btree_freeing_key() argument
1185 mutex_lock(&b->c->bucket_lock); in make_btree_freeing_key()
1187 atomic_inc(&b->c->prio_blocked); in make_btree_freeing_key()
1189 bkey_copy(k, &b->key); in make_btree_freeing_key()
1194 bch_inc_gen(b->c->cache, in make_btree_freeing_key()
1195 PTR_BUCKET(b->c, &b->key, i))); in make_btree_freeing_key()
1197 mutex_unlock(&b->c->bucket_lock); in make_btree_freeing_key()
1200 static int btree_check_reserve(struct btree *b, struct btree_op *op) in btree_check_reserve() argument
1202 struct cache_set *c = b->c; in btree_check_reserve()
1204 unsigned int reserve = (c->root->level - b->level) * 2 + 1; in btree_check_reserve()
1218 return mca_cannibalize_lock(b->c, op); in btree_check_reserve()
1275 #define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k) argument
1284 struct bucket *b = PTR_BUCKET(c, k, i); in bch_initial_mark_key() local
1286 b->gen = PTR_GEN(k, i); in bch_initial_mark_key()
1289 b->prio = BTREE_PRIO; in bch_initial_mark_key()
1290 else if (!level && b->prio == BTREE_PRIO) in bch_initial_mark_key()
1291 b->prio = INITIAL_PRIO; in bch_initial_mark_key()
1302 static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc) in btree_gc_mark_node() argument
1312 for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) { in btree_gc_mark_node()
1313 stale = max(stale, btree_mark_key(b, k)); in btree_gc_mark_node()
1316 if (bch_ptr_bad(&b->keys, k)) in btree_gc_mark_node()
1326 for (t = b->keys.set; t <= &b->keys.set[b->keys.nsets]; t++) in btree_gc_mark_node()
1328 bset_written(&b->keys, t) && in btree_gc_mark_node()
1329 bkey_cmp(&b->key, &t->end) < 0, in btree_gc_mark_node()
1330 b, "found short btree key in gc"); in btree_gc_mark_node()
1332 if (b->c->gc_always_rewrite) in btree_gc_mark_node()
1347 struct btree *b; member
1351 static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
1356 static int btree_gc_coalesce(struct btree *b, struct btree_op *op, in btree_gc_coalesce() argument
1367 if (btree_check_reserve(b, NULL)) in btree_gc_coalesce()
1373 while (nodes < GC_MERGE_NODES && !IS_ERR_OR_NULL(r[nodes].b)) in btree_gc_coalesce()
1376 blocks = btree_default_blocks(b->c) * 2 / 3; in btree_gc_coalesce()
1379 __set_blocks(b->keys.set[0].data, keys, in btree_gc_coalesce()
1380 block_bytes(b->c->cache)) > blocks * (nodes - 1)) in btree_gc_coalesce()
1384 new_nodes[i] = btree_node_alloc_replacement(r[i].b, NULL); in btree_gc_coalesce()
1395 if (btree_check_reserve(b, NULL)) in btree_gc_coalesce()
1414 block_bytes(b->c->cache)) > blocks) in btree_gc_coalesce()
1430 block_bytes(b->c->cache)) > in btree_gc_coalesce()
1436 last = &r->b->key; in btree_gc_coalesce()
1439 BUG_ON(__set_blocks(n1, n1->keys + keys, block_bytes(b->c->cache)) > in btree_gc_coalesce()
1479 if (__bch_keylist_realloc(&keylist, bkey_u64s(&r[i].b->key))) in btree_gc_coalesce()
1482 make_btree_freeing_key(r[i].b, keylist.top); in btree_gc_coalesce()
1486 bch_btree_insert_node(b, op, &keylist, NULL, NULL); in btree_gc_coalesce()
1490 btree_node_free(r[i].b); in btree_gc_coalesce()
1491 rw_unlock(true, r[i].b); in btree_gc_coalesce()
1493 r[i].b = new_nodes[i]; in btree_gc_coalesce()
1497 r[nodes - 1].b = ERR_PTR(-EINTR); in btree_gc_coalesce()
1516 atomic_dec(&b->c->prio_blocked); in btree_gc_coalesce()
1527 static int btree_gc_rewrite_node(struct btree *b, struct btree_op *op, in btree_gc_rewrite_node() argument
1533 if (btree_check_reserve(b, NULL)) in btree_gc_rewrite_node()
1541 if (btree_check_reserve(b, NULL)) { in btree_gc_rewrite_node()
1555 bch_btree_insert_node(b, op, &keys, NULL, NULL); in btree_gc_rewrite_node()
1565 static unsigned int btree_gc_count_keys(struct btree *b) in btree_gc_count_keys() argument
1571 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad) in btree_gc_count_keys()
1603 static int btree_gc_recurse(struct btree *b, struct btree_op *op, in btree_gc_recurse() argument
1613 bch_btree_iter_init(&b->keys, &iter, &b->c->gc_done); in btree_gc_recurse()
1616 i->b = ERR_PTR(-EINTR); in btree_gc_recurse()
1619 k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad); in btree_gc_recurse()
1621 r->b = bch_btree_node_get(b->c, op, k, b->level - 1, in btree_gc_recurse()
1622 true, b); in btree_gc_recurse()
1623 if (IS_ERR(r->b)) { in btree_gc_recurse()
1624 ret = PTR_ERR(r->b); in btree_gc_recurse()
1628 r->keys = btree_gc_count_keys(r->b); in btree_gc_recurse()
1630 ret = btree_gc_coalesce(b, op, gc, r); in btree_gc_recurse()
1635 if (!last->b) in btree_gc_recurse()
1638 if (!IS_ERR(last->b)) { in btree_gc_recurse()
1639 should_rewrite = btree_gc_mark_node(last->b, gc); in btree_gc_recurse()
1641 ret = btree_gc_rewrite_node(b, op, last->b); in btree_gc_recurse()
1646 if (last->b->level) { in btree_gc_recurse()
1647 ret = btree_gc_recurse(last->b, op, writes, gc); in btree_gc_recurse()
1652 bkey_copy_key(&b->c->gc_done, &last->b->key); in btree_gc_recurse()
1658 mutex_lock(&last->b->write_lock); in btree_gc_recurse()
1659 if (btree_node_dirty(last->b)) in btree_gc_recurse()
1660 bch_btree_node_write(last->b, writes); in btree_gc_recurse()
1661 mutex_unlock(&last->b->write_lock); in btree_gc_recurse()
1662 rw_unlock(true, last->b); in btree_gc_recurse()
1666 r->b = NULL; in btree_gc_recurse()
1668 if (atomic_read(&b->c->search_inflight) && in btree_gc_recurse()
1669 gc->nodes >= gc->nodes_pre + btree_gc_min_nodes(b->c)) { in btree_gc_recurse()
1682 if (!IS_ERR_OR_NULL(i->b)) { in btree_gc_recurse()
1683 mutex_lock(&i->b->write_lock); in btree_gc_recurse()
1684 if (btree_node_dirty(i->b)) in btree_gc_recurse()
1685 bch_btree_node_write(i->b, writes); in btree_gc_recurse()
1686 mutex_unlock(&i->b->write_lock); in btree_gc_recurse()
1687 rw_unlock(true, i->b); in btree_gc_recurse()
1693 static int bch_btree_gc_root(struct btree *b, struct btree_op *op, in bch_btree_gc_root() argument
1700 should_rewrite = btree_gc_mark_node(b, gc); in bch_btree_gc_root()
1702 n = btree_node_alloc_replacement(b, NULL); in bch_btree_gc_root()
1708 btree_node_free(b); in bch_btree_gc_root()
1715 __bch_btree_mark_key(b->c, b->level + 1, &b->key); in bch_btree_gc_root()
1717 if (b->level) { in bch_btree_gc_root()
1718 ret = btree_gc_recurse(b, op, writes, gc); in bch_btree_gc_root()
1723 bkey_copy_key(&b->c->gc_done, &b->key); in bch_btree_gc_root()
1731 struct bucket *b; in btree_gc_start() local
1742 for_each_bucket(b, ca) { in btree_gc_start()
1743 b->last_gc = b->gen; in btree_gc_start()
1744 if (!atomic_read(&b->pin)) { in btree_gc_start()
1745 SET_GC_MARK(b, 0); in btree_gc_start()
1746 SET_GC_SECTORS_USED(b, 0); in btree_gc_start()
1755 struct bucket *b; in bch_btree_gc_finish() local
1803 for_each_bucket(b, ca) { in bch_btree_gc_finish()
1804 c->need_gc = max(c->need_gc, bucket_gc_gen(b)); in bch_btree_gc_finish()
1806 if (atomic_read(&b->pin)) in bch_btree_gc_finish()
1809 BUG_ON(!GC_MARK(b) && GC_SECTORS_USED(b)); in bch_btree_gc_finish()
1811 if (!GC_MARK(b) || GC_MARK(b) == GC_MARK_RECLAIMABLE) in bch_btree_gc_finish()
1905 static int bch_btree_check_recurse(struct btree *b, struct btree_op *op) in bch_btree_check_recurse() argument
1911 for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) in bch_btree_check_recurse()
1912 bch_initial_mark_key(b->c, b->level, k); in bch_btree_check_recurse()
1914 bch_initial_mark_key(b->c, b->level + 1, &b->key); in bch_btree_check_recurse()
1916 if (b->level) { in bch_btree_check_recurse()
1917 bch_btree_iter_init(&b->keys, &iter, NULL); in bch_btree_check_recurse()
1920 k = bch_btree_iter_next_filter(&iter, &b->keys, in bch_btree_check_recurse()
1923 btree_node_prefetch(b, k); in bch_btree_check_recurse()
1928 b->c->gc_stats.nodes++; in bch_btree_check_recurse()
1932 ret = bcache_btree(check_recurse, p, b, op); in bch_btree_check_recurse()
2120 struct bucket *b; in bch_initial_gc_finish() local
2135 for_each_bucket(b, ca) { in bch_initial_gc_finish()
2140 if (bch_can_invalidate_bucket(ca, b) && in bch_initial_gc_finish()
2141 !GC_MARK(b)) { in bch_initial_gc_finish()
2142 __bch_invalidate_one_bucket(ca, b); in bch_initial_gc_finish()
2144 b - ca->buckets)) in bch_initial_gc_finish()
2146 b - ca->buckets); in bch_initial_gc_finish()
2155 static bool btree_insert_key(struct btree *b, struct bkey *k, in btree_insert_key() argument
2160 BUG_ON(bkey_cmp(k, &b->key) > 0); in btree_insert_key()
2162 status = bch_btree_insert_key(&b->keys, k, replace_key); in btree_insert_key()
2164 bch_check_keys(&b->keys, "%u for %s", status, in btree_insert_key()
2167 trace_bcache_btree_insert_key(b, k, replace_key != NULL, in btree_insert_key()
2174 static size_t insert_u64s_remaining(struct btree *b) in insert_u64s_remaining() argument
2176 long ret = bch_btree_keys_u64s_remaining(&b->keys); in insert_u64s_remaining()
2181 if (b->keys.ops->is_extents) in insert_u64s_remaining()
2187 static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op, in bch_btree_insert_keys() argument
2192 int oldsize = bch_count_data(&b->keys); in bch_btree_insert_keys()
2197 if (bkey_u64s(k) > insert_u64s_remaining(b)) in bch_btree_insert_keys()
2200 if (bkey_cmp(k, &b->key) <= 0) { in bch_btree_insert_keys()
2201 if (!b->level) in bch_btree_insert_keys()
2202 bkey_put(b->c, k); in bch_btree_insert_keys()
2204 ret |= btree_insert_key(b, k, replace_key); in bch_btree_insert_keys()
2206 } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) { in bch_btree_insert_keys()
2210 bch_cut_back(&b->key, &temp.key); in bch_btree_insert_keys()
2211 bch_cut_front(&b->key, insert_keys->keys); in bch_btree_insert_keys()
2213 ret |= btree_insert_key(b, &temp.key, replace_key); in bch_btree_insert_keys()
2223 BUG_ON(!bch_keylist_empty(insert_keys) && b->level); in bch_btree_insert_keys()
2225 BUG_ON(bch_count_data(&b->keys) < oldsize); in bch_btree_insert_keys()
2229 static int btree_split(struct btree *b, struct btree_op *op, in btree_split() argument
2242 if (btree_check_reserve(b, op)) { in btree_split()
2243 if (!b->level) in btree_split()
2249 n1 = btree_node_alloc_replacement(b, op); in btree_split()
2254 block_bytes(n1->c->cache)) > (btree_blocks(b) * 4) / 5; in btree_split()
2259 trace_bcache_btree_node_split(b, btree_bset_first(n1)->keys); in btree_split()
2261 n2 = bch_btree_node_alloc(b->c, op, b->level, b->parent); in btree_split()
2265 if (!b->parent) { in btree_split()
2266 n3 = bch_btree_node_alloc(b->c, op, b->level + 1, NULL); in btree_split()
2296 bkey_copy_key(&n2->key, &b->key); in btree_split()
2303 trace_bcache_btree_node_compact(b, btree_bset_first(n1)->keys); in btree_split()
2324 } else if (!b->parent) { in btree_split()
2331 make_btree_freeing_key(b, parent_keys.top); in btree_split()
2334 bch_btree_insert_node(b->parent, op, &parent_keys, NULL, NULL); in btree_split()
2338 btree_node_free(b); in btree_split()
2341 bch_time_stats_update(&b->c->btree_split_time, start_time); in btree_split()
2345 bkey_put(b->c, &n2->key); in btree_split()
2349 bkey_put(b->c, &n1->key); in btree_split()
2353 WARN(1, "bcache: btree split failed (level %u)", b->level); in btree_split()
2363 static int bch_btree_insert_node(struct btree *b, struct btree_op *op, in bch_btree_insert_node() argument
2370 BUG_ON(b->level && replace_key); in bch_btree_insert_node()
2374 mutex_lock(&b->write_lock); in bch_btree_insert_node()
2376 if (write_block(b) != btree_bset_last(b) && in bch_btree_insert_node()
2377 b->keys.last_set_unwritten) in bch_btree_insert_node()
2378 bch_btree_init_next(b); /* just wrote a set */ in bch_btree_insert_node()
2380 if (bch_keylist_nkeys(insert_keys) > insert_u64s_remaining(b)) { in bch_btree_insert_node()
2381 mutex_unlock(&b->write_lock); in bch_btree_insert_node()
2385 BUG_ON(write_block(b) != btree_bset_last(b)); in bch_btree_insert_node()
2387 if (bch_btree_insert_keys(b, op, insert_keys, replace_key)) { in bch_btree_insert_node()
2388 if (!b->level) in bch_btree_insert_node()
2389 bch_btree_leaf_dirty(b, journal_ref); in bch_btree_insert_node()
2391 bch_btree_node_write(b, &cl); in bch_btree_insert_node()
2394 mutex_unlock(&b->write_lock); in bch_btree_insert_node()
2402 op->lock = b->c->root->level + 1; in bch_btree_insert_node()
2404 } else if (op->lock <= b->c->root->level) { in bch_btree_insert_node()
2405 op->lock = b->c->root->level + 1; in bch_btree_insert_node()
2409 int ret = btree_split(b, op, insert_keys, replace_key); in bch_btree_insert_node()
2419 int bch_btree_insert_check_key(struct btree *b, struct btree_op *op, in bch_btree_insert_check_key() argument
2423 uint64_t btree_ptr = b->key.ptr[0]; in bch_btree_insert_check_key()
2424 unsigned long seq = b->seq; in bch_btree_insert_check_key()
2431 rw_unlock(false, b); in bch_btree_insert_check_key()
2432 rw_lock(true, b, b->level); in bch_btree_insert_check_key()
2434 if (b->key.ptr[0] != btree_ptr || in bch_btree_insert_check_key()
2435 b->seq != seq + 1) { in bch_btree_insert_check_key()
2436 op->lock = b->level; in bch_btree_insert_check_key()
2448 ret = bch_btree_insert_node(b, op, &insert, NULL, NULL); in bch_btree_insert_check_key()
2453 downgrade_write(&b->lock); in bch_btree_insert_check_key()
2464 static int btree_insert_fn(struct btree_op *b_op, struct btree *b) in btree_insert_fn() argument
2469 int ret = bch_btree_insert_node(b, &op->op, op->keys, in btree_insert_fn()
2511 void bch_btree_set_root(struct btree *b) in bch_btree_set_root() argument
2518 trace_bcache_btree_set_root(b); in bch_btree_set_root()
2520 BUG_ON(!b->written); in bch_btree_set_root()
2522 for (i = 0; i < KEY_PTRS(&b->key); i++) in bch_btree_set_root()
2523 BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO); in bch_btree_set_root()
2525 mutex_lock(&b->c->bucket_lock); in bch_btree_set_root()
2526 list_del_init(&b->list); in bch_btree_set_root()
2527 mutex_unlock(&b->c->bucket_lock); in bch_btree_set_root()
2529 b->c->root = b; in bch_btree_set_root()
2531 bch_journal_meta(b->c, &cl); in bch_btree_set_root()
2537 static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op, in bch_btree_map_nodes_recurse() argument
2543 if (b->level) { in bch_btree_map_nodes_recurse()
2547 bch_btree_iter_init(&b->keys, &iter, from); in bch_btree_map_nodes_recurse()
2549 while ((k = bch_btree_iter_next_filter(&iter, &b->keys, in bch_btree_map_nodes_recurse()
2551 ret = bcache_btree(map_nodes_recurse, k, b, in bch_btree_map_nodes_recurse()
2560 if (!b->level || flags == MAP_ALL_NODES) in bch_btree_map_nodes_recurse()
2561 ret = fn(op, b); in bch_btree_map_nodes_recurse()
2572 int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op, in bch_btree_map_keys_recurse() argument
2580 bch_btree_iter_init(&b->keys, &iter, from); in bch_btree_map_keys_recurse()
2582 while ((k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad))) { in bch_btree_map_keys_recurse()
2583 ret = !b->level in bch_btree_map_keys_recurse()
2584 ? fn(op, b, k) in bch_btree_map_keys_recurse()
2586 b, op, from, fn, flags); in bch_btree_map_keys_recurse()
2593 if (!b->level && (flags & MAP_END_KEY)) in bch_btree_map_keys_recurse()
2594 ret = fn(op, b, &KEY(KEY_INODE(&b->key), in bch_btree_map_keys_recurse()
2595 KEY_OFFSET(&b->key), 0)); in bch_btree_map_keys_recurse()
2632 static int refill_keybuf_fn(struct btree_op *op, struct btree *b, in refill_keybuf_fn() argument