diff options
author | Justin Husted <sigstop@gmail.com> | 2019-10-11 17:05:11 -0700 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@gmail.com> | 2019-11-01 19:02:10 -0400 |
commit | af2c6479fecfaf7b335ad2411f5519c2ca026ac8 (patch) | |
tree | 0e29da92a6791974ecd39cca2f85eadec7ae93f2 | |
parent | 9a1e7db5fae6e4eef6b9de041e2da2aa3cd38db6 (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>
-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 550a140df2a1..b36cfdf0b41c 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]); memcpy(&u->r, e, replicas_entry_bytes(e)); |