do_mounts.c 9.5 KB

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