3 * the regents of the university of michigan
6 * permission is granted to use, copy, create derivative works and redistribute
7 * this software and such derivative works for any purpose, so long as the name
8 * of the university of michigan is not used in any advertising or publicity
9 * pertaining to the use or distribution of this software without specific,
10 * written prior authorization. if the above copyright notice or any other
11 * identification of the university of michigan is included in any copy of any
12 * portion of this software, then the disclaimer below must also be included.
14 * this software is provided as is, without representation from the university
15 * of michigan as to its fitness for any purpose, and without warranty by the
16 * university of michigan of any kind, either express or implied, including
17 * without limitation the implied warranties of merchantability and fitness for
18 * a particular purpose. the regents of the university of michigan shall not be
19 * liable for any damages, including special, indirect, incidental, or
20 * consequential damages, with respect to any claim arising out of or in
21 * connection with the use of the software, even if it has been or is hereafter
22 * advised of the possibility of such damages.
26 * Copyright (c) 1992, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
30 * Rick Macklem at The University of Guelph.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 static const char copyright[] =
60 "@(#) Copyright (c) 1992, 1993, 1994\n\
61 The Regents of the University of California. All rights reserved.\n";
65 static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95";
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
71 #include <sys/param.h>
72 #include <sys/mount.h>
73 #include <sys/socket.h>
75 #include <sys/syslog.h>
79 #include <rpc/pmap_clnt.h>
80 #include <rpc/pmap_prot.h>
82 #include <nfs/rpcv2.h>
83 #include <nfs/nfsproto.h>
84 #include <nfsclient/nfs.h>
85 #include <nfsclient/nfsargs.h>
87 #include <arpa/inet.h>
102 #include "mounttab.h"
105 #define ALTF_NOCONN 0x2
106 #define ALTF_DUMBTIMR 0x4
107 #define ALTF_INTR 0x8
108 #define ALTF_NFSV3 0x20
109 #define ALTF_RDIRPLUS 0x40
110 #define ALTF_MNTUDP 0x80
111 #define ALTF_RESVPORT 0x100
112 #define ALTF_SEQPACKET 0x200
113 #define ALTF_SOFT 0x800
114 #define ALTF_TCP 0x1000
115 #define ALTF_PORT 0x2000
116 #define ALTF_NFSV2 0x4000
117 #define ALTF_ACREGMIN 0x8000
118 #define ALTF_ACREGMAX 0x10000
119 #define ALTF_ACDIRMIN 0x20000
120 #define ALTF_ACDIRMAX 0x40000
121 #define ALTF_NOLOCKD 0x80000
122 #define ALTF_NOINET4 0x100000
123 #define ALTF_NOINET6 0x200000
125 struct mntopt mopts[] = {
130 { "bg", 0, ALTF_BG, 1 },
131 { "fg", 1, ALTF_BG, 1 },
132 { "conn", 1, ALTF_NOCONN, 1 },
133 { "dumbtimer", 0, ALTF_DUMBTIMR, 1 },
134 { "intr", 0, ALTF_INTR, 1 },
135 { "nfsv3", 0, ALTF_NFSV3, 1 },
136 { "rdirplus", 0, ALTF_RDIRPLUS, 1 },
137 { "mntudp", 0, ALTF_MNTUDP, 1 },
138 { "resvport", 0, ALTF_RESVPORT, 1 },
139 { "soft", 0, ALTF_SOFT, 1 },
140 { "hard", 1, ALTF_SOFT, 1 },
141 { "tcp", 0, ALTF_TCP, 1 },
142 { "port=", 0, ALTF_PORT, 1 },
143 { "nfsv2", 0, ALTF_NFSV2, 1 },
144 { "acregmin=", 0, ALTF_ACREGMIN, 1 },
145 { "acregmax=", 0, ALTF_ACREGMAX, 1 },
146 { "acdirmin=", 0, ALTF_ACDIRMIN, 1 },
147 { "acdirmax=", 0, ALTF_ACDIRMAX, 1 },
148 { "lockd", 1, ALTF_NOLOCKD, 1 },
149 { "inet4", 1, ALTF_NOINET4, 1 },
150 { "inet6", 1, ALTF_NOINET6, 1 },
154 struct nfs_args nfsdefargs = {
157 sizeof (struct sockaddr_in),
170 0, /* was: NQ_DEFLEASE */
171 NFS_MAXDEADTHRESH, /* was: NQ_DEADTHRESH */
180 /* Table for af,sotype -> netid conversions. */
186 {"udp", AF_INET, SOCK_DGRAM},
187 {"tcp", AF_INET, SOCK_STREAM},
188 {"udp6", AF_INET6, SOCK_DGRAM},
189 {"tcp6", AF_INET6, SOCK_STREAM},
198 u_char nfh[NFSX_V3FHMAX];
206 int nfsproto = IPPROTO_UDP;
208 char *portspec = NULL; /* Server nfs port; NULL means look up via rpcbind. */
216 /* Return codes for nfs_tryproto. */
219 TRYRET_TIMEOUT, /* No response received. */
220 TRYRET_REMOTEERR, /* Error received from remote server. */
221 TRYRET_LOCALERR /* Local failure. */
224 int getnfsargs(char *, struct nfs_args *);
225 int getnfs4args(char *, struct nfs_args *);
226 /* void set_rpc_maxgrouplist(int); */
227 struct netconfig *getnetconf_cached(const char *netid);
228 char *netidbytype(int af, int sotype);
229 void usage(void) __dead2;
230 int xdr_dir(XDR *, char *);
231 int xdr_fh(XDR *, struct nfhret *);
232 enum tryret nfs_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai,
233 char *hostp, char *spec, char **errstr);
234 enum tryret nfs4_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai,
235 char *hostp, char *spec, char **errstr);
236 enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr);
239 * Used to set mount flags with getmntopts. Call with dir=TRUE to
240 * initialize altflags from the current mount flags. Call with
241 * dir=FALSE to update mount flags with the new value of altflags after
242 * the call to getmntopts.
245 set_flags(int* altflags, int* nfsflags, int dir)
249 if (*nfsflags & NFSMNT_##nf) \
250 *altflags |= ALTF_##af; \
252 *altflags &= ~ALTF_##af; \
254 if (*altflags & ALTF_##af) \
255 *nfsflags |= NFSMNT_##nf; \
257 *nfsflags &= ~NFSMNT_##nf; \
278 main(int argc, char *argv[])
281 struct nfs_args *nfsargsp;
282 struct nfs_args nfsargs;
284 int mntflags, altflags, num;
286 char *name, *p, *spec, *fstype;
287 char mntpath[MAXPATHLEN], errmsg[255];
291 nfsargs = nfsdefargs;
295 memset(errmsg, 0, sizeof(errmsg));
297 fstype = strrchr(argv[0], '_');
299 errx(EX_USAGE, "argv[0] must end in _fstype");
303 if (strcmp(fstype, "nfs4") == 0) {
304 nfsproto = IPPROTO_TCP;
306 nfsdefargs.sotype = SOCK_STREAM;
310 while ((c = getopt(argc, argv,
311 "234a:bcdD:g:I:iLlNo:PR:r:sTt:w:x:U")) != -1)
324 num = strtol(optarg, &p, 10);
326 errx(1, "illegal -a value -- %s", optarg);
327 nfsargsp->readahead = num;
328 nfsargsp->flags |= NFSMNT_READAHEAD;
334 nfsargsp->flags |= NFSMNT_NOCONN;
337 num = strtol(optarg, &p, 10);
339 errx(1, "illegal -D value -- %s", optarg);
340 nfsargsp->deadthresh = num;
341 nfsargsp->flags |= NFSMNT_DEADTHRESH;
344 nfsargsp->flags |= NFSMNT_DUMBTIMR;
348 num = strtol(optarg, &p, 10);
350 errx(1, "illegal -g value -- %s", optarg);
351 set_rpc_maxgrouplist(num);
352 nfsargsp->maxgrouplist = num;
353 nfsargsp->flags |= NFSMNT_MAXGRPS;
357 num = strtol(optarg, &p, 10);
359 errx(1, "illegal -I value -- %s", optarg);
360 nfsargsp->readdirsize = num;
361 nfsargsp->flags |= NFSMNT_READDIRSIZE;
364 nfsargsp->flags |= NFSMNT_INT;
367 nfsargsp->flags |= NFSMNT_NOLOCKD;
370 nfsargsp->flags |= NFSMNT_RDIRPLUS;
373 nfsargsp->flags &= ~NFSMNT_RESVPORT;
377 set_flags(&altflags, &nfsargsp->flags, TRUE);
379 altflags |= ALTF_NFSV2;
380 else if (mountmode == V3)
381 altflags |= ALTF_NFSV3;
382 getmntopts(optarg, mopts, &mntflags, &altflags);
383 set_flags(&altflags, &nfsargsp->flags, FALSE);
385 * Handle altflags which don't map directly to
388 if (altflags & ALTF_BG)
390 if (altflags & ALTF_NOINET4)
391 opflags |= OF_NOINET4;
392 if (altflags & ALTF_NOINET6)
393 opflags |= OF_NOINET6;
394 if (altflags & ALTF_MNTUDP)
396 if (altflags & ALTF_TCP) {
397 nfsargsp->sotype = SOCK_STREAM;
398 nfsproto = IPPROTO_TCP;
400 if (altflags & ALTF_PORT) {
402 * XXX Converting from a string to an int
403 * and back again is silly, and we should
404 * allow /etc/services names.
406 p = strstr(optarg, "port=");
408 asprintf(&portspec, "%d",
410 if (portspec == NULL)
415 if (altflags & ALTF_NFSV2)
417 if (altflags & ALTF_NFSV3)
419 if (altflags & ALTF_ACREGMIN) {
420 p = strstr(optarg, "acregmin=");
422 nfsargsp->acregmin = atoi(p + 9);
424 if (altflags & ALTF_ACREGMAX) {
425 p = strstr(optarg, "acregmax=");
427 nfsargsp->acregmax = atoi(p + 9);
429 if (altflags & ALTF_ACDIRMIN) {
430 p = strstr(optarg, "acdirmin=");
432 nfsargsp->acdirmin = atoi(p + 9);
434 if (altflags & ALTF_ACDIRMAX) {
435 p = strstr(optarg, "acdirmax=");
437 nfsargsp->acdirmax = atoi(p + 9);
441 /* obsolete for NFSMNT_RESVPORT, now default */
444 num = strtol(optarg, &p, 10);
446 errx(1, "illegal -R value -- %s", optarg);
450 num = strtol(optarg, &p, 10);
452 errx(1, "illegal -r value -- %s", optarg);
453 nfsargsp->rsize = num;
454 nfsargsp->flags |= NFSMNT_RSIZE;
457 nfsargsp->flags |= NFSMNT_SOFT;
460 nfsargsp->sotype = SOCK_STREAM;
461 nfsproto = IPPROTO_TCP;
464 num = strtol(optarg, &p, 10);
466 errx(1, "illegal -t value -- %s", optarg);
467 nfsargsp->timeo = num;
468 nfsargsp->flags |= NFSMNT_TIMEO;
471 num = strtol(optarg, &p, 10);
473 errx(1, "illegal -w value -- %s", optarg);
474 nfsargsp->wsize = num;
475 nfsargsp->flags |= NFSMNT_WSIZE;
478 num = strtol(optarg, &p, 10);
480 errx(1, "illegal -x value -- %s", optarg);
481 nfsargsp->retrans = num;
482 nfsargsp->flags |= NFSMNT_RETRANS;
486 nfsargsp->sotype = SOCK_DGRAM;
487 nfsproto = IPPROTO_UDP;
505 /* The default is to keep retrying forever. */
508 if (mountmode == V4) {
509 if (!getnfs4args(spec, nfsargsp))
512 if (!getnfsargs(spec, nfsargsp))
516 /* resolve the mountpoint with realpath(3) */
517 (void)checkpath(name, mntpath);
519 build_iovec(&iov, &iovlen, "nfs_args", nfsargsp, sizeof(*nfsargsp));
520 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
521 build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
522 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
524 if (nmount(iov, iovlen, mntflags))
525 err(1, "%s, %s", mntpath, errmsg);
531 getnfsargs(char *spec, struct nfs_args *nfsargsp)
533 struct addrinfo hints, *ai_nfs, *ai;
535 int ecode, speclen, remoteerr;
536 char *hostp, *delimp, *errstr;
538 static char nam[MNAMELEN + 1];
540 if ((delimp = strrchr(spec, ':')) != NULL) {
543 } else if ((delimp = strrchr(spec, '@')) != NULL) {
544 warnx("path@server syntax is deprecated, use server:path");
547 warnx("no <host>:<dirpath> nfs-name");
553 * If there has been a trailing slash at mounttime it seems
554 * that some mountd implementations fail to remove the mount
555 * entries from their mountlist while unmounting.
557 for (speclen = strlen(spec);
558 speclen > 1 && spec[speclen - 1] == '/';
560 spec[speclen - 1] = '\0';
561 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
562 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
565 /* Make both '@' and ':' notations equal */
566 if (*hostp != '\0') {
568 memmove(nam, hostp, len);
570 memmove(nam + len + 1, spec, speclen);
571 nam[len + speclen + 1] = '\0';
575 * Handle an internet host address.
577 memset(&hints, 0, sizeof hints);
578 hints.ai_flags = AI_NUMERICHOST;
579 hints.ai_socktype = nfsargsp->sotype;
580 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) {
582 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs))
584 if (portspec == NULL)
585 errx(1, "%s: %s", hostp, gai_strerror(ecode));
587 errx(1, "%s:%s: %s", hostp, portspec,
588 gai_strerror(ecode));
593 ret = TRYRET_LOCALERR;
596 * Try each entry returned by getaddrinfo(). Note the
597 * occurence of remote errors by setting `remoteerr'.
600 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
601 if ((ai->ai_family == AF_INET6) &&
602 (opflags & OF_NOINET6))
604 if ((ai->ai_family == AF_INET) &&
605 (opflags & OF_NOINET4))
607 ret = nfs_tryproto(nfsargsp, ai, hostp, spec, &errstr);
608 if (ret == TRYRET_SUCCESS)
610 if (ret != TRYRET_LOCALERR)
612 if ((opflags & ISBGRND) == 0)
613 fprintf(stderr, "%s\n", errstr);
615 if (ret == TRYRET_SUCCESS)
618 /* Exit if all errors were local. */
623 * If retrycnt == 0, we are to keep retrying forever.
624 * Otherwise decrement it, and exit if it hits zero.
626 if (retrycnt != 0 && --retrycnt == 0)
629 if ((opflags & (BGRND | ISBGRND)) == BGRND) {
630 warnx("Cannot immediately mount %s:%s, backgrounding",
633 if (daemon(0, 0) != 0)
638 freeaddrinfo(ai_nfs);
639 nfsargsp->hostname = nam;
640 /* Add mounted file system to PATH_MOUNTTAB */
641 if (!add_mtab(hostp, spec))
642 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec);
648 getnfs4args(char *spec, struct nfs_args *nfsargsp)
650 struct addrinfo hints, *ai_nfs, *ai;
652 int ecode, speclen, remoteerr;
653 char *hostp, *delimp, *errstr;
655 static char nam[MNAMELEN + 1];
657 if ((delimp = strrchr(spec, ':')) != NULL) {
660 } else if ((delimp = strrchr(spec, '@')) != NULL) {
661 warnx("path@server syntax is deprecated, use server:path");
664 warnx("no <host>:<dirpath> nfs-name");
670 * If there has been a trailing slash at mounttime it seems
671 * that some mountd implementations fail to remove the mount
672 * entries from their mountlist while unmounting.
674 for (speclen = strlen(spec);
675 speclen > 1 && spec[speclen - 1] == '/';
677 spec[speclen - 1] = '\0';
678 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
679 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
682 /* Make both '@' and ':' notations equal */
683 if (*hostp != '\0') {
685 memmove(nam, hostp, len);
687 memmove(nam + len + 1, spec, speclen);
688 nam[len + speclen + 1] = '\0';
692 * Handle an internet host address.
694 memset(&hints, 0, sizeof hints);
695 hints.ai_flags = AI_NUMERICHOST;
696 hints.ai_socktype = nfsargsp->sotype;
697 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) {
699 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs))
701 if (portspec == NULL)
702 errx(1, "%s: %s", hostp, gai_strerror(ecode));
704 errx(1, "%s:%s: %s", hostp, portspec,
705 gai_strerror(ecode));
710 ret = TRYRET_LOCALERR;
713 * Try each entry returned by getaddrinfo(). Note the
714 * occurence of remote errors by setting `remoteerr'.
717 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
718 if ((ai->ai_family == AF_INET6) &&
719 (opflags & OF_NOINET6))
721 if ((ai->ai_family == AF_INET) &&
722 (opflags & OF_NOINET4))
724 ret = nfs4_tryproto(nfsargsp, ai, hostp, spec, &errstr);
725 if (ret == TRYRET_SUCCESS)
727 if (ret != TRYRET_LOCALERR)
729 if ((opflags & ISBGRND) == 0)
730 fprintf(stderr, "%s\n", errstr);
732 if (ret == TRYRET_SUCCESS)
735 /* Exit if all errors were local. */
740 * If retrycnt == 0, we are to keep retrying forever.
741 * Otherwise decrement it, and exit if it hits zero.
743 if (retrycnt != 0 && --retrycnt == 0)
746 if ((opflags & (BGRND | ISBGRND)) == BGRND) {
747 warnx("Cannot immediately mount %s:%s, backgrounding",
750 if (daemon(0, 0) != 0)
755 freeaddrinfo(ai_nfs);
756 nfsargsp->hostname = nam;
757 /* Add mounted file system to PATH_MOUNTTAB */
758 if (!add_mtab(hostp, spec))
759 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec);
764 * Try to set up the NFS arguments according to the address
765 * family, protocol (and possibly port) specified in `ai'.
767 * Returns TRYRET_SUCCESS if successful, or:
768 * TRYRET_TIMEOUT The server did not respond.
769 * TRYRET_REMOTEERR The server reported an error.
770 * TRYRET_LOCALERR Local failure.
772 * In all error cases, *errstr will be set to a statically-allocated string
773 * describing the error.
776 nfs_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai, char *hostp,
777 char *spec, char **errstr)
779 static char errbuf[256];
780 struct sockaddr_storage nfs_ss;
781 struct netbuf nfs_nb;
782 struct nfhret nfhret;
784 struct rpc_err rpcerr;
786 struct netconfig *nconf, *nconf_mnt;
787 char *netid, *netid_mnt;
788 int doconnect, nfsvers, mntvers;
790 enum mountmode trymntmode;
792 trymntmode = mountmode;
796 if ((netid = netidbytype(ai->ai_family, nfsargsp->sotype)) == NULL) {
797 snprintf(errbuf, sizeof errbuf,
798 "af %d sotype %d not supported", ai->ai_family,
800 return (TRYRET_LOCALERR);
802 if ((nconf = getnetconf_cached(netid)) == NULL) {
803 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror());
804 return (TRYRET_LOCALERR);
806 /* The RPCPROG_MNT netid may be different. */
811 if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM))
813 snprintf(errbuf, sizeof errbuf,
814 "af %d sotype SOCK_DGRAM not supported",
816 return (TRYRET_LOCALERR);
818 if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) {
819 snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt,
821 return (TRYRET_LOCALERR);
826 if (trymntmode == V2) {
834 if (portspec != NULL) {
835 /* `ai' contains the complete nfsd sockaddr. */
836 nfs_nb.buf = ai->ai_addr;
837 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen;
839 /* Ask the remote rpcbind. */
840 nfs_nb.buf = &nfs_ss;
841 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss;
843 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb,
845 if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH &&
850 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s",
852 clnt_spcreateerror("RPCPROG_NFS"));
853 return (returncode(rpc_createerr.cf_stat,
854 &rpc_createerr.cf_error));
858 /* Check that the server (nfsd) responds on the port we have chosen. */
859 clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, RPCPROG_NFS, nfsvers,
862 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
863 hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS"));
864 return (returncode(rpc_createerr.cf_stat,
865 &rpc_createerr.cf_error));
867 if (nfsargsp->sotype == SOCK_DGRAM &&
868 !(nfsargsp->flags & NFSMNT_NOCONN)) {
870 * Use connect(), to match what the kernel does. This
871 * catches cases where the server responds from the
872 * wrong source address.
875 if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) {
877 snprintf(errbuf, sizeof errbuf,
878 "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp,
880 return (TRYRET_LOCALERR);
886 stat = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL,
887 (xdrproc_t)xdr_void, NULL,
889 if (stat != RPC_SUCCESS) {
890 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
895 clnt_geterr(clp, &rpcerr);
896 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
897 hostp, spec, clnt_sperror(clp, "NFSPROC_NULL"));
899 return (returncode(stat, &rpcerr));
903 /* Send the RPCMNT_MOUNT RPC to get the root filehandle. */
906 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers, nconf_mnt);
908 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
909 hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create"));
910 return (returncode(rpc_createerr.cf_stat,
911 &rpc_createerr.cf_error));
913 clp->cl_auth = authsys_create_default();
914 nfhret.auth = RPCAUTH_UNIX;
915 nfhret.vers = mntvers;
916 stat = clnt_call(clp, RPCMNT_MOUNT, (xdrproc_t)xdr_dir, spec,
917 (xdrproc_t)xdr_fh, &nfhret,
919 auth_destroy(clp->cl_auth);
920 if (stat != RPC_SUCCESS) {
921 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
926 clnt_geterr(clp, &rpcerr);
927 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
928 hostp, spec, clnt_sperror(clp, "RPCPROG_MNT"));
930 return (returncode(stat, &rpcerr));
934 if (nfhret.stat != 0) {
935 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
936 hostp, spec, strerror(nfhret.stat));
937 return (TRYRET_REMOTEERR);
941 * Store the filehandle and server address in nfsargsp, making
942 * sure to copy any locally allocated structures.
944 nfsargsp->addrlen = nfs_nb.len;
945 nfsargsp->addr = malloc(nfsargsp->addrlen);
946 nfsargsp->fhsize = nfhret.fhsize;
947 nfsargsp->fh = malloc(nfsargsp->fhsize);
948 if (nfsargsp->addr == NULL || nfsargsp->fh == NULL)
950 bcopy(nfs_nb.buf, nfsargsp->addr, nfsargsp->addrlen);
951 bcopy(nfhret.nfh, nfsargsp->fh, nfsargsp->fhsize);
954 nfsargsp->flags |= NFSMNT_NFSV3;
956 nfsargsp->flags &= ~NFSMNT_NFSV3;
958 return (TRYRET_SUCCESS);
963 * Try to set up the NFS arguments according to the address
964 * family, protocol (and possibly port) specified in `ai'.
966 * Returns TRYRET_SUCCESS if successful, or:
967 * TRYRET_TIMEOUT The server did not respond.
968 * TRYRET_REMOTEERR The server reported an error.
969 * TRYRET_LOCALERR Local failure.
971 * In all error cases, *errstr will be set to a statically-allocated string
972 * describing the error.
975 nfs4_tryproto(struct nfs_args *nfsargsp, struct addrinfo *ai, char *hostp,
976 char *spec, char **errstr)
978 static char errbuf[256];
979 struct sockaddr_storage nfs_ss;
980 struct netbuf nfs_nb;
981 struct netconfig *nconf;
988 if ((netid = netidbytype(ai->ai_family, nfsargsp->sotype)) == NULL) {
989 snprintf(errbuf, sizeof errbuf,
990 "af %d sotype %d not supported", ai->ai_family,
992 return (TRYRET_LOCALERR);
994 if ((nconf = getnetconf_cached(netid)) == NULL) {
995 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror());
996 return (TRYRET_LOCALERR);
1001 if (portspec != NULL && atoi(portspec) != 0) {
1002 /* `ai' contains the complete nfsd sockaddr. */
1003 nfs_nb.buf = ai->ai_addr;
1004 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen;
1006 /* Ask the remote rpcbind. */
1007 nfs_nb.buf = &nfs_ss;
1008 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss;
1010 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb,
1012 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s",
1014 clnt_spcreateerror("RPCPROG_NFS"));
1015 return (returncode(rpc_createerr.cf_stat,
1016 &rpc_createerr.cf_error));
1021 * Store the filehandle and server address in nfsargsp, making
1022 * sure to copy any locally allocated structures.
1024 nfsargsp->addrlen = nfs_nb.len;
1025 nfsargsp->addr = malloc(nfsargsp->addrlen);
1027 if (nfsargsp->addr == NULL)
1029 bcopy(nfs_nb.buf, nfsargsp->addr, nfsargsp->addrlen);
1032 nfsargsp->flags |= (NFSMNT_NFSV3 | NFSMNT_NFSV4);
1034 return (TRYRET_SUCCESS);
1038 * Catagorise a RPC return status and error into an `enum tryret'
1042 returncode(enum clnt_stat stat, struct rpc_err *rpcerr)
1046 return (TRYRET_TIMEOUT);
1047 case RPC_PMAPFAILURE:
1048 case RPC_PROGNOTREGISTERED:
1049 case RPC_PROGVERSMISMATCH:
1050 /* XXX, these can be local or remote. */
1053 return (TRYRET_REMOTEERR);
1054 case RPC_SYSTEMERROR:
1055 switch (rpcerr->re_errno) {
1057 return (TRYRET_TIMEOUT);
1061 return (TRYRET_REMOTEERR);
1067 return (TRYRET_LOCALERR);
1071 * Look up a netid based on an address family and socket type.
1072 * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM.
1074 * XXX there should be a library function for this.
1077 netidbytype(int af, int sotype)
1079 struct nc_protos *p;
1081 for (p = nc_protos; p->netid != NULL; p++) {
1082 if (af != p->af || sotype != p->sotype)
1090 * Look up a netconfig entry based on a netid, and cache the result so
1091 * that we don't need to remember to call freenetconfigent().
1093 * Otherwise it behaves just like getnetconfigent(), so nc_*error()
1097 getnetconf_cached(const char *netid)
1099 static struct nc_entry {
1100 struct netconfig *nconf;
1101 struct nc_entry *next;
1104 struct netconfig *nconf;
1106 for (p = head; p != NULL; p = p->next)
1107 if (strcmp(netid, p->nconf->nc_netid) == 0)
1110 if ((nconf = getnetconfigent(netid)) == NULL)
1112 if ((p = malloc(sizeof(*p))) == NULL)
1122 * xdr routines for mount rpc's
1125 xdr_dir(XDR *xdrsp, char *dirp)
1127 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
1131 xdr_fh(XDR *xdrsp, struct nfhret *np)
1134 long auth, authcnt, authfnd = 0;
1136 if (!xdr_u_long(xdrsp, &np->stat))
1142 np->fhsize = NFSX_V2FH;
1143 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
1145 if (!xdr_long(xdrsp, &np->fhsize))
1147 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
1149 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
1151 if (!xdr_long(xdrsp, &authcnt))
1153 for (i = 0; i < authcnt; i++) {
1154 if (!xdr_long(xdrsp, &auth))
1156 if (auth == np->auth)
1160 * Some servers, such as DEC's OSF/1 return a nil authenticator
1161 * list to indicate RPCAUTH_UNIX.
1163 if (!authfnd && (authcnt > 0 || np->auth != RPCAUTH_UNIX))
1173 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
1174 "usage: mount_nfs [-234bcdiLlNPsTU] [-a maxreadahead] [-D deadthresh]",
1175 " [-g maxgroups] [-I readdirsize] [-o options] [-R retrycnt]",
1176 " [-r readsize] [-t timeout] [-w writesize] [-x retrans]",
1177 " rhost:path node");