xref: /linux/init/initramfs.c (revision 02aff8480533817a29e820729360866441d7403d)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds #include <linux/init.h>
3e7cb072eSRasmus Villemoes #include <linux/async.h>
41da177e4SLinus Torvalds #include <linux/fs.h>
51da177e4SLinus Torvalds #include <linux/slab.h>
61da177e4SLinus Torvalds #include <linux/types.h>
71da177e4SLinus Torvalds #include <linux/fcntl.h>
81da177e4SLinus Torvalds #include <linux/delay.h>
91da177e4SLinus Torvalds #include <linux/string.h>
10df52092fSLi, Shaohua #include <linux/dirent.h>
111da177e4SLinus Torvalds #include <linux/syscalls.h>
12889d51a1SNye Liu #include <linux/utime.h>
1308865514SLokesh Vutla #include <linux/file.h>
14f3296f80SChristophe JAILLET #include <linux/kstrtox.h>
15899ee4afSMike Rapoport #include <linux/memblock.h>
16dd23e809SFlorian Fainelli #include <linux/mm.h>
17b2a74d5fSChristoph Hellwig #include <linux/namei.h>
188fb9f73eSChristoph Hellwig #include <linux/init_syscalls.h>
1968d85f0aSEric W. Biederman #include <linux/task_work.h>
20b234ed6dSRasmus Villemoes #include <linux/umh.h>
211da177e4SLinus Torvalds 
22800c24dcSDavid Disseldorp static __initdata bool csum_present;
23800c24dcSDavid Disseldorp static __initdata u32 io_csum;
24800c24dcSDavid Disseldorp 
25800c24dcSDavid Disseldorp static ssize_t __init xwrite(struct file *file, const unsigned char *p,
26800c24dcSDavid Disseldorp 		size_t count, loff_t *pos)
2738747439SYinghai Lu {
2838747439SYinghai Lu 	ssize_t out = 0;
2938747439SYinghai Lu 
3038747439SYinghai Lu 	/* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
3138747439SYinghai Lu 	while (count) {
32bf6419e4SChristoph Hellwig 		ssize_t rv = kernel_write(file, p, count, pos);
3338747439SYinghai Lu 
3438747439SYinghai Lu 		if (rv < 0) {
3538747439SYinghai Lu 			if (rv == -EINTR || rv == -EAGAIN)
3638747439SYinghai Lu 				continue;
3738747439SYinghai Lu 			return out ? out : rv;
3838747439SYinghai Lu 		} else if (rv == 0)
3938747439SYinghai Lu 			break;
4038747439SYinghai Lu 
41800c24dcSDavid Disseldorp 		if (csum_present) {
42800c24dcSDavid Disseldorp 			ssize_t i;
43800c24dcSDavid Disseldorp 
44800c24dcSDavid Disseldorp 			for (i = 0; i < rv; i++)
45800c24dcSDavid Disseldorp 				io_csum += p[i];
46800c24dcSDavid Disseldorp 		}
47800c24dcSDavid Disseldorp 
4838747439SYinghai Lu 		p += rv;
4938747439SYinghai Lu 		out += rv;
5038747439SYinghai Lu 		count -= rv;
5138747439SYinghai Lu 	}
5238747439SYinghai Lu 
5338747439SYinghai Lu 	return out;
5438747439SYinghai Lu }
5538747439SYinghai Lu 
561da177e4SLinus Torvalds static __initdata char *message;
571da177e4SLinus Torvalds static void __init error(char *x)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	if (!message)
601da177e4SLinus Torvalds 		message = x;
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds 
63735faf92SBenjamin Gray #define panic_show_mem(fmt, ...) \
64527ed4f7SKefeng Wang 	({ show_mem(); panic(fmt, ##__VA_ARGS__); })
65dd23e809SFlorian Fainelli 
661da177e4SLinus Torvalds /* link hash */
671da177e4SLinus Torvalds 
686a050da4SMark Huang #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
696a050da4SMark Huang 
701da177e4SLinus Torvalds static __initdata struct hash {
711da177e4SLinus Torvalds 	int ino, minor, major;
72685dd2d5SAl Viro 	umode_t mode;
731da177e4SLinus Torvalds 	struct hash *next;
746a050da4SMark Huang 	char name[N_ALIGN(PATH_MAX)];
751da177e4SLinus Torvalds } *head[32];
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds static inline int hash(int major, int minor, int ino)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	unsigned long tmp = ino + minor + (major << 3);
801da177e4SLinus Torvalds 	tmp += tmp >> 5;
811da177e4SLinus Torvalds 	return tmp & 31;
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
842139a7fbSH. Peter Anvin static char __init *find_link(int major, int minor, int ino,
85685dd2d5SAl Viro 			      umode_t mode, char *name)
861da177e4SLinus Torvalds {
871da177e4SLinus Torvalds 	struct hash **p, *q;
881da177e4SLinus Torvalds 	for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
891da177e4SLinus Torvalds 		if ((*p)->ino != ino)
901da177e4SLinus Torvalds 			continue;
911da177e4SLinus Torvalds 		if ((*p)->minor != minor)
921da177e4SLinus Torvalds 			continue;
931da177e4SLinus Torvalds 		if ((*p)->major != major)
941da177e4SLinus Torvalds 			continue;
952139a7fbSH. Peter Anvin 		if (((*p)->mode ^ mode) & S_IFMT)
962139a7fbSH. Peter Anvin 			continue;
971da177e4SLinus Torvalds 		return (*p)->name;
981da177e4SLinus Torvalds 	}
993265e66bSThomas Petazzoni 	q = kmalloc(sizeof(struct hash), GFP_KERNEL);
1001da177e4SLinus Torvalds 	if (!q)
101dd23e809SFlorian Fainelli 		panic_show_mem("can't allocate link hash entry");
1021da177e4SLinus Torvalds 	q->major = major;
1032139a7fbSH. Peter Anvin 	q->minor = minor;
1042139a7fbSH. Peter Anvin 	q->ino = ino;
1052139a7fbSH. Peter Anvin 	q->mode = mode;
1066a050da4SMark Huang 	strcpy(q->name, name);
1071da177e4SLinus Torvalds 	q->next = NULL;
1081da177e4SLinus Torvalds 	*p = q;
1091da177e4SLinus Torvalds 	return NULL;
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static void __init free_hash(void)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	struct hash **p, *q;
1151da177e4SLinus Torvalds 	for (p = head; p < head + 32; p++) {
1161da177e4SLinus Torvalds 		while (*p) {
1171da177e4SLinus Torvalds 			q = *p;
1181da177e4SLinus Torvalds 			*p = q->next;
1193265e66bSThomas Petazzoni 			kfree(q);
1201da177e4SLinus Torvalds 		}
1211da177e4SLinus Torvalds 	}
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1241274aea1SDavid Disseldorp #ifdef CONFIG_INITRAMFS_PRESERVE_MTIME
1251274aea1SDavid Disseldorp static void __init do_utime(char *filename, time64_t mtime)
126889d51a1SNye Liu {
1271274aea1SDavid Disseldorp 	struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } };
1281274aea1SDavid Disseldorp 	init_utimes(filename, t);
1291274aea1SDavid Disseldorp }
130889d51a1SNye Liu 
1311274aea1SDavid Disseldorp static void __init do_utime_path(const struct path *path, time64_t mtime)
1321274aea1SDavid Disseldorp {
1331274aea1SDavid Disseldorp 	struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } };
1341274aea1SDavid Disseldorp 	vfs_utimes(path, t);
135889d51a1SNye Liu }
136889d51a1SNye Liu 
137889d51a1SNye Liu static __initdata LIST_HEAD(dir_list);
138889d51a1SNye Liu struct dir_entry {
139889d51a1SNye Liu 	struct list_head list;
140e35c4c64SArnd Bergmann 	time64_t mtime;
141fcb7aeddSDavid Disseldorp 	char name[];
142889d51a1SNye Liu };
143889d51a1SNye Liu 
144e35c4c64SArnd Bergmann static void __init dir_add(const char *name, time64_t mtime)
145889d51a1SNye Liu {
146fcb7aeddSDavid Disseldorp 	size_t nlen = strlen(name) + 1;
147fcb7aeddSDavid Disseldorp 	struct dir_entry *de;
148fcb7aeddSDavid Disseldorp 
149fcb7aeddSDavid Disseldorp 	de = kmalloc(sizeof(struct dir_entry) + nlen, GFP_KERNEL);
150889d51a1SNye Liu 	if (!de)
151dd23e809SFlorian Fainelli 		panic_show_mem("can't allocate dir_entry buffer");
152889d51a1SNye Liu 	INIT_LIST_HEAD(&de->list);
153fcb7aeddSDavid Disseldorp 	strscpy(de->name, name, nlen);
154889d51a1SNye Liu 	de->mtime = mtime;
155889d51a1SNye Liu 	list_add(&de->list, &dir_list);
156889d51a1SNye Liu }
157889d51a1SNye Liu 
158889d51a1SNye Liu static void __init dir_utime(void)
159889d51a1SNye Liu {
160889d51a1SNye Liu 	struct dir_entry *de, *tmp;
161889d51a1SNye Liu 	list_for_each_entry_safe(de, tmp, &dir_list, list) {
162889d51a1SNye Liu 		list_del(&de->list);
163889d51a1SNye Liu 		do_utime(de->name, de->mtime);
164889d51a1SNye Liu 		kfree(de);
165889d51a1SNye Liu 	}
166889d51a1SNye Liu }
1671274aea1SDavid Disseldorp #else
1681274aea1SDavid Disseldorp static void __init do_utime(char *filename, time64_t mtime) {}
1691274aea1SDavid Disseldorp static void __init do_utime_path(const struct path *path, time64_t mtime) {}
1701274aea1SDavid Disseldorp static void __init dir_add(const char *name, time64_t mtime) {}
1711274aea1SDavid Disseldorp static void __init dir_utime(void) {}
1721274aea1SDavid Disseldorp #endif
173889d51a1SNye Liu 
174e35c4c64SArnd Bergmann static __initdata time64_t mtime;
175889d51a1SNye Liu 
1761da177e4SLinus Torvalds /* cpio header parsing */
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds static __initdata unsigned long ino, major, minor, nlink;
179685dd2d5SAl Viro static __initdata umode_t mode;
1801da177e4SLinus Torvalds static __initdata unsigned long body_len, name_len;
1811da177e4SLinus Torvalds static __initdata uid_t uid;
1821da177e4SLinus Torvalds static __initdata gid_t gid;
1831da177e4SLinus Torvalds static __initdata unsigned rdev;
184800c24dcSDavid Disseldorp static __initdata u32 hdr_csum;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds static void __init parse_header(char *s)
1871da177e4SLinus Torvalds {
188800c24dcSDavid Disseldorp 	unsigned long parsed[13];
1891da177e4SLinus Torvalds 	char buf[9];
1901da177e4SLinus Torvalds 	int i;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	buf[8] = '\0';
193800c24dcSDavid Disseldorp 	for (i = 0, s += 6; i < 13; i++, s += 8) {
1941da177e4SLinus Torvalds 		memcpy(buf, s, 8);
1951da177e4SLinus Torvalds 		parsed[i] = simple_strtoul(buf, NULL, 16);
1961da177e4SLinus Torvalds 	}
1971da177e4SLinus Torvalds 	ino = parsed[0];
1981da177e4SLinus Torvalds 	mode = parsed[1];
1991da177e4SLinus Torvalds 	uid = parsed[2];
2001da177e4SLinus Torvalds 	gid = parsed[3];
2011da177e4SLinus Torvalds 	nlink = parsed[4];
202e35c4c64SArnd Bergmann 	mtime = parsed[5]; /* breaks in y2106 */
2031da177e4SLinus Torvalds 	body_len = parsed[6];
2041da177e4SLinus Torvalds 	major = parsed[7];
2051da177e4SLinus Torvalds 	minor = parsed[8];
2061da177e4SLinus Torvalds 	rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
2071da177e4SLinus Torvalds 	name_len = parsed[11];
208800c24dcSDavid Disseldorp 	hdr_csum = parsed[12];
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /* FSM */
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds static __initdata enum state {
2141da177e4SLinus Torvalds 	Start,
2151da177e4SLinus Torvalds 	Collect,
2161da177e4SLinus Torvalds 	GotHeader,
2171da177e4SLinus Torvalds 	SkipIt,
2181da177e4SLinus Torvalds 	GotName,
2191da177e4SLinus Torvalds 	CopyFile,
2201da177e4SLinus Torvalds 	GotSymlink,
2211da177e4SLinus Torvalds 	Reset
2221da177e4SLinus Torvalds } state, next_state;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds static __initdata char *victim;
225c34d85acSMark Rustad static unsigned long byte_count __initdata;
2261da177e4SLinus Torvalds static __initdata loff_t this_header, next_header;
2271da177e4SLinus Torvalds 
228b0a5ab93SAl Viro static inline void __init eat(unsigned n)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	victim += n;
2311da177e4SLinus Torvalds 	this_header += n;
232c34d85acSMark Rustad 	byte_count -= n;
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds static __initdata char *collected;
236d97b07c5SYinghai Lu static long remains __initdata;
2371da177e4SLinus Torvalds static __initdata char *collect;
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds static void __init read_into(char *buf, unsigned size, enum state next)
2401da177e4SLinus Torvalds {
241c34d85acSMark Rustad 	if (byte_count >= size) {
2421da177e4SLinus Torvalds 		collected = victim;
2431da177e4SLinus Torvalds 		eat(size);
2441da177e4SLinus Torvalds 		state = next;
2451da177e4SLinus Torvalds 	} else {
2461da177e4SLinus Torvalds 		collect = collected = buf;
2471da177e4SLinus Torvalds 		remains = size;
2481da177e4SLinus Torvalds 		next_state = next;
2491da177e4SLinus Torvalds 		state = Collect;
2501da177e4SLinus Torvalds 	}
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds static __initdata char *header_buf, *symlink_buf, *name_buf;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds static int __init do_start(void)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	read_into(header_buf, 110, GotHeader);
2581da177e4SLinus Torvalds 	return 0;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds static int __init do_collect(void)
2621da177e4SLinus Torvalds {
263d97b07c5SYinghai Lu 	unsigned long n = remains;
264c34d85acSMark Rustad 	if (byte_count < n)
265c34d85acSMark Rustad 		n = byte_count;
2661da177e4SLinus Torvalds 	memcpy(collect, victim, n);
2671da177e4SLinus Torvalds 	eat(n);
2681da177e4SLinus Torvalds 	collect += n;
2691da177e4SLinus Torvalds 	if ((remains -= n) != 0)
2701da177e4SLinus Torvalds 		return 1;
2711da177e4SLinus Torvalds 	state = next_state;
2721da177e4SLinus Torvalds 	return 0;
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds static int __init do_header(void)
2761da177e4SLinus Torvalds {
277800c24dcSDavid Disseldorp 	if (!memcmp(collected, "070701", 6)) {
278800c24dcSDavid Disseldorp 		csum_present = false;
279800c24dcSDavid Disseldorp 	} else if (!memcmp(collected, "070702", 6)) {
280800c24dcSDavid Disseldorp 		csum_present = true;
281800c24dcSDavid Disseldorp 	} else {
282da028e4cSDavid Disseldorp 		if (memcmp(collected, "070707", 6) == 0)
283da028e4cSDavid Disseldorp 			error("incorrect cpio method used: use -H newc option");
284da028e4cSDavid Disseldorp 		else
2851da177e4SLinus Torvalds 			error("no cpio magic");
2861da177e4SLinus Torvalds 		return 1;
2871da177e4SLinus Torvalds 	}
2881da177e4SLinus Torvalds 	parse_header(collected);
2891da177e4SLinus Torvalds 	next_header = this_header + N_ALIGN(name_len) + body_len;
2901da177e4SLinus Torvalds 	next_header = (next_header + 3) & ~3;
2911da177e4SLinus Torvalds 	state = SkipIt;
2921da177e4SLinus Torvalds 	if (name_len <= 0 || name_len > PATH_MAX)
2931da177e4SLinus Torvalds 		return 0;
2941da177e4SLinus Torvalds 	if (S_ISLNK(mode)) {
2951da177e4SLinus Torvalds 		if (body_len > PATH_MAX)
2961da177e4SLinus Torvalds 			return 0;
2971da177e4SLinus Torvalds 		collect = collected = symlink_buf;
2981da177e4SLinus Torvalds 		remains = N_ALIGN(name_len) + body_len;
2991da177e4SLinus Torvalds 		next_state = GotSymlink;
3001da177e4SLinus Torvalds 		state = Collect;
3011da177e4SLinus Torvalds 		return 0;
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds 	if (S_ISREG(mode) || !body_len)
3041da177e4SLinus Torvalds 		read_into(name_buf, N_ALIGN(name_len), GotName);
3051da177e4SLinus Torvalds 	return 0;
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds static int __init do_skip(void)
3091da177e4SLinus Torvalds {
310c34d85acSMark Rustad 	if (this_header + byte_count < next_header) {
311c34d85acSMark Rustad 		eat(byte_count);
3121da177e4SLinus Torvalds 		return 1;
3131da177e4SLinus Torvalds 	} else {
3141da177e4SLinus Torvalds 		eat(next_header - this_header);
3151da177e4SLinus Torvalds 		state = next_state;
3161da177e4SLinus Torvalds 		return 0;
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds static int __init do_reset(void)
3211da177e4SLinus Torvalds {
322c34d85acSMark Rustad 	while (byte_count && *victim == '\0')
3231da177e4SLinus Torvalds 		eat(1);
324c34d85acSMark Rustad 	if (byte_count && (this_header & 3))
3251da177e4SLinus Torvalds 		error("broken padding");
3261da177e4SLinus Torvalds 	return 1;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
329c34d85acSMark Rustad static void __init clean_path(char *path, umode_t fmode)
3302139a7fbSH. Peter Anvin {
331046aa126SArnd Bergmann 	struct kstat st;
3322139a7fbSH. Peter Anvin 
3337b81ce7cSBarret Rhoden 	if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) &&
334716308a5SChristoph Hellwig 	    (st.mode ^ fmode) & S_IFMT) {
335046aa126SArnd Bergmann 		if (S_ISDIR(st.mode))
33620cce026SChristoph Hellwig 			init_rmdir(path);
3372139a7fbSH. Peter Anvin 		else
3388fb9f73eSChristoph Hellwig 			init_unlink(path);
3392139a7fbSH. Peter Anvin 	}
3402139a7fbSH. Peter Anvin }
3412139a7fbSH. Peter Anvin 
3427c0950d4SLi Zhijian static int __init maybe_link(void)
3437c0950d4SLi Zhijian {
3447c0950d4SLi Zhijian 	if (nlink >= 2) {
3457c0950d4SLi Zhijian 		char *old = find_link(major, minor, ino, mode, collected);
3467c0950d4SLi Zhijian 		if (old) {
3477c0950d4SLi Zhijian 			clean_path(collected, 0);
348812931d6SChristoph Hellwig 			return (init_link(old, collected) < 0) ? -1 : 1;
3497c0950d4SLi Zhijian 		}
3507c0950d4SLi Zhijian 	}
3517c0950d4SLi Zhijian 	return 0;
3527c0950d4SLi Zhijian }
3537c0950d4SLi Zhijian 
354bf6419e4SChristoph Hellwig static __initdata struct file *wfile;
355bf6419e4SChristoph Hellwig static __initdata loff_t wfile_pos;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds static int __init do_name(void)
3581da177e4SLinus Torvalds {
3591da177e4SLinus Torvalds 	state = SkipIt;
3601da177e4SLinus Torvalds 	next_state = Reset;
3611da177e4SLinus Torvalds 	if (strcmp(collected, "TRAILER!!!") == 0) {
3621da177e4SLinus Torvalds 		free_hash();
3631da177e4SLinus Torvalds 		return 0;
3641da177e4SLinus Torvalds 	}
3652139a7fbSH. Peter Anvin 	clean_path(collected, mode);
3661da177e4SLinus Torvalds 	if (S_ISREG(mode)) {
3672139a7fbSH. Peter Anvin 		int ml = maybe_link();
3682139a7fbSH. Peter Anvin 		if (ml >= 0) {
3692139a7fbSH. Peter Anvin 			int openflags = O_WRONLY|O_CREAT;
3702139a7fbSH. Peter Anvin 			if (ml != 1)
3712139a7fbSH. Peter Anvin 				openflags |= O_TRUNC;
372bf6419e4SChristoph Hellwig 			wfile = filp_open(collected, openflags, mode);
373bf6419e4SChristoph Hellwig 			if (IS_ERR(wfile))
374bf6419e4SChristoph Hellwig 				return 0;
375bf6419e4SChristoph Hellwig 			wfile_pos = 0;
376800c24dcSDavid Disseldorp 			io_csum = 0;
3772139a7fbSH. Peter Anvin 
378bf6419e4SChristoph Hellwig 			vfs_fchown(wfile, uid, gid);
379bf6419e4SChristoph Hellwig 			vfs_fchmod(wfile, mode);
380d20d5a74SRandy Robertson 			if (body_len)
381bf6419e4SChristoph Hellwig 				vfs_truncate(&wfile->f_path, body_len);
3821da177e4SLinus Torvalds 			state = CopyFile;
3831da177e4SLinus Torvalds 		}
3841da177e4SLinus Torvalds 	} else if (S_ISDIR(mode)) {
38583ff98c3SChristoph Hellwig 		init_mkdir(collected, mode);
386b873498fSChristoph Hellwig 		init_chown(collected, uid, gid, 0);
3871097742eSChristoph Hellwig 		init_chmod(collected, mode);
388889d51a1SNye Liu 		dir_add(collected, mtime);
3891da177e4SLinus Torvalds 	} else if (S_ISBLK(mode) || S_ISCHR(mode) ||
3901da177e4SLinus Torvalds 		   S_ISFIFO(mode) || S_ISSOCK(mode)) {
3911da177e4SLinus Torvalds 		if (maybe_link() == 0) {
3925fee64fcSChristoph Hellwig 			init_mknod(collected, mode, rdev);
393b873498fSChristoph Hellwig 			init_chown(collected, uid, gid, 0);
3941097742eSChristoph Hellwig 			init_chmod(collected, mode);
395889d51a1SNye Liu 			do_utime(collected, mtime);
3961da177e4SLinus Torvalds 		}
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds 	return 0;
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds static int __init do_copy(void)
4021da177e4SLinus Torvalds {
403c34d85acSMark Rustad 	if (byte_count >= body_len) {
404bf6419e4SChristoph Hellwig 		if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len)
4059687fd91SDavid Engraf 			error("write error");
40638b08223SChristoph Hellwig 
4071274aea1SDavid Disseldorp 		do_utime_path(&wfile->f_path, mtime);
408bf6419e4SChristoph Hellwig 		fput(wfile);
409800c24dcSDavid Disseldorp 		if (csum_present && io_csum != hdr_csum)
410800c24dcSDavid Disseldorp 			error("bad data checksum");
4111da177e4SLinus Torvalds 		eat(body_len);
4121da177e4SLinus Torvalds 		state = SkipIt;
4131da177e4SLinus Torvalds 		return 0;
4141da177e4SLinus Torvalds 	} else {
415bf6419e4SChristoph Hellwig 		if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count)
4169687fd91SDavid Engraf 			error("write error");
417c34d85acSMark Rustad 		body_len -= byte_count;
418c34d85acSMark Rustad 		eat(byte_count);
4191da177e4SLinus Torvalds 		return 1;
4201da177e4SLinus Torvalds 	}
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds static int __init do_symlink(void)
4241da177e4SLinus Torvalds {
4251da177e4SLinus Torvalds 	collected[N_ALIGN(name_len) + body_len] = '\0';
4262139a7fbSH. Peter Anvin 	clean_path(collected, 0);
427cd3acb6aSChristoph Hellwig 	init_symlink(collected + N_ALIGN(name_len), collected);
428b873498fSChristoph Hellwig 	init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW);
429889d51a1SNye Liu 	do_utime(collected, mtime);
4301da177e4SLinus Torvalds 	state = SkipIt;
4311da177e4SLinus Torvalds 	next_state = Reset;
4321da177e4SLinus Torvalds 	return 0;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds static __initdata int (*actions[])(void) = {
4361da177e4SLinus Torvalds 	[Start]		= do_start,
4371da177e4SLinus Torvalds 	[Collect]	= do_collect,
4381da177e4SLinus Torvalds 	[GotHeader]	= do_header,
4391da177e4SLinus Torvalds 	[SkipIt]	= do_skip,
4401da177e4SLinus Torvalds 	[GotName]	= do_name,
4411da177e4SLinus Torvalds 	[CopyFile]	= do_copy,
4421da177e4SLinus Torvalds 	[GotSymlink]	= do_symlink,
4431da177e4SLinus Torvalds 	[Reset]		= do_reset,
4441da177e4SLinus Torvalds };
4451da177e4SLinus Torvalds 
446d97b07c5SYinghai Lu static long __init write_buffer(char *buf, unsigned long len)
4471da177e4SLinus Torvalds {
448c34d85acSMark Rustad 	byte_count = len;
4491da177e4SLinus Torvalds 	victim = buf;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	while (!actions[state]())
4521da177e4SLinus Torvalds 		;
453c34d85acSMark Rustad 	return len - byte_count;
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
456d97b07c5SYinghai Lu static long __init flush_buffer(void *bufv, unsigned long len)
4571da177e4SLinus Torvalds {
4584197530bSXU pengfei 	char *buf = bufv;
459d97b07c5SYinghai Lu 	long written;
460d97b07c5SYinghai Lu 	long origLen = len;
4611da177e4SLinus Torvalds 	if (message)
46230d65dbfSAlain Knaff 		return -1;
4631da177e4SLinus Torvalds 	while ((written = write_buffer(buf, len)) < len && !message) {
4641da177e4SLinus Torvalds 		char c = buf[written];
4651da177e4SLinus Torvalds 		if (c == '0') {
4661da177e4SLinus Torvalds 			buf += written;
4671da177e4SLinus Torvalds 			len -= written;
4681da177e4SLinus Torvalds 			state = Start;
4691da177e4SLinus Torvalds 		} else if (c == 0) {
4701da177e4SLinus Torvalds 			buf += written;
4711da177e4SLinus Torvalds 			len -= written;
4721da177e4SLinus Torvalds 			state = Reset;
4731da177e4SLinus Torvalds 		} else
474e5eed351SDavid Engraf 			error("junk within compressed archive");
4751da177e4SLinus Torvalds 	}
47630d65dbfSAlain Knaff 	return origLen;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
479199cda13Swuchi static unsigned long my_inptr __initdata; /* index of next byte to be processed in inbuf */
4801da177e4SLinus Torvalds 
481889c92d2SH. Peter Anvin #include <linux/decompress/generic.h>
4821da177e4SLinus Torvalds 
483d97b07c5SYinghai Lu static char * __init unpack_to_rootfs(char *buf, unsigned long len)
4841da177e4SLinus Torvalds {
485d97b07c5SYinghai Lu 	long written;
486889c92d2SH. Peter Anvin 	decompress_fn decompress;
48723a22d57SH. Peter Anvin 	const char *compress_name;
48823a22d57SH. Peter Anvin 	static __initdata char msg_buf[64];
489889c92d2SH. Peter Anvin 
4903265e66bSThomas Petazzoni 	header_buf = kmalloc(110, GFP_KERNEL);
4913265e66bSThomas Petazzoni 	symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
4923265e66bSThomas Petazzoni 	name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
49330d65dbfSAlain Knaff 
49430d65dbfSAlain Knaff 	if (!header_buf || !symlink_buf || !name_buf)
495dd23e809SFlorian Fainelli 		panic_show_mem("can't allocate buffers");
49630d65dbfSAlain Knaff 
4971da177e4SLinus Torvalds 	state = Start;
4981da177e4SLinus Torvalds 	this_header = 0;
4991da177e4SLinus Torvalds 	message = NULL;
5001da177e4SLinus Torvalds 	while (!message && len) {
5011da177e4SLinus Torvalds 		loff_t saved_offset = this_header;
5021da177e4SLinus Torvalds 		if (*buf == '0' && !(this_header & 3)) {
5031da177e4SLinus Torvalds 			state = Start;
5041da177e4SLinus Torvalds 			written = write_buffer(buf, len);
5051da177e4SLinus Torvalds 			buf += written;
5061da177e4SLinus Torvalds 			len -= written;
5071da177e4SLinus Torvalds 			continue;
5081da177e4SLinus Torvalds 		}
5091da177e4SLinus Torvalds 		if (!*buf) {
5101da177e4SLinus Torvalds 			buf++;
5111da177e4SLinus Torvalds 			len--;
5121da177e4SLinus Torvalds 			this_header++;
5131da177e4SLinus Torvalds 			continue;
5141da177e4SLinus Torvalds 		}
5151da177e4SLinus Torvalds 		this_header = 0;
51623a22d57SH. Peter Anvin 		decompress = decompress_method(buf, len, &compress_name);
5176aa7a29aSDaniel M. Weeks 		pr_debug("Detected %s compressed data\n", compress_name);
51854291362SPhillip Lougher 		if (decompress) {
519d97b07c5SYinghai Lu 			int res = decompress(buf, len, NULL, flush_buffer, NULL,
520889c92d2SH. Peter Anvin 				   &my_inptr, error);
52154291362SPhillip Lougher 			if (res)
52254291362SPhillip Lougher 				error("decompressor failed");
52354291362SPhillip Lougher 		} else if (compress_name) {
52423a22d57SH. Peter Anvin 			if (!message) {
52523a22d57SH. Peter Anvin 				snprintf(msg_buf, sizeof msg_buf,
52623a22d57SH. Peter Anvin 					 "compression method %s not configured",
52723a22d57SH. Peter Anvin 					 compress_name);
52823a22d57SH. Peter Anvin 				message = msg_buf;
52923a22d57SH. Peter Anvin 			}
530df37bd15SPhillip Lougher 		} else
531e5eed351SDavid Engraf 			error("invalid magic at start of compressed archive");
5321da177e4SLinus Torvalds 		if (state != Reset)
533e5eed351SDavid Engraf 			error("junk at the end of compressed archive");
53430d65dbfSAlain Knaff 		this_header = saved_offset + my_inptr;
53530d65dbfSAlain Knaff 		buf += my_inptr;
53630d65dbfSAlain Knaff 		len -= my_inptr;
5371da177e4SLinus Torvalds 	}
538889d51a1SNye Liu 	dir_utime();
5393265e66bSThomas Petazzoni 	kfree(name_buf);
5403265e66bSThomas Petazzoni 	kfree(symlink_buf);
5413265e66bSThomas Petazzoni 	kfree(header_buf);
5421da177e4SLinus Torvalds 	return message;
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
5450a7b35cbSMichael Neuling static int __initdata do_retain_initrd;
5460a7b35cbSMichael Neuling 
5470a7b35cbSMichael Neuling static int __init retain_initrd_param(char *str)
5480a7b35cbSMichael Neuling {
5490a7b35cbSMichael Neuling 	if (*str)
5500a7b35cbSMichael Neuling 		return 0;
5510a7b35cbSMichael Neuling 	do_retain_initrd = 1;
5520a7b35cbSMichael Neuling 	return 1;
5530a7b35cbSMichael Neuling }
5540a7b35cbSMichael Neuling __setup("retain_initrd", retain_initrd_param);
5550a7b35cbSMichael Neuling 
556d8ae8a37SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_KEEPINITRD
557d8ae8a37SChristoph Hellwig static int __init keepinitrd_setup(char *__unused)
558d8ae8a37SChristoph Hellwig {
559d8ae8a37SChristoph Hellwig 	do_retain_initrd = 1;
560d8ae8a37SChristoph Hellwig 	return 1;
561d8ae8a37SChristoph Hellwig }
562d8ae8a37SChristoph Hellwig __setup("keepinitrd", keepinitrd_setup);
563d8ae8a37SChristoph Hellwig #endif
564d8ae8a37SChristoph Hellwig 
565e7cb072eSRasmus Villemoes static bool __initdata initramfs_async = true;
566e7cb072eSRasmus Villemoes static int __init initramfs_async_setup(char *str)
567e7cb072eSRasmus Villemoes {
568f3296f80SChristophe JAILLET 	return kstrtobool(str, &initramfs_async) == 0;
569e7cb072eSRasmus Villemoes }
570e7cb072eSRasmus Villemoes __setup("initramfs_async=", initramfs_async_setup);
571e7cb072eSRasmus Villemoes 
572ffe8018cSHendrik Brueckner extern char __initramfs_start[];
573ffe8018cSHendrik Brueckner extern unsigned long __initramfs_size;
5741da177e4SLinus Torvalds #include <linux/initrd.h>
5759c15e852SHaren Myneni #include <linux/kexec.h>
5760f3d2bd5SJan Beulich 
5772678fd2fSAlexander Graf static ssize_t raw_read(struct file *file, struct kobject *kobj,
5782678fd2fSAlexander Graf 			struct bin_attribute *attr, char *buf,
5792678fd2fSAlexander Graf 			loff_t pos, size_t count)
5802678fd2fSAlexander Graf {
5812678fd2fSAlexander Graf 	memcpy(buf, attr->private + pos, count);
5822678fd2fSAlexander Graf 	return count;
5832678fd2fSAlexander Graf }
5842678fd2fSAlexander Graf 
5852678fd2fSAlexander Graf static BIN_ATTR(initrd, 0440, raw_read, NULL, 0);
5862678fd2fSAlexander Graf 
587c72160feSKefeng Wang void __init reserve_initrd_mem(void)
588c72160feSKefeng Wang {
589c72160feSKefeng Wang 	phys_addr_t start;
590c72160feSKefeng Wang 	unsigned long size;
591c72160feSKefeng Wang 
592c72160feSKefeng Wang 	/* Ignore the virtul address computed during device tree parsing */
593c72160feSKefeng Wang 	initrd_start = initrd_end = 0;
594c72160feSKefeng Wang 
595c72160feSKefeng Wang 	if (!phys_initrd_size)
596c72160feSKefeng Wang 		return;
597c72160feSKefeng Wang 	/*
598c72160feSKefeng Wang 	 * Round the memory region to page boundaries as per free_initrd_mem()
599c72160feSKefeng Wang 	 * This allows us to detect whether the pages overlapping the initrd
600c72160feSKefeng Wang 	 * are in use, but more importantly, reserves the entire set of pages
601c72160feSKefeng Wang 	 * as we don't want these pages allocated for other purposes.
602c72160feSKefeng Wang 	 */
603c72160feSKefeng Wang 	start = round_down(phys_initrd_start, PAGE_SIZE);
604c72160feSKefeng Wang 	size = phys_initrd_size + (phys_initrd_start - start);
605c72160feSKefeng Wang 	size = round_up(size, PAGE_SIZE);
606c72160feSKefeng Wang 
607c72160feSKefeng Wang 	if (!memblock_is_region_memory(start, size)) {
608c72160feSKefeng Wang 		pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region",
609c72160feSKefeng Wang 		       (u64)start, size);
610c72160feSKefeng Wang 		goto disable;
611c72160feSKefeng Wang 	}
612c72160feSKefeng Wang 
613c72160feSKefeng Wang 	if (memblock_is_region_reserved(start, size)) {
614c72160feSKefeng Wang 		pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n",
615c72160feSKefeng Wang 		       (u64)start, size);
616c72160feSKefeng Wang 		goto disable;
617c72160feSKefeng Wang 	}
618c72160feSKefeng Wang 
619c72160feSKefeng Wang 	memblock_reserve(start, size);
620c72160feSKefeng Wang 	/* Now convert initrd to virtual addresses */
621c72160feSKefeng Wang 	initrd_start = (unsigned long)__va(phys_initrd_start);
622c72160feSKefeng Wang 	initrd_end = initrd_start + phys_initrd_size;
623c72160feSKefeng Wang 	initrd_below_start_ok = 1;
624c72160feSKefeng Wang 
625c72160feSKefeng Wang 	return;
626c72160feSKefeng Wang disable:
627c72160feSKefeng Wang 	pr_cont(" - disabling initrd\n");
628c72160feSKefeng Wang 	initrd_start = 0;
629c72160feSKefeng Wang 	initrd_end = 0;
630c72160feSKefeng Wang }
631c72160feSKefeng Wang 
63255d5b7ddSArnd Bergmann void __weak __init free_initrd_mem(unsigned long start, unsigned long end)
6334afd58e1SChristoph Hellwig {
634899ee4afSMike Rapoport #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
635899ee4afSMike Rapoport 	unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE);
636899ee4afSMike Rapoport 	unsigned long aligned_end = ALIGN(end, PAGE_SIZE);
637899ee4afSMike Rapoport 
6384421cca0SMike Rapoport 	memblock_free((void *)aligned_start, aligned_end - aligned_start);
639899ee4afSMike Rapoport #endif
640899ee4afSMike Rapoport 
641f94f7434SChristoph Hellwig 	free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
642f94f7434SChristoph Hellwig 			"initrd");
6434afd58e1SChristoph Hellwig }
6444afd58e1SChristoph Hellwig 
645*02aff848SBaoquan He #ifdef CONFIG_CRASH_RESERVE
646e99332e7SLinus Torvalds static bool __init kexec_free_initrd(void)
64723091e28SChristoph Hellwig {
6489c15e852SHaren Myneni 	unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
6499c15e852SHaren Myneni 	unsigned long crashk_end   = (unsigned long)__va(crashk_res.end);
6509c15e852SHaren Myneni 
6519c15e852SHaren Myneni 	/*
6529c15e852SHaren Myneni 	 * If the initrd region is overlapped with crashkernel reserved region,
6539c15e852SHaren Myneni 	 * free only memory that is not part of crashkernel region.
6549c15e852SHaren Myneni 	 */
65523091e28SChristoph Hellwig 	if (initrd_start >= crashk_end || initrd_end <= crashk_start)
65623091e28SChristoph Hellwig 		return false;
65723091e28SChristoph Hellwig 
6589c15e852SHaren Myneni 	/*
65923091e28SChristoph Hellwig 	 * Initialize initrd memory region since the kexec boot does not do.
6609c15e852SHaren Myneni 	 */
6619c15e852SHaren Myneni 	memset((void *)initrd_start, 0, initrd_end - initrd_start);
6629c15e852SHaren Myneni 	if (initrd_start < crashk_start)
6639c15e852SHaren Myneni 		free_initrd_mem(initrd_start, crashk_start);
6649c15e852SHaren Myneni 	if (initrd_end > crashk_end)
6659c15e852SHaren Myneni 		free_initrd_mem(crashk_end, initrd_end);
66623091e28SChristoph Hellwig 	return true;
6670f3d2bd5SJan Beulich }
66823091e28SChristoph Hellwig #else
66923091e28SChristoph Hellwig static inline bool kexec_free_initrd(void)
67023091e28SChristoph Hellwig {
67123091e28SChristoph Hellwig 	return false;
67223091e28SChristoph Hellwig }
67323091e28SChristoph Hellwig #endif /* CONFIG_KEXEC_CORE */
6740f3d2bd5SJan Beulich 
675a841c673SAndrew Morton #ifdef CONFIG_BLK_DEV_RAM
6764ada1e81SGeert Uytterhoeven static void __init populate_initrd_image(char *err)
6777c184ecdSChristoph Hellwig {
6787c184ecdSChristoph Hellwig 	ssize_t written;
679bf6419e4SChristoph Hellwig 	struct file *file;
680bf6419e4SChristoph Hellwig 	loff_t pos = 0;
6817c184ecdSChristoph Hellwig 
6827c184ecdSChristoph Hellwig 	unpack_to_rootfs(__initramfs_start, __initramfs_size);
6837c184ecdSChristoph Hellwig 
6847c184ecdSChristoph Hellwig 	printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n",
6857c184ecdSChristoph Hellwig 			err);
686bf6419e4SChristoph Hellwig 	file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
687bf6419e4SChristoph Hellwig 	if (IS_ERR(file))
6887c184ecdSChristoph Hellwig 		return;
6897c184ecdSChristoph Hellwig 
690bf6419e4SChristoph Hellwig 	written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start,
691bf6419e4SChristoph Hellwig 			&pos);
6927c184ecdSChristoph Hellwig 	if (written != initrd_end - initrd_start)
6937c184ecdSChristoph Hellwig 		pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
6947c184ecdSChristoph Hellwig 		       written, initrd_end - initrd_start);
695bf6419e4SChristoph Hellwig 	fput(file);
6967c184ecdSChristoph Hellwig }
6977c184ecdSChristoph Hellwig #endif /* CONFIG_BLK_DEV_RAM */
6987c184ecdSChristoph Hellwig 
699e7cb072eSRasmus Villemoes static void __init do_populate_rootfs(void *unused, async_cookie_t cookie)
7001da177e4SLinus Torvalds {
70117a9be31SStafford Horne 	/* Load the built in initramfs */
702ffe8018cSHendrik Brueckner 	char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
7031da177e4SLinus Torvalds 	if (err)
704dd23e809SFlorian Fainelli 		panic_show_mem("%s", err); /* Failed to decompress INTERNAL initramfs */
705afef7889SChristoph Hellwig 
706afef7889SChristoph Hellwig 	if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE))
707bb813f4cSTejun Heo 		goto done;
70854c7a891SChristoph Hellwig 
709afef7889SChristoph Hellwig 	if (IS_ENABLED(CONFIG_BLK_DEV_RAM))
710afef7889SChristoph Hellwig 		printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
711afef7889SChristoph Hellwig 	else
712afef7889SChristoph Hellwig 		printk(KERN_INFO "Unpacking initramfs...\n");
713afef7889SChristoph Hellwig 
714afef7889SChristoph Hellwig 	err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start);
715afef7889SChristoph Hellwig 	if (err) {
7169ab6b718SChristoph Hellwig #ifdef CONFIG_BLK_DEV_RAM
7177c184ecdSChristoph Hellwig 		populate_initrd_image(err);
7189ab6b718SChristoph Hellwig #else
7199ab6b718SChristoph Hellwig 		printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
7209ab6b718SChristoph Hellwig #endif
72117a9be31SStafford Horne 	}
72223091e28SChristoph Hellwig 
723afef7889SChristoph Hellwig done:
72423091e28SChristoph Hellwig 	/*
72523091e28SChristoph Hellwig 	 * If the initrd region is overlapped with crashkernel reserved region,
72623091e28SChristoph Hellwig 	 * free only memory that is not part of crashkernel region.
72723091e28SChristoph Hellwig 	 */
7282678fd2fSAlexander Graf 	if (!do_retain_initrd && initrd_start && !kexec_free_initrd()) {
72923091e28SChristoph Hellwig 		free_initrd_mem(initrd_start, initrd_end);
7302678fd2fSAlexander Graf 	} else if (do_retain_initrd && initrd_start) {
7312678fd2fSAlexander Graf 		bin_attr_initrd.size = initrd_end - initrd_start;
7322678fd2fSAlexander Graf 		bin_attr_initrd.private = (void *)initrd_start;
7332678fd2fSAlexander Graf 		if (sysfs_create_bin_file(firmware_kobj, &bin_attr_initrd))
7342678fd2fSAlexander Graf 			pr_err("Failed to create initrd sysfs file");
7352678fd2fSAlexander Graf 	}
73623091e28SChristoph Hellwig 	initrd_start = 0;
73723091e28SChristoph Hellwig 	initrd_end = 0;
73823091e28SChristoph Hellwig 
73908865514SLokesh Vutla 	flush_delayed_fput();
74068d85f0aSEric W. Biederman 	task_work_run();
741e7cb072eSRasmus Villemoes }
742e7cb072eSRasmus Villemoes 
743e7cb072eSRasmus Villemoes static ASYNC_DOMAIN_EXCLUSIVE(initramfs_domain);
744e7cb072eSRasmus Villemoes static async_cookie_t initramfs_cookie;
745e7cb072eSRasmus Villemoes 
746e7cb072eSRasmus Villemoes void wait_for_initramfs(void)
747e7cb072eSRasmus Villemoes {
748e7cb072eSRasmus Villemoes 	if (!initramfs_cookie) {
749e7cb072eSRasmus Villemoes 		/*
750e7cb072eSRasmus Villemoes 		 * Something before rootfs_initcall wants to access
751e7cb072eSRasmus Villemoes 		 * the filesystem/initramfs. Probably a bug. Make a
752e7cb072eSRasmus Villemoes 		 * note, avoid deadlocking the machine, and let the
753e7cb072eSRasmus Villemoes 		 * caller's access fail as it used to.
754e7cb072eSRasmus Villemoes 		 */
755e7cb072eSRasmus Villemoes 		pr_warn_once("wait_for_initramfs() called before rootfs_initcalls\n");
756e7cb072eSRasmus Villemoes 		return;
757e7cb072eSRasmus Villemoes 	}
758e7cb072eSRasmus Villemoes 	async_synchronize_cookie_domain(initramfs_cookie + 1, &initramfs_domain);
759e7cb072eSRasmus Villemoes }
760e7cb072eSRasmus Villemoes EXPORT_SYMBOL_GPL(wait_for_initramfs);
761e7cb072eSRasmus Villemoes 
762e7cb072eSRasmus Villemoes static int __init populate_rootfs(void)
763e7cb072eSRasmus Villemoes {
764e7cb072eSRasmus Villemoes 	initramfs_cookie = async_schedule_domain(do_populate_rootfs, NULL,
765e7cb072eSRasmus Villemoes 						 &initramfs_domain);
766b234ed6dSRasmus Villemoes 	usermodehelper_enable();
767e7cb072eSRasmus Villemoes 	if (!initramfs_async)
768e7cb072eSRasmus Villemoes 		wait_for_initramfs();
7698d610dd5SLinus Torvalds 	return 0;
7701da177e4SLinus Torvalds }
7718d610dd5SLinus Torvalds rootfs_initcall(populate_rootfs);
772