nfs_subs.c revision 171190
1139823Simp/*-
21541Srgrimes * Copyright (c) 1989, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * This code is derived from software contributed to Berkeley by
61541Srgrimes * Rick Macklem at The University of Guelph.
71541Srgrimes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer.
131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer in the
151541Srgrimes *    documentation and/or other materials provided with the distribution.
161541Srgrimes * 4. Neither the name of the University nor the names of its contributors
171541Srgrimes *    may be used to endorse or promote products derived from this software
181541Srgrimes *    without specific prior written permission.
191541Srgrimes *
201541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301541Srgrimes * SUCH DAMAGE.
311541Srgrimes *
3236503Speter *	@(#)nfs_subs.c  8.8 (Berkeley) 5/22/95
331541Srgrimes */
341541Srgrimes
3583651Speter#include <sys/cdefs.h>
3683651Speter__FBSDID("$FreeBSD: head/sys/nfsclient/nfs_subs.c 171190 2007-07-03 18:31:47Z jhb $");
3783651Speter
381541Srgrimes/*
391541Srgrimes * These functions support the macros and help fiddle mbuf chains for
401541Srgrimes * the nfs op functions. They do things like create the rpc header and
411541Srgrimes * copy data between mbuf chains and uio lists.
421541Srgrimes */
4383651Speter
441541Srgrimes#include <sys/param.h>
4548274Speter#include <sys/systm.h>
4648274Speter#include <sys/kernel.h>
4760041Sphk#include <sys/bio.h>
4831886Sbde#include <sys/buf.h>
491541Srgrimes#include <sys/proc.h>
501541Srgrimes#include <sys/mount.h>
511541Srgrimes#include <sys/vnode.h>
521541Srgrimes#include <sys/namei.h>
531541Srgrimes#include <sys/mbuf.h>
541541Srgrimes#include <sys/socket.h>
551541Srgrimes#include <sys/stat.h>
569336Sdfr#include <sys/malloc.h>
572997Swollman#include <sys/sysent.h>
582997Swollman#include <sys/syscall.h>
5983651Speter#include <sys/sysproto.h>
601541Srgrimes
613305Sphk#include <vm/vm.h>
6212662Sdg#include <vm/vm_object.h>
6312662Sdg#include <vm/vm_extern.h>
6492783Sjeff#include <vm/uma.h>
653305Sphk
66122698Salfred#include <rpc/rpcclnt.h>
67122698Salfred
681541Srgrimes#include <nfs/rpcv2.h>
699336Sdfr#include <nfs/nfsproto.h>
7083651Speter#include <nfsclient/nfs.h>
7183651Speter#include <nfsclient/nfsnode.h>
721541Srgrimes#include <nfs/xdr_subs.h>
7383651Speter#include <nfsclient/nfsm_subs.h>
7483651Speter#include <nfsclient/nfsmount.h>
751541Srgrimes
761541Srgrimes#include <netinet/in.h>
771541Srgrimes
781541Srgrimes/*
79158739Smohans * Note that stdarg.h and the ANSI style va_start macro is used for both
80158739Smohans * ANSI and traditional C compilers.
81158739Smohans */
82158739Smohans#include <machine/stdarg.h>
83158739Smohans
84158739Smohans/*
851541Srgrimes * Data items converted to xdr at startup, since they are constant
861541Srgrimes * This is kinda hokey, but may save a little time doing byte swaps
871541Srgrimes */
8883651Speteru_int32_t	nfs_xdrneg1;
8983651Speteru_int32_t	rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
9083651Speter		    rpc_mismatch, rpc_auth_unix, rpc_msgaccepted;
9183651Speteru_int32_t	nfs_true, nfs_false;
921541Srgrimes
931541Srgrimes/* And other global data */
94152652Sreesu_int32_t nfs_xid = 0;
9512911Sphkstatic enum vtype nv2tov_type[8]= {
9683651Speter	VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON,  VNON
9712911Sphk};
9812911Sphk
9983651Speterint		nfs_ticks;
10083651Speterint		nfs_pbuf_freecnt = -1;	/* start out unlimited */
1019336Sdfr
10283651Speterstruct nfs_reqq	nfs_reqq;
103138496Spsstruct mtx nfs_reqq_mtx;
10483651Speterstruct nfs_bufq	nfs_bufq;
1059759Sbde
1069336Sdfr/*
1079336Sdfr * and the reverse mapping from generic to Version 2 procedure numbers
1089336Sdfr */
1099336Sdfrint nfsv2_procid[NFS_NPROCS] = {
1109336Sdfr	NFSV2PROC_NULL,
1119336Sdfr	NFSV2PROC_GETATTR,
1129336Sdfr	NFSV2PROC_SETATTR,
1139336Sdfr	NFSV2PROC_LOOKUP,
1149336Sdfr	NFSV2PROC_NOOP,
1159336Sdfr	NFSV2PROC_READLINK,
1169336Sdfr	NFSV2PROC_READ,
1179336Sdfr	NFSV2PROC_WRITE,
1189336Sdfr	NFSV2PROC_CREATE,
1199336Sdfr	NFSV2PROC_MKDIR,
1209336Sdfr	NFSV2PROC_SYMLINK,
1219336Sdfr	NFSV2PROC_CREATE,
1229336Sdfr	NFSV2PROC_REMOVE,
1239336Sdfr	NFSV2PROC_RMDIR,
1249336Sdfr	NFSV2PROC_RENAME,
1259336Sdfr	NFSV2PROC_LINK,
1269336Sdfr	NFSV2PROC_READDIR,
1279336Sdfr	NFSV2PROC_NOOP,
1289336Sdfr	NFSV2PROC_STATFS,
1299336Sdfr	NFSV2PROC_NOOP,
1309336Sdfr	NFSV2PROC_NOOP,
1319336Sdfr	NFSV2PROC_NOOP,
1329336Sdfr	NFSV2PROC_NOOP,
1339336Sdfr};
1349336Sdfr
13560938SjakeLIST_HEAD(nfsnodehashhead, nfsnode);
1363664Sphk
1371541Srgrimes/*
1381541Srgrimes * Create the header for an rpc request packet
1391541Srgrimes * The hsiz is the size of the rest of the nfs request header.
1401541Srgrimes * (just used to decide if a cluster is a good idea)
1411541Srgrimes */
1421541Srgrimesstruct mbuf *
14383651Speternfsm_reqhead(struct vnode *vp, u_long procid, int hsiz)
1441541Srgrimes{
14583651Speter	struct mbuf *mb;
1461541Srgrimes
147111119Simp	MGET(mb, M_TRYWAIT, MT_DATA);
1481541Srgrimes	if (hsiz >= MINCLSIZE)
149111119Simp		MCLGET(mb, M_TRYWAIT);
1501541Srgrimes	mb->m_len = 0;
1511541Srgrimes	return (mb);
1521541Srgrimes}
1531541Srgrimes
1541541Srgrimes/*
1551541Srgrimes * Build the RPC header and fill in the authorization info.
1561541Srgrimes * The authorization string argument is only used when the credentials
1571541Srgrimes * come from outside of the kernel.
1581541Srgrimes * Returns the head of the mbuf list.
1591541Srgrimes */
1601541Srgrimesstruct mbuf *
16183651Speternfsm_rpchead(struct ucred *cr, int nmflag, int procid, int auth_type,
16283651Speter    int auth_len, struct mbuf *mrest, int mrest_len, struct mbuf **mbp,
163152652Srees    u_int32_t **xidpp)
1641541Srgrimes{
16583651Speter	struct mbuf *mb;
16683651Speter	u_int32_t *tl;
16783651Speter	caddr_t bpos;
16883651Speter	int i;
16983651Speter	struct mbuf *mreq;
17083651Speter	int grpsiz, authsiz;
1711541Srgrimes
1721541Srgrimes	authsiz = nfsm_rndup(auth_len);
173111119Simp	MGETHDR(mb, M_TRYWAIT, MT_DATA);
1749336Sdfr	if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
175111119Simp		MCLGET(mb, M_TRYWAIT);
1769336Sdfr	} else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
1779336Sdfr		MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
1781541Srgrimes	} else {
1799336Sdfr		MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
1801541Srgrimes	}
1811541Srgrimes	mb->m_len = 0;
1821541Srgrimes	mreq = mb;
1831541Srgrimes	bpos = mtod(mb, caddr_t);
1841541Srgrimes
1851541Srgrimes	/*
1861541Srgrimes	 * First the RPC header.
1871541Srgrimes	 */
18884002Speter	tl = nfsm_build(u_int32_t *, 8 * NFSX_UNSIGNED);
18917186Sdfr
190158739Smohans	mtx_lock(&nfs_reqq_mtx);
19135066Sphk	/* Get a pretty random xid to start with */
19283651Speter	if (!nfs_xid)
19335066Sphk		nfs_xid = random();
19417186Sdfr	/*
19517186Sdfr	 * Skip zero xid if it should ever happen.
19617186Sdfr	 */
1971541Srgrimes	if (++nfs_xid == 0)
1981541Srgrimes		nfs_xid++;
19917186Sdfr
200152652Srees	*xidpp = tl;
201152652Srees	*tl++ = txdr_unsigned(nfs_xid);
202158739Smohans	mtx_unlock(&nfs_reqq_mtx);
2031541Srgrimes	*tl++ = rpc_call;
2041541Srgrimes	*tl++ = rpc_vers;
20583651Speter	*tl++ = txdr_unsigned(NFS_PROG);
20683651Speter	if (nmflag & NFSMNT_NFSV3) {
20783651Speter		*tl++ = txdr_unsigned(NFS_VER3);
20883651Speter		*tl++ = txdr_unsigned(procid);
2091541Srgrimes	} else {
21083651Speter		*tl++ = txdr_unsigned(NFS_VER2);
21183651Speter		*tl++ = txdr_unsigned(nfsv2_procid[procid]);
2121541Srgrimes	}
2131541Srgrimes
2141541Srgrimes	/*
2151541Srgrimes	 * And then the authorization cred.
2161541Srgrimes	 */
2171541Srgrimes	*tl++ = txdr_unsigned(auth_type);
2181541Srgrimes	*tl = txdr_unsigned(authsiz);
2191541Srgrimes	switch (auth_type) {
2201541Srgrimes	case RPCAUTH_UNIX:
22184002Speter		tl = nfsm_build(u_int32_t *, auth_len);
2221541Srgrimes		*tl++ = 0;		/* stamp ?? */
2231541Srgrimes		*tl++ = 0;		/* NULL hostname */
2241541Srgrimes		*tl++ = txdr_unsigned(cr->cr_uid);
2251541Srgrimes		*tl++ = txdr_unsigned(cr->cr_groups[0]);
2261541Srgrimes		grpsiz = (auth_len >> 2) - 5;
2271541Srgrimes		*tl++ = txdr_unsigned(grpsiz);
2281541Srgrimes		for (i = 1; i <= grpsiz; i++)
2291541Srgrimes			*tl++ = txdr_unsigned(cr->cr_groups[i]);
2301541Srgrimes		break;
23183651Speter	}
2329336Sdfr
2339336Sdfr	/*
2349336Sdfr	 * And the verifier...
2359336Sdfr	 */
23684002Speter	tl = nfsm_build(u_int32_t *, 2 * NFSX_UNSIGNED);
23783651Speter	*tl++ = txdr_unsigned(RPCAUTH_NULL);
23883651Speter	*tl = 0;
2391541Srgrimes	mb->m_next = mrest;
2409336Sdfr	mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
24199797Sdillon	mreq->m_pkthdr.rcvif = NULL;
2421541Srgrimes	*mbp = mb;
2431541Srgrimes	return (mreq);
2441541Srgrimes}
2451541Srgrimes
2461541Srgrimes/*
24717186Sdfr * copies a uio scatter/gather list to an mbuf chain.
24817186Sdfr * NOTE: can ony handle iovcnt == 1
2491541Srgrimes */
2501549Srgrimesint
25183651Speternfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
2521541Srgrimes{
25383651Speter	char *uiocp;
25483651Speter	struct mbuf *mp, *mp2;
25583651Speter	int xfer, left, mlen;
2561541Srgrimes	int uiosiz, clflg, rem;
2571541Srgrimes	char *cp;
2581541Srgrimes
25936519Speter#ifdef DIAGNOSTIC
26017186Sdfr	if (uiop->uio_iovcnt != 1)
26117186Sdfr		panic("nfsm_uiotombuf: iovcnt != 1");
26236519Speter#endif
26317186Sdfr
2641541Srgrimes	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
2651541Srgrimes		clflg = 1;
2661541Srgrimes	else
2671541Srgrimes		clflg = 0;
2681541Srgrimes	rem = nfsm_rndup(siz)-siz;
2691541Srgrimes	mp = mp2 = *mq;
2701541Srgrimes	while (siz > 0) {
2711541Srgrimes		left = uiop->uio_iov->iov_len;
2721541Srgrimes		uiocp = uiop->uio_iov->iov_base;
2731541Srgrimes		if (left > siz)
2741541Srgrimes			left = siz;
2751541Srgrimes		uiosiz = left;
2761541Srgrimes		while (left > 0) {
2771541Srgrimes			mlen = M_TRAILINGSPACE(mp);
2781541Srgrimes			if (mlen == 0) {
279111119Simp				MGET(mp, M_TRYWAIT, MT_DATA);
2801541Srgrimes				if (clflg)
281111119Simp					MCLGET(mp, M_TRYWAIT);
2821541Srgrimes				mp->m_len = 0;
2831541Srgrimes				mp2->m_next = mp;
2841541Srgrimes				mp2 = mp;
2851541Srgrimes				mlen = M_TRAILINGSPACE(mp);
2861541Srgrimes			}
2871541Srgrimes			xfer = (left > mlen) ? mlen : left;
2881541Srgrimes#ifdef notdef
2891541Srgrimes			/* Not Yet.. */
2901541Srgrimes			if (uiop->uio_iov->iov_op != NULL)
2911541Srgrimes				(*(uiop->uio_iov->iov_op))
2921541Srgrimes				(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
2931541Srgrimes			else
2941541Srgrimes#endif
2951541Srgrimes			if (uiop->uio_segflg == UIO_SYSSPACE)
2961541Srgrimes				bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
2971541Srgrimes			else
2981541Srgrimes				copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
2991541Srgrimes			mp->m_len += xfer;
3001541Srgrimes			left -= xfer;
3011541Srgrimes			uiocp += xfer;
3021541Srgrimes			uiop->uio_offset += xfer;
3031541Srgrimes			uiop->uio_resid -= xfer;
3041541Srgrimes		}
305104908Smike		uiop->uio_iov->iov_base =
306104908Smike		    (char *)uiop->uio_iov->iov_base + uiosiz;
30717186Sdfr		uiop->uio_iov->iov_len -= uiosiz;
3081541Srgrimes		siz -= uiosiz;
3091541Srgrimes	}
3101541Srgrimes	if (rem > 0) {
3111541Srgrimes		if (rem > M_TRAILINGSPACE(mp)) {
312111119Simp			MGET(mp, M_TRYWAIT, MT_DATA);
3131541Srgrimes			mp->m_len = 0;
3141541Srgrimes			mp2->m_next = mp;
3151541Srgrimes		}
3161541Srgrimes		cp = mtod(mp, caddr_t)+mp->m_len;
3171541Srgrimes		for (left = 0; left < rem; left++)
3181541Srgrimes			*cp++ = '\0';
3191541Srgrimes		mp->m_len += rem;
3201541Srgrimes		*bpos = cp;
3211541Srgrimes	} else
3221541Srgrimes		*bpos = mtod(mp, caddr_t)+mp->m_len;
3231541Srgrimes	*mq = mp;
3241541Srgrimes	return (0);
3251541Srgrimes}
3261541Srgrimes
3271541Srgrimes/*
3281541Srgrimes * Copy a string into mbufs for the hard cases...
3291541Srgrimes */
3301549Srgrimesint
33183651Speternfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
3321541Srgrimes{
33383651Speter	struct mbuf *m1 = NULL, *m2;
3341541Srgrimes	long left, xfer, len, tlen;
33536541Speter	u_int32_t *tl;
3361541Srgrimes	int putsize;
3371541Srgrimes
3381541Srgrimes	putsize = 1;
3391541Srgrimes	m2 = *mb;
3401541Srgrimes	left = M_TRAILINGSPACE(m2);
3411541Srgrimes	if (left > 0) {
34236541Speter		tl = ((u_int32_t *)(*bpos));
3431541Srgrimes		*tl++ = txdr_unsigned(siz);
3441541Srgrimes		putsize = 0;
3451541Srgrimes		left -= NFSX_UNSIGNED;
3461541Srgrimes		m2->m_len += NFSX_UNSIGNED;
3471541Srgrimes		if (left > 0) {
3481541Srgrimes			bcopy(cp, (caddr_t) tl, left);
3491541Srgrimes			siz -= left;
3501541Srgrimes			cp += left;
3511541Srgrimes			m2->m_len += left;
3521541Srgrimes			left = 0;
3531541Srgrimes		}
3541541Srgrimes	}
3551541Srgrimes	/* Loop around adding mbufs */
3561541Srgrimes	while (siz > 0) {
357111119Simp		MGET(m1, M_TRYWAIT, MT_DATA);
3581541Srgrimes		if (siz > MLEN)
359111119Simp			MCLGET(m1, M_TRYWAIT);
3601541Srgrimes		m1->m_len = NFSMSIZ(m1);
3611541Srgrimes		m2->m_next = m1;
3621541Srgrimes		m2 = m1;
36336541Speter		tl = mtod(m1, u_int32_t *);
3641541Srgrimes		tlen = 0;
3651541Srgrimes		if (putsize) {
3661541Srgrimes			*tl++ = txdr_unsigned(siz);
3671541Srgrimes			m1->m_len -= NFSX_UNSIGNED;
3681541Srgrimes			tlen = NFSX_UNSIGNED;
3691541Srgrimes			putsize = 0;
3701541Srgrimes		}
3711541Srgrimes		if (siz < m1->m_len) {
3721541Srgrimes			len = nfsm_rndup(siz);
3731541Srgrimes			xfer = siz;
3741541Srgrimes			if (xfer < len)
3751541Srgrimes				*(tl+(xfer>>2)) = 0;
3761541Srgrimes		} else {
3771541Srgrimes			xfer = len = m1->m_len;
3781541Srgrimes		}
3791541Srgrimes		bcopy(cp, (caddr_t) tl, xfer);
3801541Srgrimes		m1->m_len = len+tlen;
3811541Srgrimes		siz -= xfer;
3821541Srgrimes		cp += xfer;
3831541Srgrimes	}
3841541Srgrimes	*mb = m1;
3851541Srgrimes	*bpos = mtod(m1, caddr_t)+m1->m_len;
3861541Srgrimes	return (0);
3871541Srgrimes}
3881541Srgrimes
3891541Srgrimes/*
3901541Srgrimes * Called once to initialize data structures...
3911541Srgrimes */
3921549Srgrimesint
39383651Speternfs_init(struct vfsconf *vfsp)
3941541Srgrimes{
39583651Speter	int i;
3961541Srgrimes
39792783Sjeff	nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount),
39892783Sjeff	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3991541Srgrimes	rpc_vers = txdr_unsigned(RPC_VER2);
4001541Srgrimes	rpc_call = txdr_unsigned(RPC_CALL);
4011541Srgrimes	rpc_reply = txdr_unsigned(RPC_REPLY);
4021541Srgrimes	rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
4031541Srgrimes	rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
4041541Srgrimes	rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
4051541Srgrimes	rpc_autherr = txdr_unsigned(RPC_AUTHERR);
4061541Srgrimes	rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
4071541Srgrimes	nfs_true = txdr_unsigned(TRUE);
4081541Srgrimes	nfs_false = txdr_unsigned(FALSE);
4093664Sphk	nfs_xdrneg1 = txdr_unsigned(-1);
4109336Sdfr	nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
4119336Sdfr	if (nfs_ticks < 1)
4129336Sdfr		nfs_ticks = 1;
4131541Srgrimes	/* Ensure async daemons disabled */
41419449Sdfr	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
41599797Sdillon		nfs_iodwant[i] = NULL;
41699797Sdillon		nfs_iodmount[i] = NULL;
41719449Sdfr	}
4181541Srgrimes	nfs_nhinit();			/* Init the nfsnode table */
4191541Srgrimes
4201541Srgrimes	/*
4211541Srgrimes	 * Initialize reply list and start timer
4221541Srgrimes	 */
4233664Sphk	TAILQ_INIT(&nfs_reqq);
424148162Sps	callout_init(&nfs_callout, CALLOUT_MPSAFE);
425138496Sps	mtx_init(&nfs_reqq_mtx, "NFS reqq lock", NULL, MTX_DEF);
426158739Smohans	mtx_init(&nfs_iod_mtx, "NFS iod lock", NULL, MTX_DEF);
42716365Sphk
42842957Sdillon	nfs_pbuf_freecnt = nswbuf / 2 + 1;
42942957Sdillon
4301549Srgrimes	return (0);
4311541Srgrimes}
4321541Srgrimes
43338894Sbdeint
43483651Speternfs_uninit(struct vfsconf *vfsp)
43538894Sbde{
436128111Speadar	int i;
43738894Sbde
438127421Srees	callout_stop(&nfs_callout);
439128111Speadar
440128126Smarcel	KASSERT(TAILQ_EMPTY(&nfs_reqq),
441128111Speadar	    ("nfs_uninit: request queue not empty"));
442128111Speadar
443128111Speadar	/*
444128111Speadar	 * Tell all nfsiod processes to exit. Clear nfs_iodmax, and wakeup
445128111Speadar	 * any sleeping nfsiods so they check nfs_iodmax and exit.
446128111Speadar	 */
447158739Smohans	mtx_lock(&nfs_iod_mtx);
448128111Speadar	nfs_iodmax = 0;
449128111Speadar	for (i = 0; i < nfs_numasync; i++)
450128111Speadar		if (nfs_iodwant[i])
451128111Speadar			wakeup(&nfs_iodwant[i]);
452128111Speadar	/* The last nfsiod to exit will wake us up when nfs_numasync hits 0 */
453128111Speadar	while (nfs_numasync)
454158739Smohans		msleep(&nfs_numasync, &nfs_iod_mtx, PWAIT, "ioddie", 0);
455158739Smohans	mtx_unlock(&nfs_iod_mtx);
456128111Speadar	nfs_nhuninit();
457128111Speadar	uma_zdestroy(nfsmount_zone);
45838894Sbde	return (0);
45938894Sbde}
46038894Sbde
461158739Smohansvoid
462158739Smohansnfs_dircookie_lock(struct nfsnode *np)
463158739Smohans{
464158739Smohans	mtx_lock(&np->n_mtx);
465158739Smohans	while (np->n_flag & NDIRCOOKIELK)
466158739Smohans		(void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
467158739Smohans	np->n_flag |= NDIRCOOKIELK;
468158739Smohans	mtx_unlock(&np->n_mtx);
469158739Smohans}
470158739Smohans
471158739Smohansvoid
472158739Smohansnfs_dircookie_unlock(struct nfsnode *np)
473158739Smohans{
474158739Smohans	mtx_lock(&np->n_mtx);
475158739Smohans	np->n_flag &= ~NDIRCOOKIELK;
476158739Smohans	wakeup(&np->n_flag);
477158739Smohans	mtx_unlock(&np->n_mtx);
478158739Smohans}
479158739Smohans
480158739Smohansint
481158739Smohansnfs_upgrade_vnlock(struct vnode *vp, struct thread *td)
482158739Smohans{
483158739Smohans	int old_lock;
484158739Smohans
485158739Smohans 	if ((old_lock = VOP_ISLOCKED(vp, td)) != LK_EXCLUSIVE) {
486158739Smohans 		if (old_lock == LK_SHARED) {
487158739Smohans 			/* Upgrade to exclusive lock, this might block */
488158739Smohans 			vn_lock(vp, LK_UPGRADE | LK_RETRY, td);
489158739Smohans 		} else {
490158739Smohans 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
491158739Smohans 		}
492158739Smohans  	}
493158739Smohans	return old_lock;
494158739Smohans}
495158739Smohans
496158739Smohansvoid
497158739Smohansnfs_downgrade_vnlock(struct vnode *vp, struct thread *td, int old_lock)
498158739Smohans{
499158739Smohans	if (old_lock != LK_EXCLUSIVE) {
500158739Smohans 		if (old_lock == LK_SHARED) {
501158739Smohans 			/* Downgrade from exclusive lock, this might block */
502158739Smohans 			vn_lock(vp, LK_DOWNGRADE, td);
503158739Smohans 		} else {
504158739Smohans 			VOP_UNLOCK(vp, 0, td);
505158739Smohans 		}
506158739Smohans  	}
507158739Smohans}
508158739Smohans
509158739Smohansvoid
510158739Smohansnfs_printf(const char *fmt, ...)
511158739Smohans{
512158739Smohans	va_list ap;
513158739Smohans
514158739Smohans	mtx_lock(&Giant);
515158739Smohans	va_start(ap, fmt);
516158739Smohans	printf(fmt, ap);
517158739Smohans	va_end(ap);
518158739Smohans	mtx_unlock(&Giant);
519158739Smohans}
520158739Smohans
5211541Srgrimes/*
5221541Srgrimes * Attribute cache routines.
5231541Srgrimes * nfs_loadattrcache() - loads or updates the cache contents from attributes
5241541Srgrimes *	that are on the mbuf list
5251541Srgrimes * nfs_getattrcache() - returns valid attributes if found in cache, returns
5261541Srgrimes *	error otherwise
5271541Srgrimes */
5281541Srgrimes
5291541Srgrimes/*
5301541Srgrimes * Load the attribute cache (that lives in the nfsnode entry) with
5311541Srgrimes * the values on the mbuf list and
5321541Srgrimes * Iff vap not NULL
5331541Srgrimes *    copy the attributes to *vaper
5341541Srgrimes */
5351549Srgrimesint
53683651Speternfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
537158739Smohans		  struct vattr *vaper, int dontshrink)
5381541Srgrimes{
53983651Speter	struct vnode *vp = *vpp;
54083651Speter	struct vattr *vap;
54183651Speter	struct nfs_fattr *fp;
54283651Speter	struct nfsnode *np;
54383651Speter	int32_t t1;
5449336Sdfr	caddr_t cp2;
54584057Speter	int rdev;
5461541Srgrimes	struct mbuf *md;
5471541Srgrimes	enum vtype vtyp;
5481541Srgrimes	u_short vmode;
549171190Sjhb	struct timespec mtime, mtime_save;
5509336Sdfr	int v3 = NFS_ISV3(vp);
551167352Smohans	struct thread *td = curthread;
5521541Srgrimes
5531541Srgrimes	md = *mdp;
5549336Sdfr	t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
555138463Sps	cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, M_TRYWAIT);
55684057Speter	if (cp2 == NULL)
55784057Speter		return EBADRPC;
5589336Sdfr	fp = (struct nfs_fattr *)cp2;
5599336Sdfr	if (v3) {
5609336Sdfr		vtyp = nfsv3tov_type(fp->fa_type);
5619336Sdfr		vmode = fxdr_unsigned(u_short, fp->fa_mode);
562130640Sphk		rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
56316634Sbde			fxdr_unsigned(int, fp->fa3_rdev.specdata2));
5649336Sdfr		fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
5651541Srgrimes	} else {
5669336Sdfr		vtyp = nfsv2tov_type(fp->fa_type);
5679336Sdfr		vmode = fxdr_unsigned(u_short, fp->fa_mode);
5689336Sdfr		/*
5699336Sdfr		 * XXX
5709336Sdfr		 *
5719336Sdfr		 * The duplicate information returned in fa_type and fa_mode
5729336Sdfr		 * is an ambiguity in the NFS version 2 protocol.
5739336Sdfr		 *
5749336Sdfr		 * VREG should be taken literally as a regular file.  If a
5759336Sdfr		 * server intents to return some type information differently
5769336Sdfr		 * in the upper bits of the mode field (e.g. for sockets, or
5779336Sdfr		 * FIFOs), NFSv2 mandates fa_type to be VNON.  Anyway, we
5789336Sdfr		 * leave the examination of the mode bits even in the VREG
5799336Sdfr		 * case to avoid breakage for bogus servers, but we make sure
5809336Sdfr		 * that there are actually type bits set in the upper part of
5819336Sdfr		 * fa_mode (and failing that, trust the va_type field).
5829336Sdfr		 *
5839336Sdfr		 * NFSv3 cleared the issue, and requires fa_mode to not
5849336Sdfr		 * contain any type information (while also introduing sockets
5859336Sdfr		 * and FIFOs for fa_type).
5869336Sdfr		 */
5879336Sdfr		if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
5889336Sdfr			vtyp = IFTOVT(vmode);
58936541Speter		rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
5909336Sdfr		fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
5919336Sdfr
5929336Sdfr		/*
5939336Sdfr		 * Really ugly NFSv2 kludge.
5949336Sdfr		 */
5959336Sdfr		if (vtyp == VCHR && rdev == 0xffffffff)
5969336Sdfr			vtyp = VFIFO;
5971541Srgrimes	}
5989336Sdfr
5991541Srgrimes	/*
6001541Srgrimes	 * If v_type == VNON it is a new node, so fill in the v_type,
6018876Srgrimes	 * n_mtime fields. Check to see if it represents a special
6021541Srgrimes	 * device, and if so, check for a possible alias. Once the
6031541Srgrimes	 * correct vnode has been obtained, fill in the rest of the
6041541Srgrimes	 * information.
6051541Srgrimes	 */
6061541Srgrimes	np = VTONFS(vp);
607158739Smohans	mtx_lock(&np->n_mtx);
60810219Sdfr	if (vp->v_type != vtyp) {
6099336Sdfr		vp->v_type = vtyp;
610126851Sphk		if (vp->v_type == VFIFO)
611138290Sphk			vp->v_op = &nfs_fifoops;
612138473Sps		np->n_mtime = mtime;
6131541Srgrimes	}
6141541Srgrimes	vap = &np->n_vattr;
6151541Srgrimes	vap->va_type = vtyp;
6161541Srgrimes	vap->va_mode = (vmode & 07777);
61747028Sphk	vap->va_rdev = rdev;
618171190Sjhb	mtime_save = vap->va_mtime;
6191541Srgrimes	vap->va_mtime = mtime;
6201541Srgrimes	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
6219336Sdfr	if (v3) {
6229336Sdfr		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
6239336Sdfr		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
6249336Sdfr		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
62547751Speter		vap->va_size = fxdr_hyper(&fp->fa3_size);
6269336Sdfr		vap->va_blocksize = NFS_FABLKSIZE;
62747751Speter		vap->va_bytes = fxdr_hyper(&fp->fa3_used);
62836541Speter		vap->va_fileid = fxdr_unsigned(int32_t,
62936541Speter		    fp->fa3_fileid.nfsuquad[1]);
6309336Sdfr		fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
6319336Sdfr		fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
6329336Sdfr		vap->va_flags = 0;
6339336Sdfr		vap->va_filerev = 0;
6341541Srgrimes	} else {
6359336Sdfr		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
6369336Sdfr		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
6379336Sdfr		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
63836541Speter		vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
63936541Speter		vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
64047751Speter		vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
64136541Speter		    * NFS_FABLKSIZE;
64236541Speter		vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
6439336Sdfr		fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
6441541Srgrimes		vap->va_flags = 0;
64536541Speter		vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
64636541Speter		    fp->fa2_ctime.nfsv2_sec);
64718397Snate		vap->va_ctime.tv_nsec = 0;
64883651Speter		vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec);
6491541Srgrimes		vap->va_filerev = 0;
6501541Srgrimes	}
65167486Sdwmalone	np->n_attrstamp = time_second;
652167352Smohans	/* Timestamp the NFS otw getattr fetch */
653167352Smohans	if (td->td_proc) {
654167352Smohans		np->n_ac_ts_tid = td->td_tid;
655167352Smohans		np->n_ac_ts_pid = td->td_proc->p_pid;
656167352Smohans		np->n_ac_ts_syscalls = td->td_syscalls;
657167352Smohans	} else
658167352Smohans		bzero(&np->n_ac_ts, sizeof(struct nfs_attrcache_timestamp));
659167352Smohans
6601541Srgrimes	if (vap->va_size != np->n_size) {
6611541Srgrimes		if (vap->va_type == VREG) {
66267486Sdwmalone			if (dontshrink && vap->va_size < np->n_size) {
66367486Sdwmalone				/*
66467486Sdwmalone				 * We've been told not to shrink the file;
66567486Sdwmalone				 * zero np->n_attrstamp to indicate that
66667486Sdwmalone				 * the attributes are stale.
66767486Sdwmalone				 */
66867486Sdwmalone				vap->va_size = np->n_size;
66967486Sdwmalone				np->n_attrstamp = 0;
67067486Sdwmalone			} else if (np->n_flag & NMODIFIED) {
671128263Speadar				/*
672128263Speadar				 * We've modified the file: Use the larger
673128263Speadar				 * of our size, and the server's size.
674128263Speadar				 */
675128263Speadar				if (vap->va_size < np->n_size) {
6761541Srgrimes					vap->va_size = np->n_size;
677128263Speadar				} else {
6781541Srgrimes					np->n_size = vap->va_size;
679128263Speadar					np->n_flag |= NSIZECHANGED;
680128263Speadar				}
68154480Sdillon			} else {
6821541Srgrimes				np->n_size = vap->va_size;
683128263Speadar				np->n_flag |= NSIZECHANGED;
68454480Sdillon			}
68541026Speter			vnode_pager_setsize(vp, np->n_size);
68654480Sdillon		} else {
6871541Srgrimes			np->n_size = vap->va_size;
68854480Sdillon		}
6891541Srgrimes	}
690171190Sjhb	/*
691171190Sjhb	 * The following checks are added to prevent a race between (say)
692171190Sjhb	 * a READDIR+ and a WRITE.
693171190Sjhb	 * READDIR+, WRITE requests sent out.
694171190Sjhb	 * READDIR+ resp, WRITE resp received on client.
695171190Sjhb	 * However, the WRITE resp was handled before the READDIR+ resp
696171190Sjhb	 * causing the post op attrs from the write to be loaded first
697171190Sjhb	 * and the attrs from the READDIR+ to be loaded later. If this
698171190Sjhb	 * happens, we have stale attrs loaded into the attrcache.
699171190Sjhb	 * We detect this by for the mtime moving back. We invalidate the
700171190Sjhb	 * attrcache when this happens.
701171190Sjhb	 */
702171190Sjhb	if (timespeccmp(&mtime_save, &vap->va_mtime, >))
703171190Sjhb		/* Size changed or mtime went backwards */
704171190Sjhb		np->n_attrstamp = 0;
7051541Srgrimes	if (vaper != NULL) {
7061541Srgrimes		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
7071541Srgrimes		if (np->n_flag & NCHG) {
7089336Sdfr			if (np->n_flag & NACC)
7099336Sdfr				vaper->va_atime = np->n_atim;
7109336Sdfr			if (np->n_flag & NUPD)
7119336Sdfr				vaper->va_mtime = np->n_mtim;
7121541Srgrimes		}
7131541Srgrimes	}
714158739Smohans	mtx_unlock(&np->n_mtx);
7151541Srgrimes	return (0);
7161541Srgrimes}
7171541Srgrimes
71836176Speter#ifdef NFS_ACDEBUG
71936176Speter#include <sys/sysctl.h>
72044101SbdeSYSCTL_DECL(_vfs_nfs);
72136176Speterstatic int nfs_acdebug;
72236176SpeterSYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
72336176Speter#endif
72436176Speter
7251541Srgrimes/*
7261541Srgrimes * Check the time stamp
7271541Srgrimes * If the cache is valid, copy contents to *vap and return 0
7281541Srgrimes * otherwise return an error
7291541Srgrimes */
7301549Srgrimesint
73183651Speternfs_getattrcache(struct vnode *vp, struct vattr *vaper)
7321541Srgrimes{
73383651Speter	struct nfsnode *np;
73483651Speter	struct vattr *vap;
73536176Speter	struct nfsmount *nmp;
73636176Speter	int timeo;
737158739Smohans
73836176Speter	np = VTONFS(vp);
73936176Speter	vap = &np->n_vattr;
74036176Speter	nmp = VFSTONFS(vp->v_mount);
741158739Smohans#ifdef NFS_ACDEBUG
742158739Smohans	mtx_lock(&Giant);	/* nfs_printf() */
743158739Smohans#endif
744158739Smohans	mtx_lock(&np->n_mtx);
74536176Speter	/* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
746138473Sps	timeo = (time_second - np->n_mtime.tv_sec) / 10;
74736176Speter
74836176Speter#ifdef NFS_ACDEBUG
74936176Speter	if (nfs_acdebug>1)
750158739Smohans		nfs_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
75136176Speter#endif
75236176Speter
75336176Speter	if (vap->va_type == VDIR) {
75436176Speter		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
75536176Speter			timeo = nmp->nm_acdirmin;
75636176Speter		else if (timeo > nmp->nm_acdirmax)
75736176Speter			timeo = nmp->nm_acdirmax;
75836176Speter	} else {
75936176Speter		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
76036176Speter			timeo = nmp->nm_acregmin;
76136176Speter		else if (timeo > nmp->nm_acregmax)
76236176Speter			timeo = nmp->nm_acregmax;
76336176Speter	}
76436176Speter
76536176Speter#ifdef NFS_ACDEBUG
76636176Speter	if (nfs_acdebug > 2)
767158739Smohans		nfs_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
768158739Smohans			   nmp->nm_acregmin, nmp->nm_acregmax,
769158739Smohans			   nmp->nm_acdirmin, nmp->nm_acdirmax);
77036176Speter
77136176Speter	if (nfs_acdebug)
772158739Smohans		nfs_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
773158739Smohans			   (time_second - np->n_attrstamp), timeo);
77436176Speter#endif
77536176Speter
77636176Speter	if ((time_second - np->n_attrstamp) >= timeo) {
7771541Srgrimes		nfsstats.attrcache_misses++;
778158739Smohans		mtx_unlock(&np->n_mtx);
779158739Smohans		return( ENOENT);
7801541Srgrimes	}
7811541Srgrimes	nfsstats.attrcache_hits++;
7821541Srgrimes	if (vap->va_size != np->n_size) {
7831541Srgrimes		if (vap->va_type == VREG) {
7841541Srgrimes			if (np->n_flag & NMODIFIED) {
7851541Srgrimes				if (vap->va_size < np->n_size)
7861541Srgrimes					vap->va_size = np->n_size;
7871541Srgrimes				else
7881541Srgrimes					np->n_size = vap->va_size;
78954480Sdillon			} else {
7901541Srgrimes				np->n_size = vap->va_size;
79154480Sdillon			}
79241026Speter			vnode_pager_setsize(vp, np->n_size);
79354480Sdillon		} else {
7941541Srgrimes			np->n_size = vap->va_size;
79554480Sdillon		}
7961541Srgrimes	}
7971541Srgrimes	bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
7981541Srgrimes	if (np->n_flag & NCHG) {
7999336Sdfr		if (np->n_flag & NACC)
8009336Sdfr			vaper->va_atime = np->n_atim;
8019336Sdfr		if (np->n_flag & NUPD)
8029336Sdfr			vaper->va_mtime = np->n_mtim;
8031541Srgrimes	}
804158739Smohans	mtx_unlock(&np->n_mtx);
805158739Smohans#ifdef NFS_ACDEBUG
806158739Smohans	mtx_unlock(&Giant);	/* nfs_printf() */
807158739Smohans#endif
8081541Srgrimes	return (0);
8091541Srgrimes}
8101541Srgrimes
81143305Sdillonstatic nfsuint64 nfs_nullcookie = { { 0, 0 } };
8129336Sdfr/*
8139336Sdfr * This function finds the directory cookie that corresponds to the
8149336Sdfr * logical byte offset given.
8159336Sdfr */
8169336Sdfrnfsuint64 *
81783651Speternfs_getcookie(struct nfsnode *np, off_t off, int add)
8189336Sdfr{
81983651Speter	struct nfsdmap *dp, *dp2;
82083651Speter	int pos;
821158739Smohans	nfsuint64 *retval = NULL;
822158739Smohans
82336979Sbde	pos = (uoff_t)off / NFS_DIRBLKSIZ;
82436979Sbde	if (pos == 0 || off < 0) {
8259336Sdfr#ifdef DIAGNOSTIC
8269336Sdfr		if (add)
82736979Sbde			panic("nfs getcookie add at <= 0");
8289336Sdfr#endif
8299336Sdfr		return (&nfs_nullcookie);
8309336Sdfr	}
8319336Sdfr	pos--;
83283651Speter	dp = LIST_FIRST(&np->n_cookies);
8339336Sdfr	if (!dp) {
8349336Sdfr		if (add) {
8359336Sdfr			MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
836111119Simp				M_NFSDIROFF, M_WAITOK);
8379336Sdfr			dp->ndm_eocookie = 0;
8389336Sdfr			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
8399336Sdfr		} else
840158739Smohans			goto out;
8419336Sdfr	}
8429336Sdfr	while (pos >= NFSNUMCOOKIES) {
8439336Sdfr		pos -= NFSNUMCOOKIES;
84483651Speter		if (LIST_NEXT(dp, ndm_list)) {
8459336Sdfr			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
846158739Smohans			    pos >= dp->ndm_eocookie)
847158739Smohans				goto out;
84883651Speter			dp = LIST_NEXT(dp, ndm_list);
8499336Sdfr		} else if (add) {
8509336Sdfr			MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
851111119Simp				M_NFSDIROFF, M_WAITOK);
8529336Sdfr			dp2->ndm_eocookie = 0;
8539336Sdfr			LIST_INSERT_AFTER(dp, dp2, ndm_list);
8549336Sdfr			dp = dp2;
8559336Sdfr		} else
856158739Smohans			goto out;
8579336Sdfr	}
8589336Sdfr	if (pos >= dp->ndm_eocookie) {
8599336Sdfr		if (add)
8609336Sdfr			dp->ndm_eocookie = pos + 1;
8619336Sdfr		else
862158739Smohans			goto out;
8639336Sdfr	}
864158739Smohans	retval = &dp->ndm_cookies[pos];
865158739Smohansout:
866158739Smohans	return (retval);
8679336Sdfr}
8689336Sdfr
8699336Sdfr/*
8709336Sdfr * Invalidate cached directory information, except for the actual directory
8719336Sdfr * blocks (which are invalidated separately).
8729336Sdfr * Done mainly to avoid the use of stale offset cookies.
8739336Sdfr */
8749336Sdfrvoid
87583651Speternfs_invaldir(struct vnode *vp)
8769336Sdfr{
87783651Speter	struct nfsnode *np = VTONFS(vp);
8789336Sdfr
8799336Sdfr#ifdef DIAGNOSTIC
8809336Sdfr	if (vp->v_type != VDIR)
8819336Sdfr		panic("nfs: invaldir not dir");
8829336Sdfr#endif
883158739Smohans	nfs_dircookie_lock(np);
8849336Sdfr	np->n_direofoffset = 0;
8859336Sdfr	np->n_cookieverf.nfsuquad[0] = 0;
8869336Sdfr	np->n_cookieverf.nfsuquad[1] = 0;
88783651Speter	if (LIST_FIRST(&np->n_cookies))
88883651Speter		LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
889158739Smohans	nfs_dircookie_unlock(np);
8909336Sdfr}
8919336Sdfr
8929336Sdfr/*
8939336Sdfr * The write verifier has changed (probably due to a server reboot), so all
8949336Sdfr * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
8959336Sdfr * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
89654480Sdillon * and B_CLUSTEROK flags.  Once done the new write verifier can be set for the
89754480Sdillon * mount point.
89854480Sdillon *
89983651Speter * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
90054480Sdillon * writes are not clusterable.
9019336Sdfr */
9029336Sdfrvoid
90383651Speternfs_clearcommit(struct mount *mp)
9049336Sdfr{
90583651Speter	struct vnode *vp, *nvp;
90683651Speter	struct buf *bp, *nbp;
9079336Sdfr	int s;
9089336Sdfr
9099336Sdfr	s = splbio();
910122091Skan	MNT_ILOCK(mp);
911131551Sphk	MNT_VNODE_FOREACH(vp, mp, nvp) {
912103939Sjeff		VI_LOCK(vp);
913143510Sjeff		if (vp->v_iflag & VI_DOOMED) {
914120787Sjeff			VI_UNLOCK(vp);
915120787Sjeff			continue;
916120787Sjeff		}
917122091Skan		MNT_IUNLOCK(mp);
918136767Sphk		TAILQ_FOREACH_SAFE(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs, nbp) {
91948225Smckusick			if (BUF_REFCNT(bp) == 0 &&
92048225Smckusick			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
9219336Sdfr				== (B_DELWRI | B_NEEDCOMMIT))
92254480Sdillon				bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
9239336Sdfr		}
924103939Sjeff		VI_UNLOCK(vp);
925122091Skan		MNT_ILOCK(mp);
9269336Sdfr	}
927122091Skan	MNT_IUNLOCK(mp);
9289336Sdfr	splx(s);
9299336Sdfr}
9309336Sdfr
9319336Sdfr/*
93283651Speter * Helper functions for former macros.  Some of these should be
93383651Speter * moved to their callers.
9349336Sdfr */
93583651Speter
9365455Sdgint
93783651Speternfsm_mtofh_xx(struct vnode *d, struct vnode **v, int v3, int *f,
93888091Siedowse    struct mbuf **md, caddr_t *dpos)
9399336Sdfr{
94083651Speter	struct nfsnode *ttnp;
94183651Speter	struct vnode *ttvp;
94283651Speter	nfsfh_t *ttfhp;
94388091Siedowse	u_int32_t *tl;
94483651Speter	int ttfhsize;
94583651Speter	int t1;
9469336Sdfr
94783651Speter	if (v3) {
94888091Siedowse		tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
94988091Siedowse		if (tl == NULL)
95084057Speter			return EBADRPC;
95188091Siedowse		*f = fxdr_unsigned(int, *tl);
95283651Speter	} else
95383651Speter		*f = 1;
95483651Speter	if (*f) {
95588091Siedowse		t1 = nfsm_getfh_xx(&ttfhp, &ttfhsize, (v3), md, dpos);
95683651Speter		if (t1 != 0)
95783651Speter			return t1;
958162288Smohans		t1 = nfs_nget(d->v_mount, ttfhp, ttfhsize, &ttnp, LK_EXCLUSIVE);
95983651Speter		if (t1 != 0)
96083651Speter			return t1;
96183651Speter		*v = NFSTOV(ttnp);
96283651Speter	}
96383651Speter	if (v3) {
96488091Siedowse		tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
96588091Siedowse		if (tl == NULL)
96684057Speter			return EBADRPC;
96783651Speter		if (*f)
96888091Siedowse			*f = fxdr_unsigned(int, *tl);
96988091Siedowse		else if (fxdr_unsigned(int, *tl))
97088091Siedowse			nfsm_adv_xx(NFSX_V3FATTR, md, dpos);
97183651Speter	}
97283651Speter	if (*f) {
97383651Speter		ttvp = *v;
97499797Sdillon		t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 0);
97583651Speter		if (t1)
97683651Speter			return t1;
97783651Speter		*v = ttvp;
97883651Speter	}
97983651Speter	return 0;
98083651Speter}
98183651Speter
98283651Speterint
98388091Siedowsenfsm_getfh_xx(nfsfh_t **f, int *s, int v3, struct mbuf **md, caddr_t *dpos)
98483651Speter{
98588091Siedowse	u_int32_t *tl;
98683651Speter
98783651Speter	if (v3) {
98888091Siedowse		tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
98988091Siedowse		if (tl == NULL)
99084057Speter			return EBADRPC;
99188091Siedowse		*s = fxdr_unsigned(int, *tl);
99284057Speter		if (*s <= 0 || *s > NFSX_V3FHMAX)
99383651Speter			return EBADRPC;
99483651Speter	} else
99583651Speter		*s = NFSX_V2FH;
99684057Speter	*f = nfsm_dissect_xx(nfsm_rndup(*s), md, dpos);
99784057Speter	if (*f == NULL)
99884057Speter		return EBADRPC;
99984057Speter	else
100084057Speter		return 0;
100183651Speter}
100283651Speter
100383651Speter
100483651Speterint
100588091Siedowsenfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md,
1006158739Smohans		 caddr_t *dpos)
100783651Speter{
100883651Speter	int t1;
100983651Speter
101083651Speter	struct vnode *ttvp = *v;
101183651Speter	t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 0);
101283651Speter	if (t1 != 0)
101383651Speter		return t1;
101483651Speter	*v = ttvp;
101583651Speter	return 0;
101683651Speter}
101783651Speter
101883651Speterint
101988091Siedowsenfsm_postop_attr_xx(struct vnode **v, int *f, struct mbuf **md,
1020158739Smohans		    caddr_t *dpos)
102183651Speter{
102288091Siedowse	u_int32_t *tl;
102383651Speter	int t1;
102483651Speter
102583651Speter	struct vnode *ttvp = *v;
102688091Siedowse	tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
102788091Siedowse	if (tl == NULL)
102884057Speter		return EBADRPC;
102988091Siedowse	*f = fxdr_unsigned(int, *tl);
103083914Siedowse	if (*f != 0) {
103199797Sdillon		t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 1);
103283651Speter		if (t1 != 0) {
103383651Speter			*f = 0;
103483651Speter			return t1;
103583651Speter		}
103683651Speter		*v = ttvp;
10379336Sdfr	}
103883651Speter	return 0;
10399336Sdfr}
10409336Sdfr
10419336Sdfrint
104288091Siedowsenfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md, caddr_t *dpos)
104331886Sbde{
104488091Siedowse	u_int32_t *tl;
104583651Speter	int ttattrf, ttretf = 0;
104683651Speter	int t1;
10475455Sdg
104888091Siedowse	tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
104988091Siedowse	if (tl == NULL)
105084057Speter		return EBADRPC;
105188091Siedowse	if (*tl == nfs_true) {
105288091Siedowse		tl = nfsm_dissect_xx(6 * NFSX_UNSIGNED, md, dpos);
105388091Siedowse		if (tl == NULL)
105484057Speter			return EBADRPC;
1055158739Smohans		mtx_lock(&(VTONFS(*v))->n_mtx);
105683651Speter		if (*f)
1057138473Sps 			ttretf = (VTONFS(*v)->n_mtime.tv_sec == fxdr_unsigned(u_int32_t, *(tl + 2)) &&
1058138473Sps				  VTONFS(*v)->n_mtime.tv_nsec == fxdr_unsigned(u_int32_t, *(tl + 3)));
1059158739Smohans		mtx_unlock(&(VTONFS(*v))->n_mtx);
106083651Speter	}
106188091Siedowse	t1 = nfsm_postop_attr_xx(v, &ttattrf, md, dpos);
106283651Speter	if (t1)
106383651Speter		return t1;
106483651Speter	if (*f)
106583651Speter		*f = ttretf;
106683651Speter	else
106783651Speter		*f = ttattrf;
106883651Speter	return 0;
10695455Sdg}
107036503Speter
107183651Speterint
107288091Siedowsenfsm_strtom_xx(const char *a, int s, int m, struct mbuf **mb, caddr_t *bpos)
107336503Speter{
107488091Siedowse	u_int32_t *tl;
107583651Speter	int t1;
107636503Speter
107783651Speter	if (s > m)
107883651Speter		return ENAMETOOLONG;
107983651Speter	t1 = nfsm_rndup(s) + NFSX_UNSIGNED;
108083651Speter	if (t1 <= M_TRAILINGSPACE(*mb)) {
108188091Siedowse		tl = nfsm_build_xx(t1, mb, bpos);
108288091Siedowse		*tl++ = txdr_unsigned(s);
108388091Siedowse		*(tl + ((t1 >> 2) - 2)) = 0;
108488091Siedowse		bcopy(a, tl, s);
108583651Speter	} else {
108683651Speter		t1 = nfsm_strtmbuf(mb, bpos, a, s);
108783651Speter		if (t1 != 0)
108883651Speter			return t1;
108936503Speter	}
109083651Speter	return 0;
109136503Speter}
109236503Speter
109383651Speterint
109488091Siedowsenfsm_fhtom_xx(struct vnode *v, int v3, struct mbuf **mb, caddr_t *bpos)
109583651Speter{
109688091Siedowse	u_int32_t *tl;
109783651Speter	int t1;
109883651Speter	caddr_t cp;
109983651Speter
110083651Speter	if (v3) {
110183651Speter		t1 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED;
110283651Speter		if (t1 < M_TRAILINGSPACE(*mb)) {
110388091Siedowse			tl = nfsm_build_xx(t1, mb, bpos);
110488091Siedowse			*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize);
110588091Siedowse			*(tl + ((t1 >> 2) - 2)) = 0;
110688091Siedowse			bcopy(VTONFS(v)->n_fhp, tl, VTONFS(v)->n_fhsize);
110783651Speter		} else {
110883651Speter			t1 = nfsm_strtmbuf(mb, bpos,
110983651Speter			    (const char *)VTONFS(v)->n_fhp,
111083651Speter			    VTONFS(v)->n_fhsize);
111183651Speter			if (t1 != 0)
111283651Speter				return t1;
111383651Speter		}
111483651Speter	} else {
111584002Speter		cp = nfsm_build_xx(NFSX_V2FH, mb, bpos);
111683651Speter		bcopy(VTONFS(v)->n_fhp, cp, NFSX_V2FH);
111783651Speter	}
111883651Speter	return 0;
111983651Speter}
112083651Speter
112136503Spetervoid
112288091Siedowsenfsm_v3attrbuild_xx(struct vattr *va, int full, struct mbuf **mb,
112388091Siedowse    caddr_t *bpos)
112436503Speter{
112588091Siedowse	u_int32_t *tl;
112636503Speter
112783651Speter	if (va->va_mode != (mode_t)VNOVAL) {
112888091Siedowse		tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
112988091Siedowse		*tl++ = nfs_true;
113088091Siedowse		*tl = txdr_unsigned(va->va_mode);
113183651Speter	} else {
113288091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
113388091Siedowse		*tl = nfs_false;
113483651Speter	}
113583651Speter	if (full && va->va_uid != (uid_t)VNOVAL) {
113688091Siedowse		tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
113788091Siedowse		*tl++ = nfs_true;
113888091Siedowse		*tl = txdr_unsigned(va->va_uid);
113983651Speter	} else {
114088091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
114188091Siedowse		*tl = nfs_false;
114283651Speter	}
114383651Speter	if (full && va->va_gid != (gid_t)VNOVAL) {
114488091Siedowse		tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
114588091Siedowse		*tl++ = nfs_true;
114688091Siedowse		*tl = txdr_unsigned(va->va_gid);
114783651Speter	} else {
114888091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
114988091Siedowse		*tl = nfs_false;
115083651Speter	}
115183651Speter	if (full && va->va_size != VNOVAL) {
115288091Siedowse		tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
115388091Siedowse		*tl++ = nfs_true;
115488091Siedowse		txdr_hyper(va->va_size, tl);
115583651Speter	} else {
115688091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
115788091Siedowse		*tl = nfs_false;
115883651Speter	}
115983651Speter	if (va->va_atime.tv_sec != VNOVAL) {
116083651Speter		if (va->va_atime.tv_sec != time_second) {
116188091Siedowse			tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
116288091Siedowse			*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
116388091Siedowse			txdr_nfsv3time(&va->va_atime, tl);
116483651Speter		} else {
116588091Siedowse			tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
116688091Siedowse			*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
116783651Speter		}
116883651Speter	} else {
116988091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
117088091Siedowse		*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
117183651Speter	}
117283651Speter	if (va->va_mtime.tv_sec != VNOVAL) {
117383651Speter		if (va->va_mtime.tv_sec != time_second) {
117488091Siedowse			tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
117588091Siedowse			*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
117688091Siedowse			txdr_nfsv3time(&va->va_mtime, tl);
117783651Speter		} else {
117888091Siedowse			tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
117988091Siedowse			*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
118083651Speter		}
118183651Speter	} else {
118288091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
118388091Siedowse		*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
118483651Speter	}
118536503Speter}
1186