do_mounts.c 8.1 KB

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