do_mounts.c 8.8 KB

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