Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. #include "etherboot.h"
  2. #include <gpxe/init.h>
  3. #include "proto.h"
  4. #include <gpxe/in.h>
  5. #include "nic.h"
  6. /* NOTE: the NFS code is heavily inspired by the NetBSD netboot code (read:
  7. * large portions are copied verbatim) as distributed in OSKit 0.97. A few
  8. * changes were necessary to adapt the code to Etherboot and to fix several
  9. * inconsistencies. Also the RPC message preparation is done "by hand" to
  10. * avoid adding netsprintf() which I find hard to understand and use. */
  11. /* NOTE 2: Etherboot does not care about things beyond the kernel image, so
  12. * it loads the kernel image off the boot server (ARP_SERVER) and does not
  13. * access the client root disk (root-path in dhcpd.conf), which would use
  14. * ARP_ROOTSERVER. The root disk is something the operating system we are
  15. * about to load needs to use. This is different from the OSKit 0.97 logic. */
  16. /* NOTE 3: Symlink handling introduced by Anselm M Hoffmeister, 2003-July-14
  17. * If a symlink is encountered, it is followed as far as possible (recursion
  18. * possible, maximum 16 steps). There is no clearing of ".."'s inside the
  19. * path, so please DON'T DO THAT. thx. */
  20. #define START_OPORT 700 /* mountd usually insists on secure ports */
  21. #define OPORT_SWEEP 200 /* make sure we don't leave secure range */
  22. static int oport = START_OPORT;
  23. static struct sockaddr_in mount_server;
  24. static struct sockaddr_in nfs_server;
  25. static unsigned long rpc_id;
  26. /**************************************************************************
  27. RPC_INIT - set up the ID counter to something fairly random
  28. **************************************************************************/
  29. static void rpc_init(void)
  30. {
  31. unsigned long t;
  32. t = currticks();
  33. rpc_id = t ^ (t << 8) ^ (t << 16);
  34. }
  35. /**************************************************************************
  36. RPC_PRINTERROR - Print a low level RPC error message
  37. **************************************************************************/
  38. static void rpc_printerror(struct rpc_t *rpc)
  39. {
  40. if (rpc->u.reply.rstatus || rpc->u.reply.verifier ||
  41. rpc->u.reply.astatus) {
  42. /* rpc_printerror() is called for any RPC related error,
  43. * suppress output if no low level RPC error happened. */
  44. DBG("RPC error: (%d,%d,%d)\n", ntohl(rpc->u.reply.rstatus),
  45. ntohl(rpc->u.reply.verifier),
  46. ntohl(rpc->u.reply.astatus));
  47. }
  48. }
  49. /**************************************************************************
  50. AWAIT_RPC - Wait for an rpc packet
  51. **************************************************************************/
  52. static int await_rpc(int ival, void *ptr,
  53. unsigned short ptype __unused, struct iphdr *ip,
  54. struct udphdr *udp, struct tcphdr *tcp __unused)
  55. {
  56. struct rpc_t *rpc;
  57. if (!udp)
  58. return 0;
  59. if (arptable[ARP_CLIENT].ipaddr.s_addr != ip->dest.s_addr)
  60. return 0;
  61. if (ntohs(udp->dest) != ival)
  62. return 0;
  63. if (nic.packetlen < ETH_HLEN + sizeof(struct iphdr) + sizeof(struct udphdr) + 8)
  64. return 0;
  65. rpc = (struct rpc_t *)&nic.packet[ETH_HLEN];
  66. if (*(unsigned long *)ptr != ntohl(rpc->u.reply.id))
  67. return 0;
  68. if (MSG_REPLY != ntohl(rpc->u.reply.type))
  69. return 0;
  70. return 1;
  71. }
  72. /**************************************************************************
  73. RPC_LOOKUP - Lookup RPC Port numbers
  74. **************************************************************************/
  75. static int rpc_lookup(struct sockaddr_in *addr, int prog, int ver, int sport)
  76. {
  77. struct rpc_t buf, *rpc;
  78. unsigned long id;
  79. int retries;
  80. long *p;
  81. id = rpc_id++;
  82. buf.u.call.id = htonl(id);
  83. buf.u.call.type = htonl(MSG_CALL);
  84. buf.u.call.rpcvers = htonl(2); /* use RPC version 2 */
  85. buf.u.call.prog = htonl(PROG_PORTMAP);
  86. buf.u.call.vers = htonl(2); /* portmapper is version 2 */
  87. buf.u.call.proc = htonl(PORTMAP_GETPORT);
  88. p = (long *)buf.u.call.data;
  89. *p++ = 0; *p++ = 0; /* auth credential */
  90. *p++ = 0; *p++ = 0; /* auth verifier */
  91. *p++ = htonl(prog);
  92. *p++ = htonl(ver);
  93. *p++ = htonl(IP_UDP);
  94. *p++ = 0;
  95. for (retries = 0; retries < MAX_RPC_RETRIES; retries++) {
  96. long timeout;
  97. udp_transmit(addr->sin_addr.s_addr, sport, addr->sin_port,
  98. (char *)p - (char *)&buf, &buf);
  99. timeout = rfc2131_sleep_interval(TIMEOUT, retries);
  100. if (await_reply(await_rpc, sport, &id, timeout)) {
  101. rpc = (struct rpc_t *)&nic.packet[ETH_HLEN];
  102. if (rpc->u.reply.rstatus || rpc->u.reply.verifier ||
  103. rpc->u.reply.astatus) {
  104. rpc_printerror(rpc);
  105. return 0;
  106. } else {
  107. return ntohl(rpc->u.reply.data[0]);
  108. }
  109. }
  110. }
  111. return 0;
  112. }
  113. /**************************************************************************
  114. RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
  115. **************************************************************************/
  116. static long *rpc_add_credentials(long *p)
  117. {
  118. int hl;
  119. /* Here's the executive summary on authentication requirements of the
  120. * various NFS server implementations: Linux accepts both AUTH_NONE
  121. * and AUTH_UNIX authentication (also accepts an empty hostname field
  122. * in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
  123. * AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
  124. * scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
  125. * it (if the BOOTP/DHCP reply didn't give one, just use an empty
  126. * hostname). */
  127. hl = (hostnamelen + 3) & ~3;
  128. /* Provide an AUTH_UNIX credential. */
  129. *p++ = htonl(1); /* AUTH_UNIX */
  130. *p++ = htonl(hl+20); /* auth length */
  131. *p++ = htonl(0); /* stamp */
  132. *p++ = htonl(hostnamelen); /* hostname string */
  133. if (hostnamelen & 3) {
  134. *(p + hostnamelen / 4) = 0; /* add zero padding */
  135. }
  136. memcpy(p, hostname, hostnamelen);
  137. p += hl / 4;
  138. *p++ = 0; /* uid */
  139. *p++ = 0; /* gid */
  140. *p++ = 0; /* auxiliary gid list */
  141. /* Provide an AUTH_NONE verifier. */
  142. *p++ = 0; /* AUTH_NONE */
  143. *p++ = 0; /* auth length */
  144. return p;
  145. }
  146. /**************************************************************************
  147. NFS_PRINTERROR - Print a NFS error message
  148. **************************************************************************/
  149. static void nfs_printerror(int err)
  150. {
  151. switch (-err) {
  152. case NFSERR_PERM:
  153. printf("Not owner\n");
  154. break;
  155. case NFSERR_NOENT:
  156. printf("No such file or directory\n");
  157. break;
  158. case NFSERR_ACCES:
  159. printf("Permission denied\n");
  160. break;
  161. case NFSERR_ISDIR:
  162. printf("Directory given where filename expected\n");
  163. break;
  164. case NFSERR_INVAL:
  165. printf("Invalid filehandle\n");
  166. break; // INVAL is not defined in NFSv2, some NFS-servers
  167. // seem to use it in answers to v2 nevertheless.
  168. case 9998:
  169. printf("low-level RPC failure (parameter decoding problem?)\n");
  170. break;
  171. case 9999:
  172. printf("low-level RPC failure (authentication problem?)\n");
  173. break;
  174. default:
  175. printf("Unknown NFS error %d\n", -err);
  176. }
  177. }
  178. /**************************************************************************
  179. NFS_MOUNT - Mount an NFS Filesystem
  180. **************************************************************************/
  181. static int nfs_mount(struct sockaddr_in *server, char *path, char *fh, int sport)
  182. {
  183. struct rpc_t buf, *rpc;
  184. unsigned long id;
  185. int retries;
  186. long *p;
  187. int pathlen = strlen(path);
  188. id = rpc_id++;
  189. buf.u.call.id = htonl(id);
  190. buf.u.call.type = htonl(MSG_CALL);
  191. buf.u.call.rpcvers = htonl(2); /* use RPC version 2 */
  192. buf.u.call.prog = htonl(PROG_MOUNT);
  193. buf.u.call.vers = htonl(1); /* mountd is version 1 */
  194. buf.u.call.proc = htonl(MOUNT_ADDENTRY);
  195. p = rpc_add_credentials((long *)buf.u.call.data);
  196. *p++ = htonl(pathlen);
  197. if (pathlen & 3) {
  198. *(p + pathlen / 4) = 0; /* add zero padding */
  199. }
  200. memcpy(p, path, pathlen);
  201. p += (pathlen + 3) / 4;
  202. for (retries = 0; retries < MAX_RPC_RETRIES; retries++) {
  203. long timeout;
  204. udp_transmit(server->sin_addr.s_addr, sport, server->sin_port,
  205. (char *)p - (char *)&buf, &buf);
  206. timeout = rfc2131_sleep_interval(TIMEOUT, retries);
  207. if (await_reply(await_rpc, sport, &id, timeout)) {
  208. rpc = (struct rpc_t *)&nic.packet[ETH_HLEN];
  209. if (rpc->u.reply.rstatus || rpc->u.reply.verifier ||
  210. rpc->u.reply.astatus || rpc->u.reply.data[0]) {
  211. rpc_printerror(rpc);
  212. if (rpc->u.reply.rstatus) {
  213. /* RPC failed, no verifier, data[0] */
  214. return -9999;
  215. }
  216. if (rpc->u.reply.astatus) {
  217. /* RPC couldn't decode parameters */
  218. return -9998;
  219. }
  220. return -ntohl(rpc->u.reply.data[0]);
  221. } else {
  222. memcpy(fh, rpc->u.reply.data + 1, NFS_FHSIZE);
  223. return 0;
  224. }
  225. }
  226. }
  227. return -1;
  228. }
  229. /**************************************************************************
  230. NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
  231. **************************************************************************/
  232. static void nfs_umountall(struct sockaddr_in *server)
  233. {
  234. struct rpc_t buf, *rpc;
  235. unsigned long id;
  236. int retries;
  237. long *p;
  238. id = rpc_id++;
  239. buf.u.call.id = htonl(id);
  240. buf.u.call.type = htonl(MSG_CALL);
  241. buf.u.call.rpcvers = htonl(2); /* use RPC version 2 */
  242. buf.u.call.prog = htonl(PROG_MOUNT);
  243. buf.u.call.vers = htonl(1); /* mountd is version 1 */
  244. buf.u.call.proc = htonl(MOUNT_UMOUNTALL);
  245. p = rpc_add_credentials((long *)buf.u.call.data);
  246. for (retries = 0; retries < MAX_RPC_RETRIES; retries++) {
  247. long timeout = rfc2131_sleep_interval(TIMEOUT, retries);
  248. udp_transmit(server->sin_addr.s_addr, oport, server->sin_port,
  249. (char *)p - (char *)&buf, &buf);
  250. if (await_reply(await_rpc, oport, &id, timeout)) {
  251. rpc = (struct rpc_t *)&nic.packet[ETH_HLEN];
  252. if (rpc->u.reply.rstatus || rpc->u.reply.verifier ||
  253. rpc->u.reply.astatus) {
  254. rpc_printerror(rpc);
  255. }
  256. break;
  257. }
  258. }
  259. }
  260. /**************************************************************************
  261. NFS_RESET - Reset the NFS subsystem
  262. **************************************************************************/
  263. static void nfs_reset ( void ) {
  264. /* If we have a mount server, call nfs_umountall() */
  265. if ( mount_server.sin_addr.s_addr ) {
  266. nfs_umountall ( &mount_server );
  267. }
  268. /* Zero the data structures */
  269. memset ( &mount_server, 0, sizeof ( mount_server ) );
  270. memset ( &nfs_server, 0, sizeof ( nfs_server ) );
  271. }
  272. /***************************************************************************
  273. * NFS_READLINK (AH 2003-07-14)
  274. * This procedure is called when read of the first block fails -
  275. * this probably happens when it's a directory or a symlink
  276. * In case of successful readlink(), the dirname is manipulated,
  277. * so that inside the nfs() function a recursion can be done.
  278. **************************************************************************/
  279. static int nfs_readlink(struct sockaddr_in *server, char *fh __unused,
  280. char *path, char *nfh, int sport)
  281. {
  282. struct rpc_t buf, *rpc;
  283. unsigned long id;
  284. long *p;
  285. int retries;
  286. int pathlen = strlen(path);
  287. id = rpc_id++;
  288. buf.u.call.id = htonl(id);
  289. buf.u.call.type = htonl(MSG_CALL);
  290. buf.u.call.rpcvers = htonl(2); /* use RPC version 2 */
  291. buf.u.call.prog = htonl(PROG_NFS);
  292. buf.u.call.vers = htonl(2); /* nfsd is version 2 */
  293. buf.u.call.proc = htonl(NFS_READLINK);
  294. p = rpc_add_credentials((long *)buf.u.call.data);
  295. memcpy(p, nfh, NFS_FHSIZE);
  296. p += (NFS_FHSIZE / 4);
  297. for (retries = 0; retries < MAX_RPC_RETRIES; retries++) {
  298. long timeout = rfc2131_sleep_interval(TIMEOUT, retries);
  299. udp_transmit(server->sin_addr.s_addr, sport, server->sin_port,
  300. (char *)p - (char *)&buf, &buf);
  301. if (await_reply(await_rpc, sport, &id, timeout)) {
  302. rpc = (struct rpc_t *)&nic.packet[ETH_HLEN];
  303. if (rpc->u.reply.rstatus || rpc->u.reply.verifier ||
  304. rpc->u.reply.astatus || rpc->u.reply.data[0]) {
  305. rpc_printerror(rpc);
  306. if (rpc->u.reply.rstatus) {
  307. /* RPC failed, no verifier, data[0] */
  308. return -9999;
  309. }
  310. if (rpc->u.reply.astatus) {
  311. /* RPC couldn't decode parameters */
  312. return -9998;
  313. }
  314. return -ntohl(rpc->u.reply.data[0]);
  315. } else {
  316. // It *is* a link.
  317. // If it's a relative link, append everything to dirname, filename TOO!
  318. retries = strlen ( (char *)(&(rpc->u.reply.data[2]) ));
  319. if ( *((char *)(&(rpc->u.reply.data[2]))) != '/' ) {
  320. path[pathlen++] = '/';
  321. while ( ( retries + pathlen ) > 298 ) {
  322. retries--;
  323. }
  324. if ( retries > 0 ) {
  325. memcpy(path + pathlen, &(rpc->u.reply.data[2]), retries + 1);
  326. } else { retries = 0; }
  327. path[pathlen + retries] = 0;
  328. } else {
  329. // Else make it the only path.
  330. if ( retries > 298 ) { retries = 298; }
  331. memcpy ( path, &(rpc->u.reply.data[2]), retries + 1 );
  332. path[retries] = 0;
  333. }
  334. return 0;
  335. }
  336. }
  337. }
  338. return -1;
  339. }
  340. /**************************************************************************
  341. NFS_LOOKUP - Lookup Pathname
  342. **************************************************************************/
  343. static int nfs_lookup(struct sockaddr_in *server, char *fh, char *path, char *nfh,
  344. int sport)
  345. {
  346. struct rpc_t buf, *rpc;
  347. unsigned long id;
  348. long *p;
  349. int retries;
  350. int pathlen = strlen(path);
  351. id = rpc_id++;
  352. buf.u.call.id = htonl(id);
  353. buf.u.call.type = htonl(MSG_CALL);
  354. buf.u.call.rpcvers = htonl(2); /* use RPC version 2 */
  355. buf.u.call.prog = htonl(PROG_NFS);
  356. buf.u.call.vers = htonl(2); /* nfsd is version 2 */
  357. buf.u.call.proc = htonl(NFS_LOOKUP);
  358. p = rpc_add_credentials((long *)buf.u.call.data);
  359. memcpy(p, fh, NFS_FHSIZE);
  360. p += (NFS_FHSIZE / 4);
  361. *p++ = htonl(pathlen);
  362. if (pathlen & 3) {
  363. *(p + pathlen / 4) = 0; /* add zero padding */
  364. }
  365. memcpy(p, path, pathlen);
  366. p += (pathlen + 3) / 4;
  367. for (retries = 0; retries < MAX_RPC_RETRIES; retries++) {
  368. long timeout = rfc2131_sleep_interval(TIMEOUT, retries);
  369. udp_transmit(server->sin_addr.s_addr, sport, server->sin_port,
  370. (char *)p - (char *)&buf, &buf);
  371. if (await_reply(await_rpc, sport, &id, timeout)) {
  372. rpc = (struct rpc_t *)&nic.packet[ETH_HLEN];
  373. if (rpc->u.reply.rstatus || rpc->u.reply.verifier ||
  374. rpc->u.reply.astatus || rpc->u.reply.data[0]) {
  375. rpc_printerror(rpc);
  376. if (rpc->u.reply.rstatus) {
  377. /* RPC failed, no verifier, data[0] */
  378. return -9999;
  379. }
  380. if (rpc->u.reply.astatus) {
  381. /* RPC couldn't decode parameters */
  382. return -9998;
  383. }
  384. return -ntohl(rpc->u.reply.data[0]);
  385. } else {
  386. memcpy(nfh, rpc->u.reply.data + 1, NFS_FHSIZE);
  387. return 0;
  388. }
  389. }
  390. }
  391. return -1;
  392. }
  393. /**************************************************************************
  394. NFS_READ - Read File on NFS Server
  395. **************************************************************************/
  396. static int nfs_read(struct sockaddr_in *server, char *fh, int offset, int len,
  397. int sport)
  398. {
  399. struct rpc_t buf, *rpc;
  400. unsigned long id;
  401. int retries;
  402. long *p;
  403. static int tokens=0;
  404. /*
  405. * Try to implement something similar to a window protocol in
  406. * terms of response to losses. On successful receive, increment
  407. * the number of tokens by 1 (cap at 256). On failure, halve it.
  408. * When the number of tokens is >= 2, use a very short timeout.
  409. */
  410. id = rpc_id++;
  411. buf.u.call.id = htonl(id);
  412. buf.u.call.type = htonl(MSG_CALL);
  413. buf.u.call.rpcvers = htonl(2); /* use RPC version 2 */
  414. buf.u.call.prog = htonl(PROG_NFS);
  415. buf.u.call.vers = htonl(2); /* nfsd is version 2 */
  416. buf.u.call.proc = htonl(NFS_READ);
  417. p = rpc_add_credentials((long *)buf.u.call.data);
  418. memcpy(p, fh, NFS_FHSIZE);
  419. p += NFS_FHSIZE / 4;
  420. *p++ = htonl(offset);
  421. *p++ = htonl(len);
  422. *p++ = 0; /* unused parameter */
  423. for (retries = 0; retries < MAX_RPC_RETRIES; retries++) {
  424. long timeout = rfc2131_sleep_interval(TIMEOUT, retries);
  425. if (tokens >= 2)
  426. timeout = TICKS_PER_SEC/2;
  427. udp_transmit(server->sin_addr.s_addr, sport, server->sin_port,
  428. (char *)p - (char *)&buf, &buf);
  429. if (await_reply(await_rpc, sport, &id, timeout)) {
  430. if (tokens < 256)
  431. tokens++;
  432. rpc = (struct rpc_t *)&nic.packet[ETH_HLEN];
  433. if (rpc->u.reply.rstatus || rpc->u.reply.verifier ||
  434. rpc->u.reply.astatus || rpc->u.reply.data[0]) {
  435. rpc_printerror(rpc);
  436. if (rpc->u.reply.rstatus) {
  437. /* RPC failed, no verifier, data[0] */
  438. return -9999;
  439. }
  440. if (rpc->u.reply.astatus) {
  441. /* RPC couldn't decode parameters */
  442. return -9998;
  443. }
  444. return -ntohl(rpc->u.reply.data[0]);
  445. } else {
  446. return 0;
  447. }
  448. } else
  449. tokens >>= 1;
  450. }
  451. return -1;
  452. }
  453. /**************************************************************************
  454. NFS - Download extended BOOTP data, or kernel image from NFS server
  455. **************************************************************************/
  456. static int nfs ( char *url __unused, struct sockaddr_in *server,
  457. char *name, struct buffer *buffer ) {
  458. static int recursion = 0;
  459. int sport;
  460. int err, namelen = strlen(name);
  461. char dirname[300], *fname;
  462. char dirfh[NFS_FHSIZE]; /* file handle of directory */
  463. char filefh[NFS_FHSIZE]; /* file handle of kernel image */
  464. int rlen, size, offs, len;
  465. struct rpc_t *rpc;
  466. sport = oport++;
  467. if (oport > START_OPORT+OPORT_SWEEP) {
  468. oport = START_OPORT;
  469. }
  470. mount_server.sin_addr = nfs_server.sin_addr = server->sin_addr;
  471. mount_server.sin_port = rpc_lookup(server, PROG_MOUNT, 1, sport);
  472. if ( ! mount_server.sin_port ) {
  473. DBG ( "Cannot get mount port from %!:%d\n",
  474. server->sin_addr.s_addr, server->sin_port );
  475. return 0;
  476. }
  477. nfs_server.sin_port = rpc_lookup(server, PROG_NFS, 2, sport);
  478. if ( ! mount_server.sin_port ) {
  479. DBG ( "Cannot get nfs port from %!:%d\n",
  480. server->sin_addr.s_addr, server->sin_port );
  481. return 0;
  482. }
  483. if ( name != dirname ) {
  484. memcpy(dirname, name, namelen + 1);
  485. }
  486. recursion = 0;
  487. nfssymlink:
  488. if ( recursion > NFS_MAXLINKDEPTH ) {
  489. DBG ( "\nRecursion: More than %d symlinks followed. Abort.\n",
  490. NFS_MAXLINKDEPTH );
  491. return 0;
  492. }
  493. recursion++;
  494. fname = dirname + (namelen - 1);
  495. while (fname >= dirname) {
  496. if (*fname == '/') {
  497. *fname = '\0';
  498. fname++;
  499. break;
  500. }
  501. fname--;
  502. }
  503. if (fname < dirname) {
  504. DBG("can't parse file name %s\n", name);
  505. return 0;
  506. }
  507. err = nfs_mount(&mount_server, dirname, dirfh, sport);
  508. if (err) {
  509. DBG("mounting %s: ", dirname);
  510. nfs_printerror(err);
  511. /* just to be sure... */
  512. nfs_reset();
  513. return 0;
  514. }
  515. err = nfs_lookup(&nfs_server, dirfh, fname, filefh, sport);
  516. if (err) {
  517. DBG("looking up %s: ", fname);
  518. nfs_printerror(err);
  519. nfs_reset();
  520. return 0;
  521. }
  522. offs = 0;
  523. size = -1; /* will be set properly with the first reply */
  524. len = NFS_READ_SIZE; /* first request is always full size */
  525. do {
  526. err = nfs_read(&nfs_server, filefh, offs, len, sport);
  527. if ((err <= -NFSERR_ISDIR)&&(err >= -NFSERR_INVAL) && (offs == 0)) {
  528. // An error occured. NFS servers tend to sending
  529. // errors 21 / 22 when symlink instead of real file
  530. // is requested. So check if it's a symlink!
  531. if ( nfs_readlink(&nfs_server, dirfh, dirname,
  532. filefh, sport) == 0 ) {
  533. printf("\nLoading symlink:%s ..",dirname);
  534. goto nfssymlink;
  535. }
  536. nfs_printerror(err);
  537. nfs_reset();
  538. return 0;
  539. }
  540. if (err) {
  541. printf("\nError reading at offset %d: ", offs);
  542. nfs_printerror(err);
  543. nfs_reset();
  544. return 0;
  545. }
  546. rpc = (struct rpc_t *)&nic.packet[ETH_HLEN];
  547. /* size must be found out early to allow EOF detection */
  548. if (size == -1) {
  549. size = ntohl(rpc->u.reply.data[6]);
  550. }
  551. rlen = ntohl(rpc->u.reply.data[18]);
  552. if (rlen > len) {
  553. rlen = len; /* shouldn't happen... */
  554. }
  555. if ( ! fill_buffer ( buffer, &rpc->u.reply.data[19],
  556. offs, rlen ) ) {
  557. nfs_reset();
  558. return 0;
  559. }
  560. offs += rlen;
  561. /* last request is done with matching requested read size */
  562. if (size-offs < NFS_READ_SIZE) {
  563. len = size-offs;
  564. }
  565. } while (len != 0);
  566. /* len == 0 means that all the file has been read */
  567. return 1;
  568. }
  569. INIT_FN ( INIT_RPC, rpc_init, nfs_reset, nfs_reset );
  570. struct protocol nfs_protocol __protocol = {
  571. .name = "nfs",
  572. .default_port = SUNRPC_PORT,
  573. .load = nfs,
  574. };