2 * Copyright (C) 2010 Kent Overstreet <kent.overstreet@gmail.com>
3 * Copyright (C) 2014 Datera Inc.
8 #include "bkey_methods.h"
9 #include "btree_locking.h"
10 #include "btree_update_interior.h"
23 #include <linux/slab.h>
24 #include <linux/bitops.h>
25 #include <linux/freezer.h>
26 #include <linux/kthread.h>
27 #include <linux/preempt.h>
28 #include <linux/rcupdate.h>
29 #include <trace/events/bcachefs.h>
39 static void btree_node_range_checks_init(struct range_checks *r, unsigned depth)
43 for (i = 0; i < BTREE_MAX_DEPTH; i++)
44 r->l[i].min = r->l[i].max = POS_MIN;
48 static void btree_node_range_checks(struct bch_fs *c, struct btree *b,
49 struct range_checks *r)
51 struct range_level *l = &r->l[b->level];
53 struct bpos expected_min = bkey_cmp(l->min, l->max)
54 ? btree_type_successor(b->btree_id, l->max)
57 bch2_fs_inconsistent_on(bkey_cmp(b->data->min_key, expected_min), c,
58 "btree node has incorrect min key: %llu:%llu != %llu:%llu",
59 b->data->min_key.inode,
60 b->data->min_key.offset,
64 l->max = b->data->max_key;
66 if (b->level > r->depth) {
67 l = &r->l[b->level - 1];
69 bch2_fs_inconsistent_on(bkey_cmp(b->data->min_key, l->min), c,
70 "btree node min doesn't match min of child nodes: %llu:%llu != %llu:%llu",
71 b->data->min_key.inode,
72 b->data->min_key.offset,
76 bch2_fs_inconsistent_on(bkey_cmp(b->data->max_key, l->max), c,
77 "btree node max doesn't match max of child nodes: %llu:%llu != %llu:%llu",
78 b->data->max_key.inode,
79 b->data->max_key.offset,
83 if (bkey_cmp(b->data->max_key, POS_MAX))
85 btree_type_successor(b->btree_id,
90 u8 bch2_btree_key_recalc_oldest_gen(struct bch_fs *c, struct bkey_s_c k)
92 const struct bch_extent_ptr *ptr;
95 if (bkey_extent_is_data(k.k)) {
96 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
98 extent_for_each_ptr(e, ptr) {
99 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
100 size_t b = PTR_BUCKET_NR(ca, ptr);
102 if (gen_after(ca->oldest_gens[b], ptr->gen))
103 ca->oldest_gens[b] = ptr->gen;
105 max_stale = max(max_stale, ptr_stale(ca, ptr));
113 * For runtime mark and sweep:
115 static u8 bch2_gc_mark_key(struct bch_fs *c, enum bkey_type type,
116 struct bkey_s_c k, unsigned flags)
118 struct gc_pos pos = { 0 };
119 struct bch_fs_usage *stats;
123 stats = this_cpu_ptr(c->usage_percpu);
125 case BKEY_TYPE_BTREE:
126 bch2_mark_key(c, k, c->opts.btree_node_size, true, pos, stats,
128 BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE|
129 BCH_BUCKET_MARK_GC_LOCK_HELD);
131 case BKEY_TYPE_EXTENTS:
132 bch2_mark_key(c, k, k.k->size, false, pos, stats,
134 BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE|
135 BCH_BUCKET_MARK_GC_LOCK_HELD);
136 ret = bch2_btree_key_recalc_oldest_gen(c, k);
146 int bch2_btree_mark_key_initial(struct bch_fs *c, enum bkey_type type,
149 enum bch_data_type data_type = type == BKEY_TYPE_BTREE
150 ? BCH_DATA_BTREE : BCH_DATA_USER;
151 struct bch_devs_list devs = bch2_bkey_devs(k);
154 if (test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) ||
155 fsck_err_on(!bch2_sb_has_replicas(c, data_type, devs), c,
156 "superblock not marked as containing replicas (type %u)",
158 ret = bch2_check_mark_super(c, data_type, devs);
165 case BCH_EXTENT_CACHED: {
166 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
167 const struct bch_extent_ptr *ptr;
169 extent_for_each_ptr(e, ptr) {
170 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
171 size_t b = PTR_BUCKET_NR(ca, ptr);
172 struct bucket *g = PTR_BUCKET(ca, ptr);
174 if (mustfix_fsck_err_on(!g->mark.gen_valid, c,
175 "found ptr with missing gen in alloc btree,\n"
177 bch2_data_types[data_type],
179 g->_mark.gen = ptr->gen;
180 g->_mark.gen_valid = 1;
181 set_bit(b, ca->buckets_dirty);
184 if (mustfix_fsck_err_on(gen_cmp(ptr->gen, g->mark.gen) > 0, c,
185 "%s ptr gen in the future: %u > %u",
186 bch2_data_types[data_type],
187 ptr->gen, g->mark.gen)) {
188 g->_mark.gen = ptr->gen;
189 g->_mark.gen_valid = 1;
190 set_bit(b, ca->buckets_dirty);
191 set_bit(BCH_FS_FIXED_GENS, &c->flags);
199 atomic64_set(&c->key_version,
200 max_t(u64, k.k->version.lo,
201 atomic64_read(&c->key_version)));
203 bch2_gc_mark_key(c, type, k, BCH_BUCKET_MARK_NOATOMIC);
208 static unsigned btree_gc_mark_node(struct bch_fs *c, struct btree *b)
210 enum bkey_type type = btree_node_type(b);
211 struct btree_node_iter iter;
212 struct bkey unpacked;
216 if (btree_node_has_ptrs(b))
217 for_each_btree_node_key_unpack(b, k, &iter,
218 btree_node_is_extents(b),
220 bch2_bkey_debugcheck(c, b, k);
221 stale = max(stale, bch2_gc_mark_key(c, type, k, 0));
227 static inline void __gc_pos_set(struct bch_fs *c, struct gc_pos new_pos)
229 write_seqcount_begin(&c->gc_pos_lock);
231 write_seqcount_end(&c->gc_pos_lock);
234 static inline void gc_pos_set(struct bch_fs *c, struct gc_pos new_pos)
236 BUG_ON(gc_pos_cmp(new_pos, c->gc_pos) <= 0);
237 __gc_pos_set(c, new_pos);
240 static int bch2_gc_btree(struct bch_fs *c, enum btree_id btree_id)
242 struct btree_iter iter;
244 struct range_checks r;
245 unsigned depth = btree_id == BTREE_ID_EXTENTS ? 0 : 1;
250 * if expensive_debug_checks is on, run range_checks on all leaf nodes:
252 if (expensive_debug_checks(c))
255 btree_node_range_checks_init(&r, depth);
257 __for_each_btree_node(&iter, c, btree_id, POS_MIN,
258 0, depth, BTREE_ITER_PREFETCH, b) {
259 btree_node_range_checks(c, b, &r);
261 bch2_verify_btree_nr_keys(b);
263 max_stale = btree_gc_mark_node(c, b);
265 gc_pos_set(c, gc_pos_btree_node(b));
268 bch2_btree_node_rewrite(c, &iter,
270 BTREE_INSERT_USE_RESERVE|
271 BTREE_INSERT_GC_LOCK_HELD);
272 else if (!btree_gc_rewrite_disabled(c) &&
273 (btree_gc_always_rewrite(c) || max_stale > 16))
274 bch2_btree_node_rewrite(c, &iter,
277 BTREE_INSERT_GC_LOCK_HELD);
279 bch2_btree_iter_cond_resched(&iter);
281 ret = bch2_btree_iter_unlock(&iter);
285 mutex_lock(&c->btree_root_lock);
287 b = c->btree_roots[btree_id].b;
288 if (!btree_node_fake(b))
289 bch2_gc_mark_key(c, BKEY_TYPE_BTREE, bkey_i_to_s_c(&b->key), 0);
290 gc_pos_set(c, gc_pos_btree_root(b->btree_id));
292 mutex_unlock(&c->btree_root_lock);
296 static void mark_metadata_sectors(struct bch_fs *c, struct bch_dev *ca,
298 enum bch_data_type type,
301 u64 b = sector_to_bucket(ca, start);
305 min_t(u64, bucket_to_sector(ca, b + 1), end) - start;
307 bch2_mark_metadata_bucket(c, ca, b, type, sectors,
308 gc_phase(GC_PHASE_SB), flags);
311 } while (start < end);
314 void bch2_mark_dev_superblock(struct bch_fs *c, struct bch_dev *ca,
317 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
321 lockdep_assert_held(&c->sb_lock);
323 for (i = 0; i < layout->nr_superblocks; i++) {
324 u64 offset = le64_to_cpu(layout->sb_offset[i]);
326 if (offset == BCH_SB_SECTOR)
327 mark_metadata_sectors(c, ca, 0, BCH_SB_SECTOR,
330 mark_metadata_sectors(c, ca, offset,
331 offset + (1 << layout->sb_max_size_bits),
335 spin_lock(&c->journal.lock);
337 for (i = 0; i < ca->journal.nr; i++) {
338 b = ca->journal.buckets[i];
339 bch2_mark_metadata_bucket(c, ca, b, BCH_DATA_JOURNAL,
341 gc_phase(GC_PHASE_SB), flags);
344 spin_unlock(&c->journal.lock);
347 static void bch2_mark_superblocks(struct bch_fs *c)
352 mutex_lock(&c->sb_lock);
353 gc_pos_set(c, gc_phase(GC_PHASE_SB));
355 for_each_online_member(ca, c, i)
356 bch2_mark_dev_superblock(c, ca,
357 BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE|
358 BCH_BUCKET_MARK_GC_LOCK_HELD);
359 mutex_unlock(&c->sb_lock);
362 /* Also see bch2_pending_btree_node_free_insert_done() */
363 static void bch2_mark_pending_btree_node_frees(struct bch_fs *c)
365 struct gc_pos pos = { 0 };
366 struct bch_fs_usage stats = { 0 };
367 struct btree_update *as;
368 struct pending_btree_node_free *d;
370 mutex_lock(&c->btree_interior_update_lock);
371 gc_pos_set(c, gc_phase(GC_PHASE_PENDING_DELETE));
373 for_each_pending_btree_node_free(c, as, d)
374 if (d->index_update_done)
375 bch2_mark_key(c, bkey_i_to_s_c(&d->key),
376 c->opts.btree_node_size, true, pos,
378 BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE|
379 BCH_BUCKET_MARK_GC_LOCK_HELD);
381 * Don't apply stats - pending deletes aren't tracked in
385 mutex_unlock(&c->btree_interior_update_lock);
388 static void bch2_mark_allocator_buckets(struct bch_fs *c)
391 struct open_bucket *ob;
395 spin_lock(&c->freelist_lock);
396 gc_pos_set(c, gc_pos_alloc(c, NULL));
398 for_each_member_device(ca, c, ci) {
399 fifo_for_each_entry(i, &ca->free_inc, iter)
400 bch2_mark_alloc_bucket(c, ca, i, true,
401 gc_pos_alloc(c, NULL),
402 BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE|
403 BCH_BUCKET_MARK_GC_LOCK_HELD);
407 for (j = 0; j < RESERVE_NR; j++)
408 fifo_for_each_entry(i, &ca->free[j], iter)
409 bch2_mark_alloc_bucket(c, ca, i, true,
410 gc_pos_alloc(c, NULL),
411 BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE|
412 BCH_BUCKET_MARK_GC_LOCK_HELD);
415 spin_unlock(&c->freelist_lock);
417 for (ob = c->open_buckets;
418 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
420 spin_lock(&ob->lock);
422 gc_pos_set(c, gc_pos_alloc(c, ob));
423 ca = bch_dev_bkey_exists(c, ob->ptr.dev);
424 bch2_mark_alloc_bucket(c, ca, PTR_BUCKET_NR(ca, &ob->ptr), true,
426 BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE|
427 BCH_BUCKET_MARK_GC_LOCK_HELD);
429 spin_unlock(&ob->lock);
433 static void bch2_gc_start(struct bch_fs *c)
436 struct bucket_array *buckets;
437 struct bucket_mark new;
442 lg_global_lock(&c->usage_lock);
445 * Indicates to buckets code that gc is now in progress - done under
446 * usage_lock to avoid racing with bch2_mark_key():
448 __gc_pos_set(c, GC_POS_MIN);
450 /* Save a copy of the existing bucket stats while we recompute them: */
451 for_each_member_device(ca, c, i) {
452 ca->usage_cached = __bch2_dev_usage_read(ca);
453 for_each_possible_cpu(cpu) {
454 struct bch_dev_usage *p =
455 per_cpu_ptr(ca->usage_percpu, cpu);
456 memset(p, 0, sizeof(*p));
460 c->usage_cached = __bch2_fs_usage_read(c);
461 for_each_possible_cpu(cpu) {
462 struct bch_fs_usage *p =
463 per_cpu_ptr(c->usage_percpu, cpu);
465 memset(p->s, 0, sizeof(p->s));
468 lg_global_unlock(&c->usage_lock);
470 /* Clear bucket marks: */
471 for_each_member_device(ca, c, i) {
472 down_read(&ca->bucket_lock);
473 buckets = bucket_array(ca);
475 for (b = buckets->first_bucket; b < buckets->nbuckets; b++) {
476 bucket_cmpxchg(buckets->b + b, new, ({
477 new.owned_by_allocator = 0;
479 new.cached_sectors = 0;
480 new.dirty_sectors = 0;
482 ca->oldest_gens[b] = new.gen;
484 up_read(&ca->bucket_lock);
489 * bch_gc - recompute bucket marks and oldest_gen, rewrite btree nodes
491 void bch2_gc(struct bch_fs *c)
494 u64 start_time = local_clock();
498 * Walk _all_ references to buckets, and recompute them:
500 * Order matters here:
501 * - Concurrent GC relies on the fact that we have a total ordering for
502 * everything that GC walks - see gc_will_visit_node(),
503 * gc_will_visit_root()
505 * - also, references move around in the course of index updates and
506 * various other crap: everything needs to agree on the ordering
507 * references are allowed to move around in - e.g., we're allowed to
508 * start with a reference owned by an open_bucket (the allocator) and
509 * move it to the btree, but not the reverse.
511 * This is necessary to ensure that gc doesn't miss references that
512 * move around - if references move backwards in the ordering GC
513 * uses, GC could skip past them
518 * Do this before taking gc_lock - bch2_disk_reservation_get() blocks on
519 * gc_lock if sectors_available goes to 0:
521 bch2_recalc_sectors_available(c);
523 down_write(&c->gc_lock);
524 if (test_bit(BCH_FS_GC_FAILURE, &c->flags))
530 while (c->gc_pos.phase < (int) BTREE_ID_NR) {
531 int ret = c->btree_roots[c->gc_pos.phase].b
532 ? bch2_gc_btree(c, (int) c->gc_pos.phase)
536 bch_err(c, "btree gc failed: %d", ret);
537 set_bit(BCH_FS_GC_FAILURE, &c->flags);
541 gc_pos_set(c, gc_phase(c->gc_pos.phase + 1));
544 bch2_mark_superblocks(c);
545 bch2_mark_pending_btree_node_frees(c);
546 bch2_mark_allocator_buckets(c);
548 for_each_member_device(ca, c, i)
549 atomic_long_set(&ca->saturated_count, 0);
551 /* Indicates that gc is no longer in progress: */
552 gc_pos_set(c, gc_phase(GC_PHASE_DONE));
555 up_write(&c->gc_lock);
557 bch2_time_stats_update(&c->btree_gc_time, start_time);
560 * Wake up allocator in case it was waiting for buckets
561 * because of not being able to inc gens
563 for_each_member_device(ca, c, i)
564 bch2_wake_allocator(ca);
567 * At startup, allocations can happen directly instead of via the
568 * allocator thread - issue wakeup in case they blocked on gc_lock:
570 closure_wake_up(&c->freelist_wait);
573 /* Btree coalescing */
575 static void recalc_packed_keys(struct btree *b)
577 struct bkey_packed *k;
579 memset(&b->nr, 0, sizeof(b->nr));
581 BUG_ON(b->nsets != 1);
583 for (k = btree_bkey_first(b, b->set);
584 k != btree_bkey_last(b, b->set);
586 btree_keys_account_key_add(&b->nr, 0, k);
589 static void bch2_coalesce_nodes(struct bch_fs *c, struct btree_iter *iter,
590 struct btree *old_nodes[GC_MERGE_NODES])
592 struct btree *parent = btree_node_parent(iter, old_nodes[0]);
593 unsigned i, nr_old_nodes, nr_new_nodes, u64s = 0;
594 unsigned blocks = btree_blocks(c) * 2 / 3;
595 struct btree *new_nodes[GC_MERGE_NODES];
596 struct btree_update *as;
597 struct keylist keylist;
598 struct bkey_format_state format_state;
599 struct bkey_format new_format;
601 memset(new_nodes, 0, sizeof(new_nodes));
602 bch2_keylist_init(&keylist, NULL);
604 /* Count keys that are not deleted */
605 for (i = 0; i < GC_MERGE_NODES && old_nodes[i]; i++)
606 u64s += old_nodes[i]->nr.live_u64s;
608 nr_old_nodes = nr_new_nodes = i;
610 /* Check if all keys in @old_nodes could fit in one fewer node */
611 if (nr_old_nodes <= 1 ||
612 __vstruct_blocks(struct btree_node, c->block_bits,
613 DIV_ROUND_UP(u64s, nr_old_nodes - 1)) > blocks)
616 /* Find a format that all keys in @old_nodes can pack into */
617 bch2_bkey_format_init(&format_state);
619 for (i = 0; i < nr_old_nodes; i++)
620 __bch2_btree_calc_format(&format_state, old_nodes[i]);
622 new_format = bch2_bkey_format_done(&format_state);
624 /* Check if repacking would make any nodes too big to fit */
625 for (i = 0; i < nr_old_nodes; i++)
626 if (!bch2_btree_node_format_fits(c, old_nodes[i], &new_format)) {
627 trace_btree_gc_coalesce_fail(c,
628 BTREE_GC_COALESCE_FAIL_FORMAT_FITS);
632 if (bch2_keylist_realloc(&keylist, NULL, 0,
633 (BKEY_U64s + BKEY_EXTENT_U64s_MAX) * nr_old_nodes)) {
634 trace_btree_gc_coalesce_fail(c,
635 BTREE_GC_COALESCE_FAIL_KEYLIST_REALLOC);
639 as = bch2_btree_update_start(c, iter->btree_id,
640 btree_update_reserve_required(c, parent) + nr_old_nodes,
642 BTREE_INSERT_USE_RESERVE,
645 trace_btree_gc_coalesce_fail(c,
646 BTREE_GC_COALESCE_FAIL_RESERVE_GET);
647 bch2_keylist_free(&keylist, NULL);
651 trace_btree_gc_coalesce(c, old_nodes[0]);
653 for (i = 0; i < nr_old_nodes; i++)
654 bch2_btree_interior_update_will_free_node(as, old_nodes[i]);
656 /* Repack everything with @new_format and sort down to one bset */
657 for (i = 0; i < nr_old_nodes; i++)
659 __bch2_btree_node_alloc_replacement(as, old_nodes[i],
663 * Conceptually we concatenate the nodes together and slice them
664 * up at different boundaries.
666 for (i = nr_new_nodes - 1; i > 0; --i) {
667 struct btree *n1 = new_nodes[i];
668 struct btree *n2 = new_nodes[i - 1];
670 struct bset *s1 = btree_bset_first(n1);
671 struct bset *s2 = btree_bset_first(n2);
672 struct bkey_packed *k, *last = NULL;
674 /* Calculate how many keys from @n2 we could fit inside @n1 */
678 k < vstruct_last(s2) &&
679 vstruct_blocks_plus(n1->data, c->block_bits,
680 u64s + k->u64s) <= blocks;
686 if (u64s == le16_to_cpu(s2->u64s)) {
687 /* n2 fits entirely in n1 */
688 n1->key.k.p = n1->data->max_key = n2->data->max_key;
690 memcpy_u64s(vstruct_last(s1),
692 le16_to_cpu(s2->u64s));
693 le16_add_cpu(&s1->u64s, le16_to_cpu(s2->u64s));
695 set_btree_bset_end(n1, n1->set);
697 six_unlock_write(&n2->lock);
698 bch2_btree_node_free_never_inserted(c, n2);
699 six_unlock_intent(&n2->lock);
701 memmove(new_nodes + i - 1,
703 sizeof(new_nodes[0]) * (nr_new_nodes - i));
704 new_nodes[--nr_new_nodes] = NULL;
706 /* move part of n2 into n1 */
707 n1->key.k.p = n1->data->max_key =
708 bkey_unpack_pos(n1, last);
711 btree_type_successor(iter->btree_id,
714 memcpy_u64s(vstruct_last(s1),
716 le16_add_cpu(&s1->u64s, u64s);
719 vstruct_idx(s2, u64s),
720 (le16_to_cpu(s2->u64s) - u64s) * sizeof(u64));
721 s2->u64s = cpu_to_le16(le16_to_cpu(s2->u64s) - u64s);
723 set_btree_bset_end(n1, n1->set);
724 set_btree_bset_end(n2, n2->set);
728 for (i = 0; i < nr_new_nodes; i++) {
729 struct btree *n = new_nodes[i];
731 recalc_packed_keys(n);
732 btree_node_reset_sib_u64s(n);
734 bch2_btree_build_aux_trees(n);
735 six_unlock_write(&n->lock);
737 bch2_btree_node_write(c, n, SIX_LOCK_intent);
741 * The keys for the old nodes get deleted. We don't want to insert keys
742 * that compare equal to the keys for the new nodes we'll also be
743 * inserting - we can't because keys on a keylist must be strictly
744 * greater than the previous keys, and we also don't need to since the
745 * key for the new node will serve the same purpose (overwriting the key
748 for (i = 0; i < nr_old_nodes; i++) {
749 struct bkey_i delete;
752 for (j = 0; j < nr_new_nodes; j++)
753 if (!bkey_cmp(old_nodes[i]->key.k.p,
754 new_nodes[j]->key.k.p))
757 bkey_init(&delete.k);
758 delete.k.p = old_nodes[i]->key.k.p;
759 bch2_keylist_add_in_order(&keylist, &delete);
765 * Keys for the new nodes get inserted: bch2_btree_insert_keys() only
766 * does the lookup once and thus expects the keys to be in sorted order
767 * so we have to make sure the new keys are correctly ordered with
768 * respect to the deleted keys added in the previous loop
770 for (i = 0; i < nr_new_nodes; i++)
771 bch2_keylist_add_in_order(&keylist, &new_nodes[i]->key);
773 /* Insert the newly coalesced nodes */
774 bch2_btree_insert_node(as, parent, iter, &keylist);
776 BUG_ON(!bch2_keylist_empty(&keylist));
778 BUG_ON(iter->l[old_nodes[0]->level].b != old_nodes[0]);
780 BUG_ON(!bch2_btree_iter_node_replace(iter, new_nodes[0]));
782 for (i = 0; i < nr_new_nodes; i++)
783 bch2_btree_open_bucket_put(c, new_nodes[i]);
785 /* Free the old nodes and update our sliding window */
786 for (i = 0; i < nr_old_nodes; i++) {
787 bch2_btree_node_free_inmem(c, old_nodes[i], iter);
788 six_unlock_intent(&old_nodes[i]->lock);
791 * the index update might have triggered a split, in which case
792 * the nodes we coalesced - the new nodes we just created -
793 * might not be sibling nodes anymore - don't add them to the
794 * sliding window (except the first):
797 old_nodes[i] = new_nodes[i];
801 six_unlock_intent(&new_nodes[i]->lock);
805 bch2_btree_update_done(as);
806 bch2_keylist_free(&keylist, NULL);
809 static int bch2_coalesce_btree(struct bch_fs *c, enum btree_id btree_id)
811 struct btree_iter iter;
815 /* Sliding window of adjacent btree nodes */
816 struct btree *merge[GC_MERGE_NODES];
817 u32 lock_seq[GC_MERGE_NODES];
820 * XXX: We don't have a good way of positively matching on sibling nodes
821 * that have the same parent - this code works by handling the cases
822 * where they might not have the same parent, and is thus fragile. Ugh.
824 * Perhaps redo this to use multiple linked iterators?
826 memset(merge, 0, sizeof(merge));
828 __for_each_btree_node(&iter, c, btree_id, POS_MIN,
830 BTREE_ITER_PREFETCH, b) {
831 memmove(merge + 1, merge,
832 sizeof(merge) - sizeof(merge[0]));
833 memmove(lock_seq + 1, lock_seq,
834 sizeof(lock_seq) - sizeof(lock_seq[0]));
838 for (i = 1; i < GC_MERGE_NODES; i++) {
840 !six_relock_intent(&merge[i]->lock, lock_seq[i]))
843 if (merge[i]->level != merge[0]->level) {
844 six_unlock_intent(&merge[i]->lock);
848 memset(merge + i, 0, (GC_MERGE_NODES - i) * sizeof(merge[0]));
850 bch2_coalesce_nodes(c, &iter, merge);
852 for (i = 1; i < GC_MERGE_NODES && merge[i]; i++) {
853 lock_seq[i] = merge[i]->lock.state.seq;
854 six_unlock_intent(&merge[i]->lock);
857 lock_seq[0] = merge[0]->lock.state.seq;
859 if (test_bit(BCH_FS_GC_STOPPING, &c->flags)) {
860 bch2_btree_iter_unlock(&iter);
864 bch2_btree_iter_cond_resched(&iter);
867 * If the parent node wasn't relocked, it might have been split
868 * and the nodes in our sliding window might not have the same
869 * parent anymore - blow away the sliding window:
871 if (btree_iter_node(&iter, iter.level + 1) &&
872 !btree_node_intent_locked(&iter, iter.level + 1))
874 (GC_MERGE_NODES - 1) * sizeof(merge[0]));
876 return bch2_btree_iter_unlock(&iter);
880 * bch_coalesce - coalesce adjacent nodes with low occupancy
882 void bch2_coalesce(struct bch_fs *c)
886 if (test_bit(BCH_FS_GC_FAILURE, &c->flags))
889 down_read(&c->gc_lock);
890 trace_gc_coalesce_start(c);
892 for (id = 0; id < BTREE_ID_NR; id++) {
893 int ret = c->btree_roots[id].b
894 ? bch2_coalesce_btree(c, id)
898 if (ret != -ESHUTDOWN)
899 bch_err(c, "btree coalescing failed: %d", ret);
900 set_bit(BCH_FS_GC_FAILURE, &c->flags);
905 trace_gc_coalesce_end(c);
906 up_read(&c->gc_lock);
909 static int bch2_gc_thread(void *arg)
911 struct bch_fs *c = arg;
912 struct io_clock *clock = &c->io_clock[WRITE];
913 unsigned long last = atomic_long_read(&clock->now);
914 unsigned last_kick = atomic_read(&c->kick_gc);
920 set_current_state(TASK_INTERRUPTIBLE);
922 if (kthread_should_stop()) {
923 __set_current_state(TASK_RUNNING);
927 if (atomic_read(&c->kick_gc) != last_kick)
930 if (c->btree_gc_periodic) {
931 unsigned long next = last + c->capacity / 16;
933 if (atomic_long_read(&clock->now) >= next)
936 bch2_io_clock_schedule_timeout(clock, next);
943 __set_current_state(TASK_RUNNING);
945 last = atomic_long_read(&clock->now);
946 last_kick = atomic_read(&c->kick_gc);
950 debug_check_no_locks_held();
956 void bch2_gc_thread_stop(struct bch_fs *c)
958 set_bit(BCH_FS_GC_STOPPING, &c->flags);
961 kthread_stop(c->gc_thread);
964 clear_bit(BCH_FS_GC_STOPPING, &c->flags);
967 int bch2_gc_thread_start(struct bch_fs *c)
969 struct task_struct *p;
971 BUG_ON(c->gc_thread);
973 p = kthread_create(bch2_gc_thread, c, "bcache_gc");
978 wake_up_process(c->gc_thread);
982 /* Initial GC computes bucket marks during startup */
984 static int bch2_initial_gc_btree(struct bch_fs *c, enum btree_id id)
986 struct btree_iter iter;
988 struct range_checks r;
991 btree_node_range_checks_init(&r, 0);
993 if (!c->btree_roots[id].b)
996 b = c->btree_roots[id].b;
997 if (!btree_node_fake(b))
998 ret = bch2_btree_mark_key_initial(c, BKEY_TYPE_BTREE,
999 bkey_i_to_s_c(&b->key));
1004 * We have to hit every btree node before starting journal replay, in
1005 * order for the journal seq blacklist machinery to work:
1007 for_each_btree_node(&iter, c, id, POS_MIN, BTREE_ITER_PREFETCH, b) {
1008 btree_node_range_checks(c, b, &r);
1010 if (btree_node_has_ptrs(b)) {
1011 struct btree_node_iter node_iter;
1012 struct bkey unpacked;
1015 for_each_btree_node_key_unpack(b, k, &node_iter,
1016 btree_node_is_extents(b),
1018 ret = bch2_btree_mark_key_initial(c,
1019 btree_node_type(b), k);
1025 bch2_btree_iter_cond_resched(&iter);
1028 return bch2_btree_iter_unlock(&iter) ?: ret;
1031 static int __bch2_initial_gc(struct bch_fs *c, struct list_head *journal)
1037 mutex_lock(&c->sb_lock);
1038 if (!bch2_sb_get_replicas(c->disk_sb)) {
1039 if (BCH_SB_INITIALIZED(c->disk_sb))
1040 bch_info(c, "building replicas info");
1041 set_bit(BCH_FS_REBUILD_REPLICAS, &c->flags);
1043 mutex_unlock(&c->sb_lock);
1047 for (id = 0; id < BTREE_ID_NR; id++) {
1048 ret = bch2_initial_gc_btree(c, id);
1053 ret = bch2_journal_mark(c, journal);
1057 if (test_bit(BCH_FS_FIXED_GENS, &c->flags)) {
1059 bch_info(c, "Unable to fix bucket gens, looping");
1063 bch_info(c, "Fixed gens, restarting initial mark and sweep:");
1064 clear_bit(BCH_FS_FIXED_GENS, &c->flags);
1069 * Skip past versions that might have possibly been used (as nonces),
1070 * but hadn't had their pointers written:
1072 if (c->sb.encryption_type)
1073 atomic64_add(1 << 16, &c->key_version);
1075 bch2_mark_superblocks(c);
1077 gc_pos_set(c, gc_phase(GC_PHASE_DONE));
1078 set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);
1083 int bch2_initial_gc(struct bch_fs *c, struct list_head *journal)
1087 down_write(&c->gc_lock);
1088 ret = __bch2_initial_gc(c, journal);
1089 up_write(&c->gc_lock);