blob: 75f819dc025575dda5426aca05f6f128e20f3dba [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
46 * Swen Thümmler : Allow to specify the NFS options in nfs_root
47 * 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>
79#include <linux/nfs.h>
80#include <linux/nfs_fs.h>
81#include <linux/nfs_mount.h>
82#include <linux/in.h>
83#include <linux/major.h>
84#include <linux/utsname.h>
85#include <linux/inet.h>
86#include <linux/root_dev.h>
87#include <net/ipconfig.h>
88#include <linux/parser.h>
89
90/* Define this to allow debugging output */
91#undef NFSROOT_DEBUG
92#define NFSDBG_FACILITY NFSDBG_ROOT
93
94/* Default path we try to mount. "%s" gets replaced by our IP address */
95#define NFS_ROOT "/tftpboot/%s"
96
97/* Parameters passed from the kernel command line */
98static char nfs_root_name[256] __initdata = "";
99
100/* Address of NFS server */
Al Viro5a874db2006-11-08 00:19:38 -0800101static __be32 servaddr __initdata = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* Name of directory to mount */
104static char nfs_path[NFS_MAXPATHLEN] __initdata = { 0, };
105
106/* NFS-related data */
107static struct nfs_mount_data nfs_data __initdata = { 0, };/* NFS mount info */
108static int nfs_port __initdata = 0; /* Port to connect to for NFS */
109static int mount_port __initdata = 0; /* Mount daemon port number */
110
111
112/***************************************************************************
113
114 Parsing of options
115
116 ***************************************************************************/
117
118enum {
119 /* Options that take integer arguments */
120 Opt_port, Opt_rsize, Opt_wsize, Opt_timeo, Opt_retrans, Opt_acregmin,
121 Opt_acregmax, Opt_acdirmin, Opt_acdirmax,
122 /* Options that take no arguments */
123 Opt_soft, Opt_hard, Opt_intr,
124 Opt_nointr, Opt_posix, Opt_noposix, Opt_cto, Opt_nocto, Opt_ac,
125 Opt_noac, Opt_lock, Opt_nolock, Opt_v2, Opt_v3, Opt_udp, Opt_tcp,
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000126 Opt_acl, Opt_noacl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /* Error token */
128 Opt_err
129};
130
131static match_table_t __initdata tokens = {
132 {Opt_port, "port=%u"},
133 {Opt_rsize, "rsize=%u"},
134 {Opt_wsize, "wsize=%u"},
135 {Opt_timeo, "timeo=%u"},
136 {Opt_retrans, "retrans=%u"},
137 {Opt_acregmin, "acregmin=%u"},
138 {Opt_acregmax, "acregmax=%u"},
139 {Opt_acdirmin, "acdirmin=%u"},
140 {Opt_acdirmax, "acdirmax=%u"},
141 {Opt_soft, "soft"},
142 {Opt_hard, "hard"},
143 {Opt_intr, "intr"},
144 {Opt_nointr, "nointr"},
145 {Opt_posix, "posix"},
146 {Opt_noposix, "noposix"},
147 {Opt_cto, "cto"},
148 {Opt_nocto, "nocto"},
149 {Opt_ac, "ac"},
150 {Opt_noac, "noac"},
151 {Opt_lock, "lock"},
152 {Opt_nolock, "nolock"},
153 {Opt_v2, "nfsvers=2"},
154 {Opt_v2, "v2"},
155 {Opt_v3, "nfsvers=3"},
156 {Opt_v3, "v3"},
157 {Opt_udp, "proto=udp"},
158 {Opt_udp, "udp"},
159 {Opt_tcp, "proto=tcp"},
160 {Opt_tcp, "tcp"},
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000161 {Opt_acl, "acl"},
162 {Opt_noacl, "noacl"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 {Opt_err, NULL}
164
165};
166
167/*
168 * Parse option string.
169 */
170
171static int __init root_nfs_parse(char *name, char *buf)
172{
173
174 char *p;
175 substring_t args[MAX_OPT_ARGS];
176 int option;
177
178 if (!name)
179 return 1;
180
181 /* Set the NFS remote path */
182 p = strsep(&name, ",");
183 if (p[0] != '\0' && strcmp(p, "default") != 0)
184 strlcpy(buf, p, NFS_MAXPATHLEN);
185
186 while ((p = strsep (&name, ",")) != NULL) {
187 int token;
188 if (!*p)
189 continue;
190 token = match_token(p, tokens, args);
191
192 /* %u tokens only. Beware if you add new tokens! */
193 if (token < Opt_soft && match_int(&args[0], &option))
194 return 0;
195 switch (token) {
196 case Opt_port:
197 nfs_port = option;
198 break;
199 case Opt_rsize:
200 nfs_data.rsize = option;
201 break;
202 case Opt_wsize:
203 nfs_data.wsize = option;
204 break;
205 case Opt_timeo:
206 nfs_data.timeo = option;
207 break;
208 case Opt_retrans:
209 nfs_data.retrans = option;
210 break;
211 case Opt_acregmin:
212 nfs_data.acregmin = option;
213 break;
214 case Opt_acregmax:
215 nfs_data.acregmax = option;
216 break;
217 case Opt_acdirmin:
218 nfs_data.acdirmin = option;
219 break;
220 case Opt_acdirmax:
221 nfs_data.acdirmax = option;
222 break;
223 case Opt_soft:
224 nfs_data.flags |= NFS_MOUNT_SOFT;
225 break;
226 case Opt_hard:
227 nfs_data.flags &= ~NFS_MOUNT_SOFT;
228 break;
229 case Opt_intr:
230 nfs_data.flags |= NFS_MOUNT_INTR;
231 break;
232 case Opt_nointr:
233 nfs_data.flags &= ~NFS_MOUNT_INTR;
234 break;
235 case Opt_posix:
236 nfs_data.flags |= NFS_MOUNT_POSIX;
237 break;
238 case Opt_noposix:
239 nfs_data.flags &= ~NFS_MOUNT_POSIX;
240 break;
241 case Opt_cto:
242 nfs_data.flags &= ~NFS_MOUNT_NOCTO;
243 break;
244 case Opt_nocto:
245 nfs_data.flags |= NFS_MOUNT_NOCTO;
246 break;
247 case Opt_ac:
248 nfs_data.flags &= ~NFS_MOUNT_NOAC;
249 break;
250 case Opt_noac:
251 nfs_data.flags |= NFS_MOUNT_NOAC;
252 break;
253 case Opt_lock:
254 nfs_data.flags &= ~NFS_MOUNT_NONLM;
255 break;
256 case Opt_nolock:
257 nfs_data.flags |= NFS_MOUNT_NONLM;
258 break;
259 case Opt_v2:
260 nfs_data.flags &= ~NFS_MOUNT_VER3;
261 break;
262 case Opt_v3:
263 nfs_data.flags |= NFS_MOUNT_VER3;
264 break;
265 case Opt_udp:
266 nfs_data.flags &= ~NFS_MOUNT_TCP;
267 break;
268 case Opt_tcp:
269 nfs_data.flags |= NFS_MOUNT_TCP;
270 break;
Andreas Gruenbacherb7fa0552005-06-22 17:16:27 +0000271 case Opt_acl:
272 nfs_data.flags &= ~NFS_MOUNT_NOACL;
273 break;
274 case Opt_noacl:
275 nfs_data.flags |= NFS_MOUNT_NOACL;
276 break;
Jorn Dreyer21b6bf12006-01-08 01:03:30 -0800277 default:
278 printk(KERN_WARNING "Root-NFS: unknown "
279 "option: %s\n", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return 0;
281 }
282 }
283
284 return 1;
285}
286
287/*
288 * Prepare the NFS data structure and parse all options.
289 */
290static int __init root_nfs_name(char *name)
291{
292 static char buf[NFS_MAXPATHLEN] __initdata;
293 char *cp;
294
295 /* Set some default values */
296 memset(&nfs_data, 0, sizeof(nfs_data));
297 nfs_port = -1;
298 nfs_data.version = NFS_MOUNT_VERSION;
299 nfs_data.flags = NFS_MOUNT_NONLM; /* No lockd in nfs root yet */
Chuck Lever40859d72005-11-30 18:09:02 -0500300 nfs_data.rsize = NFS_DEF_FILE_IO_SIZE;
301 nfs_data.wsize = NFS_DEF_FILE_IO_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 nfs_data.acregmin = 3;
303 nfs_data.acregmax = 60;
304 nfs_data.acdirmin = 30;
305 nfs_data.acdirmax = 60;
306 strcpy(buf, NFS_ROOT);
307
308 /* Process options received from the remote server */
309 root_nfs_parse(root_server_path, buf);
310
311 /* Override them by options set on kernel command-line */
312 root_nfs_parse(name, buf);
313
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700314 cp = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (strlen(buf) + strlen(cp) > NFS_MAXPATHLEN) {
316 printk(KERN_ERR "Root-NFS: Pathname for remote directory too long.\n");
317 return -1;
318 }
319 sprintf(nfs_path, buf, cp);
320
321 return 1;
322}
323
324
325/*
326 * Get NFS server address.
327 */
328static int __init root_nfs_addr(void)
329{
Al Viro5a874db2006-11-08 00:19:38 -0800330 if ((servaddr = root_server_addr) == htonl(INADDR_NONE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 printk(KERN_ERR "Root-NFS: No NFS server available, giving up.\n");
332 return -1;
333 }
334
335 snprintf(nfs_data.hostname, sizeof(nfs_data.hostname),
336 "%u.%u.%u.%u", NIPQUAD(servaddr));
337 return 0;
338}
339
340/*
341 * Tell the user what's going on.
342 */
343#ifdef NFSROOT_DEBUG
344static void __init root_nfs_print(void)
345{
346 printk(KERN_NOTICE "Root-NFS: Mounting %s on server %s as root\n",
347 nfs_path, nfs_data.hostname);
348 printk(KERN_NOTICE "Root-NFS: rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
349 nfs_data.rsize, nfs_data.wsize, nfs_data.timeo, nfs_data.retrans);
350 printk(KERN_NOTICE "Root-NFS: acreg (min,max) = (%d,%d), acdir (min,max) = (%d,%d)\n",
351 nfs_data.acregmin, nfs_data.acregmax,
352 nfs_data.acdirmin, nfs_data.acdirmax);
353 printk(KERN_NOTICE "Root-NFS: nfsd port = %d, mountd port = %d, flags = %08x\n",
354 nfs_port, mount_port, nfs_data.flags);
355}
356#endif
357
358
359static int __init root_nfs_init(void)
360{
361#ifdef NFSROOT_DEBUG
362 nfs_debug |= NFSDBG_ROOT;
363#endif
364
365 /*
366 * Decode the root directory path name and NFS options from
367 * the kernel command line. This has to go here in order to
368 * be able to use the client IP address for the remote root
369 * directory (necessary for pure RARP booting).
370 */
371 if (root_nfs_name(nfs_root_name) < 0 ||
372 root_nfs_addr() < 0)
373 return -1;
374
375#ifdef NFSROOT_DEBUG
376 root_nfs_print();
377#endif
378
379 return 0;
380}
381
382
383/*
384 * Parse NFS server and directory information passed on the kernel
385 * command line.
386 */
387static int __init nfs_root_setup(char *line)
388{
389 ROOT_DEV = Root_NFS;
390 if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) {
391 strlcpy(nfs_root_name, line, sizeof(nfs_root_name));
392 } else {
393 int n = strlen(line) + sizeof(NFS_ROOT) - 1;
394 if (n >= sizeof(nfs_root_name))
395 line[sizeof(nfs_root_name) - sizeof(NFS_ROOT) - 2] = '\0';
396 sprintf(nfs_root_name, NFS_ROOT, line);
397 }
398 root_server_addr = root_nfs_parse_addr(nfs_root_name);
399 return 1;
400}
401
402__setup("nfsroot=", nfs_root_setup);
403
404/***************************************************************************
405
406 Routines to actually mount the root directory
407
408 ***************************************************************************/
409
410/*
411 * Construct sockaddr_in from address and port number.
412 */
413static inline void
Al Viro5a874db2006-11-08 00:19:38 -0800414set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 sin->sin_family = AF_INET;
417 sin->sin_addr.s_addr = addr;
418 sin->sin_port = port;
419}
420
421/*
422 * Query server portmapper for the port of a daemon program.
423 */
424static int __init root_nfs_getport(int program, int version, int proto)
425{
426 struct sockaddr_in sin;
427
428 printk(KERN_NOTICE "Looking up port of RPC %d/%d on %u.%u.%u.%u\n",
429 program, version, NIPQUAD(servaddr));
430 set_sockaddr(&sin, servaddr, 0);
431 return rpc_getport_external(&sin, program, version, proto);
432}
433
434
435/*
436 * Use portmapper to find mountd and nfsd port numbers if not overriden
437 * by the user. Use defaults if portmapper is not available.
438 * XXX: Is there any nfs server with no portmapper?
439 */
440static int __init root_nfs_ports(void)
441{
442 int port;
443 int nfsd_ver, mountd_ver;
444 int nfsd_port, mountd_port;
445 int proto;
446
447 if (nfs_data.flags & NFS_MOUNT_VER3) {
448 nfsd_ver = NFS3_VERSION;
449 mountd_ver = NFS_MNT3_VERSION;
450 nfsd_port = NFS_PORT;
451 mountd_port = NFS_MNT_PORT;
452 } else {
453 nfsd_ver = NFS2_VERSION;
454 mountd_ver = NFS_MNT_VERSION;
455 nfsd_port = NFS_PORT;
456 mountd_port = NFS_MNT_PORT;
457 }
458
459 proto = (nfs_data.flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
460
461 if (nfs_port < 0) {
462 if ((port = root_nfs_getport(NFS_PROGRAM, nfsd_ver, proto)) < 0) {
463 printk(KERN_ERR "Root-NFS: Unable to get nfsd port "
464 "number from server, using default\n");
465 port = nfsd_port;
466 }
Al Viro8854edd2006-01-04 01:44:17 -0500467 nfs_port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 dprintk("Root-NFS: Portmapper on server returned %d "
469 "as nfsd port\n", port);
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if ((port = root_nfs_getport(NFS_MNT_PROGRAM, mountd_ver, proto)) < 0) {
473 printk(KERN_ERR "Root-NFS: Unable to get mountd port "
474 "number from server, using default\n");
475 port = mountd_port;
476 }
Al Viro5a874db2006-11-08 00:19:38 -0800477 mount_port = port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 dprintk("Root-NFS: mountd port is %d\n", port);
479
480 return 0;
481}
482
483
484/*
485 * Get a file handle from the server for the directory which is to be
486 * mounted.
487 */
488static int __init root_nfs_get_handle(void)
489{
490 struct nfs_fh fh;
491 struct sockaddr_in sin;
492 int status;
493 int protocol = (nfs_data.flags & NFS_MOUNT_TCP) ?
494 IPPROTO_TCP : IPPROTO_UDP;
495 int version = (nfs_data.flags & NFS_MOUNT_VER3) ?
496 NFS_MNT3_VERSION : NFS_MNT_VERSION;
497
Al Viro5a874db2006-11-08 00:19:38 -0800498 set_sockaddr(&sin, servaddr, htons(mount_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 status = nfsroot_mount(&sin, nfs_path, &fh, version, protocol);
500 if (status < 0)
501 printk(KERN_ERR "Root-NFS: Server returned error %d "
502 "while mounting %s\n", status, nfs_path);
503 else {
504 nfs_data.root.size = fh.size;
505 memcpy(nfs_data.root.data, fh.data, fh.size);
506 }
507
508 return status;
509}
510
511/*
512 * Get the NFS port numbers and file handle, and return the prepared 'data'
513 * argument for mount() if everything went OK. Return NULL otherwise.
514 */
515void * __init nfs_root_data(void)
516{
517 if (root_nfs_init() < 0
518 || root_nfs_ports() < 0
519 || root_nfs_get_handle() < 0)
520 return NULL;
Al Viro5a874db2006-11-08 00:19:38 -0800521 set_sockaddr((struct sockaddr_in *) &nfs_data.addr, servaddr, htons(nfs_port));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return (void*)&nfs_data;
523}