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 struct btree *b = kzalloc(sizeof(struct btree), gfp); in mca_bucket_alloc() local
571 if (!b) in mca_bucket_alloc()
574 init_rwsem(&b->lock); in mca_bucket_alloc()
575 lockdep_set_novalidate_class(&b->lock); in mca_bucket_alloc()
576 mutex_init(&b->write_lock); in mca_bucket_alloc()
577 lockdep_set_novalidate_class(&b->write_lock); in mca_bucket_alloc()
578 INIT_LIST_HEAD(&b->list); in mca_bucket_alloc()
579 INIT_DELAYED_WORK(&b->work, btree_node_write_work); in mca_bucket_alloc()
580 b->c = c; in mca_bucket_alloc()
581 sema_init(&b->io_mutex, 1); in mca_bucket_alloc()
583 mca_data_alloc(b, k, gfp); in mca_bucket_alloc()
584 return b; in mca_bucket_alloc()
587 static int mca_reap(struct btree *b, unsigned int min_order, bool flush) in mca_reap() argument
592 lockdep_assert_held(&b->c->bucket_lock); in mca_reap()
594 if (!down_write_trylock(&b->lock)) in mca_reap()
597 BUG_ON(btree_node_dirty(b) && !b->keys.set[0].data); in mca_reap()
599 if (b->keys.page_order < min_order) in mca_reap()
603 if (btree_node_dirty(b)) in mca_reap()
606 if (down_trylock(&b->io_mutex)) in mca_reap()
608 up(&b->io_mutex); in mca_reap()
617 mutex_lock(&b->write_lock); in mca_reap()
623 if (btree_node_journal_flush(b)) { in mca_reap()
624 pr_debug("bnode %p is flushing by journal, retry\n", b); in mca_reap()
625 mutex_unlock(&b->write_lock); in mca_reap()
630 if (btree_node_dirty(b)) in mca_reap()
631 __bch_btree_node_write(b, &cl); in mca_reap()
632 mutex_unlock(&b->write_lock); in mca_reap()
637 down(&b->io_mutex); in mca_reap()
638 up(&b->io_mutex); in mca_reap()
642 rw_unlock(true, b); in mca_reap()
650 struct btree *b, *t; in bch_mca_scan() local
681 list_for_each_entry_safe_reverse(b, t, &c->btree_cache_freeable, list) { in bch_mca_scan()
685 if (!mca_reap(b, 0, false)) { in bch_mca_scan()
686 mca_data_free(b); in bch_mca_scan()
687 rw_unlock(true, b); in bch_mca_scan()
694 list_for_each_entry_safe_reverse(b, t, &c->btree_cache, list) { in bch_mca_scan()
698 if (!mca_reap(b, 0, false)) { in bch_mca_scan()
699 mca_bucket_free(b); in bch_mca_scan()
700 mca_data_free(b); in bch_mca_scan()
701 rw_unlock(true, b); in bch_mca_scan()
729 struct btree *b; in bch_btree_cache_free() local
750 b = list_first_entry(&c->btree_cache, struct btree, list); in bch_btree_cache_free()
757 if (btree_node_dirty(b)) { in bch_btree_cache_free()
758 btree_complete_write(b, btree_current_write(b)); in bch_btree_cache_free()
759 clear_bit(BTREE_NODE_dirty, &b->flags); in bch_btree_cache_free()
761 mca_data_free(b); in bch_btree_cache_free()
765 b = list_first_entry(&c->btree_cache_freed, in bch_btree_cache_free()
767 list_del(&b->list); in bch_btree_cache_free()
768 cancel_delayed_work_sync(&b->work); in bch_btree_cache_free()
769 kfree(b); in bch_btree_cache_free()
831 struct btree *b; in mca_find() local
834 hlist_for_each_entry_rcu(b, mca_hash(c, k), hash) in mca_find()
835 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k)) in mca_find()
837 b = NULL; in mca_find()
840 return b; in mca_find()
863 struct btree *b; in mca_cannibalize() local
870 list_for_each_entry_reverse(b, &c->btree_cache, list) in mca_cannibalize()
871 if (!mca_reap(b, btree_order(k), false)) in mca_cannibalize()
872 return b; in mca_cannibalize()
874 list_for_each_entry_reverse(b, &c->btree_cache, list) in mca_cannibalize()
875 if (!mca_reap(b, btree_order(k), true)) in mca_cannibalize()
876 return b; in mca_cannibalize()
901 struct btree *b; in mca_alloc() local
913 list_for_each_entry(b, &c->btree_cache_freeable, list) in mca_alloc()
914 if (!mca_reap(b, btree_order(k), false)) in mca_alloc()
920 list_for_each_entry(b, &c->btree_cache_freed, list) in mca_alloc()
921 if (!mca_reap(b, 0, false)) { in mca_alloc()
922 mca_data_alloc(b, k, __GFP_NOWARN|GFP_NOIO); in mca_alloc()
923 if (!b->keys.set[0].data) in mca_alloc()
929 b = mca_bucket_alloc(c, k, __GFP_NOWARN|GFP_NOIO); in mca_alloc()
930 if (!b) in mca_alloc()
933 BUG_ON(!down_write_trylock(&b->lock)); in mca_alloc()
934 if (!b->keys.set->data) in mca_alloc()
937 BUG_ON(b->io_mutex.count != 1); in mca_alloc()
939 bkey_copy(&b->key, k); in mca_alloc()
940 list_move(&b->list, &c->btree_cache); in mca_alloc()
941 hlist_del_init_rcu(&b->hash); in mca_alloc()
942 hlist_add_head_rcu(&b->hash, mca_hash(c, k)); in mca_alloc()
944 lock_set_subclass(&b->lock.dep_map, level + 1, _THIS_IP_); in mca_alloc()
945 b->parent = (void *) ~0UL; in mca_alloc()
946 b->flags = 0; in mca_alloc()
947 b->written = 0; in mca_alloc()
948 b->level = level; in mca_alloc()
950 if (!b->level) in mca_alloc()
951 bch_btree_keys_init(&b->keys, &bch_extent_keys_ops, in mca_alloc()
952 &b->c->expensive_debug_checks); in mca_alloc()
954 bch_btree_keys_init(&b->keys, &bch_btree_keys_ops, in mca_alloc()
955 &b->c->expensive_debug_checks); in mca_alloc()
957 return b; in mca_alloc()
959 if (b) in mca_alloc()
960 rw_unlock(true, b); in mca_alloc()
962 b = mca_cannibalize(c, op, k); in mca_alloc()
963 if (!IS_ERR(b)) in mca_alloc()
966 return b; in mca_alloc()
983 struct btree *b; in bch_btree_node_get() local
987 b = mca_find(c, k); in bch_btree_node_get()
989 if (!b) { in bch_btree_node_get()
994 b = mca_alloc(c, op, k, level); in bch_btree_node_get()
997 if (!b) in bch_btree_node_get()
999 if (IS_ERR(b)) in bch_btree_node_get()
1000 return b; in bch_btree_node_get()
1002 bch_btree_node_read(b); in bch_btree_node_get()
1005 downgrade_write(&b->lock); in bch_btree_node_get()
1007 rw_lock(write, b, level); in bch_btree_node_get()
1008 if (PTR_HASH(c, &b->key) != PTR_HASH(c, k)) { in bch_btree_node_get()
1009 rw_unlock(write, b); in bch_btree_node_get()
1012 BUG_ON(b->level != level); in bch_btree_node_get()
1015 if (btree_node_io_error(b)) { in bch_btree_node_get()
1016 rw_unlock(write, b); in bch_btree_node_get()
1020 BUG_ON(!b->written); in bch_btree_node_get()
1022 b->parent = parent; in bch_btree_node_get()
1024 for (; i <= b->keys.nsets && b->keys.set[i].size; i++) { in bch_btree_node_get()
1025 prefetch(b->keys.set[i].tree); in bch_btree_node_get()
1026 prefetch(b->keys.set[i].data); in bch_btree_node_get()
1029 for (; i <= b->keys.nsets; i++) in bch_btree_node_get()
1030 prefetch(b->keys.set[i].data); in bch_btree_node_get()
1032 return b; in bch_btree_node_get()
1037 struct btree *b; in btree_node_prefetch() local
1040 b = mca_alloc(parent->c, NULL, k, parent->level - 1); in btree_node_prefetch()
1043 if (!IS_ERR_OR_NULL(b)) { in btree_node_prefetch()
1044 b->parent = parent; in btree_node_prefetch()
1045 bch_btree_node_read(b); in btree_node_prefetch()
1046 rw_unlock(true, b); in btree_node_prefetch()
1052 static void btree_node_free(struct btree *b) in btree_node_free() argument
1054 trace_bcache_btree_node_free(b); in btree_node_free()
1056 BUG_ON(b == b->c->root); in btree_node_free()
1059 mutex_lock(&b->write_lock); in btree_node_free()
1066 if (btree_node_journal_flush(b)) { in btree_node_free()
1067 mutex_unlock(&b->write_lock); in btree_node_free()
1068 pr_debug("bnode %p journal_flush set, retry\n", b); in btree_node_free()
1073 if (btree_node_dirty(b)) { in btree_node_free()
1074 btree_complete_write(b, btree_current_write(b)); in btree_node_free()
1075 clear_bit(BTREE_NODE_dirty, &b->flags); in btree_node_free()
1078 mutex_unlock(&b->write_lock); in btree_node_free()
1080 cancel_delayed_work(&b->work); in btree_node_free()
1082 mutex_lock(&b->c->bucket_lock); in btree_node_free()
1083 bch_bucket_free(b->c, &b->key); in btree_node_free()
1084 mca_bucket_free(b); in btree_node_free()
1085 mutex_unlock(&b->c->bucket_lock); in btree_node_free()
1093 struct btree *b = ERR_PTR(-EAGAIN); in __bch_btree_node_alloc() local
1103 b = mca_alloc(c, op, &k.key, level); in __bch_btree_node_alloc()
1104 if (IS_ERR(b)) in __bch_btree_node_alloc()
1107 if (!b) { in __bch_btree_node_alloc()
1113 b->parent = parent; in __bch_btree_node_alloc()
1114 bch_bset_init_next(&b->keys, b->keys.set->data, bset_magic(&b->c->cache->sb)); in __bch_btree_node_alloc()
1118 trace_bcache_btree_node_alloc(b); in __bch_btree_node_alloc()
1119 return b; in __bch_btree_node_alloc()
1126 return b; in __bch_btree_node_alloc()
1136 static struct btree *btree_node_alloc_replacement(struct btree *b, in btree_node_alloc_replacement() argument
1139 struct btree *n = bch_btree_node_alloc(b->c, op, b->level, b->parent); in btree_node_alloc_replacement()
1143 bch_btree_sort_into(&b->keys, &n->keys, &b->c->sort); in btree_node_alloc_replacement()
1144 bkey_copy_key(&n->key, &b->key); in btree_node_alloc_replacement()
1151 static void make_btree_freeing_key(struct btree *b, struct bkey *k) in make_btree_freeing_key() argument
1155 mutex_lock(&b->c->bucket_lock); in make_btree_freeing_key()
1157 atomic_inc(&b->c->prio_blocked); in make_btree_freeing_key()
1159 bkey_copy(k, &b->key); in make_btree_freeing_key()
1164 bch_inc_gen(b->c->cache, in make_btree_freeing_key()
1165 PTR_BUCKET(b->c, &b->key, i))); in make_btree_freeing_key()
1167 mutex_unlock(&b->c->bucket_lock); in make_btree_freeing_key()
1170 static int btree_check_reserve(struct btree *b, struct btree_op *op) in btree_check_reserve() argument
1172 struct cache_set *c = b->c; in btree_check_reserve()
1174 unsigned int reserve = (c->root->level - b->level) * 2 + 1; in btree_check_reserve()
1188 return mca_cannibalize_lock(b->c, op); in btree_check_reserve()
1245 #define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k) argument
1254 struct bucket *b = PTR_BUCKET(c, k, i); in bch_initial_mark_key() local
1256 b->gen = PTR_GEN(k, i); in bch_initial_mark_key()
1259 b->prio = BTREE_PRIO; in bch_initial_mark_key()
1260 else if (!level && b->prio == BTREE_PRIO) in bch_initial_mark_key()
1261 b->prio = INITIAL_PRIO; in bch_initial_mark_key()
1272 static bool btree_gc_mark_node(struct btree *b, struct gc_stat *gc) in btree_gc_mark_node() argument
1282 for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) { in btree_gc_mark_node()
1283 stale = max(stale, btree_mark_key(b, k)); in btree_gc_mark_node()
1286 if (bch_ptr_bad(&b->keys, k)) in btree_gc_mark_node()
1296 for (t = b->keys.set; t <= &b->keys.set[b->keys.nsets]; t++) in btree_gc_mark_node()
1298 bset_written(&b->keys, t) && in btree_gc_mark_node()
1299 bkey_cmp(&b->key, &t->end) < 0, in btree_gc_mark_node()
1300 b, "found short btree key in gc"); in btree_gc_mark_node()
1302 if (b->c->gc_always_rewrite) in btree_gc_mark_node()
1317 struct btree *b; member
1321 static int bch_btree_insert_node(struct btree *b, struct btree_op *op,
1326 static int btree_gc_coalesce(struct btree *b, struct btree_op *op, in btree_gc_coalesce() argument
1337 if (btree_check_reserve(b, NULL)) in btree_gc_coalesce()
1343 while (nodes < GC_MERGE_NODES && !IS_ERR_OR_NULL(r[nodes].b)) in btree_gc_coalesce()
1346 blocks = btree_default_blocks(b->c) * 2 / 3; in btree_gc_coalesce()
1349 __set_blocks(b->keys.set[0].data, keys, in btree_gc_coalesce()
1350 block_bytes(b->c->cache)) > blocks * (nodes - 1)) in btree_gc_coalesce()
1354 new_nodes[i] = btree_node_alloc_replacement(r[i].b, NULL); in btree_gc_coalesce()
1365 if (btree_check_reserve(b, NULL)) in btree_gc_coalesce()
1384 block_bytes(b->c->cache)) > blocks) in btree_gc_coalesce()
1400 block_bytes(b->c->cache)) > in btree_gc_coalesce()
1406 last = &r->b->key; in btree_gc_coalesce()
1409 BUG_ON(__set_blocks(n1, n1->keys + keys, block_bytes(b->c->cache)) > in btree_gc_coalesce()
1449 if (__bch_keylist_realloc(&keylist, bkey_u64s(&r[i].b->key))) in btree_gc_coalesce()
1452 make_btree_freeing_key(r[i].b, keylist.top); in btree_gc_coalesce()
1456 bch_btree_insert_node(b, op, &keylist, NULL, NULL); in btree_gc_coalesce()
1460 btree_node_free(r[i].b); in btree_gc_coalesce()
1461 rw_unlock(true, r[i].b); in btree_gc_coalesce()
1463 r[i].b = new_nodes[i]; in btree_gc_coalesce()
1467 r[nodes - 1].b = ERR_PTR(-EINTR); in btree_gc_coalesce()
1486 atomic_dec(&b->c->prio_blocked); in btree_gc_coalesce()
1497 static int btree_gc_rewrite_node(struct btree *b, struct btree_op *op, in btree_gc_rewrite_node() argument
1503 if (btree_check_reserve(b, NULL)) in btree_gc_rewrite_node()
1509 if (btree_check_reserve(b, NULL)) { in btree_gc_rewrite_node()
1523 bch_btree_insert_node(b, op, &keys, NULL, NULL); in btree_gc_rewrite_node()
1533 static unsigned int btree_gc_count_keys(struct btree *b) in btree_gc_count_keys() argument
1539 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad) in btree_gc_count_keys()
1571 static int btree_gc_recurse(struct btree *b, struct btree_op *op, in btree_gc_recurse() argument
1581 bch_btree_iter_init(&b->keys, &iter, &b->c->gc_done); in btree_gc_recurse()
1584 i->b = ERR_PTR(-EINTR); in btree_gc_recurse()
1587 k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad); in btree_gc_recurse()
1589 r->b = bch_btree_node_get(b->c, op, k, b->level - 1, in btree_gc_recurse()
1590 true, b); in btree_gc_recurse()
1591 if (IS_ERR(r->b)) { in btree_gc_recurse()
1592 ret = PTR_ERR(r->b); in btree_gc_recurse()
1596 r->keys = btree_gc_count_keys(r->b); in btree_gc_recurse()
1598 ret = btree_gc_coalesce(b, op, gc, r); in btree_gc_recurse()
1603 if (!last->b) in btree_gc_recurse()
1606 if (!IS_ERR(last->b)) { in btree_gc_recurse()
1607 should_rewrite = btree_gc_mark_node(last->b, gc); in btree_gc_recurse()
1609 ret = btree_gc_rewrite_node(b, op, last->b); in btree_gc_recurse()
1614 if (last->b->level) { in btree_gc_recurse()
1615 ret = btree_gc_recurse(last->b, op, writes, gc); in btree_gc_recurse()
1620 bkey_copy_key(&b->c->gc_done, &last->b->key); in btree_gc_recurse()
1626 mutex_lock(&last->b->write_lock); in btree_gc_recurse()
1627 if (btree_node_dirty(last->b)) in btree_gc_recurse()
1628 bch_btree_node_write(last->b, writes); in btree_gc_recurse()
1629 mutex_unlock(&last->b->write_lock); in btree_gc_recurse()
1630 rw_unlock(true, last->b); in btree_gc_recurse()
1634 r->b = NULL; in btree_gc_recurse()
1636 if (atomic_read(&b->c->search_inflight) && in btree_gc_recurse()
1637 gc->nodes >= gc->nodes_pre + btree_gc_min_nodes(b->c)) { in btree_gc_recurse()
1650 if (!IS_ERR_OR_NULL(i->b)) { in btree_gc_recurse()
1651 mutex_lock(&i->b->write_lock); in btree_gc_recurse()
1652 if (btree_node_dirty(i->b)) in btree_gc_recurse()
1653 bch_btree_node_write(i->b, writes); in btree_gc_recurse()
1654 mutex_unlock(&i->b->write_lock); in btree_gc_recurse()
1655 rw_unlock(true, i->b); in btree_gc_recurse()
1661 static int bch_btree_gc_root(struct btree *b, struct btree_op *op, in bch_btree_gc_root() argument
1668 should_rewrite = btree_gc_mark_node(b, gc); in bch_btree_gc_root()
1670 n = btree_node_alloc_replacement(b, NULL); in bch_btree_gc_root()
1676 btree_node_free(b); in bch_btree_gc_root()
1683 __bch_btree_mark_key(b->c, b->level + 1, &b->key); in bch_btree_gc_root()
1685 if (b->level) { in bch_btree_gc_root()
1686 ret = btree_gc_recurse(b, op, writes, gc); in bch_btree_gc_root()
1691 bkey_copy_key(&b->c->gc_done, &b->key); in bch_btree_gc_root()
1699 struct bucket *b; in btree_gc_start() local
1710 for_each_bucket(b, ca) { in btree_gc_start()
1711 b->last_gc = b->gen; in btree_gc_start()
1712 if (!atomic_read(&b->pin)) { in btree_gc_start()
1713 SET_GC_MARK(b, 0); in btree_gc_start()
1714 SET_GC_SECTORS_USED(b, 0); in btree_gc_start()
1723 struct bucket *b; in bch_btree_gc_finish() local
1771 for_each_bucket(b, ca) { in bch_btree_gc_finish()
1772 c->need_gc = max(c->need_gc, bucket_gc_gen(b)); in bch_btree_gc_finish()
1774 if (atomic_read(&b->pin)) in bch_btree_gc_finish()
1777 BUG_ON(!GC_MARK(b) && GC_SECTORS_USED(b)); in bch_btree_gc_finish()
1779 if (!GC_MARK(b) || GC_MARK(b) == GC_MARK_RECLAIMABLE) in bch_btree_gc_finish()
1873 static int bch_btree_check_recurse(struct btree *b, struct btree_op *op) in bch_btree_check_recurse() argument
1879 for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) in bch_btree_check_recurse()
1880 bch_initial_mark_key(b->c, b->level, k); in bch_btree_check_recurse()
1882 bch_initial_mark_key(b->c, b->level + 1, &b->key); in bch_btree_check_recurse()
1884 if (b->level) { in bch_btree_check_recurse()
1885 bch_btree_iter_init(&b->keys, &iter, NULL); in bch_btree_check_recurse()
1888 k = bch_btree_iter_next_filter(&iter, &b->keys, in bch_btree_check_recurse()
1891 btree_node_prefetch(b, k); in bch_btree_check_recurse()
1896 b->c->gc_stats.nodes++; in bch_btree_check_recurse()
1900 ret = bcache_btree(check_recurse, p, b, op); in bch_btree_check_recurse()
2079 struct bucket *b; in bch_initial_gc_finish() local
2094 for_each_bucket(b, ca) { in bch_initial_gc_finish()
2099 if (bch_can_invalidate_bucket(ca, b) && in bch_initial_gc_finish()
2100 !GC_MARK(b)) { in bch_initial_gc_finish()
2101 __bch_invalidate_one_bucket(ca, b); in bch_initial_gc_finish()
2103 b - ca->buckets)) in bch_initial_gc_finish()
2105 b - ca->buckets); in bch_initial_gc_finish()
2114 static bool btree_insert_key(struct btree *b, struct bkey *k, in btree_insert_key() argument
2119 BUG_ON(bkey_cmp(k, &b->key) > 0); in btree_insert_key()
2121 status = bch_btree_insert_key(&b->keys, k, replace_key); in btree_insert_key()
2123 bch_check_keys(&b->keys, "%u for %s", status, in btree_insert_key()
2126 trace_bcache_btree_insert_key(b, k, replace_key != NULL, in btree_insert_key()
2133 static size_t insert_u64s_remaining(struct btree *b) in insert_u64s_remaining() argument
2135 long ret = bch_btree_keys_u64s_remaining(&b->keys); in insert_u64s_remaining()
2140 if (b->keys.ops->is_extents) in insert_u64s_remaining()
2146 static bool bch_btree_insert_keys(struct btree *b, struct btree_op *op, in bch_btree_insert_keys() argument
2151 int oldsize = bch_count_data(&b->keys); in bch_btree_insert_keys()
2156 if (bkey_u64s(k) > insert_u64s_remaining(b)) in bch_btree_insert_keys()
2159 if (bkey_cmp(k, &b->key) <= 0) { in bch_btree_insert_keys()
2160 if (!b->level) in bch_btree_insert_keys()
2161 bkey_put(b->c, k); in bch_btree_insert_keys()
2163 ret |= btree_insert_key(b, k, replace_key); in bch_btree_insert_keys()
2165 } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) { in bch_btree_insert_keys()
2169 bch_cut_back(&b->key, &temp.key); in bch_btree_insert_keys()
2170 bch_cut_front(&b->key, insert_keys->keys); in bch_btree_insert_keys()
2172 ret |= btree_insert_key(b, &temp.key, replace_key); in bch_btree_insert_keys()
2182 BUG_ON(!bch_keylist_empty(insert_keys) && b->level); in bch_btree_insert_keys()
2184 BUG_ON(bch_count_data(&b->keys) < oldsize); in bch_btree_insert_keys()
2188 static int btree_split(struct btree *b, struct btree_op *op, in btree_split() argument
2201 if (btree_check_reserve(b, op)) { in btree_split()
2202 if (!b->level) in btree_split()
2208 n1 = btree_node_alloc_replacement(b, op); in btree_split()
2213 block_bytes(n1->c->cache)) > (btree_blocks(b) * 4) / 5; in btree_split()
2218 trace_bcache_btree_node_split(b, btree_bset_first(n1)->keys); in btree_split()
2220 n2 = bch_btree_node_alloc(b->c, op, b->level, b->parent); in btree_split()
2224 if (!b->parent) { in btree_split()
2225 n3 = bch_btree_node_alloc(b->c, op, b->level + 1, NULL); in btree_split()
2255 bkey_copy_key(&n2->key, &b->key); in btree_split()
2262 trace_bcache_btree_node_compact(b, btree_bset_first(n1)->keys); in btree_split()
2283 } else if (!b->parent) { in btree_split()
2290 make_btree_freeing_key(b, parent_keys.top); in btree_split()
2293 bch_btree_insert_node(b->parent, op, &parent_keys, NULL, NULL); in btree_split()
2297 btree_node_free(b); in btree_split()
2300 bch_time_stats_update(&b->c->btree_split_time, start_time); in btree_split()
2304 bkey_put(b->c, &n2->key); in btree_split()
2308 bkey_put(b->c, &n1->key); in btree_split()
2312 WARN(1, "bcache: btree split failed (level %u)", b->level); in btree_split()
2322 static int bch_btree_insert_node(struct btree *b, struct btree_op *op, in bch_btree_insert_node() argument
2329 BUG_ON(b->level && replace_key); in bch_btree_insert_node()
2333 mutex_lock(&b->write_lock); in bch_btree_insert_node()
2335 if (write_block(b) != btree_bset_last(b) && in bch_btree_insert_node()
2336 b->keys.last_set_unwritten) in bch_btree_insert_node()
2337 bch_btree_init_next(b); /* just wrote a set */ in bch_btree_insert_node()
2339 if (bch_keylist_nkeys(insert_keys) > insert_u64s_remaining(b)) { in bch_btree_insert_node()
2340 mutex_unlock(&b->write_lock); in bch_btree_insert_node()
2344 BUG_ON(write_block(b) != btree_bset_last(b)); in bch_btree_insert_node()
2346 if (bch_btree_insert_keys(b, op, insert_keys, replace_key)) { in bch_btree_insert_node()
2347 if (!b->level) in bch_btree_insert_node()
2348 bch_btree_leaf_dirty(b, journal_ref); in bch_btree_insert_node()
2350 bch_btree_node_write(b, &cl); in bch_btree_insert_node()
2353 mutex_unlock(&b->write_lock); in bch_btree_insert_node()
2361 op->lock = b->c->root->level + 1; in bch_btree_insert_node()
2363 } else if (op->lock <= b->c->root->level) { in bch_btree_insert_node()
2364 op->lock = b->c->root->level + 1; in bch_btree_insert_node()
2368 int ret = btree_split(b, op, insert_keys, replace_key); in bch_btree_insert_node()
2378 int bch_btree_insert_check_key(struct btree *b, struct btree_op *op, in bch_btree_insert_check_key() argument
2382 uint64_t btree_ptr = b->key.ptr[0]; in bch_btree_insert_check_key()
2383 unsigned long seq = b->seq; in bch_btree_insert_check_key()
2390 rw_unlock(false, b); in bch_btree_insert_check_key()
2391 rw_lock(true, b, b->level); in bch_btree_insert_check_key()
2393 if (b->key.ptr[0] != btree_ptr || in bch_btree_insert_check_key()
2394 b->seq != seq + 1) { in bch_btree_insert_check_key()
2395 op->lock = b->level; in bch_btree_insert_check_key()
2407 ret = bch_btree_insert_node(b, op, &insert, NULL, NULL); in bch_btree_insert_check_key()
2412 downgrade_write(&b->lock); in bch_btree_insert_check_key()
2423 static int btree_insert_fn(struct btree_op *b_op, struct btree *b) in btree_insert_fn() argument
2428 int ret = bch_btree_insert_node(b, &op->op, op->keys, in btree_insert_fn()
2470 void bch_btree_set_root(struct btree *b) in bch_btree_set_root() argument
2477 trace_bcache_btree_set_root(b); in bch_btree_set_root()
2479 BUG_ON(!b->written); in bch_btree_set_root()
2481 for (i = 0; i < KEY_PTRS(&b->key); i++) in bch_btree_set_root()
2482 BUG_ON(PTR_BUCKET(b->c, &b->key, i)->prio != BTREE_PRIO); in bch_btree_set_root()
2484 mutex_lock(&b->c->bucket_lock); in bch_btree_set_root()
2485 list_del_init(&b->list); in bch_btree_set_root()
2486 mutex_unlock(&b->c->bucket_lock); in bch_btree_set_root()
2488 b->c->root = b; in bch_btree_set_root()
2490 bch_journal_meta(b->c, &cl); in bch_btree_set_root()
2496 static int bch_btree_map_nodes_recurse(struct btree *b, struct btree_op *op, in bch_btree_map_nodes_recurse() argument
2502 if (b->level) { in bch_btree_map_nodes_recurse()
2506 bch_btree_iter_init(&b->keys, &iter, from); in bch_btree_map_nodes_recurse()
2508 while ((k = bch_btree_iter_next_filter(&iter, &b->keys, in bch_btree_map_nodes_recurse()
2510 ret = bcache_btree(map_nodes_recurse, k, b, in bch_btree_map_nodes_recurse()
2519 if (!b->level || flags == MAP_ALL_NODES) in bch_btree_map_nodes_recurse()
2520 ret = fn(op, b); in bch_btree_map_nodes_recurse()
2531 int bch_btree_map_keys_recurse(struct btree *b, struct btree_op *op, in bch_btree_map_keys_recurse() argument
2539 bch_btree_iter_init(&b->keys, &iter, from); in bch_btree_map_keys_recurse()
2541 while ((k = bch_btree_iter_next_filter(&iter, &b->keys, bch_ptr_bad))) { in bch_btree_map_keys_recurse()
2542 ret = !b->level in bch_btree_map_keys_recurse()
2543 ? fn(op, b, k) in bch_btree_map_keys_recurse()
2545 b, op, from, fn, flags); in bch_btree_map_keys_recurse()
2552 if (!b->level && (flags & MAP_END_KEY)) in bch_btree_map_keys_recurse()
2553 ret = fn(op, b, &KEY(KEY_INODE(&b->key), in bch_btree_map_keys_recurse()
2554 KEY_OFFSET(&b->key), 0)); in bch_btree_map_keys_recurse()
2591 static int refill_keybuf_fn(struct btree_op *op, struct btree *b, in refill_keybuf_fn() argument