2017-01-08 12:13:18 +03:00
|
|
|
/*
|
|
|
|
* Moving/copying garbage collector
|
|
|
|
*
|
|
|
|
* Copyright 2012 Google, Inc.
|
|
|
|
*/
|
|
|
|
|
2017-03-20 02:56:34 +03:00
|
|
|
#include "bcachefs.h"
|
2017-01-08 12:13:18 +03:00
|
|
|
#include "btree_iter.h"
|
2017-12-14 00:01:18 +03:00
|
|
|
#include "btree_update.h"
|
2017-01-08 12:13:18 +03:00
|
|
|
#include "buckets.h"
|
|
|
|
#include "clock.h"
|
|
|
|
#include "extents.h"
|
2017-04-24 08:56:57 +03:00
|
|
|
#include "eytzinger.h"
|
2017-01-08 12:13:18 +03:00
|
|
|
#include "io.h"
|
|
|
|
#include "keylist.h"
|
|
|
|
#include "move.h"
|
|
|
|
#include "movinggc.h"
|
2017-11-22 08:42:55 +03:00
|
|
|
#include "super-io.h"
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-03-20 02:56:34 +03:00
|
|
|
#include <trace/events/bcachefs.h>
|
2017-01-08 12:13:18 +03:00
|
|
|
#include <linux/freezer.h>
|
|
|
|
#include <linux/kthread.h>
|
2017-03-20 08:39:19 +03:00
|
|
|
#include <linux/math64.h>
|
2017-04-24 08:56:57 +03:00
|
|
|
#include <linux/sort.h>
|
2017-01-08 12:13:18 +03:00
|
|
|
#include <linux/wait.h>
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
/*
|
|
|
|
* We can't use the entire copygc reserve in one iteration of copygc: we may
|
|
|
|
* need the buckets we're freeing up to go back into the copygc reserve to make
|
|
|
|
* forward progress, but if the copygc reserve is full they'll be available for
|
|
|
|
* any allocation - and it's possible that in a given iteration, we free up most
|
|
|
|
* of the buckets we're going to free before we allocate most of the buckets
|
|
|
|
* we're going to allocate.
|
|
|
|
*
|
|
|
|
* If we only use half of the reserve per iteration, then in steady state we'll
|
|
|
|
* always have room in the reserve for the buckets we're going to need in the
|
|
|
|
* next iteration:
|
|
|
|
*/
|
|
|
|
#define COPYGC_BUCKETS_PER_ITER(ca) \
|
|
|
|
((ca)->free[RESERVE_MOVINGGC].size / 2)
|
2017-04-24 08:56:57 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
/*
|
|
|
|
* Max sectors to move per iteration: Have to take into account internal
|
|
|
|
* fragmentation from the multiple write points for each generation:
|
|
|
|
*/
|
|
|
|
#define COPYGC_SECTORS_PER_ITER(ca) \
|
|
|
|
((ca)->mi.bucket_size * COPYGC_BUCKETS_PER_ITER(ca))
|
2017-04-24 08:56:57 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
static inline int sectors_used_cmp(copygc_heap *heap,
|
|
|
|
struct copygc_heap_entry l,
|
|
|
|
struct copygc_heap_entry r)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
2017-12-14 00:01:18 +03:00
|
|
|
return bucket_sectors_used(l.mark) - bucket_sectors_used(r.mark);
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
static int bucket_offset_cmp(const void *_l, const void *_r, size_t size)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
2017-12-14 00:01:18 +03:00
|
|
|
const struct copygc_heap_entry *l = _l;
|
|
|
|
const struct copygc_heap_entry *r = _r;
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
return (l->offset > r->offset) - (l->offset < r->offset);
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
static bool copygc_pred(void *arg, struct bkey_s_c_extent e)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
2017-12-14 00:01:18 +03:00
|
|
|
struct bch_dev *ca = arg;
|
|
|
|
copygc_heap *h = &ca->copygc_heap;
|
|
|
|
const struct bch_extent_ptr *ptr =
|
|
|
|
bch2_extent_has_device(e, ca->dev_idx);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
if (ptr) {
|
|
|
|
struct copygc_heap_entry search = { .offset = ptr->offset };
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
size_t i = eytzinger0_find_le(h->data, h->used,
|
|
|
|
sizeof(h->data[0]),
|
|
|
|
bucket_offset_cmp, &search);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
return (i >= 0 &&
|
|
|
|
ptr->offset < h->data[i].offset + ca->mi.bucket_size &&
|
|
|
|
ptr->gen == h->data[i].mark.gen);
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
return false;
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
2017-03-11 00:40:01 +03:00
|
|
|
static bool have_copygc_reserve(struct bch_dev *ca)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
spin_lock(&ca->freelist_lock);
|
|
|
|
ret = fifo_used(&ca->free[RESERVE_MOVINGGC]) >=
|
|
|
|
COPYGC_BUCKETS_PER_ITER(ca);
|
|
|
|
spin_unlock(&ca->freelist_lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
static void bch2_copygc(struct bch_fs *c, struct bch_dev *ca)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
2017-12-14 00:01:18 +03:00
|
|
|
copygc_heap *h = &ca->copygc_heap;
|
|
|
|
struct copygc_heap_entry e, *i;
|
2017-12-28 04:32:40 +03:00
|
|
|
struct bucket_array *buckets;
|
2018-01-11 14:41:59 +03:00
|
|
|
struct bch_move_stats move_stats;
|
2017-12-14 00:01:18 +03:00
|
|
|
u64 sectors_to_move = 0, sectors_not_moved = 0;
|
|
|
|
u64 buckets_to_move, buckets_not_moved = 0;
|
2017-12-28 04:32:40 +03:00
|
|
|
size_t b;
|
2017-12-14 00:01:18 +03:00
|
|
|
int ret;
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
closure_wait_event(&c->freelist_wait, have_copygc_reserve(ca));
|
2017-01-08 12:13:18 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find buckets with lowest sector counts, skipping completely
|
|
|
|
* empty buckets, by building a maxheap sorted by sector count,
|
|
|
|
* and repeatedly replacing the maximum element until all
|
|
|
|
* buckets have been visited.
|
|
|
|
*/
|
2017-12-28 04:32:40 +03:00
|
|
|
h->used = 0;
|
2017-01-08 12:13:18 +03:00
|
|
|
|
|
|
|
/*
|
2017-04-24 08:56:57 +03:00
|
|
|
* We need bucket marks to be up to date - gc can't be recalculating
|
|
|
|
* them:
|
2017-01-08 12:13:18 +03:00
|
|
|
*/
|
|
|
|
down_read(&c->gc_lock);
|
2017-12-28 04:32:40 +03:00
|
|
|
down_read(&ca->bucket_lock);
|
|
|
|
buckets = bucket_array(ca);
|
|
|
|
|
|
|
|
for (b = buckets->first_bucket; b < buckets->nbuckets; b++) {
|
|
|
|
struct bucket_mark m = READ_ONCE(buckets->b[b].mark);
|
2017-12-14 00:01:18 +03:00
|
|
|
struct copygc_heap_entry e;
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-04-24 08:56:57 +03:00
|
|
|
if (m.owned_by_allocator ||
|
2017-12-23 04:37:52 +03:00
|
|
|
m.data_type != BCH_DATA_USER ||
|
2017-12-14 00:01:18 +03:00
|
|
|
!bucket_sectors_used(m) ||
|
|
|
|
bucket_sectors_used(m) >= ca->mi.bucket_size)
|
2017-01-08 12:13:18 +03:00
|
|
|
continue;
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
e = (struct copygc_heap_entry) {
|
2017-12-28 04:32:40 +03:00
|
|
|
.offset = bucket_to_sector(ca, b),
|
2017-12-14 00:01:18 +03:00
|
|
|
.mark = m
|
|
|
|
};
|
|
|
|
heap_add_or_replace(h, e, -sectors_used_cmp);
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
2017-12-28 04:32:40 +03:00
|
|
|
up_read(&ca->bucket_lock);
|
2017-04-24 08:56:57 +03:00
|
|
|
up_read(&c->gc_lock);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
for (i = h->data; i < h->data + h->used; i++)
|
2017-04-24 08:56:57 +03:00
|
|
|
sectors_to_move += bucket_sectors_used(i->mark);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
|
|
|
while (sectors_to_move > COPYGC_SECTORS_PER_ITER(ca)) {
|
2017-12-14 00:01:18 +03:00
|
|
|
BUG_ON(!heap_pop(h, e, -sectors_used_cmp));
|
2017-04-24 08:56:57 +03:00
|
|
|
sectors_to_move -= bucket_sectors_used(e.mark);
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
buckets_to_move = h->used;
|
|
|
|
|
|
|
|
if (!buckets_to_move)
|
|
|
|
return;
|
|
|
|
|
|
|
|
eytzinger0_sort(h->data, h->used,
|
|
|
|
sizeof(h->data[0]),
|
|
|
|
bucket_offset_cmp, NULL);
|
|
|
|
|
|
|
|
ret = bch2_move_data(c, &ca->copygc_pd.rate,
|
|
|
|
SECTORS_IN_FLIGHT_PER_DEVICE,
|
|
|
|
&ca->self,
|
|
|
|
writepoint_ptr(&ca->copygc_write_point),
|
|
|
|
BTREE_INSERT_USE_RESERVE,
|
|
|
|
ca->dev_idx,
|
|
|
|
copygc_pred, ca,
|
2018-01-11 14:41:59 +03:00
|
|
|
&move_stats);
|
2017-12-14 00:01:18 +03:00
|
|
|
|
2017-12-28 04:32:40 +03:00
|
|
|
down_read(&ca->bucket_lock);
|
|
|
|
buckets = bucket_array(ca);
|
2017-12-14 00:01:18 +03:00
|
|
|
for (i = h->data; i < h->data + h->used; i++) {
|
2017-12-28 04:32:40 +03:00
|
|
|
size_t b = sector_to_bucket(ca, i->offset);
|
|
|
|
struct bucket_mark m = READ_ONCE(buckets->b[b].mark);
|
2017-12-14 00:01:18 +03:00
|
|
|
|
|
|
|
if (i->mark.gen == m.gen && bucket_sectors_used(m)) {
|
|
|
|
sectors_not_moved += bucket_sectors_used(m);
|
|
|
|
buckets_not_moved++;
|
|
|
|
}
|
|
|
|
}
|
2017-12-28 04:32:40 +03:00
|
|
|
up_read(&ca->bucket_lock);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
if (sectors_not_moved && !ret)
|
|
|
|
bch_warn(c, "copygc finished but %llu/%llu sectors, %llu/%llu buckets not moved",
|
|
|
|
sectors_not_moved, sectors_to_move,
|
|
|
|
buckets_not_moved, buckets_to_move);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
trace_copygc(ca,
|
2018-01-11 14:41:59 +03:00
|
|
|
atomic64_read(&move_stats.sectors_moved), sectors_not_moved,
|
2017-12-14 00:01:18 +03:00
|
|
|
buckets_to_move, buckets_not_moved);
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
static int bch2_copygc_thread(void *arg)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
2017-03-11 00:40:01 +03:00
|
|
|
struct bch_dev *ca = arg;
|
|
|
|
struct bch_fs *c = ca->fs;
|
2017-01-08 12:13:18 +03:00
|
|
|
struct io_clock *clock = &c->io_clock[WRITE];
|
|
|
|
unsigned long last;
|
|
|
|
u64 available, want, next;
|
|
|
|
|
|
|
|
set_freezable();
|
|
|
|
|
|
|
|
while (!kthread_should_stop()) {
|
|
|
|
if (kthread_wait_freezable(c->copy_gc_enabled))
|
|
|
|
break;
|
|
|
|
|
|
|
|
last = atomic_long_read(&clock->now);
|
|
|
|
/*
|
|
|
|
* don't start copygc until less than half the gc reserve is
|
|
|
|
* available:
|
|
|
|
*/
|
2017-12-14 00:01:18 +03:00
|
|
|
available = dev_buckets_available(c, ca);
|
2017-01-08 12:13:18 +03:00
|
|
|
want = div64_u64((ca->mi.nbuckets - ca->mi.first_bucket) *
|
|
|
|
c->opts.gc_reserve_percent, 200);
|
|
|
|
if (available > want) {
|
|
|
|
next = last + (available - want) *
|
|
|
|
ca->mi.bucket_size;
|
2017-03-20 02:56:34 +03:00
|
|
|
bch2_kthread_io_clock_wait(clock, next);
|
2017-01-08 12:13:18 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
bch2_copygc(c, ca);
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
void bch2_copygc_stop(struct bch_dev *ca)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
2017-12-14 00:01:18 +03:00
|
|
|
ca->copygc_pd.rate.rate = UINT_MAX;
|
|
|
|
bch2_ratelimit_reset(&ca->copygc_pd.rate);
|
2017-03-01 13:45:15 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
if (ca->copygc_thread)
|
|
|
|
kthread_stop(ca->copygc_thread);
|
|
|
|
ca->copygc_thread = NULL;
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
int bch2_copygc_start(struct bch_fs *c, struct bch_dev *ca)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
|
|
|
struct task_struct *t;
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
BUG_ON(ca->copygc_thread);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
if (c->opts.nochanges)
|
2017-02-02 06:16:42 +03:00
|
|
|
return 0;
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
if (bch2_fs_init_fault("copygc_start"))
|
2017-01-08 12:13:18 +03:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
t = kthread_create(bch2_copygc_thread, ca, "bch_copygc");
|
2017-01-08 12:13:18 +03:00
|
|
|
if (IS_ERR(t))
|
|
|
|
return PTR_ERR(t);
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
ca->copygc_thread = t;
|
|
|
|
wake_up_process(ca->copygc_thread);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:01:18 +03:00
|
|
|
void bch2_dev_copygc_init(struct bch_dev *ca)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
2017-12-14 00:01:18 +03:00
|
|
|
bch2_pd_controller_init(&ca->copygc_pd);
|
|
|
|
ca->copygc_pd.d_term = 0;
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|