| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 |  *  Copyright (C) 1995, 1996  Gero Kuhlmann <gero@gkminix.han.de> | 
 | 3 |  * | 
 | 4 |  *  Allow an NFS filesystem to be mounted as root. The way this works is: | 
 | 5 |  *     (1) Use the IP autoconfig mechanism to set local IP addresses and routes. | 
 | 6 |  *     (2) Handle RPC negotiation with the system which replied to RARP or | 
 | 7 |  *         was reported as a boot server by BOOTP or manually. | 
 | 8 |  *     (3) The actual mounting is done later, when init() is running. | 
 | 9 |  * | 
 | 10 |  * | 
 | 11 |  *	Changes: | 
 | 12 |  * | 
 | 13 |  *	Alan Cox	:	Removed get_address name clash with FPU. | 
 | 14 |  *	Alan Cox	:	Reformatted a bit. | 
 | 15 |  *	Gero Kuhlmann	:	Code cleanup | 
 | 16 |  *	Michael Rausch  :	Fixed recognition of an incoming RARP answer. | 
 | 17 |  *	Martin Mares	: (2.0)	Auto-configuration via BOOTP supported. | 
 | 18 |  *	Martin Mares	:	Manual selection of interface & BOOTP/RARP. | 
 | 19 |  *	Martin Mares	:	Using network routes instead of host routes, | 
 | 20 |  *				allowing the default configuration to be used | 
 | 21 |  *				for normal operation of the host. | 
 | 22 |  *	Martin Mares	:	Randomized timer with exponential backoff | 
 | 23 |  *				installed to minimize network congestion. | 
 | 24 |  *	Martin Mares	:	Code cleanup. | 
 | 25 |  *	Martin Mares	: (2.1)	BOOTP and RARP made configuration options. | 
 | 26 |  *	Martin Mares	:	Server hostname generation fixed. | 
 | 27 |  *	Gerd Knorr	:	Fixed wired inode handling | 
 | 28 |  *	Martin Mares	: (2.2)	"0.0.0.0" addresses from command line ignored. | 
 | 29 |  *	Martin Mares	:	RARP replies not tested for server address. | 
 | 30 |  *	Gero Kuhlmann	: (2.3) Some bug fixes and code cleanup again (please | 
 | 31 |  *				send me your new patches _before_ bothering | 
 | 32 |  *				Linus so that I don' always have to cleanup | 
 | 33 |  *				_afterwards_ - thanks) | 
 | 34 |  *	Gero Kuhlmann	:	Last changes of Martin Mares undone. | 
 | 35 |  *	Gero Kuhlmann	: 	RARP replies are tested for specified server | 
 | 36 |  *				again. However, it's now possible to have | 
 | 37 |  *				different RARP and NFS servers. | 
 | 38 |  *	Gero Kuhlmann	:	"0.0.0.0" addresses from command line are | 
 | 39 |  *				now mapped to INADDR_NONE. | 
 | 40 |  *	Gero Kuhlmann	:	Fixed a bug which prevented BOOTP path name | 
 | 41 |  *				from being used (thanks to Leo Spiekman) | 
 | 42 |  *	Andy Walker	:	Allow to specify the NFS server in nfs_root | 
 | 43 |  *				without giving a path name | 
| Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 44 |  *	Swen Thümmler	:	Allow to specify the NFS options in nfs_root | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 |  *				without giving a path name. Fix BOOTP request | 
 | 46 |  *				for domainname (domainname is NIS domain, not | 
 | 47 |  *				DNS domain!). Skip dummy devices for BOOTP. | 
 | 48 |  *	Jacek Zapala	:	Fixed a bug which prevented server-ip address | 
 | 49 |  *				from nfsroot parameter from being used. | 
 | 50 |  *	Olaf Kirch	:	Adapted to new NFS code. | 
 | 51 |  *	Jakub Jelinek	:	Free used code segment. | 
 | 52 |  *	Marko Kohtala	:	Fixed some bugs. | 
 | 53 |  *	Martin Mares	:	Debug message cleanup | 
 | 54 |  *	Martin Mares	:	Changed to use the new generic IP layer autoconfig | 
 | 55 |  *				code. BOOTP and RARP moved there. | 
 | 56 |  *	Martin Mares	:	Default path now contains host name instead of | 
 | 57 |  *				host IP address (but host name defaults to IP | 
 | 58 |  *				address anyway). | 
 | 59 |  *	Martin Mares	:	Use root_server_addr appropriately during setup. | 
 | 60 |  *	Martin Mares	:	Rewrote parameter parsing, now hopefully giving | 
 | 61 |  *				correct overriding. | 
 | 62 |  *	Trond Myklebust :	Add in preliminary support for NFSv3 and TCP. | 
 | 63 |  *				Fix bug in root_nfs_addr(). nfs_data.namlen | 
 | 64 |  *				is NOT for the length of the hostname. | 
 | 65 |  *	Hua Qin		:	Support for mounting root file system via | 
 | 66 |  *				NFS over TCP. | 
 | 67 |  *	Fabian Frederick:	Option parser rebuilt (using parser lib) | 
 | 68 | */ | 
 | 69 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #include <linux/types.h> | 
 | 71 | #include <linux/string.h> | 
 | 72 | #include <linux/kernel.h> | 
 | 73 | #include <linux/time.h> | 
 | 74 | #include <linux/fs.h> | 
 | 75 | #include <linux/init.h> | 
 | 76 | #include <linux/sunrpc/clnt.h> | 
| \"Talpey, Thomas\ | 0896a72 | 2007-09-10 13:48:23 -0400 | [diff] [blame] | 77 | #include <linux/sunrpc/xprtsock.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #include <linux/nfs.h> | 
 | 79 | #include <linux/nfs_fs.h> | 
 | 80 | #include <linux/nfs_mount.h> | 
 | 81 | #include <linux/in.h> | 
 | 82 | #include <linux/major.h> | 
 | 83 | #include <linux/utsname.h> | 
 | 84 | #include <linux/inet.h> | 
 | 85 | #include <linux/root_dev.h> | 
 | 86 | #include <net/ipconfig.h> | 
 | 87 | #include <linux/parser.h> | 
 | 88 |  | 
| Chuck Lever | 146ec94 | 2008-12-23 15:21:34 -0500 | [diff] [blame] | 89 | #include "internal.h" | 
 | 90 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /* Define this to allow debugging output */ | 
 | 92 | #undef NFSROOT_DEBUG | 
 | 93 | #define NFSDBG_FACILITY NFSDBG_ROOT | 
 | 94 |  | 
| Chuck Lever | 2ad7809 | 2009-06-17 18:02:11 -0700 | [diff] [blame] | 95 | /* Default port to use if server is not running a portmapper */ | 
 | 96 | #define NFS_MNT_PORT	627 | 
 | 97 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | /* Default path we try to mount. "%s" gets replaced by our IP address */ | 
 | 99 | #define NFS_ROOT		"/tftpboot/%s" | 
 | 100 |  | 
 | 101 | /* Parameters passed from the kernel command line */ | 
 | 102 | static char nfs_root_name[256] __initdata = ""; | 
 | 103 |  | 
 | 104 | /* Address of NFS server */ | 
| Al Viro | 5a874db | 2006-11-08 00:19:38 -0800 | [diff] [blame] | 105 | static __be32 servaddr __initdata = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 |  | 
 | 107 | /* Name of directory to mount */ | 
| Chuck Lever | 7b5d2b9 | 2008-12-23 15:21:34 -0500 | [diff] [blame] | 108 | static char nfs_export_path[NFS_MAXPATHLEN] __initdata = { 0, }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 |  | 
 | 110 | /* NFS-related data */ | 
 | 111 | static struct nfs_mount_data nfs_data __initdata = { 0, };/* NFS mount info */ | 
 | 112 | static int nfs_port __initdata = 0;		/* Port to connect to for NFS */ | 
 | 113 | static int mount_port __initdata = 0;		/* Mount daemon port number */ | 
 | 114 |  | 
 | 115 |  | 
 | 116 | /*************************************************************************** | 
 | 117 |  | 
 | 118 | 			     Parsing of options | 
 | 119 |  | 
 | 120 |  ***************************************************************************/ | 
 | 121 |  | 
 | 122 | enum { | 
 | 123 | 	/* Options that take integer arguments */ | 
 | 124 | 	Opt_port, Opt_rsize, Opt_wsize, Opt_timeo, Opt_retrans, Opt_acregmin, | 
 | 125 | 	Opt_acregmax, Opt_acdirmin, Opt_acdirmax, | 
 | 126 | 	/* Options that take no arguments */ | 
 | 127 | 	Opt_soft, Opt_hard, Opt_intr, | 
 | 128 | 	Opt_nointr, Opt_posix, Opt_noposix, Opt_cto, Opt_nocto, Opt_ac,  | 
 | 129 | 	Opt_noac, Opt_lock, Opt_nolock, Opt_v2, Opt_v3, Opt_udp, Opt_tcp, | 
| Andreas Gruenbacher | b7fa055 | 2005-06-22 17:16:27 +0000 | [diff] [blame] | 130 | 	Opt_acl, Opt_noacl, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | 	/* Error token */ | 
 | 132 | 	Opt_err | 
 | 133 | }; | 
 | 134 |  | 
| Sam Ravnborg | d0367a5 | 2009-05-26 14:51:00 -0400 | [diff] [blame] | 135 | static const match_table_t tokens __initconst = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | 	{Opt_port, "port=%u"}, | 
 | 137 | 	{Opt_rsize, "rsize=%u"}, | 
 | 138 | 	{Opt_wsize, "wsize=%u"}, | 
 | 139 | 	{Opt_timeo, "timeo=%u"}, | 
 | 140 | 	{Opt_retrans, "retrans=%u"}, | 
 | 141 | 	{Opt_acregmin, "acregmin=%u"}, | 
 | 142 | 	{Opt_acregmax, "acregmax=%u"}, | 
 | 143 | 	{Opt_acdirmin, "acdirmin=%u"}, | 
 | 144 | 	{Opt_acdirmax, "acdirmax=%u"}, | 
 | 145 | 	{Opt_soft, "soft"}, | 
 | 146 | 	{Opt_hard, "hard"}, | 
 | 147 | 	{Opt_intr, "intr"}, | 
 | 148 | 	{Opt_nointr, "nointr"}, | 
 | 149 | 	{Opt_posix, "posix"}, | 
 | 150 | 	{Opt_noposix, "noposix"}, | 
 | 151 | 	{Opt_cto, "cto"}, | 
 | 152 | 	{Opt_nocto, "nocto"}, | 
 | 153 | 	{Opt_ac, "ac"}, | 
 | 154 | 	{Opt_noac, "noac"}, | 
 | 155 | 	{Opt_lock, "lock"}, | 
 | 156 | 	{Opt_nolock, "nolock"}, | 
 | 157 | 	{Opt_v2, "nfsvers=2"}, | 
 | 158 | 	{Opt_v2, "v2"}, | 
 | 159 | 	{Opt_v3, "nfsvers=3"}, | 
 | 160 | 	{Opt_v3, "v3"}, | 
 | 161 | 	{Opt_udp, "proto=udp"}, | 
 | 162 | 	{Opt_udp, "udp"}, | 
 | 163 | 	{Opt_tcp, "proto=tcp"}, | 
 | 164 | 	{Opt_tcp, "tcp"}, | 
| Andreas Gruenbacher | b7fa055 | 2005-06-22 17:16:27 +0000 | [diff] [blame] | 165 | 	{Opt_acl, "acl"}, | 
 | 166 | 	{Opt_noacl, "noacl"}, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | 	{Opt_err, NULL} | 
 | 168 | 	 | 
 | 169 | }; | 
 | 170 |  | 
 | 171 | /* | 
 | 172 |  *  Parse option string. | 
 | 173 |  */ | 
 | 174 |  | 
 | 175 | static int __init root_nfs_parse(char *name, char *buf) | 
 | 176 | { | 
 | 177 |  | 
 | 178 | 	char *p; | 
 | 179 | 	substring_t args[MAX_OPT_ARGS]; | 
 | 180 | 	int option; | 
 | 181 |  | 
 | 182 | 	if (!name) | 
 | 183 | 		return 1; | 
 | 184 |  | 
 | 185 | 	/* Set the NFS remote path */ | 
 | 186 | 	p = strsep(&name, ","); | 
 | 187 | 	if (p[0] != '\0' && strcmp(p, "default") != 0) | 
 | 188 | 		strlcpy(buf, p, NFS_MAXPATHLEN); | 
 | 189 |  | 
 | 190 | 	while ((p = strsep (&name, ",")) != NULL) { | 
 | 191 | 		int token;  | 
 | 192 | 		if (!*p) | 
 | 193 | 			continue; | 
 | 194 | 		token = match_token(p, tokens, args); | 
 | 195 |  | 
 | 196 | 		/* %u tokens only. Beware if you add new tokens! */ | 
 | 197 | 		if (token < Opt_soft && match_int(&args[0], &option)) | 
 | 198 | 			return 0; | 
 | 199 | 		switch (token) { | 
 | 200 | 			case Opt_port: | 
 | 201 | 				nfs_port = option; | 
 | 202 | 				break; | 
 | 203 | 			case Opt_rsize: | 
 | 204 | 				nfs_data.rsize = option; | 
 | 205 | 				break; | 
 | 206 | 			case Opt_wsize: | 
 | 207 | 				nfs_data.wsize = option; | 
 | 208 | 				break; | 
 | 209 | 			case Opt_timeo: | 
 | 210 | 				nfs_data.timeo = option; | 
 | 211 | 				break; | 
 | 212 | 			case Opt_retrans: | 
 | 213 | 				nfs_data.retrans = option; | 
 | 214 | 				break; | 
 | 215 | 			case Opt_acregmin: | 
 | 216 | 				nfs_data.acregmin = option; | 
 | 217 | 				break; | 
 | 218 | 			case Opt_acregmax: | 
 | 219 | 				nfs_data.acregmax = option; | 
 | 220 | 				break; | 
 | 221 | 			case Opt_acdirmin: | 
 | 222 | 				nfs_data.acdirmin = option; | 
 | 223 | 				break; | 
 | 224 | 			case Opt_acdirmax: | 
 | 225 | 				nfs_data.acdirmax = option; | 
 | 226 | 				break; | 
 | 227 | 			case Opt_soft: | 
 | 228 | 				nfs_data.flags |= NFS_MOUNT_SOFT; | 
 | 229 | 				break; | 
 | 230 | 			case Opt_hard: | 
 | 231 | 				nfs_data.flags &= ~NFS_MOUNT_SOFT; | 
 | 232 | 				break; | 
 | 233 | 			case Opt_intr: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | 			case Opt_nointr: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | 				break; | 
 | 236 | 			case Opt_posix: | 
 | 237 | 				nfs_data.flags |= NFS_MOUNT_POSIX; | 
 | 238 | 				break; | 
 | 239 | 			case Opt_noposix: | 
 | 240 | 				nfs_data.flags &= ~NFS_MOUNT_POSIX; | 
 | 241 | 				break; | 
 | 242 | 			case Opt_cto: | 
 | 243 | 				nfs_data.flags &= ~NFS_MOUNT_NOCTO; | 
 | 244 | 				break; | 
 | 245 | 			case Opt_nocto: | 
 | 246 | 				nfs_data.flags |= NFS_MOUNT_NOCTO; | 
 | 247 | 				break; | 
 | 248 | 			case Opt_ac: | 
 | 249 | 				nfs_data.flags &= ~NFS_MOUNT_NOAC; | 
 | 250 | 				break; | 
 | 251 | 			case Opt_noac: | 
 | 252 | 				nfs_data.flags |= NFS_MOUNT_NOAC; | 
 | 253 | 				break; | 
 | 254 | 			case Opt_lock: | 
 | 255 | 				nfs_data.flags &= ~NFS_MOUNT_NONLM; | 
 | 256 | 				break; | 
 | 257 | 			case Opt_nolock: | 
 | 258 | 				nfs_data.flags |= NFS_MOUNT_NONLM; | 
 | 259 | 				break; | 
 | 260 | 			case Opt_v2: | 
 | 261 | 				nfs_data.flags &= ~NFS_MOUNT_VER3; | 
 | 262 | 				break; | 
 | 263 | 			case Opt_v3: | 
 | 264 | 				nfs_data.flags |= NFS_MOUNT_VER3; | 
 | 265 | 				break; | 
 | 266 | 			case Opt_udp: | 
 | 267 | 				nfs_data.flags &= ~NFS_MOUNT_TCP; | 
 | 268 | 				break; | 
 | 269 | 			case Opt_tcp: | 
 | 270 | 				nfs_data.flags |= NFS_MOUNT_TCP; | 
 | 271 | 				break; | 
| Andreas Gruenbacher | b7fa055 | 2005-06-22 17:16:27 +0000 | [diff] [blame] | 272 | 			case Opt_acl: | 
 | 273 | 				nfs_data.flags &= ~NFS_MOUNT_NOACL; | 
 | 274 | 				break; | 
 | 275 | 			case Opt_noacl: | 
 | 276 | 				nfs_data.flags |= NFS_MOUNT_NOACL; | 
 | 277 | 				break; | 
| Jorn Dreyer | 21b6bf1 | 2006-01-08 01:03:30 -0800 | [diff] [blame] | 278 | 			default: | 
 | 279 | 				printk(KERN_WARNING "Root-NFS: unknown " | 
 | 280 | 					"option: %s\n", p); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | 				return 0; | 
 | 282 | 		} | 
 | 283 | 	} | 
 | 284 |  | 
 | 285 | 	return 1; | 
 | 286 | } | 
 | 287 |  | 
 | 288 | /* | 
 | 289 |  *  Prepare the NFS data structure and parse all options. | 
 | 290 |  */ | 
 | 291 | static int __init root_nfs_name(char *name) | 
 | 292 | { | 
 | 293 | 	static char buf[NFS_MAXPATHLEN] __initdata; | 
 | 294 | 	char *cp; | 
 | 295 |  | 
 | 296 | 	/* Set some default values */ | 
 | 297 | 	memset(&nfs_data, 0, sizeof(nfs_data)); | 
 | 298 | 	nfs_port          = -1; | 
 | 299 | 	nfs_data.version  = NFS_MOUNT_VERSION; | 
 | 300 | 	nfs_data.flags    = NFS_MOUNT_NONLM;	/* No lockd in nfs root yet */ | 
| Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 301 | 	nfs_data.rsize    = NFS_DEF_FILE_IO_SIZE; | 
 | 302 | 	nfs_data.wsize    = NFS_DEF_FILE_IO_SIZE; | 
| Chuck Lever | 0e0cab7 | 2008-06-26 17:47:12 -0400 | [diff] [blame] | 303 | 	nfs_data.acregmin = NFS_DEF_ACREGMIN; | 
 | 304 | 	nfs_data.acregmax = NFS_DEF_ACREGMAX; | 
 | 305 | 	nfs_data.acdirmin = NFS_DEF_ACDIRMIN; | 
 | 306 | 	nfs_data.acdirmax = NFS_DEF_ACDIRMAX; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | 	strcpy(buf, NFS_ROOT); | 
 | 308 |  | 
 | 309 | 	/* Process options received from the remote server */ | 
 | 310 | 	root_nfs_parse(root_server_path, buf); | 
 | 311 |  | 
 | 312 | 	/* Override them by options set on kernel command-line */ | 
 | 313 | 	root_nfs_parse(name, buf); | 
 | 314 |  | 
| Serge E. Hallyn | e9ff399 | 2006-10-02 02:18:11 -0700 | [diff] [blame] | 315 | 	cp = utsname()->nodename; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | 	if (strlen(buf) + strlen(cp) > NFS_MAXPATHLEN) { | 
 | 317 | 		printk(KERN_ERR "Root-NFS: Pathname for remote directory too long.\n"); | 
 | 318 | 		return -1; | 
 | 319 | 	} | 
| Chuck Lever | 7b5d2b9 | 2008-12-23 15:21:34 -0500 | [diff] [blame] | 320 | 	sprintf(nfs_export_path, buf, cp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 |  | 
 | 322 | 	return 1; | 
 | 323 | } | 
 | 324 |  | 
 | 325 |  | 
 | 326 | /* | 
 | 327 |  *  Get NFS server address. | 
 | 328 |  */ | 
 | 329 | static int __init root_nfs_addr(void) | 
 | 330 | { | 
| Al Viro | 5a874db | 2006-11-08 00:19:38 -0800 | [diff] [blame] | 331 | 	if ((servaddr = root_server_addr) == htonl(INADDR_NONE)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | 		printk(KERN_ERR "Root-NFS: No NFS server available, giving up.\n"); | 
 | 333 | 		return -1; | 
 | 334 | 	} | 
 | 335 |  | 
 | 336 | 	snprintf(nfs_data.hostname, sizeof(nfs_data.hostname), | 
| Harvey Harrison | be85940 | 2008-10-31 00:56:28 -0700 | [diff] [blame] | 337 | 		 "%pI4", &servaddr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | 	return 0; | 
 | 339 | } | 
 | 340 |  | 
 | 341 | /* | 
 | 342 |  *  Tell the user what's going on. | 
 | 343 |  */ | 
 | 344 | #ifdef NFSROOT_DEBUG | 
 | 345 | static void __init root_nfs_print(void) | 
 | 346 | { | 
 | 347 | 	printk(KERN_NOTICE "Root-NFS: Mounting %s on server %s as root\n", | 
| Chuck Lever | 7b5d2b9 | 2008-12-23 15:21:34 -0500 | [diff] [blame] | 348 | 		nfs_export_path, nfs_data.hostname); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | 	printk(KERN_NOTICE "Root-NFS:     rsize = %d, wsize = %d, timeo = %d, retrans = %d\n", | 
 | 350 | 		nfs_data.rsize, nfs_data.wsize, nfs_data.timeo, nfs_data.retrans); | 
 | 351 | 	printk(KERN_NOTICE "Root-NFS:     acreg (min,max) = (%d,%d), acdir (min,max) = (%d,%d)\n", | 
 | 352 | 		nfs_data.acregmin, nfs_data.acregmax, | 
 | 353 | 		nfs_data.acdirmin, nfs_data.acdirmax); | 
 | 354 | 	printk(KERN_NOTICE "Root-NFS:     nfsd port = %d, mountd port = %d, flags = %08x\n", | 
 | 355 | 		nfs_port, mount_port, nfs_data.flags); | 
 | 356 | } | 
 | 357 | #endif | 
 | 358 |  | 
 | 359 |  | 
 | 360 | static int __init root_nfs_init(void) | 
 | 361 | { | 
 | 362 | #ifdef NFSROOT_DEBUG | 
 | 363 | 	nfs_debug |= NFSDBG_ROOT; | 
 | 364 | #endif | 
 | 365 |  | 
 | 366 | 	/* | 
 | 367 | 	 * Decode the root directory path name and NFS options from | 
 | 368 | 	 * the kernel command line. This has to go here in order to | 
 | 369 | 	 * be able to use the client IP address for the remote root | 
 | 370 | 	 * directory (necessary for pure RARP booting). | 
 | 371 | 	 */ | 
 | 372 | 	if (root_nfs_name(nfs_root_name) < 0 || | 
 | 373 | 	    root_nfs_addr() < 0) | 
 | 374 | 		return -1; | 
 | 375 |  | 
 | 376 | #ifdef NFSROOT_DEBUG | 
 | 377 | 	root_nfs_print(); | 
 | 378 | #endif | 
 | 379 |  | 
 | 380 | 	return 0; | 
 | 381 | } | 
 | 382 |  | 
 | 383 |  | 
 | 384 | /* | 
 | 385 |  *  Parse NFS server and directory information passed on the kernel | 
 | 386 |  *  command line. | 
 | 387 |  */ | 
 | 388 | static int __init nfs_root_setup(char *line) | 
 | 389 | { | 
 | 390 | 	ROOT_DEV = Root_NFS; | 
 | 391 | 	if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) { | 
 | 392 | 		strlcpy(nfs_root_name, line, sizeof(nfs_root_name)); | 
 | 393 | 	} else { | 
 | 394 | 		int n = strlen(line) + sizeof(NFS_ROOT) - 1; | 
 | 395 | 		if (n >= sizeof(nfs_root_name)) | 
 | 396 | 			line[sizeof(nfs_root_name) - sizeof(NFS_ROOT) - 2] = '\0'; | 
 | 397 | 		sprintf(nfs_root_name, NFS_ROOT, line); | 
 | 398 | 	} | 
 | 399 | 	root_server_addr = root_nfs_parse_addr(nfs_root_name); | 
 | 400 | 	return 1; | 
 | 401 | } | 
 | 402 |  | 
 | 403 | __setup("nfsroot=", nfs_root_setup); | 
 | 404 |  | 
 | 405 | /*************************************************************************** | 
 | 406 |  | 
 | 407 | 	       Routines to actually mount the root directory | 
 | 408 |  | 
 | 409 |  ***************************************************************************/ | 
 | 410 |  | 
 | 411 | /* | 
 | 412 |  *  Construct sockaddr_in from address and port number. | 
 | 413 |  */ | 
 | 414 | static inline void | 
| Al Viro | 5a874db | 2006-11-08 00:19:38 -0800 | [diff] [blame] | 415 | set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { | 
 | 417 | 	sin->sin_family = AF_INET; | 
 | 418 | 	sin->sin_addr.s_addr = addr; | 
 | 419 | 	sin->sin_port = port; | 
 | 420 | } | 
 | 421 |  | 
 | 422 | /* | 
 | 423 |  *  Query server portmapper for the port of a daemon program. | 
 | 424 |  */ | 
 | 425 | static int __init root_nfs_getport(int program, int version, int proto) | 
 | 426 | { | 
 | 427 | 	struct sockaddr_in sin; | 
 | 428 |  | 
| Harvey Harrison | be85940 | 2008-10-31 00:56:28 -0700 | [diff] [blame] | 429 | 	printk(KERN_NOTICE "Looking up port of RPC %d/%d on %pI4\n", | 
 | 430 | 		program, version, &servaddr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | 	set_sockaddr(&sin, servaddr, 0); | 
| Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 432 | 	return rpcb_getport_sync(&sin, program, version, proto); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } | 
 | 434 |  | 
 | 435 |  | 
 | 436 | /* | 
 | 437 |  *  Use portmapper to find mountd and nfsd port numbers if not overriden | 
 | 438 |  *  by the user. Use defaults if portmapper is not available. | 
 | 439 |  *  XXX: Is there any nfs server with no portmapper? | 
 | 440 |  */ | 
 | 441 | static int __init root_nfs_ports(void) | 
 | 442 | { | 
 | 443 | 	int port; | 
 | 444 | 	int nfsd_ver, mountd_ver; | 
 | 445 | 	int nfsd_port, mountd_port; | 
 | 446 | 	int proto; | 
 | 447 |  | 
 | 448 | 	if (nfs_data.flags & NFS_MOUNT_VER3) { | 
 | 449 | 		nfsd_ver = NFS3_VERSION; | 
 | 450 | 		mountd_ver = NFS_MNT3_VERSION; | 
 | 451 | 		nfsd_port = NFS_PORT; | 
 | 452 | 		mountd_port = NFS_MNT_PORT; | 
 | 453 | 	} else { | 
 | 454 | 		nfsd_ver = NFS2_VERSION; | 
 | 455 | 		mountd_ver = NFS_MNT_VERSION; | 
 | 456 | 		nfsd_port = NFS_PORT; | 
 | 457 | 		mountd_port = NFS_MNT_PORT; | 
 | 458 | 	} | 
 | 459 |  | 
 | 460 | 	proto = (nfs_data.flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP; | 
 | 461 |  | 
 | 462 | 	if (nfs_port < 0) { | 
 | 463 | 		if ((port = root_nfs_getport(NFS_PROGRAM, nfsd_ver, proto)) < 0) { | 
 | 464 | 			printk(KERN_ERR "Root-NFS: Unable to get nfsd port " | 
 | 465 | 					"number from server, using default\n"); | 
 | 466 | 			port = nfsd_port; | 
 | 467 | 		} | 
| Al Viro | 8854edd | 2006-01-04 01:44:17 -0500 | [diff] [blame] | 468 | 		nfs_port = port; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | 		dprintk("Root-NFS: Portmapper on server returned %d " | 
 | 470 | 			"as nfsd port\n", port); | 
 | 471 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 |  | 
 | 473 | 	if ((port = root_nfs_getport(NFS_MNT_PROGRAM, mountd_ver, proto)) < 0) { | 
 | 474 | 		printk(KERN_ERR "Root-NFS: Unable to get mountd port " | 
 | 475 | 				"number from server, using default\n"); | 
 | 476 | 		port = mountd_port; | 
 | 477 | 	} | 
| Al Viro | 5a874db | 2006-11-08 00:19:38 -0800 | [diff] [blame] | 478 | 	mount_port = port; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | 	dprintk("Root-NFS: mountd port is %d\n", port); | 
 | 480 |  | 
 | 481 | 	return 0; | 
 | 482 | } | 
 | 483 |  | 
 | 484 |  | 
 | 485 | /* | 
 | 486 |  *  Get a file handle from the server for the directory which is to be | 
 | 487 |  *  mounted. | 
 | 488 |  */ | 
 | 489 | static int __init root_nfs_get_handle(void) | 
 | 490 | { | 
 | 491 | 	struct nfs_fh fh; | 
 | 492 | 	struct sockaddr_in sin; | 
| Chuck Lever | 8e02f6b | 2009-06-17 18:02:13 -0700 | [diff] [blame] | 493 | 	unsigned int auth_flav_len = 0; | 
| Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 494 | 	struct nfs_mount_request request = { | 
 | 495 | 		.sap		= (struct sockaddr *)&sin, | 
 | 496 | 		.salen		= sizeof(sin), | 
 | 497 | 		.dirpath	= nfs_export_path, | 
 | 498 | 		.version	= (nfs_data.flags & NFS_MOUNT_VER3) ? | 
 | 499 | 					NFS_MNT3_VERSION : NFS_MNT_VERSION, | 
 | 500 | 		.protocol	= (nfs_data.flags & NFS_MOUNT_TCP) ? | 
 | 501 | 					XPRT_TRANSPORT_TCP : XPRT_TRANSPORT_UDP, | 
 | 502 | 		.fh		= &fh, | 
| Chuck Lever | 8e02f6b | 2009-06-17 18:02:13 -0700 | [diff] [blame] | 503 | 		.auth_flav_len	= &auth_flav_len, | 
| Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 504 | 	}; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | 	int status; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 |  | 
| Al Viro | 5a874db | 2006-11-08 00:19:38 -0800 | [diff] [blame] | 507 | 	set_sockaddr(&sin, servaddr, htons(mount_port)); | 
| Chuck Lever | c5d120f | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 508 | 	status = nfs_mount(&request); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | 	if (status < 0) | 
 | 510 | 		printk(KERN_ERR "Root-NFS: Server returned error %d " | 
| Chuck Lever | 7b5d2b9 | 2008-12-23 15:21:34 -0500 | [diff] [blame] | 511 | 				"while mounting %s\n", status, nfs_export_path); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | 	else { | 
 | 513 | 		nfs_data.root.size = fh.size; | 
 | 514 | 		memcpy(nfs_data.root.data, fh.data, fh.size); | 
 | 515 | 	} | 
 | 516 |  | 
 | 517 | 	return status; | 
 | 518 | } | 
 | 519 |  | 
 | 520 | /* | 
 | 521 |  *  Get the NFS port numbers and file handle, and return the prepared 'data' | 
 | 522 |  *  argument for mount() if everything went OK. Return NULL otherwise. | 
 | 523 |  */ | 
 | 524 | void * __init nfs_root_data(void) | 
 | 525 | { | 
 | 526 | 	if (root_nfs_init() < 0 | 
 | 527 | 	 || root_nfs_ports() < 0 | 
 | 528 | 	 || root_nfs_get_handle() < 0) | 
 | 529 | 		return NULL; | 
| Al Viro | 5a874db | 2006-11-08 00:19:38 -0800 | [diff] [blame] | 530 | 	set_sockaddr((struct sockaddr_in *) &nfs_data.addr, servaddr, htons(nfs_port)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | 	return (void*)&nfs_data; | 
 | 532 | } |