nfsroot.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1995, 1996 Gero Kuhlmann <gero@gkminix.han.de>
  4. *
  5. * Allow an NFS filesystem to be mounted as root. The way this works is:
  6. * (1) Use the IP autoconfig mechanism to set local IP addresses and routes.
  7. * (2) Construct the device string and the options string using DHCP
  8. * option 17 and/or kernel command line options.
  9. * (3) When mount_root() sets up the root file system, pass these strings
  10. * to the NFS client's regular mount interface via sys_mount().
  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. * Chuck Lever : Use super.c's text-based mount option parsing
  71. * Chuck Lever : Add "nfsrootdebug".
  72. */
  73. #include <linux/types.h>
  74. #include <linux/string.h>
  75. #include <linux/init.h>
  76. #include <linux/nfs.h>
  77. #include <linux/nfs_fs.h>
  78. #include <linux/utsname.h>
  79. #include <linux/root_dev.h>
  80. #include <net/ipconfig.h>
  81. #include "internal.h"
  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. /* Default NFSROOT mount options. */
  86. #if defined(CONFIG_NFS_V2)
  87. #define NFS_DEF_OPTIONS "vers=2,tcp,rsize=4096,wsize=4096"
  88. #elif defined(CONFIG_NFS_V3)
  89. #define NFS_DEF_OPTIONS "vers=3,tcp,rsize=4096,wsize=4096"
  90. #else
  91. #define NFS_DEF_OPTIONS "vers=4,tcp,rsize=4096,wsize=4096"
  92. #endif
  93. /* Parameters passed from the kernel command line */
  94. static char nfs_root_parms[NFS_MAXPATHLEN + 1] __initdata = "";
  95. /* Text-based mount options passed to super.c */
  96. static char nfs_root_options[256] __initdata = NFS_DEF_OPTIONS;
  97. /* Address of NFS server */
  98. static __be32 servaddr __initdata = htonl(INADDR_NONE);
  99. /* Name of directory to mount */
  100. static char nfs_export_path[NFS_MAXPATHLEN + 1] __initdata = "";
  101. /* server:export path string passed to super.c */
  102. static char nfs_root_device[NFS_MAXPATHLEN + 1] __initdata = "";
  103. #ifdef NFS_DEBUG
  104. /*
  105. * When the "nfsrootdebug" kernel command line option is specified,
  106. * enable debugging messages for NFSROOT.
  107. */
  108. static int __init nfs_root_debug(char *__unused)
  109. {
  110. nfs_debug |= NFSDBG_ROOT | NFSDBG_MOUNT;
  111. return 1;
  112. }
  113. __setup("nfsrootdebug", nfs_root_debug);
  114. #endif
  115. /*
  116. * Parse NFS server and directory information passed on the kernel
  117. * command line.
  118. *
  119. * nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]
  120. *
  121. * If there is a "%s" token in the <root-dir> string, it is replaced
  122. * by the ASCII-representation of the client's IP address.
  123. */
  124. static int __init nfs_root_setup(char *line)
  125. {
  126. ROOT_DEV = Root_NFS;
  127. if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) {
  128. strlcpy(nfs_root_parms, line, sizeof(nfs_root_parms));
  129. } else {
  130. size_t n = strlen(line) + sizeof(NFS_ROOT) - 1;
  131. if (n >= sizeof(nfs_root_parms))
  132. line[sizeof(nfs_root_parms) - sizeof(NFS_ROOT) - 2] = '\0';
  133. sprintf(nfs_root_parms, NFS_ROOT, line);
  134. }
  135. /*
  136. * Extract the IP address of the NFS server containing our
  137. * root file system, if one was specified.
  138. *
  139. * Note: root_nfs_parse_addr() removes the server-ip from
  140. * nfs_root_parms, if it exists.
  141. */
  142. root_server_addr = root_nfs_parse_addr(nfs_root_parms);
  143. return 1;
  144. }
  145. __setup("nfsroot=", nfs_root_setup);
  146. static int __init root_nfs_copy(char *dest, const char *src,
  147. const size_t destlen)
  148. {
  149. if (strlcpy(dest, src, destlen) > destlen)
  150. return -1;
  151. return 0;
  152. }
  153. static int __init root_nfs_cat(char *dest, const char *src,
  154. const size_t destlen)
  155. {
  156. size_t len = strlen(dest);
  157. if (len && dest[len - 1] != ',')
  158. if (strlcat(dest, ",", destlen) > destlen)
  159. return -1;
  160. if (strlcat(dest, src, destlen) > destlen)
  161. return -1;
  162. return 0;
  163. }
  164. /*
  165. * Parse out root export path and mount options from
  166. * passed-in string @incoming.
  167. *
  168. * Copy the export path into @exppath.
  169. */
  170. static int __init root_nfs_parse_options(char *incoming, char *exppath,
  171. const size_t exppathlen)
  172. {
  173. char *p;
  174. /*
  175. * Set the NFS remote path
  176. */
  177. p = strsep(&incoming, ",");
  178. if (*p != '\0' && strcmp(p, "default") != 0)
  179. if (root_nfs_copy(exppath, p, exppathlen))
  180. return -1;
  181. /*
  182. * @incoming now points to the rest of the string; if it
  183. * contains something, append it to our root options buffer
  184. */
  185. if (incoming != NULL && *incoming != '\0')
  186. if (root_nfs_cat(nfs_root_options, incoming,
  187. sizeof(nfs_root_options)))
  188. return -1;
  189. return 0;
  190. }
  191. /*
  192. * Decode the export directory path name and NFS options from
  193. * the kernel command line. This has to be done late in order to
  194. * use a dynamically acquired client IP address for the remote
  195. * root directory path.
  196. *
  197. * Returns zero if successful; otherwise -1 is returned.
  198. */
  199. static int __init root_nfs_data(char *cmdline)
  200. {
  201. char mand_options[sizeof("nolock,addr=") + INET_ADDRSTRLEN + 1];
  202. int len, retval = -1;
  203. char *tmp = NULL;
  204. const size_t tmplen = sizeof(nfs_export_path);
  205. tmp = kzalloc(tmplen, GFP_KERNEL);
  206. if (tmp == NULL)
  207. goto out_nomem;
  208. strcpy(tmp, NFS_ROOT);
  209. if (root_server_path[0] != '\0') {
  210. dprintk("Root-NFS: DHCPv4 option 17: %s\n",
  211. root_server_path);
  212. if (root_nfs_parse_options(root_server_path, tmp, tmplen))
  213. goto out_optionstoolong;
  214. }
  215. if (cmdline[0] != '\0') {
  216. dprintk("Root-NFS: nfsroot=%s\n", cmdline);
  217. if (root_nfs_parse_options(cmdline, tmp, tmplen))
  218. goto out_optionstoolong;
  219. }
  220. /*
  221. * Append mandatory options for nfsroot so they override
  222. * what has come before
  223. */
  224. snprintf(mand_options, sizeof(mand_options), "nolock,addr=%pI4",
  225. &servaddr);
  226. if (root_nfs_cat(nfs_root_options, mand_options,
  227. sizeof(nfs_root_options)))
  228. goto out_optionstoolong;
  229. /*
  230. * Set up nfs_root_device. For NFS mounts, this looks like
  231. *
  232. * server:/path
  233. *
  234. * At this point, utsname()->nodename contains our local
  235. * IP address or hostname, set by ipconfig. If "%s" exists
  236. * in tmp, substitute the nodename, then shovel the whole
  237. * mess into nfs_root_device.
  238. */
  239. len = snprintf(nfs_export_path, sizeof(nfs_export_path),
  240. tmp, utsname()->nodename);
  241. if (len >= (int)sizeof(nfs_export_path))
  242. goto out_devnametoolong;
  243. len = snprintf(nfs_root_device, sizeof(nfs_root_device),
  244. "%pI4:%s", &servaddr, nfs_export_path);
  245. if (len >= (int)sizeof(nfs_root_device))
  246. goto out_devnametoolong;
  247. retval = 0;
  248. out:
  249. kfree(tmp);
  250. return retval;
  251. out_nomem:
  252. printk(KERN_ERR "Root-NFS: could not allocate memory\n");
  253. goto out;
  254. out_optionstoolong:
  255. printk(KERN_ERR "Root-NFS: mount options string too long\n");
  256. goto out;
  257. out_devnametoolong:
  258. printk(KERN_ERR "Root-NFS: root device name too long.\n");
  259. goto out;
  260. }
  261. /**
  262. * nfs_root_data - Return prepared 'data' for NFSROOT mount
  263. * @root_device: OUT: address of string containing NFSROOT device
  264. * @root_data: OUT: address of string containing NFSROOT mount options
  265. *
  266. * Returns zero and sets @root_device and @root_data if successful,
  267. * otherwise -1 is returned.
  268. */
  269. int __init nfs_root_data(char **root_device, char **root_data)
  270. {
  271. servaddr = root_server_addr;
  272. if (servaddr == htonl(INADDR_NONE)) {
  273. printk(KERN_ERR "Root-NFS: no NFS server address\n");
  274. return -1;
  275. }
  276. if (root_nfs_data(nfs_root_parms) < 0)
  277. return -1;
  278. *root_device = nfs_root_device;
  279. *root_data = nfs_root_options;
  280. return 0;
  281. }