commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / init / do_mounts.c
1 #include <linux/sched.h>
2 #include <linux/ctype.h>
3 #include <linux/fd.h>
4 #include <linux/tty.h>
5 #include <linux/suspend.h>
6 #include <linux/root_dev.h>
7 #include <linux/security.h>
8
9 #include <linux/nfs_fs.h>
10 #include <linux/nfs_fs_sb.h>
11 #include <linux/nfs_mount.h>
12
13 #include "do_mounts.h"
14
15 extern int get_filesystem_list(char * buf);
16
17 int __initdata rd_doload;       /* 1 = load RAM disk, 0 = don't load */
18
19 int root_mountflags = MS_RDONLY | MS_VERBOSE;
20 char * __initdata root_device_name;
21 static char __initdata saved_root_name[64];
22
23 /* this is initialized in init/main.c */
24 dev_t ROOT_DEV;
25
26 static int __init load_ramdisk(char *str)
27 {
28         rd_doload = simple_strtol(str,NULL,0) & 3;
29         return 1;
30 }
31 __setup("load_ramdisk=", load_ramdisk);
32
33 static int __init readonly(char *str)
34 {
35         if (*str)
36                 return 0;
37         root_mountflags |= MS_RDONLY;
38         return 1;
39 }
40
41 static int __init readwrite(char *str)
42 {
43         if (*str)
44                 return 0;
45         root_mountflags &= ~MS_RDONLY;
46         return 1;
47 }
48
49 __setup("ro", readonly);
50 __setup("rw", readwrite);
51
52 static dev_t __init try_name(char *name, int part)
53 {
54         char path[64];
55         char buf[32];
56         int range;
57         dev_t res;
58         char *s;
59         int len;
60         int fd;
61         unsigned int maj, min;
62
63         /* read device number from .../dev */
64
65         sprintf(path, "/sys/block/%s/dev", name);
66         fd = open(path, 0, 0);
67         if (fd < 0)
68                 goto fail;
69         len = read(fd, buf, 32);
70         close(fd);
71         if (len <= 0 || len == 32 || buf[len - 1] != '\n')
72                 goto fail;
73         buf[len - 1] = '\0';
74         /*
75          * The format of dev is now %u:%u -- see print_dev_t()
76          */
77         if (sscanf(buf, "%u:%u", &maj, &min) == 2)
78                 res = MKDEV(maj, min);
79         else
80                 goto fail;
81
82         /* if it's there and we are not looking for a partition - that's it */
83         if (!part)
84                 return res;
85
86         /* otherwise read range from .../range */
87         sprintf(path, "/sys/block/%s/range", name);
88         fd = open(path, 0, 0);
89         if (fd < 0)
90                 goto fail;
91         len = read(fd, buf, 32);
92         close(fd);
93         if (len <= 0 || len == 32 || buf[len - 1] != '\n')
94                 goto fail;
95         buf[len - 1] = '\0';
96         range = simple_strtoul(buf, &s, 10);
97         if (*s)
98                 goto fail;
99
100         /* if partition is within range - we got it */
101         if (part < range)
102                 return res + part;
103 fail:
104         return (dev_t) 0;
105 }
106
107 /*
108  *      Convert a name into device number.  We accept the following variants:
109  *
110  *      1) device number in hexadecimal represents itself
111  *      2) /dev/nfs represents Root_NFS (0xff)
112  *      3) /dev/<disk_name> represents the device number of disk
113  *      4) /dev/<disk_name><decimal> represents the device number
114  *         of partition - device number of disk plus the partition number
115  *      5) /dev/<disk_name>p<decimal> - same as the above, that form is
116  *         used when disk name of partitioned disk ends on a digit.
117  *
118  *      If name doesn't have fall into the categories above, we return 0.
119  *      Driverfs is used to check if something is a disk name - it has
120  *      all known disks under bus/block/devices.  If the disk name
121  *      contains slashes, name of driverfs node has them replaced with
122  *      dots.  try_name() does the actual checks, assuming that driverfs
123  *      is mounted on rootfs /sys.
124  */
125
126 dev_t name_to_dev_t(char *name)
127 {
128         char s[32];
129         char *p;
130         dev_t res = 0;
131         int part;
132
133         sys_mkdir("/sys", 0700);
134         if (sys_mount("sysfs", "/sys", "sysfs", 0, NULL) < 0)
135                 goto out;
136
137         if (strncmp(name, "/dev/", 5) != 0) {
138                 res = (dev_t) simple_strtoul(name, &p, 16);
139                 if (*p)
140                         goto fail;
141                 goto done;
142         }
143         name += 5;
144         res = Root_NFS;
145         if (strcmp(name, "nfs") == 0)
146                 goto done;
147
148         if (strlen(name) > 31)
149                 goto fail;
150         strcpy(s, name);
151         for (p = s; *p; p++)
152                 if (*p == '/')
153                         *p = '.';
154         res = try_name(s, 0);
155         if (res)
156                 goto done;
157
158         while (p > s && isdigit(p[-1]))
159                 p--;
160         if (p == s || !*p || *p == '0')
161                 goto fail;
162         part = simple_strtoul(p, NULL, 10);
163         *p = '\0';
164         res = try_name(s, part);
165         if (res)
166                 goto done;
167
168         if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
169                 goto fail;
170         p[-1] = '\0';
171         res = try_name(s, part);
172 done:
173         sys_umount("/sys", 0);
174 out:
175         sys_rmdir("/sys");
176         return res;
177 fail:
178         res = (dev_t) 0;
179         goto done;
180 }
181
182 static int __init root_dev_setup(char *line)
183 {
184         strlcpy(saved_root_name, line, sizeof(saved_root_name));
185         return 1;
186 }
187
188 __setup("root=", root_dev_setup);
189
190 static char * __initdata root_mount_data;
191 static int __init root_data_setup(char *str)
192 {
193         root_mount_data = str;
194         return 1;
195 }
196
197 static char * __initdata root_fs_names;
198 static int __init fs_names_setup(char *str)
199 {
200         root_fs_names = str;
201         return 1;
202 }
203
204 __setup("rootflags=", root_data_setup);
205 __setup("rootfstype=", fs_names_setup);
206
207 static void __init get_fs_names(char *page)
208 {
209         char *s = page;
210
211         if (root_fs_names) {
212                 strcpy(page, root_fs_names);
213                 while (*s++) {
214                         if (s[-1] == ',')
215                                 s[-1] = '\0';
216                 }
217         } else {
218                 int len = get_filesystem_list(page);
219                 char *p, *next;
220
221                 page[len] = '\0';
222                 for (p = page-1; p; p = next) {
223                         next = strchr(++p, '\n');
224                         if (*p++ != '\t')
225                                 continue;
226                         while ((*s++ = *p++) != '\n')
227                                 ;
228                         s[-1] = '\0';
229                 }
230         }
231         *s = '\0';
232 }
233
234 static int __init do_mount_root(char *name, char *fs, int flags, void *data)
235 {
236         int err = sys_mount(name, "/root", fs, flags, data);
237         if (err)
238                 return err;
239
240         sys_chdir("/root");
241         ROOT_DEV = current->fs->pwdmnt->mnt_sb->s_dev;
242         printk("VFS: Mounted root (%s filesystem)%s.\n",
243                current->fs->pwdmnt->mnt_sb->s_type->name,
244                current->fs->pwdmnt->mnt_sb->s_flags & MS_RDONLY ? 
245                " readonly" : "");
246         return 0;
247 }
248
249 void __init mount_block_root(char *name, int flags)
250 {
251         char *fs_names = __getname();
252         char *p;
253         char b[BDEVNAME_SIZE];
254
255         get_fs_names(fs_names);
256 retry:
257         for (p = fs_names; *p; p += strlen(p)+1) {
258                 int err = do_mount_root(name, p, flags, root_mount_data);
259                 switch (err) {
260                         case 0:
261                                 goto out;
262                         case -EACCES:
263                                 flags |= MS_RDONLY;
264                                 goto retry;
265                         case -EINVAL:
266                                 continue;
267                 }
268                 /*
269                  * Allow the user to distinguish between failed open
270                  * and bad superblock on root device.
271                  */
272                 __bdevname(ROOT_DEV, b);
273                 printk("VFS: Cannot open root device \"%s\" or %s\n",
274                                 root_device_name, b);
275                 printk("Please append a correct \"root=\" boot option\n");
276                 panic("VFS: Unable to mount root fs on %s", b);
277         }
278         panic("VFS: Unable to mount root fs on %s", __bdevname(ROOT_DEV, b));
279 out:
280         putname(fs_names);
281 }
282  
283 #ifdef CONFIG_ROOT_NFS
284 static int __init mount_nfs_root(void)
285 {
286         void *data = nfs_root_data();
287
288         create_dev("/dev/root", ROOT_DEV, NULL);
289         if (data &&
290             do_mount_root("/dev/root", "nfs", root_mountflags, data) == 0)
291                 return 1;
292         return 0;
293 }
294 #endif
295
296 #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
297 void __init change_floppy(char *fmt, ...)
298 {
299         struct termios termios;
300         char buf[80];
301         char c;
302         int fd;
303         va_list args;
304         va_start(args, fmt);
305         vsprintf(buf, fmt, args);
306         va_end(args);
307         fd = open("/dev/root", O_RDWR | O_NDELAY, 0);
308         if (fd >= 0) {
309                 sys_ioctl(fd, FDEJECT, 0);
310                 close(fd);
311         }
312         printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
313         fd = open("/dev/console", O_RDWR, 0);
314         if (fd >= 0) {
315                 sys_ioctl(fd, TCGETS, (long)&termios);
316                 termios.c_lflag &= ~ICANON;
317                 sys_ioctl(fd, TCSETSF, (long)&termios);
318                 read(fd, &c, 1);
319                 termios.c_lflag |= ICANON;
320                 sys_ioctl(fd, TCSETSF, (long)&termios);
321                 close(fd);
322         }
323 }
324 #endif
325
326 void __init mount_root(void)
327 {
328 #ifdef CONFIG_ROOT_NFS
329         if (MAJOR(ROOT_DEV) == UNNAMED_MAJOR) {
330                 if (mount_nfs_root())
331                         return;
332
333                 printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
334                 ROOT_DEV = Root_FD0;
335         }
336 #endif
337 #ifdef CONFIG_BLK_DEV_FD
338         if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
339                 /* rd_doload is 2 for a dual initrd/ramload setup */
340                 if (rd_doload==2) {
341                         if (rd_load_disk(1)) {
342                                 ROOT_DEV = Root_RAM1;
343                                 root_device_name = NULL;
344                         }
345                 } else
346                         change_floppy("root floppy");
347         }
348 #endif
349         create_dev("/dev/root", ROOT_DEV, root_device_name);
350         mount_block_root("/dev/root", root_mountflags);
351 }
352
353 /*
354  * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
355  */
356 void __init prepare_namespace(void)
357 {
358         int is_floppy;
359
360         mount_devfs();
361
362         md_run_setup();
363
364         if (saved_root_name[0]) {
365                 root_device_name = saved_root_name;
366                 ROOT_DEV = name_to_dev_t(root_device_name);
367                 if (strncmp(root_device_name, "/dev/", 5) == 0)
368                         root_device_name += 5;
369         }
370
371         is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
372
373         /* This has to be before mounting root, because even readonly mount of reiserfs would replay
374            log corrupting stuff */
375         software_resume();
376
377         if (initrd_load())
378                 goto out;
379
380         if (is_floppy && rd_doload && rd_load_disk(0))
381                 ROOT_DEV = Root_RAM0;
382
383         mount_root();
384 out:
385         umount_devfs("/dev");
386         sys_mount(".", "/", NULL, MS_MOVE, NULL);
387         sys_chroot(".");
388         security_sb_post_mountroot();
389         mount_devfs_fs ();
390 }
391