xref: /openbmc/linux/init/do_mounts.c (revision c2637e80)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds #include <linux/module.h>
31da177e4SLinus Torvalds #include <linux/sched.h>
41da177e4SLinus Torvalds #include <linux/ctype.h>
51da177e4SLinus Torvalds #include <linux/fd.h>
61da177e4SLinus Torvalds #include <linux/tty.h>
71da177e4SLinus Torvalds #include <linux/suspend.h>
81da177e4SLinus Torvalds #include <linux/root_dev.h>
91da177e4SLinus Torvalds #include <linux/security.h>
101da177e4SLinus Torvalds #include <linux/delay.h>
11dd2a345fSDave Gilbert #include <linux/genhd.h>
12d53d9f16SAndrew Morton #include <linux/mount.h>
13d779249eSGreg Kroah-Hartman #include <linux/device.h>
1446595390SAdrian Bunk #include <linux/init.h>
15011e3fcdSAdrian Bunk #include <linux/fs.h>
1682c8253aSAdrian Bunk #include <linux/initrd.h>
1722a9d645SArjan van de Ven #include <linux/async.h>
185ad4e53bSAl Viro #include <linux/fs_struct.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
2057f150a5SRob Landley #include <linux/ramfs.h>
2116203a7aSRob Landley #include <linux/shmem_fs.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/nfs_fs.h>
241da177e4SLinus Torvalds #include <linux/nfs_fs_sb.h>
251da177e4SLinus Torvalds #include <linux/nfs_mount.h>
264f5b246bSChristoph Hellwig #include <linux/raid/detect.h>
27e262e32dSDavid Howells #include <uapi/linux/mount.h>
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include "do_mounts.h"
301da177e4SLinus Torvalds 
319b04c997STheodore Ts'o int root_mountflags = MS_RDONLY | MS_SILENT;
32f56f6d30SAdrian Bunk static char * __initdata root_device_name;
331da177e4SLinus Torvalds static char __initdata saved_root_name[64];
3479975f13SWill Drewry static int root_wait;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds dev_t ROOT_DEV;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds static int __init load_ramdisk(char *str)
391da177e4SLinus Torvalds {
40c8376994SChristoph Hellwig 	pr_warn("ignoring the deprecated load_ramdisk= option\n");
411da177e4SLinus Torvalds 	return 1;
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds __setup("load_ramdisk=", load_ramdisk);
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds static int __init readonly(char *str)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	if (*str)
481da177e4SLinus Torvalds 		return 0;
491da177e4SLinus Torvalds 	root_mountflags |= MS_RDONLY;
501da177e4SLinus Torvalds 	return 1;
511da177e4SLinus Torvalds }
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds static int __init readwrite(char *str)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	if (*str)
561da177e4SLinus Torvalds 		return 0;
571da177e4SLinus Torvalds 	root_mountflags &= ~MS_RDONLY;
581da177e4SLinus Torvalds 	return 1;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds __setup("ro", readonly);
621da177e4SLinus Torvalds __setup("rw", readwrite);
631da177e4SLinus Torvalds 
646d0aed7aSJens Axboe #ifdef CONFIG_BLOCK
651ad7e899SStephen Warren struct uuidcmp {
661ad7e899SStephen Warren 	const char *uuid;
671ad7e899SStephen Warren 	int len;
681ad7e899SStephen Warren };
691ad7e899SStephen Warren 
70b5af921eSWill Drewry /**
71b5af921eSWill Drewry  * match_dev_by_uuid - callback for finding a partition using its uuid
72b5af921eSWill Drewry  * @dev:	device passed in by the caller
731ad7e899SStephen Warren  * @data:	opaque pointer to the desired struct uuidcmp to match
74b5af921eSWill Drewry  *
75b5af921eSWill Drewry  * Returns 1 if the device matches, and 0 otherwise.
76b5af921eSWill Drewry  */
779f3b795aSMichał Mirosław static int match_dev_by_uuid(struct device *dev, const void *data)
78b5af921eSWill Drewry {
799f3b795aSMichał Mirosław 	const struct uuidcmp *cmp = data;
80b5af921eSWill Drewry 	struct hd_struct *part = dev_to_part(dev);
81b5af921eSWill Drewry 
82b5af921eSWill Drewry 	if (!part->info)
83b5af921eSWill Drewry 		goto no_match;
84b5af921eSWill Drewry 
851ad7e899SStephen Warren 	if (strncasecmp(cmp->uuid, part->info->uuid, cmp->len))
86b5af921eSWill Drewry 		goto no_match;
87b5af921eSWill Drewry 
88b5af921eSWill Drewry 	return 1;
89b5af921eSWill Drewry no_match:
90b5af921eSWill Drewry 	return 0;
91b5af921eSWill Drewry }
92b5af921eSWill Drewry 
93b5af921eSWill Drewry /**
94b5af921eSWill Drewry  * devt_from_partuuid - looks up the dev_t of a partition by its UUID
95a68b3108Schishanmingshen  * @uuid_str:	char array containing ascii UUID
96b5af921eSWill Drewry  *
97b5af921eSWill Drewry  * The function will return the first partition which contains a matching
98b5af921eSWill Drewry  * UUID value in its partition_meta_info struct.  This does not search
99b5af921eSWill Drewry  * by filesystem UUIDs.
100b5af921eSWill Drewry  *
101a68b3108Schishanmingshen  * If @uuid_str is followed by a "/PARTNROFF=%d", then the number will be
10279975f13SWill Drewry  * extracted and used as an offset from the partition identified by the UUID.
10379975f13SWill Drewry  *
104b5af921eSWill Drewry  * Returns the matching dev_t on success or 0 on failure.
105b5af921eSWill Drewry  */
1061ad7e899SStephen Warren static dev_t devt_from_partuuid(const char *uuid_str)
107b5af921eSWill Drewry {
108b5af921eSWill Drewry 	dev_t res = 0;
1091ad7e899SStephen Warren 	struct uuidcmp cmp;
110b5af921eSWill Drewry 	struct device *dev = NULL;
11179975f13SWill Drewry 	struct gendisk *disk;
11279975f13SWill Drewry 	struct hd_struct *part;
11379975f13SWill Drewry 	int offset = 0;
114283f8fc0SStephen Warren 	bool clear_root_wait = false;
115283f8fc0SStephen Warren 	char *slash;
11679975f13SWill Drewry 
1171ad7e899SStephen Warren 	cmp.uuid = uuid_str;
1181ad7e899SStephen Warren 
119283f8fc0SStephen Warren 	slash = strchr(uuid_str, '/');
12079975f13SWill Drewry 	/* Check for optional partition number offset attributes. */
121283f8fc0SStephen Warren 	if (slash) {
12279975f13SWill Drewry 		char c = 0;
12379975f13SWill Drewry 		/* Explicitly fail on poor PARTUUID syntax. */
124283f8fc0SStephen Warren 		if (sscanf(slash + 1,
125283f8fc0SStephen Warren 			   "PARTNROFF=%d%c", &offset, &c) != 1) {
126283f8fc0SStephen Warren 			clear_root_wait = true;
12779975f13SWill Drewry 			goto done;
12879975f13SWill Drewry 		}
129283f8fc0SStephen Warren 		cmp.len = slash - uuid_str;
130283f8fc0SStephen Warren 	} else {
131283f8fc0SStephen Warren 		cmp.len = strlen(uuid_str);
132283f8fc0SStephen Warren 	}
133283f8fc0SStephen Warren 
134283f8fc0SStephen Warren 	if (!cmp.len) {
135283f8fc0SStephen Warren 		clear_root_wait = true;
136283f8fc0SStephen Warren 		goto done;
13779975f13SWill Drewry 	}
138b5af921eSWill Drewry 
1391ad7e899SStephen Warren 	dev = class_find_device(&block_class, NULL, &cmp,
1401ad7e899SStephen Warren 				&match_dev_by_uuid);
141b5af921eSWill Drewry 	if (!dev)
142b5af921eSWill Drewry 		goto done;
143b5af921eSWill Drewry 
144b5af921eSWill Drewry 	res = dev->devt;
145b5af921eSWill Drewry 
14679975f13SWill Drewry 	/* Attempt to find the partition by offset. */
14779975f13SWill Drewry 	if (!offset)
14879975f13SWill Drewry 		goto no_offset;
14979975f13SWill Drewry 
15079975f13SWill Drewry 	res = 0;
15179975f13SWill Drewry 	disk = part_to_disk(dev_to_part(dev));
15279975f13SWill Drewry 	part = disk_get_part(disk, dev_to_part(dev)->partno + offset);
15379975f13SWill Drewry 	if (part) {
15479975f13SWill Drewry 		res = part_devt(part);
15579975f13SWill Drewry 		put_device(part_to_dev(part));
15679975f13SWill Drewry 	}
15779975f13SWill Drewry 
15879975f13SWill Drewry no_offset:
15979975f13SWill Drewry 	put_device(dev);
160b5af921eSWill Drewry done:
161283f8fc0SStephen Warren 	if (clear_root_wait) {
162283f8fc0SStephen Warren 		pr_err("VFS: PARTUUID= is invalid.\n"
163283f8fc0SStephen Warren 		       "Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
164283f8fc0SStephen Warren 		if (root_wait)
165283f8fc0SStephen Warren 			pr_err("Disabling rootwait; root= is invalid.\n");
166283f8fc0SStephen Warren 		root_wait = 0;
167283f8fc0SStephen Warren 	}
168b5af921eSWill Drewry 	return res;
169b5af921eSWill Drewry }
170f027c34dSNikolaus Voss 
171f027c34dSNikolaus Voss /**
172f027c34dSNikolaus Voss  * match_dev_by_label - callback for finding a partition using its label
173f027c34dSNikolaus Voss  * @dev:	device passed in by the caller
174f027c34dSNikolaus Voss  * @data:	opaque pointer to the label to match
175f027c34dSNikolaus Voss  *
176f027c34dSNikolaus Voss  * Returns 1 if the device matches, and 0 otherwise.
177f027c34dSNikolaus Voss  */
178f027c34dSNikolaus Voss static int match_dev_by_label(struct device *dev, const void *data)
179f027c34dSNikolaus Voss {
180f027c34dSNikolaus Voss 	const char *label = data;
181f027c34dSNikolaus Voss 	struct hd_struct *part = dev_to_part(dev);
182f027c34dSNikolaus Voss 
183f027c34dSNikolaus Voss 	if (part->info && !strcmp(label, part->info->volname))
184f027c34dSNikolaus Voss 		return 1;
185f027c34dSNikolaus Voss 
186f027c34dSNikolaus Voss 	return 0;
187f027c34dSNikolaus Voss }
188*c2637e80SChristoph Hellwig 
189*c2637e80SChristoph Hellwig static dev_t devt_from_partlabel(const char *label)
190*c2637e80SChristoph Hellwig {
191*c2637e80SChristoph Hellwig 	struct device *dev;
192*c2637e80SChristoph Hellwig 	dev_t devt = 0;
193*c2637e80SChristoph Hellwig 
194*c2637e80SChristoph Hellwig 	dev = class_find_device(&block_class, NULL, label, &match_dev_by_label);
195*c2637e80SChristoph Hellwig 	if (dev) {
196*c2637e80SChristoph Hellwig 		devt = dev->devt;
197*c2637e80SChristoph Hellwig 		put_device(dev);
198*c2637e80SChristoph Hellwig 	}
199*c2637e80SChristoph Hellwig 
200*c2637e80SChristoph Hellwig 	return devt;
201*c2637e80SChristoph Hellwig }
202*c2637e80SChristoph Hellwig 
203*c2637e80SChristoph Hellwig static dev_t devt_from_devname(const char *name)
204*c2637e80SChristoph Hellwig {
205*c2637e80SChristoph Hellwig 	dev_t devt = 0;
206*c2637e80SChristoph Hellwig 	int part;
207*c2637e80SChristoph Hellwig 	char s[32];
208*c2637e80SChristoph Hellwig 	char *p;
209*c2637e80SChristoph Hellwig 
210*c2637e80SChristoph Hellwig 	if (strlen(name) > 31)
211*c2637e80SChristoph Hellwig 		return 0;
212*c2637e80SChristoph Hellwig 	strcpy(s, name);
213*c2637e80SChristoph Hellwig 	for (p = s; *p; p++) {
214*c2637e80SChristoph Hellwig 		if (*p == '/')
215*c2637e80SChristoph Hellwig 			*p = '!';
216*c2637e80SChristoph Hellwig 	}
217*c2637e80SChristoph Hellwig 
218*c2637e80SChristoph Hellwig 	devt = blk_lookup_devt(s, 0);
219*c2637e80SChristoph Hellwig 	if (devt)
220*c2637e80SChristoph Hellwig 		return devt;
221*c2637e80SChristoph Hellwig 
222*c2637e80SChristoph Hellwig 	/*
223*c2637e80SChristoph Hellwig 	 * Try non-existent, but valid partition, which may only exist after
224*c2637e80SChristoph Hellwig 	 * opening the device, like partitioned md devices.
225*c2637e80SChristoph Hellwig 	 */
226*c2637e80SChristoph Hellwig 	while (p > s && isdigit(p[-1]))
227*c2637e80SChristoph Hellwig 		p--;
228*c2637e80SChristoph Hellwig 	if (p == s || !*p || *p == '0')
229*c2637e80SChristoph Hellwig 		return 0;
230*c2637e80SChristoph Hellwig 
231*c2637e80SChristoph Hellwig 	/* try disk name without <part number> */
232*c2637e80SChristoph Hellwig 	part = simple_strtoul(p, NULL, 10);
233*c2637e80SChristoph Hellwig 	*p = '\0';
234*c2637e80SChristoph Hellwig 	devt = blk_lookup_devt(s, part);
235*c2637e80SChristoph Hellwig 	if (devt)
236*c2637e80SChristoph Hellwig 		return devt;
237*c2637e80SChristoph Hellwig 
238*c2637e80SChristoph Hellwig 	/* try disk name without p<part number> */
239*c2637e80SChristoph Hellwig 	if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
240*c2637e80SChristoph Hellwig 		return 0;
241*c2637e80SChristoph Hellwig 	p[-1] = '\0';
242*c2637e80SChristoph Hellwig 	return blk_lookup_devt(s, part);
243*c2637e80SChristoph Hellwig }
244*c2637e80SChristoph Hellwig #endif /* CONFIG_BLOCK */
245*c2637e80SChristoph Hellwig 
246*c2637e80SChristoph Hellwig static dev_t devt_from_devnum(const char *name)
247*c2637e80SChristoph Hellwig {
248*c2637e80SChristoph Hellwig 	unsigned maj, min, offset;
249*c2637e80SChristoph Hellwig 	dev_t devt = 0;
250*c2637e80SChristoph Hellwig 	char *p, dummy;
251*c2637e80SChristoph Hellwig 
252*c2637e80SChristoph Hellwig 	if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
253*c2637e80SChristoph Hellwig 	    sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) == 3) {
254*c2637e80SChristoph Hellwig 		devt = MKDEV(maj, min);
255*c2637e80SChristoph Hellwig 		if (maj != MAJOR(devt) || min != MINOR(devt))
256*c2637e80SChristoph Hellwig 			return 0;
257*c2637e80SChristoph Hellwig 	} else {
258*c2637e80SChristoph Hellwig 		devt = new_decode_dev(simple_strtoul(name, &p, 16));
259*c2637e80SChristoph Hellwig 		if (*p)
260*c2637e80SChristoph Hellwig 			return 0;
261*c2637e80SChristoph Hellwig 	}
262*c2637e80SChristoph Hellwig 
263*c2637e80SChristoph Hellwig 	return devt;
264*c2637e80SChristoph Hellwig }
265b5af921eSWill Drewry 
2661da177e4SLinus Torvalds /*
2671da177e4SLinus Torvalds  *	Convert a name into device number.  We accept the following variants:
2681da177e4SLinus Torvalds  *
2690bf37ae4SPavel Machek  *	1) <hex_major><hex_minor> device number in hexadecimal represents itself
2700bf37ae4SPavel Machek  *         no leading 0x, for example b302.
2711da177e4SLinus Torvalds  *	2) /dev/nfs represents Root_NFS (0xff)
2721da177e4SLinus Torvalds  *	3) /dev/<disk_name> represents the device number of disk
2731da177e4SLinus Torvalds  *	4) /dev/<disk_name><decimal> represents the device number
2741da177e4SLinus Torvalds  *         of partition - device number of disk plus the partition number
2751da177e4SLinus Torvalds  *	5) /dev/<disk_name>p<decimal> - same as the above, that form is
2761da177e4SLinus Torvalds  *	   used when disk name of partitioned disk ends on a digit.
277b5af921eSWill Drewry  *	6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
278b5af921eSWill Drewry  *	   unique id of a partition if the partition table provides it.
279d33b98fcSStephen Warren  *	   The UUID may be either an EFI/GPT UUID, or refer to an MSDOS
280d33b98fcSStephen Warren  *	   partition using the format SSSSSSSS-PP, where SSSSSSSS is a zero-
281d33b98fcSStephen Warren  *	   filled hex representation of the 32-bit "NT disk signature", and PP
282d33b98fcSStephen Warren  *	   is a zero-filled hex representation of the 1-based partition number.
28379975f13SWill Drewry  *	7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
28479975f13SWill Drewry  *	   a partition with a known unique id.
2856c251611SSebastian Capella  *	8) <major>:<minor> major and minor number of the device separated by
2866c251611SSebastian Capella  *	   a colon.
287f027c34dSNikolaus Voss  *	9) PARTLABEL=<name> with name being the GPT partition label.
288f027c34dSNikolaus Voss  *	   MSDOS partitions do not support labels!
2898902dd52SPaulo Alcantara (SUSE)  *	10) /dev/cifs represents Root_CIFS (0xfe)
2901da177e4SLinus Torvalds  *
291edfaa7c3SKay Sievers  *	If name doesn't have fall into the categories above, we return (0,0).
292edfaa7c3SKay Sievers  *	block_class is used to check if something is a disk name. If the disk
293edfaa7c3SKay Sievers  *	name contains slashes, the device name has them replaced with
294edfaa7c3SKay Sievers  *	bangs.
2951da177e4SLinus Torvalds  */
296e6e20a7aSDan Ehrenberg dev_t name_to_dev_t(const char *name)
2971da177e4SLinus Torvalds {
298*c2637e80SChristoph Hellwig 	if (strcmp(name, "/dev/nfs") == 0)
299*c2637e80SChristoph Hellwig 		return Root_NFS;
300*c2637e80SChristoph Hellwig 	if (strcmp(name, "/dev/cifs") == 0)
301*c2637e80SChristoph Hellwig 		return Root_CIFS;
302*c2637e80SChristoph Hellwig 	if (strcmp(name, "/dev/ram") == 0)
303*c2637e80SChristoph Hellwig 		return Root_RAM0;
3046d0aed7aSJens Axboe #ifdef CONFIG_BLOCK
305*c2637e80SChristoph Hellwig 	if (strncmp(name, "PARTUUID=", 9) == 0)
306*c2637e80SChristoph Hellwig 		return devt_from_partuuid(name + 9);
307*c2637e80SChristoph Hellwig 	if (strncmp(name, "PARTLABEL=", 10) == 0)
308*c2637e80SChristoph Hellwig 		return devt_from_partlabel(name + 10);
309*c2637e80SChristoph Hellwig 	if (strncmp(name, "/dev/", 5) == 0)
310*c2637e80SChristoph Hellwig 		return devt_from_devname(name + 5);
3116d0aed7aSJens Axboe #endif
312*c2637e80SChristoph Hellwig 	return devt_from_devnum(name);
3131da177e4SLinus Torvalds }
314e6e20a7aSDan Ehrenberg EXPORT_SYMBOL_GPL(name_to_dev_t);
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds static int __init root_dev_setup(char *line)
3171da177e4SLinus Torvalds {
3181da177e4SLinus Torvalds 	strlcpy(saved_root_name, line, sizeof(saved_root_name));
3191da177e4SLinus Torvalds 	return 1;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds __setup("root=", root_dev_setup);
3231da177e4SLinus Torvalds 
324cc1ed754SPierre Ossman static int __init rootwait_setup(char *str)
325cc1ed754SPierre Ossman {
326cc1ed754SPierre Ossman 	if (*str)
327cc1ed754SPierre Ossman 		return 0;
328cc1ed754SPierre Ossman 	root_wait = 1;
329cc1ed754SPierre Ossman 	return 1;
330cc1ed754SPierre Ossman }
331cc1ed754SPierre Ossman 
332cc1ed754SPierre Ossman __setup("rootwait", rootwait_setup);
333cc1ed754SPierre Ossman 
3341da177e4SLinus Torvalds static char * __initdata root_mount_data;
3351da177e4SLinus Torvalds static int __init root_data_setup(char *str)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds 	root_mount_data = str;
3381da177e4SLinus Torvalds 	return 1;
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds static char * __initdata root_fs_names;
3421da177e4SLinus Torvalds static int __init fs_names_setup(char *str)
3431da177e4SLinus Torvalds {
3441da177e4SLinus Torvalds 	root_fs_names = str;
3451da177e4SLinus Torvalds 	return 1;
3461da177e4SLinus Torvalds }
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds static unsigned int __initdata root_delay;
3491da177e4SLinus Torvalds static int __init root_delay_setup(char *str)
3501da177e4SLinus Torvalds {
3511da177e4SLinus Torvalds 	root_delay = simple_strtoul(str, NULL, 0);
3521da177e4SLinus Torvalds 	return 1;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds __setup("rootflags=", root_data_setup);
3561da177e4SLinus Torvalds __setup("rootfstype=", fs_names_setup);
3571da177e4SLinus Torvalds __setup("rootdelay=", root_delay_setup);
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds static void __init get_fs_names(char *page)
3601da177e4SLinus Torvalds {
3611da177e4SLinus Torvalds 	char *s = page;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	if (root_fs_names) {
3641da177e4SLinus Torvalds 		strcpy(page, root_fs_names);
3651da177e4SLinus Torvalds 		while (*s++) {
3661da177e4SLinus Torvalds 			if (s[-1] == ',')
3671da177e4SLinus Torvalds 				s[-1] = '\0';
3681da177e4SLinus Torvalds 		}
3691da177e4SLinus Torvalds 	} else {
3701da177e4SLinus Torvalds 		int len = get_filesystem_list(page);
3711da177e4SLinus Torvalds 		char *p, *next;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 		page[len] = '\0';
3741da177e4SLinus Torvalds 		for (p = page-1; p; p = next) {
3751da177e4SLinus Torvalds 			next = strchr(++p, '\n');
3761da177e4SLinus Torvalds 			if (*p++ != '\t')
3771da177e4SLinus Torvalds 				continue;
3781da177e4SLinus Torvalds 			while ((*s++ = *p++) != '\n')
3791da177e4SLinus Torvalds 				;
3801da177e4SLinus Torvalds 			s[-1] = '\0';
3811da177e4SLinus Torvalds 		}
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds 	*s = '\0';
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
386cccaa5e3SDominik Brodowski static int __init do_mount_root(const char *name, const char *fs,
387cccaa5e3SDominik Brodowski 				 const int flags, const void *data)
3881da177e4SLinus Torvalds {
389d8c9584eSAl Viro 	struct super_block *s;
3907de7de7cSLinus Torvalds 	struct page *p = NULL;
3917de7de7cSLinus Torvalds 	char *data_page = NULL;
392cccaa5e3SDominik Brodowski 	int ret;
393cccaa5e3SDominik Brodowski 
3947de7de7cSLinus Torvalds 	if (data) {
395c60166f0SChristoph Hellwig 		/* init_mount() requires a full page as fifth argument */
396cccaa5e3SDominik Brodowski 		p = alloc_page(GFP_KERNEL);
397cccaa5e3SDominik Brodowski 		if (!p)
398cccaa5e3SDominik Brodowski 			return -ENOMEM;
399cccaa5e3SDominik Brodowski 		data_page = page_address(p);
400c60166f0SChristoph Hellwig 		/* zero-pad. init_mount() will make sure it's terminated */
4017de7de7cSLinus Torvalds 		strncpy(data_page, data, PAGE_SIZE);
4027de7de7cSLinus Torvalds 	}
403cccaa5e3SDominik Brodowski 
404c60166f0SChristoph Hellwig 	ret = init_mount(name, "/root", fs, flags, data_page);
405cccaa5e3SDominik Brodowski 	if (ret)
406cccaa5e3SDominik Brodowski 		goto out;
4071da177e4SLinus Torvalds 
408db63f1e3SChristoph Hellwig 	init_chdir("/root");
409d8c9584eSAl Viro 	s = current->fs->pwd.dentry->d_sb;
410d8c9584eSAl Viro 	ROOT_DEV = s->s_dev;
41180cdc6daSMandeep Singh Baines 	printk(KERN_INFO
41280cdc6daSMandeep Singh Baines 	       "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
413d8c9584eSAl Viro 	       s->s_type->name,
414bc98a42cSDavid Howells 	       sb_rdonly(s) ? " readonly" : "",
415d8c9584eSAl Viro 	       MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
416cccaa5e3SDominik Brodowski 
417cccaa5e3SDominik Brodowski out:
4187de7de7cSLinus Torvalds 	if (p)
419cccaa5e3SDominik Brodowski 		put_page(p);
420cccaa5e3SDominik Brodowski 	return ret;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds void __init mount_block_root(char *name, int flags)
4241da177e4SLinus Torvalds {
42575f296d9SLevin, Alexander (Sasha Levin) 	struct page *page = alloc_page(GFP_KERNEL);
426a608ca21SJeff Layton 	char *fs_names = page_address(page);
4271da177e4SLinus Torvalds 	char *p;
4281da177e4SLinus Torvalds 	char b[BDEVNAME_SIZE];
4291da177e4SLinus Torvalds 
430ea3edd4dSChristoph Hellwig 	scnprintf(b, BDEVNAME_SIZE, "unknown-block(%u,%u)",
431ea3edd4dSChristoph Hellwig 		  MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
4321da177e4SLinus Torvalds 	get_fs_names(fs_names);
4331da177e4SLinus Torvalds retry:
4341da177e4SLinus Torvalds 	for (p = fs_names; *p; p += strlen(p)+1) {
4351da177e4SLinus Torvalds 		int err = do_mount_root(name, p, flags, root_mount_data);
4361da177e4SLinus Torvalds 		switch (err) {
4371da177e4SLinus Torvalds 			case 0:
4381da177e4SLinus Torvalds 				goto out;
4391da177e4SLinus Torvalds 			case -EACCES:
4401da177e4SLinus Torvalds 			case -EINVAL:
4411da177e4SLinus Torvalds 				continue;
4421da177e4SLinus Torvalds 		}
4431da177e4SLinus Torvalds 	        /*
4441da177e4SLinus Torvalds 		 * Allow the user to distinguish between failed sys_open
4451da177e4SLinus Torvalds 		 * and bad superblock on root device.
446dd2a345fSDave Gilbert 		 * and give them a list of the available devices
4471da177e4SLinus Torvalds 		 */
4480e0cb892SBernhard Walle 		printk("VFS: Cannot open root device \"%s\" or %s: error %d\n",
4490e0cb892SBernhard Walle 				root_device_name, b, err);
450dd2a345fSDave Gilbert 		printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
4511da177e4SLinus Torvalds 
452dd2a345fSDave Gilbert 		printk_all_partitions();
45355dc7db7STejun Heo #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
45455dc7db7STejun Heo 		printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
45555dc7db7STejun Heo 		       "explicit textual name for \"root=\" boot option.\n");
45655dc7db7STejun Heo #endif
4571da177e4SLinus Torvalds 		panic("VFS: Unable to mount root fs on %s", b);
4581da177e4SLinus Torvalds 	}
459e462ec50SDavid Howells 	if (!(flags & SB_RDONLY)) {
460e462ec50SDavid Howells 		flags |= SB_RDONLY;
46110975933SMiklos Szeredi 		goto retry;
46210975933SMiklos Szeredi 	}
463be6e028bSAndy Whitcroft 
464dd2a345fSDave Gilbert 	printk("List of all partitions:\n");
465dd2a345fSDave Gilbert 	printk_all_partitions();
466be6e028bSAndy Whitcroft 	printk("No filesystem could mount root, tried: ");
467be6e028bSAndy Whitcroft 	for (p = fs_names; *p; p += strlen(p)+1)
468be6e028bSAndy Whitcroft 		printk(" %s", p);
469be6e028bSAndy Whitcroft 	printk("\n");
4709361401eSDavid Howells 	panic("VFS: Unable to mount root fs on %s", b);
4711da177e4SLinus Torvalds out:
472a608ca21SJeff Layton 	put_page(page);
4731da177e4SLinus Torvalds }
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS
47643717c7dSChuck Lever 
47743717c7dSChuck Lever #define NFSROOT_TIMEOUT_MIN	5
47843717c7dSChuck Lever #define NFSROOT_TIMEOUT_MAX	30
47943717c7dSChuck Lever #define NFSROOT_RETRY_MAX	5
48043717c7dSChuck Lever 
4811da177e4SLinus Torvalds static int __init mount_nfs_root(void)
4821da177e4SLinus Torvalds {
48356463e50SChuck Lever 	char *root_dev, *root_data;
48443717c7dSChuck Lever 	unsigned int timeout;
48543717c7dSChuck Lever 	int try, err;
4861da177e4SLinus Torvalds 
48743717c7dSChuck Lever 	err = nfs_root_data(&root_dev, &root_data);
48843717c7dSChuck Lever 	if (err != 0)
4891da177e4SLinus Torvalds 		return 0;
49043717c7dSChuck Lever 
49143717c7dSChuck Lever 	/*
49243717c7dSChuck Lever 	 * The server or network may not be ready, so try several
49343717c7dSChuck Lever 	 * times.  Stop after a few tries in case the client wants
49443717c7dSChuck Lever 	 * to fall back to other boot methods.
49543717c7dSChuck Lever 	 */
49643717c7dSChuck Lever 	timeout = NFSROOT_TIMEOUT_MIN;
49743717c7dSChuck Lever 	for (try = 1; ; try++) {
49843717c7dSChuck Lever 		err = do_mount_root(root_dev, "nfs",
49943717c7dSChuck Lever 					root_mountflags, root_data);
50043717c7dSChuck Lever 		if (err == 0)
50156463e50SChuck Lever 			return 1;
50243717c7dSChuck Lever 		if (try > NFSROOT_RETRY_MAX)
50343717c7dSChuck Lever 			break;
50443717c7dSChuck Lever 
50543717c7dSChuck Lever 		/* Wait, in case the server refused us immediately */
50643717c7dSChuck Lever 		ssleep(timeout);
50743717c7dSChuck Lever 		timeout <<= 1;
50843717c7dSChuck Lever 		if (timeout > NFSROOT_TIMEOUT_MAX)
50943717c7dSChuck Lever 			timeout = NFSROOT_TIMEOUT_MAX;
51043717c7dSChuck Lever 	}
51143717c7dSChuck Lever 	return 0;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds #endif
5141da177e4SLinus Torvalds 
5158902dd52SPaulo Alcantara (SUSE) #ifdef CONFIG_CIFS_ROOT
5168902dd52SPaulo Alcantara (SUSE) 
5178902dd52SPaulo Alcantara (SUSE) extern int cifs_root_data(char **dev, char **opts);
5188902dd52SPaulo Alcantara (SUSE) 
5198902dd52SPaulo Alcantara (SUSE) #define CIFSROOT_TIMEOUT_MIN	5
5208902dd52SPaulo Alcantara (SUSE) #define CIFSROOT_TIMEOUT_MAX	30
5218902dd52SPaulo Alcantara (SUSE) #define CIFSROOT_RETRY_MAX	5
5228902dd52SPaulo Alcantara (SUSE) 
5238902dd52SPaulo Alcantara (SUSE) static int __init mount_cifs_root(void)
5248902dd52SPaulo Alcantara (SUSE) {
5258902dd52SPaulo Alcantara (SUSE) 	char *root_dev, *root_data;
5268902dd52SPaulo Alcantara (SUSE) 	unsigned int timeout;
5278902dd52SPaulo Alcantara (SUSE) 	int try, err;
5288902dd52SPaulo Alcantara (SUSE) 
5298902dd52SPaulo Alcantara (SUSE) 	err = cifs_root_data(&root_dev, &root_data);
5308902dd52SPaulo Alcantara (SUSE) 	if (err != 0)
5318902dd52SPaulo Alcantara (SUSE) 		return 0;
5328902dd52SPaulo Alcantara (SUSE) 
5338902dd52SPaulo Alcantara (SUSE) 	timeout = CIFSROOT_TIMEOUT_MIN;
5348902dd52SPaulo Alcantara (SUSE) 	for (try = 1; ; try++) {
5358902dd52SPaulo Alcantara (SUSE) 		err = do_mount_root(root_dev, "cifs", root_mountflags,
5368902dd52SPaulo Alcantara (SUSE) 				    root_data);
5378902dd52SPaulo Alcantara (SUSE) 		if (err == 0)
5388902dd52SPaulo Alcantara (SUSE) 			return 1;
5398902dd52SPaulo Alcantara (SUSE) 		if (try > CIFSROOT_RETRY_MAX)
5408902dd52SPaulo Alcantara (SUSE) 			break;
5418902dd52SPaulo Alcantara (SUSE) 
5428902dd52SPaulo Alcantara (SUSE) 		ssleep(timeout);
5438902dd52SPaulo Alcantara (SUSE) 		timeout <<= 1;
5448902dd52SPaulo Alcantara (SUSE) 		if (timeout > CIFSROOT_TIMEOUT_MAX)
5458902dd52SPaulo Alcantara (SUSE) 			timeout = CIFSROOT_TIMEOUT_MAX;
5468902dd52SPaulo Alcantara (SUSE) 	}
5478902dd52SPaulo Alcantara (SUSE) 	return 0;
5488902dd52SPaulo Alcantara (SUSE) }
5498902dd52SPaulo Alcantara (SUSE) #endif
5508902dd52SPaulo Alcantara (SUSE) 
5511da177e4SLinus Torvalds void __init mount_root(void)
5521da177e4SLinus Torvalds {
5531da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS
554377485f6SSasha Levin 	if (ROOT_DEV == Root_NFS) {
555c8376994SChristoph Hellwig 		if (!mount_nfs_root())
556c8376994SChristoph Hellwig 			printk(KERN_ERR "VFS: Unable to mount root fs via NFS.\n");
5571da177e4SLinus Torvalds 		return;
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds #endif
5608902dd52SPaulo Alcantara (SUSE) #ifdef CONFIG_CIFS_ROOT
5618902dd52SPaulo Alcantara (SUSE) 	if (ROOT_DEV == Root_CIFS) {
562c8376994SChristoph Hellwig 		if (!mount_cifs_root())
563c8376994SChristoph Hellwig 			printk(KERN_ERR "VFS: Unable to mount root fs via SMB.\n");
5648902dd52SPaulo Alcantara (SUSE) 		return;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds #endif
5679361401eSDavid Howells #ifdef CONFIG_BLOCK
568c69e3c3aSVishnu Pratap Singh 	{
569c69e3c3aSVishnu Pratap Singh 		int err = create_dev("/dev/root", ROOT_DEV);
570c69e3c3aSVishnu Pratap Singh 
571c69e3c3aSVishnu Pratap Singh 		if (err < 0)
572c69e3c3aSVishnu Pratap Singh 			pr_emerg("Failed to create /dev/root: %d\n", err);
5731da177e4SLinus Torvalds 		mount_block_root("/dev/root", root_mountflags);
574c69e3c3aSVishnu Pratap Singh 	}
5759361401eSDavid Howells #endif
5761da177e4SLinus Torvalds }
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds /*
5791da177e4SLinus Torvalds  * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
5801da177e4SLinus Torvalds  */
5811da177e4SLinus Torvalds void __init prepare_namespace(void)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds 	if (root_delay) {
5841da177e4SLinus Torvalds 		printk(KERN_INFO "Waiting %d sec before mounting root device...\n",
5851da177e4SLinus Torvalds 		       root_delay);
5861da177e4SLinus Torvalds 		ssleep(root_delay);
5871da177e4SLinus Torvalds 	}
5881da177e4SLinus Torvalds 
589216773a7SArjan van de Ven 	/*
590216773a7SArjan van de Ven 	 * wait for the known devices to complete their probing
591216773a7SArjan van de Ven 	 *
592216773a7SArjan van de Ven 	 * Note: this is a potential source of long boot delays.
593216773a7SArjan van de Ven 	 * For example, it is not atypical to wait 5 seconds here
594216773a7SArjan van de Ven 	 * for the touchpad of a laptop to initialize.
595216773a7SArjan van de Ven 	 */
596216773a7SArjan van de Ven 	wait_for_device_probe();
597d779249eSGreg Kroah-Hartman 
5981da177e4SLinus Torvalds 	md_run_setup();
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	if (saved_root_name[0]) {
6011da177e4SLinus Torvalds 		root_device_name = saved_root_name;
6022d62f488SAdrian Hunter 		if (!strncmp(root_device_name, "mtd", 3) ||
6032d62f488SAdrian Hunter 		    !strncmp(root_device_name, "ubi", 3)) {
604e9482b43SJoern Engel 			mount_block_root(root_device_name, root_mountflags);
605e9482b43SJoern Engel 			goto out;
606e9482b43SJoern Engel 		}
6071da177e4SLinus Torvalds 		ROOT_DEV = name_to_dev_t(root_device_name);
6081da177e4SLinus Torvalds 		if (strncmp(root_device_name, "/dev/", 5) == 0)
6091da177e4SLinus Torvalds 			root_device_name += 5;
6101da177e4SLinus Torvalds 	}
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	if (initrd_load())
6131da177e4SLinus Torvalds 		goto out;
6141da177e4SLinus Torvalds 
615cc1ed754SPierre Ossman 	/* wait for any asynchronous scanning to complete */
616cc1ed754SPierre Ossman 	if ((ROOT_DEV == 0) && root_wait) {
617cc1ed754SPierre Ossman 		printk(KERN_INFO "Waiting for root device %s...\n",
618cc1ed754SPierre Ossman 			saved_root_name);
619cc1ed754SPierre Ossman 		while (driver_probe_done() != 0 ||
620cc1ed754SPierre Ossman 			(ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
62139a0e975SJungseung Lee 			msleep(5);
622216773a7SArjan van de Ven 		async_synchronize_full();
623cc1ed754SPierre Ossman 	}
624cc1ed754SPierre Ossman 
6251da177e4SLinus Torvalds 	mount_root();
6261da177e4SLinus Torvalds out:
6275e787dbfSDominik Brodowski 	devtmpfs_mount();
628c60166f0SChristoph Hellwig 	init_mount(".", "/", NULL, MS_MOVE, NULL);
6294b7ca501SChristoph Hellwig 	init_chroot(".");
6301da177e4SLinus Torvalds }
63157f150a5SRob Landley 
6326e19ededSRob Landley static bool is_tmpfs;
633f3235626SDavid Howells static int rootfs_init_fs_context(struct fs_context *fc)
63457f150a5SRob Landley {
6356e19ededSRob Landley 	if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
636f3235626SDavid Howells 		return shmem_init_fs_context(fc);
6376e19ededSRob Landley 
638f3235626SDavid Howells 	return ramfs_init_fs_context(fc);
63957f150a5SRob Landley }
64057f150a5SRob Landley 
641fd3e007fSAl Viro struct file_system_type rootfs_fs_type = {
64257f150a5SRob Landley 	.name		= "rootfs",
643f3235626SDavid Howells 	.init_fs_context = rootfs_init_fs_context,
64457f150a5SRob Landley 	.kill_sb	= kill_litter_super,
64557f150a5SRob Landley };
64657f150a5SRob Landley 
647037f11b4SAl Viro void __init init_rootfs(void)
64857f150a5SRob Landley {
6496e19ededSRob Landley 	if (IS_ENABLED(CONFIG_TMPFS) && !saved_root_name[0] &&
650037f11b4SAl Viro 		(!root_fs_names || strstr(root_fs_names, "tmpfs")))
6516e19ededSRob Landley 		is_tmpfs = true;
65257f150a5SRob Landley }
653