do_mounts.c 10 KB

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