nfsroot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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) Handle RPC negotiation with the system which replied to RARP or
  7. * was reported as a boot server by BOOTP or manually.
  8. * (3) The actual mounting is done later, when init() is running.
  9. *
  10. *
  11. * Changes:
  12. *
  13. * Alan Cox : Removed get_address name clash with FPU.
  14. * Alan Cox : Reformatted a bit.
  15. * Gero Kuhlmann : Code cleanup
  16. * Michael Rausch : Fixed recognition of an incoming RARP answer.
  17. * Martin Mares : (2.0) Auto-configuration via BOOTP supported.
  18. * Martin Mares : Manual selection of interface & BOOTP/RARP.
  19. * Martin Mares : Using network routes instead of host routes,
  20. * allowing the default configuration to be used
  21. * for normal operation of the host.
  22. * Martin Mares : Randomized timer with exponential backoff
  23. * installed to minimize network congestion.
  24. * Martin Mares : Code cleanup.
  25. * Martin Mares : (2.1) BOOTP and RARP made configuration options.
  26. * Martin Mares : Server hostname generation fixed.
  27. * Gerd Knorr : Fixed wired inode handling
  28. * Martin Mares : (2.2) "0.0.0.0" addresses from command line ignored.
  29. * Martin Mares : RARP replies not tested for server address.
  30. * Gero Kuhlmann : (2.3) Some bug fixes and code cleanup again (please
  31. * send me your new patches _before_ bothering
  32. * Linus so that I don' always have to cleanup
  33. * _afterwards_ - thanks)
  34. * Gero Kuhlmann : Last changes of Martin Mares undone.
  35. * Gero Kuhlmann : RARP replies are tested for specified server
  36. * again. However, it's now possible to have
  37. * different RARP and NFS servers.
  38. * Gero Kuhlmann : "0.0.0.0" addresses from command line are
  39. * now mapped to INADDR_NONE.
  40. * Gero Kuhlmann : Fixed a bug which prevented BOOTP path name
  41. * from being used (thanks to Leo Spiekman)
  42. * Andy Walker : Allow to specify the NFS server in nfs_root
  43. * without giving a path name
  44. * Swen Thümmler : Allow to specify the NFS options in nfs_root
  45. * without giving a path name. Fix BOOTP request
  46. * for domainname (domainname is NIS domain, not
  47. * DNS domain!). Skip dummy devices for BOOTP.
  48. * Jacek Zapala : Fixed a bug which prevented server-ip address
  49. * from nfsroot parameter from being used.
  50. * Olaf Kirch : Adapted to new NFS code.
  51. * Jakub Jelinek : Free used code segment.
  52. * Marko Kohtala : Fixed some bugs.
  53. * Martin Mares : Debug message cleanup
  54. * Martin Mares : Changed to use the new generic IP layer autoconfig
  55. * code. BOOTP and RARP moved there.
  56. * Martin Mares : Default path now contains host name instead of
  57. * host IP address (but host name defaults to IP
  58. * address anyway).
  59. * Martin Mares : Use root_server_addr appropriately during setup.
  60. * Martin Mares : Rewrote parameter parsing, now hopefully giving
  61. * correct overriding.
  62. * Trond Myklebust : Add in preliminary support for NFSv3 and TCP.
  63. * Fix bug in root_nfs_addr(). nfs_data.namlen
  64. * is NOT for the length of the hostname.
  65. * Hua Qin : Support for mounting root file system via
  66. * NFS over TCP.
  67. * Fabian Frederick: Option parser rebuilt (using parser lib)
  68. */
  69. #include <linux/types.h>
  70. #include <linux/string.h>
  71. #include <linux/kernel.h>
  72. #include <linux/time.h>
  73. #include <linux/fs.h>
  74. #include <linux/init.h>
  75. #include <linux/sunrpc/clnt.h>
  76. #include <linux/sunrpc/xprtsock.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/major.h>
  82. #include <linux/utsname.h>
  83. #include <linux/inet.h>
  84. #include <linux/root_dev.h>
  85. #include <net/ipconfig.h>
  86. #include <linux/parser.h>
  87. #include "internal.h"
  88. /* Define this to allow debugging output */
  89. #undef NFSROOT_DEBUG
  90. #define NFSDBG_FACILITY NFSDBG_ROOT
  91. /* Default port to use if server is not running a portmapper */
  92. #define NFS_MNT_PORT 627
  93. /* Default path we try to mount. "%s" gets replaced by our IP address */
  94. #define NFS_ROOT "/tftpboot/%s"
  95. /* Parameters passed from the kernel command line */
  96. static char nfs_root_name[256] __initdata = "";
  97. /* Address of NFS server */
  98. static __be32 servaddr __initdata = 0;
  99. /* Name of directory to mount */
  100. static char nfs_export_path[NFS_MAXPATHLEN] __initdata = { 0, };
  101. /* NFS-related data */
  102. static struct nfs_mount_data nfs_data __initdata = { 0, };/* NFS mount info */
  103. static int nfs_port __initdata = 0; /* Port to connect to for NFS */
  104. static int mount_port __initdata = 0; /* Mount daemon port number */
  105. /***************************************************************************
  106. Parsing of options
  107. ***************************************************************************/
  108. enum {
  109. /* Options that take integer arguments */
  110. Opt_port, Opt_rsize, Opt_wsize, Opt_timeo, Opt_retrans, Opt_acregmin,
  111. Opt_acregmax, Opt_acdirmin, Opt_acdirmax,
  112. /* Options that take no arguments */
  113. Opt_soft, Opt_hard, Opt_intr,
  114. Opt_nointr, Opt_posix, Opt_noposix, Opt_cto, Opt_nocto, Opt_ac,
  115. Opt_noac, Opt_lock, Opt_nolock, Opt_v2, Opt_v3, Opt_udp, Opt_tcp,
  116. Opt_acl, Opt_noacl,
  117. /* Error token */
  118. Opt_err
  119. };
  120. static const match_table_t tokens __initconst = {
  121. {Opt_port, "port=%u"},
  122. {Opt_rsize, "rsize=%u"},
  123. {Opt_wsize, "wsize=%u"},
  124. {Opt_timeo, "timeo=%u"},
  125. {Opt_retrans, "retrans=%u"},
  126. {Opt_acregmin, "acregmin=%u"},
  127. {Opt_acregmax, "acregmax=%u"},
  128. {Opt_acdirmin, "acdirmin=%u"},
  129. {Opt_acdirmax, "acdirmax=%u"},
  130. {Opt_soft, "soft"},
  131. {Opt_hard, "hard"},
  132. {Opt_intr, "intr"},
  133. {Opt_nointr, "nointr"},
  134. {Opt_posix, "posix"},
  135. {Opt_noposix, "noposix"},
  136. {Opt_cto, "cto"},
  137. {Opt_nocto, "nocto"},
  138. {Opt_ac, "ac"},
  139. {Opt_noac, "noac"},
  140. {Opt_lock, "lock"},
  141. {Opt_nolock, "nolock"},
  142. {Opt_v2, "nfsvers=2"},
  143. {Opt_v2, "v2"},
  144. {Opt_v3, "nfsvers=3"},
  145. {Opt_v3, "v3"},
  146. {Opt_udp, "proto=udp"},
  147. {Opt_udp, "udp"},
  148. {Opt_tcp, "proto=tcp"},
  149. {Opt_tcp, "tcp"},
  150. {Opt_acl, "acl"},
  151. {Opt_noacl, "noacl"},
  152. {Opt_err, NULL}
  153. };
  154. /*
  155. * Parse option string.
  156. */
  157. static int __init root_nfs_parse(char *name, char *buf)
  158. {
  159. char *p;
  160. substring_t args[MAX_OPT_ARGS];
  161. int option;
  162. if (!name)
  163. return 1;
  164. /* Set the NFS remote path */
  165. p = strsep(&name, ",");
  166. if (p[0] != '\0' && strcmp(p, "default") != 0)
  167. strlcpy(buf, p, NFS_MAXPATHLEN);
  168. while ((p = strsep (&name, ",")) != NULL) {
  169. int token;
  170. if (!*p)
  171. continue;
  172. token = match_token(p, tokens, args);
  173. /* %u tokens only. Beware if you add new tokens! */
  174. if (token < Opt_soft && match_int(&args[0], &option))
  175. return 0;
  176. switch (token) {
  177. case Opt_port:
  178. nfs_port = option;
  179. break;
  180. case Opt_rsize:
  181. nfs_data.rsize = option;
  182. break;
  183. case Opt_wsize:
  184. nfs_data.wsize = option;
  185. break;
  186. case Opt_timeo:
  187. nfs_data.timeo = option;
  188. break;
  189. case Opt_retrans:
  190. nfs_data.retrans = option;
  191. break;
  192. case Opt_acregmin:
  193. nfs_data.acregmin = option;
  194. break;
  195. case Opt_acregmax:
  196. nfs_data.acregmax = option;
  197. break;
  198. case Opt_acdirmin:
  199. nfs_data.acdirmin = option;
  200. break;
  201. case Opt_acdirmax:
  202. nfs_data.acdirmax = option;
  203. break;
  204. case Opt_soft:
  205. nfs_data.flags |= NFS_MOUNT_SOFT;
  206. break;
  207. case Opt_hard:
  208. nfs_data.flags &= ~NFS_MOUNT_SOFT;
  209. break;
  210. case Opt_intr:
  211. case Opt_nointr:
  212. break;
  213. case Opt_posix:
  214. nfs_data.flags |= NFS_MOUNT_POSIX;
  215. break;
  216. case Opt_noposix:
  217. nfs_data.flags &= ~NFS_MOUNT_POSIX;
  218. break;
  219. case Opt_cto:
  220. nfs_data.flags &= ~NFS_MOUNT_NOCTO;
  221. break;
  222. case Opt_nocto:
  223. nfs_data.flags |= NFS_MOUNT_NOCTO;
  224. break;
  225. case Opt_ac:
  226. nfs_data.flags &= ~NFS_MOUNT_NOAC;
  227. break;
  228. case Opt_noac:
  229. nfs_data.flags |= NFS_MOUNT_NOAC;
  230. break;
  231. case Opt_lock:
  232. nfs_data.flags &= ~NFS_MOUNT_NONLM;
  233. break;
  234. case Opt_nolock:
  235. nfs_data.flags |= NFS_MOUNT_NONLM;
  236. break;
  237. case Opt_v2:
  238. nfs_data.flags &= ~NFS_MOUNT_VER3;
  239. break;
  240. case Opt_v3:
  241. nfs_data.flags |= NFS_MOUNT_VER3;
  242. break;
  243. case Opt_udp:
  244. nfs_data.flags &= ~NFS_MOUNT_TCP;
  245. break;
  246. case Opt_tcp:
  247. nfs_data.flags |= NFS_MOUNT_TCP;
  248. break;
  249. case Opt_acl:
  250. nfs_data.flags &= ~NFS_MOUNT_NOACL;
  251. break;
  252. case Opt_noacl:
  253. nfs_data.flags |= NFS_MOUNT_NOACL;
  254. break;
  255. default:
  256. printk(KERN_WARNING "Root-NFS: unknown "
  257. "option: %s\n", p);
  258. return 0;
  259. }
  260. }
  261. return 1;
  262. }
  263. /*
  264. * Prepare the NFS data structure and parse all options.
  265. */
  266. static int __init root_nfs_name(char *name)
  267. {
  268. static char buf[NFS_MAXPATHLEN] __initdata;
  269. char *cp;
  270. /* Set some default values */
  271. memset(&nfs_data, 0, sizeof(nfs_data));
  272. nfs_port = -1;
  273. nfs_data.version = NFS_MOUNT_VERSION;
  274. nfs_data.flags = NFS_MOUNT_NONLM; /* No lockd in nfs root yet */
  275. nfs_data.rsize = NFS_DEF_FILE_IO_SIZE;
  276. nfs_data.wsize = NFS_DEF_FILE_IO_SIZE;
  277. nfs_data.acregmin = NFS_DEF_ACREGMIN;
  278. nfs_data.acregmax = NFS_DEF_ACREGMAX;
  279. nfs_data.acdirmin = NFS_DEF_ACDIRMIN;
  280. nfs_data.acdirmax = NFS_DEF_ACDIRMAX;
  281. strcpy(buf, NFS_ROOT);
  282. /* Process options received from the remote server */
  283. root_nfs_parse(root_server_path, buf);
  284. /* Override them by options set on kernel command-line */
  285. root_nfs_parse(name, buf);
  286. cp = utsname()->nodename;
  287. if (strlen(buf) + strlen(cp) > NFS_MAXPATHLEN) {
  288. printk(KERN_ERR "Root-NFS: Pathname for remote directory too long.\n");
  289. return -1;
  290. }
  291. sprintf(nfs_export_path, buf, cp);
  292. return 1;
  293. }
  294. /*
  295. * Get NFS server address.
  296. */
  297. static int __init root_nfs_addr(void)
  298. {
  299. if ((servaddr = root_server_addr) == htonl(INADDR_NONE)) {
  300. printk(KERN_ERR "Root-NFS: No NFS server available, giving up.\n");
  301. return -1;
  302. }
  303. snprintf(nfs_data.hostname, sizeof(nfs_data.hostname),
  304. "%pI4", &servaddr);
  305. return 0;
  306. }
  307. /*
  308. * Tell the user what's going on.
  309. */
  310. #ifdef NFSROOT_DEBUG
  311. static void __init root_nfs_print(void)
  312. {
  313. printk(KERN_NOTICE "Root-NFS: Mounting %s on server %s as root\n",
  314. nfs_export_path, nfs_data.hostname);
  315. printk(KERN_NOTICE "Root-NFS: rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
  316. nfs_data.rsize, nfs_data.wsize, nfs_data.timeo, nfs_data.retrans);
  317. printk(KERN_NOTICE "Root-NFS: acreg (min,max) = (%d,%d), acdir (min,max) = (%d,%d)\n",
  318. nfs_data.acregmin, nfs_data.acregmax,
  319. nfs_data.acdirmin, nfs_data.acdirmax);
  320. printk(KERN_NOTICE "Root-NFS: nfsd port = %d, mountd port = %d, flags = %08x\n",
  321. nfs_port, mount_port, nfs_data.flags);
  322. }
  323. #endif
  324. static int __init root_nfs_init(void)
  325. {
  326. #ifdef NFSROOT_DEBUG
  327. nfs_debug |= NFSDBG_ROOT;
  328. #endif
  329. /*
  330. * Decode the root directory path name and NFS options from
  331. * the kernel command line. This has to go here in order to
  332. * be able to use the client IP address for the remote root
  333. * directory (necessary for pure RARP booting).
  334. */
  335. if (root_nfs_name(nfs_root_name) < 0 ||
  336. root_nfs_addr() < 0)
  337. return -1;
  338. #ifdef NFSROOT_DEBUG
  339. root_nfs_print();
  340. #endif
  341. return 0;
  342. }
  343. /*
  344. * Parse NFS server and directory information passed on the kernel
  345. * command line.
  346. */
  347. static int __init nfs_root_setup(char *line)
  348. {
  349. ROOT_DEV = Root_NFS;
  350. if (line[0] == '/' || line[0] == ',' || (line[0] >= '0' && line[0] <= '9')) {
  351. strlcpy(nfs_root_name, line, sizeof(nfs_root_name));
  352. } else {
  353. int n = strlen(line) + sizeof(NFS_ROOT) - 1;
  354. if (n >= sizeof(nfs_root_name))
  355. line[sizeof(nfs_root_name) - sizeof(NFS_ROOT) - 2] = '\0';
  356. sprintf(nfs_root_name, NFS_ROOT, line);
  357. }
  358. root_server_addr = root_nfs_parse_addr(nfs_root_name);
  359. return 1;
  360. }
  361. __setup("nfsroot=", nfs_root_setup);
  362. /***************************************************************************
  363. Routines to actually mount the root directory
  364. ***************************************************************************/
  365. /*
  366. * Construct sockaddr_in from address and port number.
  367. */
  368. static inline void
  369. set_sockaddr(struct sockaddr_in *sin, __be32 addr, __be16 port)
  370. {
  371. sin->sin_family = AF_INET;
  372. sin->sin_addr.s_addr = addr;
  373. sin->sin_port = port;
  374. }
  375. /*
  376. * Query server portmapper for the port of a daemon program.
  377. */
  378. static int __init root_nfs_getport(int program, int version, int proto)
  379. {
  380. struct sockaddr_in sin;
  381. printk(KERN_NOTICE "Looking up port of RPC %d/%d on %pI4\n",
  382. program, version, &servaddr);
  383. set_sockaddr(&sin, servaddr, 0);
  384. return rpcb_getport_sync(&sin, program, version, proto);
  385. }
  386. /*
  387. * Use portmapper to find mountd and nfsd port numbers if not overriden
  388. * by the user. Use defaults if portmapper is not available.
  389. * XXX: Is there any nfs server with no portmapper?
  390. */
  391. static int __init root_nfs_ports(void)
  392. {
  393. int port;
  394. int nfsd_ver, mountd_ver;
  395. int nfsd_port, mountd_port;
  396. int proto;
  397. if (nfs_data.flags & NFS_MOUNT_VER3) {
  398. nfsd_ver = NFS3_VERSION;
  399. mountd_ver = NFS_MNT3_VERSION;
  400. nfsd_port = NFS_PORT;
  401. mountd_port = NFS_MNT_PORT;
  402. } else {
  403. nfsd_ver = NFS2_VERSION;
  404. mountd_ver = NFS_MNT_VERSION;
  405. nfsd_port = NFS_PORT;
  406. mountd_port = NFS_MNT_PORT;
  407. }
  408. proto = (nfs_data.flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
  409. if (nfs_port < 0) {
  410. if ((port = root_nfs_getport(NFS_PROGRAM, nfsd_ver, proto)) < 0) {
  411. printk(KERN_ERR "Root-NFS: Unable to get nfsd port "
  412. "number from server, using default\n");
  413. port = nfsd_port;
  414. }
  415. nfs_port = port;
  416. dprintk("Root-NFS: Portmapper on server returned %d "
  417. "as nfsd port\n", port);
  418. }
  419. if ((port = root_nfs_getport(NFS_MNT_PROGRAM, mountd_ver, proto)) < 0) {
  420. printk(KERN_ERR "Root-NFS: Unable to get mountd port "
  421. "number from server, using default\n");
  422. port = mountd_port;
  423. }
  424. mount_port = port;
  425. dprintk("Root-NFS: mountd port is %d\n", port);
  426. return 0;
  427. }
  428. /*
  429. * Get a file handle from the server for the directory which is to be
  430. * mounted.
  431. */
  432. static int __init root_nfs_get_handle(void)
  433. {
  434. struct nfs_fh fh;
  435. struct sockaddr_in sin;
  436. unsigned int auth_flav_len = 0;
  437. struct nfs_mount_request request = {
  438. .sap = (struct sockaddr *)&sin,
  439. .salen = sizeof(sin),
  440. .dirpath = nfs_export_path,
  441. .version = (nfs_data.flags & NFS_MOUNT_VER3) ?
  442. NFS_MNT3_VERSION : NFS_MNT_VERSION,
  443. .protocol = (nfs_data.flags & NFS_MOUNT_TCP) ?
  444. XPRT_TRANSPORT_TCP : XPRT_TRANSPORT_UDP,
  445. .fh = &fh,
  446. .auth_flav_len = &auth_flav_len,
  447. };
  448. int status;
  449. set_sockaddr(&sin, servaddr, htons(mount_port));
  450. status = nfs_mount(&request);
  451. if (status < 0)
  452. printk(KERN_ERR "Root-NFS: Server returned error %d "
  453. "while mounting %s\n", status, nfs_export_path);
  454. else {
  455. nfs_data.root.size = fh.size;
  456. memcpy(nfs_data.root.data, fh.data, fh.size);
  457. }
  458. return status;
  459. }
  460. /*
  461. * Get the NFS port numbers and file handle, and return the prepared 'data'
  462. * argument for mount() if everything went OK. Return NULL otherwise.
  463. */
  464. void * __init nfs_root_data(void)
  465. {
  466. if (root_nfs_init() < 0
  467. || root_nfs_ports() < 0
  468. || root_nfs_get_handle() < 0)
  469. return NULL;
  470. set_sockaddr((struct sockaddr_in *) &nfs_data.addr, servaddr, htons(nfs_port));
  471. return (void*)&nfs_data;
  472. }