nfsroot.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (C) 1995, 1996 Gero Kuhlmann <gero@gkminix.han.de>
  3. *
  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) Construct the device string and the options string using DHCP
  7. * option 17 and/or kernel command line options.
  8. * (3) When mount_root() sets up the root file system, pass these strings
  9. * to the NFS client's regular mount interface via sys_mount().
  10. *
  11. *
  12. * Changes:
  13. *
  14. * Alan Cox : Removed get_address name clash with FPU.
  15. * Alan Cox : Reformatted a bit.
  16. * Gero Kuhlmann : Code cleanup
  17. * Michael Rausch : Fixed recognition of an incoming RARP answer.
  18. * Martin Mares : (2.0) Auto-configuration via BOOTP supported.
  19. * Martin Mares : Manual selection of interface & BOOTP/RARP.
  20. * Martin Mares : Using network routes instead of host routes,
  21. * allowing the default configuration to be used
  22. * for normal operation of the host.
  23. * Martin Mares : Randomized timer with exponential backoff
  24. * installed to minimize network congestion.
  25. * Martin Mares : Code cleanup.
  26. * Martin Mares : (2.1) BOOTP and RARP made configuration options.
  27. * Martin Mares : Server hostname generation fixed.
  28. * Gerd Knorr : Fixed wired inode handling
  29. * Martin Mares : (2.2) "0.0.0.0" addresses from command line ignored.
  30. * Martin Mares : RARP replies not tested for server address.
  31. * Gero Kuhlmann : (2.3) Some bug fixes and code cleanup again (please
  32. * send me your new patches _before_ bothering
  33. * Linus so that I don' always have to cleanup
  34. * _afterwards_ - thanks)
  35. * Gero Kuhlmann : Last changes of Martin Mares undone.
  36. * Gero Kuhlmann : RARP replies are tested for specified server
  37. * again. However, it's now possible to have
  38. * different RARP and NFS servers.
  39. * Gero Kuhlmann : "0.0.0.0" addresses from command line are
  40. * now mapped to INADDR_NONE.
  41. * Gero Kuhlmann : Fixed a bug which prevented BOOTP path name
  42. * from being used (thanks to Leo Spiekman)
  43. * Andy Walker : Allow to specify the NFS server in nfs_root
  44. * without giving a path name
  45. * Swen Thümmler : Allow to specify the NFS options in nfs_root
  46. * without giving a path name. Fix BOOTP request
  47. * for domainname (domainname is NIS domain, not
  48. * DNS domain!). Skip dummy devices for BOOTP.
  49. * Jacek Zapala : Fixed a bug which prevented server-ip address
  50. * from nfsroot parameter from being used.
  51. * Olaf Kirch : Adapted to new NFS code.
  52. * Jakub Jelinek : Free used code segment.
  53. * Marko Kohtala : Fixed some bugs.
  54. * Martin Mares : Debug message cleanup
  55. * Martin Mares : Changed to use the new generic IP layer autoconfig
  56. * code. BOOTP and RARP moved there.
  57. * Martin Mares : Default path now contains host name instead of
  58. * host IP address (but host name defaults to IP
  59. * address anyway).
  60. * Martin Mares : Use root_server_addr appropriately during setup.
  61. * Martin Mares : Rewrote parameter parsing, now hopefully giving
  62. * correct overriding.
  63. * Trond Myklebust : Add in preliminary support for NFSv3 and TCP.
  64. * Fix bug in root_nfs_addr(). nfs_data.namlen
  65. * is NOT for the length of the hostname.
  66. * Hua Qin : Support for mounting root file system via
  67. * NFS over TCP.
  68. * Fabian Frederick: Option parser rebuilt (using parser lib)
  69. * Chuck Lever : Use super.c's text-based mount option parsing
  70. */
  71. #include <linux/types.h>
  72. #include <linux/string.h>
  73. #include <linux/init.h>
  74. #include <linux/nfs.h>
  75. #include <linux/nfs_fs.h>
  76. #include <linux/utsname.h>
  77. #include <linux/root_dev.h>
  78. #include <net/ipconfig.h>
  79. #include "internal.h"
  80. /* Define this to allow debugging output */
  81. #undef NFSROOT_DEBUG
  82. #define NFSDBG_FACILITY NFSDBG_ROOT
  83. /* Default path we try to mount. "%s" gets replaced by our IP address */
  84. #define NFS_ROOT "/tftpboot/%s"
  85. /* Parameters passed from the kernel command line */
  86. static char nfs_root_parms[256] __initdata = "";
  87. /* Text-based mount options passed to super.c */
  88. static char nfs_root_options[256] __initdata = "";
  89. /* Address of NFS server */
  90. static __be32 servaddr __initdata = htonl(INADDR_NONE);
  91. /* Name of directory to mount */
  92. static char nfs_export_path[NFS_MAXPATHLEN + 1] __initdata = "";
  93. /* server:export path string passed to super.c */
  94. static char nfs_root_device[NFS_MAXPATHLEN + 1] __initdata = "";
  95. /*
  96. * Parse NFS server and directory information passed on the kernel
  97. * command line.
  98. *
  99. * nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]
  100. *
  101. * If there is a "%s" token in the <root-dir> string, it is replaced
  102. * by the ASCII-representation of the client's IP address.
  103. */
  104. static int __init nfs_root_setup(char *line)
  105. {
  106. ROOT_DEV = Root_NFS;
  107. if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) {
  108. strlcpy(nfs_root_parms, line, sizeof(nfs_root_parms));
  109. } else {
  110. size_t n = strlen(line) + sizeof(NFS_ROOT) - 1;
  111. if (n >= sizeof(nfs_root_parms))
  112. line[sizeof(nfs_root_parms) - sizeof(NFS_ROOT) - 2] = '\0';
  113. sprintf(nfs_root_parms, NFS_ROOT, line);
  114. }
  115. /*
  116. * Extract the IP address of the NFS server containing our
  117. * root file system, if one was specified.
  118. *
  119. * Note: root_nfs_parse_addr() removes the server-ip from
  120. * nfs_root_parms, if it exists.
  121. */
  122. root_server_addr = root_nfs_parse_addr(nfs_root_parms);
  123. return 1;
  124. }
  125. __setup("nfsroot=", nfs_root_setup);
  126. static int __init root_nfs_copy(char *dest, const char *src,
  127. const size_t destlen)
  128. {
  129. if (strlcpy(dest, src, destlen) > destlen)
  130. return -1;
  131. return 0;
  132. }
  133. static int __init root_nfs_cat(char *dest, const char *src,
  134. const size_t destlen)
  135. {
  136. if (strlcat(dest, src, destlen) > destlen)
  137. return -1;
  138. return 0;
  139. }
  140. /*
  141. * Parse out root export path and mount options from
  142. * passed-in string @incoming.
  143. *
  144. * Copy the export path into @exppath.
  145. */
  146. static int __init root_nfs_parse_options(char *incoming, char *exppath,
  147. const size_t exppathlen)
  148. {
  149. char *p;
  150. /*
  151. * Set the NFS remote path
  152. */
  153. p = strsep(&incoming, ",");
  154. if (*p != '\0' && strcmp(p, "default") != 0)
  155. if (root_nfs_copy(exppath, p, exppathlen))
  156. return -1;
  157. /*
  158. * @incoming now points to the rest of the string; if it
  159. * contains something, append it to our root options buffer
  160. */
  161. if (incoming != NULL && *incoming != '\0')
  162. if (root_nfs_cat(nfs_root_options, incoming,
  163. sizeof(nfs_root_options)))
  164. return -1;
  165. /*
  166. * Possibly prepare for more options to be appended
  167. */
  168. if (nfs_root_options[0] != '\0' &&
  169. nfs_root_options[strlen(nfs_root_options)] != ',')
  170. if (root_nfs_cat(nfs_root_options, ",",
  171. sizeof(nfs_root_options)))
  172. return -1;
  173. return 0;
  174. }
  175. /*
  176. * Decode the export directory path name and NFS options from
  177. * the kernel command line. This has to be done late in order to
  178. * use a dynamically acquired client IP address for the remote
  179. * root directory path.
  180. *
  181. * Returns zero if successful; otherwise -1 is returned.
  182. */
  183. static int __init root_nfs_data(char *cmdline)
  184. {
  185. char addr_option[sizeof("nolock,addr=") + INET_ADDRSTRLEN + 1];
  186. int len, retval = -1;
  187. char *tmp = NULL;
  188. const size_t tmplen = sizeof(nfs_export_path);
  189. tmp = kzalloc(tmplen, GFP_KERNEL);
  190. if (tmp == NULL)
  191. goto out_nomem;
  192. strcpy(tmp, NFS_ROOT);
  193. if (root_server_path[0] != '\0') {
  194. dprintk("Root-NFS: DHCPv4 option 17: %s\n",
  195. root_server_path);
  196. if (root_nfs_parse_options(root_server_path, tmp, tmplen))
  197. goto out_optionstoolong;
  198. }
  199. if (cmdline[0] != '\0') {
  200. dprintk("Root-NFS: nfsroot=%s\n", cmdline);
  201. if (root_nfs_parse_options(cmdline, tmp, tmplen))
  202. goto out_optionstoolong;
  203. }
  204. /*
  205. * Append mandatory options for nfsroot so they override
  206. * what has come before
  207. */
  208. snprintf(addr_option, sizeof(addr_option), "nolock,addr=%pI4",
  209. &servaddr);
  210. if (root_nfs_cat(nfs_root_options, addr_option,
  211. sizeof(nfs_root_options)))
  212. goto out_optionstoolong;
  213. /*
  214. * Set up nfs_root_device. For NFS mounts, this looks like
  215. *
  216. * server:/path
  217. *
  218. * At this point, utsname()->nodename contains our local
  219. * IP address or hostname, set by ipconfig. If "%s" exists
  220. * in tmp, substitute the nodename, then shovel the whole
  221. * mess into nfs_root_device.
  222. */
  223. len = snprintf(nfs_export_path, sizeof(nfs_export_path),
  224. tmp, utsname()->nodename);
  225. if (len > (int)sizeof(nfs_export_path))
  226. goto out_devnametoolong;
  227. len = snprintf(nfs_root_device, sizeof(nfs_root_device),
  228. "%pI4:%s", &servaddr, nfs_export_path);
  229. if (len > (int)sizeof(nfs_root_device))
  230. goto out_devnametoolong;
  231. retval = 0;
  232. out:
  233. kfree(tmp);
  234. return retval;
  235. out_nomem:
  236. printk(KERN_ERR "Root-NFS: could not allocate memory\n");
  237. goto out;
  238. out_optionstoolong:
  239. printk(KERN_ERR "Root-NFS: mount options string too long\n");
  240. goto out;
  241. out_devnametoolong:
  242. printk(KERN_ERR "Root-NFS: root device name too long.\n");
  243. goto out;
  244. }
  245. /**
  246. * nfs_root_data - Return prepared 'data' for NFSROOT mount
  247. * @root_device: OUT: address of string containing NFSROOT device
  248. * @root_data: OUT: address of string containing NFSROOT mount options
  249. *
  250. * Returns zero and sets @root_device and @root_data if successful,
  251. * otherwise -1 is returned.
  252. */
  253. int __init nfs_root_data(char **root_device, char **root_data)
  254. {
  255. #ifdef NFSROOT_DEBUG
  256. nfs_debug |= NFSDBG_ROOT | NFSDBG_MOUNT;
  257. #endif /* NFSROOT_DEBUG */
  258. servaddr = root_server_addr;
  259. if (servaddr == htonl(INADDR_NONE)) {
  260. printk(KERN_ERR "Root-NFS: no NFS server address\n");
  261. return -1;
  262. }
  263. if (root_nfs_data(nfs_root_parms) < 0)
  264. return -1;
  265. *root_device = nfs_root_device;
  266. *root_data = nfs_root_options;
  267. return 0;
  268. }