do_mounts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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/ktime.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 saved_root_name[64];
  30. static int root_wait;
  31. dev_t ROOT_DEV;
  32. static int __init readonly(char *str)
  33. {
  34. if (*str)
  35. return 0;
  36. root_mountflags |= MS_RDONLY;
  37. return 1;
  38. }
  39. static int __init readwrite(char *str)
  40. {
  41. if (*str)
  42. return 0;
  43. root_mountflags &= ~MS_RDONLY;
  44. return 1;
  45. }
  46. __setup("ro", readonly);
  47. __setup("rw", readwrite);
  48. static int __init root_dev_setup(char *line)
  49. {
  50. strscpy(saved_root_name, line, sizeof(saved_root_name));
  51. return 1;
  52. }
  53. __setup("root=", root_dev_setup);
  54. static int __init rootwait_setup(char *str)
  55. {
  56. if (*str)
  57. return 0;
  58. root_wait = -1;
  59. return 1;
  60. }
  61. __setup("rootwait", rootwait_setup);
  62. static int __init rootwait_timeout_setup(char *str)
  63. {
  64. int sec;
  65. if (kstrtoint(str, 0, &sec) || sec < 0) {
  66. pr_warn("ignoring invalid rootwait value\n");
  67. goto ignore;
  68. }
  69. if (check_mul_overflow(sec, MSEC_PER_SEC, &root_wait)) {
  70. pr_warn("ignoring excessive rootwait value\n");
  71. goto ignore;
  72. }
  73. return 1;
  74. ignore:
  75. /* Fallback to indefinite wait */
  76. root_wait = -1;
  77. return 1;
  78. }
  79. __setup("rootwait=", rootwait_timeout_setup);
  80. static char * __initdata root_mount_data;
  81. static int __init root_data_setup(char *str)
  82. {
  83. root_mount_data = str;
  84. return 1;
  85. }
  86. static char * __initdata root_fs_names;
  87. static int __init fs_names_setup(char *str)
  88. {
  89. root_fs_names = str;
  90. return 1;
  91. }
  92. static unsigned int __initdata root_delay;
  93. static int __init root_delay_setup(char *str)
  94. {
  95. if (kstrtouint(str, 0, &root_delay))
  96. return 0;
  97. return 1;
  98. }
  99. __setup("rootflags=", root_data_setup);
  100. __setup("rootfstype=", fs_names_setup);
  101. __setup("rootdelay=", root_delay_setup);
  102. /* This can return zero length strings. Caller should check */
  103. static int __init split_fs_names(char *page, size_t size)
  104. {
  105. int count = 1;
  106. char *p = page;
  107. strscpy(p, root_fs_names, size);
  108. while (*p++) {
  109. if (p[-1] == ',') {
  110. p[-1] = '\0';
  111. count++;
  112. }
  113. }
  114. return count;
  115. }
  116. static int __init do_mount_root(const char *name, const char *fs,
  117. const int flags, const void *data)
  118. {
  119. struct super_block *s;
  120. struct page *p = NULL;
  121. char *data_page = NULL;
  122. int ret;
  123. if (data) {
  124. /* init_mount() requires a full page as fifth argument */
  125. p = alloc_page(GFP_KERNEL);
  126. if (!p)
  127. return -ENOMEM;
  128. data_page = page_address(p);
  129. strscpy_pad(data_page, data, PAGE_SIZE);
  130. }
  131. ret = init_mount(name, "/root", fs, flags, data_page);
  132. if (ret)
  133. goto out;
  134. init_chdir("/root");
  135. s = current->fs->pwd.dentry->d_sb;
  136. ROOT_DEV = s->s_dev;
  137. printk(KERN_INFO
  138. "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
  139. s->s_type->name,
  140. sb_rdonly(s) ? " readonly" : "",
  141. MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
  142. out:
  143. if (p)
  144. put_page(p);
  145. return ret;
  146. }
  147. void __init mount_root_generic(char *name, char *pretty_name, int flags)
  148. {
  149. struct page *page = alloc_page(GFP_KERNEL);
  150. char *fs_names = page_address(page);
  151. char *p;
  152. char b[BDEVNAME_SIZE];
  153. int num_fs, i;
  154. scnprintf(b, BDEVNAME_SIZE, "unknown-block(%u,%u)",
  155. MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
  156. if (root_fs_names)
  157. num_fs = split_fs_names(fs_names, PAGE_SIZE);
  158. else
  159. num_fs = list_bdev_fs_names(fs_names, PAGE_SIZE);
  160. retry:
  161. for (i = 0, p = fs_names; i < num_fs; i++, p += strlen(p)+1) {
  162. int err;
  163. if (!*p)
  164. continue;
  165. err = do_mount_root(name, p, flags, root_mount_data);
  166. switch (err) {
  167. case 0:
  168. goto out;
  169. case -EACCES:
  170. case -EINVAL:
  171. #ifdef CONFIG_BLOCK
  172. init_flush_fput();
  173. #endif
  174. continue;
  175. }
  176. /*
  177. * Allow the user to distinguish between failed sys_open
  178. * and bad superblock on root device.
  179. * and give them a list of the available devices
  180. */
  181. printk("VFS: Cannot open root device \"%s\" or %s: error %d\n",
  182. pretty_name, b, err);
  183. printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
  184. printk_all_partitions();
  185. if (root_fs_names)
  186. num_fs = list_bdev_fs_names(fs_names, PAGE_SIZE);
  187. if (!num_fs)
  188. pr_err("Can't find any bdev filesystem to be used for mount!\n");
  189. else {
  190. pr_err("List of all bdev filesystems:\n");
  191. for (i = 0, p = fs_names; i < num_fs; i++, p += strlen(p)+1)
  192. pr_err(" %s", p);
  193. pr_err("\n");
  194. }
  195. panic("VFS: Unable to mount root fs on %s", b);
  196. }
  197. if (!(flags & SB_RDONLY)) {
  198. flags |= SB_RDONLY;
  199. goto retry;
  200. }
  201. printk("List of all partitions:\n");
  202. printk_all_partitions();
  203. printk("No filesystem could mount root, tried: ");
  204. for (i = 0, p = fs_names; i < num_fs; i++, p += strlen(p)+1)
  205. printk(" %s", p);
  206. printk("\n");
  207. panic("VFS: Unable to mount root fs on \"%s\" or %s", pretty_name, b);
  208. out:
  209. put_page(page);
  210. }
  211. #ifdef CONFIG_ROOT_NFS
  212. #define NFSROOT_TIMEOUT_MIN 5
  213. #define NFSROOT_TIMEOUT_MAX 30
  214. #define NFSROOT_RETRY_MAX 5
  215. static void __init mount_nfs_root(void)
  216. {
  217. char *root_dev, *root_data;
  218. unsigned int timeout;
  219. int try;
  220. if (nfs_root_data(&root_dev, &root_data))
  221. goto fail;
  222. /*
  223. * The server or network may not be ready, so try several
  224. * times. Stop after a few tries in case the client wants
  225. * to fall back to other boot methods.
  226. */
  227. timeout = NFSROOT_TIMEOUT_MIN;
  228. for (try = 1; ; try++) {
  229. if (!do_mount_root(root_dev, "nfs", root_mountflags, root_data))
  230. return;
  231. if (try > NFSROOT_RETRY_MAX)
  232. break;
  233. /* Wait, in case the server refused us immediately */
  234. ssleep(timeout);
  235. timeout <<= 1;
  236. if (timeout > NFSROOT_TIMEOUT_MAX)
  237. timeout = NFSROOT_TIMEOUT_MAX;
  238. }
  239. fail:
  240. pr_err("VFS: Unable to mount root fs via NFS.\n");
  241. }
  242. #else
  243. static inline void mount_nfs_root(void)
  244. {
  245. }
  246. #endif /* CONFIG_ROOT_NFS */
  247. #ifdef CONFIG_CIFS_ROOT
  248. #define CIFSROOT_TIMEOUT_MIN 5
  249. #define CIFSROOT_TIMEOUT_MAX 30
  250. #define CIFSROOT_RETRY_MAX 5
  251. static void __init mount_cifs_root(void)
  252. {
  253. char *root_dev, *root_data;
  254. unsigned int timeout;
  255. int try;
  256. if (cifs_root_data(&root_dev, &root_data))
  257. goto fail;
  258. timeout = CIFSROOT_TIMEOUT_MIN;
  259. for (try = 1; ; try++) {
  260. if (!do_mount_root(root_dev, "cifs", root_mountflags,
  261. root_data))
  262. return;
  263. if (try > CIFSROOT_RETRY_MAX)
  264. break;
  265. ssleep(timeout);
  266. timeout <<= 1;
  267. if (timeout > CIFSROOT_TIMEOUT_MAX)
  268. timeout = CIFSROOT_TIMEOUT_MAX;
  269. }
  270. fail:
  271. pr_err("VFS: Unable to mount root fs via SMB.\n");
  272. }
  273. #else
  274. static inline void mount_cifs_root(void)
  275. {
  276. }
  277. #endif /* CONFIG_CIFS_ROOT */
  278. static bool __init fs_is_nodev(char *fstype)
  279. {
  280. struct file_system_type *fs = get_fs_type(fstype);
  281. bool ret = false;
  282. if (fs) {
  283. ret = !(fs->fs_flags & FS_REQUIRES_DEV);
  284. put_filesystem(fs);
  285. }
  286. return ret;
  287. }
  288. static int __init mount_nodev_root(char *root_device_name)
  289. {
  290. char *fs_names, *fstype;
  291. int err = -EINVAL;
  292. int num_fs, i;
  293. fs_names = (void *)__get_free_page(GFP_KERNEL);
  294. if (!fs_names)
  295. return -EINVAL;
  296. num_fs = split_fs_names(fs_names, PAGE_SIZE);
  297. for (i = 0, fstype = fs_names; i < num_fs;
  298. i++, fstype += strlen(fstype) + 1) {
  299. if (!*fstype)
  300. continue;
  301. if (!fs_is_nodev(fstype))
  302. continue;
  303. err = do_mount_root(root_device_name, fstype, root_mountflags,
  304. root_mount_data);
  305. if (!err)
  306. break;
  307. }
  308. free_page((unsigned long)fs_names);
  309. return err;
  310. }
  311. #ifdef CONFIG_BLOCK
  312. static void __init mount_block_root(char *root_device_name)
  313. {
  314. int err = create_dev("/dev/root", ROOT_DEV);
  315. if (err < 0)
  316. pr_emerg("Failed to create /dev/root: %d\n", err);
  317. mount_root_generic("/dev/root", root_device_name, root_mountflags);
  318. }
  319. #else
  320. static inline void mount_block_root(char *root_device_name)
  321. {
  322. }
  323. #endif /* CONFIG_BLOCK */
  324. void __init mount_root(char *root_device_name)
  325. {
  326. switch (ROOT_DEV) {
  327. case Root_NFS:
  328. mount_nfs_root();
  329. break;
  330. case Root_CIFS:
  331. mount_cifs_root();
  332. break;
  333. case Root_Generic:
  334. mount_root_generic(root_device_name, root_device_name,
  335. root_mountflags);
  336. break;
  337. case 0:
  338. if (root_device_name && root_fs_names &&
  339. mount_nodev_root(root_device_name) == 0)
  340. break;
  341. fallthrough;
  342. default:
  343. mount_block_root(root_device_name);
  344. break;
  345. }
  346. }
  347. /* wait for any asynchronous scanning to complete */
  348. static void __init wait_for_root(char *root_device_name)
  349. {
  350. ktime_t end;
  351. if (ROOT_DEV != 0)
  352. return;
  353. pr_info("Waiting for root device %s...\n", root_device_name);
  354. end = ktime_add_ms(ktime_get_raw(), root_wait);
  355. while (!driver_probe_done() ||
  356. early_lookup_bdev(root_device_name, &ROOT_DEV) < 0) {
  357. msleep(5);
  358. if (root_wait > 0 && ktime_after(ktime_get_raw(), end))
  359. break;
  360. }
  361. async_synchronize_full();
  362. }
  363. static dev_t __init parse_root_device(char *root_device_name)
  364. {
  365. int error;
  366. dev_t dev;
  367. if (!strncmp(root_device_name, "mtd", 3) ||
  368. !strncmp(root_device_name, "ubi", 3))
  369. return Root_Generic;
  370. if (strcmp(root_device_name, "/dev/nfs") == 0)
  371. return Root_NFS;
  372. if (strcmp(root_device_name, "/dev/cifs") == 0)
  373. return Root_CIFS;
  374. if (strcmp(root_device_name, "/dev/ram") == 0)
  375. return Root_RAM0;
  376. error = early_lookup_bdev(root_device_name, &dev);
  377. if (error) {
  378. if (error == -EINVAL && root_wait) {
  379. pr_err("Disabling rootwait; root= is invalid.\n");
  380. root_wait = 0;
  381. }
  382. return 0;
  383. }
  384. return dev;
  385. }
  386. /*
  387. * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
  388. */
  389. void __init prepare_namespace(void)
  390. {
  391. if (root_delay) {
  392. printk(KERN_INFO "Waiting %d sec before mounting root device...\n",
  393. root_delay);
  394. ssleep(root_delay);
  395. }
  396. /*
  397. * wait for the known devices to complete their probing
  398. *
  399. * Note: this is a potential source of long boot delays.
  400. * For example, it is not atypical to wait 5 seconds here
  401. * for the touchpad of a laptop to initialize.
  402. */
  403. wait_for_device_probe();
  404. md_run_setup();
  405. if (saved_root_name[0])
  406. ROOT_DEV = parse_root_device(saved_root_name);
  407. initrd_load();
  408. if (root_wait)
  409. wait_for_root(saved_root_name);
  410. mount_root(saved_root_name);
  411. devtmpfs_mount();
  412. if (init_pivot_root(".", ".")) {
  413. pr_err("VFS: Failed to pivot into new rootfs\n");
  414. return;
  415. }
  416. if (init_umount(".", MNT_DETACH)) {
  417. pr_err("VFS: Failed to unmount old rootfs\n");
  418. return;
  419. }
  420. pr_info("VFS: Pivoted into new rootfs\n");
  421. }
  422. static bool is_tmpfs;
  423. static int rootfs_init_fs_context(struct fs_context *fc)
  424. {
  425. if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
  426. return shmem_init_fs_context(fc);
  427. return ramfs_init_fs_context(fc);
  428. }
  429. struct file_system_type rootfs_fs_type = {
  430. .name = "rootfs",
  431. .init_fs_context = rootfs_init_fs_context,
  432. .kill_sb = kill_anon_super,
  433. };
  434. void __init init_rootfs(void)
  435. {
  436. if (IS_ENABLED(CONFIG_TMPFS)) {
  437. if (!saved_root_name[0] && !root_fs_names)
  438. is_tmpfs = true;
  439. else if (root_fs_names && !!strstr(root_fs_names, "tmpfs"))
  440. is_tmpfs = true;
  441. }
  442. }