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>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
55 #include <sys/mount.h>
62 #include "nfsumount.h"
63 #include "nfs_mount.h"
64 #include "mount_constants.h"
75 #define MAX_NFSPROT ((nfs_mount_data_version >= 4) ? 3 : 2)
76 #define MAX_MNTPROT ((nfs_mount_data_version >= 4) ? 3 : 2)
77 #define HAVE_RELIABLE_TCP (nfs_mount_data_version >= 4)
79 #ifndef HAVE_INET_ATON
80 #define inet_aton(a,b) (0)
83 typedef dirpath mnt2arg_t;
84 typedef dirpath mnt3arg_t;
85 typedef dirpath mntarg_t;
87 typedef struct fhstatus mnt2res_t;
88 typedef struct mountres3 mnt3res_t;
94 extern int nfs_mount_data_version;
98 extern int linux_version_code();
100 /* Define the order in which to probe for UDP/TCP services */
107 proto_probelist(enum plist list)
109 static const u_int probe_udp_tcp[] = { IPPROTO_UDP, IPPROTO_TCP, 0 };
110 static const u_int probe_both[] = { IPPROTO_TCP, IPPROTO_UDP, 0 };
111 static const u_int probe_udponly[] = { IPPROTO_UDP, 0 };
116 return probe_udp_tcp;
117 return probe_udponly;
120 /* Define the order in which NFS versions are probed on portmapper */
121 static const u_long *
122 nfs_probelist(const int vers)
124 static const u_long nfs2_probe[] = { 2, 0};
125 static const u_long nfs3_probe[] = { 3, 2, 0};
134 /* Define the order in which Mountd versions are probed on portmapper */
135 static const u_long *
136 mnt_probelist(const int vers)
138 static const u_long mnt1_probe[] = { 1, 2, 0 };
139 static const u_long mnt3_probe[] = { 3, 1, 2, 0 };
148 int nfs_gethostbyname(const char *, struct sockaddr_in *);
149 int nfs_gethostbyname(const char *hostname, struct sockaddr_in *saddr)
153 saddr->sin_family = AF_INET;
154 if (!inet_aton(hostname, &saddr->sin_addr)) {
155 if ((hp = gethostbyname(hostname)) == NULL) {
156 fprintf(stderr, _("mount: can't get address for %s\n"),
160 if (hp->h_length > sizeof(*saddr)) {
162 _("mount: got bad hp->h_length\n"));
163 hp->h_length = sizeof(*saddr);
165 memcpy(&saddr->sin_addr, hp->h_addr, hp->h_length);
172 * getport() is very similar to pmap_getport() with
173 * the exception this version uses a non-reserve ports
174 * instead of reserve ports since reserve ports
175 * are not needed for pmap requests.
179 struct sockaddr_in *saddr,
190 saddr->sin_port = htons (PMAPPORT);
191 socket = get_socket(saddr, prot, FALSE, FALSE);
195 clnt = clntudp_bufcreate(saddr,
196 PMAPPROG, PMAPVERS, TIMEOUT, &socket,
197 UDPMSGSIZE, UDPMSGSIZE);
200 clnt = clnttcp_create(saddr,
201 PMAPPROG, PMAPVERS, &socket, 50, 500);
205 parms.pm_prog = prog;
206 parms.pm_vers = vers;
207 parms.pm_prot = prot;
208 parms.pm_port = 0; /* not needed or used */
210 stat = clnt_call(clnt, PMAPPROC_GETPORT, (xdrproc_t)xdr_pmap,
211 (caddr_t)&parms, (xdrproc_t)xdr_u_short, (caddr_t)&port, TIMEOUT);
213 clnt_geterr(clnt, &rpc_createerr.cf_error);
214 rpc_createerr.cf_stat = stat;
217 if (stat != RPC_SUCCESS)
220 rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
229 * Use the portmapper to discover whether or not the service we want is
230 * available. The lists 'versions' and 'protos' define ordered sequences
231 * of service versions and udp/tcp protocols to probe for.
234 probe_port(clnt_addr_t *server,
235 const u_long *versions,
238 struct sockaddr_in *saddr = &server->saddr;
239 struct pmap *pmap = &server->pmap;
240 const u_long prog = pmap->pm_prog, *p_vers;
241 const u_int prot = (u_int)pmap->pm_prot,
243 const u_short port = (u_short) pmap->pm_port;
244 u_long vers = pmap->pm_vers;
246 p_prot = prot ? &prot : protos;
247 p_vers = vers ? &vers : versions;
248 rpc_createerr.cf_stat = 0;
250 saddr->sin_port = htons(PMAPPORT);
251 p_port = getport(saddr, prog, *p_vers, *p_prot);
253 if (!port || port == p_port) {
254 saddr->sin_port = htons(p_port);
257 "mount: trying %s prog %ld vers %ld prot %s port %d\n",
258 inet_ntoa(saddr->sin_addr), prog, *p_vers,
259 *p_prot == IPPROTO_UDP ? "udp" : "tcp", p_port);
261 if (clnt_ping(saddr, prog, *p_vers, *p_prot, NULL))
263 if (rpc_createerr.cf_stat == RPC_TIMEDOUT)
267 if (rpc_createerr.cf_stat != RPC_PROGNOTREGISTERED)
275 if (vers == pmap->pm_vers) {
279 if (vers || !*++p_vers)
287 pmap->pm_vers = *p_vers;
289 pmap->pm_prot = *p_prot;
291 pmap->pm_port = p_port;
292 rpc_createerr.cf_stat = 0;
297 probe_nfsport(clnt_addr_t *nfs_server)
299 const struct pmap *pmap = &nfs_server->pmap;
300 const u_long *probe_vers;
301 const u_int *probe_prot;
303 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
305 probe_vers = nfs_probelist(MAX_NFSPROT);
306 probe_prot = proto_probelist(HAVE_RELIABLE_TCP ? use_tcp : udp_only);
307 return probe_port(nfs_server, probe_vers, probe_prot);
310 int probe_mntport(clnt_addr_t *mnt_server)
312 const struct pmap *pmap = &mnt_server->pmap;
313 const u_long *probe_vers;
314 const u_int *probe_prot;
316 if (pmap->pm_vers && pmap->pm_prot && pmap->pm_port)
318 probe_vers = mnt_probelist(MAX_MNTPROT);
319 probe_prot = proto_probelist(HAVE_RELIABLE_TCP ? udp_tcp : udp_only);
320 return probe_port(mnt_server, probe_vers, probe_prot);
324 probe_bothports(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server)
326 struct pmap *nfs_pmap = &nfs_server->pmap;
327 struct pmap *mnt_pmap = &mnt_server->pmap;
328 struct pmap save_nfs, save_mnt;
330 const u_long *probe_vers;
332 if (mnt_pmap->pm_vers && !nfs_pmap->pm_vers)
333 nfs_pmap->pm_vers = mntvers_to_nfs(mnt_pmap->pm_vers);
334 else if (nfs_pmap->pm_vers && !mnt_pmap->pm_vers)
335 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
336 if (nfs_pmap->pm_vers)
338 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
339 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
340 for (probe_vers = mnt_probelist(MAX_MNTPROT); *probe_vers; probe_vers++) {
341 nfs_pmap->pm_vers = mntvers_to_nfs(*probe_vers);
342 if ((res = probe_nfsport(nfs_server) != 0)) {
343 mnt_pmap->pm_vers = nfsvers_to_mnt(nfs_pmap->pm_vers);
344 if ((res = probe_mntport(mnt_server)) != 0)
346 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
348 switch (rpc_createerr.cf_stat) {
349 case RPC_PROGVERSMISMATCH:
350 case RPC_PROGNOTREGISTERED:
355 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
360 if (!probe_nfsport(nfs_server))
362 return probe_mntport(mnt_server);
365 static inline enum clnt_stat
366 nfs3_mount(CLIENT *clnt, mnt3arg_t *mnt3arg, mnt3res_t *mnt3res)
368 return clnt_call(clnt, MOUNTPROC3_MNT,
369 (xdrproc_t) xdr_dirpath, (caddr_t) mnt3arg,
370 (xdrproc_t) xdr_mountres3, (caddr_t) mnt3res,
374 static inline enum clnt_stat
375 nfs2_mount(CLIENT *clnt, mnt2arg_t *mnt2arg, mnt2res_t *mnt2res)
377 return clnt_call(clnt, MOUNTPROC_MNT,
378 (xdrproc_t) xdr_dirpath, (caddr_t) mnt2arg,
379 (xdrproc_t) xdr_fhstatus, (caddr_t) mnt2res,
384 nfs_call_mount(clnt_addr_t *mnt_server, clnt_addr_t *nfs_server,
385 mntarg_t *mntarg, mntres_t *mntres)
391 if (!probe_bothports(mnt_server, nfs_server))
394 clnt = mnt_openclnt(mnt_server, &msock);
397 /* make pointers in xdr_mountres3 NULL so
398 * that xdr_array allocates memory for us
400 memset(mntres, 0, sizeof(*mntres));
401 switch (mnt_server->pmap.pm_vers) {
403 stat = nfs3_mount(clnt, mntarg, &mntres->nfsv3);
407 stat = nfs2_mount(clnt, mntarg, &mntres->nfsv2);
412 if (stat != RPC_SUCCESS) {
413 clnt_geterr(clnt, &rpc_createerr.cf_error);
414 rpc_createerr.cf_stat = stat;
416 mnt_closeclnt(clnt, msock);
417 if (stat == RPC_SUCCESS)
424 parse_options(char *old_opts, struct nfs_mount_data *data,
425 int *bg, int *retry, clnt_addr_t *mnt_server,
426 clnt_addr_t *nfs_server, char *new_opts, const int opt_size)
428 struct sockaddr_in *mnt_saddr = &mnt_server->saddr;
429 struct pmap *mnt_pmap = &mnt_server->pmap;
430 struct pmap *nfs_pmap = &nfs_server->pmap;
432 char *opt, *opteq, *p, *opt_b;
433 char *mounthost = NULL;
440 len = strlen(new_opts);
441 for (p=old_opts, opt_b=NULL; p && *p; p++) {
443 opt_b = p; /* begin of the option item */
445 open_quote ^= 1; /* reverse the status */
447 continue; /* still in a quoted block */
449 *p = '\0'; /* terminate the option item */
450 if (*p == '\0' || *(p+1) == '\0') {
451 opt = opt_b; /* opt is useful now */
455 continue; /* still somewhere in the option item */
457 if (strlen(opt) >= sizeof(cbuf))
459 if ((opteq = strchr(opt, '=')) && isdigit(opteq[1])) {
460 int val = atoi(opteq + 1);
462 /* printf("opt=%s\n", opt); */
463 if (!strcmp(opt, "rsize"))
465 else if (!strcmp(opt, "wsize"))
467 else if (!strcmp(opt, "timeo"))
469 else if (!strcmp(opt, "retrans"))
471 else if (!strcmp(opt, "acregmin"))
472 data->acregmin = val;
473 else if (!strcmp(opt, "acregmax"))
474 data->acregmax = val;
475 else if (!strcmp(opt, "acdirmin"))
476 data->acdirmin = val;
477 else if (!strcmp(opt, "acdirmax"))
478 data->acdirmax = val;
479 else if (!strcmp(opt, "actimeo")) {
480 data->acregmin = val;
481 data->acregmax = val;
482 data->acdirmin = val;
483 data->acdirmax = val;
485 else if (!strcmp(opt, "retry"))
487 else if (!strcmp(opt, "port"))
488 nfs_pmap->pm_port = val;
489 else if (!strcmp(opt, "mountport"))
490 mnt_pmap->pm_port = val;
491 else if (!strcmp(opt, "mountprog"))
492 mnt_pmap->pm_prog = val;
493 else if (!strcmp(opt, "mountvers"))
494 mnt_pmap->pm_vers = val;
495 else if (!strcmp(opt, "mounthost"))
496 mounthost=xstrndup(opteq+1, strcspn(opteq+1," \t\n\r,"));
497 else if (!strcmp(opt, "nfsprog"))
498 nfs_pmap->pm_prog = val;
499 else if (!strcmp(opt, "nfsvers") ||
500 !strcmp(opt, "vers")) {
501 nfs_pmap->pm_vers = val;
503 #if NFS_MOUNT_VERSION >= 2
504 } else if (!strcmp(opt, "namlen")) {
505 if (nfs_mount_data_version >= 2)
512 } else if (!strcmp(opt, "addr")) {
519 sprintf(cbuf, "%s=%s,", opt, opteq+1);
522 if (!strcmp(opt, "proto")) {
523 if (!strcmp(opteq+1, "udp")) {
524 nfs_pmap->pm_prot = IPPROTO_UDP;
525 mnt_pmap->pm_prot = IPPROTO_UDP;
526 #if NFS_MOUNT_VERSION >= 2
527 data->flags &= ~NFS_MOUNT_TCP;
528 } else if (!strcmp(opteq+1, "tcp") &&
529 nfs_mount_data_version > 2) {
530 nfs_pmap->pm_prot = IPPROTO_TCP;
531 mnt_pmap->pm_prot = IPPROTO_TCP;
532 data->flags |= NFS_MOUNT_TCP;
538 #if NFS_MOUNT_VERSION >= 5
539 } else if (!strcmp(opt, "sec")) {
540 char *secflavor = opteq+1;
542 if (nfs_mount_data_version < 5) {
543 printf(_("Warning: ignoring sec=%s option\n"), secflavor);
545 } else if (!strcmp(secflavor, "none"))
546 data->pseudoflavor = AUTH_NONE;
547 else if (!strcmp(secflavor, "sys"))
548 data->pseudoflavor = AUTH_SYS;
549 else if (!strcmp(secflavor, "krb5"))
550 data->pseudoflavor = AUTH_GSS_KRB5;
551 else if (!strcmp(secflavor, "krb5i"))
552 data->pseudoflavor = AUTH_GSS_KRB5I;
553 else if (!strcmp(secflavor, "krb5p"))
554 data->pseudoflavor = AUTH_GSS_KRB5P;
555 else if (!strcmp(secflavor, "lipkey"))
556 data->pseudoflavor = AUTH_GSS_LKEY;
557 else if (!strcmp(secflavor, "lipkey-i"))
558 data->pseudoflavor = AUTH_GSS_LKEYI;
559 else if (!strcmp(secflavor, "lipkey-p"))
560 data->pseudoflavor = AUTH_GSS_LKEYP;
561 else if (!strcmp(secflavor, "spkm3"))
562 data->pseudoflavor = AUTH_GSS_SPKM;
563 else if (!strcmp(secflavor, "spkm3i"))
564 data->pseudoflavor = AUTH_GSS_SPKMI;
565 else if (!strcmp(secflavor, "spkm3p"))
566 data->pseudoflavor = AUTH_GSS_SPKMP;
570 printf(_("Warning: Unrecognized security flavor %s.\n"),
574 data->flags |= NFS_MOUNT_SECFLAVOUR;
576 } else if (!strcmp(opt, "mounthost"))
577 mounthost=xstrndup(opteq+1,
578 strcspn(opteq+1," \t\n\r,"));
579 else if (!strcmp(opt, "context")) {
580 char *context = opteq + 1;
581 int ctxlen = strlen(context);
583 if (ctxlen > NFS_MAX_CONTEXT_LEN) {
584 printf(_("context parameter exceeds limit of %d\n"),
585 NFS_MAX_CONTEXT_LEN);
588 /* The context string is in the format of
589 * "system_u:object_r:...". We only want
590 * the context str between the quotes.
593 strncpy(data->context, context+1,
596 strncpy(data->context, context,
597 NFS_MAX_CONTEXT_LEN);
602 sprintf(cbuf, "%s=%s,", opt, opteq+1);
605 if (!strncmp(opt, "no", 2)) {
609 if (!strcmp(opt, "bg"))
611 else if (!strcmp(opt, "fg"))
613 else if (!strcmp(opt, "soft")) {
614 data->flags &= ~NFS_MOUNT_SOFT;
616 data->flags |= NFS_MOUNT_SOFT;
617 } else if (!strcmp(opt, "hard")) {
618 data->flags &= ~NFS_MOUNT_SOFT;
620 data->flags |= NFS_MOUNT_SOFT;
621 } else if (!strcmp(opt, "intr")) {
622 data->flags &= ~NFS_MOUNT_INTR;
624 data->flags |= NFS_MOUNT_INTR;
625 } else if (!strcmp(opt, "posix")) {
626 data->flags &= ~NFS_MOUNT_POSIX;
628 data->flags |= NFS_MOUNT_POSIX;
629 } else if (!strcmp(opt, "cto")) {
630 data->flags &= ~NFS_MOUNT_NOCTO;
632 data->flags |= NFS_MOUNT_NOCTO;
633 } else if (!strcmp(opt, "ac")) {
634 data->flags &= ~NFS_MOUNT_NOAC;
636 data->flags |= NFS_MOUNT_NOAC;
637 #if NFS_MOUNT_VERSION >= 2
638 } else if (!strcmp(opt, "tcp")) {
639 data->flags &= ~NFS_MOUNT_TCP;
641 if (nfs_mount_data_version < 2)
643 nfs_pmap->pm_prot = IPPROTO_TCP;
644 mnt_pmap->pm_prot = IPPROTO_TCP;
645 data->flags |= NFS_MOUNT_TCP;
647 mnt_pmap->pm_prot = IPPROTO_UDP;
648 nfs_pmap->pm_prot = IPPROTO_UDP;
650 } else if (!strcmp(opt, "udp")) {
651 data->flags &= ~NFS_MOUNT_TCP;
653 if (nfs_mount_data_version < 2)
655 nfs_pmap->pm_prot = IPPROTO_TCP;
656 mnt_pmap->pm_prot = IPPROTO_TCP;
657 data->flags |= NFS_MOUNT_TCP;
659 nfs_pmap->pm_prot = IPPROTO_UDP;
660 mnt_pmap->pm_prot = IPPROTO_UDP;
663 #if NFS_MOUNT_VERSION >= 3
664 } else if (!strcmp(opt, "lock")) {
665 data->flags &= ~NFS_MOUNT_NONLM;
667 if (nfs_mount_data_version < 3)
669 data->flags |= NFS_MOUNT_NONLM;
672 #if NFS_MOUNT_VERSION >= 4
673 } else if (!strcmp(opt, "broken_suid")) {
674 data->flags &= ~NFS_MOUNT_BROKEN_SUID;
676 if (nfs_mount_data_version < 4)
678 data->flags |= NFS_MOUNT_BROKEN_SUID;
680 } else if (!strcmp(opt, "acl")) {
681 data->flags &= ~NFS_MOUNT_NOACL;
683 data->flags |= NFS_MOUNT_NOACL;
684 } else if (!strcmp(opt, "rdirplus")) {
685 data->flags &= ~NFS_MOUNT_NORDIRPLUS;
687 data->flags |= NFS_MOUNT_NORDIRPLUS;
688 } else if (!strcmp(opt, "sharecache")) {
689 data->flags &= ~NFS_MOUNT_UNSHARED;
691 data->flags |= NFS_MOUNT_UNSHARED;
697 printf(_("Unsupported nfs mount option: "
698 "%s%s\n"), val ? "" : "no", opt);
701 sprintf(cbuf, val ? "%s,":"no%s,", opt);
704 if (len >= opt_size) {
705 printf(_("mount: excessively long option argument\n"));
708 strcat(new_opts, cbuf);
710 /* See if the nfs host = mount host. */
712 if (!nfs_gethostbyname(mounthost, mnt_saddr))
714 *mnt_server->hostname = mounthost;
718 printf(_("Bad nfs mount parameter: %s\n"), opt);
724 nfsmnt_check_compat(const struct pmap *nfs_pmap, const struct pmap *mnt_pmap)
726 if (nfs_pmap->pm_vers &&
727 (nfs_pmap->pm_vers > MAX_NFSPROT || nfs_pmap->pm_vers < 2)) {
728 if (nfs_pmap->pm_vers == 4)
729 fprintf(stderr, _("'vers=4' is not supported. "
730 "Use '-t nfs4' instead.\n"));
732 fprintf(stderr, _("NFS version %ld is not supported.\n"),
736 if (mnt_pmap->pm_vers > MAX_MNTPROT) {
737 fprintf(stderr, _("NFS mount version %ld s not supported.\n"),
747 nfsmount(const char *spec, const char *node, int *flags,
748 char **extra_opts, char **mount_opts,
749 int running_bg, int *need_statd)
751 static char *prev_bg_host;
753 char *hostname, *dirname, *old_opts, *mounthost = NULL;
754 char new_opts[1024], cbuf[1024];
755 static struct nfs_mount_data data;
757 static int doonce = 0;
759 clnt_addr_t mnt_server = { &mounthost, };
760 clnt_addr_t nfs_server = { &hostname, };
761 struct sockaddr_in *nfs_saddr = &nfs_server.saddr;
762 struct pmap *mnt_pmap = &mnt_server.pmap,
763 *nfs_pmap = &nfs_server.pmap;
764 struct pmap save_mnt, save_nfs;
773 int retval = EX_FAIL;
778 if (strlen(spec) >= sizeof(hostdir)) {
779 fprintf(stderr, _("mount: "
780 "excessively long host:dir argument\n"));
783 strcpy(hostdir, spec);
784 if ((s = strchr(hostdir, ':'))) {
788 /* Ignore all but first hostname in replicated mounts
789 until they can be fully supported. (mack@sgi.com) */
790 if ((s = strchr(hostdir, ','))) {
794 "multiple hostnames not supported\n"));
799 "directory to mount not in host:dir format\n"));
803 if (!nfs_gethostbyname(hostname, nfs_saddr))
805 mounthost = hostname;
806 memcpy (&mnt_server.saddr, nfs_saddr, sizeof (mnt_server.saddr));
808 /* add IP address to mtab options for use when unmounting */
810 s = inet_ntoa(nfs_saddr->sin_addr);
811 old_opts = *extra_opts;
815 /* Set default options.
816 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
817 * let the kernel decide.
818 * timeo is filled in after we know whether it'll be TCP or UDP. */
819 memset(&data, 0, sizeof(data));
824 #if NFS_MOUNT_VERSION >= 2
825 data.namlen = NAME_MAX;
829 retry = 10000; /* 10000 minutes ~ 1 week */
831 memset(mnt_pmap, 0, sizeof(*mnt_pmap));
832 mnt_pmap->pm_prog = MOUNTPROG;
833 memset(nfs_pmap, 0, sizeof(*nfs_pmap));
834 nfs_pmap->pm_prog = NFS_PROGRAM;
838 if (!parse_options(old_opts, &data, &bg, &retry, &mnt_server, &nfs_server,
839 new_opts, sizeof(new_opts)))
841 if (!nfsmnt_check_compat(nfs_pmap, mnt_pmap))
844 if (retry == 10000 && !bg)
845 retry = 2; /* reset for fg mounts */
848 #ifdef NFS_MOUNT_DEBUG
849 printf("rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
850 data.rsize, data.wsize, data.timeo, data.retrans);
851 printf("acreg (min, max) = (%d, %d), acdir (min, max) = (%d, %d)\n",
852 data.acregmin, data.acregmax, data.acdirmin, data.acdirmax);
853 printf("port = %d, bg = %d, retry = %d, flags = %.8x\n",
854 nfs_pmap->pm_port, bg, retry, data.flags);
855 printf("mountprog = %d, mountvers = %d, nfsprog = %d, nfsvers = %d\n",
856 mnt_pmap->pm_prog, mnt_pmap->pm_vers,
857 nfs_pmap->pm_prog, nfs_pmap->pm_vers);
858 printf("soft = %d, intr = %d, posix = %d, nocto = %d, noac = %d ",
859 (data.flags & NFS_MOUNT_SOFT) != 0,
860 (data.flags & NFS_MOUNT_INTR) != 0,
861 (data.flags & NFS_MOUNT_POSIX) != 0,
862 (data.flags & NFS_MOUNT_NOCTO) != 0,
863 (data.flags & NFS_MOUNT_NOAC) != 0);
864 #if NFS_MOUNT_VERSION >= 2
866 (data.flags & NFS_MOUNT_TCP) != 0);
868 #if NFS_MOUNT_VERSION >= 4
869 printf("noacl = %d ", (data.flags & NFS_MOUNT_NOACL) != 0);
871 #if NFS_MOUNT_VERSION >= 5
872 printf("sec = %u ", data.pseudoflavor);
873 printf("readdirplus = %d ", (data.flags & NFS_MOUNT_NORDIRPLUS) != 0);
878 data.version = nfs_mount_data_version;
879 *mount_opts = (char *) &data;
881 if (*flags & MS_REMOUNT)
885 * If the previous mount operation on the same host was
886 * backgrounded, and the "bg" for this mount is also set,
887 * give up immediately, to avoid the initial timeout.
889 if (bg && !running_bg &&
890 prev_bg_host && strcmp(hostname, prev_bg_host) == 0) {
896 /* create mount deamon client */
899 * The following loop implements the mount retries. On the first
900 * call, "running_bg" is 0. When the mount times out, and the
901 * "bg" option is set, the exit status EX_BG will be returned.
902 * For a backgrounded mount, there will be a second call by the
903 * child process with "running_bg" set to 1.
905 * The case where the mount point is not present and the "bg"
906 * option is set, is treated as a timeout. This is done to
907 * support nested mounts.
909 * The "retry" count specified by the user is the number of
910 * minutes to retry before giving up.
912 * Only the first error message will be displayed.
914 timeout = time(NULL) + 60 * retry;
919 memcpy(&save_nfs, nfs_pmap, sizeof(save_nfs));
920 memcpy(&save_mnt, mnt_pmap, sizeof(save_mnt));
922 if (bg && stat(node, &statbuf) == -1) {
923 /* no mount point yet - sleep */
925 sleep(val); /* 1, 2, 4, 8, 16, 30, ... */
932 /* be careful not to use too many CPU cycles */
936 stat = nfs_call_mount(&mnt_server, &nfs_server,
940 memcpy(nfs_pmap, &save_nfs, sizeof(*nfs_pmap));
941 memcpy(mnt_pmap, &save_mnt, sizeof(*mnt_pmap));
945 switch(rpc_createerr.cf_stat){
948 case RPC_SYSTEMERROR:
949 if (errno == ETIMEDOUT)
952 mount_errors(*nfs_server.hostname, 0, bg);
957 mount_errors(*nfs_server.hostname, 0, bg);
960 mount_errors(*nfs_server.hostname, 1, bg);
964 prev_bg_host = xstrdup(hostname);
971 mount_errors(*nfs_server.hostname, 0, bg);
975 mount_errors(*nfs_server.hostname, 1, bg);
978 if (nfs_pmap->pm_vers == 2) {
979 if (mntres.nfsv2.fhs_status != 0) {
981 _("mount: %s:%s failed, reason given by server: %s\n"),
983 nfs_strerror(mntres.nfsv2.fhs_status));
986 memcpy(data.root.data,
987 (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
989 #if NFS_MOUNT_VERSION >= 4
990 data.root.size = NFS_FHSIZE;
991 memcpy(data.old_root.data,
992 (char *) mntres.nfsv2.fhstatus_u.fhs_fhandle,
996 #if NFS_MOUNT_VERSION >= 4
997 mountres3_ok *mountres;
999 int i, *flavor, yum = 0;
1000 if (mntres.nfsv3.fhs_status != 0) {
1002 _("mount: %s:%s failed, reason given by server: %s\n"),
1004 nfs_strerror(mntres.nfsv3.fhs_status));
1007 #if NFS_MOUNT_VERSION >= 5
1008 mountres = &mntres.nfsv3.mountres3_u.mountinfo;
1009 i = mountres->auth_flavors.auth_flavors_len;
1011 goto noauth_flavors;
1013 flavor = mountres->auth_flavors.auth_flavors_val;
1015 /* If no flavour requested, use first simple
1016 * flavour that is offered.
1018 if (! (data.flags & NFS_MOUNT_SECFLAVOUR) &&
1019 (flavor[i] == AUTH_SYS ||
1020 flavor[i] == AUTH_NONE)) {
1021 data.pseudoflavor = flavor[i];
1022 data.flags |= NFS_MOUNT_SECFLAVOUR;
1024 if (flavor[i] == data.pseudoflavor)
1026 #ifdef NFS_MOUNT_DEBUG
1027 printf("auth flavor %d: %d\n",
1033 "mount: %s:%s failed, "
1034 "security flavor not supported\n",
1036 /* server has registered us in rmtab, send umount */
1037 nfs_call_umount(&mnt_server, &dirname);
1042 fhandle = &mntres.nfsv3.mountres3_u.mountinfo.fhandle;
1043 memset(data.old_root.data, 0, NFS_FHSIZE);
1044 memset(&data.root, 0, sizeof(data.root));
1045 data.root.size = fhandle->fhandle3_len;
1046 memcpy(data.root.data,
1047 (char *) fhandle->fhandle3_val,
1048 fhandle->fhandle3_len);
1050 data.flags |= NFS_MOUNT_VER3;
1054 if (nfs_mount_data_version == 1) {
1055 /* create nfs socket for kernel */
1056 if (nfs_pmap->pm_prot == IPPROTO_TCP)
1057 fsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
1059 fsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1061 perror(_("nfs socket"));
1064 if (bindresvport(fsock, 0) < 0) {
1065 perror(_("nfs bindresvport"));
1070 #ifdef NFS_MOUNT_DEBUG
1071 printf(_("using port %d for nfs deamon\n"), nfs_pmap->pm_port);
1073 nfs_saddr->sin_port = htons(nfs_pmap->pm_port);
1075 * connect() the socket for kernels 1.3.10 and below only,
1076 * to avoid problems with multihomed hosts.
1079 if (linux_version_code() <= 0x01030a && fsock != -1
1080 && connect(fsock, (struct sockaddr *) nfs_saddr,
1081 sizeof (*nfs_saddr)) < 0) {
1082 perror(_("nfs connect"));
1086 #if NFS_MOUNT_VERSION >= 2
1087 if (nfs_pmap->pm_prot == IPPROTO_TCP)
1088 data.flags |= NFS_MOUNT_TCP;
1090 data.flags &= ~NFS_MOUNT_TCP;
1093 /* prepare data structure for kernel */
1096 memcpy((char *) &data.addr, (char *) nfs_saddr, sizeof(data.addr));
1097 strncpy(data.hostname, hostname, sizeof(data.hostname));
1100 /* Ensure we have enough padding for the following strcat()s */
1101 if (strlen(new_opts) + strlen(s) + 30 >= sizeof(new_opts)) {
1102 fprintf(stderr, _("mount: "
1103 "excessively long option argument\n"));
1107 snprintf(cbuf, sizeof(cbuf)-1, "addr=%s", s);
1108 strcat(new_opts, cbuf);
1110 *extra_opts = xstrdup(new_opts);
1111 *need_statd = ! (data.flags & NFS_MOUNT_NONLM);