nfsroot.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:13k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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