Lines Matching +full:last +full:- +full:level

1 // SPDX-License-Identifier: GPL-2.0
21 * All configuration is done via sysfs; see Documentation/admin-guide/bcache.rst.
100 (((k)->ptr[0] >> c->bucket_bits) | PTR_GEN(k, 0))
104 #define insert_lock(s, b) ((b)->level <= (s)->lock)
109 return ((void *) btree_bset_first(b)) + b->written * block_bytes(b->c->cache); in write_block()
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()
134 atomic_dec_bug(&PTR_BUCKET(c, k, i)->pin); in bkey_put()
141 uint64_t crc = b->key.ptr[0]; in btree_csum_set()
144 crc = crc64_be(crc, data, end - data); in btree_csum_set()
155 * c->fill_iter can allocate an iterator with more memory space in bch_btree_node_read_done()
157 * See the comment arount cache_set->fill_iter. 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()
161 iter->used = 0; in bch_btree_node_read_done()
164 iter->b = &b->keys; in bch_btree_node_read_done()
167 if (!i->seq) 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()
174 if (i->version > BCACHE_BSET_VERSION) in bch_btree_node_read_done()
178 if (b->written + set_blocks(i, block_bytes(b->c->cache)) > in bch_btree_node_read_done()
183 if (i->magic != bset_magic(&b->c->cache->sb)) in bch_btree_node_read_done()
187 switch (i->version) { in bch_btree_node_read_done()
189 if (i->csum != csum_set(i)) 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()
202 bch_btree_iter_push(iter, i->start, bset_bkey_last(i)); in bch_btree_node_read_done()
204 b->written += set_blocks(i, block_bytes(b->c->cache)); 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()
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()
238 struct closure *cl = bio->bi_private; in btree_node_read_endio()
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()
255 bio->bi_end_io = btree_node_read_endio; in bch_btree_node_read()
256 bio->bi_private = &cl; in bch_btree_node_read()
257 bio->bi_opf = REQ_OP_READ | REQ_META; 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()
264 if (bio->bi_status) in bch_btree_node_read()
267 bch_bbio_free(bio, b->c); 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()
283 if (w->prio_blocked && in btree_complete_write()
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()
287 if (w->journal) { in btree_complete_write()
288 atomic_dec_bug(w->journal); in btree_complete_write()
289 __closure_wake_up(&b->c->journal.wait); in btree_complete_write()
292 w->prio_blocked = 0; in btree_complete_write()
293 w->journal = NULL; in btree_complete_write()
300 up(&b->io_mutex); in btree_node_write_unlock()
308 bch_bbio_free(b->bio, b->c); in __btree_node_write_done()
309 b->bio = NULL; in __btree_node_write_done()
313 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ); in __btree_node_write_done()
322 bio_free_pages(b->bio); in btree_node_write_done()
328 struct closure *cl = bio->bi_private; in btree_node_write_endio()
331 if (bio->bi_status) in btree_node_write_endio()
334 bch_bbio_count_io_errors(b->c, bio, bio->bi_status, "writing btree"); in btree_node_write_endio()
340 struct closure *cl = &b->io; in do_btree_node_write()
344 i->version = BCACHE_BSET_VERSION; 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()
357 * If we're appending to a leaf node, we don't technically need FUA - in do_btree_node_write()
361 * Similarly if we're writing a new btree root - the pointer is going to 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()
377 void *addr = (void *) ((unsigned long) i & ~(PAGE_SIZE - 1)); in do_btree_node_write()
380 bio_for_each_segment_all(bv, b->bio, iter_all) { in do_btree_node_write()
381 memcpy(page_address(bv->bv_page), addr, PAGE_SIZE); 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()
407 lockdep_assert_held(&b->write_lock); in __bch_btree_node_write()
411 BUG_ON(current->bio_list); 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()
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()
436 unsigned int nsets = b->keys.nsets; in bch_btree_node_write()
438 lockdep_assert_held(&b->lock); in bch_btree_node_write()
446 if (nsets && !b->keys.nsets) in bch_btree_node_write()
458 mutex_lock(&b->write_lock); in bch_btree_node_write_sync()
460 mutex_unlock(&b->write_lock); in bch_btree_node_write_sync()
469 mutex_lock(&b->write_lock); in btree_node_write_work()
472 mutex_unlock(&b->write_lock); in btree_node_write_work()
480 lockdep_assert_held(&b->write_lock); in bch_btree_leaf_dirty()
482 BUG_ON(!b->written); in bch_btree_leaf_dirty()
483 BUG_ON(!i->keys); in bch_btree_leaf_dirty()
486 queue_delayed_work(btree_io_wq, &b->work, 30 * HZ); in bch_btree_leaf_dirty()
491 * w->journal is always the oldest journal pin of all bkeys in bch_btree_leaf_dirty()
496 if (w->journal && in bch_btree_leaf_dirty()
497 journal_pin_cmp(b->c, w->journal, journal_ref)) { in bch_btree_leaf_dirty()
498 atomic_dec_bug(w->journal); in bch_btree_leaf_dirty()
499 w->journal = NULL; in bch_btree_leaf_dirty()
502 if (!w->journal) { in bch_btree_leaf_dirty()
503 w->journal = journal_ref; in bch_btree_leaf_dirty()
504 atomic_inc(w->journal); in bch_btree_leaf_dirty()
509 if (set_bytes(i) > PAGE_SIZE - 48 && in bch_btree_leaf_dirty()
510 !current->bio_list) in bch_btree_leaf_dirty()
515 * Btree in memory cache - allocation/freeing
516 * mca -> memory cache
519 #define mca_reserve(c) (((!IS_ERR_OR_NULL(c->root) && c->root->level) \
520 ? c->root->level : 1) * 8 + 16)
522 max_t(int, 0, c->btree_cache_used - mca_reserve(c))
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()
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()
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()
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()
592 lockdep_assert_held(&b->c->bucket_lock); in mca_reap()
594 if (!down_write_trylock(&b->lock)) in mca_reap()
595 return -ENOMEM; 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()
606 if (down_trylock(&b->io_mutex)) in mca_reap()
608 up(&b->io_mutex); in mca_reap()
615 * b->write_lock before checking BTREE_NODE_dirty bit. in mca_reap()
617 mutex_lock(&b->write_lock); in mca_reap()
625 mutex_unlock(&b->write_lock); 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()
643 return -ENOMEM; in mca_reap()
651 unsigned long i, nr = sc->nr_to_scan; in bch_mca_scan()
655 if (c->shrinker_disabled) in bch_mca_scan()
658 if (c->btree_cache_alloc_lock) in bch_mca_scan()
661 /* Return -1 if we can't do anything right now */ in bch_mca_scan()
662 if (sc->gfp_mask & __GFP_IO) in bch_mca_scan()
663 mutex_lock(&c->bucket_lock); in bch_mca_scan()
664 else if (!mutex_trylock(&c->bucket_lock)) in bch_mca_scan()
665 return -1; in bch_mca_scan()
668 * It's _really_ critical that we don't free too many btree nodes - we in bch_mca_scan()
674 nr /= c->btree_pages; in bch_mca_scan()
680 btree_cache_used = c->btree_cache_used; in bch_mca_scan()
681 list_for_each_entry_safe_reverse(b, t, &c->btree_cache_freeable, list) { in bch_mca_scan()
690 nr--; in bch_mca_scan()
694 list_for_each_entry_safe_reverse(b, t, &c->btree_cache, list) { in bch_mca_scan()
705 nr--; in bch_mca_scan()
709 mutex_unlock(&c->bucket_lock); in bch_mca_scan()
710 return freed * c->btree_pages; in bch_mca_scan()
718 if (c->shrinker_disabled) in bch_mca_count()
721 if (c->btree_cache_alloc_lock) in bch_mca_count()
724 return mca_can_free(c) * c->btree_pages; in bch_mca_count()
734 if (c->shrink.list.next) in bch_btree_cache_free()
735 unregister_shrinker(&c->shrink); in bch_btree_cache_free()
737 mutex_lock(&c->bucket_lock); in bch_btree_cache_free()
740 if (c->verify_data) in bch_btree_cache_free()
741 list_move(&c->verify_data->list, &c->btree_cache); in bch_btree_cache_free()
743 free_pages((unsigned long) c->verify_ondisk, ilog2(meta_bucket_pages(&c->cache->sb))); in bch_btree_cache_free()
746 list_splice(&c->btree_cache_freeable, in bch_btree_cache_free()
747 &c->btree_cache); in bch_btree_cache_free()
749 while (!list_empty(&c->btree_cache)) { in bch_btree_cache_free()
750 b = list_first_entry(&c->btree_cache, struct btree, list); in bch_btree_cache_free()
755 * b->write_lock before clearing BTREE_NODE_dirty anymore. in bch_btree_cache_free()
759 clear_bit(BTREE_NODE_dirty, &b->flags); in bch_btree_cache_free()
764 while (!list_empty(&c->btree_cache_freed)) { 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()
772 mutex_unlock(&c->bucket_lock); in bch_btree_cache_free()
781 return -ENOMEM; in bch_btree_cache_alloc()
783 list_splice_init(&c->btree_cache, in bch_btree_cache_alloc()
784 &c->btree_cache_freeable); in bch_btree_cache_alloc()
787 mutex_init(&c->verify_lock); in bch_btree_cache_alloc()
789 c->verify_ondisk = (void *) in bch_btree_cache_alloc()
791 ilog2(meta_bucket_pages(&c->cache->sb))); in bch_btree_cache_alloc()
792 if (!c->verify_ondisk) { in bch_btree_cache_alloc()
795 * allocated in previous for-loop, they will be in bch_btree_cache_alloc()
798 return -ENOMEM; in bch_btree_cache_alloc()
801 c->verify_data = mca_bucket_alloc(c, &ZERO_KEY, GFP_KERNEL); in bch_btree_cache_alloc()
803 if (c->verify_data && in bch_btree_cache_alloc()
804 c->verify_data->keys.set->data) in bch_btree_cache_alloc()
805 list_del_init(&c->verify_data->list); in bch_btree_cache_alloc()
807 c->verify_data = NULL; in bch_btree_cache_alloc()
810 c->shrink.count_objects = bch_mca_count; in bch_btree_cache_alloc()
811 c->shrink.scan_objects = bch_mca_scan; in bch_btree_cache_alloc()
812 c->shrink.seeks = 4; in bch_btree_cache_alloc()
813 c->shrink.batch = c->btree_pages * 2; in bch_btree_cache_alloc()
815 if (register_shrinker(&c->shrink, "md-bcache:%pU", c->set_uuid)) in bch_btree_cache_alloc()
822 /* Btree in memory cache - hash table */
826 return &c->bucket_hash[hash_32(PTR_HASH(c, k), BUCKET_HASH_BITS)]; in mca_hash()
835 if (PTR_HASH(c, &b->key) == PTR_HASH(c, k)) in mca_find()
845 spin_lock(&c->btree_cannibalize_lock); in mca_cannibalize_lock()
846 if (likely(c->btree_cache_alloc_lock == NULL)) { in mca_cannibalize_lock()
847 c->btree_cache_alloc_lock = current; in mca_cannibalize_lock()
848 } else if (c->btree_cache_alloc_lock != current) { in mca_cannibalize_lock()
850 prepare_to_wait(&c->btree_cache_wait, &op->wait, in mca_cannibalize_lock()
852 spin_unlock(&c->btree_cannibalize_lock); in mca_cannibalize_lock()
853 return -EINTR; in mca_cannibalize_lock()
855 spin_unlock(&c->btree_cannibalize_lock); in mca_cannibalize_lock()
868 return ERR_PTR(-EINTR); in mca_cannibalize()
870 list_for_each_entry_reverse(b, &c->btree_cache, list) in mca_cannibalize()
874 list_for_each_entry_reverse(b, &c->btree_cache, list) in mca_cannibalize()
879 return ERR_PTR(-ENOMEM); in mca_cannibalize()
890 spin_lock(&c->btree_cannibalize_lock); in bch_cannibalize_unlock()
891 if (c->btree_cache_alloc_lock == current) { in bch_cannibalize_unlock()
892 c->btree_cache_alloc_lock = NULL; in bch_cannibalize_unlock()
893 wake_up(&c->btree_cache_wait); in bch_cannibalize_unlock()
895 spin_unlock(&c->btree_cannibalize_lock); in bch_cannibalize_unlock()
899 struct bkey *k, int level) in mca_alloc() argument
903 BUG_ON(current->bio_list); in mca_alloc()
905 lockdep_assert_held(&c->bucket_lock); in mca_alloc()
913 list_for_each_entry(b, &c->btree_cache_freeable, list) in mca_alloc()
920 list_for_each_entry(b, &c->btree_cache_freed, list) in mca_alloc()
923 if (!b->keys.set[0].data) 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()
970 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
973 * If IO is necessary and running under submit_bio_noacct, returns -EAGAIN.
976 * level and op->lock.
979 struct bkey *k, int level, bool write, in bch_btree_node_get() argument
985 BUG_ON(level < 0); in bch_btree_node_get()
990 if (current->bio_list) in bch_btree_node_get()
991 return ERR_PTR(-EAGAIN); in bch_btree_node_get()
993 mutex_lock(&c->bucket_lock); in bch_btree_node_get()
994 b = mca_alloc(c, op, k, level); in bch_btree_node_get()
995 mutex_unlock(&c->bucket_lock); 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()
1012 BUG_ON(b->level != level); in bch_btree_node_get()
1017 return ERR_PTR(-EIO); 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()
1039 mutex_lock(&parent->c->bucket_lock); in btree_node_prefetch()
1040 b = mca_alloc(parent->c, NULL, k, parent->level - 1); in btree_node_prefetch()
1041 mutex_unlock(&parent->c->bucket_lock); in btree_node_prefetch()
1044 b->parent = parent; in btree_node_prefetch()
1056 BUG_ON(b == b->c->root); in btree_node_free()
1059 mutex_lock(&b->write_lock); in btree_node_free()
1067 mutex_unlock(&b->write_lock); 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()
1085 mutex_unlock(&b->c->bucket_lock); in btree_node_free()
1089 int level, bool wait, in __bch_btree_node_alloc() argument
1093 struct btree *b = ERR_PTR(-EAGAIN); in __bch_btree_node_alloc()
1095 mutex_lock(&c->bucket_lock); in __bch_btree_node_alloc()
1101 SET_KEY_SIZE(&k.key, c->btree_pages * PAGE_SECTORS); in __bch_btree_node_alloc()
1103 b = mca_alloc(c, op, &k.key, level); 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()
1116 mutex_unlock(&c->bucket_lock); in __bch_btree_node_alloc()
1123 mutex_unlock(&c->bucket_lock); in __bch_btree_node_alloc()
1130 struct btree_op *op, int level, in bch_btree_node_alloc() argument
1133 return __bch_btree_node_alloc(c, op, level, op != NULL, parent); in bch_btree_node_alloc()
1139 struct btree *n = bch_btree_node_alloc(b->c, op, b->level, b->parent); in btree_node_alloc_replacement()
1142 mutex_lock(&n->write_lock); 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()
1145 mutex_unlock(&n->write_lock); in btree_node_alloc_replacement()
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()
1172 struct cache_set *c = b->c; in btree_check_reserve()
1173 struct cache *ca = c->cache; in btree_check_reserve()
1174 unsigned int reserve = (c->root->level - b->level) * 2 + 1; in btree_check_reserve()
1176 mutex_lock(&c->bucket_lock); in btree_check_reserve()
1178 if (fifo_used(&ca->free[RESERVE_BTREE]) < reserve) { in btree_check_reserve()
1180 prepare_to_wait(&c->btree_cache_wait, &op->wait, in btree_check_reserve()
1182 mutex_unlock(&c->bucket_lock); in btree_check_reserve()
1183 return -EINTR; in btree_check_reserve()
1186 mutex_unlock(&c->bucket_lock); in btree_check_reserve()
1188 return mca_cannibalize_lock(b->c, op); in btree_check_reserve()
1193 static uint8_t __bch_btree_mark_key(struct cache_set *c, int level, in __bch_btree_mark_key() argument
1214 if (gen_after(g->last_gc, PTR_GEN(k, i))) in __bch_btree_mark_key()
1215 g->last_gc = PTR_GEN(k, i); in __bch_btree_mark_key()
1223 (GC_MARK(g) == GC_MARK_METADATA) != (level != 0), in __bch_btree_mark_key()
1224 c, "inconsistent ptrs: mark = %llu, level = %i", in __bch_btree_mark_key()
1225 GC_MARK(g), level); in __bch_btree_mark_key()
1227 if (level) in __bch_btree_mark_key()
1245 #define btree_mark_key(b, k) __bch_btree_mark_key(b->c, b->level, k)
1247 void bch_initial_mark_key(struct cache_set *c, int level, struct bkey *k) in bch_initial_mark_key() argument
1256 b->gen = PTR_GEN(k, i); in bch_initial_mark_key()
1258 if (level && bkey_cmp(k, &ZERO_KEY)) 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()
1264 __bch_btree_mark_key(c, level, k); in bch_initial_mark_key()
1269 stats->in_use = (c->nbuckets - c->avail_nbuckets) * 100 / c->nbuckets; in bch_update_bucket_in_use()
1280 gc->nodes++; in btree_gc_mark_node()
1282 for_each_key_filter(&b->keys, k, &iter, bch_ptr_invalid) { in btree_gc_mark_node()
1286 if (bch_ptr_bad(&b->keys, k)) in btree_gc_mark_node()
1289 gc->key_bytes += bkey_u64s(k); in btree_gc_mark_node()
1290 gc->nkeys++; in btree_gc_mark_node()
1293 gc->data += KEY_SIZE(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()
1297 btree_bug_on(t->size && 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()
1302 if (b->c->gc_always_rewrite) in btree_gc_mark_node()
1308 if ((keys - good_keys) * 2 > keys) in btree_gc_mark_node()
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()
1361 * nodes, to make sure the insert below will succeed - we also check in btree_gc_coalesce()
1369 mutex_lock(&new_nodes[i]->write_lock); in btree_gc_coalesce()
1371 for (i = nodes - 1; i > 0; --i) { in btree_gc_coalesce()
1373 struct bset *n2 = btree_bset_first(new_nodes[i - 1]); in btree_gc_coalesce()
1374 struct bkey *k, *last = NULL; in btree_gc_coalesce() local
1379 for (k = n2->start; in btree_gc_coalesce()
1382 if (__set_blocks(n1, n1->keys + keys + in btree_gc_coalesce()
1384 block_bytes(b->c->cache)) > blocks) in btree_gc_coalesce()
1387 last = k; in btree_gc_coalesce()
1392 * Last node we're not getting rid of - we're getting in btree_gc_coalesce()
1399 if (__set_blocks(n1, n1->keys + n2->keys, in btree_gc_coalesce()
1400 block_bytes(b->c->cache)) > in btree_gc_coalesce()
1404 keys = n2->keys; 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()
1412 if (last) in btree_gc_coalesce()
1413 bkey_copy_key(&new_nodes[i]->key, last); in btree_gc_coalesce()
1416 n2->start, in btree_gc_coalesce()
1417 (void *) bset_bkey_idx(n2, keys) - (void *) n2->start); in btree_gc_coalesce()
1419 n1->keys += keys; in btree_gc_coalesce()
1420 r[i].keys = n1->keys; in btree_gc_coalesce()
1422 memmove(n2->start, in btree_gc_coalesce()
1424 (void *) bset_bkey_last(n2) - in btree_gc_coalesce()
1427 n2->keys -= keys; in btree_gc_coalesce()
1430 bkey_u64s(&new_nodes[i]->key))) in btree_gc_coalesce()
1434 bch_keylist_add(&keylist, &new_nodes[i]->key); in btree_gc_coalesce()
1438 mutex_unlock(&new_nodes[i]->write_lock); in btree_gc_coalesce()
1443 BUG_ON(btree_bset_first(new_nodes[0])->keys); in btree_gc_coalesce()
1449 if (__bch_keylist_realloc(&keylist, bkey_u64s(&r[i].b->key))) in btree_gc_coalesce()
1466 memmove(r, r + 1, sizeof(r[0]) * (nodes - 1)); in btree_gc_coalesce()
1467 r[nodes - 1].b = ERR_PTR(-EINTR); in btree_gc_coalesce()
1470 gc->nodes--; in btree_gc_coalesce()
1475 return -EINTR; in btree_gc_coalesce()
1479 mutex_unlock(&new_nodes[i]->write_lock); in btree_gc_coalesce()
1486 atomic_dec(&b->c->prio_blocked); in btree_gc_coalesce()
1518 bch_keylist_add(&keys, &n->key); in btree_gc_rewrite_node()
1530 return -EINTR; in btree_gc_rewrite_node()
1539 for_each_key_filter(&b->keys, k, &iter, bch_ptr_bad) in btree_gc_count_keys()
1553 * GC would last a long time, and the front side I/Os in btree_gc_min_nodes()
1563 min_nodes = c->gc_stats.nodes / MAX_GC_TIMES; in btree_gc_min_nodes()
1579 struct gc_merge_info *i, *last = r + ARRAY_SIZE(r) - 1; in btree_gc_recurse() local
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()
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()
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()
1633 memmove(r + 1, r, sizeof(r[0]) * (GC_MERGE_NODES - 1)); 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()
1638 gc->nodes_pre = gc->nodes; in btree_gc_recurse()
1639 ret = -EAGAIN; in btree_gc_recurse()
1644 ret = -EAGAIN; 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()
1679 return -EINTR; 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()
1691 bkey_copy_key(&b->c->gc_done, &b->key); in bch_btree_gc_root()
1701 if (!c->gc_mark_valid) in btree_gc_start()
1704 mutex_lock(&c->bucket_lock); in btree_gc_start()
1706 c->gc_mark_valid = 0; in btree_gc_start()
1707 c->gc_done = ZERO_KEY; in btree_gc_start()
1709 ca = c->cache; in btree_gc_start()
1711 b->last_gc = b->gen; in btree_gc_start()
1712 if (!atomic_read(&b->pin)) { in btree_gc_start()
1718 mutex_unlock(&c->bucket_lock); in btree_gc_start()
1728 mutex_lock(&c->bucket_lock); in bch_btree_gc_finish()
1731 c->gc_mark_valid = 1; in bch_btree_gc_finish()
1732 c->need_gc = 0; in bch_btree_gc_finish()
1734 for (i = 0; i < KEY_PTRS(&c->uuid_bucket); i++) in bch_btree_gc_finish()
1735 SET_GC_MARK(PTR_BUCKET(c, &c->uuid_bucket, i), in bch_btree_gc_finish()
1740 for (i = 0; i < c->devices_max_used; i++) { in bch_btree_gc_finish()
1741 struct bcache_device *d = c->devices[i]; in bch_btree_gc_finish()
1745 if (!d || UUID_FLASH_ONLY(&c->uuids[i])) in bch_btree_gc_finish()
1749 spin_lock(&dc->writeback_keys.lock); in bch_btree_gc_finish()
1751 &dc->writeback_keys.keys, node) in bch_btree_gc_finish()
1752 for (j = 0; j < KEY_PTRS(&w->key); j++) in bch_btree_gc_finish()
1753 SET_GC_MARK(PTR_BUCKET(c, &w->key, j), in bch_btree_gc_finish()
1755 spin_unlock(&dc->writeback_keys.lock); in bch_btree_gc_finish()
1759 c->avail_nbuckets = 0; in bch_btree_gc_finish()
1761 ca = c->cache; in bch_btree_gc_finish()
1762 ca->invalidate_needs_gc = 0; in bch_btree_gc_finish()
1764 for (k = ca->sb.d; k < ca->sb.d + ca->sb.keys; k++) in bch_btree_gc_finish()
1765 SET_GC_MARK(ca->buckets + *k, GC_MARK_METADATA); in bch_btree_gc_finish()
1767 for (k = ca->prio_buckets; in bch_btree_gc_finish()
1768 k < ca->prio_buckets + prio_buckets(ca) * 2; k++) in bch_btree_gc_finish()
1769 SET_GC_MARK(ca->buckets + *k, GC_MARK_METADATA); 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()
1780 c->avail_nbuckets++; in bch_btree_gc_finish()
1783 mutex_unlock(&c->bucket_lock); in bch_btree_gc_finish()
1808 if (ret == -EAGAIN) in bch_btree_gc()
1813 } while (ret && !test_bit(CACHE_SET_IO_DISABLE, &c->flags)); in bch_btree_gc()
1818 bch_time_stats_update(&c->btree_gc_time, start_time); in bch_btree_gc()
1823 memcpy(&c->gc_stats, &stats, sizeof(struct gc_stat)); in bch_btree_gc()
1832 struct cache *ca = c->cache; in gc_should_run()
1834 if (ca->invalidate_needs_gc) in gc_should_run()
1837 if (atomic_read(&c->sectors_to_gc) < 0) in gc_should_run()
1848 wait_event_interruptible(c->gc_wait, in bch_gc_thread()
1850 test_bit(CACHE_SET_IO_DISABLE, &c->flags) || in bch_gc_thread()
1854 test_bit(CACHE_SET_IO_DISABLE, &c->flags)) in bch_gc_thread()
1867 c->gc_thread = kthread_run(bch_gc_thread, c, "bcache_gc"); in bch_gc_thread_start()
1868 return PTR_ERR_OR_ZERO(c->gc_thread); in bch_gc_thread_start()
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()
1893 * initiallize c->gc_stats.nodes in bch_btree_check_recurse()
1896 b->c->gc_stats.nodes++; in bch_btree_check_recurse()
1914 struct btree_check_state *check_state = info->state; in bch_btree_check_thread()
1915 struct cache_set *c = check_state->c; in bch_btree_check_thread()
1925 bch_btree_iter_init(&c->root->keys, &iter, NULL); in bch_btree_check_thread()
1926 k = bch_btree_iter_next_filter(&iter, &c->root->keys, bch_ptr_bad); in bch_btree_check_thread()
1934 * sub-tree indexed by the fetched key. in bch_btree_check_thread()
1936 spin_lock(&check_state->idx_lock); in bch_btree_check_thread()
1937 cur_idx = check_state->key_idx; in bch_btree_check_thread()
1938 check_state->key_idx++; in bch_btree_check_thread()
1939 spin_unlock(&check_state->idx_lock); in bch_btree_check_thread()
1941 skip_nr = cur_idx - prev_idx; in bch_btree_check_thread()
1945 &c->root->keys, in bch_btree_check_thread()
1955 atomic_set(&check_state->enough, 1); in bch_btree_check_thread()
1956 /* Update check_state->enough earlier */ in bch_btree_check_thread()
1960 skip_nr--; in bch_btree_check_thread()
1967 btree_node_prefetch(c->root, p); in bch_btree_check_thread()
1968 c->gc_stats.nodes++; in bch_btree_check_thread()
1970 ret = bcache_btree(check_recurse, p, c->root, &op); in bch_btree_check_thread()
1980 info->result = ret; in bch_btree_check_thread()
1981 /* update check_state->started among all CPUs */ in bch_btree_check_thread()
1983 if (atomic_dec_and_test(&check_state->started)) in bch_btree_check_thread()
1984 wake_up(&check_state->wait); in bch_btree_check_thread()
2012 for_each_key_filter(&c->root->keys, k, &iter, bch_ptr_invalid) in bch_btree_check()
2013 bch_initial_mark_key(c, c->root->level, k); in bch_btree_check()
2015 bch_initial_mark_key(c, c->root->level + 1, &c->root->key); in bch_btree_check()
2017 if (c->root->level == 0) in bch_btree_check()
2029 rw_lock(0, c->root, c->root->level); in bch_btree_check()
2032 * if check_state.enough is non-zero, it means current in bch_btree_check()
2051 for (--i; i >= 0; i--) in bch_btree_check()
2053 ret = -ENOMEM; in bch_btree_check()
2072 rw_unlock(0, c->root); in bch_btree_check()
2078 struct cache *ca = c->cache; in bch_initial_gc_finish()
2083 mutex_lock(&c->bucket_lock); in bch_initial_gc_finish()
2087 * order to get the allocator thread started - it needs freed buckets in in bch_initial_gc_finish()
2095 if (fifo_full(&ca->free[RESERVE_PRIO]) && in bch_initial_gc_finish()
2096 fifo_full(&ca->free[RESERVE_BTREE])) in bch_initial_gc_finish()
2102 if (!fifo_push(&ca->free[RESERVE_PRIO], in bch_initial_gc_finish()
2103 b - ca->buckets)) in bch_initial_gc_finish()
2104 fifo_push(&ca->free[RESERVE_BTREE], in bch_initial_gc_finish()
2105 b - ca->buckets); in bch_initial_gc_finish()
2109 mutex_unlock(&c->bucket_lock); in bch_initial_gc_finish()
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()
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()
2141 ret -= KEY_MAX_U64S; in insert_u64s_remaining()
2151 int oldsize = bch_count_data(&b->keys); in bch_btree_insert_keys()
2154 struct bkey *k = insert_keys->keys; 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()
2165 } else if (bkey_cmp(&START_KEY(k), &b->key) < 0) { in bch_btree_insert_keys()
2167 bkey_copy(&temp.key, insert_keys->keys); 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()
2180 op->insert_collision = true; 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()
2202 if (!b->level) in btree_split()
2203 return -EINTR; 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()
2230 mutex_lock(&n1->write_lock); in btree_split()
2231 mutex_lock(&n2->write_lock); in btree_split()
2240 while (keys < (btree_bset_first(n1)->keys * 3) / 5) in btree_split()
2244 bkey_copy_key(&n1->key, in btree_split()
2248 btree_bset_first(n2)->keys = btree_bset_first(n1)->keys - keys; in btree_split()
2249 btree_bset_first(n1)->keys = keys; in btree_split()
2251 memcpy(btree_bset_first(n2)->start, in btree_split()
2253 btree_bset_first(n2)->keys * sizeof(uint64_t)); in btree_split()
2255 bkey_copy_key(&n2->key, &b->key); in btree_split()
2257 bch_keylist_add(&parent_keys, &n2->key); in btree_split()
2259 mutex_unlock(&n2->write_lock); in btree_split()
2262 trace_bcache_btree_node_compact(b, btree_bset_first(n1)->keys); in btree_split()
2264 mutex_lock(&n1->write_lock); in btree_split()
2268 bch_keylist_add(&parent_keys, &n1->key); in btree_split()
2270 mutex_unlock(&n1->write_lock); in btree_split()
2274 mutex_lock(&n3->write_lock); in btree_split()
2275 bkey_copy_key(&n3->key, &MAX_KEY); in btree_split()
2278 mutex_unlock(&n3->write_lock); in btree_split()
2283 } else if (!b->parent) { in btree_split()
2293 bch_btree_insert_node(b->parent, op, &parent_keys, NULL, NULL); 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()
2314 if (n3 == ERR_PTR(-EAGAIN) || in btree_split()
2315 n2 == ERR_PTR(-EAGAIN) || in btree_split()
2316 n1 == ERR_PTR(-EAGAIN)) in btree_split()
2317 return -EAGAIN; in btree_split()
2319 return -ENOMEM; in btree_split()
2329 BUG_ON(b->level && replace_key); in bch_btree_insert_node()
2333 mutex_lock(&b->write_lock); in bch_btree_insert_node()
2336 b->keys.last_set_unwritten) in bch_btree_insert_node()
2340 mutex_unlock(&b->write_lock); in bch_btree_insert_node()
2347 if (!b->level) in bch_btree_insert_node()
2353 mutex_unlock(&b->write_lock); in bch_btree_insert_node()
2360 if (current->bio_list) { in bch_btree_insert_node()
2361 op->lock = b->c->root->level + 1; in bch_btree_insert_node()
2362 return -EAGAIN; 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()
2365 return -EINTR; in bch_btree_insert_node()
2373 return -EINTR; in bch_btree_insert_node()
2381 int ret = -EINTR; in bch_btree_insert_check_key()
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()
2385 bool upgrade = op->lock == -1; 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()
2401 get_random_bytes(&check_key->ptr[0], sizeof(uint64_t)); in bch_btree_insert_check_key()
2412 downgrade_write(&b->lock); in bch_btree_insert_check_key()
2428 int ret = bch_btree_insert_node(b, &op->op, op->keys, in btree_insert_fn()
2429 op->journal_ref, op->replace_key); in btree_insert_fn()
2430 if (ret && !bch_keylist_empty(op->keys)) in btree_insert_fn()
2442 BUG_ON(current->bio_list); in bch_btree_insert()
2453 &START_KEY(keys->keys), in bch_btree_insert()
2465 ret = -ESRCH; in bch_btree_insert()
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()
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()
2519 if (!b->level || flags == MAP_ALL_NODES) in bch_btree_map_nodes_recurse()
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()
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()
2570 if (bkey_cmp(&l->key, &START_KEY(&r->key)) <= 0) in keybuf_cmp()
2571 return -1; in keybuf_cmp()
2572 if (bkey_cmp(&START_KEY(&l->key), &r->key) >= 0) in keybuf_cmp()
2580 return clamp_t(int64_t, bkey_cmp(&l->key, &r->key), -1, 1); in keybuf_nonoverlapping_cmp()
2595 struct keybuf *buf = refill->buf; in refill_keybuf_fn()
2598 if (bkey_cmp(k, refill->end) > 0) { in refill_keybuf_fn()
2606 if (refill->pred(buf, k)) { in refill_keybuf_fn()
2609 spin_lock(&buf->lock); in refill_keybuf_fn()
2611 w = array_alloc(&buf->freelist); in refill_keybuf_fn()
2613 spin_unlock(&buf->lock); in refill_keybuf_fn()
2617 w->private = NULL; in refill_keybuf_fn()
2618 bkey_copy(&w->key, k); in refill_keybuf_fn()
2620 if (RB_INSERT(&buf->keys, w, node, keybuf_cmp)) in refill_keybuf_fn()
2621 array_free(&buf->freelist, w); in refill_keybuf_fn()
2623 refill->nr_found++; in refill_keybuf_fn()
2625 if (array_freelist_empty(&buf->freelist)) in refill_keybuf_fn()
2628 spin_unlock(&buf->lock); in refill_keybuf_fn()
2631 buf->last_scanned = *k; in refill_keybuf_fn()
2638 struct bkey start = buf->last_scanned; in bch_refill_keybuf()
2643 bch_btree_op_init(&refill.op, -1); in bch_refill_keybuf()
2649 bch_btree_map_keys(&refill.op, c, &buf->last_scanned, in bch_refill_keybuf()
2654 KEY_INODE(&buf->last_scanned), in bch_refill_keybuf()
2655 KEY_OFFSET(&buf->last_scanned)); in bch_refill_keybuf()
2657 spin_lock(&buf->lock); in bch_refill_keybuf()
2659 if (!RB_EMPTY_ROOT(&buf->keys)) { in bch_refill_keybuf()
2662 w = RB_FIRST(&buf->keys, struct keybuf_key, node); in bch_refill_keybuf()
2663 buf->start = START_KEY(&w->key); in bch_refill_keybuf()
2665 w = RB_LAST(&buf->keys, struct keybuf_key, node); in bch_refill_keybuf()
2666 buf->end = w->key; in bch_refill_keybuf()
2668 buf->start = MAX_KEY; in bch_refill_keybuf()
2669 buf->end = MAX_KEY; in bch_refill_keybuf()
2672 spin_unlock(&buf->lock); in bch_refill_keybuf()
2677 rb_erase(&w->node, &buf->keys); in __bch_keybuf_del()
2678 array_free(&buf->freelist, w); in __bch_keybuf_del()
2683 spin_lock(&buf->lock); in bch_keybuf_del()
2685 spin_unlock(&buf->lock); in bch_keybuf_del()
2696 if (bkey_cmp(end, &buf->start) <= 0 || in bch_keybuf_check_overlapping()
2697 bkey_cmp(start, &buf->end) >= 0) in bch_keybuf_check_overlapping()
2700 spin_lock(&buf->lock); in bch_keybuf_check_overlapping()
2701 w = RB_GREATER(&buf->keys, s, node, keybuf_nonoverlapping_cmp); in bch_keybuf_check_overlapping()
2703 while (w && bkey_cmp(&START_KEY(&w->key), end) < 0) { in bch_keybuf_check_overlapping()
2707 if (p->private) in bch_keybuf_check_overlapping()
2713 spin_unlock(&buf->lock); in bch_keybuf_check_overlapping()
2721 spin_lock(&buf->lock); in bch_keybuf_next()
2723 w = RB_FIRST(&buf->keys, struct keybuf_key, node); in bch_keybuf_next()
2725 while (w && w->private) in bch_keybuf_next()
2729 w->private = ERR_PTR(-EINTR); in bch_keybuf_next()
2731 spin_unlock(&buf->lock); in bch_keybuf_next()
2747 if (bkey_cmp(&buf->last_scanned, end) >= 0) { in bch_keybuf_next_rescan()
2760 buf->last_scanned = MAX_KEY; in bch_keybuf_init()
2761 buf->keys = RB_ROOT; in bch_keybuf_init()
2763 spin_lock_init(&buf->lock); in bch_keybuf_init()
2764 array_allocator_init(&buf->freelist); in bch_keybuf_init()
2777 return -ENOMEM; in bch_btree_init()