summaryrefslogtreecommitdiff
path: root/fs/bcachefs/bkey_methods.h
blob: 62b86a8e2ba85950f25f1b3773191cd8ce7cd7b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _BCACHEFS_BKEY_METHODS_H
#define _BCACHEFS_BKEY_METHODS_H

#include "bkey.h"

#define DEF_BTREE_ID(kwd, val, name) BKEY_TYPE_##kwd = val,

enum bkey_type {
	DEFINE_BCH_BTREE_IDS()
	BKEY_TYPE_BTREE,
};

#undef DEF_BTREE_ID

/* Type of a key in btree @id at level @level: */
static inline enum bkey_type bkey_type(unsigned level, enum btree_id id)
{
	return level ? BKEY_TYPE_BTREE : (enum bkey_type) id;
}

struct bch_fs;
struct btree;
struct bkey;

enum merge_result {
	BCH_MERGE_NOMERGE,

	/*
	 * The keys were mergeable, but would have overflowed size - so instead
	 * l was changed to the maximum size, and both keys were modified:
	 */
	BCH_MERGE_PARTIAL,
	BCH_MERGE_MERGE,
};

typedef bool (*key_filter_fn)(struct bch_fs *, struct btree *,
			      struct bkey_s);
typedef enum merge_result (*key_merge_fn)(struct bch_fs *,
					  struct btree *,
					  struct bkey_i *, struct bkey_i *);

struct bkey_ops {
	/* Returns reason for being invalid if invalid, else NULL: */
	const char *	(*key_invalid)(const struct bch_fs *,
				       struct bkey_s_c);
	void		(*key_debugcheck)(struct bch_fs *, struct btree *,
					  struct bkey_s_c);
	void		(*val_to_text)(struct printbuf *, struct bch_fs *,
				       struct bkey_s_c);
	void		(*swab)(const struct bkey_format *, struct bkey_packed *);
	key_filter_fn	key_normalize;
	key_merge_fn	key_merge;
	bool		is_extents;
};

static inline bool bkey_type_needs_gc(enum bkey_type type)
{
	switch (type) {
	case BKEY_TYPE_BTREE:
	case BKEY_TYPE_EXTENTS:
	case BKEY_TYPE_EC:
		return true;
	default:
		return false;
	}
}

const char *bch2_bkey_val_invalid(struct bch_fs *, enum bkey_type,
				  struct bkey_s_c);
const char *__bch2_bkey_invalid(struct bch_fs *, enum bkey_type, struct bkey_s_c);
const char *bch2_bkey_invalid(struct bch_fs *, enum bkey_type, struct bkey_s_c);
const char *bch2_bkey_in_btree_node(struct btree *, struct bkey_s_c);

void bch2_bkey_debugcheck(struct bch_fs *, struct btree *, struct bkey_s_c);

void bch2_bpos_to_text(struct printbuf *, struct bpos);
void bch2_bkey_to_text(struct printbuf *, const struct bkey *);
void bch2_val_to_text(struct printbuf *, struct bch_fs *, enum bkey_type,
		      struct bkey_s_c);
void bch2_bkey_val_to_text(struct printbuf *, struct bch_fs *,
			   enum bkey_type, struct bkey_s_c);

void bch2_bkey_swab(enum bkey_type, const struct bkey_format *,
		    struct bkey_packed *);

extern const struct bkey_ops bch2_bkey_ops[];

#endif /* _BCACHEFS_BKEY_METHODS_H */