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"
76 #define MAKE_VERSION(p,q,r) (65536*(p) + 256*(q) + (r))
77 #define MAX_NFSPROT ((nfs_mount_version >= 4) ? 3 : 2)
78 #define MAX_MNTPROT ((nfs_mount_version >= 4) ? 3 : 2)
79 #define HAVE_RELIABLE_TCP (nfs_mount_version >= 4)
81 #ifndef HAVE_INET_ATON
82 #define inet_aton(a,b) (0)
85 typedef dirpath mnt2arg_t;
86 typedef dirpath mnt3arg_t;
87 typedef dirpath mntarg_t;
89 typedef struct fhstatus mnt2res_t;
90 typedef struct mountres3 mnt3res_t;
99 /* Define the order in which to probe for UDP/TCP services */
106 proto_probelist(enum plist list)
108 static const u_int probe_udp_tcp[] = { IPPROTO_UDP, IPPROTO_TCP, 0 };
109 static const u_int probe_both[] = { IPPROTO_TCP, IPPROTO_UDP, 0 };
110 static const u_int probe_udponly[] = { IPPROTO_UDP, 0 };
115 return probe_udp_tcp;
116 return probe_udponly;
119 /* Define the order in which NFS versions are probed on portmapper */
120 static const u_long *
121 nfs_probelist(const int vers)
123 static const u_long nfs2_probe[] = { 2, 0};
124 static const u_long nfs3_probe[] = { 3, 2, 0};
133 /* Define the order in which Mountd versions are probed on portmapper */
134 static const u_long *
135 mnt_probelist(const int vers)
137 static const u_long mnt1_probe[] = { 1, 2, 0 };
138 static const u_long mnt3_probe[] = { 3, 1, 2, 0 };
148 linux_version_code(void) {
149 struct utsname my_utsname;
152 if (uname(&my_utsname) == 0) {
153 p = atoi(strtok(my_utsname.release, "."));
154 q = atoi(strtok(NULL, "."));
155 r = atoi(strtok(NULL, "."));
156 return MAKE_VERSION(p,q,r);
162 * Unfortunately, the kernel prints annoying console messages
163 * in case of an unexpected nfs mount version (instead of
164 * just returning some error). Therefore we'll have to try
165 * and figure out what version the kernel expects.
168 * NFS_MOUNT_VERSION: these nfsmount sources at compile time
169 * nfs_mount_version: version this source and running kernel can handle
171 int nfs_mount_version = NFS_MOUNT_VERSION;
174 find_kernel_nfs_mount_version(void) {
175 static int kernel_version = -1;
176 int mnt_version = NFS_MOUNT_VERSION;
178 if (kernel_version == -1)
179 kernel_version = linux_version_code();
181 if (kernel_version) {
182 if (kernel_version < MAKE_VERSION(2,1,32))
184 else if (kernel_version < MAKE_VERSION(2,2,18))
186 else if (kernel_version < MAKE_VERSION(2,3,0))
187 mnt_version = 4; /* since 2.2.18pre9 */
188 else if (kernel_version < MAKE_VERSION(2,3,99))
190 else if (kernel_version < MAKE_VERSION(2,6,3))
195 if (mnt_version > NFS_MOUNT_VERSION)
196 mnt_version = NFS_MOUNT_VERSION;
200 int nfs_gethostbyname(const char *, struct sockaddr_in *);
201 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
205 saddr->sin_family = AF_INET;
206 if (!inet_aton(hostname, &saddr->sin_addr)) {
207 if ((hp = gethostbyname(hostname)) == NULL) {
208 fprintf(stderr, _("mount: can't get address for %s\n"),
212 if (hp->h_length > sizeof(*saddr)) {
214 _("mount: got bad hp->h_length\n"));
215 hp->h_length = sizeof(*saddr);
217 memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
224 * getport() is very similar to pmap_getport() with
225 * the exception this version uses a non-reserve ports
226 * instead of reserve ports since reserve ports
227 * are not needed for pmap requests.
231 struct sockaddr_in *saddr,
242 saddr->sin_port = htons (PMAPPORT);
243 socket = get_socket(saddr, prot, FALSE, FALSE);
247 clnt = clntudp_bufcreate(saddr,
248 PMAPPROG, PMAPVERS, TIMEOUT, &socket,
249 UDPMSGSIZE, UDPMSGSIZE);
252 clnt = clnttcp_create(saddr,
253 PMAPPROG, PMAPVERS, &socket, 50, 500);
257 parms.pm_prog = prog;
258 parms.pm_vers = vers;
259 parms.pm_prot = prot;
260 parms.pm_port = 0; /* not needed or used */
262 stat = clnt_call(clnt, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
263 (caddr_t)&parms, (xdrproc_t)xdr_u_short, (caddr_t)&port, TIMEOUT);
265 clnt_geterr(clnt, &rpc_createerr.cf_error);
266 rpc_createerr.cf_stat = stat;
269 if (stat != RPC_SUCCESS)
272 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
281 * Use the portmapper to discover whether or not the service we want is
282 * available. The lists 'versions' and 'protos' define ordered sequences
283 * of service versions and udp/tcp protocols to probe for.
286 probe_port(clnt_addr_t *server,
287 const u_long *versions,
290 struct sockaddr_in *saddr = &server->saddr;
291 struct pmap *pmap = &server->pmap;
292 const u_long prog = pmap->pm_prog, *p_vers;
293 const u_int prot = (u_int)pmap->pm_prot,
295 const u_short port = (u_short) pmap->pm_port;
296 u_long vers = pmap->pm_vers;
298 p_prot = prot ? &prot : protos;
299 p_vers = vers ? &vers : versions;
300 rpc_createerr.cf_stat = 0;
302 saddr->sin_port = htons(PMAPPORT);
303 p_port = getport(saddr, prog, *p_vers, *p_prot);
305 if (!port || port == p_port) {
306 saddr->sin_port = htons(p_port);
309 "mount: trying %s prog %ld vers %ld prot %s port %d\n",
310 inet_ntoa(saddr->sin_addr), prog, *p_vers,
311 *p_prot == IPPROTO_UDP ? "udp" : "tcp", p_port);
313 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
315 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
319 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED)
327 if (vers == pmap->pm_vers) {
331 if (vers || !*++p_vers)
339 pmap->pm_vers = *p_vers;
341 pmap->pm_prot = *p_prot;
343 pmap->pm_port = p_port;
344 rpc_createerr.cf_stat = 0;
349 probe_nfsport(clnt_addr_t *nfs_server)
351 const struct pmap *pmap = &nfs_server->pmap;
352 const u_long *probe_vers;
353 const u_int *probe_prot;
355 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
357 probe_vers = nfs_probelist(MAX_NFSPROT);
358 probe_prot = proto_probelist(HAVE_RELIABLE_TCP ? use_tcp : udp_only);
359 return probe_port(nfs_server, probe_vers, probe_prot);
362 int probe_mntport(clnt_addr_t *mnt_server)
364 const struct pmap *pmap = &mnt_server->pmap;
365 const u_long *probe_vers;
366 const u_int *probe_prot;
368 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
370 probe_vers = mnt_probelist(MAX_MNTPROT);
371 probe_prot = proto_probelist(HAVE_RELIABLE_TCP ? udp_tcp : udp_only);
372 return probe_port(mnt_server, probe_vers, probe_prot);
376 probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
378 struct pmap *nfs_pmap = &nfs_server->pmap;
379 struct pmap *mnt_pmap = &mnt_server->pmap;
380 struct pmap save_nfs, save_mnt;
382 const u_long *probe_vers;
384 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
385 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
386 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
387 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
388 if (nfs_pmap->pm_vers)
390 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
391 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
392 for (probe_vers = mnt_probelist(MAX_MNTPROT); *probe_vers; probe_vers++) {
393 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
394 if ((res = probe_nfsport(nfs_server) != 0)) {
395 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
396 if ((res = probe_mntport(mnt_server)) != 0)
398 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
400 switch (rpc_createerr.cf_stat) {
401 case RPC_PROGVERSMISMATCH:
402 case RPC_PROGNOTREGISTERED:
407 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
412 if (!probe_nfsport(nfs_server))
414 return probe_mntport(mnt_server);
417 static inline enum clnt_stat
418 nfs3_mount(CLIENT *clnt, mnt3arg_t *mnt3arg, mnt3res_t *mnt3res)
420 return clnt_call(clnt, MOUNTPROC3_MNT,
421 (xdrproc_t) xdr_dirpath, (caddr_t) mnt3arg,
422 (xdrproc_t) xdr_mountres3, (caddr_t) mnt3res,
426 static inline enum clnt_stat
427 nfs2_mount(CLIENT *clnt, mnt2arg_t *mnt2arg, mnt2res_t *mnt2res)
429 return clnt_call(clnt, MOUNTPROC_MNT,
430 (xdrproc_t) xdr_dirpath, (caddr_t) mnt2arg,
431 (xdrproc_t) xdr_fhstatus, (caddr_t) mnt2res,
436 nfs_call_mount(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server,
437 mntarg_t *mntarg, mntres_t *mntres)
443 if (!probe_bothports(mnt_server, nfs_server))
446 clnt = mnt_openclnt(mnt_server, &msock);
449 /* make pointers in xdr_mountres3 NULL so
450 * that xdr_array allocates memory for us
452 memset(mntres, 0, sizeof(*mntres));
453 switch (mnt_server->pmap.pm_vers) {
455 stat = nfs3_mount(clnt, mntarg, &mntres->nfsv3);
459 stat = nfs2_mount(clnt, mntarg, &mntres->nfsv2);
464 if (stat != RPC_SUCCESS) {
465 clnt_geterr(clnt, &rpc_createerr.cf_error);
466 rpc_createerr.cf_stat = stat;
468 mnt_closeclnt(clnt, msock);
469 if (stat == RPC_SUCCESS)
476 parse_options(char *old_opts, struct nfs_mount_data *data,
477 int *bg, int *retry, clnt_addr_t *mnt_server,
478 clnt_addr_t *nfs_server, char *new_opts, const int opt_size)
480 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
481 struct pmap *mnt_pmap = &mnt_server->pmap;
482 struct pmap *nfs_pmap = &nfs_server->pmap;
484 char *opt, *opteq, *p, *opt_b;
485 char *mounthost = NULL;
492 len = strlen(new_opts);
493 for (p=old_opts, opt_b=NULL; p && *p; p++) {
495 opt_b = p; /* begin of the option item */
497 open_quote ^= 1; /* reverse the status */
499 continue; /* still in a quoted block */
501 *p = '\0'; /* terminate the option item */
502 if (*p == '\0' || *(p+1) == '\0') {
503 opt = opt_b; /* opt is useful now */
507 continue; /* still somewhere in the option item */
509 if (strlen(opt) >= sizeof(cbuf))
511 if ((opteq = strchr(opt, '=')) && isdigit(opteq[1])) {
512 int val = atoi(opteq + 1);
514 /* printf("opt=%s\n", opt); */
515 if (!strcmp(opt, "rsize"))
517 else if (!strcmp(opt, "wsize"))
519 else if (!strcmp(opt, "timeo"))
521 else if (!strcmp(opt, "retrans"))
523 else if (!strcmp(opt, "acregmin"))
524 data->acregmin = val;
525 else if (!strcmp(opt, "acregmax"))
526 data->acregmax = val;
527 else if (!strcmp(opt, "acdirmin"))
528 data->acdirmin = val;
529 else if (!strcmp(opt, "acdirmax"))
530 data->acdirmax = val;
531 else if (!strcmp(opt, "actimeo")) {
532 data->acregmin = val;
533 data->acregmax = val;
534 data->acdirmin = val;
535 data->acdirmax = val;
537 else if (!strcmp(opt, "retry"))
539 else if (!strcmp(opt, "port"))
540 nfs_pmap->pm_port = val;
541 else if (!strcmp(opt, "mountport"))
542 mnt_pmap->pm_port = val;
543 else if (!strcmp(opt, "mountprog"))
544 mnt_pmap->pm_prog = val;
545 else if (!strcmp(opt, "mountvers"))
546 mnt_pmap->pm_vers = val;
547 else if (!strcmp(opt, "mounthost"))
548 mounthost=xstrndup(opteq+1, strcspn(opteq+1," \t\n\r,"));
549 else if (!strcmp(opt, "nfsprog"))
550 nfs_pmap->pm_prog = val;
551 else if (!strcmp(opt, "nfsvers") ||
552 !strcmp(opt, "vers")) {
553 nfs_pmap->pm_vers = val;
555 #if NFS_MOUNT_VERSION >= 2
556 } else if (!strcmp(opt, "namlen")) {
557 if (nfs_mount_version >= 2)
564 } else if (!strcmp(opt, "addr")) {
571 sprintf(cbuf, "%s=%s,", opt, opteq+1);
574 if (!strcmp(opt, "proto")) {
575 if (!strcmp(opteq+1, "udp")) {
576 nfs_pmap->pm_prot = IPPROTO_UDP;
577 mnt_pmap->pm_prot = IPPROTO_UDP;
578 #if NFS_MOUNT_VERSION >= 2
579 data->flags &= ~NFS_MOUNT_TCP;
580 } else if (!strcmp(opteq+1, "tcp") &&
581 nfs_mount_version > 2) {
582 nfs_pmap->pm_prot = IPPROTO_TCP;
583 mnt_pmap->pm_prot = IPPROTO_TCP;
584 data->flags |= NFS_MOUNT_TCP;
590 #if NFS_MOUNT_VERSION >= 5
591 } else if (!strcmp(opt, "sec")) {
592 char *secflavor = opteq+1;
594 if (nfs_mount_version < 5) {
595 printf(_("Warning: ignoring sec=%s option\n"), secflavor);
597 } else if (!strcmp(secflavor, "none"))
598 data->pseudoflavor = AUTH_NONE;
599 else if (!strcmp(secflavor, "sys"))
600 data->pseudoflavor = AUTH_SYS;
601 else if (!strcmp(secflavor, "krb5"))
602 data->pseudoflavor = AUTH_GSS_KRB5;
603 else if (!strcmp(secflavor, "krb5i"))
604 data->pseudoflavor = AUTH_GSS_KRB5I;
605 else if (!strcmp(secflavor, "krb5p"))
606 data->pseudoflavor = AUTH_GSS_KRB5P;
607 else if (!strcmp(secflavor, "lipkey"))
608 data->pseudoflavor = AUTH_GSS_LKEY;
609 else if (!strcmp(secflavor, "lipkey-i"))
610 data->pseudoflavor = AUTH_GSS_LKEYI;
611 else if (!strcmp(secflavor, "lipkey-p"))
612 data->pseudoflavor = AUTH_GSS_LKEYP;
613 else if (!strcmp(secflavor, "spkm3"))
614 data->pseudoflavor = AUTH_GSS_SPKM;
615 else if (!strcmp(secflavor, "spkm3i"))
616 data->pseudoflavor = AUTH_GSS_SPKMI;
617 else if (!strcmp(secflavor, "spkm3p"))
618 data->pseudoflavor = AUTH_GSS_SPKMP;
622 printf(_("Warning: Unrecognized security flavor %s.\n"),
626 data->flags |= NFS_MOUNT_SECFLAVOUR;
628 } else if (!strcmp(opt, "mounthost"))
629 mounthost=xstrndup(opteq+1,
630 strcspn(opteq+1," \t\n\r,"));
631 else if (!strcmp(opt, "context")) {
632 char *context = opteq + 1;
633 int ctxlen = strlen(context);
635 if (ctxlen > NFS_MAX_CONTEXT_LEN) {
636 printf(_("context parameter exceeds limit of %d\n"),
637 NFS_MAX_CONTEXT_LEN);
640 /* The context string is in the format of
641 * "system_u:object_r:...". We only want
642 * the context str between the quotes.
645 strncpy(data->context, context+1,
648 strncpy(data->context, context,
649 NFS_MAX_CONTEXT_LEN);
654 sprintf(cbuf, "%s=%s,", opt, opteq+1);
657 if (!strncmp(opt, "no", 2)) {
661 if (!strcmp(opt, "bg"))
663 else if (!strcmp(opt, "fg"))
665 else if (!strcmp(opt, "soft")) {
666 data->flags &= ~NFS_MOUNT_SOFT;
668 data->flags |= NFS_MOUNT_SOFT;
669 } else if (!strcmp(opt, "hard")) {
670 data->flags &= ~NFS_MOUNT_SOFT;
672 data->flags |= NFS_MOUNT_SOFT;
673 } else if (!strcmp(opt, "intr")) {
674 data->flags &= ~NFS_MOUNT_INTR;
676 data->flags |= NFS_MOUNT_INTR;
677 } else if (!strcmp(opt, "posix")) {
678 data->flags &= ~NFS_MOUNT_POSIX;
680 data->flags |= NFS_MOUNT_POSIX;
681 } else if (!strcmp(opt, "cto")) {
682 data->flags &= ~NFS_MOUNT_NOCTO;
684 data->flags |= NFS_MOUNT_NOCTO;
685 } else if (!strcmp(opt, "ac")) {
686 data->flags &= ~NFS_MOUNT_NOAC;
688 data->flags |= NFS_MOUNT_NOAC;
689 #if NFS_MOUNT_VERSION >= 2
690 } else if (!strcmp(opt, "tcp")) {
691 data->flags &= ~NFS_MOUNT_TCP;
693 if (nfs_mount_version < 2)
695 nfs_pmap->pm_prot = IPPROTO_TCP;
696 mnt_pmap->pm_prot = IPPROTO_TCP;
697 data->flags |= NFS_MOUNT_TCP;
699 mnt_pmap->pm_prot = IPPROTO_UDP;
700 nfs_pmap->pm_prot = IPPROTO_UDP;
702 } else if (!strcmp(opt, "udp")) {
703 data->flags &= ~NFS_MOUNT_TCP;
705 if (nfs_mount_version < 2)
707 nfs_pmap->pm_prot = IPPROTO_TCP;
708 mnt_pmap->pm_prot = IPPROTO_TCP;
709 data->flags |= NFS_MOUNT_TCP;
711 nfs_pmap->pm_prot = IPPROTO_UDP;
712 mnt_pmap->pm_prot = IPPROTO_UDP;
715 #if NFS_MOUNT_VERSION >= 3
716 } else if (!strcmp(opt, "lock")) {
717 data->flags &= ~NFS_MOUNT_NONLM;
719 if (nfs_mount_version < 3)
721 data->flags |= NFS_MOUNT_NONLM;
724 #if NFS_MOUNT_VERSION >= 4
725 } else if (!strcmp(opt, "broken_suid")) {
726 data->flags &= ~NFS_MOUNT_BROKEN_SUID;
728 if (nfs_mount_version < 4)
730 data->flags |= NFS_MOUNT_BROKEN_SUID;
732 } else if (!strcmp(opt, "acl")) {
733 data->flags &= ~NFS_MOUNT_NOACL;
735 data->flags |= NFS_MOUNT_NOACL;
736 } else if (!strcmp(opt, "rdirplus")) {
737 data->flags &= ~NFS_MOUNT_NORDIRPLUS;
739 data->flags |= NFS_MOUNT_NORDIRPLUS;
740 } else if (!strcmp(opt, "sharecache")) {
741 data->flags &= ~NFS_MOUNT_UNSHARED;
743 data->flags |= NFS_MOUNT_UNSHARED;
749 printf(_("Unsupported nfs mount option: "
750 "%s%s\n"), val ? "" : "no", opt);
753 sprintf(cbuf, val ? "%s,":"no%s,", opt);
756 if (len >= opt_size) {
757 printf(_("mount: excessively long option argument\n"));
760 strcat(new_opts, cbuf);
762 /* See if the nfs host = mount host. */
764 if (!nfs_gethostbyname(mounthost, mnt_saddr))
766 *mnt_server->hostname = mounthost;
770 printf(_("Bad nfs mount parameter: %s\n"), opt);
776 nfsmnt_check_compat(const struct pmap *nfs_pmap, const struct pmap *mnt_pmap)
778 if (nfs_pmap->pm_vers &&
779 (nfs_pmap->pm_vers > MAX_NFSPROT || nfs_pmap->pm_vers < 2)) {
780 if (nfs_pmap->pm_vers == 4)
781 fprintf(stderr, _("'vers=4' is not supported. "
782 "Use '-t nfs4' instead.\n"));
784 fprintf(stderr, _("NFS version %ld is not supported.\n"),
788 if (mnt_pmap->pm_vers > MAX_MNTPROT) {
789 fprintf(stderr, _("NFS mount version %ld s not supported.\n"),
799 nfsmount(const char *spec, const char *node, int *flags,
800 char **extra_opts, char **mount_opts,
801 int running_bg, int *need_statd)
803 static char *prev_bg_host;
805 char *hostname, *dirname, *old_opts, *mounthost = NULL;
806 char new_opts[1024], cbuf[1024];
807 static struct nfs_mount_data data;
809 static int doonce = 0;
811 clnt_addr_t mnt_server = { &mounthost, };
812 clnt_addr_t nfs_server = { &hostname, };
813 struct sockaddr_in *nfs_saddr = &nfs_server.saddr;
814 struct pmap *mnt_pmap = &mnt_server.pmap,
815 *nfs_pmap = &nfs_server.pmap;
816 struct pmap save_mnt, save_nfs;
825 int retval = EX_FAIL;
830 nfs_mount_version = find_kernel_nfs_mount_version();
832 if (strlen(spec) >= sizeof(hostdir)) {
833 fprintf(stderr, _("mount: "
834 "excessively long host:dir argument\n"));
837 strcpy(hostdir, spec);
838 if ((s = strchr(hostdir, ':'))) {
842 /* Ignore all but first hostname in replicated mounts
843 until they can be fully supported. (mack@sgi.com) */
844 if ((s = strchr(hostdir, ','))) {
848 "multiple hostnames not supported\n"));
853 "directory to mount not in host:dir format\n"));
857 if (!nfs_gethostbyname(hostname, nfs_saddr))
859 mounthost = hostname;
860 memcpy (&mnt_server.saddr, nfs_saddr, sizeof (mnt_server.saddr));
862 /* add IP address to mtab options for use when unmounting */
864 s = inet_ntoa(nfs_saddr->sin_addr);
865 old_opts = *extra_opts;
869 /* Set default options.
870 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
871 * let the kernel decide.
872 * timeo is filled in after we know whether it'll be TCP or UDP. */
873 memset(&data, 0, sizeof(data));
878 #if NFS_MOUNT_VERSION >= 2
879 data.namlen = NAME_MAX;
883 retry = 10000; /* 10000 minutes ~ 1 week */
885 memset(mnt_pmap, 0, sizeof(*mnt_pmap));
886 mnt_pmap->pm_prog = MOUNTPROG;
887 memset(nfs_pmap, 0, sizeof(*nfs_pmap));
888 nfs_pmap->pm_prog = NFS_PROGRAM;
892 if (!parse_options(old_opts, &data, &bg, &retry, &mnt_server, &nfs_server,
893 new_opts, sizeof(new_opts)))
895 if (!nfsmnt_check_compat(nfs_pmap, mnt_pmap))
898 if (retry == 10000 && !bg)
899 retry = 2; /* reset for fg mounts */
902 #ifdef NFS_MOUNT_DEBUG
903 printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
904 data.rsize, data.wsize, data.timeo, data.retrans);
905 printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
906 data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
907 printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
908 nfs_pmap->pm_port, bg, retry, data.flags);
909 printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
910 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
911 nfs_pmap->pm_prog, nfs_pmap->pm_vers);
912 printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d ",
913 (data.flags & NFS_MOUNT_SOFT) != 0,
914 (data.flags & NFS_MOUNT_INTR) != 0,
915 (data.flags & NFS_MOUNT_POSIX) != 0,
916 (data.flags & NFS_MOUNT_NOCTO) != 0,
917 (data.flags & NFS_MOUNT_NOAC) != 0);
918 #if NFS_MOUNT_VERSION >= 2
920 (data.flags & NFS_MOUNT_TCP) != 0);
922 #if NFS_MOUNT_VERSION >= 4
923 printf("noacl = %d ", (data.flags & NFS_MOUNT_NOACL) != 0);
925 #if NFS_MOUNT_VERSION >= 5
926 printf("sec = %u ", data.pseudoflavor);
927 printf("readdirplus = %d ", (data.flags & NFS_MOUNT_NORDIRPLUS) != 0);
932 data.version = nfs_mount_version;
933 *mount_opts = (char *) &data;
935 if (*flags & MS_REMOUNT)
939 * If the previous mount operation on the same host was
940 * backgrounded, and the "bg" for this mount is also set,
941 * give up immediately, to avoid the initial timeout.
943 if (bg && !running_bg &&
944 prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
950 /* create mount deamon client */
953 * The following loop implements the mount retries. On the first
954 * call, "running_bg" is 0. When the mount times out, and the
955 * "bg" option is set, the exit status EX_BG will be returned.
956 * For a backgrounded mount, there will be a second call by the
957 * child process with "running_bg" set to 1.
959 * The case where the mount point is not present and the "bg"
960 * option is set, is treated as a timeout. This is done to
961 * support nested mounts.
963 * The "retry" count specified by the user is the number of
964 * minutes to retry before giving up.
966 * Only the first error message will be displayed.
968 timeout = time(NULL) + 60 * retry;
973 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
974 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
976 if (bg && stat(node, &statbuf) == -1) {
977 /* no mount point yet - sleep */
979 sleep(val); /* 1, 2, 4, 8, 16, 30, ... */
986 /* be careful not to use too many CPU cycles */
990 stat = nfs_call_mount(&mnt_server, &nfs_server,
994 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
995 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
999 switch(rpc_createerr.cf_stat){
1002 case RPC_SYSTEMERROR:
1003 if (errno == ETIMEDOUT)
1006 mount_errors(*nfs_server.hostname, 0, bg);
1011 mount_errors(*nfs_server.hostname, 0, bg);
1014 mount_errors(*nfs_server.hostname, 1, bg);
1018 prev_bg_host = xstrdup(hostname);
1025 mount_errors(*nfs_server.hostname, 0, bg);
1029 mount_errors(*nfs_server.hostname, 1, bg);
1032 if (nfs_pmap->pm_vers == 2) {
1033 if (mntres.nfsv2.fhs_status != 0) {
1035 _("mount: %s:%s failed, reason given by server: %s\n"),
1037 nfs_strerror(mntres.nfsv2.fhs_status));
1040 memcpy(data.root.data,
1041 (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
1043 #if NFS_MOUNT_VERSION >= 4
1044 data.root.size = NFS_FHSIZE;
1045 memcpy(data.old_root.data,
1046 (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
1050 #if NFS_MOUNT_VERSION >= 4
1051 mountres3_ok *mountres;
1053 int i, *flavor, yum = 0;
1054 if (mntres.nfsv3.fhs_status != 0) {
1056 _("mount: %s:%s failed, reason given by server: %s\n"),
1058 nfs_strerror(mntres.nfsv3.fhs_status));
1061 #if NFS_MOUNT_VERSION >= 5
1062 mountres = &mntres.nfsv3.mountres3_u.mountinfo;
1063 i = mountres->auth_flavors.auth_flavors_len;
1065 goto noauth_flavors;
1067 flavor = mountres->auth_flavors.auth_flavors_val;
1069 /* If no flavour requested, use first simple
1070 * flavour that is offered.
1072 if (! (data.flags & NFS_MOUNT_SECFLAVOUR) &&
1073 (flavor[i] == AUTH_SYS ||
1074 flavor[i] == AUTH_NONE)) {
1075 data.pseudoflavor = flavor[i];
1076 data.flags |= NFS_MOUNT_SECFLAVOUR;
1078 if (flavor[i] == data.pseudoflavor)
1080 #ifdef NFS_MOUNT_DEBUG
1081 printf("auth flavor %d: %d\n",
1087 "mount: %s:%s failed, "
1088 "security flavor not supported\n",
1090 /* server has registered us in rmtab, send umount */
1091 nfs_call_umount(&mnt_server, &dirname);
1096 fhandle = &mntres.nfsv3.mountres3_u.mountinfo.fhandle;
1097 memset(data.old_root.data, 0, NFS_FHSIZE);
1098 memset(&data.root, 0, sizeof(data.root));
1099 data.root.size = fhandle->fhandle3_len;
1100 memcpy(data.root.data,
1101 (char *) fhandle->fhandle3_val,
1102 fhandle->fhandle3_len);
1104 data.flags |= NFS_MOUNT_VER3;
1108 if (nfs_mount_version == 1) {
1109 /* create nfs socket for kernel */
1110 if (nfs_pmap->pm_prot == IPPROTO_TCP)
1111 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1113 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1115 perror(_("nfs socket"));
1118 if (bindresvport(fsock, 0) < 0) {
1119 perror(_("nfs bindresvport"));
1124 #ifdef NFS_MOUNT_DEBUG
1125 printf(_("using port %d for nfs deamon\n"), nfs_pmap->pm_port);
1127 nfs_saddr->sin_port = htons(nfs_pmap->pm_port);
1129 * connect() the socket for kernels 1.3.10 and below only,
1130 * to avoid problems with multihomed hosts.
1133 if (linux_version_code() <= 0x01030a && fsock != -1
1134 && connect(fsock, (struct sockaddr *) nfs_saddr,
1135 sizeof (*nfs_saddr)) < 0) {
1136 perror(_("nfs connect"));
1140 #if NFS_MOUNT_VERSION >= 2
1141 if (nfs_pmap->pm_prot == IPPROTO_TCP)
1142 data.flags |= NFS_MOUNT_TCP;
1144 data.flags &= ~NFS_MOUNT_TCP;
1147 /* prepare data structure for kernel */
1150 memcpy((char *) &data.addr, (char *) nfs_saddr, sizeof(data.addr));
1151 strncpy(data.hostname, hostname, sizeof(data.hostname));
1154 /* Ensure we have enough padding for the following strcat()s */
1155 if (strlen(new_opts) + strlen(s) + 30 >= sizeof(new_opts)) {
1156 fprintf(stderr, _("mount: "
1157 "excessively long option argument\n"));
1161 snprintf(cbuf, sizeof(cbuf)-1, "addr=%s", s);
1162 strcat(new_opts, cbuf);
1164 *extra_opts = xstrdup(new_opts);
1165 *need_statd = ! (data.flags & NFS_MOUNT_NONLM);