]> git.decadent.org.uk Git - nfs-utils.git/blob - utils/mount/network.c
umount.nfs: Fix return value of nfs_mount_protocol()
[nfs-utils.git] / utils / mount / network.c
1 /*
2  * network.c -- Provide common network functions for NFS mount/umount
3  *
4  * Copyright (C) 2007 Oracle.  All rights reserved.
5  * Copyright (C) 2007 Chuck Lever <chuck.lever@oracle.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 021110-1307, USA.
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <netdb.h>
35 #include <time.h>
36
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/wait.h>
40 #include <netinet/in.h>
41 #include <rpc/rpc.h>
42 #include <rpc/pmap_prot.h>
43 #include <rpc/pmap_clnt.h>
44
45 #include "xcommon.h"
46 #include "mount.h"
47 #include "nls.h"
48 #include "nfs_mount.h"
49 #include "mount_constants.h"
50 #include "nfsrpc.h"
51 #include "parse_opt.h"
52 #include "network.h"
53
54 #define PMAP_TIMEOUT    (10)
55 #define CONNECT_TIMEOUT (20)
56 #define MOUNT_TIMEOUT   (30)
57
58 #if SIZEOF_SOCKLEN_T - 0 == 0
59 #define socklen_t unsigned int
60 #endif
61
62 extern int nfs_mount_data_version;
63 extern char *progname;
64 extern int verbose;
65
66 static const char *nfs_ns_pgmtbl[] = {
67         "status",
68         NULL,
69 };
70
71 static const char *nfs_mnt_pgmtbl[] = {
72         "mount",
73         "mountd",
74         NULL,
75 };
76
77 static const char *nfs_nfs_pgmtbl[] = {
78         "nfs",
79         "nfsprog",
80         NULL,
81 };
82
83 static const char *nfs_transport_opttbl[] = {
84         "udp",
85         "tcp",
86         "proto",
87         NULL,
88 };
89
90 static const char *nfs_version_opttbl[] = {
91         "v2",
92         "v3",
93         "vers",
94         "nfsvers",
95         NULL,
96 };
97
98 static const unsigned long nfs_to_mnt[] = {
99         0,
100         0,
101         1,
102         3,
103 };
104
105 static const unsigned long mnt_to_nfs[] = {
106         0,
107         2,
108         2,
109         3,
110 };
111
112 /*
113  * Map an NFS version into the corresponding Mountd version
114  */
115 unsigned long nfsvers_to_mnt(const unsigned long vers)
116 {
117         if (vers <= 3)
118                 return nfs_to_mnt[vers];
119         return 0;
120 }
121
122 /*
123  * Map a Mountd version into the corresponding NFS version
124  */
125 static unsigned long mntvers_to_nfs(const unsigned long vers)
126 {
127         if (vers <= 3)
128                 return mnt_to_nfs[vers];
129         return 0;
130 }
131
132 static const unsigned int probe_udp_only[] = {
133         IPPROTO_UDP,
134         0,
135 };
136
137 static const unsigned int probe_udp_first[] = {
138         IPPROTO_UDP,
139         IPPROTO_TCP,
140         0,
141 };
142
143 static const unsigned int probe_tcp_first[] = {
144         IPPROTO_TCP,
145         IPPROTO_UDP,
146         0,
147 };
148
149 static const unsigned long probe_nfs2_only[] = {
150         2,
151         0,
152 };
153
154 static const unsigned long probe_nfs3_first[] = {
155         3,
156         2,
157         0,
158 };
159
160 static const unsigned long probe_mnt1_first[] = {
161         1,
162         2,
163         0,
164 };
165
166 static const unsigned long probe_mnt3_first[] = {
167         3,
168         1,
169         2,
170         0,
171 };
172
173 static void nfs_set_port(struct sockaddr *sap, const unsigned short port)
174 {
175         switch (sap->sa_family) {
176         case AF_INET:
177                 ((struct sockaddr_in *)sap)->sin_port = htons(port);
178                 break;
179         case AF_INET6:
180                 ((struct sockaddr_in6 *)sap)->sin6_port = htons(port);
181                 break;
182         default:
183                 nfs_error(_("%s: unrecognized address family in %s"),
184                         progname, __func__);
185         }
186 }
187
188 #ifdef HAVE_DECL_AI_ADDRCONFIG
189 /**
190  * nfs_name_to_address - resolve hostname to an IPv4 or IPv6 socket address
191  * @hostname: pointer to C string containing DNS hostname to resolve
192  * @af_hint: hint to restrict resolution to one address family
193  * @sap: pointer to buffer to fill with socket address
194  * @len: IN: size of buffer to fill; OUT: size of socket address
195  *
196  * Returns 1 and places a socket address at @sap if successful;
197  * otherwise zero.
198  */
199 int nfs_name_to_address(const char *hostname,
200                         const sa_family_t af_hint,
201                         struct sockaddr *sap, socklen_t *salen)
202 {
203         struct addrinfo *gai_results;
204         struct addrinfo gai_hint = {
205                 .ai_family      = af_hint,
206                 .ai_flags       = AI_ADDRCONFIG,
207         };
208         socklen_t len = *salen;
209         int error, ret = 0;
210
211         if (af_hint == AF_INET6)
212                 gai_hint.ai_flags |= AI_V4MAPPED|AI_ALL;
213
214         *salen = 0;
215
216         error = getaddrinfo(hostname, NULL, &gai_hint, &gai_results);
217         if (error) {
218                 nfs_error(_("%s: DNS resolution failed for %s: %s"),
219                         progname, hostname, (error == EAI_SYSTEM ?
220                                 strerror(errno) : gai_strerror(error)));
221                 return ret;
222         }
223
224         switch (gai_results->ai_addr->sa_family) {
225         case AF_INET:
226         case AF_INET6:
227                 if (len >= gai_results->ai_addrlen) {
228                         *salen = gai_results->ai_addrlen;
229                         memcpy(sap, gai_results->ai_addr, *salen);
230                         ret = 1;
231                 }
232                 break;
233         default:
234                 /* things are really broken if we get here, so warn */
235                 nfs_error(_("%s: unrecognized DNS resolution results for %s"),
236                                 progname, hostname);
237                 break;
238         }
239
240         freeaddrinfo(gai_results);
241         return ret;
242 }
243 #else   /* HAVE_DECL_AI_ADDRCONFIG */
244 /**
245  * nfs_name_to_address - resolve hostname to an IPv4 socket address
246  * @hostname: pointer to C string containing DNS hostname to resolve
247  * @af_hint: hint to restrict resolution to one address family
248  * @sap: pointer to buffer to fill with socket address
249  * @len: IN: size of buffer to fill; OUT: size of socket address
250  *
251  * Returns 1 and places a socket address at @sap if successful;
252  * otherwise zero.
253  *
254  * Some older getaddrinfo(3) implementations don't support
255  * AI_ADDRCONFIG or AI_V4MAPPED properly.  For those cases, a DNS
256  * resolver based on the traditional gethostbyname(3) is provided.
257  */
258 int nfs_name_to_address(const char *hostname,
259                         const sa_family_t af_hint,
260                         struct sockaddr *sap, socklen_t *salen)
261 {
262         struct sockaddr_in *sin = (struct sockaddr_in *)sap;
263         socklen_t len = *salen;
264         struct hostent *hp;
265
266         *salen = 0;
267
268         if (af_hint != AF_INET) {
269                 nfs_error(_("%s: address family not supported by DNS resolver\n"),
270                                 progname, hostname);
271                 return 0;
272         }
273
274         sin->sin_family = AF_INET;
275         if (inet_aton(hostname, &sin->sin_addr)) {
276                 *salen = sizeof(*sin);
277                 return 1;
278         }
279
280         hp = gethostbyname(hostname);
281         if (hp == NULL) {
282                 nfs_error(_("%s: DNS resolution failed for %s: %s"),
283                                 progname, hostname, hstrerror(h_errno));
284                 return 0;
285         }
286
287         if (hp->h_length > len) {
288                 nfs_error(_("%s: DNS resolution results too long for buffer\n"),
289                                 progname);
290                 return 0;
291         }
292
293         memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
294         *salen = sizeof(struct sockaddr_in);
295         return 1;
296 }
297 #endif  /* HAVE_DECL_AI_ADDRCONFIG */
298
299 /**
300  * nfs_gethostbyname - resolve a hostname to an IPv4 address
301  * @hostname: pointer to a C string containing a DNS hostname
302  * @sin: returns an IPv4 address 
303  *
304  * Returns 1 if successful, otherwise zero.
305  */
306 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *sin)
307 {
308         socklen_t len = sizeof(*sin);
309
310         return nfs_name_to_address(hostname, AF_INET,
311                                         (struct sockaddr *)sin, &len);
312 }
313
314 /**
315  * nfs_string_to_sockaddr - convert string address to sockaddr
316  * @address:    pointer to presentation format address to convert
317  * @addrlen:    length of presentation address
318  * @sap:        pointer to socket address buffer to fill in
319  * @salen:      IN: length of address buffer
320  *              OUT: length of converted socket address
321  *
322  * Convert a presentation format address string to a socket address.
323  * Similar to nfs_name_to_address(), but the DNS query is squelched,
324  * and won't make any noise if the getaddrinfo() call fails.
325  *
326  * Returns 1 and fills in @sap and @salen if successful; otherwise zero.
327  *
328  * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
329  * on presenting IPv6 addresses as text strings.
330  */
331 int nfs_string_to_sockaddr(const char *address, const size_t addrlen,
332                            struct sockaddr *sap, socklen_t *salen)
333 {
334         struct addrinfo *gai_results;
335         struct addrinfo gai_hint = {
336                 .ai_flags       = AI_NUMERICHOST,
337         };
338         socklen_t len = *salen;
339         int ret = 0;
340
341         *salen = 0;
342
343         if (getaddrinfo(address, NULL, &gai_hint, &gai_results) == 0) {
344                 switch (gai_results->ai_addr->sa_family) {
345                 case AF_INET:
346                 case AF_INET6:
347                         if (len >= gai_results->ai_addrlen) {
348                                 *salen = gai_results->ai_addrlen;
349                                 memcpy(sap, gai_results->ai_addr, *salen);
350                                 ret = 1;
351                         }
352                         break;
353                 }
354                 freeaddrinfo(gai_results);
355         }
356
357         return ret;
358 }
359
360 /**
361  * nfs_present_sockaddr - convert sockaddr to string
362  * @sap: pointer to socket address to convert
363  * @salen: length of socket address
364  * @buf: pointer to buffer to fill in
365  * @buflen: length of buffer
366  *
367  * Convert the passed-in sockaddr-style address to presentation format.
368  * The presentation format address is placed in @buf and is
369  * '\0'-terminated.
370  *
371  * Returns 1 if successful; otherwise zero.
372  *
373  * See RFC 4038 section 5.1 or RFC 3513 section 2.2 for more details
374  * on presenting IPv6 addresses as text strings.
375  */
376 int nfs_present_sockaddr(const struct sockaddr *sap, const socklen_t salen,
377                          char *buf, const size_t buflen)
378 {
379 #ifdef HAVE_GETNAMEINFO
380         int result;
381
382         result = getnameinfo(sap, salen, buf, buflen,
383                                         NULL, 0, NI_NUMERICHOST);
384         if (!result)
385                 return 1;
386
387         nfs_error(_("%s: invalid server address: %s"), progname,
388                         gai_strerror(result));
389         return 0;
390 #else   /* HAVE_GETNAMEINFO */
391         char *addr;
392
393         if (sap->sa_family == AF_INET) {
394                 addr = inet_ntoa(((struct sockaddr_in *)sap)->sin_addr);
395                 if (addr && strlen(addr) < buflen) {
396                         strcpy(buf, addr);
397                         return 1;
398                 }
399         }
400
401         nfs_error(_("%s: invalid server address"), progname);
402         return 0;
403 #endif  /* HAVE_GETNAMEINFO */
404 }
405
406 /*
407  * Attempt to connect a socket, but time out after "timeout" seconds.
408  *
409  * On error return, caller closes the socket.
410  */
411 static int connect_to(int fd, struct sockaddr *addr,
412                         socklen_t addrlen, int timeout)
413 {
414         int ret, saved;
415         fd_set rset, wset;
416         struct timeval tv = {
417                 .tv_sec = timeout,
418         };
419
420         saved = fcntl(fd, F_GETFL, 0);
421         fcntl(fd, F_SETFL, saved | O_NONBLOCK);
422
423         ret = connect(fd, addr, addrlen);
424         if (ret < 0 && errno != EINPROGRESS)
425                 return -1;
426         if (ret == 0)
427                 goto out;
428
429         FD_ZERO(&rset);
430         FD_SET(fd, &rset);
431         wset = rset;
432         ret = select(fd + 1, &rset, &wset, NULL, &tv);
433         if (ret == 0) {
434                 errno = ETIMEDOUT;
435                 return -1;
436         }
437         if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
438                 int error;
439                 socklen_t len = sizeof(error);
440                 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0)
441                         return -1;
442                 if (error) {
443                         errno = error;
444                         return -1;
445                 }
446         } else
447                 return -1;
448
449 out:
450         fcntl(fd, F_SETFL, saved);
451         return 0;
452 }
453
454 /*
455  * Create a socket that is locally bound to a reserved or non-reserved port.
456  *
457  * The caller should check rpc_createerr to determine the cause of any error.
458  */
459 static int get_socket(struct sockaddr_in *saddr, unsigned int p_prot,
460                         unsigned int timeout, int resvp, int conn)
461 {
462         int so, cc, type;
463         struct sockaddr_in laddr;
464         socklen_t namelen = sizeof(laddr);
465
466         type = (p_prot == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM);
467         if ((so = socket (AF_INET, type, p_prot)) < 0)
468                 goto err_socket;
469
470         laddr.sin_family = AF_INET;
471         laddr.sin_port = 0;
472         laddr.sin_addr.s_addr = htonl(INADDR_ANY);
473         if (resvp) {
474                 if (bindresvport(so, &laddr) < 0)
475                         goto err_bindresvport;
476         } else {
477                 cc = bind(so, (struct sockaddr *)&laddr, namelen);
478                 if (cc < 0)
479                         goto err_bind;
480         }
481         if (type == SOCK_STREAM || (conn && type == SOCK_DGRAM)) {
482                 cc = connect_to(so, (struct sockaddr *)saddr, namelen,
483                                 timeout);
484                 if (cc < 0)
485                         goto err_connect;
486         }
487         return so;
488
489 err_socket:
490         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
491         rpc_createerr.cf_error.re_errno = errno;
492         if (verbose) {
493                 nfs_error(_("%s: Unable to create %s socket: errno %d (%s)\n"),
494                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
495                         errno, strerror(errno));
496         }
497         return RPC_ANYSOCK;
498
499 err_bindresvport:
500         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
501         rpc_createerr.cf_error.re_errno = errno;
502         if (verbose) {
503                 nfs_error(_("%s: Unable to bindresvport %s socket: errno %d"
504                                 " (%s)\n"),
505                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
506                         errno, strerror(errno));
507         }
508         close(so);
509         return RPC_ANYSOCK;
510
511 err_bind:
512         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
513         rpc_createerr.cf_error.re_errno = errno;
514         if (verbose) {
515                 nfs_error(_("%s: Unable to bind to %s socket: errno %d (%s)\n"),
516                         progname, p_prot == IPPROTO_UDP ? _("UDP") : _("TCP"),
517                         errno, strerror(errno));
518         }
519         close(so);
520         return RPC_ANYSOCK;
521
522 err_connect:
523         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
524         rpc_createerr.cf_error.re_errno = errno;
525         if (verbose) {
526                 nfs_error(_("%s: Unable to connect to %s:%d, errno %d (%s)\n"),
527                         progname, inet_ntoa(saddr->sin_addr),
528                         ntohs(saddr->sin_port), errno, strerror(errno));
529         }
530         close(so);
531         return RPC_ANYSOCK;
532 }
533
534 static void nfs_pp_debug(const struct sockaddr *sap, const socklen_t salen,
535                          const rpcprog_t program, const rpcvers_t version,
536                          const unsigned short protocol,
537                          const unsigned short port)
538 {
539         char buf[NI_MAXHOST];
540
541         if (!verbose)
542                 return;
543
544         if (nfs_present_sockaddr(sap, salen, buf, sizeof(buf)) == 0) {
545                 buf[0] = '\0';
546                 strcat(buf, "unknown host");
547         }
548
549         fprintf(stderr, _("%s: trying %s prog %lu vers %lu prot %s port %d\n"),
550                         progname, buf, (unsigned long)program,
551                         (unsigned long)version,
552                         (protocol == IPPROTO_UDP ? _("UDP") : _("TCP")),
553                         port);
554 }
555
556 /*
557  * Use the portmapper to discover whether or not the service we want is
558  * available. The lists 'versions' and 'protos' define ordered sequences
559  * of service versions and udp/tcp protocols to probe for.
560  *
561  * Returns 1 if the requested service port is unambiguous and pingable;
562  * @pmap is filled in with the version, port, and transport protocol used
563  * during the successful ping.  Note that if a port is already specified
564  * in @pmap and it matches the rpcbind query result, nfs_probe_port() does
565  * not perform an RPC ping.
566  * 
567  * If an error occurs or the requested service isn't available, zero is
568  * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
569  */
570 static int nfs_probe_port(const struct sockaddr *sap, const socklen_t salen,
571                           struct pmap *pmap, const unsigned long *versions,
572                           const unsigned int *protos)
573 {
574         struct sockaddr_storage address;
575         struct sockaddr *saddr = (struct sockaddr *)&address;
576         const unsigned long prog = pmap->pm_prog, *p_vers;
577         const unsigned int prot = (u_int)pmap->pm_prot, *p_prot;
578         const u_short port = (u_short) pmap->pm_port;
579         unsigned long vers = pmap->pm_vers;
580         unsigned short p_port;
581
582         memcpy(saddr, sap, salen);
583         p_prot = prot ? &prot : protos;
584         p_vers = vers ? &vers : versions;
585         rpc_createerr.cf_stat = 0;
586
587         for (;;) {
588                 p_port = nfs_getport(saddr, salen, prog, *p_vers, *p_prot);
589                 if (p_port) {
590                         if (!port || port == p_port) {
591                                 nfs_set_port(saddr, p_port);
592                                 nfs_pp_debug(saddr, salen, prog, *p_vers,
593                                                 *p_prot, p_port);
594                                 if (nfs_rpc_ping(saddr, salen, prog,
595                                                         *p_vers, *p_prot, NULL))
596                                         goto out_ok;
597                         }
598                 }
599                 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED &&
600                     rpc_createerr.cf_stat != RPC_TIMEDOUT &&
601                     rpc_createerr.cf_stat != RPC_CANTRECV &&
602                     rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH)
603                         goto out_bad;
604
605                 if (!prot) {
606                         if (*++p_prot)
607                                 continue;
608                         p_prot = protos;
609                 }
610                 if (rpc_createerr.cf_stat == RPC_TIMEDOUT ||
611                     rpc_createerr.cf_stat == RPC_CANTRECV)
612                         goto out_bad;
613
614                 if (vers || !*++p_vers)
615                         break;
616         }
617
618 out_bad:
619         return 0;
620
621 out_ok:
622         if (!vers)
623                 pmap->pm_vers = *p_vers;
624         if (!prot)
625                 pmap->pm_prot = *p_prot;
626         if (!port)
627                 pmap->pm_port = p_port;
628         rpc_createerr.cf_stat = 0;
629         return 1;
630 }
631
632 /*
633  * Probe a server's NFS service to determine which versions and
634  * transport protocols are supported.
635  *
636  * Returns 1 if the requested service port is unambiguous and pingable;
637  * @pmap is filled in with the version, port, and transport protocol used
638  * during the successful ping.  If all three are already specified, simply
639  * return success without an rpcbind query or RPC ping (we may be trying
640  * to mount an NFS service that is not advertised via rpcbind).
641  *
642  * If an error occurs or the requested service isn't available, zero is
643  * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
644  */
645 static int nfs_probe_nfsport(const struct sockaddr *sap, const socklen_t salen,
646                                 struct pmap *pmap)
647 {
648         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
649                 return 1;
650
651         if (nfs_mount_data_version >= 4)
652                 return nfs_probe_port(sap, salen, pmap,
653                                         probe_nfs3_first, probe_tcp_first);
654         else
655                 return nfs_probe_port(sap, salen, pmap,
656                                         probe_nfs2_only, probe_udp_only);
657 }
658
659 /*
660  * Probe a server's mountd service to determine which versions and
661  * transport protocols are supported.
662  *
663  * Returns 1 if the requested service port is unambiguous and pingable;
664  * @pmap is filled in with the version, port, and transport protocol used
665  * during the successful ping.  If all three are already specified, simply
666  * return success without an rpcbind query or RPC ping (we may be trying
667  * to mount an NFS service that is not advertised via rpcbind).
668  * 
669  * If an error occurs or the requested service isn't available, zero is
670  * returned; rpccreateerr.cf_stat is set to reflect the nature of the error.
671  */
672 static int nfs_probe_mntport(const struct sockaddr *sap, const socklen_t salen,
673                                 struct pmap *pmap)
674 {
675         if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
676                 return 1;
677
678         if (nfs_mount_data_version >= 4)
679                 return nfs_probe_port(sap, salen, pmap,
680                                         probe_mnt3_first, probe_udp_first);
681         else
682                 return nfs_probe_port(sap, salen, pmap,
683                                         probe_mnt1_first, probe_udp_only);
684 }
685
686 /*
687  * Probe a server's mountd service to determine which versions and
688  * transport protocols are supported.  Invoked when the protocol
689  * version is already known for both the NFS and mountd service.
690  *
691  * Returns 1 and fills in both @pmap structs if the requested service
692  * ports are unambiguous and pingable.  Otherwise zero is returned;
693  * rpccreateerr.cf_stat is set to reflect the nature of the error.
694  */
695 static int nfs_probe_version_fixed(const struct sockaddr *mnt_saddr,
696                         const socklen_t mnt_salen,
697                         struct pmap *mnt_pmap,
698                         const struct sockaddr *nfs_saddr,
699                         const socklen_t nfs_salen,
700                         struct pmap *nfs_pmap)
701 {
702         if (!nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap))
703                 return 0;
704         return nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap);
705 }
706
707 /**
708  * nfs_probe_bothports - discover the RPC endpoints of mountd and NFS server
709  * @mnt_saddr:  pointer to socket address of mountd server
710  * @mnt_salen:  length of mountd server's address
711  * @mnt_pmap:   IN: partially filled-in mountd RPC service tuple;
712  *              OUT: fully filled-in mountd RPC service tuple
713  * @nfs_saddr:  pointer to socket address of NFS server
714  * @nfs_salen:  length of NFS server's address
715  * @nfs_pmap:   IN: partially filled-in NFS RPC service tuple;
716  *              OUT: fully filled-in NFS RPC service tuple
717  *
718  * Returns 1 and fills in both @pmap structs if the requested service
719  * ports are unambiguous and pingable.  Otherwise zero is returned;
720  * rpccreateerr.cf_stat is set to reflect the nature of the error.
721  */
722 int nfs_probe_bothports(const struct sockaddr *mnt_saddr,
723                         const socklen_t mnt_salen,
724                         struct pmap *mnt_pmap,
725                         const struct sockaddr *nfs_saddr,
726                         const socklen_t nfs_salen,
727                         struct pmap *nfs_pmap)
728 {
729         struct pmap save_nfs, save_mnt;
730         const unsigned long *probe_vers;
731
732         if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
733                 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
734         else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
735                 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
736
737         if (nfs_pmap->pm_vers)
738                 return nfs_probe_version_fixed(mnt_saddr, mnt_salen, mnt_pmap,
739                                                nfs_saddr, nfs_salen, nfs_pmap);
740
741         memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
742         memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
743         probe_vers = (nfs_mount_data_version >= 4) ?
744                         probe_mnt3_first : probe_mnt1_first;
745
746         for (; *probe_vers; probe_vers++) {
747                 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
748                 if (nfs_probe_nfsport(nfs_saddr, nfs_salen, nfs_pmap) != 0) {
749                         mnt_pmap->pm_vers = *probe_vers;
750                         if (nfs_probe_mntport(mnt_saddr, mnt_salen, mnt_pmap) != 0)
751                                 return 1;
752                         memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
753                 }
754                 switch (rpc_createerr.cf_stat) {
755                 case RPC_PROGVERSMISMATCH:
756                 case RPC_PROGNOTREGISTERED:
757                         break;
758                 default:
759                         return 0;
760                 }
761                 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
762         }
763
764         return 0;
765 }
766
767 /**
768  * probe_bothports - discover the RPC endpoints of mountd and NFS server
769  * @mnt_server: pointer to address and pmap argument for mountd results
770  * @nfs_server: pointer to address and pmap argument for NFS server
771  *
772  * This is the legacy API that takes "clnt_addr_t" for both servers,
773  * but supports only AF_INET addresses.
774  *
775  * Returns 1 and fills in the pmap field in both clnt_addr_t structs
776  * if the requested service ports are unambiguous and pingable.
777  * Otherwise zero is returned; rpccreateerr.cf_stat is set to reflect
778  * the nature of the error.
779  */
780 int probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
781 {
782         return nfs_probe_bothports((struct sockaddr *)&mnt_server->saddr,
783                                         sizeof(mnt_server->saddr),
784                                         &mnt_server->pmap,
785                                         (struct sockaddr *)&nfs_server->saddr,
786                                         sizeof(nfs_server->saddr),
787                                         &nfs_server->pmap);
788 }
789
790 static int nfs_probe_statd(void)
791 {
792         struct sockaddr_in addr = {
793                 .sin_family             = AF_INET,
794                 .sin_addr.s_addr        = htonl(INADDR_LOOPBACK),
795         };
796         rpcprog_t program = nfs_getrpcbyname(NSMPROG, nfs_ns_pgmtbl);
797
798         return nfs_getport_ping((struct sockaddr *)&addr, sizeof(addr),
799                                 program, (rpcvers_t)1, IPPROTO_UDP);
800 }
801
802 /**
803  * start_statd - attempt to start rpc.statd
804  *
805  * Returns 1 if statd is running; otherwise zero.
806  */
807 int start_statd(void)
808 {
809 #ifdef START_STATD
810         struct stat stb;
811 #endif
812
813         if (nfs_probe_statd())
814                 return 1;
815
816 #ifdef START_STATD
817         if (stat(START_STATD, &stb) == 0) {
818                 if (S_ISREG(stb.st_mode) && (stb.st_mode & S_IXUSR)) {
819                         pid_t pid = fork();
820                         switch (pid) {
821                         case 0: /* child */
822                                 execl(START_STATD, START_STATD, NULL);
823                                 exit(1);
824                         case -1: /* error */
825                                 nfs_error(_("fork failed: %s"),
826                                                         strerror(errno));
827                                 break;
828                         default: /* parent */
829                                 waitpid(pid, NULL,0);
830                                 break;
831                         }
832                         if (nfs_probe_statd())
833                                 return 1;
834                 }
835         }
836 #endif
837
838         return 0;
839 }
840
841 /**
842  * nfs_advise_umount - ask the server to remove a share from it's rmtab
843  * @sap: pointer to IP address of server to call
844  * @salen: length of server address
845  * @pmap: partially filled-in mountd RPC service tuple
846  * @argp: directory path of share to "unmount"
847  *
848  * Returns one if the unmount call succeeded; zero if the unmount
849  * failed for any reason;  rpccreateerr.cf_stat is set to reflect
850  * the nature of the error.
851  *
852  * We use a fast timeout since this call is advisory only.
853  */
854 int nfs_advise_umount(const struct sockaddr *sap, const socklen_t salen,
855                       const struct pmap *pmap, const dirpath *argp)
856 {
857         struct sockaddr_storage address;
858         struct sockaddr *saddr = (struct sockaddr *)&address;
859         struct pmap mnt_pmap = *pmap;
860         struct timeval timeout = {
861                 .tv_sec         = MOUNT_TIMEOUT >> 3,
862         };
863         CLIENT *client;
864         enum clnt_stat res = 0;
865
866         if (nfs_probe_mntport(sap, salen, &mnt_pmap) == 0)
867                 return 0;
868
869         memcpy(saddr, sap, salen);
870         nfs_set_port(saddr, mnt_pmap.pm_port);
871
872         client = nfs_get_priv_rpcclient(saddr, salen, mnt_pmap.pm_prot,
873                                         mnt_pmap.pm_prog, mnt_pmap.pm_vers,
874                                         &timeout);
875         if (client == NULL)
876                 return 0;
877
878         client->cl_auth = authunix_create_default();
879
880         res = CLNT_CALL(client, MOUNTPROC_UMNT,
881                         (xdrproc_t)xdr_dirpath, (caddr_t)argp,
882                         (xdrproc_t)xdr_void, NULL,
883                         timeout);
884
885         auth_destroy(client->cl_auth);
886         CLNT_DESTROY(client);
887
888         if (res != RPC_SUCCESS)
889                 return 0;
890
891         return 1;
892 }
893
894 /**
895  * nfs_call_umount - ask the server to remove a share from it's rmtab
896  * @mnt_server: address of RPC MNT program server
897  * @argp: directory path of share to "unmount"
898  *
899  * Returns one if the unmount call succeeded; zero if the unmount
900  * failed for any reason.
901  *
902  * Note that a side effect of calling this function is that rpccreateerr
903  * is set.
904  */
905 int nfs_call_umount(clnt_addr_t *mnt_server, dirpath *argp)
906 {
907         struct sockaddr *sap = (struct sockaddr *)&mnt_server->saddr;
908         socklen_t salen = sizeof(mnt_server->saddr);
909         struct pmap *pmap = &mnt_server->pmap;
910         CLIENT *clnt;
911         enum clnt_stat res = 0;
912         int msock;
913
914         if (!nfs_probe_mntport(sap, salen, pmap))
915                 return 0;
916         clnt = mnt_openclnt(mnt_server, &msock);
917         if (!clnt)
918                 return 0;
919         res = clnt_call(clnt, MOUNTPROC_UMNT,
920                         (xdrproc_t)xdr_dirpath, (caddr_t)argp,
921                         (xdrproc_t)xdr_void, NULL,
922                         TIMEOUT);
923         mnt_closeclnt(clnt, msock);
924
925         if (res == RPC_SUCCESS)
926                 return 1;
927         return 0;
928 }
929
930 /**
931  * mnt_openclnt - get a handle for a remote mountd service
932  * @mnt_server: address and pmap arguments of mountd service
933  * @msock: returns a file descriptor of the underlying transport socket
934  *
935  * Returns an active handle for the remote's mountd service
936  */
937 CLIENT *mnt_openclnt(clnt_addr_t *mnt_server, int *msock)
938 {
939         struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
940         struct pmap *mnt_pmap = &mnt_server->pmap;
941         CLIENT *clnt = NULL;
942
943         mnt_saddr->sin_port = htons((u_short)mnt_pmap->pm_port);
944         *msock = get_socket(mnt_saddr, mnt_pmap->pm_prot, MOUNT_TIMEOUT,
945                                 TRUE, FALSE);
946         if (*msock == RPC_ANYSOCK) {
947                 if (rpc_createerr.cf_error.re_errno == EADDRINUSE)
948                         /*
949                          * Probably in-use by a TIME_WAIT connection,
950                          * It is worth waiting a while and trying again.
951                          */
952                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
953                 return NULL;
954         }
955
956         switch (mnt_pmap->pm_prot) {
957         case IPPROTO_UDP:
958                 clnt = clntudp_bufcreate(mnt_saddr,
959                                          mnt_pmap->pm_prog, mnt_pmap->pm_vers,
960                                          RETRY_TIMEOUT, msock,
961                                          MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
962                 break;
963         case IPPROTO_TCP:
964                 clnt = clnttcp_create(mnt_saddr,
965                                       mnt_pmap->pm_prog, mnt_pmap->pm_vers,
966                                       msock,
967                                       MNT_SENDBUFSIZE, MNT_RECVBUFSIZE);
968                 break;
969         }
970         if (clnt) {
971                 /* try to mount hostname:dirname */
972                 clnt->cl_auth = authunix_create_default();
973                 return clnt;
974         }
975         return NULL;
976 }
977
978 /**
979  * mnt_closeclnt - terminate a handle for a remote mountd service
980  * @clnt: pointer to an active handle for a remote mountd service
981  * @msock: file descriptor of the underlying transport socket
982  *
983  */
984 void mnt_closeclnt(CLIENT *clnt, int msock)
985 {
986         auth_destroy(clnt->cl_auth);
987         clnt_destroy(clnt);
988         close(msock);
989 }
990
991 /**
992  * clnt_ping - send an RPC ping to the remote RPC service endpoint
993  * @saddr: server's address
994  * @prog: target RPC program number
995  * @vers: target RPC version number
996  * @prot: target RPC protocol
997  * @caddr: filled in with our network address
998  *
999  * Sigh... GETPORT queries don't actually check the version number.
1000  * In order to make sure that the server actually supports the service
1001  * we're requesting, we open an RPC client, and fire off a NULL
1002  * RPC call.
1003  *
1004  * caddr is the network address that the server will use to call us back.
1005  * On multi-homed clients, this address depends on which NIC we use to
1006  * route requests to the server.
1007  *
1008  * Returns one if successful, otherwise zero.
1009  */
1010 int clnt_ping(struct sockaddr_in *saddr, const unsigned long prog,
1011                 const unsigned long vers, const unsigned int prot,
1012                 struct sockaddr_in *caddr)
1013 {
1014         CLIENT *clnt = NULL;
1015         int sock, stat;
1016         static char clnt_res;
1017         struct sockaddr dissolve;
1018
1019         rpc_createerr.cf_stat = stat = 0;
1020         sock = get_socket(saddr, prot, CONNECT_TIMEOUT, FALSE, TRUE);
1021         if (sock == RPC_ANYSOCK) {
1022                 if (rpc_createerr.cf_error.re_errno == ETIMEDOUT) {
1023                         /*
1024                          * TCP timeout. Bubble up the error to see 
1025                          * how it should be handled.
1026                          */
1027                         rpc_createerr.cf_stat = RPC_TIMEDOUT;
1028                 }
1029                 return 0;
1030         }
1031
1032         if (caddr) {
1033                 /* Get the address of our end of this connection */
1034                 socklen_t len = sizeof(*caddr);
1035                 if (getsockname(sock, caddr, &len) != 0)
1036                         caddr->sin_family = 0;
1037         }
1038
1039         switch(prot) {
1040         case IPPROTO_UDP:
1041                 /* The socket is connected (so we could getsockname successfully),
1042                  * but some servers on multi-homed hosts reply from
1043                  * the wrong address, so if we stay connected, we lose the reply.
1044                  */
1045                 dissolve.sa_family = AF_UNSPEC;
1046                 connect(sock, &dissolve, sizeof(dissolve));
1047
1048                 clnt = clntudp_bufcreate(saddr, prog, vers,
1049                                          RETRY_TIMEOUT, &sock,
1050                                          RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1051                 break;
1052         case IPPROTO_TCP:
1053                 clnt = clnttcp_create(saddr, prog, vers, &sock,
1054                                       RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
1055                 break;
1056         }
1057         if (!clnt) {
1058                 close(sock);
1059                 return 0;
1060         }
1061         memset(&clnt_res, 0, sizeof(clnt_res));
1062         stat = clnt_call(clnt, NULLPROC,
1063                          (xdrproc_t)xdr_void, (caddr_t)NULL,
1064                          (xdrproc_t)xdr_void, (caddr_t)&clnt_res,
1065                          TIMEOUT);
1066         if (stat) {
1067                 clnt_geterr(clnt, &rpc_createerr.cf_error);
1068                 rpc_createerr.cf_stat = stat;
1069         }
1070         clnt_destroy(clnt);
1071         close(sock);
1072
1073         if (stat == RPC_SUCCESS)
1074                 return 1;
1075         else
1076                 return 0;
1077 }
1078
1079 /*
1080  * Try a getsockname() on a connected datagram socket.
1081  *
1082  * Returns 1 and fills in @buf if successful; otherwise, zero.
1083  *
1084  * A connected datagram socket prevents leaving a socket in TIME_WAIT.
1085  * This conserves the ephemeral port number space, helping reduce failed
1086  * socket binds during mount storms.
1087  */
1088 static int nfs_ca_sockname(const struct sockaddr *sap, const socklen_t salen,
1089                            struct sockaddr *buf, socklen_t *buflen)
1090 {
1091         struct sockaddr_in sin = {
1092                 .sin_family             = AF_INET,
1093                 .sin_addr.s_addr        = htonl(INADDR_ANY),
1094         };
1095         struct sockaddr_in6 sin6 = {
1096                 .sin6_family            = AF_INET6,
1097                 .sin6_addr              = IN6ADDR_ANY_INIT,
1098         };
1099         int sock;
1100
1101         sock = socket(sap->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1102         if (sock < 0)
1103                 return 0;
1104
1105         switch (sap->sa_family) {
1106         case AF_INET:
1107                 if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
1108                         close(sock);
1109                         return 0;
1110                 }
1111                 break;
1112         case AF_INET6:
1113                 if (bind(sock, (struct sockaddr *)&sin6, sizeof(sin6)) < 0) {
1114                         close(sock);
1115                         return 0;
1116                 }
1117                 break;
1118         default:
1119                 errno = EAFNOSUPPORT;
1120                 return 0;
1121         }
1122
1123         if (connect(sock, sap, salen) < 0) {
1124                 close(sock);
1125                 return 0;
1126         }
1127
1128         return !getsockname(sock, buf, buflen);
1129 }
1130
1131 /*
1132  * Try to generate an address that prevents the server from calling us.
1133  *
1134  * Returns 1 and fills in @buf if successful; otherwise, zero.
1135  */
1136 static int nfs_ca_gai(const struct sockaddr *sap, const socklen_t salen,
1137                       struct sockaddr *buf, socklen_t *buflen)
1138 {
1139         struct addrinfo *gai_results;
1140         struct addrinfo gai_hint = {
1141                 .ai_family      = sap->sa_family,
1142                 .ai_flags       = AI_PASSIVE,   /* ANYADDR */
1143         };
1144
1145         if (getaddrinfo(NULL, "", &gai_hint, &gai_results))
1146                 return 0;
1147
1148         *buflen = gai_results->ai_addrlen;
1149         memcpy(buf, gai_results->ai_addr, *buflen);
1150
1151         freeaddrinfo(gai_results);
1152
1153         return 1;
1154 }
1155
1156 /**
1157  * nfs_callback_address - acquire our local network address
1158  * @sap: pointer to address of remote
1159  * @sap_len: length of address
1160  * @buf: pointer to buffer to be filled in with local network address
1161  * @buflen: IN: length of buffer to fill in; OUT: length of filled-in address
1162  *
1163  * Discover a network address that an NFSv4 server can use to call us back.
1164  * On multi-homed clients, this address depends on which NIC we use to
1165  * route requests to the server.
1166  *
1167  * Returns 1 and fills in @buf if an unambiguous local address is
1168  * available; returns 1 and fills in an appropriate ANYADDR address
1169  * if a local address isn't available; otherwise, returns zero.
1170  */
1171 int nfs_callback_address(const struct sockaddr *sap, const socklen_t salen,
1172                          struct sockaddr *buf, socklen_t *buflen)
1173 {
1174         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1175
1176         if (nfs_ca_sockname(sap, salen, buf, buflen) == 0)
1177                 if (nfs_ca_gai(sap, salen, buf, buflen) == 0)
1178                         goto out_failed;
1179
1180         /*
1181          * The server can't use an interface ID that was generated
1182          * here on the client, so always clear sin6_scope_id.
1183          */
1184         if (sin6->sin6_family == AF_INET6)
1185                 sin6->sin6_scope_id = 0;
1186
1187         return 1;
1188
1189 out_failed:
1190         *buflen = 0;
1191         if (verbose)
1192                 nfs_error(_("%s: failed to construct callback address"));
1193         return 0;
1194
1195 }
1196
1197 /*
1198  * "nfsprog" is only supported by the legacy mount command.  The
1199  * kernel mount client does not support this option.
1200  *
1201  * Returns the value set by the nfsprog= option, the value of
1202  * the RPC NFS program specified in /etc/rpc, or a baked-in
1203  * default program number, if all fails.
1204  */
1205 static rpcprog_t nfs_nfs_program(struct mount_options *options)
1206 {
1207         long tmp;
1208
1209         if (po_get_numeric(options, "nfsprog", &tmp) == PO_FOUND)
1210                 if (tmp >= 0)
1211                         return tmp;
1212         return nfs_getrpcbyname(NFSPROG, nfs_nfs_pgmtbl);
1213 }
1214
1215
1216 /*
1217  * Returns the RPC version number specified by the given mount
1218  * options for the NFS service, or zero if all fails.
1219  */
1220 static rpcvers_t nfs_nfs_version(struct mount_options *options)
1221 {
1222         long tmp;
1223
1224         switch (po_rightmost(options, nfs_version_opttbl)) {
1225         case 0: /* v2 */
1226                 return 2;
1227         case 1: /* v3 */
1228                 return 3;
1229         case 2: /* vers */
1230                 if (po_get_numeric(options, "vers", &tmp) == PO_FOUND)
1231                         if (tmp >= 2 && tmp <= 3)
1232                                 return tmp;
1233                 break;
1234         case 3: /* nfsvers */
1235                 if (po_get_numeric(options, "nfsvers", &tmp) == PO_FOUND)
1236                         if (tmp >= 2 && tmp <= 3)
1237                                 return tmp;
1238                 break;
1239         }
1240
1241         return 0;
1242 }
1243
1244 /*
1245  * Returns the NFS transport protocol specified by the given mount options
1246  *
1247  * Returns the IPPROTO_ value specified by the given mount options, or
1248  * IPPROTO_UDP if all fails.
1249  */
1250 static unsigned short nfs_nfs_protocol(struct mount_options *options)
1251 {
1252         char *option;
1253
1254         switch (po_rightmost(options, nfs_transport_opttbl)) {
1255         case 1: /* tcp */
1256                 return IPPROTO_TCP;
1257         case 2: /* proto */
1258                 option = po_get(options, "proto");
1259                 if (option) {
1260                         if (strcmp(option, "tcp") == 0)
1261                                 return IPPROTO_TCP;
1262                         if (strcmp(option, "udp") == 0)
1263                                 return IPPROTO_UDP;
1264                 }
1265         }
1266
1267         return IPPROTO_UDP;
1268 }
1269
1270 /*
1271  * Returns the NFS server's port number specified by the given
1272  * mount options, or zero if all fails.  Zero results in a portmap
1273  * query to discover the server's mountd service port.
1274  *
1275  * port=0 will guarantee an rpcbind request precedes the first
1276  * NFS RPC so the client can determine the server's port number.
1277  */
1278 static unsigned short nfs_nfs_port(struct mount_options *options)
1279 {
1280         long tmp;
1281
1282         if (po_get_numeric(options, "port", &tmp) == PO_FOUND)
1283                 if (tmp >= 0 && tmp <= 65535)
1284                         return tmp;
1285         return 0;
1286 }
1287
1288 /*
1289  * "mountprog" is only supported by the legacy mount command.  The
1290  * kernel mount client does not support this option.
1291  *
1292  * Returns the value set by the mountprog= option, the value of
1293  * the RPC mount program specified in /etc/rpc, or a baked-in
1294  * default program number, if all fails.
1295  */
1296 static rpcprog_t nfs_mount_program(struct mount_options *options)
1297 {
1298         long tmp;
1299
1300         if (po_get_numeric(options, "mountprog", &tmp) == PO_FOUND)
1301                 if (tmp >= 0)
1302                         return tmp;
1303         return nfs_getrpcbyname(MOUNTPROG, nfs_mnt_pgmtbl);
1304 }
1305
1306 /*
1307  * Returns the RPC version number specified by the given mount options,
1308  * or the version "3" if all fails.
1309  */
1310 static rpcvers_t nfs_mount_version(struct mount_options *options)
1311 {
1312         long tmp;
1313
1314         if (po_get_numeric(options, "mountvers", &tmp) == PO_FOUND)
1315                 if (tmp >= 1 && tmp <= 4)
1316                         return tmp;
1317
1318         return nfsvers_to_mnt(nfs_nfs_version(options));
1319 }
1320
1321 /*
1322  * Returns the transport protocol to use for the mount service
1323  *
1324  * Returns the IPPROTO_ value specified by the mountproto option, or
1325  * if that doesn't exist, the IPPROTO_ value specified for NFS
1326  * itself.
1327  */
1328 static unsigned short nfs_mount_protocol(struct mount_options *options)
1329 {
1330         char *option;
1331
1332         option = po_get(options, "mountproto");
1333         if (option) {
1334                 if (strcmp(option, "tcp") == 0)
1335                         return IPPROTO_TCP;
1336                 if (strcmp(option, "udp") == 0)
1337                         return IPPROTO_UDP;
1338         }
1339
1340         return nfs_nfs_protocol(options);
1341 }
1342
1343 /*
1344  * Returns the mountd server's port number specified by the given
1345  * mount options, or zero if all fails.  Zero results in a portmap
1346  * query to discover the server's mountd service port.
1347  *
1348  * port=0 will guarantee an rpcbind request precedes the mount
1349  * RPC so the client can determine the server's port number.
1350  */
1351 static unsigned short nfs_mount_port(struct mount_options *options)
1352 {
1353         long tmp;
1354
1355         if (po_get_numeric(options, "mountport", &tmp) == PO_FOUND)
1356                 if (tmp >= 0 && tmp <= 65535)
1357                         return tmp;
1358         return 0;
1359 }
1360
1361 /**
1362  * nfs_options2pmap - set up pmap structs based on mount options
1363  * @options: pointer to mount options
1364  * @nfs_pmap: OUT: pointer to pmap arguments for NFS server
1365  * @mnt_pmap: OUT: pointer to pmap arguments for mountd server
1366  *
1367  */
1368 void nfs_options2pmap(struct mount_options *options,
1369                       struct pmap *nfs_pmap, struct pmap *mnt_pmap)
1370 {
1371         nfs_pmap->pm_prog = nfs_nfs_program(options);
1372         nfs_pmap->pm_vers = nfs_nfs_version(options);
1373         nfs_pmap->pm_prot = nfs_nfs_protocol(options);
1374         nfs_pmap->pm_port = nfs_nfs_port(options);
1375
1376         mnt_pmap->pm_prog = nfs_mount_program(options);
1377         mnt_pmap->pm_vers = nfs_mount_version(options);
1378         mnt_pmap->pm_prot = nfs_mount_protocol(options);
1379         mnt_pmap->pm_port = nfs_mount_port(options);
1380 }