mount: rebuild error handling in do_new_mount
Commit Message
When a function execution error is detected in do_new_mount, it should
return immediately. Using this can make the code easier to understand.
Signed-off-by: Chuang Wang <nashuiliang@gmail.com>
---
fs/namespace.c | 53 ++++++++++++++++++++++++++++++++------------------
1 file changed, 34 insertions(+), 19 deletions(-)
Comments
On Sun, Dec 04, 2022 at 11:00:05PM +0800, Chuang Wang wrote:
> When a function execution error is detected in do_new_mount, it should
> return immediately. Using this can make the code easier to understand.
Your piles of goto make it harder to follow and reason about.
NAKed-by: Al Viro <viro@zeniv.linux.org.uk>
@@ -3116,36 +3116,51 @@ static int do_new_mount(struct path *path, const char *fstype, int sb_flags,
if (!type)
return -ENODEV;
+ fc = fs_context_for_mount(type, sb_flags);
+ put_filesystem(type);
+ if (IS_ERR(fc))
+ return PTR_ERR(fc);
+
+ /* subtype */
if (type->fs_flags & FS_HAS_SUBTYPE) {
subtype = strchr(fstype, '.');
if (subtype) {
subtype++;
if (!*subtype) {
- put_filesystem(type);
- return -EINVAL;
+ err = -EINVAL;
+ goto err_fc;
}
+
+ err = vfs_parse_fs_string(fc, "subtype",
+ subtype, strlen(subtype));
+ if (err)
+ goto err_fc;
}
}
- fc = fs_context_for_mount(type, sb_flags);
- put_filesystem(type);
- if (IS_ERR(fc))
- return PTR_ERR(fc);
-
- if (subtype)
- err = vfs_parse_fs_string(fc, "subtype",
- subtype, strlen(subtype));
- if (!err && name)
+ /* source */
+ if (name) {
err = vfs_parse_fs_string(fc, "source", name, strlen(name));
- if (!err)
- err = parse_monolithic_mount_data(fc, data);
- if (!err && !mount_capable(fc))
- err = -EPERM;
- if (!err)
- err = vfs_get_tree(fc);
- if (!err)
- err = do_new_mount_fc(fc, path, mnt_flags);
+ if (err)
+ goto err_fc;
+ }
+
+ /* monolithic data */
+ err = parse_monolithic_mount_data(fc, data);
+ if (err)
+ goto err_fc;
+
+ err = -EPERM;
+ if (!mount_capable(fc))
+ goto err_fc;
+
+ err = vfs_get_tree(fc);
+ if (err)
+ goto err_fc;
+
+ err = do_new_mount_fc(fc, path, mnt_flags);
+err_fc:
put_fs_context(fc);
return err;
}