1 #include <linux/module.h>
2 #include <linux/sched.h>
3 #include <linux/ctype.h>
6 #include <linux/suspend.h>
7 #include <linux/root_dev.h>
8 #include <linux/security.h>
9 #include <linux/delay.h>
10 #include <linux/genhd.h>
11 #include <linux/mount.h>
12 #include <linux/device.h>
13 #include <linux/init.h>
15 #include <linux/initrd.h>
16 #include <linux/async.h>
17 #include <linux/fs_struct.h>
18 #include <linux/slab.h>
19 #include <linux/ramfs.h>
20 #include <linux/shmem_fs.h>
22 #include <linux/nfs_fs.h>
23 #include <linux/nfs_fs_sb.h>
24 #include <linux/nfs_mount.h>
26 #include "do_mounts.h"
28 int __initdata rd_doload; /* 1 = load RAM disk, 0 = don't load */
30 int root_mountflags = MS_RDONLY | MS_SILENT;
31 static char * __initdata root_device_name;
32 static char __initdata saved_root_name[64];
37 static int __init load_ramdisk(char *str)
39 rd_doload = simple_strtol(str,NULL,0) & 3;
42 __setup("load_ramdisk=", load_ramdisk);
44 static int __init readonly(char *str)
48 root_mountflags |= MS_RDONLY;
52 static int __init readwrite(char *str)
56 root_mountflags &= ~MS_RDONLY;
60 __setup("ro", readonly);
61 __setup("rw", readwrite);
70 * match_dev_by_uuid - callback for finding a partition using its uuid
71 * @dev: device passed in by the caller
72 * @data: opaque pointer to the desired struct uuidcmp to match
74 * Returns 1 if the device matches, and 0 otherwise.
76 static int match_dev_by_uuid(struct device *dev, const void *data)
78 const struct uuidcmp *cmp = data;
79 struct hd_struct *part = dev_to_part(dev);
84 if (strncasecmp(cmp->uuid, part->info->uuid, cmp->len))
94 * devt_from_partuuid - looks up the dev_t of a partition by its UUID
95 * @uuid_str: char array containing ascii UUID
97 * The function will return the first partition which contains a matching
98 * UUID value in its partition_meta_info struct. This does not search
99 * by filesystem UUIDs.
101 * If @uuid_str is followed by a "/PARTNROFF=%d", then the number will be
102 * extracted and used as an offset from the partition identified by the UUID.
104 * Returns the matching dev_t on success or 0 on failure.
106 static dev_t devt_from_partuuid(const char *uuid_str)
110 struct device *dev = NULL;
111 struct gendisk *disk;
112 struct hd_struct *part;
114 bool clear_root_wait = false;
119 slash = strchr(uuid_str, '/');
120 /* Check for optional partition number offset attributes. */
123 /* Explicitly fail on poor PARTUUID syntax. */
124 if (sscanf(slash + 1,
125 "PARTNROFF=%d%c", &offset, &c) != 1) {
126 clear_root_wait = true;
129 cmp.len = slash - uuid_str;
131 cmp.len = strlen(uuid_str);
135 clear_root_wait = true;
139 dev = class_find_device(&block_class, NULL, &cmp,
146 /* Attempt to find the partition by offset. */
151 disk = part_to_disk(dev_to_part(dev));
152 part = disk_get_part(disk, dev_to_part(dev)->partno + offset);
154 res = part_devt(part);
155 put_device(part_to_dev(part));
161 if (clear_root_wait) {
162 pr_err("VFS: PARTUUID= is invalid.\n"
163 "Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
165 pr_err("Disabling rootwait; root= is invalid.\n");
172 * match_dev_by_label - callback for finding a partition using its label
173 * @dev: device passed in by the caller
174 * @data: opaque pointer to the label to match
176 * Returns 1 if the device matches, and 0 otherwise.
178 static int match_dev_by_label(struct device *dev, const void *data)
180 const char *label = data;
181 struct hd_struct *part = dev_to_part(dev);
183 if (part->info && !strcmp(label, part->info->volname))
191 * Convert a name into device number. We accept the following variants:
193 * 1) <hex_major><hex_minor> device number in hexadecimal represents itself
194 * no leading 0x, for example b302.
195 * 2) /dev/nfs represents Root_NFS (0xff)
196 * 3) /dev/<disk_name> represents the device number of disk
197 * 4) /dev/<disk_name><decimal> represents the device number
198 * of partition - device number of disk plus the partition number
199 * 5) /dev/<disk_name>p<decimal> - same as the above, that form is
200 * used when disk name of partitioned disk ends on a digit.
201 * 6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
202 * unique id of a partition if the partition table provides it.
203 * The UUID may be either an EFI/GPT UUID, or refer to an MSDOS
204 * partition using the format SSSSSSSS-PP, where SSSSSSSS is a zero-
205 * filled hex representation of the 32-bit "NT disk signature", and PP
206 * is a zero-filled hex representation of the 1-based partition number.
207 * 7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
208 * a partition with a known unique id.
209 * 8) <major>:<minor> major and minor number of the device separated by
211 * 9) PARTLABEL=<name> with name being the GPT partition label.
212 * MSDOS partitions do not support labels!
214 * If name doesn't have fall into the categories above, we return (0,0).
215 * block_class is used to check if something is a disk name. If the disk
216 * name contains slashes, the device name has them replaced with
220 dev_t name_to_dev_t(const char *name)
228 if (strncmp(name, "PARTUUID=", 9) == 0) {
230 res = devt_from_partuuid(name);
234 } else if (strncmp(name, "PARTLABEL=", 10) == 0) {
237 dev = class_find_device(&block_class, NULL, name + 10,
238 &match_dev_by_label);
248 if (strncmp(name, "/dev/", 5) != 0) {
249 unsigned maj, min, offset;
252 if ((sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2) ||
253 (sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) == 3)) {
254 res = MKDEV(maj, min);
255 if (maj != MAJOR(res) || min != MINOR(res))
258 res = new_decode_dev(simple_strtoul(name, &p, 16));
267 if (strcmp(name, "nfs") == 0)
270 if (strcmp(name, "ram") == 0)
273 if (strlen(name) > 31)
279 res = blk_lookup_devt(s, 0);
284 * try non-existent, but valid partition, which may only exist
285 * after revalidating the disk, like partitioned md devices
287 while (p > s && isdigit(p[-1]))
289 if (p == s || !*p || *p == '0')
292 /* try disk name without <part number> */
293 part = simple_strtoul(p, NULL, 10);
295 res = blk_lookup_devt(s, part);
299 /* try disk name without p<part number> */
300 if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
303 res = blk_lookup_devt(s, part);
312 EXPORT_SYMBOL_GPL(name_to_dev_t);
314 static int __init root_dev_setup(char *line)
316 strlcpy(saved_root_name, line, sizeof(saved_root_name));
320 __setup("root=", root_dev_setup);
322 static int __init rootwait_setup(char *str)
330 __setup("rootwait", rootwait_setup);
332 static char * __initdata root_mount_data;
333 static int __init root_data_setup(char *str)
335 root_mount_data = str;
339 static char * __initdata root_fs_names;
340 static int __init fs_names_setup(char *str)
346 static unsigned int __initdata root_delay;
347 static int __init root_delay_setup(char *str)
349 root_delay = simple_strtoul(str, NULL, 0);
353 __setup("rootflags=", root_data_setup);
354 __setup("rootfstype=", fs_names_setup);
355 __setup("rootdelay=", root_delay_setup);
357 static void __init get_fs_names(char *page)
362 strcpy(page, root_fs_names);
368 int len = get_filesystem_list(page);
372 for (p = page-1; p; p = next) {
373 next = strchr(++p, '\n');
376 while ((*s++ = *p++) != '\n')
384 static int __init do_mount_root(char *name, char *fs, int flags, void *data)
386 struct super_block *s;
387 int err = ksys_mount(name, "/root", fs, flags, data);
392 s = current->fs->pwd.dentry->d_sb;
395 "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
397 sb_rdonly(s) ? " readonly" : "",
398 MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
402 void __init mount_block_root(char *name, int flags)
404 struct page *page = alloc_page(GFP_KERNEL);
405 char *fs_names = page_address(page);
408 char b[BDEVNAME_SIZE];
410 const char *b = name;
413 get_fs_names(fs_names);
415 for (p = fs_names; *p; p += strlen(p)+1) {
416 int err = do_mount_root(name, p, flags, root_mount_data);
425 * Allow the user to distinguish between failed sys_open
426 * and bad superblock on root device.
427 * and give them a list of the available devices
430 __bdevname(ROOT_DEV, b);
432 printk("VFS: Cannot open root device \"%s\" or %s: error %d\n",
433 root_device_name, b, err);
434 printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
436 printk_all_partitions();
437 #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
438 printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
439 "explicit textual name for \"root=\" boot option.\n");
441 panic("VFS: Unable to mount root fs on %s", b);
443 if (!(flags & SB_RDONLY)) {
448 printk("List of all partitions:\n");
449 printk_all_partitions();
450 printk("No filesystem could mount root, tried: ");
451 for (p = fs_names; *p; p += strlen(p)+1)
455 __bdevname(ROOT_DEV, b);
457 panic("VFS: Unable to mount root fs on %s", b);
462 #ifdef CONFIG_ROOT_NFS
464 #define NFSROOT_TIMEOUT_MIN 5
465 #define NFSROOT_TIMEOUT_MAX 30
466 #define NFSROOT_RETRY_MAX 5
468 static int __init mount_nfs_root(void)
470 char *root_dev, *root_data;
471 unsigned int timeout;
474 err = nfs_root_data(&root_dev, &root_data);
479 * The server or network may not be ready, so try several
480 * times. Stop after a few tries in case the client wants
481 * to fall back to other boot methods.
483 timeout = NFSROOT_TIMEOUT_MIN;
484 for (try = 1; ; try++) {
485 err = do_mount_root(root_dev, "nfs",
486 root_mountflags, root_data);
489 if (try > NFSROOT_RETRY_MAX)
492 /* Wait, in case the server refused us immediately */
495 if (timeout > NFSROOT_TIMEOUT_MAX)
496 timeout = NFSROOT_TIMEOUT_MAX;
502 #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
503 void __init change_floppy(char *fmt, ...)
505 struct termios termios;
511 vsprintf(buf, fmt, args);
513 fd = ksys_open("/dev/root", O_RDWR | O_NDELAY, 0);
515 ksys_ioctl(fd, FDEJECT, 0);
518 printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
519 fd = ksys_open("/dev/console", O_RDWR, 0);
521 ksys_ioctl(fd, TCGETS, (long)&termios);
522 termios.c_lflag &= ~ICANON;
523 ksys_ioctl(fd, TCSETSF, (long)&termios);
524 ksys_read(fd, &c, 1);
525 termios.c_lflag |= ICANON;
526 ksys_ioctl(fd, TCSETSF, (long)&termios);
532 void __init mount_root(void)
534 #ifdef CONFIG_ROOT_NFS
535 if (ROOT_DEV == Root_NFS) {
536 if (mount_nfs_root())
539 printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
543 #ifdef CONFIG_BLK_DEV_FD
544 if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
545 /* rd_doload is 2 for a dual initrd/ramload setup */
547 if (rd_load_disk(1)) {
548 ROOT_DEV = Root_RAM1;
549 root_device_name = NULL;
552 change_floppy("root floppy");
557 int err = create_dev("/dev/root", ROOT_DEV);
560 pr_emerg("Failed to create /dev/root: %d\n", err);
561 mount_block_root("/dev/root", root_mountflags);
567 * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
569 void __init prepare_namespace(void)
574 printk(KERN_INFO "Waiting %d sec before mounting root device...\n",
580 * wait for the known devices to complete their probing
582 * Note: this is a potential source of long boot delays.
583 * For example, it is not atypical to wait 5 seconds here
584 * for the touchpad of a laptop to initialize.
586 wait_for_device_probe();
590 if (saved_root_name[0]) {
591 root_device_name = saved_root_name;
592 if (!strncmp(root_device_name, "mtd", 3) ||
593 !strncmp(root_device_name, "ubi", 3)) {
594 mount_block_root(root_device_name, root_mountflags);
597 ROOT_DEV = name_to_dev_t(root_device_name);
598 if (strncmp(root_device_name, "/dev/", 5) == 0)
599 root_device_name += 5;
605 /* wait for any asynchronous scanning to complete */
606 if ((ROOT_DEV == 0) && root_wait) {
607 printk(KERN_INFO "Waiting for root device %s...\n",
609 while (driver_probe_done() != 0 ||
610 (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
612 async_synchronize_full();
615 is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
617 if (is_floppy && rd_doload && rd_load_disk(0))
618 ROOT_DEV = Root_RAM0;
622 devtmpfs_mount("dev");
623 ksys_mount(".", "/", NULL, MS_MOVE, NULL);
627 static bool is_tmpfs;
628 static struct dentry *rootfs_mount(struct file_system_type *fs_type,
629 int flags, const char *dev_name, void *data)
631 static unsigned long once;
632 void *fill = ramfs_fill_super;
634 if (test_and_set_bit(0, &once))
635 return ERR_PTR(-ENODEV);
637 if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
638 fill = shmem_fill_super;
640 return mount_nodev(fs_type, flags, data, fill);
643 static struct file_system_type rootfs_fs_type = {
645 .mount = rootfs_mount,
646 .kill_sb = kill_litter_super,
649 int __init init_rootfs(void)
651 int err = register_filesystem(&rootfs_fs_type);
656 if (IS_ENABLED(CONFIG_TMPFS) && !saved_root_name[0] &&
657 (!root_fs_names || strstr(root_fs_names, "tmpfs"))) {
661 err = init_ramfs_fs();
665 unregister_filesystem(&rootfs_fs_type);