do_mounts.c 9.0 KB

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