do_mounts.c 9.0 KB

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