2023-08-16 20:54:33 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _BCACHEFS_SNAPSHOT_H
|
|
|
|
#define _BCACHEFS_SNAPSHOT_H
|
|
|
|
|
|
|
|
enum bkey_invalid_flags;
|
|
|
|
|
|
|
|
void bch2_snapshot_tree_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
|
2023-10-25 00:44:36 +00:00
|
|
|
int bch2_snapshot_tree_invalid(struct bch_fs *, struct bkey_s_c,
|
2023-08-16 20:54:33 +00:00
|
|
|
enum bkey_invalid_flags, struct printbuf *);
|
|
|
|
|
|
|
|
#define bch2_bkey_ops_snapshot_tree ((struct bkey_ops) { \
|
|
|
|
.key_invalid = bch2_snapshot_tree_invalid, \
|
|
|
|
.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);
|
2023-10-25 00:44:36 +00:00
|
|
|
int bch2_snapshot_invalid(struct bch_fs *, struct bkey_s_c,
|
2023-08-16 20:54:33 +00:00
|
|
|
enum bkey_invalid_flags, struct printbuf *);
|
|
|
|
int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
|
2023-12-28 04:19:09 +00:00
|
|
|
struct bkey_s_c, struct bkey_s, unsigned);
|
2023-08-16 20:54:33 +00:00
|
|
|
|
|
|
|
#define bch2_bkey_ops_snapshot ((struct bkey_ops) { \
|
|
|
|
.key_invalid = bch2_snapshot_invalid, \
|
|
|
|
.val_to_text = bch2_snapshot_to_text, \
|
2024-01-01 02:01:06 +00:00
|
|
|
.trigger = bch2_mark_snapshot, \
|
2023-08-16 20:54:33 +00:00
|
|
|
.min_val_size = 24, \
|
|
|
|
})
|
|
|
|
|
|
|
|
static inline struct snapshot_t *__snapshot_t(struct snapshot_table *t, u32 id)
|
|
|
|
{
|
2024-03-22 00:16:23 +00:00
|
|
|
u32 idx = U32_MAX - id;
|
|
|
|
|
|
|
|
return likely(t && idx < t->nr)
|
|
|
|
? &t->s[idx]
|
|
|
|
: NULL;
|
2023-08-16 20:54:33 +00: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)
|
|
|
|
{
|
|
|
|
rcu_read_lock();
|
2024-03-22 20:29:23 +00:00
|
|
|
const struct snapshot_t *s = snapshot_t(c, id);
|
|
|
|
id = s ? s->tree : 0;
|
2023-08-16 20:54:33 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
|
|
|
|
{
|
2024-03-22 20:29:23 +00:00
|
|
|
const struct snapshot_t *s = snapshot_t(c, id);
|
|
|
|
return s ? s->parent : 0;
|
2023-08-16 20:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
|
|
|
|
{
|
|
|
|
rcu_read_lock();
|
|
|
|
id = __bch2_snapshot_parent_early(c, id);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 __bch2_snapshot_parent(struct bch_fs *c, u32 id)
|
|
|
|
{
|
2024-03-22 20:29:23 +00:00
|
|
|
const struct snapshot_t *s = snapshot_t(c, id);
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
2023-08-16 20:54:33 +00:00
|
|
|
|
2024-03-22 20:29:23 +00:00
|
|
|
u32 parent = s->parent;
|
|
|
|
if (IS_ENABLED(CONFIG_BCACHEFS_DEBU) &&
|
|
|
|
parent &&
|
|
|
|
s->depth != snapshot_t(c, parent)->depth + 1)
|
2023-08-16 20:54:33 +00: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)
|
|
|
|
{
|
|
|
|
rcu_read_lock();
|
|
|
|
id = __bch2_snapshot_parent(c, id);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
|
|
|
|
{
|
|
|
|
rcu_read_lock();
|
|
|
|
while (n--)
|
|
|
|
id = __bch2_snapshot_parent(c, id);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 bch2_snapshot_skiplist_get(struct bch_fs *, u32);
|
|
|
|
|
|
|
|
static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
|
|
|
|
{
|
|
|
|
u32 parent;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
while ((parent = __bch2_snapshot_parent(c, id)))
|
|
|
|
id = parent;
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 __bch2_snapshot_equiv(struct bch_fs *c, u32 id)
|
|
|
|
{
|
2024-03-22 20:29:23 +00:00
|
|
|
const struct snapshot_t *s = snapshot_t(c, id);
|
|
|
|
return s ? s->equiv : 0;
|
2023-08-16 20:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
|
|
|
|
{
|
|
|
|
rcu_read_lock();
|
|
|
|
id = __bch2_snapshot_equiv(c, id);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
|
|
|
|
{
|
|
|
|
return id == bch2_snapshot_equiv(c, id);
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:29:23 +00:00
|
|
|
static inline int bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
|
2023-08-16 20:54:33 +00:00
|
|
|
{
|
|
|
|
rcu_read_lock();
|
2024-03-22 20:29:23 +00:00
|
|
|
const struct snapshot_t *s = snapshot_t(c, id);
|
|
|
|
int ret = s ? s->children[0] : -BCH_ERR_invalid_snapshot_node;
|
2023-08-16 20:54:33 +00:00
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-03-22 20:29:23 +00:00
|
|
|
static inline int bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
|
2023-08-16 20:54:33 +00:00
|
|
|
{
|
2024-03-22 20:29:23 +00:00
|
|
|
int ret = bch2_snapshot_is_internal_node(c, id);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
return !ret;
|
2023-08-16 20:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline u32 bch2_snapshot_depth(struct bch_fs *c, u32 parent)
|
|
|
|
{
|
|
|
|
u32 depth;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
depth = parent ? snapshot_t(c, parent)->depth + 1 : 0;
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
const struct snapshot_t *t;
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
t = snapshot_t(c, id);
|
|
|
|
ret = (t->children[0]|t->children[1]) != 0;
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
|
|
|
|
{
|
|
|
|
darray_for_each(*s, i)
|
|
|
|
if (*i == id)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
BUG_ON(snapshot_list_has_id(s, id));
|
|
|
|
ret = darray_push(s, id);
|
|
|
|
if (ret)
|
|
|
|
bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 *);
|
|
|
|
|
|
|
|
int bch2_snapshot_node_set_deleted(struct btree_trans *, u32);
|
|
|
|
void bch2_delete_dead_snapshots_work(struct work_struct *);
|
|
|
|
|
2023-08-19 01:13:44 +00: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 20:29:23 +00:00
|
|
|
bch2_snapshot_is_leaf(trans->c, pos.snapshot) > 0)
|
2023-08-19 01:13:44 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return __bch2_key_has_snapshot_overwrites(trans, id, pos);
|
|
|
|
}
|
|
|
|
|
bcachefs: bch2_propagate_key_to_snapshot_leaves()
If fsck finds a key that needs work done, the primary example being an
unlinked inode that needs to be deleted, and the key is in an internal
snapshot node, we have a bit of a conundrum.
The conundrum is that internal snapshot nodes are shared, and we in
general do updates in internal snapshot nodes because there may be
overwrites in some snapshots and not others, and this may affect other
keys referenced by this key (i.e. extents).
For example, we might be seeing an unlinked inode in an internal
snapshot node, but then in one child snapshot the inode might have been
reattached and might not be unlinked. Deleting the inode in the internal
snapshot node would be wrong, because then we'll delete all the extents
that the child snapshot references.
But if an unlinked inode does not have any overwrites in child
snapshots, we're fine: the inode is overwrritten in all child snapshots,
so we can do the deletion at the point of comonality in the snapshot
tree, i.e. the node where we found it.
This patch adds a new helper, bch2_propagate_key_to_snapshot_leaves(),
to handle the case where we need a to update a key that does have
overwrites in child snapshots: we copy the key to leaf snapshot nodes,
and then rewind fsck and process the needed updates there.
With this, fsck can now always correctly handle unlinked inodes found in
internal snapshot nodes.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
2023-08-19 01:14:33 +00:00
|
|
|
int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *, enum btree_id,
|
|
|
|
struct bkey_s_c, struct bpos *);
|
|
|
|
|
2023-08-16 20:54:33 +00:00
|
|
|
int bch2_snapshots_read(struct bch_fs *);
|
|
|
|
void bch2_fs_snapshots_exit(struct bch_fs *);
|
|
|
|
|
|
|
|
#endif /* _BCACHEFS_SNAPSHOT_H */
|