do_mounts.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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/ramfs.h>
  20. #include <linux/shmem_fs.h>
  21. #include <linux/nfs_fs.h>
  22. #include <linux/nfs_fs_sb.h>
  23. #include <linux/nfs_mount.h>
  24. #include "do_mounts.h"
  25. int __initdata rd_doload; /* 1 = load RAM disk, 0 = don't load */
  26. int root_mountflags = MS_RDONLY | MS_SILENT;
  27. static char * __initdata root_device_name;
  28. static char __initdata saved_root_name[64];
  29. static int root_wait;
  30. dev_t ROOT_DEV;
  31. static int __init load_ramdisk(char *str)
  32. {
  33. rd_doload = simple_strtol(str,NULL,0) & 3;
  34. return 1;
  35. }
  36. __setup("load_ramdisk=", load_ramdisk);
  37. static int __init readonly(char *str)
  38. {
  39. if (*str)
  40. return 0;
  41. root_mountflags |= MS_RDONLY;
  42. return 1;
  43. }
  44. static int __init readwrite(char *str)
  45. {
  46. if (*str)
  47. return 0;
  48. root_mountflags &= ~MS_RDONLY;
  49. return 1;
  50. }
  51. __setup("ro", readonly);
  52. __setup("rw", readwrite);
  53. #ifdef CONFIG_BLOCK
  54. struct uuidcmp {
  55. const char *uuid;
  56. int len;
  57. };
  58. /**
  59. * match_dev_by_uuid - callback for finding a partition using its uuid
  60. * @dev: device passed in by the caller
  61. * @data: opaque pointer to the desired struct uuidcmp to match
  62. *
  63. * Returns 1 if the device matches, and 0 otherwise.
  64. */
  65. static int match_dev_by_uuid(struct device *dev, const void *data)
  66. {
  67. const struct uuidcmp *cmp = data;
  68. struct hd_struct *part = dev_to_part(dev);
  69. if (!part->info)
  70. goto no_match;
  71. if (strncasecmp(cmp->uuid, part->info->uuid, cmp->len))
  72. goto no_match;
  73. return 1;
  74. no_match:
  75. return 0;
  76. }
  77. /**
  78. * devt_from_partuuid - looks up the dev_t of a partition by its UUID
  79. * @uuid_str: char array containing ascii UUID
  80. *
  81. * The function will return the first partition which contains a matching
  82. * UUID value in its partition_meta_info struct. This does not search
  83. * by filesystem UUIDs.
  84. *
  85. * If @uuid_str is followed by a "/PARTNROFF=%d", then the number will be
  86. * extracted and used as an offset from the partition identified by the UUID.
  87. *
  88. * Returns the matching dev_t on success or 0 on failure.
  89. */
  90. static dev_t devt_from_partuuid(const char *uuid_str)
  91. {
  92. dev_t res = 0;
  93. struct uuidcmp cmp;
  94. struct device *dev = NULL;
  95. struct gendisk *disk;
  96. struct hd_struct *part;
  97. int offset = 0;
  98. bool clear_root_wait = false;
  99. char *slash;
  100. cmp.uuid = uuid_str;
  101. slash = strchr(uuid_str, '/');
  102. /* Check for optional partition number offset attributes. */
  103. if (slash) {
  104. char c = 0;
  105. /* Explicitly fail on poor PARTUUID syntax. */
  106. if (sscanf(slash + 1,
  107. "PARTNROFF=%d%c", &offset, &c) != 1) {
  108. clear_root_wait = true;
  109. goto done;
  110. }
  111. cmp.len = slash - uuid_str;
  112. } else {
  113. cmp.len = strlen(uuid_str);
  114. }
  115. if (!cmp.len) {
  116. clear_root_wait = true;
  117. goto done;
  118. }
  119. dev = class_find_device(&block_class, NULL, &cmp,
  120. &match_dev_by_uuid);
  121. if (!dev)
  122. goto done;
  123. res = dev->devt;
  124. /* Attempt to find the partition by offset. */
  125. if (!offset)
  126. goto no_offset;
  127. res = 0;
  128. disk = part_to_disk(dev_to_part(dev));
  129. part = disk_get_part(disk, dev_to_part(dev)->partno + offset);
  130. if (part) {
  131. res = part_devt(part);
  132. put_device(part_to_dev(part));
  133. }
  134. no_offset:
  135. put_device(dev);
  136. done:
  137. if (clear_root_wait) {
  138. pr_err("VFS: PARTUUID= is invalid.\n"
  139. "Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
  140. if (root_wait)
  141. pr_err("Disabling rootwait; root= is invalid.\n");
  142. root_wait = 0;
  143. }
  144. return res;
  145. }
  146. /**
  147. * match_dev_by_label - callback for finding a partition using its label
  148. * @dev: device passed in by the caller
  149. * @data: opaque pointer to the label to match
  150. *
  151. * Returns 1 if the device matches, and 0 otherwise.
  152. */
  153. static int match_dev_by_label(struct device *dev, const void *data)
  154. {
  155. const char *label = data;
  156. struct hd_struct *part = dev_to_part(dev);
  157. if (part->info && !strcmp(label, part->info->volname))
  158. return 1;
  159. return 0;
  160. }
  161. #endif
  162. /*
  163. * Convert a name into device number. We accept the following variants:
  164. *
  165. * 1) <hex_major><hex_minor> device number in hexadecimal represents itself
  166. * no leading 0x, for example b302.
  167. * 2) /dev/nfs represents Root_NFS (0xff)
  168. * 3) /dev/<disk_name> represents the device number of disk
  169. * 4) /dev/<disk_name><decimal> represents the device number
  170. * of partition - device number of disk plus the partition number
  171. * 5) /dev/<disk_name>p<decimal> - same as the above, that form is
  172. * used when disk name of partitioned disk ends on a digit.
  173. * 6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
  174. * unique id of a partition if the partition table provides it.
  175. * The UUID may be either an EFI/GPT UUID, or refer to an MSDOS
  176. * partition using the format SSSSSSSS-PP, where SSSSSSSS is a zero-
  177. * filled hex representation of the 32-bit "NT disk signature", and PP
  178. * is a zero-filled hex representation of the 1-based partition number.
  179. * 7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
  180. * a partition with a known unique id.
  181. * 8) <major>:<minor> major and minor number of the device separated by
  182. * a colon.
  183. * 9) PARTLABEL=<name> with name being the GPT partition label.
  184. * MSDOS partitions do not support labels!
  185. *
  186. * If name doesn't have fall into the categories above, we return (0,0).
  187. * block_class is used to check if something is a disk name. If the disk
  188. * name contains slashes, the device name has them replaced with
  189. * bangs.
  190. */
  191. dev_t name_to_dev_t(const char *name)
  192. {
  193. char s[32];
  194. char *p;
  195. dev_t res = 0;
  196. int part;
  197. #ifdef CONFIG_BLOCK
  198. if (strncmp(name, "PARTUUID=", 9) == 0) {
  199. name += 9;
  200. res = devt_from_partuuid(name);
  201. if (!res)
  202. goto fail;
  203. goto done;
  204. } else if (strncmp(name, "PARTLABEL=", 10) == 0) {
  205. struct device *dev;
  206. dev = class_find_device(&block_class, NULL, name + 10,
  207. &match_dev_by_label);
  208. if (!dev)
  209. goto fail;
  210. res = dev->devt;
  211. put_device(dev);
  212. goto done;
  213. }
  214. #endif
  215. if (strncmp(name, "/dev/", 5) != 0) {
  216. unsigned maj, min, offset;
  217. char dummy;
  218. if ((sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2) ||
  219. (sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) == 3)) {
  220. res = MKDEV(maj, min);
  221. if (maj != MAJOR(res) || min != MINOR(res))
  222. goto fail;
  223. } else {
  224. res = new_decode_dev(simple_strtoul(name, &p, 16));
  225. if (*p)
  226. goto fail;
  227. }
  228. goto done;
  229. }
  230. name += 5;
  231. res = Root_NFS;
  232. if (strcmp(name, "nfs") == 0)
  233. goto done;
  234. res = Root_RAM0;
  235. if (strcmp(name, "ram") == 0)
  236. goto done;
  237. if (strlen(name) > 31)
  238. goto fail;
  239. strcpy(s, name);
  240. for (p = s; *p; p++)
  241. if (*p == '/')
  242. *p = '!';
  243. res = blk_lookup_devt(s, 0);
  244. if (res)
  245. goto done;
  246. /*
  247. * try non-existent, but valid partition, which may only exist
  248. * after revalidating the disk, like partitioned md devices
  249. */
  250. while (p > s && isdigit(p[-1]))
  251. p--;
  252. if (p == s || !*p || *p == '0')
  253. goto fail;
  254. /* try disk name without <part number> */
  255. part = simple_strtoul(p, NULL, 10);
  256. *p = '\0';
  257. res = blk_lookup_devt(s, part);
  258. if (res)
  259. goto done;
  260. /* try disk name without p<part number> */
  261. if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
  262. goto fail;
  263. p[-1] = '\0';
  264. res = blk_lookup_devt(s, part);
  265. if (res)
  266. goto done;
  267. fail:
  268. return 0;
  269. done:
  270. return res;
  271. }
  272. EXPORT_SYMBOL_GPL(name_to_dev_t);
  273. static int __init root_dev_setup(char *line)
  274. {
  275. strlcpy(saved_root_name, line, sizeof(saved_root_name));
  276. return 1;
  277. }
  278. __setup("root=", root_dev_setup);
  279. static int __init rootwait_setup(char *str)
  280. {
  281. if (*str)
  282. return 0;
  283. root_wait = 1;
  284. return 1;
  285. }
  286. __setup("rootwait", rootwait_setup);
  287. static char * __initdata root_mount_data;
  288. static int __init root_data_setup(char *str)
  289. {
  290. root_mount_data = str;
  291. return 1;
  292. }
  293. static char * __initdata root_fs_names;
  294. static int __init fs_names_setup(char *str)
  295. {
  296. root_fs_names = str;
  297. return 1;
  298. }
  299. static unsigned int __initdata root_delay;
  300. static int __init root_delay_setup(char *str)
  301. {
  302. root_delay = simple_strtoul(str, NULL, 0);
  303. return 1;
  304. }
  305. __setup("rootflags=", root_data_setup);
  306. __setup("rootfstype=", fs_names_setup);
  307. __setup("rootdelay=", root_delay_setup);
  308. static void __init get_fs_names(char *page)
  309. {
  310. char *s = page;
  311. if (root_fs_names) {
  312. strcpy(page, root_fs_names);
  313. while (*s++) {
  314. if (s[-1] == ',')
  315. s[-1] = '\0';
  316. }
  317. } else {
  318. int len = get_filesystem_list(page);
  319. char *p, *next;
  320. page[len] = '\0';
  321. for (p = page-1; p; p = next) {
  322. next = strchr(++p, '\n');
  323. if (*p++ != '\t')
  324. continue;
  325. while ((*s++ = *p++) != '\n')
  326. ;
  327. s[-1] = '\0';
  328. }
  329. }
  330. *s = '\0';
  331. }
  332. static int __init do_mount_root(char *name, char *fs, int flags, void *data)
  333. {
  334. struct super_block *s;
  335. int err = ksys_mount(name, "/root", fs, flags, data);
  336. if (err)
  337. return err;
  338. ksys_chdir("/root");
  339. s = current->fs->pwd.dentry->d_sb;
  340. ROOT_DEV = s->s_dev;
  341. printk(KERN_INFO
  342. "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
  343. s->s_type->name,
  344. sb_rdonly(s) ? " readonly" : "",
  345. MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
  346. return 0;
  347. }
  348. void __init mount_block_root(char *name, int flags)
  349. {
  350. struct page *page = alloc_page(GFP_KERNEL);
  351. char *fs_names = page_address(page);
  352. char *p;
  353. #ifdef CONFIG_BLOCK
  354. char b[BDEVNAME_SIZE];
  355. #else
  356. const char *b = name;
  357. #endif
  358. get_fs_names(fs_names);
  359. retry:
  360. for (p = fs_names; *p; p += strlen(p)+1) {
  361. int err = do_mount_root(name, p, flags, root_mount_data);
  362. switch (err) {
  363. case 0:
  364. goto out;
  365. case -EACCES:
  366. case -EINVAL:
  367. continue;
  368. }
  369. /*
  370. * Allow the user to distinguish between failed sys_open
  371. * and bad superblock on root device.
  372. * and give them a list of the available devices
  373. */
  374. #ifdef CONFIG_BLOCK
  375. __bdevname(ROOT_DEV, b);
  376. #endif
  377. printk("VFS: Cannot open root device \"%s\" or %s: error %d\n",
  378. root_device_name, b, err);
  379. printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
  380. printk_all_partitions();
  381. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  382. printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
  383. "explicit textual name for \"root=\" boot option.\n");
  384. #endif
  385. panic("VFS: Unable to mount root fs on %s", b);
  386. }
  387. if (!(flags & SB_RDONLY)) {
  388. flags |= SB_RDONLY;
  389. goto retry;
  390. }
  391. printk("List of all partitions:\n");
  392. printk_all_partitions();
  393. printk("No filesystem could mount root, tried: ");
  394. for (p = fs_names; *p; p += strlen(p)+1)
  395. printk(" %s", p);
  396. printk("\n");
  397. #ifdef CONFIG_BLOCK
  398. __bdevname(ROOT_DEV, b);
  399. #endif
  400. panic("VFS: Unable to mount root fs on %s", b);
  401. out:
  402. put_page(page);
  403. }
  404. #ifdef CONFIG_ROOT_NFS
  405. #define NFSROOT_TIMEOUT_MIN 5
  406. #define NFSROOT_TIMEOUT_MAX 30
  407. #define NFSROOT_RETRY_MAX 5
  408. static int __init mount_nfs_root(void)
  409. {
  410. char *root_dev, *root_data;
  411. unsigned int timeout;
  412. int try, err;
  413. err = nfs_root_data(&root_dev, &root_data);
  414. if (err != 0)
  415. return 0;
  416. /*
  417. * The server or network may not be ready, so try several
  418. * times. Stop after a few tries in case the client wants
  419. * to fall back to other boot methods.
  420. */
  421. timeout = NFSROOT_TIMEOUT_MIN;
  422. for (try = 1; ; try++) {
  423. err = do_mount_root(root_dev, "nfs",
  424. root_mountflags, root_data);
  425. if (err == 0)
  426. return 1;
  427. if (try > NFSROOT_RETRY_MAX)
  428. break;
  429. /* Wait, in case the server refused us immediately */
  430. ssleep(timeout);
  431. timeout <<= 1;
  432. if (timeout > NFSROOT_TIMEOUT_MAX)
  433. timeout = NFSROOT_TIMEOUT_MAX;
  434. }
  435. return 0;
  436. }
  437. #endif
  438. #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
  439. void __init change_floppy(char *fmt, ...)
  440. {
  441. struct termios termios;
  442. char buf[80];
  443. char c;
  444. int fd;
  445. va_list args;
  446. va_start(args, fmt);
  447. vsprintf(buf, fmt, args);
  448. va_end(args);
  449. fd = ksys_open("/dev/root", O_RDWR | O_NDELAY, 0);
  450. if (fd >= 0) {
  451. ksys_ioctl(fd, FDEJECT, 0);
  452. ksys_close(fd);
  453. }
  454. printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
  455. fd = ksys_open("/dev/console", O_RDWR, 0);
  456. if (fd >= 0) {
  457. ksys_ioctl(fd, TCGETS, (long)&termios);
  458. termios.c_lflag &= ~ICANON;
  459. ksys_ioctl(fd, TCSETSF, (long)&termios);
  460. ksys_read(fd, &c, 1);
  461. termios.c_lflag |= ICANON;
  462. ksys_ioctl(fd, TCSETSF, (long)&termios);
  463. ksys_close(fd);
  464. }
  465. }
  466. #endif
  467. void __init mount_root(void)
  468. {
  469. #ifdef CONFIG_ROOT_NFS
  470. if (ROOT_DEV == Root_NFS) {
  471. if (mount_nfs_root())
  472. return;
  473. printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
  474. ROOT_DEV = Root_FD0;
  475. }
  476. #endif
  477. #ifdef CONFIG_BLK_DEV_FD
  478. if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
  479. /* rd_doload is 2 for a dual initrd/ramload setup */
  480. if (rd_doload==2) {
  481. if (rd_load_disk(1)) {
  482. ROOT_DEV = Root_RAM1;
  483. root_device_name = NULL;
  484. }
  485. } else
  486. change_floppy("root floppy");
  487. }
  488. #endif
  489. #ifdef CONFIG_BLOCK
  490. {
  491. int err = create_dev("/dev/root", ROOT_DEV);
  492. if (err < 0)
  493. pr_emerg("Failed to create /dev/root: %d\n", err);
  494. mount_block_root("/dev/root", root_mountflags);
  495. }
  496. #endif
  497. }
  498. /*
  499. * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
  500. */
  501. void __init prepare_namespace(void)
  502. {
  503. int is_floppy;
  504. if (root_delay) {
  505. printk(KERN_INFO "Waiting %d sec before mounting root device...\n",
  506. root_delay);
  507. ssleep(root_delay);
  508. }
  509. /*
  510. * wait for the known devices to complete their probing
  511. *
  512. * Note: this is a potential source of long boot delays.
  513. * For example, it is not atypical to wait 5 seconds here
  514. * for the touchpad of a laptop to initialize.
  515. */
  516. wait_for_device_probe();
  517. md_run_setup();
  518. if (saved_root_name[0]) {
  519. root_device_name = saved_root_name;
  520. if (!strncmp(root_device_name, "mtd", 3) ||
  521. !strncmp(root_device_name, "ubi", 3)) {
  522. mount_block_root(root_device_name, root_mountflags);
  523. goto out;
  524. }
  525. ROOT_DEV = name_to_dev_t(root_device_name);
  526. if (strncmp(root_device_name, "/dev/", 5) == 0)
  527. root_device_name += 5;
  528. }
  529. if (initrd_load())
  530. goto out;
  531. /* wait for any asynchronous scanning to complete */
  532. if ((ROOT_DEV == 0) && root_wait) {
  533. printk(KERN_INFO "Waiting for root device %s...\n",
  534. saved_root_name);
  535. while (driver_probe_done() != 0 ||
  536. (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
  537. msleep(5);
  538. async_synchronize_full();
  539. }
  540. is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
  541. if (is_floppy && rd_doload && rd_load_disk(0))
  542. ROOT_DEV = Root_RAM0;
  543. mount_root();
  544. out:
  545. devtmpfs_mount("dev");
  546. ksys_mount(".", "/", NULL, MS_MOVE, NULL);
  547. ksys_chroot(".");
  548. }
  549. static bool is_tmpfs;
  550. static struct dentry *rootfs_mount(struct file_system_type *fs_type,
  551. int flags, const char *dev_name, void *data)
  552. {
  553. static unsigned long once;
  554. void *fill = ramfs_fill_super;
  555. if (test_and_set_bit(0, &once))
  556. return ERR_PTR(-ENODEV);
  557. if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
  558. fill = shmem_fill_super;
  559. return mount_nodev(fs_type, flags, data, fill);
  560. }
  561. static struct file_system_type rootfs_fs_type = {
  562. .name = "rootfs",
  563. .mount = rootfs_mount,
  564. .kill_sb = kill_litter_super,
  565. };
  566. int __init init_rootfs(void)
  567. {
  568. int err = register_filesystem(&rootfs_fs_type);
  569. if (err)
  570. return err;
  571. if (IS_ENABLED(CONFIG_TMPFS) && !saved_root_name[0] &&
  572. (!root_fs_names || strstr(root_fs_names, "tmpfs"))) {
  573. err = shmem_init();
  574. is_tmpfs = true;
  575. } else {
  576. err = init_ramfs_fs();
  577. }
  578. if (err)
  579. unregister_filesystem(&rootfs_fs_type);
  580. return err;
  581. }