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"
23 #include <linux/freezer.h>
24 #include <linux/kthread.h>
25 #include <linux/math64.h>
26 #include <linux/sched/task.h>
27 #include <linux/wait.h>
29 struct buckets_in_flight {
30 struct rhashtable table;
31 struct move_bucket_in_flight *first;
32 struct move_bucket_in_flight *last;
37 static const struct rhashtable_params bch_move_bucket_params = {
38 .head_offset = offsetof(struct move_bucket_in_flight, hash),
39 .key_offset = offsetof(struct move_bucket_in_flight, bucket.k),
40 .key_len = sizeof(struct move_bucket_key),
43 static struct move_bucket_in_flight *
44 move_bucket_in_flight_add(struct buckets_in_flight *list, struct move_bucket b)
46 struct move_bucket_in_flight *new = kzalloc(sizeof(*new), GFP_KERNEL);
50 return ERR_PTR(-ENOMEM);
54 ret = rhashtable_lookup_insert_fast(&list->table, &new->hash,
55 bch_move_bucket_params);
64 list->last->next = new;
68 list->sectors += b.sectors;
72 static int bch2_bucket_is_movable(struct btree_trans *trans,
73 struct move_bucket *b, u64 time)
75 struct btree_iter iter;
77 struct bch_alloc_v4 _a;
78 const struct bch_alloc_v4 *a;
81 if (bch2_bucket_is_open(trans->c,
86 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc,
87 b->k.bucket, BTREE_ITER_CACHED);
92 a = bch2_alloc_to_v4(k, &_a);
94 b->sectors = bch2_bucket_sectors_dirty(*a);
96 ret = data_type_movable(a->data_type) &&
97 a->fragmentation_lru &&
98 a->fragmentation_lru <= time;
100 bch2_trans_iter_exit(trans, &iter);
104 static void move_buckets_wait(struct moving_context *ctxt,
105 struct buckets_in_flight *list,
108 struct move_bucket_in_flight *i;
111 while ((i = list->first)) {
113 move_ctxt_wait_event(ctxt, !atomic_read(&i->count));
115 if (atomic_read(&i->count))
118 list->first = i->next;
123 list->sectors -= i->bucket.sectors;
125 ret = rhashtable_remove_fast(&list->table, &i->hash,
126 bch_move_bucket_params);
131 bch2_trans_unlock_long(ctxt->trans);
134 static bool bucket_in_flight(struct buckets_in_flight *list,
135 struct move_bucket_key k)
137 return rhashtable_lookup_fast(&list->table, &k, bch_move_bucket_params);
140 typedef DARRAY(struct move_bucket) move_buckets;
142 static int bch2_copygc_get_buckets(struct moving_context *ctxt,
143 struct buckets_in_flight *buckets_in_flight,
144 move_buckets *buckets)
146 struct btree_trans *trans = ctxt->trans;
147 struct bch_fs *c = trans->c;
148 struct btree_iter iter;
150 size_t nr_to_get = max_t(size_t, 16U, buckets_in_flight->nr / 4);
151 size_t saw = 0, in_flight = 0, not_movable = 0, sectors = 0;
154 move_buckets_wait(ctxt, buckets_in_flight, false);
156 ret = bch2_btree_write_buffer_tryflush(trans);
157 if (bch2_err_matches(ret, EROFS))
160 if (bch2_fs_fatal_err_on(ret, c, "%s: error %s from bch2_btree_write_buffer_tryflush()",
161 __func__, bch2_err_str(ret)))
164 ret = for_each_btree_key2_upto(trans, iter, BTREE_ID_lru,
165 lru_pos(BCH_LRU_FRAGMENTATION_START, 0, 0),
166 lru_pos(BCH_LRU_FRAGMENTATION_START, U64_MAX, LRU_TIME_MAX),
168 struct move_bucket b = { .k.bucket = u64_to_bucket(k.k->p.offset) };
173 ret2 = bch2_bucket_is_movable(trans, &b, lru_pos_time(k.k->p));
179 else if (bucket_in_flight(buckets_in_flight, b.k))
182 ret2 = darray_push(buckets, b);
185 sectors += b.sectors;
188 ret2 = buckets->nr >= nr_to_get;
193 pr_debug("have: %zu (%zu) saw %zu in flight %zu not movable %zu got %zu (%zu)/%zu buckets ret %i",
194 buckets_in_flight->nr, buckets_in_flight->sectors,
195 saw, in_flight, not_movable, buckets->nr, sectors, nr_to_get, ret);
197 return ret < 0 ? ret : 0;
201 static int bch2_copygc(struct moving_context *ctxt,
202 struct buckets_in_flight *buckets_in_flight,
205 struct btree_trans *trans = ctxt->trans;
206 struct bch_fs *c = trans->c;
207 struct data_update_opts data_opts = {
208 .btree_insert_flags = BCH_WATERMARK_copygc,
210 move_buckets buckets = { 0 };
211 struct move_bucket_in_flight *f;
212 struct move_bucket *i;
213 u64 moved = atomic64_read(&ctxt->stats->sectors_moved);
216 ret = bch2_copygc_get_buckets(ctxt, buckets_in_flight, &buckets);
220 darray_for_each(buckets, i) {
221 if (kthread_should_stop() || freezing(current))
224 f = move_bucket_in_flight_add(buckets_in_flight, *i);
225 ret = PTR_ERR_OR_ZERO(f);
226 if (ret == -EEXIST) { /* rare race: copygc_get_buckets returned same bucket more than once */
230 if (ret == -ENOMEM) { /* flush IO, continue later */
235 ret = bch2_evacuate_bucket(ctxt, f, f->bucket.k.bucket,
236 f->bucket.k.gen, data_opts);
243 darray_exit(&buckets);
245 /* no entries in LRU btree found, or got to end: */
246 if (bch2_err_matches(ret, ENOENT))
249 if (ret < 0 && !bch2_err_matches(ret, EROFS))
250 bch_err_msg(c, ret, "from bch2_move_data()");
252 moved = atomic64_read(&ctxt->stats->sectors_moved) - moved;
253 trace_and_count(c, copygc, c, moved, 0, 0, 0);
258 * Copygc runs when the amount of fragmented data is above some arbitrary
261 * The threshold at the limit - when the device is full - is the amount of space
262 * we reserved in bch2_recalc_capacity; we can't have more than that amount of
263 * disk space stranded due to fragmentation and store everything we have
266 * But we don't want to be running copygc unnecessarily when the device still
267 * has plenty of free space - rather, we want copygc to smoothly run every so
268 * often and continually reduce the amount of fragmented space as the device
269 * fills up. So, we increase the threshold by half the current free space.
271 unsigned long bch2_copygc_wait_amount(struct bch_fs *c)
275 s64 wait = S64_MAX, fragmented_allowed, fragmented;
278 for_each_rw_member(ca, c, dev_idx) {
279 struct bch_dev_usage usage = bch2_dev_usage_read(ca);
281 fragmented_allowed = ((__dev_buckets_available(ca, usage, BCH_WATERMARK_stripe) *
282 ca->mi.bucket_size) >> 1);
285 for (i = 0; i < BCH_DATA_NR; i++)
286 if (data_type_movable(i))
287 fragmented += usage.d[i].fragmented;
289 wait = min(wait, max(0LL, fragmented_allowed - fragmented));
295 void bch2_copygc_wait_to_text(struct printbuf *out, struct bch_fs *c)
297 prt_printf(out, "Currently waiting for: ");
298 prt_human_readable_u64(out, max(0LL, c->copygc_wait -
299 atomic64_read(&c->io_clock[WRITE].now)) << 9);
302 prt_printf(out, "Currently waiting since: ");
303 prt_human_readable_u64(out, max(0LL,
304 atomic64_read(&c->io_clock[WRITE].now) -
305 c->copygc_wait_at) << 9);
308 prt_printf(out, "Currently calculated wait: ");
309 prt_human_readable_u64(out, bch2_copygc_wait_amount(c));
313 static int bch2_copygc_thread(void *arg)
315 struct bch_fs *c = arg;
316 struct moving_context ctxt;
317 struct bch_move_stats move_stats;
318 struct io_clock *clock = &c->io_clock[WRITE];
319 struct buckets_in_flight *buckets;
323 buckets = kzalloc(sizeof(struct buckets_in_flight), GFP_KERNEL);
326 ret = rhashtable_init(&buckets->table, &bch_move_bucket_params);
329 bch_err_msg(c, ret, "allocating copygc buckets in flight");
335 bch2_move_stats_init(&move_stats, "copygc");
336 bch2_moving_ctxt_init(&ctxt, c, NULL, &move_stats,
337 writepoint_ptr(&c->copygc_write_point),
340 while (!ret && !kthread_should_stop()) {
341 bool did_work = false;
343 bch2_trans_unlock_long(ctxt.trans);
346 if (!c->copy_gc_enabled) {
347 move_buckets_wait(&ctxt, buckets, true);
348 kthread_wait_freezable(c->copy_gc_enabled ||
349 kthread_should_stop());
352 if (unlikely(freezing(current))) {
353 move_buckets_wait(&ctxt, 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(&ctxt, 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(&ctxt, buckets, &did_work);
375 c->copygc_running = false;
377 wake_up(&c->copygc_running_wq);
379 if (!wait && !did_work) {
380 u64 min_member_capacity = bch2_min_rw_member_capacity(c);
382 if (min_member_capacity == U64_MAX)
383 min_member_capacity = 128 * 2048;
385 bch2_trans_unlock_long(ctxt.trans);
386 bch2_kthread_io_clock_wait(clock, last + (min_member_capacity >> 6),
387 MAX_SCHEDULE_TIMEOUT);
391 move_buckets_wait(&ctxt, buckets, true);
393 rhashtable_destroy(&buckets->table);
395 bch2_moving_ctxt_exit(&ctxt);
396 bch2_move_stats_exit(&move_stats, c);
401 void bch2_copygc_stop(struct bch_fs *c)
403 if (c->copygc_thread) {
404 kthread_stop(c->copygc_thread);
405 put_task_struct(c->copygc_thread);
407 c->copygc_thread = NULL;
410 int bch2_copygc_start(struct bch_fs *c)
412 struct task_struct *t;
415 if (c->copygc_thread)
418 if (c->opts.nochanges)
421 if (bch2_fs_init_fault("copygc_start"))
424 t = kthread_create(bch2_copygc_thread, c, "bch-copygc/%s", c->name);
425 ret = PTR_ERR_OR_ZERO(t);
427 bch_err_msg(c, ret, "creating copygc thread");
433 c->copygc_thread = t;
434 wake_up_process(c->copygc_thread);
439 void bch2_fs_copygc_init(struct bch_fs *c)
441 init_waitqueue_head(&c->copygc_running_wq);
442 c->copygc_running = false;