1c67e5382SH Hartley Sweeten /* 2c67e5382SH Hartley Sweeten * Many of the syscalls used in this file expect some of the arguments 3c67e5382SH Hartley Sweeten * to be __user pointers not __kernel pointers. To limit the sparse 4c67e5382SH Hartley Sweeten * noise, turn off sparse checking for this file. 5c67e5382SH Hartley Sweeten */ 6c67e5382SH Hartley Sweeten #ifdef __CHECKER__ 7c67e5382SH Hartley Sweeten #undef __CHECKER__ 8c67e5382SH Hartley Sweeten #warning "Sparse checking disabled for this file" 9c67e5382SH Hartley Sweeten #endif 10c67e5382SH Hartley Sweeten 111da177e4SLinus Torvalds #include <linux/module.h> 121da177e4SLinus Torvalds #include <linux/sched.h> 131da177e4SLinus Torvalds #include <linux/ctype.h> 141da177e4SLinus Torvalds #include <linux/fd.h> 151da177e4SLinus Torvalds #include <linux/tty.h> 161da177e4SLinus Torvalds #include <linux/suspend.h> 171da177e4SLinus Torvalds #include <linux/root_dev.h> 181da177e4SLinus Torvalds #include <linux/security.h> 191da177e4SLinus Torvalds #include <linux/delay.h> 20dd2a345fSDave Gilbert #include <linux/genhd.h> 21d53d9f16SAndrew Morton #include <linux/mount.h> 22d779249eSGreg Kroah-Hartman #include <linux/device.h> 2346595390SAdrian Bunk #include <linux/init.h> 24011e3fcdSAdrian Bunk #include <linux/fs.h> 2582c8253aSAdrian Bunk #include <linux/initrd.h> 2622a9d645SArjan van de Ven #include <linux/async.h> 275ad4e53bSAl Viro #include <linux/fs_struct.h> 285a0e3ad6STejun Heo #include <linux/slab.h> 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #include <linux/nfs_fs.h> 311da177e4SLinus Torvalds #include <linux/nfs_fs_sb.h> 321da177e4SLinus Torvalds #include <linux/nfs_mount.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #include "do_mounts.h" 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds int __initdata rd_doload; /* 1 = load RAM disk, 0 = don't load */ 371da177e4SLinus Torvalds 389b04c997STheodore Ts'o int root_mountflags = MS_RDONLY | MS_SILENT; 39f56f6d30SAdrian Bunk static char * __initdata root_device_name; 401da177e4SLinus Torvalds static char __initdata saved_root_name[64]; 4179975f13SWill Drewry static int root_wait; 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds dev_t ROOT_DEV; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds static int __init load_ramdisk(char *str) 461da177e4SLinus Torvalds { 471da177e4SLinus Torvalds rd_doload = simple_strtol(str,NULL,0) & 3; 481da177e4SLinus Torvalds return 1; 491da177e4SLinus Torvalds } 501da177e4SLinus Torvalds __setup("load_ramdisk=", load_ramdisk); 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds static int __init readonly(char *str) 531da177e4SLinus Torvalds { 541da177e4SLinus Torvalds if (*str) 551da177e4SLinus Torvalds return 0; 561da177e4SLinus Torvalds root_mountflags |= MS_RDONLY; 571da177e4SLinus Torvalds return 1; 581da177e4SLinus Torvalds } 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds static int __init readwrite(char *str) 611da177e4SLinus Torvalds { 621da177e4SLinus Torvalds if (*str) 631da177e4SLinus Torvalds return 0; 641da177e4SLinus Torvalds root_mountflags &= ~MS_RDONLY; 651da177e4SLinus Torvalds return 1; 661da177e4SLinus Torvalds } 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds __setup("ro", readonly); 691da177e4SLinus Torvalds __setup("rw", readwrite); 701da177e4SLinus Torvalds 716d0aed7aSJens Axboe #ifdef CONFIG_BLOCK 72b5af921eSWill Drewry /** 73b5af921eSWill Drewry * match_dev_by_uuid - callback for finding a partition using its uuid 74b5af921eSWill Drewry * @dev: device passed in by the caller 75b5af921eSWill Drewry * @data: opaque pointer to a 36 byte char array with a UUID 76b5af921eSWill Drewry * 77b5af921eSWill Drewry * Returns 1 if the device matches, and 0 otherwise. 78b5af921eSWill Drewry */ 7938b6f45aSJens Axboe static int match_dev_by_uuid(struct device *dev, void *data) 80b5af921eSWill Drewry { 81b5af921eSWill Drewry u8 *uuid = data; 82b5af921eSWill Drewry struct hd_struct *part = dev_to_part(dev); 83b5af921eSWill Drewry 84b5af921eSWill Drewry if (!part->info) 85b5af921eSWill Drewry goto no_match; 86b5af921eSWill Drewry 87b5af921eSWill Drewry if (memcmp(uuid, part->info->uuid, sizeof(part->info->uuid))) 88b5af921eSWill Drewry goto no_match; 89b5af921eSWill Drewry 90b5af921eSWill Drewry return 1; 91b5af921eSWill Drewry no_match: 92b5af921eSWill Drewry return 0; 93b5af921eSWill Drewry } 94b5af921eSWill Drewry 95b5af921eSWill Drewry 96b5af921eSWill Drewry /** 97b5af921eSWill Drewry * devt_from_partuuid - looks up the dev_t of a partition by its UUID 9879975f13SWill Drewry * @uuid: min 36 byte char array containing a hex ascii UUID 99b5af921eSWill Drewry * 100b5af921eSWill Drewry * The function will return the first partition which contains a matching 101b5af921eSWill Drewry * UUID value in its partition_meta_info struct. This does not search 102b5af921eSWill Drewry * by filesystem UUIDs. 103b5af921eSWill Drewry * 10479975f13SWill Drewry * If @uuid is followed by a "/PARTNROFF=%d", then the number will be 10579975f13SWill Drewry * extracted and used as an offset from the partition identified by the UUID. 10679975f13SWill Drewry * 107b5af921eSWill Drewry * Returns the matching dev_t on success or 0 on failure. 108b5af921eSWill Drewry */ 109a1cf11d8SJan Beulich static dev_t devt_from_partuuid(char *uuid_str) 110b5af921eSWill Drewry { 111b5af921eSWill Drewry dev_t res = 0; 112b5af921eSWill Drewry struct device *dev = NULL; 113b5af921eSWill Drewry u8 uuid[16]; 11479975f13SWill Drewry struct gendisk *disk; 11579975f13SWill Drewry struct hd_struct *part; 11679975f13SWill Drewry int offset = 0; 11779975f13SWill Drewry 11879975f13SWill Drewry if (strlen(uuid_str) < 36) 11979975f13SWill Drewry goto done; 12079975f13SWill Drewry 12179975f13SWill Drewry /* Check for optional partition number offset attributes. */ 12279975f13SWill Drewry if (uuid_str[36]) { 12379975f13SWill Drewry char c = 0; 12479975f13SWill Drewry /* Explicitly fail on poor PARTUUID syntax. */ 12579975f13SWill Drewry if (sscanf(&uuid_str[36], 12679975f13SWill Drewry "/PARTNROFF=%d%c", &offset, &c) != 1) { 12779975f13SWill Drewry printk(KERN_ERR "VFS: PARTUUID= is invalid.\n" 12879975f13SWill Drewry "Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n"); 12979975f13SWill Drewry if (root_wait) 13079975f13SWill Drewry printk(KERN_ERR 13179975f13SWill Drewry "Disabling rootwait; root= is invalid.\n"); 13279975f13SWill Drewry root_wait = 0; 13379975f13SWill Drewry goto done; 13479975f13SWill Drewry } 13579975f13SWill Drewry } 136b5af921eSWill Drewry 137b5af921eSWill Drewry /* Pack the requested UUID in the expected format. */ 138b5af921eSWill Drewry part_pack_uuid(uuid_str, uuid); 139b5af921eSWill Drewry 140b5af921eSWill Drewry dev = class_find_device(&block_class, NULL, uuid, &match_dev_by_uuid); 141b5af921eSWill Drewry if (!dev) 142b5af921eSWill Drewry goto done; 143b5af921eSWill Drewry 144b5af921eSWill Drewry res = dev->devt; 145b5af921eSWill Drewry 14679975f13SWill Drewry /* Attempt to find the partition by offset. */ 14779975f13SWill Drewry if (!offset) 14879975f13SWill Drewry goto no_offset; 14979975f13SWill Drewry 15079975f13SWill Drewry res = 0; 15179975f13SWill Drewry disk = part_to_disk(dev_to_part(dev)); 15279975f13SWill Drewry part = disk_get_part(disk, dev_to_part(dev)->partno + offset); 15379975f13SWill Drewry if (part) { 15479975f13SWill Drewry res = part_devt(part); 15579975f13SWill Drewry put_device(part_to_dev(part)); 15679975f13SWill Drewry } 15779975f13SWill Drewry 15879975f13SWill Drewry no_offset: 15979975f13SWill Drewry put_device(dev); 160b5af921eSWill Drewry done: 161b5af921eSWill Drewry return res; 162b5af921eSWill Drewry } 1636d0aed7aSJens Axboe #endif 164b5af921eSWill Drewry 1651da177e4SLinus Torvalds /* 1661da177e4SLinus Torvalds * Convert a name into device number. We accept the following variants: 1671da177e4SLinus Torvalds * 1681da177e4SLinus Torvalds * 1) device number in hexadecimal represents itself 1691da177e4SLinus Torvalds * 2) /dev/nfs represents Root_NFS (0xff) 1701da177e4SLinus Torvalds * 3) /dev/<disk_name> represents the device number of disk 1711da177e4SLinus Torvalds * 4) /dev/<disk_name><decimal> represents the device number 1721da177e4SLinus Torvalds * of partition - device number of disk plus the partition number 1731da177e4SLinus Torvalds * 5) /dev/<disk_name>p<decimal> - same as the above, that form is 1741da177e4SLinus Torvalds * used when disk name of partitioned disk ends on a digit. 175b5af921eSWill Drewry * 6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the 176b5af921eSWill Drewry * unique id of a partition if the partition table provides it. 17779975f13SWill Drewry * 7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to 17879975f13SWill Drewry * a partition with a known unique id. 1791da177e4SLinus Torvalds * 180edfaa7c3SKay Sievers * If name doesn't have fall into the categories above, we return (0,0). 181edfaa7c3SKay Sievers * block_class is used to check if something is a disk name. If the disk 182edfaa7c3SKay Sievers * name contains slashes, the device name has them replaced with 183edfaa7c3SKay Sievers * bangs. 1841da177e4SLinus Torvalds */ 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds dev_t name_to_dev_t(char *name) 1871da177e4SLinus Torvalds { 1881da177e4SLinus Torvalds char s[32]; 1891da177e4SLinus Torvalds char *p; 1901da177e4SLinus Torvalds dev_t res = 0; 19130f2f0ebSKay Sievers int part; 1921da177e4SLinus Torvalds 1936d0aed7aSJens Axboe #ifdef CONFIG_BLOCK 194b5af921eSWill Drewry if (strncmp(name, "PARTUUID=", 9) == 0) { 195b5af921eSWill Drewry name += 9; 196b5af921eSWill Drewry res = devt_from_partuuid(name); 197b5af921eSWill Drewry if (!res) 198b5af921eSWill Drewry goto fail; 199b5af921eSWill Drewry goto done; 200b5af921eSWill Drewry } 2016d0aed7aSJens Axboe #endif 202b5af921eSWill Drewry 2031da177e4SLinus Torvalds if (strncmp(name, "/dev/", 5) != 0) { 2041da177e4SLinus Torvalds unsigned maj, min; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds if (sscanf(name, "%u:%u", &maj, &min) == 2) { 2071da177e4SLinus Torvalds res = MKDEV(maj, min); 2081da177e4SLinus Torvalds if (maj != MAJOR(res) || min != MINOR(res)) 2091da177e4SLinus Torvalds goto fail; 2101da177e4SLinus Torvalds } else { 2111da177e4SLinus Torvalds res = new_decode_dev(simple_strtoul(name, &p, 16)); 2121da177e4SLinus Torvalds if (*p) 2131da177e4SLinus Torvalds goto fail; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds goto done; 2161da177e4SLinus Torvalds } 217edfaa7c3SKay Sievers 2181da177e4SLinus Torvalds name += 5; 2191da177e4SLinus Torvalds res = Root_NFS; 2201da177e4SLinus Torvalds if (strcmp(name, "nfs") == 0) 2211da177e4SLinus Torvalds goto done; 2221da177e4SLinus Torvalds res = Root_RAM0; 2231da177e4SLinus Torvalds if (strcmp(name, "ram") == 0) 2241da177e4SLinus Torvalds goto done; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds if (strlen(name) > 31) 2271da177e4SLinus Torvalds goto fail; 2281da177e4SLinus Torvalds strcpy(s, name); 2291da177e4SLinus Torvalds for (p = s; *p; p++) 2301da177e4SLinus Torvalds if (*p == '/') 2311da177e4SLinus Torvalds *p = '!'; 23230f2f0ebSKay Sievers res = blk_lookup_devt(s, 0); 23330f2f0ebSKay Sievers if (res) 23430f2f0ebSKay Sievers goto done; 23530f2f0ebSKay Sievers 23630f2f0ebSKay Sievers /* 23725985edcSLucas De Marchi * try non-existent, but valid partition, which may only exist 23830f2f0ebSKay Sievers * after revalidating the disk, like partitioned md devices 23930f2f0ebSKay Sievers */ 24030f2f0ebSKay Sievers while (p > s && isdigit(p[-1])) 24130f2f0ebSKay Sievers p--; 24230f2f0ebSKay Sievers if (p == s || !*p || *p == '0') 24330f2f0ebSKay Sievers goto fail; 24430f2f0ebSKay Sievers 24530f2f0ebSKay Sievers /* try disk name without <part number> */ 24630f2f0ebSKay Sievers part = simple_strtoul(p, NULL, 10); 24730f2f0ebSKay Sievers *p = '\0'; 24830f2f0ebSKay Sievers res = blk_lookup_devt(s, part); 24930f2f0ebSKay Sievers if (res) 25030f2f0ebSKay Sievers goto done; 25130f2f0ebSKay Sievers 25230f2f0ebSKay Sievers /* try disk name without p<part number> */ 25330f2f0ebSKay Sievers if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p') 25430f2f0ebSKay Sievers goto fail; 25530f2f0ebSKay Sievers p[-1] = '\0'; 25630f2f0ebSKay Sievers res = blk_lookup_devt(s, part); 2571da177e4SLinus Torvalds if (res) 2581da177e4SLinus Torvalds goto done; 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds fail: 261edfaa7c3SKay Sievers return 0; 262edfaa7c3SKay Sievers done: 263edfaa7c3SKay Sievers return res; 2641da177e4SLinus Torvalds } 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds static int __init root_dev_setup(char *line) 2671da177e4SLinus Torvalds { 2681da177e4SLinus Torvalds strlcpy(saved_root_name, line, sizeof(saved_root_name)); 2691da177e4SLinus Torvalds return 1; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds __setup("root=", root_dev_setup); 2731da177e4SLinus Torvalds 274cc1ed754SPierre Ossman static int __init rootwait_setup(char *str) 275cc1ed754SPierre Ossman { 276cc1ed754SPierre Ossman if (*str) 277cc1ed754SPierre Ossman return 0; 278cc1ed754SPierre Ossman root_wait = 1; 279cc1ed754SPierre Ossman return 1; 280cc1ed754SPierre Ossman } 281cc1ed754SPierre Ossman 282cc1ed754SPierre Ossman __setup("rootwait", rootwait_setup); 283cc1ed754SPierre Ossman 2841da177e4SLinus Torvalds static char * __initdata root_mount_data; 2851da177e4SLinus Torvalds static int __init root_data_setup(char *str) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds root_mount_data = str; 2881da177e4SLinus Torvalds return 1; 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds static char * __initdata root_fs_names; 2921da177e4SLinus Torvalds static int __init fs_names_setup(char *str) 2931da177e4SLinus Torvalds { 2941da177e4SLinus Torvalds root_fs_names = str; 2951da177e4SLinus Torvalds return 1; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds static unsigned int __initdata root_delay; 2991da177e4SLinus Torvalds static int __init root_delay_setup(char *str) 3001da177e4SLinus Torvalds { 3011da177e4SLinus Torvalds root_delay = simple_strtoul(str, NULL, 0); 3021da177e4SLinus Torvalds return 1; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds __setup("rootflags=", root_data_setup); 3061da177e4SLinus Torvalds __setup("rootfstype=", fs_names_setup); 3071da177e4SLinus Torvalds __setup("rootdelay=", root_delay_setup); 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds static void __init get_fs_names(char *page) 3101da177e4SLinus Torvalds { 3111da177e4SLinus Torvalds char *s = page; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds if (root_fs_names) { 3141da177e4SLinus Torvalds strcpy(page, root_fs_names); 3151da177e4SLinus Torvalds while (*s++) { 3161da177e4SLinus Torvalds if (s[-1] == ',') 3171da177e4SLinus Torvalds s[-1] = '\0'; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds } else { 3201da177e4SLinus Torvalds int len = get_filesystem_list(page); 3211da177e4SLinus Torvalds char *p, *next; 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds page[len] = '\0'; 3241da177e4SLinus Torvalds for (p = page-1; p; p = next) { 3251da177e4SLinus Torvalds next = strchr(++p, '\n'); 3261da177e4SLinus Torvalds if (*p++ != '\t') 3271da177e4SLinus Torvalds continue; 3281da177e4SLinus Torvalds while ((*s++ = *p++) != '\n') 3291da177e4SLinus Torvalds ; 3301da177e4SLinus Torvalds s[-1] = '\0'; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds *s = '\0'; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds static int __init do_mount_root(char *name, char *fs, int flags, void *data) 3371da177e4SLinus Torvalds { 338d8c9584eSAl Viro struct super_block *s; 3391da177e4SLinus Torvalds int err = sys_mount(name, "/root", fs, flags, data); 3401da177e4SLinus Torvalds if (err) 3411da177e4SLinus Torvalds return err; 3421da177e4SLinus Torvalds 343c67e5382SH Hartley Sweeten sys_chdir("/root"); 344d8c9584eSAl Viro s = current->fs->pwd.dentry->d_sb; 345d8c9584eSAl Viro ROOT_DEV = s->s_dev; 34680cdc6daSMandeep Singh Baines printk(KERN_INFO 34780cdc6daSMandeep Singh Baines "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n", 348d8c9584eSAl Viro s->s_type->name, 349d8c9584eSAl Viro s->s_flags & MS_RDONLY ? " readonly" : "", 350d8c9584eSAl Viro MAJOR(ROOT_DEV), MINOR(ROOT_DEV)); 3511da177e4SLinus Torvalds return 0; 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds void __init mount_block_root(char *name, int flags) 3551da177e4SLinus Torvalds { 3563b5c760eSVegard Nossum char *fs_names = __getname_gfp(GFP_KERNEL 3573b5c760eSVegard Nossum | __GFP_NOTRACK_FALSE_POSITIVE); 3581da177e4SLinus Torvalds char *p; 3599361401eSDavid Howells #ifdef CONFIG_BLOCK 3601da177e4SLinus Torvalds char b[BDEVNAME_SIZE]; 3619361401eSDavid Howells #else 3629361401eSDavid Howells const char *b = name; 3639361401eSDavid Howells #endif 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds get_fs_names(fs_names); 3661da177e4SLinus Torvalds retry: 3671da177e4SLinus Torvalds for (p = fs_names; *p; p += strlen(p)+1) { 3681da177e4SLinus Torvalds int err = do_mount_root(name, p, flags, root_mount_data); 3691da177e4SLinus Torvalds switch (err) { 3701da177e4SLinus Torvalds case 0: 3711da177e4SLinus Torvalds goto out; 3721da177e4SLinus Torvalds case -EACCES: 3731da177e4SLinus Torvalds flags |= MS_RDONLY; 3741da177e4SLinus Torvalds goto retry; 3751da177e4SLinus Torvalds case -EINVAL: 3761da177e4SLinus Torvalds continue; 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds /* 3791da177e4SLinus Torvalds * Allow the user to distinguish between failed sys_open 3801da177e4SLinus Torvalds * and bad superblock on root device. 381dd2a345fSDave Gilbert * and give them a list of the available devices 3821da177e4SLinus Torvalds */ 3839361401eSDavid Howells #ifdef CONFIG_BLOCK 3841da177e4SLinus Torvalds __bdevname(ROOT_DEV, b); 3859361401eSDavid Howells #endif 3860e0cb892SBernhard Walle printk("VFS: Cannot open root device \"%s\" or %s: error %d\n", 3870e0cb892SBernhard Walle root_device_name, b, err); 388dd2a345fSDave Gilbert printk("Please append a correct \"root=\" boot option; here are the available partitions:\n"); 3891da177e4SLinus Torvalds 390dd2a345fSDave Gilbert printk_all_partitions(); 39155dc7db7STejun Heo #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT 39255dc7db7STejun Heo printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify " 39355dc7db7STejun Heo "explicit textual name for \"root=\" boot option.\n"); 39455dc7db7STejun Heo #endif 3951da177e4SLinus Torvalds panic("VFS: Unable to mount root fs on %s", b); 3961da177e4SLinus Torvalds } 397be6e028bSAndy Whitcroft 398dd2a345fSDave Gilbert printk("List of all partitions:\n"); 399dd2a345fSDave Gilbert printk_all_partitions(); 400be6e028bSAndy Whitcroft printk("No filesystem could mount root, tried: "); 401be6e028bSAndy Whitcroft for (p = fs_names; *p; p += strlen(p)+1) 402be6e028bSAndy Whitcroft printk(" %s", p); 403be6e028bSAndy Whitcroft printk("\n"); 4049361401eSDavid Howells #ifdef CONFIG_BLOCK 4059361401eSDavid Howells __bdevname(ROOT_DEV, b); 4069361401eSDavid Howells #endif 4079361401eSDavid Howells panic("VFS: Unable to mount root fs on %s", b); 4081da177e4SLinus Torvalds out: 4091da177e4SLinus Torvalds putname(fs_names); 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS 41343717c7dSChuck Lever 41443717c7dSChuck Lever #define NFSROOT_TIMEOUT_MIN 5 41543717c7dSChuck Lever #define NFSROOT_TIMEOUT_MAX 30 41643717c7dSChuck Lever #define NFSROOT_RETRY_MAX 5 41743717c7dSChuck Lever 4181da177e4SLinus Torvalds static int __init mount_nfs_root(void) 4191da177e4SLinus Torvalds { 42056463e50SChuck Lever char *root_dev, *root_data; 42143717c7dSChuck Lever unsigned int timeout; 42243717c7dSChuck Lever int try, err; 4231da177e4SLinus Torvalds 42443717c7dSChuck Lever err = nfs_root_data(&root_dev, &root_data); 42543717c7dSChuck Lever if (err != 0) 4261da177e4SLinus Torvalds return 0; 42743717c7dSChuck Lever 42843717c7dSChuck Lever /* 42943717c7dSChuck Lever * The server or network may not be ready, so try several 43043717c7dSChuck Lever * times. Stop after a few tries in case the client wants 43143717c7dSChuck Lever * to fall back to other boot methods. 43243717c7dSChuck Lever */ 43343717c7dSChuck Lever timeout = NFSROOT_TIMEOUT_MIN; 43443717c7dSChuck Lever for (try = 1; ; try++) { 43543717c7dSChuck Lever err = do_mount_root(root_dev, "nfs", 43643717c7dSChuck Lever root_mountflags, root_data); 43743717c7dSChuck Lever if (err == 0) 43856463e50SChuck Lever return 1; 43943717c7dSChuck Lever if (try > NFSROOT_RETRY_MAX) 44043717c7dSChuck Lever break; 44143717c7dSChuck Lever 44243717c7dSChuck Lever /* Wait, in case the server refused us immediately */ 44343717c7dSChuck Lever ssleep(timeout); 44443717c7dSChuck Lever timeout <<= 1; 44543717c7dSChuck Lever if (timeout > NFSROOT_TIMEOUT_MAX) 44643717c7dSChuck Lever timeout = NFSROOT_TIMEOUT_MAX; 44743717c7dSChuck Lever } 44843717c7dSChuck Lever return 0; 4491da177e4SLinus Torvalds } 4501da177e4SLinus Torvalds #endif 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD) 4531da177e4SLinus Torvalds void __init change_floppy(char *fmt, ...) 4541da177e4SLinus Torvalds { 4551da177e4SLinus Torvalds struct termios termios; 4561da177e4SLinus Torvalds char buf[80]; 4571da177e4SLinus Torvalds char c; 4581da177e4SLinus Torvalds int fd; 4591da177e4SLinus Torvalds va_list args; 4601da177e4SLinus Torvalds va_start(args, fmt); 4611da177e4SLinus Torvalds vsprintf(buf, fmt, args); 4621da177e4SLinus Torvalds va_end(args); 4631da177e4SLinus Torvalds fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0); 4641da177e4SLinus Torvalds if (fd >= 0) { 4651da177e4SLinus Torvalds sys_ioctl(fd, FDEJECT, 0); 4661da177e4SLinus Torvalds sys_close(fd); 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf); 4691da177e4SLinus Torvalds fd = sys_open("/dev/console", O_RDWR, 0); 4701da177e4SLinus Torvalds if (fd >= 0) { 4711da177e4SLinus Torvalds sys_ioctl(fd, TCGETS, (long)&termios); 4721da177e4SLinus Torvalds termios.c_lflag &= ~ICANON; 4731da177e4SLinus Torvalds sys_ioctl(fd, TCSETSF, (long)&termios); 4741da177e4SLinus Torvalds sys_read(fd, &c, 1); 4751da177e4SLinus Torvalds termios.c_lflag |= ICANON; 4761da177e4SLinus Torvalds sys_ioctl(fd, TCSETSF, (long)&termios); 4771da177e4SLinus Torvalds sys_close(fd); 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds #endif 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds void __init mount_root(void) 4831da177e4SLinus Torvalds { 4841da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS 485377485f6SSasha Levin if (ROOT_DEV == Root_NFS) { 4861da177e4SLinus Torvalds if (mount_nfs_root()) 4871da177e4SLinus Torvalds return; 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n"); 4901da177e4SLinus Torvalds ROOT_DEV = Root_FD0; 4911da177e4SLinus Torvalds } 4921da177e4SLinus Torvalds #endif 4931da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_FD 4941da177e4SLinus Torvalds if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) { 4951da177e4SLinus Torvalds /* rd_doload is 2 for a dual initrd/ramload setup */ 4961da177e4SLinus Torvalds if (rd_doload==2) { 4971da177e4SLinus Torvalds if (rd_load_disk(1)) { 4981da177e4SLinus Torvalds ROOT_DEV = Root_RAM1; 4991da177e4SLinus Torvalds root_device_name = NULL; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds } else 5021da177e4SLinus Torvalds change_floppy("root floppy"); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds #endif 5059361401eSDavid Howells #ifdef CONFIG_BLOCK 506bdaf8529SGreg Kroah-Hartman create_dev("/dev/root", ROOT_DEV); 5071da177e4SLinus Torvalds mount_block_root("/dev/root", root_mountflags); 5089361401eSDavid Howells #endif 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds /* 5121da177e4SLinus Torvalds * Prepare the namespace - decide what/where to mount, load ramdisks, etc. 5131da177e4SLinus Torvalds */ 5141da177e4SLinus Torvalds void __init prepare_namespace(void) 5151da177e4SLinus Torvalds { 5161da177e4SLinus Torvalds int is_floppy; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds if (root_delay) { 5191da177e4SLinus Torvalds printk(KERN_INFO "Waiting %dsec before mounting root device...\n", 5201da177e4SLinus Torvalds root_delay); 5211da177e4SLinus Torvalds ssleep(root_delay); 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 524216773a7SArjan van de Ven /* 525216773a7SArjan van de Ven * wait for the known devices to complete their probing 526216773a7SArjan van de Ven * 527216773a7SArjan van de Ven * Note: this is a potential source of long boot delays. 528216773a7SArjan van de Ven * For example, it is not atypical to wait 5 seconds here 529216773a7SArjan van de Ven * for the touchpad of a laptop to initialize. 530216773a7SArjan van de Ven */ 531216773a7SArjan van de Ven wait_for_device_probe(); 532d779249eSGreg Kroah-Hartman 5331da177e4SLinus Torvalds md_run_setup(); 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds if (saved_root_name[0]) { 5361da177e4SLinus Torvalds root_device_name = saved_root_name; 5372d62f488SAdrian Hunter if (!strncmp(root_device_name, "mtd", 3) || 5382d62f488SAdrian Hunter !strncmp(root_device_name, "ubi", 3)) { 539e9482b43SJoern Engel mount_block_root(root_device_name, root_mountflags); 540e9482b43SJoern Engel goto out; 541e9482b43SJoern Engel } 5421da177e4SLinus Torvalds ROOT_DEV = name_to_dev_t(root_device_name); 5431da177e4SLinus Torvalds if (strncmp(root_device_name, "/dev/", 5) == 0) 5441da177e4SLinus Torvalds root_device_name += 5; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds if (initrd_load()) 5481da177e4SLinus Torvalds goto out; 5491da177e4SLinus Torvalds 550cc1ed754SPierre Ossman /* wait for any asynchronous scanning to complete */ 551cc1ed754SPierre Ossman if ((ROOT_DEV == 0) && root_wait) { 552cc1ed754SPierre Ossman printk(KERN_INFO "Waiting for root device %s...\n", 553cc1ed754SPierre Ossman saved_root_name); 554cc1ed754SPierre Ossman while (driver_probe_done() != 0 || 555cc1ed754SPierre Ossman (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0) 556cc1ed754SPierre Ossman msleep(100); 557216773a7SArjan van de Ven async_synchronize_full(); 558cc1ed754SPierre Ossman } 559cc1ed754SPierre Ossman 560cc1ed754SPierre Ossman is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR; 561cc1ed754SPierre Ossman 5621da177e4SLinus Torvalds if (is_floppy && rd_doload && rd_load_disk(0)) 5631da177e4SLinus Torvalds ROOT_DEV = Root_RAM0; 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds mount_root(); 5661da177e4SLinus Torvalds out: 5672b2af54aSKay Sievers devtmpfs_mount("dev"); 5681da177e4SLinus Torvalds sys_mount(".", "/", NULL, MS_MOVE, NULL); 569c67e5382SH Hartley Sweeten sys_chroot("."); 5701da177e4SLinus Torvalds } 571