do_mounts.c 16 KB

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