2019-07-10 23:12:15 +03:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2017-10-06 01:41:44 +03:00
|
|
|
#ifndef _BCACHEFS_BTREE_CACHE_H
|
|
|
|
#define _BCACHEFS_BTREE_CACHE_H
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2017-03-20 02:56:34 +03:00
|
|
|
#include "bcachefs.h"
|
2017-01-08 12:13:18 +03:00
|
|
|
#include "btree_types.h"
|
2022-10-22 20:25:25 +03:00
|
|
|
#include "bkey_methods.h"
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2022-02-27 20:01:32 +03:00
|
|
|
extern const char * const bch2_btree_node_flags[];
|
|
|
|
|
2017-01-08 12:13:18 +03:00
|
|
|
struct btree_iter;
|
|
|
|
|
2017-03-20 02:56:34 +03:00
|
|
|
void bch2_recalc_btree_reserve(struct bch_fs *);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2024-08-23 22:38:12 +03:00
|
|
|
void bch2_btree_node_to_freelist(struct bch_fs *, struct btree *);
|
|
|
|
|
2024-11-30 05:08:00 +03:00
|
|
|
void __bch2_btree_node_hash_remove(struct btree_cache *, struct btree *);
|
2017-11-22 08:42:55 +03:00
|
|
|
void bch2_btree_node_hash_remove(struct btree_cache *, struct btree *);
|
2024-11-30 05:08:00 +03:00
|
|
|
|
2017-11-22 08:42:55 +03:00
|
|
|
int __bch2_btree_node_hash_insert(struct btree_cache *, struct btree *);
|
|
|
|
int bch2_btree_node_hash_insert(struct btree_cache *, struct btree *,
|
2017-03-20 02:56:34 +03:00
|
|
|
unsigned, enum btree_id);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2024-09-08 04:01:09 +03:00
|
|
|
void bch2_node_pin(struct bch_fs *, struct btree *);
|
|
|
|
void bch2_btree_cache_unpin(struct bch_fs *);
|
|
|
|
|
2024-05-03 22:22:22 +03:00
|
|
|
void bch2_btree_node_update_key_early(struct btree_trans *, enum btree_id, unsigned,
|
|
|
|
struct bkey_s_c, struct bkey_i *);
|
|
|
|
|
2023-12-05 00:03:52 +03:00
|
|
|
void bch2_btree_cache_cannibalize_unlock(struct btree_trans *);
|
|
|
|
int bch2_btree_cache_cannibalize_lock(struct btree_trans *, struct closure *);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2021-04-22 01:13:43 +03:00
|
|
|
struct btree *__bch2_btree_node_mem_alloc(struct bch_fs *);
|
2023-03-03 08:39:26 +03:00
|
|
|
struct btree *bch2_btree_node_mem_alloc(struct btree_trans *, bool);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2021-09-10 02:06:29 +03:00
|
|
|
struct btree *bch2_btree_node_get(struct btree_trans *, struct btree_path *,
|
2017-06-14 04:06:05 +03:00
|
|
|
const struct bkey_i *, unsigned,
|
2020-11-07 19:26:00 +03:00
|
|
|
enum six_lock_type, unsigned long);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2022-09-04 21:21:58 +03:00
|
|
|
struct btree *bch2_btree_node_get_noiter(struct btree_trans *, const struct bkey_i *,
|
2021-01-29 00:16:51 +03:00
|
|
|
enum btree_id, unsigned, bool);
|
2020-03-25 22:56:38 +03:00
|
|
|
|
2023-03-03 08:39:26 +03:00
|
|
|
int bch2_btree_node_prefetch(struct btree_trans *, struct btree_path *,
|
2021-07-29 00:26:38 +03:00
|
|
|
const struct bkey_i *, enum btree_id, unsigned);
|
2017-04-24 08:56:57 +03:00
|
|
|
|
2022-09-04 21:21:58 +03:00
|
|
|
void bch2_btree_node_evict(struct btree_trans *, const struct bkey_i *);
|
2021-04-24 23:33:06 +03:00
|
|
|
|
2017-11-22 08:42:55 +03:00
|
|
|
void bch2_fs_btree_cache_exit(struct bch_fs *);
|
|
|
|
int bch2_fs_btree_cache_init(struct bch_fs *);
|
|
|
|
void bch2_fs_btree_cache_init_early(struct btree_cache *);
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2020-02-23 00:21:32 +03:00
|
|
|
static inline u64 btree_ptr_hash_val(const struct bkey_i *k)
|
|
|
|
{
|
|
|
|
switch (k->k.type) {
|
|
|
|
case KEY_TYPE_btree_ptr:
|
|
|
|
return *((u64 *) bkey_i_to_btree_ptr_c(k)->v.start);
|
|
|
|
case KEY_TYPE_btree_ptr_v2:
|
2023-07-07 11:24:54 +03:00
|
|
|
/*
|
|
|
|
* The cast/deref is only necessary to avoid sparse endianness
|
|
|
|
* warnings:
|
|
|
|
*/
|
|
|
|
return *((u64 *) &bkey_i_to_btree_ptr_v2_c(k)->v.seq);
|
2020-02-23 00:21:32 +03:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-05-13 05:45:15 +03:00
|
|
|
|
2020-02-27 01:35:47 +03:00
|
|
|
static inline struct btree *btree_node_mem_ptr(const struct bkey_i *k)
|
|
|
|
{
|
|
|
|
return k->k.type == KEY_TYPE_btree_ptr_v2
|
|
|
|
? (void *)(unsigned long)bkey_i_to_btree_ptr_v2_c(k)->v.mem_ptr
|
|
|
|
: NULL;
|
|
|
|
}
|
|
|
|
|
2017-05-13 05:45:15 +03:00
|
|
|
/* is btree node in hash table? */
|
|
|
|
static inline bool btree_node_hashed(struct btree *b)
|
|
|
|
{
|
2020-02-23 00:21:32 +03:00
|
|
|
return b->hash_val != 0;
|
2017-05-13 05:45:15 +03:00
|
|
|
}
|
|
|
|
|
2017-01-08 12:13:18 +03:00
|
|
|
#define for_each_cached_btree(_b, _c, _tbl, _iter, _pos) \
|
2018-01-11 14:41:59 +03:00
|
|
|
for ((_tbl) = rht_dereference_rcu((_c)->btree_cache.table.tbl, \
|
|
|
|
&(_c)->btree_cache.table), \
|
2017-01-08 12:13:18 +03:00
|
|
|
_iter = 0; _iter < (_tbl)->size; _iter++) \
|
|
|
|
rht_for_each_entry_rcu((_b), (_pos), _tbl, _iter, hash)
|
|
|
|
|
2024-01-17 06:45:53 +03:00
|
|
|
static inline size_t btree_buf_bytes(const struct btree *b)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
2024-01-17 06:45:53 +03:00
|
|
|
return 1UL << b->byte_order;
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
2024-01-17 06:45:53 +03:00
|
|
|
static inline size_t btree_buf_max_u64s(const struct btree *b)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
2024-01-17 06:45:53 +03:00
|
|
|
return (btree_buf_bytes(b) - sizeof(struct btree_node)) / sizeof(u64);
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
2024-01-17 06:45:53 +03:00
|
|
|
static inline size_t btree_max_u64s(const struct bch_fs *c)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
2024-01-17 06:45:53 +03:00
|
|
|
return (c->opts.btree_node_size - sizeof(struct btree_node)) / sizeof(u64);
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
2024-01-17 06:45:53 +03:00
|
|
|
static inline size_t btree_sectors(const struct bch_fs *c)
|
|
|
|
{
|
|
|
|
return c->opts.btree_node_size >> SECTOR_SHIFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned btree_blocks(const struct bch_fs *c)
|
2017-01-08 12:13:18 +03:00
|
|
|
{
|
2021-12-22 07:13:52 +03:00
|
|
|
return btree_sectors(c) >> c->block_bits;
|
2017-01-08 12:13:18 +03:00
|
|
|
}
|
|
|
|
|
2020-05-15 04:46:09 +03:00
|
|
|
#define BTREE_SPLIT_THRESHOLD(c) (btree_max_u64s(c) * 2 / 3)
|
2017-02-02 06:16:42 +03:00
|
|
|
|
|
|
|
#define BTREE_FOREGROUND_MERGE_THRESHOLD(c) (btree_max_u64s(c) * 1 / 3)
|
|
|
|
#define BTREE_FOREGROUND_MERGE_HYSTERESIS(c) \
|
|
|
|
(BTREE_FOREGROUND_MERGE_THRESHOLD(c) + \
|
2021-04-05 05:12:56 +03:00
|
|
|
(BTREE_FOREGROUND_MERGE_THRESHOLD(c) >> 2))
|
2017-02-02 06:16:42 +03:00
|
|
|
|
2023-07-07 11:24:54 +03:00
|
|
|
static inline unsigned btree_id_nr_alive(struct bch_fs *c)
|
|
|
|
{
|
|
|
|
return BTREE_ID_NR + c->btree_roots_extra.nr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct btree_root *bch2_btree_id_root(struct bch_fs *c, unsigned id)
|
|
|
|
{
|
|
|
|
if (likely(id < BTREE_ID_NR)) {
|
|
|
|
return &c->btree_roots_known[id];
|
|
|
|
} else {
|
|
|
|
unsigned idx = id - BTREE_ID_NR;
|
|
|
|
|
2024-11-30 05:08:00 +03:00
|
|
|
/* This can happen when we're called from btree_node_scan */
|
|
|
|
if (idx >= c->btree_roots_extra.nr)
|
|
|
|
return NULL;
|
|
|
|
|
2023-07-07 11:24:54 +03:00
|
|
|
return &c->btree_roots_extra.data[idx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct btree *btree_node_root(struct bch_fs *c, struct btree *b)
|
|
|
|
{
|
2024-11-30 05:08:00 +03:00
|
|
|
struct btree_root *r = bch2_btree_id_root(c, b->c.btree_id);
|
|
|
|
|
|
|
|
return r ? r->b : NULL;
|
2023-07-07 11:24:54 +03:00
|
|
|
}
|
2017-01-08 12:13:18 +03:00
|
|
|
|
2024-10-12 05:54:16 +03:00
|
|
|
const char *bch2_btree_id_str(enum btree_id); /* avoid */
|
2024-06-17 18:31:26 +03:00
|
|
|
void bch2_btree_id_to_text(struct printbuf *, enum btree_id);
|
2024-10-12 05:54:16 +03:00
|
|
|
void bch2_btree_id_level_to_text(struct printbuf *, enum btree_id, unsigned);
|
2024-06-17 18:31:26 +03:00
|
|
|
|
2024-12-30 20:34:22 +03:00
|
|
|
void __bch2_btree_pos_to_text(struct printbuf *, struct bch_fs *,
|
|
|
|
enum btree_id, unsigned, struct bkey_s_c);
|
2023-10-20 05:52:02 +03:00
|
|
|
void bch2_btree_pos_to_text(struct printbuf *, struct bch_fs *, const struct btree *);
|
|
|
|
void bch2_btree_node_to_text(struct printbuf *, struct bch_fs *, const struct btree *);
|
2024-05-03 22:22:22 +03:00
|
|
|
void bch2_btree_cache_to_text(struct printbuf *, const struct btree_cache *);
|
2017-02-02 06:16:42 +03:00
|
|
|
|
2017-10-06 01:41:44 +03:00
|
|
|
#endif /* _BCACHEFS_BTREE_CACHE_H */
|