do_mounts.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  232. printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
  233. "explicit textual name for \"root=\" boot option.\n");
  234. #endif
  235. panic("VFS: Unable to mount root fs on %s", b);
  236. }
  237. printk("List of all partitions:\n");
  238. printk_all_partitions();
  239. printk("No filesystem could mount root, tried: ");
  240. for (p = fs_names; *p; p += strlen(p)+1)
  241. printk(" %s", p);
  242. printk("\n");
  243. #ifdef CONFIG_BLOCK
  244. __bdevname(ROOT_DEV, b);
  245. #endif
  246. panic("VFS: Unable to mount root fs on %s", b);
  247. out:
  248. putname(fs_names);
  249. }
  250. #ifdef CONFIG_ROOT_NFS
  251. static int __init mount_nfs_root(void)
  252. {
  253. void *data = nfs_root_data();
  254. create_dev("/dev/root", ROOT_DEV);
  255. if (data &&
  256. do_mount_root("/dev/root", "nfs", root_mountflags, data) == 0)
  257. return 1;
  258. return 0;
  259. }
  260. #endif
  261. #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
  262. void __init change_floppy(char *fmt, ...)
  263. {
  264. struct termios termios;
  265. char buf[80];
  266. char c;
  267. int fd;
  268. va_list args;
  269. va_start(args, fmt);
  270. vsprintf(buf, fmt, args);
  271. va_end(args);
  272. fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0);
  273. if (fd >= 0) {
  274. sys_ioctl(fd, FDEJECT, 0);
  275. sys_close(fd);
  276. }
  277. printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
  278. fd = sys_open("/dev/console", O_RDWR, 0);
  279. if (fd >= 0) {
  280. sys_ioctl(fd, TCGETS, (long)&termios);
  281. termios.c_lflag &= ~ICANON;
  282. sys_ioctl(fd, TCSETSF, (long)&termios);
  283. sys_read(fd, &c, 1);
  284. termios.c_lflag |= ICANON;
  285. sys_ioctl(fd, TCSETSF, (long)&termios);
  286. sys_close(fd);
  287. }
  288. }
  289. #endif
  290. void __init mount_root(void)
  291. {
  292. #ifdef CONFIG_ROOT_NFS
  293. if (MAJOR(ROOT_DEV) == UNNAMED_MAJOR) {
  294. if (mount_nfs_root())
  295. return;
  296. printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
  297. ROOT_DEV = Root_FD0;
  298. }
  299. #endif
  300. #ifdef CONFIG_BLK_DEV_FD
  301. if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
  302. /* rd_doload is 2 for a dual initrd/ramload setup */
  303. if (rd_doload==2) {
  304. if (rd_load_disk(1)) {
  305. ROOT_DEV = Root_RAM1;
  306. root_device_name = NULL;
  307. }
  308. } else
  309. change_floppy("root floppy");
  310. }
  311. #endif
  312. #ifdef CONFIG_BLOCK
  313. create_dev("/dev/root", ROOT_DEV);
  314. mount_block_root("/dev/root", root_mountflags);
  315. #endif
  316. }
  317. /*
  318. * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
  319. */
  320. void __init prepare_namespace(void)
  321. {
  322. int is_floppy;
  323. if (root_delay) {
  324. printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
  325. root_delay);
  326. ssleep(root_delay);
  327. }
  328. /* wait for the known devices to complete their probing */
  329. while (driver_probe_done() != 0)
  330. msleep(100);
  331. md_run_setup();
  332. if (saved_root_name[0]) {
  333. root_device_name = saved_root_name;
  334. if (!strncmp(root_device_name, "mtd", 3) ||
  335. !strncmp(root_device_name, "ubi", 3)) {
  336. mount_block_root(root_device_name, root_mountflags);
  337. goto out;
  338. }
  339. ROOT_DEV = name_to_dev_t(root_device_name);
  340. if (strncmp(root_device_name, "/dev/", 5) == 0)
  341. root_device_name += 5;
  342. }
  343. if (initrd_load())
  344. goto out;
  345. /* wait for any asynchronous scanning to complete */
  346. if ((ROOT_DEV == 0) && root_wait) {
  347. printk(KERN_INFO "Waiting for root device %s...\n",
  348. saved_root_name);
  349. while (driver_probe_done() != 0 ||
  350. (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
  351. msleep(100);
  352. }
  353. is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
  354. if (is_floppy && rd_doload && rd_load_disk(0))
  355. ROOT_DEV = Root_RAM0;
  356. mount_root();
  357. out:
  358. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  359. sys_chroot(".");
  360. }