do_mounts.c 9.2 KB

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