blob: 1e5b080062b5b5b7493427890f56092fe2b49938 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include <linux/module.h>
2#include <linux/sched.h>
3#include <linux/ctype.h>
4#include <linux/fd.h>
5#include <linux/tty.h>
6#include <linux/suspend.h>
7#include <linux/root_dev.h>
8#include <linux/security.h>
9#include <linux/delay.h>
10#include <linux/genhd.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
20#include <linux/nfs_fs.h>
21#include <linux/nfs_fs_sb.h>
22#include <linux/nfs_mount.h>
xf.liaa4d92f2023-09-13 00:18:58 -070023#include <linux/reboot.h>
lh9ed821d2023-04-07 01:36:19 -070024
25#include "do_mounts.h"
xf.liaa4d92f2023-09-13 00:18:58 -070026#ifdef CONFIG_FLAGS_UTILS
27#include "pub_flags.h"
28#endif
lh9ed821d2023-04-07 01:36:19 -070029
30int __initdata rd_doload; /* 1 = load RAM disk, 0 = don't load */
31
32int root_mountflags = MS_RDONLY | MS_SILENT;
33static char * __initdata root_device_name;
34static char __initdata saved_root_name[64];
35static int root_wait;
36
37dev_t ROOT_DEV;
38
39static int __init load_ramdisk(char *str)
40{
41 rd_doload = simple_strtol(str,NULL,0) & 3;
42 return 1;
43}
44__setup("load_ramdisk=", load_ramdisk);
45
46static int __init readonly(char *str)
47{
48 if (*str)
49 return 0;
50 root_mountflags |= MS_RDONLY;
51 return 1;
52}
53
54static int __init readwrite(char *str)
55{
56 if (*str)
57 return 0;
58 root_mountflags &= ~MS_RDONLY;
59 return 1;
60}
61
62__setup("ro", readonly);
63__setup("rw", readwrite);
64
65#ifdef CONFIG_BLOCK
66/**
67 * match_dev_by_uuid - callback for finding a partition using its uuid
68 * @dev: device passed in by the caller
69 * @data: opaque pointer to a 36 byte char array with a UUID
70 *
71 * Returns 1 if the device matches, and 0 otherwise.
72 */
73static int match_dev_by_uuid(struct device *dev, void *data)
74{
75 u8 *uuid = data;
76 struct hd_struct *part = dev_to_part(dev);
77
78 if (!part->info)
79 goto no_match;
80
81 if (memcmp(uuid, part->info->uuid, sizeof(part->info->uuid)))
82 goto no_match;
83
84 return 1;
85no_match:
86 return 0;
87}
88
89
90/**
91 * devt_from_partuuid - looks up the dev_t of a partition by its UUID
92 * @uuid: min 36 byte char array containing a hex ascii UUID
93 *
94 * The function will return the first partition which contains a matching
95 * UUID value in its partition_meta_info struct. This does not search
96 * by filesystem UUIDs.
97 *
98 * If @uuid is followed by a "/PARTNROFF=%d", then the number will be
99 * extracted and used as an offset from the partition identified by the UUID.
100 *
101 * Returns the matching dev_t on success or 0 on failure.
102 */
103static dev_t devt_from_partuuid(char *uuid_str)
104{
105 dev_t res = 0;
106 struct device *dev = NULL;
107 u8 uuid[16];
108 struct gendisk *disk;
109 struct hd_struct *part;
110 int offset = 0;
111
112 if (strlen(uuid_str) < 36)
113 goto done;
114
115 /* Check for optional partition number offset attributes. */
116 if (uuid_str[36]) {
117 char c = 0;
118 /* Explicitly fail on poor PARTUUID syntax. */
119 if (sscanf(&uuid_str[36],
120 "/PARTNROFF=%d%c", &offset, &c) != 1) {
121 printk(KERN_ERR "VFS: PARTUUID= is invalid.\n"
122 "Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
123 if (root_wait)
124 printk(KERN_ERR
125 "Disabling rootwait; root= is invalid.\n");
126 root_wait = 0;
127 goto done;
128 }
129 }
130
131 /* Pack the requested UUID in the expected format. */
132 part_pack_uuid(uuid_str, uuid);
133
134 dev = class_find_device(&block_class, NULL, uuid, &match_dev_by_uuid);
135 if (!dev)
136 goto done;
137
138 res = dev->devt;
139
140 /* Attempt to find the partition by offset. */
141 if (!offset)
142 goto no_offset;
143
144 res = 0;
145 disk = part_to_disk(dev_to_part(dev));
146 part = disk_get_part(disk, dev_to_part(dev)->partno + offset);
147 if (part) {
148 res = part_devt(part);
149 put_device(part_to_dev(part));
150 }
151
152no_offset:
153 put_device(dev);
154done:
155 return res;
156}
157#endif
158
159/*
160 * Convert a name into device number. We accept the following variants:
161 *
162 * 1) device number in hexadecimal represents itself
163 * 2) /dev/nfs represents Root_NFS (0xff)
164 * 3) /dev/<disk_name> represents the device number of disk
165 * 4) /dev/<disk_name><decimal> represents the device number
166 * of partition - device number of disk plus the partition number
167 * 5) /dev/<disk_name>p<decimal> - same as the above, that form is
168 * used when disk name of partitioned disk ends on a digit.
169 * 6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
170 * unique id of a partition if the partition table provides it.
171 * 7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
172 * a partition with a known unique id.
173 *
174 * If name doesn't have fall into the categories above, we return (0,0).
175 * block_class is used to check if something is a disk name. If the disk
176 * name contains slashes, the device name has them replaced with
177 * bangs.
178 */
179
180dev_t name_to_dev_t(char *name)
181{
182 char s[32];
183 char *p;
184 dev_t res = 0;
185 int part;
186
187#ifdef CONFIG_BLOCK
188 if (strncmp(name, "PARTUUID=", 9) == 0) {
189 name += 9;
190 res = devt_from_partuuid(name);
191 if (!res)
192 goto fail;
193 goto done;
194 }
195#endif
196
197 if (strncmp(name, "/dev/", 5) != 0) {
198 unsigned maj, min;
199
200 if (sscanf(name, "%u:%u", &maj, &min) == 2) {
201 res = MKDEV(maj, min);
202 if (maj != MAJOR(res) || min != MINOR(res))
203 goto fail;
204 } else {
205 res = new_decode_dev(simple_strtoul(name, &p, 16));
206 if (*p)
207 goto fail;
208 }
209 goto done;
210 }
211
212 name += 5;
213 res = Root_NFS;
214 if (strcmp(name, "nfs") == 0)
215 goto done;
216 res = Root_RAM0;
217 if (strcmp(name, "ram") == 0)
218 goto done;
219
220 if (strlen(name) > 31)
221 goto fail;
222 strncpy(s, name, sizeof(s) - 1);
223 s[sizeof(s) - 1] = '\0';
224 for (p = s; *p; p++)
225 if (*p == '/')
226 *p = '!';
227 res = blk_lookup_devt(s, 0);
228 if (res)
229 goto done;
230
231 /*
232 * try non-existent, but valid partition, which may only exist
233 * after revalidating the disk, like partitioned md devices
234 */
235 while (p > s && isdigit(p[-1]))
236 p--;
237 if (p == s || !*p || *p == '0')
238 goto fail;
239
240 /* try disk name without <part number> */
241 part = simple_strtoul(p, NULL, 10);
242 *p = '\0';
243 res = blk_lookup_devt(s, part);
244 if (res)
245 goto done;
246
247 /* try disk name without p<part number> */
248 if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
249 goto fail;
250 p[-1] = '\0';
251 res = blk_lookup_devt(s, part);
252 if (res)
253 goto done;
254
255fail:
256 return 0;
257done:
258 return res;
259}
260
261static int __init root_dev_setup(char *line)
262{
263 strlcpy(saved_root_name, line, sizeof(saved_root_name));
264 return 1;
265}
266
267__setup("root=", root_dev_setup);
268
269static int __init rootwait_setup(char *str)
270{
271 if (*str)
272 return 0;
273 root_wait = 1;
274 return 1;
275}
276
277__setup("rootwait", rootwait_setup);
278
279static char * __initdata root_mount_data;
280static int __init root_data_setup(char *str)
281{
282 root_mount_data = str;
283 return 1;
284}
285
286static char * __initdata root_fs_names;
287static int __init fs_names_setup(char *str)
288{
289 root_fs_names = str;
290 return 1;
291}
292
293static unsigned int __initdata root_delay;
294static int __init root_delay_setup(char *str)
295{
296 root_delay = simple_strtoul(str, NULL, 0);
297 return 1;
298}
299
300__setup("rootflags=", root_data_setup);
301__setup("rootfstype=", fs_names_setup);
302__setup("rootdelay=", root_delay_setup);
303
304static void __init get_fs_names(char *page)
305{
306 char *s = page;
307
308 if (root_fs_names) {
309 strcpy(page, root_fs_names);
310 while (*s++) {
311 if (s[-1] == ',')
312 s[-1] = '\0';
313 }
314 } else {
315 int len = get_filesystem_list(page);
316 char *p, *next;
317
318 page[len] = '\0';
319 for (p = page-1; p; p = next) {
320 next = strchr(++p, '\n');
321 if (*p++ != '\t')
322 continue;
323 while ((*s++ = *p++) != '\n')
324 ;
325 s[-1] = '\0';
326 }
327 }
328 *s = '\0';
329}
330
331static int __init do_mount_root(char *name, char *fs, int flags, void *data)
332{
333 struct super_block *s;
334 int err = sys_mount(name, "/root", fs, flags, data);
335 if (err)
336 return err;
337
338 sys_chdir((const char __user __force *)"/root");
339 s = current->fs->pwd.dentry->d_sb;
340 ROOT_DEV = s->s_dev;
341 printk(KERN_INFO
342 "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
343 s->s_type->name,
344 s->s_flags & MS_RDONLY ? " readonly" : "",
345 MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
346 return 0;
347}
348
349void __init mount_block_root(char *name, int flags)
350{
351 char *fs_names = __getname_gfp(GFP_KERNEL
352 | __GFP_NOTRACK_FALSE_POSITIVE);
353 char *p;
354#ifdef CONFIG_BLOCK
355 char b[BDEVNAME_SIZE];
356#else
357 const char *b = name;
358#endif
359
360 BUG_ON(!fs_names);
361 get_fs_names(fs_names);
362retry:
363 for (p = fs_names; *p; p += strlen(p)+1) {
364 int err = do_mount_root(name, p, flags, root_mount_data);
365 switch (err) {
366 case 0:
367 goto out;
368 case -EACCES:
369 flags |= MS_RDONLY;
370 goto retry;
371 case -EINVAL:
372 continue;
373 }
374 /*
375 * Allow the user to distinguish between failed sys_open
376 * and bad superblock on root device.
377 * and give them a list of the available devices
378 */
379#ifdef CONFIG_BLOCK
380 __bdevname(ROOT_DEV, b);
381#endif
382 printk("VFS: Cannot open root device \"%s\" or %s: error %d\n",
383 root_device_name, b, err);
384 printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
385
386 printk_all_partitions();
387#ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
388 printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
389 "explicit textual name for \"root=\" boot option.\n");
390#endif
xf.liaa4d92f2023-09-13 00:18:58 -0700391#ifdef CONFIG_FLAGS_UTILS
xf.lie31de8b2023-12-26 23:38:58 -0800392 extern int flags_sys_switch(void);
393 printk(KERN_EMERG "VFS: Unable to mount root fs on %s", b);
394 if (flags_sys_switch() < 0)
395 panic("VFS: flags_sys_switch fail");
xf.liaa4d92f2023-09-13 00:18:58 -0700396 else
xf.lie31de8b2023-12-26 23:38:58 -0800397 kernel_restart("VFS: Switch to another system, please reset machine");
xf.liaa4d92f2023-09-13 00:18:58 -0700398#endif
lh9ed821d2023-04-07 01:36:19 -0700399 panic("VFS: Unable to mount root fs on %s", b);
400 }
401
402 printk("List of all partitions:\n");
403 printk_all_partitions();
404 printk("No filesystem could mount root, tried: ");
405 for (p = fs_names; *p; p += strlen(p)+1)
406 printk(" %s", p);
407 printk("\n");
408#ifdef CONFIG_BLOCK
409 __bdevname(ROOT_DEV, b);
410#endif
411 panic("VFS: Unable to mount root fs on %s", b);
412out:
413 putname(fs_names);
414}
415
416#ifdef CONFIG_ROOT_NFS
417
418#define NFSROOT_TIMEOUT_MIN 5
419#define NFSROOT_TIMEOUT_MAX 30
420#define NFSROOT_RETRY_MAX 5
421
422static int __init mount_nfs_root(void)
423{
424 char *root_dev, *root_data;
425 unsigned int timeout;
426 int try, err;
427
428 err = nfs_root_data(&root_dev, &root_data);
429 if (err != 0)
430 return 0;
431
432 /*
433 * The server or network may not be ready, so try several
434 * times. Stop after a few tries in case the client wants
435 * to fall back to other boot methods.
436 */
437 timeout = NFSROOT_TIMEOUT_MIN;
438 for (try = 1; ; try++) {
439 err = do_mount_root(root_dev, "nfs",
440 root_mountflags, root_data);
441 if (err == 0)
442 return 1;
443 if (try > NFSROOT_RETRY_MAX)
444 break;
445
446 /* Wait, in case the server refused us immediately */
447 ssleep(timeout);
448 timeout <<= 1;
449 if (timeout > NFSROOT_TIMEOUT_MAX)
450 timeout = NFSROOT_TIMEOUT_MAX;
451 }
452 return 0;
453}
454#endif
455
456#if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
457void __init change_floppy(char *fmt, ...)
458{
459 struct termios termios = {0,};
460 char buf[80];
461 char c;
462 int fd;
463 va_list args;
464 va_start(args, fmt);
465 vsprintf(buf, fmt, args);
466 va_end(args);
467 fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0);
468 if (fd >= 0) {
469 sys_ioctl(fd, FDEJECT, 0);
470 sys_close(fd);
471 }
472 printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
473 fd = sys_open("/dev/console", O_RDWR, 0);
474 if (fd >= 0) {
475 sys_ioctl(fd, TCGETS, (long)&termios);
476 termios.c_lflag &= ~ICANON;
477 sys_ioctl(fd, TCSETSF, (long)&termios);
478 sys_read(fd, &c, 1);
479 termios.c_lflag |= ICANON;
480 sys_ioctl(fd, TCSETSF, (long)&termios);
481 sys_close(fd);
482 }
483}
484#endif
485
486void __init mount_root(void)
487{
488#ifdef CONFIG_ROOT_NFS
489 if (ROOT_DEV == Root_NFS) {
490 if (mount_nfs_root())
491 return;
492
493 printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
494 ROOT_DEV = Root_FD0;
495 }
496#endif
497#ifdef CONFIG_BLK_DEV_FD
498 if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
499 /* rd_doload is 2 for a dual initrd/ramload setup */
500 if (rd_doload==2) {
501 if (rd_load_disk(1)) {
502 ROOT_DEV = Root_RAM1;
503 root_device_name = NULL;
504 }
505 } else
506 change_floppy("root floppy");
507 }
508#endif
509#ifdef CONFIG_BLOCK
510 create_dev("/dev/root", ROOT_DEV);
511 mount_block_root("/dev/root", root_mountflags);
512#endif
513}
514
515/*
516 * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
517 */
518void __init prepare_namespace(void)
519{
520 int is_floppy;
521
522 if (root_delay) {
523 printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
524 root_delay);
525 ssleep(root_delay);
526 }
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
537 md_run_setup();
538
539 if (saved_root_name[0]) {
540 root_device_name = saved_root_name;
541 if (!strncmp(root_device_name, "mtd", 3) ||
542 !strncmp(root_device_name, "ubi", 3)) {
543 mount_block_root(root_device_name, root_mountflags);
544 goto out;
545 }
546 ROOT_DEV = name_to_dev_t(root_device_name);
547 if (strncmp(root_device_name, "/dev/", 5) == 0)
548 root_device_name += 5;
549 }
550
551 if (initrd_load())
552 goto out;
553
554 /* wait for any asynchronous scanning to complete */
555 if ((ROOT_DEV == 0) && root_wait) {
556 printk(KERN_INFO "Waiting for root device %s...\n",
557 saved_root_name);
558 while (driver_probe_done() != 0 ||
559 (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
560 msleep(100);
561 async_synchronize_full();
562 }
563
564 is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
565
566 if (is_floppy && rd_doload && rd_load_disk(0))
567 ROOT_DEV = Root_RAM0;
568
569 mount_root();
570out:
571 devtmpfs_mount("dev");
572 sys_mount(".", "/", NULL, MS_MOVE, NULL);
573 sys_chroot((const char __user __force *)".");
574}