Skip to contentSkip to conversation

rebalance + reflink improvements

PATCH New Kent Overstreet 1 participant 13 messages Oct 21, 2024

lore
Share & tools
lore
#0 lore raw

cover rebalance + reflink improvements

Reed Riley found an interesting bug where too many cached replicas were being kept around in extents, and the data update path barfed when it hit the limit.

Fixing this meant adding some sort of policy for when we keep a cached copy around (answer: we now keep one, if a promote target is set and it's on the promote target).

But for that to work properly, the data update path needs to know what the promote target is...

This series plumbs the full set of io path options that apply to rebalance through bch_extent_rebalance (i.e. they're kept with the extent when needed, meaning they're available for indirect extents), regularizes and cleans up a fair amount of stuff, and makes it possible for new options to take effect over the options in bch_extent_rebalance, in the extent (we now track whether the option came from the inode or the filesystem, so inode options can correctly take precedence).

data_replicas and data_checksum are now plumbed through bch_extent_rebalance, as well.

Things that did not make the cut:

- Setting new options on a file doesn't yet apply them to indirect extents. I wrote the code for this, but it's a security hole without knowing if this was "your" reflinked data, or copied from another user. Handling this will probably require bch_reflink_p_v2.

- Rebalance doesn't yet apply new data_replicas settings in the background, like it does for the compression and target options (this would remove the need to manually run 'bcachefs rereplicate'.

This one will be easier to do, but I think I want to do some cleanup on data_opts next.

The first patch in the series will go to Linus shortly, the rest next merge window.

Kent Overstreet (12):
bcachefs: Don't keep tons of cached pointers around
bcachefs: use bch2_data_update_opts_to_text() in
trace_move_extent_fail()
bcachefs: bch2_io_opts_fixups()
bcachefs: small cleanup for extent ptr bitmasks
bcachefs: kill bch2_bkey_needs_rebalance()
bcachefs: kill __bch2_bkey_sectors_need_rebalance()
bcachefs: rename bch_extent_rebalance fields to match other opts
structs
bcachefs: io_opts_to_rebalance_opts()
bcachefs: Add bch_io_opts fields for indicating whether the opts came
from the inode
bcachefs: New bch_extent_rebalance fields
bcachefs: bch2_indirect_extent_error()
bcachefs: bch2_write_inode() now checks for changing rebalance options

 fs/bcachefs/data_update.c    |  88 +++++++-------
 fs/bcachefs/data_update.h    |   3 +-
 fs/bcachefs/extents.c        | 223 +++++++++++++++++++----------------
 fs/bcachefs/extents.h        |  13 +-
 fs/bcachefs/extents_format.h |  45 ++++++-
 fs/bcachefs/fs.c             |  26 +++-
 fs/bcachefs/inode.c          |  12 +-
 fs/bcachefs/inode.h          |   8 ++
 fs/bcachefs/io_misc.c        |   2 +-
 fs/bcachefs/io_read.c        |  31 ++---
 fs/bcachefs/io_write.c       |   2 +-
 fs/bcachefs/move.c           |  90 ++++++++++----
 fs/bcachefs/move.h           |   5 +-
 fs/bcachefs/opts.c           |   5 +-
 fs/bcachefs/opts.h           |  29 ++++-
 fs/bcachefs/rebalance.c      |  48 +++-----
 fs/bcachefs/rebalance.h      |   1 +
 fs/bcachefs/reflink.c        |  15 ++-
 fs/bcachefs/reflink.h        |   2 +
 fs/bcachefs/xattr.c          |   7 --
 20 files changed, 393 insertions(+), 262 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/12 bcachefs: Don't keep tons of cached pointers around

We had a bug report where the data update path was creating an extent that failed to validate because it had too many pointers; almost all of them were cached.

To fix this, we have:
- want_cached_ptr(), a new helper that checks if we even want a cached pointer (is on appropriate target, device is readable).

- bch2_extent_set_ptr_cached() now only sets a pointer cached if we want it.

- bch2_extent_normalize_by_opts() now ensures that we only have a single cached pointer that we want.

While working on this, it was noticed that this doesn't work well with reflinked data and per-file options. Another patch series is coming that plumbs through additional io path options through bch_extent_rebalance, with improved option handling.

---
 fs/bcachefs/data_update.c | 21 ++++++----
 fs/bcachefs/data_update.h |  3 +-
 fs/bcachefs/extents.c     | 86 +++++++++++++++++++++++++++++++--------
 fs/bcachefs/extents.h     |  5 ++-
 fs/bcachefs/move.c        |  2 +-
 5 files changed, 89 insertions(+), 28 deletions(-)
fs/bcachefs/data_update.c +12 -9
index a6ee0beee6b0..8e75a852b358 100644
@@ -236,7 +236,8 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
 			if (((1U << i) & m->data_opts.rewrite_ptrs) &&
 			    (ptr = bch2_extent_has_ptr(old, p, bkey_i_to_s(insert))) &&
 			    !ptr->cached) {
-				bch2_extent_ptr_set_cached(bkey_i_to_s(insert), ptr);
+				bch2_extent_ptr_set_cached(c, &m->op.opts,
+							   bkey_i_to_s(insert), ptr);
 				rewrites_found |= 1U << i;
 			}
 			i++;
@@ -284,7 +285,8 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
 			    durability - ptr_durability >= m->op.opts.data_replicas) {
 				durability -= ptr_durability;
 
-				bch2_extent_ptr_set_cached(bkey_i_to_s(insert), &entry->ptr);
+				bch2_extent_ptr_set_cached(c, &m->op.opts,
+							   bkey_i_to_s(insert), &entry->ptr);
 				goto restart_drop_extra_replicas;
 			}
 		}
@@ -295,7 +297,7 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
 			bch2_extent_ptr_decoded_append(insert, &p);
 
 		bch2_bkey_narrow_crcs(insert, (struct bch_extent_crc_unpacked) { 0 });
-		bch2_extent_normalize(c, bkey_i_to_s(insert));
+		bch2_extent_normalize_by_opts(c, &m->op.opts, bkey_i_to_s(insert));
 
 		ret = bch2_sum_sector_overwrites(trans, &iter, insert,
 						 &should_check_enospc,
@@ -558,7 +560,8 @@ void bch2_data_update_to_text(struct printbuf *out, struct data_update *m)
 int bch2_extent_drop_ptrs(struct btree_trans *trans,
 			  struct btree_iter *iter,
 			  struct bkey_s_c k,
-			  struct data_update_opts data_opts)
+			  struct bch_io_opts *io_opts,
+			  struct data_update_opts *data_opts)
 {
 	struct bch_fs *c = trans->c;
 	struct bkey_i *n;
@@ -569,11 +572,11 @@ int bch2_extent_drop_ptrs(struct btree_trans *trans,
 	if (ret)
 		return ret;
 
-	while (data_opts.kill_ptrs) {
-		unsigned i = 0, drop = __fls(data_opts.kill_ptrs);
+	while (data_opts->kill_ptrs) {
+		unsigned i = 0, drop = __fls(data_opts->kill_ptrs);
 
 		bch2_bkey_drop_ptrs_noerror(bkey_i_to_s(n), ptr, i++ == drop);
-		data_opts.kill_ptrs ^= 1U << drop;
+		data_opts->kill_ptrs ^= 1U << drop;
 	}
 
 	/*
@@ -581,7 +584,7 @@ int bch2_extent_drop_ptrs(struct btree_trans *trans,
 	 * will do the appropriate thing with it (turning it into a
 	 * KEY_TYPE_error key, or just a discard if it was a cached extent)
 	 */
-	bch2_extent_normalize(c, bkey_i_to_s(n));
+	bch2_extent_normalize_by_opts(c, io_opts, bkey_i_to_s(n));
 
 	/*
 	 * Since we're not inserting through an extent iterator
@@ -720,7 +723,7 @@ int bch2_data_update_init(struct btree_trans *trans,
 		m->data_opts.rewrite_ptrs = 0;
 		/* if iter == NULL, it's just a promote */
 		if (iter)
-			ret = bch2_extent_drop_ptrs(trans, iter, k, m->data_opts);
+			ret = bch2_extent_drop_ptrs(trans, iter, k, &io_opts, &m->data_opts);
 		goto out;
 	}
 
fs/bcachefs/data_update.h +2 -1
index 8d36365bdea8..e4b50723428e 100644
@@ -40,7 +40,8 @@ void bch2_data_update_read_done(struct data_update *,
 int bch2_extent_drop_ptrs(struct btree_trans *,
 			  struct btree_iter *,
 			  struct bkey_s_c,
-			  struct data_update_opts);
+			  struct bch_io_opts *,
+			  struct data_update_opts *);
 
 void bch2_data_update_exit(struct data_update *);
 int bch2_data_update_init(struct btree_trans *, struct btree_iter *,
fs/bcachefs/extents.c +70 -16

109 lines not on this page · raw message

fs/bcachefs/extents.h +4 -1
index 923a5f1849a8..bcffcf60aaaf 100644
@@ -686,9 +686,12 @@ bool bch2_extents_match(struct bkey_s_c, struct bkey_s_c);
 struct bch_extent_ptr *
 bch2_extent_has_ptr(struct bkey_s_c, struct extent_ptr_decoded, struct bkey_s);
 
-void bch2_extent_ptr_set_cached(struct bkey_s, struct bch_extent_ptr *);
+void bch2_extent_ptr_set_cached(struct bch_fs *, struct bch_io_opts *,
+				struct bkey_s, struct bch_extent_ptr *);
 
+bool bch2_extent_normalize_by_opts(struct bch_fs *, struct bch_io_opts *, struct bkey_s);
 bool bch2_extent_normalize(struct bch_fs *, struct bkey_s);
+
 void bch2_extent_ptr_to_text(struct printbuf *out, struct bch_fs *, const struct bch_extent_ptr *);
 void bch2_bkey_ptrs_to_text(struct printbuf *, struct bch_fs *,
 			    struct bkey_s_c);
fs/bcachefs/move.c +1 -1
index 8c456d8b8b99..0ef4a86850bb 100644
@@ -266,7 +266,7 @@ int bch2_move_extent(struct moving_context *ctxt,
 	if (!data_opts.rewrite_ptrs &&
 	    !data_opts.extra_replicas) {
 		if (data_opts.kill_ptrs)
-			return bch2_extent_drop_ptrs(trans, iter, k, data_opts);
+			return bch2_extent_drop_ptrs(trans, iter, k, &io_opts, &data_opts);
 		return 0;
 	}
 
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/12 bcachefs: use bch2_data_update_opts_to_text() in trace_move_extent_fail()

In reply to Kent Overstreet
---
 fs/bcachefs/data_update.c | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)
fs/bcachefs/data_update.c +14 -19
index 8e75a852b358..5ea432bc0052 100644
@@ -110,11 +110,8 @@ static void trace_move_extent_fail2(struct data_update *m,
 {
 	struct bch_fs *c = m->op.c;
 	struct bkey_s_c old = bkey_i_to_s_c(m->k.k);
-	const union bch_extent_entry *entry;
-	struct bch_extent_ptr *ptr;
-	struct extent_ptr_decoded p;
 	struct printbuf buf = PRINTBUF;
-	unsigned i, rewrites_found = 0;
+	unsigned rewrites_found = 0;
 
 	if (!trace_move_extent_fail_enabled())
 		return;
@@ -122,27 +119,25 @@ static void trace_move_extent_fail2(struct data_update *m,
 	prt_str(&buf, msg);
 
 	if (insert) {
-		i = 0;
+		const union bch_extent_entry *entry;
+		struct bch_extent_ptr *ptr;
+		struct extent_ptr_decoded p;
+
+		unsigned ptr_bit = 1;
 		bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry) {
-			if (((1U << i) & m->data_opts.rewrite_ptrs) &&
+			if ((ptr_bit & m->data_opts.rewrite_ptrs) &&
 			    (ptr = bch2_extent_has_ptr(old, p, bkey_i_to_s(insert))) &&
 			    !ptr->cached)
-				rewrites_found |= 1U << i;
-			i++;
+				rewrites_found |= ptr_bit;
+			ptr_bit <<= 1;
 		}
 	}
 
-	prt_printf(&buf, "\nrewrite ptrs:   %u%u%u%u",
-		   (m->data_opts.rewrite_ptrs & (1 << 0)) != 0,
-		   (m->data_opts.rewrite_ptrs & (1 << 1)) != 0,
-		   (m->data_opts.rewrite_ptrs & (1 << 2)) != 0,
-		   (m->data_opts.rewrite_ptrs & (1 << 3)) != 0);
-
-	prt_printf(&buf, "\nrewrites found: %u%u%u%u",
-		   (rewrites_found & (1 << 0)) != 0,
-		   (rewrites_found & (1 << 1)) != 0,
-		   (rewrites_found & (1 << 2)) != 0,
-		   (rewrites_found & (1 << 3)) != 0);
+	prt_str(&buf, "rewrites found:\t");
+	bch2_prt_u64_base2(&buf, rewrites_found);
+	prt_newline(&buf);
+
+	bch2_data_update_opts_to_text(&buf, c, &m->op.opts, &m->data_opts);
 
 	prt_str(&buf, "\nold:    ");
 	bch2_bkey_val_to_text(&buf, c, old);
Signature · 1 line
2.45.2
#3 lore raw

PATCH 3/12 bcachefs: bch2_io_opts_fixups()

In reply to Kent Overstreet

Centralize some io path option fixups - they weren't always being applied correctly:

- background_compression uses compression if unset
- background_target uses foreground_target if unset
- nocow disables most fancy io path options

---
 fs/bcachefs/data_update.c |  4 ++--
 fs/bcachefs/extents.c     |  2 +-
 fs/bcachefs/inode.c       |  4 ++--
 fs/bcachefs/opts.c        |  5 ++++-
 fs/bcachefs/opts.h        | 12 ++++++++++--
 fs/bcachefs/rebalance.c   |  4 ++--
 6 files changed, 21 insertions(+), 10 deletions(-)
fs/bcachefs/data_update.c +2 -2
index 5ea432bc0052..a176e5439cbf 100644
@@ -535,7 +535,7 @@ void bch2_data_update_opts_to_text(struct printbuf *out, struct bch_fs *c,
 	prt_newline(out);
 
 	prt_str(out, "compression:\t");
-	bch2_compression_opt_to_text(out, background_compression(*io_opts));
+	bch2_compression_opt_to_text(out, io_opts->background_compression);
 	prt_newline(out);
 
 	prt_str(out, "opts.replicas:\t");
@@ -647,7 +647,7 @@ int bch2_data_update_init(struct btree_trans *trans,
 		BCH_WRITE_DATA_ENCODED|
 		BCH_WRITE_MOVE|
 		m->data_opts.write_flags;
-	m->op.compression_opt	= background_compression(io_opts);
+	m->op.compression_opt	= io_opts.background_compression;
 	m->op.watermark		= m->data_opts.btree_insert_flags & BCH_WATERMARK_MASK;
 
 	unsigned durability_have = 0, durability_removing = 0;
fs/bcachefs/extents.c +1 -1
index 33f611f13570..f0d9390250c4 100644
@@ -1501,7 +1501,7 @@ int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bkey_i *_k,
 	struct bkey_s k = bkey_i_to_s(_k);
 	struct bch_extent_rebalance *r;
 	unsigned target = opts->background_target;
-	unsigned compression = background_compression(*opts);
+	unsigned compression = opts->background_compression;
 	bool needs_rebalance;
 
 	if (!bkey_extent_is_direct_data(k.k))
fs/bcachefs/inode.c +2 -2
index 46310fcdd2b3..3af6029ea9b7 100644
@@ -14,6 +14,7 @@
 #include "extent_update.h"
 #include "fs.h"
 #include "inode.h"
+#include "opts.h"
 #include "str_hash.h"
 #include "snapshot.h"
 #include "subvolume.h"
@@ -1145,8 +1146,7 @@ void bch2_inode_opts_get(struct bch_io_opts *opts, struct bch_fs *c,
 	BCH_INODE_OPTS()
 #undef x
 
-	if (opts->nocow)
-		opts->compression = opts->background_compression = opts->data_checksum = opts->erasure_code = 0;
+	bch2_io_opts_fixups(opts);
 }
 
 int bch2_inum_opts_get(struct btree_trans *trans, subvol_inum inum, struct bch_io_opts *opts)
fs/bcachefs/opts.c +4 -1
index 6673cbd8bdb9..3fa8a85b008b 100644
@@ -710,11 +710,14 @@ void bch2_opt_set_sb(struct bch_fs *c, struct bch_dev *ca,
 
 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
 {
-	return (struct bch_io_opts) {
+	struct bch_io_opts opts = {
 #define x(_name, _bits)	._name = src._name,
 	BCH_INODE_OPTS()
 #undef x
 	};
+
+	bch2_io_opts_fixups(&opts);
+	return opts;
 }
 
 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
fs/bcachefs/opts.h +10 -2
index 23dda014e331..dd27ef556611 100644
@@ -626,9 +626,17 @@ struct bch_io_opts {
 #undef x
 };
 
-static inline unsigned background_compression(struct bch_io_opts opts)
+static inline void bch2_io_opts_fixups(struct bch_io_opts *opts)
 {
-	return opts.background_compression ?: opts.compression;
+	if (!opts->background_target)
+		opts->background_target = opts->foreground_target;
+	if (!opts->background_compression)
+		opts->background_compression = opts->compression;
+	if (opts->nocow) {
+		opts->compression = opts->background_compression = 0;
+		opts->data_checksum = 0;
+		opts->erasure_code = 0;
+	}
 }
 
 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts);
fs/bcachefs/rebalance.c +2 -2
index cd6647374353..2f93ae8781bb 100644
@@ -257,12 +257,12 @@ static bool rebalance_pred(struct bch_fs *c, void *arg,
 
 	if (k.k->p.inode) {
 		target		= io_opts->background_target;
-		compression	= background_compression(*io_opts);
+		compression	= io_opts->background_compression;
 	} else {
 		const struct bch_extent_rebalance *r = bch2_bkey_rebalance_opts(k);
 
 		target		= r ? r->target : io_opts->background_target;
-		compression	= r ? r->compression : background_compression(*io_opts);
+		compression	= r ? r->compression : io_opts->background_compression;
 	}
 
 	data_opts->rewrite_ptrs		= bch2_bkey_ptrs_need_rebalance(c, k, target, compression);
Signature · 1 line
2.45.2
#4 lore raw

PATCH 4/12 bcachefs: small cleanup for extent ptr bitmasks

In reply to Kent Overstreet
---
 fs/bcachefs/data_update.c | 30 +++++++++++++++---------------
 fs/bcachefs/extents.c     | 12 ++++++------
 fs/bcachefs/io_read.c     |  6 +++---
 3 files changed, 24 insertions(+), 24 deletions(-)
fs/bcachefs/data_update.c +15 -15
index a176e5439cbf..90da57a26962 100644
@@ -189,7 +189,7 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
 		struct bpos next_pos;
 		bool should_check_enospc;
 		s64 i_sectors_delta = 0, disk_sectors_delta = 0;
-		unsigned rewrites_found = 0, durability, i;
+		unsigned rewrites_found = 0, durability, ptr_bit;
 
 		bch2_trans_begin(trans);
 
@@ -226,16 +226,16 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
 		 *
 		 * Fist, drop rewrite_ptrs from @new:
 		 */
-		i = 0;
+		ptr_bit = 1;
 		bkey_for_each_ptr_decode(old.k, bch2_bkey_ptrs_c(old), p, entry_c) {
-			if (((1U << i) & m->data_opts.rewrite_ptrs) &&
+			if ((ptr_bit & m->data_opts.rewrite_ptrs) &&
 			    (ptr = bch2_extent_has_ptr(old, p, bkey_i_to_s(insert))) &&
 			    !ptr->cached) {
 				bch2_extent_ptr_set_cached(c, &m->op.opts,
 							   bkey_i_to_s(insert), ptr);
-				rewrites_found |= 1U << i;
+				rewrites_found |= ptr_bit;
 			}
-			i++;
+			ptr_bit <<= 1;
 		}
 
 		if (m->data_opts.rewrite_ptrs &&
@@ -609,7 +609,7 @@ int bch2_data_update_init(struct btree_trans *trans,
 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
 	const union bch_extent_entry *entry;
 	struct extent_ptr_decoded p;
-	unsigned i, reserve_sectors = k.k->size * data_opts.extra_replicas;
+	unsigned reserve_sectors = k.k->size * data_opts.extra_replicas;
 	int ret = 0;
 
 	/*
@@ -652,17 +652,17 @@ int bch2_data_update_init(struct btree_trans *trans,
 
 	unsigned durability_have = 0, durability_removing = 0;
 
-	i = 0;
+	unsigned ptr_bit = 1;
 	bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
 		if (!p.ptr.cached) {
 			rcu_read_lock();
-			if (BIT(i) & m->data_opts.rewrite_ptrs) {
+			if (ptr_bit & m->data_opts.rewrite_ptrs) {
 				if (crc_is_compressed(p.crc))
 					reserve_sectors += k.k->size;
 
 				m->op.nr_replicas += bch2_extent_ptr_desired_durability(c, &p);
 				durability_removing += bch2_extent_ptr_desired_durability(c, &p);
-			} else if (!(BIT(i) & m->data_opts.kill_ptrs)) {
+			} else if (!(ptr_bit & m->data_opts.kill_ptrs)) {
 				bch2_dev_list_add_dev(&m->op.devs_have, p.ptr.dev);
 				durability_have += bch2_extent_ptr_durability(c, &p);
 			}
@@ -682,7 +682,7 @@ int bch2_data_update_init(struct btree_trans *trans,
 		if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
 			m->op.incompressible = true;
 
-		i++;
+		ptr_bit <<= 1;
 	}
 
 	unsigned durability_required = max(0, (int) (io_opts.data_replicas - durability_have));
@@ -745,14 +745,14 @@ int bch2_data_update_init(struct btree_trans *trans,
 void bch2_data_update_opts_normalize(struct bkey_s_c k, struct data_update_opts *opts)
 {
 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
-	unsigned i = 0;
+	unsigned ptr_bit = 1;
 
 	bkey_for_each_ptr(ptrs, ptr) {
-		if ((opts->rewrite_ptrs & (1U << i)) && ptr->cached) {
-			opts->kill_ptrs |= 1U << i;
-			opts->rewrite_ptrs ^= 1U << i;
+		if ((opts->rewrite_ptrs & ptr_bit) && ptr->cached) {
+			opts->kill_ptrs |= ptr_bit;
+			opts->rewrite_ptrs ^= ptr_bit;
 		}
 
-		i++;
+		ptr_bit <<= 1;
 	}
 }
fs/bcachefs/extents.c +6 -6
index f0d9390250c4..87e7969b55f4 100644
@@ -1411,7 +1411,7 @@ unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *c, struct bkey_s_c k,
 		unsigned compression_type = bch2_compression_opt_to_type(compression);
 		const union bch_extent_entry *entry;
 		struct extent_ptr_decoded p;
-		unsigned i = 0;
+		unsigned ptr_bit = 1;
 
 		bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
 			if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible ||
@@ -1421,18 +1421,18 @@ unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *c, struct bkey_s_c k,
 			}
 
 			if (!p.ptr.cached && p.crc.compression_type != compression_type)
-				rewrite_ptrs |= 1U << i;
-			i++;
+				rewrite_ptrs |= ptr_bit;
+			ptr_bit <<= 1;
 		}
 	}
 incompressible:
 	if (target && bch2_target_accepts_data(c, BCH_DATA_user, target)) {
-		unsigned i = 0;
+		unsigned ptr_bit = 1;
 
 		bkey_for_each_ptr(ptrs, ptr) {
 			if (!ptr->cached && !bch2_dev_in_target(c, ptr->dev, target))
-				rewrite_ptrs |= 1U << i;
-			i++;
+				rewrite_ptrs |= ptr_bit;
+			ptr_bit <<= 1;
 		}
 	}
 
fs/bcachefs/io_read.c +3 -3
index fc246f342820..13dc35cd6562 100644
@@ -231,11 +231,11 @@ static struct promote_op *__promote_alloc(struct btree_trans *trans,
 		update_opts.target = opts.foreground_target;
 
 		struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
-		unsigned i = 0;
+		unsigned ptr_bit = 1;
 		bkey_for_each_ptr(ptrs, ptr) {
 			if (bch2_dev_io_failures(failed, ptr->dev))
-				update_opts.rewrite_ptrs |= BIT(i);
-			i++;
+				update_opts.rewrite_ptrs |= ptr_bit;
+			ptr_bit <<= 1;
 		}
 	}
 
Signature · 1 line
2.45.2
#5 lore raw

PATCH 5/12 bcachefs: kill bch2_bkey_needs_rebalance()

In reply to Kent Overstreet

Dead code

---
 fs/bcachefs/extents.c | 17 -----------------
 fs/bcachefs/extents.h |  1 -
 2 files changed, 18 deletions(-)
fs/bcachefs/extents.c +0 -17
index 87e7969b55f4..7a9bc5122674 100644
@@ -1439,23 +1439,6 @@ unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *c, struct bkey_s_c k,
 	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;
-}
-
 static u64 __bch2_bkey_sectors_need_rebalance(struct bch_fs *c, struct bkey_s_c k,
 				       unsigned target, unsigned compression)
 {
fs/bcachefs/extents.h +0 -1
index bcffcf60aaaf..9374599b384d 100644
@@ -713,7 +713,6 @@ 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);
 u64 bch2_bkey_sectors_need_rebalance(struct bch_fs *, struct bkey_s_c);
 
 int bch2_bkey_set_needs_rebalance(struct bch_fs *, struct bkey_i *,
Signature · 1 line
2.45.2
#6 lore raw

PATCH 6/12 bcachefs: kill __bch2_bkey_sectors_need_rebalance()

In reply to Kent Overstreet

Single caller, fold into bch2_bkey_sectors_need_rebalance()

---
 fs/bcachefs/extents.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
fs/bcachefs/extents.c +10 -13
index 7a9bc5122674..a409e1258583 100644
@@ -1439,16 +1439,19 @@ unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *c, struct bkey_s_c k,
 	return rewrite_ptrs;
 }
 
-static u64 __bch2_bkey_sectors_need_rebalance(struct bch_fs *c, struct bkey_s_c k,
-				       unsigned target, unsigned compression)
+u64 bch2_bkey_sectors_need_rebalance(struct bch_fs *c, struct bkey_s_c k)
 {
+	const struct bch_extent_rebalance *opts = bch2_bkey_rebalance_opts(k);
+	if (!opts)
+		return 0;
+
 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
 	const union bch_extent_entry *entry;
 	struct extent_ptr_decoded p;
 	u64 sectors = 0;
 
-	if (compression) {
-		unsigned compression_type = bch2_compression_opt_to_type(compression);
+	if (opts->compression) {
+		unsigned compression_type = bch2_compression_opt_to_type(opts->compression);
 
 		bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
 			if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible ||
@@ -1462,22 +1465,16 @@ static u64 __bch2_bkey_sectors_need_rebalance(struct bch_fs *c, struct bkey_s_c
 		}
 	}
 incompressible:
-	if (target && bch2_target_accepts_data(c, BCH_DATA_user, target)) {
+	if (opts->target &&
+	    bch2_target_accepts_data(c, BCH_DATA_user, opts->target)) {
 		bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
-			if (!p.ptr.cached && !bch2_dev_in_target(c, p.ptr.dev, target))
+			if (!p.ptr.cached && !bch2_dev_in_target(c, p.ptr.dev, opts->target))
 				sectors += p.crc.compressed_size;
 	}
 
 	return sectors;
 }
 
-u64 bch2_bkey_sectors_need_rebalance(struct bch_fs *c, struct bkey_s_c k)
-{
-	const struct bch_extent_rebalance *r = bch2_bkey_rebalance_opts(k);
-
-	return r ? __bch2_bkey_sectors_need_rebalance(c, k, r->target, r->compression) : 0;
-}
-
 int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bkey_i *_k,
 				  struct bch_io_opts *opts)
 {
Signature · 1 line
2.45.2
#7 lore raw

PATCH 7/12 bcachefs: rename bch_extent_rebalance fields to match other opts structs

In reply to Kent Overstreet
---
 fs/bcachefs/extents.c        | 36 ++++++++++++++++++------------------
 fs/bcachefs/extents_format.h |  8 ++++----
 fs/bcachefs/rebalance.c      | 12 ++++++------
 3 files changed, 28 insertions(+), 28 deletions(-)
fs/bcachefs/extents.c +18 -18
index a409e1258583..132669ee7aaa 100644
@@ -1161,11 +1161,11 @@ void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
 
 			prt_str(out, "rebalance: target ");
 			if (c)
-				bch2_target_to_text(out, c, r->target);
+				bch2_target_to_text(out, c, r->background_target);
 			else
-				prt_printf(out, "%u", r->target);
+				prt_printf(out, "%u", r->background_target);
 			prt_str(out, " compression ");
-			bch2_compression_opt_to_text(out, r->compression);
+			bch2_compression_opt_to_text(out, r->background_compression);
 			break;
 		}
 		default:
@@ -1450,8 +1450,8 @@ u64 bch2_bkey_sectors_need_rebalance(struct bch_fs *c, struct bkey_s_c k)
 	struct extent_ptr_decoded p;
 	u64 sectors = 0;
 
-	if (opts->compression) {
-		unsigned compression_type = bch2_compression_opt_to_type(opts->compression);
+	if (opts->background_compression) {
+		unsigned compression_type = bch2_compression_opt_to_type(opts->background_compression);
 
 		bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
 			if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible ||
@@ -1465,10 +1465,10 @@ u64 bch2_bkey_sectors_need_rebalance(struct bch_fs *c, struct bkey_s_c k)
 		}
 	}
 incompressible:
-	if (opts->target &&
-	    bch2_target_accepts_data(c, BCH_DATA_user, opts->target)) {
+	if (opts->background_target &&
+	    bch2_target_accepts_data(c, BCH_DATA_user, opts->background_target)) {
 		bkey_for_each_ptr_decode(k.k, ptrs, p, entry)
-			if (!p.ptr.cached && !bch2_dev_in_target(c, p.ptr.dev, opts->target))
+			if (!p.ptr.cached && !bch2_dev_in_target(c, p.ptr.dev, opts->background_target))
 				sectors += p.crc.compressed_size;
 	}
 
@@ -1497,14 +1497,14 @@ int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bkey_i *_k,
 			 * they're referenced by different inodes with different
 			 * options:
 			 */
-			if (r->target)
-				target = r->target;
-			if (r->compression)
-				compression = r->compression;
+			if (r->background_target)
+				target = r->background_target;
+			if (r->background_compression)
+				compression = r->background_compression;
 		}
 
-		r->target	= target;
-		r->compression	= compression;
+		r->background_target		= target;
+		r->background_compression	= compression;
 	}
 
 	needs_rebalance = bch2_bkey_ptrs_need_rebalance(c, k.s_c, target, compression);
@@ -1512,10 +1512,10 @@ int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bkey_i *_k,
 	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;
+		new->rebalance.type			= 1U << BCH_EXTENT_ENTRY_rebalance;
+		new->rebalance.background_compression	= compression;
+		new->rebalance.background_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) {
 		/*
fs/bcachefs/extents_format.h +4 -4
index 3bd2fdbb0817..2cc3e60f3b12 100644
@@ -205,11 +205,11 @@ struct bch_extent_rebalance {
 #if defined(__LITTLE_ENDIAN_BITFIELD)
 	__u64			type:6,
 				unused:34,
-				compression:8, /* enum bch_compression_opt */
-				target:16;
+				background_compression:8, /* enum bch_compression_opt */
+				background_target:16;
 #elif defined (__BIG_ENDIAN_BITFIELD)
-	__u64			target:16,
-				compression:8,
+	__u64			background_target:16,
+				background_compression:8,
 				unused:34,
 				type:6;
 #endif
fs/bcachefs/rebalance.c +6 -6
index 2f93ae8781bb..dc70e6feaf79 100644
@@ -157,8 +157,8 @@ static struct bkey_s_c next_rebalance_extent(struct btree_trans *trans,
 	memset(data_opts, 0, sizeof(*data_opts));
 
 	data_opts->rewrite_ptrs		=
-		bch2_bkey_ptrs_need_rebalance(c, k, r->target, r->compression);
-	data_opts->target		= r->target;
+		bch2_bkey_ptrs_need_rebalance(c, k, r->background_target, r->background_compression);
+	data_opts->target		= r->background_target;
 	data_opts->write_flags		|= BCH_WRITE_ONLY_SPECIFIED_DEVS;
 
 	if (!data_opts->rewrite_ptrs) {
@@ -179,9 +179,9 @@ static struct bkey_s_c next_rebalance_extent(struct btree_trans *trans,
 		struct printbuf buf = PRINTBUF;
 
 		prt_str(&buf, "target=");
-		bch2_target_to_text(&buf, c, r->target);
+		bch2_target_to_text(&buf, c, r->background_target);
 		prt_str(&buf, " compression=");
-		bch2_compression_opt_to_text(&buf, r->compression);
+		bch2_compression_opt_to_text(&buf, r->background_compression);
 		prt_str(&buf, " ");
 		bch2_bkey_val_to_text(&buf, c, k);
 
@@ -261,8 +261,8 @@ static bool rebalance_pred(struct bch_fs *c, void *arg,
 	} else {
 		const struct bch_extent_rebalance *r = bch2_bkey_rebalance_opts(k);
 
-		target		= r ? r->target : io_opts->background_target;
-		compression	= r ? r->compression : io_opts->background_compression;
+		target		= r ? r->background_target : io_opts->background_target;
+		compression	= r ? r->background_compression : io_opts->background_compression;
 	}
 
 	data_opts->rewrite_ptrs		= bch2_bkey_ptrs_need_rebalance(c, k, target, compression);
Signature · 1 line
2.45.2
#8 lore raw

PATCH 8/12 bcachefs: io_opts_to_rebalance_opts()

In reply to Kent Overstreet

New helper to simplify bch2_bkey_set_needs_rebalance()

---
 fs/bcachefs/data_update.c    |  2 +-
 fs/bcachefs/extents.c        | 60 ++++++++++--------------------------
 fs/bcachefs/extents.h        |  3 +-
 fs/bcachefs/extents_format.h |  5 +++
 fs/bcachefs/io_misc.c        |  2 +-
 fs/bcachefs/io_write.c       |  2 +-
 fs/bcachefs/opts.h           | 13 ++++++++
 fs/bcachefs/reflink.c        |  2 +-
 8 files changed, 39 insertions(+), 50 deletions(-)
fs/bcachefs/data_update.c +1 -1
index 90da57a26962..e4af2ccdf4c8 100644
@@ -357,7 +357,7 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
 						k.k->p, bkey_start_pos(&insert->k)) ?:
 			bch2_insert_snapshot_whiteouts(trans, m->btree_id,
 						k.k->p, insert->k.p) ?:
-			bch2_bkey_set_needs_rebalance(c, insert, &op->opts) ?:
+			bch2_bkey_set_needs_rebalance(c, &op->opts, insert) ?:
 			bch2_trans_update(trans, &iter, insert,
 				BTREE_UPDATE_internal_snapshot_node) ?:
 			bch2_trans_commit(trans, &op->res,
fs/bcachefs/extents.c +16 -44

71 lines not on this page · raw message

fs/bcachefs/extents.h +1 -2
index 9374599b384d..97af0d6e4319 100644
@@ -715,8 +715,7 @@ unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *, struct bkey_s_c,
 				       unsigned, unsigned);
 u64 bch2_bkey_sectors_need_rebalance(struct bch_fs *, struct bkey_s_c);
 
-int bch2_bkey_set_needs_rebalance(struct bch_fs *, struct bkey_i *,
-				  struct bch_io_opts *);
+int bch2_bkey_set_needs_rebalance(struct bch_fs *, struct bch_io_opts *, struct bkey_i *);
 
 /* Generic extent code: */
 
fs/bcachefs/extents_format.h +5 -0
index 2cc3e60f3b12..520697f236c0 100644
@@ -215,6 +215,11 @@ struct bch_extent_rebalance {
 #endif
 };
 
+/* subset of BCH_INODE_OPTS */
+#define BCH_REBALANCE_OPTS()			\
+	x(background_compression)		\
+	x(background_target)
+
 union bch_extent_entry {
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ||  __BITS_PER_LONG == 64
 	unsigned long			type;
fs/bcachefs/io_misc.c +1 -1
index f283051758d6..e2acf21ac9b0 100644
@@ -461,7 +461,7 @@ case LOGGED_OP_FINSERT_shift_extents:
 
 		op->v.pos = cpu_to_le64(insert ? bkey_start_offset(&delete.k) : delete.k.p.offset);
 
-		ret =   bch2_bkey_set_needs_rebalance(c, copy, &opts) ?:
+		ret =   bch2_bkey_set_needs_rebalance(c, &opts, copy) ?:
 			bch2_btree_insert_trans(trans, BTREE_ID_extents, &delete, 0) ?:
 			bch2_btree_insert_trans(trans, BTREE_ID_extents, copy, 0) ?:
 			bch2_logged_op_update(trans, &op->k_i) ?:
fs/bcachefs/io_write.c +1 -1
index 8609e25e450f..a6b7d97540db 100644
@@ -369,7 +369,7 @@ static int bch2_write_index_default(struct bch_write_op *op)
 				     bkey_start_pos(&sk.k->k),
 				     BTREE_ITER_slots|BTREE_ITER_intent);
 
-		ret =   bch2_bkey_set_needs_rebalance(c, sk.k, &op->opts) ?:
+		ret =   bch2_bkey_set_needs_rebalance(c, &op->opts, sk.k) ?:
 			bch2_extent_update(trans, inum, &iter, sk.k,
 					&op->res,
 					op->new_i_size, &op->i_sectors_delta,
fs/bcachefs/opts.h +13 -0
index dd27ef556611..13555bc35f00 100644
@@ -642,4 +642,17 @@ static inline void bch2_io_opts_fixups(struct bch_io_opts *opts)
 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts);
 bool bch2_opt_is_inode_opt(enum bch_opt_id);
 
+/* rebalance opts: */
+
+static inline struct bch_extent_rebalance io_opts_to_rebalance_opts(struct bch_io_opts *opts)
+{
+	return (struct bch_extent_rebalance) {
+		.type = BIT(BCH_EXTENT_ENTRY_rebalance),
+#define x(_name)							\
+		._name = opts->_name,
+		BCH_REBALANCE_OPTS()
+#undef x
+	};
+};
+
 #endif /* _BCACHEFS_OPTS_H */
fs/bcachefs/reflink.c +1 -1
index f457925fa362..8a36ebd9dd9c 100644
@@ -547,7 +547,7 @@ s64 bch2_remap_range(struct bch_fs *c,
 				min(src_k.k->p.offset - src_want.offset,
 				    dst_end.offset - dst_iter.pos.offset));
 
-		ret =   bch2_bkey_set_needs_rebalance(c, new_dst.k, &opts) ?:
+		ret =   bch2_bkey_set_needs_rebalance(c, &opts, new_dst.k) ?:
 			bch2_extent_update(trans, dst_inum, &dst_iter,
 					new_dst.k, &disk_res,
 					new_i_size, i_sectors_delta,
Signature · 1 line
2.45.2
#9 lore raw

PATCH 9/12 bcachefs: Add bch_io_opts fields for indicating whether the opts came from the inode

In reply to Kent Overstreet

This is going to be used in the bch_extent_rebalance improvements, which propagate io_path options into the extent (important for rebalance, which needs something present in the extent for transactionally tagging them in the rebalance_work btree, and also for indirect extents).

By tracking in bch_extent_rebalance whether the option came from the filesystem or the inode we can correctly handle options being changed on indirect extents.

---
 fs/bcachefs/inode.c | 8 +++++++-
 fs/bcachefs/opts.h  | 3 +++
 2 files changed, 10 insertions(+), 1 deletion(-)
fs/bcachefs/inode.c +7 -1
index 3af6029ea9b7..62fcc3a0bf69 100644
@@ -1142,7 +1142,13 @@ struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *inode)
 void bch2_inode_opts_get(struct bch_io_opts *opts, struct bch_fs *c,
 			 struct bch_inode_unpacked *inode)
 {
-#define x(_name, _bits)		opts->_name = inode_opt_get(c, inode, _name);
+#define x(_name, _bits)							\
+	if ((inode)->bi_##_name) {					\
+		opts->_name = inode->bi_##_name - 1;			\
+		opts->_name##_from_inode = true;			\
+	} else {							\
+		opts->_name = c->opts._name;				\
+	}
 	BCH_INODE_OPTS()
 #undef x
 
fs/bcachefs/opts.h +3 -0
index 13555bc35f00..918eb6730117 100644
@@ -624,6 +624,9 @@ struct bch_io_opts {
 #define x(_name, _bits)	u##_bits _name;
 	BCH_INODE_OPTS()
 #undef x
+#define x(_name, _bits)	u64 _name##_from_inode:1;
+	BCH_INODE_OPTS()
+#undef x
 };
 
 static inline void bch2_io_opts_fixups(struct bch_io_opts *opts)
Signature · 1 line
2.45.2
#10 lore raw

PATCH 10/12 bcachefs: New bch_extent_rebalance fields

In reply to Kent Overstreet

- Add more io path options to bch_extent_rebalance
- For each option, track whether it came from the filesystem or the inode

This lets us do some new things:

bch2_move_get_io_opts() now synchronizes options loaded from the filesystem and inode (if present, i.e. not walking the reflink btree directly) with options from the bch_extent_rebalance_entry, updating the extent if necessary.

Since bch_extent_rebalance tracks where its option came from we can preserve "inode options override filesystem options", even for indirect extents where we don't have access to the inode the options came from.

Since bch2_move_get_io_opts() now synchronizes io_opts with options from bch_extent_rebalance, that also lets us delete the ad-hoc logic in rebalance.c that previously did this.

---
 fs/bcachefs/extents.c        | 33 ++++++++++----
 fs/bcachefs/extents.h        |  4 +-
 fs/bcachefs/extents_format.h | 34 ++++++++++++--
 fs/bcachefs/move.c           | 88 ++++++++++++++++++++++++++----------
 fs/bcachefs/move.h           |  5 +-
 fs/bcachefs/opts.h           |  3 +-
 fs/bcachefs/rebalance.c      | 44 +++++-------------
 7 files changed, 138 insertions(+), 73 deletions(-)
fs/bcachefs/extents.c +25 -8
index b12c251669f9..be39318ca298 100644
@@ -1401,14 +1401,15 @@ const struct bch_extent_rebalance *bch2_bkey_rebalance_opts(struct bkey_s_c k)
 	return NULL;
 }
 
-unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *c, struct bkey_s_c k,
-				       unsigned target, unsigned compression)
+unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *c,
+				       struct bch_io_opts *opts,
+				       struct bkey_s_c k)
 {
 	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);
+	if (opts->background_compression) {
+		unsigned compression_type = bch2_compression_opt_to_type(opts->background_compression);
 		const union bch_extent_entry *entry;
 		struct extent_ptr_decoded p;
 		unsigned ptr_bit = 1;
@@ -1426,11 +1427,12 @@ unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *c, struct bkey_s_c k,
 		}
 	}
 incompressible:
-	if (target && bch2_target_accepts_data(c, BCH_DATA_user, target)) {
+	if (opts->background_target &&
+	    bch2_target_accepts_data(c, BCH_DATA_user, opts->background_target)) {
 		unsigned ptr_bit = 1;
 
 		bkey_for_each_ptr(ptrs, ptr) {
-			if (!ptr->cached && !bch2_dev_in_target(c, ptr->dev, target))
+			if (!ptr->cached && !bch2_dev_in_target(c, ptr->dev, opts->background_target))
 				rewrite_ptrs |= ptr_bit;
 			ptr_bit <<= 1;
 		}
@@ -1475,6 +1477,22 @@ u64 bch2_bkey_sectors_need_rebalance(struct bch_fs *c, struct bkey_s_c k)
 	return sectors;
 }
 
+bool bch2_bkey_rebalance_needs_update(struct bch_fs *c, struct bch_io_opts *opts,
+				      struct bkey_s_c k)
+{
+	if (!bkey_extent_is_direct_data(k.k))
+		return 0;
+
+	const struct bch_extent_rebalance *old = bch2_bkey_rebalance_opts(k);
+
+	if (k.k->type == KEY_TYPE_reflink_v || bch2_bkey_ptrs_need_rebalance(c, opts, k)) {
+		struct bch_extent_rebalance new = io_opts_to_rebalance_opts(opts);
+		return old == NULL || memcmp(old, &new, sizeof(new));
+	} else {
+		return old != NULL;
+	}
+}
+
 int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bch_io_opts *opts,
 				  struct bkey_i *_k)
 {
@@ -1485,8 +1503,7 @@ int bch2_bkey_set_needs_rebalance(struct bch_fs *c, struct bch_io_opts *opts,
 	struct bch_extent_rebalance *old =
 		(struct bch_extent_rebalance *) bch2_bkey_rebalance_opts(k.s_c);
 
-	if (k.k->type == KEY_TYPE_reflink_v ||
-	    bch2_bkey_ptrs_need_rebalance(c, k.s_c, opts->background_target, opts->background_compression)) {
+	if (k.k->type == KEY_TYPE_reflink_v || bch2_bkey_ptrs_need_rebalance(c, opts, k.s_c)) {
 		if (!old) {
 			old = bkey_val_end(k);
 			k.k->u64s += sizeof(*old) / sizeof(u64);
fs/bcachefs/extents.h +2 -2
index 97af0d6e4319..156fbb8e04d5 100644
@@ -711,10 +711,10 @@ static inline bool bch2_extent_ptr_eq(struct bch_extent_ptr ptr1,
 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);
+unsigned bch2_bkey_ptrs_need_rebalance(struct bch_fs *, struct bch_io_opts *, struct bkey_s_c);
 u64 bch2_bkey_sectors_need_rebalance(struct bch_fs *, struct bkey_s_c);
 
+bool bch2_bkey_rebalance_needs_update(struct bch_fs *, struct bch_io_opts *, struct bkey_s_c);
 int bch2_bkey_set_needs_rebalance(struct bch_fs *, struct bch_io_opts *, struct bkey_i *);
 
 /* Generic extent code: */
fs/bcachefs/extents_format.h +31 -3
index 520697f236c0..222eed6b46d8 100644
@@ -204,21 +204,49 @@ struct bch_extent_stripe_ptr {
 struct bch_extent_rebalance {
 #if defined(__LITTLE_ENDIAN_BITFIELD)
 	__u64			type:6,
-				unused:34,
+				unused:3,
+
+				promote_target_from_inode:1,
+				erasure_code_from_inode:1,
+				data_checksum_from_inode:1,
+				background_compression_from_inode:1,
+				data_replicas_from_inode:1,
+				background_target_from_inode:1,
+
+				promote_target:16,
+				erasure_code:1,
+				data_checksum:4,
+				data_replicas:4,
 				background_compression:8, /* enum bch_compression_opt */
 				background_target:16;
 #elif defined (__BIG_ENDIAN_BITFIELD)
 	__u64			background_target:16,
 				background_compression:8,
-				unused:34,
+				data_replicas:4,
+				data_checksum:4,
+				erasure_code:1,
+				promote_target:16,
+
+				background_target_from_inode:1,
+				data_replicas_from_inode:1,
+				background_compression_from_inode:1,
+				data_checksum_from_inode:1,
+				erasure_code_from_inode:1,
+				promote_target_from_inode:1,
+
+				unused:3,
 				type:6;
 #endif
 };
 
 /* subset of BCH_INODE_OPTS */
 #define BCH_REBALANCE_OPTS()			\
+	x(data_checksum)			\
 	x(background_compression)		\
-	x(background_target)
+	x(data_replicas)			\
+	x(promote_target)			\
+	x(background_target)			\
+	x(erasure_code)
 
 union bch_extent_entry {
 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ||  __BITS_PER_LONG == 64
fs/bcachefs/move.c +64 -24

135 lines not on this page · raw message

fs/bcachefs/move.h +2 -3
index 9baf3093a678..51e0505a8156 100644
@@ -110,9 +110,8 @@ static inline void per_snapshot_io_opts_exit(struct per_snapshot_io_opts *io_opt
 	darray_exit(&io_opts->d);
 }
 
-struct bch_io_opts *bch2_move_get_io_opts(struct btree_trans *,
-				struct per_snapshot_io_opts *, struct bkey_s_c);
-int bch2_move_get_io_opts_one(struct btree_trans *, struct bch_io_opts *, struct bkey_s_c);
+int bch2_move_get_io_opts_one(struct btree_trans *, struct bch_io_opts *,
+			      struct btree_iter *, struct bkey_s_c);
 
 int bch2_scan_old_btree_nodes(struct bch_fs *, struct bch_move_stats *);
 
fs/bcachefs/opts.h +2 -1
index 918eb6730117..725de97148bc 100644
@@ -652,7 +652,8 @@ static inline struct bch_extent_rebalance io_opts_to_rebalance_opts(struct bch_i
 	return (struct bch_extent_rebalance) {
 		.type = BIT(BCH_EXTENT_ENTRY_rebalance),
 #define x(_name)							\
-		._name = opts->_name,
+		._name = opts->_name,					\
+		._name##_from_inode = opts->_name##_from_inode,
 		BCH_REBALANCE_OPTS()
 #undef x
 	};
fs/bcachefs/rebalance.c +12 -32

86 lines not on this page · raw message

Signature · 1 line
2.45.2
#11 lore raw

PATCH 11/12 bcachefs: bch2_indirect_extent_error()

In reply to Kent Overstreet

Factor out a small helper for missing indirect extents, to be used by the new move.c support for reflinked data.

---
 fs/bcachefs/io_read.c | 25 +++++++------------------
 fs/bcachefs/reflink.c | 13 +++++++++++++
 fs/bcachefs/reflink.h |  2 ++
 3 files changed, 22 insertions(+), 18 deletions(-)
fs/bcachefs/io_read.c +7 -18
index 13dc35cd6562..d070482ba533 100644
@@ -21,6 +21,7 @@
 #include "io_read.h"
 #include "io_misc.h"
 #include "io_write.h"
+#include "reflink.h"
 #include "subvolume.h"
 #include "trace.h"
 
@@ -753,31 +754,19 @@ int __bch2_read_indirect_extent(struct btree_trans *trans,
 				unsigned *offset_into_extent,
 				struct bkey_buf *orig_k)
 {
-	struct btree_iter iter;
-	struct bkey_s_c k;
-	u64 reflink_offset;
-	int ret;
+	struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(bkey_i_to_s_c(orig_k->k));
+	u64 reflink_offset = le64_to_cpu(p.v->idx) + *offset_into_extent;
 
-	reflink_offset = le64_to_cpu(bkey_i_to_reflink_p(orig_k->k)->v.idx) +
-		*offset_into_extent;
-
-	k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_reflink,
+	struct btree_iter iter;
+	struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_reflink,
 			       POS(0, reflink_offset), 0);
-	ret = bkey_err(k);
+	int ret = bkey_err(k);
 	if (ret)
 		goto err;
 
 	if (k.k->type != KEY_TYPE_reflink_v &&
 	    k.k->type != KEY_TYPE_indirect_inline_data) {
-		bch_err_inum_offset_ratelimited(trans->c,
-			orig_k->k->k.p.inode,
-			orig_k->k->k.p.offset << 9,
-			"%llu len %u points to nonexistent indirect extent %llu",
-			orig_k->k->k.p.offset,
-			orig_k->k->k.size,
-			reflink_offset);
-		bch2_inconsistent_error(trans->c);
-		ret = -BCH_ERR_missing_indirect_extent;
+		ret = bch2_indirect_extent_error(trans, p);
 		goto err;
 	}
 
fs/bcachefs/reflink.c +13 -0
index 8a36ebd9dd9c..61cc67f8636a 100644
@@ -27,6 +27,19 @@ static inline unsigned bkey_type_to_indirect(const struct bkey *k)
 	}
 }
 
+int bch2_indirect_extent_error(struct btree_trans *trans, struct bkey_s_c_reflink_p k)
+{
+	bch_err_inum_offset_ratelimited(trans->c,
+		k.k->p.inode,
+		k.k->p.offset << 9,
+		"%llu len %u points to nonexistent indirect extent %llu",
+		k.k->p.offset,
+		k.k->size,
+		le64_to_cpu(k.v->idx));
+	bch2_inconsistent_error(trans->c);
+	return -BCH_ERR_missing_indirect_extent;
+}
+
 /* reflink pointers */
 
 int bch2_reflink_p_validate(struct bch_fs *c, struct bkey_s_c k,
fs/bcachefs/reflink.h +2 -0
index 51afe11d8ed6..a4da4bfc2785 100644
@@ -4,6 +4,8 @@
 
 enum bch_validate_flags;
 
+int bch2_indirect_extent_error(struct btree_trans *, struct bkey_s_c_reflink_p);
+
 int bch2_reflink_p_validate(struct bch_fs *, struct bkey_s_c, enum bch_validate_flags);
 void bch2_reflink_p_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
 bool bch2_reflink_p_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c);
Signature · 1 line
2.45.2
#12 lore raw

PATCH 12/12 bcachefs: bch2_write_inode() now checks for changing rebalance options

In reply to Kent Overstreet

Previously, BCHFS_IOC_REINHERIT_ATTRS didn't trigger rebalance scans when changing rebalance options - it had been missed, only the xattr interface triggered them.

Ideally they'd be done by the transactional trigger, but unpacking the inode to get the options is too heavy to be done in the low level trigger - the inode trigger is run on every extent update, since the bch_inode.bi_journal_seq has to be updated for fsync.

bch2_write_inode() is a good compromise, it already unpacks and repacks and is not run in any super-fast paths.

Additionally, creating the new rebalance entry to trigger the scan is now done in the same transaction as the inode update that changed the options.

---
 fs/bcachefs/fs.c        | 26 +++++++++++++++++++++-----
 fs/bcachefs/inode.h     |  8 ++++++++
 fs/bcachefs/rebalance.c |  4 ++--
 fs/bcachefs/rebalance.h |  1 +
 fs/bcachefs/xattr.c     |  7 -------
 5 files changed, 32 insertions(+), 14 deletions(-)
fs/bcachefs/fs.c +21 -5
index 79bc54e779ab..9ca001a1d0cb 100644
@@ -23,6 +23,7 @@
 #include "journal.h"
 #include "keylist.h"
 #include "quota.h"
+#include "rebalance.h"
 #include "snapshot.h"
 #include "super.h"
 #include "xattr.h"
@@ -89,10 +90,25 @@ int __must_check bch2_write_inode(struct bch_fs *c,
 retry:
 	bch2_trans_begin(trans);
 
-	ret   = bch2_inode_peek(trans, &iter, &inode_u, inode_inum(inode),
-				BTREE_ITER_intent) ?:
-		(set ? set(trans, inode, &inode_u, p) : 0) ?:
-		bch2_inode_write(trans, &iter, &inode_u) ?:
+	ret = bch2_inode_peek(trans, &iter, &inode_u, inode_inum(inode), BTREE_ITER_intent);
+	if (ret)
+		goto err;
+
+	struct bch_extent_rebalance old_r = bch2_inode_rebalance_opts_get(c, &inode_u);
+
+	ret = (set ? set(trans, inode, &inode_u, p) : 0);
+	if (ret)
+		goto err;
+
+	struct bch_extent_rebalance new_r = bch2_inode_rebalance_opts_get(c, &inode_u);
+
+	if (memcmp(&old_r, &new_r, sizeof(new_r))) {
+		ret = bch2_set_rebalance_needs_scan_trans(trans, inode_u.bi_inum);
+		if (ret)
+			goto err;
+	}
+
+	ret   = bch2_inode_write(trans, &iter, &inode_u) ?:
 		bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
 
 	/*
@@ -101,7 +117,7 @@ int __must_check bch2_write_inode(struct bch_fs *c,
 	 */
 	if (!ret)
 		bch2_inode_update_after_write(trans, inode, &inode_u, fields);
-
+err:
 	bch2_trans_iter_exit(trans, &iter);
 
 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
fs/bcachefs/inode.h +8 -0
index bdeb6be76038..f52336cb298f 100644
@@ -262,6 +262,14 @@ void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
 			 struct bch_inode_unpacked *);
 int bch2_inum_opts_get(struct btree_trans*, subvol_inum, struct bch_io_opts *);
 
+static inline struct bch_extent_rebalance
+bch2_inode_rebalance_opts_get(struct bch_fs *c, struct bch_inode_unpacked *inode)
+{
+	struct bch_io_opts io_opts;
+	bch2_inode_opts_get(&io_opts, c, inode);
+	return io_opts_to_rebalance_opts(&io_opts);
+}
+
 int bch2_inode_rm_snapshot(struct btree_trans *, u64, u32);
 int bch2_delete_dead_inodes(struct bch_fs *);
 
fs/bcachefs/rebalance.c +2 -2
index d094dcd72fb4..7ebf71ad0358 100644
@@ -33,7 +33,7 @@ static const char * const bch2_rebalance_state_strs[] = {
 #undef x
 };
 
-static int __bch2_set_rebalance_needs_scan(struct btree_trans *trans, u64 inum)
+int bch2_set_rebalance_needs_scan_trans(struct btree_trans *trans, u64 inum)
 {
 	struct btree_iter iter;
 	struct bkey_s_c k;
@@ -73,7 +73,7 @@ int bch2_set_rebalance_needs_scan(struct bch_fs *c, u64 inum)
 	int ret = bch2_trans_commit_do(c, NULL, NULL,
 				       BCH_TRANS_COMMIT_no_enospc|
 				       BCH_TRANS_COMMIT_lazy_rw,
-			    __bch2_set_rebalance_needs_scan(trans, inum));
+			    bch2_set_rebalance_needs_scan_trans(trans, inum));
 	rebalance_wakeup(c);
 	return ret;
 }
fs/bcachefs/rebalance.h +1 -0
index 28a52638f16c..791649c04ff5 100644
@@ -4,6 +4,7 @@
 
 #include "rebalance_types.h"
 
+int bch2_set_rebalance_needs_scan_trans(struct btree_trans *, u64);
 int bch2_set_rebalance_needs_scan(struct bch_fs *, u64 inum);
 int bch2_set_fs_needs_rebalance(struct bch_fs *);
 
fs/bcachefs/xattr.c +0 -7
index bf3c6bb50495..ed418a747cdd 100644
@@ -565,13 +565,6 @@ static int bch2_xattr_bcachefs_set(const struct xattr_handler *handler,
 	ret = bch2_write_inode(c, inode, inode_opt_set_fn, &s, 0);
 err:
 	mutex_unlock(&inode->ei_update_lock);
-
-	if (value &&
-	    (opt_id == Opt_background_target ||
-	     opt_id == Opt_background_compression ||
-	     (opt_id == Opt_compression && !inode_opt_get(c, &inode->ei_inode, background_compression))))
-		bch2_set_rebalance_needs_scan(c, inode->ei_inode.bi_inum);
-
 err_class_exit:
 	return bch2_err_class(ret);
 }
Signature · 1 line
2.45.2