xref: /openbmc/linux/init/do_mounts.c (revision 0d02129e)
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 {
79*0d02129eSChristoph Hellwig 	struct block_device *bdev = dev_to_bdev(dev);
809f3b795aSMichał Mirosław 	const struct uuidcmp *cmp = data;
81b5af921eSWill Drewry 
82*0d02129eSChristoph Hellwig 	if (!bdev->bd_meta_info ||
83*0d02129eSChristoph Hellwig 	    strncasecmp(cmp->uuid, bdev->bd_meta_info->uuid, cmp->len))
84b5af921eSWill Drewry 		return 0;
85013b0e96SChristoph Hellwig 	return 1;
86b5af921eSWill Drewry }
87b5af921eSWill Drewry 
88b5af921eSWill Drewry /**
89b5af921eSWill Drewry  * devt_from_partuuid - looks up the dev_t of a partition by its UUID
90a68b3108Schishanmingshen  * @uuid_str:	char array containing ascii UUID
91b5af921eSWill Drewry  *
92b5af921eSWill Drewry  * The function will return the first partition which contains a matching
93b5af921eSWill Drewry  * UUID value in its partition_meta_info struct.  This does not search
94b5af921eSWill Drewry  * by filesystem UUIDs.
95b5af921eSWill Drewry  *
96a68b3108Schishanmingshen  * If @uuid_str is followed by a "/PARTNROFF=%d", then the number will be
9779975f13SWill Drewry  * extracted and used as an offset from the partition identified by the UUID.
9879975f13SWill Drewry  *
99b5af921eSWill Drewry  * Returns the matching dev_t on success or 0 on failure.
100b5af921eSWill Drewry  */
1011ad7e899SStephen Warren static dev_t devt_from_partuuid(const char *uuid_str)
102b5af921eSWill Drewry {
1031ad7e899SStephen Warren 	struct uuidcmp cmp;
104b5af921eSWill Drewry 	struct device *dev = NULL;
105e036bb8eSChristoph Hellwig 	dev_t devt = 0;
10679975f13SWill Drewry 	int offset = 0;
107283f8fc0SStephen Warren 	char *slash;
10879975f13SWill Drewry 
1091ad7e899SStephen Warren 	cmp.uuid = uuid_str;
1101ad7e899SStephen Warren 
111283f8fc0SStephen Warren 	slash = strchr(uuid_str, '/');
11279975f13SWill Drewry 	/* Check for optional partition number offset attributes. */
113283f8fc0SStephen Warren 	if (slash) {
11479975f13SWill Drewry 		char c = 0;
115e036bb8eSChristoph Hellwig 
11679975f13SWill Drewry 		/* Explicitly fail on poor PARTUUID syntax. */
117e036bb8eSChristoph Hellwig 		if (sscanf(slash + 1, "PARTNROFF=%d%c", &offset, &c) != 1)
118e036bb8eSChristoph Hellwig 			goto clear_root_wait;
119283f8fc0SStephen Warren 		cmp.len = slash - uuid_str;
120283f8fc0SStephen Warren 	} else {
121283f8fc0SStephen Warren 		cmp.len = strlen(uuid_str);
122283f8fc0SStephen Warren 	}
123283f8fc0SStephen Warren 
124e036bb8eSChristoph Hellwig 	if (!cmp.len)
125e036bb8eSChristoph Hellwig 		goto clear_root_wait;
126b5af921eSWill Drewry 
127e036bb8eSChristoph Hellwig 	dev = class_find_device(&block_class, NULL, &cmp, &match_dev_by_uuid);
128b5af921eSWill Drewry 	if (!dev)
129e036bb8eSChristoph Hellwig 		return 0;
130b5af921eSWill Drewry 
131e036bb8eSChristoph Hellwig 	if (offset) {
132e036bb8eSChristoph Hellwig 		/*
133e036bb8eSChristoph Hellwig 		 * Attempt to find the requested partition by adding an offset
134e036bb8eSChristoph Hellwig 		 * to the partition number found by UUID.
135e036bb8eSChristoph Hellwig 		 */
136*0d02129eSChristoph Hellwig 		struct block_device *part;
137b5af921eSWill Drewry 
138*0d02129eSChristoph Hellwig 		part = bdget_disk(dev_to_disk(dev),
139*0d02129eSChristoph Hellwig 				  dev_to_bdev(dev)->bd_partno + offset);
14079975f13SWill Drewry 		if (part) {
141*0d02129eSChristoph Hellwig 			devt = part->bd_dev;
142*0d02129eSChristoph Hellwig 			bdput(part);
14379975f13SWill Drewry 		}
144e036bb8eSChristoph Hellwig 	} else {
145e036bb8eSChristoph Hellwig 		devt = dev->devt;
146e036bb8eSChristoph Hellwig 	}
14779975f13SWill Drewry 
14879975f13SWill Drewry 	put_device(dev);
149e036bb8eSChristoph Hellwig 	return devt;
150e036bb8eSChristoph Hellwig 
151e036bb8eSChristoph Hellwig clear_root_wait:
152283f8fc0SStephen Warren 	pr_err("VFS: PARTUUID= is invalid.\n"
153283f8fc0SStephen Warren 	       "Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
154283f8fc0SStephen Warren 	if (root_wait)
155283f8fc0SStephen Warren 		pr_err("Disabling rootwait; root= is invalid.\n");
156283f8fc0SStephen Warren 	root_wait = 0;
157e036bb8eSChristoph Hellwig 	return 0;
158b5af921eSWill Drewry }
159f027c34dSNikolaus Voss 
160f027c34dSNikolaus Voss /**
161f027c34dSNikolaus Voss  * match_dev_by_label - callback for finding a partition using its label
162f027c34dSNikolaus Voss  * @dev:	device passed in by the caller
163f027c34dSNikolaus Voss  * @data:	opaque pointer to the label to match
164f027c34dSNikolaus Voss  *
165f027c34dSNikolaus Voss  * Returns 1 if the device matches, and 0 otherwise.
166f027c34dSNikolaus Voss  */
167f027c34dSNikolaus Voss static int match_dev_by_label(struct device *dev, const void *data)
168f027c34dSNikolaus Voss {
169*0d02129eSChristoph Hellwig 	struct block_device *bdev = dev_to_bdev(dev);
170f027c34dSNikolaus Voss 	const char *label = data;
171f027c34dSNikolaus Voss 
172*0d02129eSChristoph Hellwig 	if (!bdev->bd_meta_info || strcmp(label, bdev->bd_meta_info->volname))
173f027c34dSNikolaus Voss 		return 0;
174013b0e96SChristoph Hellwig 	return 1;
175f027c34dSNikolaus Voss }
176c2637e80SChristoph Hellwig 
177c2637e80SChristoph Hellwig static dev_t devt_from_partlabel(const char *label)
178c2637e80SChristoph Hellwig {
179c2637e80SChristoph Hellwig 	struct device *dev;
180c2637e80SChristoph Hellwig 	dev_t devt = 0;
181c2637e80SChristoph Hellwig 
182c2637e80SChristoph Hellwig 	dev = class_find_device(&block_class, NULL, label, &match_dev_by_label);
183c2637e80SChristoph Hellwig 	if (dev) {
184c2637e80SChristoph Hellwig 		devt = dev->devt;
185c2637e80SChristoph Hellwig 		put_device(dev);
186c2637e80SChristoph Hellwig 	}
187c2637e80SChristoph Hellwig 
188c2637e80SChristoph Hellwig 	return devt;
189c2637e80SChristoph Hellwig }
190c2637e80SChristoph Hellwig 
191c2637e80SChristoph Hellwig static dev_t devt_from_devname(const char *name)
192c2637e80SChristoph Hellwig {
193c2637e80SChristoph Hellwig 	dev_t devt = 0;
194c2637e80SChristoph Hellwig 	int part;
195c2637e80SChristoph Hellwig 	char s[32];
196c2637e80SChristoph Hellwig 	char *p;
197c2637e80SChristoph Hellwig 
198c2637e80SChristoph Hellwig 	if (strlen(name) > 31)
199c2637e80SChristoph Hellwig 		return 0;
200c2637e80SChristoph Hellwig 	strcpy(s, name);
201c2637e80SChristoph Hellwig 	for (p = s; *p; p++) {
202c2637e80SChristoph Hellwig 		if (*p == '/')
203c2637e80SChristoph Hellwig 			*p = '!';
204c2637e80SChristoph Hellwig 	}
205c2637e80SChristoph Hellwig 
206c2637e80SChristoph Hellwig 	devt = blk_lookup_devt(s, 0);
207c2637e80SChristoph Hellwig 	if (devt)
208c2637e80SChristoph Hellwig 		return devt;
209c2637e80SChristoph Hellwig 
210c2637e80SChristoph Hellwig 	/*
211c2637e80SChristoph Hellwig 	 * Try non-existent, but valid partition, which may only exist after
212c2637e80SChristoph Hellwig 	 * opening the device, like partitioned md devices.
213c2637e80SChristoph Hellwig 	 */
214c2637e80SChristoph Hellwig 	while (p > s && isdigit(p[-1]))
215c2637e80SChristoph Hellwig 		p--;
216c2637e80SChristoph Hellwig 	if (p == s || !*p || *p == '0')
217c2637e80SChristoph Hellwig 		return 0;
218c2637e80SChristoph Hellwig 
219c2637e80SChristoph Hellwig 	/* try disk name without <part number> */
220c2637e80SChristoph Hellwig 	part = simple_strtoul(p, NULL, 10);
221c2637e80SChristoph Hellwig 	*p = '\0';
222c2637e80SChristoph Hellwig 	devt = blk_lookup_devt(s, part);
223c2637e80SChristoph Hellwig 	if (devt)
224c2637e80SChristoph Hellwig 		return devt;
225c2637e80SChristoph Hellwig 
226c2637e80SChristoph Hellwig 	/* try disk name without p<part number> */
227c2637e80SChristoph Hellwig 	if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
228c2637e80SChristoph Hellwig 		return 0;
229c2637e80SChristoph Hellwig 	p[-1] = '\0';
230c2637e80SChristoph Hellwig 	return blk_lookup_devt(s, part);
231c2637e80SChristoph Hellwig }
232c2637e80SChristoph Hellwig #endif /* CONFIG_BLOCK */
233c2637e80SChristoph Hellwig 
234c2637e80SChristoph Hellwig static dev_t devt_from_devnum(const char *name)
235c2637e80SChristoph Hellwig {
236c2637e80SChristoph Hellwig 	unsigned maj, min, offset;
237c2637e80SChristoph Hellwig 	dev_t devt = 0;
238c2637e80SChristoph Hellwig 	char *p, dummy;
239c2637e80SChristoph Hellwig 
240c2637e80SChristoph Hellwig 	if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
241c2637e80SChristoph Hellwig 	    sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) == 3) {
242c2637e80SChristoph Hellwig 		devt = MKDEV(maj, min);
243c2637e80SChristoph Hellwig 		if (maj != MAJOR(devt) || min != MINOR(devt))
244c2637e80SChristoph Hellwig 			return 0;
245c2637e80SChristoph Hellwig 	} else {
246c2637e80SChristoph Hellwig 		devt = new_decode_dev(simple_strtoul(name, &p, 16));
247c2637e80SChristoph Hellwig 		if (*p)
248c2637e80SChristoph Hellwig 			return 0;
249c2637e80SChristoph Hellwig 	}
250c2637e80SChristoph Hellwig 
251c2637e80SChristoph Hellwig 	return devt;
252c2637e80SChristoph Hellwig }
253b5af921eSWill Drewry 
2541da177e4SLinus Torvalds /*
2551da177e4SLinus Torvalds  *	Convert a name into device number.  We accept the following variants:
2561da177e4SLinus Torvalds  *
2570bf37ae4SPavel Machek  *	1) <hex_major><hex_minor> device number in hexadecimal represents itself
2580bf37ae4SPavel Machek  *         no leading 0x, for example b302.
2591da177e4SLinus Torvalds  *	2) /dev/nfs represents Root_NFS (0xff)
2601da177e4SLinus Torvalds  *	3) /dev/<disk_name> represents the device number of disk
2611da177e4SLinus Torvalds  *	4) /dev/<disk_name><decimal> represents the device number
2621da177e4SLinus Torvalds  *         of partition - device number of disk plus the partition number
2631da177e4SLinus Torvalds  *	5) /dev/<disk_name>p<decimal> - same as the above, that form is
2641da177e4SLinus Torvalds  *	   used when disk name of partitioned disk ends on a digit.
265b5af921eSWill Drewry  *	6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
266b5af921eSWill Drewry  *	   unique id of a partition if the partition table provides it.
267d33b98fcSStephen Warren  *	   The UUID may be either an EFI/GPT UUID, or refer to an MSDOS
268d33b98fcSStephen Warren  *	   partition using the format SSSSSSSS-PP, where SSSSSSSS is a zero-
269d33b98fcSStephen Warren  *	   filled hex representation of the 32-bit "NT disk signature", and PP
270d33b98fcSStephen Warren  *	   is a zero-filled hex representation of the 1-based partition number.
27179975f13SWill Drewry  *	7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
27279975f13SWill Drewry  *	   a partition with a known unique id.
2736c251611SSebastian Capella  *	8) <major>:<minor> major and minor number of the device separated by
2746c251611SSebastian Capella  *	   a colon.
275f027c34dSNikolaus Voss  *	9) PARTLABEL=<name> with name being the GPT partition label.
276f027c34dSNikolaus Voss  *	   MSDOS partitions do not support labels!
2778902dd52SPaulo Alcantara (SUSE)  *	10) /dev/cifs represents Root_CIFS (0xfe)
2781da177e4SLinus Torvalds  *
279edfaa7c3SKay Sievers  *	If name doesn't have fall into the categories above, we return (0,0).
280edfaa7c3SKay Sievers  *	block_class is used to check if something is a disk name. If the disk
281edfaa7c3SKay Sievers  *	name contains slashes, the device name has them replaced with
282edfaa7c3SKay Sievers  *	bangs.
2831da177e4SLinus Torvalds  */
284e6e20a7aSDan Ehrenberg dev_t name_to_dev_t(const char *name)
2851da177e4SLinus Torvalds {
286c2637e80SChristoph Hellwig 	if (strcmp(name, "/dev/nfs") == 0)
287c2637e80SChristoph Hellwig 		return Root_NFS;
288c2637e80SChristoph Hellwig 	if (strcmp(name, "/dev/cifs") == 0)
289c2637e80SChristoph Hellwig 		return Root_CIFS;
290c2637e80SChristoph Hellwig 	if (strcmp(name, "/dev/ram") == 0)
291c2637e80SChristoph Hellwig 		return Root_RAM0;
2926d0aed7aSJens Axboe #ifdef CONFIG_BLOCK
293c2637e80SChristoph Hellwig 	if (strncmp(name, "PARTUUID=", 9) == 0)
294c2637e80SChristoph Hellwig 		return devt_from_partuuid(name + 9);
295c2637e80SChristoph Hellwig 	if (strncmp(name, "PARTLABEL=", 10) == 0)
296c2637e80SChristoph Hellwig 		return devt_from_partlabel(name + 10);
297c2637e80SChristoph Hellwig 	if (strncmp(name, "/dev/", 5) == 0)
298c2637e80SChristoph Hellwig 		return devt_from_devname(name + 5);
2996d0aed7aSJens Axboe #endif
300c2637e80SChristoph Hellwig 	return devt_from_devnum(name);
3011da177e4SLinus Torvalds }
302e6e20a7aSDan Ehrenberg EXPORT_SYMBOL_GPL(name_to_dev_t);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds static int __init root_dev_setup(char *line)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	strlcpy(saved_root_name, line, sizeof(saved_root_name));
3071da177e4SLinus Torvalds 	return 1;
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds __setup("root=", root_dev_setup);
3111da177e4SLinus Torvalds 
312cc1ed754SPierre Ossman static int __init rootwait_setup(char *str)
313cc1ed754SPierre Ossman {
314cc1ed754SPierre Ossman 	if (*str)
315cc1ed754SPierre Ossman 		return 0;
316cc1ed754SPierre Ossman 	root_wait = 1;
317cc1ed754SPierre Ossman 	return 1;
318cc1ed754SPierre Ossman }
319cc1ed754SPierre Ossman 
320cc1ed754SPierre Ossman __setup("rootwait", rootwait_setup);
321cc1ed754SPierre Ossman 
3221da177e4SLinus Torvalds static char * __initdata root_mount_data;
3231da177e4SLinus Torvalds static int __init root_data_setup(char *str)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	root_mount_data = str;
3261da177e4SLinus Torvalds 	return 1;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds static char * __initdata root_fs_names;
3301da177e4SLinus Torvalds static int __init fs_names_setup(char *str)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	root_fs_names = str;
3331da177e4SLinus Torvalds 	return 1;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds static unsigned int __initdata root_delay;
3371da177e4SLinus Torvalds static int __init root_delay_setup(char *str)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	root_delay = simple_strtoul(str, NULL, 0);
3401da177e4SLinus Torvalds 	return 1;
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds __setup("rootflags=", root_data_setup);
3441da177e4SLinus Torvalds __setup("rootfstype=", fs_names_setup);
3451da177e4SLinus Torvalds __setup("rootdelay=", root_delay_setup);
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds static void __init get_fs_names(char *page)
3481da177e4SLinus Torvalds {
3491da177e4SLinus Torvalds 	char *s = page;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	if (root_fs_names) {
3521da177e4SLinus Torvalds 		strcpy(page, root_fs_names);
3531da177e4SLinus Torvalds 		while (*s++) {
3541da177e4SLinus Torvalds 			if (s[-1] == ',')
3551da177e4SLinus Torvalds 				s[-1] = '\0';
3561da177e4SLinus Torvalds 		}
3571da177e4SLinus Torvalds 	} else {
3581da177e4SLinus Torvalds 		int len = get_filesystem_list(page);
3591da177e4SLinus Torvalds 		char *p, *next;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 		page[len] = '\0';
3621da177e4SLinus Torvalds 		for (p = page-1; p; p = next) {
3631da177e4SLinus Torvalds 			next = strchr(++p, '\n');
3641da177e4SLinus Torvalds 			if (*p++ != '\t')
3651da177e4SLinus Torvalds 				continue;
3661da177e4SLinus Torvalds 			while ((*s++ = *p++) != '\n')
3671da177e4SLinus Torvalds 				;
3681da177e4SLinus Torvalds 			s[-1] = '\0';
3691da177e4SLinus Torvalds 		}
3701da177e4SLinus Torvalds 	}
3711da177e4SLinus Torvalds 	*s = '\0';
3721da177e4SLinus Torvalds }
3731da177e4SLinus Torvalds 
374cccaa5e3SDominik Brodowski static int __init do_mount_root(const char *name, const char *fs,
375cccaa5e3SDominik Brodowski 				 const int flags, const void *data)
3761da177e4SLinus Torvalds {
377d8c9584eSAl Viro 	struct super_block *s;
3787de7de7cSLinus Torvalds 	struct page *p = NULL;
3797de7de7cSLinus Torvalds 	char *data_page = NULL;
380cccaa5e3SDominik Brodowski 	int ret;
381cccaa5e3SDominik Brodowski 
3827de7de7cSLinus Torvalds 	if (data) {
383c60166f0SChristoph Hellwig 		/* init_mount() requires a full page as fifth argument */
384cccaa5e3SDominik Brodowski 		p = alloc_page(GFP_KERNEL);
385cccaa5e3SDominik Brodowski 		if (!p)
386cccaa5e3SDominik Brodowski 			return -ENOMEM;
387cccaa5e3SDominik Brodowski 		data_page = page_address(p);
388c60166f0SChristoph Hellwig 		/* zero-pad. init_mount() will make sure it's terminated */
3897de7de7cSLinus Torvalds 		strncpy(data_page, data, PAGE_SIZE);
3907de7de7cSLinus Torvalds 	}
391cccaa5e3SDominik Brodowski 
392c60166f0SChristoph Hellwig 	ret = init_mount(name, "/root", fs, flags, data_page);
393cccaa5e3SDominik Brodowski 	if (ret)
394cccaa5e3SDominik Brodowski 		goto out;
3951da177e4SLinus Torvalds 
396db63f1e3SChristoph Hellwig 	init_chdir("/root");
397d8c9584eSAl Viro 	s = current->fs->pwd.dentry->d_sb;
398d8c9584eSAl Viro 	ROOT_DEV = s->s_dev;
39980cdc6daSMandeep Singh Baines 	printk(KERN_INFO
40080cdc6daSMandeep Singh Baines 	       "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
401d8c9584eSAl Viro 	       s->s_type->name,
402bc98a42cSDavid Howells 	       sb_rdonly(s) ? " readonly" : "",
403d8c9584eSAl Viro 	       MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
404cccaa5e3SDominik Brodowski 
405cccaa5e3SDominik Brodowski out:
4067de7de7cSLinus Torvalds 	if (p)
407cccaa5e3SDominik Brodowski 		put_page(p);
408cccaa5e3SDominik Brodowski 	return ret;
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds void __init mount_block_root(char *name, int flags)
4121da177e4SLinus Torvalds {
41375f296d9SLevin, Alexander (Sasha Levin) 	struct page *page = alloc_page(GFP_KERNEL);
414a608ca21SJeff Layton 	char *fs_names = page_address(page);
4151da177e4SLinus Torvalds 	char *p;
4161da177e4SLinus Torvalds 	char b[BDEVNAME_SIZE];
4171da177e4SLinus Torvalds 
418ea3edd4dSChristoph Hellwig 	scnprintf(b, BDEVNAME_SIZE, "unknown-block(%u,%u)",
419ea3edd4dSChristoph Hellwig 		  MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
4201da177e4SLinus Torvalds 	get_fs_names(fs_names);
4211da177e4SLinus Torvalds retry:
4221da177e4SLinus Torvalds 	for (p = fs_names; *p; p += strlen(p)+1) {
4231da177e4SLinus Torvalds 		int err = do_mount_root(name, p, flags, root_mount_data);
4241da177e4SLinus Torvalds 		switch (err) {
4251da177e4SLinus Torvalds 			case 0:
4261da177e4SLinus Torvalds 				goto out;
4271da177e4SLinus Torvalds 			case -EACCES:
4281da177e4SLinus Torvalds 			case -EINVAL:
4291da177e4SLinus Torvalds 				continue;
4301da177e4SLinus Torvalds 		}
4311da177e4SLinus Torvalds 	        /*
4321da177e4SLinus Torvalds 		 * Allow the user to distinguish between failed sys_open
4331da177e4SLinus Torvalds 		 * and bad superblock on root device.
434dd2a345fSDave Gilbert 		 * and give them a list of the available devices
4351da177e4SLinus Torvalds 		 */
4360e0cb892SBernhard Walle 		printk("VFS: Cannot open root device \"%s\" or %s: error %d\n",
4370e0cb892SBernhard Walle 				root_device_name, b, err);
438dd2a345fSDave Gilbert 		printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
4391da177e4SLinus Torvalds 
440dd2a345fSDave Gilbert 		printk_all_partitions();
44155dc7db7STejun Heo #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
44255dc7db7STejun Heo 		printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
44355dc7db7STejun Heo 		       "explicit textual name for \"root=\" boot option.\n");
44455dc7db7STejun Heo #endif
4451da177e4SLinus Torvalds 		panic("VFS: Unable to mount root fs on %s", b);
4461da177e4SLinus Torvalds 	}
447e462ec50SDavid Howells 	if (!(flags & SB_RDONLY)) {
448e462ec50SDavid Howells 		flags |= SB_RDONLY;
44910975933SMiklos Szeredi 		goto retry;
45010975933SMiklos Szeredi 	}
451be6e028bSAndy Whitcroft 
452dd2a345fSDave Gilbert 	printk("List of all partitions:\n");
453dd2a345fSDave Gilbert 	printk_all_partitions();
454be6e028bSAndy Whitcroft 	printk("No filesystem could mount root, tried: ");
455be6e028bSAndy Whitcroft 	for (p = fs_names; *p; p += strlen(p)+1)
456be6e028bSAndy Whitcroft 		printk(" %s", p);
457be6e028bSAndy Whitcroft 	printk("\n");
4589361401eSDavid Howells 	panic("VFS: Unable to mount root fs on %s", b);
4591da177e4SLinus Torvalds out:
460a608ca21SJeff Layton 	put_page(page);
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS
46443717c7dSChuck Lever 
46543717c7dSChuck Lever #define NFSROOT_TIMEOUT_MIN	5
46643717c7dSChuck Lever #define NFSROOT_TIMEOUT_MAX	30
46743717c7dSChuck Lever #define NFSROOT_RETRY_MAX	5
46843717c7dSChuck Lever 
4691da177e4SLinus Torvalds static int __init mount_nfs_root(void)
4701da177e4SLinus Torvalds {
47156463e50SChuck Lever 	char *root_dev, *root_data;
47243717c7dSChuck Lever 	unsigned int timeout;
47343717c7dSChuck Lever 	int try, err;
4741da177e4SLinus Torvalds 
47543717c7dSChuck Lever 	err = nfs_root_data(&root_dev, &root_data);
47643717c7dSChuck Lever 	if (err != 0)
4771da177e4SLinus Torvalds 		return 0;
47843717c7dSChuck Lever 
47943717c7dSChuck Lever 	/*
48043717c7dSChuck Lever 	 * The server or network may not be ready, so try several
48143717c7dSChuck Lever 	 * times.  Stop after a few tries in case the client wants
48243717c7dSChuck Lever 	 * to fall back to other boot methods.
48343717c7dSChuck Lever 	 */
48443717c7dSChuck Lever 	timeout = NFSROOT_TIMEOUT_MIN;
48543717c7dSChuck Lever 	for (try = 1; ; try++) {
48643717c7dSChuck Lever 		err = do_mount_root(root_dev, "nfs",
48743717c7dSChuck Lever 					root_mountflags, root_data);
48843717c7dSChuck Lever 		if (err == 0)
48956463e50SChuck Lever 			return 1;
49043717c7dSChuck Lever 		if (try > NFSROOT_RETRY_MAX)
49143717c7dSChuck Lever 			break;
49243717c7dSChuck Lever 
49343717c7dSChuck Lever 		/* Wait, in case the server refused us immediately */
49443717c7dSChuck Lever 		ssleep(timeout);
49543717c7dSChuck Lever 		timeout <<= 1;
49643717c7dSChuck Lever 		if (timeout > NFSROOT_TIMEOUT_MAX)
49743717c7dSChuck Lever 			timeout = NFSROOT_TIMEOUT_MAX;
49843717c7dSChuck Lever 	}
49943717c7dSChuck Lever 	return 0;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds #endif
5021da177e4SLinus Torvalds 
5038902dd52SPaulo Alcantara (SUSE) #ifdef CONFIG_CIFS_ROOT
5048902dd52SPaulo Alcantara (SUSE) 
5058902dd52SPaulo Alcantara (SUSE) extern int cifs_root_data(char **dev, char **opts);
5068902dd52SPaulo Alcantara (SUSE) 
5078902dd52SPaulo Alcantara (SUSE) #define CIFSROOT_TIMEOUT_MIN	5
5088902dd52SPaulo Alcantara (SUSE) #define CIFSROOT_TIMEOUT_MAX	30
5098902dd52SPaulo Alcantara (SUSE) #define CIFSROOT_RETRY_MAX	5
5108902dd52SPaulo Alcantara (SUSE) 
5118902dd52SPaulo Alcantara (SUSE) static int __init mount_cifs_root(void)
5128902dd52SPaulo Alcantara (SUSE) {
5138902dd52SPaulo Alcantara (SUSE) 	char *root_dev, *root_data;
5148902dd52SPaulo Alcantara (SUSE) 	unsigned int timeout;
5158902dd52SPaulo Alcantara (SUSE) 	int try, err;
5168902dd52SPaulo Alcantara (SUSE) 
5178902dd52SPaulo Alcantara (SUSE) 	err = cifs_root_data(&root_dev, &root_data);
5188902dd52SPaulo Alcantara (SUSE) 	if (err != 0)
5198902dd52SPaulo Alcantara (SUSE) 		return 0;
5208902dd52SPaulo Alcantara (SUSE) 
5218902dd52SPaulo Alcantara (SUSE) 	timeout = CIFSROOT_TIMEOUT_MIN;
5228902dd52SPaulo Alcantara (SUSE) 	for (try = 1; ; try++) {
5238902dd52SPaulo Alcantara (SUSE) 		err = do_mount_root(root_dev, "cifs", root_mountflags,
5248902dd52SPaulo Alcantara (SUSE) 				    root_data);
5258902dd52SPaulo Alcantara (SUSE) 		if (err == 0)
5268902dd52SPaulo Alcantara (SUSE) 			return 1;
5278902dd52SPaulo Alcantara (SUSE) 		if (try > CIFSROOT_RETRY_MAX)
5288902dd52SPaulo Alcantara (SUSE) 			break;
5298902dd52SPaulo Alcantara (SUSE) 
5308902dd52SPaulo Alcantara (SUSE) 		ssleep(timeout);
5318902dd52SPaulo Alcantara (SUSE) 		timeout <<= 1;
5328902dd52SPaulo Alcantara (SUSE) 		if (timeout > CIFSROOT_TIMEOUT_MAX)
5338902dd52SPaulo Alcantara (SUSE) 			timeout = CIFSROOT_TIMEOUT_MAX;
5348902dd52SPaulo Alcantara (SUSE) 	}
5358902dd52SPaulo Alcantara (SUSE) 	return 0;
5368902dd52SPaulo Alcantara (SUSE) }
5378902dd52SPaulo Alcantara (SUSE) #endif
5388902dd52SPaulo Alcantara (SUSE) 
5391da177e4SLinus Torvalds void __init mount_root(void)
5401da177e4SLinus Torvalds {
5411da177e4SLinus Torvalds #ifdef CONFIG_ROOT_NFS
542377485f6SSasha Levin 	if (ROOT_DEV == Root_NFS) {
543c8376994SChristoph Hellwig 		if (!mount_nfs_root())
544c8376994SChristoph Hellwig 			printk(KERN_ERR "VFS: Unable to mount root fs via NFS.\n");
5451da177e4SLinus Torvalds 		return;
5461da177e4SLinus Torvalds 	}
5471da177e4SLinus Torvalds #endif
5488902dd52SPaulo Alcantara (SUSE) #ifdef CONFIG_CIFS_ROOT
5498902dd52SPaulo Alcantara (SUSE) 	if (ROOT_DEV == Root_CIFS) {
550c8376994SChristoph Hellwig 		if (!mount_cifs_root())
551c8376994SChristoph Hellwig 			printk(KERN_ERR "VFS: Unable to mount root fs via SMB.\n");
5528902dd52SPaulo Alcantara (SUSE) 		return;
5531da177e4SLinus Torvalds 	}
5541da177e4SLinus Torvalds #endif
5559361401eSDavid Howells #ifdef CONFIG_BLOCK
556c69e3c3aSVishnu Pratap Singh 	{
557c69e3c3aSVishnu Pratap Singh 		int err = create_dev("/dev/root", ROOT_DEV);
558c69e3c3aSVishnu Pratap Singh 
559c69e3c3aSVishnu Pratap Singh 		if (err < 0)
560c69e3c3aSVishnu Pratap Singh 			pr_emerg("Failed to create /dev/root: %d\n", err);
5611da177e4SLinus Torvalds 		mount_block_root("/dev/root", root_mountflags);
562c69e3c3aSVishnu Pratap Singh 	}
5639361401eSDavid Howells #endif
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds /*
5671da177e4SLinus Torvalds  * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
5681da177e4SLinus Torvalds  */
5691da177e4SLinus Torvalds void __init prepare_namespace(void)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	if (root_delay) {
5721da177e4SLinus Torvalds 		printk(KERN_INFO "Waiting %d sec before mounting root device...\n",
5731da177e4SLinus Torvalds 		       root_delay);
5741da177e4SLinus Torvalds 		ssleep(root_delay);
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds 
577216773a7SArjan van de Ven 	/*
578216773a7SArjan van de Ven 	 * wait for the known devices to complete their probing
579216773a7SArjan van de Ven 	 *
580216773a7SArjan van de Ven 	 * Note: this is a potential source of long boot delays.
581216773a7SArjan van de Ven 	 * For example, it is not atypical to wait 5 seconds here
582216773a7SArjan van de Ven 	 * for the touchpad of a laptop to initialize.
583216773a7SArjan van de Ven 	 */
584216773a7SArjan van de Ven 	wait_for_device_probe();
585d779249eSGreg Kroah-Hartman 
5861da177e4SLinus Torvalds 	md_run_setup();
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	if (saved_root_name[0]) {
5891da177e4SLinus Torvalds 		root_device_name = saved_root_name;
5902d62f488SAdrian Hunter 		if (!strncmp(root_device_name, "mtd", 3) ||
5912d62f488SAdrian Hunter 		    !strncmp(root_device_name, "ubi", 3)) {
592e9482b43SJoern Engel 			mount_block_root(root_device_name, root_mountflags);
593e9482b43SJoern Engel 			goto out;
594e9482b43SJoern Engel 		}
5951da177e4SLinus Torvalds 		ROOT_DEV = name_to_dev_t(root_device_name);
5961da177e4SLinus Torvalds 		if (strncmp(root_device_name, "/dev/", 5) == 0)
5971da177e4SLinus Torvalds 			root_device_name += 5;
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	if (initrd_load())
6011da177e4SLinus Torvalds 		goto out;
6021da177e4SLinus Torvalds 
603cc1ed754SPierre Ossman 	/* wait for any asynchronous scanning to complete */
604cc1ed754SPierre Ossman 	if ((ROOT_DEV == 0) && root_wait) {
605cc1ed754SPierre Ossman 		printk(KERN_INFO "Waiting for root device %s...\n",
606cc1ed754SPierre Ossman 			saved_root_name);
607cc1ed754SPierre Ossman 		while (driver_probe_done() != 0 ||
608cc1ed754SPierre Ossman 			(ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
60939a0e975SJungseung Lee 			msleep(5);
610216773a7SArjan van de Ven 		async_synchronize_full();
611cc1ed754SPierre Ossman 	}
612cc1ed754SPierre Ossman 
6131da177e4SLinus Torvalds 	mount_root();
6141da177e4SLinus Torvalds out:
6155e787dbfSDominik Brodowski 	devtmpfs_mount();
616c60166f0SChristoph Hellwig 	init_mount(".", "/", NULL, MS_MOVE, NULL);
6174b7ca501SChristoph Hellwig 	init_chroot(".");
6181da177e4SLinus Torvalds }
61957f150a5SRob Landley 
6206e19ededSRob Landley static bool is_tmpfs;
621f3235626SDavid Howells static int rootfs_init_fs_context(struct fs_context *fc)
62257f150a5SRob Landley {
6236e19ededSRob Landley 	if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
624f3235626SDavid Howells 		return shmem_init_fs_context(fc);
6256e19ededSRob Landley 
626f3235626SDavid Howells 	return ramfs_init_fs_context(fc);
62757f150a5SRob Landley }
62857f150a5SRob Landley 
629fd3e007fSAl Viro struct file_system_type rootfs_fs_type = {
63057f150a5SRob Landley 	.name		= "rootfs",
631f3235626SDavid Howells 	.init_fs_context = rootfs_init_fs_context,
63257f150a5SRob Landley 	.kill_sb	= kill_litter_super,
63357f150a5SRob Landley };
63457f150a5SRob Landley 
635037f11b4SAl Viro void __init init_rootfs(void)
63657f150a5SRob Landley {
6376e19ededSRob Landley 	if (IS_ENABLED(CONFIG_TMPFS) && !saved_root_name[0] &&
638037f11b4SAl Viro 		(!root_fs_names || strstr(root_fs_names, "tmpfs")))
6396e19ededSRob Landley 		is_tmpfs = true;
64057f150a5SRob Landley }
641