do_mounts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/fs.h>
  15. #include <linux/initrd.h>
  16. #include <linux/async.h>
  17. #include <linux/fs_struct.h>
  18. #include <linux/slab.h>
  19. #include <linux/nfs_fs.h>
  20. #include <linux/nfs_fs_sb.h>
  21. #include <linux/nfs_mount.h>
  22. #include "do_mounts.h"
  23. int __initdata rd_doload; /* 1 = load RAM disk, 0 = don't load */
  24. int root_mountflags = MS_RDONLY | MS_SILENT;
  25. static char * __initdata root_device_name;
  26. static char __initdata saved_root_name[64];
  27. static int __initdata root_wait;
  28. dev_t ROOT_DEV;
  29. static int __init load_ramdisk(char *str)
  30. {
  31. rd_doload = simple_strtol(str,NULL,0) & 3;
  32. return 1;
  33. }
  34. __setup("load_ramdisk=", load_ramdisk);
  35. static int __init readonly(char *str)
  36. {
  37. if (*str)
  38. return 0;
  39. root_mountflags |= MS_RDONLY;
  40. return 1;
  41. }
  42. static int __init readwrite(char *str)
  43. {
  44. if (*str)
  45. return 0;
  46. root_mountflags &= ~MS_RDONLY;
  47. return 1;
  48. }
  49. __setup("ro", readonly);
  50. __setup("rw", readwrite);
  51. /**
  52. * match_dev_by_uuid - callback for finding a partition using its uuid
  53. * @dev: device passed in by the caller
  54. * @data: opaque pointer to a 36 byte char array with a UUID
  55. *
  56. * Returns 1 if the device matches, and 0 otherwise.
  57. */
  58. static int __init match_dev_by_uuid(struct device *dev, void *data)
  59. {
  60. u8 *uuid = data;
  61. struct hd_struct *part = dev_to_part(dev);
  62. if (!part->info)
  63. goto no_match;
  64. if (memcmp(uuid, part->info->uuid, sizeof(part->info->uuid)))
  65. goto no_match;
  66. return 1;
  67. no_match:
  68. return 0;
  69. }
  70. /**
  71. * devt_from_partuuid - looks up the dev_t of a partition by its UUID
  72. * @uuid: 36 byte char array containing a hex ascii UUID
  73. *
  74. * The function will return the first partition which contains a matching
  75. * UUID value in its partition_meta_info struct. This does not search
  76. * by filesystem UUIDs.
  77. *
  78. * Returns the matching dev_t on success or 0 on failure.
  79. */
  80. static dev_t __init devt_from_partuuid(char *uuid_str)
  81. {
  82. dev_t res = 0;
  83. struct device *dev = NULL;
  84. u8 uuid[16];
  85. /* Pack the requested UUID in the expected format. */
  86. part_pack_uuid(uuid_str, uuid);
  87. dev = class_find_device(&block_class, NULL, uuid, &match_dev_by_uuid);
  88. if (!dev)
  89. goto done;
  90. res = dev->devt;
  91. put_device(dev);
  92. done:
  93. return res;
  94. }
  95. /*
  96. * Convert a name into device number. We accept the following variants:
  97. *
  98. * 1) device number in hexadecimal represents itself
  99. * 2) /dev/nfs represents Root_NFS (0xff)
  100. * 3) /dev/<disk_name> represents the device number of disk
  101. * 4) /dev/<disk_name><decimal> represents the device number
  102. * of partition - device number of disk plus the partition number
  103. * 5) /dev/<disk_name>p<decimal> - same as the above, that form is
  104. * used when disk name of partitioned disk ends on a digit.
  105. * 6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
  106. * unique id of a partition if the partition table provides it.
  107. *
  108. * If name doesn't have fall into the categories above, we return (0,0).
  109. * block_class is used to check if something is a disk name. If the disk
  110. * name contains slashes, the device name has them replaced with
  111. * bangs.
  112. */
  113. dev_t name_to_dev_t(char *name)
  114. {
  115. char s[32];
  116. char *p;
  117. dev_t res = 0;
  118. int part;
  119. if (strncmp(name, "PARTUUID=", 9) == 0) {
  120. name += 9;
  121. if (strlen(name) != 36)
  122. goto fail;
  123. res = devt_from_partuuid(name);
  124. if (!res)
  125. goto fail;
  126. goto done;
  127. }
  128. if (strncmp(name, "/dev/", 5) != 0) {
  129. unsigned maj, min;
  130. if (sscanf(name, "%u:%u", &maj, &min) == 2) {
  131. res = MKDEV(maj, min);
  132. if (maj != MAJOR(res) || min != MINOR(res))
  133. goto fail;
  134. } else {
  135. res = new_decode_dev(simple_strtoul(name, &p, 16));
  136. if (*p)
  137. goto fail;
  138. }
  139. goto done;
  140. }
  141. name += 5;
  142. res = Root_NFS;
  143. if (strcmp(name, "nfs") == 0)
  144. goto done;
  145. res = Root_RAM0;
  146. if (strcmp(name, "ram") == 0)
  147. goto done;
  148. if (strlen(name) > 31)
  149. goto fail;
  150. strcpy(s, name);
  151. for (p = s; *p; p++)
  152. if (*p == '/')
  153. *p = '!';
  154. res = blk_lookup_devt(s, 0);
  155. if (res)
  156. goto done;
  157. /*
  158. * try non-existant, but valid partition, which may only exist
  159. * after revalidating the disk, like partitioned md devices
  160. */
  161. while (p > s && isdigit(p[-1]))
  162. p--;
  163. if (p == s || !*p || *p == '0')
  164. goto fail;
  165. /* try disk name without <part number> */
  166. part = simple_strtoul(p, NULL, 10);
  167. *p = '\0';
  168. res = blk_lookup_devt(s, part);
  169. if (res)
  170. goto done;
  171. /* try disk name without p<part number> */
  172. if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
  173. goto fail;
  174. p[-1] = '\0';
  175. res = blk_lookup_devt(s, part);
  176. if (res)
  177. goto done;
  178. fail:
  179. return 0;
  180. done:
  181. return res;
  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 int __init rootwait_setup(char *str)
  190. {
  191. if (*str)
  192. return 0;
  193. root_wait = 1;
  194. return 1;
  195. }
  196. __setup("rootwait", rootwait_setup);
  197. static char * __initdata root_mount_data;
  198. static int __init root_data_setup(char *str)
  199. {
  200. root_mount_data = str;
  201. return 1;
  202. }
  203. static char * __initdata root_fs_names;
  204. static int __init fs_names_setup(char *str)
  205. {
  206. root_fs_names = str;
  207. return 1;
  208. }
  209. static unsigned int __initdata root_delay;
  210. static int __init root_delay_setup(char *str)
  211. {
  212. root_delay = simple_strtoul(str, NULL, 0);
  213. return 1;
  214. }
  215. __setup("rootflags=", root_data_setup);
  216. __setup("rootfstype=", fs_names_setup);
  217. __setup("rootdelay=", root_delay_setup);
  218. static void __init get_fs_names(char *page)
  219. {
  220. char *s = page;
  221. if (root_fs_names) {
  222. strcpy(page, root_fs_names);
  223. while (*s++) {
  224. if (s[-1] == ',')
  225. s[-1] = '\0';
  226. }
  227. } else {
  228. int len = get_filesystem_list(page);
  229. char *p, *next;
  230. page[len] = '\0';
  231. for (p = page-1; p; p = next) {
  232. next = strchr(++p, '\n');
  233. if (*p++ != '\t')
  234. continue;
  235. while ((*s++ = *p++) != '\n')
  236. ;
  237. s[-1] = '\0';
  238. }
  239. }
  240. *s = '\0';
  241. }
  242. static int __init do_mount_root(char *name, char *fs, int flags, void *data)
  243. {
  244. int err = sys_mount(name, "/root", fs, flags, data);
  245. if (err)
  246. return err;
  247. sys_chdir("/root");
  248. ROOT_DEV = current->fs->pwd.mnt->mnt_sb->s_dev;
  249. printk("VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
  250. current->fs->pwd.mnt->mnt_sb->s_type->name,
  251. current->fs->pwd.mnt->mnt_sb->s_flags & MS_RDONLY ?
  252. " readonly" : "", MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
  253. return 0;
  254. }
  255. void __init mount_block_root(char *name, int flags)
  256. {
  257. char *fs_names = __getname_gfp(GFP_KERNEL
  258. | __GFP_NOTRACK_FALSE_POSITIVE);
  259. char *p;
  260. #ifdef CONFIG_BLOCK
  261. char b[BDEVNAME_SIZE];
  262. #else
  263. const char *b = name;
  264. #endif
  265. get_fs_names(fs_names);
  266. retry:
  267. for (p = fs_names; *p; p += strlen(p)+1) {
  268. int err = do_mount_root(name, p, flags, root_mount_data);
  269. switch (err) {
  270. case 0:
  271. goto out;
  272. case -EACCES:
  273. flags |= MS_RDONLY;
  274. goto retry;
  275. case -EINVAL:
  276. continue;
  277. }
  278. /*
  279. * Allow the user to distinguish between failed sys_open
  280. * and bad superblock on root device.
  281. * and give them a list of the available devices
  282. */
  283. #ifdef CONFIG_BLOCK
  284. __bdevname(ROOT_DEV, b);
  285. #endif
  286. printk("VFS: Cannot open root device \"%s\" or %s\n",
  287. root_device_name, b);
  288. printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
  289. printk_all_partitions();
  290. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  291. printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
  292. "explicit textual name for \"root=\" boot option.\n");
  293. #endif
  294. panic("VFS: Unable to mount root fs on %s", b);
  295. }
  296. printk("List of all partitions:\n");
  297. printk_all_partitions();
  298. printk("No filesystem could mount root, tried: ");
  299. for (p = fs_names; *p; p += strlen(p)+1)
  300. printk(" %s", p);
  301. printk("\n");
  302. #ifdef CONFIG_BLOCK
  303. __bdevname(ROOT_DEV, b);
  304. #endif
  305. panic("VFS: Unable to mount root fs on %s", b);
  306. out:
  307. putname(fs_names);
  308. }
  309. #ifdef CONFIG_ROOT_NFS
  310. static int __init mount_nfs_root(void)
  311. {
  312. void *data = nfs_root_data();
  313. create_dev("/dev/root", ROOT_DEV);
  314. if (data &&
  315. do_mount_root("/dev/root", "nfs", root_mountflags, data) == 0)
  316. return 1;
  317. return 0;
  318. }
  319. #endif
  320. #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
  321. void __init change_floppy(char *fmt, ...)
  322. {
  323. struct termios termios;
  324. char buf[80];
  325. char c;
  326. int fd;
  327. va_list args;
  328. va_start(args, fmt);
  329. vsprintf(buf, fmt, args);
  330. va_end(args);
  331. fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0);
  332. if (fd >= 0) {
  333. sys_ioctl(fd, FDEJECT, 0);
  334. sys_close(fd);
  335. }
  336. printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
  337. fd = sys_open("/dev/console", O_RDWR, 0);
  338. if (fd >= 0) {
  339. sys_ioctl(fd, TCGETS, (long)&termios);
  340. termios.c_lflag &= ~ICANON;
  341. sys_ioctl(fd, TCSETSF, (long)&termios);
  342. sys_read(fd, &c, 1);
  343. termios.c_lflag |= ICANON;
  344. sys_ioctl(fd, TCSETSF, (long)&termios);
  345. sys_close(fd);
  346. }
  347. }
  348. #endif
  349. void __init mount_root(void)
  350. {
  351. #ifdef CONFIG_ROOT_NFS
  352. if (MAJOR(ROOT_DEV) == UNNAMED_MAJOR) {
  353. if (mount_nfs_root())
  354. return;
  355. printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
  356. ROOT_DEV = Root_FD0;
  357. }
  358. #endif
  359. #ifdef CONFIG_BLK_DEV_FD
  360. if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
  361. /* rd_doload is 2 for a dual initrd/ramload setup */
  362. if (rd_doload==2) {
  363. if (rd_load_disk(1)) {
  364. ROOT_DEV = Root_RAM1;
  365. root_device_name = NULL;
  366. }
  367. } else
  368. change_floppy("root floppy");
  369. }
  370. #endif
  371. #ifdef CONFIG_BLOCK
  372. create_dev("/dev/root", ROOT_DEV);
  373. mount_block_root("/dev/root", root_mountflags);
  374. #endif
  375. }
  376. /*
  377. * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
  378. */
  379. void __init prepare_namespace(void)
  380. {
  381. int is_floppy;
  382. if (root_delay) {
  383. printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
  384. root_delay);
  385. ssleep(root_delay);
  386. }
  387. /*
  388. * wait for the known devices to complete their probing
  389. *
  390. * Note: this is a potential source of long boot delays.
  391. * For example, it is not atypical to wait 5 seconds here
  392. * for the touchpad of a laptop to initialize.
  393. */
  394. wait_for_device_probe();
  395. md_run_setup();
  396. if (saved_root_name[0]) {
  397. root_device_name = saved_root_name;
  398. if (!strncmp(root_device_name, "mtd", 3) ||
  399. !strncmp(root_device_name, "ubi", 3)) {
  400. mount_block_root(root_device_name, root_mountflags);
  401. goto out;
  402. }
  403. ROOT_DEV = name_to_dev_t(root_device_name);
  404. if (strncmp(root_device_name, "/dev/", 5) == 0)
  405. root_device_name += 5;
  406. }
  407. if (initrd_load())
  408. goto out;
  409. /* wait for any asynchronous scanning to complete */
  410. if ((ROOT_DEV == 0) && root_wait) {
  411. printk(KERN_INFO "Waiting for root device %s...\n",
  412. saved_root_name);
  413. while (driver_probe_done() != 0 ||
  414. (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
  415. msleep(100);
  416. async_synchronize_full();
  417. }
  418. is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
  419. if (is_floppy && rd_doload && rd_load_disk(0))
  420. ROOT_DEV = Root_RAM0;
  421. mount_root();
  422. out:
  423. devtmpfs_mount("dev");
  424. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  425. sys_chroot(".");
  426. }