Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/sched.h> |
| 3 | #include <linux/ctype.h> |
| 4 | #include <linux/fd.h> |
| 5 | #include <linux/tty.h> |
| 6 | #include <linux/suspend.h> |
| 7 | #include <linux/root_dev.h> |
| 8 | #include <linux/security.h> |
| 9 | #include <linux/delay.h> |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 10 | #include <linux/genhd.h> |
Andrew Morton | d53d9f1 | 2005-07-12 13:58:07 -0700 | [diff] [blame] | 11 | #include <linux/mount.h> |
Greg Kroah-Hartman | d779249 | 2006-07-18 10:59:59 -0700 | [diff] [blame] | 12 | #include <linux/device.h> |
Adrian Bunk | 4659539 | 2007-05-08 00:24:47 -0700 | [diff] [blame] | 13 | #include <linux/init.h> |
Adrian Bunk | 011e3fc | 2008-02-06 01:36:47 -0800 | [diff] [blame] | 14 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include <linux/nfs_fs.h> |
| 17 | #include <linux/nfs_fs_sb.h> |
| 18 | #include <linux/nfs_mount.h> |
| 19 | |
| 20 | #include "do_mounts.h" |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | int __initdata rd_doload; /* 1 = load RAM disk, 0 = don't load */ |
| 23 | |
Theodore Ts'o | 9b04c99 | 2006-03-24 03:15:10 -0800 | [diff] [blame] | 24 | int root_mountflags = MS_RDONLY | MS_SILENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | char * __initdata root_device_name; |
| 26 | static char __initdata saved_root_name[64]; |
Pierre Ossman | cc1ed75 | 2007-07-15 23:40:35 -0700 | [diff] [blame] | 27 | static int __initdata root_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | dev_t ROOT_DEV; |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | static int __init load_ramdisk(char *str) |
| 32 | { |
| 33 | rd_doload = simple_strtol(str,NULL,0) & 3; |
| 34 | return 1; |
| 35 | } |
| 36 | __setup("load_ramdisk=", load_ramdisk); |
| 37 | |
| 38 | static int __init readonly(char *str) |
| 39 | { |
| 40 | if (*str) |
| 41 | return 0; |
| 42 | root_mountflags |= MS_RDONLY; |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | static int __init readwrite(char *str) |
| 47 | { |
| 48 | if (*str) |
| 49 | return 0; |
| 50 | root_mountflags &= ~MS_RDONLY; |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | __setup("ro", readonly); |
| 55 | __setup("rw", readwrite); |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Convert a name into device number. We accept the following variants: |
| 59 | * |
| 60 | * 1) device number in hexadecimal represents itself |
| 61 | * 2) /dev/nfs represents Root_NFS (0xff) |
| 62 | * 3) /dev/<disk_name> represents the device number of disk |
| 63 | * 4) /dev/<disk_name><decimal> represents the device number |
| 64 | * of partition - device number of disk plus the partition number |
| 65 | * 5) /dev/<disk_name>p<decimal> - same as the above, that form is |
| 66 | * used when disk name of partitioned disk ends on a digit. |
| 67 | * |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 68 | * If name doesn't have fall into the categories above, we return (0,0). |
| 69 | * block_class is used to check if something is a disk name. If the disk |
| 70 | * name contains slashes, the device name has them replaced with |
| 71 | * bangs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | */ |
| 73 | |
| 74 | dev_t name_to_dev_t(char *name) |
| 75 | { |
| 76 | char s[32]; |
| 77 | char *p; |
| 78 | dev_t res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | if (strncmp(name, "/dev/", 5) != 0) { |
| 81 | unsigned maj, min; |
| 82 | |
| 83 | if (sscanf(name, "%u:%u", &maj, &min) == 2) { |
| 84 | res = MKDEV(maj, min); |
| 85 | if (maj != MAJOR(res) || min != MINOR(res)) |
| 86 | goto fail; |
| 87 | } else { |
| 88 | res = new_decode_dev(simple_strtoul(name, &p, 16)); |
| 89 | if (*p) |
| 90 | goto fail; |
| 91 | } |
| 92 | goto done; |
| 93 | } |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | name += 5; |
| 96 | res = Root_NFS; |
| 97 | if (strcmp(name, "nfs") == 0) |
| 98 | goto done; |
| 99 | res = Root_RAM0; |
| 100 | if (strcmp(name, "ram") == 0) |
| 101 | goto done; |
| 102 | |
| 103 | if (strlen(name) > 31) |
| 104 | goto fail; |
| 105 | strcpy(s, name); |
| 106 | for (p = s; *p; p++) |
| 107 | if (*p == '/') |
| 108 | *p = '!'; |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 109 | res = blk_lookup_devt(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | if (res) |
| 111 | goto done; |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | fail: |
Kay Sievers | edfaa7c | 2007-05-21 22:08:01 +0200 | [diff] [blame] | 114 | return 0; |
| 115 | done: |
| 116 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int __init root_dev_setup(char *line) |
| 120 | { |
| 121 | strlcpy(saved_root_name, line, sizeof(saved_root_name)); |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | __setup("root=", root_dev_setup); |
| 126 | |
Pierre Ossman | cc1ed75 | 2007-07-15 23:40:35 -0700 | [diff] [blame] | 127 | static int __init rootwait_setup(char *str) |
| 128 | { |
| 129 | if (*str) |
| 130 | return 0; |
| 131 | root_wait = 1; |
| 132 | return 1; |
| 133 | } |
| 134 | |
| 135 | __setup("rootwait", rootwait_setup); |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | static char * __initdata root_mount_data; |
| 138 | static int __init root_data_setup(char *str) |
| 139 | { |
| 140 | root_mount_data = str; |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | static char * __initdata root_fs_names; |
| 145 | static int __init fs_names_setup(char *str) |
| 146 | { |
| 147 | root_fs_names = str; |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | static unsigned int __initdata root_delay; |
| 152 | static int __init root_delay_setup(char *str) |
| 153 | { |
| 154 | root_delay = simple_strtoul(str, NULL, 0); |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | __setup("rootflags=", root_data_setup); |
| 159 | __setup("rootfstype=", fs_names_setup); |
| 160 | __setup("rootdelay=", root_delay_setup); |
| 161 | |
| 162 | static void __init get_fs_names(char *page) |
| 163 | { |
| 164 | char *s = page; |
| 165 | |
| 166 | if (root_fs_names) { |
| 167 | strcpy(page, root_fs_names); |
| 168 | while (*s++) { |
| 169 | if (s[-1] == ',') |
| 170 | s[-1] = '\0'; |
| 171 | } |
| 172 | } else { |
| 173 | int len = get_filesystem_list(page); |
| 174 | char *p, *next; |
| 175 | |
| 176 | page[len] = '\0'; |
| 177 | for (p = page-1; p; p = next) { |
| 178 | next = strchr(++p, '\n'); |
| 179 | if (*p++ != '\t') |
| 180 | continue; |
| 181 | while ((*s++ = *p++) != '\n') |
| 182 | ; |
| 183 | s[-1] = '\0'; |
| 184 | } |
| 185 | } |
| 186 | *s = '\0'; |
| 187 | } |
| 188 | |
| 189 | static int __init do_mount_root(char *name, char *fs, int flags, void *data) |
| 190 | { |
| 191 | int err = sys_mount(name, "/root", fs, flags, data); |
| 192 | if (err) |
| 193 | return err; |
| 194 | |
| 195 | sys_chdir("/root"); |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 196 | ROOT_DEV = current->fs->pwd.mnt->mnt_sb->s_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | printk("VFS: Mounted root (%s filesystem)%s.\n", |
Jan Blunck | 6ac08c3 | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 198 | current->fs->pwd.mnt->mnt_sb->s_type->name, |
| 199 | current->fs->pwd.mnt->mnt_sb->s_flags & MS_RDONLY ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | " readonly" : ""); |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | void __init mount_block_root(char *name, int flags) |
| 205 | { |
| 206 | char *fs_names = __getname(); |
| 207 | char *p; |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 208 | #ifdef CONFIG_BLOCK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | char b[BDEVNAME_SIZE]; |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 210 | #else |
| 211 | const char *b = name; |
| 212 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | get_fs_names(fs_names); |
| 215 | retry: |
| 216 | for (p = fs_names; *p; p += strlen(p)+1) { |
| 217 | int err = do_mount_root(name, p, flags, root_mount_data); |
| 218 | switch (err) { |
| 219 | case 0: |
| 220 | goto out; |
| 221 | case -EACCES: |
| 222 | flags |= MS_RDONLY; |
| 223 | goto retry; |
| 224 | case -EINVAL: |
| 225 | continue; |
| 226 | } |
| 227 | /* |
| 228 | * Allow the user to distinguish between failed sys_open |
| 229 | * and bad superblock on root device. |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 230 | * and give them a list of the available devices |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | */ |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 232 | #ifdef CONFIG_BLOCK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | __bdevname(ROOT_DEV, b); |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 234 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | printk("VFS: Cannot open root device \"%s\" or %s\n", |
| 236 | root_device_name, b); |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 237 | printk("Please append a correct \"root=\" boot option; here are the available partitions:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 239 | printk_all_partitions(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | panic("VFS: Unable to mount root fs on %s", b); |
| 241 | } |
Andy Whitcroft | be6e028b | 2006-05-15 09:44:29 -0700 | [diff] [blame] | 242 | |
Dave Gilbert | dd2a345 | 2007-05-09 02:33:24 -0700 | [diff] [blame] | 243 | printk("List of all partitions:\n"); |
| 244 | printk_all_partitions(); |
Andy Whitcroft | be6e028b | 2006-05-15 09:44:29 -0700 | [diff] [blame] | 245 | printk("No filesystem could mount root, tried: "); |
| 246 | for (p = fs_names; *p; p += strlen(p)+1) |
| 247 | printk(" %s", p); |
| 248 | printk("\n"); |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 249 | #ifdef CONFIG_BLOCK |
| 250 | __bdevname(ROOT_DEV, b); |
| 251 | #endif |
| 252 | panic("VFS: Unable to mount root fs on %s", b); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | out: |
| 254 | putname(fs_names); |
| 255 | } |
| 256 | |
| 257 | #ifdef CONFIG_ROOT_NFS |
| 258 | static int __init mount_nfs_root(void) |
| 259 | { |
| 260 | void *data = nfs_root_data(); |
| 261 | |
Greg Kroah-Hartman | bdaf852 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 262 | create_dev("/dev/root", ROOT_DEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | if (data && |
| 264 | do_mount_root("/dev/root", "nfs", root_mountflags, data) == 0) |
| 265 | return 1; |
| 266 | return 0; |
| 267 | } |
| 268 | #endif |
| 269 | |
| 270 | #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD) |
| 271 | void __init change_floppy(char *fmt, ...) |
| 272 | { |
| 273 | struct termios termios; |
| 274 | char buf[80]; |
| 275 | char c; |
| 276 | int fd; |
| 277 | va_list args; |
| 278 | va_start(args, fmt); |
| 279 | vsprintf(buf, fmt, args); |
| 280 | va_end(args); |
| 281 | fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0); |
| 282 | if (fd >= 0) { |
| 283 | sys_ioctl(fd, FDEJECT, 0); |
| 284 | sys_close(fd); |
| 285 | } |
| 286 | printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf); |
| 287 | fd = sys_open("/dev/console", O_RDWR, 0); |
| 288 | if (fd >= 0) { |
| 289 | sys_ioctl(fd, TCGETS, (long)&termios); |
| 290 | termios.c_lflag &= ~ICANON; |
| 291 | sys_ioctl(fd, TCSETSF, (long)&termios); |
| 292 | sys_read(fd, &c, 1); |
| 293 | termios.c_lflag |= ICANON; |
| 294 | sys_ioctl(fd, TCSETSF, (long)&termios); |
| 295 | sys_close(fd); |
| 296 | } |
| 297 | } |
| 298 | #endif |
| 299 | |
| 300 | void __init mount_root(void) |
| 301 | { |
| 302 | #ifdef CONFIG_ROOT_NFS |
| 303 | if (MAJOR(ROOT_DEV) == UNNAMED_MAJOR) { |
| 304 | if (mount_nfs_root()) |
| 305 | return; |
| 306 | |
| 307 | printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n"); |
| 308 | ROOT_DEV = Root_FD0; |
| 309 | } |
| 310 | #endif |
| 311 | #ifdef CONFIG_BLK_DEV_FD |
| 312 | if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) { |
| 313 | /* rd_doload is 2 for a dual initrd/ramload setup */ |
| 314 | if (rd_doload==2) { |
| 315 | if (rd_load_disk(1)) { |
| 316 | ROOT_DEV = Root_RAM1; |
| 317 | root_device_name = NULL; |
| 318 | } |
| 319 | } else |
| 320 | change_floppy("root floppy"); |
| 321 | } |
| 322 | #endif |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 323 | #ifdef CONFIG_BLOCK |
Greg Kroah-Hartman | bdaf852 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 324 | create_dev("/dev/root", ROOT_DEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | mount_block_root("/dev/root", root_mountflags); |
David Howells | 9361401 | 2006-09-30 20:45:40 +0200 | [diff] [blame] | 326 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Prepare the namespace - decide what/where to mount, load ramdisks, etc. |
| 331 | */ |
| 332 | void __init prepare_namespace(void) |
| 333 | { |
| 334 | int is_floppy; |
| 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | if (root_delay) { |
| 337 | printk(KERN_INFO "Waiting %dsec before mounting root device...\n", |
| 338 | root_delay); |
| 339 | ssleep(root_delay); |
| 340 | } |
| 341 | |
Greg Kroah-Hartman | d779249 | 2006-07-18 10:59:59 -0700 | [diff] [blame] | 342 | /* wait for the known devices to complete their probing */ |
| 343 | while (driver_probe_done() != 0) |
| 344 | msleep(100); |
| 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | md_run_setup(); |
| 347 | |
| 348 | if (saved_root_name[0]) { |
| 349 | root_device_name = saved_root_name; |
Joern Engel | e9482b4 | 2006-05-30 14:25:46 +0200 | [diff] [blame] | 350 | if (!strncmp(root_device_name, "mtd", 3)) { |
| 351 | mount_block_root(root_device_name, root_mountflags); |
| 352 | goto out; |
| 353 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | ROOT_DEV = name_to_dev_t(root_device_name); |
| 355 | if (strncmp(root_device_name, "/dev/", 5) == 0) |
| 356 | root_device_name += 5; |
| 357 | } |
| 358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | if (initrd_load()) |
| 360 | goto out; |
| 361 | |
Pierre Ossman | cc1ed75 | 2007-07-15 23:40:35 -0700 | [diff] [blame] | 362 | /* wait for any asynchronous scanning to complete */ |
| 363 | if ((ROOT_DEV == 0) && root_wait) { |
| 364 | printk(KERN_INFO "Waiting for root device %s...\n", |
| 365 | saved_root_name); |
| 366 | while (driver_probe_done() != 0 || |
| 367 | (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0) |
| 368 | msleep(100); |
| 369 | } |
| 370 | |
| 371 | is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR; |
| 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | if (is_floppy && rd_doload && rd_load_disk(0)) |
| 374 | ROOT_DEV = Root_RAM0; |
| 375 | |
| 376 | mount_root(); |
| 377 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | sys_mount(".", "/", NULL, MS_MOVE, NULL); |
| 379 | sys_chroot("."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |