| 1 | |
| 2 | #include <linux/module.h>
|
| 3 | #include <linux/sched.h>
|
| 4 | #include <linux/ctype.h>
|
| 5 | #include <linux/fd.h>
|
| 6 | #include <linux/tty.h>
|
| 7 | #include <linux/suspend.h>
|
| 8 | #include <linux/root_dev.h>
|
| 9 | #include <linux/security.h>
|
| 10 | #include <linux/delay.h>
|
| 11 | #include <linux/mount.h>
|
| 12 | #include <linux/device.h>
|
| 13 | #include <linux/init.h>
|
| 14 | #include <linux/fs.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>
|
| 21 | #include <linux/ktime.h>
|
| 22 |
|
| 23 | #include <linux/nfs_fs.h>
|
| 24 | #include <linux/nfs_fs_sb.h>
|
| 25 | #include <linux/nfs_mount.h>
|
| 26 | #include <linux/raid/detect.h>
|
| 27 | #include <uapi/linux/mount.h>
|
| 28 |
|
| 29 | #include "do_mounts.h"
|
| 30 |
|
| 31 | int root_mountflags = MS_RDONLY | MS_SILENT;
|
| 32 | static char __initdata saved_root_name[64];
|
| 33 | static int root_wait;
|
| 34 |
|
| 35 | dev_t ROOT_DEV;
|
| 36 |
|
| 37 | static int __init readonly(char *str)
|
| 38 | {
|
| 39 | if (*str)
|
| 40 | return 0;
|
| 41 | root_mountflags |= MS_RDONLY;
|
| 42 | return 1;
|
| 43 | }
|
| 44 |
|
| 45 | static int __init readwrite(char *str)
|
| 46 | {
|
| 47 | if (*str)
|
| 48 | return 0;
|
| 49 | root_mountflags &= ~MS_RDONLY;
|
| 50 | return 1;
|
| 51 | }
|
| 52 |
|
| 53 | __setup("ro", readonly);
|
| 54 | __setup("rw", readwrite);
|
| 55 |
|
| 56 | static int __init root_dev_setup(char *line)
|
| 57 | {
|
| 58 | strscpy(saved_root_name, line, sizeof(saved_root_name));
|
| 59 | return 1;
|
| 60 | }
|
| 61 |
|
| 62 | __setup("root=", root_dev_setup);
|
| 63 |
|
| 64 | static int __init rootwait_setup(char *str)
|
| 65 | {
|
| 66 | if (*str)
|
| 67 | return 0;
|
| 68 | root_wait = -1;
|
| 69 | return 1;
|
| 70 | }
|
| 71 |
|
| 72 | __setup("rootwait", rootwait_setup);
|
| 73 |
|
| 74 | static int __init rootwait_timeout_setup(char *str)
|
| 75 | {
|
| 76 | int sec;
|
| 77 |
|
| 78 | if (kstrtoint(str, 0, &sec) || sec < 0) {
|
| 79 | pr_warn("ignoring invalid rootwait value\n");
|
| 80 | goto ignore;
|
| 81 | }
|
| 82 |
|
| 83 | if (check_mul_overflow(sec, MSEC_PER_SEC, &root_wait)) {
|
| 84 | pr_warn("ignoring excessive rootwait value\n");
|
| 85 | goto ignore;
|
| 86 | }
|
| 87 |
|
| 88 | return 1;
|
| 89 |
|
| 90 | ignore:
|
| 91 |
|
| 92 | root_wait = -1;
|
| 93 |
|
| 94 | return 1;
|
| 95 | }
|
| 96 |
|
| 97 | __setup("rootwait=", rootwait_timeout_setup);
|
| 98 |
|
| 99 | static char * __initdata root_mount_data;
|
| 100 | static int __init root_data_setup(char *str)
|
| 101 | {
|
| 102 | root_mount_data = str;
|
| 103 | return 1;
|
| 104 | }
|
| 105 |
|
| 106 | static char * __initdata root_fs_names;
|
| 107 | static int __init fs_names_setup(char *str)
|
| 108 | {
|
| 109 | root_fs_names = str;
|
| 110 | return 1;
|
| 111 | }
|
| 112 |
|
| 113 | static unsigned int __initdata root_delay;
|
| 114 | static int __init root_delay_setup(char *str)
|
| 115 | {
|
| 116 | if (kstrtouint(str, 0, &root_delay))
|
| 117 | return 0;
|
| 118 | return 1;
|
| 119 | }
|
| 120 |
|
| 121 | __setup("rootflags=", root_data_setup);
|
| 122 | __setup("rootfstype=", fs_names_setup);
|
| 123 | __setup("rootdelay=", root_delay_setup);
|
| 124 |
|
| 125 |
|
| 126 | static int __init split_fs_names(char *page, size_t size)
|
| 127 | {
|
| 128 | int count = 1;
|
| 129 | char *p = page;
|
| 130 |
|
| 131 | strscpy(p, root_fs_names, size);
|
| 132 | while (*p++) {
|
| 133 | if (p[-1] == ',') {
|
| 134 | p[-1] = '\0';
|
| 135 | count++;
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | return count;
|
| 140 | }
|
| 141 |
|
| 142 | static int __init do_mount_root(const char *name, const char *fs,
|
| 143 | const int flags, const void *data)
|
| 144 | {
|
| 145 | struct super_block *s;
|
| 146 | char *data_page = NULL;
|
| 147 | int ret;
|
| 148 |
|
| 149 | if (data) {
|
| 150 |
|
| 151 | data_page = kmalloc(PAGE_SIZE, GFP_KERNEL);
|
| 152 | if (!data_page)
|
| 153 | return -ENOMEM;
|
| 154 | strscpy_pad(data_page, data, PAGE_SIZE);
|
| 155 | }
|
| 156 |
|
| 157 | ret = init_mount(name, "/root", fs, flags, data_page);
|
| 158 | if (ret)
|
| 159 | goto out;
|
| 160 |
|
| 161 | init_chdir("/root");
|
| 162 | s = current->fs->pwd.dentry->d_sb;
|
| 163 | ROOT_DEV = s->s_dev;
|
| 164 | printk(KERN_INFO
|
| 165 | "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
|
| 166 | s->s_type->name,
|
| 167 | sb_rdonly(s) ? " readonly" : "",
|
| 168 | MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
|
| 169 |
|
| 170 | out:
|
| 171 | kfree(data_page);
|
| 172 | return ret;
|
| 173 | }
|
| 174 |
|
| 175 | void __init mount_root_generic(char *name, char *pretty_name, int flags)
|
| 176 | {
|
| 177 | char *fs_names = kmalloc(PAGE_SIZE, GFP_KERNEL);
|
| 178 | char *p;
|
| 179 | char b[BDEVNAME_SIZE];
|
| 180 | int num_fs, i;
|
| 181 |
|
| 182 | if (!fs_names)
|
| 183 | panic("VFS: Unable to mount root fs: not enough memory");
|
| 184 |
|
| 185 | scnprintf(b, BDEVNAME_SIZE, "unknown-block(%u,%u)",
|
| 186 | MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
|
| 187 | if (root_fs_names)
|
| 188 | num_fs = split_fs_names(fs_names, PAGE_SIZE);
|
| 189 | else
|
| 190 | num_fs = list_bdev_fs_names(fs_names, PAGE_SIZE);
|
| 191 | retry:
|
| 192 | for (i = 0, p = fs_names; i < num_fs; i++, p += strlen(p)+1) {
|
| 193 | int err;
|
| 194 |
|
| 195 | if (!*p)
|
| 196 | continue;
|
| 197 | err = do_mount_root(name, p, flags, root_mount_data);
|
| 198 | switch (err) {
|
| 199 | case 0:
|
| 200 | goto out;
|
| 201 | case -EACCES:
|
| 202 | case -EINVAL:
|
| 203 | #ifdef CONFIG_BLOCK
|
| 204 | init_flush_fput();
|
| 205 | #endif
|
| 206 | continue;
|
| 207 | }
|
| 208 | |
| 209 | * Allow the user to distinguish between failed sys_open
|
| 210 | * and bad superblock on root device.
|
| 211 | * and give them a list of the available devices
|
| 212 |
|
| 213 | printk("VFS: Cannot open root device \"%s\" or %s: error %d\n",
|
| 214 | pretty_name, b, err);
|
| 215 | printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
|
| 216 | printk_all_partitions();
|
| 217 |
|
| 218 | if (root_fs_names)
|
| 219 | num_fs = list_bdev_fs_names(fs_names, PAGE_SIZE);
|
| 220 | if (!num_fs)
|
| 221 | pr_err("Can't find any bdev filesystem to be used for mount!\n");
|
| 222 | else {
|
| 223 | pr_err("List of all bdev filesystems:\n");
|
| 224 | for (i = 0, p = fs_names; i < num_fs; i++, p += strlen(p)+1)
|
| 225 | pr_err(" %s", p);
|
| 226 | pr_err("\n");
|
| 227 | }
|
| 228 |
|
| 229 | panic("VFS: Unable to mount root fs on %s", b);
|
| 230 | }
|
| 231 | if (!(flags & SB_RDONLY)) {
|
| 232 | flags |= SB_RDONLY;
|
| 233 | goto retry;
|
| 234 | }
|
| 235 |
|
| 236 | printk("List of all partitions:\n");
|
| 237 | printk_all_partitions();
|
| 238 | printk("No filesystem could mount root, tried: ");
|
| 239 | for (i = 0, p = fs_names; i < num_fs; i++, p += strlen(p)+1)
|
| 240 | printk(" %s", p);
|
| 241 | printk("\n");
|
| 242 | panic("VFS: Unable to mount root fs on \"%s\" or %s", pretty_name, b);
|
| 243 | out:
|
| 244 | kfree(fs_names);
|
| 245 | }
|
| 246 |
|
| 247 | #ifdef CONFIG_ROOT_NFS
|
| 248 |
|
| 249 | #define NFSROOT_TIMEOUT_MIN 5
|
| 250 | #define NFSROOT_TIMEOUT_MAX 30
|
| 251 | #define NFSROOT_RETRY_MAX 5
|
| 252 |
|
| 253 | static void __init mount_nfs_root(void)
|
| 254 | {
|
| 255 | char *root_dev, *root_data;
|
| 256 | unsigned int timeout;
|
| 257 | int try;
|
| 258 |
|
| 259 | if (nfs_root_data(&root_dev, &root_data))
|
| 260 | goto fail;
|
| 261 |
|
| 262 | |
| 263 | * The server or network may not be ready, so try several
|
| 264 | * times. Stop after a few tries in case the client wants
|
| 265 | * to fall back to other boot methods.
|
| 266 |
|
| 267 | timeout = NFSROOT_TIMEOUT_MIN;
|
| 268 | for (try = 1; ; try++) {
|
| 269 | if (!do_mount_root(root_dev, "nfs", root_mountflags, root_data))
|
| 270 | return;
|
| 271 | if (try > NFSROOT_RETRY_MAX)
|
| 272 | break;
|
| 273 |
|
| 274 |
|
| 275 | ssleep(timeout);
|
| 276 | timeout <<= 1;
|
| 277 | if (timeout > NFSROOT_TIMEOUT_MAX)
|
| 278 | timeout = NFSROOT_TIMEOUT_MAX;
|
| 279 | }
|
| 280 | fail:
|
| 281 | pr_err("VFS: Unable to mount root fs via NFS.\n");
|
| 282 | }
|
| 283 | #else
|
| 284 | static inline void mount_nfs_root(void)
|
| 285 | {
|
| 286 | }
|
| 287 | #endif
|
| 288 |
|
| 289 | #ifdef CONFIG_CIFS_ROOT
|
| 290 |
|
| 291 | #define CIFSROOT_TIMEOUT_MIN 5
|
| 292 | #define CIFSROOT_TIMEOUT_MAX 30
|
| 293 | #define CIFSROOT_RETRY_MAX 5
|
| 294 |
|
| 295 | static void __init mount_cifs_root(void)
|
| 296 | {
|
| 297 | char *root_dev, *root_data;
|
| 298 | unsigned int timeout;
|
| 299 | int try;
|
| 300 |
|
| 301 | if (cifs_root_data(&root_dev, &root_data))
|
| 302 | goto fail;
|
| 303 |
|
| 304 | timeout = CIFSROOT_TIMEOUT_MIN;
|
| 305 | for (try = 1; ; try++) {
|
| 306 | if (!do_mount_root(root_dev, "cifs", root_mountflags,
|
| 307 | root_data))
|
| 308 | return;
|
| 309 | if (try > CIFSROOT_RETRY_MAX)
|
| 310 | break;
|
| 311 |
|
| 312 | ssleep(timeout);
|
| 313 | timeout <<= 1;
|
| 314 | if (timeout > CIFSROOT_TIMEOUT_MAX)
|
| 315 | timeout = CIFSROOT_TIMEOUT_MAX;
|
| 316 | }
|
| 317 | fail:
|
| 318 | pr_err("VFS: Unable to mount root fs via SMB.\n");
|
| 319 | }
|
| 320 | #else
|
| 321 | static inline void mount_cifs_root(void)
|
| 322 | {
|
| 323 | }
|
| 324 | #endif
|
| 325 |
|
| 326 | static bool __init fs_is_nodev(char *fstype)
|
| 327 | {
|
| 328 | struct file_system_type *fs = get_fs_type(fstype);
|
| 329 | bool ret = false;
|
| 330 |
|
| 331 | if (fs) {
|
| 332 | ret = !(fs->fs_flags & FS_REQUIRES_DEV);
|
| 333 | put_filesystem(fs);
|
| 334 | }
|
| 335 |
|
| 336 | return ret;
|
| 337 | }
|
| 338 |
|
| 339 | static int __init mount_nodev_root(char *root_device_name)
|
| 340 | {
|
| 341 | char *fs_names, *fstype;
|
| 342 | int err = -EINVAL;
|
| 343 | int num_fs, i;
|
| 344 |
|
| 345 | fs_names = kmalloc(PAGE_SIZE, GFP_KERNEL);
|
| 346 | if (!fs_names)
|
| 347 | return -EINVAL;
|
| 348 | num_fs = split_fs_names(fs_names, PAGE_SIZE);
|
| 349 |
|
| 350 | for (i = 0, fstype = fs_names; i < num_fs;
|
| 351 | i++, fstype += strlen(fstype) + 1) {
|
| 352 | if (!*fstype)
|
| 353 | continue;
|
| 354 | if (!fs_is_nodev(fstype))
|
| 355 | continue;
|
| 356 | err = do_mount_root(root_device_name, fstype, root_mountflags,
|
| 357 | root_mount_data);
|
| 358 | if (!err)
|
| 359 | break;
|
| 360 | }
|
| 361 |
|
| 362 | kfree(fs_names);
|
| 363 | return err;
|
| 364 | }
|
| 365 |
|
| 366 | #ifdef CONFIG_BLOCK
|
| 367 | static void __init mount_block_root(char *root_device_name)
|
| 368 | {
|
| 369 | int err = create_dev("/dev/root", ROOT_DEV);
|
| 370 |
|
| 371 | if (err < 0)
|
| 372 | pr_emerg("Failed to create /dev/root: %d\n", err);
|
| 373 | mount_root_generic("/dev/root", root_device_name, root_mountflags);
|
| 374 | }
|
| 375 | #else
|
| 376 | static inline void mount_block_root(char *root_device_name)
|
| 377 | {
|
| 378 | }
|
| 379 | #endif
|
| 380 |
|
| 381 | void __init mount_root(char *root_device_name)
|
| 382 | {
|
| 383 | switch (ROOT_DEV) {
|
| 384 | case Root_NFS:
|
| 385 | mount_nfs_root();
|
| 386 | break;
|
| 387 | case Root_CIFS:
|
| 388 | mount_cifs_root();
|
| 389 | break;
|
| 390 | case Root_Generic:
|
| 391 | mount_root_generic(root_device_name, root_device_name,
|
| 392 | root_mountflags);
|
| 393 | break;
|
| 394 | case 0:
|
| 395 | if (root_device_name && root_fs_names &&
|
| 396 | mount_nodev_root(root_device_name) == 0)
|
| 397 | break;
|
| 398 | fallthrough;
|
| 399 | default:
|
| 400 | mount_block_root(root_device_name);
|
| 401 | break;
|
| 402 | }
|
| 403 | }
|
| 404 |
|
| 405 |
|
| 406 | static void __init wait_for_root(char *root_device_name)
|
| 407 | {
|
| 408 | ktime_t end;
|
| 409 |
|
| 410 | if (ROOT_DEV != 0)
|
| 411 | return;
|
| 412 |
|
| 413 | pr_info("Waiting for root device %s...\n", root_device_name);
|
| 414 |
|
| 415 | end = ktime_add_ms(ktime_get_raw(), root_wait);
|
| 416 |
|
| 417 | while (!driver_probe_done() ||
|
| 418 | early_lookup_bdev(root_device_name, &ROOT_DEV) < 0) {
|
| 419 | msleep(5);
|
| 420 | if (root_wait > 0 && ktime_after(ktime_get_raw(), end))
|
| 421 | break;
|
| 422 | }
|
| 423 |
|
| 424 | async_synchronize_full();
|
| 425 |
|
| 426 | }
|
| 427 |
|
| 428 | static dev_t __init parse_root_device(char *root_device_name)
|
| 429 | {
|
| 430 | int error;
|
| 431 | dev_t dev;
|
| 432 |
|
| 433 | if (!strncmp(root_device_name, "mtd", 3) ||
|
| 434 | !strncmp(root_device_name, "ubi", 3))
|
| 435 | return Root_Generic;
|
| 436 | if (strcmp(root_device_name, "/dev/nfs") == 0)
|
| 437 | return Root_NFS;
|
| 438 | if (strcmp(root_device_name, "/dev/cifs") == 0)
|
| 439 | return Root_CIFS;
|
| 440 | if (strcmp(root_device_name, "/dev/ram") == 0)
|
| 441 | return Root_RAM0;
|
| 442 |
|
| 443 | error = early_lookup_bdev(root_device_name, &dev);
|
| 444 | if (error) {
|
| 445 | if (error == -EINVAL && root_wait) {
|
| 446 | pr_err("Disabling rootwait; root= is invalid.\n");
|
| 447 | root_wait = 0;
|
| 448 | }
|
| 449 | return 0;
|
| 450 | }
|
| 451 | return dev;
|
| 452 | }
|
| 453 |
|
| 454 | |
| 455 | * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
|
| 456 |
|
| 457 | void __init prepare_namespace(void)
|
| 458 | {
|
| 459 | if (root_delay) {
|
| 460 | printk(KERN_INFO "Waiting %d sec before mounting root device...\n",
|
| 461 | root_delay);
|
| 462 | ssleep(root_delay);
|
| 463 | }
|
| 464 |
|
| 465 | |
| 466 | * wait for the known devices to complete their probing
|
| 467 | *
|
| 468 | * Note: this is a potential source of long boot delays.
|
| 469 | * For example, it is not atypical to wait 5 seconds here
|
| 470 | * for the touchpad of a laptop to initialize.
|
| 471 |
|
| 472 | wait_for_device_probe();
|
| 473 |
|
| 474 | md_run_setup();
|
| 475 |
|
| 476 | if (saved_root_name[0])
|
| 477 | ROOT_DEV = parse_root_device(saved_root_name);
|
| 478 |
|
| 479 | initrd_load();
|
| 480 |
|
| 481 | if (root_wait)
|
| 482 | wait_for_root(saved_root_name);
|
| 483 | mount_root(saved_root_name);
|
| 484 | devtmpfs_mount();
|
| 485 |
|
| 486 | if (init_pivot_root(".", ".")) {
|
| 487 | pr_err("VFS: Failed to pivot into new rootfs\n");
|
| 488 | return;
|
| 489 | }
|
| 490 | if (init_umount(".", MNT_DETACH)) {
|
| 491 | pr_err("VFS: Failed to unmount old rootfs\n");
|
| 492 | return;
|
| 493 | }
|
| 494 | pr_info("VFS: Pivoted into new rootfs\n");
|
| 495 | }
|
| 496 |
|
| 497 | static bool is_tmpfs;
|
| 498 | static int rootfs_init_fs_context(struct fs_context *fc)
|
| 499 | {
|
| 500 | if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
|
| 501 | return shmem_init_fs_context(fc);
|
| 502 |
|
| 503 | return ramfs_init_fs_context(fc);
|
| 504 | }
|
| 505 |
|
| 506 | struct file_system_type rootfs_fs_type = {
|
| 507 | .name = "rootfs",
|
| 508 | .init_fs_context = rootfs_init_fs_context,
|
| 509 | .kill_sb = kill_anon_super,
|
| 510 | };
|
| 511 |
|
| 512 | void __init init_rootfs(void)
|
| 513 | {
|
| 514 | if (IS_ENABLED(CONFIG_TMPFS)) {
|
| 515 | if (!saved_root_name[0] && !root_fs_names)
|
| 516 | is_tmpfs = true;
|
| 517 | else if (root_fs_names && !!strstr(root_fs_names, "tmpfs"))
|
| 518 | is_tmpfs = true;
|
| 519 | }
|
| 520 | }
|