Merge branch 'linux-next' of git://git.infradead.org/~dedekind/ubi-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / nfsroot.c
blob8478fc25daee196383f3e40a34999795be41585e
1 /*
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.
11 * Changes:
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
58 * address anyway).
59 * Martin Mares : Use root_server_addr appropriately during setup.
60 * Martin Mares : Rewrote parameter parsing, now hopefully giving
61 * correct overriding.
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
66 * NFS over TCP.
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>
74 #include <linux/fs.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>
81 #include <linux/in.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>
89 /* Define this to allow debugging output */
90 #undef NFSROOT_DEBUG
91 #define NFSDBG_FACILITY NFSDBG_ROOT
93 /* Default path we try to mount. "%s" gets replaced by our IP address */
94 #define NFS_ROOT "/tftpboot/%s"
96 /* Parameters passed from the kernel command line */
97 static char nfs_root_name[256] __initdata = "";
99 /* Address of NFS server */
100 static __be32 servaddr __initdata = 0;
102 /* Name of directory to mount */
103 static char nfs_path[NFS_MAXPATHLEN] __initdata = { 0, };
105 /* NFS-related data */
106 static struct nfs_mount_data nfs_data __initdata = { 0, };/* NFS mount info */
107 static int nfs_port __initdata = 0; /* Port to connect to for NFS */
108 static int mount_port __initdata = 0; /* Mount daemon port number */
111 /***************************************************************************
113 Parsing of options
115 ***************************************************************************/
117 enum {
118 /* Options that take integer arguments */
119 Opt_port, Opt_rsize, Opt_wsize, Opt_timeo, Opt_retrans, Opt_acregmin,
120 Opt_acregmax, Opt_acdirmin, Opt_acdirmax,
121 /* Options that take no arguments */
122 Opt_soft, Opt_hard, Opt_intr,
123 Opt_nointr, Opt_posix, Opt_noposix, Opt_cto, Opt_nocto, Opt_ac,
124 Opt_noac, Opt_lock, Opt_nolock, Opt_v2, Opt_v3, Opt_udp, Opt_tcp,
125 Opt_acl, Opt_noacl,
126 /* Error token */
127 Opt_err
130 static match_table_t __initconst tokens = {
131 {Opt_port, "port=%u"},
132 {Opt_rsize, "rsize=%u"},
133 {Opt_wsize, "wsize=%u"},
134 {Opt_timeo, "timeo=%u"},
135 {Opt_retrans, "retrans=%u"},
136 {Opt_acregmin, "acregmin=%u"},
137 {Opt_acregmax, "acregmax=%u"},
138 {Opt_acdirmin, "acdirmin=%u"},
139 {Opt_acdirmax, "acdirmax=%u"},
140 {Opt_soft, "soft"},
141 {Opt_hard, "hard"},
142 {Opt_intr, "intr"},
143 {Opt_nointr, "nointr"},
144 {Opt_posix, "posix"},
145 {Opt_noposix, "noposix"},
146 {Opt_cto, "cto"},
147 {Opt_nocto, "nocto"},
148 {Opt_ac, "ac"},
149 {Opt_noac, "noac"},
150 {Opt_lock, "lock"},
151 {Opt_nolock, "nolock"},
152 {Opt_v2, "nfsvers=2"},
153 {Opt_v2, "v2"},
154 {Opt_v3, "nfsvers=3"},
155 {Opt_v3, "v3"},
156 {Opt_udp, "proto=udp"},
157 {Opt_udp, "udp"},
158 {Opt_tcp, "proto=tcp"},
159 {Opt_tcp, "tcp"},
160 {Opt_acl, "acl"},
161 {Opt_noacl, "noacl"},
162 {Opt_err, NULL}
167 * Parse option string.
170 static int __init root_nfs_parse(char *name, char *buf)
173 char *p;
174 substring_t args[MAX_OPT_ARGS];
175 int option;
177 if (!name)
178 return 1;
180 /* Set the NFS remote path */
181 p = strsep(&name, ",");
182 if (p[0] != '\0' && strcmp(p, "default") != 0)
183 strlcpy(buf, p, NFS_MAXPATHLEN);
185 while ((p = strsep (&name, ",")) != NULL) {
186 int token;
187 if (!*p)
188 continue;
189 token = match_token(p, tokens, args);
191 /* %u tokens only. Beware if you add new tokens! */
192 if (token < Opt_soft && match_int(&args[0], &option))
193 return 0;
194 switch (token) {
195 case Opt_port:
196 nfs_port = option;
197 break;
198 case Opt_rsize:
199 nfs_data.rsize = option;
200 break;
201 case Opt_wsize:
202 nfs_data.wsize = option;
203 break;
204 case Opt_timeo:
205 nfs_data.timeo = option;
206 break;
207 case Opt_retrans:
208 nfs_data.retrans = option;
209 break;
210 case Opt_acregmin:
211 nfs_data.acregmin = option;
212 break;
213 case Opt_acregmax:
214 nfs_data.acregmax = option;
215 break;
216 case Opt_acdirmin:
217 nfs_data.acdirmin = option;
218 break;
219 case Opt_acdirmax:
220 nfs_data.acdirmax = option;
221 break;
222 case Opt_soft:
223 nfs_data.flags |= NFS_MOUNT_SOFT;
224 break;
225 case Opt_hard:
226 nfs_data.flags &= ~NFS_MOUNT_SOFT;
227 break;
228 case Opt_intr:
229 case Opt_nointr:
230 break;
231 case Opt_posix:
232 nfs_data.flags |= NFS_MOUNT_POSIX;
233 break;
234 case Opt_noposix:
235 nfs_data.flags &= ~NFS_MOUNT_POSIX;
236 break;
237 case Opt_cto:
238 nfs_data.flags &= ~NFS_MOUNT_NOCTO;
239 break;
240 case Opt_nocto:
241 nfs_data.flags |= NFS_MOUNT_NOCTO;
242 break;
243 case Opt_ac:
244 nfs_data.flags &= ~NFS_MOUNT_NOAC;
245 break;
246 case Opt_noac:
247 nfs_data.flags |= NFS_MOUNT_NOAC;
248 break;
249 case Opt_lock:
250 nfs_data.flags &= ~NFS_MOUNT_NONLM;
251 break;
252 case Opt_nolock:
253 nfs_data.flags |= NFS_MOUNT_NONLM;
254 break;
255 case Opt_v2:
256 nfs_data.flags &= ~NFS_MOUNT_VER3;
257 break;
258 case Opt_v3:
259 nfs_data.flags |= NFS_MOUNT_VER3;
260 break;
261 case Opt_udp:
262 nfs_data.flags &= ~NFS_MOUNT_TCP;
263 break;
264 case Opt_tcp:
265 nfs_data.flags |= NFS_MOUNT_TCP;
266 break;
267 case Opt_acl:
268 nfs_data.flags &= ~NFS_MOUNT_NOACL;
269 break;
270 case Opt_noacl:
271 nfs_data.flags |= NFS_MOUNT_NOACL;
272 break;
273 default:
274 printk(KERN_WARNING "Root-NFS: unknown "
275 "option: %s\n", p);
276 return 0;
280 return 1;
284 * Prepare the NFS data structure and parse all options.
286 static int __init root_nfs_name(char *name)
288 static char buf[NFS_MAXPATHLEN] __initdata;
289 char *cp;
291 /* Set some default values */
292 memset(&nfs_data, 0, sizeof(nfs_data));
293 nfs_port = -1;
294 nfs_data.version = NFS_MOUNT_VERSION;
295 nfs_data.flags = NFS_MOUNT_NONLM; /* No lockd in nfs root yet */
296 nfs_data.rsize = NFS_DEF_FILE_IO_SIZE;
297 nfs_data.wsize = NFS_DEF_FILE_IO_SIZE;
298 nfs_data.acregmin = NFS_DEF_ACREGMIN;
299 nfs_data.acregmax = NFS_DEF_ACREGMAX;
300 nfs_data.acdirmin = NFS_DEF_ACDIRMIN;
301 nfs_data.acdirmax = NFS_DEF_ACDIRMAX;
302 strcpy(buf, NFS_ROOT);
304 /* Process options received from the remote server */
305 root_nfs_parse(root_server_path, buf);
307 /* Override them by options set on kernel command-line */
308 root_nfs_parse(name, buf);
310 cp = utsname()->nodename;
311 if (strlen(buf) + strlen(cp) > NFS_MAXPATHLEN) {
312 printk(KERN_ERR "Root-NFS: Pathname for remote directory too long.\n");
313 return -1;
315 sprintf(nfs_path, buf, cp);
317 return 1;
322 * Get NFS server address.
324 static int __init root_nfs_addr(void)
326 if ((servaddr = root_server_addr) == htonl(INADDR_NONE)) {
327 printk(KERN_ERR "Root-NFS: No NFS server available, giving up.\n");
328 return -1;
331 snprintf(nfs_data.hostname, sizeof(nfs_data.hostname),
332 "%u.%u.%u.%u", NIPQUAD(servaddr));
333 return 0;
337 * Tell the user what's going on.
339 #ifdef NFSROOT_DEBUG
340 static void __init root_nfs_print(void)
342 printk(KERN_NOTICE "Root-NFS: Mounting %s on server %s as root\n",
343 nfs_path, nfs_data.hostname);
344 printk(KERN_NOTICE "Root-NFS: rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
345 nfs_data.rsize, nfs_data.wsize, nfs_data.timeo, nfs_data.retrans);
346 printk(KERN_NOTICE "Root-NFS: acreg (min,max) = (%d,%d), acdir (min,max) = (%d,%d)\n",
347 nfs_data.acregmin, nfs_data.acregmax,
348 nfs_data.acdirmin, nfs_data.acdirmax);
349 printk(KERN_NOTICE "Root-NFS: nfsd port = %d, mountd port = %d, flags = %08x\n",
350 nfs_port, mount_port, nfs_data.flags);
352 #endif
355 static int __init root_nfs_init(void)
357 #ifdef NFSROOT_DEBUG
358 nfs_debug |= NFSDBG_ROOT;
359 #endif
362 * Decode the root directory path name and NFS options from
363 * the kernel command line. This has to go here in order to
364 * be able to use the client IP address for the remote root
365 * directory (necessary for pure RARP booting).
367 if (root_nfs_name(nfs_root_name) < 0 ||
368 root_nfs_addr() < 0)
369 return -1;
371 #ifdef NFSROOT_DEBUG
372 root_nfs_print();
373 #endif
375 return 0;
380 * Parse NFS server and directory information passed on the kernel
381 * command line.
383 static int __init nfs_root_setup(char *line)
385 ROOT_DEV = Root_NFS;
386 if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) {
387 strlcpy(nfs_root_name, line, sizeof(nfs_root_name));
388 } else {
389 int n = strlen(line) + sizeof(NFS_ROOT) - 1;
390 if (n >= sizeof(nfs_root_name))
391 line[sizeof(nfs_root_name) - sizeof(NFS_ROOT) - 2] = '\0';
392 sprintf(nfs_root_name, NFS_ROOT, line);
394 root_server_addr = root_nfs_parse_addr(nfs_root_name);
395 return 1;
398 __setup("nfsroot=", nfs_root_setup);
400 /***************************************************************************
402 Routines to actually mount the root directory
404 ***************************************************************************/
407 * Construct sockaddr_in from address and port number.
409 static inline void
410 set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
412 sin->sin_family = AF_INET;
413 sin->sin_addr.s_addr = addr;
414 sin->sin_port = port;
418 * Query server portmapper for the port of a daemon program.
420 static int __init root_nfs_getport(int program, int version, int proto)
422 struct sockaddr_in sin;
424 printk(KERN_NOTICE "Looking up port of RPC %d/%d on %u.%u.%u.%u\n",
425 program, version, NIPQUAD(servaddr));
426 set_sockaddr(&sin, servaddr, 0);
427 return rpcb_getport_sync(&sin, program, version, proto);
432 * Use portmapper to find mountd and nfsd port numbers if not overriden
433 * by the user. Use defaults if portmapper is not available.
434 * XXX: Is there any nfs server with no portmapper?
436 static int __init root_nfs_ports(void)
438 int port;
439 int nfsd_ver, mountd_ver;
440 int nfsd_port, mountd_port;
441 int proto;
443 if (nfs_data.flags & NFS_MOUNT_VER3) {
444 nfsd_ver = NFS3_VERSION;
445 mountd_ver = NFS_MNT3_VERSION;
446 nfsd_port = NFS_PORT;
447 mountd_port = NFS_MNT_PORT;
448 } else {
449 nfsd_ver = NFS2_VERSION;
450 mountd_ver = NFS_MNT_VERSION;
451 nfsd_port = NFS_PORT;
452 mountd_port = NFS_MNT_PORT;
455 proto = (nfs_data.flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
457 if (nfs_port < 0) {
458 if ((port = root_nfs_getport(NFS_PROGRAM, nfsd_ver, proto)) < 0) {
459 printk(KERN_ERR "Root-NFS: Unable to get nfsd port "
460 "number from server, using default\n");
461 port = nfsd_port;
463 nfs_port = port;
464 dprintk("Root-NFS: Portmapper on server returned %d "
465 "as nfsd port\n", port);
468 if ((port = root_nfs_getport(NFS_MNT_PROGRAM, mountd_ver, proto)) < 0) {
469 printk(KERN_ERR "Root-NFS: Unable to get mountd port "
470 "number from server, using default\n");
471 port = mountd_port;
473 mount_port = port;
474 dprintk("Root-NFS: mountd port is %d\n", port);
476 return 0;
481 * Get a file handle from the server for the directory which is to be
482 * mounted.
484 static int __init root_nfs_get_handle(void)
486 struct nfs_fh fh;
487 struct sockaddr_in sin;
488 int status;
489 int protocol = (nfs_data.flags & NFS_MOUNT_TCP) ?
490 XPRT_TRANSPORT_TCP : XPRT_TRANSPORT_UDP;
491 int version = (nfs_data.flags & NFS_MOUNT_VER3) ?
492 NFS_MNT3_VERSION : NFS_MNT_VERSION;
494 set_sockaddr(&sin, servaddr, htons(mount_port));
495 status = nfs_mount((struct sockaddr *) &sin, sizeof(sin), NULL,
496 nfs_path, version, protocol, &fh);
497 if (status < 0)
498 printk(KERN_ERR "Root-NFS: Server returned error %d "
499 "while mounting %s\n", status, nfs_path);
500 else {
501 nfs_data.root.size = fh.size;
502 memcpy(nfs_data.root.data, fh.data, fh.size);
505 return status;
509 * Get the NFS port numbers and file handle, and return the prepared 'data'
510 * argument for mount() if everything went OK. Return NULL otherwise.
512 void * __init nfs_root_data(void)
514 if (root_nfs_init() < 0
515 || root_nfs_ports() < 0
516 || root_nfs_get_handle() < 0)
517 return NULL;
518 set_sockaddr((struct sockaddr_in *) &nfs_data.addr, servaddr, htons(nfs_port));
519 return (void*)&nfs_data;