do_mounts.c 9.7 KB

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