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