do_mounts.c 9.3 KB

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