nfs_subs.c revision 203072
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 203072 2010-01-27 15:22:20Z rmacklem $");
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
44190380Srwatson#include "opt_kdtrace.h"
45190380Srwatson
461541Srgrimes#include <sys/param.h>
4748274Speter#include <sys/systm.h>
4848274Speter#include <sys/kernel.h>
4960041Sphk#include <sys/bio.h>
5031886Sbde#include <sys/buf.h>
511541Srgrimes#include <sys/proc.h>
521541Srgrimes#include <sys/mount.h>
531541Srgrimes#include <sys/vnode.h>
541541Srgrimes#include <sys/namei.h>
551541Srgrimes#include <sys/mbuf.h>
561541Srgrimes#include <sys/socket.h>
571541Srgrimes#include <sys/stat.h>
589336Sdfr#include <sys/malloc.h>
592997Swollman#include <sys/sysent.h>
602997Swollman#include <sys/syscall.h>
6183651Speter#include <sys/sysproto.h>
621541Srgrimes
633305Sphk#include <vm/vm.h>
6412662Sdg#include <vm/vm_object.h>
6512662Sdg#include <vm/vm_extern.h>
6692783Sjeff#include <vm/uma.h>
673305Sphk
689336Sdfr#include <nfs/nfsproto.h>
6983651Speter#include <nfsclient/nfs.h>
7083651Speter#include <nfsclient/nfsnode.h>
71190380Srwatson#include <nfsclient/nfs_kdtrace.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
84190380Srwatson#ifdef KDTRACE_HOOKS
85190380Srwatsondtrace_nfsclient_attrcache_flush_probe_func_t
86190380Srwatson    dtrace_nfsclient_attrcache_flush_done_probe;
87190380Srwatsonuint32_t nfsclient_attrcache_flush_done_id;
88190380Srwatson
89190380Srwatsondtrace_nfsclient_attrcache_get_hit_probe_func_t
90190380Srwatson    dtrace_nfsclient_attrcache_get_hit_probe;
91190380Srwatsonuint32_t nfsclient_attrcache_get_hit_id;
92190380Srwatson
93190380Srwatsondtrace_nfsclient_attrcache_get_miss_probe_func_t
94190380Srwatson    dtrace_nfsclient_attrcache_get_miss_probe;
95190380Srwatsonuint32_t nfsclient_attrcache_get_miss_id;
96190380Srwatson
97190380Srwatsondtrace_nfsclient_attrcache_load_probe_func_t
98190380Srwatson    dtrace_nfsclient_attrcache_load_done_probe;
99190380Srwatsonuint32_t nfsclient_attrcache_load_done_id;
100190380Srwatson#endif /* !KDTRACE_HOOKS */
101190380Srwatson
102158739Smohans/*
1031541Srgrimes * Data items converted to xdr at startup, since they are constant
1041541Srgrimes * This is kinda hokey, but may save a little time doing byte swaps
1051541Srgrimes */
10683651Speteru_int32_t	nfs_xdrneg1;
10783651Speteru_int32_t	nfs_true, nfs_false;
1081541Srgrimes
1091541Srgrimes/* And other global data */
110176224Sjhbstatic u_int32_t nfs_xid = 0;
11112911Sphkstatic enum vtype nv2tov_type[8]= {
11283651Speter	VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON,  VNON
11312911Sphk};
11412911Sphk
11583651Speterint		nfs_ticks;
11683651Speterint		nfs_pbuf_freecnt = -1;	/* start out unlimited */
1179336Sdfr
11883651Speterstruct nfs_bufq	nfs_bufq;
119176224Sjhbstatic struct mtx nfs_xid_mtx;
1209759Sbde
1219336Sdfr/*
1229336Sdfr * and the reverse mapping from generic to Version 2 procedure numbers
1239336Sdfr */
1249336Sdfrint nfsv2_procid[NFS_NPROCS] = {
1259336Sdfr	NFSV2PROC_NULL,
1269336Sdfr	NFSV2PROC_GETATTR,
1279336Sdfr	NFSV2PROC_SETATTR,
1289336Sdfr	NFSV2PROC_LOOKUP,
1299336Sdfr	NFSV2PROC_NOOP,
1309336Sdfr	NFSV2PROC_READLINK,
1319336Sdfr	NFSV2PROC_READ,
1329336Sdfr	NFSV2PROC_WRITE,
1339336Sdfr	NFSV2PROC_CREATE,
1349336Sdfr	NFSV2PROC_MKDIR,
1359336Sdfr	NFSV2PROC_SYMLINK,
1369336Sdfr	NFSV2PROC_CREATE,
1379336Sdfr	NFSV2PROC_REMOVE,
1389336Sdfr	NFSV2PROC_RMDIR,
1399336Sdfr	NFSV2PROC_RENAME,
1409336Sdfr	NFSV2PROC_LINK,
1419336Sdfr	NFSV2PROC_READDIR,
1429336Sdfr	NFSV2PROC_NOOP,
1439336Sdfr	NFSV2PROC_STATFS,
1449336Sdfr	NFSV2PROC_NOOP,
1459336Sdfr	NFSV2PROC_NOOP,
1469336Sdfr	NFSV2PROC_NOOP,
1479336Sdfr	NFSV2PROC_NOOP,
1489336Sdfr};
1499336Sdfr
15060938SjakeLIST_HEAD(nfsnodehashhead, nfsnode);
1513664Sphk
152176224Sjhbu_int32_t
153176224Sjhbnfs_xid_gen(void)
154176224Sjhb{
155176224Sjhb	uint32_t xid;
156176224Sjhb
157176224Sjhb	mtx_lock(&nfs_xid_mtx);
158176224Sjhb
159176224Sjhb	/* Get a pretty random xid to start with */
160176224Sjhb	if (!nfs_xid)
161176224Sjhb		nfs_xid = random();
162176224Sjhb	/*
163176224Sjhb	 * Skip zero xid if it should ever happen.
164176224Sjhb	 */
165176224Sjhb	if (++nfs_xid == 0)
166176224Sjhb		nfs_xid++;
167176224Sjhb	xid = nfs_xid;
168176224Sjhb	mtx_unlock(&nfs_xid_mtx);
169176224Sjhb	return xid;
170176224Sjhb}
171176224Sjhb
1721541Srgrimes/*
1731541Srgrimes * Create the header for an rpc request packet
1741541Srgrimes * The hsiz is the size of the rest of the nfs request header.
1751541Srgrimes * (just used to decide if a cluster is a good idea)
1761541Srgrimes */
1771541Srgrimesstruct mbuf *
17883651Speternfsm_reqhead(struct vnode *vp, u_long procid, int hsiz)
1791541Srgrimes{
18083651Speter	struct mbuf *mb;
1811541Srgrimes
182177599Sru	MGET(mb, M_WAIT, MT_DATA);
1831541Srgrimes	if (hsiz >= MINCLSIZE)
184177599Sru		MCLGET(mb, M_WAIT);
1851541Srgrimes	mb->m_len = 0;
1861541Srgrimes	return (mb);
1871541Srgrimes}
1881541Srgrimes
1891541Srgrimes/*
19017186Sdfr * copies a uio scatter/gather list to an mbuf chain.
19117186Sdfr * NOTE: can ony handle iovcnt == 1
1921541Srgrimes */
1931549Srgrimesint
19483651Speternfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
1951541Srgrimes{
19683651Speter	char *uiocp;
19783651Speter	struct mbuf *mp, *mp2;
19883651Speter	int xfer, left, mlen;
1991541Srgrimes	int uiosiz, clflg, rem;
2001541Srgrimes	char *cp;
2011541Srgrimes
20236519Speter#ifdef DIAGNOSTIC
20317186Sdfr	if (uiop->uio_iovcnt != 1)
20417186Sdfr		panic("nfsm_uiotombuf: iovcnt != 1");
20536519Speter#endif
20617186Sdfr
2071541Srgrimes	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
2081541Srgrimes		clflg = 1;
2091541Srgrimes	else
2101541Srgrimes		clflg = 0;
2111541Srgrimes	rem = nfsm_rndup(siz)-siz;
2121541Srgrimes	mp = mp2 = *mq;
2131541Srgrimes	while (siz > 0) {
2141541Srgrimes		left = uiop->uio_iov->iov_len;
2151541Srgrimes		uiocp = uiop->uio_iov->iov_base;
2161541Srgrimes		if (left > siz)
2171541Srgrimes			left = siz;
2181541Srgrimes		uiosiz = left;
2191541Srgrimes		while (left > 0) {
2201541Srgrimes			mlen = M_TRAILINGSPACE(mp);
2211541Srgrimes			if (mlen == 0) {
222177599Sru				MGET(mp, M_WAIT, MT_DATA);
2231541Srgrimes				if (clflg)
224177599Sru					MCLGET(mp, M_WAIT);
2251541Srgrimes				mp->m_len = 0;
2261541Srgrimes				mp2->m_next = mp;
2271541Srgrimes				mp2 = mp;
2281541Srgrimes				mlen = M_TRAILINGSPACE(mp);
2291541Srgrimes			}
2301541Srgrimes			xfer = (left > mlen) ? mlen : left;
2311541Srgrimes#ifdef notdef
2321541Srgrimes			/* Not Yet.. */
2331541Srgrimes			if (uiop->uio_iov->iov_op != NULL)
2341541Srgrimes				(*(uiop->uio_iov->iov_op))
2351541Srgrimes				(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
2361541Srgrimes			else
2371541Srgrimes#endif
2381541Srgrimes			if (uiop->uio_segflg == UIO_SYSSPACE)
2391541Srgrimes				bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
2401541Srgrimes			else
2411541Srgrimes				copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
2421541Srgrimes			mp->m_len += xfer;
2431541Srgrimes			left -= xfer;
2441541Srgrimes			uiocp += xfer;
2451541Srgrimes			uiop->uio_offset += xfer;
2461541Srgrimes			uiop->uio_resid -= xfer;
2471541Srgrimes		}
248104908Smike		uiop->uio_iov->iov_base =
249104908Smike		    (char *)uiop->uio_iov->iov_base + uiosiz;
25017186Sdfr		uiop->uio_iov->iov_len -= uiosiz;
2511541Srgrimes		siz -= uiosiz;
2521541Srgrimes	}
2531541Srgrimes	if (rem > 0) {
2541541Srgrimes		if (rem > M_TRAILINGSPACE(mp)) {
255177599Sru			MGET(mp, M_WAIT, MT_DATA);
2561541Srgrimes			mp->m_len = 0;
2571541Srgrimes			mp2->m_next = mp;
2581541Srgrimes		}
2591541Srgrimes		cp = mtod(mp, caddr_t)+mp->m_len;
2601541Srgrimes		for (left = 0; left < rem; left++)
2611541Srgrimes			*cp++ = '\0';
2621541Srgrimes		mp->m_len += rem;
2631541Srgrimes		*bpos = cp;
2641541Srgrimes	} else
2651541Srgrimes		*bpos = mtod(mp, caddr_t)+mp->m_len;
2661541Srgrimes	*mq = mp;
2671541Srgrimes	return (0);
2681541Srgrimes}
2691541Srgrimes
2701541Srgrimes/*
2711541Srgrimes * Copy a string into mbufs for the hard cases...
2721541Srgrimes */
2731549Srgrimesint
27483651Speternfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
2751541Srgrimes{
27683651Speter	struct mbuf *m1 = NULL, *m2;
2771541Srgrimes	long left, xfer, len, tlen;
27836541Speter	u_int32_t *tl;
2791541Srgrimes	int putsize;
2801541Srgrimes
2811541Srgrimes	putsize = 1;
2821541Srgrimes	m2 = *mb;
2831541Srgrimes	left = M_TRAILINGSPACE(m2);
2841541Srgrimes	if (left > 0) {
28536541Speter		tl = ((u_int32_t *)(*bpos));
2861541Srgrimes		*tl++ = txdr_unsigned(siz);
2871541Srgrimes		putsize = 0;
2881541Srgrimes		left -= NFSX_UNSIGNED;
2891541Srgrimes		m2->m_len += NFSX_UNSIGNED;
2901541Srgrimes		if (left > 0) {
2911541Srgrimes			bcopy(cp, (caddr_t) tl, left);
2921541Srgrimes			siz -= left;
2931541Srgrimes			cp += left;
2941541Srgrimes			m2->m_len += left;
2951541Srgrimes			left = 0;
2961541Srgrimes		}
2971541Srgrimes	}
2981541Srgrimes	/* Loop around adding mbufs */
2991541Srgrimes	while (siz > 0) {
300177599Sru		MGET(m1, M_WAIT, MT_DATA);
3011541Srgrimes		if (siz > MLEN)
302177599Sru			MCLGET(m1, M_WAIT);
3031541Srgrimes		m1->m_len = NFSMSIZ(m1);
3041541Srgrimes		m2->m_next = m1;
3051541Srgrimes		m2 = m1;
30636541Speter		tl = mtod(m1, u_int32_t *);
3071541Srgrimes		tlen = 0;
3081541Srgrimes		if (putsize) {
3091541Srgrimes			*tl++ = txdr_unsigned(siz);
3101541Srgrimes			m1->m_len -= NFSX_UNSIGNED;
3111541Srgrimes			tlen = NFSX_UNSIGNED;
3121541Srgrimes			putsize = 0;
3131541Srgrimes		}
3141541Srgrimes		if (siz < m1->m_len) {
3151541Srgrimes			len = nfsm_rndup(siz);
3161541Srgrimes			xfer = siz;
3171541Srgrimes			if (xfer < len)
3181541Srgrimes				*(tl+(xfer>>2)) = 0;
3191541Srgrimes		} else {
3201541Srgrimes			xfer = len = m1->m_len;
3211541Srgrimes		}
3221541Srgrimes		bcopy(cp, (caddr_t) tl, xfer);
3231541Srgrimes		m1->m_len = len+tlen;
3241541Srgrimes		siz -= xfer;
3251541Srgrimes		cp += xfer;
3261541Srgrimes	}
3271541Srgrimes	*mb = m1;
3281541Srgrimes	*bpos = mtod(m1, caddr_t)+m1->m_len;
3291541Srgrimes	return (0);
3301541Srgrimes}
3311541Srgrimes
3321541Srgrimes/*
3331541Srgrimes * Called once to initialize data structures...
3341541Srgrimes */
3351549Srgrimesint
33683651Speternfs_init(struct vfsconf *vfsp)
3371541Srgrimes{
33883651Speter	int i;
3391541Srgrimes
34092783Sjeff	nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount),
34192783Sjeff	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3421541Srgrimes	nfs_true = txdr_unsigned(TRUE);
3431541Srgrimes	nfs_false = txdr_unsigned(FALSE);
3443664Sphk	nfs_xdrneg1 = txdr_unsigned(-1);
3459336Sdfr	nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
3469336Sdfr	if (nfs_ticks < 1)
3479336Sdfr		nfs_ticks = 1;
3481541Srgrimes	/* Ensure async daemons disabled */
34919449Sdfr	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
350203072Srmacklem		nfs_iodwant[i] = NFSIOD_NOT_AVAILABLE;
35199797Sdillon		nfs_iodmount[i] = NULL;
35219449Sdfr	}
3531541Srgrimes	nfs_nhinit();			/* Init the nfsnode table */
3541541Srgrimes
3551541Srgrimes	/*
3561541Srgrimes	 * Initialize reply list and start timer
3571541Srgrimes	 */
358158739Smohans	mtx_init(&nfs_iod_mtx, "NFS iod lock", NULL, MTX_DEF);
359172600Smohans	mtx_init(&nfs_xid_mtx, "NFS xid lock", NULL, MTX_DEF);
36016365Sphk
36142957Sdillon	nfs_pbuf_freecnt = nswbuf / 2 + 1;
36242957Sdillon
3631549Srgrimes	return (0);
3641541Srgrimes}
3651541Srgrimes
36638894Sbdeint
36783651Speternfs_uninit(struct vfsconf *vfsp)
36838894Sbde{
369128111Speadar	int i;
37038894Sbde
371128111Speadar	/*
372128111Speadar	 * Tell all nfsiod processes to exit. Clear nfs_iodmax, and wakeup
373128111Speadar	 * any sleeping nfsiods so they check nfs_iodmax and exit.
374128111Speadar	 */
375158739Smohans	mtx_lock(&nfs_iod_mtx);
376128111Speadar	nfs_iodmax = 0;
377128111Speadar	for (i = 0; i < nfs_numasync; i++)
378203072Srmacklem		if (nfs_iodwant[i] == NFSIOD_AVAILABLE)
379128111Speadar			wakeup(&nfs_iodwant[i]);
380128111Speadar	/* The last nfsiod to exit will wake us up when nfs_numasync hits 0 */
381128111Speadar	while (nfs_numasync)
382158739Smohans		msleep(&nfs_numasync, &nfs_iod_mtx, PWAIT, "ioddie", 0);
383158739Smohans	mtx_unlock(&nfs_iod_mtx);
384128111Speadar	nfs_nhuninit();
385128111Speadar	uma_zdestroy(nfsmount_zone);
38638894Sbde	return (0);
38738894Sbde}
38838894Sbde
389158739Smohansvoid
390158739Smohansnfs_dircookie_lock(struct nfsnode *np)
391158739Smohans{
392158739Smohans	mtx_lock(&np->n_mtx);
393158739Smohans	while (np->n_flag & NDIRCOOKIELK)
394158739Smohans		(void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
395158739Smohans	np->n_flag |= NDIRCOOKIELK;
396158739Smohans	mtx_unlock(&np->n_mtx);
397158739Smohans}
398158739Smohans
399158739Smohansvoid
400158739Smohansnfs_dircookie_unlock(struct nfsnode *np)
401158739Smohans{
402158739Smohans	mtx_lock(&np->n_mtx);
403158739Smohans	np->n_flag &= ~NDIRCOOKIELK;
404158739Smohans	wakeup(&np->n_flag);
405158739Smohans	mtx_unlock(&np->n_mtx);
406158739Smohans}
407158739Smohans
408158739Smohansint
409176134Sattilionfs_upgrade_vnlock(struct vnode *vp)
410158739Smohans{
411158739Smohans	int old_lock;
412196205Skib
413196205Skib	ASSERT_VOP_LOCKED(vp, "nfs_upgrade_vnlock");
414196205Skib	old_lock = VOP_ISLOCKED(vp);
415196205Skib	if (old_lock != LK_EXCLUSIVE) {
416196205Skib		KASSERT(old_lock == LK_SHARED,
417196205Skib		    ("nfs_upgrade_vnlock: wrong old_lock %d", old_lock));
418196205Skib		/* Upgrade to exclusive lock, this might block */
419196205Skib		vn_lock(vp, LK_UPGRADE | LK_RETRY);
420158739Smohans  	}
421196205Skib	return (old_lock);
422158739Smohans}
423158739Smohans
424158739Smohansvoid
425176134Sattilionfs_downgrade_vnlock(struct vnode *vp, int old_lock)
426158739Smohans{
427158739Smohans	if (old_lock != LK_EXCLUSIVE) {
428196205Skib		KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock));
429196205Skib		/* Downgrade from exclusive lock. */
430196205Skib		vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
431158739Smohans  	}
432158739Smohans}
433158739Smohans
434158739Smohansvoid
435158739Smohansnfs_printf(const char *fmt, ...)
436158739Smohans{
437158739Smohans	va_list ap;
438158739Smohans
439158739Smohans	mtx_lock(&Giant);
440158739Smohans	va_start(ap, fmt);
441158739Smohans	printf(fmt, ap);
442158739Smohans	va_end(ap);
443158739Smohans	mtx_unlock(&Giant);
444158739Smohans}
445158739Smohans
4461541Srgrimes/*
4471541Srgrimes * Attribute cache routines.
4481541Srgrimes * nfs_loadattrcache() - loads or updates the cache contents from attributes
4491541Srgrimes *	that are on the mbuf list
4501541Srgrimes * nfs_getattrcache() - returns valid attributes if found in cache, returns
4511541Srgrimes *	error otherwise
4521541Srgrimes */
4531541Srgrimes
4541541Srgrimes/*
4551541Srgrimes * Load the attribute cache (that lives in the nfsnode entry) with
4561541Srgrimes * the values on the mbuf list and
4571541Srgrimes * Iff vap not NULL
4581541Srgrimes *    copy the attributes to *vaper
4591541Srgrimes */
4601549Srgrimesint
46183651Speternfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
462158739Smohans		  struct vattr *vaper, int dontshrink)
4631541Srgrimes{
46483651Speter	struct vnode *vp = *vpp;
46583651Speter	struct vattr *vap;
46683651Speter	struct nfs_fattr *fp;
467190380Srwatson	struct nfsnode *np = NULL;
46883651Speter	int32_t t1;
4699336Sdfr	caddr_t cp2;
47084057Speter	int rdev;
4711541Srgrimes	struct mbuf *md;
4721541Srgrimes	enum vtype vtyp;
4731541Srgrimes	u_short vmode;
474171190Sjhb	struct timespec mtime, mtime_save;
4759336Sdfr	int v3 = NFS_ISV3(vp);
476167352Smohans	struct thread *td = curthread;
477190380Srwatson	int error = 0;
4781541Srgrimes
4791541Srgrimes	md = *mdp;
4809336Sdfr	t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
481177599Sru	cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, M_WAIT);
482190380Srwatson	if (cp2 == NULL) {
483190380Srwatson		error = EBADRPC;
484190380Srwatson		goto out;
485190380Srwatson	}
4869336Sdfr	fp = (struct nfs_fattr *)cp2;
4879336Sdfr	if (v3) {
4889336Sdfr		vtyp = nfsv3tov_type(fp->fa_type);
4899336Sdfr		vmode = fxdr_unsigned(u_short, fp->fa_mode);
490130640Sphk		rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
49116634Sbde			fxdr_unsigned(int, fp->fa3_rdev.specdata2));
4929336Sdfr		fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
4931541Srgrimes	} else {
4949336Sdfr		vtyp = nfsv2tov_type(fp->fa_type);
4959336Sdfr		vmode = fxdr_unsigned(u_short, fp->fa_mode);
4969336Sdfr		/*
4979336Sdfr		 * XXX
4989336Sdfr		 *
4999336Sdfr		 * The duplicate information returned in fa_type and fa_mode
5009336Sdfr		 * is an ambiguity in the NFS version 2 protocol.
5019336Sdfr		 *
5029336Sdfr		 * VREG should be taken literally as a regular file.  If a
5039336Sdfr		 * server intents to return some type information differently
5049336Sdfr		 * in the upper bits of the mode field (e.g. for sockets, or
5059336Sdfr		 * FIFOs), NFSv2 mandates fa_type to be VNON.  Anyway, we
5069336Sdfr		 * leave the examination of the mode bits even in the VREG
5079336Sdfr		 * case to avoid breakage for bogus servers, but we make sure
5089336Sdfr		 * that there are actually type bits set in the upper part of
5099336Sdfr		 * fa_mode (and failing that, trust the va_type field).
5109336Sdfr		 *
5119336Sdfr		 * NFSv3 cleared the issue, and requires fa_mode to not
5129336Sdfr		 * contain any type information (while also introduing sockets
5139336Sdfr		 * and FIFOs for fa_type).
5149336Sdfr		 */
5159336Sdfr		if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
5169336Sdfr			vtyp = IFTOVT(vmode);
51736541Speter		rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
5189336Sdfr		fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
5199336Sdfr
5209336Sdfr		/*
5219336Sdfr		 * Really ugly NFSv2 kludge.
5229336Sdfr		 */
5239336Sdfr		if (vtyp == VCHR && rdev == 0xffffffff)
5249336Sdfr			vtyp = VFIFO;
5251541Srgrimes	}
5269336Sdfr
5271541Srgrimes	/*
5281541Srgrimes	 * If v_type == VNON it is a new node, so fill in the v_type,
5298876Srgrimes	 * n_mtime fields. Check to see if it represents a special
5301541Srgrimes	 * device, and if so, check for a possible alias. Once the
5311541Srgrimes	 * correct vnode has been obtained, fill in the rest of the
5321541Srgrimes	 * information.
5331541Srgrimes	 */
5341541Srgrimes	np = VTONFS(vp);
535158739Smohans	mtx_lock(&np->n_mtx);
53610219Sdfr	if (vp->v_type != vtyp) {
5379336Sdfr		vp->v_type = vtyp;
538126851Sphk		if (vp->v_type == VFIFO)
539138290Sphk			vp->v_op = &nfs_fifoops;
540138473Sps		np->n_mtime = mtime;
5411541Srgrimes	}
5421541Srgrimes	vap = &np->n_vattr;
5431541Srgrimes	vap->va_type = vtyp;
5441541Srgrimes	vap->va_mode = (vmode & 07777);
54547028Sphk	vap->va_rdev = rdev;
546171190Sjhb	mtime_save = vap->va_mtime;
5471541Srgrimes	vap->va_mtime = mtime;
5481541Srgrimes	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
5499336Sdfr	if (v3) {
5509336Sdfr		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
5519336Sdfr		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
5529336Sdfr		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
55347751Speter		vap->va_size = fxdr_hyper(&fp->fa3_size);
5549336Sdfr		vap->va_blocksize = NFS_FABLKSIZE;
55547751Speter		vap->va_bytes = fxdr_hyper(&fp->fa3_used);
55636541Speter		vap->va_fileid = fxdr_unsigned(int32_t,
55736541Speter		    fp->fa3_fileid.nfsuquad[1]);
5589336Sdfr		fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
5599336Sdfr		fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
5609336Sdfr		vap->va_flags = 0;
5619336Sdfr		vap->va_filerev = 0;
5621541Srgrimes	} else {
5639336Sdfr		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
5649336Sdfr		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
5659336Sdfr		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
56636541Speter		vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
56736541Speter		vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
56847751Speter		vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
56936541Speter		    * NFS_FABLKSIZE;
57036541Speter		vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
5719336Sdfr		fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
5721541Srgrimes		vap->va_flags = 0;
57336541Speter		vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
57436541Speter		    fp->fa2_ctime.nfsv2_sec);
57518397Snate		vap->va_ctime.tv_nsec = 0;
57683651Speter		vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec);
5771541Srgrimes		vap->va_filerev = 0;
5781541Srgrimes	}
57967486Sdwmalone	np->n_attrstamp = time_second;
580167352Smohans	/* Timestamp the NFS otw getattr fetch */
581167352Smohans	if (td->td_proc) {
582167352Smohans		np->n_ac_ts_tid = td->td_tid;
583167352Smohans		np->n_ac_ts_pid = td->td_proc->p_pid;
584167352Smohans		np->n_ac_ts_syscalls = td->td_syscalls;
585167352Smohans	} else
586167352Smohans		bzero(&np->n_ac_ts, sizeof(struct nfs_attrcache_timestamp));
587167352Smohans
5881541Srgrimes	if (vap->va_size != np->n_size) {
5891541Srgrimes		if (vap->va_type == VREG) {
59067486Sdwmalone			if (dontshrink && vap->va_size < np->n_size) {
59167486Sdwmalone				/*
59267486Sdwmalone				 * We've been told not to shrink the file;
59367486Sdwmalone				 * zero np->n_attrstamp to indicate that
59467486Sdwmalone				 * the attributes are stale.
59567486Sdwmalone				 */
59667486Sdwmalone				vap->va_size = np->n_size;
59767486Sdwmalone				np->n_attrstamp = 0;
598190380Srwatson				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
59967486Sdwmalone			} else if (np->n_flag & NMODIFIED) {
600128263Speadar				/*
601128263Speadar				 * We've modified the file: Use the larger
602128263Speadar				 * of our size, and the server's size.
603128263Speadar				 */
604128263Speadar				if (vap->va_size < np->n_size) {
6051541Srgrimes					vap->va_size = np->n_size;
606128263Speadar				} else {
6071541Srgrimes					np->n_size = vap->va_size;
608128263Speadar					np->n_flag |= NSIZECHANGED;
609128263Speadar				}
61054480Sdillon			} else {
6111541Srgrimes				np->n_size = vap->va_size;
612128263Speadar				np->n_flag |= NSIZECHANGED;
61354480Sdillon			}
61441026Speter			vnode_pager_setsize(vp, np->n_size);
61554480Sdillon		} else {
6161541Srgrimes			np->n_size = vap->va_size;
61754480Sdillon		}
6181541Srgrimes	}
619171190Sjhb	/*
620171190Sjhb	 * The following checks are added to prevent a race between (say)
621171190Sjhb	 * a READDIR+ and a WRITE.
622171190Sjhb	 * READDIR+, WRITE requests sent out.
623171190Sjhb	 * READDIR+ resp, WRITE resp received on client.
624171190Sjhb	 * However, the WRITE resp was handled before the READDIR+ resp
625171190Sjhb	 * causing the post op attrs from the write to be loaded first
626171190Sjhb	 * and the attrs from the READDIR+ to be loaded later. If this
627171190Sjhb	 * happens, we have stale attrs loaded into the attrcache.
628171190Sjhb	 * We detect this by for the mtime moving back. We invalidate the
629171190Sjhb	 * attrcache when this happens.
630171190Sjhb	 */
631190380Srwatson	if (timespeccmp(&mtime_save, &vap->va_mtime, >)) {
632171190Sjhb		/* Size changed or mtime went backwards */
633171190Sjhb		np->n_attrstamp = 0;
634190380Srwatson		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
635190380Srwatson	}
6361541Srgrimes	if (vaper != NULL) {
6371541Srgrimes		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
6381541Srgrimes		if (np->n_flag & NCHG) {
6399336Sdfr			if (np->n_flag & NACC)
6409336Sdfr				vaper->va_atime = np->n_atim;
6419336Sdfr			if (np->n_flag & NUPD)
6429336Sdfr				vaper->va_mtime = np->n_mtim;
6431541Srgrimes		}
6441541Srgrimes	}
645190396Srwatson
646190396Srwatson#ifdef KDTRACE_HOOKS
647190396Srwatson	if (np->n_attrstamp != 0)
648190396Srwatson		KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, &np->n_vattr, 0);
649190396Srwatson#endif
650158739Smohans	mtx_unlock(&np->n_mtx);
651190380Srwatsonout:
652190396Srwatson#ifdef KDTRACE_HOOKS
653190396Srwatson	if (error)
654190396Srwatson		KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, NULL, error);
655190380Srwatson#endif
656190380Srwatson	return (error);
6571541Srgrimes}
6581541Srgrimes
65936176Speter#ifdef NFS_ACDEBUG
66036176Speter#include <sys/sysctl.h>
66144101SbdeSYSCTL_DECL(_vfs_nfs);
66236176Speterstatic int nfs_acdebug;
663184561StrhodesSYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0,
664184561Strhodes    "Toggle acdebug (access cache debug) flag");
66536176Speter#endif
66636176Speter
6671541Srgrimes/*
6681541Srgrimes * Check the time stamp
6691541Srgrimes * If the cache is valid, copy contents to *vap and return 0
6701541Srgrimes * otherwise return an error
6711541Srgrimes */
6721549Srgrimesint
67383651Speternfs_getattrcache(struct vnode *vp, struct vattr *vaper)
6741541Srgrimes{
67583651Speter	struct nfsnode *np;
67683651Speter	struct vattr *vap;
67736176Speter	struct nfsmount *nmp;
67836176Speter	int timeo;
679158739Smohans
68036176Speter	np = VTONFS(vp);
68136176Speter	vap = &np->n_vattr;
68236176Speter	nmp = VFSTONFS(vp->v_mount);
683158739Smohans#ifdef NFS_ACDEBUG
684158739Smohans	mtx_lock(&Giant);	/* nfs_printf() */
685158739Smohans#endif
686158739Smohans	mtx_lock(&np->n_mtx);
68736176Speter	/* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
688138473Sps	timeo = (time_second - np->n_mtime.tv_sec) / 10;
68936176Speter
69036176Speter#ifdef NFS_ACDEBUG
69136176Speter	if (nfs_acdebug>1)
692158739Smohans		nfs_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
69336176Speter#endif
69436176Speter
69536176Speter	if (vap->va_type == VDIR) {
69636176Speter		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
69736176Speter			timeo = nmp->nm_acdirmin;
69836176Speter		else if (timeo > nmp->nm_acdirmax)
69936176Speter			timeo = nmp->nm_acdirmax;
70036176Speter	} else {
70136176Speter		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
70236176Speter			timeo = nmp->nm_acregmin;
70336176Speter		else if (timeo > nmp->nm_acregmax)
70436176Speter			timeo = nmp->nm_acregmax;
70536176Speter	}
70636176Speter
70736176Speter#ifdef NFS_ACDEBUG
70836176Speter	if (nfs_acdebug > 2)
709158739Smohans		nfs_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
710158739Smohans			   nmp->nm_acregmin, nmp->nm_acregmax,
711158739Smohans			   nmp->nm_acdirmin, nmp->nm_acdirmax);
71236176Speter
71336176Speter	if (nfs_acdebug)
714158739Smohans		nfs_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
715158739Smohans			   (time_second - np->n_attrstamp), timeo);
71636176Speter#endif
71736176Speter
71836176Speter	if ((time_second - np->n_attrstamp) >= timeo) {
7191541Srgrimes		nfsstats.attrcache_misses++;
720158739Smohans		mtx_unlock(&np->n_mtx);
721190380Srwatson		KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
722190380Srwatson		return (ENOENT);
7231541Srgrimes	}
7241541Srgrimes	nfsstats.attrcache_hits++;
7251541Srgrimes	if (vap->va_size != np->n_size) {
7261541Srgrimes		if (vap->va_type == VREG) {
7271541Srgrimes			if (np->n_flag & NMODIFIED) {
7281541Srgrimes				if (vap->va_size < np->n_size)
7291541Srgrimes					vap->va_size = np->n_size;
7301541Srgrimes				else
7311541Srgrimes					np->n_size = vap->va_size;
73254480Sdillon			} else {
7331541Srgrimes				np->n_size = vap->va_size;
73454480Sdillon			}
73541026Speter			vnode_pager_setsize(vp, np->n_size);
73654480Sdillon		} else {
7371541Srgrimes			np->n_size = vap->va_size;
73854480Sdillon		}
7391541Srgrimes	}
7401541Srgrimes	bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
7411541Srgrimes	if (np->n_flag & NCHG) {
7429336Sdfr		if (np->n_flag & NACC)
7439336Sdfr			vaper->va_atime = np->n_atim;
7449336Sdfr		if (np->n_flag & NUPD)
7459336Sdfr			vaper->va_mtime = np->n_mtim;
7461541Srgrimes	}
747158739Smohans	mtx_unlock(&np->n_mtx);
748158739Smohans#ifdef NFS_ACDEBUG
749158739Smohans	mtx_unlock(&Giant);	/* nfs_printf() */
750158739Smohans#endif
751190380Srwatson	KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
7521541Srgrimes	return (0);
7531541Srgrimes}
7541541Srgrimes
755190785Sjhb/*
756190785Sjhb * Purge all cached information about an NFS vnode including name
757190785Sjhb * cache entries, the attribute cache, and the access cache.  This is
758190785Sjhb * called when an NFS request for a node fails with a stale
759190785Sjhb * filehandle.
760190785Sjhb */
761190785Sjhbvoid
762190785Sjhbnfs_purgecache(struct vnode *vp)
763190785Sjhb{
764190785Sjhb	struct nfsnode *np;
765190785Sjhb	int i;
766190785Sjhb
767190785Sjhb	np = VTONFS(vp);
768190785Sjhb	cache_purge(vp);
769190785Sjhb	mtx_lock(&np->n_mtx);
770190785Sjhb	np->n_attrstamp = 0;
771190785Sjhb	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
772190785Sjhb	for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
773190785Sjhb		np->n_accesscache[i].stamp = 0;
774190785Sjhb	KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
775190785Sjhb	mtx_unlock(&np->n_mtx);
776190785Sjhb}
777190785Sjhb
77843305Sdillonstatic nfsuint64 nfs_nullcookie = { { 0, 0 } };
7799336Sdfr/*
7809336Sdfr * This function finds the directory cookie that corresponds to the
7819336Sdfr * logical byte offset given.
7829336Sdfr */
7839336Sdfrnfsuint64 *
78483651Speternfs_getcookie(struct nfsnode *np, off_t off, int add)
7859336Sdfr{
78683651Speter	struct nfsdmap *dp, *dp2;
78783651Speter	int pos;
788158739Smohans	nfsuint64 *retval = NULL;
789158739Smohans
79036979Sbde	pos = (uoff_t)off / NFS_DIRBLKSIZ;
79136979Sbde	if (pos == 0 || off < 0) {
7929336Sdfr#ifdef DIAGNOSTIC
7939336Sdfr		if (add)
79436979Sbde			panic("nfs getcookie add at <= 0");
7959336Sdfr#endif
7969336Sdfr		return (&nfs_nullcookie);
7979336Sdfr	}
7989336Sdfr	pos--;
79983651Speter	dp = LIST_FIRST(&np->n_cookies);
8009336Sdfr	if (!dp) {
8019336Sdfr		if (add) {
802184205Sdes			dp = malloc(sizeof (struct nfsdmap),
803111119Simp				M_NFSDIROFF, M_WAITOK);
8049336Sdfr			dp->ndm_eocookie = 0;
8059336Sdfr			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
8069336Sdfr		} else
807158739Smohans			goto out;
8089336Sdfr	}
8099336Sdfr	while (pos >= NFSNUMCOOKIES) {
8109336Sdfr		pos -= NFSNUMCOOKIES;
81183651Speter		if (LIST_NEXT(dp, ndm_list)) {
8129336Sdfr			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
813158739Smohans			    pos >= dp->ndm_eocookie)
814158739Smohans				goto out;
81583651Speter			dp = LIST_NEXT(dp, ndm_list);
8169336Sdfr		} else if (add) {
817184205Sdes			dp2 = malloc(sizeof (struct nfsdmap),
818111119Simp				M_NFSDIROFF, M_WAITOK);
8199336Sdfr			dp2->ndm_eocookie = 0;
8209336Sdfr			LIST_INSERT_AFTER(dp, dp2, ndm_list);
8219336Sdfr			dp = dp2;
8229336Sdfr		} else
823158739Smohans			goto out;
8249336Sdfr	}
8259336Sdfr	if (pos >= dp->ndm_eocookie) {
8269336Sdfr		if (add)
8279336Sdfr			dp->ndm_eocookie = pos + 1;
8289336Sdfr		else
829158739Smohans			goto out;
8309336Sdfr	}
831158739Smohans	retval = &dp->ndm_cookies[pos];
832158739Smohansout:
833158739Smohans	return (retval);
8349336Sdfr}
8359336Sdfr
8369336Sdfr/*
8379336Sdfr * Invalidate cached directory information, except for the actual directory
8389336Sdfr * blocks (which are invalidated separately).
8399336Sdfr * Done mainly to avoid the use of stale offset cookies.
8409336Sdfr */
8419336Sdfrvoid
84283651Speternfs_invaldir(struct vnode *vp)
8439336Sdfr{
84483651Speter	struct nfsnode *np = VTONFS(vp);
8459336Sdfr
8469336Sdfr#ifdef DIAGNOSTIC
8479336Sdfr	if (vp->v_type != VDIR)
8489336Sdfr		panic("nfs: invaldir not dir");
8499336Sdfr#endif
850158739Smohans	nfs_dircookie_lock(np);
8519336Sdfr	np->n_direofoffset = 0;
8529336Sdfr	np->n_cookieverf.nfsuquad[0] = 0;
8539336Sdfr	np->n_cookieverf.nfsuquad[1] = 0;
85483651Speter	if (LIST_FIRST(&np->n_cookies))
85583651Speter		LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
856158739Smohans	nfs_dircookie_unlock(np);
8579336Sdfr}
8589336Sdfr
8599336Sdfr/*
8609336Sdfr * The write verifier has changed (probably due to a server reboot), so all
8619336Sdfr * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
8629336Sdfr * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
86354480Sdillon * and B_CLUSTEROK flags.  Once done the new write verifier can be set for the
86454480Sdillon * mount point.
86554480Sdillon *
86683651Speter * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
86754480Sdillon * writes are not clusterable.
8689336Sdfr */
8699336Sdfrvoid
87083651Speternfs_clearcommit(struct mount *mp)
8719336Sdfr{
87283651Speter	struct vnode *vp, *nvp;
87383651Speter	struct buf *bp, *nbp;
874177493Sjeff	struct bufobj *bo;
8759336Sdfr
876122091Skan	MNT_ILOCK(mp);
877131551Sphk	MNT_VNODE_FOREACH(vp, mp, nvp) {
878177493Sjeff		bo = &vp->v_bufobj;
879103939Sjeff		VI_LOCK(vp);
880143510Sjeff		if (vp->v_iflag & VI_DOOMED) {
881120787Sjeff			VI_UNLOCK(vp);
882120787Sjeff			continue;
883120787Sjeff		}
884177493Sjeff		vholdl(vp);
885177493Sjeff		VI_UNLOCK(vp);
886122091Skan		MNT_IUNLOCK(mp);
887177493Sjeff		BO_LOCK(bo);
888177493Sjeff		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
889175486Sattilio			if (!BUF_ISLOCKED(bp) &&
89048225Smckusick			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
8919336Sdfr				== (B_DELWRI | B_NEEDCOMMIT))
89254480Sdillon				bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
8939336Sdfr		}
894177493Sjeff		BO_UNLOCK(bo);
895177493Sjeff		vdrop(vp);
896122091Skan		MNT_ILOCK(mp);
8979336Sdfr	}
898122091Skan	MNT_IUNLOCK(mp);
8999336Sdfr}
9009336Sdfr
9019336Sdfr/*
90283651Speter * Helper functions for former macros.  Some of these should be
90383651Speter * moved to their callers.
9049336Sdfr */
90583651Speter
9065455Sdgint
90783651Speternfsm_mtofh_xx(struct vnode *d, struct vnode **v, int v3, int *f,
90888091Siedowse    struct mbuf **md, caddr_t *dpos)
9099336Sdfr{
91083651Speter	struct nfsnode *ttnp;
91183651Speter	struct vnode *ttvp;
91283651Speter	nfsfh_t *ttfhp;
91388091Siedowse	u_int32_t *tl;
91483651Speter	int ttfhsize;
91583651Speter	int t1;
9169336Sdfr
91783651Speter	if (v3) {
91888091Siedowse		tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
91988091Siedowse		if (tl == NULL)
92084057Speter			return EBADRPC;
92188091Siedowse		*f = fxdr_unsigned(int, *tl);
92283651Speter	} else
92383651Speter		*f = 1;
92483651Speter	if (*f) {
92588091Siedowse		t1 = nfsm_getfh_xx(&ttfhp, &ttfhsize, (v3), md, dpos);
92683651Speter		if (t1 != 0)
92783651Speter			return t1;
928162288Smohans		t1 = nfs_nget(d->v_mount, ttfhp, ttfhsize, &ttnp, LK_EXCLUSIVE);
92983651Speter		if (t1 != 0)
93083651Speter			return t1;
93183651Speter		*v = NFSTOV(ttnp);
93283651Speter	}
93383651Speter	if (v3) {
93488091Siedowse		tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
93588091Siedowse		if (tl == NULL)
93684057Speter			return EBADRPC;
93783651Speter		if (*f)
93888091Siedowse			*f = fxdr_unsigned(int, *tl);
93988091Siedowse		else if (fxdr_unsigned(int, *tl))
94088091Siedowse			nfsm_adv_xx(NFSX_V3FATTR, md, dpos);
94183651Speter	}
94283651Speter	if (*f) {
94383651Speter		ttvp = *v;
94499797Sdillon		t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 0);
94583651Speter		if (t1)
94683651Speter			return t1;
94783651Speter		*v = ttvp;
94883651Speter	}
94983651Speter	return 0;
95083651Speter}
95183651Speter
95283651Speterint
95388091Siedowsenfsm_getfh_xx(nfsfh_t **f, int *s, int v3, struct mbuf **md, caddr_t *dpos)
95483651Speter{
95588091Siedowse	u_int32_t *tl;
95683651Speter
95783651Speter	if (v3) {
95888091Siedowse		tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
95988091Siedowse		if (tl == NULL)
96084057Speter			return EBADRPC;
96188091Siedowse		*s = fxdr_unsigned(int, *tl);
96284057Speter		if (*s <= 0 || *s > NFSX_V3FHMAX)
96383651Speter			return EBADRPC;
96483651Speter	} else
96583651Speter		*s = NFSX_V2FH;
96684057Speter	*f = nfsm_dissect_xx(nfsm_rndup(*s), md, dpos);
96784057Speter	if (*f == NULL)
96884057Speter		return EBADRPC;
96984057Speter	else
97084057Speter		return 0;
97183651Speter}
97283651Speter
97383651Speter
97483651Speterint
97588091Siedowsenfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md,
976158739Smohans		 caddr_t *dpos)
97783651Speter{
97883651Speter	int t1;
97983651Speter
98083651Speter	struct vnode *ttvp = *v;
98183651Speter	t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 0);
98283651Speter	if (t1 != 0)
98383651Speter		return t1;
98483651Speter	*v = ttvp;
98583651Speter	return 0;
98683651Speter}
98783651Speter
98883651Speterint
98988091Siedowsenfsm_postop_attr_xx(struct vnode **v, int *f, struct mbuf **md,
990158739Smohans		    caddr_t *dpos)
99183651Speter{
99288091Siedowse	u_int32_t *tl;
99383651Speter	int t1;
99483651Speter
99583651Speter	struct vnode *ttvp = *v;
99688091Siedowse	tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
99788091Siedowse	if (tl == NULL)
99884057Speter		return EBADRPC;
99988091Siedowse	*f = fxdr_unsigned(int, *tl);
100083914Siedowse	if (*f != 0) {
100199797Sdillon		t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 1);
100283651Speter		if (t1 != 0) {
100383651Speter			*f = 0;
100483651Speter			return t1;
100583651Speter		}
100683651Speter		*v = ttvp;
10079336Sdfr	}
100883651Speter	return 0;
10099336Sdfr}
10109336Sdfr
10119336Sdfrint
101288091Siedowsenfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md, caddr_t *dpos)
101331886Sbde{
101488091Siedowse	u_int32_t *tl;
101583651Speter	int ttattrf, ttretf = 0;
101683651Speter	int t1;
10175455Sdg
101888091Siedowse	tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
101988091Siedowse	if (tl == NULL)
102084057Speter		return EBADRPC;
102188091Siedowse	if (*tl == nfs_true) {
102288091Siedowse		tl = nfsm_dissect_xx(6 * NFSX_UNSIGNED, md, dpos);
102388091Siedowse		if (tl == NULL)
102484057Speter			return EBADRPC;
1025158739Smohans		mtx_lock(&(VTONFS(*v))->n_mtx);
102683651Speter		if (*f)
1027138473Sps 			ttretf = (VTONFS(*v)->n_mtime.tv_sec == fxdr_unsigned(u_int32_t, *(tl + 2)) &&
1028138473Sps				  VTONFS(*v)->n_mtime.tv_nsec == fxdr_unsigned(u_int32_t, *(tl + 3)));
1029158739Smohans		mtx_unlock(&(VTONFS(*v))->n_mtx);
103083651Speter	}
103188091Siedowse	t1 = nfsm_postop_attr_xx(v, &ttattrf, md, dpos);
103283651Speter	if (t1)
103383651Speter		return t1;
103483651Speter	if (*f)
103583651Speter		*f = ttretf;
103683651Speter	else
103783651Speter		*f = ttattrf;
103883651Speter	return 0;
10395455Sdg}
104036503Speter
104183651Speterint
104288091Siedowsenfsm_strtom_xx(const char *a, int s, int m, struct mbuf **mb, caddr_t *bpos)
104336503Speter{
104488091Siedowse	u_int32_t *tl;
104583651Speter	int t1;
104636503Speter
104783651Speter	if (s > m)
104883651Speter		return ENAMETOOLONG;
104983651Speter	t1 = nfsm_rndup(s) + NFSX_UNSIGNED;
105083651Speter	if (t1 <= M_TRAILINGSPACE(*mb)) {
105188091Siedowse		tl = nfsm_build_xx(t1, mb, bpos);
105288091Siedowse		*tl++ = txdr_unsigned(s);
105388091Siedowse		*(tl + ((t1 >> 2) - 2)) = 0;
105488091Siedowse		bcopy(a, tl, s);
105583651Speter	} else {
105683651Speter		t1 = nfsm_strtmbuf(mb, bpos, a, s);
105783651Speter		if (t1 != 0)
105883651Speter			return t1;
105936503Speter	}
106083651Speter	return 0;
106136503Speter}
106236503Speter
106383651Speterint
106488091Siedowsenfsm_fhtom_xx(struct vnode *v, int v3, struct mbuf **mb, caddr_t *bpos)
106583651Speter{
106688091Siedowse	u_int32_t *tl;
106783651Speter	int t1;
106883651Speter	caddr_t cp;
106983651Speter
107083651Speter	if (v3) {
107183651Speter		t1 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED;
107283651Speter		if (t1 < M_TRAILINGSPACE(*mb)) {
107388091Siedowse			tl = nfsm_build_xx(t1, mb, bpos);
107488091Siedowse			*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize);
107588091Siedowse			*(tl + ((t1 >> 2) - 2)) = 0;
107688091Siedowse			bcopy(VTONFS(v)->n_fhp, tl, VTONFS(v)->n_fhsize);
107783651Speter		} else {
107883651Speter			t1 = nfsm_strtmbuf(mb, bpos,
107983651Speter			    (const char *)VTONFS(v)->n_fhp,
108083651Speter			    VTONFS(v)->n_fhsize);
108183651Speter			if (t1 != 0)
108283651Speter				return t1;
108383651Speter		}
108483651Speter	} else {
108584002Speter		cp = nfsm_build_xx(NFSX_V2FH, mb, bpos);
108683651Speter		bcopy(VTONFS(v)->n_fhp, cp, NFSX_V2FH);
108783651Speter	}
108883651Speter	return 0;
108983651Speter}
109083651Speter
109136503Spetervoid
109288091Siedowsenfsm_v3attrbuild_xx(struct vattr *va, int full, struct mbuf **mb,
109388091Siedowse    caddr_t *bpos)
109436503Speter{
109588091Siedowse	u_int32_t *tl;
109636503Speter
109783651Speter	if (va->va_mode != (mode_t)VNOVAL) {
109888091Siedowse		tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
109988091Siedowse		*tl++ = nfs_true;
110088091Siedowse		*tl = txdr_unsigned(va->va_mode);
110183651Speter	} else {
110288091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
110388091Siedowse		*tl = nfs_false;
110483651Speter	}
110583651Speter	if (full && va->va_uid != (uid_t)VNOVAL) {
110688091Siedowse		tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
110788091Siedowse		*tl++ = nfs_true;
110888091Siedowse		*tl = txdr_unsigned(va->va_uid);
110983651Speter	} else {
111088091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
111188091Siedowse		*tl = nfs_false;
111283651Speter	}
111383651Speter	if (full && va->va_gid != (gid_t)VNOVAL) {
111488091Siedowse		tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
111588091Siedowse		*tl++ = nfs_true;
111688091Siedowse		*tl = txdr_unsigned(va->va_gid);
111783651Speter	} else {
111888091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
111988091Siedowse		*tl = nfs_false;
112083651Speter	}
112183651Speter	if (full && va->va_size != VNOVAL) {
112288091Siedowse		tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
112388091Siedowse		*tl++ = nfs_true;
112488091Siedowse		txdr_hyper(va->va_size, tl);
112583651Speter	} else {
112688091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
112788091Siedowse		*tl = nfs_false;
112883651Speter	}
112983651Speter	if (va->va_atime.tv_sec != VNOVAL) {
113083651Speter		if (va->va_atime.tv_sec != time_second) {
113188091Siedowse			tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
113288091Siedowse			*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
113388091Siedowse			txdr_nfsv3time(&va->va_atime, tl);
113483651Speter		} else {
113588091Siedowse			tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
113688091Siedowse			*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
113783651Speter		}
113883651Speter	} else {
113988091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
114088091Siedowse		*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
114183651Speter	}
114283651Speter	if (va->va_mtime.tv_sec != VNOVAL) {
114383651Speter		if (va->va_mtime.tv_sec != time_second) {
114488091Siedowse			tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
114588091Siedowse			*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
114688091Siedowse			txdr_nfsv3time(&va->va_mtime, tl);
114783651Speter		} else {
114888091Siedowse			tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
114988091Siedowse			*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
115083651Speter		}
115183651Speter	} else {
115288091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
115388091Siedowse		*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
115483651Speter	}
115536503Speter}
1156