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 struct buckets_in_flight {
38 struct rhashtable table;
39 struct move_bucket_in_flight *first;
40 struct move_bucket_in_flight *last;
45 static const struct rhashtable_params bch_move_bucket_params = {
46 .head_offset = offsetof(struct move_bucket_in_flight, hash),
47 .key_offset = offsetof(struct move_bucket_in_flight, bucket.k),
48 .key_len = sizeof(struct move_bucket_key),
51 static struct move_bucket_in_flight *
52 move_bucket_in_flight_add(struct buckets_in_flight *list, struct move_bucket b)
54 struct move_bucket_in_flight *new = kzalloc(sizeof(*new), GFP_KERNEL);
58 return ERR_PTR(-ENOMEM);
62 ret = rhashtable_lookup_insert_fast(&list->table, &new->hash,
63 bch_move_bucket_params);
72 list->last->next = new;
76 list->sectors += b.sectors;
80 static int bch2_bucket_is_movable(struct btree_trans *trans,
81 struct move_bucket *b, u64 time)
83 struct btree_iter iter;
85 struct bch_alloc_v4 _a;
86 const struct bch_alloc_v4 *a;
89 if (bch2_bucket_is_open(trans->c,
94 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
95 b->k.bucket, BTREE_ITER_CACHED);
96 k = bch2_btree_iter_peek_slot(&iter);
98 bch2_trans_iter_exit(trans, &iter);
103 a = bch2_alloc_to_v4(k, &_a);
105 b->sectors = a->dirty_sectors;
107 ret = data_type_movable(a->data_type) &&
108 a->fragmentation_lru &&
109 a->fragmentation_lru <= time;
112 struct printbuf buf = PRINTBUF;
114 bch2_bkey_val_to_text(&buf, trans->c, k);
115 pr_debug("%s", buf.buf);
122 static void move_buckets_wait(struct btree_trans *trans,
123 struct moving_context *ctxt,
124 struct buckets_in_flight *list,
127 struct move_bucket_in_flight *i;
130 while ((i = list->first)) {
132 move_ctxt_wait_event(ctxt, trans, !atomic_read(&i->count));
134 if (atomic_read(&i->count))
137 list->first = i->next;
142 list->sectors -= i->bucket.sectors;
144 ret = rhashtable_remove_fast(&list->table, &i->hash,
145 bch_move_bucket_params);
150 bch2_trans_unlock(trans);
153 static bool bucket_in_flight(struct buckets_in_flight *list,
154 struct move_bucket_key k)
156 return rhashtable_lookup_fast(&list->table, &k, bch_move_bucket_params);
159 typedef DARRAY(struct move_bucket) move_buckets;
161 static int bch2_copygc_get_buckets(struct btree_trans *trans,
162 struct moving_context *ctxt,
163 struct buckets_in_flight *buckets_in_flight,
164 move_buckets *buckets)
166 struct bch_fs *c = trans->c;
167 struct btree_iter iter;
169 size_t nr_to_get = max(16UL, buckets_in_flight->nr / 4);
170 size_t saw = 0, in_flight = 0, not_movable = 0, sectors = 0;
173 move_buckets_wait(trans, ctxt, buckets_in_flight, false);
175 ret = bch2_btree_write_buffer_flush(trans);
176 if (bch2_fs_fatal_err_on(ret, c, "%s: error %s from bch2_btree_write_buffer_flush()",
177 __func__, bch2_err_str(ret)))
180 ret = for_each_btree_key2_upto(trans, iter, BTREE_ID_lru,
181 lru_pos(BCH_LRU_FRAGMENTATION_START, 0, 0),
182 lru_pos(BCH_LRU_FRAGMENTATION_START, U64_MAX, LRU_TIME_MAX),
184 struct move_bucket b = { .k.bucket = u64_to_bucket(k.k->p.offset) };
189 if (!bch2_bucket_is_movable(trans, &b, lru_pos_time(k.k->p)))
191 else if (bucket_in_flight(buckets_in_flight, b.k))
194 ret = darray_push(buckets, b) ?: buckets->nr >= nr_to_get;
196 sectors += b.sectors;
201 pr_debug("have: %zu (%zu) saw %zu in flight %zu not movable %zu got %zu (%zu)/%zu buckets ret %i",
202 buckets_in_flight->nr, buckets_in_flight->sectors,
203 saw, in_flight, not_movable, buckets->nr, sectors, nr_to_get, ret);
205 return ret < 0 ? ret : 0;
208 static int bch2_copygc(struct btree_trans *trans,
209 struct moving_context *ctxt,
210 struct buckets_in_flight *buckets_in_flight)
212 struct bch_fs *c = trans->c;
213 struct data_update_opts data_opts = {
214 .btree_insert_flags = BTREE_INSERT_USE_RESERVE|JOURNAL_WATERMARK_copygc,
216 move_buckets buckets = { 0 };
217 struct move_bucket_in_flight *f;
218 struct move_bucket *i;
219 u64 moved = atomic64_read(&ctxt->stats->sectors_moved);
222 ret = bch2_copygc_get_buckets(trans, ctxt, buckets_in_flight, &buckets);
226 darray_for_each(buckets, i) {
227 if (unlikely(freezing(current)))
230 f = move_bucket_in_flight_add(buckets_in_flight, *i);
231 ret = PTR_ERR_OR_ZERO(f);
232 if (ret == -EEXIST) /* rare race: copygc_get_buckets returned same bucket more than once */
234 if (ret == -ENOMEM) { /* flush IO, continue later */
239 ret = __bch2_evacuate_bucket(trans, ctxt, f, f->bucket.k.bucket,
240 f->bucket.k.gen, data_opts);
245 darray_exit(&buckets);
247 /* no entries in LRU btree found, or got to end: */
251 if (ret < 0 && !bch2_err_matches(ret, EROFS))
252 bch_err(c, "error from bch2_move_data() in copygc: %s", bch2_err_str(ret));
254 moved = atomic64_read(&ctxt->stats->sectors_moved) - moved;
255 trace_and_count(c, copygc, c, moved, 0, 0, 0);
260 * Copygc runs when the amount of fragmented data is above some arbitrary
263 * The threshold at the limit - when the device is full - is the amount of space
264 * we reserved in bch2_recalc_capacity; we can't have more than that amount of
265 * disk space stranded due to fragmentation and store everything we have
268 * But we don't want to be running copygc unnecessarily when the device still
269 * has plenty of free space - rather, we want copygc to smoothly run every so
270 * often and continually reduce the amount of fragmented space as the device
271 * fills up. So, we increase the threshold by half the current free space.
273 unsigned long bch2_copygc_wait_amount(struct bch_fs *c)
277 s64 wait = S64_MAX, fragmented_allowed, fragmented;
280 for_each_rw_member(ca, c, dev_idx) {
281 struct bch_dev_usage usage = bch2_dev_usage_read(ca);
283 fragmented_allowed = ((__dev_buckets_available(ca, usage, RESERVE_stripe) *
284 ca->mi.bucket_size) >> 1);
287 for (i = 0; i < BCH_DATA_NR; i++)
288 if (data_type_movable(i))
289 fragmented += usage.d[i].fragmented;
291 wait = min(wait, max(0LL, fragmented_allowed - fragmented));
297 void bch2_copygc_wait_to_text(struct printbuf *out, struct bch_fs *c)
299 prt_printf(out, "Currently waiting for: ");
300 prt_human_readable_u64(out, max(0LL, c->copygc_wait -
301 atomic64_read(&c->io_clock[WRITE].now)) << 9);
304 prt_printf(out, "Currently waiting since: ");
305 prt_human_readable_u64(out, max(0LL,
306 atomic64_read(&c->io_clock[WRITE].now) -
307 c->copygc_wait_at) << 9);
310 prt_printf(out, "Currently calculated wait: ");
311 prt_human_readable_u64(out, bch2_copygc_wait_amount(c));
315 static int bch2_copygc_thread(void *arg)
317 struct bch_fs *c = arg;
318 struct btree_trans trans;
319 struct moving_context ctxt;
320 struct bch_move_stats move_stats;
321 struct io_clock *clock = &c->io_clock[WRITE];
322 struct buckets_in_flight move_buckets;
326 memset(&move_buckets, 0, sizeof(move_buckets));
328 ret = rhashtable_init(&move_buckets.table, &bch_move_bucket_params);
330 bch_err(c, "error allocating copygc buckets in flight: %s",
336 bch2_trans_init(&trans, c, 0, 0);
338 bch2_move_stats_init(&move_stats, "copygc");
339 bch2_moving_ctxt_init(&ctxt, c, NULL, &move_stats,
340 writepoint_ptr(&c->copygc_write_point),
343 while (!ret && !kthread_should_stop()) {
344 bch2_trans_unlock(&trans);
347 if (!c->copy_gc_enabled) {
348 move_buckets_wait(&trans, &ctxt, &move_buckets, true);
349 kthread_wait_freezable(c->copy_gc_enabled);
352 if (unlikely(freezing(current))) {
353 move_buckets_wait(&trans, &ctxt, &move_buckets, true);
354 __refrigerator(false);
358 last = atomic64_read(&clock->now);
359 wait = bch2_copygc_wait_amount(c);
361 if (wait > clock->max_slop) {
362 c->copygc_wait_at = last;
363 c->copygc_wait = last + wait;
364 move_buckets_wait(&trans, &ctxt, &move_buckets, true);
365 trace_and_count(c, copygc_wait, c, wait, last + wait);
366 bch2_kthread_io_clock_wait(clock, last + wait,
367 MAX_SCHEDULE_TIMEOUT);
373 c->copygc_running = true;
374 ret = bch2_copygc(&trans, &ctxt, &move_buckets);
375 c->copygc_running = false;
377 wake_up(&c->copygc_running_wq);
380 move_buckets_wait(&trans, &ctxt, &move_buckets, true);
381 bch2_trans_exit(&trans);
382 bch2_moving_ctxt_exit(&ctxt);
387 void bch2_copygc_stop(struct bch_fs *c)
389 if (c->copygc_thread) {
390 kthread_stop(c->copygc_thread);
391 put_task_struct(c->copygc_thread);
393 c->copygc_thread = NULL;
396 int bch2_copygc_start(struct bch_fs *c)
398 struct task_struct *t;
401 if (c->copygc_thread)
404 if (c->opts.nochanges)
407 if (bch2_fs_init_fault("copygc_start"))
410 t = kthread_create(bch2_copygc_thread, c, "bch-copygc/%s", c->name);
411 ret = PTR_ERR_OR_ZERO(t);
413 bch_err(c, "error creating copygc thread: %s", bch2_err_str(ret));
419 c->copygc_thread = t;
420 wake_up_process(c->copygc_thread);
425 void bch2_fs_copygc_init(struct bch_fs *c)
427 init_waitqueue_head(&c->copygc_running_wq);
428 c->copygc_running = false;