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