do_mounts.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. int part;
  69. if (strncmp(name, "/dev/", 5) != 0) {
  70. unsigned maj, min;
  71. if (sscanf(name, "%u:%u", &maj, &min) == 2) {
  72. res = MKDEV(maj, min);
  73. if (maj != MAJOR(res) || min != MINOR(res))
  74. goto fail;
  75. } else {
  76. res = new_decode_dev(simple_strtoul(name, &p, 16));
  77. if (*p)
  78. goto fail;
  79. }
  80. goto done;
  81. }
  82. name += 5;
  83. res = Root_NFS;
  84. if (strcmp(name, "nfs") == 0)
  85. goto done;
  86. res = Root_RAM0;
  87. if (strcmp(name, "ram") == 0)
  88. goto done;
  89. if (strlen(name) > 31)
  90. goto fail;
  91. strcpy(s, name);
  92. for (p = s; *p; p++)
  93. if (*p == '/')
  94. *p = '!';
  95. res = blk_lookup_devt(s, 0);
  96. if (res)
  97. goto done;
  98. /*
  99. * try non-existant, but valid partition, which may only exist
  100. * after revalidating the disk, like partitioned md devices
  101. */
  102. while (p > s && isdigit(p[-1]))
  103. p--;
  104. if (p == s || !*p || *p == '0')
  105. goto fail;
  106. /* try disk name without <part number> */
  107. part = simple_strtoul(p, NULL, 10);
  108. *p = '\0';
  109. res = blk_lookup_devt(s, part);
  110. if (res)
  111. goto done;
  112. /* try disk name without p<part number> */
  113. if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
  114. goto fail;
  115. p[-1] = '\0';
  116. res = blk_lookup_devt(s, part);
  117. if (res)
  118. goto done;
  119. fail:
  120. return 0;
  121. done:
  122. return res;
  123. }
  124. static int __init root_dev_setup(char *line)
  125. {
  126. strlcpy(saved_root_name, line, sizeof(saved_root_name));
  127. return 1;
  128. }
  129. __setup("root=", root_dev_setup);
  130. static int __init rootwait_setup(char *str)
  131. {
  132. if (*str)
  133. return 0;
  134. root_wait = 1;
  135. return 1;
  136. }
  137. __setup("rootwait", rootwait_setup);
  138. static char * __initdata root_mount_data;
  139. static int __init root_data_setup(char *str)
  140. {
  141. root_mount_data = str;
  142. return 1;
  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. static unsigned int __initdata root_delay;
  151. static int __init root_delay_setup(char *str)
  152. {
  153. root_delay = simple_strtoul(str, NULL, 0);
  154. return 1;
  155. }
  156. __setup("rootflags=", root_data_setup);
  157. __setup("rootfstype=", fs_names_setup);
  158. __setup("rootdelay=", root_delay_setup);
  159. static void __init get_fs_names(char *page)
  160. {
  161. char *s = page;
  162. if (root_fs_names) {
  163. strcpy(page, root_fs_names);
  164. while (*s++) {
  165. if (s[-1] == ',')
  166. s[-1] = '\0';
  167. }
  168. } else {
  169. int len = get_filesystem_list(page);
  170. char *p, *next;
  171. page[len] = '\0';
  172. for (p = page-1; p; p = next) {
  173. next = strchr(++p, '\n');
  174. if (*p++ != '\t')
  175. continue;
  176. while ((*s++ = *p++) != '\n')
  177. ;
  178. s[-1] = '\0';
  179. }
  180. }
  181. *s = '\0';
  182. }
  183. static int __init do_mount_root(char *name, char *fs, int flags, void *data)
  184. {
  185. int err = sys_mount(name, "/root", fs, flags, data);
  186. if (err)
  187. return err;
  188. sys_chdir("/root");
  189. ROOT_DEV = current->fs->pwd.mnt->mnt_sb->s_dev;
  190. printk("VFS: Mounted root (%s filesystem)%s.\n",
  191. current->fs->pwd.mnt->mnt_sb->s_type->name,
  192. current->fs->pwd.mnt->mnt_sb->s_flags & MS_RDONLY ?
  193. " readonly" : "");
  194. return 0;
  195. }
  196. void __init mount_block_root(char *name, int flags)
  197. {
  198. char *fs_names = __getname();
  199. char *p;
  200. #ifdef CONFIG_BLOCK
  201. char b[BDEVNAME_SIZE];
  202. #else
  203. const char *b = name;
  204. #endif
  205. get_fs_names(fs_names);
  206. retry:
  207. for (p = fs_names; *p; p += strlen(p)+1) {
  208. int err = do_mount_root(name, p, flags, root_mount_data);
  209. switch (err) {
  210. case 0:
  211. goto out;
  212. case -EACCES:
  213. flags |= MS_RDONLY;
  214. goto retry;
  215. case -EINVAL:
  216. continue;
  217. }
  218. /*
  219. * Allow the user to distinguish between failed sys_open
  220. * and bad superblock on root device.
  221. * and give them a list of the available devices
  222. */
  223. #ifdef CONFIG_BLOCK
  224. __bdevname(ROOT_DEV, b);
  225. #endif
  226. printk("VFS: Cannot open root device \"%s\" or %s\n",
  227. root_device_name, b);
  228. printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
  229. printk_all_partitions();
  230. panic("VFS: Unable to mount root fs on %s", b);
  231. }
  232. printk("List of all partitions:\n");
  233. printk_all_partitions();
  234. printk("No filesystem could mount root, tried: ");
  235. for (p = fs_names; *p; p += strlen(p)+1)
  236. printk(" %s", p);
  237. printk("\n");
  238. #ifdef CONFIG_BLOCK
  239. __bdevname(ROOT_DEV, b);
  240. #endif
  241. panic("VFS: Unable to mount root fs on %s", b);
  242. out:
  243. putname(fs_names);
  244. }
  245. #ifdef CONFIG_ROOT_NFS
  246. static int __init mount_nfs_root(void)
  247. {
  248. void *data = nfs_root_data();
  249. create_dev("/dev/root", ROOT_DEV);
  250. if (data &&
  251. do_mount_root("/dev/root", "nfs", root_mountflags, data) == 0)
  252. return 1;
  253. return 0;
  254. }
  255. #endif
  256. #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
  257. void __init change_floppy(char *fmt, ...)
  258. {
  259. struct termios termios;
  260. char buf[80];
  261. char c;
  262. int fd;
  263. va_list args;
  264. va_start(args, fmt);
  265. vsprintf(buf, fmt, args);
  266. va_end(args);
  267. fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0);
  268. if (fd >= 0) {
  269. sys_ioctl(fd, FDEJECT, 0);
  270. sys_close(fd);
  271. }
  272. printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
  273. fd = sys_open("/dev/console", O_RDWR, 0);
  274. if (fd >= 0) {
  275. sys_ioctl(fd, TCGETS, (long)&termios);
  276. termios.c_lflag &= ~ICANON;
  277. sys_ioctl(fd, TCSETSF, (long)&termios);
  278. sys_read(fd, &c, 1);
  279. termios.c_lflag |= ICANON;
  280. sys_ioctl(fd, TCSETSF, (long)&termios);
  281. sys_close(fd);
  282. }
  283. }
  284. #endif
  285. void __init mount_root(void)
  286. {
  287. #ifdef CONFIG_ROOT_NFS
  288. if (MAJOR(ROOT_DEV) == UNNAMED_MAJOR) {
  289. if (mount_nfs_root())
  290. return;
  291. printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
  292. ROOT_DEV = Root_FD0;
  293. }
  294. #endif
  295. #ifdef CONFIG_BLK_DEV_FD
  296. if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
  297. /* rd_doload is 2 for a dual initrd/ramload setup */
  298. if (rd_doload==2) {
  299. if (rd_load_disk(1)) {
  300. ROOT_DEV = Root_RAM1;
  301. root_device_name = NULL;
  302. }
  303. } else
  304. change_floppy("root floppy");
  305. }
  306. #endif
  307. #ifdef CONFIG_BLOCK
  308. create_dev("/dev/root", ROOT_DEV);
  309. mount_block_root("/dev/root", root_mountflags);
  310. #endif
  311. }
  312. /*
  313. * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
  314. */
  315. void __init prepare_namespace(void)
  316. {
  317. int is_floppy;
  318. if (root_delay) {
  319. printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
  320. root_delay);
  321. ssleep(root_delay);
  322. }
  323. /* wait for the known devices to complete their probing */
  324. while (driver_probe_done() != 0)
  325. msleep(100);
  326. md_run_setup();
  327. if (saved_root_name[0]) {
  328. root_device_name = saved_root_name;
  329. if (!strncmp(root_device_name, "mtd", 3)) {
  330. mount_block_root(root_device_name, root_mountflags);
  331. goto out;
  332. }
  333. ROOT_DEV = name_to_dev_t(root_device_name);
  334. if (strncmp(root_device_name, "/dev/", 5) == 0)
  335. root_device_name += 5;
  336. }
  337. if (initrd_load())
  338. goto out;
  339. /* wait for any asynchronous scanning to complete */
  340. if ((ROOT_DEV == 0) && root_wait) {
  341. printk(KERN_INFO "Waiting for root device %s...\n",
  342. saved_root_name);
  343. while (driver_probe_done() != 0 ||
  344. (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
  345. msleep(100);
  346. }
  347. is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
  348. if (is_floppy && rd_doload && rd_load_disk(0))
  349. ROOT_DEV = Root_RAM0;
  350. mount_root();
  351. out:
  352. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  353. sys_chroot(".");
  354. }