nfsroot.c 14 KB

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