2019-07-10 23:12:15 +03:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2017-10-06 01:41:44 +03:00
|
|
|
#ifndef _BCACHEFS_BKEY_METHODS_H
|
|
|
|
#define _BCACHEFS_BKEY_METHODS_H
|
2017-01-08 12:13:18 +03:00
|
|
|
|
|
|
|
#include "bkey.h"
|
|
|
|
|
2017-03-11 00:40:01 +03:00
|
|
|
struct bch_fs;
|
2017-01-08 12:13:18 +03:00
|
|
|
struct btree;
|
2022-03-14 02:14:01 +03:00
|
|
|
struct btree_trans;
|
2017-01-08 12:13:18 +03:00
|
|
|
struct bkey;
|
2018-11-30 20:38:54 +03:00
|
|
|
enum btree_node_type;
|
|
|
|
|
2019-08-21 20:17:42 +03:00
|
|
|
extern const char * const bch2_bkey_types[];
|
2017-01-08 12:13:18 +03:00
|
|
|
|
|
|
|
struct bkey_ops {
|
|
|
|
/* Returns reason for being invalid if invalid, else NULL: */
|
2017-03-11 00:40:01 +03:00
|
|
|
const char * (*key_invalid)(const struct bch_fs *,
|
2017-01-08 12:13:18 +03:00
|
|
|
struct bkey_s_c);
|
2018-11-09 12:00:42 +03:00
|
|
|
void (*val_to_text)(struct printbuf *, struct bch_fs *,
|
|
|
|
struct bkey_s_c);
|
2020-02-23 00:21:32 +03:00
|
|
|
void (*swab)(struct bkey_s);
|
2018-11-30 20:38:54 +03:00
|
|
|
bool (*key_normalize)(struct bch_fs *, struct bkey_s);
|
2021-06-11 03:21:18 +03:00
|
|
|
bool (*key_merge)(struct bch_fs *, struct bkey_s, struct bkey_s_c);
|
2022-03-14 02:14:01 +03:00
|
|
|
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);
|
2020-03-30 21:34:20 +03:00
|
|
|
void (*compat)(enum btree_id id, unsigned version,
|
|
|
|
unsigned big_endian, int write,
|
|
|
|
struct bkey_s);
|
2017-01-08 12:13:18 +03:00
|
|
|
};
|
|
|
|
|
2021-06-11 03:21:18 +03:00
|
|
|
extern const struct bkey_ops bch2_bkey_ops[];
|
|
|
|
|
2018-11-30 20:38:54 +03:00
|
|
|
const char *bch2_bkey_val_invalid(struct bch_fs *, struct bkey_s_c);
|
|
|
|
const char *__bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c,
|
|
|
|
enum btree_node_type);
|
|
|
|
const char *bch2_bkey_invalid(struct bch_fs *, struct bkey_s_c,
|
|
|
|
enum btree_node_type);
|
2017-12-22 02:00:30 +03:00
|
|
|
const char *bch2_bkey_in_btree_node(struct btree *, struct bkey_s_c);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2018-11-09 12:00:42 +03:00
|
|
|
void bch2_bpos_to_text(struct printbuf *, struct bpos);
|
|
|
|
void bch2_bkey_to_text(struct printbuf *, const struct bkey *);
|
2018-11-30 20:38:54 +03:00
|
|
|
void bch2_val_to_text(struct printbuf *, struct bch_fs *,
|
2018-11-09 12:00:42 +03:00
|
|
|
struct bkey_s_c);
|
|
|
|
void bch2_bkey_val_to_text(struct printbuf *, struct bch_fs *,
|
2018-11-30 20:38:54 +03:00
|
|
|
struct bkey_s_c);
|
|
|
|
|
2020-02-23 00:21:32 +03:00
|
|
|
void bch2_bkey_swab_val(struct bkey_s);
|
2018-11-30 20:38:54 +03:00
|
|
|
|
|
|
|
bool bch2_bkey_normalize(struct bch_fs *, struct bkey_s);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2021-06-11 03:21:18 +03:00
|
|
|
static inline bool bch2_bkey_maybe_mergable(const struct bkey *l, const struct bkey *r)
|
|
|
|
{
|
|
|
|
return l->type == r->type &&
|
|
|
|
!bversion_cmp(l->version, r->version) &&
|
|
|
|
!bpos_cmp(l->p, bkey_start_pos(r)) &&
|
|
|
|
(u64) l->size + r->size <= KEY_SIZE_MAX &&
|
|
|
|
bch2_bkey_ops[l->type].key_merge &&
|
|
|
|
!bch2_key_merging_disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bch2_bkey_merge(struct bch_fs *, struct bkey_s, struct bkey_s_c);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2022-03-14 02:14:01 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-30 20:38:54 +03:00
|
|
|
void bch2_bkey_renumber(enum btree_node_type, struct bkey_packed *, int);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2020-03-30 21:34:20 +03:00
|
|
|
void __bch2_bkey_compat(unsigned, enum btree_id, unsigned, unsigned,
|
|
|
|
int, struct bkey_format *, struct bkey_packed *);
|
|
|
|
|
|
|
|
static inline void bch2_bkey_compat(unsigned level, enum btree_id btree_id,
|
|
|
|
unsigned version, unsigned big_endian,
|
|
|
|
int write,
|
|
|
|
struct bkey_format *f,
|
|
|
|
struct bkey_packed *k)
|
|
|
|
{
|
|
|
|
if (version < bcachefs_metadata_version_current ||
|
|
|
|
big_endian != CPU_BIG_ENDIAN)
|
|
|
|
__bch2_bkey_compat(level, btree_id, version,
|
|
|
|
big_endian, write, f, k);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-06 01:41:44 +03:00
|
|
|
#endif /* _BCACHEFS_BKEY_METHODS_H */
|