Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/module.h>
0003 #include <linux/sched.h>
0004 #include <linux/ctype.h>
0005 #include <linux/fd.h>
0006 #include <linux/tty.h>
0007 #include <linux/suspend.h>
0008 #include <linux/root_dev.h>
0009 #include <linux/security.h>
0010 #include <linux/delay.h>
0011 #include <linux/mount.h>
0012 #include <linux/device.h>
0013 #include <linux/init.h>
0014 #include <linux/fs.h>
0015 #include <linux/initrd.h>
0016 #include <linux/async.h>
0017 #include <linux/fs_struct.h>
0018 #include <linux/slab.h>
0019 #include <linux/ramfs.h>
0020 #include <linux/shmem_fs.h>
0021 
0022 #include <linux/nfs_fs.h>
0023 #include <linux/nfs_fs_sb.h>
0024 #include <linux/nfs_mount.h>
0025 #include <linux/raid/detect.h>
0026 #include <uapi/linux/mount.h>
0027 
0028 #include "do_mounts.h"
0029 
0030 int root_mountflags = MS_RDONLY | MS_SILENT;
0031 static char * __initdata root_device_name;
0032 static char __initdata saved_root_name[64];
0033 static int root_wait;
0034 
0035 dev_t ROOT_DEV;
0036 
0037 static int __init load_ramdisk(char *str)
0038 {
0039     pr_warn("ignoring the deprecated load_ramdisk= option\n");
0040     return 1;
0041 }
0042 __setup("load_ramdisk=", load_ramdisk);
0043 
0044 static int __init readonly(char *str)
0045 {
0046     if (*str)
0047         return 0;
0048     root_mountflags |= MS_RDONLY;
0049     return 1;
0050 }
0051 
0052 static int __init readwrite(char *str)
0053 {
0054     if (*str)
0055         return 0;
0056     root_mountflags &= ~MS_RDONLY;
0057     return 1;
0058 }
0059 
0060 __setup("ro", readonly);
0061 __setup("rw", readwrite);
0062 
0063 #ifdef CONFIG_BLOCK
0064 struct uuidcmp {
0065     const char *uuid;
0066     int len;
0067 };
0068 
0069 /**
0070  * match_dev_by_uuid - callback for finding a partition using its uuid
0071  * @dev:    device passed in by the caller
0072  * @data:   opaque pointer to the desired struct uuidcmp to match
0073  *
0074  * Returns 1 if the device matches, and 0 otherwise.
0075  */
0076 static int match_dev_by_uuid(struct device *dev, const void *data)
0077 {
0078     struct block_device *bdev = dev_to_bdev(dev);
0079     const struct uuidcmp *cmp = data;
0080 
0081     if (!bdev->bd_meta_info ||
0082         strncasecmp(cmp->uuid, bdev->bd_meta_info->uuid, cmp->len))
0083         return 0;
0084     return 1;
0085 }
0086 
0087 /**
0088  * devt_from_partuuid - looks up the dev_t of a partition by its UUID
0089  * @uuid_str:   char array containing ascii UUID
0090  *
0091  * The function will return the first partition which contains a matching
0092  * UUID value in its partition_meta_info struct.  This does not search
0093  * by filesystem UUIDs.
0094  *
0095  * If @uuid_str is followed by a "/PARTNROFF=%d", then the number will be
0096  * extracted and used as an offset from the partition identified by the UUID.
0097  *
0098  * Returns the matching dev_t on success or 0 on failure.
0099  */
0100 static dev_t devt_from_partuuid(const char *uuid_str)
0101 {
0102     struct uuidcmp cmp;
0103     struct device *dev = NULL;
0104     dev_t devt = 0;
0105     int offset = 0;
0106     char *slash;
0107 
0108     cmp.uuid = uuid_str;
0109 
0110     slash = strchr(uuid_str, '/');
0111     /* Check for optional partition number offset attributes. */
0112     if (slash) {
0113         char c = 0;
0114 
0115         /* Explicitly fail on poor PARTUUID syntax. */
0116         if (sscanf(slash + 1, "PARTNROFF=%d%c", &offset, &c) != 1)
0117             goto clear_root_wait;
0118         cmp.len = slash - uuid_str;
0119     } else {
0120         cmp.len = strlen(uuid_str);
0121     }
0122 
0123     if (!cmp.len)
0124         goto clear_root_wait;
0125 
0126     dev = class_find_device(&block_class, NULL, &cmp, &match_dev_by_uuid);
0127     if (!dev)
0128         return 0;
0129 
0130     if (offset) {
0131         /*
0132          * Attempt to find the requested partition by adding an offset
0133          * to the partition number found by UUID.
0134          */
0135         devt = part_devt(dev_to_disk(dev),
0136                  dev_to_bdev(dev)->bd_partno + offset);
0137     } else {
0138         devt = dev->devt;
0139     }
0140 
0141     put_device(dev);
0142     return devt;
0143 
0144 clear_root_wait:
0145     pr_err("VFS: PARTUUID= is invalid.\n"
0146            "Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
0147     if (root_wait)
0148         pr_err("Disabling rootwait; root= is invalid.\n");
0149     root_wait = 0;
0150     return 0;
0151 }
0152 
0153 /**
0154  * match_dev_by_label - callback for finding a partition using its label
0155  * @dev:    device passed in by the caller
0156  * @data:   opaque pointer to the label to match
0157  *
0158  * Returns 1 if the device matches, and 0 otherwise.
0159  */
0160 static int match_dev_by_label(struct device *dev, const void *data)
0161 {
0162     struct block_device *bdev = dev_to_bdev(dev);
0163     const char *label = data;
0164 
0165     if (!bdev->bd_meta_info || strcmp(label, bdev->bd_meta_info->volname))
0166         return 0;
0167     return 1;
0168 }
0169 
0170 static dev_t devt_from_partlabel(const char *label)
0171 {
0172     struct device *dev;
0173     dev_t devt = 0;
0174 
0175     dev = class_find_device(&block_class, NULL, label, &match_dev_by_label);
0176     if (dev) {
0177         devt = dev->devt;
0178         put_device(dev);
0179     }
0180 
0181     return devt;
0182 }
0183 
0184 static dev_t devt_from_devname(const char *name)
0185 {
0186     dev_t devt = 0;
0187     int part;
0188     char s[32];
0189     char *p;
0190 
0191     if (strlen(name) > 31)
0192         return 0;
0193     strcpy(s, name);
0194     for (p = s; *p; p++) {
0195         if (*p == '/')
0196             *p = '!';
0197     }
0198 
0199     devt = blk_lookup_devt(s, 0);
0200     if (devt)
0201         return devt;
0202 
0203     /*
0204      * Try non-existent, but valid partition, which may only exist after
0205      * opening the device, like partitioned md devices.
0206      */
0207     while (p > s && isdigit(p[-1]))
0208         p--;
0209     if (p == s || !*p || *p == '0')
0210         return 0;
0211 
0212     /* try disk name without <part number> */
0213     part = simple_strtoul(p, NULL, 10);
0214     *p = '\0';
0215     devt = blk_lookup_devt(s, part);
0216     if (devt)
0217         return devt;
0218 
0219     /* try disk name without p<part number> */
0220     if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
0221         return 0;
0222     p[-1] = '\0';
0223     return blk_lookup_devt(s, part);
0224 }
0225 #endif /* CONFIG_BLOCK */
0226 
0227 static dev_t devt_from_devnum(const char *name)
0228 {
0229     unsigned maj, min, offset;
0230     dev_t devt = 0;
0231     char *p, dummy;
0232 
0233     if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
0234         sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) == 3) {
0235         devt = MKDEV(maj, min);
0236         if (maj != MAJOR(devt) || min != MINOR(devt))
0237             return 0;
0238     } else {
0239         devt = new_decode_dev(simple_strtoul(name, &p, 16));
0240         if (*p)
0241             return 0;
0242     }
0243 
0244     return devt;
0245 }
0246 
0247 /*
0248  *  Convert a name into device number.  We accept the following variants:
0249  *
0250  *  1) <hex_major><hex_minor> device number in hexadecimal represents itself
0251  *         no leading 0x, for example b302.
0252  *  2) /dev/nfs represents Root_NFS (0xff)
0253  *  3) /dev/<disk_name> represents the device number of disk
0254  *  4) /dev/<disk_name><decimal> represents the device number
0255  *         of partition - device number of disk plus the partition number
0256  *  5) /dev/<disk_name>p<decimal> - same as the above, that form is
0257  *     used when disk name of partitioned disk ends on a digit.
0258  *  6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
0259  *     unique id of a partition if the partition table provides it.
0260  *     The UUID may be either an EFI/GPT UUID, or refer to an MSDOS
0261  *     partition using the format SSSSSSSS-PP, where SSSSSSSS is a zero-
0262  *     filled hex representation of the 32-bit "NT disk signature", and PP
0263  *     is a zero-filled hex representation of the 1-based partition number.
0264  *  7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
0265  *     a partition with a known unique id.
0266  *  8) <major>:<minor> major and minor number of the device separated by
0267  *     a colon.
0268  *  9) PARTLABEL=<name> with name being the GPT partition label.
0269  *     MSDOS partitions do not support labels!
0270  *  10) /dev/cifs represents Root_CIFS (0xfe)
0271  *
0272  *  If name doesn't have fall into the categories above, we return (0,0).
0273  *  block_class is used to check if something is a disk name. If the disk
0274  *  name contains slashes, the device name has them replaced with
0275  *  bangs.
0276  */
0277 dev_t name_to_dev_t(const char *name)
0278 {
0279     if (strcmp(name, "/dev/nfs") == 0)
0280         return Root_NFS;
0281     if (strcmp(name, "/dev/cifs") == 0)
0282         return Root_CIFS;
0283     if (strcmp(name, "/dev/ram") == 0)
0284         return Root_RAM0;
0285 #ifdef CONFIG_BLOCK
0286     if (strncmp(name, "PARTUUID=", 9) == 0)
0287         return devt_from_partuuid(name + 9);
0288     if (strncmp(name, "PARTLABEL=", 10) == 0)
0289         return devt_from_partlabel(name + 10);
0290     if (strncmp(name, "/dev/", 5) == 0)
0291         return devt_from_devname(name + 5);
0292 #endif
0293     return devt_from_devnum(name);
0294 }
0295 EXPORT_SYMBOL_GPL(name_to_dev_t);
0296 
0297 static int __init root_dev_setup(char *line)
0298 {
0299     strlcpy(saved_root_name, line, sizeof(saved_root_name));
0300     return 1;
0301 }
0302 
0303 __setup("root=", root_dev_setup);
0304 
0305 static int __init rootwait_setup(char *str)
0306 {
0307     if (*str)
0308         return 0;
0309     root_wait = 1;
0310     return 1;
0311 }
0312 
0313 __setup("rootwait", rootwait_setup);
0314 
0315 static char * __initdata root_mount_data;
0316 static int __init root_data_setup(char *str)
0317 {
0318     root_mount_data = str;
0319     return 1;
0320 }
0321 
0322 static char * __initdata root_fs_names;
0323 static int __init fs_names_setup(char *str)
0324 {
0325     root_fs_names = str;
0326     return 1;
0327 }
0328 
0329 static unsigned int __initdata root_delay;
0330 static int __init root_delay_setup(char *str)
0331 {
0332     root_delay = simple_strtoul(str, NULL, 0);
0333     return 1;
0334 }
0335 
0336 __setup("rootflags=", root_data_setup);
0337 __setup("rootfstype=", fs_names_setup);
0338 __setup("rootdelay=", root_delay_setup);
0339 
0340 /* This can return zero length strings. Caller should check */
0341 static int __init split_fs_names(char *page, size_t size, char *names)
0342 {
0343     int count = 1;
0344     char *p = page;
0345 
0346     strlcpy(p, root_fs_names, size);
0347     while (*p++) {
0348         if (p[-1] == ',') {
0349             p[-1] = '\0';
0350             count++;
0351         }
0352     }
0353 
0354     return count;
0355 }
0356 
0357 static int __init do_mount_root(const char *name, const char *fs,
0358                  const int flags, const void *data)
0359 {
0360     struct super_block *s;
0361     struct page *p = NULL;
0362     char *data_page = NULL;
0363     int ret;
0364 
0365     if (data) {
0366         /* init_mount() requires a full page as fifth argument */
0367         p = alloc_page(GFP_KERNEL);
0368         if (!p)
0369             return -ENOMEM;
0370         data_page = page_address(p);
0371         /* zero-pad. init_mount() will make sure it's terminated */
0372         strncpy(data_page, data, PAGE_SIZE);
0373     }
0374 
0375     ret = init_mount(name, "/root", fs, flags, data_page);
0376     if (ret)
0377         goto out;
0378 
0379     init_chdir("/root");
0380     s = current->fs->pwd.dentry->d_sb;
0381     ROOT_DEV = s->s_dev;
0382     printk(KERN_INFO
0383            "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
0384            s->s_type->name,
0385            sb_rdonly(s) ? " readonly" : "",
0386            MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
0387 
0388 out:
0389     if (p)
0390         put_page(p);
0391     return ret;
0392 }
0393 
0394 void __init mount_block_root(char *name, int flags)
0395 {
0396     struct page *page = alloc_page(GFP_KERNEL);
0397     char *fs_names = page_address(page);
0398     char *p;
0399     char b[BDEVNAME_SIZE];
0400     int num_fs, i;
0401 
0402     scnprintf(b, BDEVNAME_SIZE, "unknown-block(%u,%u)",
0403           MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
0404     if (root_fs_names)
0405         num_fs = split_fs_names(fs_names, PAGE_SIZE, root_fs_names);
0406     else
0407         num_fs = list_bdev_fs_names(fs_names, PAGE_SIZE);
0408 retry:
0409     for (i = 0, p = fs_names; i < num_fs; i++, p += strlen(p)+1) {
0410         int err;
0411 
0412         if (!*p)
0413             continue;
0414         err = do_mount_root(name, p, flags, root_mount_data);
0415         switch (err) {
0416             case 0:
0417                 goto out;
0418             case -EACCES:
0419             case -EINVAL:
0420                 continue;
0421         }
0422             /*
0423          * Allow the user to distinguish between failed sys_open
0424          * and bad superblock on root device.
0425          * and give them a list of the available devices
0426          */
0427         printk("VFS: Cannot open root device \"%s\" or %s: error %d\n",
0428                 root_device_name, b, err);
0429         printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
0430 
0431         printk_all_partitions();
0432         panic("VFS: Unable to mount root fs on %s", b);
0433     }
0434     if (!(flags & SB_RDONLY)) {
0435         flags |= SB_RDONLY;
0436         goto retry;
0437     }
0438 
0439     printk("List of all partitions:\n");
0440     printk_all_partitions();
0441     printk("No filesystem could mount root, tried: ");
0442     for (i = 0, p = fs_names; i < num_fs; i++, p += strlen(p)+1)
0443         printk(" %s", p);
0444     printk("\n");
0445     panic("VFS: Unable to mount root fs on %s", b);
0446 out:
0447     put_page(page);
0448 }
0449  
0450 #ifdef CONFIG_ROOT_NFS
0451 
0452 #define NFSROOT_TIMEOUT_MIN 5
0453 #define NFSROOT_TIMEOUT_MAX 30
0454 #define NFSROOT_RETRY_MAX   5
0455 
0456 static int __init mount_nfs_root(void)
0457 {
0458     char *root_dev, *root_data;
0459     unsigned int timeout;
0460     int try, err;
0461 
0462     err = nfs_root_data(&root_dev, &root_data);
0463     if (err != 0)
0464         return 0;
0465 
0466     /*
0467      * The server or network may not be ready, so try several
0468      * times.  Stop after a few tries in case the client wants
0469      * to fall back to other boot methods.
0470      */
0471     timeout = NFSROOT_TIMEOUT_MIN;
0472     for (try = 1; ; try++) {
0473         err = do_mount_root(root_dev, "nfs",
0474                     root_mountflags, root_data);
0475         if (err == 0)
0476             return 1;
0477         if (try > NFSROOT_RETRY_MAX)
0478             break;
0479 
0480         /* Wait, in case the server refused us immediately */
0481         ssleep(timeout);
0482         timeout <<= 1;
0483         if (timeout > NFSROOT_TIMEOUT_MAX)
0484             timeout = NFSROOT_TIMEOUT_MAX;
0485     }
0486     return 0;
0487 }
0488 #endif
0489 
0490 #ifdef CONFIG_CIFS_ROOT
0491 
0492 extern int cifs_root_data(char **dev, char **opts);
0493 
0494 #define CIFSROOT_TIMEOUT_MIN    5
0495 #define CIFSROOT_TIMEOUT_MAX    30
0496 #define CIFSROOT_RETRY_MAX  5
0497 
0498 static int __init mount_cifs_root(void)
0499 {
0500     char *root_dev, *root_data;
0501     unsigned int timeout;
0502     int try, err;
0503 
0504     err = cifs_root_data(&root_dev, &root_data);
0505     if (err != 0)
0506         return 0;
0507 
0508     timeout = CIFSROOT_TIMEOUT_MIN;
0509     for (try = 1; ; try++) {
0510         err = do_mount_root(root_dev, "cifs", root_mountflags,
0511                     root_data);
0512         if (err == 0)
0513             return 1;
0514         if (try > CIFSROOT_RETRY_MAX)
0515             break;
0516 
0517         ssleep(timeout);
0518         timeout <<= 1;
0519         if (timeout > CIFSROOT_TIMEOUT_MAX)
0520             timeout = CIFSROOT_TIMEOUT_MAX;
0521     }
0522     return 0;
0523 }
0524 #endif
0525 
0526 static bool __init fs_is_nodev(char *fstype)
0527 {
0528     struct file_system_type *fs = get_fs_type(fstype);
0529     bool ret = false;
0530 
0531     if (fs) {
0532         ret = !(fs->fs_flags & FS_REQUIRES_DEV);
0533         put_filesystem(fs);
0534     }
0535 
0536     return ret;
0537 }
0538 
0539 static int __init mount_nodev_root(void)
0540 {
0541     char *fs_names, *fstype;
0542     int err = -EINVAL;
0543     int num_fs, i;
0544 
0545     fs_names = (void *)__get_free_page(GFP_KERNEL);
0546     if (!fs_names)
0547         return -EINVAL;
0548     num_fs = split_fs_names(fs_names, PAGE_SIZE, root_fs_names);
0549 
0550     for (i = 0, fstype = fs_names; i < num_fs;
0551          i++, fstype += strlen(fstype) + 1) {
0552         if (!*fstype)
0553             continue;
0554         if (!fs_is_nodev(fstype))
0555             continue;
0556         err = do_mount_root(root_device_name, fstype, root_mountflags,
0557                     root_mount_data);
0558         if (!err)
0559             break;
0560     }
0561 
0562     free_page((unsigned long)fs_names);
0563     return err;
0564 }
0565 
0566 void __init mount_root(void)
0567 {
0568 #ifdef CONFIG_ROOT_NFS
0569     if (ROOT_DEV == Root_NFS) {
0570         if (!mount_nfs_root())
0571             printk(KERN_ERR "VFS: Unable to mount root fs via NFS.\n");
0572         return;
0573     }
0574 #endif
0575 #ifdef CONFIG_CIFS_ROOT
0576     if (ROOT_DEV == Root_CIFS) {
0577         if (!mount_cifs_root())
0578             printk(KERN_ERR "VFS: Unable to mount root fs via SMB.\n");
0579         return;
0580     }
0581 #endif
0582     if (ROOT_DEV == 0 && root_device_name && root_fs_names) {
0583         if (mount_nodev_root() == 0)
0584             return;
0585     }
0586 #ifdef CONFIG_BLOCK
0587     {
0588         int err = create_dev("/dev/root", ROOT_DEV);
0589 
0590         if (err < 0)
0591             pr_emerg("Failed to create /dev/root: %d\n", err);
0592         mount_block_root("/dev/root", root_mountflags);
0593     }
0594 #endif
0595 }
0596 
0597 /*
0598  * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
0599  */
0600 void __init prepare_namespace(void)
0601 {
0602     if (root_delay) {
0603         printk(KERN_INFO "Waiting %d sec before mounting root device...\n",
0604                root_delay);
0605         ssleep(root_delay);
0606     }
0607 
0608     /*
0609      * wait for the known devices to complete their probing
0610      *
0611      * Note: this is a potential source of long boot delays.
0612      * For example, it is not atypical to wait 5 seconds here
0613      * for the touchpad of a laptop to initialize.
0614      */
0615     wait_for_device_probe();
0616 
0617     md_run_setup();
0618 
0619     if (saved_root_name[0]) {
0620         root_device_name = saved_root_name;
0621         if (!strncmp(root_device_name, "mtd", 3) ||
0622             !strncmp(root_device_name, "ubi", 3)) {
0623             mount_block_root(root_device_name, root_mountflags);
0624             goto out;
0625         }
0626         ROOT_DEV = name_to_dev_t(root_device_name);
0627         if (strncmp(root_device_name, "/dev/", 5) == 0)
0628             root_device_name += 5;
0629     }
0630 
0631     if (initrd_load())
0632         goto out;
0633 
0634     /* wait for any asynchronous scanning to complete */
0635     if ((ROOT_DEV == 0) && root_wait) {
0636         printk(KERN_INFO "Waiting for root device %s...\n",
0637             saved_root_name);
0638         while (driver_probe_done() != 0 ||
0639             (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
0640             msleep(5);
0641         async_synchronize_full();
0642     }
0643 
0644     mount_root();
0645 out:
0646     devtmpfs_mount();
0647     init_mount(".", "/", NULL, MS_MOVE, NULL);
0648     init_chroot(".");
0649 }
0650 
0651 static bool is_tmpfs;
0652 static int rootfs_init_fs_context(struct fs_context *fc)
0653 {
0654     if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
0655         return shmem_init_fs_context(fc);
0656 
0657     return ramfs_init_fs_context(fc);
0658 }
0659 
0660 struct file_system_type rootfs_fs_type = {
0661     .name       = "rootfs",
0662     .init_fs_context = rootfs_init_fs_context,
0663     .kill_sb    = kill_litter_super,
0664 };
0665 
0666 void __init init_rootfs(void)
0667 {
0668     if (IS_ENABLED(CONFIG_TMPFS) && !saved_root_name[0] &&
0669         (!root_fs_names || strstr(root_fs_names, "tmpfs")))
0670         is_tmpfs = true;
0671 }