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