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