diff options
author | Justin Husted <sigstop@gmail.com> | 2019-10-11 17:05:11 -0700 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2023-10-22 17:08:30 -0400 |
commit | 43cfbad6e431568d14e5d1407203c39a140f0148 (patch) | |
tree | 4c62005b2cad54875399eb0c040755017b8af595 /fs | |
parent | e3728b50034504e5e64604d72896973374cb1fa5 (diff) |
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: Justin Husted <sigstop@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'fs')
-rw-r--r-- | fs/bcachefs/super-io.c | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/fs/bcachefs/super-io.c b/fs/bcachefs/super-io.c index 14ff191ad702..6544bbf18e70 100644 --- a/fs/bcachefs/super-io.c +++ b/fs/bcachefs/super-io.c @@ -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), |