do_mounts.c 9.2 KB

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