Commit 880e2275 authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet
Browse files

bcachefs: Move trigger fns to bkey_ops



This replaces the switch statements in bch2_mark_key(),
bch2_trans_mark_key() with new bkey methods - prep work for the next
patch, which fixes BTREE_TRIGGER_WANTS_OLD_AND_NEW.

Signed-off-by: default avatarKent Overstreet <kent.overstreet@gmail.com>
parent d13f9ee6
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -65,16 +65,19 @@ void bch2_alloc_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
#define bch2_bkey_ops_alloc (struct bkey_ops) {		\
	.key_invalid	= bch2_alloc_v1_invalid,	\
	.val_to_text	= bch2_alloc_to_text,		\
	.atomic_trigger	= bch2_mark_alloc,		\
}

#define bch2_bkey_ops_alloc_v2 (struct bkey_ops) {	\
	.key_invalid	= bch2_alloc_v2_invalid,	\
	.val_to_text	= bch2_alloc_to_text,		\
	.atomic_trigger	= bch2_mark_alloc,		\
}

#define bch2_bkey_ops_alloc_v3 (struct bkey_ops) {	\
	.key_invalid	= bch2_alloc_v3_invalid,	\
	.val_to_text	= bch2_alloc_to_text,		\
	.atomic_trigger	= bch2_mark_alloc,		\
}

static inline bool bkey_is_alloc(const struct bkey *k)
+27 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@

struct bch_fs;
struct btree;
struct btree_trans;
struct bkey;
enum btree_node_type;

@@ -20,6 +21,10 @@ struct bkey_ops {
	void		(*swab)(struct bkey_s);
	bool		(*key_normalize)(struct bch_fs *, struct bkey_s);
	bool		(*key_merge)(struct bch_fs *, struct bkey_s, struct bkey_s_c);
	int		(*trans_trigger)(struct btree_trans *, struct bkey_s_c,
					 struct bkey_i *, unsigned);
	int		(*atomic_trigger)(struct btree_trans *, struct bkey_s_c,
					  struct bkey_s_c, unsigned);
	void		(*compat)(enum btree_id id, unsigned version,
				  unsigned big_endian, int write,
				  struct bkey_s);
@@ -54,6 +59,28 @@ static inline bool bch2_bkey_maybe_mergable(const struct bkey *l, const struct b

bool bch2_bkey_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c);

static inline int bch2_mark_key(struct btree_trans *trans,
		  struct bkey_s_c old,
		  struct bkey_s_c new,
		  unsigned flags)
{
	const struct bkey_ops *ops = &bch2_bkey_ops[old.k->type ?: new.k->type];

	return ops->atomic_trigger
		? ops->atomic_trigger(trans, old, new, flags)
		: 0;
}

static inline int bch2_trans_mark_key(struct btree_trans *trans, struct bkey_s_c old,
			struct bkey_i *new, unsigned flags)
{
	const struct bkey_ops *ops = &bch2_bkey_ops[old.k->type ?: new->k.type];

	return ops->trans_trigger
		? ops->trans_trigger(trans, old, new, flags)
		: 0;
}

void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int);

void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned,
+45 −91
Original line number Diff line number Diff line
@@ -501,7 +501,7 @@ void bch2_mark_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
	BUG_ON(owned_by_allocator == old.owned_by_allocator);
}

static int bch2_mark_alloc(struct btree_trans *trans,
int bch2_mark_alloc(struct btree_trans *trans,
		    struct bkey_s_c old, struct bkey_s_c new,
		    unsigned flags)
{
@@ -933,7 +933,7 @@ static int bch2_mark_stripe_ptr(struct btree_trans *trans,
	return 0;
}

static int bch2_mark_extent(struct btree_trans *trans,
int bch2_mark_extent(struct btree_trans *trans,
		     struct bkey_s_c old, struct bkey_s_c new,
		     unsigned flags)
{
@@ -1015,7 +1015,7 @@ static int bch2_mark_extent(struct btree_trans *trans,
	return 0;
}

static int bch2_mark_stripe(struct btree_trans *trans,
int bch2_mark_stripe(struct btree_trans *trans,
		     struct bkey_s_c old, struct bkey_s_c new,
		     unsigned flags)
{
@@ -1122,7 +1122,7 @@ static int bch2_mark_stripe(struct btree_trans *trans,
	return 0;
}

static int bch2_mark_inode(struct btree_trans *trans,
int bch2_mark_inode(struct btree_trans *trans,
		    struct bkey_s_c old, struct bkey_s_c new,
		    unsigned flags)
{
@@ -1153,7 +1153,7 @@ static int bch2_mark_inode(struct btree_trans *trans,
	return 0;
}

static int bch2_mark_reservation(struct btree_trans *trans,
int bch2_mark_reservation(struct btree_trans *trans,
			  struct bkey_s_c old, struct bkey_s_c new,
			  unsigned flags)
{
@@ -1232,7 +1232,7 @@ static s64 __bch2_mark_reflink_p(struct btree_trans *trans,
	return ret;
}

static int bch2_mark_reflink_p(struct btree_trans *trans,
int bch2_mark_reflink_p(struct btree_trans *trans,
			struct bkey_s_c old, struct bkey_s_c new,
			unsigned flags)
{
@@ -1271,39 +1271,6 @@ static int bch2_mark_reflink_p(struct btree_trans *trans,
	return ret;
}

int bch2_mark_key(struct btree_trans *trans,
		  struct bkey_s_c old,
		  struct bkey_s_c new,
		  unsigned flags)
{
	struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE ? old: new;

	switch (k.k->type) {
	case KEY_TYPE_alloc:
	case KEY_TYPE_alloc_v2:
	case KEY_TYPE_alloc_v3:
		return bch2_mark_alloc(trans, old, new, flags);
	case KEY_TYPE_btree_ptr:
	case KEY_TYPE_btree_ptr_v2:
	case KEY_TYPE_extent:
	case KEY_TYPE_reflink_v:
		return bch2_mark_extent(trans, old, new, flags);
	case KEY_TYPE_stripe:
		return bch2_mark_stripe(trans, old, new, flags);
	case KEY_TYPE_inode:
	case KEY_TYPE_inode_v2:
		return bch2_mark_inode(trans, old, new, flags);
	case KEY_TYPE_reservation:
		return bch2_mark_reservation(trans, old, new, flags);
	case KEY_TYPE_reflink_p:
		return bch2_mark_reflink_p(trans, old, new, flags);
	case KEY_TYPE_snapshot:
		return bch2_mark_snapshot(trans, old, new, flags);
	default:
		return 0;
	}
}

static noinline __cold
void fs_usage_apply_warn(struct btree_trans *trans,
			 unsigned disk_res_sectors,
@@ -1518,10 +1485,14 @@ static int bch2_trans_mark_stripe_ptr(struct btree_trans *trans,
	return ret;
}

static int bch2_trans_mark_extent(struct btree_trans *trans,
			struct bkey_s_c k, unsigned flags)
int bch2_trans_mark_extent(struct btree_trans *trans,
			   struct bkey_s_c old, struct bkey_i *new,
			   unsigned flags)
{
	struct bch_fs *c = trans->c;
	struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
		? old
		: bkey_i_to_s_c(new);
	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
	const union bch_extent_entry *entry;
	struct extent_ptr_decoded p;
@@ -1652,7 +1623,7 @@ static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
	return ret;
}

static int bch2_trans_mark_stripe(struct btree_trans *trans,
int bch2_trans_mark_stripe(struct btree_trans *trans,
			   struct bkey_s_c old, struct bkey_i *new,
			   unsigned flags)
{
@@ -1722,7 +1693,7 @@ static int bch2_trans_mark_stripe(struct btree_trans *trans,
	return ret;
}

static int bch2_trans_mark_inode(struct btree_trans *trans,
int bch2_trans_mark_inode(struct btree_trans *trans,
			  struct bkey_s_c old,
			  struct bkey_i *new,
			  unsigned flags)
@@ -1738,9 +1709,14 @@ static int bch2_trans_mark_inode(struct btree_trans *trans,
	return 0;
}

static int bch2_trans_mark_reservation(struct btree_trans *trans,
				       struct bkey_s_c k, unsigned flags)
int bch2_trans_mark_reservation(struct btree_trans *trans,
				struct bkey_s_c old,
				struct bkey_i *new,
				unsigned flags)
{
	struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
		? old
		: bkey_i_to_s_c(new);
	unsigned replicas = bkey_s_c_to_reservation(k).v->nr_replicas;
	s64 sectors = (s64) k.k->size;
	struct replicas_delta_list *d;
@@ -1839,9 +1815,14 @@ static int __bch2_trans_mark_reflink_p(struct btree_trans *trans,
	return ret;
}

static int bch2_trans_mark_reflink_p(struct btree_trans *trans,
				     struct bkey_s_c k, unsigned flags)
int bch2_trans_mark_reflink_p(struct btree_trans *trans,
			      struct bkey_s_c old,
			      struct bkey_i *new,
			      unsigned flags)
{
	struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
		? old
		: bkey_i_to_s_c(new);
	struct bkey_s_c_reflink_p p = bkey_s_c_to_reflink_p(k);
	u64 idx, end_idx;
	int ret = 0;
@@ -1862,33 +1843,6 @@ static int bch2_trans_mark_reflink_p(struct btree_trans *trans,
	return ret;
}

int bch2_trans_mark_key(struct btree_trans *trans, struct bkey_s_c old,
			struct bkey_i *new, unsigned flags)
{
	struct bkey_s_c k = flags & BTREE_TRIGGER_OVERWRITE
		? old
		: bkey_i_to_s_c(new);

	switch (k.k->type) {
	case KEY_TYPE_btree_ptr:
	case KEY_TYPE_btree_ptr_v2:
	case KEY_TYPE_extent:
	case KEY_TYPE_reflink_v:
		return bch2_trans_mark_extent(trans, k, flags);
	case KEY_TYPE_stripe:
		return bch2_trans_mark_stripe(trans, old, new, flags);
	case KEY_TYPE_inode:
	case KEY_TYPE_inode_v2:
		return bch2_trans_mark_inode(trans, old, new, flags);
	case KEY_TYPE_reservation:
		return bch2_trans_mark_reservation(trans, k, flags);
	case KEY_TYPE_reflink_p:
		return bch2_trans_mark_reflink_p(trans, k, flags);
	default:
		return 0;
	}
}

static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
				    struct bch_dev *ca, size_t b,
				    enum bch_data_type type,
+13 −0
Original line number Diff line number Diff line
@@ -229,6 +229,19 @@ void bch2_mark_metadata_bucket(struct bch_fs *, struct bch_dev *,
			       size_t, enum bch_data_type, unsigned,
			       struct gc_pos, unsigned);

int bch2_mark_alloc(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
int bch2_mark_extent(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
int bch2_mark_stripe(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
int bch2_mark_inode(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
int bch2_mark_reservation(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);
int bch2_mark_reflink_p(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);

int bch2_trans_mark_extent(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
int bch2_trans_mark_stripe(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
int bch2_trans_mark_inode(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
int bch2_trans_mark_reservation(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);
int bch2_trans_mark_reflink_p(struct btree_trans *, struct bkey_s_c, struct bkey_i *, unsigned);

int bch2_mark_key(struct btree_trans *, struct bkey_s_c, struct bkey_s_c, unsigned);

int bch2_trans_mark_key(struct btree_trans *, struct bkey_s_c,
+2 −0
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@ void bch2_stripe_to_text(struct printbuf *, struct bch_fs *,
	.key_invalid	= bch2_stripe_invalid,		\
	.val_to_text	= bch2_stripe_to_text,		\
	.swab		= bch2_ptr_swab,		\
	.trans_trigger	= bch2_trans_mark_stripe,	\
	.atomic_trigger	= bch2_mark_stripe,		\
}

static inline unsigned stripe_csums_per_device(const struct bch_stripe *s)
Loading