]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/subvolume.h
Update bcachefs sources to 717b356d1d bcachefs: Convert journal validation to bkey_in...
[bcachefs-tools-debian] / libbcachefs / subvolume.h
index c694c1c24483beeebb6f5534be511a21d7a3ba3e..6905e91a947087fabdcce1525ade64d5cd4ef684 100644 (file)
 #include "darray.h"
 #include "subvolume_types.h"
 
+enum bkey_invalid_flags;
+
+void bch2_snapshot_tree_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
+int bch2_snapshot_tree_invalid(const struct bch_fs *, struct bkey_s_c,
+                              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,                                    \
+})
+
+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);
 int bch2_snapshot_invalid(const struct bch_fs *, struct bkey_s_c,
-                         int rw, struct printbuf *);
+                         enum bkey_invalid_flags, struct printbuf *);
+int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
+                      struct bkey_s_c, struct bkey_s_c, unsigned);
 
 #define bch2_bkey_ops_snapshot ((struct bkey_ops) {            \
        .key_invalid    = bch2_snapshot_invalid,                \
        .val_to_text    = bch2_snapshot_to_text,                \
+       .atomic_trigger = bch2_mark_snapshot,                   \
+       .min_val_size   = 24,                                   \
 })
 
-int bch2_mark_snapshot(struct btree_trans *, struct bkey_s_c,
-                      struct bkey_s_c, unsigned);
+static inline struct snapshot_t *__snapshot_t(struct snapshot_table *t, u32 id)
+{
+       return &t->s[U32_MAX - id];
+}
+
+static inline const struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
+{
+       return __snapshot_t(rcu_dereference(c->snapshots), id);
+}
 
-static inline struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
+static inline u32 bch2_snapshot_tree(struct bch_fs *c, u32 id)
 {
-       return genradix_ptr(&c->snapshots, U32_MAX - id);
+       rcu_read_lock();
+       id = snapshot_t(c, id)->tree;
+       rcu_read_unlock();
+
+       return id;
 }
 
-static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
+static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
 {
        return snapshot_t(c, id)->parent;
 }
 
-static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
+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)
+{
+#ifdef CONFIG_BCACHEFS_DEBUG
+       u32 parent = snapshot_t(c, id)->parent;
+
+       if (parent &&
+           snapshot_t(c, id)->depth != snapshot_t(c, parent)->depth + 1)
+               panic("id %u depth=%u parent %u depth=%u\n",
+                     id, snapshot_t(c, id)->depth,
+                     parent, snapshot_t(c, parent)->depth);
+
+       return parent;
+#else
+       return snapshot_t(c, id)->parent;
+#endif
+}
+
+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;
+}
+
+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)
 {
        return snapshot_t(c, id)->equiv;
 }
 
+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 == snapshot_t(c, id)->equiv;
+       return id == bch2_snapshot_equiv(c, id);
 }
 
-static inline u32 bch2_snapshot_internal_node(struct bch_fs *c, u32 id)
+static inline bool bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
 {
-       struct snapshot_t *s = snapshot_t(c, id);
+       const struct snapshot_t *s;
+       bool ret;
 
-       return s->children[0] || s->children[1];
+       rcu_read_lock();
+       s = snapshot_t(c, id);
+       ret = s->children[0];
+       rcu_read_unlock();
+
+       return ret;
+}
+
+static inline u32 bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
+{
+       return !bch2_snapshot_is_internal_node(c, id);
 }
 
 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
 {
-       struct snapshot_t *s;
-       u32 parent = bch2_snapshot_parent(c, id);
+       const struct snapshot_t *s;
+       u32 parent = __bch2_snapshot_parent(c, id);
 
        if (!parent)
                return 0;
 
-       s = snapshot_t(c, bch2_snapshot_parent(c, id));
+       s = snapshot_t(c, __bch2_snapshot_parent(c, id));
        if (id == s->children[0])
                return s->children[1];
        if (id == s->children[1])
@@ -60,12 +166,26 @@ static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
        return 0;
 }
 
+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)
 {
-       while (id && id < ancestor)
-               id = bch2_snapshot_parent(c, id);
+       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 id == ancestor;
+       return ret;
 }
 
 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
@@ -99,19 +219,21 @@ static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 i
        return ret;
 }
 
-int bch2_fs_check_snapshots(struct bch_fs *);
-int bch2_fs_check_subvols(struct bch_fs *);
+int bch2_check_snapshot_trees(struct bch_fs *);
+int bch2_check_snapshots(struct bch_fs *);
+int bch2_check_subvols(struct bch_fs *);
 
 void bch2_fs_snapshots_exit(struct bch_fs *);
-int bch2_fs_snapshots_start(struct bch_fs *);
+int bch2_snapshots_read(struct bch_fs *);
 
 int bch2_subvolume_invalid(const struct bch_fs *, struct bkey_s_c,
-                          int rw, struct printbuf *);
+                          unsigned, struct printbuf *);
 void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
 
 #define bch2_bkey_ops_subvolume ((struct bkey_ops) {           \
        .key_invalid    = bch2_subvolume_invalid,               \
        .val_to_text    = bch2_subvolume_to_text,               \
+       .min_val_size   = 16,                                   \
 })
 
 int bch2_subvolume_get(struct btree_trans *, unsigned,
@@ -127,7 +249,6 @@ int bch2_snapshot_node_create(struct btree_trans *, u32,
 int bch2_delete_dead_snapshots(struct bch_fs *);
 void bch2_delete_dead_snapshots_async(struct bch_fs *);
 
-int bch2_subvolume_delete(struct btree_trans *, u32);
 int bch2_subvolume_unlink(struct btree_trans *, u32);
 int bch2_subvolume_create(struct btree_trans *, u64, u32,
                          u32 *, u32 *, bool);