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