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