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.h"
23 #include <linux/slab.h>
24 #include <linux/bitops.h>
25 #include <linux/freezer.h>
26 #include <linux/kthread.h>
27 #include <linux/rcupdate.h>
28 #include <trace/events/bcachefs.h>
38 static void btree_node_range_checks_init(struct range_checks *r, unsigned depth)
42 for (i = 0; i < BTREE_MAX_DEPTH; i++)
43 r->l[i].min = r->l[i].max = POS_MIN;
47 static void btree_node_range_checks(struct bch_fs *c, struct btree *b,
48 struct range_checks *r)
50 struct range_level *l = &r->l[b->level];
52 struct bpos expected_min = bkey_cmp(l->min, l->max)
53 ? btree_type_successor(b->btree_id, l->max)
56 bch2_fs_inconsistent_on(bkey_cmp(b->data->min_key, expected_min), c,
57 "btree node has incorrect min key: %llu:%llu != %llu:%llu",
58 b->data->min_key.inode,
59 b->data->min_key.offset,
63 l->max = b->data->max_key;
65 if (b->level > r->depth) {
66 l = &r->l[b->level - 1];
68 bch2_fs_inconsistent_on(bkey_cmp(b->data->min_key, l->min), c,
69 "btree node min doesn't match min of child nodes: %llu:%llu != %llu:%llu",
70 b->data->min_key.inode,
71 b->data->min_key.offset,
75 bch2_fs_inconsistent_on(bkey_cmp(b->data->max_key, l->max), c,
76 "btree node max doesn't match max of child nodes: %llu:%llu != %llu:%llu",
77 b->data->max_key.inode,
78 b->data->max_key.offset,
82 if (bkey_cmp(b->data->max_key, POS_MAX))
84 btree_type_successor(b->btree_id,
89 u8 bch2_btree_key_recalc_oldest_gen(struct bch_fs *c, struct bkey_s_c k)
91 const struct bch_extent_ptr *ptr;
94 if (bkey_extent_is_data(k.k)) {
95 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
97 extent_for_each_ptr(e, ptr) {
98 struct bch_dev *ca = c->devs[ptr->dev];
99 size_t b = PTR_BUCKET_NR(ca, ptr);
101 if (__gen_after(ca->oldest_gens[b], ptr->gen))
102 ca->oldest_gens[b] = ptr->gen;
104 max_stale = max(max_stale, ptr_stale(ca, ptr));
112 * For runtime mark and sweep:
114 static u8 bch2_btree_mark_key(struct bch_fs *c, enum bkey_type type,
118 case BKEY_TYPE_BTREE:
119 bch2_gc_mark_key(c, k, c->sb.btree_node_size, true);
121 case BKEY_TYPE_EXTENTS:
122 bch2_gc_mark_key(c, k, k.k->size, false);
123 return bch2_btree_key_recalc_oldest_gen(c, k);
129 u8 bch2_btree_mark_key_initial(struct bch_fs *c, enum bkey_type type,
132 atomic64_set(&c->key_version,
133 max_t(u64, k.k->version.lo,
134 atomic64_read(&c->key_version)));
136 return bch2_btree_mark_key(c, type, k);
139 static bool btree_gc_mark_node(struct bch_fs *c, struct btree *b)
141 if (btree_node_has_ptrs(b)) {
142 struct btree_node_iter iter;
143 struct bkey unpacked;
147 for_each_btree_node_key_unpack(b, k, &iter,
148 btree_node_is_extents(b),
150 bch2_bkey_debugcheck(c, b, k);
151 stale = max(stale, bch2_btree_mark_key(c,
152 btree_node_type(b), k));
155 if (btree_gc_rewrite_disabled(c))
162 if (btree_gc_always_rewrite(c))
168 static inline void __gc_pos_set(struct bch_fs *c, struct gc_pos new_pos)
170 write_seqcount_begin(&c->gc_pos_lock);
172 write_seqcount_end(&c->gc_pos_lock);
175 static inline void gc_pos_set(struct bch_fs *c, struct gc_pos new_pos)
177 BUG_ON(gc_pos_cmp(new_pos, c->gc_pos) <= 0);
178 __gc_pos_set(c, new_pos);
181 static int bch2_gc_btree(struct bch_fs *c, enum btree_id btree_id)
183 struct btree_iter iter;
186 struct range_checks r;
187 unsigned depth = btree_id == BTREE_ID_EXTENTS ? 0 : 1;
191 * if expensive_debug_checks is on, run range_checks on all leaf nodes:
193 if (expensive_debug_checks(c))
196 btree_node_range_checks_init(&r, depth);
198 for_each_btree_node(&iter, c, btree_id, POS_MIN, depth, b) {
199 btree_node_range_checks(c, b, &r);
201 bch2_verify_btree_nr_keys(b);
203 should_rewrite = btree_gc_mark_node(c, b);
205 gc_pos_set(c, gc_pos_btree_node(b));
208 bch2_btree_node_rewrite(&iter, b, NULL);
210 bch2_btree_iter_cond_resched(&iter);
212 ret = bch2_btree_iter_unlock(&iter);
216 mutex_lock(&c->btree_root_lock);
218 b = c->btree_roots[btree_id].b;
219 bch2_btree_mark_key(c, BKEY_TYPE_BTREE, bkey_i_to_s_c(&b->key));
220 gc_pos_set(c, gc_pos_btree_root(b->btree_id));
222 mutex_unlock(&c->btree_root_lock);
226 static void bch2_mark_allocator_buckets(struct bch_fs *c)
229 struct open_bucket *ob;
233 for_each_member_device(ca, c, ci) {
234 spin_lock(&ca->freelist_lock);
236 fifo_for_each_entry(i, &ca->free_inc, iter)
237 bch2_mark_alloc_bucket(ca, &ca->buckets[i], true);
239 for (j = 0; j < RESERVE_NR; j++)
240 fifo_for_each_entry(i, &ca->free[j], iter)
241 bch2_mark_alloc_bucket(ca, &ca->buckets[i], true);
243 spin_unlock(&ca->freelist_lock);
246 for (ob = c->open_buckets;
247 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
249 const struct bch_extent_ptr *ptr;
251 mutex_lock(&ob->lock);
252 open_bucket_for_each_ptr(ob, ptr) {
253 ca = c->devs[ptr->dev];
254 bch2_mark_alloc_bucket(ca, PTR_BUCKET(ca, ptr), true);
256 mutex_unlock(&ob->lock);
260 static void mark_metadata_sectors(struct bch_dev *ca, u64 start, u64 end,
261 enum bucket_data_type type)
263 u64 b = start >> ca->bucket_bits;
266 bch2_mark_metadata_bucket(ca, ca->buckets + b, type, true);
268 } while (b < end >> ca->bucket_bits);
271 static void bch2_dev_mark_superblocks(struct bch_dev *ca)
273 struct bch_sb_layout *layout = &ca->disk_sb.sb->layout;
276 for (i = 0; i < layout->nr_superblocks; i++) {
277 if (layout->sb_offset[i] == BCH_SB_SECTOR)
278 mark_metadata_sectors(ca, 0, BCH_SB_SECTOR,
281 mark_metadata_sectors(ca,
282 layout->sb_offset[i],
283 layout->sb_offset[i] +
284 (1 << layout->sb_max_size_bits),
290 * Mark non btree metadata - prios, journal
292 void bch2_mark_dev_metadata(struct bch_fs *c, struct bch_dev *ca)
297 lockdep_assert_held(&c->sb_lock);
299 bch2_dev_mark_superblocks(ca);
301 spin_lock(&c->journal.lock);
303 for (i = 0; i < ca->journal.nr; i++) {
304 b = ca->journal.buckets[i];
305 bch2_mark_metadata_bucket(ca, ca->buckets + b,
306 BUCKET_JOURNAL, true);
309 spin_unlock(&c->journal.lock);
311 spin_lock(&ca->prio_buckets_lock);
313 for (i = 0; i < prio_buckets(ca) * 2; i++) {
314 b = ca->prio_buckets[i];
316 bch2_mark_metadata_bucket(ca, ca->buckets + b,
320 spin_unlock(&ca->prio_buckets_lock);
323 static void bch2_mark_metadata(struct bch_fs *c)
328 mutex_lock(&c->sb_lock);
329 gc_pos_set(c, gc_phase(GC_PHASE_SB_METADATA));
331 for_each_online_member(ca, c, i)
332 bch2_mark_dev_metadata(c, ca);
333 mutex_unlock(&c->sb_lock);
336 /* Also see bch2_pending_btree_node_free_insert_done() */
337 static void bch2_mark_pending_btree_node_frees(struct bch_fs *c)
339 struct bch_fs_usage stats = { 0 };
340 struct btree_interior_update *as;
341 struct pending_btree_node_free *d;
343 mutex_lock(&c->btree_interior_update_lock);
344 gc_pos_set(c, gc_phase(GC_PHASE_PENDING_DELETE));
346 for_each_pending_btree_node_free(c, as, d)
347 if (d->index_update_done)
348 __bch2_gc_mark_key(c, bkey_i_to_s_c(&d->key),
349 c->sb.btree_node_size, true,
352 * Don't apply stats - pending deletes aren't tracked in
356 mutex_unlock(&c->btree_interior_update_lock);
360 * bch_gc - recompute bucket marks and oldest_gen, rewrite btree nodes
362 void bch2_gc(struct bch_fs *c)
366 struct bucket_mark new;
367 u64 start_time = local_clock();
372 * Walk _all_ references to buckets, and recompute them:
374 * Order matters here:
375 * - Concurrent GC relies on the fact that we have a total ordering for
376 * everything that GC walks - see gc_will_visit_node(),
377 * gc_will_visit_root()
379 * - also, references move around in the course of index updates and
380 * various other crap: everything needs to agree on the ordering
381 * references are allowed to move around in - e.g., we're allowed to
382 * start with a reference owned by an open_bucket (the allocator) and
383 * move it to the btree, but not the reverse.
385 * This is necessary to ensure that gc doesn't miss references that
386 * move around - if references move backwards in the ordering GC
387 * uses, GC could skip past them
390 if (test_bit(BCH_FS_GC_FAILURE, &c->flags))
396 * Do this before taking gc_lock - bch2_disk_reservation_get() blocks on
397 * gc_lock if sectors_available goes to 0:
399 bch2_recalc_sectors_available(c);
401 down_write(&c->gc_lock);
403 lg_global_lock(&c->usage_lock);
406 * Indicates to buckets code that gc is now in progress - done under
407 * usage_lock to avoid racing with bch2_mark_key():
409 __gc_pos_set(c, GC_POS_MIN);
411 /* Save a copy of the existing bucket stats while we recompute them: */
412 for_each_member_device(ca, c, i) {
413 ca->usage_cached = __bch2_dev_usage_read(ca);
414 for_each_possible_cpu(cpu) {
415 struct bch_dev_usage *p =
416 per_cpu_ptr(ca->usage_percpu, cpu);
417 memset(p, 0, sizeof(*p));
421 c->usage_cached = __bch2_fs_usage_read(c);
422 for_each_possible_cpu(cpu) {
423 struct bch_fs_usage *p =
424 per_cpu_ptr(c->usage_percpu, cpu);
426 memset(p->s, 0, sizeof(p->s));
427 p->persistent_reserved = 0;
430 lg_global_unlock(&c->usage_lock);
432 /* Clear bucket marks: */
433 for_each_member_device(ca, c, i)
434 for_each_bucket(g, ca) {
435 bucket_cmpxchg(g, new, ({
436 new.owned_by_allocator = 0;
438 new.cached_sectors = 0;
439 new.dirty_sectors = 0;
441 ca->oldest_gens[g - ca->buckets] = new.gen;
444 /* Walk allocator's references: */
445 bch2_mark_allocator_buckets(c);
448 while (c->gc_pos.phase < (int) BTREE_ID_NR) {
449 int ret = c->btree_roots[c->gc_pos.phase].b
450 ? bch2_gc_btree(c, (int) c->gc_pos.phase)
454 bch_err(c, "btree gc failed: %d", ret);
455 set_bit(BCH_FS_GC_FAILURE, &c->flags);
456 up_write(&c->gc_lock);
460 gc_pos_set(c, gc_phase(c->gc_pos.phase + 1));
463 bch2_mark_metadata(c);
464 bch2_mark_pending_btree_node_frees(c);
466 for_each_member_device(ca, c, i)
467 atomic_long_set(&ca->saturated_count, 0);
469 /* Indicates that gc is no longer in progress: */
470 gc_pos_set(c, gc_phase(GC_PHASE_DONE));
472 up_write(&c->gc_lock);
474 bch2_time_stats_update(&c->btree_gc_time, start_time);
477 * Wake up allocator in case it was waiting for buckets
478 * because of not being able to inc gens
480 for_each_member_device(ca, c, i)
481 bch2_wake_allocator(ca);
484 /* Btree coalescing */
486 static void recalc_packed_keys(struct btree *b)
488 struct bkey_packed *k;
490 memset(&b->nr, 0, sizeof(b->nr));
492 BUG_ON(b->nsets != 1);
494 for (k = btree_bkey_first(b, b->set);
495 k != btree_bkey_last(b, b->set);
497 btree_keys_account_key_add(&b->nr, 0, k);
500 static void bch2_coalesce_nodes(struct btree *old_nodes[GC_MERGE_NODES],
501 struct btree_iter *iter)
503 struct btree *parent = iter->nodes[old_nodes[0]->level + 1];
504 struct bch_fs *c = iter->c;
505 unsigned i, nr_old_nodes, nr_new_nodes, u64s = 0;
506 unsigned blocks = btree_blocks(c) * 2 / 3;
507 struct btree *new_nodes[GC_MERGE_NODES];
508 struct btree_interior_update *as;
509 struct btree_reserve *res;
510 struct keylist keylist;
511 struct bkey_format_state format_state;
512 struct bkey_format new_format;
514 memset(new_nodes, 0, sizeof(new_nodes));
515 bch2_keylist_init(&keylist, NULL, 0);
517 /* Count keys that are not deleted */
518 for (i = 0; i < GC_MERGE_NODES && old_nodes[i]; i++)
519 u64s += old_nodes[i]->nr.live_u64s;
521 nr_old_nodes = nr_new_nodes = i;
523 /* Check if all keys in @old_nodes could fit in one fewer node */
524 if (nr_old_nodes <= 1 ||
525 __vstruct_blocks(struct btree_node, c->block_bits,
526 DIV_ROUND_UP(u64s, nr_old_nodes - 1)) > blocks)
529 res = bch2_btree_reserve_get(c, parent, nr_old_nodes,
531 BTREE_INSERT_USE_RESERVE,
534 trace_btree_gc_coalesce_fail(c,
535 BTREE_GC_COALESCE_FAIL_RESERVE_GET);
539 if (bch2_keylist_realloc(&keylist, NULL, 0,
540 (BKEY_U64s + BKEY_EXTENT_U64s_MAX) * nr_old_nodes)) {
541 trace_btree_gc_coalesce_fail(c,
542 BTREE_GC_COALESCE_FAIL_KEYLIST_REALLOC);
546 /* Find a format that all keys in @old_nodes can pack into */
547 bch2_bkey_format_init(&format_state);
549 for (i = 0; i < nr_old_nodes; i++)
550 __bch2_btree_calc_format(&format_state, old_nodes[i]);
552 new_format = bch2_bkey_format_done(&format_state);
554 /* Check if repacking would make any nodes too big to fit */
555 for (i = 0; i < nr_old_nodes; i++)
556 if (!bch2_btree_node_format_fits(c, old_nodes[i], &new_format)) {
557 trace_btree_gc_coalesce_fail(c,
558 BTREE_GC_COALESCE_FAIL_FORMAT_FITS);
562 trace_btree_gc_coalesce(c, parent, nr_old_nodes);
564 as = bch2_btree_interior_update_alloc(c);
566 for (i = 0; i < nr_old_nodes; i++)
567 bch2_btree_interior_update_will_free_node(c, as, old_nodes[i]);
569 /* Repack everything with @new_format and sort down to one bset */
570 for (i = 0; i < nr_old_nodes; i++)
572 __bch2_btree_node_alloc_replacement(c, old_nodes[i],
576 * Conceptually we concatenate the nodes together and slice them
577 * up at different boundaries.
579 for (i = nr_new_nodes - 1; i > 0; --i) {
580 struct btree *n1 = new_nodes[i];
581 struct btree *n2 = new_nodes[i - 1];
583 struct bset *s1 = btree_bset_first(n1);
584 struct bset *s2 = btree_bset_first(n2);
585 struct bkey_packed *k, *last = NULL;
587 /* Calculate how many keys from @n2 we could fit inside @n1 */
591 k < vstruct_last(s2) &&
592 vstruct_blocks_plus(n1->data, c->block_bits,
593 u64s + k->u64s) <= blocks;
599 if (u64s == le16_to_cpu(s2->u64s)) {
600 /* n2 fits entirely in n1 */
601 n1->key.k.p = n1->data->max_key = n2->data->max_key;
603 memcpy_u64s(vstruct_last(s1),
605 le16_to_cpu(s2->u64s));
606 le16_add_cpu(&s1->u64s, le16_to_cpu(s2->u64s));
608 set_btree_bset_end(n1, n1->set);
610 six_unlock_write(&n2->lock);
611 bch2_btree_node_free_never_inserted(c, n2);
612 six_unlock_intent(&n2->lock);
614 memmove(new_nodes + i - 1,
616 sizeof(new_nodes[0]) * (nr_new_nodes - i));
617 new_nodes[--nr_new_nodes] = NULL;
619 /* move part of n2 into n1 */
620 n1->key.k.p = n1->data->max_key =
621 bkey_unpack_pos(n1, last);
624 btree_type_successor(iter->btree_id,
627 memcpy_u64s(vstruct_last(s1),
629 le16_add_cpu(&s1->u64s, u64s);
632 vstruct_idx(s2, u64s),
633 (le16_to_cpu(s2->u64s) - u64s) * sizeof(u64));
634 s2->u64s = cpu_to_le16(le16_to_cpu(s2->u64s) - u64s);
636 set_btree_bset_end(n1, n1->set);
637 set_btree_bset_end(n2, n2->set);
641 for (i = 0; i < nr_new_nodes; i++) {
642 struct btree *n = new_nodes[i];
644 recalc_packed_keys(n);
645 btree_node_reset_sib_u64s(n);
647 bch2_btree_build_aux_trees(n);
648 six_unlock_write(&n->lock);
650 bch2_btree_node_write(c, n, &as->cl, SIX_LOCK_intent, -1);
654 * The keys for the old nodes get deleted. We don't want to insert keys
655 * that compare equal to the keys for the new nodes we'll also be
656 * inserting - we can't because keys on a keylist must be strictly
657 * greater than the previous keys, and we also don't need to since the
658 * key for the new node will serve the same purpose (overwriting the key
661 for (i = 0; i < nr_old_nodes; i++) {
662 struct bkey_i delete;
665 for (j = 0; j < nr_new_nodes; j++)
666 if (!bkey_cmp(old_nodes[i]->key.k.p,
667 new_nodes[j]->key.k.p))
670 bkey_init(&delete.k);
671 delete.k.p = old_nodes[i]->key.k.p;
672 bch2_keylist_add_in_order(&keylist, &delete);
678 * Keys for the new nodes get inserted: bch2_btree_insert_keys() only
679 * does the lookup once and thus expects the keys to be in sorted order
680 * so we have to make sure the new keys are correctly ordered with
681 * respect to the deleted keys added in the previous loop
683 for (i = 0; i < nr_new_nodes; i++)
684 bch2_keylist_add_in_order(&keylist, &new_nodes[i]->key);
686 /* Insert the newly coalesced nodes */
687 bch2_btree_insert_node(parent, iter, &keylist, res, as);
689 BUG_ON(!bch2_keylist_empty(&keylist));
691 BUG_ON(iter->nodes[old_nodes[0]->level] != old_nodes[0]);
693 BUG_ON(!bch2_btree_iter_node_replace(iter, new_nodes[0]));
695 for (i = 0; i < nr_new_nodes; i++)
696 bch2_btree_open_bucket_put(c, new_nodes[i]);
698 /* Free the old nodes and update our sliding window */
699 for (i = 0; i < nr_old_nodes; i++) {
700 bch2_btree_node_free_inmem(iter, old_nodes[i]);
701 six_unlock_intent(&old_nodes[i]->lock);
704 * the index update might have triggered a split, in which case
705 * the nodes we coalesced - the new nodes we just created -
706 * might not be sibling nodes anymore - don't add them to the
707 * sliding window (except the first):
710 old_nodes[i] = new_nodes[i];
714 six_unlock_intent(&new_nodes[i]->lock);
718 bch2_keylist_free(&keylist, NULL);
719 bch2_btree_reserve_put(c, res);
722 static int bch2_coalesce_btree(struct bch_fs *c, enum btree_id btree_id)
724 struct btree_iter iter;
728 /* Sliding window of adjacent btree nodes */
729 struct btree *merge[GC_MERGE_NODES];
730 u32 lock_seq[GC_MERGE_NODES];
733 * XXX: We don't have a good way of positively matching on sibling nodes
734 * that have the same parent - this code works by handling the cases
735 * where they might not have the same parent, and is thus fragile. Ugh.
737 * Perhaps redo this to use multiple linked iterators?
739 memset(merge, 0, sizeof(merge));
741 __for_each_btree_node(&iter, c, btree_id, POS_MIN, 0, b, U8_MAX) {
742 memmove(merge + 1, merge,
743 sizeof(merge) - sizeof(merge[0]));
744 memmove(lock_seq + 1, lock_seq,
745 sizeof(lock_seq) - sizeof(lock_seq[0]));
749 for (i = 1; i < GC_MERGE_NODES; i++) {
751 !six_relock_intent(&merge[i]->lock, lock_seq[i]))
754 if (merge[i]->level != merge[0]->level) {
755 six_unlock_intent(&merge[i]->lock);
759 memset(merge + i, 0, (GC_MERGE_NODES - i) * sizeof(merge[0]));
761 bch2_coalesce_nodes(merge, &iter);
763 for (i = 1; i < GC_MERGE_NODES && merge[i]; i++) {
764 lock_seq[i] = merge[i]->lock.state.seq;
765 six_unlock_intent(&merge[i]->lock);
768 lock_seq[0] = merge[0]->lock.state.seq;
770 if (test_bit(BCH_FS_GC_STOPPING, &c->flags)) {
771 bch2_btree_iter_unlock(&iter);
775 bch2_btree_iter_cond_resched(&iter);
778 * If the parent node wasn't relocked, it might have been split
779 * and the nodes in our sliding window might not have the same
780 * parent anymore - blow away the sliding window:
782 if (iter.nodes[iter.level + 1] &&
783 !btree_node_intent_locked(&iter, iter.level + 1))
785 (GC_MERGE_NODES - 1) * sizeof(merge[0]));
787 return bch2_btree_iter_unlock(&iter);
791 * bch_coalesce - coalesce adjacent nodes with low occupancy
793 void bch2_coalesce(struct bch_fs *c)
798 if (test_bit(BCH_FS_GC_FAILURE, &c->flags))
801 down_read(&c->gc_lock);
802 trace_gc_coalesce_start(c);
803 start_time = local_clock();
805 for (id = 0; id < BTREE_ID_NR; id++) {
806 int ret = c->btree_roots[id].b
807 ? bch2_coalesce_btree(c, id)
811 if (ret != -ESHUTDOWN)
812 bch_err(c, "btree coalescing failed: %d", ret);
813 set_bit(BCH_FS_GC_FAILURE, &c->flags);
818 bch2_time_stats_update(&c->btree_coalesce_time, start_time);
819 trace_gc_coalesce_end(c);
820 up_read(&c->gc_lock);
823 static int bch2_gc_thread(void *arg)
825 struct bch_fs *c = arg;
826 struct io_clock *clock = &c->io_clock[WRITE];
827 unsigned long last = atomic_long_read(&clock->now);
828 unsigned last_kick = atomic_read(&c->kick_gc);
833 unsigned long next = last + c->capacity / 16;
835 while (atomic_long_read(&clock->now) < next) {
836 set_current_state(TASK_INTERRUPTIBLE);
838 if (kthread_should_stop()) {
839 __set_current_state(TASK_RUNNING);
843 if (atomic_read(&c->kick_gc) != last_kick) {
844 __set_current_state(TASK_RUNNING);
848 bch2_io_clock_schedule_timeout(clock, next);
852 last = atomic_long_read(&clock->now);
853 last_kick = atomic_read(&c->kick_gc);
856 if (!btree_gc_coalesce_disabled(c))
859 debug_check_no_locks_held();
865 void bch2_gc_thread_stop(struct bch_fs *c)
867 set_bit(BCH_FS_GC_STOPPING, &c->flags);
870 kthread_stop(c->gc_thread);
873 clear_bit(BCH_FS_GC_STOPPING, &c->flags);
876 int bch2_gc_thread_start(struct bch_fs *c)
878 struct task_struct *p;
880 BUG_ON(c->gc_thread);
882 p = kthread_create(bch2_gc_thread, c, "bcache_gc");
887 wake_up_process(c->gc_thread);
891 /* Initial GC computes bucket marks during startup */
893 static void bch2_initial_gc_btree(struct bch_fs *c, enum btree_id id)
895 struct btree_iter iter;
897 struct range_checks r;
899 btree_node_range_checks_init(&r, 0);
901 if (!c->btree_roots[id].b)
905 * We have to hit every btree node before starting journal replay, in
906 * order for the journal seq blacklist machinery to work:
908 for_each_btree_node(&iter, c, id, POS_MIN, 0, b) {
909 btree_node_range_checks(c, b, &r);
911 if (btree_node_has_ptrs(b)) {
912 struct btree_node_iter node_iter;
913 struct bkey unpacked;
916 for_each_btree_node_key_unpack(b, k, &node_iter,
917 btree_node_is_extents(b),
919 bch2_btree_mark_key_initial(c, btree_node_type(b), k);
922 bch2_btree_iter_cond_resched(&iter);
925 bch2_btree_iter_unlock(&iter);
927 bch2_btree_mark_key(c, BKEY_TYPE_BTREE,
928 bkey_i_to_s_c(&c->btree_roots[id].b->key));
931 int bch2_initial_gc(struct bch_fs *c, struct list_head *journal)
935 for (id = 0; id < BTREE_ID_NR; id++)
936 bch2_initial_gc_btree(c, id);
939 bch2_journal_mark(c, journal);
941 bch2_mark_metadata(c);
944 * Skip past versions that might have possibly been used (as nonces),
945 * but hadn't had their pointers written:
947 if (c->sb.encryption_type)
948 atomic64_add(1 << 16, &c->key_version);
950 gc_pos_set(c, gc_phase(GC_PHASE_DONE));
951 set_bit(BCH_FS_INITIAL_GC_DONE, &c->flags);