~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/nfs/nfsroot.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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  */
 68 
 69 #include <linux/config.h>
 70 #include <linux/types.h>
 71 #include <linux/string.h>
 72 #include <linux/kernel.h>
 73 #include <linux/sched.h>
 74 #include <linux/fs.h>
 75 #include <linux/init.h>
 76 #include <linux/sunrpc/clnt.h>
 77 #include <linux/nfs.h>
 78 #include <linux/nfs_fs.h>
 79 #include <linux/nfs_mount.h>
 80 #include <linux/in.h>
 81 #include <linux/inet.h>
 82 #include <linux/major.h>
 83 #include <linux/utsname.h>
 84 #include <net/ipconfig.h>
 85 
 86 /* Define this to allow debugging output */
 87 #undef NFSROOT_DEBUG
 88 #define NFSDBG_FACILITY NFSDBG_ROOT
 89 
 90 /* Default path we try to mount. "%s" gets replaced by our IP address */
 91 #define NFS_ROOT                "/tftpboot/%s"
 92 
 93 /* Parameters passed from the kernel command line */
 94 static char nfs_root_name[256] __initdata = "";
 95 
 96 /* Address of NFS server */
 97 static __u32 servaddr __initdata = 0;
 98 
 99 /* Name of directory to mount */
100 static char nfs_path[NFS_MAXPATHLEN] __initdata = { 0, };
101 
102 /* NFS-related data */
103 static struct nfs_mount_data nfs_data __initdata = { 0, };/* NFS mount info */
104 static int nfs_port __initdata = 0;             /* Port to connect to for NFS */
105 static int mount_port __initdata = 0;           /* Mount daemon port number */
106 
107 
108 /***************************************************************************
109 
110                              Parsing of options
111 
112  ***************************************************************************/
113 
114 /*
115  *  The following integer options are recognized
116  */
117 static struct nfs_int_opts {
118         char *name;
119         int  *val;
120 } root_int_opts[] __initdata = {
121         { "port",       &nfs_port },
122         { "rsize",      &nfs_data.rsize },
123         { "wsize",      &nfs_data.wsize },
124         { "timeo",      &nfs_data.timeo },
125         { "retrans",    &nfs_data.retrans },
126         { "acregmin",   &nfs_data.acregmin },
127         { "acregmax",   &nfs_data.acregmax },
128         { "acdirmin",   &nfs_data.acdirmin },
129         { "acdirmax",   &nfs_data.acdirmax },
130         { NULL,         NULL }
131 };
132 
133 
134 /*
135  *  And now the flag options
136  */
137 static struct nfs_bool_opts {
138         char *name;
139         int  and_mask;
140         int  or_mask;
141 } root_bool_opts[] __initdata = {
142         { "soft",       ~NFS_MOUNT_SOFT,        NFS_MOUNT_SOFT },
143         { "hard",       ~NFS_MOUNT_SOFT,        0 },
144         { "intr",       ~NFS_MOUNT_INTR,        NFS_MOUNT_INTR },
145         { "nointr",     ~NFS_MOUNT_INTR,        0 },
146         { "posix",      ~NFS_MOUNT_POSIX,       NFS_MOUNT_POSIX },
147         { "noposix",    ~NFS_MOUNT_POSIX,       0 },
148         { "cto",        ~NFS_MOUNT_NOCTO,       0 },
149         { "nocto",      ~NFS_MOUNT_NOCTO,       NFS_MOUNT_NOCTO },
150         { "ac",         ~NFS_MOUNT_NOAC,        0 },
151         { "noac",       ~NFS_MOUNT_NOAC,        NFS_MOUNT_NOAC },
152         { "lock",       ~NFS_MOUNT_NONLM,       0 },
153         { "nolock",     ~NFS_MOUNT_NONLM,       NFS_MOUNT_NONLM },
154 #ifdef CONFIG_NFS_V3
155         { "v2",         ~NFS_MOUNT_VER3,        0 },
156         { "v3",         ~NFS_MOUNT_VER3,        NFS_MOUNT_VER3 },
157 #endif
158         { "udp",        ~NFS_MOUNT_TCP,         0 },
159         { "tcp",        ~NFS_MOUNT_TCP,         NFS_MOUNT_TCP },
160         { "broken_suid",~NFS_MOUNT_BROKEN_SUID, NFS_MOUNT_BROKEN_SUID },
161         { NULL,         0,                      0 }
162 };
163 
164 
165 /*
166  *  Extract IP address from the parameter string if needed. Note that we
167  *  need to have root_server_addr set _before_ IPConfig gets called as it
168  *  can override it.
169  */
170 static void __init root_nfs_parse_addr(char *name)
171 {
172         int octets = 0;
173         char *cp, *cq;
174 
175         cp = cq = name;
176         while (octets < 4) {
177                 while (*cp >= '' && *cp <= '9')
178                         cp++;
179                 if (cp == cq || cp - cq > 3)
180                         break;
181                 if (*cp == '.' || octets == 3)
182                         octets++;
183                 if (octets < 4)
184                         cp++;
185                 cq = cp;
186         }
187         if (octets == 4 && (*cp == ':' || *cp == '\0')) {
188                 if (*cp == ':')
189                         *cp++ = '\0';
190                 root_server_addr = in_aton(name);
191                 strcpy(name, cp);
192         }
193 }
194 
195 
196 /*
197  *  Parse option string.
198  */
199 static void __init root_nfs_parse(char *name, char *buf)
200 {
201         char *options, *val, *cp;
202 
203         if ((options = strchr(name, ','))) {
204                 *options++ = 0;
205                 cp = strtok(options, ",");
206                 while (cp) {
207                         if ((val = strchr(cp, '='))) {
208                                 struct nfs_int_opts *opts = root_int_opts;
209                                 *val++ = '\0';
210                                 while (opts->name && strcmp(opts->name, cp))
211                                         opts++;
212                                 if (opts->name)
213                                         *(opts->val) = (int) simple_strtoul(val, NULL, 10);
214                         } else {
215                                 struct nfs_bool_opts *opts = root_bool_opts;
216                                 while (opts->name && strcmp(opts->name, cp))
217                                         opts++;
218                                 if (opts->name) {
219                                         nfs_data.flags &= opts->and_mask;
220                                         nfs_data.flags |= opts->or_mask;
221                                 }
222                         }
223                         cp = strtok(NULL, ",");
224                 }
225         }
226         if (name[0] && strcmp(name, "default")) {
227                 strncpy(buf, name, NFS_MAXPATHLEN-1);
228                 buf[NFS_MAXPATHLEN-1] = 0;
229         }
230 }
231 
232 
233 /*
234  *  Prepare the NFS data structure and parse all options.
235  */
236 static int __init root_nfs_name(char *name)
237 {
238         char buf[NFS_MAXPATHLEN];
239         char *cp;
240 
241         /* Set some default values */
242         memset(&nfs_data, 0, sizeof(nfs_data));
243         nfs_port          = -1;
244         nfs_data.version  = NFS_MOUNT_VERSION;
245         nfs_data.flags    = NFS_MOUNT_NONLM;    /* No lockd in nfs root yet */
246         nfs_data.rsize    = NFS_DEF_FILE_IO_BUFFER_SIZE;
247         nfs_data.wsize    = NFS_DEF_FILE_IO_BUFFER_SIZE;
248         nfs_data.bsize    = 0;
249         nfs_data.timeo    = 7;
250         nfs_data.retrans  = 3;
251         nfs_data.acregmin = 3;
252         nfs_data.acregmax = 60;
253         nfs_data.acdirmin = 30;
254         nfs_data.acdirmax = 60;
255         strcpy(buf, NFS_ROOT);
256 
257         /* Process options received from the remote server */
258         root_nfs_parse(root_server_path, buf);
259 
260         /* Override them by options set on kernel command-line */
261         root_nfs_parse(name, buf);
262 
263         cp = system_utsname.nodename;
264         if (strlen(buf) + strlen(cp) > NFS_MAXPATHLEN) {
265                 printk(KERN_ERR "Root-NFS: Pathname for remote directory too long.\n");
266                 return -1;
267         }
268         sprintf(nfs_path, buf, cp);
269 
270         return 1;
271 }
272 
273 
274 /*
275  *  Get NFS server address.
276  */
277 static int __init root_nfs_addr(void)
278 {
279         if ((servaddr = root_server_addr) == INADDR_NONE) {
280                 printk(KERN_ERR "Root-NFS: No NFS server available, giving up.\n");
281                 return -1;
282         }
283 
284         strncpy(nfs_data.hostname, in_ntoa(servaddr), sizeof(nfs_data.hostname)-1);
285         return 0;
286 }
287 
288 /*
289  *  Tell the user what's going on.
290  */
291 #ifdef NFSROOT_DEBUG
292 static void __init root_nfs_print(void)
293 {
294         printk(KERN_NOTICE "Root-NFS: Mounting %s on server %s as root\n",
295                 nfs_path, nfs_data.hostname);
296         printk(KERN_NOTICE "Root-NFS:     rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
297                 nfs_data.rsize, nfs_data.wsize, nfs_data.timeo, nfs_data.retrans);
298         printk(KERN_NOTICE "Root-NFS:     acreg (min,max) = (%d,%d), acdir (min,max) = (%d,%d)\n",
299                 nfs_data.acregmin, nfs_data.acregmax,
300                 nfs_data.acdirmin, nfs_data.acdirmax);
301         printk(KERN_NOTICE "Root-NFS:     nfsd port = %d, mountd port = %d, flags = %08x\n",
302                 nfs_port, mount_port, nfs_data.flags);
303 }
304 #endif
305 
306 
307 int __init root_nfs_init(void)
308 {
309 #ifdef NFSROOT_DEBUG
310         nfs_debug |= NFSDBG_ROOT;
311 #endif
312 
313         /*
314          * Decode the root directory path name and NFS options from
315          * the kernel command line. This has to go here in order to
316          * be able to use the client IP address for the remote root
317          * directory (necessary for pure RARP booting).
318          */
319         if (root_nfs_name(nfs_root_name) < 0 ||
320             root_nfs_addr() < 0)
321                 return -1;
322 
323 #ifdef NFSROOT_DEBUG
324         root_nfs_print();
325 #endif
326 
327         return 0;
328 }
329 
330 
331 /*
332  *  Parse NFS server and directory information passed on the kernel
333  *  command line.
334  */
335 int __init nfs_root_setup(char *line)
336 {
337         ROOT_DEV = MKDEV(UNNAMED_MAJOR, 255);
338         if (line[0] == '/' || line[0] == ',' || (line[0] >= '' && line[0] <= '9')) {
339                 strncpy(nfs_root_name, line, sizeof(nfs_root_name));
340                 nfs_root_name[sizeof(nfs_root_name)-1] = '\0';
341         } else {
342                 int n = strlen(line) + strlen(NFS_ROOT);
343                 if (n >= sizeof(nfs_root_name))
344                         line[sizeof(nfs_root_name) - strlen(NFS_ROOT) - 1] = '\0';
345                 sprintf(nfs_root_name, NFS_ROOT, line);
346         }
347         root_nfs_parse_addr(nfs_root_name);
348         return 1;
349 }
350 
351 __setup("nfsroot=", nfs_root_setup);
352 
353 /***************************************************************************
354 
355                Routines to actually mount the root directory
356 
357  ***************************************************************************/
358 
359 /*
360  *  Construct sockaddr_in from address and port number.
361  */
362 static inline void
363 set_sockaddr(struct sockaddr_in *sin, __u32 addr, __u16 port)
364 {
365         sin->sin_family = AF_INET;
366         sin->sin_addr.s_addr = addr;
367         sin->sin_port = port;
368 }
369 
370 /*
371  *  Query server portmapper for the port of a daemon program.
372  */
373 static int __init root_nfs_getport(int program, int version, int proto)
374 {
375         struct sockaddr_in sin;
376 
377         printk(KERN_NOTICE "Looking up port of RPC %d/%d on %s\n",
378                 program, version, in_ntoa(servaddr));
379         set_sockaddr(&sin, servaddr, 0);
380         return rpc_getport_external(&sin, program, version, proto);
381 }
382 
383 
384 /*
385  *  Use portmapper to find mountd and nfsd port numbers if not overriden
386  *  by the user. Use defaults if portmapper is not available.
387  *  XXX: Is there any nfs server with no portmapper?
388  */
389 static int __init root_nfs_ports(void)
390 {
391         int port;
392         int nfsd_ver, mountd_ver;
393         int nfsd_port, mountd_port;
394         int proto;
395 
396         if (nfs_data.flags & NFS_MOUNT_VER3) {
397                 nfsd_ver = NFS3_VERSION;
398                 mountd_ver = NFS_MNT3_VERSION;
399                 nfsd_port = NFS_PORT;
400                 mountd_port = NFS_MNT_PORT;
401         } else {
402                 nfsd_ver = NFS2_VERSION;
403                 mountd_ver = NFS_MNT_VERSION;
404                 nfsd_port = NFS_PORT;
405                 mountd_port = NFS_MNT_PORT;
406         }
407 
408         proto = (nfs_data.flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
409 
410         if (nfs_port < 0) {
411                 if ((port = root_nfs_getport(NFS_PROGRAM, nfsd_ver, proto)) < 0) {
412                         printk(KERN_ERR "Root-NFS: Unable to get nfsd port "
413                                         "number from server, using default\n");
414                         port = nfsd_port;
415                 }
416                 nfs_port = htons(port);
417                 dprintk("Root-NFS: Portmapper on server returned %d "
418                         "as nfsd port\n", port);
419         }
420 
421         if ((port = root_nfs_getport(NFS_MNT_PROGRAM, nfsd_ver, proto)) < 0) {
422                 printk(KERN_ERR "Root-NFS: Unable to get mountd port "
423                                 "number from server, using default\n");
424                 port = mountd_port;
425         }
426         mount_port = htons(port);
427         dprintk("Root-NFS: mountd port is %d\n", port);
428 
429         return 0;
430 }
431 
432 
433 /*
434  *  Get a file handle from the server for the directory which is to be
435  *  mounted.
436  */
437 static int __init root_nfs_get_handle(void)
438 {
439         struct sockaddr_in sin;
440         int status;
441 
442         set_sockaddr(&sin, servaddr, mount_port);
443         if (nfs_data.flags & NFS_MOUNT_VER3)
444                 status = nfs3_mount(&sin, nfs_path, &nfs_data.root);
445         else
446                 status = nfs_mount(&sin, nfs_path, &nfs_data.root);
447         if (status < 0)
448                 printk(KERN_ERR "Root-NFS: Server returned error %d "
449                                 "while mounting %s\n", status, nfs_path);
450 
451         return status;
452 }
453 
454 /*
455  *  Get the NFS port numbers and file handle, and return the prepared 'data'
456  *  argument for ->read_super() if everything went OK. Return NULL otherwise.
457  */
458 void * __init nfs_root_data(void)
459 {
460         if (root_nfs_init() < 0
461          || root_nfs_ports() < 0
462          || root_nfs_get_handle() < 0)
463                 return NULL;
464         set_sockaddr((struct sockaddr_in *) &nfs_data.addr, servaddr, nfs_port);
465         return (void*)&nfs_data;
466 }
467 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.