nfsroot.c 14 KB

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