init: refactor mount_root
authorChristoph Hellwig <hch@lst.de>
Wed, 31 May 2023 12:55:18 +0000 (14:55 +0200)
committerJens Axboe <axboe@kernel.dk>
Mon, 5 Jun 2023 16:55:20 +0000 (10:55 -0600)
Provide stubs for all the lower level mount helpers, and just switch
on ROOT_DEV in the main function.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20230531125535.676098-8-hch@lst.de
Signed-off-by: Jens Axboe <axboe@kernel.dk>
init/do_mounts.c

index a2c0baa..e708b02 100644 (file)
@@ -453,15 +453,14 @@ out:
 #define NFSROOT_TIMEOUT_MAX    30
 #define NFSROOT_RETRY_MAX      5
 
-static int __init mount_nfs_root(void)
+static void __init mount_nfs_root(void)
 {
        char *root_dev, *root_data;
        unsigned int timeout;
-       int try, err;
+       int try;
 
-       err = nfs_root_data(&root_dev, &root_data);
-       if (err != 0)
-               return 0;
+       if (nfs_root_data(&root_dev, &root_data))
+               goto fail;
 
        /*
         * The server or network may not be ready, so try several
@@ -470,10 +469,8 @@ static int __init mount_nfs_root(void)
         */
        timeout = NFSROOT_TIMEOUT_MIN;
        for (try = 1; ; try++) {
-               err = do_mount_root(root_dev, "nfs",
-                                       root_mountflags, root_data);
-               if (err == 0)
-                       return 1;
+               if (!do_mount_root(root_dev, "nfs", root_mountflags, root_data))
+                       return;
                if (try > NFSROOT_RETRY_MAX)
                        break;
 
@@ -483,9 +480,14 @@ static int __init mount_nfs_root(void)
                if (timeout > NFSROOT_TIMEOUT_MAX)
                        timeout = NFSROOT_TIMEOUT_MAX;
        }
-       return 0;
+fail:
+       pr_err("VFS: Unable to mount root fs via NFS.\n");
 }
-#endif
+#else
+static inline void mount_nfs_root(void)
+{
+}
+#endif /* CONFIG_ROOT_NFS */
 
 #ifdef CONFIG_CIFS_ROOT
 
@@ -495,22 +497,20 @@ extern int cifs_root_data(char **dev, char **opts);
 #define CIFSROOT_TIMEOUT_MAX   30
 #define CIFSROOT_RETRY_MAX     5
 
-static int __init mount_cifs_root(void)
+static void __init mount_cifs_root(void)
 {
        char *root_dev, *root_data;
        unsigned int timeout;
-       int try, err;
+       int try;
 
-       err = cifs_root_data(&root_dev, &root_data);
-       if (err != 0)
-               return 0;
+       if (cifs_root_data(&root_dev, &root_data))
+               goto fail;
 
        timeout = CIFSROOT_TIMEOUT_MIN;
        for (try = 1; ; try++) {
-               err = do_mount_root(root_dev, "cifs", root_mountflags,
-                                   root_data);
-               if (err == 0)
-                       return 1;
+               if (!do_mount_root(root_dev, "cifs", root_mountflags,
+                                  root_data))
+                       return;
                if (try > CIFSROOT_RETRY_MAX)
                        break;
 
@@ -519,9 +519,14 @@ static int __init mount_cifs_root(void)
                if (timeout > CIFSROOT_TIMEOUT_MAX)
                        timeout = CIFSROOT_TIMEOUT_MAX;
        }
-       return 0;
+fail:
+       pr_err("VFS: Unable to mount root fs via SMB.\n");
 }
-#endif
+#else
+static inline void mount_cifs_root(void)
+{
+}
+#endif /* CONFIG_CIFS_ROOT */
 
 static bool __init fs_is_nodev(char *fstype)
 {
@@ -563,35 +568,38 @@ static int __init mount_nodev_root(void)
        return err;
 }
 
-void __init mount_root(void)
-{
-#ifdef CONFIG_ROOT_NFS
-       if (ROOT_DEV == Root_NFS) {
-               if (!mount_nfs_root())
-                       printk(KERN_ERR "VFS: Unable to mount root fs via NFS.\n");
-               return;
-       }
-#endif
-#ifdef CONFIG_CIFS_ROOT
-       if (ROOT_DEV == Root_CIFS) {
-               if (!mount_cifs_root())
-                       printk(KERN_ERR "VFS: Unable to mount root fs via SMB.\n");
-               return;
-       }
-#endif
-       if (ROOT_DEV == 0 && root_device_name && root_fs_names) {
-               if (mount_nodev_root() == 0)
-                       return;
-       }
 #ifdef CONFIG_BLOCK
-       {
-               int err = create_dev("/dev/root", ROOT_DEV);
+static void __init mount_block_root(void)
+{
+       int err = create_dev("/dev/root", ROOT_DEV);
+
+       if (err < 0)
+               pr_emerg("Failed to create /dev/root: %d\n", err);
+       mount_root_generic("/dev/root", root_mountflags);
+}
+#else
+static inline void mount_block_root(void)
+{
+}
+#endif /* CONFIG_BLOCK */
 
-               if (err < 0)
-                       pr_emerg("Failed to create /dev/root: %d\n", err);
-               mount_root_generic("/dev/root", root_mountflags);
+void __init mount_root(void)
+{
+       switch (ROOT_DEV) {
+       case Root_NFS:
+               mount_nfs_root();
+               break;
+       case Root_CIFS:
+               mount_cifs_root();
+               break;
+       case 0:
+               if (root_device_name && root_fs_names && mount_nodev_root() == 0)
+                       break;
+               fallthrough;
+       default:
+               mount_block_root();
+               break;
        }
-#endif
 }
 
 /*