do_mounts.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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_gfp(GFP_KERNEL
  202. | __GFP_NOTRACK_FALSE_POSITIVE);
  203. char *p;
  204. #ifdef CONFIG_BLOCK
  205. char b[BDEVNAME_SIZE];
  206. #else
  207. const char *b = name;
  208. #endif
  209. get_fs_names(fs_names);
  210. retry:
  211. for (p = fs_names; *p; p += strlen(p)+1) {
  212. int err = do_mount_root(name, p, flags, root_mount_data);
  213. switch (err) {
  214. case 0:
  215. goto out;
  216. case -EACCES:
  217. flags |= MS_RDONLY;
  218. goto retry;
  219. case -EINVAL:
  220. continue;
  221. }
  222. /*
  223. * Allow the user to distinguish between failed sys_open
  224. * and bad superblock on root device.
  225. * and give them a list of the available devices
  226. */
  227. #ifdef CONFIG_BLOCK
  228. __bdevname(ROOT_DEV, b);
  229. #endif
  230. printk("VFS: Cannot open root device \"%s\" or %s\n",
  231. root_device_name, b);
  232. printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
  233. printk_all_partitions();
  234. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  235. printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
  236. "explicit textual name for \"root=\" boot option.\n");
  237. #endif
  238. panic("VFS: Unable to mount root fs on %s", b);
  239. }
  240. printk("List of all partitions:\n");
  241. printk_all_partitions();
  242. printk("No filesystem could mount root, tried: ");
  243. for (p = fs_names; *p; p += strlen(p)+1)
  244. printk(" %s", p);
  245. printk("\n");
  246. #ifdef CONFIG_BLOCK
  247. __bdevname(ROOT_DEV, b);
  248. #endif
  249. panic("VFS: Unable to mount root fs on %s", b);
  250. out:
  251. putname(fs_names);
  252. }
  253. #ifdef CONFIG_ROOT_NFS
  254. static int __init mount_nfs_root(void)
  255. {
  256. void *data = nfs_root_data();
  257. create_dev("/dev/root", ROOT_DEV);
  258. if (data &&
  259. do_mount_root("/dev/root", "nfs", root_mountflags, data) == 0)
  260. return 1;
  261. return 0;
  262. }
  263. #endif
  264. #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
  265. void __init change_floppy(char *fmt, ...)
  266. {
  267. struct termios termios;
  268. char buf[80];
  269. char c;
  270. int fd;
  271. va_list args;
  272. va_start(args, fmt);
  273. vsprintf(buf, fmt, args);
  274. va_end(args);
  275. fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0);
  276. if (fd >= 0) {
  277. sys_ioctl(fd, FDEJECT, 0);
  278. sys_close(fd);
  279. }
  280. printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
  281. fd = sys_open("/dev/console", O_RDWR, 0);
  282. if (fd >= 0) {
  283. sys_ioctl(fd, TCGETS, (long)&termios);
  284. termios.c_lflag &= ~ICANON;
  285. sys_ioctl(fd, TCSETSF, (long)&termios);
  286. sys_read(fd, &c, 1);
  287. termios.c_lflag |= ICANON;
  288. sys_ioctl(fd, TCSETSF, (long)&termios);
  289. sys_close(fd);
  290. }
  291. }
  292. #endif
  293. void __init mount_root(void)
  294. {
  295. #ifdef CONFIG_ROOT_NFS
  296. if (MAJOR(ROOT_DEV) == UNNAMED_MAJOR) {
  297. if (mount_nfs_root())
  298. return;
  299. printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
  300. ROOT_DEV = Root_FD0;
  301. }
  302. #endif
  303. #ifdef CONFIG_BLK_DEV_FD
  304. if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
  305. /* rd_doload is 2 for a dual initrd/ramload setup */
  306. if (rd_doload==2) {
  307. if (rd_load_disk(1)) {
  308. ROOT_DEV = Root_RAM1;
  309. root_device_name = NULL;
  310. }
  311. } else
  312. change_floppy("root floppy");
  313. }
  314. #endif
  315. #ifdef CONFIG_BLOCK
  316. create_dev("/dev/root", ROOT_DEV);
  317. mount_block_root("/dev/root", root_mountflags);
  318. #endif
  319. }
  320. /*
  321. * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
  322. */
  323. void __init prepare_namespace(void)
  324. {
  325. int is_floppy;
  326. if (root_delay) {
  327. printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
  328. root_delay);
  329. ssleep(root_delay);
  330. }
  331. /*
  332. * wait for the known devices to complete their probing
  333. *
  334. * Note: this is a potential source of long boot delays.
  335. * For example, it is not atypical to wait 5 seconds here
  336. * for the touchpad of a laptop to initialize.
  337. */
  338. wait_for_device_probe();
  339. md_run_setup();
  340. if (saved_root_name[0]) {
  341. root_device_name = saved_root_name;
  342. if (!strncmp(root_device_name, "mtd", 3) ||
  343. !strncmp(root_device_name, "ubi", 3)) {
  344. mount_block_root(root_device_name, root_mountflags);
  345. goto out;
  346. }
  347. ROOT_DEV = name_to_dev_t(root_device_name);
  348. if (strncmp(root_device_name, "/dev/", 5) == 0)
  349. root_device_name += 5;
  350. }
  351. if (initrd_load())
  352. goto out;
  353. /* wait for any asynchronous scanning to complete */
  354. if ((ROOT_DEV == 0) && root_wait) {
  355. printk(KERN_INFO "Waiting for root device %s...\n",
  356. saved_root_name);
  357. while (driver_probe_done() != 0 ||
  358. (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
  359. msleep(100);
  360. async_synchronize_full();
  361. }
  362. is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
  363. if (is_floppy && rd_doload && rd_load_disk(0))
  364. ROOT_DEV = Root_RAM0;
  365. mount_root();
  366. out:
  367. devtmpfs_mount("dev");
  368. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  369. sys_chroot(".");
  370. }