Home | History | Annotate | Line # | Download | only in amd
ops_nfs.c revision 1.1.1.1
      1  1.1  christos /*	$NetBSD: ops_nfs.c,v 1.1.1.1 2008/09/19 20:07:16 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/ops_nfs.c
     43  1.1  christos  *
     44  1.1  christos  */
     45  1.1  christos 
     46  1.1  christos /*
     47  1.1  christos  * Network file system
     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  * Convert from nfsstat to UN*X error code
     58  1.1  christos  */
     59  1.1  christos #define unx_error(e)	((int)(e))
     60  1.1  christos 
     61  1.1  christos /*
     62  1.1  christos  * FH_TTL is the time a file handle will remain in the cache since
     63  1.1  christos  * last being used.  If the file handle becomes invalid, then it
     64  1.1  christos  * will be flushed anyway.
     65  1.1  christos  */
     66  1.1  christos #define	FH_TTL			(5 * 60) /* five minutes */
     67  1.1  christos #define	FH_TTL_ERROR		(30) /* 30 seconds */
     68  1.1  christos #define	FHID_ALLOC()		(++fh_id)
     69  1.1  christos 
     70  1.1  christos /*
     71  1.1  christos  * The NFS layer maintains a cache of file handles.
     72  1.1  christos  * This is *fundamental* to the implementation and
     73  1.1  christos  * also allows quick remounting when a filesystem
     74  1.1  christos  * is accessed soon after timing out.
     75  1.1  christos  *
     76  1.1  christos  * The NFS server layer knows to flush this cache
     77  1.1  christos  * when a server goes down so avoiding stale handles.
     78  1.1  christos  *
     79  1.1  christos  * Each cache entry keeps a hard reference to
     80  1.1  christos  * the corresponding server.  This ensures that
     81  1.1  christos  * the server keepalive information is maintained.
     82  1.1  christos  *
     83  1.1  christos  * The copy of the sockaddr_in here is taken so
     84  1.1  christos  * that the port can be twiddled to talk to mountd
     85  1.1  christos  * instead of portmap or the NFS server as used
     86  1.1  christos  * elsewhere.
     87  1.1  christos  * The port# is flushed if a server goes down.
     88  1.1  christos  * The IP address is never flushed - we assume
     89  1.1  christos  * that the address of a mounted machine never
     90  1.1  christos  * changes.  If it does, then you have other
     91  1.1  christos  * problems...
     92  1.1  christos  */
     93  1.1  christos typedef struct fh_cache fh_cache;
     94  1.1  christos struct fh_cache {
     95  1.1  christos   qelem			fh_q;		/* List header */
     96  1.1  christos   wchan_t		fh_wchan;	/* Wait channel */
     97  1.1  christos   int			fh_error;	/* Valid data? */
     98  1.1  christos   int			fh_id;		/* Unique id */
     99  1.1  christos   int			fh_cid;		/* Callout id */
    100  1.1  christos   u_long		fh_nfs_version;	/* highest NFS version on host */
    101  1.1  christos   am_nfs_handle_t	fh_nfs_handle;	/* Handle on filesystem */
    102  1.1  christos   int			fh_status;	/* Status of last rpc */
    103  1.1  christos   struct sockaddr_in	fh_sin;		/* Address of mountd */
    104  1.1  christos   fserver		*fh_fs;		/* Server holding filesystem */
    105  1.1  christos   char			*fh_path;	/* Filesystem on host */
    106  1.1  christos };
    107  1.1  christos 
    108  1.1  christos /* forward definitions */
    109  1.1  christos static int nfs_init(mntfs *mf);
    110  1.1  christos static char *nfs_match(am_opts *fo);
    111  1.1  christos static int nfs_mount(am_node *am, mntfs *mf);
    112  1.1  christos static int nfs_umount(am_node *am, mntfs *mf);
    113  1.1  christos static void nfs_umounted(mntfs *mf);
    114  1.1  christos static int call_mountd(fh_cache *fp, u_long proc, fwd_fun f, wchan_t wchan);
    115  1.1  christos static int webnfs_lookup(fh_cache *fp, fwd_fun f, wchan_t wchan);
    116  1.1  christos static int fh_id = 0;
    117  1.1  christos 
    118  1.1  christos /* globals */
    119  1.1  christos AUTH *nfs_auth;
    120  1.1  christos qelem fh_head = {&fh_head, &fh_head};
    121  1.1  christos 
    122  1.1  christos /*
    123  1.1  christos  * Network file system operations
    124  1.1  christos  */
    125  1.1  christos am_ops nfs_ops =
    126  1.1  christos {
    127  1.1  christos   "nfs",
    128  1.1  christos   nfs_match,
    129  1.1  christos   nfs_init,
    130  1.1  christos   nfs_mount,
    131  1.1  christos   nfs_umount,
    132  1.1  christos   amfs_error_lookup_child,
    133  1.1  christos   amfs_error_mount_child,
    134  1.1  christos   amfs_error_readdir,
    135  1.1  christos   0,				/* nfs_readlink */
    136  1.1  christos   0,				/* nfs_mounted */
    137  1.1  christos   nfs_umounted,
    138  1.1  christos   find_nfs_srvr,
    139  1.1  christos   0,				/* nfs_get_wchan */
    140  1.1  christos   FS_MKMNT | FS_BACKGROUND | FS_AMQINFO,	/* nfs_fs_flags */
    141  1.1  christos #ifdef HAVE_FS_AUTOFS
    142  1.1  christos   AUTOFS_NFS_FS_FLAGS,
    143  1.1  christos #endif /* HAVE_FS_AUTOFS */
    144  1.1  christos };
    145  1.1  christos 
    146  1.1  christos 
    147  1.1  christos static fh_cache *
    148  1.1  christos find_nfs_fhandle_cache(opaque_t arg, int done)
    149  1.1  christos {
    150  1.1  christos   fh_cache *fp, *fp2 = NULL;
    151  1.1  christos   int id = (long) arg;		/* for 64-bit archs */
    152  1.1  christos 
    153  1.1  christos   ITER(fp, fh_cache, &fh_head) {
    154  1.1  christos     if (fp->fh_id == id) {
    155  1.1  christos       fp2 = fp;
    156  1.1  christos       break;
    157  1.1  christos     }
    158  1.1  christos   }
    159  1.1  christos 
    160  1.1  christos   if (fp2) {
    161  1.1  christos     dlog("fh cache gives fp %#lx, fs %s", (unsigned long) fp2, fp2->fh_path);
    162  1.1  christos   } else {
    163  1.1  christos     dlog("fh cache search failed");
    164  1.1  christos   }
    165  1.1  christos 
    166  1.1  christos   if (fp2 && !done) {
    167  1.1  christos     fp2->fh_error = ETIMEDOUT;
    168  1.1  christos     return 0;
    169  1.1  christos   }
    170  1.1  christos 
    171  1.1  christos   return fp2;
    172  1.1  christos }
    173  1.1  christos 
    174  1.1  christos 
    175  1.1  christos /*
    176  1.1  christos  * Called when a filehandle appears via the mount protocol
    177  1.1  christos  */
    178  1.1  christos static void
    179  1.1  christos got_nfs_fh_mount(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, opaque_t arg, int done)
    180  1.1  christos {
    181  1.1  christos   fh_cache *fp;
    182  1.1  christos   struct fhstatus res;
    183  1.1  christos #ifdef HAVE_FS_NFS3
    184  1.1  christos   struct am_mountres3 res3;
    185  1.1  christos #endif /* HAVE_FS_NFS3 */
    186  1.1  christos 
    187  1.1  christos   fp = find_nfs_fhandle_cache(arg, done);
    188  1.1  christos   if (!fp)
    189  1.1  christos     return;
    190  1.1  christos 
    191  1.1  christos   /*
    192  1.1  christos    * retrieve the correct RPC reply for the file handle, based on the
    193  1.1  christos    * NFS protocol version.
    194  1.1  christos    */
    195  1.1  christos #ifdef HAVE_FS_NFS3
    196  1.1  christos   if (fp->fh_nfs_version == NFS_VERSION3) {
    197  1.1  christos     memset(&res3, 0, sizeof(res3));
    198  1.1  christos     fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res3,
    199  1.1  christos 				    (XDRPROC_T_TYPE) xdr_am_mountres3);
    200  1.1  christos     fp->fh_status = unx_error(res3.fhs_status);
    201  1.1  christos     memset(&fp->fh_nfs_handle.v3, 0, sizeof(am_nfs_fh3));
    202  1.1  christos     fp->fh_nfs_handle.v3.am_fh3_length = res3.mountres3_u.mountinfo.fhandle.fhandle3_len;
    203  1.1  christos     memmove(fp->fh_nfs_handle.v3.am_fh3_data,
    204  1.1  christos 	    res3.mountres3_u.mountinfo.fhandle.fhandle3_val,
    205  1.1  christos 	    fp->fh_nfs_handle.v3.am_fh3_length);
    206  1.1  christos   } else {
    207  1.1  christos #endif /* HAVE_FS_NFS3 */
    208  1.1  christos     memset(&res, 0, sizeof(res));
    209  1.1  christos     fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res,
    210  1.1  christos 				    (XDRPROC_T_TYPE) xdr_fhstatus);
    211  1.1  christos     fp->fh_status = unx_error(res.fhs_status);
    212  1.1  christos     memmove(&fp->fh_nfs_handle.v2, &res.fhs_fh, NFS_FHSIZE);
    213  1.1  christos #ifdef HAVE_FS_NFS3
    214  1.1  christos   }
    215  1.1  christos #endif /* HAVE_FS_NFS3 */
    216  1.1  christos 
    217  1.1  christos   if (!fp->fh_error) {
    218  1.1  christos     dlog("got filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
    219  1.1  christos   } else {
    220  1.1  christos     plog(XLOG_USER, "filehandle denied for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
    221  1.1  christos     /*
    222  1.1  christos      * Force the error to be EACCES. It's debatable whether it should be
    223  1.1  christos      * ENOENT instead, but the server really doesn't give us any clues, and
    224  1.1  christos      * EACCES is more in line with the "filehandle denied" message.
    225  1.1  christos      */
    226  1.1  christos     fp->fh_error = EACCES;
    227  1.1  christos   }
    228  1.1  christos 
    229  1.1  christos   /*
    230  1.1  christos    * Wakeup anything sleeping on this filehandle
    231  1.1  christos    */
    232  1.1  christos   if (fp->fh_wchan) {
    233  1.1  christos     dlog("Calling wakeup on %#lx", (unsigned long) fp->fh_wchan);
    234  1.1  christos     wakeup(fp->fh_wchan);
    235  1.1  christos   }
    236  1.1  christos }
    237  1.1  christos 
    238  1.1  christos 
    239  1.1  christos /*
    240  1.1  christos  * Called when a filehandle appears via WebNFS
    241  1.1  christos  */
    242  1.1  christos static void
    243  1.1  christos got_nfs_fh_webnfs(voidp pkt, int len, struct sockaddr_in *sa, struct sockaddr_in *ia, opaque_t arg, int done)
    244  1.1  christos {
    245  1.1  christos   fh_cache *fp;
    246  1.1  christos   nfsdiropres res;
    247  1.1  christos #ifdef HAVE_FS_NFS3
    248  1.1  christos   am_LOOKUP3res res3;
    249  1.1  christos #endif /* HAVE_FS_NFS3 */
    250  1.1  christos 
    251  1.1  christos   fp = find_nfs_fhandle_cache(arg, done);
    252  1.1  christos   if (!fp)
    253  1.1  christos     return;
    254  1.1  christos 
    255  1.1  christos   /*
    256  1.1  christos    * retrieve the correct RPC reply for the file handle, based on the
    257  1.1  christos    * NFS protocol version.
    258  1.1  christos    */
    259  1.1  christos #ifdef HAVE_FS_NFS3
    260  1.1  christos   if (fp->fh_nfs_version == NFS_VERSION3) {
    261  1.1  christos     memset(&res3, 0, sizeof(res3));
    262  1.1  christos     fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res3,
    263  1.1  christos 				    (XDRPROC_T_TYPE) xdr_am_LOOKUP3res);
    264  1.1  christos     fp->fh_status = unx_error(res3.status);
    265  1.1  christos     memset(&fp->fh_nfs_handle.v3, 0, sizeof(am_nfs_fh3));
    266  1.1  christos     fp->fh_nfs_handle.v3.am_fh3_length = res3.res_u.ok.object.am_fh3_length;
    267  1.1  christos     memmove(fp->fh_nfs_handle.v3.am_fh3_data,
    268  1.1  christos 	    res3.res_u.ok.object.am_fh3_data,
    269  1.1  christos 	    fp->fh_nfs_handle.v3.am_fh3_length);
    270  1.1  christos   } else {
    271  1.1  christos #endif /* HAVE_FS_NFS3 */
    272  1.1  christos     memset(&res, 0, sizeof(res));
    273  1.1  christos     fp->fh_error = pickup_rpc_reply(pkt, len, (voidp) &res,
    274  1.1  christos 				    (XDRPROC_T_TYPE) xdr_diropres);
    275  1.1  christos     fp->fh_status = unx_error(res.dr_status);
    276  1.1  christos     memmove(&fp->fh_nfs_handle.v2, &res.dr_u.dr_drok_u.drok_fhandle, NFS_FHSIZE);
    277  1.1  christos #ifdef HAVE_FS_NFS3
    278  1.1  christos   }
    279  1.1  christos #endif /* HAVE_FS_NFS3 */
    280  1.1  christos 
    281  1.1  christos   if (!fp->fh_error) {
    282  1.1  christos     dlog("got filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
    283  1.1  christos   } else {
    284  1.1  christos     plog(XLOG_USER, "filehandle denied for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
    285  1.1  christos     /*
    286  1.1  christos      * Force the error to be EACCES. It's debatable whether it should be
    287  1.1  christos      * ENOENT instead, but the server really doesn't give us any clues, and
    288  1.1  christos      * EACCES is more in line with the "filehandle denied" message.
    289  1.1  christos      */
    290  1.1  christos     fp->fh_error = EACCES;
    291  1.1  christos   }
    292  1.1  christos 
    293  1.1  christos   /*
    294  1.1  christos    * Wakeup anything sleeping on this filehandle
    295  1.1  christos    */
    296  1.1  christos   if (fp->fh_wchan) {
    297  1.1  christos     dlog("Calling wakeup on %#lx", (unsigned long) fp->fh_wchan);
    298  1.1  christos     wakeup(fp->fh_wchan);
    299  1.1  christos   }
    300  1.1  christos }
    301  1.1  christos 
    302  1.1  christos 
    303  1.1  christos void
    304  1.1  christos flush_nfs_fhandle_cache(fserver *fs)
    305  1.1  christos {
    306  1.1  christos   fh_cache *fp;
    307  1.1  christos 
    308  1.1  christos   ITER(fp, fh_cache, &fh_head) {
    309  1.1  christos     if (fp->fh_fs == fs || fs == NULL) {
    310  1.1  christos       /*
    311  1.1  christos        * Only invalidate port info for non-WebNFS servers
    312  1.1  christos        */
    313  1.1  christos       if (!(fp->fh_fs->fs_flags & FSF_WEBNFS))
    314  1.1  christos 	fp->fh_sin.sin_port = (u_short) 0;
    315  1.1  christos       fp->fh_error = -1;
    316  1.1  christos     }
    317  1.1  christos   }
    318  1.1  christos }
    319  1.1  christos 
    320  1.1  christos 
    321  1.1  christos static void
    322  1.1  christos discard_fh(opaque_t arg)
    323  1.1  christos {
    324  1.1  christos   fh_cache *fp = (fh_cache *) arg;
    325  1.1  christos 
    326  1.1  christos   rem_que(&fp->fh_q);
    327  1.1  christos   if (fp->fh_fs) {
    328  1.1  christos     dlog("Discarding filehandle for %s:%s", fp->fh_fs->fs_host, fp->fh_path);
    329  1.1  christos     free_srvr(fp->fh_fs);
    330  1.1  christos   }
    331  1.1  christos   if (fp->fh_path)
    332  1.1  christos     XFREE(fp->fh_path);
    333  1.1  christos   XFREE(fp);
    334  1.1  christos }
    335  1.1  christos 
    336  1.1  christos 
    337  1.1  christos /*
    338  1.1  christos  * Determine the file handle for a node
    339  1.1  christos  */
    340  1.1  christos static int
    341  1.1  christos prime_nfs_fhandle_cache(char *path, fserver *fs, am_nfs_handle_t *fhbuf, mntfs *mf)
    342  1.1  christos {
    343  1.1  christos   fh_cache *fp, *fp_save = NULL;
    344  1.1  christos   int error;
    345  1.1  christos   int reuse_id = FALSE;
    346  1.1  christos 
    347  1.1  christos   dlog("Searching cache for %s:%s", fs->fs_host, path);
    348  1.1  christos 
    349  1.1  christos   /*
    350  1.1  christos    * First search the cache
    351  1.1  christos    */
    352  1.1  christos   ITER(fp, fh_cache, &fh_head) {
    353  1.1  christos     if (fs != fp->fh_fs  ||  !STREQ(path, fp->fh_path))
    354  1.1  christos       continue;			/* skip to next ITER item */
    355  1.1  christos     /* else we got a match */
    356  1.1  christos     switch (fp->fh_error) {
    357  1.1  christos     case 0:
    358  1.1  christos       plog(XLOG_INFO, "prime_nfs_fhandle_cache: NFS version %d", (int) fp->fh_nfs_version);
    359  1.1  christos 
    360  1.1  christos       error = fp->fh_error = fp->fh_status;
    361  1.1  christos 
    362  1.1  christos       if (error == 0) {
    363  1.1  christos 	if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
    364  1.1  christos 	  fp_save = fp;
    365  1.1  christos 	  /* XXX: why reuse the ID? */
    366  1.1  christos 	  reuse_id = TRUE;
    367  1.1  christos 	  break;
    368  1.1  christos 	}
    369  1.1  christos 
    370  1.1  christos 	if (fhbuf) {
    371  1.1  christos #ifdef HAVE_FS_NFS3
    372  1.1  christos 	  if (fp->fh_nfs_version == NFS_VERSION3) {
    373  1.1  christos 	    memmove((voidp) &(fhbuf->v3), (voidp) &(fp->fh_nfs_handle.v3),
    374  1.1  christos 		    sizeof(fp->fh_nfs_handle.v3));
    375  1.1  christos 	  } else
    376  1.1  christos #endif /* HAVE_FS_NFS3 */
    377  1.1  christos 	    {
    378  1.1  christos 	      memmove((voidp) &(fhbuf->v2), (voidp) &(fp->fh_nfs_handle.v2),
    379  1.1  christos 		      sizeof(fp->fh_nfs_handle.v2));
    380  1.1  christos 	    }
    381  1.1  christos 	}
    382  1.1  christos 	if (fp->fh_cid)
    383  1.1  christos 	  untimeout(fp->fh_cid);
    384  1.1  christos 	fp->fh_cid = timeout(FH_TTL, discard_fh, (opaque_t) fp);
    385  1.1  christos       } else if (error == EACCES) {
    386  1.1  christos 	/*
    387  1.1  christos 	 * Now decode the file handle return code.
    388  1.1  christos 	 */
    389  1.1  christos 	plog(XLOG_INFO, "Filehandle denied for \"%s:%s\"",
    390  1.1  christos 	     fs->fs_host, path);
    391  1.1  christos       } else {
    392  1.1  christos 	errno = error;	/* XXX */
    393  1.1  christos 	plog(XLOG_INFO, "Filehandle error for \"%s:%s\": %m",
    394  1.1  christos 	     fs->fs_host, path);
    395  1.1  christos       }
    396  1.1  christos 
    397  1.1  christos       /*
    398  1.1  christos        * The error was returned from the remote mount daemon.
    399  1.1  christos        * Policy: this error will be cached for now...
    400  1.1  christos        */
    401  1.1  christos       return error;
    402  1.1  christos 
    403  1.1  christos     case -1:
    404  1.1  christos       /*
    405  1.1  christos        * Still thinking about it, but we can re-use.
    406  1.1  christos        */
    407  1.1  christos       fp_save = fp;
    408  1.1  christos       reuse_id = TRUE;
    409  1.1  christos       break;
    410  1.1  christos 
    411  1.1  christos     default:
    412  1.1  christos       /*
    413  1.1  christos        * Return the error.
    414  1.1  christos        * Policy: make sure we recompute if required again
    415  1.1  christos        * in case this was caused by a network failure.
    416  1.1  christos        * This can thrash mountd's though...  If you find
    417  1.1  christos        * your mountd going slowly then:
    418  1.1  christos        * 1.  Add a fork() loop to main.
    419  1.1  christos        * 2.  Remove the call to innetgr() and don't use
    420  1.1  christos        *     netgroups, especially if you don't use YP.
    421  1.1  christos        */
    422  1.1  christos       error = fp->fh_error;
    423  1.1  christos       fp->fh_error = -1;
    424  1.1  christos       return error;
    425  1.1  christos     }	/* end of switch statement */
    426  1.1  christos   } /* end of ITER loop */
    427  1.1  christos 
    428  1.1  christos   /*
    429  1.1  christos    * Not in cache
    430  1.1  christos    */
    431  1.1  christos   if (fp_save) {
    432  1.1  christos     fp = fp_save;
    433  1.1  christos     /*
    434  1.1  christos      * Re-use existing slot
    435  1.1  christos      */
    436  1.1  christos     untimeout(fp->fh_cid);
    437  1.1  christos     free_srvr(fp->fh_fs);
    438  1.1  christos     XFREE(fp->fh_path);
    439  1.1  christos   } else {
    440  1.1  christos     fp = ALLOC(struct fh_cache);
    441  1.1  christos     memset((voidp) fp, 0, sizeof(struct fh_cache));
    442  1.1  christos     ins_que(&fp->fh_q, &fh_head);
    443  1.1  christos   }
    444  1.1  christos   if (!reuse_id)
    445  1.1  christos     fp->fh_id = FHID_ALLOC();
    446  1.1  christos   fp->fh_wchan = get_mntfs_wchan(mf);
    447  1.1  christos   fp->fh_error = -1;
    448  1.1  christos   fp->fh_cid = timeout(FH_TTL, discard_fh, (opaque_t) fp);
    449  1.1  christos 
    450  1.1  christos   /*
    451  1.1  christos    * If fs->fs_ip is null, remote server is probably down.
    452  1.1  christos    */
    453  1.1  christos   if (!fs->fs_ip) {
    454  1.1  christos     /* Mark the fileserver down and invalid again */
    455  1.1  christos     fs->fs_flags &= ~FSF_VALID;
    456  1.1  christos     fs->fs_flags |= FSF_DOWN;
    457  1.1  christos     error = AM_ERRNO_HOST_DOWN;
    458  1.1  christos     return error;
    459  1.1  christos   }
    460  1.1  christos 
    461  1.1  christos   /*
    462  1.1  christos    * Either fp has been freshly allocated or the address has changed.
    463  1.1  christos    * Initialize address and nfs version.  Don't try to re-use the port
    464  1.1  christos    * information unless using WebNFS where the port is fixed either by
    465  1.1  christos    * the spec or the "port" mount option.
    466  1.1  christos    */
    467  1.1  christos   if (fp->fh_sin.sin_addr.s_addr != fs->fs_ip->sin_addr.s_addr) {
    468  1.1  christos     fp->fh_sin = *fs->fs_ip;
    469  1.1  christos     if (!(mf->mf_flags & MFF_WEBNFS))
    470  1.1  christos 	fp->fh_sin.sin_port = 0;
    471  1.1  christos     fp->fh_nfs_version = fs->fs_version;
    472  1.1  christos   }
    473  1.1  christos 
    474  1.1  christos   fp->fh_fs = dup_srvr(fs);
    475  1.1  christos   fp->fh_path = strdup(path);
    476  1.1  christos 
    477  1.1  christos   if (mf->mf_flags & MFF_WEBNFS)
    478  1.1  christos     error = webnfs_lookup(fp, got_nfs_fh_webnfs, get_mntfs_wchan(mf));
    479  1.1  christos   else
    480  1.1  christos     error = call_mountd(fp, MOUNTPROC_MNT, got_nfs_fh_mount, get_mntfs_wchan(mf));
    481  1.1  christos   if (error) {
    482  1.1  christos     /*
    483  1.1  christos      * Local error - cache for a short period
    484  1.1  christos      * just to prevent thrashing.
    485  1.1  christos      */
    486  1.1  christos     untimeout(fp->fh_cid);
    487  1.1  christos     fp->fh_cid = timeout(error < 0 ? 2 * ALLOWED_MOUNT_TIME : FH_TTL_ERROR,
    488  1.1  christos 			 discard_fh, (opaque_t) fp);
    489  1.1  christos     fp->fh_error = error;
    490  1.1  christos   } else {
    491  1.1  christos     error = fp->fh_error;
    492  1.1  christos   }
    493  1.1  christos 
    494  1.1  christos   return error;
    495  1.1  christos }
    496  1.1  christos 
    497  1.1  christos 
    498  1.1  christos int
    499  1.1  christos make_nfs_auth(void)
    500  1.1  christos {
    501  1.1  christos   AUTH_CREATE_GIDLIST_TYPE group_wheel = 0;
    502  1.1  christos 
    503  1.1  christos   /* Some NFS mounts (particularly cross-domain) require FQDNs to succeed */
    504  1.1  christos 
    505  1.1  christos #ifdef HAVE_TRANSPORT_TYPE_TLI
    506  1.1  christos   if (gopt.flags & CFM_FULLY_QUALIFIED_HOSTS) {
    507  1.1  christos     plog(XLOG_INFO, "Using NFS auth for FQHN \"%s\"", hostd);
    508  1.1  christos     nfs_auth = authsys_create(hostd, 0, 0, 1, &group_wheel);
    509  1.1  christos   } else {
    510  1.1  christos     nfs_auth = authsys_create_default();
    511  1.1  christos   }
    512  1.1  christos #else /* not HAVE_TRANSPORT_TYPE_TLI */
    513  1.1  christos   if (gopt.flags & CFM_FULLY_QUALIFIED_HOSTS) {
    514  1.1  christos     plog(XLOG_INFO, "Using NFS auth for FQHN \"%s\"", hostd);
    515  1.1  christos     nfs_auth = authunix_create(hostd, 0, 0, 1, &group_wheel);
    516  1.1  christos   } else {
    517  1.1  christos     nfs_auth = authunix_create_default();
    518  1.1  christos   }
    519  1.1  christos #endif /* not HAVE_TRANSPORT_TYPE_TLI */
    520  1.1  christos 
    521  1.1  christos   if (!nfs_auth)
    522  1.1  christos     return ENOBUFS;
    523  1.1  christos 
    524  1.1  christos   return 0;
    525  1.1  christos }
    526  1.1  christos 
    527  1.1  christos 
    528  1.1  christos static int
    529  1.1  christos call_mountd(fh_cache *fp, u_long proc, fwd_fun fun, wchan_t wchan)
    530  1.1  christos {
    531  1.1  christos   struct rpc_msg mnt_msg;
    532  1.1  christos   int len;
    533  1.1  christos   char iobuf[UDPMSGSIZE];
    534  1.1  christos   int error;
    535  1.1  christos   u_long mnt_version;
    536  1.1  christos 
    537  1.1  christos   if (!nfs_auth) {
    538  1.1  christos     error = make_nfs_auth();
    539  1.1  christos     if (error)
    540  1.1  christos       return error;
    541  1.1  christos   }
    542  1.1  christos 
    543  1.1  christos   if (fp->fh_sin.sin_port == 0) {
    544  1.1  christos     u_short mountd_port;
    545  1.1  christos     error = get_mountd_port(fp->fh_fs, &mountd_port, wchan);
    546  1.1  christos     if (error)
    547  1.1  christos       return error;
    548  1.1  christos     fp->fh_sin.sin_port = mountd_port;
    549  1.1  christos   }
    550  1.1  christos 
    551  1.1  christos   /* find the right version of the mount protocol */
    552  1.1  christos #ifdef HAVE_FS_NFS3
    553  1.1  christos   if (fp->fh_nfs_version == NFS_VERSION3)
    554  1.1  christos     mnt_version = AM_MOUNTVERS3;
    555  1.1  christos   else
    556  1.1  christos #endif /* HAVE_FS_NFS3 */
    557  1.1  christos     mnt_version = MOUNTVERS;
    558  1.1  christos   plog(XLOG_INFO, "call_mountd: NFS version %d, mount version %d",
    559  1.1  christos        (int) fp->fh_nfs_version, (int) mnt_version);
    560  1.1  christos 
    561  1.1  christos   rpc_msg_init(&mnt_msg, MOUNTPROG, mnt_version, MOUNTPROC_NULL);
    562  1.1  christos   len = make_rpc_packet(iobuf,
    563  1.1  christos 			sizeof(iobuf),
    564  1.1  christos 			proc,
    565  1.1  christos 			&mnt_msg,
    566  1.1  christos 			(voidp) &fp->fh_path,
    567  1.1  christos 			(XDRPROC_T_TYPE) xdr_nfspath,
    568  1.1  christos 			nfs_auth);
    569  1.1  christos 
    570  1.1  christos   if (len > 0) {
    571  1.1  christos     error = fwd_packet(MK_RPC_XID(RPC_XID_MOUNTD, fp->fh_id),
    572  1.1  christos 		       iobuf,
    573  1.1  christos 		       len,
    574  1.1  christos 		       &fp->fh_sin,
    575  1.1  christos 		       &fp->fh_sin,
    576  1.1  christos 		       (opaque_t) ((long) fp->fh_id), /* cast to long needed for 64-bit archs */
    577  1.1  christos 		       fun);
    578  1.1  christos   } else {
    579  1.1  christos     error = -len;
    580  1.1  christos   }
    581  1.1  christos 
    582  1.1  christos   /*
    583  1.1  christos    * It may be the case that we're sending to the wrong MOUNTD port.  This
    584  1.1  christos    * occurs if mountd is restarted on the server after the port has been
    585  1.1  christos    * looked up and stored in the filehandle cache somewhere.  The correct
    586  1.1  christos    * solution, if we're going to cache port numbers is to catch the ICMP
    587  1.1  christos    * port unreachable reply from the server and cause the portmap request
    588  1.1  christos    * to be redone.  The quick solution here is to invalidate the MOUNTD
    589  1.1  christos    * port.
    590  1.1  christos    */
    591  1.1  christos   fp->fh_sin.sin_port = 0;
    592  1.1  christos 
    593  1.1  christos   return error;
    594  1.1  christos }
    595  1.1  christos 
    596  1.1  christos 
    597  1.1  christos static int
    598  1.1  christos webnfs_lookup(fh_cache *fp, fwd_fun fun, wchan_t wchan)
    599  1.1  christos {
    600  1.1  christos   struct rpc_msg wnfs_msg;
    601  1.1  christos   int len;
    602  1.1  christos   char iobuf[UDPMSGSIZE];
    603  1.1  christos   int error;
    604  1.1  christos   u_long proc;
    605  1.1  christos   XDRPROC_T_TYPE xdr_fn;
    606  1.1  christos   voidp argp;
    607  1.1  christos   nfsdiropargs args;
    608  1.1  christos #ifdef HAVE_FS_NFS3
    609  1.1  christos   am_LOOKUP3args args3;
    610  1.1  christos #endif /* HAVE_FS_NFS3 */
    611  1.1  christos   char *wnfs_path;
    612  1.1  christos   size_t l;
    613  1.1  christos 
    614  1.1  christos   if (!nfs_auth) {
    615  1.1  christos     error = make_nfs_auth();
    616  1.1  christos     if (error)
    617  1.1  christos       return error;
    618  1.1  christos   }
    619  1.1  christos 
    620  1.1  christos   if (fp->fh_sin.sin_port == 0) {
    621  1.1  christos     /* FIXME: wrong, don't discard sin_port in the first place for WebNFS. */
    622  1.1  christos     plog(XLOG_WARNING, "webnfs_lookup: port == 0 for nfs on %s, fixed",
    623  1.1  christos 	 fp->fh_fs->fs_host);
    624  1.1  christos     fp->fh_sin.sin_port = htons(NFS_PORT);
    625  1.1  christos   }
    626  1.1  christos 
    627  1.1  christos   /*
    628  1.1  christos    * Use native path like the rest of amd (cf. RFC 2054, 6.1).
    629  1.1  christos    */
    630  1.1  christos   l = strlen(fp->fh_path) + 2;
    631  1.1  christos   wnfs_path = (char *) xmalloc(l);
    632  1.1  christos   wnfs_path[0] = 0x80;
    633  1.1  christos   xstrlcpy(wnfs_path + 1, fp->fh_path, l - 1);
    634  1.1  christos 
    635  1.1  christos   /* find the right program and lookup procedure */
    636  1.1  christos #ifdef HAVE_FS_NFS3
    637  1.1  christos   if (fp->fh_nfs_version == NFS_VERSION3) {
    638  1.1  christos     proc = AM_NFSPROC3_LOOKUP;
    639  1.1  christos     xdr_fn = (XDRPROC_T_TYPE) xdr_am_LOOKUP3args;
    640  1.1  christos     argp = &args3;
    641  1.1  christos     /* WebNFS public file handle */
    642  1.1  christos     args3.what.dir.am_fh3_length = 0;
    643  1.1  christos     args3.what.name = wnfs_path;
    644  1.1  christos   } else {
    645  1.1  christos #endif /* HAVE_FS_NFS3 */
    646  1.1  christos     proc = NFSPROC_LOOKUP;
    647  1.1  christos     xdr_fn = (XDRPROC_T_TYPE) xdr_diropargs;
    648  1.1  christos     argp = &args;
    649  1.1  christos     /* WebNFS public file handle */
    650  1.1  christos     memset(&args.da_fhandle, 0, NFS_FHSIZE);
    651  1.1  christos     args.da_name = wnfs_path;
    652  1.1  christos #ifdef HAVE_FS_NFS3
    653  1.1  christos   }
    654  1.1  christos #endif /* HAVE_FS_NFS3 */
    655  1.1  christos 
    656  1.1  christos   plog(XLOG_INFO, "webnfs_lookup: NFS version %d", (int) fp->fh_nfs_version);
    657  1.1  christos 
    658  1.1  christos   rpc_msg_init(&wnfs_msg, NFS_PROGRAM, fp->fh_nfs_version, proc);
    659  1.1  christos   len = make_rpc_packet(iobuf,
    660  1.1  christos 			sizeof(iobuf),
    661  1.1  christos 			proc,
    662  1.1  christos 			&wnfs_msg,
    663  1.1  christos 			argp,
    664  1.1  christos 			(XDRPROC_T_TYPE) xdr_fn,
    665  1.1  christos 			nfs_auth);
    666  1.1  christos 
    667  1.1  christos   if (len > 0) {
    668  1.1  christos     error = fwd_packet(MK_RPC_XID(RPC_XID_WEBNFS, fp->fh_id),
    669  1.1  christos 		       iobuf,
    670  1.1  christos 		       len,
    671  1.1  christos 		       &fp->fh_sin,
    672  1.1  christos 		       &fp->fh_sin,
    673  1.1  christos 		       (opaque_t) ((long) fp->fh_id), /* cast to long needed for 64-bit archs */
    674  1.1  christos 		       fun);
    675  1.1  christos   } else {
    676  1.1  christos     error = -len;
    677  1.1  christos   }
    678  1.1  christos 
    679  1.1  christos   XFREE(wnfs_path);
    680  1.1  christos   return error;
    681  1.1  christos }
    682  1.1  christos 
    683  1.1  christos 
    684  1.1  christos /*
    685  1.1  christos  * NFS needs the local filesystem, remote filesystem
    686  1.1  christos  * remote hostname.
    687  1.1  christos  * Local filesystem defaults to remote and vice-versa.
    688  1.1  christos  */
    689  1.1  christos static char *
    690  1.1  christos nfs_match(am_opts *fo)
    691  1.1  christos {
    692  1.1  christos   char *xmtab;
    693  1.1  christos   size_t l;
    694  1.1  christos 
    695  1.1  christos   if (fo->opt_fs && !fo->opt_rfs)
    696  1.1  christos     fo->opt_rfs = fo->opt_fs;
    697  1.1  christos   if (!fo->opt_rfs) {
    698  1.1  christos     plog(XLOG_USER, "nfs: no remote filesystem specified");
    699  1.1  christos     return NULL;
    700  1.1  christos   }
    701  1.1  christos   if (!fo->opt_rhost) {
    702  1.1  christos     plog(XLOG_USER, "nfs: no remote host specified");
    703  1.1  christos     return NULL;
    704  1.1  christos   }
    705  1.1  christos 
    706  1.1  christos   /*
    707  1.1  christos    * Determine magic cookie to put in mtab
    708  1.1  christos    */
    709  1.1  christos   l = strlen(fo->opt_rhost) + strlen(fo->opt_rfs) + 2;
    710  1.1  christos   xmtab = (char *) xmalloc(l);
    711  1.1  christos   xsnprintf(xmtab, l, "%s:%s", fo->opt_rhost, fo->opt_rfs);
    712  1.1  christos   dlog("NFS: mounting remote server \"%s\", remote fs \"%s\" on \"%s\"",
    713  1.1  christos        fo->opt_rhost, fo->opt_rfs, fo->opt_fs);
    714  1.1  christos 
    715  1.1  christos   return xmtab;
    716  1.1  christos }
    717  1.1  christos 
    718  1.1  christos 
    719  1.1  christos /*
    720  1.1  christos  * Initialize am structure for nfs
    721  1.1  christos  */
    722  1.1  christos static int
    723  1.1  christos nfs_init(mntfs *mf)
    724  1.1  christos {
    725  1.1  christos   int error;
    726  1.1  christos   am_nfs_handle_t fhs;
    727  1.1  christos   char *colon;
    728  1.1  christos 
    729  1.1  christos   if (mf->mf_private) {
    730  1.1  christos     if (mf->mf_flags & MFF_NFS_SCALEDOWN) {
    731  1.1  christos       fserver *fs;
    732  1.1  christos 
    733  1.1  christos       /* tell remote mountd that we're done with this filehandle */
    734  1.1  christos       mf->mf_ops->umounted(mf);
    735  1.1  christos 
    736  1.1  christos       mf->mf_prfree(mf->mf_private);
    737  1.1  christos       fs = mf->mf_ops->ffserver(mf);
    738  1.1  christos       free_srvr(mf->mf_server);
    739  1.1  christos       mf->mf_server = fs;
    740  1.1  christos     } else
    741  1.1  christos       return 0;
    742  1.1  christos   }
    743  1.1  christos 
    744  1.1  christos   colon = strchr(mf->mf_info, ':');
    745  1.1  christos   if (colon == 0)
    746  1.1  christos     return ENOENT;
    747  1.1  christos 
    748  1.1  christos   error = prime_nfs_fhandle_cache(colon + 1, mf->mf_server, &fhs, mf);
    749  1.1  christos   if (!error) {
    750  1.1  christos     mf->mf_private = (opaque_t) ALLOC(am_nfs_handle_t);
    751  1.1  christos     mf->mf_prfree = (void (*)(opaque_t)) free;
    752  1.1  christos     memmove(mf->mf_private, (voidp) &fhs, sizeof(fhs));
    753  1.1  christos   }
    754  1.1  christos   return error;
    755  1.1  christos }
    756  1.1  christos 
    757  1.1  christos 
    758  1.1  christos int
    759  1.1  christos mount_nfs_fh(am_nfs_handle_t *fhp, char *mntdir, char *fs_name, mntfs *mf)
    760  1.1  christos {
    761  1.1  christos   MTYPE_TYPE type;
    762  1.1  christos   char *colon;
    763  1.1  christos   char *xopts=NULL, transp_timeo_opts[40], transp_retrans_opts[40];
    764  1.1  christos   char host[MAXHOSTNAMELEN + MAXPATHLEN + 2];
    765  1.1  christos   fserver *fs = mf->mf_server;
    766  1.1  christos   u_long nfs_version = fs->fs_version;
    767  1.1  christos   char *nfs_proto = fs->fs_proto; /* "tcp" or "udp" */
    768  1.1  christos   int on_autofs = mf->mf_flags & MFF_ON_AUTOFS;
    769  1.1  christos   int error;
    770  1.1  christos   int genflags;
    771  1.1  christos   int retry;
    772  1.1  christos   int proto = AMU_TYPE_NONE;
    773  1.1  christos   mntent_t mnt;
    774  1.1  christos   nfs_args_t nfs_args;
    775  1.1  christos 
    776  1.1  christos   /*
    777  1.1  christos    * Extract HOST name to give to kernel.
    778  1.1  christos    * Some systems like osf1/aix3/bsd44 variants may need old code
    779  1.1  christos    * for NFS_ARGS_NEEDS_PATH.
    780  1.1  christos    */
    781  1.1  christos   if (!(colon = strchr(fs_name, ':')))
    782  1.1  christos     return ENOENT;
    783  1.1  christos #ifdef MOUNT_TABLE_ON_FILE
    784  1.1  christos   *colon = '\0';
    785  1.1  christos #endif /* MOUNT_TABLE_ON_FILE */
    786  1.1  christos   xstrlcpy(host, fs_name, sizeof(host));
    787  1.1  christos #ifdef MOUNT_TABLE_ON_FILE
    788  1.1  christos   *colon = ':';
    789  1.1  christos #endif /* MOUNT_TABLE_ON_FILE */
    790  1.1  christos #ifdef MAXHOSTNAMELEN
    791  1.1  christos   /* most kernels have a name length restriction */
    792  1.1  christos   if (strlen(host) >= MAXHOSTNAMELEN)
    793  1.1  christos     xstrlcpy(host + MAXHOSTNAMELEN - 3, "..",
    794  1.1  christos 	     sizeof(host) - MAXHOSTNAMELEN + 3);
    795  1.1  christos #endif /* MAXHOSTNAMELEN */
    796  1.1  christos 
    797  1.1  christos   /*
    798  1.1  christos    * Create option=VAL for udp/tcp specific timeouts and retrans values, but
    799  1.1  christos    * only if these options were specified.
    800  1.1  christos    */
    801  1.1  christos 
    802  1.1  christos   transp_timeo_opts[0] = transp_retrans_opts[0] = '\0';	/* initialize */
    803  1.1  christos   if (STREQ(nfs_proto, "udp"))
    804  1.1  christos     proto = AMU_TYPE_UDP;
    805  1.1  christos   else if (STREQ(nfs_proto, "tcp"))
    806  1.1  christos     proto = AMU_TYPE_TCP;
    807  1.1  christos   if (proto != AMU_TYPE_NONE) {
    808  1.1  christos     if (gopt.amfs_auto_timeo[proto] > 0)
    809  1.1  christos       xsnprintf(transp_timeo_opts, sizeof(transp_timeo_opts), "%s=%d,",
    810  1.1  christos 		MNTTAB_OPT_TIMEO, gopt.amfs_auto_timeo[proto]);
    811  1.1  christos     if (gopt.amfs_auto_retrans[proto] > 0)
    812  1.1  christos       xsnprintf(transp_retrans_opts, sizeof(transp_retrans_opts), "%s=%d,",
    813  1.1  christos 		MNTTAB_OPT_RETRANS, gopt.amfs_auto_retrans[proto]);
    814  1.1  christos   }
    815  1.1  christos 
    816  1.1  christos   if (mf->mf_remopts && *mf->mf_remopts &&
    817  1.1  christos       !islocalnet(fs->fs_ip->sin_addr.s_addr)) {
    818  1.1  christos     plog(XLOG_INFO, "Using remopts=\"%s\"", mf->mf_remopts);
    819  1.1  christos     /* use transp_opts first, so map-specific opts will override */
    820  1.1  christos     xopts = str3cat(xopts, transp_timeo_opts, transp_retrans_opts, mf->mf_remopts);
    821  1.1  christos   } else {
    822  1.1  christos     /* use transp_opts first, so map-specific opts will override */
    823  1.1  christos     xopts = str3cat(xopts, transp_timeo_opts, transp_retrans_opts, mf->mf_mopts);
    824  1.1  christos   }
    825  1.1  christos 
    826  1.1  christos   memset((voidp) &mnt, 0, sizeof(mnt));
    827  1.1  christos   mnt.mnt_dir = mntdir;
    828  1.1  christos   mnt.mnt_fsname = fs_name;
    829  1.1  christos   mnt.mnt_opts = xopts;
    830  1.1  christos 
    831  1.1  christos   /*
    832  1.1  christos    * Set mount types accordingly
    833  1.1  christos    */
    834  1.1  christos #ifndef HAVE_FS_NFS3
    835  1.1  christos   type = MOUNT_TYPE_NFS;
    836  1.1  christos   mnt.mnt_type = MNTTAB_TYPE_NFS;
    837  1.1  christos #else /* HAVE_FS_NFS3 */
    838  1.1  christos   if (nfs_version == NFS_VERSION3) {
    839  1.1  christos     type = MOUNT_TYPE_NFS3;
    840  1.1  christos     /*
    841  1.1  christos      * Systems that include the mount table "vers" option generally do not
    842  1.1  christos      * set the mnttab entry to "nfs3", but to "nfs" and then they set
    843  1.1  christos      * "vers=3".  Setting it to "nfs3" works, but it may break some things
    844  1.1  christos      * like "df -t nfs" and the "quota" program (esp. on Solaris and Irix).
    845  1.1  christos      * So on those systems, set it to "nfs".
    846  1.1  christos      * Note: MNTTAB_OPT_VERS is always set for NFS3 (see am_compat.h).
    847  1.1  christos      */
    848  1.1  christos # if defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE)
    849  1.1  christos     mnt.mnt_type = MNTTAB_TYPE_NFS;
    850  1.1  christos # else /* defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE) */
    851  1.1  christos     mnt.mnt_type = MNTTAB_TYPE_NFS3;
    852  1.1  christos # endif /* defined(MNTTAB_OPT_VERS) && defined(MOUNT_TABLE_ON_FILE) */
    853  1.1  christos   } else {
    854  1.1  christos     type = MOUNT_TYPE_NFS;
    855  1.1  christos     mnt.mnt_type = MNTTAB_TYPE_NFS;
    856  1.1  christos   }
    857  1.1  christos #endif /* HAVE_FS_NFS3 */
    858  1.1  christos   plog(XLOG_INFO, "mount_nfs_fh: NFS version %d", (int) nfs_version);
    859  1.1  christos   plog(XLOG_INFO, "mount_nfs_fh: using NFS transport %s", nfs_proto);
    860  1.1  christos 
    861  1.1  christos   retry = hasmntval(&mnt, MNTTAB_OPT_RETRY);
    862  1.1  christos   if (retry <= 0)
    863  1.1  christos     retry = 1;			/* XXX */
    864  1.1  christos 
    865  1.1  christos   genflags = compute_mount_flags(&mnt);
    866  1.1  christos #ifdef HAVE_FS_AUTOFS
    867  1.1  christos   if (on_autofs)
    868  1.1  christos     genflags |= autofs_compute_mount_flags(&mnt);
    869  1.1  christos #endif /* HAVE_FS_AUTOFS */
    870  1.1  christos 
    871  1.1  christos   /* setup the many fields and flags within nfs_args */
    872  1.1  christos   compute_nfs_args(&nfs_args,
    873  1.1  christos 		   &mnt,
    874  1.1  christos 		   genflags,
    875  1.1  christos 		   NULL,	/* struct netconfig *nfsncp */
    876  1.1  christos 		   fs->fs_ip,
    877  1.1  christos 		   nfs_version,
    878  1.1  christos 		   nfs_proto,
    879  1.1  christos 		   fhp,
    880  1.1  christos 		   host,
    881  1.1  christos 		   fs_name);
    882  1.1  christos 
    883  1.1  christos   /* finally call the mounting function */
    884  1.1  christos   if (amuDebug(D_TRACE)) {
    885  1.1  christos     print_nfs_args(&nfs_args, nfs_version);
    886  1.1  christos     plog(XLOG_DEBUG, "Generic mount flags 0x%x used for NFS mount", genflags);
    887  1.1  christos   }
    888  1.1  christos   error = mount_fs(&mnt, genflags, (caddr_t) &nfs_args, retry, type,
    889  1.1  christos 		    nfs_version, nfs_proto, mnttab_file_name, on_autofs);
    890  1.1  christos   XFREE(xopts);
    891  1.1  christos 
    892  1.1  christos #ifdef HAVE_TRANSPORT_TYPE_TLI
    893  1.1  christos   free_knetconfig(nfs_args.knconf);
    894  1.1  christos   if (nfs_args.addr)
    895  1.1  christos     XFREE(nfs_args.addr);	/* allocated in compute_nfs_args() */
    896  1.1  christos #endif /* HAVE_TRANSPORT_TYPE_TLI */
    897  1.1  christos 
    898  1.1  christos   return error;
    899  1.1  christos }
    900  1.1  christos 
    901  1.1  christos 
    902  1.1  christos static int
    903  1.1  christos nfs_mount(am_node *am, mntfs *mf)
    904  1.1  christos {
    905  1.1  christos   int error = 0;
    906  1.1  christos   mntent_t mnt;
    907  1.1  christos 
    908  1.1  christos   if (!mf->mf_private) {
    909  1.1  christos     plog(XLOG_ERROR, "Missing filehandle for %s", mf->mf_info);
    910  1.1  christos     return EINVAL;
    911  1.1  christos   }
    912  1.1  christos 
    913  1.1  christos   mnt.mnt_opts = mf->mf_mopts;
    914  1.1  christos   if (amu_hasmntopt(&mnt, "softlookup") ||
    915  1.1  christos       (amu_hasmntopt(&mnt, "soft") && !amu_hasmntopt(&mnt, "nosoftlookup")))
    916  1.1  christos     am->am_flags |= AMF_SOFTLOOKUP;
    917  1.1  christos 
    918  1.1  christos   error = mount_nfs_fh((am_nfs_handle_t *) mf->mf_private,
    919  1.1  christos 		       mf->mf_mount,
    920  1.1  christos 		       mf->mf_info,
    921  1.1  christos 		       mf);
    922  1.1  christos 
    923  1.1  christos   if (error) {
    924  1.1  christos     errno = error;
    925  1.1  christos     dlog("mount_nfs: %m");
    926  1.1  christos   }
    927  1.1  christos 
    928  1.1  christos   return error;
    929  1.1  christos }
    930  1.1  christos 
    931  1.1  christos 
    932  1.1  christos static int
    933  1.1  christos nfs_umount(am_node *am, mntfs *mf)
    934  1.1  christos {
    935  1.1  christos   int unmount_flags, new_unmount_flags, error;
    936  1.1  christos 
    937  1.1  christos   unmount_flags = (mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
    938  1.1  christos   error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
    939  1.1  christos 
    940  1.1  christos #if defined(HAVE_UMOUNT2) && (defined(MNT2_GEN_OPT_FORCE) || defined(MNT2_GEN_OPT_DETACH))
    941  1.1  christos   /*
    942  1.1  christos    * If the attempt to unmount failed with EBUSY, and this fserver was
    943  1.1  christos    * marked for forced unmounts, then use forced/lazy unmounts.
    944  1.1  christos    */
    945  1.1  christos   if (error == EBUSY &&
    946  1.1  christos       gopt.flags & CFM_FORCED_UNMOUNTS &&
    947  1.1  christos       mf->mf_server->fs_flags & FSF_FORCE_UNMOUNT) {
    948  1.1  christos     plog(XLOG_INFO, "EZK: nfs_umount: trying forced/lazy unmounts");
    949  1.1  christos     /*
    950  1.1  christos      * XXX: turning off the FSF_FORCE_UNMOUNT may not be perfectly
    951  1.1  christos      * incorrect.  Multiple nodes may need to be timed out and restarted for
    952  1.1  christos      * a single hung fserver.
    953  1.1  christos      */
    954  1.1  christos     mf->mf_server->fs_flags &= ~FSF_FORCE_UNMOUNT;
    955  1.1  christos     new_unmount_flags = unmount_flags | AMU_UMOUNT_FORCE | AMU_UMOUNT_DETACH;
    956  1.1  christos     error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, new_unmount_flags);
    957  1.1  christos   }
    958  1.1  christos #endif /* HAVE_UMOUNT2 && (MNT2_GEN_OPT_FORCE || MNT2_GEN_OPT_DETACH) */
    959  1.1  christos 
    960  1.1  christos   /*
    961  1.1  christos    * Here is some code to unmount 'restarted' file systems.
    962  1.1  christos    * The restarted file systems are marked as 'nfs', not
    963  1.1  christos    * 'host', so we only have the map information for the
    964  1.1  christos    * the top-level mount.  The unmount will fail (EBUSY)
    965  1.1  christos    * if there are anything else from the NFS server mounted
    966  1.1  christos    * below the mount-point.  This code checks to see if there
    967  1.1  christos    * is anything mounted with the same prefix as the
    968  1.1  christos    * file system to be unmounted ("/a/b/c" when unmounting "/a/b").
    969  1.1  christos    * If there is, and it is a 'restarted' file system, we unmount
    970  1.1  christos    * it.
    971  1.1  christos    * Added by Mike Mitchell, mcm (at) unx.sas.com, 09/08/93
    972  1.1  christos    */
    973  1.1  christos   if (error == EBUSY) {
    974  1.1  christos     mntfs *new_mf;
    975  1.1  christos     int len = strlen(mf->mf_mount);
    976  1.1  christos     int didsome = 0;
    977  1.1  christos 
    978  1.1  christos     ITER(new_mf, mntfs, &mfhead) {
    979  1.1  christos       if (new_mf->mf_ops != mf->mf_ops ||
    980  1.1  christos 	  new_mf->mf_refc > 1 ||
    981  1.1  christos 	  mf == new_mf ||
    982  1.1  christos 	  ((new_mf->mf_flags & (MFF_MOUNTED | MFF_UNMOUNTING | MFF_RESTART)) == (MFF_MOUNTED | MFF_RESTART)))
    983  1.1  christos 	continue;
    984  1.1  christos 
    985  1.1  christos       if (NSTREQ(mf->mf_mount, new_mf->mf_mount, len) &&
    986  1.1  christos 	  new_mf->mf_mount[len] == '/') {
    987  1.1  christos 	new_unmount_flags =
    988  1.1  christos 	  (new_mf->mf_flags & MFF_ON_AUTOFS) ? AMU_UMOUNT_AUTOFS : 0;
    989  1.1  christos 	UMOUNT_FS(new_mf->mf_mount, mnttab_file_name, new_unmount_flags);
    990  1.1  christos 	didsome = 1;
    991  1.1  christos       }
    992  1.1  christos     }
    993  1.1  christos     if (didsome)
    994  1.1  christos       error = UMOUNT_FS(mf->mf_mount, mnttab_file_name, unmount_flags);
    995  1.1  christos   }
    996  1.1  christos   if (error)
    997  1.1  christos     return error;
    998  1.1  christos 
    999  1.1  christos   return 0;
   1000  1.1  christos }
   1001  1.1  christos 
   1002  1.1  christos 
   1003  1.1  christos static void
   1004  1.1  christos nfs_umounted(mntfs *mf)
   1005  1.1  christos {
   1006  1.1  christos   fserver *fs;
   1007  1.1  christos   char *colon, *path;
   1008  1.1  christos 
   1009  1.1  christos   if (mf->mf_error || mf->mf_refc > 1)
   1010  1.1  christos     return;
   1011  1.1  christos 
   1012  1.1  christos   /*
   1013  1.1  christos    * No need to inform mountd when WebNFS is in use.
   1014  1.1  christos    */
   1015  1.1  christos   if (mf->mf_flags & MFF_WEBNFS)
   1016  1.1  christos     return;
   1017  1.1  christos 
   1018  1.1  christos   /*
   1019  1.1  christos    * Call the mount daemon on the server to announce that we are not using
   1020  1.1  christos    * the fs any more.
   1021  1.1  christos    *
   1022  1.1  christos    * XXX: This is *wrong*.  The mountd should be called when the fhandle is
   1023  1.1  christos    * flushed from the cache, and a reference held to the cached entry while
   1024  1.1  christos    * the fs is mounted...
   1025  1.1  christos    */
   1026  1.1  christos   fs = mf->mf_server;
   1027  1.1  christos   colon = path = strchr(mf->mf_info, ':');
   1028  1.1  christos   if (fs && colon) {
   1029  1.1  christos     fh_cache f;
   1030  1.1  christos 
   1031  1.1  christos     dlog("calling mountd for %s", mf->mf_info);
   1032  1.1  christos     *path++ = '\0';
   1033  1.1  christos     f.fh_path = path;
   1034  1.1  christos     f.fh_sin = *fs->fs_ip;
   1035  1.1  christos     f.fh_sin.sin_port = (u_short) 0;
   1036  1.1  christos     f.fh_nfs_version = fs->fs_version;
   1037  1.1  christos     f.fh_fs = fs;
   1038  1.1  christos     f.fh_id = 0;
   1039  1.1  christos     f.fh_error = 0;
   1040  1.1  christos     prime_nfs_fhandle_cache(colon + 1, mf->mf_server, (am_nfs_handle_t *) NULL, mf);
   1041  1.1  christos     call_mountd(&f, MOUNTPROC_UMNT, (fwd_fun *) NULL, (wchan_t) NULL);
   1042  1.1  christos     *colon = ':';
   1043  1.1  christos   }
   1044  1.1  christos }
   1045