fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / nfs / nfsroot.c
1 /*
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
72 #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 */
98 static char nfs_root_name[256] __initdata = "";
99
100 /* Address of NFS server */
101 static __be32 servaddr __initdata = 0;
102
103 /* Name of directory to mount */
104 static char nfs_path[NFS_MAXPATHLEN] __initdata = { 0, };
105
106 /* NFS-related data */
107 static struct nfs_mount_data nfs_data __initdata = { 0, };/* NFS mount info */
108 static int nfs_port __initdata = 0;             /* Port to connect to for NFS */
109 static int mount_port __initdata = 0;           /* Mount daemon port number */
110
111
112 /***************************************************************************
113
114                              Parsing of options
115
116  ***************************************************************************/
117
118 enum {
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, Opt_tagid,
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,
126         Opt_acl, Opt_noacl, Opt_tag, Opt_notag,
127         /* Error token */
128         Opt_err
129 };
130
131 static 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"},
161         {Opt_acl, "acl"},
162         {Opt_noacl, "noacl"},
163         {Opt_tag, "tag"},
164         {Opt_notag, "notag"},
165         {Opt_tagid, "tagid=%u"},
166         {Opt_tag, "tagxid"},
167         {Opt_err, NULL}
168         
169 };
170
171 /*
172  *  Parse option string.
173  */
174
175 static int __init root_nfs_parse(char *name, char *buf)
176 {
177
178         char *p;
179         substring_t args[MAX_OPT_ARGS];
180         int option;
181
182         if (!name)
183                 return 1;
184
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);
189
190         while ((p = strsep (&name, ",")) != NULL) {
191                 int token; 
192                 if (!*p)
193                         continue;
194                 token = match_token(p, tokens, args);
195
196                 /* %u tokens only. Beware if you add new tokens! */
197                 if (token < Opt_soft && match_int(&args[0], &option))
198                         return 0;
199                 switch (token) {
200                         case Opt_port:
201                                 nfs_port = option;
202                                 break;
203                         case Opt_rsize:
204                                 nfs_data.rsize = option;
205                                 break;
206                         case Opt_wsize:
207                                 nfs_data.wsize = option;
208                                 break;
209                         case Opt_timeo:
210                                 nfs_data.timeo = option;
211                                 break;
212                         case Opt_retrans:
213                                 nfs_data.retrans = option;
214                                 break;
215                         case Opt_acregmin:
216                                 nfs_data.acregmin = option;
217                                 break;
218                         case Opt_acregmax:
219                                 nfs_data.acregmax = option;
220                                 break;
221                         case Opt_acdirmin:
222                                 nfs_data.acdirmin = option;
223                                 break;
224                         case Opt_acdirmax:
225                                 nfs_data.acdirmax = option;
226                                 break;
227                         case Opt_soft:
228                                 nfs_data.flags |= NFS_MOUNT_SOFT;
229                                 break;
230                         case Opt_hard:
231                                 nfs_data.flags &= ~NFS_MOUNT_SOFT;
232                                 break;
233                         case Opt_intr:
234                                 nfs_data.flags |= NFS_MOUNT_INTR;
235                                 break;
236                         case Opt_nointr:
237                                 nfs_data.flags &= ~NFS_MOUNT_INTR;
238                                 break;
239                         case Opt_posix:
240                                 nfs_data.flags |= NFS_MOUNT_POSIX;
241                                 break;
242                         case Opt_noposix:
243                                 nfs_data.flags &= ~NFS_MOUNT_POSIX;
244                                 break;
245                         case Opt_cto:
246                                 nfs_data.flags &= ~NFS_MOUNT_NOCTO;
247                                 break;
248                         case Opt_nocto:
249                                 nfs_data.flags |= NFS_MOUNT_NOCTO;
250                                 break;
251                         case Opt_ac:
252                                 nfs_data.flags &= ~NFS_MOUNT_NOAC;
253                                 break;
254                         case Opt_noac:
255                                 nfs_data.flags |= NFS_MOUNT_NOAC;
256                                 break;
257                         case Opt_lock:
258                                 nfs_data.flags &= ~NFS_MOUNT_NONLM;
259                                 break;
260                         case Opt_nolock:
261                                 nfs_data.flags |= NFS_MOUNT_NONLM;
262                                 break;
263                         case Opt_v2:
264                                 nfs_data.flags &= ~NFS_MOUNT_VER3;
265                                 break;
266                         case Opt_v3:
267                                 nfs_data.flags |= NFS_MOUNT_VER3;
268                                 break;
269                         case Opt_udp:
270                                 nfs_data.flags &= ~NFS_MOUNT_TCP;
271                                 break;
272                         case Opt_tcp:
273                                 nfs_data.flags |= NFS_MOUNT_TCP;
274                                 break;
275                         case Opt_acl:
276                                 nfs_data.flags &= ~NFS_MOUNT_NOACL;
277                                 break;
278                         case Opt_noacl:
279                                 nfs_data.flags |= NFS_MOUNT_NOACL;
280                                 break;
281 #ifndef CONFIG_TAGGING_NONE
282                         case Opt_tag:
283                                 nfs_data.flags |= NFS_MOUNT_TAGGED;
284                                 break;
285                         case Opt_notag:
286                                 nfs_data.flags &= ~NFS_MOUNT_TAGGED;
287                                 break;
288 #endif
289 #ifdef CONFIG_PROPAGATE
290                         case Opt_tagid:
291                                 /* use args[0] */
292                                 nfs_data.flags |= NFS_MOUNT_TAGGED;
293                                 break;
294 #endif
295                         default:
296                                 printk(KERN_WARNING "Root-NFS: unknown "
297                                         "option: %s\n", p);
298                                 return 0;
299                 }
300         }
301
302         return 1;
303 }
304
305 /*
306  *  Prepare the NFS data structure and parse all options.
307  */
308 static int __init root_nfs_name(char *name)
309 {
310         static char buf[NFS_MAXPATHLEN] __initdata;
311         char *cp;
312
313         /* Set some default values */
314         memset(&nfs_data, 0, sizeof(nfs_data));
315         nfs_port          = -1;
316         nfs_data.version  = NFS_MOUNT_VERSION;
317         nfs_data.flags    = NFS_MOUNT_NONLM;    /* No lockd in nfs root yet */
318         nfs_data.rsize    = NFS_DEF_FILE_IO_SIZE;
319         nfs_data.wsize    = NFS_DEF_FILE_IO_SIZE;
320         nfs_data.acregmin = 3;
321         nfs_data.acregmax = 60;
322         nfs_data.acdirmin = 30;
323         nfs_data.acdirmax = 60;
324         strcpy(buf, NFS_ROOT);
325
326         /* Process options received from the remote server */
327         root_nfs_parse(root_server_path, buf);
328
329         /* Override them by options set on kernel command-line */
330         root_nfs_parse(name, buf);
331
332         cp = utsname()->nodename;
333         if (strlen(buf) + strlen(cp) > NFS_MAXPATHLEN) {
334                 printk(KERN_ERR "Root-NFS: Pathname for remote directory too long.\n");
335                 return -1;
336         }
337         sprintf(nfs_path, buf, cp);
338
339         return 1;
340 }
341
342
343 /*
344  *  Get NFS server address.
345  */
346 static int __init root_nfs_addr(void)
347 {
348         if ((servaddr = root_server_addr) == htonl(INADDR_NONE)) {
349                 printk(KERN_ERR "Root-NFS: No NFS server available, giving up.\n");
350                 return -1;
351         }
352
353         snprintf(nfs_data.hostname, sizeof(nfs_data.hostname),
354                  "%u.%u.%u.%u", NIPQUAD(servaddr));
355         return 0;
356 }
357
358 /*
359  *  Tell the user what's going on.
360  */
361 #ifdef NFSROOT_DEBUG
362 static void __init root_nfs_print(void)
363 {
364         printk(KERN_NOTICE "Root-NFS: Mounting %s on server %s as root\n",
365                 nfs_path, nfs_data.hostname);
366         printk(KERN_NOTICE "Root-NFS:     rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
367                 nfs_data.rsize, nfs_data.wsize, nfs_data.timeo, nfs_data.retrans);
368         printk(KERN_NOTICE "Root-NFS:     acreg (min,max) = (%d,%d), acdir (min,max) = (%d,%d)\n",
369                 nfs_data.acregmin, nfs_data.acregmax,
370                 nfs_data.acdirmin, nfs_data.acdirmax);
371         printk(KERN_NOTICE "Root-NFS:     nfsd port = %d, mountd port = %d, flags = %08x\n",
372                 nfs_port, mount_port, nfs_data.flags);
373 }
374 #endif
375
376
377 static int __init root_nfs_init(void)
378 {
379 #ifdef NFSROOT_DEBUG
380         nfs_debug |= NFSDBG_ROOT;
381 #endif
382
383         /*
384          * Decode the root directory path name and NFS options from
385          * the kernel command line. This has to go here in order to
386          * be able to use the client IP address for the remote root
387          * directory (necessary for pure RARP booting).
388          */
389         if (root_nfs_name(nfs_root_name) < 0 ||
390             root_nfs_addr() < 0)
391                 return -1;
392
393 #ifdef NFSROOT_DEBUG
394         root_nfs_print();
395 #endif
396
397         return 0;
398 }
399
400
401 /*
402  *  Parse NFS server and directory information passed on the kernel
403  *  command line.
404  */
405 static int __init nfs_root_setup(char *line)
406 {
407         ROOT_DEV = Root_NFS;
408         if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) {
409                 strlcpy(nfs_root_name, line, sizeof(nfs_root_name));
410         } else {
411                 int n = strlen(line) + sizeof(NFS_ROOT) - 1;
412                 if (n >= sizeof(nfs_root_name))
413                         line[sizeof(nfs_root_name) - sizeof(NFS_ROOT) - 2] = '\0';
414                 sprintf(nfs_root_name, NFS_ROOT, line);
415         }
416         root_server_addr = root_nfs_parse_addr(nfs_root_name);
417         return 1;
418 }
419
420 __setup("nfsroot=", nfs_root_setup);
421
422 /***************************************************************************
423
424                Routines to actually mount the root directory
425
426  ***************************************************************************/
427
428 /*
429  *  Construct sockaddr_in from address and port number.
430  */
431 static inline void
432 set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
433 {
434         sin->sin_family = AF_INET;
435         sin->sin_addr.s_addr = addr;
436         sin->sin_port = port;
437 }
438
439 /*
440  *  Query server portmapper for the port of a daemon program.
441  */
442 static int __init root_nfs_getport(int program, int version, int proto)
443 {
444         struct sockaddr_in sin;
445
446         printk(KERN_NOTICE "Looking up port of RPC %d/%d on %u.%u.%u.%u\n",
447                 program, version, NIPQUAD(servaddr));
448         set_sockaddr(&sin, servaddr, 0);
449         return rpc_getport_external(&sin, program, version, proto);
450 }
451
452
453 /*
454  *  Use portmapper to find mountd and nfsd port numbers if not overriden
455  *  by the user. Use defaults if portmapper is not available.
456  *  XXX: Is there any nfs server with no portmapper?
457  */
458 static int __init root_nfs_ports(void)
459 {
460         int port;
461         int nfsd_ver, mountd_ver;
462         int nfsd_port, mountd_port;
463         int proto;
464
465         if (nfs_data.flags & NFS_MOUNT_VER3) {
466                 nfsd_ver = NFS3_VERSION;
467                 mountd_ver = NFS_MNT3_VERSION;
468                 nfsd_port = NFS_PORT;
469                 mountd_port = NFS_MNT_PORT;
470         } else {
471                 nfsd_ver = NFS2_VERSION;
472                 mountd_ver = NFS_MNT_VERSION;
473                 nfsd_port = NFS_PORT;
474                 mountd_port = NFS_MNT_PORT;
475         }
476
477         proto = (nfs_data.flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
478
479         if (nfs_port < 0) {
480                 if ((port = root_nfs_getport(NFS_PROGRAM, nfsd_ver, proto)) < 0) {
481                         printk(KERN_ERR "Root-NFS: Unable to get nfsd port "
482                                         "number from server, using default\n");
483                         port = nfsd_port;
484                 }
485                 nfs_port = port;
486                 dprintk("Root-NFS: Portmapper on server returned %d "
487                         "as nfsd port\n", port);
488         }
489
490         if ((port = root_nfs_getport(NFS_MNT_PROGRAM, mountd_ver, proto)) < 0) {
491                 printk(KERN_ERR "Root-NFS: Unable to get mountd port "
492                                 "number from server, using default\n");
493                 port = mountd_port;
494         }
495         mount_port = port;
496         dprintk("Root-NFS: mountd port is %d\n", port);
497
498         return 0;
499 }
500
501
502 /*
503  *  Get a file handle from the server for the directory which is to be
504  *  mounted.
505  */
506 static int __init root_nfs_get_handle(void)
507 {
508         struct nfs_fh fh;
509         struct sockaddr_in sin;
510         int status;
511         int protocol = (nfs_data.flags & NFS_MOUNT_TCP) ?
512                                         IPPROTO_TCP : IPPROTO_UDP;
513         int version = (nfs_data.flags & NFS_MOUNT_VER3) ?
514                                         NFS_MNT3_VERSION : NFS_MNT_VERSION;
515
516         set_sockaddr(&sin, servaddr, htons(mount_port));
517         status = nfsroot_mount(&sin, nfs_path, &fh, version, protocol);
518         if (status < 0)
519                 printk(KERN_ERR "Root-NFS: Server returned error %d "
520                                 "while mounting %s\n", status, nfs_path);
521         else {
522                 nfs_data.root.size = fh.size;
523                 memcpy(nfs_data.root.data, fh.data, fh.size);
524         }
525
526         return status;
527 }
528
529 /*
530  *  Get the NFS port numbers and file handle, and return the prepared 'data'
531  *  argument for mount() if everything went OK. Return NULL otherwise.
532  */
533 void * __init nfs_root_data(void)
534 {
535         if (root_nfs_init() < 0
536          || root_nfs_ports() < 0
537          || root_nfs_get_handle() < 0)
538                 return NULL;
539         set_sockaddr((struct sockaddr_in *) &nfs_data.addr, servaddr, htons(nfs_port));
540         return (void*)&nfs_data;
541 }