bcachefs: Further padding fixes in bch2_journal_super_entries_add_common()
authorJustin Husted <sigstop@gmail.com>
Sat, 12 Oct 2019 00:05:11 +0000 (17:05 -0700)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:08:30 +0000 (17:08 -0400)
The previous patch 128cb1a to fix uninitialized data was incorrect and
did not initialize the padding space correctly. Furthermore, several
other cases in this function do not initialize their padding space
correctly.

Move initialization into some helper functions in a more robust way.

Signed-off-by: Justin Husted <sigstop@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/super-io.c

index 14ff191ad702402149985768c13783010fd1ff05..6544bbf18e704e6cda39e902b163793864b04ecc 100644 (file)
@@ -949,6 +949,25 @@ int bch2_fs_mark_dirty(struct bch_fs *c)
        return ret;
 }
 
+static void
+entry_init_u64s(struct jset_entry *entry, unsigned u64s)
+{
+       memset(entry, 0, u64s * sizeof(u64));
+
+       /*
+        * The u64s field counts from the start of data, ignoring the shared
+        * fields.
+        */
+       entry->u64s = u64s - 1;
+}
+
+static void
+entry_init_size(struct jset_entry *entry, size_t size)
+{
+       unsigned u64s = DIV_ROUND_UP(size, sizeof(u64));
+       entry_init_u64s(entry, u64s);
+}
+
 struct jset_entry *
 bch2_journal_super_entries_add_common(struct bch_fs *c,
                                      struct jset_entry *entry,
@@ -963,7 +982,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c,
             r < c->btree_roots + BTREE_ID_NR;
             r++)
                if (r->alive) {
-                       entry->u64s     = r->key.u64s;
+                       entry_init_u64s(entry, r->key.u64s + 1);
                        entry->btree_id = r - c->btree_roots;
                        entry->level    = r->level;
                        entry->type     = BCH_JSET_ENTRY_btree_root;
@@ -988,8 +1007,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c,
                struct jset_entry_usage *u =
                        container_of(entry, struct jset_entry_usage, entry);
 
-               memset(u, 0, sizeof(*u));
-               u->entry.u64s   = DIV_ROUND_UP(sizeof(*u), sizeof(u64)) - 1;
+               entry_init_size(entry, sizeof(*u));
                u->entry.type   = BCH_JSET_ENTRY_usage;
                u->entry.btree_id = FS_USAGE_INODES;
                u->v            = cpu_to_le64(c->usage_base->nr_inodes);
@@ -1001,8 +1019,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c,
                struct jset_entry_usage *u =
                        container_of(entry, struct jset_entry_usage, entry);
 
-               memset(u, 0, sizeof(*u));
-               u->entry.u64s   = DIV_ROUND_UP(sizeof(*u), sizeof(u64)) - 1;
+               entry_init_size(entry, sizeof(*u));
                u->entry.type   = BCH_JSET_ENTRY_usage;
                u->entry.btree_id = FS_USAGE_KEY_VERSION;
                u->v            = cpu_to_le64(atomic64_read(&c->key_version));
@@ -1014,8 +1031,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c,
                struct jset_entry_usage *u =
                        container_of(entry, struct jset_entry_usage, entry);
 
-               memset(u, 0, sizeof(*u));
-               u->entry.u64s   = DIV_ROUND_UP(sizeof(*u), sizeof(u64)) - 1;
+               entry_init_size(entry, sizeof(*u));
                u->entry.type   = BCH_JSET_ENTRY_usage;
                u->entry.btree_id = FS_USAGE_RESERVED;
                u->entry.level  = i;
@@ -1030,10 +1046,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c,
                struct jset_entry_data_usage *u =
                        container_of(entry, struct jset_entry_data_usage, entry);
 
-               int u64s = DIV_ROUND_UP(sizeof(*u) + e->nr_devs,
-                                       sizeof(u64)) - 1;
-               memset(u, 0, u64s * sizeof(u64));
-               u->entry.u64s   = u64s;
+               entry_init_size(entry, sizeof(*u) + e->nr_devs);
                u->entry.type   = BCH_JSET_ENTRY_data_usage;
                u->v            = cpu_to_le64(c->usage_base->replicas[i]);
                unsafe_memcpy(&u->r, e, replicas_entry_bytes(e),