]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/movinggc.c
1a64643313cf188c4d197a3cfd11988250c28bd0
[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/freezer.h>
30 #include <linux/kthread.h>
31 #include <linux/math64.h>
32 #include <linux/sched/task.h>
33 #include <linux/sort.h>
34 #include <linux/wait.h>
35
36 static int bch2_bucket_is_movable(struct btree_trans *trans,
37                                   struct bpos bucket, u64 time, u8 *gen)
38 {
39         struct btree_iter iter;
40         struct bkey_s_c k;
41         struct bch_alloc_v4 _a;
42         const struct bch_alloc_v4 *a;
43         int ret;
44
45         if (bch2_bucket_is_open(trans->c, bucket.inode, bucket.offset))
46                 return 0;
47
48         bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, bucket, 0);
49         k = bch2_btree_iter_peek_slot(&iter);
50         ret = bkey_err(k);
51         bch2_trans_iter_exit(trans, &iter);
52
53         if (ret)
54                 return ret;
55
56         a = bch2_alloc_to_v4(k, &_a);
57         *gen = a->gen;
58         ret = (a->data_type == BCH_DATA_btree ||
59                a->data_type == BCH_DATA_user) &&
60                 a->fragmentation_lru &&
61                 a->fragmentation_lru <= time;
62
63         if (ret) {
64                 struct printbuf buf = PRINTBUF;
65
66                 bch2_bkey_val_to_text(&buf, trans->c, k);
67                 pr_debug("%s", buf.buf);
68                 printbuf_exit(&buf);
69         }
70
71         return ret;
72 }
73
74 static int bch2_copygc_next_bucket(struct btree_trans *trans,
75                                    struct bpos *bucket, u8 *gen, struct bpos *pos)
76 {
77         struct btree_iter iter;
78         struct bkey_s_c k;
79         int ret;
80
81         ret = for_each_btree_key2_upto(trans, iter, BTREE_ID_lru,
82                                   bpos_max(*pos, lru_pos(BCH_LRU_FRAGMENTATION_START, 0, 0)),
83                                   lru_pos(BCH_LRU_FRAGMENTATION_START, U64_MAX, LRU_TIME_MAX),
84                                   0, k, ({
85                 *bucket = u64_to_bucket(k.k->p.offset);
86
87                 bch2_bucket_is_movable(trans, *bucket, lru_pos_time(k.k->p), gen);
88         }));
89
90         *pos = iter.pos;
91         if (ret < 0)
92                 return ret;
93         return ret ? 0 : -ENOENT;
94 }
95
96 static int bch2_copygc(struct bch_fs *c)
97 {
98         struct bch_move_stats move_stats;
99         struct btree_trans trans;
100         struct moving_context ctxt;
101         struct data_update_opts data_opts = {
102                 .btree_insert_flags = BTREE_INSERT_USE_RESERVE|JOURNAL_WATERMARK_copygc,
103         };
104         struct bpos bucket;
105         struct bpos pos;
106         u8 gen = 0;
107         unsigned nr_evacuated;
108         int ret = 0;
109
110         bch2_move_stats_init(&move_stats, "copygc");
111         bch2_moving_ctxt_init(&ctxt, c, NULL, &move_stats,
112                               writepoint_ptr(&c->copygc_write_point),
113                               false);
114         bch2_trans_init(&trans, c, 0, 0);
115
116         ret = bch2_btree_write_buffer_flush(&trans);
117         BUG_ON(ret);
118
119         for (nr_evacuated = 0, pos = POS_MIN;
120              nr_evacuated < 32 && !ret;
121              nr_evacuated++, pos = bpos_nosnap_successor(pos)) {
122                 ret = bch2_copygc_next_bucket(&trans, &bucket, &gen, &pos) ?:
123                         __bch2_evacuate_bucket(&trans, &ctxt, bucket, gen, data_opts);
124                 if (bkey_eq(pos, POS_MAX))
125                         break;
126         }
127
128         bch2_trans_exit(&trans);
129         bch2_moving_ctxt_exit(&ctxt);
130
131         /* no entries in LRU btree found, or got to end: */
132         if (ret == -ENOENT)
133                 ret = 0;
134
135         if (ret < 0 && !bch2_err_matches(ret, EROFS))
136                 bch_err(c, "error from bch2_move_data() in copygc: %s", bch2_err_str(ret));
137
138         trace_and_count(c, copygc, c, atomic64_read(&move_stats.sectors_moved), 0, 0, 0);
139         return ret;
140 }
141
142 /*
143  * Copygc runs when the amount of fragmented data is above some arbitrary
144  * threshold:
145  *
146  * The threshold at the limit - when the device is full - is the amount of space
147  * we reserved in bch2_recalc_capacity; we can't have more than that amount of
148  * disk space stranded due to fragmentation and store everything we have
149  * promised to store.
150  *
151  * But we don't want to be running copygc unnecessarily when the device still
152  * has plenty of free space - rather, we want copygc to smoothly run every so
153  * often and continually reduce the amount of fragmented space as the device
154  * fills up. So, we increase the threshold by half the current free space.
155  */
156 unsigned long bch2_copygc_wait_amount(struct bch_fs *c)
157 {
158         struct bch_dev *ca;
159         unsigned dev_idx;
160         s64 wait = S64_MAX, fragmented_allowed, fragmented;
161
162         for_each_rw_member(ca, c, dev_idx) {
163                 struct bch_dev_usage usage = bch2_dev_usage_read(ca);
164
165                 fragmented_allowed = ((__dev_buckets_available(ca, usage, RESERVE_none) *
166                                        ca->mi.bucket_size) >> 1);
167                 fragmented = usage.d[BCH_DATA_user].fragmented;
168
169                 wait = min(wait, max(0LL, fragmented_allowed - fragmented));
170         }
171
172         return wait;
173 }
174
175 static int bch2_copygc_thread(void *arg)
176 {
177         struct bch_fs *c = arg;
178         struct io_clock *clock = &c->io_clock[WRITE];
179         u64 last, wait;
180         int ret = 0;
181
182         set_freezable();
183
184         while (!ret && !kthread_should_stop()) {
185                 cond_resched();
186
187                 if (kthread_wait_freezable(c->copy_gc_enabled))
188                         break;
189
190                 last = atomic64_read(&clock->now);
191                 wait = bch2_copygc_wait_amount(c);
192
193                 if (wait > clock->max_slop) {
194                         trace_and_count(c, copygc_wait, c, wait, last + wait);
195                         c->copygc_wait = last + wait;
196                         bch2_kthread_io_clock_wait(clock, last + wait,
197                                         MAX_SCHEDULE_TIMEOUT);
198                         continue;
199                 }
200
201                 c->copygc_wait = 0;
202
203                 c->copygc_running = true;
204                 ret = bch2_copygc(c);
205                 c->copygc_running = false;
206
207                 wake_up(&c->copygc_running_wq);
208         }
209
210         return 0;
211 }
212
213 void bch2_copygc_stop(struct bch_fs *c)
214 {
215         if (c->copygc_thread) {
216                 kthread_stop(c->copygc_thread);
217                 put_task_struct(c->copygc_thread);
218         }
219         c->copygc_thread = NULL;
220 }
221
222 int bch2_copygc_start(struct bch_fs *c)
223 {
224         struct task_struct *t;
225         int ret;
226
227         if (c->copygc_thread)
228                 return 0;
229
230         if (c->opts.nochanges)
231                 return 0;
232
233         if (bch2_fs_init_fault("copygc_start"))
234                 return -ENOMEM;
235
236         t = kthread_create(bch2_copygc_thread, c, "bch-copygc/%s", c->name);
237         ret = PTR_ERR_OR_ZERO(t);
238         if (ret) {
239                 bch_err(c, "error creating copygc thread: %s", bch2_err_str(ret));
240                 return ret;
241         }
242
243         get_task_struct(t);
244
245         c->copygc_thread = t;
246         wake_up_process(c->copygc_thread);
247
248         return 0;
249 }
250
251 void bch2_fs_copygc_init(struct bch_fs *c)
252 {
253         init_waitqueue_head(&c->copygc_running_wq);
254         c->copygc_running = false;
255 }