xref: /OK3568_Linux_fs/kernel/fs/nfs/nfsroot.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *  Copyright (C) 1995, 1996  Gero Kuhlmann <gero@gkminix.han.de>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  *  Allow an NFS filesystem to be mounted as root. The way this works is:
6*4882a593Smuzhiyun  *     (1) Use the IP autoconfig mechanism to set local IP addresses and routes.
7*4882a593Smuzhiyun  *     (2) Construct the device string and the options string using DHCP
8*4882a593Smuzhiyun  *         option 17 and/or kernel command line options.
9*4882a593Smuzhiyun  *     (3) When mount_root() sets up the root file system, pass these strings
10*4882a593Smuzhiyun  *         to the NFS client's regular mount interface via sys_mount().
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  *	Changes:
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  *	Alan Cox	:	Removed get_address name clash with FPU.
16*4882a593Smuzhiyun  *	Alan Cox	:	Reformatted a bit.
17*4882a593Smuzhiyun  *	Gero Kuhlmann	:	Code cleanup
18*4882a593Smuzhiyun  *	Michael Rausch  :	Fixed recognition of an incoming RARP answer.
19*4882a593Smuzhiyun  *	Martin Mares	: (2.0)	Auto-configuration via BOOTP supported.
20*4882a593Smuzhiyun  *	Martin Mares	:	Manual selection of interface & BOOTP/RARP.
21*4882a593Smuzhiyun  *	Martin Mares	:	Using network routes instead of host routes,
22*4882a593Smuzhiyun  *				allowing the default configuration to be used
23*4882a593Smuzhiyun  *				for normal operation of the host.
24*4882a593Smuzhiyun  *	Martin Mares	:	Randomized timer with exponential backoff
25*4882a593Smuzhiyun  *				installed to minimize network congestion.
26*4882a593Smuzhiyun  *	Martin Mares	:	Code cleanup.
27*4882a593Smuzhiyun  *	Martin Mares	: (2.1)	BOOTP and RARP made configuration options.
28*4882a593Smuzhiyun  *	Martin Mares	:	Server hostname generation fixed.
29*4882a593Smuzhiyun  *	Gerd Knorr	:	Fixed wired inode handling
30*4882a593Smuzhiyun  *	Martin Mares	: (2.2)	"0.0.0.0" addresses from command line ignored.
31*4882a593Smuzhiyun  *	Martin Mares	:	RARP replies not tested for server address.
32*4882a593Smuzhiyun  *	Gero Kuhlmann	: (2.3) Some bug fixes and code cleanup again (please
33*4882a593Smuzhiyun  *				send me your new patches _before_ bothering
34*4882a593Smuzhiyun  *				Linus so that I don' always have to cleanup
35*4882a593Smuzhiyun  *				_afterwards_ - thanks)
36*4882a593Smuzhiyun  *	Gero Kuhlmann	:	Last changes of Martin Mares undone.
37*4882a593Smuzhiyun  *	Gero Kuhlmann	: 	RARP replies are tested for specified server
38*4882a593Smuzhiyun  *				again. However, it's now possible to have
39*4882a593Smuzhiyun  *				different RARP and NFS servers.
40*4882a593Smuzhiyun  *	Gero Kuhlmann	:	"0.0.0.0" addresses from command line are
41*4882a593Smuzhiyun  *				now mapped to INADDR_NONE.
42*4882a593Smuzhiyun  *	Gero Kuhlmann	:	Fixed a bug which prevented BOOTP path name
43*4882a593Smuzhiyun  *				from being used (thanks to Leo Spiekman)
44*4882a593Smuzhiyun  *	Andy Walker	:	Allow to specify the NFS server in nfs_root
45*4882a593Smuzhiyun  *				without giving a path name
46*4882a593Smuzhiyun  *	Swen Thümmler	:	Allow to specify the NFS options in nfs_root
47*4882a593Smuzhiyun  *				without giving a path name. Fix BOOTP request
48*4882a593Smuzhiyun  *				for domainname (domainname is NIS domain, not
49*4882a593Smuzhiyun  *				DNS domain!). Skip dummy devices for BOOTP.
50*4882a593Smuzhiyun  *	Jacek Zapala	:	Fixed a bug which prevented server-ip address
51*4882a593Smuzhiyun  *				from nfsroot parameter from being used.
52*4882a593Smuzhiyun  *	Olaf Kirch	:	Adapted to new NFS code.
53*4882a593Smuzhiyun  *	Jakub Jelinek	:	Free used code segment.
54*4882a593Smuzhiyun  *	Marko Kohtala	:	Fixed some bugs.
55*4882a593Smuzhiyun  *	Martin Mares	:	Debug message cleanup
56*4882a593Smuzhiyun  *	Martin Mares	:	Changed to use the new generic IP layer autoconfig
57*4882a593Smuzhiyun  *				code. BOOTP and RARP moved there.
58*4882a593Smuzhiyun  *	Martin Mares	:	Default path now contains host name instead of
59*4882a593Smuzhiyun  *				host IP address (but host name defaults to IP
60*4882a593Smuzhiyun  *				address anyway).
61*4882a593Smuzhiyun  *	Martin Mares	:	Use root_server_addr appropriately during setup.
62*4882a593Smuzhiyun  *	Martin Mares	:	Rewrote parameter parsing, now hopefully giving
63*4882a593Smuzhiyun  *				correct overriding.
64*4882a593Smuzhiyun  *	Trond Myklebust :	Add in preliminary support for NFSv3 and TCP.
65*4882a593Smuzhiyun  *				Fix bug in root_nfs_addr(). nfs_data.namlen
66*4882a593Smuzhiyun  *				is NOT for the length of the hostname.
67*4882a593Smuzhiyun  *	Hua Qin		:	Support for mounting root file system via
68*4882a593Smuzhiyun  *				NFS over TCP.
69*4882a593Smuzhiyun  *	Fabian Frederick:	Option parser rebuilt (using parser lib)
70*4882a593Smuzhiyun  *	Chuck Lever	:	Use super.c's text-based mount option parsing
71*4882a593Smuzhiyun  *	Chuck Lever	:	Add "nfsrootdebug".
72*4882a593Smuzhiyun  */
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun #include <linux/types.h>
75*4882a593Smuzhiyun #include <linux/string.h>
76*4882a593Smuzhiyun #include <linux/init.h>
77*4882a593Smuzhiyun #include <linux/nfs.h>
78*4882a593Smuzhiyun #include <linux/nfs_fs.h>
79*4882a593Smuzhiyun #include <linux/utsname.h>
80*4882a593Smuzhiyun #include <linux/root_dev.h>
81*4882a593Smuzhiyun #include <net/ipconfig.h>
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun #include "internal.h"
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #define NFSDBG_FACILITY NFSDBG_ROOT
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /* Default path we try to mount. "%s" gets replaced by our IP address */
88*4882a593Smuzhiyun #define NFS_ROOT		"/tftpboot/%s"
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /* Default NFSROOT mount options. */
91*4882a593Smuzhiyun #if defined(CONFIG_NFS_V2)
92*4882a593Smuzhiyun #define NFS_DEF_OPTIONS		"vers=2,tcp,rsize=4096,wsize=4096"
93*4882a593Smuzhiyun #elif defined(CONFIG_NFS_V3)
94*4882a593Smuzhiyun #define NFS_DEF_OPTIONS		"vers=3,tcp,rsize=4096,wsize=4096"
95*4882a593Smuzhiyun #else
96*4882a593Smuzhiyun #define NFS_DEF_OPTIONS		"vers=4,tcp,rsize=4096,wsize=4096"
97*4882a593Smuzhiyun #endif
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun /* Parameters passed from the kernel command line */
100*4882a593Smuzhiyun static char nfs_root_parms[NFS_MAXPATHLEN + 1] __initdata = "";
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun /* Text-based mount options passed to super.c */
103*4882a593Smuzhiyun static char nfs_root_options[256] __initdata = NFS_DEF_OPTIONS;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /* Address of NFS server */
106*4882a593Smuzhiyun static __be32 servaddr __initdata = htonl(INADDR_NONE);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /* Name of directory to mount */
109*4882a593Smuzhiyun static char nfs_export_path[NFS_MAXPATHLEN + 1] __initdata = "";
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /* server:export path string passed to super.c */
112*4882a593Smuzhiyun static char nfs_root_device[NFS_MAXPATHLEN + 1] __initdata = "";
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun #ifdef NFS_DEBUG
115*4882a593Smuzhiyun /*
116*4882a593Smuzhiyun  * When the "nfsrootdebug" kernel command line option is specified,
117*4882a593Smuzhiyun  * enable debugging messages for NFSROOT.
118*4882a593Smuzhiyun  */
nfs_root_debug(char * __unused)119*4882a593Smuzhiyun static int __init nfs_root_debug(char *__unused)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	nfs_debug |= NFSDBG_ROOT | NFSDBG_MOUNT;
122*4882a593Smuzhiyun 	return 1;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun __setup("nfsrootdebug", nfs_root_debug);
126*4882a593Smuzhiyun #endif
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun  *  Parse NFS server and directory information passed on the kernel
130*4882a593Smuzhiyun  *  command line.
131*4882a593Smuzhiyun  *
132*4882a593Smuzhiyun  *  nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]
133*4882a593Smuzhiyun  *
134*4882a593Smuzhiyun  *  If there is a "%s" token in the <root-dir> string, it is replaced
135*4882a593Smuzhiyun  *  by the ASCII-representation of the client's IP address.
136*4882a593Smuzhiyun  */
nfs_root_setup(char * line)137*4882a593Smuzhiyun static int __init nfs_root_setup(char *line)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	ROOT_DEV = Root_NFS;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) {
142*4882a593Smuzhiyun 		strlcpy(nfs_root_parms, line, sizeof(nfs_root_parms));
143*4882a593Smuzhiyun 	} else {
144*4882a593Smuzhiyun 		size_t n = strlen(line) + sizeof(NFS_ROOT) - 1;
145*4882a593Smuzhiyun 		if (n >= sizeof(nfs_root_parms))
146*4882a593Smuzhiyun 			line[sizeof(nfs_root_parms) - sizeof(NFS_ROOT) - 2] = '\0';
147*4882a593Smuzhiyun 		sprintf(nfs_root_parms, NFS_ROOT, line);
148*4882a593Smuzhiyun 	}
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	/*
151*4882a593Smuzhiyun 	 * Extract the IP address of the NFS server containing our
152*4882a593Smuzhiyun 	 * root file system, if one was specified.
153*4882a593Smuzhiyun 	 *
154*4882a593Smuzhiyun 	 * Note: root_nfs_parse_addr() removes the server-ip from
155*4882a593Smuzhiyun 	 *	 nfs_root_parms, if it exists.
156*4882a593Smuzhiyun 	 */
157*4882a593Smuzhiyun 	root_server_addr = root_nfs_parse_addr(nfs_root_parms);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	return 1;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun __setup("nfsroot=", nfs_root_setup);
163*4882a593Smuzhiyun 
root_nfs_copy(char * dest,const char * src,const size_t destlen)164*4882a593Smuzhiyun static int __init root_nfs_copy(char *dest, const char *src,
165*4882a593Smuzhiyun 				     const size_t destlen)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	if (strlcpy(dest, src, destlen) > destlen)
168*4882a593Smuzhiyun 		return -1;
169*4882a593Smuzhiyun 	return 0;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun 
root_nfs_cat(char * dest,const char * src,const size_t destlen)172*4882a593Smuzhiyun static int __init root_nfs_cat(char *dest, const char *src,
173*4882a593Smuzhiyun 			       const size_t destlen)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	size_t len = strlen(dest);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	if (len && dest[len - 1] != ',')
178*4882a593Smuzhiyun 		if (strlcat(dest, ",", destlen) > destlen)
179*4882a593Smuzhiyun 			return -1;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	if (strlcat(dest, src, destlen) > destlen)
182*4882a593Smuzhiyun 		return -1;
183*4882a593Smuzhiyun 	return 0;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun /*
187*4882a593Smuzhiyun  * Parse out root export path and mount options from
188*4882a593Smuzhiyun  * passed-in string @incoming.
189*4882a593Smuzhiyun  *
190*4882a593Smuzhiyun  * Copy the export path into @exppath.
191*4882a593Smuzhiyun  */
root_nfs_parse_options(char * incoming,char * exppath,const size_t exppathlen)192*4882a593Smuzhiyun static int __init root_nfs_parse_options(char *incoming, char *exppath,
193*4882a593Smuzhiyun 					 const size_t exppathlen)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	char *p;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	/*
198*4882a593Smuzhiyun 	 * Set the NFS remote path
199*4882a593Smuzhiyun 	 */
200*4882a593Smuzhiyun 	p = strsep(&incoming, ",");
201*4882a593Smuzhiyun 	if (*p != '\0' && strcmp(p, "default") != 0)
202*4882a593Smuzhiyun 		if (root_nfs_copy(exppath, p, exppathlen))
203*4882a593Smuzhiyun 			return -1;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/*
206*4882a593Smuzhiyun 	 * @incoming now points to the rest of the string; if it
207*4882a593Smuzhiyun 	 * contains something, append it to our root options buffer
208*4882a593Smuzhiyun 	 */
209*4882a593Smuzhiyun 	if (incoming != NULL && *incoming != '\0')
210*4882a593Smuzhiyun 		if (root_nfs_cat(nfs_root_options, incoming,
211*4882a593Smuzhiyun 						sizeof(nfs_root_options)))
212*4882a593Smuzhiyun 			return -1;
213*4882a593Smuzhiyun 	return 0;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun  *  Decode the export directory path name and NFS options from
218*4882a593Smuzhiyun  *  the kernel command line.  This has to be done late in order to
219*4882a593Smuzhiyun  *  use a dynamically acquired client IP address for the remote
220*4882a593Smuzhiyun  *  root directory path.
221*4882a593Smuzhiyun  *
222*4882a593Smuzhiyun  *  Returns zero if successful; otherwise -1 is returned.
223*4882a593Smuzhiyun  */
root_nfs_data(char * cmdline)224*4882a593Smuzhiyun static int __init root_nfs_data(char *cmdline)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	char mand_options[sizeof("nolock,addr=") + INET_ADDRSTRLEN + 1];
227*4882a593Smuzhiyun 	int len, retval = -1;
228*4882a593Smuzhiyun 	char *tmp = NULL;
229*4882a593Smuzhiyun 	const size_t tmplen = sizeof(nfs_export_path);
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	tmp = kzalloc(tmplen, GFP_KERNEL);
232*4882a593Smuzhiyun 	if (tmp == NULL)
233*4882a593Smuzhiyun 		goto out_nomem;
234*4882a593Smuzhiyun 	strcpy(tmp, NFS_ROOT);
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	if (root_server_path[0] != '\0') {
237*4882a593Smuzhiyun 		dprintk("Root-NFS: DHCPv4 option 17: %s\n",
238*4882a593Smuzhiyun 			root_server_path);
239*4882a593Smuzhiyun 		if (root_nfs_parse_options(root_server_path, tmp, tmplen))
240*4882a593Smuzhiyun 			goto out_optionstoolong;
241*4882a593Smuzhiyun 	}
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	if (cmdline[0] != '\0') {
244*4882a593Smuzhiyun 		dprintk("Root-NFS: nfsroot=%s\n", cmdline);
245*4882a593Smuzhiyun 		if (root_nfs_parse_options(cmdline, tmp, tmplen))
246*4882a593Smuzhiyun 			goto out_optionstoolong;
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	/*
250*4882a593Smuzhiyun 	 * Append mandatory options for nfsroot so they override
251*4882a593Smuzhiyun 	 * what has come before
252*4882a593Smuzhiyun 	 */
253*4882a593Smuzhiyun 	snprintf(mand_options, sizeof(mand_options), "nolock,addr=%pI4",
254*4882a593Smuzhiyun 			&servaddr);
255*4882a593Smuzhiyun 	if (root_nfs_cat(nfs_root_options, mand_options,
256*4882a593Smuzhiyun 						sizeof(nfs_root_options)))
257*4882a593Smuzhiyun 		goto out_optionstoolong;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	/*
260*4882a593Smuzhiyun 	 * Set up nfs_root_device.  For NFS mounts, this looks like
261*4882a593Smuzhiyun 	 *
262*4882a593Smuzhiyun 	 *	server:/path
263*4882a593Smuzhiyun 	 *
264*4882a593Smuzhiyun 	 * At this point, utsname()->nodename contains our local
265*4882a593Smuzhiyun 	 * IP address or hostname, set by ipconfig.  If "%s" exists
266*4882a593Smuzhiyun 	 * in tmp, substitute the nodename, then shovel the whole
267*4882a593Smuzhiyun 	 * mess into nfs_root_device.
268*4882a593Smuzhiyun 	 */
269*4882a593Smuzhiyun 	len = snprintf(nfs_export_path, sizeof(nfs_export_path),
270*4882a593Smuzhiyun 				tmp, utsname()->nodename);
271*4882a593Smuzhiyun 	if (len >= (int)sizeof(nfs_export_path))
272*4882a593Smuzhiyun 		goto out_devnametoolong;
273*4882a593Smuzhiyun 	len = snprintf(nfs_root_device, sizeof(nfs_root_device),
274*4882a593Smuzhiyun 				"%pI4:%s", &servaddr, nfs_export_path);
275*4882a593Smuzhiyun 	if (len >= (int)sizeof(nfs_root_device))
276*4882a593Smuzhiyun 		goto out_devnametoolong;
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	retval = 0;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun out:
281*4882a593Smuzhiyun 	kfree(tmp);
282*4882a593Smuzhiyun 	return retval;
283*4882a593Smuzhiyun out_nomem:
284*4882a593Smuzhiyun 	printk(KERN_ERR "Root-NFS: could not allocate memory\n");
285*4882a593Smuzhiyun 	goto out;
286*4882a593Smuzhiyun out_optionstoolong:
287*4882a593Smuzhiyun 	printk(KERN_ERR "Root-NFS: mount options string too long\n");
288*4882a593Smuzhiyun 	goto out;
289*4882a593Smuzhiyun out_devnametoolong:
290*4882a593Smuzhiyun 	printk(KERN_ERR "Root-NFS: root device name too long.\n");
291*4882a593Smuzhiyun 	goto out;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun /**
295*4882a593Smuzhiyun  * nfs_root_data - Return prepared 'data' for NFSROOT mount
296*4882a593Smuzhiyun  * @root_device: OUT: address of string containing NFSROOT device
297*4882a593Smuzhiyun  * @root_data: OUT: address of string containing NFSROOT mount options
298*4882a593Smuzhiyun  *
299*4882a593Smuzhiyun  * Returns zero and sets @root_device and @root_data if successful,
300*4882a593Smuzhiyun  * otherwise -1 is returned.
301*4882a593Smuzhiyun  */
nfs_root_data(char ** root_device,char ** root_data)302*4882a593Smuzhiyun int __init nfs_root_data(char **root_device, char **root_data)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun 	servaddr = root_server_addr;
305*4882a593Smuzhiyun 	if (servaddr == htonl(INADDR_NONE)) {
306*4882a593Smuzhiyun 		printk(KERN_ERR "Root-NFS: no NFS server address\n");
307*4882a593Smuzhiyun 		return -1;
308*4882a593Smuzhiyun 	}
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	if (root_nfs_data(nfs_root_parms) < 0)
311*4882a593Smuzhiyun 		return -1;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	*root_device = nfs_root_device;
314*4882a593Smuzhiyun 	*root_data = nfs_root_options;
315*4882a593Smuzhiyun 	return 0;
316*4882a593Smuzhiyun }
317