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_open.c 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * Copyright (C) 2013 Marin Hannache <ipxe@mareo.fr>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. #include <errno.h>
  25. #include <libgen.h>
  26. #include <byteswap.h>
  27. #include <ipxe/time.h>
  28. #include <ipxe/socket.h>
  29. #include <ipxe/tcpip.h>
  30. #include <ipxe/in.h>
  31. #include <ipxe/iobuf.h>
  32. #include <ipxe/xfer.h>
  33. #include <ipxe/open.h>
  34. #include <ipxe/uri.h>
  35. #include <ipxe/features.h>
  36. #include <ipxe/nfs.h>
  37. #include <ipxe/nfs_open.h>
  38. #include <ipxe/oncrpc.h>
  39. #include <ipxe/oncrpc_iob.h>
  40. #include <ipxe/portmap.h>
  41. #include <ipxe/mount.h>
  42. /** @file
  43. *
  44. * Network File System protocol
  45. *
  46. */
  47. FEATURE ( FEATURE_PROTOCOL, "NFS", DHCP_EB_FEATURE_NFS, 1 );
  48. #define NFS_RSIZE 100000
  49. enum nfs_pm_state {
  50. NFS_PORTMAP_NONE = 0,
  51. NFS_PORTMAP_MOUNTPORT,
  52. NFS_PORTMAP_NFSPORT,
  53. MFS_PORTMAP_CLOSED,
  54. };
  55. enum nfs_mount_state {
  56. NFS_MOUNT_NONE = 0,
  57. NFS_MOUNT_MNT,
  58. NFS_MOUNT_UMNT,
  59. NFS_MOUNT_CLOSED,
  60. };
  61. enum nfs_state {
  62. NFS_NONE = 0,
  63. NFS_LOOKUP,
  64. NFS_LOOKUP_SENT,
  65. NFS_READLINK,
  66. NFS_READLINK_SENT,
  67. NFS_READ,
  68. NFS_READ_SENT,
  69. NFS_CLOSED,
  70. };
  71. /**
  72. * A NFS request
  73. *
  74. */
  75. struct nfs_request {
  76. /** Reference counter */
  77. struct refcnt refcnt;
  78. /** Data transfer interface */
  79. struct interface xfer;
  80. struct interface pm_intf;
  81. struct interface mount_intf;
  82. struct interface nfs_intf;
  83. enum nfs_pm_state pm_state;
  84. enum nfs_mount_state mount_state;
  85. enum nfs_state nfs_state;
  86. struct oncrpc_session pm_session;
  87. struct oncrpc_session mount_session;
  88. struct oncrpc_session nfs_session;
  89. struct oncrpc_cred_sys auth_sys;
  90. char * hostname;
  91. char * path;
  92. char * mountpoint;
  93. char * filename;
  94. size_t filename_offset;
  95. struct nfs_fh readlink_fh;
  96. struct nfs_fh current_fh;
  97. uint64_t file_offset;
  98. size_t remaining;
  99. int eof;
  100. };
  101. static void nfs_step ( struct nfs_request *nfs );
  102. /**
  103. * Free NFS request
  104. *
  105. * @v refcnt Reference counter
  106. */
  107. static void nfs_free ( struct refcnt *refcnt ) {
  108. struct nfs_request *nfs;
  109. nfs = container_of ( refcnt, struct nfs_request, refcnt );
  110. DBGC ( nfs, "NFS_OPEN %p freed\n", nfs );
  111. free ( nfs->hostname );
  112. free ( nfs->path );
  113. free ( nfs->auth_sys.hostname );
  114. free ( nfs );
  115. }
  116. /**
  117. * Mark NFS operation as complete
  118. *
  119. * @v nfs NFS request
  120. * @v rc Return status code
  121. */
  122. static void nfs_done ( struct nfs_request *nfs, int rc ) {
  123. if ( rc == 0 && nfs->nfs_state != NFS_CLOSED )
  124. rc = -ECONNRESET;
  125. DBGC ( nfs, "NFS_OPEN %p completed (%s)\n", nfs, strerror ( rc ) );
  126. intf_shutdown ( &nfs->xfer, rc );
  127. intf_shutdown ( &nfs->pm_intf, rc );
  128. intf_shutdown ( &nfs->mount_intf, rc );
  129. intf_shutdown ( &nfs->nfs_intf, rc );
  130. }
  131. static int nfs_connect ( struct interface *intf, uint16_t port,
  132. const char *hostname ) {
  133. struct sockaddr_tcpip peer;
  134. struct sockaddr_tcpip local;
  135. if ( ! intf || ! hostname || ! port )
  136. return -EINVAL;
  137. memset ( &peer, 0, sizeof ( peer ) );
  138. memset ( &local, 0, sizeof ( local ) );
  139. peer.st_port = htons ( port );
  140. /* Use a local port < 1024 to avoid using the 'insecure' option in
  141. * /etc/exports file. */
  142. local.st_flags = TCPIP_BIND_PRIVILEGED;
  143. return xfer_open_named_socket ( intf, SOCK_STREAM,
  144. ( struct sockaddr * ) &peer, hostname,
  145. ( struct sockaddr * ) &local );
  146. }
  147. static void nfs_pm_step ( struct nfs_request *nfs ) {
  148. int rc;
  149. if ( ! xfer_window ( &nfs->pm_intf ) )
  150. return;
  151. if ( nfs->pm_state == NFS_PORTMAP_NONE ) {
  152. DBGC ( nfs, "NFS_OPEN %p GETPORT call (mount)\n", nfs );
  153. rc = portmap_getport ( &nfs->pm_intf, &nfs->pm_session,
  154. ONCRPC_MOUNT, MOUNT_VERS,
  155. PORTMAP_PROTO_TCP );
  156. if ( rc != 0 )
  157. goto err;
  158. nfs->pm_state++;
  159. return;
  160. }
  161. if ( nfs->pm_state == NFS_PORTMAP_NFSPORT ) {
  162. DBGC ( nfs, "NFS_OPEN %p GETPORT call (nfs)\n", nfs );
  163. rc = portmap_getport ( &nfs->pm_intf, &nfs->pm_session,
  164. ONCRPC_NFS, NFS_VERS,
  165. PORTMAP_PROTO_TCP );
  166. if ( rc != 0 )
  167. goto err;
  168. return;
  169. }
  170. return;
  171. err:
  172. nfs_done ( nfs, rc );
  173. }
  174. static int nfs_pm_deliver ( struct nfs_request *nfs,
  175. struct io_buffer *io_buf,
  176. struct xfer_metadata *meta __unused ) {
  177. int rc;
  178. struct oncrpc_reply reply;
  179. struct portmap_getport_reply getport_reply;
  180. oncrpc_get_reply ( &nfs->pm_session, &reply, io_buf );
  181. if ( reply.accept_state != 0 )
  182. {
  183. rc = -EPROTO;
  184. goto err;
  185. }
  186. if ( nfs->pm_state == NFS_PORTMAP_MOUNTPORT ) {
  187. DBGC ( nfs, "NFS_OPEN %p got GETPORT reply (mount)\n", nfs );
  188. rc = portmap_get_getport_reply ( &getport_reply, &reply );
  189. if ( rc != 0 )
  190. goto err;
  191. rc = nfs_connect ( &nfs->mount_intf, getport_reply.port,
  192. nfs->hostname );
  193. if ( rc != 0 )
  194. goto err;
  195. nfs->pm_state++;
  196. nfs_pm_step ( nfs );
  197. goto done;
  198. }
  199. if ( nfs->pm_state == NFS_PORTMAP_NFSPORT ) {
  200. DBGC ( nfs, "NFS_OPEN %p got GETPORT reply (nfs)\n", nfs );
  201. rc = portmap_get_getport_reply ( &getport_reply, &reply );
  202. if ( rc != 0 )
  203. goto err;
  204. rc = nfs_connect ( &nfs->nfs_intf, getport_reply.port,
  205. nfs->hostname );
  206. if ( rc != 0 )
  207. goto err;
  208. intf_shutdown ( &nfs->pm_intf, 0 );
  209. nfs->pm_state++;
  210. goto done;
  211. }
  212. rc = -EPROTO;
  213. err:
  214. nfs_done ( nfs, rc );
  215. done:
  216. free_iob ( io_buf );
  217. return 0;
  218. }
  219. static void nfs_mount_step ( struct nfs_request *nfs ) {
  220. int rc;
  221. if ( ! xfer_window ( &nfs->mount_intf ) )
  222. return;
  223. if ( nfs->mount_state == NFS_MOUNT_NONE ) {
  224. DBGC ( nfs, "NFS_OPEN %p MNT call (%s)\n", nfs,
  225. nfs->mountpoint );
  226. rc = mount_mnt ( &nfs->mount_intf, &nfs->mount_session,
  227. nfs->mountpoint );
  228. if ( rc != 0 )
  229. goto err;
  230. nfs->mount_state++;
  231. return;
  232. }
  233. if ( nfs->mount_state == NFS_MOUNT_UMNT ) {
  234. DBGC ( nfs, "NFS_OPEN %p UMNT call\n", nfs );
  235. rc = mount_umnt ( &nfs->mount_intf, &nfs->mount_session,
  236. nfs->mountpoint );
  237. if ( rc != 0 )
  238. goto err;
  239. }
  240. return;
  241. err:
  242. nfs_done ( nfs, rc );
  243. }
  244. static int nfs_mount_deliver ( struct nfs_request *nfs,
  245. struct io_buffer *io_buf,
  246. struct xfer_metadata *meta __unused ) {
  247. int rc;
  248. char *sep;
  249. struct oncrpc_reply reply;
  250. struct mount_mnt_reply mnt_reply;
  251. oncrpc_get_reply ( &nfs->mount_session, &reply, io_buf );
  252. if ( reply.accept_state != 0 )
  253. {
  254. rc = -EPROTO;
  255. goto err;
  256. }
  257. if ( nfs->mount_state == NFS_MOUNT_MNT ) {
  258. DBGC ( nfs, "NFS_OPEN %p got MNT reply\n", nfs );
  259. rc = mount_get_mnt_reply ( &mnt_reply, &reply );
  260. if ( rc != 0 ) {
  261. if ( mnt_reply.status != MNT3ERR_NOTDIR )
  262. goto err;
  263. if ( strcmp ( nfs->mountpoint, "/" ) == 0 )
  264. goto err;
  265. sep = strrchr ( nfs->mountpoint, '/' );
  266. nfs->filename[-1] = '/';
  267. nfs->filename = sep + 1;
  268. *sep = '\0';
  269. DBGC ( nfs, "NFS_OPEN %p ENOTDIR received retrying" \
  270. "with %s\n", nfs, nfs->mountpoint );
  271. goto done;
  272. }
  273. nfs->current_fh = mnt_reply.fh;
  274. nfs->nfs_state = NFS_LOOKUP;
  275. nfs_step ( nfs );
  276. goto done;
  277. }
  278. if ( nfs->mount_state == NFS_MOUNT_UMNT ) {
  279. DBGC ( nfs, "NFS_OPEN %p got UMNT reply\n", nfs );
  280. nfs_done ( nfs, 0 );
  281. goto done;
  282. }
  283. rc = -EPROTO;
  284. err:
  285. nfs_done ( nfs, rc );
  286. done:
  287. free_iob ( io_buf );
  288. return 0;
  289. }
  290. static void nfs_step ( struct nfs_request *nfs ) {
  291. int rc;
  292. char *path_component = NULL;
  293. if ( ! xfer_window ( &nfs->nfs_intf ) )
  294. return;
  295. if ( nfs->nfs_state == NFS_LOOKUP ) {
  296. while ( path_component == NULL || path_component[0] == '\0') {
  297. path_component = nfs->filename;
  298. while ( *nfs->filename != '\0' ) {
  299. nfs->filename_offset++;
  300. if ( *nfs->filename++ == '/' ) {
  301. *(nfs->filename - 1) = '\0';
  302. break;
  303. }
  304. }
  305. }
  306. DBGC ( nfs, "NFS_OPEN %p LOOKUP call (%s)\n", nfs,
  307. path_component );
  308. rc = nfs_lookup ( &nfs->nfs_intf, &nfs->nfs_session,
  309. &nfs->current_fh, path_component );
  310. if ( rc != 0 )
  311. goto err;
  312. nfs->nfs_state++;
  313. return;
  314. }
  315. if ( nfs->nfs_state == NFS_READLINK ) {
  316. DBGC ( nfs, "NFS_OPEN %p READLINK call\n", nfs );
  317. rc = nfs_readlink ( &nfs->nfs_intf, &nfs->nfs_session,
  318. &nfs->readlink_fh );
  319. if ( rc != 0 )
  320. goto err;
  321. nfs->nfs_state++;
  322. return;
  323. }
  324. if ( nfs->nfs_state == NFS_READ ) {
  325. DBGC ( nfs, "NFS_OPEN %p READ call\n", nfs );
  326. rc = nfs_read ( &nfs->nfs_intf, &nfs->nfs_session,
  327. &nfs->current_fh, nfs->file_offset,
  328. NFS_RSIZE );
  329. if ( rc != 0 )
  330. goto err;
  331. nfs->nfs_state++;
  332. return;
  333. }
  334. return;
  335. err:
  336. nfs_done ( nfs, rc );
  337. }
  338. static int nfs_deliver ( struct nfs_request *nfs,
  339. struct io_buffer *io_buf,
  340. struct xfer_metadata *meta __unused ) {
  341. int rc;
  342. struct oncrpc_reply reply;
  343. if ( nfs->remaining == 0 ) {
  344. oncrpc_get_reply ( &nfs->nfs_session, &reply, io_buf );
  345. if ( reply.accept_state != 0 ) {
  346. rc = -EPROTO;
  347. goto err;
  348. }
  349. }
  350. if ( nfs->nfs_state == NFS_LOOKUP_SENT ) {
  351. struct nfs_lookup_reply lookup_reply;
  352. DBGC ( nfs, "NFS_OPEN %p got LOOKUP reply\n", nfs );
  353. rc = nfs_get_lookup_reply ( &lookup_reply, &reply );
  354. if ( rc != 0 )
  355. goto err;
  356. if ( lookup_reply.ent_type == NFS_ATTR_SYMLINK ) {
  357. nfs->readlink_fh = lookup_reply.fh;
  358. nfs->nfs_state = NFS_READLINK;
  359. } else {
  360. nfs->current_fh = lookup_reply.fh;
  361. if ( nfs->filename[0] == '\0' )
  362. nfs->nfs_state = NFS_READ;
  363. else
  364. nfs->nfs_state--;
  365. }
  366. nfs_step ( nfs );
  367. goto done;
  368. }
  369. if ( nfs->nfs_state == NFS_READLINK_SENT ) {
  370. char *new_filename;
  371. struct nfs_readlink_reply readlink_reply;
  372. DBGC ( nfs, "NFS_OPEN %p got READLINK reply\n", nfs );
  373. rc = nfs_get_readlink_reply ( &readlink_reply, &reply );
  374. if ( rc != 0 )
  375. goto err;
  376. if ( readlink_reply.path_len == 0 )
  377. return -EINVAL;
  378. if ( readlink_reply.path[0] == '/' ) {
  379. if ( strncmp ( readlink_reply.path, nfs->mountpoint,
  380. strlen ( nfs->mountpoint ) ) != 0 )
  381. return -EINVAL;
  382. /* The mountpoint part of the path is ended by a '/' */
  383. if ( strlen ( nfs->mountpoint ) !=
  384. readlink_reply.path_len ) {
  385. readlink_reply.path += 1;
  386. readlink_reply.path_len -= 1;
  387. }
  388. /* We are considering the last part of the absolute
  389. * path as a relative path from the mountpoint.
  390. */
  391. readlink_reply.path += strlen ( nfs->mountpoint );
  392. readlink_reply.path_len -= strlen ( nfs->mountpoint );
  393. }
  394. new_filename = malloc ( readlink_reply.path_len +
  395. strlen ( nfs->filename ) + 2 );
  396. if ( ! new_filename ) {
  397. rc = -ENOMEM;
  398. goto err;
  399. }
  400. memcpy ( new_filename, readlink_reply.path,
  401. readlink_reply.path_len );
  402. strcpy ( new_filename + readlink_reply.path_len + 1,
  403. nfs->filename );
  404. new_filename[readlink_reply.path_len] = '/';
  405. free ( nfs->filename - nfs->filename_offset );
  406. nfs->filename = new_filename;
  407. nfs->filename_offset = 0;
  408. DBGC ( nfs, "NFS_OPEN %p new filename: %s\n", nfs,
  409. nfs->filename );
  410. nfs->nfs_state = NFS_LOOKUP;
  411. nfs_step ( nfs );
  412. goto done;
  413. }
  414. if ( nfs->nfs_state == NFS_READ_SENT ) {
  415. if ( nfs->remaining == 0 ) {
  416. DBGC ( nfs, "NFS_OPEN %p got READ reply\n", nfs );
  417. struct nfs_read_reply read_reply;
  418. rc = nfs_get_read_reply ( &read_reply, &reply );
  419. if ( rc != 0 )
  420. goto err;
  421. if ( nfs->file_offset == 0 ) {
  422. DBGC2 ( nfs, "NFS_OPEN %p size: %llu bytes\n",
  423. nfs, read_reply.filesize );
  424. xfer_seek ( &nfs->xfer, read_reply.filesize );
  425. xfer_seek ( &nfs->xfer, 0 );
  426. }
  427. nfs->file_offset += read_reply.count;
  428. nfs->remaining = read_reply.count;
  429. nfs->eof = read_reply.eof;
  430. }
  431. size_t len = iob_len ( io_buf );
  432. if ( len > nfs->remaining )
  433. iob_unput ( io_buf, len - nfs->remaining );
  434. nfs->remaining -= iob_len ( io_buf );
  435. DBGC ( nfs, "NFS_OPEN %p got %zd bytes\n", nfs,
  436. iob_len ( io_buf ) );
  437. rc = xfer_deliver_iob ( &nfs->xfer, iob_disown ( io_buf ) );
  438. if ( rc != 0 )
  439. goto err;
  440. if ( nfs->remaining == 0 ) {
  441. if ( ! nfs->eof ) {
  442. nfs->nfs_state--;
  443. nfs_step ( nfs );
  444. } else {
  445. intf_shutdown ( &nfs->nfs_intf, 0 );
  446. nfs->nfs_state++;
  447. nfs->mount_state++;
  448. nfs_mount_step ( nfs );
  449. }
  450. }
  451. return 0;
  452. }
  453. rc = -EPROTO;
  454. err:
  455. nfs_done ( nfs, rc );
  456. done:
  457. free_iob ( io_buf );
  458. return 0;
  459. }
  460. /*****************************************************************************
  461. * Interfaces
  462. *
  463. */
  464. static struct interface_operation nfs_xfer_operations[] = {
  465. INTF_OP ( intf_close, struct nfs_request *, nfs_done ),
  466. };
  467. /** NFS data transfer interface descriptor */
  468. static struct interface_descriptor nfs_xfer_desc =
  469. INTF_DESC ( struct nfs_request, xfer, nfs_xfer_operations );
  470. static struct interface_operation nfs_pm_operations[] = {
  471. INTF_OP ( intf_close, struct nfs_request *, nfs_done ),
  472. INTF_OP ( xfer_deliver, struct nfs_request *, nfs_pm_deliver ),
  473. INTF_OP ( xfer_window_changed, struct nfs_request *, nfs_pm_step ),
  474. };
  475. static struct interface_descriptor nfs_pm_desc =
  476. INTF_DESC ( struct nfs_request, pm_intf, nfs_pm_operations );
  477. static struct interface_operation nfs_mount_operations[] = {
  478. INTF_OP ( intf_close, struct nfs_request *, nfs_done ),
  479. INTF_OP ( xfer_deliver, struct nfs_request *, nfs_mount_deliver ),
  480. INTF_OP ( xfer_window_changed, struct nfs_request *, nfs_mount_step ),
  481. };
  482. static struct interface_descriptor nfs_mount_desc =
  483. INTF_DESC ( struct nfs_request, mount_intf, nfs_mount_operations );
  484. static struct interface_operation nfs_operations[] = {
  485. INTF_OP ( intf_close, struct nfs_request *, nfs_done ),
  486. INTF_OP ( xfer_deliver, struct nfs_request *, nfs_deliver ),
  487. INTF_OP ( xfer_window_changed, struct nfs_request *, nfs_step ),
  488. };
  489. static struct interface_descriptor nfs_desc =
  490. INTF_DESC_PASSTHRU ( struct nfs_request, nfs_intf, nfs_operations,
  491. xfer );
  492. /*****************************************************************************
  493. *
  494. * URI opener
  495. *
  496. */
  497. static int nfs_parse_uri ( struct nfs_request *nfs, const struct uri *uri ) {
  498. int rc;
  499. if ( ! uri || ! uri->host || ! uri->path )
  500. return -EINVAL;
  501. if ( ! ( nfs->path = strdup ( uri->path ) ) )
  502. return -ENOMEM;
  503. if ( ! ( nfs->hostname = strdup ( uri->host ) ) ) {
  504. rc = -ENOMEM;
  505. goto err_hostname;
  506. }
  507. nfs->filename = basename ( nfs->path );
  508. nfs->mountpoint = dirname ( nfs->path );
  509. if ( nfs->filename[0] == '\0' )
  510. goto err_filename;
  511. DBGC ( nfs, "NFS_OPEN %p URI parsed: (mountpoint=%s, filename=%s)\n",
  512. nfs, nfs->mountpoint, nfs->filename );
  513. return 0;
  514. err_filename:
  515. rc = -EINVAL;
  516. free ( nfs->hostname );
  517. err_hostname:
  518. free ( nfs->path );
  519. return rc;
  520. }
  521. /**
  522. * Initiate a NFS connection
  523. *
  524. * @v xfer Data transfer interface
  525. * @v uri Uniform Resource Identifier
  526. * @ret rc Return status code
  527. */
  528. static int nfs_open ( struct interface *xfer, struct uri *uri ) {
  529. int rc;
  530. struct nfs_request *nfs;
  531. nfs = zalloc ( sizeof ( *nfs ) );
  532. if ( ! nfs )
  533. return -ENOMEM;
  534. rc = nfs_parse_uri( nfs, uri );
  535. if ( rc != 0 )
  536. goto err_uri;
  537. rc = oncrpc_init_cred_sys ( &nfs->auth_sys );
  538. if ( rc != 0 )
  539. goto err_cred;
  540. ref_init ( &nfs->refcnt, nfs_free );
  541. intf_init ( &nfs->xfer, &nfs_xfer_desc, &nfs->refcnt );
  542. intf_init ( &nfs->pm_intf, &nfs_pm_desc, &nfs->refcnt );
  543. intf_init ( &nfs->mount_intf, &nfs_mount_desc, &nfs->refcnt );
  544. intf_init ( &nfs->nfs_intf, &nfs_desc, &nfs->refcnt );
  545. portmap_init_session ( &nfs->pm_session, &nfs->auth_sys.credential );
  546. mount_init_session ( &nfs->mount_session, &nfs->auth_sys.credential );
  547. nfs_init_session ( &nfs->nfs_session, &nfs->auth_sys.credential );
  548. rc = nfs_connect ( &nfs->pm_intf, PORTMAP_PORT, nfs->hostname );
  549. if ( rc != 0 )
  550. goto err_connect;
  551. /* Attach to parent interface, mortalise self, and return */
  552. intf_plug_plug ( &nfs->xfer, xfer );
  553. ref_put ( &nfs->refcnt );
  554. return 0;
  555. err_connect:
  556. free ( nfs->auth_sys.hostname );
  557. err_cred:
  558. err_uri:
  559. free ( nfs );
  560. return rc;
  561. }
  562. /** NFS URI opener */
  563. struct uri_opener nfs_uri_opener __uri_opener = {
  564. .scheme = "nfs",
  565. .open = nfs_open,
  566. };