2 * $Id: nfsroot.c,v 1.45 1998/03/07 10:44:46 mj Exp $
4 * Copyright (C) 1995, 1996 Gero Kuhlmann <gero@gkminix.han.de>
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.
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
61 * Martin Mares : Use root_server_addr appropriately during setup.
62 * Martin Mares : Rewrote parameter parsing, now hopefully giving
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
69 * Fabian Frederick: Option parser rebuilt (using parser lib)
72 #include <linux/config.h>
73 #include <linux/types.h>
74 #include <linux/string.h>
75 #include <linux/kernel.h>
76 #include <linux/time.h>
78 #include <linux/init.h>
79 #include <linux/sunrpc/clnt.h>
80 #include <linux/nfs.h>
81 #include <linux/nfs_fs.h>
82 #include <linux/nfs_mount.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 #include <linux/vs_base.h>
91 #include <linux/vs_cvirt.h>
93 /* Define this to allow debugging output */
95 #define NFSDBG_FACILITY NFSDBG_ROOT
97 /* Default path we try to mount. "%s" gets replaced by our IP address */
98 #define NFS_ROOT "/tftpboot/%s"
100 /* Parameters passed from the kernel command line */
101 static char nfs_root_name[256] __initdata = "";
103 /* Address of NFS server */
104 static __u32 servaddr __initdata = 0;
106 /* Name of directory to mount */
107 static char nfs_path[NFS_MAXPATHLEN] __initdata = { 0, };
109 /* NFS-related data */
110 static struct nfs_mount_data nfs_data __initdata = { 0, };/* NFS mount info */
111 static int nfs_port __initdata = 0; /* Port to connect to for NFS */
112 static int mount_port __initdata = 0; /* Mount daemon port number */
115 /***************************************************************************
119 ***************************************************************************/
122 /* Options that take integer arguments */
123 Opt_port, Opt_rsize, Opt_wsize, Opt_timeo, Opt_retrans, Opt_acregmin,
124 Opt_acregmax, Opt_acdirmin, Opt_acdirmax,
125 /* Options that take no arguments */
126 Opt_soft, Opt_hard, Opt_intr,
127 Opt_nointr, Opt_posix, Opt_noposix, Opt_cto, Opt_nocto, Opt_ac,
128 Opt_noac, Opt_lock, Opt_nolock, Opt_v2, Opt_v3, Opt_udp, Opt_tcp,
129 Opt_acl, Opt_noacl, Opt_tagxid,
134 static match_table_t __initdata tokens = {
135 {Opt_port, "port=%u"},
136 {Opt_rsize, "rsize=%u"},
137 {Opt_wsize, "wsize=%u"},
138 {Opt_timeo, "timeo=%u"},
139 {Opt_retrans, "retrans=%u"},
140 {Opt_acregmin, "acregmin=%u"},
141 {Opt_acregmax, "acregmax=%u"},
142 {Opt_acdirmin, "acdirmin=%u"},
143 {Opt_acdirmax, "acdirmax=%u"},
147 {Opt_nointr, "nointr"},
148 {Opt_posix, "posix"},
149 {Opt_noposix, "noposix"},
151 {Opt_nocto, "nocto"},
155 {Opt_nolock, "nolock"},
156 {Opt_v2, "nfsvers=2"},
158 {Opt_v3, "nfsvers=3"},
160 {Opt_udp, "proto=udp"},
162 {Opt_tcp, "proto=tcp"},
165 {Opt_noacl, "noacl"},
166 {Opt_tagxid, "tagxid"},
172 * Parse option string.
175 static int __init root_nfs_parse(char *name, char *buf)
179 substring_t args[MAX_OPT_ARGS];
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);
190 while ((p = strsep (&name, ",")) != NULL) {
194 token = match_token(p, tokens, args);
196 /* %u tokens only. Beware if you add new tokens! */
197 if (token < Opt_soft && match_int(&args[0], &option))
204 nfs_data.rsize = option;
207 nfs_data.wsize = option;
210 nfs_data.timeo = option;
213 nfs_data.retrans = option;
216 nfs_data.acregmin = option;
219 nfs_data.acregmax = option;
222 nfs_data.acdirmin = option;
225 nfs_data.acdirmax = option;
228 nfs_data.flags |= NFS_MOUNT_SOFT;
231 nfs_data.flags &= ~NFS_MOUNT_SOFT;
234 nfs_data.flags |= NFS_MOUNT_INTR;
237 nfs_data.flags &= ~NFS_MOUNT_INTR;
240 nfs_data.flags |= NFS_MOUNT_POSIX;
243 nfs_data.flags &= ~NFS_MOUNT_POSIX;
246 nfs_data.flags &= ~NFS_MOUNT_NOCTO;
249 nfs_data.flags |= NFS_MOUNT_NOCTO;
252 nfs_data.flags &= ~NFS_MOUNT_NOAC;
255 nfs_data.flags |= NFS_MOUNT_NOAC;
258 nfs_data.flags &= ~NFS_MOUNT_NONLM;
261 nfs_data.flags |= NFS_MOUNT_NONLM;
264 nfs_data.flags &= ~NFS_MOUNT_VER3;
267 nfs_data.flags |= NFS_MOUNT_VER3;
270 nfs_data.flags &= ~NFS_MOUNT_TCP;
273 nfs_data.flags |= NFS_MOUNT_TCP;
276 nfs_data.flags &= ~NFS_MOUNT_NOACL;
279 nfs_data.flags |= NFS_MOUNT_NOACL;
281 #ifndef CONFIG_INOXID_NONE
283 nfs_data.flags |= NFS_MOUNT_TAGXID;
287 printk(KERN_WARNING "Root-NFS: unknown "
297 * Prepare the NFS data structure and parse all options.
299 static int __init root_nfs_name(char *name)
301 static char buf[NFS_MAXPATHLEN] __initdata;
304 /* Set some default values */
305 memset(&nfs_data, 0, sizeof(nfs_data));
307 nfs_data.version = NFS_MOUNT_VERSION;
308 nfs_data.flags = NFS_MOUNT_NONLM; /* No lockd in nfs root yet */
309 nfs_data.rsize = NFS_DEF_FILE_IO_SIZE;
310 nfs_data.wsize = NFS_DEF_FILE_IO_SIZE;
311 nfs_data.acregmin = 3;
312 nfs_data.acregmax = 60;
313 nfs_data.acdirmin = 30;
314 nfs_data.acdirmax = 60;
315 strcpy(buf, NFS_ROOT);
317 /* Process options received from the remote server */
318 root_nfs_parse(root_server_path, buf);
320 /* Override them by options set on kernel command-line */
321 root_nfs_parse(name, buf);
323 cp = vx_new_uts(nodename);
324 if (strlen(buf) + strlen(cp) > NFS_MAXPATHLEN) {
325 printk(KERN_ERR "Root-NFS: Pathname for remote directory too long.\n");
328 sprintf(nfs_path, buf, cp);
335 * Get NFS server address.
337 static int __init root_nfs_addr(void)
339 if ((servaddr = root_server_addr) == INADDR_NONE) {
340 printk(KERN_ERR "Root-NFS: No NFS server available, giving up.\n");
344 snprintf(nfs_data.hostname, sizeof(nfs_data.hostname),
345 "%u.%u.%u.%u", NIPQUAD(servaddr));
350 * Tell the user what's going on.
353 static void __init root_nfs_print(void)
355 printk(KERN_NOTICE "Root-NFS: Mounting %s on server %s as root\n",
356 nfs_path, nfs_data.hostname);
357 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);
368 static int __init root_nfs_init(void)
371 nfs_debug |= NFSDBG_ROOT;
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).
380 if (root_nfs_name(nfs_root_name) < 0 ||
393 * Parse NFS server and directory information passed on the kernel
396 static int __init nfs_root_setup(char *line)
399 if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) {
400 strlcpy(nfs_root_name, line, sizeof(nfs_root_name));
402 int n = strlen(line) + sizeof(NFS_ROOT) - 1;
403 if (n >= sizeof(nfs_root_name))
404 line[sizeof(nfs_root_name) - sizeof(NFS_ROOT) - 2] = '\0';
405 sprintf(nfs_root_name, NFS_ROOT, line);
407 root_server_addr = root_nfs_parse_addr(nfs_root_name);
411 __setup("nfsroot=", nfs_root_setup);
413 /***************************************************************************
415 Routines to actually mount the root directory
417 ***************************************************************************/
420 * Construct sockaddr_in from address and port number.
423 set_sockaddr(struct sockaddr_in *sin, __u32 addr, __u16 port)
425 sin->sin_family = AF_INET;
426 sin->sin_addr.s_addr = addr;
427 sin->sin_port = port;
431 * Query server portmapper for the port of a daemon program.
433 static int __init root_nfs_getport(int program, int version, int proto)
435 struct sockaddr_in sin;
437 printk(KERN_NOTICE "Looking up port of RPC %d/%d on %u.%u.%u.%u\n",
438 program, version, NIPQUAD(servaddr));
439 set_sockaddr(&sin, servaddr, 0);
440 return rpc_getport_external(&sin, program, version, proto);
445 * Use portmapper to find mountd and nfsd port numbers if not overriden
446 * by the user. Use defaults if portmapper is not available.
447 * XXX: Is there any nfs server with no portmapper?
449 static int __init root_nfs_ports(void)
452 int nfsd_ver, mountd_ver;
453 int nfsd_port, mountd_port;
456 if (nfs_data.flags & NFS_MOUNT_VER3) {
457 nfsd_ver = NFS3_VERSION;
458 mountd_ver = NFS_MNT3_VERSION;
459 nfsd_port = NFS_PORT;
460 mountd_port = NFS_MNT_PORT;
462 nfsd_ver = NFS2_VERSION;
463 mountd_ver = NFS_MNT_VERSION;
464 nfsd_port = NFS_PORT;
465 mountd_port = NFS_MNT_PORT;
468 proto = (nfs_data.flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
471 if ((port = root_nfs_getport(NFS_PROGRAM, nfsd_ver, proto)) < 0) {
472 printk(KERN_ERR "Root-NFS: Unable to get nfsd port "
473 "number from server, using default\n");
477 dprintk("Root-NFS: Portmapper on server returned %d "
478 "as nfsd port\n", port);
480 nfs_port = htons(nfs_port);
482 if ((port = root_nfs_getport(NFS_MNT_PROGRAM, mountd_ver, proto)) < 0) {
483 printk(KERN_ERR "Root-NFS: Unable to get mountd port "
484 "number from server, using default\n");
487 mount_port = htons(port);
488 dprintk("Root-NFS: mountd port is %d\n", port);
495 * Get a file handle from the server for the directory which is to be
498 static int __init root_nfs_get_handle(void)
501 struct sockaddr_in sin;
503 int protocol = (nfs_data.flags & NFS_MOUNT_TCP) ?
504 IPPROTO_TCP : IPPROTO_UDP;
505 int version = (nfs_data.flags & NFS_MOUNT_VER3) ?
506 NFS_MNT3_VERSION : NFS_MNT_VERSION;
508 set_sockaddr(&sin, servaddr, mount_port);
509 status = nfsroot_mount(&sin, nfs_path, &fh, version, protocol);
511 printk(KERN_ERR "Root-NFS: Server returned error %d "
512 "while mounting %s\n", status, nfs_path);
514 nfs_data.root.size = fh.size;
515 memcpy(nfs_data.root.data, fh.data, fh.size);
522 * Get the NFS port numbers and file handle, and return the prepared 'data'
523 * argument for mount() if everything went OK. Return NULL otherwise.
525 void * __init nfs_root_data(void)
527 if (root_nfs_init() < 0
528 || root_nfs_ports() < 0
529 || root_nfs_get_handle() < 0)
531 set_sockaddr((struct sockaddr_in *) &nfs_data.addr, servaddr, nfs_port);
532 return (void*)&nfs_data;