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