Commit 43cfbad6 authored by Justin Husted's avatar Justin Husted Committed by Kent Overstreet

bcachefs: Further padding fixes in bch2_journal_super_entries_add_common()

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: default avatarJustin Husted <sigstop@gmail.com>
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent e3728b50
...@@ -949,6 +949,25 @@ int bch2_fs_mark_dirty(struct bch_fs *c) ...@@ -949,6 +949,25 @@ int bch2_fs_mark_dirty(struct bch_fs *c)
return ret; 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 * struct jset_entry *
bch2_journal_super_entries_add_common(struct bch_fs *c, bch2_journal_super_entries_add_common(struct bch_fs *c,
struct jset_entry *entry, struct jset_entry *entry,
...@@ -963,7 +982,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c, ...@@ -963,7 +982,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c,
r < c->btree_roots + BTREE_ID_NR; r < c->btree_roots + BTREE_ID_NR;
r++) r++)
if (r->alive) { if (r->alive) {
entry->u64s = r->key.u64s; entry_init_u64s(entry, r->key.u64s + 1);
entry->btree_id = r - c->btree_roots; entry->btree_id = r - c->btree_roots;
entry->level = r->level; entry->level = r->level;
entry->type = BCH_JSET_ENTRY_btree_root; entry->type = BCH_JSET_ENTRY_btree_root;
...@@ -988,8 +1007,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c, ...@@ -988,8 +1007,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c,
struct jset_entry_usage *u = struct jset_entry_usage *u =
container_of(entry, struct jset_entry_usage, entry); container_of(entry, struct jset_entry_usage, entry);
memset(u, 0, sizeof(*u)); entry_init_size(entry, sizeof(*u));
u->entry.u64s = DIV_ROUND_UP(sizeof(*u), sizeof(u64)) - 1;
u->entry.type = BCH_JSET_ENTRY_usage; u->entry.type = BCH_JSET_ENTRY_usage;
u->entry.btree_id = FS_USAGE_INODES; u->entry.btree_id = FS_USAGE_INODES;
u->v = cpu_to_le64(c->usage_base->nr_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, ...@@ -1001,8 +1019,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c,
struct jset_entry_usage *u = struct jset_entry_usage *u =
container_of(entry, struct jset_entry_usage, entry); container_of(entry, struct jset_entry_usage, entry);
memset(u, 0, sizeof(*u)); entry_init_size(entry, sizeof(*u));
u->entry.u64s = DIV_ROUND_UP(sizeof(*u), sizeof(u64)) - 1;
u->entry.type = BCH_JSET_ENTRY_usage; u->entry.type = BCH_JSET_ENTRY_usage;
u->entry.btree_id = FS_USAGE_KEY_VERSION; u->entry.btree_id = FS_USAGE_KEY_VERSION;
u->v = cpu_to_le64(atomic64_read(&c->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, ...@@ -1014,8 +1031,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c,
struct jset_entry_usage *u = struct jset_entry_usage *u =
container_of(entry, struct jset_entry_usage, entry); container_of(entry, struct jset_entry_usage, entry);
memset(u, 0, sizeof(*u)); entry_init_size(entry, sizeof(*u));
u->entry.u64s = DIV_ROUND_UP(sizeof(*u), sizeof(u64)) - 1;
u->entry.type = BCH_JSET_ENTRY_usage; u->entry.type = BCH_JSET_ENTRY_usage;
u->entry.btree_id = FS_USAGE_RESERVED; u->entry.btree_id = FS_USAGE_RESERVED;
u->entry.level = i; u->entry.level = i;
...@@ -1030,10 +1046,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c, ...@@ -1030,10 +1046,7 @@ bch2_journal_super_entries_add_common(struct bch_fs *c,
struct jset_entry_data_usage *u = struct jset_entry_data_usage *u =
container_of(entry, struct jset_entry_data_usage, entry); container_of(entry, struct jset_entry_data_usage, entry);
int u64s = DIV_ROUND_UP(sizeof(*u) + e->nr_devs, entry_init_size(entry, sizeof(*u) + e->nr_devs);
sizeof(u64)) - 1;
memset(u, 0, u64s * sizeof(u64));
u->entry.u64s = u64s;
u->entry.type = BCH_JSET_ENTRY_data_usage; u->entry.type = BCH_JSET_ENTRY_data_usage;
u->v = cpu_to_le64(c->usage_base->replicas[i]); u->v = cpu_to_le64(c->usage_base->replicas[i]);
unsafe_memcpy(&u->r, e, replicas_entry_bytes(e), unsafe_memcpy(&u->r, e, replicas_entry_bytes(e),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment