2 * Copyright (c) 1997-2006 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * File: am-utils/amd/srvr_nfs.c
50 #endif /* HAVE_CONFIG_H */
55 * Number of pings allowed to fail before host is declared down
56 * - three-fifths of the allowed mount time...
58 #define MAX_ALLOWED_PINGS (3 + /* for luck ... */ 1)
61 * How often to ping when starting a new server
63 #define FAST_NFS_PING 3
65 #if (FAST_NFS_PING * MAX_ALLOWED_PINGS) >= ALLOWED_MOUNT_TIME
66 # error: sanity check failed in srvr_nfs.c
68 * you cannot do things this way...
69 * sufficient fast pings must be given the chance to fail
70 * within the allowed mount time
72 #endif /* (FAST_NFS_PING * MAX_ALLOWED_PINGS) >= ALLOWED_MOUNT_TIME */
74 /* structures and typedefs */
75 typedef struct nfs_private {
76 u_short np_mountd; /* Mount daemon port number */
77 char np_mountd_inval; /* Port *may* be invalid */
78 int np_ping; /* Number of failed ping attempts */
79 time_t np_ttl; /* Time when server is thought dead */
80 int np_xid; /* RPC transaction id for pings */
81 int np_error; /* Error during portmap request */
85 qelem nfs_srvr_list = {&nfs_srvr_list, &nfs_srvr_list};
88 static int global_xid; /* For NFS pings */
89 #define XID_ALLOC() (++global_xid)
92 # define NUM_NFS_VERS 2
93 #else /* not HAVE_FS_NFS3 */
94 # define NUM_NFS_VERS 1
95 #endif /* not HAVE_FS_NFS3 */
96 static int ping_len[NUM_NFS_VERS];
97 static char ping_buf[NUM_NFS_VERS][sizeof(struct rpc_msg) + 32];
99 #if defined(MNTTAB_OPT_PROTO) || defined(HAVE_FS_NFS3)
101 * Protocols we know about, in order of preference.
103 * Note that Solaris 8 and newer NetBSD systems are switching to UDP first,
104 * so this order may have to be adjusted for Amd in the future once more
105 * vendors make that change. -Erez 11/24/2000
107 * Or we might simply make this is a platform-specific order. -Ion 09/13/2003
109 static char *protocols[] = { "tcp", "udp", NULL };
110 #endif /* defined(MNTTAB_OPT_PROTO) || defined(HAVE_FS_NFS3) */
112 /* forward definitions */
113 static void nfs_keepalive(voidp);
117 * Flush cached data for an fserver (or for all, if fs==NULL)
120 flush_srvr_nfs_cache(fserver *fs)
124 ITER(fs2, fserver, &nfs_srvr_list) {
125 if (fs == NULL || fs == fs2) {
126 nfs_private *np = (nfs_private *) fs2->fs_private;
128 np->np_mountd_inval = TRUE;
137 * Startup the NFS ping for a particular version.
140 create_ping_payload(u_long nfs_version)
143 struct rpc_msg ping_msg;
146 * Non nfs mounts like /afs/glue.umd.edu have ended up here.
148 if (nfs_version == 0) {
149 nfs_version = NFS_VERSION;
150 plog(XLOG_WARNING, "create_ping_payload: nfs_version = 0, changed to 2");
152 plog(XLOG_INFO, "create_ping_payload: nfs_version: %d", (int) nfs_version);
154 rpc_msg_init(&ping_msg, NFS_PROGRAM, nfs_version, NFSPROC_NULL);
157 * Create an XDR endpoint
159 xdrmem_create(&ping_xdr, ping_buf[nfs_version - NFS_VERSION], sizeof(ping_buf[0]), XDR_ENCODE);
162 * Create the NFS ping message
164 if (!xdr_callmsg(&ping_xdr, &ping_msg)) {
165 plog(XLOG_ERROR, "Couldn't create ping RPC message");
169 * Find out how long it is
171 ping_len[nfs_version - NFS_VERSION] = xdr_getpos(&ping_xdr);
174 * Destroy the XDR endpoint - we don't need it anymore
176 xdr_destroy(&ping_xdr);
181 * Called when a portmap reply arrives
184 got_portmap(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, voidp idv, int done)
186 fserver *fs2 = (fserver *) idv;
190 * Find which fileserver we are talking about
192 ITER(fs, fserver, &nfs_srvr_list)
197 u_long port = 0; /* XXX - should be short but protocol is naff */
198 int error = done ? pickup_rpc_reply(pkt, len, (voidp) &port, (XDRPROC_T_TYPE) xdr_u_long) : -1;
199 nfs_private *np = (nfs_private *) fs->fs_private;
201 if (!error && port) {
202 dlog("got port (%d) for mountd on %s", (int) port, fs->fs_host);
204 * Grab the port number. Portmap sends back
205 * an u_long in native ordering, so it
206 * needs converting to a u_short in
209 np->np_mountd = htons((u_short) port);
210 np->np_mountd_inval = FALSE;
213 dlog("Error fetching port for mountd on %s", fs->fs_host);
214 dlog("\t error=%d, port=%d", error, (int) port);
216 * Almost certainly no mountd running on remote host
218 np->np_error = error ? error : ETIMEDOUT;
221 if (fs->fs_flags & FSF_WANT)
224 dlog("Got portmap for old port request");
226 dlog("portmap request timed out");
232 * Obtain portmap information
235 call_portmap(fserver *fs, AUTH *auth, u_long prog, u_long vers, u_long prot)
237 struct rpc_msg pmap_msg;
239 char iobuf[UDPMSGSIZE];
243 rpc_msg_init(&pmap_msg, PMAPPROG, PMAPVERS, PMAPPROC_NULL);
248 len = make_rpc_packet(iobuf,
253 (XDRPROC_T_TYPE) xdr_pmap,
256 struct sockaddr_in sin;
257 memset((voidp) &sin, 0, sizeof(sin));
259 sin.sin_port = htons(PMAPPORT);
260 error = fwd_packet(RPC_XID_PORTMAP, iobuf, len,
261 &sin, &sin, (voidp) fs, got_portmap);
271 recompute_portmap(fserver *fs)
277 * No portmap calls for pure WebNFS servers.
279 if (fs->fs_flags & FSF_WEBNFS)
285 error = make_nfs_auth();
288 nfs_private *np = (nfs_private *) fs->fs_private;
289 np->np_error = error;
293 if (fs->fs_version == 0)
294 plog(XLOG_WARNING, "recompute_portmap: nfs_version = 0 fixed");
296 plog(XLOG_INFO, "recompute_portmap: NFS version %d on %s",
297 (int) fs->fs_version, fs->fs_host);
299 if (fs->fs_version == NFS_VERSION3)
300 mnt_version = AM_MOUNTVERS3;
302 #endif /* HAVE_FS_NFS3 */
303 mnt_version = MOUNTVERS;
305 plog(XLOG_INFO, "Using MOUNT version: %d", (int) mnt_version);
306 call_portmap(fs, nfs_auth, MOUNTPROG, mnt_version, (u_long) IPPROTO_UDP);
311 get_mountd_port(fserver *fs, u_short *port, wchan_t wchan)
318 nfs_private *np = (nfs_private *) fs->fs_private;
319 if (np->np_error == 0) {
320 *port = np->np_mountd;
323 error = np->np_error;
326 * Now go get the port mapping again in case it changed.
327 * Note that it is used even if (np_mountd_inval)
328 * is True. The flag is used simply as an
329 * indication that the mountd may be invalid, not
330 * that it is known to be invalid.
332 if (np->np_mountd_inval)
333 recompute_portmap(fs);
335 np->np_mountd_inval = TRUE;
337 if (error < 0 && wchan && !(fs->fs_flags & FSF_WANT)) {
339 * If a wait channel is supplied, and no
340 * error has yet occurred, then arrange
341 * that a wakeup is done on the wait channel,
342 * whenever a wakeup is done on this fs node.
343 * Wakeup's are done on the fs node whenever
344 * it changes state - thus causing control to
345 * come back here and new, better things to happen.
347 fs->fs_flags |= FSF_WANT;
348 sched_task(wakeup_task, wchan, (wchan_t) fs);
355 * This is called when we get a reply to an RPC ping.
356 * The value of id was taken from the nfs_private
357 * structure when the ping was transmitted.
360 nfs_keepalive_callback(voidp pkt, int len, struct sockaddr_in *sp, struct sockaddr_in *tsp, voidp idv, int done)
362 int xid = (long) idv; /* cast needed for 64-bit archs */
372 ITER(fs, fserver, &nfs_srvr_list) {
373 nfs_private *np = (nfs_private *) fs->fs_private;
374 if (np->np_xid == xid && (fs->fs_flags & FSF_PINGING)) {
376 * Reset the ping counter.
377 * Update the keepalive timer.
380 if (fs->fs_flags & FSF_DOWN) {
381 fs->fs_flags &= ~FSF_DOWN;
382 if (fs->fs_flags & FSF_VALID) {
383 srvrlog(fs, "is up");
388 srvrlog(fs, "starts up");
389 fs->fs_flags |= FSF_VALID;
394 if (fs->fs_flags & FSF_VALID) {
395 dlog("file server %s type nfs is still up", fs->fs_host);
399 fs->fs_flags |= FSF_VALID;
404 * Adjust ping interval
406 untimeout(fs->fs_cid);
407 fs->fs_cid = timeout(fs->fs_pinger, nfs_keepalive, (voidp) fs);
410 * Update ttl for this server
412 np->np_ttl = clocktime(NULL) +
413 (MAX_ALLOWED_PINGS - 1) * FAST_NFS_PING + fs->fs_pinger - 1;
418 np->np_xid = XID_ALLOC();
421 * Failed pings is zero...
426 * Recompute portmap information if not known
428 if (np->np_mountd_inval)
429 recompute_portmap(fs);
437 dlog("Spurious ping packet");
442 check_fs_addr_change(fserver *fs)
444 struct hostent *hp = NULL;
446 char *old_ipaddr, *new_ipaddr;
448 hp = gethostbyname(fs->fs_host);
450 hp->h_addrtype != AF_INET ||
451 !STREQ((char *) hp->h_name, fs->fs_host) ||
452 memcmp((voidp) &fs->fs_ip->sin_addr,
454 sizeof(fs->fs_ip->sin_addr)) == 0)
456 /* if got here: downed server changed IP address */
457 old_ipaddr = strdup(inet_ntoa(fs->fs_ip->sin_addr));
458 memmove((voidp) &ia, (voidp) hp->h_addr, sizeof(struct in_addr));
459 new_ipaddr = inet_ntoa(ia); /* ntoa uses static buf */
460 plog(XLOG_WARNING, "EZK: down fileserver %s changed ip: %s -> %s",
461 fs->fs_host, old_ipaddr, new_ipaddr);
463 /* copy new IP addr */
464 memmove((voidp) &fs->fs_ip->sin_addr,
466 sizeof(fs->fs_ip->sin_addr));
467 /* XXX: do we need to un/set these flags? */
468 fs->fs_flags &= ~FSF_DOWN;
469 fs->fs_flags |= FSF_VALID | FSF_WANT;
470 map_flush_srvr(fs); /* XXX: a race with flush_srvr_nfs_cache? */
471 flush_srvr_nfs_cache(fs);
472 fs->fs_flags |= FSF_FORCE_UNMOUNT;
475 flush_nfs_fhandle_cache(fs); /* done in caller: nfs_keepalive_timeout */
476 /* XXX: need to purge nfs_private so that somehow it will get re-initialized? */
482 * Called when no ping-reply received
485 nfs_keepalive_timeout(voidp v)
488 nfs_private *np = (nfs_private *) fs->fs_private;
491 * Another ping has failed
495 srvrlog(fs, "not responding");
498 * Not known to be up any longer
501 fs->fs_flags &= ~FSF_VALID;
504 * If ttl has expired then guess that it is dead
506 if (np->np_ttl < clocktime(NULL)) {
507 int oflags = fs->fs_flags;
508 dlog("ttl has expired");
509 if ((fs->fs_flags & FSF_DOWN) == 0) {
511 * Server was up, but is now down.
513 srvrlog(fs, "is down");
514 fs->fs_flags |= FSF_DOWN | FSF_VALID;
516 * Since the server is down, the portmap
517 * information may now be wrong, so it
518 * must be flushed from the local cache
520 flush_nfs_fhandle_cache(fs);
522 check_fs_addr_change(fs); /* check if IP addr of fserver changed */
527 if ((fs->fs_flags & FSF_VALID) == 0)
528 srvrlog(fs, "starts down");
529 fs->fs_flags |= FSF_VALID;
531 if (oflags != fs->fs_flags && (fs->fs_flags & FSF_WANT))
534 * Reset failed ping count
539 dlog("%d pings to %s failed - at most %d allowed", np->np_ping, fs->fs_host, MAX_ALLOWED_PINGS);
543 * New RPC xid, so any late responses to the previous ping
546 np->np_xid = XID_ALLOC();
549 * Run keepalive again
556 * Keep track of whether a server is alive
559 nfs_keepalive(voidp v)
563 nfs_private *np = (nfs_private *) fs->fs_private;
567 * Send an NFS ping to this node
570 if (ping_len[fs->fs_version - NFS_VERSION] == 0)
571 create_ping_payload(fs->fs_version);
574 * Queue the packet...
576 error = fwd_packet(MK_RPC_XID(RPC_XID_NFSPING, np->np_xid),
577 ping_buf[fs->fs_version - NFS_VERSION],
578 ping_len[fs->fs_version - NFS_VERSION],
580 (struct sockaddr_in *) 0,
581 (voidp) ((long) np->np_xid), /* cast needed for 64-bit archs */
582 nfs_keepalive_callback);
585 * See if a hard error occurred
592 np->np_ping = MAX_ALLOWED_PINGS; /* immediately down */
593 np->np_ttl = (time_t) 0;
595 * This causes an immediate call to nfs_keepalive_timeout
596 * whenever the server was thought to be up.
603 dlog("Sent NFS ping to %s", fs->fs_host);
608 * Back off the ping interval if we are not getting replies and
609 * the remote system is known to be down.
611 switch (fs->fs_flags & (FSF_DOWN | FSF_VALID)) {
612 case FSF_VALID: /* Up */
613 if (fstimeo < 0) /* +++ see above */
614 fstimeo = FAST_NFS_PING;
617 case FSF_VALID | FSF_DOWN: /* Down */
618 fstimeo = fs->fs_pinger;
621 default: /* Unknown */
622 fstimeo = FAST_NFS_PING;
626 dlog("NFS timeout in %d seconds", fstimeo);
628 fs->fs_cid = timeout(fstimeo, nfs_keepalive_timeout, (voidp) fs);
633 start_nfs_pings(fserver *fs, int pingval)
635 if (pingval == 0) /* could be because ping mnt option not found */
637 /* if pings haven't been initalized, then init them for first time */
638 if (fs->fs_flags & FSF_PING_UNINIT) {
639 fs->fs_flags &= ~FSF_PING_UNINIT;
640 plog(XLOG_INFO, "initializing %s's pinger to %d sec", fs->fs_host, pingval);
644 if ((fs->fs_flags & FSF_PINGING) && fs->fs_pinger == pingval) {
645 dlog("already running pings to %s", fs->fs_host);
649 /* if got here, then we need to update the ping value */
650 plog(XLOG_INFO, "changing %s's ping value from %d%s to %d%s",
652 fs->fs_pinger, (fs->fs_pinger < 0 ? " (off)" : ""),
653 pingval, (pingval < 0 ? " (off)" : ""));
655 fs->fs_pinger = pingval;
658 untimeout(fs->fs_cid);
660 srvrlog(fs, "wired up (pings disabled)");
661 fs->fs_flags |= FSF_VALID;
662 fs->fs_flags &= ~FSF_DOWN;
664 fs->fs_flags |= FSF_PINGING;
671 * Find an nfs server for a host.
674 find_nfs_srvr(mntfs *mf)
676 char *host = mf->mf_fo->opt_rhost;
681 struct hostent *hp = NULL;
682 struct sockaddr_in *ip = NULL;
683 u_long nfs_version = 0; /* default is no version specified */
684 u_long best_nfs_version = 0;
685 char *nfs_proto = NULL; /* no IP protocol either */
687 int nfs_port_opt = 0;
688 int fserver_is_down = 0;
691 * Get ping interval from mount options.
692 * Current only used to decide whether pings
693 * are required or not. < 0 = no pings.
695 mnt.mnt_opts = mf->mf_mopts;
696 pingval = hasmntval(&mnt, "ping");
698 if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
700 * the server granted us a filehandle, but we were unable to mount it.
701 * therefore, scale down to NFSv2/UDP and try again.
703 nfs_version = NFS_VERSION;
705 plog(XLOG_WARNING, "find_nfs_srvr: NFS mount failed, trying again with NFSv2/UDP");
706 mf->mf_flags &= ~MFF_NFS_SCALEDOWN;
709 * Get the NFS version from the mount options. This is used
710 * to decide the highest NFS version to try.
712 #ifdef MNTTAB_OPT_VERS
713 nfs_version = hasmntval(&mnt, MNTTAB_OPT_VERS);
714 #endif /* MNTTAB_OPT_VERS */
716 #ifdef MNTTAB_OPT_PROTO
718 char *proto_opt = hasmnteq(&mnt, MNTTAB_OPT_PROTO);
721 for (p = protocols; *p; p++)
722 if (NSTREQ(proto_opt, *p, strlen(*p))) {
727 plog(XLOG_WARNING, "ignoring unknown protocol option for %s:%s",
728 host, mf->mf_fo->opt_rfs);
731 #endif /* MNTTAB_OPT_PROTO */
733 #ifdef HAVE_NFS_NFSV2_H
734 /* allow overriding if nfsv2 option is specified in mount options */
735 if (amu_hasmntopt(&mnt, "nfsv2")) {
736 nfs_version = NFS_VERSION;/* nullify any ``vers=X'' statements */
737 nfs_proto = "udp"; /* nullify any ``proto=tcp'' statements */
738 plog(XLOG_WARNING, "found compatibility option \"nfsv2\": set options vers=2,proto=udp for host %s", host);
740 #endif /* HAVE_NFS_NFSV2_H */
742 /* check if we've globally overridden the NFS version/protocol */
744 nfs_version = gopt.nfs_vers;
745 plog(XLOG_INFO, "find_nfs_srvr: force NFS version to %d",
748 if (gopt.nfs_proto) {
749 nfs_proto = gopt.nfs_proto;
750 plog(XLOG_INFO, "find_nfs_srvr: force NFS protocol transport to %s", nfs_proto);
755 * lookup host address and canonical name
757 hp = gethostbyname(host);
760 * New code from Bob Harris <harris@basil-rathbone.mit.edu>
761 * Use canonical name to keep track of file server
762 * information. This way aliases do not generate
763 * multiple NFS pingers. (Except when we're normalizing
766 if (hp && !(gopt.flags & CFM_NORMALIZE_HOSTNAMES))
767 host = (char *) hp->h_name;
770 switch (hp->h_addrtype) {
772 ip = ALLOC(struct sockaddr_in);
773 memset((voidp) ip, 0, sizeof(*ip));
774 /* as per POSIX, sin_len need not be set (used internally by kernel) */
775 ip->sin_family = AF_INET;
776 memmove((voidp) &ip->sin_addr, (voidp) hp->h_addr, sizeof(ip->sin_addr));
780 plog(XLOG_USER, "No IP address for host %s", host);
784 plog(XLOG_USER, "Unknown host: %s", host);
789 * This may not be the best way to do things, but it really doesn't make
790 * sense to query a file server which is marked as 'down' for any
791 * version/proto combination.
793 ITER(fs, fserver, &nfs_srvr_list) {
794 if (FSRV_ISDOWN(fs) &&
795 STREQ(host, fs->fs_host)) {
796 plog(XLOG_WARNING, "fileserver %s is already hung - not running NFS proto/version discovery", host);
805 * Get the NFS Version, and verify server is up.
806 * If the client only supports NFSv2, hardcode it but still try to
807 * contact the remote portmapper to see if the service is running.
810 nfs_version = NFS_VERSION;
812 plog(XLOG_INFO, "The client supports only NFS(2,udp)");
813 #endif /* not HAVE_FS_NFS3 */
816 if (amu_hasmntopt(&mnt, MNTTAB_OPT_PUBLIC)) {
818 * Use WebNFS to obtain file handles.
820 mf->mf_flags |= MFF_WEBNFS;
821 plog(XLOG_INFO, "%s option used, NOT contacting the portmapper on %s",
822 MNTTAB_OPT_PUBLIC, host);
824 * Prefer NFSv3/tcp if the client supports it (cf. RFC 2054, 7).
828 nfs_version = NFS_VERSION3;
829 #else /* not HAVE_FS_NFS3 */
830 nfs_version = NFS_VERSION;
831 #endif /* not HAVE_FS_NFS3 */
832 plog(XLOG_INFO, "No NFS version specified, will use NFSv%d",
836 #if defined(MNTTAB_OPT_PROTO) || defined(HAVE_FS_NFS3)
838 #else /* not defined(MNTTAB_OPT_PROTO) || defined(HAVE_FS_NFS3) */
840 #endif /* not defined(MNTTAB_OPT_PROTO) || defined(HAVE_FS_NFS3) */
841 plog(XLOG_INFO, "No NFS protocol transport specified, will use %s",
846 * Find the best combination of NFS version and protocol.
847 * When given a choice, use the highest available version,
848 * and use TCP over UDP if available.
850 if (check_pmap_up(host, ip)) {
852 best_nfs_version = get_nfs_version(host, ip, nfs_version, nfs_proto);
853 nfs_port = ip->sin_port;
855 #ifdef MNTTAB_OPT_PROTO
857 u_int proto_nfs_version;
860 for (p = protocols; *p; p++) {
861 proto_nfs_version = get_nfs_version(host, ip, nfs_version, *p);
863 if (proto_nfs_version > best_nfs_version) {
864 best_nfs_version = proto_nfs_version;
866 nfs_port = ip->sin_port;
870 #endif /* MNTTAB_OPT_PROTO */
872 plog(XLOG_INFO, "portmapper service not running on %s", host);
875 /* use the portmapper results only nfs_version is not set yet */
876 if (!best_nfs_version) {
878 * If the NFS server is down or does not support the portmapper call
879 * (such as certain Novell NFS servers) we mark it as version 2 and we
880 * let the nfs code deal with the case when it is down. If/when the
881 * server comes back up and it can support NFSv3 and/or TCP, it will
884 if (nfs_version == 0) {
885 nfs_version = NFS_VERSION;
888 plog(XLOG_INFO, "NFS service not running on %s", host);
891 if (nfs_version == 0)
892 nfs_version = best_nfs_version;
893 plog(XLOG_INFO, "Using NFS version %d, protocol %s on host %s",
894 (int) nfs_version, nfs_proto, host);
899 * Determine the NFS port.
901 * A valid "port" mount option overrides anything else.
902 * If the port has been determined from the portmapper, use that.
903 * Default to NFS_PORT otherwise (cf. RFC 2054, 3).
905 nfs_port_opt = hasmntval(&mnt, MNTTAB_OPT_PORT);
906 if (nfs_port_opt > 0)
907 nfs_port = htons(nfs_port_opt);
909 nfs_port = htons(NFS_PORT);
911 dlog("find_nfs_srvr: using port %d for nfs on %s",
912 (int) ntohs(nfs_port), host);
913 ip->sin_port = nfs_port;
917 * Try to find an existing fs server structure for this host.
918 * Note that differing versions or protocols have their own structures.
919 * XXX: Need to fix the ping mechanism to actually use the NFS protocol
920 * chosen here (right now it always uses datagram sockets).
922 ITER(fs, fserver, &nfs_srvr_list) {
923 if (STREQ(host, fs->fs_host) &&
924 nfs_version == fs->fs_version &&
925 STREQ(nfs_proto, fs->fs_proto)) {
927 * fill in the IP address -- this is only needed
928 * if there is a chance an IP address will change
930 * Mike Mitchell, mcm@unx.sas.com, 09/08/93
932 if (hp && fs->fs_ip &&
933 memcmp((voidp) &fs->fs_ip->sin_addr,
935 sizeof(fs->fs_ip->sin_addr)) != 0) {
937 char *old_ipaddr, *new_ipaddr;
938 old_ipaddr = strdup(inet_ntoa(fs->fs_ip->sin_addr));
939 memmove((voidp) &ia, (voidp) hp->h_addr, sizeof(struct in_addr));
940 new_ipaddr = inet_ntoa(ia); /* ntoa uses static buf */
941 plog(XLOG_WARNING, "fileserver %s changed ip: %s -> %s",
942 fs->fs_host, old_ipaddr, new_ipaddr);
944 flush_nfs_fhandle_cache(fs);
945 memmove((voidp) &fs->fs_ip->sin_addr, (voidp) hp->h_addr, sizeof(fs->fs_ip->sin_addr));
949 * If the new file systems doesn't use WebNFS, the nfs pings may
950 * try to contact the portmapper.
952 if (!(mf->mf_flags & MFF_WEBNFS))
953 fs->fs_flags &= ~FSF_WEBNFS;
955 /* check if pingval needs to be updated/set/reset */
956 start_nfs_pings(fs, pingval);
959 * Following if statement from Mike Mitchell <mcm@unx.sas.com>
960 * Initialize the ping data if we aren't pinging now. The np_ttl and
961 * np_ping fields are especially important.
963 if (!(fs->fs_flags & FSF_PINGING)) {
964 np = (nfs_private *) fs->fs_private;
965 np->np_mountd_inval = TRUE;
966 np->np_xid = XID_ALLOC();
970 * Initially the server will be deemed dead
971 * after MAX_ALLOWED_PINGS of the fast variety
974 np->np_ttl = MAX_ALLOWED_PINGS * FAST_NFS_PING + clocktime(NULL) - 1;
975 start_nfs_pings(fs, pingval);
977 fs->fs_flags |= FSF_VALID | FSF_DOWN;
988 * Get here if we can't find an entry
992 * Allocate a new server
994 fs = ALLOC(struct fserver);
996 fs->fs_host = strdup(hp ? hp->h_name : "unknown_hostname");
997 if (gopt.flags & CFM_NORMALIZE_HOSTNAMES)
998 host_normalize(&fs->fs_host);
1002 fs->fs_flags = FSF_DOWN; /* Starts off down */
1004 fs->fs_flags = FSF_ERROR | FSF_VALID;
1005 mf->mf_flags |= MFF_ERROR;
1006 mf->mf_error = ENOENT;
1008 if (mf->mf_flags & MFF_WEBNFS)
1009 fs->fs_flags |= FSF_WEBNFS;
1010 fs->fs_version = nfs_version;
1011 fs->fs_proto = nfs_proto;
1012 fs->fs_type = MNTTAB_TYPE_NFS;
1013 fs->fs_pinger = AM_PINGER;
1014 fs->fs_flags |= FSF_PING_UNINIT; /* pinger hasn't been initialized */
1015 np = ALLOC(struct nfs_private);
1016 memset((voidp) np, 0, sizeof(*np));
1017 np->np_mountd_inval = TRUE;
1018 np->np_xid = XID_ALLOC();
1022 * Initially the server will be deemed dead after
1023 * MAX_ALLOWED_PINGS of the fast variety have failed.
1025 np->np_ttl = clocktime(NULL) + MAX_ALLOWED_PINGS * FAST_NFS_PING - 1;
1026 fs->fs_private = (voidp) np;
1027 fs->fs_prfree = (void (*)(voidp)) free;
1029 if (!FSRV_ERROR(fs)) {
1030 /* start of keepalive timer, first updating pingval */
1031 start_nfs_pings(fs, pingval);
1032 if (fserver_is_down)
1033 fs->fs_flags |= FSF_VALID | FSF_DOWN;
1037 * Add to list of servers
1039 ins_que(&fs->fs_q, &nfs_srvr_list);