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