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