Home | History | Annotate | Line # | Download | only in nfs
nfs_vfsops.c revision 1.21
      1 /*
      2  * Copyright (c) 1989, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Rick Macklem at The University of Guelph.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	from: @(#)nfs_vfsops.c	8.3 (Berkeley) 1/4/94
     37  *	$Id: nfs_vfsops.c,v 1.21 1994/06/08 11:37:03 mycroft Exp $
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/conf.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/signal.h>
     44 #include <sys/proc.h>
     45 #include <sys/namei.h>
     46 #include <sys/vnode.h>
     47 #include <sys/kernel.h>
     48 #include <sys/mount.h>
     49 #include <sys/buf.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/socket.h>
     52 #include <sys/systm.h>
     53 
     54 #include <net/if.h>
     55 #include <net/route.h>
     56 #include <netinet/in.h>
     57 
     58 #include <nfs/rpcv2.h>
     59 #include <nfs/nfsv2.h>
     60 #include <nfs/nfsnode.h>
     61 #include <nfs/nfsmount.h>
     62 #include <nfs/nfs.h>
     63 #include <nfs/xdr_subs.h>
     64 #include <nfs/nfsm_subs.h>
     65 #include <nfs/nfsdiskless.h>
     66 #include <nfs/nqnfs.h>
     67 
     68 /*
     69  * nfs vfs operations.
     70  */
     71 struct vfsops nfs_vfsops = {
     72 	MOUNT_NFS,
     73 	nfs_mount,
     74 	nfs_start,
     75 	nfs_unmount,
     76 	nfs_root,
     77 	nfs_quotactl,
     78 	nfs_statfs,
     79 	nfs_sync,
     80 	nfs_vget,
     81 	nfs_fhtovp,
     82 	nfs_vptofh,
     83 	nfs_init,
     84 };
     85 
     86 /*
     87  * This structure must be filled in by a primary bootstrap or bootstrap
     88  * server for a diskless/dataless machine. It is initialized below just
     89  * to ensure that it is allocated to initialized data (.data not .bss).
     90  */
     91 struct nfs_diskless nfs_diskless = { 0 };
     92 
     93 extern u_long nfs_procids[NFS_NPROCS];
     94 extern u_long nfs_prog, nfs_vers;
     95 void nfs_disconnect __P((struct nfsmount *));
     96 void nfsargs_ntoh __P((struct nfs_args *));
     97 static struct mount *nfs_mountdiskless __P((char *, char *, int,
     98     struct sockaddr_in *, struct nfs_args *, register struct vnode **));
     99 
    100 #define TRUE	1
    101 #define	FALSE	0
    102 
    103 /*
    104  * nfs statfs call
    105  */
    106 int
    107 nfs_statfs(mp, sbp, p)
    108 	struct mount *mp;
    109 	register struct statfs *sbp;
    110 	struct proc *p;
    111 {
    112 	register struct vnode *vp;
    113 	register struct nfsv2_statfs *sfp;
    114 	register caddr_t cp;
    115 	register long t1;
    116 	caddr_t bpos, dpos, cp2;
    117 	int error = 0, isnq;
    118 	struct mbuf *mreq, *mrep, *md, *mb, *mb2;
    119 	struct nfsmount *nmp;
    120 	struct ucred *cred;
    121 	struct nfsnode *np;
    122 
    123 	nmp = VFSTONFS(mp);
    124 	isnq = (nmp->nm_flag & NFSMNT_NQNFS);
    125 	if (error = nfs_nget(mp, &nmp->nm_fh, &np))
    126 		return (error);
    127 	vp = NFSTOV(np);
    128 	nfsstats.rpccnt[NFSPROC_STATFS]++;
    129 	cred = crget();
    130 	cred->cr_ngroups = 1;
    131 	nfsm_reqhead(vp, NFSPROC_STATFS, NFSX_FH);
    132 	nfsm_fhtom(vp);
    133 	nfsm_request(vp, NFSPROC_STATFS, p, cred);
    134 	nfsm_dissect(sfp, struct nfsv2_statfs *, NFSX_STATFS(isnq));
    135 #ifdef COMPAT_09
    136 	sbp->f_type = 2;
    137 #else
    138 	sbp->f_type = 0;
    139 #endif
    140 	sbp->f_flags = nmp->nm_flag;
    141 	sbp->f_iosize = NFS_MAXDGRAMDATA;
    142 	sbp->f_bsize = fxdr_unsigned(long, sfp->sf_bsize);
    143 	sbp->f_blocks = fxdr_unsigned(long, sfp->sf_blocks);
    144 	sbp->f_bfree = fxdr_unsigned(long, sfp->sf_bfree);
    145 	sbp->f_bavail = fxdr_unsigned(long, sfp->sf_bavail);
    146 	if (isnq) {
    147 		sbp->f_files = fxdr_unsigned(long, sfp->sf_files);
    148 		sbp->f_ffree = fxdr_unsigned(long, sfp->sf_ffree);
    149 	} else {
    150 		sbp->f_files = 0;
    151 		sbp->f_ffree = 0;
    152 	}
    153 	if (sbp != &mp->mnt_stat) {
    154 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
    155 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
    156 	}
    157 	strncpy(&sbp->f_fstypename[0], mp->mnt_op->vfs_name, MFSNAMELEN);
    158 	sbp->f_fstypename[MFSNAMELEN] = '\0';
    159 	nfsm_reqdone;
    160 	vrele(vp);
    161 	crfree(cred);
    162 	return (error);
    163 }
    164 
    165 /*
    166  * Mount a remote root fs via. nfs. This depends on the info in the
    167  * nfs_diskless structure that has been filled in properly by some primary
    168  * bootstrap.
    169  * It goes something like this:
    170  * - do enough of "ifconfig" by calling ifioctl() so that the system
    171  *   can talk to the server
    172  * - If nfs_diskless.mygateway is filled in, use that address as
    173  *   a default gateway.
    174  * - hand craft the swap nfs vnode hanging off a fake mount point
    175  *	if swdevt[0].sw_dev == NODEV
    176  * - build the rootfs mount point and call mountnfs() to do the rest.
    177  */
    178 int
    179 nfs_mountroot()
    180 {
    181 	register struct mount *mp;
    182 	register struct nfs_diskless *nd = &nfs_diskless;
    183 	struct socket *so;
    184 	struct vnode *vp;
    185 	struct proc *p = curproc;		/* XXX */
    186 	int error, i;
    187 
    188 	/*
    189 	 * XXX time must be non-zero when we init the interface or else
    190 	 * the arp code will wedge...
    191 	 */
    192 	if (time.tv_sec == 0)
    193 		time.tv_sec = 1;
    194 
    195 #ifdef notyet
    196 	/* Set up swap credentials. */
    197 	proc0.p_ucred->cr_uid = ntohl(nd->swap_ucred.cr_uid);
    198 	proc0.p_ucred->cr_gid = ntohl(nd->swap_ucred.cr_gid);
    199 	if ((proc0.p_ucred->cr_ngroups = ntohs(nd->swap_ucred.cr_ngroups)) >
    200 		NGROUPS)
    201 		proc0.p_ucred->cr_ngroups = NGROUPS;
    202 	for (i = 0; i < proc0.p_ucred->cr_ngroups; i++)
    203 	    proc0.p_ucred->cr_groups[i] = ntohl(nd->swap_ucred.cr_groups[i]);
    204 #endif
    205 
    206 	/*
    207 	 * Do enough of ifconfig(8) so that the critical net interface can
    208 	 * talk to the server.
    209 	 */
    210 	if (error = socreate(nd->myif.ifra_addr.sa_family, &so, SOCK_DGRAM, 0))
    211 		panic("nfs_mountroot: socreate: %d", error);
    212 	if (error = ifioctl(so, SIOCAIFADDR, (caddr_t)&nd->myif, p))
    213 		panic("nfs_mountroot: SIOCAIFADDR: %d", error);
    214 	soclose(so);
    215 
    216 	/*
    217 	 * If the gateway field is filled in, set it as the default route.
    218 	 */
    219 	if (nd->mygateway.sin_len != 0) {
    220 		struct sockaddr_in mask, sin;
    221 
    222 		bzero((caddr_t)&mask, sizeof(mask));
    223 		sin = mask;
    224 		sin.sin_family = AF_INET;
    225 		sin.sin_len = sizeof(sin);
    226 		if (error = rtrequest(RTM_ADD, (struct sockaddr *)&sin,
    227 		    (struct sockaddr *)&nd->mygateway,
    228 		    (struct sockaddr *)&mask,
    229 		    RTF_UP | RTF_GATEWAY, (struct rtentry **)0))
    230 			panic("nfs_mountroot: RTM_ADD: %d", error);
    231 	}
    232 
    233 	/*
    234 	 * If swapping to an nfs node (indicated by swdevt[0].sw_dev == NODEV):
    235 	 * Create a fake mount point just for the swap vnode so that the
    236 	 * swap file can be on a different server from the rootfs.
    237 	 */
    238 	if (swdevt[0].sw_dev == NODEV) {
    239 		nd->swap_args.fh = (nfsv2fh_t *)nd->swap_fh;
    240 		(void) nfs_mountdiskless(nd->swap_hostnam, "/swap", 0,
    241 		    &nd->swap_saddr, &nd->swap_args, &vp);
    242 
    243 		/*
    244 		 * Since the swap file is not the root dir of a file system,
    245 		 * hack it to a regular file.
    246 		 */
    247 		vp->v_type = VREG;
    248 		vp->v_flag = 0;
    249 		swapdev_vp = vp;
    250 		VREF(vp);
    251 		swdevt[0].sw_vp = vp;
    252 		swdevt[0].sw_nblks = ntohl(nd->swap_nblks);
    253 	} else if (bdevvp(swapdev, &swapdev_vp))
    254 		panic("nfs_mountroot: can't setup swapdev_vp");
    255 
    256 	/*
    257 	 * Create the rootfs mount point.
    258 	 */
    259 	nd->root_args.fh = (nfsv2fh_t *)nd->root_fh;
    260 	mp = nfs_mountdiskless(nd->root_hostnam, "/", MNT_RDONLY,
    261 	    &nd->root_saddr, &nd->root_args, &vp);
    262 
    263 	if (vfs_lock(mp))
    264 		panic("nfs_mountroot: vfs_lock");
    265 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
    266 	mp->mnt_flag |= MNT_ROOTFS;
    267 	mp->mnt_vnodecovered = NULLVP;
    268 	vfs_unlock(mp);
    269 	rootvp = vp;
    270 
    271 	/*
    272 	 * This is not really an nfs issue, but it is much easier to
    273 	 * set hostname here and then let the "/etc/rc.xxx" files
    274 	 * mount the right /var based upon its preset value.
    275 	 */
    276 	bcopy(nd->my_hostnam, hostname, MAXHOSTNAMELEN);
    277 	hostname[MAXHOSTNAMELEN - 1] = '\0';
    278 	for (i = 0; i < MAXHOSTNAMELEN; i++)
    279 		if (hostname[i] == '\0')
    280 			break;
    281 	hostnamelen = i;
    282 	inittodr(ntohl(nd->root_time));
    283 	return (0);
    284 }
    285 
    286 /*
    287  * Internal version of mount system call for diskless setup.
    288  */
    289 static struct mount *
    290 nfs_mountdiskless(path, which, mountflag, sin, args, vpp)
    291 	char *path;
    292 	char *which;
    293 	int mountflag;
    294 	struct sockaddr_in *sin;
    295 	struct nfs_args *args;
    296 	register struct vnode **vpp;
    297 {
    298 	register struct mount *mp;
    299 	register struct mbuf *m;
    300 	register int error;
    301 
    302 	mp = (struct mount *)malloc((u_long)sizeof(struct mount),
    303 	    M_MOUNT, M_NOWAIT);
    304 	if (mp == NULL)
    305 		panic("nfs_mountroot: %s mount malloc", which);
    306 	bzero((char *)mp, (u_long)sizeof(struct mount));
    307 	mp->mnt_op = &nfs_vfsops;
    308 	mp->mnt_flag = mountflag;
    309 
    310 	MGET(m, MT_SONAME, M_DONTWAIT);
    311 	if (m == NULL)
    312 		panic("nfs_mountroot: %s mount mbuf", which);
    313 	bcopy((caddr_t)sin, mtod(m, caddr_t), sin->sin_len);
    314 	m->m_len = sin->sin_len;
    315 	nfsargs_ntoh(args);
    316 	if (error = mountnfs(args, mp, m, which, path, vpp))
    317 		panic("nfs_mountroot: mount %s on %s: %d", path, which, error);
    318 
    319 	return (mp);
    320 }
    321 
    322 /*
    323  * Convert the integer fields of the nfs_args structure from net byte order
    324  * to host byte order. Called by nfs_mountroot() above.
    325  */
    326 void
    327 nfsargs_ntoh(nfsp)
    328 	register struct nfs_args *nfsp;
    329 {
    330 
    331 	NTOHL(nfsp->sotype);
    332 	NTOHL(nfsp->proto);
    333 	NTOHL(nfsp->flags);
    334 	NTOHL(nfsp->wsize);
    335 	NTOHL(nfsp->rsize);
    336 	NTOHL(nfsp->timeo);
    337 	NTOHL(nfsp->retrans);
    338 	NTOHL(nfsp->maxgrouplist);
    339 	NTOHL(nfsp->readahead);
    340 	NTOHL(nfsp->leaseterm);
    341 	NTOHL(nfsp->deadthresh);
    342 }
    343 
    344 void
    345 nfs_decode_args(nmp, argp)
    346 	struct nfsmount *nmp;
    347 	struct nfs_args *argp;
    348 {
    349 	int s;
    350 
    351 	s = splnet();
    352 	/* Update flags atomically.  Don't change the lock bits. */
    353 	nmp->nm_flag =
    354 	    (argp->flags & ~NFSMNT_INTERNAL) | (nmp->nm_flag & NFSMNT_INTERNAL);
    355 	splx(s);
    356 
    357 	if ((argp->flags & NFSMNT_TIMEO) && argp->timeo > 0) {
    358 		nmp->nm_timeo = (argp->timeo * NFS_HZ + 5) / 10;
    359 		if (nmp->nm_timeo < NFS_MINTIMEO)
    360 			nmp->nm_timeo = NFS_MINTIMEO;
    361 		else if (nmp->nm_timeo > NFS_MAXTIMEO)
    362 			nmp->nm_timeo = NFS_MAXTIMEO;
    363 	}
    364 
    365 	if ((argp->flags & NFSMNT_RETRANS) && argp->retrans > 1) {
    366 		nmp->nm_retry = argp->retrans;
    367 		if (nmp->nm_retry > NFS_MAXREXMIT)
    368 			nmp->nm_retry = NFS_MAXREXMIT;
    369 	}
    370 
    371 	if ((argp->flags & NFSMNT_WSIZE) && argp->wsize > 0) {
    372 		nmp->nm_wsize = argp->wsize;
    373 		/* Round down to multiple of blocksize */
    374 		nmp->nm_wsize &= ~0x1ff;
    375 		if (nmp->nm_wsize <= 0)
    376 			nmp->nm_wsize = 512;
    377 		else if (nmp->nm_wsize > NFS_MAXDATA)
    378 			nmp->nm_wsize = NFS_MAXDATA;
    379 	}
    380 	if (nmp->nm_wsize > MAXBSIZE)
    381 		nmp->nm_wsize = MAXBSIZE;
    382 
    383 	if ((argp->flags & NFSMNT_RSIZE) && argp->rsize > 0) {
    384 		nmp->nm_rsize = argp->rsize;
    385 		/* Round down to multiple of blocksize */
    386 		nmp->nm_rsize &= ~0x1ff;
    387 		if (nmp->nm_rsize <= 0)
    388 			nmp->nm_rsize = 512;
    389 		else if (nmp->nm_rsize > NFS_MAXDATA)
    390 			nmp->nm_rsize = NFS_MAXDATA;
    391 	}
    392 	if (nmp->nm_rsize > MAXBSIZE)
    393 		nmp->nm_rsize = MAXBSIZE;
    394 
    395 	if ((argp->flags & NFSMNT_MAXGRPS) && argp->maxgrouplist >= 0 &&
    396 		argp->maxgrouplist <= NFS_MAXGRPS)
    397 		nmp->nm_numgrps = argp->maxgrouplist;
    398 	if ((argp->flags & NFSMNT_READAHEAD) && argp->readahead >= 0 &&
    399 		argp->readahead <= NFS_MAXRAHEAD)
    400 		nmp->nm_readahead = argp->readahead;
    401 	if ((argp->flags & NFSMNT_LEASETERM) && argp->leaseterm >= 2 &&
    402 		argp->leaseterm <= NQ_MAXLEASE)
    403 		nmp->nm_leaseterm = argp->leaseterm;
    404 	if ((argp->flags & NFSMNT_DEADTHRESH) && argp->deadthresh >= 1 &&
    405 		argp->deadthresh <= NQ_NEVERDEAD)
    406 		nmp->nm_deadthresh = argp->deadthresh;
    407 }
    408 
    409 /*
    410  * VFS Operations.
    411  *
    412  * mount system call
    413  * It seems a bit dumb to copyinstr() the host and path here and then
    414  * bcopy() them in mountnfs(), but I wanted to detect errors before
    415  * doing the sockargs() call because sockargs() allocates an mbuf and
    416  * an error after that means that I have to release the mbuf.
    417  */
    418 /* ARGSUSED */
    419 int
    420 nfs_mount(mp, path, data, ndp, p)
    421 	struct mount *mp;
    422 	char *path;
    423 	caddr_t data;
    424 	struct nameidata *ndp;
    425 	struct proc *p;
    426 {
    427 	int error;
    428 	struct nfs_args args;
    429 	struct mbuf *nam;
    430 	struct vnode *vp;
    431 	char pth[MNAMELEN], hst[MNAMELEN];
    432 	u_int len;
    433 	nfsv2fh_t nfh;
    434 
    435 	if (error = copyin(data, (caddr_t)&args, sizeof (struct nfs_args)))
    436 		return (error);
    437 	if (mp->mnt_flag & MNT_UPDATE) {
    438 		register struct nfsmount *nmp = VFSTONFS(mp);
    439 
    440 		if (nmp == NULL)
    441 			return (EIO);
    442 		nfs_decode_args(nmp, &args);
    443 		return (0);
    444 	}
    445 	if (error = copyin((caddr_t)args.fh, (caddr_t)&nfh, sizeof (nfsv2fh_t)))
    446 		return (error);
    447 	if (error = copyinstr(path, pth, MNAMELEN-1, &len))
    448 		return (error);
    449 	bzero(&pth[len], MNAMELEN - len);
    450 	if (error = copyinstr(args.hostname, hst, MNAMELEN-1, &len))
    451 		return (error);
    452 	bzero(&hst[len], MNAMELEN - len);
    453 	/* sockargs() call must be after above copyin() calls */
    454 	if (error = sockargs(&nam, (caddr_t)args.addr,
    455 		args.addrlen, MT_SONAME))
    456 		return (error);
    457 	args.fh = &nfh;
    458 	error = mountnfs(&args, mp, nam, pth, hst, &vp);
    459 	return (error);
    460 }
    461 
    462 /*
    463  * Common code for mount and mountroot
    464  */
    465 int
    466 mountnfs(argp, mp, nam, pth, hst, vpp)
    467 	register struct nfs_args *argp;
    468 	register struct mount *mp;
    469 	struct mbuf *nam;
    470 	char *pth, *hst;
    471 	struct vnode **vpp;
    472 {
    473 	register struct nfsmount *nmp;
    474 	struct nfsnode *np;
    475 	int error;
    476 
    477 	if (mp->mnt_flag & MNT_UPDATE) {
    478 		nmp = VFSTONFS(mp);
    479 		/* update paths, file handles, etc, here	XXX */
    480 		m_freem(nam);
    481 		return (0);
    482 	} else {
    483 		MALLOC(nmp, struct nfsmount *, sizeof (struct nfsmount),
    484 		    M_NFSMNT, M_WAITOK);
    485 		bzero((caddr_t)nmp, sizeof (struct nfsmount));
    486 		mp->mnt_data = (qaddr_t)nmp;
    487 	}
    488 	getnewfsid(mp, makefstype(MOUNT_NFS));
    489 	nmp->nm_mountp = mp;
    490 	if ((argp->flags & (NFSMNT_NQNFS | NFSMNT_MYWRITE)) ==
    491 		(NFSMNT_NQNFS | NFSMNT_MYWRITE)) {
    492 		error = EPERM;
    493 		goto bad;
    494 	}
    495 	if (argp->flags & NFSMNT_NQNFS)
    496 		/*
    497 		 * We have to set mnt_maxsymlink to a non-zero value so
    498 		 * that COMPAT_43 routines will know that we are setting
    499 		 * the d_type field in directories (and can zero it for
    500 		 * unsuspecting binaries).
    501 		 */
    502 		mp->mnt_maxsymlinklen = 1;
    503 	nmp->nm_timeo = NFS_TIMEO;
    504 	nmp->nm_retry = NFS_RETRANS;
    505 	nmp->nm_wsize = NFS_WSIZE;
    506 	nmp->nm_rsize = NFS_RSIZE;
    507 	nmp->nm_numgrps = NFS_MAXGRPS;
    508 	nmp->nm_readahead = NFS_DEFRAHEAD;
    509 	nmp->nm_leaseterm = NQ_DEFLEASE;
    510 	nmp->nm_deadthresh = NQ_DEADTHRESH;
    511 	nmp->nm_tnext = (struct nfsnode *)nmp;
    512 	nmp->nm_tprev = (struct nfsnode *)nmp;
    513 	nmp->nm_inprog = NULLVP;
    514 	bcopy((caddr_t)argp->fh, (caddr_t)&nmp->nm_fh, sizeof(nfsv2fh_t));
    515 #ifdef COMPAT_09
    516 	mp->mnt_stat.f_type = 2;
    517 #else
    518 	mp->mnt_stat.f_type = 0;
    519 #endif
    520 	bcopy(hst, mp->mnt_stat.f_mntfromname, MNAMELEN);
    521 	bcopy(pth, mp->mnt_stat.f_mntonname, MNAMELEN);
    522 	nmp->nm_nam = nam;
    523 	nfs_decode_args(nmp, argp);
    524 
    525 	/* Set up the sockets and per-host congestion */
    526 	nmp->nm_sotype = argp->sotype;
    527 	nmp->nm_soproto = argp->proto;
    528 
    529 	/*
    530 	 * For Connection based sockets (TCP,...) defer the connect until
    531 	 * the first request, in case the server is not responding.
    532 	 */
    533 	if (nmp->nm_sotype == SOCK_DGRAM &&
    534 		(error = nfs_connect(nmp, (struct nfsreq *)0)))
    535 		goto bad;
    536 
    537 	/*
    538 	 * This is silly, but it has to be set so that vinifod() works.
    539 	 * We do not want to do an nfs_statfs() here since we can get
    540 	 * stuck on a dead server and we are holding a lock on the mount
    541 	 * point.
    542 	 */
    543 	mp->mnt_stat.f_iosize = NFS_MAXDGRAMDATA;
    544 	/*
    545 	 * A reference count is needed on the nfsnode representing the
    546 	 * remote root.  If this object is not persistent, then backward
    547 	 * traversals of the mount point (i.e. "..") will not work if
    548 	 * the nfsnode gets flushed out of the cache. Ufs does not have
    549 	 * this problem, because one can identify root inodes by their
    550 	 * number == ROOTINO (2).
    551 	 */
    552 	if (error = nfs_nget(mp, &nmp->nm_fh, &np))
    553 		goto bad;
    554 	*vpp = NFSTOV(np);
    555 
    556 	return (0);
    557 bad:
    558 	nfs_disconnect(nmp);
    559 	free((caddr_t)nmp, M_NFSMNT);
    560 	m_freem(nam);
    561 	return (error);
    562 }
    563 
    564 /*
    565  * unmount system call
    566  */
    567 int
    568 nfs_unmount(mp, mntflags, p)
    569 	struct mount *mp;
    570 	int mntflags;
    571 	struct proc *p;
    572 {
    573 	register struct nfsmount *nmp;
    574 	struct nfsnode *np;
    575 	struct vnode *vp;
    576 	int error, flags = 0;
    577 	extern int doforce;
    578 
    579 	if (mntflags & MNT_FORCE) {
    580 		if (!doforce || (mp->mnt_flag & MNT_ROOTFS))
    581 			return (EINVAL);
    582 		flags |= FORCECLOSE;
    583 	}
    584 	nmp = VFSTONFS(mp);
    585 	/*
    586 	 * Goes something like this..
    587 	 * - Check for activity on the root vnode (other than ourselves).
    588 	 * - Call vflush() to clear out vnodes for this file system,
    589 	 *   except for the root vnode.
    590 	 * - Decrement reference on the vnode representing remote root.
    591 	 * - Close the socket
    592 	 * - Free up the data structures
    593 	 */
    594 	/*
    595 	 * We need to decrement the ref. count on the nfsnode representing
    596 	 * the remote root.  See comment in mountnfs().  The VFS unmount()
    597 	 * has done vput on this vnode, otherwise we would get deadlock!
    598 	 */
    599 	if (error = nfs_nget(mp, &nmp->nm_fh, &np))
    600 		return(error);
    601 	vp = NFSTOV(np);
    602 	if (vp->v_usecount > 2) {
    603 		vput(vp);
    604 		return (EBUSY);
    605 	}
    606 
    607 	/*
    608 	 * Must handshake with nqnfs_clientd() if it is active.
    609 	 */
    610 	nmp->nm_flag |= NFSMNT_DISMINPROG;
    611 	while (nmp->nm_inprog != NULLVP)
    612 		(void) tsleep((caddr_t)&lbolt, PSOCK, "nfsdism", 0);
    613 	if (error = vflush(mp, vp, flags)) {
    614 		vput(vp);
    615 		nmp->nm_flag &= ~NFSMNT_DISMINPROG;
    616 		return (error);
    617 	}
    618 
    619 	/*
    620 	 * We are now committed to the unmount.
    621 	 * For NQNFS, let the server daemon free the nfsmount structure.
    622 	 */
    623 	if (nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB))
    624 		nmp->nm_flag |= NFSMNT_DISMNT;
    625 
    626 	/*
    627 	 * There are two reference counts to get rid of here.
    628 	 */
    629 	vrele(vp);
    630 	vrele(vp);
    631 	vgone(vp);
    632 	nfs_disconnect(nmp);
    633 	m_freem(nmp->nm_nam);
    634 
    635 	if ((nmp->nm_flag & (NFSMNT_NQNFS | NFSMNT_KERB)) == 0)
    636 		free((caddr_t)nmp, M_NFSMNT);
    637 	return (0);
    638 }
    639 
    640 /*
    641  * Return root of a filesystem
    642  */
    643 int
    644 nfs_root(mp, vpp)
    645 	struct mount *mp;
    646 	struct vnode **vpp;
    647 {
    648 	register struct vnode *vp;
    649 	struct nfsmount *nmp;
    650 	struct nfsnode *np;
    651 	int error;
    652 
    653 	nmp = VFSTONFS(mp);
    654 	if (error = nfs_nget(mp, &nmp->nm_fh, &np))
    655 		return (error);
    656 	vp = NFSTOV(np);
    657 	vp->v_type = VDIR;
    658 	vp->v_flag = VROOT;
    659 	*vpp = vp;
    660 	return (0);
    661 }
    662 
    663 extern int syncprt;
    664 
    665 /*
    666  * Flush out the buffer cache
    667  */
    668 /* ARGSUSED */
    669 int
    670 nfs_sync(mp, waitfor, cred, p)
    671 	struct mount *mp;
    672 	int waitfor;
    673 	struct ucred *cred;
    674 	struct proc *p;
    675 {
    676 	register struct vnode *vp;
    677 	int error, allerror = 0;
    678 
    679 	/*
    680 	 * Force stale buffer cache information to be flushed.
    681 	 */
    682 loop:
    683 	for (vp = mp->mnt_vnodelist.lh_first;
    684 	     vp != NULL;
    685 	     vp = vp->v_mntvnodes.le_next) {
    686 		/*
    687 		 * If the vnode that we are about to sync is no longer
    688 		 * associated with this mount point, start over.
    689 		 */
    690 		if (vp->v_mount != mp)
    691 			goto loop;
    692 		if (VOP_ISLOCKED(vp) || vp->v_dirtyblkhd.lh_first == NULL)
    693 			continue;
    694 		if (vget(vp, 1))
    695 			goto loop;
    696 		if (error = VOP_FSYNC(vp, cred, waitfor, p))
    697 			allerror = error;
    698 		vput(vp);
    699 	}
    700 	return (allerror);
    701 }
    702 
    703 /*
    704  * NFS flat namespace lookup.
    705  * Currently unsupported.
    706  */
    707 /* ARGSUSED */
    708 int
    709 nfs_vget(mp, ino, vpp)
    710 	struct mount *mp;
    711 	ino_t ino;
    712 	struct vnode **vpp;
    713 {
    714 
    715 	return (EOPNOTSUPP);
    716 }
    717 
    718 /*
    719  * At this point, this should never happen
    720  */
    721 /* ARGSUSED */
    722 int
    723 nfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
    724 	register struct mount *mp;
    725 	struct fid *fhp;
    726 	struct mbuf *nam;
    727 	struct vnode **vpp;
    728 	int *exflagsp;
    729 	struct ucred **credanonp;
    730 {
    731 
    732 	return (EINVAL);
    733 }
    734 
    735 /*
    736  * Vnode pointer to File handle, should never happen either
    737  */
    738 /* ARGSUSED */
    739 int
    740 nfs_vptofh(vp, fhp)
    741 	struct vnode *vp;
    742 	struct fid *fhp;
    743 {
    744 
    745 	return (EINVAL);
    746 }
    747 
    748 /*
    749  * Vfs start routine, a no-op.
    750  */
    751 /* ARGSUSED */
    752 int
    753 nfs_start(mp, flags, p)
    754 	struct mount *mp;
    755 	int flags;
    756 	struct proc *p;
    757 {
    758 
    759 	return (0);
    760 }
    761 
    762 /*
    763  * Do operations associated with quotas, not supported
    764  */
    765 /* ARGSUSED */
    766 int
    767 nfs_quotactl(mp, cmd, uid, arg, p)
    768 	struct mount *mp;
    769 	int cmd;
    770 	uid_t uid;
    771 	caddr_t arg;
    772 	struct proc *p;
    773 {
    774 
    775 	return (EOPNOTSUPP);
    776 }
    777