blob: cb4a6bdca8712762e3a841fa48578f0c09d8ddf8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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.
Chuck Lever56463e52010-09-17 10:54:37 -04006 * (2) Construct the device string and the options string using DHCP
7 * option 17 and/or kernel command line options.
8 * (3) When mount_root() sets up the root file system, pass these strings
9 * to the NFS client's regular mount interface via sys_mount().
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 *
12 * Changes:
13 *
14 * Alan Cox : Removed get_address name clash with FPU.
15 * Alan Cox : Reformatted a bit.
16 * Gero Kuhlmann : Code cleanup
17 * Michael Rausch : Fixed recognition of an incoming RARP answer.
18 * Martin Mares : (2.0) Auto-configuration via BOOTP supported.
19 * Martin Mares : Manual selection of interface & BOOTP/RARP.
20 * Martin Mares : Using network routes instead of host routes,
21 * allowing the default configuration to be used
22 * for normal operation of the host.
23 * Martin Mares : Randomized timer with exponential backoff
24 * installed to minimize network congestion.
25 * Martin Mares : Code cleanup.
26 * Martin Mares : (2.1) BOOTP and RARP made configuration options.
27 * Martin Mares : Server hostname generation fixed.
28 * Gerd Knorr : Fixed wired inode handling
29 * Martin Mares : (2.2) "0.0.0.0" addresses from command line ignored.
30 * Martin Mares : RARP replies not tested for server address.
31 * Gero Kuhlmann : (2.3) Some bug fixes and code cleanup again (please
32 * send me your new patches _before_ bothering
33 * Linus so that I don' always have to cleanup
34 * _afterwards_ - thanks)
35 * Gero Kuhlmann : Last changes of Martin Mares undone.
36 * Gero Kuhlmann : RARP replies are tested for specified server
37 * again. However, it's now possible to have
38 * different RARP and NFS servers.
39 * Gero Kuhlmann : "0.0.0.0" addresses from command line are
40 * now mapped to INADDR_NONE.
41 * Gero Kuhlmann : Fixed a bug which prevented BOOTP path name
42 * from being used (thanks to Leo Spiekman)
43 * Andy Walker : Allow to specify the NFS server in nfs_root
44 * without giving a path name
Jan Engelhardt96de0e22007-10-19 23:21:04 +020045 * Swen Thümmler : Allow to specify the NFS options in nfs_root
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * without giving a path name. Fix BOOTP request
47 * for domainname (domainname is NIS domain, not
48 * DNS domain!). Skip dummy devices for BOOTP.
49 * Jacek Zapala : Fixed a bug which prevented server-ip address
50 * from nfsroot parameter from being used.
51 * Olaf Kirch : Adapted to new NFS code.
52 * Jakub Jelinek : Free used code segment.
53 * Marko Kohtala : Fixed some bugs.
54 * Martin Mares : Debug message cleanup
55 * Martin Mares : Changed to use the new generic IP layer autoconfig
56 * code. BOOTP and RARP moved there.
57 * Martin Mares : Default path now contains host name instead of
58 * host IP address (but host name defaults to IP
59 * address anyway).
60 * Martin Mares : Use root_server_addr appropriately during setup.
61 * Martin Mares : Rewrote parameter parsing, now hopefully giving
62 * correct overriding.
63 * Trond Myklebust : Add in preliminary support for NFSv3 and TCP.
64 * Fix bug in root_nfs_addr(). nfs_data.namlen
65 * is NOT for the length of the hostname.
66 * Hua Qin : Support for mounting root file system via
67 * NFS over TCP.
68 * Fabian Frederick: Option parser rebuilt (using parser lib)
Chuck Lever56463e52010-09-17 10:54:37 -040069 * Chuck Lever : Use super.c's text-based mount option parsing
70 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/types.h>
73#include <linux/string.h>
74#include <linux/kernel.h>
75#include <linux/time.h>
76#include <linux/fs.h>
77#include <linux/init.h>
78#include <linux/sunrpc/clnt.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040079#include <linux/sunrpc/xprtsock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/nfs.h>
81#include <linux/nfs_fs.h>
82#include <linux/nfs_mount.h>
83#include <linux/in.h>
84#include <linux/major.h>
85#include <linux/utsname.h>
86#include <linux/inet.h>
87#include <linux/root_dev.h>
88#include <net/ipconfig.h>
89#include <linux/parser.h>
90
Chuck Lever146ec942008-12-23 15:21:34 -050091#include "internal.h"
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* Define this to allow debugging output */
94#undef NFSROOT_DEBUG
95#define NFSDBG_FACILITY NFSDBG_ROOT
96
Chuck Lever2ad78092009-06-17 18:02:11 -070097/* Default port to use if server is not running a portmapper */
98#define NFS_MNT_PORT 627
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* Default path we try to mount. "%s" gets replaced by our IP address */
101#define NFS_ROOT "/tftpboot/%s"
102
103/* Parameters passed from the kernel command line */
Chuck Lever60ac0362010-09-17 10:54:37 -0400104static char nfs_root_parms[256] __initdata = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Chuck Lever56463e52010-09-17 10:54:37 -0400106/* Text-based mount options passed to super.c */
107static char nfs_root_options[256] __initdata = "";
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* Address of NFS server */
Al Viro5a874db2006-11-08 00:19:38 -0800110static __be32 servaddr __initdata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/* Name of directory to mount */
Chuck Lever56463e52010-09-17 10:54:37 -0400113static char nfs_export_path[NFS_MAXPATHLEN + 1] __initdata = "";
114
115/* server:export path string passed to super.c */
116static char nfs_root_device[NFS_MAXPATHLEN + 1] __initdata = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/* NFS-related data */
119static struct nfs_mount_data nfs_data __initdata = { 0, };/* NFS mount info */
120static int nfs_port __initdata = 0; /* Port to connect to for NFS */
121static int mount_port __initdata = 0; /* Mount daemon port number */
122
123
124/***************************************************************************
125
126 Parsing of options
127
128 ***************************************************************************/
129
130enum {
131 /* Options that take integer arguments */
132 Opt_port, Opt_rsize, Opt_wsize, Opt_timeo, Opt_retrans, Opt_acregmin,
133 Opt_acregmax, Opt_acdirmin, Opt_acdirmax,
134 /* Options that take no arguments */
135 Opt_soft, Opt_hard, Opt_intr,
136 Opt_nointr, Opt_posix, Opt_noposix, Opt_cto, Opt_nocto, Opt_ac,
137 Opt_noac, Opt_lock, Opt_nolock, Opt_v2, Opt_v3, Opt_udp, Opt_tcp,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000138 Opt_acl, Opt_noacl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* Error token */
140 Opt_err
141};
142
Sam Ravnborgd0367a52009-05-26 14:51:00 -0400143static const match_table_t tokens __initconst = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 {Opt_port, "port=%u"},
145 {Opt_rsize, "rsize=%u"},
146 {Opt_wsize, "wsize=%u"},
147 {Opt_timeo, "timeo=%u"},
148 {Opt_retrans, "retrans=%u"},
149 {Opt_acregmin, "acregmin=%u"},
150 {Opt_acregmax, "acregmax=%u"},
151 {Opt_acdirmin, "acdirmin=%u"},
152 {Opt_acdirmax, "acdirmax=%u"},
153 {Opt_soft, "soft"},
154 {Opt_hard, "hard"},
155 {Opt_intr, "intr"},
156 {Opt_nointr, "nointr"},
157 {Opt_posix, "posix"},
158 {Opt_noposix, "noposix"},
159 {Opt_cto, "cto"},
160 {Opt_nocto, "nocto"},
161 {Opt_ac, "ac"},
162 {Opt_noac, "noac"},
163 {Opt_lock, "lock"},
164 {Opt_nolock, "nolock"},
165 {Opt_v2, "nfsvers=2"},
166 {Opt_v2, "v2"},
167 {Opt_v3, "nfsvers=3"},
168 {Opt_v3, "v3"},
169 {Opt_udp, "proto=udp"},
170 {Opt_udp, "udp"},
171 {Opt_tcp, "proto=tcp"},
172 {Opt_tcp, "tcp"},
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000173 {Opt_acl, "acl"},
174 {Opt_noacl, "noacl"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 {Opt_err, NULL}
176
177};
178
179/*
180 * Parse option string.
181 */
182
183static int __init root_nfs_parse(char *name, char *buf)
184{
185
186 char *p;
187 substring_t args[MAX_OPT_ARGS];
188 int option;
189
190 if (!name)
191 return 1;
192
193 /* Set the NFS remote path */
194 p = strsep(&name, ",");
195 if (p[0] != '\0' && strcmp(p, "default") != 0)
196 strlcpy(buf, p, NFS_MAXPATHLEN);
197
198 while ((p = strsep (&name, ",")) != NULL) {
199 int token;
200 if (!*p)
201 continue;
202 token = match_token(p, tokens, args);
203
204 /* %u tokens only. Beware if you add new tokens! */
205 if (token < Opt_soft && match_int(&args[0], &option))
206 return 0;
207 switch (token) {
208 case Opt_port:
209 nfs_port = option;
210 break;
211 case Opt_rsize:
212 nfs_data.rsize = option;
213 break;
214 case Opt_wsize:
215 nfs_data.wsize = option;
216 break;
217 case Opt_timeo:
218 nfs_data.timeo = option;
219 break;
220 case Opt_retrans:
221 nfs_data.retrans = option;
222 break;
223 case Opt_acregmin:
224 nfs_data.acregmin = option;
225 break;
226 case Opt_acregmax:
227 nfs_data.acregmax = option;
228 break;
229 case Opt_acdirmin:
230 nfs_data.acdirmin = option;
231 break;
232 case Opt_acdirmax:
233 nfs_data.acdirmax = option;
234 break;
235 case Opt_soft:
236 nfs_data.flags |= NFS_MOUNT_SOFT;
237 break;
238 case Opt_hard:
239 nfs_data.flags &= ~NFS_MOUNT_SOFT;
240 break;
241 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 break;
244 case Opt_posix:
245 nfs_data.flags |= NFS_MOUNT_POSIX;
246 break;
247 case Opt_noposix:
248 nfs_data.flags &= ~NFS_MOUNT_POSIX;
249 break;
250 case Opt_cto:
251 nfs_data.flags &= ~NFS_MOUNT_NOCTO;
252 break;
253 case Opt_nocto:
254 nfs_data.flags |= NFS_MOUNT_NOCTO;
255 break;
256 case Opt_ac:
257 nfs_data.flags &= ~NFS_MOUNT_NOAC;
258 break;
259 case Opt_noac:
260 nfs_data.flags |= NFS_MOUNT_NOAC;
261 break;
262 case Opt_lock:
263 nfs_data.flags &= ~NFS_MOUNT_NONLM;
264 break;
265 case Opt_nolock:
266 nfs_data.flags |= NFS_MOUNT_NONLM;
267 break;
268 case Opt_v2:
269 nfs_data.flags &= ~NFS_MOUNT_VER3;
270 break;
271 case Opt_v3:
272 nfs_data.flags |= NFS_MOUNT_VER3;
273 break;
274 case Opt_udp:
275 nfs_data.flags &= ~NFS_MOUNT_TCP;
276 break;
277 case Opt_tcp:
278 nfs_data.flags |= NFS_MOUNT_TCP;
279 break;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000280 case Opt_acl:
281 nfs_data.flags &= ~NFS_MOUNT_NOACL;
282 break;
283 case Opt_noacl:
284 nfs_data.flags |= NFS_MOUNT_NOACL;
285 break;
Jorn Dreyer21b6bf12006-01-08 01:03:30 -0800286 default:
287 printk(KERN_WARNING "Root-NFS: unknown "
288 "option: %s\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return 0;
290 }
291 }
292
293 return 1;
294}
295
296/*
297 * Prepare the NFS data structure and parse all options.
298 */
299static int __init root_nfs_name(char *name)
300{
301 static char buf[NFS_MAXPATHLEN] __initdata;
302 char *cp;
303
304 /* Set some default values */
305 memset(&nfs_data, 0, sizeof(nfs_data));
306 nfs_port = -1;
307 nfs_data.version = NFS_MOUNT_VERSION;
308 nfs_data.flags = NFS_MOUNT_NONLM; /* No lockd in nfs root yet */
Chuck Lever40859d72005-11-30 18:09:02 -0500309 nfs_data.rsize = NFS_DEF_FILE_IO_SIZE;
310 nfs_data.wsize = NFS_DEF_FILE_IO_SIZE;
Chuck Lever0e0cab72008-06-26 17:47:12 -0400311 nfs_data.acregmin = NFS_DEF_ACREGMIN;
312 nfs_data.acregmax = NFS_DEF_ACREGMAX;
313 nfs_data.acdirmin = NFS_DEF_ACDIRMIN;
314 nfs_data.acdirmax = NFS_DEF_ACDIRMAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 strcpy(buf, NFS_ROOT);
316
317 /* Process options received from the remote server */
318 root_nfs_parse(root_server_path, buf);
319
320 /* Override them by options set on kernel command-line */
321 root_nfs_parse(name, buf);
322
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700323 cp = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (strlen(buf) + strlen(cp) > NFS_MAXPATHLEN) {
325 printk(KERN_ERR "Root-NFS: Pathname for remote directory too long.\n");
326 return -1;
327 }
Chuck Lever7b5d2b92008-12-23 15:21:34 -0500328 sprintf(nfs_export_path, buf, cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 return 1;
331}
332
333
334/*
335 * Get NFS server address.
336 */
337static int __init root_nfs_addr(void)
338{
Al Viro5a874db2006-11-08 00:19:38 -0800339 if ((servaddr = root_server_addr) == htonl(INADDR_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 printk(KERN_ERR "Root-NFS: No NFS server available, giving up.\n");
341 return -1;
342 }
343
344 snprintf(nfs_data.hostname, sizeof(nfs_data.hostname),
Harvey Harrisonbe859402008-10-31 00:56:28 -0700345 "%pI4", &servaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return 0;
347}
348
349/*
350 * Tell the user what's going on.
351 */
352#ifdef NFSROOT_DEBUG
353static void __init root_nfs_print(void)
354{
355 printk(KERN_NOTICE "Root-NFS: Mounting %s on server %s as root\n",
Chuck Lever7b5d2b92008-12-23 15:21:34 -0500356 nfs_export_path, nfs_data.hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 printk(KERN_NOTICE "Root-NFS: rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
358 nfs_data.rsize, nfs_data.wsize, nfs_data.timeo, nfs_data.retrans);
359 printk(KERN_NOTICE "Root-NFS: acreg (min,max) = (%d,%d), acdir (min,max) = (%d,%d)\n",
360 nfs_data.acregmin, nfs_data.acregmax,
361 nfs_data.acdirmin, nfs_data.acdirmax);
362 printk(KERN_NOTICE "Root-NFS: nfsd port = %d, mountd port = %d, flags = %08x\n",
363 nfs_port, mount_port, nfs_data.flags);
364}
365#endif
366
367
368static int __init root_nfs_init(void)
369{
370#ifdef NFSROOT_DEBUG
371 nfs_debug |= NFSDBG_ROOT;
372#endif
373
374 /*
375 * Decode the root directory path name and NFS options from
376 * the kernel command line. This has to go here in order to
377 * be able to use the client IP address for the remote root
378 * directory (necessary for pure RARP booting).
379 */
Chuck Lever60ac0362010-09-17 10:54:37 -0400380 if (root_nfs_name(nfs_root_parms) < 0 ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 root_nfs_addr() < 0)
382 return -1;
383
384#ifdef NFSROOT_DEBUG
385 root_nfs_print();
386#endif
387
388 return 0;
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
392 * Parse NFS server and directory information passed on the kernel
393 * command line.
Chuck Lever60ac0362010-09-17 10:54:37 -0400394 *
395 * nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]
396 *
397 * If there is a "%s" token in the <root-dir> string, it is replaced
398 * by the ASCII-representation of the client's IP address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
400static int __init nfs_root_setup(char *line)
401{
402 ROOT_DEV = Root_NFS;
Chuck Lever60ac0362010-09-17 10:54:37 -0400403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) {
Chuck Lever60ac0362010-09-17 10:54:37 -0400405 strlcpy(nfs_root_parms, line, sizeof(nfs_root_parms));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 } else {
Chuck Lever60ac0362010-09-17 10:54:37 -0400407 size_t n = strlen(line) + sizeof(NFS_ROOT) - 1;
408 if (n >= sizeof(nfs_root_parms))
409 line[sizeof(nfs_root_parms) - sizeof(NFS_ROOT) - 2] = '\0';
410 sprintf(nfs_root_parms, NFS_ROOT, line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Chuck Lever60ac0362010-09-17 10:54:37 -0400412
413 /*
414 * Extract the IP address of the NFS server containing our
415 * root file system, if one was specified.
416 *
417 * Note: root_nfs_parse_addr() removes the server-ip from
418 * nfs_root_parms, if it exists.
419 */
420 root_server_addr = root_nfs_parse_addr(nfs_root_parms);
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return 1;
423}
424
425__setup("nfsroot=", nfs_root_setup);
426
427/***************************************************************************
428
429 Routines to actually mount the root directory
430
431 ***************************************************************************/
432
433/*
434 * Construct sockaddr_in from address and port number.
435 */
436static inline void
Al Viro5a874db2006-11-08 00:19:38 -0800437set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 sin->sin_family = AF_INET;
440 sin->sin_addr.s_addr = addr;
441 sin->sin_port = port;
442}
443
444/*
445 * Query server portmapper for the port of a daemon program.
446 */
447static int __init root_nfs_getport(int program, int version, int proto)
448{
449 struct sockaddr_in sin;
450
Harvey Harrisonbe859402008-10-31 00:56:28 -0700451 printk(KERN_NOTICE "Looking up port of RPC %d/%d on %pI4\n",
452 program, version, &servaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 set_sockaddr(&sin, servaddr, 0);
Chuck Levercce63cd2007-07-01 12:13:12 -0400454 return rpcb_getport_sync(&sin, program, version, proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457
458/*
459 * Use portmapper to find mountd and nfsd port numbers if not overriden
460 * by the user. Use defaults if portmapper is not available.
461 * XXX: Is there any nfs server with no portmapper?
462 */
463static int __init root_nfs_ports(void)
464{
465 int port;
466 int nfsd_ver, mountd_ver;
467 int nfsd_port, mountd_port;
468 int proto;
469
470 if (nfs_data.flags & NFS_MOUNT_VER3) {
471 nfsd_ver = NFS3_VERSION;
472 mountd_ver = NFS_MNT3_VERSION;
473 nfsd_port = NFS_PORT;
474 mountd_port = NFS_MNT_PORT;
475 } else {
476 nfsd_ver = NFS2_VERSION;
477 mountd_ver = NFS_MNT_VERSION;
478 nfsd_port = NFS_PORT;
479 mountd_port = NFS_MNT_PORT;
480 }
481
482 proto = (nfs_data.flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
483
484 if (nfs_port < 0) {
485 if ((port = root_nfs_getport(NFS_PROGRAM, nfsd_ver, proto)) < 0) {
486 printk(KERN_ERR "Root-NFS: Unable to get nfsd port "
487 "number from server, using default\n");
488 port = nfsd_port;
489 }
Al Viro8854edd2006-01-04 01:44:17 -0500490 nfs_port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 dprintk("Root-NFS: Portmapper on server returned %d "
492 "as nfsd port\n", port);
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if ((port = root_nfs_getport(NFS_MNT_PROGRAM, mountd_ver, proto)) < 0) {
496 printk(KERN_ERR "Root-NFS: Unable to get mountd port "
497 "number from server, using default\n");
498 port = mountd_port;
499 }
Al Viro5a874db2006-11-08 00:19:38 -0800500 mount_port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 dprintk("Root-NFS: mountd port is %d\n", port);
502
503 return 0;
504}
505
506
507/*
508 * Get a file handle from the server for the directory which is to be
509 * mounted.
510 */
511static int __init root_nfs_get_handle(void)
512{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct sockaddr_in sin;
Chuck Lever8e02f6b2009-06-17 18:02:13 -0700514 unsigned int auth_flav_len = 0;
Chuck Leverc5d120f2008-12-23 15:21:35 -0500515 struct nfs_mount_request request = {
516 .sap = (struct sockaddr *)&sin,
517 .salen = sizeof(sin),
518 .dirpath = nfs_export_path,
519 .version = (nfs_data.flags & NFS_MOUNT_VER3) ?
520 NFS_MNT3_VERSION : NFS_MNT_VERSION,
521 .protocol = (nfs_data.flags & NFS_MOUNT_TCP) ?
522 XPRT_TRANSPORT_TCP : XPRT_TRANSPORT_UDP,
Chuck Lever8e02f6b2009-06-17 18:02:13 -0700523 .auth_flav_len = &auth_flav_len,
Chuck Leverc5d120f2008-12-23 15:21:35 -0500524 };
Trond Myklebust1b4c6062010-04-19 19:14:28 -0400525 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Trond Myklebust1b4c6062010-04-19 19:14:28 -0400527 request.fh = nfs_alloc_fhandle();
528 if (!request.fh)
529 goto out;
Al Viro5a874db2006-11-08 00:19:38 -0800530 set_sockaddr(&sin, servaddr, htons(mount_port));
Chuck Leverc5d120f2008-12-23 15:21:35 -0500531 status = nfs_mount(&request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (status < 0)
533 printk(KERN_ERR "Root-NFS: Server returned error %d "
Chuck Lever7b5d2b92008-12-23 15:21:34 -0500534 "while mounting %s\n", status, nfs_export_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 else {
Trond Myklebust1b4c6062010-04-19 19:14:28 -0400536 nfs_data.root.size = request.fh->size;
537 memcpy(&nfs_data.root.data, request.fh->data, request.fh->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Trond Myklebust1b4c6062010-04-19 19:14:28 -0400539 nfs_free_fhandle(request.fh);
540out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return status;
542}
543
544/*
545 * Get the NFS port numbers and file handle, and return the prepared 'data'
546 * argument for mount() if everything went OK. Return NULL otherwise.
547 */
Chuck Lever56463e52010-09-17 10:54:37 -0400548void * __init old_nfs_root_data(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 if (root_nfs_init() < 0
551 || root_nfs_ports() < 0
552 || root_nfs_get_handle() < 0)
553 return NULL;
Al Viro5a874db2006-11-08 00:19:38 -0800554 set_sockaddr((struct sockaddr_in *) &nfs_data.addr, servaddr, htons(nfs_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return (void*)&nfs_data;
556}
Chuck Lever56463e52010-09-17 10:54:37 -0400557
558static int __init root_nfs_copy(char *dest, const char *src,
559 const size_t destlen)
560{
561 if (strlcpy(dest, src, destlen) > destlen)
562 return -1;
563 return 0;
564}
565
566static int __init root_nfs_cat(char *dest, const char *src,
567 const size_t destlen)
568{
569 if (strlcat(dest, src, destlen) > destlen)
570 return -1;
571 return 0;
572}
573
574/*
575 * Parse out root export path and mount options from
576 * passed-in string @incoming.
577 *
578 * Copy the export path into @exppath.
579 */
580static int __init root_nfs_parse_options(char *incoming, char *exppath,
581 const size_t exppathlen)
582{
583 char *p;
584
585 /*
586 * Set the NFS remote path
587 */
588 p = strsep(&incoming, ",");
589 if (*p != '\0' && strcmp(p, "default") != 0)
590 if (root_nfs_copy(exppath, p, exppathlen))
591 return -1;
592
593 /*
594 * @incoming now points to the rest of the string; if it
595 * contains something, append it to our root options buffer
596 */
597 if (incoming != NULL && *incoming != '\0')
598 if (root_nfs_cat(nfs_root_options, incoming,
599 sizeof(nfs_root_options)))
600 return -1;
601
602 /*
603 * Possibly prepare for more options to be appended
604 */
605 if (nfs_root_options[0] != '\0' &&
606 nfs_root_options[strlen(nfs_root_options)] != ',')
607 if (root_nfs_cat(nfs_root_options, ",",
608 sizeof(nfs_root_options)))
609 return -1;
610
611 return 0;
612}
613
614/*
615 * Decode the export directory path name and NFS options from
616 * the kernel command line. This has to be done late in order to
617 * use a dynamically acquired client IP address for the remote
618 * root directory path.
619 *
620 * Returns zero if successful; otherwise -1 is returned.
621 */
622static int __init root_nfs_data(char *cmdline)
623{
624 char addr_option[sizeof("nolock,addr=") + INET_ADDRSTRLEN + 1];
625 int len, retval = -1;
626 char *tmp = NULL;
627 const size_t tmplen = sizeof(nfs_export_path);
628
629 tmp = kzalloc(tmplen, GFP_KERNEL);
630 if (tmp == NULL)
631 goto out_nomem;
632 strcpy(tmp, NFS_ROOT);
633
634 if (root_server_path[0] != '\0') {
635 dprintk("Root-NFS: DHCPv4 option 17: %s\n",
636 root_server_path);
637 if (root_nfs_parse_options(root_server_path, tmp, tmplen))
638 goto out_optionstoolong;
639 }
640
641 if (cmdline[0] != '\0') {
642 dprintk("Root-NFS: nfsroot=%s\n", cmdline);
643 if (root_nfs_parse_options(cmdline, tmp, tmplen))
644 goto out_optionstoolong;
645 }
646
647 /*
648 * Append mandatory options for nfsroot so they override
649 * what has come before
650 */
651 snprintf(addr_option, sizeof(addr_option), "nolock,addr=%pI4",
652 &servaddr);
653 if (root_nfs_cat(nfs_root_options, addr_option,
654 sizeof(nfs_root_options)))
655 goto out_optionstoolong;
656
657 /*
658 * Set up nfs_root_device. For NFS mounts, this looks like
659 *
660 * server:/path
661 *
662 * At this point, utsname()->nodename contains our local
663 * IP address or hostname, set by ipconfig. If "%s" exists
664 * in tmp, substitute the nodename, then shovel the whole
665 * mess into nfs_root_device.
666 */
667 len = snprintf(nfs_export_path, sizeof(nfs_export_path),
668 tmp, utsname()->nodename);
669 if (len > (int)sizeof(nfs_export_path))
670 goto out_devnametoolong;
671 len = snprintf(nfs_root_device, sizeof(nfs_root_device),
672 "%pI4:%s", &servaddr, nfs_export_path);
673 if (len > (int)sizeof(nfs_root_device))
674 goto out_devnametoolong;
675
676 retval = 0;
677
678out:
679 kfree(tmp);
680 return retval;
681out_nomem:
682 printk(KERN_ERR "Root-NFS: could not allocate memory\n");
683 goto out;
684out_optionstoolong:
685 printk(KERN_ERR "Root-NFS: mount options string too long\n");
686 goto out;
687out_devnametoolong:
688 printk(KERN_ERR "Root-NFS: root device name too long.\n");
689 goto out;
690}
691
692/**
693 * nfs_root_data - Return prepared 'data' for NFSROOT mount
694 * @root_device: OUT: address of string containing NFSROOT device
695 * @root_data: OUT: address of string containing NFSROOT mount options
696 *
697 * Returns zero and sets @root_device and @root_data if successful,
698 * otherwise -1 is returned.
699 */
700int __init nfs_root_data(char **root_device, char **root_data)
701{
702#ifdef NFSROOT_DEBUG
703 nfs_debug |= NFSDBG_ROOT | NFSDBG_MOUNT;
704#endif /* NFSROOT_DEBUG */
705
706 servaddr = root_server_addr;
707 if (servaddr == htonl(INADDR_NONE)) {
708 printk(KERN_ERR "Root-NFS: no NFS server address\n");
709 return -1;
710 }
711
712 if (root_nfs_data(nfs_root_parms) < 0)
713 return -1;
714
715 *root_device = nfs_root_device;
716 *root_data = nfs_root_options;
717 return 0;
718}