2 * Copyright (C) 1995, 1996 Gero Kuhlmann <gero@gkminix.han.de>
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.
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
44 * Swen Thümmler : Allow to specify the NFS options in nfs_root
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
59 * Martin Mares : Use root_server_addr appropriately during setup.
60 * Martin Mares : Rewrote parameter parsing, now hopefully giving
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
67 * Fabian Frederick: Option parser rebuilt (using parser lib)
70 #include <linux/types.h>
71 #include <linux/string.h>
72 #include <linux/kernel.h>
73 #include <linux/time.h>
75 #include <linux/init.h>
76 #include <linux/sunrpc/clnt.h>
77 #include <linux/sunrpc/xprtsock.h>
78 #include <linux/nfs.h>
79 #include <linux/nfs_fs.h>
80 #include <linux/nfs_mount.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>
91 /* Define this to allow debugging output */
93 #define NFSDBG_FACILITY NFSDBG_ROOT
95 /* Default path we try to mount. "%s" gets replaced by our IP address */
96 #define NFS_ROOT "/tftpboot/%s"
98 /* Parameters passed from the kernel command line */
99 static char nfs_root_name
[256] __initdata
= "";
101 /* Address of NFS server */
102 static __be32 servaddr __initdata
= 0;
104 /* Name of directory to mount */
105 static char nfs_export_path
[NFS_MAXPATHLEN
] __initdata
= { 0, };
107 /* NFS-related data */
108 static struct nfs_mount_data nfs_data __initdata
= { 0, };/* NFS mount info */
109 static int nfs_port __initdata
= 0; /* Port to connect to for NFS */
110 static int mount_port __initdata
= 0; /* Mount daemon port number */
113 /***************************************************************************
117 ***************************************************************************/
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
,
132 static match_table_t __initconst 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"},
145 {Opt_nointr
, "nointr"},
146 {Opt_posix
, "posix"},
147 {Opt_noposix
, "noposix"},
149 {Opt_nocto
, "nocto"},
153 {Opt_nolock
, "nolock"},
154 {Opt_v2
, "nfsvers=2"},
156 {Opt_v3
, "nfsvers=3"},
158 {Opt_udp
, "proto=udp"},
160 {Opt_tcp
, "proto=tcp"},
163 {Opt_noacl
, "noacl"},
169 * Parse option string.
172 static int __init
root_nfs_parse(char *name
, char *buf
)
176 substring_t args
[MAX_OPT_ARGS
];
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
);
187 while ((p
= strsep (&name
, ",")) != NULL
) {
191 token
= match_token(p
, tokens
, args
);
193 /* %u tokens only. Beware if you add new tokens! */
194 if (token
< Opt_soft
&& match_int(&args
[0], &option
))
201 nfs_data
.rsize
= option
;
204 nfs_data
.wsize
= option
;
207 nfs_data
.timeo
= option
;
210 nfs_data
.retrans
= option
;
213 nfs_data
.acregmin
= option
;
216 nfs_data
.acregmax
= option
;
219 nfs_data
.acdirmin
= option
;
222 nfs_data
.acdirmax
= option
;
225 nfs_data
.flags
|= NFS_MOUNT_SOFT
;
228 nfs_data
.flags
&= ~NFS_MOUNT_SOFT
;
234 nfs_data
.flags
|= NFS_MOUNT_POSIX
;
237 nfs_data
.flags
&= ~NFS_MOUNT_POSIX
;
240 nfs_data
.flags
&= ~NFS_MOUNT_NOCTO
;
243 nfs_data
.flags
|= NFS_MOUNT_NOCTO
;
246 nfs_data
.flags
&= ~NFS_MOUNT_NOAC
;
249 nfs_data
.flags
|= NFS_MOUNT_NOAC
;
252 nfs_data
.flags
&= ~NFS_MOUNT_NONLM
;
255 nfs_data
.flags
|= NFS_MOUNT_NONLM
;
258 nfs_data
.flags
&= ~NFS_MOUNT_VER3
;
261 nfs_data
.flags
|= NFS_MOUNT_VER3
;
264 nfs_data
.flags
&= ~NFS_MOUNT_TCP
;
267 nfs_data
.flags
|= NFS_MOUNT_TCP
;
270 nfs_data
.flags
&= ~NFS_MOUNT_NOACL
;
273 nfs_data
.flags
|= NFS_MOUNT_NOACL
;
276 printk(KERN_WARNING
"Root-NFS: unknown "
286 * Prepare the NFS data structure and parse all options.
288 static int __init
root_nfs_name(char *name
)
290 static char buf
[NFS_MAXPATHLEN
] __initdata
;
293 /* Set some default values */
294 memset(&nfs_data
, 0, sizeof(nfs_data
));
296 nfs_data
.version
= NFS_MOUNT_VERSION
;
297 nfs_data
.flags
= NFS_MOUNT_NONLM
; /* No lockd in nfs root yet */
298 nfs_data
.rsize
= NFS_DEF_FILE_IO_SIZE
;
299 nfs_data
.wsize
= NFS_DEF_FILE_IO_SIZE
;
300 nfs_data
.acregmin
= NFS_DEF_ACREGMIN
;
301 nfs_data
.acregmax
= NFS_DEF_ACREGMAX
;
302 nfs_data
.acdirmin
= NFS_DEF_ACDIRMIN
;
303 nfs_data
.acdirmax
= NFS_DEF_ACDIRMAX
;
304 strcpy(buf
, NFS_ROOT
);
306 /* Process options received from the remote server */
307 root_nfs_parse(root_server_path
, buf
);
309 /* Override them by options set on kernel command-line */
310 root_nfs_parse(name
, buf
);
312 cp
= utsname()->nodename
;
313 if (strlen(buf
) + strlen(cp
) > NFS_MAXPATHLEN
) {
314 printk(KERN_ERR
"Root-NFS: Pathname for remote directory too long.\n");
317 sprintf(nfs_export_path
, buf
, cp
);
324 * Get NFS server address.
326 static int __init
root_nfs_addr(void)
328 if ((servaddr
= root_server_addr
) == htonl(INADDR_NONE
)) {
329 printk(KERN_ERR
"Root-NFS: No NFS server available, giving up.\n");
333 snprintf(nfs_data
.hostname
, sizeof(nfs_data
.hostname
),
339 * Tell the user what's going on.
342 static void __init
root_nfs_print(void)
344 printk(KERN_NOTICE
"Root-NFS: Mounting %s on server %s as root\n",
345 nfs_export_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
);
357 static int __init
root_nfs_init(void)
360 nfs_debug
|= NFSDBG_ROOT
;
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).
369 if (root_nfs_name(nfs_root_name
) < 0 ||
382 * Parse NFS server and directory information passed on the kernel
385 static int __init
nfs_root_setup(char *line
)
388 if (line
[0] == '/' || line
[0] == ',' || (line
[0] >= '0' && line
[0] <= '9')) {
389 strlcpy(nfs_root_name
, line
, sizeof(nfs_root_name
));
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
);
396 root_server_addr
= root_nfs_parse_addr(nfs_root_name
);
400 __setup("nfsroot=", nfs_root_setup
);
402 /***************************************************************************
404 Routines to actually mount the root directory
406 ***************************************************************************/
409 * Construct sockaddr_in from address and port number.
412 set_sockaddr(struct sockaddr_in
*sin
, __be32 addr
, __be16 port
)
414 sin
->sin_family
= AF_INET
;
415 sin
->sin_addr
.s_addr
= addr
;
416 sin
->sin_port
= port
;
420 * Query server portmapper for the port of a daemon program.
422 static int __init
root_nfs_getport(int program
, int version
, int proto
)
424 struct sockaddr_in sin
;
426 printk(KERN_NOTICE
"Looking up port of RPC %d/%d on %pI4\n",
427 program
, version
, &servaddr
);
428 set_sockaddr(&sin
, servaddr
, 0);
429 return rpcb_getport_sync(&sin
, program
, version
, proto
);
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?
438 static int __init
root_nfs_ports(void)
441 int nfsd_ver
, mountd_ver
;
442 int nfsd_port
, mountd_port
;
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
;
451 nfsd_ver
= NFS2_VERSION
;
452 mountd_ver
= NFS_MNT_VERSION
;
453 nfsd_port
= NFS_PORT
;
454 mountd_port
= NFS_MNT_PORT
;
457 proto
= (nfs_data
.flags
& NFS_MOUNT_TCP
) ? IPPROTO_TCP
: IPPROTO_UDP
;
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");
466 dprintk("Root-NFS: Portmapper on server returned %d "
467 "as nfsd port\n", port
);
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");
476 dprintk("Root-NFS: mountd port is %d\n", port
);
483 * Get a file handle from the server for the directory which is to be
486 static int __init
root_nfs_get_handle(void)
489 struct sockaddr_in sin
;
490 struct nfs_mount_request request
= {
491 .sap
= (struct sockaddr
*)&sin
,
492 .salen
= sizeof(sin
),
493 .dirpath
= nfs_export_path
,
494 .version
= (nfs_data
.flags
& NFS_MOUNT_VER3
) ?
495 NFS_MNT3_VERSION
: NFS_MNT_VERSION
,
496 .protocol
= (nfs_data
.flags
& NFS_MOUNT_TCP
) ?
497 XPRT_TRANSPORT_TCP
: XPRT_TRANSPORT_UDP
,
502 set_sockaddr(&sin
, servaddr
, htons(mount_port
));
503 status
= nfs_mount(&request
);
505 printk(KERN_ERR
"Root-NFS: Server returned error %d "
506 "while mounting %s\n", status
, nfs_export_path
);
508 nfs_data
.root
.size
= fh
.size
;
509 memcpy(nfs_data
.root
.data
, fh
.data
, fh
.size
);
516 * Get the NFS port numbers and file handle, and return the prepared 'data'
517 * argument for mount() if everything went OK. Return NULL otherwise.
519 void * __init
nfs_root_data(void)
521 if (root_nfs_init() < 0
522 || root_nfs_ports() < 0
523 || root_nfs_get_handle() < 0)
525 set_sockaddr((struct sockaddr_in
*) &nfs_data
.addr
, servaddr
, htons(nfs_port
));
526 return (void*)&nfs_data
;