2017-03-17 06:18:50 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _BCACHEFS_BTREE_TYPES_H
|
|
|
|
#define _BCACHEFS_BTREE_TYPES_H
|
|
|
|
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/rhashtable.h>
|
|
|
|
|
2022-10-21 23:15:07 +00:00
|
|
|
//#include "bkey_methods.h"
|
2019-03-11 18:59:58 +00:00
|
|
|
#include "buckets_types.h"
|
2022-03-29 20:29:10 +00:00
|
|
|
#include "darray.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "journal_types.h"
|
2023-02-05 19:07:34 +00:00
|
|
|
#include "replicas_types.h"
|
2017-03-17 06:18:50 +00:00
|
|
|
#include "six.h"
|
|
|
|
|
|
|
|
struct open_bucket;
|
|
|
|
struct btree_update;
|
2019-03-25 21:06:42 +00:00
|
|
|
struct btree_trans;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
#define MAX_BSETS 3U
|
|
|
|
|
|
|
|
struct btree_nr_keys {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Amount of live metadata (i.e. size of node after a compaction) in
|
|
|
|
* units of u64s
|
|
|
|
*/
|
|
|
|
u16 live_u64s;
|
|
|
|
u16 bset_u64s[MAX_BSETS];
|
|
|
|
|
|
|
|
/* live keys only: */
|
|
|
|
u16 packed_keys;
|
|
|
|
u16 unpacked_keys;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct bset_tree {
|
|
|
|
/*
|
|
|
|
* We construct a binary tree in an array as if the array
|
|
|
|
* started at 1, so that things line up on the same cachelines
|
|
|
|
* better: see comments in bset.c at cacheline_to_bkey() for
|
|
|
|
* details
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* size of the binary tree and prev array */
|
|
|
|
u16 size;
|
|
|
|
|
|
|
|
/* function of size - precalculated for to_inorder() */
|
|
|
|
u16 extra;
|
|
|
|
|
|
|
|
u16 data_offset;
|
|
|
|
u16 aux_data_offset;
|
|
|
|
u16 end_offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct btree_write {
|
|
|
|
struct journal_entry_pin journal;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct btree_alloc {
|
2018-10-06 08:12:42 +00:00
|
|
|
struct open_buckets ob;
|
2020-12-17 20:08:58 +00:00
|
|
|
__BKEY_PADDED(k, BKEY_BTREE_PTR_VAL_U64s_MAX);
|
2017-03-17 06:18:50 +00:00
|
|
|
};
|
|
|
|
|
2020-06-06 16:28:01 +00:00
|
|
|
struct btree_bkey_cached_common {
|
|
|
|
struct six_lock lock;
|
|
|
|
u8 level;
|
|
|
|
u8 btree_id;
|
2022-08-31 22:53:42 +00:00
|
|
|
bool cached;
|
2020-06-06 16:28:01 +00:00
|
|
|
};
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
struct btree {
|
2020-06-06 16:28:01 +00:00
|
|
|
struct btree_bkey_cached_common c;
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
struct rhash_head hash;
|
2020-02-18 22:15:32 +00:00
|
|
|
u64 hash_val;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
unsigned long flags;
|
|
|
|
u16 written;
|
|
|
|
u8 nsets;
|
|
|
|
u8 nr_key_bits;
|
2021-03-14 23:01:14 +00:00
|
|
|
u16 version_ondisk;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
struct bkey_format format;
|
|
|
|
|
|
|
|
struct btree_node *data;
|
|
|
|
void *aux_data;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets of sorted keys - the real btree node - plus a binary search tree
|
|
|
|
*
|
|
|
|
* set[0] is special; set[0]->tree, set[0]->prev and set[0]->data point
|
|
|
|
* to the memory we have allocated for this btree node. Additionally,
|
|
|
|
* set[0]->data points to the entire btree node as it exists on disk.
|
|
|
|
*/
|
|
|
|
struct bset_tree set[MAX_BSETS];
|
|
|
|
|
|
|
|
struct btree_nr_keys nr;
|
|
|
|
u16 sib_u64s[2];
|
|
|
|
u16 whiteout_u64s;
|
2020-07-25 19:07:37 +00:00
|
|
|
u8 byte_order;
|
2017-03-17 06:18:50 +00:00
|
|
|
u8 unpack_fn_len;
|
|
|
|
|
2021-04-04 01:54:14 +00:00
|
|
|
struct btree_write writes[2];
|
|
|
|
|
|
|
|
/* Key/pointer for this btree node */
|
|
|
|
__BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
/*
|
|
|
|
* XXX: add a delete sequence number, so when bch2_btree_node_relock()
|
|
|
|
* fails because the lock sequence number has changed - i.e. the
|
|
|
|
* contents were modified - we can still relock the node if it's still
|
|
|
|
* the one we want, without redoing the traversal
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For asynchronous splits/interior node updates:
|
|
|
|
* When we do a split, we allocate new child nodes and update the parent
|
|
|
|
* node to point to them: we update the parent in memory immediately,
|
|
|
|
* but then we must wait until the children have been written out before
|
|
|
|
* the update to the parent can be written - this is a list of the
|
|
|
|
* btree_updates that are blocking this node from being
|
|
|
|
* written:
|
|
|
|
*/
|
|
|
|
struct list_head write_blocked;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Also for asynchronous splits/interior node updates:
|
|
|
|
* If a btree node isn't reachable yet, we don't want to kick off
|
|
|
|
* another write - because that write also won't yet be reachable and
|
|
|
|
* marking it as completed before it's reachable would be incorrect:
|
|
|
|
*/
|
|
|
|
unsigned long will_make_reachable;
|
|
|
|
|
2018-10-06 08:12:42 +00:00
|
|
|
struct open_buckets ob;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
/* lru list */
|
|
|
|
struct list_head list;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct btree_cache {
|
|
|
|
struct rhashtable table;
|
|
|
|
bool table_init_done;
|
|
|
|
/*
|
|
|
|
* We never free a struct btree, except on shutdown - we just put it on
|
|
|
|
* the btree_cache_freed list and reuse it later. This simplifies the
|
|
|
|
* code, and it doesn't cost us much memory as the memory usage is
|
|
|
|
* dominated by buffers that hold the actual btree node data and those
|
|
|
|
* can be freed - and the number of struct btrees allocated is
|
|
|
|
* effectively bounded.
|
|
|
|
*
|
|
|
|
* btree_cache_freeable effectively is a small cache - we use it because
|
|
|
|
* high order page allocations can be rather expensive, and it's quite
|
|
|
|
* common to delete and allocate btree nodes in quick succession. It
|
|
|
|
* should never grow past ~2-3 nodes in practice.
|
|
|
|
*/
|
|
|
|
struct mutex lock;
|
|
|
|
struct list_head live;
|
|
|
|
struct list_head freeable;
|
2022-03-05 00:16:04 +00:00
|
|
|
struct list_head freed_pcpu;
|
|
|
|
struct list_head freed_nonpcpu;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
/* Number of elements in live + freeable lists */
|
|
|
|
unsigned used;
|
|
|
|
unsigned reserve;
|
2020-11-09 18:01:52 +00:00
|
|
|
atomic_t dirty;
|
2017-03-17 06:18:50 +00:00
|
|
|
struct shrinker shrink;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we need to allocate memory for a new btree node and that
|
|
|
|
* allocation fails, we can cannibalize another node in the btree cache
|
|
|
|
* to satisfy the allocation - lock to guarantee only one thread does
|
|
|
|
* this at a time:
|
|
|
|
*/
|
|
|
|
struct task_struct *alloc_lock;
|
|
|
|
struct closure_waitlist alloc_wait;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct btree_node_iter {
|
|
|
|
struct btree_node_iter_set {
|
|
|
|
u16 k, end;
|
|
|
|
} data[MAX_BSETS];
|
|
|
|
};
|
|
|
|
|
2020-01-10 01:43:58 +00:00
|
|
|
/*
|
|
|
|
* Iterate over all possible positions, synthesizing deleted keys for holes:
|
|
|
|
*/
|
2021-08-30 19:54:41 +00:00
|
|
|
#define BTREE_ITER_SLOTS (1 << 0)
|
2022-04-12 22:04:08 +00:00
|
|
|
#define BTREE_ITER_ALL_LEVELS (1 << 1)
|
2020-01-10 01:43:58 +00:00
|
|
|
/*
|
|
|
|
* Indicates that intent locks should be taken on leaf nodes, because we expect
|
|
|
|
* to be doing updates:
|
|
|
|
*/
|
2022-04-12 22:04:08 +00:00
|
|
|
#define BTREE_ITER_INTENT (1 << 2)
|
2020-01-10 01:43:58 +00:00
|
|
|
/*
|
|
|
|
* Causes the btree iterator code to prefetch additional btree nodes from disk:
|
|
|
|
*/
|
2022-04-12 22:04:08 +00:00
|
|
|
#define BTREE_ITER_PREFETCH (1 << 3)
|
2017-03-17 06:18:50 +00:00
|
|
|
/*
|
|
|
|
* Used in bch2_btree_iter_traverse(), to indicate whether we're searching for
|
|
|
|
* @pos or the first key strictly greater than @pos
|
|
|
|
*/
|
2021-08-30 19:54:41 +00:00
|
|
|
#define BTREE_ITER_IS_EXTENTS (1 << 4)
|
|
|
|
#define BTREE_ITER_NOT_EXTENTS (1 << 5)
|
2022-02-07 04:15:12 +00:00
|
|
|
#define BTREE_ITER_CACHED (1 << 6)
|
2022-08-11 16:23:21 +00:00
|
|
|
#define BTREE_ITER_WITH_KEY_CACHE (1 << 7)
|
|
|
|
#define BTREE_ITER_WITH_UPDATES (1 << 8)
|
|
|
|
#define BTREE_ITER_WITH_JOURNAL (1 << 9)
|
|
|
|
#define __BTREE_ITER_ALL_SNAPSHOTS (1 << 10)
|
|
|
|
#define BTREE_ITER_ALL_SNAPSHOTS (1 << 11)
|
|
|
|
#define BTREE_ITER_FILTER_SNAPSHOTS (1 << 12)
|
|
|
|
#define BTREE_ITER_NOPRESERVE (1 << 13)
|
2022-12-20 16:26:57 +00:00
|
|
|
#define BTREE_ITER_CACHED_NOFILL (1 << 14)
|
2022-11-23 01:15:33 +00:00
|
|
|
#define BTREE_ITER_KEY_CACHE_FILL (1 << 15)
|
2019-03-08 00:46:10 +00:00
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
enum btree_path_uptodate {
|
2017-03-17 06:18:50 +00:00
|
|
|
BTREE_ITER_UPTODATE = 0,
|
2021-08-30 21:31:09 +00:00
|
|
|
BTREE_ITER_NEED_RELOCK = 1,
|
|
|
|
BTREE_ITER_NEED_TRAVERSE = 2,
|
2017-03-17 06:18:50 +00:00
|
|
|
};
|
|
|
|
|
2023-02-05 00:39:59 +00:00
|
|
|
#if defined(CONFIG_BCACHEFS_LOCK_TIME_STATS) || defined(CONFIG_BCACHEFS_DEBUG)
|
|
|
|
#define TRACK_PATH_ALLOCATED
|
|
|
|
#endif
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
struct btree_path {
|
2021-03-20 19:12:05 +00:00
|
|
|
u8 idx;
|
2021-06-12 19:45:45 +00:00
|
|
|
u8 sorted_idx;
|
2021-08-30 19:18:31 +00:00
|
|
|
u8 ref;
|
|
|
|
u8 intent_ref;
|
2021-03-20 19:12:05 +00:00
|
|
|
|
|
|
|
/* btree_iter_copy starts here: */
|
|
|
|
struct bpos pos;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2022-12-03 01:36:06 +00:00
|
|
|
enum btree_id btree_id:5;
|
2021-08-30 19:54:41 +00:00
|
|
|
bool cached:1;
|
2021-08-30 19:18:31 +00:00
|
|
|
bool preserve:1;
|
|
|
|
enum btree_path_uptodate uptodate:2;
|
2021-06-04 21:17:45 +00:00
|
|
|
/*
|
2021-08-30 19:18:31 +00:00
|
|
|
* When true, failing to relock this path will cause the transaction to
|
|
|
|
* restart:
|
2021-06-04 21:17:45 +00:00
|
|
|
*/
|
|
|
|
bool should_be_locked:1;
|
2021-08-30 19:18:31 +00:00
|
|
|
unsigned level:3,
|
2022-12-03 01:36:06 +00:00
|
|
|
locks_want:3;
|
2022-08-21 21:20:42 +00:00
|
|
|
u8 nodes_locked;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
struct btree_path_level {
|
2017-03-17 06:18:50 +00:00
|
|
|
struct btree *b;
|
|
|
|
struct btree_node_iter iter;
|
2018-08-05 18:41:29 +00:00
|
|
|
u32 lock_seq;
|
2022-07-14 08:33:09 +00:00
|
|
|
#ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
|
|
|
|
u64 lock_taken_time;
|
|
|
|
#endif
|
2017-03-17 06:18:50 +00:00
|
|
|
} l[BTREE_MAX_DEPTH];
|
2023-02-05 00:39:59 +00:00
|
|
|
#ifdef TRACK_PATH_ALLOCATED
|
2021-08-30 19:18:31 +00:00
|
|
|
unsigned long ip_allocated;
|
|
|
|
#endif
|
|
|
|
};
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
static inline struct btree_path_level *path_l(struct btree_path *path)
|
|
|
|
{
|
|
|
|
return path->l + path->level;
|
|
|
|
}
|
|
|
|
|
2023-02-05 00:39:59 +00:00
|
|
|
static inline unsigned long btree_path_ip_allocated(struct btree_path *path)
|
|
|
|
{
|
|
|
|
#ifdef TRACK_PATH_ALLOCATED
|
|
|
|
return path->ip_allocated;
|
|
|
|
#else
|
|
|
|
return _THIS_IP_;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
/*
|
|
|
|
* @pos - iterator's current position
|
|
|
|
* @level - current btree depth
|
|
|
|
* @locks_want - btree level below which we start taking intent locks
|
|
|
|
* @nodes_locked - bitmask indicating which nodes in @nodes are locked
|
|
|
|
* @nodes_intent_locked - bitmask indicating which locks are intent locks
|
|
|
|
*/
|
|
|
|
struct btree_iter {
|
|
|
|
struct btree_trans *trans;
|
|
|
|
struct btree_path *path;
|
2022-01-09 02:22:31 +00:00
|
|
|
struct btree_path *update_path;
|
2022-02-07 04:15:12 +00:00
|
|
|
struct btree_path *key_cache_path;
|
2021-08-30 19:18:31 +00:00
|
|
|
|
2022-12-03 01:36:06 +00:00
|
|
|
enum btree_id btree_id:8;
|
2022-04-12 22:04:08 +00:00
|
|
|
unsigned min_depth:3;
|
|
|
|
unsigned advanced:1;
|
2021-08-30 19:18:31 +00:00
|
|
|
|
|
|
|
/* btree_iter_copy starts here: */
|
|
|
|
u16 flags;
|
|
|
|
|
|
|
|
/* When we're filtering by snapshot, the snapshot ID we're looking for: */
|
|
|
|
unsigned snapshot;
|
|
|
|
|
|
|
|
struct bpos pos;
|
2017-03-17 06:18:50 +00:00
|
|
|
/*
|
|
|
|
* Current unpacked key - so that bch2_btree_iter_next()/
|
|
|
|
* bch2_btree_iter_next_slot() can correctly advance pos.
|
|
|
|
*/
|
|
|
|
struct bkey k;
|
2022-05-21 17:10:39 +00:00
|
|
|
|
|
|
|
/* BTREE_ITER_WITH_JOURNAL: */
|
|
|
|
size_t journal_idx;
|
|
|
|
struct bpos journal_pos;
|
2023-02-05 00:39:59 +00:00
|
|
|
#ifdef TRACK_PATH_ALLOCATED
|
2023-01-09 06:11:18 +00:00
|
|
|
unsigned long ip_allocated;
|
|
|
|
#endif
|
2017-03-17 06:18:50 +00:00
|
|
|
};
|
|
|
|
|
2022-06-17 05:07:54 +00:00
|
|
|
struct btree_key_cache_freelist {
|
|
|
|
struct bkey_cached *objs[16];
|
|
|
|
unsigned nr;
|
|
|
|
};
|
|
|
|
|
2019-03-08 00:46:10 +00:00
|
|
|
struct btree_key_cache {
|
|
|
|
struct mutex lock;
|
|
|
|
struct rhashtable table;
|
2020-11-30 04:48:20 +00:00
|
|
|
bool table_init_done;
|
2022-09-04 02:07:31 +00:00
|
|
|
struct list_head freed_pcpu;
|
|
|
|
struct list_head freed_nonpcpu;
|
2020-11-12 22:19:47 +00:00
|
|
|
struct shrinker shrink;
|
2021-03-25 03:37:33 +00:00
|
|
|
unsigned shrink_iter;
|
2022-06-17 05:07:54 +00:00
|
|
|
struct btree_key_cache_freelist __percpu *pcpu_freed;
|
2020-11-09 18:01:52 +00:00
|
|
|
|
2022-06-17 05:07:54 +00:00
|
|
|
atomic_long_t nr_freed;
|
2021-03-25 03:37:33 +00:00
|
|
|
atomic_long_t nr_keys;
|
|
|
|
atomic_long_t nr_dirty;
|
2019-03-08 00:46:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct bkey_cached_key {
|
|
|
|
u32 btree_id;
|
|
|
|
struct bpos pos;
|
2022-10-22 19:00:16 +00:00
|
|
|
} __packed __aligned(4);
|
2019-03-08 00:46:10 +00:00
|
|
|
|
2020-11-19 20:38:27 +00:00
|
|
|
#define BKEY_CACHED_ACCESSED 0
|
|
|
|
#define BKEY_CACHED_DIRTY 1
|
2019-03-08 00:46:10 +00:00
|
|
|
|
|
|
|
struct bkey_cached {
|
|
|
|
struct btree_bkey_cached_common c;
|
|
|
|
|
|
|
|
unsigned long flags;
|
2022-03-18 01:35:51 +00:00
|
|
|
u16 u64s;
|
2019-03-08 00:46:10 +00:00
|
|
|
bool valid;
|
2020-11-12 22:19:47 +00:00
|
|
|
u32 btree_trans_barrier_seq;
|
2019-03-08 00:46:10 +00:00
|
|
|
struct bkey_cached_key key;
|
|
|
|
|
|
|
|
struct rhash_head hash;
|
|
|
|
struct list_head list;
|
|
|
|
|
|
|
|
struct journal_preres res;
|
|
|
|
struct journal_entry_pin journal;
|
2023-01-04 09:34:16 +00:00
|
|
|
u64 seq;
|
2019-03-08 00:46:10 +00:00
|
|
|
|
|
|
|
struct bkey_i *k;
|
|
|
|
};
|
|
|
|
|
2022-08-31 22:53:42 +00:00
|
|
|
static inline struct bpos btree_node_pos(struct btree_bkey_cached_common *b)
|
2022-08-19 19:35:34 +00:00
|
|
|
{
|
2022-08-31 22:53:42 +00:00
|
|
|
return !b->cached
|
2022-08-19 19:35:34 +00:00
|
|
|
? container_of(b, struct btree, c)->key.k.p
|
|
|
|
: container_of(b, struct bkey_cached, c)->key.pos;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
struct btree_insert_entry {
|
2021-07-06 02:16:02 +00:00
|
|
|
unsigned flags;
|
2021-02-21 01:51:57 +00:00
|
|
|
u8 bkey_type;
|
bcachefs: Start using bpos.snapshot field
This patch starts treating the bpos.snapshot field like part of the key
in the btree code:
* bpos_successor() and bpos_predecessor() now include the snapshot field
* Keys in btrees that will be using snapshots (extents, inodes, dirents
and xattrs) now always have their snapshot field set to U32_MAX
The btree iterator code gets a new flag, BTREE_ITER_ALL_SNAPSHOTS, that
determines whether we're iterating over keys in all snapshots or not -
internally, this controlls whether bkey_(successor|predecessor)
increment/decrement the snapshot field, or only the higher bits of the
key.
We add a new member to struct btree_iter, iter->snapshot: when
BTREE_ITER_ALL_SNAPSHOTS is not set, iter->pos.snapshot should always
equal iter->snapshot, which will be 0 for btrees that don't use
snapshots, and alsways U32_MAX for btrees that will use snapshots
(until we enable snapshot creation).
This patch also introduces a new metadata version number, and compat
code for reading from/writing to older versions - this isn't a forced
upgrade (yet).
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2021-03-24 22:02:16 +00:00
|
|
|
enum btree_id btree_id:8;
|
2022-02-24 16:02:58 +00:00
|
|
|
u8 level:4;
|
2021-08-30 20:08:34 +00:00
|
|
|
bool cached:1;
|
2021-10-27 16:51:12 +00:00
|
|
|
bool insert_trigger_run:1;
|
|
|
|
bool overwrite_trigger_run:1;
|
2022-01-12 06:14:47 +00:00
|
|
|
bool key_cache_already_flushed:1;
|
2022-02-24 16:02:58 +00:00
|
|
|
/*
|
|
|
|
* @old_k may be a key from the journal; @old_btree_u64s always refers
|
|
|
|
* to the size of the key being overwritten in the btree:
|
|
|
|
*/
|
|
|
|
u8 old_btree_u64s;
|
2018-07-17 17:50:15 +00:00
|
|
|
struct bkey_i *k;
|
2021-08-30 19:18:31 +00:00
|
|
|
struct btree_path *path;
|
2022-02-24 16:02:58 +00:00
|
|
|
/* key being overwritten: */
|
|
|
|
struct bkey old_k;
|
|
|
|
const struct bch_val *old_v;
|
2021-08-25 01:26:43 +00:00
|
|
|
unsigned long ip_allocated;
|
2017-03-17 06:18:50 +00:00
|
|
|
};
|
|
|
|
|
2020-03-29 21:01:05 +00:00
|
|
|
#ifndef CONFIG_LOCKDEP
|
2019-03-08 04:13:39 +00:00
|
|
|
#define BTREE_ITER_MAX 64
|
2020-03-29 21:01:05 +00:00
|
|
|
#else
|
|
|
|
#define BTREE_ITER_MAX 32
|
|
|
|
#endif
|
2019-03-08 04:13:39 +00:00
|
|
|
|
2021-02-04 02:51:56 +00:00
|
|
|
struct btree_trans_commit_hook;
|
|
|
|
typedef int (btree_trans_commit_hook_fn)(struct btree_trans *, struct btree_trans_commit_hook *);
|
|
|
|
|
|
|
|
struct btree_trans_commit_hook {
|
|
|
|
btree_trans_commit_hook_fn *fn;
|
|
|
|
struct btree_trans_commit_hook *next;
|
|
|
|
};
|
|
|
|
|
2022-05-19 19:29:50 +00:00
|
|
|
#define BTREE_TRANS_MEM_MAX (1U << 16)
|
2021-04-24 04:09:06 +00:00
|
|
|
|
2022-07-13 10:03:21 +00:00
|
|
|
#define BTREE_TRANS_MAX_LOCK_HOLD_TIME_NS 10000
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
struct btree_trans {
|
|
|
|
struct bch_fs *c;
|
2022-01-04 05:33:52 +00:00
|
|
|
const char *fn;
|
bcachefs: Deadlock cycle detector
We've outgrown our own deadlock avoidance strategy.
The btree iterator API provides an interface where the user doesn't need
to concern themselves with lock ordering - different btree iterators can
be traversed in any order. Without special care, this will lead to
deadlocks.
Our previous strategy was to define a lock ordering internally, and
whenever we attempt to take a lock and trylock() fails, we'd check if
the current btree transaction is holding any locks that cause a lock
ordering violation. If so, we'd issue a transaction restart, and then
bch2_trans_begin() would re-traverse all previously used iterators, but
in the correct order.
That approach had some issues, though.
- Sometimes we'd issue transaction restarts unnecessarily, when no
deadlock would have actually occured. Lock ordering restarts have
become our primary cause of transaction restarts, on some workloads
totally 20% of actual transaction commits.
- To avoid deadlock or livelock, we'd often have to take intent locks
when we only wanted a read lock: with the lock ordering approach, it
is actually illegal to hold _any_ read lock while blocking on an intent
lock, and this has been causing us unnecessary lock contention.
- It was getting fragile - the various lock ordering rules are not
trivial, and we'd been seeing occasional livelock issues related to
this machinery.
So, since bcachefs is already a relational database masquerading as a
filesystem, we're stealing the next traditional database technique and
switching to a cycle detector for avoiding deadlocks.
When we block taking a btree lock, after adding ourself to the waitlist
but before sleeping, we do a DFS of btree transactions waiting on other
btree transactions, starting with the current transaction and walking
our held locks, and transactions blocking on our held locks.
If we find a cycle, we emit a transaction restart. Occasionally (e.g.
the btree split path) we can not allow the lock() operation to fail, so
if necessary we'll tell another transaction that it has to fail.
Result: trans_restart_would_deadlock events are reduced by a factor of
10 to 100, and we'll be able to delete a whole bunch of grotty, fragile
code.
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-08-22 17:23:47 +00:00
|
|
|
struct closure ref;
|
2020-06-02 20:36:11 +00:00
|
|
|
struct list_head list;
|
2022-07-13 10:03:21 +00:00
|
|
|
u64 last_begin_time;
|
bcachefs: Deadlock cycle detector
We've outgrown our own deadlock avoidance strategy.
The btree iterator API provides an interface where the user doesn't need
to concern themselves with lock ordering - different btree iterators can
be traversed in any order. Without special care, this will lead to
deadlocks.
Our previous strategy was to define a lock ordering internally, and
whenever we attempt to take a lock and trylock() fails, we'd check if
the current btree transaction is holding any locks that cause a lock
ordering violation. If so, we'd issue a transaction restart, and then
bch2_trans_begin() would re-traverse all previously used iterators, but
in the correct order.
That approach had some issues, though.
- Sometimes we'd issue transaction restarts unnecessarily, when no
deadlock would have actually occured. Lock ordering restarts have
become our primary cause of transaction restarts, on some workloads
totally 20% of actual transaction commits.
- To avoid deadlock or livelock, we'd often have to take intent locks
when we only wanted a read lock: with the lock ordering approach, it
is actually illegal to hold _any_ read lock while blocking on an intent
lock, and this has been causing us unnecessary lock contention.
- It was getting fragile - the various lock ordering rules are not
trivial, and we'd been seeing occasional livelock issues related to
this machinery.
So, since bcachefs is already a relational database masquerading as a
filesystem, we're stealing the next traditional database technique and
switching to a cycle detector for avoiding deadlocks.
When we block taking a btree lock, after adding ourself to the waitlist
but before sleeping, we do a DFS of btree transactions waiting on other
btree transactions, starting with the current transaction and walking
our held locks, and transactions blocking on our held locks.
If we find a cycle, we emit a transaction restart. Occasionally (e.g.
the btree split path) we can not allow the lock() operation to fail, so
if necessary we'll tell another transaction that it has to fail.
Result: trans_restart_would_deadlock events are reduced by a factor of
10 to 100, and we'll be able to delete a whole bunch of grotty, fragile
code.
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
2022-08-22 17:23:47 +00:00
|
|
|
|
|
|
|
u8 lock_may_not_fail;
|
|
|
|
u8 lock_must_abort;
|
|
|
|
struct btree_bkey_cached_common *locking;
|
|
|
|
struct six_lock_waiter locking_wait;
|
|
|
|
|
2020-11-15 21:30:22 +00:00
|
|
|
int srcu_idx;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2022-08-11 23:36:24 +00:00
|
|
|
u8 fn_idx;
|
2021-06-12 19:45:45 +00:00
|
|
|
u8 nr_sorted;
|
2017-03-17 06:18:50 +00:00
|
|
|
u8 nr_updates;
|
bcachefs: Btree write buffer
This adds a new method of doing btree updates - a straight write buffer,
implemented as a flat fixed size array.
This is only useful when we don't need to read from the btree in order
to do the update, and when reading is infrequent - perfect for the LRU
btree.
This will make LRU btree updates fast enough that we'll be able to use
it for persistently indexing buckets by fragmentation, which will be a
massive boost to copygc performance.
Changes:
- A new btree_insert_type enum, for btree_insert_entries. Specifies
btree, btree key cache, or btree write buffer.
- bch2_trans_update_buffered(): updates via the btree write buffer
don't need a btree path, so we need a new update path.
- Transaction commit path changes:
The update to the btree write buffer both mutates global, and can
fail if there isn't currently room. Therefore we do all write buffer
updates in the transaction all at once, and also if it fails we have
to revert filesystem usage counter changes.
If there isn't room we flush the write buffer in the transaction
commit error path and retry.
- A new persistent option, for specifying the number of entries in the
write buffer.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-01-04 05:00:50 +00:00
|
|
|
u8 nr_wb_updates;
|
|
|
|
u8 wb_updates_size;
|
2021-07-25 21:19:52 +00:00
|
|
|
bool used_mempool:1;
|
|
|
|
bool in_traverse_all:1;
|
2021-08-30 19:18:31 +00:00
|
|
|
bool paths_sorted:1;
|
2022-02-18 05:47:45 +00:00
|
|
|
bool memory_allocation_failure:1;
|
2021-12-31 21:12:54 +00:00
|
|
|
bool journal_transaction_names:1;
|
2021-12-26 01:07:00 +00:00
|
|
|
bool journal_replay_not_finished:1;
|
2023-02-09 19:48:54 +00:00
|
|
|
bool notrace_relock_fail:1;
|
2022-07-18 03:06:38 +00:00
|
|
|
enum bch_errcode restarted:16;
|
2022-07-17 23:35:38 +00:00
|
|
|
u32 restart_count;
|
|
|
|
unsigned long last_restarted_ip;
|
2022-12-16 02:44:32 +00:00
|
|
|
unsigned long srcu_lock_time;
|
2022-07-17 23:35:38 +00:00
|
|
|
|
2021-06-12 19:45:56 +00:00
|
|
|
/*
|
|
|
|
* For when bch2_trans_update notices we'll be splitting a compressed
|
|
|
|
* extent:
|
|
|
|
*/
|
|
|
|
unsigned extra_journal_res;
|
2022-08-12 00:14:54 +00:00
|
|
|
unsigned nr_max_paths;
|
2017-03-17 06:18:50 +00:00
|
|
|
|
2021-08-30 19:18:31 +00:00
|
|
|
u64 paths_allocated;
|
2020-12-02 04:11:53 +00:00
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
unsigned mem_top;
|
2022-08-23 01:49:55 +00:00
|
|
|
unsigned mem_max;
|
2017-03-17 06:18:50 +00:00
|
|
|
unsigned mem_bytes;
|
|
|
|
void *mem;
|
|
|
|
|
2021-06-12 19:45:45 +00:00
|
|
|
u8 sorted[BTREE_ITER_MAX + 8];
|
2021-08-30 19:18:31 +00:00
|
|
|
struct btree_path *paths;
|
2019-03-08 04:13:39 +00:00
|
|
|
struct btree_insert_entry *updates;
|
bcachefs: Btree write buffer
This adds a new method of doing btree updates - a straight write buffer,
implemented as a flat fixed size array.
This is only useful when we don't need to read from the btree in order
to do the update, and when reading is infrequent - perfect for the LRU
btree.
This will make LRU btree updates fast enough that we'll be able to use
it for persistently indexing buckets by fragmentation, which will be a
massive boost to copygc performance.
Changes:
- A new btree_insert_type enum, for btree_insert_entries. Specifies
btree, btree key cache, or btree write buffer.
- bch2_trans_update_buffered(): updates via the btree write buffer
don't need a btree path, so we need a new update path.
- Transaction commit path changes:
The update to the btree write buffer both mutates global, and can
fail if there isn't currently room. Therefore we do all write buffer
updates in the transaction all at once, and also if it fails we have
to revert filesystem usage counter changes.
If there isn't room we flush the write buffer in the transaction
commit error path and retry.
- A new persistent option, for specifying the number of entries in the
write buffer.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-01-04 05:00:50 +00:00
|
|
|
struct btree_write_buffered_key *wb_updates;
|
2019-03-14 02:44:04 +00:00
|
|
|
|
|
|
|
/* update path: */
|
2021-02-04 02:51:56 +00:00
|
|
|
struct btree_trans_commit_hook *hooks;
|
2022-12-14 15:39:04 +00:00
|
|
|
darray_u64 extra_journal_entries;
|
2020-05-25 18:57:06 +00:00
|
|
|
struct journal_entry_pin *journal_pin;
|
2020-05-25 23:29:48 +00:00
|
|
|
|
2019-03-14 02:44:04 +00:00
|
|
|
struct journal_res journal_res;
|
|
|
|
struct journal_preres journal_preres;
|
|
|
|
u64 *journal_seq;
|
2019-03-15 21:11:58 +00:00
|
|
|
struct disk_reservation *disk_res;
|
2019-05-15 13:49:46 +00:00
|
|
|
unsigned journal_u64s;
|
2019-12-24 23:03:53 +00:00
|
|
|
unsigned journal_preres_u64s;
|
2019-10-19 23:03:23 +00:00
|
|
|
struct replicas_delta_list *fs_usage_deltas;
|
2017-03-17 06:18:50 +00:00
|
|
|
};
|
|
|
|
|
2022-11-17 21:03:15 +00:00
|
|
|
#define BCH_BTREE_WRITE_TYPES() \
|
|
|
|
x(initial, 0) \
|
|
|
|
x(init_next_bset, 1) \
|
|
|
|
x(cache_reclaim, 2) \
|
|
|
|
x(journal_reclaim, 3) \
|
|
|
|
x(interior, 4)
|
|
|
|
|
|
|
|
enum btree_write_type {
|
|
|
|
#define x(t, n) BTREE_WRITE_##t,
|
|
|
|
BCH_BTREE_WRITE_TYPES()
|
|
|
|
#undef x
|
|
|
|
BTREE_WRITE_TYPE_NR,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define BTREE_WRITE_TYPE_MASK (roundup_pow_of_two(BTREE_WRITE_TYPE_NR) - 1)
|
|
|
|
#define BTREE_WRITE_TYPE_BITS ilog2(roundup_pow_of_two(BTREE_WRITE_TYPE_NR))
|
|
|
|
|
2022-02-26 16:10:20 +00:00
|
|
|
#define BTREE_FLAGS() \
|
|
|
|
x(read_in_flight) \
|
|
|
|
x(read_error) \
|
|
|
|
x(dirty) \
|
|
|
|
x(need_write) \
|
2022-02-27 14:56:33 +00:00
|
|
|
x(write_blocked) \
|
|
|
|
x(will_make_reachable) \
|
2022-02-26 16:10:20 +00:00
|
|
|
x(noevict) \
|
|
|
|
x(write_idx) \
|
|
|
|
x(accessed) \
|
|
|
|
x(write_in_flight) \
|
|
|
|
x(write_in_flight_inner) \
|
|
|
|
x(just_written) \
|
|
|
|
x(dying) \
|
|
|
|
x(fake) \
|
|
|
|
x(need_rewrite) \
|
|
|
|
x(never_write)
|
|
|
|
|
|
|
|
enum btree_flags {
|
2022-11-17 21:03:15 +00:00
|
|
|
/* First bits for btree node write type */
|
|
|
|
BTREE_NODE_FLAGS_START = BTREE_WRITE_TYPE_BITS - 1,
|
2022-02-26 16:10:20 +00:00
|
|
|
#define x(flag) BTREE_NODE_##flag,
|
|
|
|
BTREE_FLAGS()
|
|
|
|
#undef x
|
|
|
|
};
|
|
|
|
|
|
|
|
#define x(flag) \
|
2017-03-17 06:18:50 +00:00
|
|
|
static inline bool btree_node_ ## flag(struct btree *b) \
|
|
|
|
{ return test_bit(BTREE_NODE_ ## flag, &b->flags); } \
|
|
|
|
\
|
|
|
|
static inline void set_btree_node_ ## flag(struct btree *b) \
|
|
|
|
{ set_bit(BTREE_NODE_ ## flag, &b->flags); } \
|
|
|
|
\
|
|
|
|
static inline void clear_btree_node_ ## flag(struct btree *b) \
|
|
|
|
{ clear_bit(BTREE_NODE_ ## flag, &b->flags); }
|
|
|
|
|
2022-02-26 16:10:20 +00:00
|
|
|
BTREE_FLAGS()
|
|
|
|
#undef x
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
static inline struct btree_write *btree_current_write(struct btree *b)
|
|
|
|
{
|
|
|
|
return b->writes + btree_node_write_idx(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct btree_write *btree_prev_write(struct btree *b)
|
|
|
|
{
|
|
|
|
return b->writes + (btree_node_write_idx(b) ^ 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct bset_tree *bset_tree_last(struct btree *b)
|
|
|
|
{
|
|
|
|
EBUG_ON(!b->nsets);
|
|
|
|
return b->set + b->nsets - 1;
|
|
|
|
}
|
|
|
|
|
2018-08-06 02:23:44 +00:00
|
|
|
static inline void *
|
|
|
|
__btree_node_offset_to_ptr(const struct btree *b, u16 offset)
|
|
|
|
{
|
|
|
|
return (void *) ((u64 *) b->data + 1 + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u16
|
|
|
|
__btree_node_ptr_to_offset(const struct btree *b, const void *p)
|
|
|
|
{
|
|
|
|
u16 ret = (u64 *) p - 1 - (u64 *) b->data;
|
|
|
|
|
|
|
|
EBUG_ON(__btree_node_offset_to_ptr(b, ret) != p);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static inline struct bset *bset(const struct btree *b,
|
|
|
|
const struct bset_tree *t)
|
|
|
|
{
|
2018-08-06 02:23:44 +00:00
|
|
|
return __btree_node_offset_to_ptr(b, t->data_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void set_btree_bset_end(struct btree *b, struct bset_tree *t)
|
|
|
|
{
|
|
|
|
t->end_offset =
|
|
|
|
__btree_node_ptr_to_offset(b, vstruct_last(bset(b, t)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void set_btree_bset(struct btree *b, struct bset_tree *t,
|
|
|
|
const struct bset *i)
|
|
|
|
{
|
|
|
|
t->data_offset = __btree_node_ptr_to_offset(b, i);
|
|
|
|
set_btree_bset_end(b, t);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct bset *btree_bset_first(struct btree *b)
|
|
|
|
{
|
|
|
|
return bset(b, b->set);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct bset *btree_bset_last(struct btree *b)
|
|
|
|
{
|
|
|
|
return bset(b, bset_tree_last(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u16
|
|
|
|
__btree_node_key_to_offset(const struct btree *b, const struct bkey_packed *k)
|
|
|
|
{
|
2018-08-06 02:23:44 +00:00
|
|
|
return __btree_node_ptr_to_offset(b, k);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct bkey_packed *
|
|
|
|
__btree_node_offset_to_key(const struct btree *b, u16 k)
|
|
|
|
{
|
2018-08-06 02:23:44 +00:00
|
|
|
return __btree_node_offset_to_ptr(b, k);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-06 02:34:03 +00:00
|
|
|
static inline unsigned btree_bkey_first_offset(const struct bset_tree *t)
|
|
|
|
{
|
|
|
|
return t->data_offset + offsetof(struct bset, _data) / sizeof(u64);
|
|
|
|
}
|
|
|
|
|
2018-08-06 02:23:44 +00:00
|
|
|
#define btree_bkey_first(_b, _t) \
|
|
|
|
({ \
|
|
|
|
EBUG_ON(bset(_b, _t)->start != \
|
|
|
|
__btree_node_offset_to_key(_b, btree_bkey_first_offset(_t)));\
|
|
|
|
\
|
|
|
|
bset(_b, _t)->start; \
|
|
|
|
})
|
2017-03-17 06:18:50 +00:00
|
|
|
|
|
|
|
#define btree_bkey_last(_b, _t) \
|
|
|
|
({ \
|
|
|
|
EBUG_ON(__btree_node_offset_to_key(_b, (_t)->end_offset) != \
|
|
|
|
vstruct_last(bset(_b, _t))); \
|
|
|
|
\
|
|
|
|
__btree_node_offset_to_key(_b, (_t)->end_offset); \
|
|
|
|
})
|
|
|
|
|
2019-10-19 23:03:23 +00:00
|
|
|
static inline unsigned bset_u64s(struct bset_tree *t)
|
|
|
|
{
|
|
|
|
return t->end_offset - t->data_offset -
|
|
|
|
sizeof(struct bset) / sizeof(u64);
|
|
|
|
}
|
|
|
|
|
2019-12-13 18:08:37 +00:00
|
|
|
static inline unsigned bset_dead_u64s(struct btree *b, struct bset_tree *t)
|
|
|
|
{
|
|
|
|
return bset_u64s(t) - b->nr.bset_u64s[t - b->set];
|
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
static inline unsigned bset_byte_offset(struct btree *b, void *i)
|
|
|
|
{
|
|
|
|
return i - (void *) b->data;
|
|
|
|
}
|
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
enum btree_node_type {
|
2021-02-21 00:27:37 +00:00
|
|
|
#define x(kwd, val) BKEY_TYPE_##kwd = val,
|
2018-11-01 19:10:01 +00:00
|
|
|
BCH_BTREE_IDS()
|
|
|
|
#undef x
|
2021-02-21 00:27:37 +00:00
|
|
|
BKEY_TYPE_btree,
|
2018-11-01 19:10:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Type of a key in btree @id at level @level: */
|
|
|
|
static inline enum btree_node_type __btree_node_type(unsigned level, enum btree_id id)
|
|
|
|
{
|
2021-02-21 00:27:37 +00:00
|
|
|
return level ? BKEY_TYPE_btree : (enum btree_node_type) id;
|
2018-11-01 19:10:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 06:18:50 +00:00
|
|
|
/* Type of keys @b contains: */
|
2018-11-01 19:10:01 +00:00
|
|
|
static inline enum btree_node_type btree_node_type(struct btree *b)
|
2017-03-17 06:18:50 +00:00
|
|
|
{
|
2020-06-06 16:28:01 +00:00
|
|
|
return __btree_node_type(b->c.level, b->c.btree_id);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 01:51:57 +00:00
|
|
|
#define BTREE_NODE_TYPE_HAS_TRANS_TRIGGERS \
|
2021-02-21 00:27:37 +00:00
|
|
|
((1U << BKEY_TYPE_extents)| \
|
2021-12-11 22:13:09 +00:00
|
|
|
(1U << BKEY_TYPE_alloc)| \
|
2021-02-21 00:27:37 +00:00
|
|
|
(1U << BKEY_TYPE_inodes)| \
|
|
|
|
(1U << BKEY_TYPE_stripes)| \
|
2021-02-21 01:51:57 +00:00
|
|
|
(1U << BKEY_TYPE_reflink)| \
|
2021-02-21 00:27:37 +00:00
|
|
|
(1U << BKEY_TYPE_btree))
|
2019-10-20 04:24:51 +00:00
|
|
|
|
2021-02-21 01:51:57 +00:00
|
|
|
#define BTREE_NODE_TYPE_HAS_MEM_TRIGGERS \
|
|
|
|
((1U << BKEY_TYPE_alloc)| \
|
2021-10-30 01:14:23 +00:00
|
|
|
(1U << BKEY_TYPE_inodes)| \
|
2021-03-16 04:42:25 +00:00
|
|
|
(1U << BKEY_TYPE_stripes)| \
|
|
|
|
(1U << BKEY_TYPE_snapshots))
|
2021-02-21 01:51:57 +00:00
|
|
|
|
|
|
|
#define BTREE_NODE_TYPE_HAS_TRIGGERS \
|
|
|
|
(BTREE_NODE_TYPE_HAS_TRANS_TRIGGERS| \
|
|
|
|
BTREE_NODE_TYPE_HAS_MEM_TRIGGERS)
|
2019-10-20 04:24:51 +00:00
|
|
|
|
2021-12-11 22:13:09 +00:00
|
|
|
#define BTREE_ID_IS_EXTENTS \
|
|
|
|
((1U << BTREE_ID_extents)| \
|
|
|
|
(1U << BTREE_ID_reflink)| \
|
|
|
|
(1U << BTREE_ID_freespace))
|
|
|
|
|
|
|
|
static inline bool btree_node_type_is_extents(enum btree_node_type type)
|
|
|
|
{
|
|
|
|
return (1U << type) & BTREE_ID_IS_EXTENTS;
|
|
|
|
}
|
|
|
|
|
bcachefs: Start using bpos.snapshot field
This patch starts treating the bpos.snapshot field like part of the key
in the btree code:
* bpos_successor() and bpos_predecessor() now include the snapshot field
* Keys in btrees that will be using snapshots (extents, inodes, dirents
and xattrs) now always have their snapshot field set to U32_MAX
The btree iterator code gets a new flag, BTREE_ITER_ALL_SNAPSHOTS, that
determines whether we're iterating over keys in all snapshots or not -
internally, this controlls whether bkey_(successor|predecessor)
increment/decrement the snapshot field, or only the higher bits of the
key.
We add a new member to struct btree_iter, iter->snapshot: when
BTREE_ITER_ALL_SNAPSHOTS is not set, iter->pos.snapshot should always
equal iter->snapshot, which will be 0 for btrees that don't use
snapshots, and alsways U32_MAX for btrees that will use snapshots
(until we enable snapshot creation).
This patch also introduces a new metadata version number, and compat
code for reading from/writing to older versions - this isn't a forced
upgrade (yet).
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2021-03-24 22:02:16 +00:00
|
|
|
#define BTREE_ID_HAS_SNAPSHOTS \
|
|
|
|
((1U << BTREE_ID_extents)| \
|
|
|
|
(1U << BTREE_ID_inodes)| \
|
|
|
|
(1U << BTREE_ID_dirents)| \
|
|
|
|
(1U << BTREE_ID_xattrs))
|
|
|
|
|
2021-04-09 20:52:30 +00:00
|
|
|
#define BTREE_ID_HAS_PTRS \
|
|
|
|
((1U << BTREE_ID_extents)| \
|
|
|
|
(1U << BTREE_ID_reflink))
|
|
|
|
|
bcachefs: Start using bpos.snapshot field
This patch starts treating the bpos.snapshot field like part of the key
in the btree code:
* bpos_successor() and bpos_predecessor() now include the snapshot field
* Keys in btrees that will be using snapshots (extents, inodes, dirents
and xattrs) now always have their snapshot field set to U32_MAX
The btree iterator code gets a new flag, BTREE_ITER_ALL_SNAPSHOTS, that
determines whether we're iterating over keys in all snapshots or not -
internally, this controlls whether bkey_(successor|predecessor)
increment/decrement the snapshot field, or only the higher bits of the
key.
We add a new member to struct btree_iter, iter->snapshot: when
BTREE_ITER_ALL_SNAPSHOTS is not set, iter->pos.snapshot should always
equal iter->snapshot, which will be 0 for btrees that don't use
snapshots, and alsways U32_MAX for btrees that will use snapshots
(until we enable snapshot creation).
This patch also introduces a new metadata version number, and compat
code for reading from/writing to older versions - this isn't a forced
upgrade (yet).
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2021-03-24 22:02:16 +00:00
|
|
|
static inline bool btree_type_has_snapshots(enum btree_id id)
|
|
|
|
{
|
|
|
|
return (1 << id) & BTREE_ID_HAS_SNAPSHOTS;
|
|
|
|
}
|
|
|
|
|
2022-07-17 04:31:40 +00:00
|
|
|
static inline bool btree_type_has_ptrs(enum btree_id id)
|
|
|
|
{
|
|
|
|
return (1 << id) & BTREE_ID_HAS_PTRS;
|
|
|
|
}
|
|
|
|
|
2018-11-01 19:10:01 +00:00
|
|
|
static inline bool btree_node_type_needs_gc(enum btree_node_type type)
|
|
|
|
{
|
2019-10-20 04:24:51 +00:00
|
|
|
return BTREE_NODE_TYPE_HAS_TRIGGERS & (1U << type);
|
2017-03-17 06:18:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct btree_root {
|
|
|
|
struct btree *b;
|
|
|
|
|
|
|
|
/* On disk root - see async splits: */
|
|
|
|
__BKEY_PADDED(key, BKEY_BTREE_PTR_VAL_U64s_MAX);
|
|
|
|
u8 level;
|
|
|
|
u8 alive;
|
2019-01-25 00:09:49 +00:00
|
|
|
s8 error;
|
2017-03-17 06:18:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum btree_gc_coalesce_fail_reason {
|
|
|
|
BTREE_GC_COALESCE_FAIL_RESERVE_GET,
|
|
|
|
BTREE_GC_COALESCE_FAIL_KEYLIST_REALLOC,
|
|
|
|
BTREE_GC_COALESCE_FAIL_FORMAT_FITS,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum btree_node_sibling {
|
|
|
|
btree_prev_sib,
|
|
|
|
btree_next_sib,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _BCACHEFS_BTREE_TYPES_H */
|