]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/movinggc.c
Update bcachefs sources to f7670cba39 bcachefs: Fix for building in userspace
[bcachefs-tools-debian] / libbcachefs / movinggc.c
1 /*
2  * Moving/copying garbage collector
3  *
4  * Copyright 2012 Google, Inc.
5  */
6
7 #include "bcachefs.h"
8 #include "alloc_foreground.h"
9 #include "btree_iter.h"
10 #include "btree_update.h"
11 #include "buckets.h"
12 #include "clock.h"
13 #include "disk_groups.h"
14 #include "extents.h"
15 #include "eytzinger.h"
16 #include "io.h"
17 #include "keylist.h"
18 #include "move.h"
19 #include "movinggc.h"
20 #include "super-io.h"
21
22 #include <trace/events/bcachefs.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/sort.h>
28 #include <linux/wait.h>
29
30 /*
31  * We can't use the entire copygc reserve in one iteration of copygc: we may
32  * need the buckets we're freeing up to go back into the copygc reserve to make
33  * forward progress, but if the copygc reserve is full they'll be available for
34  * any allocation - and it's possible that in a given iteration, we free up most
35  * of the buckets we're going to free before we allocate most of the buckets
36  * we're going to allocate.
37  *
38  * If we only use half of the reserve per iteration, then in steady state we'll
39  * always have room in the reserve for the buckets we're going to need in the
40  * next iteration:
41  */
42 #define COPYGC_BUCKETS_PER_ITER(ca)                                     \
43         ((ca)->free[RESERVE_MOVINGGC].size / 2)
44
45 /*
46  * Max sectors to move per iteration: Have to take into account internal
47  * fragmentation from the multiple write points for each generation:
48  */
49 #define COPYGC_SECTORS_PER_ITER(ca)                                     \
50         ((ca)->mi.bucket_size * COPYGC_BUCKETS_PER_ITER(ca))
51
52 static inline int sectors_used_cmp(copygc_heap *heap,
53                                    struct copygc_heap_entry l,
54                                    struct copygc_heap_entry r)
55 {
56         return (l.sectors > r.sectors) - (l.sectors < r.sectors);
57 }
58
59 static int bucket_offset_cmp(const void *_l, const void *_r, size_t size)
60 {
61         const struct copygc_heap_entry *l = _l;
62         const struct copygc_heap_entry *r = _r;
63
64         return (l->offset > r->offset) - (l->offset < r->offset);
65 }
66
67 static bool __copygc_pred(struct bch_dev *ca,
68                           struct bkey_s_c k)
69 {
70         copygc_heap *h = &ca->copygc_heap;
71
72         switch (k.k->type) {
73         case KEY_TYPE_extent: {
74                 struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
75                 const struct bch_extent_ptr *ptr =
76                         bch2_extent_has_device(e, ca->dev_idx);
77
78                 if (ptr) {
79                         struct copygc_heap_entry search = { .offset = ptr->offset };
80
81                         ssize_t i = eytzinger0_find_le(h->data, h->used,
82                                                        sizeof(h->data[0]),
83                                                        bucket_offset_cmp, &search);
84
85                         return (i >= 0 &&
86                                 ptr->offset < h->data[i].offset + ca->mi.bucket_size &&
87                                 ptr->gen == h->data[i].gen);
88                 }
89                 break;
90         }
91         }
92
93         return false;
94 }
95
96 static enum data_cmd copygc_pred(struct bch_fs *c, void *arg,
97                                  struct bkey_s_c k,
98                                  struct bch_io_opts *io_opts,
99                                  struct data_opts *data_opts)
100 {
101         struct bch_dev *ca = arg;
102
103         if (!__copygc_pred(ca, k))
104                 return DATA_SKIP;
105
106         data_opts->target               = dev_to_target(ca->dev_idx);
107         data_opts->btree_insert_flags   = BTREE_INSERT_USE_RESERVE;
108         data_opts->rewrite_dev          = ca->dev_idx;
109         return DATA_REWRITE;
110 }
111
112 static bool have_copygc_reserve(struct bch_dev *ca)
113 {
114         bool ret;
115
116         spin_lock(&ca->freelist_lock);
117         ret = fifo_full(&ca->free[RESERVE_MOVINGGC]) ||
118                 ca->allocator_blocked;
119         spin_unlock(&ca->freelist_lock);
120
121         return ret;
122 }
123
124 static void bch2_copygc(struct bch_fs *c, struct bch_dev *ca)
125 {
126         copygc_heap *h = &ca->copygc_heap;
127         struct copygc_heap_entry e, *i;
128         struct bucket_array *buckets;
129         struct bch_move_stats move_stats;
130         u64 sectors_to_move = 0, sectors_not_moved = 0;
131         u64 buckets_to_move, buckets_not_moved = 0;
132         size_t b;
133         int ret;
134
135         memset(&move_stats, 0, sizeof(move_stats));
136         closure_wait_event(&c->freelist_wait, have_copygc_reserve(ca));
137
138         /*
139          * Find buckets with lowest sector counts, skipping completely
140          * empty buckets, by building a maxheap sorted by sector count,
141          * and repeatedly replacing the maximum element until all
142          * buckets have been visited.
143          */
144         h->used = 0;
145
146         /*
147          * We need bucket marks to be up to date - gc can't be recalculating
148          * them:
149          */
150         down_read(&c->gc_lock);
151         down_read(&ca->bucket_lock);
152         buckets = bucket_array(ca);
153
154         for (b = buckets->first_bucket; b < buckets->nbuckets; b++) {
155                 struct bucket_mark m = READ_ONCE(buckets->b[b].mark);
156                 struct copygc_heap_entry e;
157
158                 if (m.owned_by_allocator ||
159                     m.data_type != BCH_DATA_USER ||
160                     !bucket_sectors_used(m) ||
161                     bucket_sectors_used(m) >= ca->mi.bucket_size)
162                         continue;
163
164                 e = (struct copygc_heap_entry) {
165                         .gen            = m.gen,
166                         .sectors        = bucket_sectors_used(m),
167                         .offset         = bucket_to_sector(ca, b),
168                 };
169                 heap_add_or_replace(h, e, -sectors_used_cmp, NULL);
170         }
171         up_read(&ca->bucket_lock);
172         up_read(&c->gc_lock);
173
174         for (i = h->data; i < h->data + h->used; i++)
175                 sectors_to_move += i->sectors;
176
177         while (sectors_to_move > COPYGC_SECTORS_PER_ITER(ca)) {
178                 BUG_ON(!heap_pop(h, e, -sectors_used_cmp, NULL));
179                 sectors_to_move -= e.sectors;
180         }
181
182         buckets_to_move = h->used;
183
184         if (!buckets_to_move)
185                 return;
186
187         eytzinger0_sort(h->data, h->used,
188                         sizeof(h->data[0]),
189                         bucket_offset_cmp, NULL);
190
191         ret = bch2_move_data(c, &ca->copygc_pd.rate,
192                              writepoint_ptr(&ca->copygc_write_point),
193                              POS_MIN, POS_MAX,
194                              copygc_pred, ca,
195                              &move_stats);
196
197         down_read(&ca->bucket_lock);
198         buckets = bucket_array(ca);
199         for (i = h->data; i < h->data + h->used; i++) {
200                 size_t b = sector_to_bucket(ca, i->offset);
201                 struct bucket_mark m = READ_ONCE(buckets->b[b].mark);
202
203                 if (i->gen == m.gen && bucket_sectors_used(m)) {
204                         sectors_not_moved += bucket_sectors_used(m);
205                         buckets_not_moved++;
206                 }
207         }
208         up_read(&ca->bucket_lock);
209
210         if (sectors_not_moved && !ret)
211                 bch_warn(c, "copygc finished but %llu/%llu sectors, %llu/%llu buckets not moved",
212                          sectors_not_moved, sectors_to_move,
213                          buckets_not_moved, buckets_to_move);
214
215         trace_copygc(ca,
216                      atomic64_read(&move_stats.sectors_moved), sectors_not_moved,
217                      buckets_to_move, buckets_not_moved);
218 }
219
220 static int bch2_copygc_thread(void *arg)
221 {
222         struct bch_dev *ca = arg;
223         struct bch_fs *c = ca->fs;
224         struct io_clock *clock = &c->io_clock[WRITE];
225         struct bch_dev_usage usage;
226         unsigned long last;
227         u64 available, fragmented, reserve, next;
228
229         set_freezable();
230
231         while (!kthread_should_stop()) {
232                 if (kthread_wait_freezable(c->copy_gc_enabled))
233                         break;
234
235                 last = atomic_long_read(&clock->now);
236
237                 reserve = ca->copygc_threshold;
238
239                 usage = bch2_dev_usage_read(c, ca);
240
241                 available = __dev_buckets_available(ca, usage) *
242                         ca->mi.bucket_size;
243                 if (available > reserve) {
244                         next = last + available - reserve;
245                         bch2_kthread_io_clock_wait(clock, next,
246                                         MAX_SCHEDULE_TIMEOUT);
247                         continue;
248                 }
249
250                 /*
251                  * don't start copygc until there's more than half the copygc
252                  * reserve of fragmented space:
253                  */
254                 fragmented = usage.sectors_fragmented;
255                 if (fragmented < reserve) {
256                         next = last + reserve - fragmented;
257                         bch2_kthread_io_clock_wait(clock, next,
258                                         MAX_SCHEDULE_TIMEOUT);
259                         continue;
260                 }
261
262                 bch2_copygc(c, ca);
263         }
264
265         return 0;
266 }
267
268 void bch2_copygc_stop(struct bch_dev *ca)
269 {
270         ca->copygc_pd.rate.rate = UINT_MAX;
271         bch2_ratelimit_reset(&ca->copygc_pd.rate);
272
273         if (ca->copygc_thread) {
274                 kthread_stop(ca->copygc_thread);
275                 put_task_struct(ca->copygc_thread);
276         }
277         ca->copygc_thread = NULL;
278 }
279
280 int bch2_copygc_start(struct bch_fs *c, struct bch_dev *ca)
281 {
282         struct task_struct *t;
283
284         BUG_ON(ca->copygc_thread);
285
286         if (c->opts.nochanges)
287                 return 0;
288
289         if (bch2_fs_init_fault("copygc_start"))
290                 return -ENOMEM;
291
292         t = kthread_create(bch2_copygc_thread, ca,
293                            "bch_copygc[%s]", ca->name);
294         if (IS_ERR(t))
295                 return PTR_ERR(t);
296
297         get_task_struct(t);
298
299         ca->copygc_thread = t;
300         wake_up_process(ca->copygc_thread);
301
302         return 0;
303 }
304
305 void bch2_dev_copygc_init(struct bch_dev *ca)
306 {
307         bch2_pd_controller_init(&ca->copygc_pd);
308         ca->copygc_pd.d_term = 0;
309 }