blob: f86573126f83fe76c3b87d85342786cb5a158204 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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 Gilbertdd2a3452007-05-09 02:33:24 -070010#include <linux/genhd.h>
Andrew Mortond53d9f12005-07-12 13:58:07 -070011#include <linux/mount.h>
Greg Kroah-Hartmand7792492006-07-18 10:59:59 -070012#include <linux/device.h>
Adrian Bunk46595392007-05-08 00:24:47 -070013#include <linux/init.h>
Adrian Bunk011e3fc2008-02-06 01:36:47 -080014#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
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 Torvalds1da177e2005-04-16 15:20:36 -070022int __initdata rd_doload; /* 1 = load RAM disk, 0 = don't load */
23
Theodore Ts'o9b04c992006-03-24 03:15:10 -080024int root_mountflags = MS_RDONLY | MS_SILENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025char * __initdata root_device_name;
26static char __initdata saved_root_name[64];
Pierre Ossmancc1ed752007-07-15 23:40:35 -070027static int __initdata root_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029dev_t ROOT_DEV;
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static 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
38static int __init readonly(char *str)
39{
40 if (*str)
41 return 0;
42 root_mountflags |= MS_RDONLY;
43 return 1;
44}
45
46static 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 Torvalds1da177e2005-04-16 15:20:36 -070057/*
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 Sieversedfaa7c2007-05-21 22:08:01 +020068 * 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 Torvalds1da177e2005-04-16 15:20:36 -070072 */
73
74dev_t name_to_dev_t(char *name)
75{
76 char s[32];
77 char *p;
78 dev_t res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
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 Sieversedfaa7c2007-05-21 22:08:01 +020094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 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 Sieversedfaa7c2007-05-21 22:08:01 +0200109 res = blk_lookup_devt(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (res)
111 goto done;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113fail:
Kay Sieversedfaa7c2007-05-21 22:08:01 +0200114 return 0;
115done:
116 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119static 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 Ossmancc1ed752007-07-15 23:40:35 -0700127static 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 Torvalds1da177e2005-04-16 15:20:36 -0700137static char * __initdata root_mount_data;
138static int __init root_data_setup(char *str)
139{
140 root_mount_data = str;
141 return 1;
142}
143
144static char * __initdata root_fs_names;
145static int __init fs_names_setup(char *str)
146{
147 root_fs_names = str;
148 return 1;
149}
150
151static unsigned int __initdata root_delay;
152static 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
162static 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
189static 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");
196 ROOT_DEV = current->fs->pwdmnt->mnt_sb->s_dev;
197 printk("VFS: Mounted root (%s filesystem)%s.\n",
198 current->fs->pwdmnt->mnt_sb->s_type->name,
199 current->fs->pwdmnt->mnt_sb->s_flags & MS_RDONLY ?
200 " readonly" : "");
201 return 0;
202}
203
204void __init mount_block_root(char *name, int flags)
205{
206 char *fs_names = __getname();
207 char *p;
David Howells93614012006-09-30 20:45:40 +0200208#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 char b[BDEVNAME_SIZE];
David Howells93614012006-09-30 20:45:40 +0200210#else
211 const char *b = name;
212#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 get_fs_names(fs_names);
215retry:
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 Gilbertdd2a3452007-05-09 02:33:24 -0700230 * and give them a list of the available devices
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
David Howells93614012006-09-30 20:45:40 +0200232#ifdef CONFIG_BLOCK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 __bdevname(ROOT_DEV, b);
David Howells93614012006-09-30 20:45:40 +0200234#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 printk("VFS: Cannot open root device \"%s\" or %s\n",
236 root_device_name, b);
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700237 printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700239 printk_all_partitions();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 panic("VFS: Unable to mount root fs on %s", b);
241 }
Andy Whitcroftbe6e028b2006-05-15 09:44:29 -0700242
Dave Gilbertdd2a3452007-05-09 02:33:24 -0700243 printk("List of all partitions:\n");
244 printk_all_partitions();
Andy Whitcroftbe6e028b2006-05-15 09:44:29 -0700245 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 Howells93614012006-09-30 20:45:40 +0200249#ifdef CONFIG_BLOCK
250 __bdevname(ROOT_DEV, b);
251#endif
252 panic("VFS: Unable to mount root fs on %s", b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253out:
254 putname(fs_names);
255}
256
257#ifdef CONFIG_ROOT_NFS
258static int __init mount_nfs_root(void)
259{
260 void *data = nfs_root_data();
261
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -0700262 create_dev("/dev/root", ROOT_DEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 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)
271void __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
300void __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 Howells93614012006-09-30 20:45:40 +0200323#ifdef CONFIG_BLOCK
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -0700324 create_dev("/dev/root", ROOT_DEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 mount_block_root("/dev/root", root_mountflags);
David Howells93614012006-09-30 20:45:40 +0200326#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
329/*
330 * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
331 */
332void __init prepare_namespace(void)
333{
334 int is_floppy;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 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-Hartmand7792492006-07-18 10:59:59 -0700342 /* wait for the known devices to complete their probing */
343 while (driver_probe_done() != 0)
344 msleep(100);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 md_run_setup();
347
348 if (saved_root_name[0]) {
349 root_device_name = saved_root_name;
Joern Engele9482b42006-05-30 14:25:46 +0200350 if (!strncmp(root_device_name, "mtd", 3)) {
351 mount_block_root(root_device_name, root_mountflags);
352 goto out;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 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 Torvalds1da177e2005-04-16 15:20:36 -0700359 if (initrd_load())
360 goto out;
361
Pierre Ossmancc1ed752007-07-15 23:40:35 -0700362 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700373 if (is_floppy && rd_doload && rd_load_disk(0))
374 ROOT_DEV = Root_RAM0;
375
376 mount_root();
377out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 sys_mount(".", "/", NULL, MS_MOVE, NULL);
379 sys_chroot(".");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381