2023-08-16 16:54:33 -04:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _BCACHEFS_SNAPSHOT_H
|
|
|
|
#define _BCACHEFS_SNAPSHOT_H
|
|
|
|
|
|
|
|
void bch2_snapshot_tree_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
|
2024-08-12 21:31:25 -04:00
|
|
|
int bch2_snapshot_tree_validate(struct bch_fs *, struct bkey_s_c,
|
2024-11-27 00:29:52 -05:00
|
|
|
struct bkey_validate_context);
|
2023-08-16 16:54:33 -04:00
|
|
|
|
|
|
|
#define bch2_bkey_ops_snapshot_tree ((struct bkey_ops) { \
|
2024-08-12 21:31:25 -04:00
|
|
|
.key_validate = bch2_snapshot_tree_validate, \
|
2023-08-16 16:54:33 -04:00
|
|
|
.val_to_text = bch2_snapshot_tree_to_text, \
|
|
|
|
.min_val_size = 8, \
|
|
|
|
})
|
|
|
|
|
|
|
|
struct bkey_i_snapshot_tree *__bch2_snapshot_tree_create(struct btree_trans *);
|
|
|
|
|
|
|
|
int bch2_snapshot_tree_lookup(struct btree_trans *, u32, struct bch_snapshot_tree *);
|
|
|
|
|
|
|
|
void bch2_snapshot_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
|
2024-11-27 00:29:52 -05:00
|
|
|
int bch2_snapshot_validate(struct bch_fs *, struct bkey_s_c,
|
|
|
|
struct bkey_validate_context);
|
2023-08-16 16:54:33 -04:00
|
|
|
int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
|
bcachefs: Fix type of flags parameter for some ->trigger() implementations
When building with clang's -Wincompatible-function-pointer-types-strict
(a warning designed to catch potential kCFI failures at build time),
there are several warnings along the lines of:
fs/bcachefs/bkey_methods.c:118:2: error: incompatible function pointer types initializing 'int (*)(struct btree_trans *, enum btree_id, unsigned int, struct bkey_s_c, struct bkey_s, enum btree_iter_update_trigger_flags)' with an expression of type 'int (struct btree_trans *, enum btree_id, unsigned int, struct bkey_s_c, struct bkey_s, unsigned int)' [-Werror,-Wincompatible-function-pointer-types-strict]
118 | BCH_BKEY_TYPES()
| ^~~~~~~~~~~~~~~~
fs/bcachefs/bcachefs_format.h:394:2: note: expanded from macro 'BCH_BKEY_TYPES'
394 | x(inode, 8) \
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
fs/bcachefs/bkey_methods.c:117:41: note: expanded from macro 'x'
117 | #define x(name, nr) [KEY_TYPE_##name] = bch2_bkey_ops_##name,
| ^~~~~~~~~~~~~~~~~~~~
<scratch space>:277:1: note: expanded from here
277 | bch2_bkey_ops_inode
| ^~~~~~~~~~~~~~~~~~~
fs/bcachefs/inode.h:26:13: note: expanded from macro 'bch2_bkey_ops_inode'
26 | .trigger = bch2_trigger_inode, \
| ^~~~~~~~~~~~~~~~~~
There are several functions that did not have their flags parameter
converted to 'enum btree_iter_update_trigger_flags' in the recent
unification, which will cause kCFI failures at runtime because the
types, while ABI compatible (hence no warning from the non-strict
version of this warning), do not match exactly.
Fix up these functions (as well as a few other obvious functions that
should have it, even if there are no warnings currently) to resolve the
warnings and potential kCFI runtime failures.
Fixes: 31e4ef3280c8 ("bcachefs: iter/update/trigger/str_hash flag cleanup")
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2024-04-23 11:58:09 -07:00
|
|
|
struct bkey_s_c, struct bkey_s,
|
|
|
|
enum btree_iter_update_trigger_flags);
|
2023-08-16 16:54:33 -04:00
|
|
|
|
|
|
|
#define bch2_bkey_ops_snapshot ((struct bkey_ops) { \
|
2024-08-12 21:31:25 -04:00
|
|
|
.key_validate = bch2_snapshot_validate, \
|
2023-08-16 16:54:33 -04:00
|
|
|
.val_to_text = bch2_snapshot_to_text, \
|
2023-12-31 21:01:06 -05:00
|
|
|
.trigger = bch2_mark_snapshot, \
|
2023-08-16 16:54:33 -04:00
|
|
|
.min_val_size = 24, \
|
|
|
|
})
|
|
|
|
|
|
|
|
static inline struct snapshot_t *__snapshot_t(struct snapshot_table *t, u32 id)
|
|
|
|
{
|
2024-03-21 20:16:23 -04:00
|
|
|
u32 idx = U32_MAX - id;
|
|
|
|
|
|
|
|
return likely(t && idx < t->nr)
|
|
|
|
? &t->s[idx]
|
|
|
|
: NULL;
|
2023-08-16 16:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline const struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
|
|
|
|
{
|
|
|
|
return __snapshot_t(rcu_dereference(c->snapshots), id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 bch2_snapshot_tree(struct bch_fs *c, u32 id)
|
|
|
|
{
|
2025-05-24 16:33:39 -04:00
|
|
|
guard(rcu)();
|
2024-03-22 16:29:23 -04:00
|
|
|
const struct snapshot_t *s = snapshot_t(c, id);
|
2025-05-24 16:33:39 -04:00
|
|
|
return s ? s->tree : 0;
|
2023-08-16 16:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
|
|
|
|
{
|
2024-03-22 16:29:23 -04:00
|
|
|
const struct snapshot_t *s = snapshot_t(c, id);
|
|
|
|
return s ? s->parent : 0;
|
2023-08-16 16:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
|
|
|
|
{
|
2025-05-24 16:33:39 -04:00
|
|
|
guard(rcu)();
|
|
|
|
return __bch2_snapshot_parent_early(c, id);
|
2023-08-16 16:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 __bch2_snapshot_parent(struct bch_fs *c, u32 id)
|
|
|
|
{
|
2024-03-22 16:29:23 -04:00
|
|
|
const struct snapshot_t *s = snapshot_t(c, id);
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
2023-08-16 16:54:33 -04:00
|
|
|
|
2024-03-22 16:29:23 -04:00
|
|
|
u32 parent = s->parent;
|
2024-04-11 10:29:31 +02:00
|
|
|
if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
|
2024-03-22 16:29:23 -04:00
|
|
|
parent &&
|
|
|
|
s->depth != snapshot_t(c, parent)->depth + 1)
|
2023-08-16 16:54:33 -04:00
|
|
|
panic("id %u depth=%u parent %u depth=%u\n",
|
|
|
|
id, snapshot_t(c, id)->depth,
|
|
|
|
parent, snapshot_t(c, parent)->depth);
|
|
|
|
|
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
|
|
|
|
{
|
2025-05-24 16:33:39 -04:00
|
|
|
guard(rcu)();
|
|
|
|
return __bch2_snapshot_parent(c, id);
|
2023-08-16 16:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
|
|
|
|
{
|
2025-05-24 16:33:39 -04:00
|
|
|
guard(rcu)();
|
2023-08-16 16:54:33 -04:00
|
|
|
while (n--)
|
|
|
|
id = __bch2_snapshot_parent(c, id);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2025-05-19 09:48:50 -04:00
|
|
|
u32 bch2_snapshot_oldest_subvol(struct bch_fs *, u32, snapshot_id_list *);
|
2023-08-16 16:54:33 -04:00
|
|
|
u32 bch2_snapshot_skiplist_get(struct bch_fs *, u32);
|
|
|
|
|
|
|
|
static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
|
|
|
|
{
|
2025-05-24 16:33:39 -04:00
|
|
|
guard(rcu)();
|
2023-08-16 16:54:33 -04:00
|
|
|
|
2025-05-24 16:33:39 -04:00
|
|
|
u32 parent;
|
2023-08-16 16:54:33 -04:00
|
|
|
while ((parent = __bch2_snapshot_parent(c, id)))
|
|
|
|
id = parent;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2025-05-02 12:37:36 -04:00
|
|
|
static inline enum snapshot_id_state __bch2_snapshot_id_state(struct bch_fs *c, u32 id)
|
2024-12-09 01:31:43 -05:00
|
|
|
{
|
|
|
|
const struct snapshot_t *s = snapshot_t(c, id);
|
2025-05-02 12:37:36 -04:00
|
|
|
return s ? s->state : SNAPSHOT_ID_empty;
|
2024-12-09 01:31:43 -05:00
|
|
|
}
|
|
|
|
|
2025-05-02 12:37:36 -04:00
|
|
|
static inline enum snapshot_id_state bch2_snapshot_id_state(struct bch_fs *c, u32 id)
|
2024-12-09 01:31:43 -05:00
|
|
|
{
|
2025-05-24 16:33:39 -04:00
|
|
|
guard(rcu)();
|
|
|
|
return __bch2_snapshot_id_state(c, id);
|
2024-12-09 01:31:43 -05:00
|
|
|
}
|
|
|
|
|
2025-05-02 12:37:36 -04:00
|
|
|
static inline bool bch2_snapshot_exists(struct bch_fs *c, u32 id)
|
|
|
|
{
|
|
|
|
return bch2_snapshot_id_state(c, id) == SNAPSHOT_ID_live;
|
|
|
|
}
|
|
|
|
|
2024-03-22 16:29:23 -04:00
|
|
|
static inline int bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
|
2023-08-16 16:54:33 -04:00
|
|
|
{
|
2025-05-24 16:33:39 -04:00
|
|
|
guard(rcu)();
|
2024-03-22 16:29:23 -04:00
|
|
|
const struct snapshot_t *s = snapshot_t(c, id);
|
2025-05-24 16:33:39 -04:00
|
|
|
return s ? s->children[0] : -BCH_ERR_invalid_snapshot_node;
|
2023-08-16 16:54:33 -04:00
|
|
|
}
|
|
|
|
|
2024-03-22 16:29:23 -04:00
|
|
|
static inline int bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
|
2023-08-16 16:54:33 -04:00
|
|
|
{
|
2024-03-22 16:29:23 -04:00
|
|
|
int ret = bch2_snapshot_is_internal_node(c, id);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
return !ret;
|
2023-08-16 16:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 bch2_snapshot_depth(struct bch_fs *c, u32 parent)
|
|
|
|
{
|
2025-05-24 16:33:39 -04:00
|
|
|
guard(rcu)();
|
|
|
|
return parent ? snapshot_t(c, parent)->depth + 1 : 0;
|
2023-08-16 16:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool __bch2_snapshot_is_ancestor(struct bch_fs *, u32, u32);
|
|
|
|
|
|
|
|
static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
|
|
|
|
{
|
|
|
|
return id == ancestor
|
|
|
|
? true
|
|
|
|
: __bch2_snapshot_is_ancestor(c, id, ancestor);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
|
|
|
|
{
|
2025-05-24 16:33:39 -04:00
|
|
|
guard(rcu)();
|
2024-05-19 19:16:22 -04:00
|
|
|
const struct snapshot_t *t = snapshot_t(c, id);
|
2025-05-24 16:33:39 -04:00
|
|
|
return t && (t->children[0]|t->children[1]) != 0;
|
2023-08-16 16:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
|
|
|
|
{
|
2025-05-29 16:56:50 -04:00
|
|
|
return darray_find(*s, id) != NULL;
|
2023-08-16 16:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
|
|
|
|
{
|
|
|
|
darray_for_each(*s, i)
|
|
|
|
if (bch2_snapshot_is_ancestor(c, id, *i))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
|
|
|
|
{
|
|
|
|
BUG_ON(snapshot_list_has_id(s, id));
|
2024-03-27 22:50:19 -04:00
|
|
|
int ret = darray_push(s, id);
|
2023-08-16 16:54:33 -04:00
|
|
|
if (ret)
|
|
|
|
bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-03-27 22:50:19 -04:00
|
|
|
static inline int snapshot_list_add_nodup(struct bch_fs *c, snapshot_id_list *s, u32 id)
|
|
|
|
{
|
|
|
|
int ret = snapshot_list_has_id(s, id)
|
|
|
|
? 0
|
|
|
|
: darray_push(s, id);
|
|
|
|
if (ret)
|
|
|
|
bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int snapshot_list_merge(struct bch_fs *c, snapshot_id_list *dst, snapshot_id_list *src)
|
|
|
|
{
|
|
|
|
darray_for_each(*src, i) {
|
|
|
|
int ret = snapshot_list_add_nodup(c, dst, *i);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-16 16:54:33 -04:00
|
|
|
int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
|
|
|
|
struct bch_snapshot *s);
|
|
|
|
int bch2_snapshot_get_subvol(struct btree_trans *, u32,
|
|
|
|
struct bch_subvolume *);
|
|
|
|
|
|
|
|
/* only exported for tests: */
|
|
|
|
int bch2_snapshot_node_create(struct btree_trans *, u32,
|
|
|
|
u32 *, u32 *, unsigned);
|
|
|
|
|
|
|
|
int bch2_check_snapshot_trees(struct bch_fs *);
|
|
|
|
int bch2_check_snapshots(struct bch_fs *);
|
2024-03-27 22:50:19 -04:00
|
|
|
int bch2_reconstruct_snapshots(struct bch_fs *);
|
2025-05-02 12:37:36 -04:00
|
|
|
|
|
|
|
int __bch2_check_key_has_snapshot(struct btree_trans *, struct btree_iter *, struct bkey_s_c);
|
|
|
|
|
|
|
|
static inline int bch2_check_key_has_snapshot(struct btree_trans *trans,
|
|
|
|
struct btree_iter *iter,
|
|
|
|
struct bkey_s_c k)
|
|
|
|
{
|
|
|
|
return likely(bch2_snapshot_exists(trans->c, k.k->p.snapshot))
|
|
|
|
? 0
|
|
|
|
: __bch2_check_key_has_snapshot(trans, iter, k);
|
|
|
|
}
|
2023-08-16 16:54:33 -04:00
|
|
|
|
2025-05-28 15:08:19 -04:00
|
|
|
int __bch2_get_snapshot_overwrites(struct btree_trans *,
|
|
|
|
enum btree_id, struct bpos,
|
|
|
|
snapshot_id_list *);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a list of snapshot IDs that have overwritten a given key:
|
|
|
|
*/
|
|
|
|
static inline int bch2_get_snapshot_overwrites(struct btree_trans *trans,
|
|
|
|
enum btree_id btree, struct bpos pos,
|
|
|
|
snapshot_id_list *s)
|
|
|
|
{
|
|
|
|
darray_init(s);
|
|
|
|
|
|
|
|
return bch2_snapshot_has_children(trans->c, pos.snapshot)
|
|
|
|
? __bch2_get_snapshot_overwrites(trans, btree, pos, s)
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-16 16:54:33 -04:00
|
|
|
int bch2_snapshot_node_set_deleted(struct btree_trans *, u32);
|
|
|
|
|
2023-08-18 21:13:44 -04:00
|
|
|
int __bch2_key_has_snapshot_overwrites(struct btree_trans *, enum btree_id, struct bpos);
|
|
|
|
|
|
|
|
static inline int bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
|
|
|
|
enum btree_id id,
|
|
|
|
struct bpos pos)
|
|
|
|
{
|
|
|
|
if (!btree_type_has_snapshots(id) ||
|
2024-03-22 16:29:23 -04:00
|
|
|
bch2_snapshot_is_leaf(trans->c, pos.snapshot) > 0)
|
2023-08-18 21:13:44 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return __bch2_key_has_snapshot_overwrites(trans, id, pos);
|
|
|
|
}
|
|
|
|
|
2025-05-07 14:26:18 -04:00
|
|
|
int __bch2_delete_dead_snapshots(struct bch_fs *);
|
2025-05-01 14:47:39 -04:00
|
|
|
int bch2_delete_dead_snapshots(struct bch_fs *);
|
|
|
|
void bch2_delete_dead_snapshots_work(struct work_struct *);
|
|
|
|
void bch2_delete_dead_snapshots_async(struct bch_fs *);
|
|
|
|
void bch2_snapshot_delete_status_to_text(struct printbuf *, struct bch_fs *);
|
|
|
|
|
2023-08-16 16:54:33 -04:00
|
|
|
int bch2_snapshots_read(struct bch_fs *);
|
|
|
|
void bch2_fs_snapshots_exit(struct bch_fs *);
|
2025-05-01 14:47:39 -04:00
|
|
|
void bch2_fs_snapshots_init_early(struct bch_fs *);
|
2023-08-16 16:54:33 -04:00
|
|
|
|
|
|
|
#endif /* _BCACHEFS_SNAPSHOT_H */
|