1 // SPDX-License-Identifier: GPL-2.0
3 * Moving/copying garbage collector
5 * Copyright 2012 Google, Inc.
9 #include "alloc_background.h"
10 #include "alloc_foreground.h"
11 #include "btree_iter.h"
12 #include "btree_update.h"
13 #include "btree_write_buffer.h"
16 #include "disk_groups.h"
20 #include "eytzinger.h"
28 #include <trace/events/bcachefs.h>
29 #include <linux/bsearch.h>
30 #include <linux/freezer.h>
31 #include <linux/kthread.h>
32 #include <linux/math64.h>
33 #include <linux/sched/task.h>
34 #include <linux/sort.h>
35 #include <linux/wait.h>
37 static int bch2_bucket_is_movable(struct btree_trans *trans,
38 struct bpos bucket, u64 time, u8 *gen)
40 struct btree_iter iter;
42 struct bch_alloc_v4 _a;
43 const struct bch_alloc_v4 *a;
46 if (bch2_bucket_is_open(trans->c, bucket.inode, bucket.offset))
49 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, bucket, 0);
50 k = bch2_btree_iter_peek_slot(&iter);
52 bch2_trans_iter_exit(trans, &iter);
57 a = bch2_alloc_to_v4(k, &_a);
59 ret = data_type_movable(a->data_type) &&
60 a->fragmentation_lru &&
61 a->fragmentation_lru <= time;
64 struct printbuf buf = PRINTBUF;
66 bch2_bkey_val_to_text(&buf, trans->c, k);
67 pr_debug("%s", buf.buf);
74 typedef FIFO(struct move_bucket_in_flight) move_buckets_in_flight;
81 typedef DARRAY(struct move_bucket) move_buckets;
83 static int move_bucket_cmp(const void *_l, const void *_r)
85 const struct move_bucket *l = _l;
86 const struct move_bucket *r = _r;
88 return bpos_cmp(l->bucket, r->bucket) ?: cmp_int(l->gen, r->gen);
91 static bool bucket_in_flight(move_buckets *buckets_sorted, struct move_bucket b)
96 sizeof(buckets_sorted->data[0]),
97 move_bucket_cmp) != NULL;
100 static void move_buckets_wait(struct btree_trans *trans,
101 struct moving_context *ctxt,
102 move_buckets_in_flight *buckets_in_flight,
103 size_t nr, bool verify_evacuated)
105 while (!fifo_empty(buckets_in_flight)) {
106 struct move_bucket_in_flight *i = &fifo_peek_front(buckets_in_flight);
108 if (fifo_used(buckets_in_flight) > nr)
109 move_ctxt_wait_event(ctxt, trans, !atomic_read(&i->count));
111 if (atomic_read(&i->count))
115 * moving_ctxt_exit calls bch2_write as it flushes pending
116 * reads, which inits another btree_trans; this one must be
119 if (verify_evacuated)
120 bch2_verify_bucket_evacuated(trans, i->bucket, i->gen);
121 buckets_in_flight->front++;
124 bch2_trans_unlock(trans);
127 static int bch2_copygc_get_buckets(struct btree_trans *trans,
128 struct moving_context *ctxt,
129 move_buckets_in_flight *buckets_in_flight,
130 move_buckets *buckets)
132 struct btree_iter iter;
133 move_buckets buckets_sorted = { 0 };
134 struct move_bucket_in_flight *i;
136 size_t fifo_iter, nr_to_get;
139 move_buckets_wait(trans, ctxt, buckets_in_flight, buckets_in_flight->size / 2, true);
141 nr_to_get = max(16UL, fifo_used(buckets_in_flight) / 4);
143 fifo_for_each_entry_ptr(i, buckets_in_flight, fifo_iter) {
144 ret = darray_push(&buckets_sorted, ((struct move_bucket) {i->bucket, i->gen}));
146 bch_err(trans->c, "error allocating move_buckets_sorted");
151 sort(buckets_sorted.data,
153 sizeof(buckets_sorted.data[0]),
157 ret = for_each_btree_key2_upto(trans, iter, BTREE_ID_lru,
158 lru_pos(BCH_LRU_FRAGMENTATION_START, 0, 0),
159 lru_pos(BCH_LRU_FRAGMENTATION_START, U64_MAX, LRU_TIME_MAX),
161 struct move_bucket b = { .bucket = u64_to_bucket(k.k->p.offset) };
164 if (!bucket_in_flight(&buckets_sorted, b) &&
165 bch2_bucket_is_movable(trans, b.bucket, lru_pos_time(k.k->p), &b.gen))
166 ret = darray_push(buckets, b) ?: buckets->nr >= nr_to_get;
171 darray_exit(&buckets_sorted);
173 return ret < 0 ? ret : 0;
176 static int bch2_copygc(struct btree_trans *trans,
177 struct moving_context *ctxt,
178 move_buckets_in_flight *buckets_in_flight)
180 struct bch_fs *c = trans->c;
181 struct bch_move_stats move_stats;
182 struct data_update_opts data_opts = {
183 .btree_insert_flags = BTREE_INSERT_USE_RESERVE|JOURNAL_WATERMARK_copygc,
185 move_buckets buckets = { 0 };
186 struct move_bucket_in_flight *f;
187 struct move_bucket *i;
190 ret = bch2_btree_write_buffer_flush(trans);
191 if (bch2_fs_fatal_err_on(ret, c, "%s: error %s from bch2_btree_write_buffer_flush()",
192 __func__, bch2_err_str(ret)))
195 bch2_move_stats_init(&move_stats, "copygc");
196 ctxt->stats = &move_stats;
198 ret = bch2_copygc_get_buckets(trans, ctxt, buckets_in_flight, &buckets);
202 darray_for_each(buckets, i) {
203 if (unlikely(freezing(current)))
206 f = fifo_push_ref(buckets_in_flight);
207 f->bucket = i->bucket;
209 atomic_set(&f->count, 0);
211 ret = __bch2_evacuate_bucket(trans, ctxt, f, f->bucket, f->gen, data_opts);
216 darray_exit(&buckets);
218 /* no entries in LRU btree found, or got to end: */
222 if (ret < 0 && !bch2_err_matches(ret, EROFS))
223 bch_err(c, "error from bch2_move_data() in copygc: %s", bch2_err_str(ret));
225 trace_and_count(c, copygc, c, atomic64_read(&move_stats.sectors_moved), 0, 0, 0);
231 * Copygc runs when the amount of fragmented data is above some arbitrary
234 * The threshold at the limit - when the device is full - is the amount of space
235 * we reserved in bch2_recalc_capacity; we can't have more than that amount of
236 * disk space stranded due to fragmentation and store everything we have
239 * But we don't want to be running copygc unnecessarily when the device still
240 * has plenty of free space - rather, we want copygc to smoothly run every so
241 * often and continually reduce the amount of fragmented space as the device
242 * fills up. So, we increase the threshold by half the current free space.
244 unsigned long bch2_copygc_wait_amount(struct bch_fs *c)
248 s64 wait = S64_MAX, fragmented_allowed, fragmented;
251 for_each_rw_member(ca, c, dev_idx) {
252 struct bch_dev_usage usage = bch2_dev_usage_read(ca);
254 fragmented_allowed = ((__dev_buckets_available(ca, usage, RESERVE_stripe) *
255 ca->mi.bucket_size) >> 1);
258 for (i = 0; i < BCH_DATA_NR; i++)
259 if (data_type_movable(i))
260 fragmented += usage.d[i].fragmented;
262 wait = min(wait, max(0LL, fragmented_allowed - fragmented));
268 void bch2_copygc_wait_to_text(struct printbuf *out, struct bch_fs *c)
270 prt_printf(out, "Currently waiting for: ");
271 prt_human_readable_u64(out, max(0LL, c->copygc_wait -
272 atomic64_read(&c->io_clock[WRITE].now)) << 9);
275 prt_printf(out, "Currently calculated wait: ");
276 prt_human_readable_u64(out, bch2_copygc_wait_amount(c));
280 static int bch2_copygc_thread(void *arg)
282 struct bch_fs *c = arg;
283 struct btree_trans trans;
284 struct moving_context ctxt;
285 struct io_clock *clock = &c->io_clock[WRITE];
286 move_buckets_in_flight move_buckets;
290 if (!init_fifo(&move_buckets, 1 << 14, GFP_KERNEL)) {
291 bch_err(c, "error allocating copygc buckets in flight");
296 bch2_trans_init(&trans, c, 0, 0);
297 bch2_moving_ctxt_init(&ctxt, c, NULL, NULL,
298 writepoint_ptr(&c->copygc_write_point),
301 while (!ret && !kthread_should_stop()) {
302 bch2_trans_unlock(&trans);
305 if (!c->copy_gc_enabled) {
306 move_buckets_wait(&trans, &ctxt, &move_buckets, 0, true);
307 kthread_wait_freezable(c->copy_gc_enabled);
310 if (unlikely(freezing(current))) {
311 move_buckets_wait(&trans, &ctxt, &move_buckets, 0, true);
312 __refrigerator(false);
316 last = atomic64_read(&clock->now);
317 wait = bch2_copygc_wait_amount(c);
319 if (wait > clock->max_slop) {
320 move_buckets_wait(&trans, &ctxt, &move_buckets, 0, true);
321 trace_and_count(c, copygc_wait, c, wait, last + wait);
322 c->copygc_wait = last + wait;
323 bch2_kthread_io_clock_wait(clock, last + wait,
324 MAX_SCHEDULE_TIMEOUT);
330 c->copygc_running = true;
331 ret = bch2_copygc(&trans, &ctxt, &move_buckets);
332 c->copygc_running = false;
334 wake_up(&c->copygc_running_wq);
337 bch2_moving_ctxt_exit(&ctxt);
338 bch2_trans_exit(&trans);
339 free_fifo(&move_buckets);
344 void bch2_copygc_stop(struct bch_fs *c)
346 if (c->copygc_thread) {
347 kthread_stop(c->copygc_thread);
348 put_task_struct(c->copygc_thread);
350 c->copygc_thread = NULL;
353 int bch2_copygc_start(struct bch_fs *c)
355 struct task_struct *t;
358 if (c->copygc_thread)
361 if (c->opts.nochanges)
364 if (bch2_fs_init_fault("copygc_start"))
367 t = kthread_create(bch2_copygc_thread, c, "bch-copygc/%s", c->name);
368 ret = PTR_ERR_OR_ZERO(t);
370 bch_err(c, "error creating copygc thread: %s", bch2_err_str(ret));
376 c->copygc_thread = t;
377 wake_up_process(c->copygc_thread);
382 void bch2_fs_copygc_init(struct bch_fs *c)
384 init_waitqueue_head(&c->copygc_running_wq);
385 c->copygc_running = false;