Commit fb3f57bb authored by Kent Overstreet's avatar Kent Overstreet

bcachefs: rebalance_work

This adds a new btree, rebalance_work, to eliminate scanning required
for finding extents that need work done on them in the background - i.e.
for the background_target and background_compression options.

rebalance_work is a bitset btree, where a KEY_TYPE_set corresponds to an
extent in the extents or reflink btree at the same pos.

A new extent field is added, bch_extent_rebalance, which indicates that
this extent has work that needs to be done in the background - and which
options to use. This allows per-inode options to be propagated to
indirect extents - at least in some circumstances. In this patch,
changing IO options on a file will not propagate the new options to
indirect extents pointed to by that file.

Updating (setting/clearing) the rebalance_work btree is done by the
extent trigger, which looks at the bch_extent_rebalance field.

Scanning is still requrired after changing IO path options - either just
for a given inode, or for the whole filesystem. We indicate that
scanning is required by adding a KEY_TYPE_cookie key to the
rebalance_work btree: the cookie counter is so that we can detect that
scanning is still required when an option has been flipped mid-way
through an existing scan.

Future possible work:
 - Propagate options to indirect extents when being changed
 - Add other IO path options - nr_replicas, ec, to rebalance_work so
   they can be applied in the background when they change
 - Add a counter, for bcachefs fs usage output, showing the pending
   amount of rebalance work: we'll probably want to do this after the
   disk space accounting rewrite (moving it to a new btree)
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 55c11a15
...@@ -464,6 +464,7 @@ enum gc_phase { ...@@ -464,6 +464,7 @@ enum gc_phase {
GC_PHASE_BTREE_snapshot_trees, GC_PHASE_BTREE_snapshot_trees,
GC_PHASE_BTREE_deleted_inodes, GC_PHASE_BTREE_deleted_inodes,
GC_PHASE_BTREE_logged_ops, GC_PHASE_BTREE_logged_ops,
GC_PHASE_BTREE_rebalance_work,
GC_PHASE_PENDING_DELETE, GC_PHASE_PENDING_DELETE,
}; };
......
...@@ -613,31 +613,17 @@ struct bch_extent_stripe_ptr { ...@@ -613,31 +613,17 @@ struct bch_extent_stripe_ptr {
#endif #endif
}; };
struct bch_extent_reservation {
#if defined(__LITTLE_ENDIAN_BITFIELD)
__u64 type:6,
unused:22,
replicas:4,
generation:32;
#elif defined (__BIG_ENDIAN_BITFIELD)
__u64 generation:32,
replicas:4,
unused:22,
type:6;
#endif
};
struct bch_extent_rebalance { struct bch_extent_rebalance {
#if defined(__LITTLE_ENDIAN_BITFIELD) #if defined(__LITTLE_ENDIAN_BITFIELD)
__u64 type:7, __u64 type:6,
unused:33, unused:34,
compression:8, compression:8, /* enum bch_compression_opt */
target:16; target:16;
#elif defined (__BIG_ENDIAN_BITFIELD) #elif defined (__BIG_ENDIAN_BITFIELD)
__u64 target:16, __u64 target:16,
compression:8, compression:8,
unused:33, unused:34,
type:7; type:6;
#endif #endif
}; };
...@@ -1682,7 +1668,9 @@ struct bch_sb_field_journal_seq_blacklist { ...@@ -1682,7 +1668,9 @@ struct bch_sb_field_journal_seq_blacklist {
x(snapshot_skiplists, BCH_VERSION(1, 1), \ x(snapshot_skiplists, BCH_VERSION(1, 1), \
BIT_ULL(BCH_RECOVERY_PASS_check_snapshots)) \ BIT_ULL(BCH_RECOVERY_PASS_check_snapshots)) \
x(deleted_inodes, BCH_VERSION(1, 2), \ x(deleted_inodes, BCH_VERSION(1, 2), \
BIT_ULL(BCH_RECOVERY_PASS_check_inodes)) BIT_ULL(BCH_RECOVERY_PASS_check_inodes)) \
x(rebalance_work, BCH_VERSION(1, 3), \
BIT_ULL(BCH_RECOVERY_PASS_set_fs_needs_rebalance))
enum bcachefs_metadata_version { enum bcachefs_metadata_version {
bcachefs_metadata_version_min = 9, bcachefs_metadata_version_min = 9,
...@@ -1693,7 +1681,7 @@ enum bcachefs_metadata_version { ...@@ -1693,7 +1681,7 @@ enum bcachefs_metadata_version {
}; };
static const __maybe_unused static const __maybe_unused
unsigned bcachefs_metadata_required_upgrade_below = bcachefs_metadata_version_major_minor; unsigned bcachefs_metadata_required_upgrade_below = bcachefs_metadata_version_rebalance_work;
#define bcachefs_metadata_version_current (bcachefs_metadata_version_max - 1) #define bcachefs_metadata_version_current (bcachefs_metadata_version_max - 1)
...@@ -2306,7 +2294,9 @@ enum btree_id_flags { ...@@ -2306,7 +2294,9 @@ enum btree_id_flags {
BIT_ULL(KEY_TYPE_set)) \ BIT_ULL(KEY_TYPE_set)) \
x(logged_ops, 17, 0, \ x(logged_ops, 17, 0, \
BIT_ULL(KEY_TYPE_logged_op_truncate)| \ BIT_ULL(KEY_TYPE_logged_op_truncate)| \
BIT_ULL(KEY_TYPE_logged_op_finsert)) BIT_ULL(KEY_TYPE_logged_op_finsert)) \
x(rebalance_work, 18, BTREE_ID_SNAPSHOTS, \
BIT_ULL(KEY_TYPE_set)|BIT_ULL(KEY_TYPE_cookie))
enum btree_id { enum btree_id {
#define x(name, nr, ...) BTREE_ID_##name = nr, #define x(name, nr, ...) BTREE_ID_##name = nr,
......
...@@ -1536,6 +1536,16 @@ int bch2_trans_mark_extent(struct btree_trans *trans, ...@@ -1536,6 +1536,16 @@ int bch2_trans_mark_extent(struct btree_trans *trans,
struct bkey_s_c old, struct bkey_i *new, struct bkey_s_c old, struct bkey_i *new,
unsigned flags) unsigned flags)
{ {
struct bch_fs *c = trans->c;
int mod = (int) bch2_bkey_needs_rebalance(c, bkey_i_to_s_c(new)) -
(int) bch2_bkey_needs_rebalance(c, old);
if (mod) {
int ret = bch2_btree_bit_mod(trans, BTREE_ID_rebalance_work, new->k.p, mod > 0);
if (ret)
return ret;
}
return trigger_run_overwrite_then_insert(__trans_mark_extent, trans, btree_id, level, old, new, flags); return trigger_run_overwrite_then_insert(__trans_mark_extent, trans, btree_id, level, old, new, flags);
} }
......
...@@ -697,18 +697,26 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res, ...@@ -697,18 +697,26 @@ int bch2_opt_compression_parse(struct bch_fs *c, const char *_val, u64 *res,
return ret; return ret;
} }
void bch2_opt_compression_to_text(struct printbuf *out, void bch2_compression_opt_to_text(struct printbuf *out, u64 v)
struct bch_fs *c,
struct bch_sb *sb,
u64 v)
{ {
struct bch_compression_opt opt = bch2_compression_decode(v); struct bch_compression_opt opt = bch2_compression_decode(v);
if (opt.type < BCH_COMPRESSION_OPT_NR)
prt_str(out, bch2_compression_opts[opt.type]); prt_str(out, bch2_compression_opts[opt.type]);
else
prt_printf(out, "(unknown compression opt %u)", opt.type);
if (opt.level) if (opt.level)
prt_printf(out, ":%u", opt.level); prt_printf(out, ":%u", opt.level);
} }
void bch2_opt_compression_to_text(struct printbuf *out,
struct bch_fs *c,
struct bch_sb *sb,
u64 v)
{
return bch2_compression_opt_to_text(out, v);
}
int bch2_opt_compression_validate(u64 v, struct printbuf *err) int bch2_opt_compression_validate(u64 v, struct printbuf *err)
{ {
if (!bch2_compression_opt_valid(v)) { if (!bch2_compression_opt_valid(v)) {
......
...@@ -58,6 +58,8 @@ int bch2_check_set_has_compressed_data(struct bch_fs *, unsigned); ...@@ -58,6 +58,8 @@ int bch2_check_set_has_compressed_data(struct bch_fs *, unsigned);
void bch2_fs_compress_exit(struct bch_fs *); void bch2_fs_compress_exit(struct bch_fs *);
int bch2_fs_compress_init(struct bch_fs *); int bch2_fs_compress_init(struct bch_fs *);
void bch2_compression_opt_to_text(struct printbuf *, u64);
int bch2_opt_compression_parse(struct bch_fs *, const char *, u64 *, struct printbuf *); int bch2_opt_compression_parse(struct bch_fs *, const char *, u64 *, struct printbuf *);
void bch2_opt_compression_to_text(struct printbuf *, struct bch_fs *, struct bch_sb *, u64); void bch2_opt_compression_to_text(struct printbuf *, struct bch_fs *, struct bch_sb *, u64);
int bch2_opt_compression_validate(u64, struct printbuf *); int bch2_opt_compression_validate(u64, struct printbuf *);
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "keylist.h" #include "keylist.h"
#include "move.h" #include "move.h"
#include "nocow_locking.h" #include "nocow_locking.h"
#include "rebalance.h"
#include "subvolume.h" #include "subvolume.h"
#include "trace.h" #include "trace.h"
...@@ -251,11 +252,11 @@ static int __bch2_data_update_index_update(struct btree_trans *trans, ...@@ -251,11 +252,11 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
ret = bch2_insert_snapshot_whiteouts(trans, m->btree_id, ret = bch2_insert_snapshot_whiteouts(trans, m->btree_id,
k.k->p, bkey_start_pos(&insert->k)) ?: k.k->p, bkey_start_pos(&insert->k)) ?:
bch2_insert_snapshot_whiteouts(trans, m->btree_id, bch2_insert_snapshot_whiteouts(trans, m->btree_id,
k.k->p, insert->k.p); k.k->p, insert->k.p) ?:
if (ret) bch2_bkey_set_needs_rebalance(c, insert,
goto err; op->opts.background_target,
op->opts.background_compression) ?:
ret = bch2_trans_update(trans, &iter, insert, bch2_trans_update(trans, &iter, insert,
BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
bch2_trans_commit(trans, &op->res, bch2_trans_commit(trans, &op->res,
NULL, NULL,
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "btree_iter.h" #include "btree_iter.h"
#include "buckets.h" #include "buckets.h"
#include "checksum.h" #include "checksum.h"
#include "compress.h"
#include "debug.h" #include "debug.h"
#include "disk_groups.h" #include "disk_groups.h"
#include "error.h" #include "error.h"
...@@ -757,18 +758,6 @@ static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs, ...@@ -757,18 +758,6 @@ static union bch_extent_entry *extent_entry_prev(struct bkey_ptrs ptrs,
return i; return i;
} }
static void extent_entry_drop(struct bkey_s k, union bch_extent_entry *entry)
{
union bch_extent_entry *next = extent_entry_next(entry);
/* stripes have ptrs, but their layout doesn't work with this code */
BUG_ON(k.k->type == KEY_TYPE_stripe);
memmove_u64s_down(entry, next,
(u64 *) bkey_val_end(k) - (u64 *) next);
k.k->u64s -= (u64 *) next - (u64 *) entry;
}
/* /*
* Returns pointer to the next entry after the one being dropped: * Returns pointer to the next entry after the one being dropped:
*/ */
...@@ -1048,6 +1037,18 @@ void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c, ...@@ -1048,6 +1037,18 @@ void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
(u64) ec->idx, ec->block); (u64) ec->idx, ec->block);
break; break;
} }
case BCH_EXTENT_ENTRY_rebalance: {
const struct bch_extent_rebalance *r = &entry->rebalance;
prt_str(out, "rebalance: target ");
if (c)
bch2_target_to_text(out, c, r->target);
else
prt_printf(out, "%u", r->target);
prt_str(out, " compression ");
bch2_compression_opt_to_text(out, r->compression);
break;
}
default: default:
prt_printf(out, "(invalid extent entry %.16llx)", *((u64 *) entry)); prt_printf(out, "(invalid extent entry %.16llx)", *((u64 *) entry));
return; return;
...@@ -1223,10 +1224,19 @@ int bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k, ...@@ -1223,10 +1224,19 @@ int bch2_bkey_ptrs_invalid(const struct bch_fs *c, struct bkey_s_c k,
} }
have_ec = true; have_ec = true;
break; break;
case BCH_EXTENT_ENTRY_rebalance: case BCH_EXTENT_ENTRY_rebalance: {
const struct bch_extent_rebalance *r = &entry->rebalance;
if (!bch2_compression_opt_valid(r->compression)) {
struct bch_compression_opt opt = __bch2_compression_decode(r->compression);
prt_printf(err, "invalid compression opt %u:%u",
opt.type, opt.level);
return -BCH_ERR_invalid_bkey;
}
break; break;
} }
} }
}
if (!nr_ptrs) { if (!nr_ptrs) {
prt_str(err, "no ptrs"); prt_str(err, "no ptrs");
...@@ -1289,6 +1299,125 @@ void bch2_ptr_swab(struct bkey_s k) ...@@ -1289,6 +1299,125 @@ void bch2_ptr_swab(struct bkey_s k)
} }
} }
const struct bch_extent_rebalance *bch2_bkey_rebalance_opts(struct bkey_s_c k)
{
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
const union bch_extent_entry *entry;
bkey_extent_entry_for_each(ptrs, entry)
if (__extent_entry_type(entry) == BCH_EXTENT_ENTRY_rebalance)
return &entry->rebalance;
return NULL;
}
unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *c, struct bkey_s_c k,
unsigned target, unsigned compression)
{
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
unsigned rewrite_ptrs = 0;
if (compression) {
unsigned compression_type = bch2_compression_opt_to_type(compression);
const union bch_extent_entry *entry;
struct extent_ptr_decoded p;
unsigned i = 0;
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible) {
rewrite_ptrs = 0;
goto incompressible;
}
if (!p.ptr.cached && p.crc.compression_type != compression_type)
rewrite_ptrs |= 1U << i;
i++;
}
}
incompressible:
if (target && bch2_target_accepts_data(c, BCH_DATA_user, target)) {
const struct bch_extent_ptr *ptr;
unsigned i = 0;
bkey_for_each_ptr(ptrs, ptr) {
if (!ptr->cached && !bch2_dev_in_target(c, ptr->dev, target))
rewrite_ptrs |= 1U << i;
i++;
}
}
return rewrite_ptrs;
}
bool bch2_bkey_needs_rebalance(struct bch_fs *c, struct bkey_s_c k)
{
const struct bch_extent_rebalance *r = bch2_bkey_rebalance_opts(k);
/*
* If it's an indirect extent, we don't delete the rebalance entry when
* done so that we know what options were applied - check if it still
* needs work done:
*/
if (r &&
k.k->type == KEY_TYPE_reflink_v &&
!bch2_bkey_ptrs_need_rebalance(c, k, r->target, r->compression))
r = NULL;
return r != NULL;
}
int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bkey_i *_k,
unsigned target, unsigned compression)
{
struct bkey_s k = bkey_i_to_s(_k);
struct bch_extent_rebalance *r;
bool needs_rebalance;
if (!bkey_extent_is_direct_data(k.k))
return 0;
/* get existing rebalance entry: */
r = (struct bch_extent_rebalance *) bch2_bkey_rebalance_opts(k.s_c);
if (r) {
if (k.k->type == KEY_TYPE_reflink_v) {
/*
* indirect extents: existing options take precedence,
* so that we don't move extents back and forth if
* they're referenced by different inodes with different
* options:
*/
if (r->target)
target = r->target;
if (r->compression)
compression = r->compression;
}
r->target = target;
r->compression = compression;
}
needs_rebalance = bch2_bkey_ptrs_need_rebalance(c, k.s_c, target, compression);
if (needs_rebalance && !r) {
union bch_extent_entry *new = bkey_val_end(k);
new->rebalance.type = 1U << BCH_EXTENT_ENTRY_rebalance;
new->rebalance.compression = compression;
new->rebalance.target = target;
new->rebalance.unused = 0;
k.k->u64s += extent_entry_u64s(new);
} else if (!needs_rebalance && r && k.k->type != KEY_TYPE_reflink_v) {
/*
* For indirect extents, don't delete the rebalance entry when
* we're finished so that we know we specifically moved it or
* compressed it to its current location/compression type
*/
extent_entry_drop(k, (union bch_extent_entry *) r);
}
return 0;
}
/* Generic extent code: */ /* Generic extent code: */
int bch2_cut_front_s(struct bpos where, struct bkey_s k) int bch2_cut_front_s(struct bpos where, struct bkey_s k)
......
...@@ -89,6 +89,18 @@ static inline void __extent_entry_insert(struct bkey_i *k, ...@@ -89,6 +89,18 @@ static inline void __extent_entry_insert(struct bkey_i *k,
memcpy_u64s_small(dst, new, extent_entry_u64s(new)); memcpy_u64s_small(dst, new, extent_entry_u64s(new));
} }
static inline void extent_entry_drop(struct bkey_s k, union bch_extent_entry *entry)
{
union bch_extent_entry *next = extent_entry_next(entry);
/* stripes have ptrs, but their layout doesn't work with this code */
BUG_ON(k.k->type == KEY_TYPE_stripe);
memmove_u64s_down(entry, next,
(u64 *) bkey_val_end(k) - (u64 *) next);
k.k->u64s -= (u64 *) next - (u64 *) entry;
}
static inline bool extent_entry_is_ptr(const union bch_extent_entry *e) static inline bool extent_entry_is_ptr(const union bch_extent_entry *e)
{ {
return extent_entry_type(e) == BCH_EXTENT_ENTRY_ptr; return extent_entry_type(e) == BCH_EXTENT_ENTRY_ptr;
...@@ -698,6 +710,14 @@ int bch2_bkey_ptrs_invalid(const struct bch_fs *, struct bkey_s_c, ...@@ -698,6 +710,14 @@ int bch2_bkey_ptrs_invalid(const struct bch_fs *, struct bkey_s_c,
void bch2_ptr_swab(struct bkey_s); void bch2_ptr_swab(struct bkey_s);
const struct bch_extent_rebalance *bch2_bkey_rebalance_opts(struct bkey_s_c);
unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *, struct bkey_s_c,
unsigned, unsigned);
bool bch2_bkey_needs_rebalance(struct bch_fs *, struct bkey_s_c);
int bch2_bkey_set_needs_rebalance(struct bch_fs *, struct bkey_i *,
unsigned, unsigned);
/* Generic extent code: */ /* Generic extent code: */
enum bch_extent_overlap { enum bch_extent_overlap {
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "io_misc.h" #include "io_misc.h"
#include "io_write.h" #include "io_write.h"
#include "logged_ops.h" #include "logged_ops.h"
#include "rebalance.h"
#include "subvolume.h" #include "subvolume.h"
/* Overwrites whatever was present with zeroes: */ /* Overwrites whatever was present with zeroes: */
...@@ -355,6 +356,7 @@ static int __bch2_resume_logged_op_finsert(struct btree_trans *trans, ...@@ -355,6 +356,7 @@ static int __bch2_resume_logged_op_finsert(struct btree_trans *trans,
struct btree_iter iter; struct btree_iter iter;
struct bkey_i_logged_op_finsert *op = bkey_i_to_logged_op_finsert(op_k); struct bkey_i_logged_op_finsert *op = bkey_i_to_logged_op_finsert(op_k);
subvol_inum inum = { le32_to_cpu(op->v.subvol), le64_to_cpu(op->v.inum) }; subvol_inum inum = { le32_to_cpu(op->v.subvol), le64_to_cpu(op->v.inum) };
struct bch_io_opts opts;
u64 dst_offset = le64_to_cpu(op->v.dst_offset); u64 dst_offset = le64_to_cpu(op->v.dst_offset);
u64 src_offset = le64_to_cpu(op->v.src_offset); u64 src_offset = le64_to_cpu(op->v.src_offset);
s64 shift = dst_offset - src_offset; s64 shift = dst_offset - src_offset;
...@@ -363,6 +365,10 @@ static int __bch2_resume_logged_op_finsert(struct btree_trans *trans, ...@@ -363,6 +365,10 @@ static int __bch2_resume_logged_op_finsert(struct btree_trans *trans,
bool insert = shift > 0; bool insert = shift > 0;
int ret = 0; int ret = 0;
ret = bch2_inum_opts_get(trans, inum, &opts);
if (ret)
return ret;
bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
POS(inum.inum, 0), POS(inum.inum, 0),
BTREE_ITER_INTENT); BTREE_ITER_INTENT);
...@@ -443,7 +449,10 @@ case LOGGED_OP_FINSERT_shift_extents: ...@@ -443,7 +449,10 @@ case LOGGED_OP_FINSERT_shift_extents:
op->v.pos = cpu_to_le64(insert ? bkey_start_offset(&delete.k) : delete.k.p.offset); op->v.pos = cpu_to_le64(insert ? bkey_start_offset(&delete.k) : delete.k.p.offset);
ret = bch2_btree_insert_trans(trans, BTREE_ID_extents, &delete, 0) ?: ret = bch2_bkey_set_needs_rebalance(c, copy,
opts.background_target,
opts.background_compression) ?:
bch2_btree_insert_trans(trans, BTREE_ID_extents, &delete, 0) ?:
bch2_btree_insert_trans(trans, BTREE_ID_extents, copy, 0) ?: bch2_btree_insert_trans(trans, BTREE_ID_extents, copy, 0) ?:
bch2_logged_op_update(trans, &op->k_i) ?: bch2_logged_op_update(trans, &op->k_i) ?:
bch2_trans_commit(trans, &disk_res, NULL, BTREE_INSERT_NOFAIL); bch2_trans_commit(trans, &disk_res, NULL, BTREE_INSERT_NOFAIL);
......
...@@ -351,7 +351,10 @@ static int bch2_write_index_default(struct bch_write_op *op) ...@@ -351,7 +351,10 @@ static int bch2_write_index_default(struct bch_write_op *op)
bkey_start_pos(&sk.k->k), bkey_start_pos(&sk.k->k),
BTREE_ITER_SLOTS|BTREE_ITER_INTENT); BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
ret = bch2_extent_update(trans, inum, &iter, sk.k, ret = bch2_bkey_set_needs_rebalance(c, sk.k,
op->opts.background_target,
op->opts.background_compression) ?:
bch2_extent_update(trans, inum, &iter, sk.k,
&op->res, &op->res,
op->new_i_size, &op->i_sectors_delta, op->new_i_size, &op->i_sectors_delta,
op->flags & BCH_WRITE_CHECK_ENOSPC); op->flags & BCH_WRITE_CHECK_ENOSPC);
...@@ -495,7 +498,6 @@ static void __bch2_write_index(struct bch_write_op *op) ...@@ -495,7 +498,6 @@ static void __bch2_write_index(struct bch_write_op *op)
{ {
struct bch_fs *c = op->c; struct bch_fs *c = op->c;
struct keylist *keys = &op->insert_keys; struct keylist *keys = &op->insert_keys;
struct bkey_i *k;
unsigned dev; unsigned dev;
int ret = 0; int ret = 0;
...@@ -505,14 +507,6 @@ static void __bch2_write_index(struct bch_write_op *op) ...@@ -505,14 +507,6 @@ static void __bch2_write_index(struct bch_write_op *op)
goto err; goto err;
} }
/*
* probably not the ideal place to hook this in, but I don't
* particularly want to plumb io_opts all the way through the btree
* update stack right now
*/
for_each_keylist_key(keys, k)
bch2_rebalance_add_key(c, bkey_i_to_s_c(k), &op->opts);
if (!bch2_keylist_empty(keys)) { if (!bch2_keylist_empty(keys)) {
u64 sectors_start = keylist_sectors(keys); u64 sectors_start = keylist_sectors(keys);
......
...@@ -3,13 +3,18 @@ ...@@ -3,13 +3,18 @@
#include "bcachefs.h" #include "bcachefs.h"
#include "alloc_foreground.h" #include "alloc_foreground.h"
#include "btree_iter.h" #include "btree_iter.h"
#include "btree_update.h"
#include "btree_write_buffer.h"
#include "buckets.h" #include "buckets.h"
#include "clock.h" #include "clock.h"
#include "compress.h" #include "compress.h"
#include "disk_groups.h" #include "disk_groups.h"
#include "errcode.h" #include "errcode.h"
#include "error.h"
#include "inode.h"
#include "move.h" #include "move.h"
#include "rebalance.h" #include "rebalance.h"
#include "subvolume.h"
#include "super-io.h" #include "super-io.h"
#include "trace.h" #include "trace.h"
...@@ -17,301 +22,399 @@ ...@@ -17,301 +22,399 @@
#include <linux/kthread.h> #include <linux/kthread.h>
#include <linux/sched/cputime.h> #include <linux/sched/cputime.h>
/* #define REBALANCE_WORK_SCAN_OFFSET (U64_MAX - 1)
* Check if an extent should be moved:
* returns -1 if it should not be moved, or
* device of pointer that should be moved, if known, or INT_MAX if unknown
*/
static bool rebalance_pred(struct bch_fs *c, void *arg,
struct bkey_s_c k,
struct bch_io_opts *io_opts,
struct data_update_opts *data_opts)
{
struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
unsigned i;
data_opts->rewrite_ptrs = 0; static const char * const bch2_rebalance_state_strs[] = {
data_opts->target = io_opts->background_target; #define x(t) #t,
data_opts->extra_replicas = 0; BCH_REBALANCE_STATES()
data_opts->btree_insert_flags = 0; NULL
#undef x
if (io_opts->background_compression && };
!bch2_bkey_is_incompressible(k)) {
const union bch_extent_entry *entry;
struct extent_ptr_decoded p;
i = 0;
bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
if (!p.ptr.cached &&
p.crc.compression_type !=
bch2_compression_opt_to_type(io_opts->background_compression))
data_opts->rewrite_ptrs |= 1U << i;
i++;
}
}
if (io_opts->background_target) { static int __bch2_set_rebalance_needs_scan(struct btree_trans *trans, u64 inum)
const struct bch_extent_ptr *ptr; {
struct btree_iter iter;
struct bkey_s_c k;
struct bkey_i_cookie *cookie;
u64 v;
int ret;
i = 0; bch2_trans_iter_init(trans, &iter, BTREE_ID_rebalance_work,
bkey_for_each_ptr(ptrs, ptr) { SPOS(inum, REBALANCE_WORK_SCAN_OFFSET, U32_MAX),
if (!ptr->cached && BTREE_ITER_INTENT);
!bch2_dev_in_target(c, ptr->dev, io_opts->background_target) && k = bch2_btree_iter_peek_slot(&iter);
bch2_target_accepts_data(c, BCH_DATA_user, io_opts->background_target)) ret = bkey_err(k);
data_opts->rewrite_ptrs |= 1U << i; if (ret)
i++; goto err;
}
} v = k.k->type == KEY_TYPE_cookie
? le64_to_cpu(bkey_s_c_to_cookie(k).v->cookie)
: 0;
cookie = bch2_trans_kmalloc(trans, sizeof(*cookie));
ret = PTR_ERR_OR_ZERO(cookie);
if (ret)
goto err;
bkey_cookie_init(&cookie->k_i);
cookie->k.p = iter.pos;
cookie->v.cookie = cpu_to_le64(v + 1);
ret = bch2_trans_update(trans, &iter, &cookie->k_i, 0);
err:
bch2_trans_iter_exit(trans, &iter);
return ret;
}
return data_opts->rewrite_ptrs != 0; int bch2_set_rebalance_needs_scan(struct bch_fs *c, u64 inum)
{
int ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
__bch2_set_rebalance_needs_scan(trans, inum));
rebalance_wakeup(c);
return ret;
} }
void bch2_rebalance_add_key(struct bch_fs *c, int bch2_set_fs_needs_rebalance(struct bch_fs *c)
struct bkey_s_c k,
struct bch_io_opts *io_opts)
{ {
struct data_update_opts update_opts = { 0 }; return bch2_set_rebalance_needs_scan(c, 0);
struct bkey_ptrs_c ptrs; }
const struct bch_extent_ptr *ptr;
unsigned i;
if (!rebalance_pred(c, NULL, k, io_opts, &update_opts)) static int bch2_clear_rebalance_needs_scan(struct btree_trans *trans, u64 inum, u64 cookie)
return; {
struct btree_iter iter;
struct bkey_s_c k;
u64 v;
int ret;
i = 0; bch2_trans_iter_init(trans, &iter, BTREE_ID_rebalance_work,
ptrs = bch2_bkey_ptrs_c(k); SPOS(inum, REBALANCE_WORK_SCAN_OFFSET, U32_MAX),
bkey_for_each_ptr(ptrs, ptr) { BTREE_ITER_INTENT);
if ((1U << i) && update_opts.rewrite_ptrs) k = bch2_btree_iter_peek_slot(&iter);
if (atomic64_add_return(k.k->size, ret = bkey_err(k);
&bch_dev_bkey_exists(c, ptr->dev)->rebalance_work) == if (ret)
k.k->size) goto err;
rebalance_wakeup(c);
i++; v = k.k->type == KEY_TYPE_cookie
} ? le64_to_cpu(bkey_s_c_to_cookie(k).v->cookie)
: 0;
if (v == cookie)
ret = bch2_btree_delete_at(trans, &iter, 0);
err:
bch2_trans_iter_exit(trans, &iter);
return ret;
} }
void bch2_rebalance_add_work(struct bch_fs *c, u64 sectors) static struct bkey_s_c next_rebalance_entry(struct btree_trans *trans,
struct btree_iter *work_iter)
{ {
if (atomic64_add_return(sectors, &c->rebalance.work_unknown_dev) == return !kthread_should_stop()
sectors) ? bch2_btree_iter_peek(work_iter)
rebalance_wakeup(c); : bkey_s_c_null;
} }
struct rebalance_work { static int bch2_bkey_clear_needs_rebalance(struct btree_trans *trans,
int dev_most_full_idx; struct btree_iter *iter,
unsigned dev_most_full_percent; struct bkey_s_c k)
u64 dev_most_full_work;
u64 dev_most_full_capacity;
u64 total_work;
};
static void rebalance_work_accumulate(struct rebalance_work *w,
u64 dev_work, u64 unknown_dev, u64 capacity, int idx)
{ {
unsigned percent_full; struct bkey_i *n = bch2_bkey_make_mut(trans, iter, &k, 0);
u64 work = dev_work + unknown_dev; int ret = PTR_ERR_OR_ZERO(n);
if (ret)
return ret;
/* avoid divide by 0 */ extent_entry_drop(bkey_i_to_s(n),
if (!capacity) (void *) bch2_bkey_rebalance_opts(bkey_i_to_s_c(n)));
return; return bch2_trans_commit(trans, NULL, NULL, BTREE_INSERT_NOFAIL);
}
static struct bkey_s_c next_rebalance_extent(struct btree_trans *trans,
struct bpos work_pos,
struct btree_iter *extent_iter,
struct data_update_opts *data_opts)
{
struct bch_fs *c = trans->c;
struct bkey_s_c k;
bch2_trans_iter_exit(trans, extent_iter);
bch2_trans_iter_init(trans, extent_iter,
work_pos.inode ? BTREE_ID_extents : BTREE_ID_reflink,
work_pos,
BTREE_ITER_ALL_SNAPSHOTS);
k = bch2_btree_iter_peek_slot(extent_iter);
if (bkey_err(k))
return k;
const struct bch_extent_rebalance *r = k.k ? bch2_bkey_rebalance_opts(k) : NULL;
if (!r) {
/* raced due to btree write buffer, nothing to do */
return bkey_s_c_null;
}
if (work < dev_work || work < unknown_dev) memset(data_opts, 0, sizeof(*data_opts));
work = U64_MAX;
work = min(work, capacity);
percent_full = div64_u64(work * 100, capacity); data_opts->rewrite_ptrs =
bch2_bkey_ptrs_need_rebalance(c, k, r->target, r->compression);
data_opts->target = r->target;
if (percent_full >= w->dev_most_full_percent) { if (!data_opts->rewrite_ptrs) {
w->dev_most_full_idx = idx; /*
w->dev_most_full_percent = percent_full; * device we would want to write to offline? devices in target
w->dev_most_full_work = work; * changed?
w->dev_most_full_capacity = capacity; *
* We'll now need a full scan before this extent is picked up
* again:
*/
int ret = bch2_bkey_clear_needs_rebalance(trans, extent_iter, k);
if (ret)
return bkey_s_c_err(ret);
return bkey_s_c_null;
} }
if (w->total_work + dev_work >= w->total_work && return k;
w->total_work + dev_work >= dev_work)
w->total_work += dev_work;
} }
static struct rebalance_work rebalance_work(struct bch_fs *c) noinline_for_stack
static int do_rebalance_extent(struct moving_context *ctxt,
struct bpos work_pos,
struct btree_iter *extent_iter)
{ {
struct bch_dev *ca; struct btree_trans *trans = ctxt->trans;
struct rebalance_work ret = { .dev_most_full_idx = -1 }; struct bch_fs *c = trans->c;
u64 unknown_dev = atomic64_read(&c->rebalance.work_unknown_dev); struct bch_fs_rebalance *r = &trans->c->rebalance;
unsigned i; struct data_update_opts data_opts;
struct bch_io_opts io_opts;
struct bkey_s_c k;
struct bkey_buf sk;
int ret;
for_each_online_member(ca, c, i) ctxt->stats = &r->work_stats;
rebalance_work_accumulate(&ret, r->state = BCH_REBALANCE_working;
atomic64_read(&ca->rebalance_work),
unknown_dev,
bucket_to_sector(ca, ca->mi.nbuckets -
ca->mi.first_bucket),
i);
rebalance_work_accumulate(&ret, bch2_bkey_buf_init(&sk);
unknown_dev, 0, c->capacity, -1);
ret = bkey_err(k = next_rebalance_extent(trans, work_pos,
extent_iter, &data_opts));
if (ret || !k.k)
goto out;
ret = bch2_move_get_io_opts_one(trans, &io_opts, k);
if (ret)
goto out;
atomic64_add(k.k->size, &ctxt->stats->sectors_seen);
/*
* The iterator gets unlocked by __bch2_read_extent - need to
* save a copy of @k elsewhere:
*/
bch2_bkey_buf_reassemble(&sk, c, k);
k = bkey_i_to_s_c(sk.k);
ret = bch2_move_extent(ctxt, NULL, extent_iter, k, io_opts, data_opts);
if (ret) {
if (bch2_err_matches(ret, ENOMEM)) {
/* memory allocation failure, wait for some IO to finish */
bch2_move_ctxt_wait_for_io(ctxt);
ret = -BCH_ERR_transaction_restart_nested;
}
if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
goto out;
/* skip it and continue, XXX signal failure */
ret = 0;
}
out:
bch2_bkey_buf_exit(&sk, c);
return ret; return ret;
} }
static void rebalance_work_reset(struct bch_fs *c) static bool rebalance_pred(struct bch_fs *c, void *arg,
struct bkey_s_c k,
struct bch_io_opts *io_opts,
struct data_update_opts *data_opts)
{ {
struct bch_dev *ca; unsigned target, compression;
unsigned i;
for_each_online_member(ca, c, i) if (k.k->p.inode) {
atomic64_set(&ca->rebalance_work, 0); target = io_opts->background_target;
compression = io_opts->background_compression ?: io_opts->compression;
} else {
const struct bch_extent_rebalance *r = bch2_bkey_rebalance_opts(k);
atomic64_set(&c->rebalance.work_unknown_dev, 0); target = r ? r->target : io_opts->background_target;
compression = r ? r->compression :
(io_opts->background_compression ?: io_opts->compression);
}
data_opts->rewrite_ptrs = bch2_bkey_ptrs_need_rebalance(c, k, target, compression);
data_opts->target = target;
return data_opts->rewrite_ptrs != 0;
} }
static unsigned long curr_cputime(void) static int do_rebalance_scan(struct moving_context *ctxt, u64 inum, u64 cookie)
{ {
u64 utime, stime; struct btree_trans *trans = ctxt->trans;
struct bch_fs_rebalance *r = &trans->c->rebalance;
int ret;
bch2_move_stats_init(&r->scan_stats, "rebalance_scan");
ctxt->stats = &r->scan_stats;
if (!inum) {
r->scan_start = BBPOS_MIN;
r->scan_end = BBPOS_MAX;
} else {
r->scan_start = BBPOS(BTREE_ID_extents, POS(inum, 0));
r->scan_end = BBPOS(BTREE_ID_extents, POS(inum, U64_MAX));
}
task_cputime_adjusted(current, &utime, &stime); r->state = BCH_REBALANCE_scanning;
return nsecs_to_jiffies(utime + stime);
ret = __bch2_move_data(ctxt, r->scan_start, r->scan_end, rebalance_pred, NULL) ?:
commit_do(trans, NULL, NULL, BTREE_INSERT_NOFAIL,
bch2_clear_rebalance_needs_scan(trans, inum, cookie));
bch2_move_stats_exit(&r->scan_stats, trans->c);
return ret;
} }
static int bch2_rebalance_thread(void *arg) static void rebalance_wait(struct bch_fs *c)
{ {
struct bch_fs *c = arg;
struct bch_fs_rebalance *r = &c->rebalance; struct bch_fs_rebalance *r = &c->rebalance;
struct bch_dev *ca;
struct io_clock *clock = &c->io_clock[WRITE]; struct io_clock *clock = &c->io_clock[WRITE];
struct rebalance_work w, p; u64 now = atomic64_read(&clock->now);
struct bch_move_stats move_stats; u64 min_member_capacity = 128 * 2048;
unsigned long start, prev_start; unsigned i;
unsigned long prev_run_time, prev_run_cputime;
unsigned long cputime, prev_cputime;
u64 io_start;
long throttle;
set_freezable(); for_each_rw_member(ca, c, i)
min_member_capacity = min(min_member_capacity,
ca->mi.nbuckets * ca->mi.bucket_size);
r->wait_iotime_end = now + (min_member_capacity >> 6);
if (r->state != BCH_REBALANCE_waiting) {
r->wait_iotime_start = now;
r->wait_wallclock_start = ktime_get_real_ns();
r->state = BCH_REBALANCE_waiting;
}
io_start = atomic64_read(&clock->now); bch2_kthread_io_clock_wait(clock, r->wait_iotime_end, MAX_SCHEDULE_TIMEOUT);
p = rebalance_work(c); }
prev_start = jiffies;
prev_cputime = curr_cputime();
bch2_move_stats_init(&move_stats, "rebalance"); static int do_rebalance(struct moving_context *ctxt)
while (!kthread_wait_freezable(r->enabled)) { {
cond_resched(); struct btree_trans *trans = ctxt->trans;
struct bch_fs *c = trans->c;
struct bch_fs_rebalance *r = &c->rebalance;
struct btree_iter rebalance_work_iter, extent_iter = { NULL };
struct bkey_s_c k;
int ret = 0;
start = jiffies; bch2_move_stats_init(&r->work_stats, "rebalance_work");
cputime = curr_cputime(); bch2_move_stats_init(&r->scan_stats, "rebalance_scan");
prev_run_time = start - prev_start; bch2_trans_iter_init(trans, &rebalance_work_iter,
prev_run_cputime = cputime - prev_cputime; BTREE_ID_rebalance_work, POS_MIN,
BTREE_ITER_ALL_SNAPSHOTS);
w = rebalance_work(c); while (!bch2_move_ratelimit(ctxt) &&
BUG_ON(!w.dev_most_full_capacity); !kthread_wait_freezable(r->enabled)) {
bch2_trans_begin(trans);
if (!w.total_work) { ret = bkey_err(k = next_rebalance_entry(trans, &rebalance_work_iter));
r->state = REBALANCE_WAITING; if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
kthread_wait_freezable(rebalance_work(c).total_work);
continue; continue;
} if (ret || !k.k)
break;
/* ret = k.k->type == KEY_TYPE_cookie
* If there isn't much work to do, throttle cpu usage: ? do_rebalance_scan(ctxt, k.k->p.inode,
*/ le64_to_cpu(bkey_s_c_to_cookie(k).v->cookie))
throttle = prev_run_cputime * 100 / : do_rebalance_extent(ctxt, k.k->p, &extent_iter);
max(1U, w.dev_most_full_percent) -
prev_run_time; if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
if (w.dev_most_full_percent < 20 && throttle > 0) {
r->throttled_until_iotime = io_start +
div_u64(w.dev_most_full_capacity *
(20 - w.dev_most_full_percent),
50);
if (atomic64_read(&clock->now) + clock->max_slop <
r->throttled_until_iotime) {
r->throttled_until_cputime = start + throttle;
r->state = REBALANCE_THROTTLED;
bch2_kthread_io_clock_wait(clock,
r->throttled_until_iotime,
throttle);
continue; continue;
} if (ret)
break;
bch2_btree_iter_advance(&rebalance_work_iter);
} }
/* minimum 1 mb/sec: */ bch2_trans_iter_exit(trans, &extent_iter);
r->pd.rate.rate = bch2_trans_iter_exit(trans, &rebalance_work_iter);
max_t(u64, 1 << 11, bch2_move_stats_exit(&r->scan_stats, c);
r->pd.rate.rate *
max(p.dev_most_full_percent, 1U) / if (!ret &&
max(w.dev_most_full_percent, 1U)); !kthread_should_stop() &&
!atomic64_read(&r->work_stats.sectors_seen) &&
io_start = atomic64_read(&clock->now); !atomic64_read(&r->scan_stats.sectors_seen)) {
p = w; bch2_trans_unlock(trans);
prev_start = start; rebalance_wait(c);
prev_cputime = cputime;
r->state = REBALANCE_RUNNING;
memset(&move_stats, 0, sizeof(move_stats));
rebalance_work_reset(c);
bch2_move_data(c,
BBPOS_MIN, BBPOS_MAX,
/* ratelimiting disabled for now */
NULL, /* &r->pd.rate, */
&move_stats,
writepoint_ptr(&c->rebalance_write_point),
true,
rebalance_pred, NULL);
} }
return 0; if (!bch2_err_matches(ret, EROFS))
bch_err_fn(c, ret);
return ret;
} }
void bch2_rebalance_work_to_text(struct printbuf *out, struct bch_fs *c) static int bch2_rebalance_thread(void *arg)
{ {
struct bch_fs *c = arg;
struct bch_fs_rebalance *r = &c->rebalance; struct bch_fs_rebalance *r = &c->rebalance;
struct rebalance_work w = rebalance_work(c); struct moving_context ctxt;
int ret;
if (!out->nr_tabstops) set_freezable();
printbuf_tabstop_push(out, 20);
prt_printf(out, "fullest_dev (%i):", w.dev_most_full_idx); bch2_moving_ctxt_init(&ctxt, c, NULL, &r->work_stats,
prt_tab(out); writepoint_ptr(&c->rebalance_write_point),
true);
prt_human_readable_u64(out, w.dev_most_full_work << 9); while (!kthread_should_stop() &&
prt_printf(out, "/"); !(ret = do_rebalance(&ctxt)))
prt_human_readable_u64(out, w.dev_most_full_capacity << 9); ;
bch2_moving_ctxt_exit(&ctxt);
return 0;
}
void bch2_rebalance_status_to_text(struct printbuf *out, struct bch_fs *c)
{
struct bch_fs_rebalance *r = &c->rebalance;
prt_str(out, bch2_rebalance_state_strs[r->state]);
prt_newline(out); prt_newline(out);
printbuf_indent_add(out, 2);
prt_printf(out, "total work:"); switch (r->state) {
prt_tab(out); case BCH_REBALANCE_waiting: {
u64 now = atomic64_read(&c->io_clock[WRITE].now);
prt_human_readable_u64(out, w.total_work << 9); prt_str(out, "io wait duration: ");
prt_printf(out, "/"); bch2_prt_human_readable_s64(out, r->wait_iotime_end - r->wait_iotime_start);
prt_human_readable_u64(out, c->capacity << 9);
prt_newline(out); prt_newline(out);
prt_printf(out, "rate:"); prt_str(out, "io wait remaining: ");
prt_tab(out); bch2_prt_human_readable_s64(out, r->wait_iotime_end - now);
prt_printf(out, "%u", r->pd.rate.rate);
prt_newline(out); prt_newline(out);
switch (r->state) { prt_str(out, "duration waited: ");
case REBALANCE_WAITING: bch2_pr_time_units(out, ktime_get_real_ns() - r->wait_wallclock_start);
prt_printf(out, "waiting"); prt_newline(out);
break; break;
case REBALANCE_THROTTLED: }
prt_printf(out, "throttled for %lu sec or ", case BCH_REBALANCE_working:
(r->throttled_until_cputime - jiffies) / HZ); bch2_move_stats_to_text(out, &r->work_stats);
prt_human_readable_u64(out,
(r->throttled_until_iotime -
atomic64_read(&c->io_clock[WRITE].now)) << 9);
prt_printf(out, " io");
break; break;
case REBALANCE_RUNNING: case BCH_REBALANCE_scanning:
prt_printf(out, "running"); bch2_move_stats_to_text(out, &r->scan_stats);
break; break;
} }
prt_newline(out); prt_newline(out);
printbuf_indent_sub(out, 2);
} }
void bch2_rebalance_stop(struct bch_fs *c) void bch2_rebalance_stop(struct bch_fs *c)
...@@ -360,6 +463,4 @@ int bch2_rebalance_start(struct bch_fs *c) ...@@ -360,6 +463,4 @@ int bch2_rebalance_start(struct bch_fs *c)
void bch2_fs_rebalance_init(struct bch_fs *c) void bch2_fs_rebalance_init(struct bch_fs *c)
{ {
bch2_pd_controller_init(&c->rebalance.pd); bch2_pd_controller_init(&c->rebalance.pd);
atomic64_set(&c->rebalance.work_unknown_dev, S64_MAX);
} }
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include "rebalance_types.h" #include "rebalance_types.h"
int bch2_set_rebalance_needs_scan(struct bch_fs *, u64 inum);
int bch2_set_fs_needs_rebalance(struct bch_fs *);
static inline void rebalance_wakeup(struct bch_fs *c) static inline void rebalance_wakeup(struct bch_fs *c)
{ {
struct task_struct *p; struct task_struct *p;
...@@ -15,11 +18,7 @@ static inline void rebalance_wakeup(struct bch_fs *c) ...@@ -15,11 +18,7 @@ static inline void rebalance_wakeup(struct bch_fs *c)
rcu_read_unlock(); rcu_read_unlock();
} }
void bch2_rebalance_add_key(struct bch_fs *, struct bkey_s_c, void bch2_rebalance_status_to_text(struct printbuf *, struct bch_fs *);
struct bch_io_opts *);
void bch2_rebalance_add_work(struct bch_fs *, u64);
void bch2_rebalance_work_to_text(struct printbuf *, struct bch_fs *);
void bch2_rebalance_stop(struct bch_fs *); void bch2_rebalance_stop(struct bch_fs *);
int bch2_rebalance_start(struct bch_fs *); int bch2_rebalance_start(struct bch_fs *);
......
...@@ -2,23 +2,34 @@ ...@@ -2,23 +2,34 @@
#ifndef _BCACHEFS_REBALANCE_TYPES_H #ifndef _BCACHEFS_REBALANCE_TYPES_H
#define _BCACHEFS_REBALANCE_TYPES_H #define _BCACHEFS_REBALANCE_TYPES_H
#include "bbpos_types.h"
#include "move_types.h" #include "move_types.h"
enum rebalance_state { #define BCH_REBALANCE_STATES() \
REBALANCE_WAITING, x(waiting) \
REBALANCE_THROTTLED, x(working) \
REBALANCE_RUNNING, x(scanning)
enum bch_rebalance_states {
#define x(t) BCH_REBALANCE_##t,
BCH_REBALANCE_STATES()
#undef x
}; };
struct bch_fs_rebalance { struct bch_fs_rebalance {
struct task_struct __rcu *thread; struct task_struct __rcu *thread;
struct bch_pd_controller pd; struct bch_pd_controller pd;
atomic64_t work_unknown_dev; enum bch_rebalance_states state;
u64 wait_iotime_start;
u64 wait_iotime_end;
u64 wait_wallclock_start;
struct bch_move_stats work_stats;
enum rebalance_state state; struct bbpos scan_start;
u64 throttled_until_iotime; struct bbpos scan_end;
unsigned long throttled_until_cputime; struct bch_move_stats scan_stats;
unsigned enabled:1; unsigned enabled:1;
}; };
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "logged_ops.h" #include "logged_ops.h"
#include "move.h" #include "move.h"
#include "quota.h" #include "quota.h"
#include "rebalance.h"
#include "recovery.h" #include "recovery.h"
#include "replicas.h" #include "replicas.h"
#include "sb-clean.h" #include "sb-clean.h"
......
...@@ -42,6 +42,7 @@ ...@@ -42,6 +42,7 @@
x(check_nlinks, PASS_FSCK) \ x(check_nlinks, PASS_FSCK) \
x(delete_dead_inodes, PASS_FSCK|PASS_UNCLEAN) \ x(delete_dead_inodes, PASS_FSCK|PASS_UNCLEAN) \
x(fix_reflink_p, 0) \ x(fix_reflink_p, 0) \
x(set_fs_needs_rebalance, 0) \
enum bch_recovery_pass { enum bch_recovery_pass {
#define x(n, when) BCH_RECOVERY_PASS_##n, #define x(n, when) BCH_RECOVERY_PASS_##n,
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "inode.h" #include "inode.h"
#include "io_misc.h" #include "io_misc.h"
#include "io_write.h" #include "io_write.h"
#include "rebalance.h"
#include "reflink.h" #include "reflink.h"
#include "subvolume.h" #include "subvolume.h"
#include "super-io.h" #include "super-io.h"
...@@ -252,8 +253,9 @@ s64 bch2_remap_range(struct bch_fs *c, ...@@ -252,8 +253,9 @@ s64 bch2_remap_range(struct bch_fs *c,
struct bpos dst_start = POS(dst_inum.inum, dst_offset); struct bpos dst_start = POS(dst_inum.inum, dst_offset);
struct bpos src_start = POS(src_inum.inum, src_offset); struct bpos src_start = POS(src_inum.inum, src_offset);
struct bpos dst_end = dst_start, src_end = src_start; struct bpos dst_end = dst_start, src_end = src_start;
struct bch_io_opts opts;
struct bpos src_want; struct bpos src_want;
u64 dst_done; u64 dst_done = 0;
u32 dst_snapshot, src_snapshot; u32 dst_snapshot, src_snapshot;
int ret = 0, ret2 = 0; int ret = 0, ret2 = 0;
...@@ -269,6 +271,10 @@ s64 bch2_remap_range(struct bch_fs *c, ...@@ -269,6 +271,10 @@ s64 bch2_remap_range(struct bch_fs *c,
bch2_bkey_buf_init(&new_src); bch2_bkey_buf_init(&new_src);
trans = bch2_trans_get(c); trans = bch2_trans_get(c);
ret = bch2_inum_opts_get(trans, src_inum, &opts);
if (ret)
goto err;
bch2_trans_iter_init(trans, &src_iter, BTREE_ID_extents, src_start, bch2_trans_iter_init(trans, &src_iter, BTREE_ID_extents, src_start,
BTREE_ITER_INTENT); BTREE_ITER_INTENT);
bch2_trans_iter_init(trans, &dst_iter, BTREE_ID_extents, dst_start, bch2_trans_iter_init(trans, &dst_iter, BTREE_ID_extents, dst_start,
...@@ -352,7 +358,10 @@ s64 bch2_remap_range(struct bch_fs *c, ...@@ -352,7 +358,10 @@ s64 bch2_remap_range(struct bch_fs *c,
min(src_k.k->p.offset - src_want.offset, min(src_k.k->p.offset - src_want.offset,
dst_end.offset - dst_iter.pos.offset)); dst_end.offset - dst_iter.pos.offset));
ret = bch2_extent_update(trans, dst_inum, &dst_iter, ret = bch2_bkey_set_needs_rebalance(c, new_dst.k,
opts.background_target,
opts.background_compression) ?:
bch2_extent_update(trans, dst_inum, &dst_iter,
new_dst.k, &disk_res, new_dst.k, &disk_res,
new_i_size, i_sectors_delta, new_i_size, i_sectors_delta,
true); true);
...@@ -386,7 +395,7 @@ s64 bch2_remap_range(struct bch_fs *c, ...@@ -386,7 +395,7 @@ s64 bch2_remap_range(struct bch_fs *c,
bch2_trans_iter_exit(trans, &inode_iter); bch2_trans_iter_exit(trans, &inode_iter);
} while (bch2_err_matches(ret2, BCH_ERR_transaction_restart)); } while (bch2_err_matches(ret2, BCH_ERR_transaction_restart));
err:
bch2_trans_put(trans); bch2_trans_put(trans);
bch2_bkey_buf_exit(&new_src, c); bch2_bkey_buf_exit(&new_src, c);
bch2_bkey_buf_exit(&new_dst, c); bch2_bkey_buf_exit(&new_dst, c);
......
...@@ -212,7 +212,7 @@ read_attribute(copy_gc_wait); ...@@ -212,7 +212,7 @@ read_attribute(copy_gc_wait);
rw_attribute(rebalance_enabled); rw_attribute(rebalance_enabled);
sysfs_pd_controller_attribute(rebalance); sysfs_pd_controller_attribute(rebalance);
read_attribute(rebalance_work); read_attribute(rebalance_status);
rw_attribute(promote_whole_extents); rw_attribute(promote_whole_extents);
read_attribute(new_stripes); read_attribute(new_stripes);
...@@ -386,8 +386,8 @@ SHOW(bch2_fs) ...@@ -386,8 +386,8 @@ SHOW(bch2_fs)
if (attr == &sysfs_copy_gc_wait) if (attr == &sysfs_copy_gc_wait)
bch2_copygc_wait_to_text(out, c); bch2_copygc_wait_to_text(out, c);
if (attr == &sysfs_rebalance_work) if (attr == &sysfs_rebalance_status)
bch2_rebalance_work_to_text(out, c); bch2_rebalance_status_to_text(out, c);
sysfs_print(promote_whole_extents, c->promote_whole_extents); sysfs_print(promote_whole_extents, c->promote_whole_extents);
...@@ -646,7 +646,7 @@ struct attribute *bch2_fs_internal_files[] = { ...@@ -646,7 +646,7 @@ struct attribute *bch2_fs_internal_files[] = {
&sysfs_copy_gc_wait, &sysfs_copy_gc_wait,
&sysfs_rebalance_enabled, &sysfs_rebalance_enabled,
&sysfs_rebalance_work, &sysfs_rebalance_status,
sysfs_pd_controller_files(rebalance), sysfs_pd_controller_files(rebalance),
&sysfs_moving_ctxts, &sysfs_moving_ctxts,
...@@ -707,10 +707,8 @@ STORE(bch2_fs_opts_dir) ...@@ -707,10 +707,8 @@ STORE(bch2_fs_opts_dir)
bch2_opt_set_by_id(&c->opts, id, v); bch2_opt_set_by_id(&c->opts, id, v);
if ((id == Opt_background_target || if ((id == Opt_background_target ||
id == Opt_background_compression) && v) { id == Opt_background_compression) && v)
bch2_rebalance_add_work(c, S64_MAX); bch2_set_rebalance_needs_scan(c, 0);
rebalance_wakeup(c);
}
ret = size; ret = size;
err: err:
......
...@@ -590,7 +590,7 @@ static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler, ...@@ -590,7 +590,7 @@ static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
if (value && if (value &&
(opt_id == Opt_background_compression || (opt_id == Opt_background_compression ||
opt_id == Opt_background_target)) opt_id == Opt_background_target))
bch2_rebalance_add_work(c, inode->v.i_blocks); bch2_set_rebalance_needs_scan(c, inode->ei_inode.bi_inum);
return bch2_err_class(ret); return bch2_err_class(ret);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment