do_mounts.c 15 KB

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