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