You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nfs.c 19KB

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