bcachefs: Assert that btree write buffer only touches the right btrees
authorKent Overstreet <kent.overstreet@linux.dev>
Tue, 24 Dec 2024 21:57:24 +0000 (16:57 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Fri, 10 Jan 2025 04:38:41 +0000 (23:38 -0500)
More asserts, more better.

Also, clean up the per-btree flags a bit.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/bcachefs_format.h
fs/bcachefs/btree_types.h
fs/bcachefs/btree_write_buffer.c

index b0fac8b7915bb2ffb54a885a1a80fc26e0bb3b98..0680930508a36ad14f34f63b1cf307e1d7684bf3 100644 (file)
@@ -1288,14 +1288,18 @@ LE32_BITMASK(JSET_NO_FLUSH,     struct jset, flags, 5, 6);
 /* Btree: */
 
 enum btree_id_flags {
-       BTREE_ID_EXTENTS        = BIT(0),
-       BTREE_ID_SNAPSHOTS      = BIT(1),
-       BTREE_ID_SNAPSHOT_FIELD = BIT(2),
-       BTREE_ID_DATA           = BIT(3),
+       BTREE_IS_extents        = BIT(0),
+       BTREE_IS_snapshots      = BIT(1),
+       BTREE_IS_snapshot_field = BIT(2),
+       BTREE_IS_data           = BIT(3),
+       BTREE_IS_write_buffer   = BIT(4),
 };
 
 #define BCH_BTREE_IDS()                                                                \
-       x(extents,              0,      BTREE_ID_EXTENTS|BTREE_ID_SNAPSHOTS|BTREE_ID_DATA,\
+       x(extents,              0,                                              \
+         BTREE_IS_extents|                                                     \
+         BTREE_IS_snapshots|                                                   \
+         BTREE_IS_data,                                                        \
          BIT_ULL(KEY_TYPE_whiteout)|                                           \
          BIT_ULL(KEY_TYPE_error)|                                              \
          BIT_ULL(KEY_TYPE_cookie)|                                             \
@@ -1303,17 +1307,20 @@ enum btree_id_flags {
          BIT_ULL(KEY_TYPE_reservation)|                                        \
          BIT_ULL(KEY_TYPE_reflink_p)|                                          \
          BIT_ULL(KEY_TYPE_inline_data))                                        \
-       x(inodes,               1,      BTREE_ID_SNAPSHOTS,                     \
+       x(inodes,               1,                                              \
+         BTREE_IS_snapshots,                                                   \
          BIT_ULL(KEY_TYPE_whiteout)|                                           \
          BIT_ULL(KEY_TYPE_inode)|                                              \
          BIT_ULL(KEY_TYPE_inode_v2)|                                           \
          BIT_ULL(KEY_TYPE_inode_v3)|                                           \
          BIT_ULL(KEY_TYPE_inode_generation))                                   \
-       x(dirents,              2,      BTREE_ID_SNAPSHOTS,                     \
+       x(dirents,              2,                                              \
+         BTREE_IS_snapshots,                                                   \
          BIT_ULL(KEY_TYPE_whiteout)|                                           \
          BIT_ULL(KEY_TYPE_hash_whiteout)|                                      \
          BIT_ULL(KEY_TYPE_dirent))                                             \
-       x(xattrs,               3,      BTREE_ID_SNAPSHOTS,                     \
+       x(xattrs,               3,                                              \
+         BTREE_IS_snapshots,                                                   \
          BIT_ULL(KEY_TYPE_whiteout)|                                           \
          BIT_ULL(KEY_TYPE_cookie)|                                             \
          BIT_ULL(KEY_TYPE_hash_whiteout)|                                      \
@@ -1327,7 +1334,9 @@ enum btree_id_flags {
          BIT_ULL(KEY_TYPE_quota))                                              \
        x(stripes,              6,      0,                                      \
          BIT_ULL(KEY_TYPE_stripe))                                             \
-       x(reflink,              7,      BTREE_ID_EXTENTS|BTREE_ID_DATA,         \
+       x(reflink,              7,                                              \
+         BTREE_IS_extents|                                                     \
+         BTREE_IS_data,                                                        \
          BIT_ULL(KEY_TYPE_reflink_v)|                                          \
          BIT_ULL(KEY_TYPE_indirect_inline_data)|                               \
          BIT_ULL(KEY_TYPE_error))                                              \
@@ -1335,29 +1344,38 @@ enum btree_id_flags {
          BIT_ULL(KEY_TYPE_subvolume))                                          \
        x(snapshots,            9,      0,                                      \
          BIT_ULL(KEY_TYPE_snapshot))                                           \
-       x(lru,                  10,     0,                                      \
+       x(lru,                  10,                                             \
+         BTREE_IS_write_buffer,                                                \
          BIT_ULL(KEY_TYPE_set))                                                \
-       x(freespace,            11,     BTREE_ID_EXTENTS,                       \
+       x(freespace,            11,                                             \
+         BTREE_IS_extents,                                                     \
          BIT_ULL(KEY_TYPE_set))                                                \
        x(need_discard,         12,     0,                                      \
          BIT_ULL(KEY_TYPE_set))                                                \
-       x(backpointers,         13,     0,                                      \
+       x(backpointers,         13,                                             \
+         BTREE_IS_write_buffer,                                                \
          BIT_ULL(KEY_TYPE_backpointer))                                        \
        x(bucket_gens,          14,     0,                                      \
          BIT_ULL(KEY_TYPE_bucket_gens))                                        \
        x(snapshot_trees,       15,     0,                                      \
          BIT_ULL(KEY_TYPE_snapshot_tree))                                      \
-       x(deleted_inodes,       16,     BTREE_ID_SNAPSHOT_FIELD,                \
+       x(deleted_inodes,       16,                                             \
+         BTREE_IS_snapshot_field|                                              \
+         BTREE_IS_write_buffer,                                                \
          BIT_ULL(KEY_TYPE_set))                                                \
        x(logged_ops,           17,     0,                                      \
          BIT_ULL(KEY_TYPE_logged_op_truncate)|                                 \
          BIT_ULL(KEY_TYPE_logged_op_finsert)|                                  \
          BIT_ULL(KEY_TYPE_inode_alloc_cursor))                                 \
-       x(rebalance_work,       18,     BTREE_ID_SNAPSHOT_FIELD,                \
+       x(rebalance_work,       18,                                             \
+         BTREE_IS_snapshot_field|                                              \
+         BTREE_IS_write_buffer,                                                \
          BIT_ULL(KEY_TYPE_set)|BIT_ULL(KEY_TYPE_cookie))                       \
        x(subvolume_children,   19,     0,                                      \
          BIT_ULL(KEY_TYPE_set))                                                \
-       x(accounting,           20,     BTREE_ID_SNAPSHOT_FIELD,                \
+       x(accounting,           20,                                             \
+         BTREE_IS_snapshot_field|                                              \
+         BTREE_IS_write_buffer,                                                \
          BIT_ULL(KEY_TYPE_accounting))                                         \
 
 enum btree_id {
index baab5288ecc9bf67eb44980218fcbcefda9dde75..a6f251eb416469f429546229de4e37150e4ada8c 100644 (file)
@@ -790,53 +790,64 @@ static inline bool btree_node_type_has_triggers(enum btree_node_type type)
        return BIT_ULL(type) & BTREE_NODE_TYPE_HAS_TRIGGERS;
 }
 
-static inline bool btree_node_type_is_extents(enum btree_node_type type)
+static inline bool btree_id_is_extents(enum btree_id btree)
 {
        const u64 mask = 0
-#define x(name, nr, flags, ...)        |((!!((flags) & BTREE_ID_EXTENTS)) << (nr + 1))
+#define x(name, nr, flags, ...)        |((!!((flags) & BTREE_IS_extents)) << nr)
        BCH_BTREE_IDS()
 #undef x
        ;
 
-       return BIT_ULL(type) & mask;
+       return BIT_ULL(btree) & mask;
 }
 
-static inline bool btree_id_is_extents(enum btree_id btree)
+static inline bool btree_node_type_is_extents(enum btree_node_type type)
+{
+       return type != BKEY_TYPE_btree && btree_id_is_extents(type - 1);
+}
+
+static inline bool btree_type_has_snapshots(enum btree_id btree)
 {
-       return btree_node_type_is_extents(__btree_node_type(0, btree));
+       const u64 mask = 0
+#define x(name, nr, flags, ...)        |((!!((flags) & BTREE_IS_snapshots)) << nr)
+       BCH_BTREE_IDS()
+#undef x
+       ;
+
+       return BIT_ULL(btree) & mask;
 }
 
-static inline bool btree_type_has_snapshots(enum btree_id id)
+static inline bool btree_type_has_snapshot_field(enum btree_id btree)
 {
        const u64 mask = 0
-#define x(name, nr, flags, ...)        |((!!((flags) & BTREE_ID_SNAPSHOTS)) << nr)
+#define x(name, nr, flags, ...)        |((!!((flags) & (BTREE_IS_snapshot_field|BTREE_IS_snapshots))) << nr)
        BCH_BTREE_IDS()
 #undef x
        ;
 
-       return BIT_ULL(id) & mask;
+       return BIT_ULL(btree) & mask;
 }
 
-static inline bool btree_type_has_snapshot_field(enum btree_id id)
+static inline bool btree_type_has_ptrs(enum btree_id btree)
 {
        const u64 mask = 0
-#define x(name, nr, flags, ...)        |((!!((flags) & (BTREE_ID_SNAPSHOT_FIELD|BTREE_ID_SNAPSHOTS))) << nr)
+#define x(name, nr, flags, ...)        |((!!((flags) & BTREE_IS_data)) << nr)
        BCH_BTREE_IDS()
 #undef x
        ;
 
-       return BIT_ULL(id) & mask;
+       return BIT_ULL(btree) & mask;
 }
 
-static inline bool btree_type_has_ptrs(enum btree_id id)
+static inline bool btree_type_uses_write_buffer(enum btree_id btree)
 {
        const u64 mask = 0
-#define x(name, nr, flags, ...)        |((!!((flags) & BTREE_ID_DATA)) << nr)
+#define x(name, nr, flags, ...)        |((!!((flags) & BTREE_IS_write_buffer)) << nr)
        BCH_BTREE_IDS()
 #undef x
        ;
 
-       return BIT_ULL(id) & mask;
+       return BIT_ULL(btree) & mask;
 }
 
 struct btree_root {
index 746db6d5a0fbae3c93974b96572a38c65cac80a0..b56c4987b8c97ac261712083fad4079037768512 100644 (file)
@@ -312,6 +312,8 @@ static int bch2_btree_write_buffer_flush_locked(struct btree_trans *trans)
        darray_for_each(wb->sorted, i) {
                struct btree_write_buffered_key *k = &wb->flushing.keys.data[i->idx];
 
+               BUG_ON(!btree_type_uses_write_buffer(k->btree));
+
                for (struct wb_key_ref *n = i + 1; n < min(i + 4, &darray_top(wb->sorted)); n++)
                        prefetch(&wb->flushing.keys.data[n->idx]);