Back to home page

LXR

 
 

    


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