]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/movinggc.c
Update bcachefs sources to 84f132d569 bcachefs: fsck: Break walk_inode() up into...
[bcachefs-tools-debian] / libbcachefs / movinggc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Moving/copying garbage collector
4  *
5  * Copyright 2012 Google, Inc.
6  */
7
8 #include "bcachefs.h"
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"
14 #include "buckets.h"
15 #include "clock.h"
16 #include "disk_groups.h"
17 #include "errcode.h"
18 #include "error.h"
19 #include "extents.h"
20 #include "eytzinger.h"
21 #include "io.h"
22 #include "keylist.h"
23 #include "lru.h"
24 #include "move.h"
25 #include "movinggc.h"
26 #include "super-io.h"
27 #include "trace.h"
28
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>
36
37 struct buckets_in_flight {
38         struct rhashtable               table;
39         struct move_bucket_in_flight    *first;
40         struct move_bucket_in_flight    *last;
41         size_t                          nr;
42         size_t                          sectors;
43 };
44
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),
49 };
50
51 static struct move_bucket_in_flight *
52 move_bucket_in_flight_add(struct buckets_in_flight *list, struct move_bucket b)
53 {
54         struct move_bucket_in_flight *new = kzalloc(sizeof(*new), GFP_KERNEL);
55         int ret;
56
57         if (!new)
58                 return ERR_PTR(-ENOMEM);
59
60         new->bucket = b;
61
62         ret = rhashtable_lookup_insert_fast(&list->table, &new->hash,
63                                             bch_move_bucket_params);
64         if (ret) {
65                 kfree(new);
66                 return ERR_PTR(ret);
67         }
68
69         if (!list->first)
70                 list->first = new;
71         else
72                 list->last->next = new;
73
74         list->last = new;
75         list->nr++;
76         list->sectors += b.sectors;
77         return new;
78 }
79
80 static int bch2_bucket_is_movable(struct btree_trans *trans,
81                                   struct move_bucket *b, u64 time)
82 {
83         struct btree_iter iter;
84         struct bkey_s_c k;
85         struct bch_alloc_v4 _a;
86         const struct bch_alloc_v4 *a;
87         int ret;
88
89         if (bch2_bucket_is_open(trans->c,
90                                 b->k.bucket.inode,
91                                 b->k.bucket.offset))
92                 return 0;
93
94         k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc,
95                                b->k.bucket, BTREE_ITER_CACHED);
96         ret = bkey_err(k);
97         if (ret)
98                 return ret;
99
100         a = bch2_alloc_to_v4(k, &_a);
101         b->k.gen        = a->gen;
102         b->sectors      = a->dirty_sectors;
103
104         ret = data_type_movable(a->data_type) &&
105                 a->fragmentation_lru &&
106                 a->fragmentation_lru <= time;
107
108         bch2_trans_iter_exit(trans, &iter);
109         return ret;
110 }
111
112 static void move_buckets_wait(struct btree_trans *trans,
113                               struct moving_context *ctxt,
114                               struct buckets_in_flight *list,
115                               bool flush)
116 {
117         struct move_bucket_in_flight *i;
118         int ret;
119
120         while ((i = list->first)) {
121                 if (flush)
122                         move_ctxt_wait_event(ctxt, trans, !atomic_read(&i->count));
123
124                 if (atomic_read(&i->count))
125                         break;
126
127                 list->first = i->next;
128                 if (!list->first)
129                         list->last = NULL;
130
131                 list->nr--;
132                 list->sectors -= i->bucket.sectors;
133
134                 ret = rhashtable_remove_fast(&list->table, &i->hash,
135                                              bch_move_bucket_params);
136                 BUG_ON(ret);
137                 kfree(i);
138         }
139
140         bch2_trans_unlock(trans);
141 }
142
143 static bool bucket_in_flight(struct buckets_in_flight *list,
144                              struct move_bucket_key k)
145 {
146         return rhashtable_lookup_fast(&list->table, &k, bch_move_bucket_params);
147 }
148
149 typedef DARRAY(struct move_bucket) move_buckets;
150
151 static int bch2_copygc_get_buckets(struct btree_trans *trans,
152                         struct moving_context *ctxt,
153                         struct buckets_in_flight *buckets_in_flight,
154                         move_buckets *buckets)
155 {
156         struct bch_fs *c = trans->c;
157         struct btree_iter iter;
158         struct bkey_s_c k;
159         size_t nr_to_get = max(16UL, buckets_in_flight->nr / 4);
160         size_t saw = 0, in_flight = 0, not_movable = 0, sectors = 0;
161         int ret;
162
163         move_buckets_wait(trans, ctxt, buckets_in_flight, false);
164
165         ret = bch2_btree_write_buffer_flush(trans);
166         if (bch2_fs_fatal_err_on(ret, c, "%s: error %s from bch2_btree_write_buffer_flush()",
167                                  __func__, bch2_err_str(ret)))
168                 return ret;
169
170         ret = for_each_btree_key2_upto(trans, iter, BTREE_ID_lru,
171                                   lru_pos(BCH_LRU_FRAGMENTATION_START, 0, 0),
172                                   lru_pos(BCH_LRU_FRAGMENTATION_START, U64_MAX, LRU_TIME_MAX),
173                                   0, k, ({
174                 struct move_bucket b = { .k.bucket = u64_to_bucket(k.k->p.offset) };
175                 int ret = 0;
176
177                 saw++;
178
179                 if (!bch2_bucket_is_movable(trans, &b, lru_pos_time(k.k->p)))
180                         not_movable++;
181                 else if (bucket_in_flight(buckets_in_flight, b.k))
182                         in_flight++;
183                 else {
184                         ret = darray_push(buckets, b) ?: buckets->nr >= nr_to_get;
185                         if (ret >= 0)
186                                 sectors += b.sectors;
187                 }
188                 ret;
189         }));
190
191         pr_debug("have: %zu (%zu) saw %zu in flight %zu not movable %zu got %zu (%zu)/%zu buckets ret %i",
192                  buckets_in_flight->nr, buckets_in_flight->sectors,
193                  saw, in_flight, not_movable, buckets->nr, sectors, nr_to_get, ret);
194
195         return ret < 0 ? ret : 0;
196 }
197
198 noinline
199 static int bch2_copygc(struct btree_trans *trans,
200                        struct moving_context *ctxt,
201                        struct buckets_in_flight *buckets_in_flight)
202 {
203         struct bch_fs *c = trans->c;
204         struct data_update_opts data_opts = {
205                 .btree_insert_flags = BTREE_INSERT_USE_RESERVE|JOURNAL_WATERMARK_copygc,
206         };
207         move_buckets buckets = { 0 };
208         struct move_bucket_in_flight *f;
209         struct move_bucket *i;
210         u64 moved = atomic64_read(&ctxt->stats->sectors_moved);
211         int ret = 0;
212
213         ret = bch2_copygc_get_buckets(trans, ctxt, buckets_in_flight, &buckets);
214         if (ret)
215                 goto err;
216
217         darray_for_each(buckets, i) {
218                 if (unlikely(freezing(current)))
219                         break;
220
221                 f = move_bucket_in_flight_add(buckets_in_flight, *i);
222                 ret = PTR_ERR_OR_ZERO(f);
223                 if (ret == -EEXIST) /* rare race: copygc_get_buckets returned same bucket more than once */
224                         continue;
225                 if (ret == -ENOMEM) { /* flush IO, continue later */
226                         ret = 0;
227                         break;
228                 }
229
230                 ret = __bch2_evacuate_bucket(trans, ctxt, f, f->bucket.k.bucket,
231                                              f->bucket.k.gen, data_opts);
232                 if (ret)
233                         goto err;
234         }
235 err:
236         darray_exit(&buckets);
237
238         /* no entries in LRU btree found, or got to end: */
239         if (bch2_err_matches(ret, ENOENT))
240                 ret = 0;
241
242         if (ret < 0 && !bch2_err_matches(ret, EROFS))
243                 bch_err(c, "error from bch2_move_data() in copygc: %s", bch2_err_str(ret));
244
245         moved = atomic64_read(&ctxt->stats->sectors_moved) - moved;
246         trace_and_count(c, copygc, c, moved, 0, 0, 0);
247         return ret;
248 }
249
250 /*
251  * Copygc runs when the amount of fragmented data is above some arbitrary
252  * threshold:
253  *
254  * The threshold at the limit - when the device is full - is the amount of space
255  * we reserved in bch2_recalc_capacity; we can't have more than that amount of
256  * disk space stranded due to fragmentation and store everything we have
257  * promised to store.
258  *
259  * But we don't want to be running copygc unnecessarily when the device still
260  * has plenty of free space - rather, we want copygc to smoothly run every so
261  * often and continually reduce the amount of fragmented space as the device
262  * fills up. So, we increase the threshold by half the current free space.
263  */
264 unsigned long bch2_copygc_wait_amount(struct bch_fs *c)
265 {
266         struct bch_dev *ca;
267         unsigned dev_idx;
268         s64 wait = S64_MAX, fragmented_allowed, fragmented;
269         unsigned i;
270
271         for_each_rw_member(ca, c, dev_idx) {
272                 struct bch_dev_usage usage = bch2_dev_usage_read(ca);
273
274                 fragmented_allowed = ((__dev_buckets_available(ca, usage, BCH_WATERMARK_stripe) *
275                                        ca->mi.bucket_size) >> 1);
276                 fragmented = 0;
277
278                 for (i = 0; i < BCH_DATA_NR; i++)
279                         if (data_type_movable(i))
280                                 fragmented += usage.d[i].fragmented;
281
282                 wait = min(wait, max(0LL, fragmented_allowed - fragmented));
283         }
284
285         return wait;
286 }
287
288 void bch2_copygc_wait_to_text(struct printbuf *out, struct bch_fs *c)
289 {
290         prt_printf(out, "Currently waiting for:     ");
291         prt_human_readable_u64(out, max(0LL, c->copygc_wait -
292                                         atomic64_read(&c->io_clock[WRITE].now)) << 9);
293         prt_newline(out);
294
295         prt_printf(out, "Currently waiting since:   ");
296         prt_human_readable_u64(out, max(0LL,
297                                         atomic64_read(&c->io_clock[WRITE].now) -
298                                         c->copygc_wait_at) << 9);
299         prt_newline(out);
300
301         prt_printf(out, "Currently calculated wait: ");
302         prt_human_readable_u64(out, bch2_copygc_wait_amount(c));
303         prt_newline(out);
304 }
305
306 static int bch2_copygc_thread(void *arg)
307 {
308         struct bch_fs *c = arg;
309         struct btree_trans trans;
310         struct moving_context ctxt;
311         struct bch_move_stats move_stats;
312         struct io_clock *clock = &c->io_clock[WRITE];
313         struct buckets_in_flight move_buckets;
314         u64 last, wait;
315         int ret = 0;
316
317         memset(&move_buckets, 0, sizeof(move_buckets));
318
319         ret = rhashtable_init(&move_buckets.table, &bch_move_bucket_params);
320         if (ret) {
321                 bch_err(c, "error allocating copygc buckets in flight: %s",
322                         bch2_err_str(ret));
323                 return ret;
324         }
325
326         set_freezable();
327         bch2_trans_init(&trans, c, 0, 0);
328
329         bch2_move_stats_init(&move_stats, "copygc");
330         bch2_moving_ctxt_init(&ctxt, c, NULL, &move_stats,
331                               writepoint_ptr(&c->copygc_write_point),
332                               false);
333
334         while (!ret && !kthread_should_stop()) {
335                 bch2_trans_unlock(&trans);
336                 cond_resched();
337
338                 if (!c->copy_gc_enabled) {
339                         move_buckets_wait(&trans, &ctxt, &move_buckets, true);
340                         kthread_wait_freezable(c->copy_gc_enabled);
341                 }
342
343                 if (unlikely(freezing(current))) {
344                         move_buckets_wait(&trans, &ctxt, &move_buckets, true);
345                         __refrigerator(false);
346                         continue;
347                 }
348
349                 last = atomic64_read(&clock->now);
350                 wait = bch2_copygc_wait_amount(c);
351
352                 if (wait > clock->max_slop) {
353                         c->copygc_wait_at = last;
354                         c->copygc_wait = last + wait;
355                         move_buckets_wait(&trans, &ctxt, &move_buckets, true);
356                         trace_and_count(c, copygc_wait, c, wait, last + wait);
357                         bch2_kthread_io_clock_wait(clock, last + wait,
358                                         MAX_SCHEDULE_TIMEOUT);
359                         continue;
360                 }
361
362                 c->copygc_wait = 0;
363
364                 c->copygc_running = true;
365                 ret = bch2_copygc(&trans, &ctxt, &move_buckets);
366                 c->copygc_running = false;
367
368                 wake_up(&c->copygc_running_wq);
369         }
370
371         move_buckets_wait(&trans, &ctxt, &move_buckets, true);
372         rhashtable_destroy(&move_buckets.table);
373         bch2_trans_exit(&trans);
374         bch2_moving_ctxt_exit(&ctxt);
375
376         return 0;
377 }
378
379 void bch2_copygc_stop(struct bch_fs *c)
380 {
381         if (c->copygc_thread) {
382                 kthread_stop(c->copygc_thread);
383                 put_task_struct(c->copygc_thread);
384         }
385         c->copygc_thread = NULL;
386 }
387
388 int bch2_copygc_start(struct bch_fs *c)
389 {
390         struct task_struct *t;
391         int ret;
392
393         if (c->copygc_thread)
394                 return 0;
395
396         if (c->opts.nochanges)
397                 return 0;
398
399         if (bch2_fs_init_fault("copygc_start"))
400                 return -ENOMEM;
401
402         t = kthread_create(bch2_copygc_thread, c, "bch-copygc/%s", c->name);
403         ret = PTR_ERR_OR_ZERO(t);
404         if (ret) {
405                 bch_err(c, "error creating copygc thread: %s", bch2_err_str(ret));
406                 return ret;
407         }
408
409         get_task_struct(t);
410
411         c->copygc_thread = t;
412         wake_up_process(c->copygc_thread);
413
414         return 0;
415 }
416
417 void bch2_fs_copygc_init(struct bch_fs *c)
418 {
419         init_waitqueue_head(&c->copygc_running_wq);
420         c->copygc_running = false;
421 }