linux-erofs.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
From: Chengguang Xu <cgxu519@mykernel.net>
To: xiang@kernel.org, chao@kernel.org
Cc: Chengguang Xu <cgxu519@mykernel.net>, linux-erofs@lists.ozlabs.org
Subject: [PATCH] erofs: add error handling for erofs_fill_super()
Date: Tue, 19 Nov 2019 19:37:44 +0800	[thread overview]
Message-ID: <20191119113744.11635-1-cgxu519@mykernel.net> (raw)

There are some potential resource leaks in error case
of erofs_fill_super(), so add proper error handling
for it.

Signed-off-by: Chengguang Xu <cgxu519@mykernel.net>
---
 fs/erofs/super.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/fs/erofs/super.c b/fs/erofs/super.c
index 0e369494f2f2..06e721bd1c8c 100644
--- a/fs/erofs/super.c
+++ b/fs/erofs/super.c
@@ -369,7 +369,7 @@ static int erofs_fill_super(struct super_block *sb, void *data, int silent)
 	sb->s_fs_info = sbi;
 	err = erofs_read_superblock(sb);
 	if (err)
-		return err;
+		goto free;
 
 	sb->s_flags |= SB_RDONLY | SB_NOATIME;
 	sb->s_maxbytes = MAX_LFS_FILESIZE;
@@ -385,7 +385,7 @@ static int erofs_fill_super(struct super_block *sb, void *data, int silent)
 
 	err = erofs_parse_options(sb, data);
 	if (err)
-		return err;
+		goto free;
 
 	if (test_opt(sbi, POSIX_ACL))
 		sb->s_flags |= SB_POSIXACL;
@@ -398,29 +398,44 @@ static int erofs_fill_super(struct super_block *sb, void *data, int silent)
 
 	/* get the root inode */
 	inode = erofs_iget(sb, ROOT_NID(sbi), true);
-	if (IS_ERR(inode))
-		return PTR_ERR(inode);
+	if (IS_ERR(inode)) {
+		err = PTR_ERR(inode);
+		goto free;
+	}
 
 	if (!S_ISDIR(inode->i_mode)) {
 		erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
 			  ROOT_NID(sbi), inode->i_mode);
 		iput(inode);
-		return -EINVAL;
+		err = -EINVAL;
+		goto free;
 	}
 
 	sb->s_root = d_make_root(inode);
-	if (!sb->s_root)
-		return -ENOMEM;
+	if (!sb->s_root) {
+		err = -ENOMEM;
+		goto free;
+	}
 
 	erofs_shrinker_register(sb);
 	/* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
 	err = erofs_init_managed_cache(sb);
 	if (err)
-		return err;
+		goto free_root_inode;
 
 	erofs_info(sb, "mounted with opts: %s, root inode @ nid %llu.",
 		   (char *)data, ROOT_NID(sbi));
+
 	return 0;
+
+free_root_inode:
+	dput(sb->s_root);
+	sb->s_root = NULL;
+	erofs_shrinker_unregister(sb);
+free:
+	kfree(sbi);
+	sb->s_fs_info = NULL;
+	return err;
 }
 
 static struct dentry *erofs_mount(struct file_system_type *fs_type, int flags,
-- 
2.20.1




             reply	other threads:[~2019-11-19 11:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-19 11:37 Chengguang Xu [this message]
2019-11-19 12:53 ` [PATCH] erofs: add error handling for erofs_fill_super() Gao Xiang
2019-11-20 10:08   ` Chengguang Xu
2019-11-20 10:08   ` Chengguang Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191119113744.11635-1-cgxu519@mykernel.net \
    --to=cgxu519@mykernel.net \
    --cc=chao@kernel.org \
    --cc=linux-erofs@lists.ozlabs.org \
    --cc=xiang@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).