do_mounts.c 9.6 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/mount.h>
  11. #include <linux/device.h>
  12. #include <linux/nfs_fs.h>
  13. #include <linux/nfs_fs_sb.h>
  14. #include <linux/nfs_mount.h>
  15. #include "do_mounts.h"
  16. /* qisda tim.huang 091015 delay 200ms for more time to mount sd card { */
  17. #define MOUNT_DELAY
  18. /* qisda tim.huang 091015 delay 200ms for more time to mount sd card } */
  19. extern int get_filesystem_list(char * buf);
  20. int __initdata rd_doload; /* 1 = load RAM disk, 0 = don't load */
  21. int root_mountflags = MS_RDONLY | MS_SILENT;
  22. char * __initdata root_device_name;
  23. static char __initdata saved_root_name[64];
  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 char * __initdata root_mount_data;
  194. static int __init root_data_setup(char *str)
  195. {
  196. root_mount_data = str;
  197. return 1;
  198. }
  199. static char * __initdata root_fs_names;
  200. static int __init fs_names_setup(char *str)
  201. {
  202. root_fs_names = str;
  203. return 1;
  204. }
  205. static unsigned int __initdata root_delay;
  206. static int __init root_delay_setup(char *str)
  207. {
  208. root_delay = simple_strtoul(str, NULL, 0);
  209. return 1;
  210. }
  211. __setup("rootflags=", root_data_setup);
  212. __setup("rootfstype=", fs_names_setup);
  213. __setup("rootdelay=", root_delay_setup);
  214. static void __init get_fs_names(char *page)
  215. {
  216. char *s = page;
  217. if (root_fs_names) {
  218. strcpy(page, root_fs_names);
  219. while (*s++) {
  220. if (s[-1] == ',')
  221. s[-1] = '\0';
  222. }
  223. } else {
  224. int len = get_filesystem_list(page);
  225. char *p, *next;
  226. page[len] = '\0';
  227. for (p = page-1; p; p = next) {
  228. next = strchr(++p, '\n');
  229. if (*p++ != '\t')
  230. continue;
  231. while ((*s++ = *p++) != '\n')
  232. ;
  233. s[-1] = '\0';
  234. }
  235. }
  236. *s = '\0';
  237. }
  238. static int __init do_mount_root(char *name, char *fs, int flags, void *data)
  239. {
  240. int err = sys_mount(name, "/root", fs, flags, data);
  241. if (err)
  242. return err;
  243. sys_chdir("/root");
  244. ROOT_DEV = current->fs->pwdmnt->mnt_sb->s_dev;
  245. printk("VFS: Mounted root (%s filesystem)%s.\n",
  246. current->fs->pwdmnt->mnt_sb->s_type->name,
  247. current->fs->pwdmnt->mnt_sb->s_flags & MS_RDONLY ?
  248. " readonly" : "");
  249. return 0;
  250. }
  251. void __init mount_block_root(char *name, int flags)
  252. {
  253. char *fs_names = __getname();
  254. char *p;
  255. #ifdef CONFIG_BLOCK
  256. char b[BDEVNAME_SIZE];
  257. #else
  258. const char *b = name;
  259. #endif
  260. get_fs_names(fs_names);
  261. retry:
  262. for (p = fs_names; *p; p += strlen(p)+1) {
  263. int err = do_mount_root(name, p, flags, root_mount_data);
  264. switch (err) {
  265. case 0:
  266. goto out;
  267. case -EACCES:
  268. flags |= MS_RDONLY;
  269. goto retry;
  270. case -EINVAL:
  271. continue;
  272. }
  273. /*
  274. * Allow the user to distinguish between failed sys_open
  275. * and bad superblock on root device.
  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\n");
  283. panic("VFS: Unable to mount root fs on %s", b);
  284. }
  285. printk("No filesystem could mount root, tried: ");
  286. for (p = fs_names; *p; p += strlen(p)+1)
  287. printk(" %s", p);
  288. printk("\n");
  289. #ifdef CONFIG_BLOCK
  290. __bdevname(ROOT_DEV, b);
  291. #endif
  292. panic("VFS: Unable to mount root fs on %s", b);
  293. out:
  294. putname(fs_names);
  295. }
  296. #ifdef CONFIG_ROOT_NFS
  297. static int __init mount_nfs_root(void)
  298. {
  299. void *data = nfs_root_data();
  300. create_dev("/dev/root", ROOT_DEV);
  301. if (data &&
  302. do_mount_root("/dev/root", "nfs", root_mountflags, data) == 0)
  303. return 1;
  304. return 0;
  305. }
  306. #endif
  307. #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
  308. void __init change_floppy(char *fmt, ...)
  309. {
  310. struct termios termios;
  311. char buf[80];
  312. char c;
  313. int fd;
  314. va_list args;
  315. va_start(args, fmt);
  316. vsprintf(buf, fmt, args);
  317. va_end(args);
  318. fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0);
  319. if (fd >= 0) {
  320. sys_ioctl(fd, FDEJECT, 0);
  321. sys_close(fd);
  322. }
  323. printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
  324. fd = sys_open("/dev/console", O_RDWR, 0);
  325. if (fd >= 0) {
  326. sys_ioctl(fd, TCGETS, (long)&termios);
  327. termios.c_lflag &= ~ICANON;
  328. sys_ioctl(fd, TCSETSF, (long)&termios);
  329. sys_read(fd, &c, 1);
  330. termios.c_lflag |= ICANON;
  331. sys_ioctl(fd, TCSETSF, (long)&termios);
  332. sys_close(fd);
  333. }
  334. }
  335. #endif
  336. void __init mount_root(void)
  337. {
  338. #ifdef CONFIG_ROOT_NFS
  339. if (MAJOR(ROOT_DEV) == UNNAMED_MAJOR) {
  340. if (mount_nfs_root())
  341. return;
  342. printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
  343. ROOT_DEV = Root_FD0;
  344. }
  345. #endif
  346. #ifdef CONFIG_BLK_DEV_FD
  347. if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
  348. /* rd_doload is 2 for a dual initrd/ramload setup */
  349. if (rd_doload==2) {
  350. if (rd_load_disk(1)) {
  351. ROOT_DEV = Root_RAM1;
  352. root_device_name = NULL;
  353. }
  354. } else
  355. change_floppy("root floppy");
  356. }
  357. #endif
  358. #ifdef CONFIG_BLOCK
  359. create_dev("/dev/root", ROOT_DEV);
  360. mount_block_root("/dev/root", root_mountflags);
  361. #endif
  362. }
  363. /*
  364. * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
  365. */
  366. void __init prepare_namespace(void)
  367. {
  368. int is_floppy;
  369. if (root_delay) {
  370. printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
  371. root_delay);
  372. ssleep(root_delay);
  373. }
  374. /* wait for the known devices to complete their probing */
  375. while (driver_probe_done() != 0)
  376. msleep(100);
  377. md_run_setup();
  378. if (saved_root_name[0]) {
  379. root_device_name = saved_root_name;
  380. if (!strncmp(root_device_name, "mtd", 3)) {
  381. mount_block_root(root_device_name, root_mountflags);
  382. goto out;
  383. }
  384. ROOT_DEV = name_to_dev_t(root_device_name);
  385. if (strncmp(root_device_name, "/dev/", 5) == 0)
  386. root_device_name += 5;
  387. }
  388. is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
  389. if (initrd_load())
  390. goto out;
  391. if (is_floppy && rd_doload && rd_load_disk(0))
  392. ROOT_DEV = Root_RAM0;
  393. mount_root();
  394. out:
  395. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  396. sys_chroot(".");
  397. security_sb_post_mountroot();
  398. }