2 * nfsmount.c -- Linux NFS mount
3 * Copyright (C) 1993 Rick Sladkey <jrs@world.std.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * Wed Feb 8 12:51:48 1995, biro@yggdrasil.com (Ross Biro): allow all port
16 * numbers to be specified on the command line.
18 * Fri, 8 Mar 1996 18:01:39, Swen Thuemmler <swen@uni-paderborn.de>:
19 * Omit the call to connect() for Linux version 1.3.11 or later.
21 * Wed Oct 1 23:55:28 1997: Dick Streefland <dick_streefland@tasking.com>
22 * Implemented the "bg", "fg" and "retry" mount options for NFS.
24 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
25 * - added Native Language Support
27 * Modified by Olaf Kirch and Trond Myklebust for new NFS code,
30 * 2006-06-06 Amit Gud <agud@redhat.com>
31 * - Moved with modifcations to nfs-utils/utils/mount from util-linux/mount.
35 * nfsmount.c,v 1.1.1.1 1993/11/18 08:40:51 jrs Exp
47 #include <rpc/pmap_prot.h>
48 #include <rpc/pmap_clnt.h>
49 #include <sys/socket.h>
51 #include <sys/utsname.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
56 #include <sys/mount.h>
63 #include "nfsumount.h"
64 #include "nfs_mount.h"
65 #include "mount_constants.h"
68 #ifdef HAVE_RPCSVC_NFS_PROT_H
69 #include <rpcsvc/nfs_prot.h>
71 #include <linux/nfs.h>
72 #define nfsstat nfs_stat
82 static char *nfs_strerror(int stat);
84 #define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r))
85 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
86 #define MAX_MNTPROT ((nfs_mount_version >= 4) ? 3 : 2)
87 #define HAVE_RELIABLE_TCP (nfs_mount_version >= 4)
89 #ifndef HAVE_INET_ATON
90 #define inet_aton(a,b) (0)
93 typedef dirpath mnt2arg_t;
94 typedef dirpath mnt3arg_t;
95 typedef dirpath mntarg_t;
97 typedef struct fhstatus mnt2res_t;
98 typedef struct mountres3 mnt3res_t;
104 static char errbuf[BUFSIZ];
105 static char *erreob = &errbuf[BUFSIZ];
109 /* Convert RPC errors into strings */
110 int rpc_strerror(int);
111 int rpc_strerror(int spos)
113 int cf_stat = rpc_createerr.cf_stat;
114 int pos=0, cf_errno = rpc_createerr.cf_error.re_errno;
115 char *ptr, *estr = clnt_sperrno(cf_stat);
119 if ((ptr = index(estr, ':')))
123 if (cf_stat == RPC_SYSTEMERROR)
124 pos = snprintf(tmp, (erreob - tmp),
125 "System Error: %s", strerror(cf_errno));
127 pos = snprintf(tmp, (erreob - tmp), "RPC Error:%s", estr);
131 void mount_errors(char *, int, int);
132 void mount_errors(char *server, int will_retry, int bg)
136 static int onlyonce = 0;
140 pos = snprintf(tmp, (erreob - tmp),
141 "mount to NFS server '%s' failed: ", server);
143 pos = snprintf(tmp, (erreob - tmp),
144 "mount: mount to NFS server '%s' failed: ", server);
147 if (rpc_createerr.cf_stat == RPC_TIMEDOUT) {
148 pos = snprintf(tmp, (erreob - tmp), "timed out %s",
149 will_retry ? "(retrying)" : "(giving up)");
151 pos += rpc_strerror(pos);
154 pos = snprintf(tmp, (erreob - tmp), " %s",
155 will_retry ? "(retrying)" : "(giving up)");
160 openlog("mount", LOG_CONS|LOG_PID, LOG_AUTH);
161 syslog(LOG_ERR, "%s.", errbuf);
163 fprintf(stderr, "%s.\n", errbuf);
166 /* Define the order in which to probe for UDP/TCP services */
173 proto_probelist(enum plist list)
175 static const u_int probe_udp_tcp[] = { IPPROTO_UDP, IPPROTO_TCP, 0 };
176 static const u_int probe_both[] = { IPPROTO_TCP, IPPROTO_UDP, 0 };
177 static const u_int probe_udponly[] = { IPPROTO_UDP, 0 };
182 return probe_udp_tcp;
183 return probe_udponly;
186 /* Define the order in which NFS versions are probed on portmapper */
187 static const u_long *
188 nfs_probelist(const int vers)
190 static const u_long nfs2_probe[] = { 2, 0};
191 static const u_long nfs3_probe[] = { 3, 2, 0};
200 /* Define the order in which Mountd versions are probed on portmapper */
201 static const u_long *
202 mnt_probelist(const int vers)
204 static const u_long mnt1_probe[] = { 1, 2, 0 };
205 static const u_long mnt3_probe[] = { 3, 1, 2, 0 };
215 linux_version_code(void) {
216 struct utsname my_utsname;
219 if (uname(&my_utsname) == 0) {
220 p = atoi(strtok(my_utsname.release, "."));
221 q = atoi(strtok(NULL, "."));
222 r = atoi(strtok(NULL, "."));
223 return MAKE_VERSION(p,q,r);
229 * Unfortunately, the kernel prints annoying console messages
230 * in case of an unexpected nfs mount version (instead of
231 * just returning some error). Therefore we'll have to try
232 * and figure out what version the kernel expects.
235 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
236 * nfs_mount_version: version this source and running kernel can handle
238 int nfs_mount_version = NFS_MOUNT_VERSION;
241 find_kernel_nfs_mount_version(void) {
242 static int kernel_version = -1;
243 int mnt_version = NFS_MOUNT_VERSION;
245 if (kernel_version == -1)
246 kernel_version = linux_version_code();
248 if (kernel_version) {
249 if (kernel_version < MAKE_VERSION(2,1,32))
251 else if (kernel_version < MAKE_VERSION(2,2,18))
253 else if (kernel_version < MAKE_VERSION(2,3,0))
254 mnt_version = 4; /* since 2.2.18pre9 */
255 else if (kernel_version < MAKE_VERSION(2,3,99))
257 else if (kernel_version < MAKE_VERSION(2,6,3))
262 if (mnt_version > NFS_MOUNT_VERSION)
263 mnt_version = NFS_MOUNT_VERSION;
267 int nfs_gethostbyname(const char *, struct sockaddr_in *);
268 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
272 saddr->sin_family = AF_INET;
273 if (!inet_aton(hostname, &saddr->sin_addr)) {
274 if ((hp = gethostbyname(hostname)) == NULL) {
275 fprintf(stderr, _("mount: can't get address for %s\n"),
279 if (hp->h_length > sizeof(*saddr)) {
281 _("mount: got bad hp->h_length\n"));
282 hp->h_length = sizeof(*saddr);
284 memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
291 * getport() is very similar to pmap_getport() with
292 * the exception this version uses a non-reserve ports
293 * instead of reserve ports since reserve ports
294 * are not needed for pmap requests.
298 struct sockaddr_in *saddr,
309 saddr->sin_port = htons (PMAPPORT);
310 socket = get_socket(saddr, prot, FALSE, FALSE);
314 clnt = clntudp_bufcreate(saddr,
315 PMAPPROG, PMAPVERS, TIMEOUT, &socket,
316 UDPMSGSIZE, UDPMSGSIZE);
319 clnt = clnttcp_create(saddr,
320 PMAPPROG, PMAPVERS, &socket, 50, 500);
324 parms.pm_prog = prog;
325 parms.pm_vers = vers;
326 parms.pm_prot = prot;
327 parms.pm_port = 0; /* not needed or used */
329 stat = clnt_call(clnt, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
330 (caddr_t)&parms, (xdrproc_t)xdr_u_short, (caddr_t)&port, TIMEOUT);
332 clnt_geterr(clnt, &rpc_createerr.cf_error);
333 rpc_createerr.cf_stat = stat;
336 if (stat != RPC_SUCCESS)
339 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
348 * Use the portmapper to discover whether or not the service we want is
349 * available. The lists 'versions' and 'protos' define ordered sequences
350 * of service versions and udp/tcp protocols to probe for.
353 probe_port(clnt_addr_t *server,
354 const u_long *versions,
357 struct sockaddr_in *saddr = &server->saddr;
358 struct pmap *pmap = &server->pmap;
359 const u_long prog = pmap->pm_prog, *p_vers;
360 const u_int prot = (u_int)pmap->pm_prot,
362 const u_short port = (u_short) pmap->pm_port;
363 u_long vers = pmap->pm_vers;
365 p_prot = prot ? &prot : protos;
366 p_vers = vers ? &vers : versions;
367 rpc_createerr.cf_stat = 0;
369 saddr->sin_port = htons(PMAPPORT);
370 p_port = getport(saddr, prog, *p_vers, *p_prot);
372 if (!port || port == p_port) {
373 saddr->sin_port = htons(p_port);
376 "mount: trying %s prog %ld vers %ld prot %s port %d\n",
377 inet_ntoa(saddr->sin_addr), prog, *p_vers,
378 *p_prot == IPPROTO_UDP ? "udp" : "tcp", p_port);
380 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
382 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
386 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED)
394 if (vers == pmap->pm_vers) {
398 if (vers || !*++p_vers)
406 pmap->pm_vers = *p_vers;
408 pmap->pm_prot = *p_prot;
410 pmap->pm_port = p_port;
411 rpc_createerr.cf_stat = 0;
416 probe_nfsport(clnt_addr_t *nfs_server)
418 const struct pmap *pmap = &nfs_server->pmap;
419 const u_long *probe_vers;
420 const u_int *probe_prot;
422 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
424 probe_vers = nfs_probelist(MAX_NFSPROT);
425 probe_prot = proto_probelist(HAVE_RELIABLE_TCP ? use_tcp : udp_only);
426 return probe_port(nfs_server, probe_vers, probe_prot);
429 int probe_mntport(clnt_addr_t *mnt_server)
431 const struct pmap *pmap = &mnt_server->pmap;
432 const u_long *probe_vers;
433 const u_int *probe_prot;
435 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
437 probe_vers = mnt_probelist(MAX_MNTPROT);
438 probe_prot = proto_probelist(HAVE_RELIABLE_TCP ? udp_tcp : udp_only);
439 return probe_port(mnt_server, probe_vers, probe_prot);
443 probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
445 struct pmap *nfs_pmap = &nfs_server->pmap;
446 struct pmap *mnt_pmap = &mnt_server->pmap;
447 struct pmap save_nfs, save_mnt;
449 const u_long *probe_vers;
451 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
452 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
453 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
454 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
455 if (nfs_pmap->pm_vers)
457 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
458 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
459 for (probe_vers = mnt_probelist(MAX_MNTPROT); *probe_vers; probe_vers++) {
460 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
461 if ((res = probe_nfsport(nfs_server) != 0)) {
462 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
463 if ((res = probe_mntport(mnt_server)) != 0)
465 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
467 switch (rpc_createerr.cf_stat) {
468 case RPC_PROGVERSMISMATCH:
469 case RPC_PROGNOTREGISTERED:
474 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
479 if (!probe_nfsport(nfs_server))
481 return probe_mntport(mnt_server);
484 static inline enum clnt_stat
485 nfs3_mount(CLIENT *clnt, mnt3arg_t *mnt3arg, mnt3res_t *mnt3res)
487 return clnt_call(clnt, MOUNTPROC3_MNT,
488 (xdrproc_t) xdr_dirpath, (caddr_t) mnt3arg,
489 (xdrproc_t) xdr_mountres3, (caddr_t) mnt3res,
493 static inline enum clnt_stat
494 nfs2_mount(CLIENT *clnt, mnt2arg_t *mnt2arg, mnt2res_t *mnt2res)
496 return clnt_call(clnt, MOUNTPROC_MNT,
497 (xdrproc_t) xdr_dirpath, (caddr_t) mnt2arg,
498 (xdrproc_t) xdr_fhstatus, (caddr_t) mnt2res,
503 nfs_call_mount(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server,
504 mntarg_t *mntarg, mntres_t *mntres)
510 if (!probe_bothports(mnt_server, nfs_server))
513 clnt = mnt_openclnt(mnt_server, &msock);
516 /* make pointers in xdr_mountres3 NULL so
517 * that xdr_array allocates memory for us
519 memset(mntres, 0, sizeof(*mntres));
520 switch (mnt_server->pmap.pm_vers) {
522 stat = nfs3_mount(clnt, mntarg, &mntres->nfsv3);
526 stat = nfs2_mount(clnt, mntarg, &mntres->nfsv2);
531 if (stat != RPC_SUCCESS) {
532 clnt_geterr(clnt, &rpc_createerr.cf_error);
533 rpc_createerr.cf_stat = stat;
535 mnt_closeclnt(clnt, msock);
536 if (stat == RPC_SUCCESS)
543 parse_options(char *old_opts, struct nfs_mount_data *data,
544 int *bg, int *retry, clnt_addr_t *mnt_server,
545 clnt_addr_t *nfs_server, char *new_opts, const int opt_size)
547 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
548 struct pmap *mnt_pmap = &mnt_server->pmap;
549 struct pmap *nfs_pmap = &nfs_server->pmap;
551 char *opt, *opteq, *p, *opt_b;
552 char *mounthost = NULL;
559 len = strlen(new_opts);
560 for (p=old_opts, opt_b=NULL; p && *p; p++) {
562 opt_b = p; /* begin of the option item */
564 open_quote ^= 1; /* reverse the status */
566 continue; /* still in a quoted block */
568 *p = '\0'; /* terminate the option item */
569 if (*p == '\0' || *(p+1) == '\0') {
570 opt = opt_b; /* opt is useful now */
574 continue; /* still somewhere in the option item */
576 if (strlen(opt) >= sizeof(cbuf))
578 if ((opteq = strchr(opt, '=')) && isdigit(opteq[1])) {
579 int val = atoi(opteq + 1);
581 /* printf("opt=%s\n", opt); */
582 if (!strcmp(opt, "rsize"))
584 else if (!strcmp(opt, "wsize"))
586 else if (!strcmp(opt, "timeo"))
588 else if (!strcmp(opt, "retrans"))
590 else if (!strcmp(opt, "acregmin"))
591 data->acregmin = val;
592 else if (!strcmp(opt, "acregmax"))
593 data->acregmax = val;
594 else if (!strcmp(opt, "acdirmin"))
595 data->acdirmin = val;
596 else if (!strcmp(opt, "acdirmax"))
597 data->acdirmax = val;
598 else if (!strcmp(opt, "actimeo")) {
599 data->acregmin = val;
600 data->acregmax = val;
601 data->acdirmin = val;
602 data->acdirmax = val;
604 else if (!strcmp(opt, "retry"))
606 else if (!strcmp(opt, "port"))
607 nfs_pmap->pm_port = val;
608 else if (!strcmp(opt, "mountport"))
609 mnt_pmap->pm_port = val;
610 else if (!strcmp(opt, "mountprog"))
611 mnt_pmap->pm_prog = val;
612 else if (!strcmp(opt, "mountvers"))
613 mnt_pmap->pm_vers = val;
614 else if (!strcmp(opt, "mounthost"))
615 mounthost=xstrndup(opteq+1, strcspn(opteq+1," \t\n\r,"));
616 else if (!strcmp(opt, "nfsprog"))
617 nfs_pmap->pm_prog = val;
618 else if (!strcmp(opt, "nfsvers") ||
619 !strcmp(opt, "vers")) {
620 nfs_pmap->pm_vers = val;
622 #if NFS_MOUNT_VERSION >= 2
623 } else if (!strcmp(opt, "namlen")) {
624 if (nfs_mount_version >= 2)
631 } else if (!strcmp(opt, "addr")) {
638 sprintf(cbuf, "%s=%s,", opt, opteq+1);
641 if (!strcmp(opt, "proto")) {
642 if (!strcmp(opteq+1, "udp")) {
643 nfs_pmap->pm_prot = IPPROTO_UDP;
644 mnt_pmap->pm_prot = IPPROTO_UDP;
645 #if NFS_MOUNT_VERSION >= 2
646 data->flags &= ~NFS_MOUNT_TCP;
647 } else if (!strcmp(opteq+1, "tcp") &&
648 nfs_mount_version > 2) {
649 nfs_pmap->pm_prot = IPPROTO_TCP;
650 mnt_pmap->pm_prot = IPPROTO_TCP;
651 data->flags |= NFS_MOUNT_TCP;
657 #if NFS_MOUNT_VERSION >= 5
658 } else if (!strcmp(opt, "sec")) {
659 char *secflavor = opteq+1;
661 if (nfs_mount_version < 5) {
662 printf(_("Warning: ignoring sec=%s option\n"), secflavor);
664 } else if (!strcmp(secflavor, "none"))
665 data->pseudoflavor = AUTH_NONE;
666 else if (!strcmp(secflavor, "sys"))
667 data->pseudoflavor = AUTH_SYS;
668 else if (!strcmp(secflavor, "krb5"))
669 data->pseudoflavor = AUTH_GSS_KRB5;
670 else if (!strcmp(secflavor, "krb5i"))
671 data->pseudoflavor = AUTH_GSS_KRB5I;
672 else if (!strcmp(secflavor, "krb5p"))
673 data->pseudoflavor = AUTH_GSS_KRB5P;
674 else if (!strcmp(secflavor, "lipkey"))
675 data->pseudoflavor = AUTH_GSS_LKEY;
676 else if (!strcmp(secflavor, "lipkey-i"))
677 data->pseudoflavor = AUTH_GSS_LKEYI;
678 else if (!strcmp(secflavor, "lipkey-p"))
679 data->pseudoflavor = AUTH_GSS_LKEYP;
680 else if (!strcmp(secflavor, "spkm3"))
681 data->pseudoflavor = AUTH_GSS_SPKM;
682 else if (!strcmp(secflavor, "spkm3i"))
683 data->pseudoflavor = AUTH_GSS_SPKMI;
684 else if (!strcmp(secflavor, "spkm3p"))
685 data->pseudoflavor = AUTH_GSS_SPKMP;
689 printf(_("Warning: Unrecognized security flavor %s.\n"),
693 data->flags |= NFS_MOUNT_SECFLAVOUR;
695 } else if (!strcmp(opt, "mounthost"))
696 mounthost=xstrndup(opteq+1,
697 strcspn(opteq+1," \t\n\r,"));
698 else if (!strcmp(opt, "context")) {
699 char *context = opteq + 1;
700 int ctxlen = strlen(context);
702 if (ctxlen > NFS_MAX_CONTEXT_LEN) {
703 printf(_("context parameter exceeds limit of %d\n"),
704 NFS_MAX_CONTEXT_LEN);
707 /* The context string is in the format of
708 * "system_u:object_r:...". We only want
709 * the context str between the quotes.
712 strncpy(data->context, context+1,
715 strncpy(data->context, context,
716 NFS_MAX_CONTEXT_LEN);
721 sprintf(cbuf, "%s=%s,", opt, opteq+1);
724 if (!strncmp(opt, "no", 2)) {
728 if (!strcmp(opt, "bg"))
730 else if (!strcmp(opt, "fg"))
732 else if (!strcmp(opt, "soft")) {
733 data->flags &= ~NFS_MOUNT_SOFT;
735 data->flags |= NFS_MOUNT_SOFT;
736 } else if (!strcmp(opt, "hard")) {
737 data->flags &= ~NFS_MOUNT_SOFT;
739 data->flags |= NFS_MOUNT_SOFT;
740 } else if (!strcmp(opt, "intr")) {
741 data->flags &= ~NFS_MOUNT_INTR;
743 data->flags |= NFS_MOUNT_INTR;
744 } else if (!strcmp(opt, "posix")) {
745 data->flags &= ~NFS_MOUNT_POSIX;
747 data->flags |= NFS_MOUNT_POSIX;
748 } else if (!strcmp(opt, "cto")) {
749 data->flags &= ~NFS_MOUNT_NOCTO;
751 data->flags |= NFS_MOUNT_NOCTO;
752 } else if (!strcmp(opt, "ac")) {
753 data->flags &= ~NFS_MOUNT_NOAC;
755 data->flags |= NFS_MOUNT_NOAC;
756 #if NFS_MOUNT_VERSION >= 2
757 } else if (!strcmp(opt, "tcp")) {
758 data->flags &= ~NFS_MOUNT_TCP;
760 if (nfs_mount_version < 2)
762 nfs_pmap->pm_prot = IPPROTO_TCP;
763 mnt_pmap->pm_prot = IPPROTO_TCP;
764 data->flags |= NFS_MOUNT_TCP;
766 mnt_pmap->pm_prot = IPPROTO_UDP;
767 nfs_pmap->pm_prot = IPPROTO_UDP;
769 } else if (!strcmp(opt, "udp")) {
770 data->flags &= ~NFS_MOUNT_TCP;
772 if (nfs_mount_version < 2)
774 nfs_pmap->pm_prot = IPPROTO_TCP;
775 mnt_pmap->pm_prot = IPPROTO_TCP;
776 data->flags |= NFS_MOUNT_TCP;
778 nfs_pmap->pm_prot = IPPROTO_UDP;
779 mnt_pmap->pm_prot = IPPROTO_UDP;
782 #if NFS_MOUNT_VERSION >= 3
783 } else if (!strcmp(opt, "lock")) {
784 data->flags &= ~NFS_MOUNT_NONLM;
786 if (nfs_mount_version < 3)
788 data->flags |= NFS_MOUNT_NONLM;
791 #if NFS_MOUNT_VERSION >= 4
792 } else if (!strcmp(opt, "broken_suid")) {
793 data->flags &= ~NFS_MOUNT_BROKEN_SUID;
795 if (nfs_mount_version < 4)
797 data->flags |= NFS_MOUNT_BROKEN_SUID;
799 } else if (!strcmp(opt, "acl")) {
800 data->flags &= ~NFS_MOUNT_NOACL;
802 data->flags |= NFS_MOUNT_NOACL;
803 } else if (!strcmp(opt, "rdirplus")) {
804 data->flags &= ~NFS_MOUNT_NORDIRPLUS;
806 data->flags |= NFS_MOUNT_NORDIRPLUS;
812 printf(_("Unsupported nfs mount option: "
813 "%s%s\n"), val ? "" : "no", opt);
816 sprintf(cbuf, val ? "%s,":"no%s,", opt);
819 if (len >= opt_size) {
820 printf(_("mount: excessively long option argument\n"));
823 strcat(new_opts, cbuf);
825 /* See if the nfs host = mount host. */
827 if (!nfs_gethostbyname(mounthost, mnt_saddr))
829 *mnt_server->hostname = mounthost;
833 printf(_("Bad nfs mount parameter: %s\n"), opt);
839 nfsmnt_check_compat(const struct pmap *nfs_pmap, const struct pmap *mnt_pmap)
841 if (nfs_pmap->pm_vers &&
842 (nfs_pmap->pm_vers > MAX_NFSPROT || nfs_pmap->pm_vers < 2)) {
843 if (nfs_pmap->pm_vers == 4)
844 fprintf(stderr, _("'vers=4' is not supported. "
845 "Use '-t nfs4' instead.\n"));
847 fprintf(stderr, _("NFS version %ld is not supported.\n"),
851 if (mnt_pmap->pm_vers > MAX_MNTPROT) {
852 fprintf(stderr, _("NFS mount version %ld s not supported.\n"),
862 nfsmount(const char *spec, const char *node, int *flags,
863 char **extra_opts, char **mount_opts,
864 int running_bg, int *need_statd)
866 static char *prev_bg_host;
868 char *hostname, *dirname, *old_opts, *mounthost = NULL;
869 char new_opts[1024], cbuf[1024];
870 static struct nfs_mount_data data;
872 static int doonce = 0;
874 clnt_addr_t mnt_server = { &mounthost, };
875 clnt_addr_t nfs_server = { &hostname, };
876 struct sockaddr_in *nfs_saddr = &nfs_server.saddr;
877 struct pmap *mnt_pmap = &mnt_server.pmap,
878 *nfs_pmap = &nfs_server.pmap;
879 struct pmap save_mnt, save_nfs;
888 int retval = EX_FAIL;
893 nfs_mount_version = find_kernel_nfs_mount_version();
895 if (strlen(spec) >= sizeof(hostdir)) {
896 fprintf(stderr, _("mount: "
897 "excessively long host:dir argument\n"));
900 strcpy(hostdir, spec);
901 if ((s = strchr(hostdir, ':'))) {
905 /* Ignore all but first hostname in replicated mounts
906 until they can be fully supported. (mack@sgi.com) */
907 if ((s = strchr(hostdir, ','))) {
911 "multiple hostnames not supported\n"));
916 "directory to mount not in host:dir format\n"));
920 if (!nfs_gethostbyname(hostname, nfs_saddr))
922 mounthost = hostname;
923 memcpy (&mnt_server.saddr, nfs_saddr, sizeof (mnt_server.saddr));
925 /* add IP address to mtab options for use when unmounting */
927 s = inet_ntoa(nfs_saddr->sin_addr);
928 old_opts = *extra_opts;
932 /* Set default options.
933 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
934 * let the kernel decide.
935 * timeo is filled in after we know whether it'll be TCP or UDP. */
936 memset(&data, 0, sizeof(data));
941 #if NFS_MOUNT_VERSION >= 2
942 data.namlen = NAME_MAX;
946 retry = 10000; /* 10000 minutes ~ 1 week */
948 memset(mnt_pmap, 0, sizeof(*mnt_pmap));
949 mnt_pmap->pm_prog = MOUNTPROG;
950 memset(nfs_pmap, 0, sizeof(*nfs_pmap));
951 nfs_pmap->pm_prog = NFS_PROGRAM;
955 if (!parse_options(old_opts, &data, &bg, &retry, &mnt_server, &nfs_server,
956 new_opts, sizeof(new_opts)))
958 if (!nfsmnt_check_compat(nfs_pmap, mnt_pmap))
961 if (retry == 10000 && !bg)
962 retry = 2; /* reset for fg mounts */
965 #ifdef NFS_MOUNT_DEBUG
966 printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
967 data.rsize, data.wsize, data.timeo, data.retrans);
968 printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
969 data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
970 printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
971 nfs_pmap->pm_port, bg, retry, data.flags);
972 printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
973 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
974 nfs_pmap->pm_prog, nfs_pmap->pm_vers);
975 printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d ",
976 (data.flags & NFS_MOUNT_SOFT) != 0,
977 (data.flags & NFS_MOUNT_INTR) != 0,
978 (data.flags & NFS_MOUNT_POSIX) != 0,
979 (data.flags & NFS_MOUNT_NOCTO) != 0,
980 (data.flags & NFS_MOUNT_NOAC) != 0);
981 #if NFS_MOUNT_VERSION >= 2
983 (data.flags & NFS_MOUNT_TCP) != 0);
985 #if NFS_MOUNT_VERSION >= 4
986 printf("noacl = %d ", (data.flags & NFS_MOUNT_NOACL) != 0);
988 #if NFS_MOUNT_VERSION >= 5
989 printf("sec = %u ", data.pseudoflavor);
990 printf("readdirplus = %d ", (data.flags & NFS_MOUNT_NORDIRPLUS) != 0);
995 data.version = nfs_mount_version;
996 *mount_opts = (char *) &data;
998 if (*flags & MS_REMOUNT)
1002 * If the previous mount operation on the same host was
1003 * backgrounded, and the "bg" for this mount is also set,
1004 * give up immediately, to avoid the initial timeout.
1006 if (bg && !running_bg &&
1007 prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
1013 /* create mount deamon client */
1016 * The following loop implements the mount retries. On the first
1017 * call, "running_bg" is 0. When the mount times out, and the
1018 * "bg" option is set, the exit status EX_BG will be returned.
1019 * For a backgrounded mount, there will be a second call by the
1020 * child process with "running_bg" set to 1.
1022 * The case where the mount point is not present and the "bg"
1023 * option is set, is treated as a timeout. This is done to
1024 * support nested mounts.
1026 * The "retry" count specified by the user is the number of
1027 * minutes to retry before giving up.
1029 * Only the first error message will be displayed.
1031 timeout = time(NULL) + 60 * retry;
1036 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
1037 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
1039 if (bg && stat(node, &statbuf) == -1) {
1040 /* no mount point yet - sleep */
1042 sleep(val); /* 1, 2, 4, 8, 16, 30, ... */
1049 /* be careful not to use too many CPU cycles */
1053 stat = nfs_call_mount(&mnt_server, &nfs_server,
1057 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
1058 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
1062 switch(rpc_createerr.cf_stat){
1065 case RPC_SYSTEMERROR:
1066 if (errno == ETIMEDOUT)
1069 mount_errors(*nfs_server.hostname, 0, bg);
1074 mount_errors(*nfs_server.hostname, 0, bg);
1077 mount_errors(*nfs_server.hostname, 1, bg);
1081 prev_bg_host = xstrdup(hostname);
1088 mount_errors(*nfs_server.hostname, 0, bg);
1092 mount_errors(*nfs_server.hostname, 1, bg);
1095 if (nfs_pmap->pm_vers == 2) {
1096 if (mntres.nfsv2.fhs_status != 0) {
1098 _("mount: %s:%s failed, reason given by server: %s\n"),
1100 nfs_strerror(mntres.nfsv2.fhs_status));
1103 memcpy(data.root.data,
1104 (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
1106 #if NFS_MOUNT_VERSION >= 4
1107 data.root.size = NFS_FHSIZE;
1108 memcpy(data.old_root.data,
1109 (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
1113 #if NFS_MOUNT_VERSION >= 4
1114 mountres3_ok *mountres;
1116 int i, *flavor, yum = 0;
1117 if (mntres.nfsv3.fhs_status != 0) {
1119 _("mount: %s:%s failed, reason given by server: %s\n"),
1121 nfs_strerror(mntres.nfsv3.fhs_status));
1124 #if NFS_MOUNT_VERSION >= 5
1125 mountres = &mntres.nfsv3.mountres3_u.mountinfo;
1126 i = mountres->auth_flavors.auth_flavors_len;
1128 goto noauth_flavors;
1130 flavor = mountres->auth_flavors.auth_flavors_val;
1132 /* If no flavour requested, use first simple
1133 * flavour that is offered.
1135 if (! (data.flags & NFS_MOUNT_SECFLAVOUR) &&
1136 (flavor[i] == AUTH_SYS ||
1137 flavor[i] == AUTH_NONE)) {
1138 data.pseudoflavor = flavor[i];
1139 data.flags |= NFS_MOUNT_SECFLAVOUR;
1141 if (flavor[i] == data.pseudoflavor)
1143 #ifdef NFS_MOUNT_DEBUG
1144 printf("auth flavor %d: %d\n",
1150 "mount: %s:%s failed, "
1151 "security flavor not supported\n",
1153 /* server has registered us in rmtab, send umount */
1154 nfs_call_umount(&mnt_server, &dirname);
1159 fhandle = &mntres.nfsv3.mountres3_u.mountinfo.fhandle;
1160 memset(data.old_root.data, 0, NFS_FHSIZE);
1161 memset(&data.root, 0, sizeof(data.root));
1162 data.root.size = fhandle->fhandle3_len;
1163 memcpy(data.root.data,
1164 (char *) fhandle->fhandle3_val,
1165 fhandle->fhandle3_len);
1167 data.flags |= NFS_MOUNT_VER3;
1171 if (nfs_mount_version == 1) {
1172 /* create nfs socket for kernel */
1173 if (nfs_pmap->pm_prot == IPPROTO_TCP)
1174 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1176 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1178 perror(_("nfs socket"));
1181 if (bindresvport(fsock, 0) < 0) {
1182 perror(_("nfs bindresvport"));
1187 #ifdef NFS_MOUNT_DEBUG
1188 printf(_("using port %d for nfs deamon\n"), nfs_pmap->pm_port);
1190 nfs_saddr->sin_port = htons(nfs_pmap->pm_port);
1192 * connect() the socket for kernels 1.3.10 and below only,
1193 * to avoid problems with multihomed hosts.
1196 if (linux_version_code() <= 0x01030a && fsock != -1
1197 && connect(fsock, (struct sockaddr *) nfs_saddr,
1198 sizeof (*nfs_saddr)) < 0) {
1199 perror(_("nfs connect"));
1203 #if NFS_MOUNT_VERSION >= 2
1204 if (nfs_pmap->pm_prot == IPPROTO_TCP)
1205 data.flags |= NFS_MOUNT_TCP;
1207 data.flags &= ~NFS_MOUNT_TCP;
1210 /* prepare data structure for kernel */
1213 memcpy((char *) &data.addr, (char *) nfs_saddr, sizeof(data.addr));
1214 strncpy(data.hostname, hostname, sizeof(data.hostname));
1217 /* Ensure we have enough padding for the following strcat()s */
1218 if (strlen(new_opts) + strlen(s) + 30 >= sizeof(new_opts)) {
1219 fprintf(stderr, _("mount: "
1220 "excessively long option argument\n"));
1224 snprintf(cbuf, sizeof(cbuf)-1, "addr=%s", s);
1225 strcat(new_opts, cbuf);
1227 *extra_opts = xstrdup(new_opts);
1228 *need_statd = ! (data.flags & NFS_MOUNT_NONLM);
1239 * We need to translate between nfs status return values and
1240 * the local errno values which may not be the same.
1242 * Andreas Schwab <schwab@LS5.informatik.uni-dortmund.de>: change errno:
1243 * "after #include <errno.h> the symbol errno is reserved for any use,
1244 * it cannot even be used as a struct tag or field name".
1248 #define EDQUOT ENOSPC
1256 { NFSERR_PERM, EPERM },
1257 { NFSERR_NOENT, ENOENT },
1259 { NFSERR_NXIO, ENXIO },
1260 { NFSERR_ACCES, EACCES },
1261 { NFSERR_EXIST, EEXIST },
1262 { NFSERR_NODEV, ENODEV },
1263 { NFSERR_NOTDIR, ENOTDIR },
1264 { NFSERR_ISDIR, EISDIR },
1266 { NFSERR_INVAL, EINVAL }, /* that Sun forgot */
1268 { NFSERR_FBIG, EFBIG },
1269 { NFSERR_NOSPC, ENOSPC },
1270 { NFSERR_ROFS, EROFS },
1271 { NFSERR_NAMETOOLONG, ENAMETOOLONG },
1272 { NFSERR_NOTEMPTY, ENOTEMPTY },
1273 { NFSERR_DQUOT, EDQUOT },
1274 { NFSERR_STALE, ESTALE },
1276 { NFSERR_WFLUSH, EWFLUSH },
1278 /* Throw in some NFSv3 values for even more fun (HP returns these) */
1284 static char *nfs_strerror(int stat)
1287 static char buf[256];
1289 for (i = 0; nfs_errtbl[i].stat != -1; i++) {
1290 if (nfs_errtbl[i].stat == stat)
1291 return strerror(nfs_errtbl[i].errnum);
1293 sprintf(buf, _("unknown nfs status return value: %d"), stat);