Home | History | Annotate | Line # | Download | only in amd
srvr_nfs.c revision 1.1
      1  1.1  christos /*	$NetBSD: srvr_nfs.c,v 1.1 2008/09/19 20:07:17 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 1997-2007 Erez Zadok
      5  1.1  christos  * Copyright (c) 1990 Jan-Simon Pendry
      6  1.1  christos  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
      7  1.1  christos  * Copyright (c) 1990 The Regents of the University of California.
      8  1.1  christos  * All rights reserved.
      9  1.1  christos  *
     10  1.1  christos  * This code is derived from software contributed to Berkeley by
     11  1.1  christos  * Jan-Simon Pendry at Imperial College, London.
     12  1.1  christos  *
     13  1.1  christos  * Redistribution and use in source and binary forms, with or without
     14  1.1  christos  * modification, are permitted provided that the following conditions
     15  1.1  christos  * are met:
     16  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     17  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     18  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     20  1.1  christos  *    documentation and/or other materials provided with the distribution.
     21  1.1  christos  * 3. All advertising materials mentioning features or use of this software
     22  1.1  christos  *    must display the following acknowledgment:
     23  1.1  christos  *      This product includes software developed by the University of
     24  1.1  christos  *      California, Berkeley and its contributors.
     25  1.1  christos  * 4. Neither the name of the University nor the names of its contributors
     26  1.1  christos  *    may be used to endorse or promote products derived from this software
     27  1.1  christos  *    without specific prior written permission.
     28  1.1  christos  *
     29  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  1.1  christos  * SUCH DAMAGE.
     40  1.1  christos  *
     41  1.1  christos  *
     42  1.1  christos  * File: am-utils/amd/srvr_nfs.c
     43  1.1  christos  *
     44  1.1  christos  */
     45  1.1  christos 
     46  1.1  christos /*
     47  1.1  christos  * NFS server modeling
     48  1.1  christos  */
     49  1.1  christos 
     50  1.1  christos #ifdef HAVE_CONFIG_H
     51  1.1  christos # include <config.h>
     52  1.1  christos #endif /* HAVE_CONFIG_H */
     53  1.1  christos #include <am_defs.h>
     54  1.1  christos #include <amd.h>
     55  1.1  christos 
     56  1.1  christos /*
     57  1.1  christos  * Number of pings allowed to fail before host is declared down
     58  1.1  christos  * - three-fifths of the allowed mount time...
     59  1.1  christos  */
     60  1.1  christos #define	MAX_ALLOWED_PINGS	(3 + /* for luck ... */ 1)
     61  1.1  christos 
     62  1.1  christos /*
     63  1.1  christos  * How often to ping when starting a new server
     64  1.1  christos  */
     65  1.1  christos #define	FAST_NFS_PING		3
     66  1.1  christos 
     67  1.1  christos #if (FAST_NFS_PING * MAX_ALLOWED_PINGS) >= ALLOWED_MOUNT_TIME
     68  1.1  christos # error: sanity check failed in srvr_nfs.c
     69  1.1  christos /*
     70  1.1  christos  * you cannot do things this way...
     71  1.1  christos  * sufficient fast pings must be given the chance to fail
     72  1.1  christos  * within the allowed mount time
     73  1.1  christos  */
     74  1.1  christos #endif /* (FAST_NFS_PING * MAX_ALLOWED_PINGS) >= ALLOWED_MOUNT_TIME */
     75  1.1  christos 
     76  1.1  christos /* structures and typedefs */
     77  1.1  christos typedef struct nfs_private {
     78  1.1  christos   u_short np_mountd;		/* Mount daemon port number */
     79  1.1  christos   char np_mountd_inval;		/* Port *may* be invalid */
     80  1.1  christos   int np_ping;			/* Number of failed ping attempts */
     81  1.1  christos   time_t np_ttl;		/* Time when server is thought dead */
     82  1.1  christos   int np_xid;			/* RPC transaction id for pings */
     83  1.1  christos   int np_error;			/* Error during portmap request */
     84  1.1  christos } nfs_private;
     85  1.1  christos 
     86  1.1  christos /* globals */
     87  1.1  christos qelem nfs_srvr_list = {&nfs_srvr_list, &nfs_srvr_list};
     88  1.1  christos 
     89  1.1  christos /* statics */
     90  1.1  christos static int global_xid;		/* For NFS pings */
     91  1.1  christos #define	XID_ALLOC()		(++global_xid)
     92  1.1  christos 
     93  1.1  christos #ifdef HAVE_FS_NFS3
     94  1.1  christos # define NUM_NFS_VERS 2
     95  1.1  christos #else  /* not HAVE_FS_NFS3 */
     96  1.1  christos # define NUM_NFS_VERS 1
     97  1.1  christos #endif /* not HAVE_FS_NFS3 */
     98  1.1  christos static int ping_len[NUM_NFS_VERS];
     99  1.1  christos static char ping_buf[NUM_NFS_VERS][sizeof(struct rpc_msg) + 32];
    100  1.1  christos 
    101  1.1  christos #if defined(MNTTAB_OPT_PROTO) || defined(HAVE_FS_NFS3)
    102  1.1  christos /*
    103  1.1  christos  * Protocols we know about, in order of preference.
    104  1.1  christos  *
    105  1.1  christos  * Note that Solaris 8 and newer NetBSD systems are switching to UDP first,
    106  1.1  christos  * so this order may have to be adjusted for Amd in the future once more
    107  1.1  christos  * vendors make that change. -Erez 11/24/2000
    108  1.1  christos  *
    109  1.1  christos  * Or we might simply make this is a platform-specific order. -Ion 09/13/2003
    110  1.1  christos  */
    111  1.1  christos static char *protocols[] = { "tcp", "udp", NULL };
    112  1.1  christos #endif /* defined(MNTTAB_OPT_PROTO) || defined(HAVE_FS_NFS3) */
    113  1.1  christos 
    114  1.1  christos /* forward definitions */
    115  1.1  christos static void nfs_keepalive(voidp);
    116  1.1  christos 
    117  1.1  christos 
    118  1.1  christos /*
    119  1.1  christos  * Flush cached data for an fserver (or for all, if fs==NULL)
    120  1.1  christos  */
    121  1.1  christos void
    122  1.1  christos flush_srvr_nfs_cache(fserver *fs)
    123  1.1  christos {
    124  1.1  christos   fserver *fs2 = NULL;
    125  1.1  christos 
    126  1.1  christos   ITER(fs2, fserver, &nfs_srvr_list) {
    127  1.1  christos     if (fs == NULL || fs == fs2) {
    128  1.1  christos       nfs_private *np = (nfs_private *) fs2->fs_private;
    129  1.1  christos       if (np) {
    130  1.1  christos 	np->np_mountd_inval = TRUE;
    131  1.1  christos 	np->np_error = -1;
    132  1.1  christos       }
    133  1.1  christos     }
    134  1.1  christos   }
    135  1.1  christos }
    136  1.1  christos 
    137  1.1  christos 
    138  1.1  christos /*
    139  1.1  christos  * Startup the NFS ping for a particular version.
    140  1.1  christos  */
    141  1.1  christos static void
    142  1.1  christos create_ping_payload(u_long nfs_version)
    143  1.1  christos {
    144  1.1  christos   XDR ping_xdr;
    145  1.1  christos   struct rpc_msg ping_msg;
    146  1.1  christos 
    147  1.1  christos   /*
    148  1.1  christos    * Non nfs mounts like /afs/glue.umd.edu have ended up here.
    149  1.1  christos    */
    150  1.1  christos   if (nfs_version == 0) {
    151  1.1  christos     nfs_version = NFS_VERSION;
    152  1.1  christos     plog(XLOG_WARNING, "create_ping_payload: nfs_version = 0, changed to 2");
    153  1.1  christos   } else
    154  1.1  christos     plog(XLOG_INFO, "create_ping_payload: nfs_version: %d", (int) nfs_version);
    155  1.1  christos 
    156  1.1  christos   rpc_msg_init(&ping_msg, NFS_PROGRAM, nfs_version, NFSPROC_NULL);
    157  1.1  christos 
    158  1.1  christos   /*
    159  1.1  christos    * Create an XDR endpoint
    160  1.1  christos    */
    161  1.1  christos   xdrmem_create(&ping_xdr, ping_buf[nfs_version - NFS_VERSION], sizeof(ping_buf[0]), XDR_ENCODE);
    162  1.1  christos 
    163  1.1  christos   /*
    164  1.1  christos    * Create the NFS ping message
    165  1.1  christos    */
    166  1.1  christos   if (!xdr_callmsg(&ping_xdr, &ping_msg)) {
    167  1.1  christos     plog(XLOG_ERROR, "Couldn't create ping RPC message");
    168  1.1  christos     going_down(3);
    169  1.1  christos   }
    170  1.1  christos   /*
    171  1.1  christos    * Find out how long it is
    172  1.1  christos    */
    173  1.1  christos   ping_len[nfs_version - NFS_VERSION] = xdr_getpos(&ping_xdr);
    174  1.1  christos 
    175  1.1  christos   /*
    176  1.1  christos    * Destroy the XDR endpoint - we don't need it anymore
    177  1.1  christos    */
    178  1.1  christos   xdr_destroy(&ping_xdr);
    179  1.1  christos }
    180  1.1  christos 
    181  1.1  christos 
    182  1.1  christos /*
    183  1.1  christos  * Called when a portmap reply arrives
    184  1.1  christos  */
    185  1.1  christos static void
    186  1.1  christos got_portmap(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, voidp idv, int done)
    187  1.1  christos {
    188  1.1  christos   fserver *fs2 = (fserver *) idv;
    189  1.1  christos   fserver *fs = NULL;
    190  1.1  christos 
    191  1.1  christos   /*
    192  1.1  christos    * Find which fileserver we are talking about
    193  1.1  christos    */
    194  1.1  christos   ITER(fs, fserver, &nfs_srvr_list)
    195  1.1  christos     if (fs == fs2)
    196  1.1  christos       break;
    197  1.1  christos 
    198  1.1  christos   if (fs == fs2) {
    199  1.1  christos     u_long port = 0;	/* XXX - should be short but protocol is naff */
    200  1.1  christos     int error = done ? pickup_rpc_reply(pkt, len, (voidp) &port, (XDRPROC_T_TYPE) xdr_u_long) : -1;
    201  1.1  christos     nfs_private *np = (nfs_private *) fs->fs_private;
    202  1.1  christos 
    203  1.1  christos     if (!error && port) {
    204  1.1  christos       dlog("got port (%d) for mountd on %s", (int) port, fs->fs_host);
    205  1.1  christos       /*
    206  1.1  christos        * Grab the port number.  Portmap sends back
    207  1.1  christos        * an u_long in native ordering, so it
    208  1.1  christos        * needs converting to a u_short in
    209  1.1  christos        * network ordering.
    210  1.1  christos        */
    211  1.1  christos       np->np_mountd = htons((u_short) port);
    212  1.1  christos       np->np_mountd_inval = FALSE;
    213  1.1  christos       np->np_error = 0;
    214  1.1  christos     } else {
    215  1.1  christos       dlog("Error fetching port for mountd on %s", fs->fs_host);
    216  1.1  christos       dlog("\t error=%d, port=%d", error, (int) port);
    217  1.1  christos       /*
    218  1.1  christos        * Almost certainly no mountd running on remote host
    219  1.1  christos        */
    220  1.1  christos       np->np_error = error ? error : ETIMEDOUT;
    221  1.1  christos     }
    222  1.1  christos 
    223  1.1  christos     if (fs->fs_flags & FSF_WANT)
    224  1.1  christos       wakeup_srvr(fs);
    225  1.1  christos   } else if (done) {
    226  1.1  christos     dlog("Got portmap for old port request");
    227  1.1  christos   } else {
    228  1.1  christos     dlog("portmap request timed out");
    229  1.1  christos   }
    230  1.1  christos }
    231  1.1  christos 
    232  1.1  christos 
    233  1.1  christos /*
    234  1.1  christos  * Obtain portmap information
    235  1.1  christos  */
    236  1.1  christos static int
    237  1.1  christos call_portmap(fserver *fs, AUTH *auth, u_long prog, u_long vers, u_long prot)
    238  1.1  christos {
    239  1.1  christos   struct rpc_msg pmap_msg;
    240  1.1  christos   int len;
    241  1.1  christos   char iobuf[UDPMSGSIZE];
    242  1.1  christos   int error;
    243  1.1  christos   struct pmap pmap;
    244  1.1  christos 
    245  1.1  christos   rpc_msg_init(&pmap_msg, PMAPPROG, PMAPVERS, PMAPPROC_NULL);
    246  1.1  christos   pmap.pm_prog = prog;
    247  1.1  christos   pmap.pm_vers = vers;
    248  1.1  christos   pmap.pm_prot = prot;
    249  1.1  christos   pmap.pm_port = 0;
    250  1.1  christos   len = make_rpc_packet(iobuf,
    251  1.1  christos 			sizeof(iobuf),
    252  1.1  christos 			PMAPPROC_GETPORT,
    253  1.1  christos 			&pmap_msg,
    254  1.1  christos 			(voidp) &pmap,
    255  1.1  christos 			(XDRPROC_T_TYPE) xdr_pmap,
    256  1.1  christos 			auth);
    257  1.1  christos   if (len > 0) {
    258  1.1  christos     struct sockaddr_in sin;
    259  1.1  christos     memset((voidp) &sin, 0, sizeof(sin));
    260  1.1  christos     sin = *fs->fs_ip;
    261  1.1  christos     sin.sin_port = htons(PMAPPORT);
    262  1.1  christos     error = fwd_packet(RPC_XID_PORTMAP, iobuf, len,
    263  1.1  christos 		       &sin, &sin, (voidp) fs, got_portmap);
    264  1.1  christos   } else {
    265  1.1  christos     error = -len;
    266  1.1  christos   }
    267  1.1  christos 
    268  1.1  christos   return error;
    269  1.1  christos }
    270  1.1  christos 
    271  1.1  christos 
    272  1.1  christos static void
    273  1.1  christos recompute_portmap(fserver *fs)
    274  1.1  christos {
    275  1.1  christos   int error;
    276  1.1  christos   u_long mnt_version;
    277  1.1  christos 
    278  1.1  christos   /*
    279  1.1  christos    * No portmap calls for pure WebNFS servers.
    280  1.1  christos    */
    281  1.1  christos   if (fs->fs_flags & FSF_WEBNFS)
    282  1.1  christos     return;
    283  1.1  christos 
    284  1.1  christos   if (nfs_auth)
    285  1.1  christos     error = 0;
    286  1.1  christos   else
    287  1.1  christos     error = make_nfs_auth();
    288  1.1  christos 
    289  1.1  christos   if (error) {
    290  1.1  christos     nfs_private *np = (nfs_private *) fs->fs_private;
    291  1.1  christos     np->np_error = error;
    292  1.1  christos     return;
    293  1.1  christos   }
    294  1.1  christos 
    295  1.1  christos   if (fs->fs_version == 0)
    296  1.1  christos     plog(XLOG_WARNING, "recompute_portmap: nfs_version = 0 fixed");
    297  1.1  christos 
    298  1.1  christos   plog(XLOG_INFO, "recompute_portmap: NFS version %d on %s",
    299  1.1  christos        (int) fs->fs_version, fs->fs_host);
    300  1.1  christos #ifdef HAVE_FS_NFS3
    301  1.1  christos   if (fs->fs_version == NFS_VERSION3)
    302  1.1  christos     mnt_version = AM_MOUNTVERS3;
    303  1.1  christos   else
    304  1.1  christos #endif /* HAVE_FS_NFS3 */
    305  1.1  christos     mnt_version = MOUNTVERS;
    306  1.1  christos 
    307  1.1  christos   plog(XLOG_INFO, "Using MOUNT version: %d", (int) mnt_version);
    308  1.1  christos   call_portmap(fs, nfs_auth, MOUNTPROG, mnt_version, (u_long) IPPROTO_UDP);
    309  1.1  christos }
    310  1.1  christos 
    311  1.1  christos 
    312  1.1  christos int
    313  1.1  christos get_mountd_port(fserver *fs, u_short *port, wchan_t wchan)
    314  1.1  christos {
    315  1.1  christos   int error = -1;
    316  1.1  christos   if (FSRV_ISDOWN(fs))
    317  1.1  christos     return EWOULDBLOCK;
    318  1.1  christos 
    319  1.1  christos   if (FSRV_ISUP(fs)) {
    320  1.1  christos     nfs_private *np = (nfs_private *) fs->fs_private;
    321  1.1  christos     if (np->np_error == 0) {
    322  1.1  christos       *port = np->np_mountd;
    323  1.1  christos       error = 0;
    324  1.1  christos     } else {
    325  1.1  christos       error = np->np_error;
    326  1.1  christos     }
    327  1.1  christos     /*
    328  1.1  christos      * Now go get the port mapping again in case it changed.
    329  1.1  christos      * Note that it is used even if (np_mountd_inval)
    330  1.1  christos      * is True.  The flag is used simply as an
    331  1.1  christos      * indication that the mountd may be invalid, not
    332  1.1  christos      * that it is known to be invalid.
    333  1.1  christos      */
    334  1.1  christos     if (np->np_mountd_inval)
    335  1.1  christos       recompute_portmap(fs);
    336  1.1  christos     else
    337  1.1  christos       np->np_mountd_inval = TRUE;
    338  1.1  christos   }
    339  1.1  christos   if (error < 0 && wchan && !(fs->fs_flags & FSF_WANT)) {
    340  1.1  christos     /*
    341  1.1  christos      * If a wait channel is supplied, and no
    342  1.1  christos      * error has yet occurred, then arrange
    343  1.1  christos      * that a wakeup is done on the wait channel,
    344  1.1  christos      * whenever a wakeup is done on this fs node.
    345  1.1  christos      * Wakeup's are done on the fs node whenever
    346  1.1  christos      * it changes state - thus causing control to
    347  1.1  christos      * come back here and new, better things to happen.
    348  1.1  christos      */
    349  1.1  christos     fs->fs_flags |= FSF_WANT;
    350  1.1  christos     sched_task(wakeup_task, wchan, (wchan_t) fs);
    351  1.1  christos   }
    352  1.1  christos   return error;
    353  1.1  christos }
    354  1.1  christos 
    355  1.1  christos 
    356  1.1  christos /*
    357  1.1  christos  * This is called when we get a reply to an RPC ping.
    358  1.1  christos  * The value of id was taken from the nfs_private
    359  1.1  christos  * structure when the ping was transmitted.
    360  1.1  christos  */
    361  1.1  christos static void
    362  1.1  christos nfs_keepalive_callback(voidp pkt, int len, struct sockaddr_in *sp, struct sockaddr_in *tsp, voidp idv, int done)
    363  1.1  christos {
    364  1.1  christos   int xid = (long) idv;		/* cast needed for 64-bit archs */
    365  1.1  christos   fserver *fs;
    366  1.1  christos   int found_map = 0;
    367  1.1  christos 
    368  1.1  christos   if (!done)
    369  1.1  christos     return;
    370  1.1  christos 
    371  1.1  christos   /*
    372  1.1  christos    * For each node...
    373  1.1  christos    */
    374  1.1  christos   ITER(fs, fserver, &nfs_srvr_list) {
    375  1.1  christos     nfs_private *np = (nfs_private *) fs->fs_private;
    376  1.1  christos     if (np->np_xid == xid && (fs->fs_flags & FSF_PINGING)) {
    377  1.1  christos       /*
    378  1.1  christos        * Reset the ping counter.
    379  1.1  christos        * Update the keepalive timer.
    380  1.1  christos        * Log what happened.
    381  1.1  christos        */
    382  1.1  christos       if (fs->fs_flags & FSF_DOWN) {
    383  1.1  christos 	fs->fs_flags &= ~FSF_DOWN;
    384  1.1  christos 	if (fs->fs_flags & FSF_VALID) {
    385  1.1  christos 	  srvrlog(fs, "is up");
    386  1.1  christos 	} else {
    387  1.1  christos 	  if (np->np_ping > 1)
    388  1.1  christos 	    srvrlog(fs, "ok");
    389  1.1  christos 	  else
    390  1.1  christos 	    srvrlog(fs, "starts up");
    391  1.1  christos 	  fs->fs_flags |= FSF_VALID;
    392  1.1  christos 	}
    393  1.1  christos 
    394  1.1  christos 	map_flush_srvr(fs);
    395  1.1  christos       } else {
    396  1.1  christos 	if (fs->fs_flags & FSF_VALID) {
    397  1.1  christos 	  dlog("file server %s type nfs is still up", fs->fs_host);
    398  1.1  christos 	} else {
    399  1.1  christos 	  if (np->np_ping > 1)
    400  1.1  christos 	    srvrlog(fs, "ok");
    401  1.1  christos 	  fs->fs_flags |= FSF_VALID;
    402  1.1  christos 	}
    403  1.1  christos       }
    404  1.1  christos 
    405  1.1  christos       /*
    406  1.1  christos        * Adjust ping interval
    407  1.1  christos        */
    408  1.1  christos       untimeout(fs->fs_cid);
    409  1.1  christos       fs->fs_cid = timeout(fs->fs_pinger, nfs_keepalive, (voidp) fs);
    410  1.1  christos 
    411  1.1  christos       /*
    412  1.1  christos        * Update ttl for this server
    413  1.1  christos        */
    414  1.1  christos       np->np_ttl = clocktime(NULL) +
    415  1.1  christos 	(MAX_ALLOWED_PINGS - 1) * FAST_NFS_PING + fs->fs_pinger - 1;
    416  1.1  christos 
    417  1.1  christos       /*
    418  1.1  christos        * New RPC xid...
    419  1.1  christos        */
    420  1.1  christos       np->np_xid = XID_ALLOC();
    421  1.1  christos 
    422  1.1  christos       /*
    423  1.1  christos        * Failed pings is zero...
    424  1.1  christos        */
    425  1.1  christos       np->np_ping = 0;
    426  1.1  christos 
    427  1.1  christos       /*
    428  1.1  christos        * Recompute portmap information if not known
    429  1.1  christos        */
    430  1.1  christos       if (np->np_mountd_inval)
    431  1.1  christos 	recompute_portmap(fs);
    432  1.1  christos 
    433  1.1  christos       found_map++;
    434  1.1  christos       break;
    435  1.1  christos     }
    436  1.1  christos   }
    437  1.1  christos 
    438  1.1  christos   if (found_map == 0)
    439  1.1  christos     dlog("Spurious ping packet");
    440  1.1  christos }
    441  1.1  christos 
    442  1.1  christos 
    443  1.1  christos static void
    444  1.1  christos check_fs_addr_change(fserver *fs)
    445  1.1  christos {
    446  1.1  christos   struct hostent *hp = NULL;
    447  1.1  christos   struct in_addr ia;
    448  1.1  christos   char *old_ipaddr, *new_ipaddr;
    449  1.1  christos 
    450  1.1  christos   hp = gethostbyname(fs->fs_host);
    451  1.1  christos   if (!hp ||
    452  1.1  christos       hp->h_addrtype != AF_INET ||
    453  1.1  christos       !STREQ((char *) hp->h_name, fs->fs_host) ||
    454  1.1  christos       memcmp((voidp) &fs->fs_ip->sin_addr,
    455  1.1  christos 	     (voidp) hp->h_addr,
    456  1.1  christos 	     sizeof(fs->fs_ip->sin_addr)) == 0)
    457  1.1  christos     return;
    458  1.1  christos   /* if got here: downed server changed IP address */
    459  1.1  christos   old_ipaddr = strdup(inet_ntoa(fs->fs_ip->sin_addr));
    460  1.1  christos   memmove((voidp) &ia, (voidp) hp->h_addr, sizeof(struct in_addr));
    461  1.1  christos   new_ipaddr = inet_ntoa(ia);	/* ntoa uses static buf */
    462  1.1  christos   plog(XLOG_WARNING, "EZK: down fileserver %s changed ip: %s -> %s",
    463  1.1  christos        fs->fs_host, old_ipaddr, new_ipaddr);
    464  1.1  christos   XFREE(old_ipaddr);
    465  1.1  christos   /* copy new IP addr */
    466  1.1  christos   memmove((voidp) &fs->fs_ip->sin_addr,
    467  1.1  christos 	  (voidp) hp->h_addr,
    468  1.1  christos 	  sizeof(fs->fs_ip->sin_addr));
    469  1.1  christos   /* XXX: do we need to un/set these flags? */
    470  1.1  christos   fs->fs_flags &= ~FSF_DOWN;
    471  1.1  christos   fs->fs_flags |= FSF_VALID | FSF_WANT;
    472  1.1  christos   map_flush_srvr(fs);		/* XXX: a race with flush_srvr_nfs_cache? */
    473  1.1  christos   flush_srvr_nfs_cache(fs);
    474  1.1  christos   fs->fs_flags |= FSF_FORCE_UNMOUNT;
    475  1.1  christos 
    476  1.1  christos #if 0
    477  1.1  christos   flush_nfs_fhandle_cache(fs);	/* done in caller: nfs_keepalive_timeout */
    478  1.1  christos   /* XXX: need to purge nfs_private so that somehow it will get re-initialized? */
    479  1.1  christos #endif /* 0 */
    480  1.1  christos }
    481  1.1  christos 
    482  1.1  christos 
    483  1.1  christos /*
    484  1.1  christos  * Called when no ping-reply received
    485  1.1  christos  */
    486  1.1  christos static void
    487  1.1  christos nfs_keepalive_timeout(voidp v)
    488  1.1  christos {
    489  1.1  christos   fserver *fs = v;
    490  1.1  christos   nfs_private *np = (nfs_private *) fs->fs_private;
    491  1.1  christos 
    492  1.1  christos   /*
    493  1.1  christos    * Another ping has failed
    494  1.1  christos    */
    495  1.1  christos   np->np_ping++;
    496  1.1  christos   if (np->np_ping > 1)
    497  1.1  christos     srvrlog(fs, "not responding");
    498  1.1  christos 
    499  1.1  christos   /*
    500  1.1  christos    * Not known to be up any longer
    501  1.1  christos    */
    502  1.1  christos   if (FSRV_ISUP(fs))
    503  1.1  christos     fs->fs_flags &= ~FSF_VALID;
    504  1.1  christos 
    505  1.1  christos   /*
    506  1.1  christos    * If ttl has expired then guess that it is dead
    507  1.1  christos    */
    508  1.1  christos   if (np->np_ttl < clocktime(NULL)) {
    509  1.1  christos     int oflags = fs->fs_flags;
    510  1.1  christos     dlog("ttl has expired");
    511  1.1  christos     if ((fs->fs_flags & FSF_DOWN) == 0) {
    512  1.1  christos       /*
    513  1.1  christos        * Server was up, but is now down.
    514  1.1  christos        */
    515  1.1  christos       srvrlog(fs, "is down");
    516  1.1  christos       fs->fs_flags |= FSF_DOWN | FSF_VALID;
    517  1.1  christos       /*
    518  1.1  christos        * Since the server is down, the portmap
    519  1.1  christos        * information may now be wrong, so it
    520  1.1  christos        * must be flushed from the local cache
    521  1.1  christos        */
    522  1.1  christos       flush_nfs_fhandle_cache(fs);
    523  1.1  christos       np->np_error = -1;
    524  1.1  christos       check_fs_addr_change(fs); /* check if IP addr of fserver changed */
    525  1.1  christos     } else {
    526  1.1  christos       /*
    527  1.1  christos        * Known to be down
    528  1.1  christos        */
    529  1.1  christos       if ((fs->fs_flags & FSF_VALID) == 0)
    530  1.1  christos 	srvrlog(fs, "starts down");
    531  1.1  christos       fs->fs_flags |= FSF_VALID;
    532  1.1  christos     }
    533  1.1  christos     if (oflags != fs->fs_flags && (fs->fs_flags & FSF_WANT))
    534  1.1  christos       wakeup_srvr(fs);
    535  1.1  christos     /*
    536  1.1  christos      * Reset failed ping count
    537  1.1  christos      */
    538  1.1  christos     np->np_ping = 0;
    539  1.1  christos   } else {
    540  1.1  christos     if (np->np_ping > 1)
    541  1.1  christos       dlog("%d pings to %s failed - at most %d allowed", np->np_ping, fs->fs_host, MAX_ALLOWED_PINGS);
    542  1.1  christos   }
    543  1.1  christos 
    544  1.1  christos   /*
    545  1.1  christos    * New RPC xid, so any late responses to the previous ping
    546  1.1  christos    * get ignored...
    547  1.1  christos    */
    548  1.1  christos   np->np_xid = XID_ALLOC();
    549  1.1  christos 
    550  1.1  christos   /*
    551  1.1  christos    * Run keepalive again
    552  1.1  christos    */
    553  1.1  christos   nfs_keepalive(fs);
    554  1.1  christos }
    555  1.1  christos 
    556  1.1  christos 
    557  1.1  christos /*
    558  1.1  christos  * Keep track of whether a server is alive
    559  1.1  christos  */
    560  1.1  christos static void
    561  1.1  christos nfs_keepalive(voidp v)
    562  1.1  christos {
    563  1.1  christos   fserver *fs = v;
    564  1.1  christos   int error;
    565  1.1  christos   nfs_private *np = (nfs_private *) fs->fs_private;
    566  1.1  christos   int fstimeo = -1;
    567  1.1  christos 
    568  1.1  christos   /*
    569  1.1  christos    * Send an NFS ping to this node
    570  1.1  christos    */
    571  1.1  christos 
    572  1.1  christos   if (ping_len[fs->fs_version - NFS_VERSION] == 0)
    573  1.1  christos     create_ping_payload(fs->fs_version);
    574  1.1  christos 
    575  1.1  christos   /*
    576  1.1  christos    * Queue the packet...
    577  1.1  christos    */
    578  1.1  christos   error = fwd_packet(MK_RPC_XID(RPC_XID_NFSPING, np->np_xid),
    579  1.1  christos 		     ping_buf[fs->fs_version - NFS_VERSION],
    580  1.1  christos 		     ping_len[fs->fs_version - NFS_VERSION],
    581  1.1  christos 		     fs->fs_ip,
    582  1.1  christos 		     (struct sockaddr_in *) NULL,
    583  1.1  christos 		     (voidp) ((long) np->np_xid), /* cast needed for 64-bit archs */
    584  1.1  christos 		     nfs_keepalive_callback);
    585  1.1  christos 
    586  1.1  christos   /*
    587  1.1  christos    * See if a hard error occurred
    588  1.1  christos    */
    589  1.1  christos   switch (error) {
    590  1.1  christos   case ENETDOWN:
    591  1.1  christos   case ENETUNREACH:
    592  1.1  christos   case EHOSTDOWN:
    593  1.1  christos   case EHOSTUNREACH:
    594  1.1  christos     np->np_ping = MAX_ALLOWED_PINGS;	/* immediately down */
    595  1.1  christos     np->np_ttl = (time_t) 0;
    596  1.1  christos     /*
    597  1.1  christos      * This causes an immediate call to nfs_keepalive_timeout
    598  1.1  christos      * whenever the server was thought to be up.
    599  1.1  christos      * See +++ below.
    600  1.1  christos      */
    601  1.1  christos     fstimeo = 0;
    602  1.1  christos     break;
    603  1.1  christos 
    604  1.1  christos   case 0:
    605  1.1  christos     dlog("Sent NFS ping to %s", fs->fs_host);
    606  1.1  christos     break;
    607  1.1  christos   }
    608  1.1  christos 
    609  1.1  christos   /*
    610  1.1  christos    * Back off the ping interval if we are not getting replies and
    611  1.1  christos    * the remote system is known to be down.
    612  1.1  christos    */
    613  1.1  christos   switch (fs->fs_flags & (FSF_DOWN | FSF_VALID)) {
    614  1.1  christos   case FSF_VALID:		/* Up */
    615  1.1  christos     if (fstimeo < 0)		/* +++ see above */
    616  1.1  christos       fstimeo = FAST_NFS_PING;
    617  1.1  christos     break;
    618  1.1  christos 
    619  1.1  christos   case FSF_VALID | FSF_DOWN:	/* Down */
    620  1.1  christos     fstimeo = fs->fs_pinger;
    621  1.1  christos     break;
    622  1.1  christos 
    623  1.1  christos   default:			/* Unknown */
    624  1.1  christos     fstimeo = FAST_NFS_PING;
    625  1.1  christos     break;
    626  1.1  christos   }
    627  1.1  christos 
    628  1.1  christos   dlog("NFS timeout in %d seconds", fstimeo);
    629  1.1  christos 
    630  1.1  christos   fs->fs_cid = timeout(fstimeo, nfs_keepalive_timeout, (voidp) fs);
    631  1.1  christos }
    632  1.1  christos 
    633  1.1  christos 
    634  1.1  christos static void
    635  1.1  christos start_nfs_pings(fserver *fs, int pingval)
    636  1.1  christos {
    637  1.1  christos   if (pingval == 0)	    /* could be because ping mnt option not found */
    638  1.1  christos     pingval = AM_PINGER;
    639  1.1  christos   /* if pings haven't been initalized, then init them for first time */
    640  1.1  christos   if (fs->fs_flags & FSF_PING_UNINIT) {
    641  1.1  christos     fs->fs_flags &= ~FSF_PING_UNINIT;
    642  1.1  christos     plog(XLOG_INFO, "initializing %s's pinger to %d sec", fs->fs_host, pingval);
    643  1.1  christos     goto do_pings;
    644  1.1  christos   }
    645  1.1  christos 
    646  1.1  christos   if ((fs->fs_flags & FSF_PINGING)  &&  fs->fs_pinger == pingval) {
    647  1.1  christos     dlog("already running pings to %s", fs->fs_host);
    648  1.1  christos     return;
    649  1.1  christos   }
    650  1.1  christos 
    651  1.1  christos   /* if got here, then we need to update the ping value */
    652  1.1  christos   plog(XLOG_INFO, "changing %s's ping value from %d%s to %d%s",
    653  1.1  christos        fs->fs_host,
    654  1.1  christos        fs->fs_pinger, (fs->fs_pinger < 0 ? " (off)" : ""),
    655  1.1  christos        pingval, (pingval < 0 ? " (off)" : ""));
    656  1.1  christos  do_pings:
    657  1.1  christos   fs->fs_pinger = pingval;
    658  1.1  christos 
    659  1.1  christos   if (fs->fs_cid)
    660  1.1  christos     untimeout(fs->fs_cid);
    661  1.1  christos   if (pingval < 0) {
    662  1.1  christos     srvrlog(fs, "wired up (pings disabled)");
    663  1.1  christos     fs->fs_flags |= FSF_VALID;
    664  1.1  christos     fs->fs_flags &= ~FSF_DOWN;
    665  1.1  christos   } else {
    666  1.1  christos     fs->fs_flags |= FSF_PINGING;
    667  1.1  christos     nfs_keepalive(fs);
    668  1.1  christos   }
    669  1.1  christos }
    670  1.1  christos 
    671  1.1  christos 
    672  1.1  christos /*
    673  1.1  christos  * Find an nfs server for a host.
    674  1.1  christos  */
    675  1.1  christos fserver *
    676  1.1  christos find_nfs_srvr(mntfs *mf)
    677  1.1  christos {
    678  1.1  christos   char *host = mf->mf_fo->opt_rhost;
    679  1.1  christos   fserver *fs;
    680  1.1  christos   int pingval;
    681  1.1  christos   mntent_t mnt;
    682  1.1  christos   nfs_private *np;
    683  1.1  christos   struct hostent *hp = NULL;
    684  1.1  christos   struct sockaddr_in *ip = NULL;
    685  1.1  christos   u_long nfs_version = 0;	/* default is no version specified */
    686  1.1  christos   u_long best_nfs_version = 0;
    687  1.1  christos   char *nfs_proto = NULL;	/* no IP protocol either */
    688  1.1  christos   int nfs_port = 0;
    689  1.1  christos   int nfs_port_opt = 0;
    690  1.1  christos   int fserver_is_down = 0;
    691  1.1  christos 
    692  1.1  christos   /*
    693  1.1  christos    * Get ping interval from mount options.
    694  1.1  christos    * Current only used to decide whether pings
    695  1.1  christos    * are required or not.  < 0 = no pings.
    696  1.1  christos    */
    697  1.1  christos   mnt.mnt_opts = mf->mf_mopts;
    698  1.1  christos   pingval = hasmntval(&mnt, "ping");
    699  1.1  christos 
    700  1.1  christos   if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
    701  1.1  christos     /*
    702  1.1  christos      * the server granted us a filehandle, but we were unable to mount it.
    703  1.1  christos      * therefore, scale down to NFSv2/UDP and try again.
    704  1.1  christos      */
    705  1.1  christos     nfs_version = NFS_VERSION;
    706  1.1  christos     nfs_proto = "udp";
    707  1.1  christos     plog(XLOG_WARNING, "find_nfs_srvr: NFS mount failed, trying again with NFSv2/UDP");
    708  1.1  christos     mf->mf_flags &= ~MFF_NFS_SCALEDOWN;
    709  1.1  christos   } else {
    710  1.1  christos     /*
    711  1.1  christos      * Get the NFS version from the mount options. This is used
    712  1.1  christos      * to decide the highest NFS version to try.
    713  1.1  christos      */
    714  1.1  christos #ifdef MNTTAB_OPT_VERS
    715  1.1  christos     nfs_version = hasmntval(&mnt, MNTTAB_OPT_VERS);
    716  1.1  christos #endif /* MNTTAB_OPT_VERS */
    717  1.1  christos 
    718  1.1  christos #ifdef MNTTAB_OPT_PROTO
    719  1.1  christos     {
    720  1.1  christos       char *proto_opt = hasmnteq(&mnt, MNTTAB_OPT_PROTO);
    721  1.1  christos       if (proto_opt) {
    722  1.1  christos 	char **p;
    723  1.1  christos 	for (p = protocols; *p; p++)
    724  1.1  christos 	  if (NSTREQ(proto_opt, *p, strlen(*p))) {
    725  1.1  christos 	    nfs_proto = *p;
    726  1.1  christos 	    break;
    727  1.1  christos 	  }
    728  1.1  christos 	if (*p == NULL)
    729  1.1  christos 	  plog(XLOG_WARNING, "ignoring unknown protocol option for %s:%s",
    730  1.1  christos 	       host, mf->mf_fo->opt_rfs);
    731  1.1  christos       }
    732  1.1  christos     }
    733  1.1  christos #endif /* MNTTAB_OPT_PROTO */
    734  1.1  christos 
    735  1.1  christos #ifdef HAVE_NFS_NFSV2_H
    736  1.1  christos     /* allow overriding if nfsv2 option is specified in mount options */
    737  1.1  christos     if (amu_hasmntopt(&mnt, "nfsv2")) {
    738  1.1  christos       nfs_version = NFS_VERSION;/* nullify any ``vers=X'' statements */
    739  1.1  christos       nfs_proto = "udp";	/* nullify any ``proto=tcp'' statements */
    740  1.1  christos       plog(XLOG_WARNING, "found compatibility option \"nfsv2\": set options vers=2,proto=udp for host %s", host);
    741  1.1  christos     }
    742  1.1  christos #endif /* HAVE_NFS_NFSV2_H */
    743  1.1  christos 
    744  1.1  christos     /* check if we've globally overridden the NFS version/protocol */
    745  1.1  christos     if (gopt.nfs_vers) {
    746  1.1  christos       nfs_version = gopt.nfs_vers;
    747  1.1  christos       plog(XLOG_INFO, "find_nfs_srvr: force NFS version to %d",
    748  1.1  christos 	   (int) nfs_version);
    749  1.1  christos     }
    750  1.1  christos     if (gopt.nfs_proto) {
    751  1.1  christos       nfs_proto = gopt.nfs_proto;
    752  1.1  christos       plog(XLOG_INFO, "find_nfs_srvr: force NFS protocol transport to %s", nfs_proto);
    753  1.1  christos     }
    754  1.1  christos   }
    755  1.1  christos 
    756  1.1  christos   /*
    757  1.1  christos    * lookup host address and canonical name
    758  1.1  christos    */
    759  1.1  christos   hp = gethostbyname(host);
    760  1.1  christos 
    761  1.1  christos   /*
    762  1.1  christos    * New code from Bob Harris <harris (at) basil-rathbone.mit.edu>
    763  1.1  christos    * Use canonical name to keep track of file server
    764  1.1  christos    * information.  This way aliases do not generate
    765  1.1  christos    * multiple NFS pingers.  (Except when we're normalizing
    766  1.1  christos    * hosts.)
    767  1.1  christos    */
    768  1.1  christos   if (hp && !(gopt.flags & CFM_NORMALIZE_HOSTNAMES))
    769  1.1  christos     host = (char *) hp->h_name;
    770  1.1  christos 
    771  1.1  christos   if (hp) {
    772  1.1  christos     switch (hp->h_addrtype) {
    773  1.1  christos     case AF_INET:
    774  1.1  christos       ip = ALLOC(struct sockaddr_in);
    775  1.1  christos       memset((voidp) ip, 0, sizeof(*ip));
    776  1.1  christos       /* as per POSIX, sin_len need not be set (used internally by kernel) */
    777  1.1  christos       ip->sin_family = AF_INET;
    778  1.1  christos       memmove((voidp) &ip->sin_addr, (voidp) hp->h_addr, sizeof(ip->sin_addr));
    779  1.1  christos       break;
    780  1.1  christos 
    781  1.1  christos     default:
    782  1.1  christos       plog(XLOG_USER, "No IP address for host %s", host);
    783  1.1  christos       goto no_dns;
    784  1.1  christos     }
    785  1.1  christos   } else {
    786  1.1  christos     plog(XLOG_USER, "Unknown host: %s", host);
    787  1.1  christos     goto no_dns;
    788  1.1  christos   }
    789  1.1  christos 
    790  1.1  christos   /*
    791  1.1  christos    * This may not be the best way to do things, but it really doesn't make
    792  1.1  christos    * sense to query a file server which is marked as 'down' for any
    793  1.1  christos    * version/proto combination.
    794  1.1  christos    */
    795  1.1  christos   ITER(fs, fserver, &nfs_srvr_list) {
    796  1.1  christos     if (FSRV_ISDOWN(fs) &&
    797  1.1  christos 	STREQ(host, fs->fs_host)) {
    798  1.1  christos       plog(XLOG_WARNING, "fileserver %s is already hung - not running NFS proto/version discovery", host);
    799  1.1  christos       fs->fs_refc++;
    800  1.1  christos       if (ip)
    801  1.1  christos 	XFREE(ip);
    802  1.1  christos       return fs;
    803  1.1  christos     }
    804  1.1  christos   }
    805  1.1  christos 
    806  1.1  christos   /*
    807  1.1  christos    * Get the NFS Version, and verify server is up.
    808  1.1  christos    * If the client only supports NFSv2, hardcode it but still try to
    809  1.1  christos    * contact the remote portmapper to see if the service is running.
    810  1.1  christos    */
    811  1.1  christos #ifndef HAVE_FS_NFS3
    812  1.1  christos   nfs_version = NFS_VERSION;
    813  1.1  christos   nfs_proto = "udp";
    814  1.1  christos   plog(XLOG_INFO, "The client supports only NFS(2,udp)");
    815  1.1  christos #endif /* not HAVE_FS_NFS3 */
    816  1.1  christos 
    817  1.1  christos 
    818  1.1  christos   if (amu_hasmntopt(&mnt, MNTTAB_OPT_PUBLIC)) {
    819  1.1  christos     /*
    820  1.1  christos      * Use WebNFS to obtain file handles.
    821  1.1  christos      */
    822  1.1  christos     mf->mf_flags |= MFF_WEBNFS;
    823  1.1  christos     plog(XLOG_INFO, "%s option used, NOT contacting the portmapper on %s",
    824  1.1  christos 	 MNTTAB_OPT_PUBLIC, host);
    825  1.1  christos     /*
    826  1.1  christos      * Prefer NFSv3/tcp if the client supports it (cf. RFC 2054, 7).
    827  1.1  christos      */
    828  1.1  christos     if (!nfs_version) {
    829  1.1  christos #ifdef HAVE_FS_NFS3
    830  1.1  christos       nfs_version = NFS_VERSION3;
    831  1.1  christos #else /* not HAVE_FS_NFS3 */
    832  1.1  christos       nfs_version = NFS_VERSION;
    833  1.1  christos #endif /* not HAVE_FS_NFS3 */
    834  1.1  christos       plog(XLOG_INFO, "No NFS version specified, will use NFSv%d",
    835  1.1  christos 	   (int) nfs_version);
    836  1.1  christos     }
    837  1.1  christos     if (!nfs_proto) {
    838  1.1  christos #if defined(MNTTAB_OPT_PROTO) || defined(HAVE_FS_NFS3)
    839  1.1  christos       nfs_proto = "tcp";
    840  1.1  christos #else /* not defined(MNTTAB_OPT_PROTO) || defined(HAVE_FS_NFS3) */
    841  1.1  christos       nfs_proto = "udp";
    842  1.1  christos #endif /* not defined(MNTTAB_OPT_PROTO) || defined(HAVE_FS_NFS3) */
    843  1.1  christos       plog(XLOG_INFO, "No NFS protocol transport specified, will use %s",
    844  1.1  christos 	   nfs_proto);
    845  1.1  christos     }
    846  1.1  christos   } else {
    847  1.1  christos     /*
    848  1.1  christos      * Find the best combination of NFS version and protocol.
    849  1.1  christos      * When given a choice, use the highest available version,
    850  1.1  christos      * and use TCP over UDP if available.
    851  1.1  christos      */
    852  1.1  christos     if (check_pmap_up(host, ip)) {
    853  1.1  christos       if (nfs_proto) {
    854  1.1  christos 	best_nfs_version = get_nfs_version(host, ip, nfs_version, nfs_proto);
    855  1.1  christos 	nfs_port = ip->sin_port;
    856  1.1  christos       }
    857  1.1  christos #ifdef MNTTAB_OPT_PROTO
    858  1.1  christos       else {
    859  1.1  christos 	u_int proto_nfs_version;
    860  1.1  christos 	char **p;
    861  1.1  christos 
    862  1.1  christos 	for (p = protocols; *p; p++) {
    863  1.1  christos 	  proto_nfs_version = get_nfs_version(host, ip, nfs_version, *p);
    864  1.1  christos 
    865  1.1  christos 	  if (proto_nfs_version > best_nfs_version) {
    866  1.1  christos 	    best_nfs_version = proto_nfs_version;
    867  1.1  christos 	    nfs_proto = *p;
    868  1.1  christos 	    nfs_port = ip->sin_port;
    869  1.1  christos 	  }
    870  1.1  christos 	}
    871  1.1  christos       }
    872  1.1  christos #endif /* MNTTAB_OPT_PROTO */
    873  1.1  christos     } else {
    874  1.1  christos       plog(XLOG_INFO, "portmapper service not running on %s", host);
    875  1.1  christos     }
    876  1.1  christos 
    877  1.1  christos     /* use the portmapper results only nfs_version is not set yet */
    878  1.1  christos     if (!best_nfs_version) {
    879  1.1  christos       /*
    880  1.1  christos        * If the NFS server is down or does not support the portmapper call
    881  1.1  christos        * (such as certain Novell NFS servers) we mark it as version 2 and we
    882  1.1  christos        * let the nfs code deal with the case when it is down.  If/when the
    883  1.1  christos        * server comes back up and it can support NFSv3 and/or TCP, it will
    884  1.1  christos        * use those.
    885  1.1  christos        */
    886  1.1  christos       if (nfs_version == 0) {
    887  1.1  christos 	nfs_version = NFS_VERSION;
    888  1.1  christos 	nfs_proto = "udp";
    889  1.1  christos       }
    890  1.1  christos       plog(XLOG_INFO, "NFS service not running on %s", host);
    891  1.1  christos       fserver_is_down = 1;
    892  1.1  christos     } else {
    893  1.1  christos       if (nfs_version == 0)
    894  1.1  christos 	nfs_version = best_nfs_version;
    895  1.1  christos       plog(XLOG_INFO, "Using NFS version %d, protocol %s on host %s",
    896  1.1  christos 	   (int) nfs_version, nfs_proto, host);
    897  1.1  christos     }
    898  1.1  christos   }
    899  1.1  christos 
    900  1.1  christos   /*
    901  1.1  christos    * Determine the NFS port.
    902  1.1  christos    *
    903  1.1  christos    * A valid "port" mount option overrides anything else.
    904  1.1  christos    * If the port has been determined from the portmapper, use that.
    905  1.1  christos    * Default to NFS_PORT otherwise (cf. RFC 2054, 3).
    906  1.1  christos    */
    907  1.1  christos   nfs_port_opt = hasmntval(&mnt, MNTTAB_OPT_PORT);
    908  1.1  christos   if (nfs_port_opt > 0)
    909  1.1  christos     nfs_port = htons(nfs_port_opt);
    910  1.1  christos   if (!nfs_port)
    911  1.1  christos     nfs_port = htons(NFS_PORT);
    912  1.1  christos 
    913  1.1  christos   dlog("find_nfs_srvr: using port %d for nfs on %s",
    914  1.1  christos        (int) ntohs(nfs_port), host);
    915  1.1  christos   ip->sin_port = nfs_port;
    916  1.1  christos 
    917  1.1  christos no_dns:
    918  1.1  christos   /*
    919  1.1  christos    * Try to find an existing fs server structure for this host.
    920  1.1  christos    * Note that differing versions or protocols have their own structures.
    921  1.1  christos    * XXX: Need to fix the ping mechanism to actually use the NFS protocol
    922  1.1  christos    * chosen here (right now it always uses datagram sockets).
    923  1.1  christos    */
    924  1.1  christos   ITER(fs, fserver, &nfs_srvr_list) {
    925  1.1  christos     if (STREQ(host, fs->fs_host) &&
    926  1.1  christos  	nfs_version == fs->fs_version &&
    927  1.1  christos 	STREQ(nfs_proto, fs->fs_proto)) {
    928  1.1  christos       /*
    929  1.1  christos        * fill in the IP address -- this is only needed
    930  1.1  christos        * if there is a chance an IP address will change
    931  1.1  christos        * between mounts.
    932  1.1  christos        * Mike Mitchell, mcm (at) unx.sas.com, 09/08/93
    933  1.1  christos        */
    934  1.1  christos       if (hp && fs->fs_ip &&
    935  1.1  christos 	  memcmp((voidp) &fs->fs_ip->sin_addr,
    936  1.1  christos 		 (voidp) hp->h_addr,
    937  1.1  christos 		 sizeof(fs->fs_ip->sin_addr)) != 0) {
    938  1.1  christos 	struct in_addr ia;
    939  1.1  christos 	char *old_ipaddr, *new_ipaddr;
    940  1.1  christos 	old_ipaddr = strdup(inet_ntoa(fs->fs_ip->sin_addr));
    941  1.1  christos 	memmove((voidp) &ia, (voidp) hp->h_addr, sizeof(struct in_addr));
    942  1.1  christos 	new_ipaddr = inet_ntoa(ia);	/* ntoa uses static buf */
    943  1.1  christos 	plog(XLOG_WARNING, "fileserver %s changed ip: %s -> %s",
    944  1.1  christos 	     fs->fs_host, old_ipaddr, new_ipaddr);
    945  1.1  christos 	XFREE(old_ipaddr);
    946  1.1  christos 	flush_nfs_fhandle_cache(fs);
    947  1.1  christos 	memmove((voidp) &fs->fs_ip->sin_addr, (voidp) hp->h_addr, sizeof(fs->fs_ip->sin_addr));
    948  1.1  christos       }
    949  1.1  christos 
    950  1.1  christos       /*
    951  1.1  christos        * If the new file systems doesn't use WebNFS, the nfs pings may
    952  1.1  christos        * try to contact the portmapper.
    953  1.1  christos        */
    954  1.1  christos       if (!(mf->mf_flags & MFF_WEBNFS))
    955  1.1  christos 	fs->fs_flags &= ~FSF_WEBNFS;
    956  1.1  christos 
    957  1.1  christos       /* check if pingval needs to be updated/set/reset */
    958  1.1  christos       start_nfs_pings(fs, pingval);
    959  1.1  christos 
    960  1.1  christos       /*
    961  1.1  christos        * Following if statement from Mike Mitchell <mcm (at) unx.sas.com>
    962  1.1  christos        * Initialize the ping data if we aren't pinging now.  The np_ttl and
    963  1.1  christos        * np_ping fields are especially important.
    964  1.1  christos        */
    965  1.1  christos       if (!(fs->fs_flags & FSF_PINGING)) {
    966  1.1  christos 	np = (nfs_private *) fs->fs_private;
    967  1.1  christos 	np->np_mountd_inval = TRUE;
    968  1.1  christos 	np->np_xid = XID_ALLOC();
    969  1.1  christos 	np->np_error = -1;
    970  1.1  christos 	np->np_ping = 0;
    971  1.1  christos 	/*
    972  1.1  christos 	 * Initially the server will be deemed dead
    973  1.1  christos 	 * after MAX_ALLOWED_PINGS of the fast variety
    974  1.1  christos 	 * have failed.
    975  1.1  christos 	 */
    976  1.1  christos 	np->np_ttl = MAX_ALLOWED_PINGS * FAST_NFS_PING + clocktime(NULL) - 1;
    977  1.1  christos 	start_nfs_pings(fs, pingval);
    978  1.1  christos 	if (fserver_is_down)
    979  1.1  christos 	  fs->fs_flags |= FSF_VALID | FSF_DOWN;
    980  1.1  christos       }
    981  1.1  christos 
    982  1.1  christos       fs->fs_refc++;
    983  1.1  christos       if (ip)
    984  1.1  christos 	XFREE(ip);
    985  1.1  christos       return fs;
    986  1.1  christos     }
    987  1.1  christos   }
    988  1.1  christos 
    989  1.1  christos   /*
    990  1.1  christos    * Get here if we can't find an entry
    991  1.1  christos    */
    992  1.1  christos 
    993  1.1  christos   /*
    994  1.1  christos    * Allocate a new server
    995  1.1  christos    */
    996  1.1  christos   fs = ALLOC(struct fserver);
    997  1.1  christos   fs->fs_refc = 1;
    998  1.1  christos   fs->fs_host = strdup(hp ? hp->h_name : "unknown_hostname");
    999  1.1  christos   if (gopt.flags & CFM_NORMALIZE_HOSTNAMES)
   1000  1.1  christos     host_normalize(&fs->fs_host);
   1001  1.1  christos   fs->fs_ip = ip;
   1002  1.1  christos   fs->fs_cid = 0;
   1003  1.1  christos   if (ip) {
   1004  1.1  christos     fs->fs_flags = FSF_DOWN;	/* Starts off down */
   1005  1.1  christos   } else {
   1006  1.1  christos     fs->fs_flags = FSF_ERROR | FSF_VALID;
   1007  1.1  christos     mf->mf_flags |= MFF_ERROR;
   1008  1.1  christos     mf->mf_error = ENOENT;
   1009  1.1  christos   }
   1010  1.1  christos   if (mf->mf_flags & MFF_WEBNFS)
   1011  1.1  christos     fs->fs_flags |= FSF_WEBNFS;
   1012  1.1  christos   fs->fs_version = nfs_version;
   1013  1.1  christos   fs->fs_proto = nfs_proto;
   1014  1.1  christos   fs->fs_type = MNTTAB_TYPE_NFS;
   1015  1.1  christos   fs->fs_pinger = AM_PINGER;
   1016  1.1  christos   fs->fs_flags |= FSF_PING_UNINIT; /* pinger hasn't been initialized */
   1017  1.1  christos   np = ALLOC(struct nfs_private);
   1018  1.1  christos   memset((voidp) np, 0, sizeof(*np));
   1019  1.1  christos   np->np_mountd_inval = TRUE;
   1020  1.1  christos   np->np_xid = XID_ALLOC();
   1021  1.1  christos   np->np_error = -1;
   1022  1.1  christos 
   1023  1.1  christos   /*
   1024  1.1  christos    * Initially the server will be deemed dead after
   1025  1.1  christos    * MAX_ALLOWED_PINGS of the fast variety have failed.
   1026  1.1  christos    */
   1027  1.1  christos   np->np_ttl = clocktime(NULL) + MAX_ALLOWED_PINGS * FAST_NFS_PING - 1;
   1028  1.1  christos   fs->fs_private = (voidp) np;
   1029  1.1  christos   fs->fs_prfree = (void (*)(voidp)) free;
   1030  1.1  christos 
   1031  1.1  christos   if (!FSRV_ERROR(fs)) {
   1032  1.1  christos     /* start of keepalive timer, first updating pingval */
   1033  1.1  christos     start_nfs_pings(fs, pingval);
   1034  1.1  christos     if (fserver_is_down)
   1035  1.1  christos       fs->fs_flags |= FSF_VALID | FSF_DOWN;
   1036  1.1  christos   }
   1037  1.1  christos 
   1038  1.1  christos   /*
   1039  1.1  christos    * Add to list of servers
   1040  1.1  christos    */
   1041  1.1  christos   ins_que(&fs->fs_q, &nfs_srvr_list);
   1042  1.1  christos 
   1043  1.1  christos   return fs;
   1044  1.1  christos }
   1045