Skip to content

Commit

Permalink
jbd2: don't leak memory if setting up journal fails
Browse files Browse the repository at this point in the history
In journal_init_common(), if we failed to allocate the j_wbuf array, or
if we failed to create the buffer_head for the journal superblock, we
leaked the memory allocated for the revocation tables.  Fix this.

Cc: [email protected] # 4.9
Fixes: f0c9fd5
Signed-off-by: Eric Biggers <[email protected]>
Signed-off-by: Theodore Ts'o <[email protected]>
Reviewed-by: Jan Kara <[email protected]>
  • Loading branch information
ebiggers authored and tytso committed Mar 15, 2017
1 parent b9cf625 commit cd9cb40
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
22 changes: 11 additions & 11 deletions fs/jbd2/journal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1125,10 +1125,8 @@ static journal_t *journal_init_common(struct block_device *bdev,

/* Set up a default-sized revoke table for the new mount. */
err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
if (err) {
kfree(journal);
return NULL;
}
if (err)
goto err_cleanup;

spin_lock_init(&journal->j_history_lock);

Expand All @@ -1145,23 +1143,25 @@ static journal_t *journal_init_common(struct block_device *bdev,
journal->j_wbufsize = n;
journal->j_wbuf = kmalloc_array(n, sizeof(struct buffer_head *),
GFP_KERNEL);
if (!journal->j_wbuf) {
kfree(journal);
return NULL;
}
if (!journal->j_wbuf)
goto err_cleanup;

bh = getblk_unmovable(journal->j_dev, start, journal->j_blocksize);
if (!bh) {
pr_err("%s: Cannot get buffer for journal superblock\n",
__func__);
kfree(journal->j_wbuf);
kfree(journal);
return NULL;
goto err_cleanup;
}
journal->j_sb_buffer = bh;
journal->j_superblock = (journal_superblock_t *)bh->b_data;

return journal;

err_cleanup:
kfree(journal->j_wbuf);
jbd2_journal_destroy_revoke(journal);
kfree(journal);
return NULL;
}

/* jbd2_journal_init_dev and jbd2_journal_init_inode:
Expand Down
1 change: 1 addition & 0 deletions fs/jbd2/revoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ int jbd2_journal_init_revoke(journal_t *journal, int hash_size)

fail1:
jbd2_journal_destroy_revoke_table(journal->j_revoke_table[0]);
journal->j_revoke_table[0] = NULL;
fail0:
return -ENOMEM;
}
Expand Down

0 comments on commit cd9cb40

Please sign in to comment.