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>
86 #include <arpa/inet.h>
101 #include "mounttab.h"
103 /* Table for af,sotype -> netid conversions. */
109 {"udp", AF_INET, SOCK_DGRAM},
110 {"tcp", AF_INET, SOCK_STREAM},
111 {"udp6", AF_INET6, SOCK_DGRAM},
112 {"tcp6", AF_INET6, SOCK_STREAM},
121 u_char nfh[NFSX_V3FHMAX];
129 int nfsproto = IPPROTO_UDP;
132 char *portspec = NULL; /* Server nfs port; NULL means look up via rpcbind. */
133 struct sockaddr *addr;
145 /* Return codes for nfs_tryproto. */
148 TRYRET_TIMEOUT, /* No response received. */
149 TRYRET_REMOTEERR, /* Error received from remote server. */
150 TRYRET_LOCALERR /* Local failure. */
153 int fallback_mount(struct iovec *iov, int iovlen, int mntflags);
154 int getnfsargs(char *, struct iovec **iov, int *iovlen);
155 int getnfs4args(char *, struct iovec **iov, int *iovlen);
156 /* void set_rpc_maxgrouplist(int); */
157 struct netconfig *getnetconf_cached(const char *netid);
158 const char *netidbytype(int af, int sotype);
159 void usage(void) __dead2;
160 int xdr_dir(XDR *, char *);
161 int xdr_fh(XDR *, struct nfhret *);
162 enum tryret nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec,
163 char **errstr, struct iovec **iov, int *iovlen);
164 enum tryret nfs4_tryproto(struct addrinfo *ai, char *hostp, char *spec,
166 enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr);
167 extern int getosreldate(void);
170 main(int argc, char *argv[])
174 int mntflags, num, iovlen;
176 char *name, *p, *spec, *fstype;
177 char mntpath[MAXPATHLEN], errmsg[255];
182 memset(errmsg, 0, sizeof(errmsg));
184 fstype = strrchr(argv[0], '_');
186 errx(EX_USAGE, "argv[0] must end in _fstype");
190 if (strcmp(fstype, "nfs4") == 0) {
191 nfsproto = IPPROTO_TCP;
193 build_iovec(&iov, &iovlen, "tcp", NULL, 0);
197 while ((c = getopt(argc, argv,
198 "234a:bcdD:g:I:iLlNo:PR:r:sTt:w:x:U")) != -1)
211 printf("-a deprecated, use -o readhead=<value>\n");
212 build_iovec(&iov, &iovlen, "readahead", optarg, (size_t)-1);
218 printf("-c deprecated, use -o noconn\n");
219 build_iovec(&iov, &iovlen, "noconn", NULL, 0);
223 printf("-D deprecated, use -o deadthresh=<value>\n");
224 build_iovec(&iov, &iovlen, "deadthresh", optarg, (size_t)-1);
227 printf("-d deprecated, use -o dumbtimer");
228 build_iovec(&iov, &iovlen, "dumbtimer", NULL, 0);
231 printf("-g deprecated, use -o maxgroups");
232 num = strtol(optarg, &p, 10);
234 errx(1, "illegal -g value -- %s", optarg);
235 //set_rpc_maxgrouplist(num);
236 build_iovec(&iov, &iovlen, "maxgroups", optarg, (size_t)-1);
239 printf("-I deprecated, use -o readdirsize=<value>\n");
240 build_iovec(&iov, &iovlen, "readdirsize", optarg, (size_t)-1);
243 printf("-i deprecated, use -o intr\n");
244 build_iovec(&iov, &iovlen, "intr", NULL, 0);
247 printf("-i deprecated, use -o nolockd\n");
248 build_iovec(&iov, &iovlen, "nolockd", NULL, 0);
251 printf("-l deprecated, -o rdirplus\n");
252 build_iovec(&iov, &iovlen, "rdirplus", NULL, 0);
255 printf("-N deprecated, do not specify -o resvport\n");
258 int pass_flag_to_nmount;
262 char *pnextopt = NULL;
264 pass_flag_to_nmount = 1;
265 pval = strchr(opt, '=');
266 pnextopt = strchr(opt, ',');
275 if (strcmp(opt, "bg") == 0) {
277 pass_flag_to_nmount=0;
278 } else if (strcmp(opt, "fg") == 0) {
279 /* same as not specifying -o bg */
280 pass_flag_to_nmount=0;
281 } else if (strcmp(opt, "mntudp") == 0) {
283 nfsproto = IPPROTO_UDP;
284 } else if (strcmp(opt, "udp") == 0) {
285 nfsproto = IPPROTO_UDP;
286 } else if (strcmp(opt, "tcp") == 0) {
287 nfsproto = IPPROTO_TCP;
288 } else if (strcmp(opt, "noinet4") == 0) {
289 pass_flag_to_nmount=0;
290 opflags |= OF_NOINET4;
291 } else if (strcmp(opt, "noinet6") == 0) {
292 pass_flag_to_nmount=0;
293 opflags |= OF_NOINET6;
294 } else if (strcmp(opt, "noconn") == 0) {
296 } else if (strcmp(opt, "nfsv2") == 0) {
297 pass_flag_to_nmount=0;
299 } else if (strcmp(opt, "nfsv3") == 0) {
301 } else if (strcmp(opt, "nfsv4") == 0) {
302 pass_flag_to_nmount=0;
305 } else if (strcmp(opt, "port") == 0) {
306 pass_flag_to_nmount=0;
307 asprintf(&portspec, "%d",
309 if (portspec == NULL)
311 } else if (strcmp(opt, "retrycnt") == 0) {
312 pass_flag_to_nmount=0;
313 num = strtol(val, &p, 10);
315 errx(1, "illegal retrycnt value -- %s", val);
317 } else if (strcmp(opt, "maxgroups") == 0) {
318 num = strtol(val, &p, 10);
320 errx(1, "illegal maxgroups value -- %s", val);
321 //set_rpc_maxgrouplist(num);
323 if (pass_flag_to_nmount)
324 build_iovec(&iov, &iovlen, opt, val,
331 /* obsolete for -o noresvport now default */
332 printf("-P deprecated, use -o noresvport\n");
333 build_iovec(&iov, &iovlen, "noresvport", NULL, 0);
336 printf("-R deprecated, use -o retrycnt=<retrycnt>\n");
337 num = strtol(optarg, &p, 10);
339 errx(1, "illegal -R value -- %s", optarg);
343 printf("-r deprecated, use -o rsize=<rsize>\n");
344 build_iovec(&iov, &iovlen, "rsize", optarg, (size_t)-1);
347 printf("-s deprecated, use -o soft\n");
348 build_iovec(&iov, &iovlen, "soft", NULL, 0);
351 nfsproto = IPPROTO_TCP;
352 printf("-T deprecated, use -o tcp\n");
355 printf("-t deprecated, use -o timeout=<value>\n");
356 build_iovec(&iov, &iovlen, "timeout", optarg, (size_t)-1);
359 printf("-w deprecated, use -o wsize=<value>\n");
360 build_iovec(&iov, &iovlen, "wsize", optarg, (size_t)-1);
363 printf("-x deprecated, use -o retrans=<value>\n");
364 build_iovec(&iov, &iovlen, "retrans", optarg, (size_t)-1);
367 printf("-U deprecated, use -o mntudp\n");
369 nfsproto = IPPROTO_UDP;
370 build_iovec(&iov, &iovlen, "mntudp", NULL, 0);
388 /* The default is to keep retrying forever. */
391 if (mountmode == V4) {
392 if (!getnfs4args(spec, &iov, &iovlen))
395 if (!getnfsargs(spec, &iov, &iovlen))
399 /* resolve the mountpoint with realpath(3) */
400 (void)checkpath(name, mntpath);
402 build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
403 build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
404 build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg));
408 * Backwards compatibility routines for older kernels.
409 * Remove this and fallback_mount() code when we do not need to support
410 * NFS mounts against older kernels which still need
411 * struct nfs_args to be passed in via nmount().
413 osversion = getosreldate();
414 if (osversion >= 800048) {
415 if (nmount(iov, iovlen, mntflags))
416 err(1, "%s, %s", mntpath, errmsg);
418 if (fallback_mount(iov, iovlen, mntflags))
419 err(1, "%s, %s", mntpath, errmsg);
426 findopt(struct iovec *iov, int iovlen, const char *name,
427 char **valuep, int *lenp)
431 for (i = 0; i < iovlen/2; i++, iov += 2) {
432 if (strcmp(name, iov[0].iov_base) == 0) {
434 *valuep = iov[1].iov_base;
436 *lenp = iov[1].iov_len;
444 copyopt(struct iovec **newiov, int *newiovlen,
445 struct iovec *iov, int iovlen, const char *name)
450 if (findopt(iov, iovlen, name, &value, &len) == 0)
451 build_iovec(newiov, newiovlen, name, value, len);
455 fallback_mount(struct iovec *iov, int iovlen, int mntflags)
457 struct nfs_args args = {
458 .version = NFS_ARGSVERSION,
460 .addrlen = sizeof (struct sockaddr_in),
461 .sotype = SOCK_STREAM,
465 .flags = NFSMNT_RESVPORT,
468 .readdirsize = NFS_READDIRSIZE,
470 .retrans = NFS_RETRANS,
471 .maxgrouplist = NFS_MAXGRPS,
472 .readahead = NFS_DEFRAHEAD,
473 .wcommitsize = 0, /* was: NQ_DEFLEASE */
474 .deadthresh = NFS_MAXDEADTHRESH, /* was: NQ_DEADTHRESH */
477 .acregmin = NFS_MINATTRTIMO,
478 .acregmax = NFS_MAXATTRTIMO,
479 .acdirmin = NFS_MINDIRATTRTIMO,
480 .acdirmax = NFS_MAXDIRATTRTIMO,
484 struct iovec *newiov;
487 if (findopt(iov, iovlen, "dumbtimer", NULL, NULL) == 0)
488 args.flags |= NFSMNT_DUMBTIMR;
489 if (findopt(iov, iovlen, "noconn", NULL, NULL) == 0)
490 args.flags |= NFSMNT_NOCONN;
491 if (findopt(iov, iovlen, "conn", NULL, NULL) == 0)
492 args.flags |= NFSMNT_NOCONN;
493 if (findopt(iov, iovlen, "nolockd", NULL, NULL) == 0)
494 args.flags |= NFSMNT_NOLOCKD;
495 if (findopt(iov, iovlen, "lockd", NULL, NULL) == 0)
496 args.flags &= ~NFSMNT_NOLOCKD;
497 if (findopt(iov, iovlen, "intr", NULL, NULL) == 0)
498 args.flags |= NFSMNT_INT;
499 if (findopt(iov, iovlen, "rdirplus", NULL, NULL) == 0)
500 args.flags |= NFSMNT_RDIRPLUS;
501 if (findopt(iov, iovlen, "resvport", NULL, NULL) == 0)
502 args.flags |= NFSMNT_RESVPORT;
503 if (findopt(iov, iovlen, "noresvport", NULL, NULL) == 0)
504 args.flags &= ~NFSMNT_RESVPORT;
505 if (findopt(iov, iovlen, "soft", NULL, NULL) == 0)
506 args.flags |= NFSMNT_SOFT;
507 if (findopt(iov, iovlen, "hard", NULL, NULL) == 0)
508 args.flags &= ~NFSMNT_SOFT;
509 if (findopt(iov, iovlen, "mntudp", NULL, NULL) == 0)
510 args.sotype = SOCK_DGRAM;
511 if (findopt(iov, iovlen, "udp", NULL, NULL) == 0)
512 args.sotype = SOCK_DGRAM;
513 if (findopt(iov, iovlen, "tcp", NULL, NULL) == 0)
514 args.sotype = SOCK_STREAM;
515 if (findopt(iov, iovlen, "nfsv3", NULL, NULL) == 0)
516 args.flags |= NFSMNT_NFSV3;
517 if (findopt(iov, iovlen, "readdirsize", &opt, NULL) == 0) {
519 errx(1, "illegal readdirsize");
521 ret = sscanf(opt, "%d", &args.readdirsize);
522 if (ret != 1 || args.readdirsize <= 0) {
523 errx(1, "illegal readdirsize: %s", opt);
525 args.flags |= NFSMNT_READDIRSIZE;
527 if (findopt(iov, iovlen, "readahead", &opt, NULL) == 0) {
529 errx(1, "illegal readahead");
531 ret = sscanf(opt, "%d", &args.readahead);
532 if (ret != 1 || args.readahead <= 0) {
533 errx(1, "illegal readahead: %s", opt);
535 args.flags |= NFSMNT_READAHEAD;
537 if (findopt(iov, iovlen, "wsize", &opt, NULL) == 0) {
539 errx(1, "illegal wsize");
541 ret = sscanf(opt, "%d", &args.wsize);
542 if (ret != 1 || args.wsize <= 0) {
543 errx(1, "illegal wsize: %s", opt);
545 args.flags |= NFSMNT_WSIZE;
547 if (findopt(iov, iovlen, "rsize", &opt, NULL) == 0) {
549 errx(1, "illegal rsize");
551 ret = sscanf(opt, "%d", &args.rsize);
552 if (ret != 1 || args.rsize <= 0) {
553 errx(1, "illegal wsize: %s", opt);
555 args.flags |= NFSMNT_RSIZE;
557 if (findopt(iov, iovlen, "retrans", &opt, NULL) == 0) {
559 errx(1, "illegal retrans");
561 ret = sscanf(opt, "%d", &args.retrans);
562 if (ret != 1 || args.retrans <= 0) {
563 errx(1, "illegal retrans: %s", opt);
565 args.flags |= NFSMNT_RETRANS;
567 if (findopt(iov, iovlen, "acregmin", &opt, NULL) == 0) {
568 ret = sscanf(opt, "%d", &args.acregmin);
569 if (ret != 1 || args.acregmin <= 0) {
570 errx(1, "illegal acregmin: %s", opt);
573 if (findopt(iov, iovlen, "acregmax", &opt, NULL) == 0) {
574 ret = sscanf(opt, "%d", &args.acregmax);
575 if (ret != 1 || args.acregmax <= 0) {
576 errx(1, "illegal acregmax: %s", opt);
579 if (findopt(iov, iovlen, "acdirmin", &opt, NULL) == 0) {
580 ret = sscanf(opt, "%d", &args.acdirmin);
581 if (ret != 1 || args.acdirmin <= 0) {
582 errx(1, "illegal acdirmin: %s", opt);
585 if (findopt(iov, iovlen, "acdirmax", &opt, NULL) == 0) {
586 ret = sscanf(opt, "%d", &args.acdirmax);
587 if (ret != 1 || args.acdirmax <= 0) {
588 errx(1, "illegal acdirmax: %s", opt);
591 if (findopt(iov, iovlen, "deadthresh", &opt, NULL) == 0) {
592 ret = sscanf(opt, "%d", &args.deadthresh);
593 if (ret != 1 || args.deadthresh <= 0) {
594 errx(1, "illegal deadthresh: %s", opt);
596 args.flags |= NFSMNT_DEADTHRESH;
598 if (findopt(iov, iovlen, "timeout", &opt, NULL) == 0) {
599 ret = sscanf(opt, "%d", &args.timeo);
600 if (ret != 1 || args.timeo <= 0) {
601 errx(1, "illegal timeout: %s", opt);
603 args.flags |= NFSMNT_TIMEO;
605 if (findopt(iov, iovlen, "maxgroups", &opt, NULL) == 0) {
606 ret = sscanf(opt, "%d", &args.maxgrouplist);
607 if (ret != 1 || args.timeo <= 0) {
608 errx(1, "illegal maxgroups: %s", opt);
610 args.flags |= NFSMNT_MAXGRPS;
612 if (findopt(iov, iovlen, "addr", &opt,
613 &args.addrlen) == 0) {
614 args.addr = (struct sockaddr *) opt;
616 if (findopt(iov, iovlen, "fh", &opt, &args.fhsize) == 0) {
619 if (findopt(iov, iovlen, "hostname", &args.hostname,
622 if (args.hostname == NULL) {
623 errx(1, "Invalid hostname");
629 build_iovec(&newiov, &newiovlen, "nfs_args", &args, sizeof(args));
630 copyopt(&newiov, &newiovlen, iov, iovlen, "fstype");
631 copyopt(&newiov, &newiovlen, iov, iovlen, "fspath");
632 copyopt(&newiov, &newiovlen, iov, iovlen, "errmsg");
634 return nmount(newiov, newiovlen, mntflags);
638 getnfsargs(char *spec, struct iovec **iov, int *iovlen)
640 struct addrinfo hints, *ai_nfs, *ai;
642 int ecode, speclen, remoteerr;
643 char *hostp, *delimp, *errstr;
645 static char nam[MNAMELEN + 1];
647 if ((delimp = strrchr(spec, ':')) != NULL) {
650 } else if ((delimp = strrchr(spec, '@')) != NULL) {
651 warnx("path@server syntax is deprecated, use server:path");
654 warnx("no <host>:<dirpath> nfs-name");
660 * If there has been a trailing slash at mounttime it seems
661 * that some mountd implementations fail to remove the mount
662 * entries from their mountlist while unmounting.
664 for (speclen = strlen(spec);
665 speclen > 1 && spec[speclen - 1] == '/';
667 spec[speclen - 1] = '\0';
668 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
669 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
672 /* Make both '@' and ':' notations equal */
673 if (*hostp != '\0') {
675 memmove(nam, hostp, len);
677 memmove(nam + len + 1, spec, speclen);
678 nam[len + speclen + 1] = '\0';
682 * Handle an internet host address.
684 memset(&hints, 0, sizeof hints);
685 hints.ai_flags = AI_NUMERICHOST;
686 if (nfsproto == IPPROTO_TCP)
687 hints.ai_socktype = SOCK_STREAM;
688 else if (nfsproto == IPPROTO_UDP)
689 hints.ai_socktype = SOCK_DGRAM;
691 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) {
693 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs))
695 if (portspec == NULL)
696 errx(1, "%s: %s", hostp, gai_strerror(ecode));
698 errx(1, "%s:%s: %s", hostp, portspec,
699 gai_strerror(ecode));
704 ret = TRYRET_LOCALERR;
707 * Try each entry returned by getaddrinfo(). Note the
708 * occurence of remote errors by setting `remoteerr'.
711 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
712 if ((ai->ai_family == AF_INET6) &&
713 (opflags & OF_NOINET6))
715 if ((ai->ai_family == AF_INET) &&
716 (opflags & OF_NOINET4))
718 ret = nfs_tryproto(ai, hostp, spec, &errstr, iov,
720 if (ret == TRYRET_SUCCESS)
722 if (ret != TRYRET_LOCALERR)
724 if ((opflags & ISBGRND) == 0)
725 fprintf(stderr, "%s\n", errstr);
727 if (ret == TRYRET_SUCCESS)
730 /* Exit if all errors were local. */
735 * If retrycnt == 0, we are to keep retrying forever.
736 * Otherwise decrement it, and exit if it hits zero.
738 if (retrycnt != 0 && --retrycnt == 0)
741 if ((opflags & (BGRND | ISBGRND)) == BGRND) {
742 warnx("Cannot immediately mount %s:%s, backgrounding",
745 if (daemon(0, 0) != 0)
750 freeaddrinfo(ai_nfs);
752 build_iovec(iov, iovlen, "hostname", nam, (size_t)-1);
753 /* Add mounted file system to PATH_MOUNTTAB */
754 if (!add_mtab(hostp, spec))
755 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec);
761 getnfs4args(char *spec, struct iovec **iov, int *iovlen)
763 struct addrinfo hints, *ai_nfs, *ai;
765 int ecode, speclen, remoteerr, sotype;
766 char *hostp, *delimp, *errstr;
768 static char nam[MNAMELEN + 1];
770 if (nfsproto == IPPROTO_TCP)
771 sotype = SOCK_STREAM;
772 else if (nfsproto == IPPROTO_UDP)
776 if ((delimp = strrchr(spec, ':')) != NULL) {
779 } else if ((delimp = strrchr(spec, '@')) != NULL) {
780 warnx("path@server syntax is deprecated, use server:path");
783 warnx("no <host>:<dirpath> nfs-name");
789 * If there has been a trailing slash at mounttime it seems
790 * that some mountd implementations fail to remove the mount
791 * entries from their mountlist while unmounting.
793 for (speclen = strlen(spec);
794 speclen > 1 && spec[speclen - 1] == '/';
796 spec[speclen - 1] = '\0';
797 if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) {
798 warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG));
801 /* Make both '@' and ':' notations equal */
802 if (*hostp != '\0') {
804 memmove(nam, hostp, len);
806 memmove(nam + len + 1, spec, speclen);
807 nam[len + speclen + 1] = '\0';
811 * Handle an internet host address.
813 memset(&hints, 0, sizeof hints);
814 hints.ai_flags = AI_NUMERICHOST;
815 hints.ai_socktype = sotype;
816 if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) {
818 if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs))
820 if (portspec == NULL)
821 errx(1, "%s: %s", hostp, gai_strerror(ecode));
823 errx(1, "%s:%s: %s", hostp, portspec,
824 gai_strerror(ecode));
829 ret = TRYRET_LOCALERR;
832 * Try each entry returned by getaddrinfo(). Note the
833 * occurence of remote errors by setting `remoteerr'.
836 for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) {
837 if ((ai->ai_family == AF_INET6) &&
838 (opflags & OF_NOINET6))
840 if ((ai->ai_family == AF_INET) &&
841 (opflags & OF_NOINET4))
843 ret = nfs4_tryproto(ai, hostp, spec, &errstr);
844 if (ret == TRYRET_SUCCESS)
846 if (ret != TRYRET_LOCALERR)
848 if ((opflags & ISBGRND) == 0)
849 fprintf(stderr, "%s\n", errstr);
851 if (ret == TRYRET_SUCCESS)
854 /* Exit if all errors were local. */
859 * If retrycnt == 0, we are to keep retrying forever.
860 * Otherwise decrement it, and exit if it hits zero.
862 if (retrycnt != 0 && --retrycnt == 0)
865 if ((opflags & (BGRND | ISBGRND)) == BGRND) {
866 warnx("Cannot immediately mount %s:%s, backgrounding",
869 if (daemon(0, 0) != 0)
874 freeaddrinfo(ai_nfs);
875 build_iovec(iov, iovlen, "hostname", nam, (size_t)-1);
876 /* Add mounted file system to PATH_MOUNTTAB */
877 if (!add_mtab(hostp, spec))
878 warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec);
883 * Try to set up the NFS arguments according to the address
884 * family, protocol (and possibly port) specified in `ai'.
886 * Returns TRYRET_SUCCESS if successful, or:
887 * TRYRET_TIMEOUT The server did not respond.
888 * TRYRET_REMOTEERR The server reported an error.
889 * TRYRET_LOCALERR Local failure.
891 * In all error cases, *errstr will be set to a statically-allocated string
892 * describing the error.
895 nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec, char **errstr,
896 struct iovec **iov, int *iovlen)
898 static char errbuf[256];
899 struct sockaddr_storage nfs_ss;
900 struct netbuf nfs_nb;
901 struct nfhret nfhret;
903 struct rpc_err rpcerr;
905 struct netconfig *nconf, *nconf_mnt;
906 const char *netid, *netid_mnt;
907 int doconnect, nfsvers, mntvers, sotype;
909 enum mountmode trymntmode;
911 trymntmode = mountmode;
915 if (nfsproto == IPPROTO_TCP)
916 sotype = SOCK_STREAM;
917 else if (nfsproto == IPPROTO_UDP)
920 if ((netid = netidbytype(ai->ai_family, sotype)) == NULL) {
921 snprintf(errbuf, sizeof errbuf,
922 "af %d sotype %d not supported", ai->ai_family, sotype);
923 return (TRYRET_LOCALERR);
925 if ((nconf = getnetconf_cached(netid)) == NULL) {
926 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror());
927 return (TRYRET_LOCALERR);
929 /* The RPCPROG_MNT netid may be different. */
934 if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM))
936 snprintf(errbuf, sizeof errbuf,
937 "af %d sotype SOCK_DGRAM not supported",
939 return (TRYRET_LOCALERR);
941 if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) {
942 snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt,
944 return (TRYRET_LOCALERR);
949 if (trymntmode == V2) {
957 if (portspec != NULL) {
958 /* `ai' contains the complete nfsd sockaddr. */
959 nfs_nb.buf = ai->ai_addr;
960 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen;
962 /* Ask the remote rpcbind. */
963 nfs_nb.buf = &nfs_ss;
964 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss;
966 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb,
968 if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH &&
973 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s",
975 clnt_spcreateerror("RPCPROG_NFS"));
976 return (returncode(rpc_createerr.cf_stat,
977 &rpc_createerr.cf_error));
981 /* Check that the server (nfsd) responds on the port we have chosen. */
982 clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, RPCPROG_NFS, nfsvers,
985 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
986 hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS"));
987 return (returncode(rpc_createerr.cf_stat,
988 &rpc_createerr.cf_error));
990 if (sotype == SOCK_DGRAM && noconn == 0) {
992 * Use connect(), to match what the kernel does. This
993 * catches cases where the server responds from the
994 * wrong source address.
997 if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) {
999 snprintf(errbuf, sizeof errbuf,
1000 "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp,
1002 return (TRYRET_LOCALERR);
1008 stat = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL,
1009 (xdrproc_t)xdr_void, NULL,
1011 if (stat != RPC_SUCCESS) {
1012 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
1017 clnt_geterr(clp, &rpcerr);
1018 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid,
1019 hostp, spec, clnt_sperror(clp, "NFSPROC_NULL"));
1021 return (returncode(stat, &rpcerr));
1025 /* Send the RPCMNT_MOUNT RPC to get the root filehandle. */
1028 clp = clnt_tp_create(hostp, RPCPROG_MNT, mntvers, nconf_mnt);
1030 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
1031 hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create"));
1032 return (returncode(rpc_createerr.cf_stat,
1033 &rpc_createerr.cf_error));
1035 clp->cl_auth = authsys_create_default();
1037 nfhret.vers = mntvers;
1038 stat = clnt_call(clp, RPCMNT_MOUNT, (xdrproc_t)xdr_dir, spec,
1039 (xdrproc_t)xdr_fh, &nfhret,
1041 auth_destroy(clp->cl_auth);
1042 if (stat != RPC_SUCCESS) {
1043 if (stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) {
1048 clnt_geterr(clp, &rpcerr);
1049 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
1050 hostp, spec, clnt_sperror(clp, "RPCPROG_MNT"));
1052 return (returncode(stat, &rpcerr));
1056 if (nfhret.stat != 0) {
1057 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt,
1058 hostp, spec, strerror(nfhret.stat));
1059 return (TRYRET_REMOTEERR);
1063 * Store the filehandle and server address in nfsargsp, making
1064 * sure to copy any locally allocated structures.
1066 addrlen = nfs_nb.len;
1067 addr = malloc(addrlen);
1068 fhsize = nfhret.fhsize;
1069 fh = malloc(fhsize);
1070 if (addr == NULL || fh == NULL)
1072 bcopy(nfs_nb.buf, addr, addrlen);
1073 bcopy(nfhret.nfh, fh, fhsize);
1075 build_iovec(iov, iovlen, "addr", addr, addrlen);
1076 build_iovec(iov, iovlen, "fh", fh, fhsize);
1078 build_iovec(iov, iovlen, "nfsv3", NULL, 0);
1080 return (TRYRET_SUCCESS);
1085 * Try to set up the NFS arguments according to the address
1086 * family, protocol (and possibly port) specified in `ai'.
1088 * Returns TRYRET_SUCCESS if successful, or:
1089 * TRYRET_TIMEOUT The server did not respond.
1090 * TRYRET_REMOTEERR The server reported an error.
1091 * TRYRET_LOCALERR Local failure.
1093 * In all error cases, *errstr will be set to a statically-allocated string
1094 * describing the error.
1097 nfs4_tryproto(struct addrinfo *ai, char *hostp, char *spec, char **errstr)
1099 static char errbuf[256];
1100 struct sockaddr_storage nfs_ss;
1101 struct netbuf nfs_nb;
1102 struct netconfig *nconf;
1104 int nfsvers, sotype;
1109 if (nfsproto == IPPROTO_TCP)
1110 sotype = SOCK_STREAM;
1111 else if (nfsproto == IPPROTO_UDP)
1112 sotype = SOCK_DGRAM;
1114 if ((netid = netidbytype(ai->ai_family, sotype)) == NULL) {
1115 snprintf(errbuf, sizeof errbuf,
1116 "af %d sotype %d not supported", ai->ai_family, sotype);
1117 return (TRYRET_LOCALERR);
1119 if ((nconf = getnetconf_cached(netid)) == NULL) {
1120 snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror());
1121 return (TRYRET_LOCALERR);
1126 if (portspec != NULL && atoi(portspec) != 0) {
1127 /* `ai' contains the complete nfsd sockaddr. */
1128 nfs_nb.buf = ai->ai_addr;
1129 nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen;
1131 /* Ask the remote rpcbind. */
1132 nfs_nb.buf = &nfs_ss;
1133 nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss;
1135 if (!rpcb_getaddr(RPCPROG_NFS, nfsvers, nconf, &nfs_nb,
1137 snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s",
1139 clnt_spcreateerror("RPCPROG_NFS"));
1140 return (returncode(rpc_createerr.cf_stat,
1141 &rpc_createerr.cf_error));
1146 * Store the filehandle and server address in nfsargsp, making
1147 * sure to copy any locally allocated structures.
1149 addrlen = nfs_nb.len;
1150 addr = malloc(addrlen);
1154 bcopy(nfs_nb.buf, addr, addrlen);
1156 return (TRYRET_SUCCESS);
1160 * Catagorise a RPC return status and error into an `enum tryret'
1164 returncode(enum clnt_stat stat, struct rpc_err *rpcerr)
1168 return (TRYRET_TIMEOUT);
1169 case RPC_PMAPFAILURE:
1170 case RPC_PROGNOTREGISTERED:
1171 case RPC_PROGVERSMISMATCH:
1172 /* XXX, these can be local or remote. */
1175 return (TRYRET_REMOTEERR);
1176 case RPC_SYSTEMERROR:
1177 switch (rpcerr->re_errno) {
1179 return (TRYRET_TIMEOUT);
1183 return (TRYRET_REMOTEERR);
1189 return (TRYRET_LOCALERR);
1193 * Look up a netid based on an address family and socket type.
1194 * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM.
1196 * XXX there should be a library function for this.
1199 netidbytype(int af, int sotype)
1201 struct nc_protos *p;
1203 for (p = nc_protos; p->netid != NULL; p++) {
1204 if (af != p->af || sotype != p->sotype)
1212 * Look up a netconfig entry based on a netid, and cache the result so
1213 * that we don't need to remember to call freenetconfigent().
1215 * Otherwise it behaves just like getnetconfigent(), so nc_*error()
1219 getnetconf_cached(const char *netid)
1221 static struct nc_entry {
1222 struct netconfig *nconf;
1223 struct nc_entry *next;
1226 struct netconfig *nconf;
1228 for (p = head; p != NULL; p = p->next)
1229 if (strcmp(netid, p->nconf->nc_netid) == 0)
1232 if ((nconf = getnetconfigent(netid)) == NULL)
1234 if ((p = malloc(sizeof(*p))) == NULL)
1244 * xdr routines for mount rpc's
1247 xdr_dir(XDR *xdrsp, char *dirp)
1249 return (xdr_string(xdrsp, &dirp, RPCMNT_PATHLEN));
1253 xdr_fh(XDR *xdrsp, struct nfhret *np)
1256 long auth, authcnt, authfnd = 0;
1258 if (!xdr_u_long(xdrsp, &np->stat))
1264 np->fhsize = NFSX_V2FH;
1265 return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFSX_V2FH));
1267 if (!xdr_long(xdrsp, &np->fhsize))
1269 if (np->fhsize <= 0 || np->fhsize > NFSX_V3FHMAX)
1271 if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize))
1273 if (!xdr_long(xdrsp, &authcnt))
1275 for (i = 0; i < authcnt; i++) {
1276 if (!xdr_long(xdrsp, &auth))
1278 if (np->auth == -1) {
1281 } else if (auth == np->auth) {
1286 * Some servers, such as DEC's OSF/1 return a nil authenticator
1287 * list to indicate RPCAUTH_UNIX.
1289 if (authcnt == 0 && np->auth == -1)
1290 np->auth = AUTH_SYS;
1291 if (!authfnd && (authcnt > 0 || np->auth != AUTH_SYS))
1301 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
1302 "usage: mount_nfs [-234bcdiLlNPsTU] [-a maxreadahead] [-D deadthresh]",
1303 " [-g maxgroups] [-I readdirsize] [-o options] [-R retrycnt]",
1304 " [-r readsize] [-t timeout] [-w writesize] [-x retrans]",
1305 " rhost:path node");