nfs_subs.c revision 220595
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 220595 2011-04-13 12:46:25Z ru $");
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>
62212506Skib#include <sys/taskqueue.h>
631541Srgrimes
643305Sphk#include <vm/vm.h>
6512662Sdg#include <vm/vm_object.h>
6612662Sdg#include <vm/vm_extern.h>
6792783Sjeff#include <vm/uma.h>
683305Sphk
699336Sdfr#include <nfs/nfsproto.h>
7083651Speter#include <nfsclient/nfs.h>
7183651Speter#include <nfsclient/nfsnode.h>
72190380Srwatson#include <nfsclient/nfs_kdtrace.h>
731541Srgrimes#include <nfs/xdr_subs.h>
7483651Speter#include <nfsclient/nfsm_subs.h>
7583651Speter#include <nfsclient/nfsmount.h>
761541Srgrimes
771541Srgrimes#include <netinet/in.h>
781541Srgrimes
791541Srgrimes/*
80158739Smohans * Note that stdarg.h and the ANSI style va_start macro is used for both
81158739Smohans * ANSI and traditional C compilers.
82158739Smohans */
83158739Smohans#include <machine/stdarg.h>
84158739Smohans
85190380Srwatson#ifdef KDTRACE_HOOKS
86190380Srwatsondtrace_nfsclient_attrcache_flush_probe_func_t
87190380Srwatson    dtrace_nfsclient_attrcache_flush_done_probe;
88190380Srwatsonuint32_t nfsclient_attrcache_flush_done_id;
89190380Srwatson
90190380Srwatsondtrace_nfsclient_attrcache_get_hit_probe_func_t
91190380Srwatson    dtrace_nfsclient_attrcache_get_hit_probe;
92190380Srwatsonuint32_t nfsclient_attrcache_get_hit_id;
93190380Srwatson
94190380Srwatsondtrace_nfsclient_attrcache_get_miss_probe_func_t
95190380Srwatson    dtrace_nfsclient_attrcache_get_miss_probe;
96190380Srwatsonuint32_t nfsclient_attrcache_get_miss_id;
97190380Srwatson
98190380Srwatsondtrace_nfsclient_attrcache_load_probe_func_t
99190380Srwatson    dtrace_nfsclient_attrcache_load_done_probe;
100190380Srwatsonuint32_t nfsclient_attrcache_load_done_id;
101190380Srwatson#endif /* !KDTRACE_HOOKS */
102190380Srwatson
103158739Smohans/*
1041541Srgrimes * Data items converted to xdr at startup, since they are constant
1051541Srgrimes * This is kinda hokey, but may save a little time doing byte swaps
1061541Srgrimes */
10783651Speteru_int32_t	nfs_xdrneg1;
10883651Speteru_int32_t	nfs_true, nfs_false;
1091541Srgrimes
1101541Srgrimes/* And other global data */
111176224Sjhbstatic u_int32_t nfs_xid = 0;
11212911Sphkstatic enum vtype nv2tov_type[8]= {
11383651Speter	VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON,  VNON
11412911Sphk};
11512911Sphk
11683651Speterint		nfs_ticks;
11783651Speterint		nfs_pbuf_freecnt = -1;	/* start out unlimited */
1189336Sdfr
11983651Speterstruct nfs_bufq	nfs_bufq;
120176224Sjhbstatic struct mtx nfs_xid_mtx;
121212506Skibstruct task	nfs_nfsiodnew_task;
1229759Sbde
1239336Sdfr/*
1249336Sdfr * and the reverse mapping from generic to Version 2 procedure numbers
1259336Sdfr */
1269336Sdfrint nfsv2_procid[NFS_NPROCS] = {
1279336Sdfr	NFSV2PROC_NULL,
1289336Sdfr	NFSV2PROC_GETATTR,
1299336Sdfr	NFSV2PROC_SETATTR,
1309336Sdfr	NFSV2PROC_LOOKUP,
1319336Sdfr	NFSV2PROC_NOOP,
1329336Sdfr	NFSV2PROC_READLINK,
1339336Sdfr	NFSV2PROC_READ,
1349336Sdfr	NFSV2PROC_WRITE,
1359336Sdfr	NFSV2PROC_CREATE,
1369336Sdfr	NFSV2PROC_MKDIR,
1379336Sdfr	NFSV2PROC_SYMLINK,
1389336Sdfr	NFSV2PROC_CREATE,
1399336Sdfr	NFSV2PROC_REMOVE,
1409336Sdfr	NFSV2PROC_RMDIR,
1419336Sdfr	NFSV2PROC_RENAME,
1429336Sdfr	NFSV2PROC_LINK,
1439336Sdfr	NFSV2PROC_READDIR,
1449336Sdfr	NFSV2PROC_NOOP,
1459336Sdfr	NFSV2PROC_STATFS,
1469336Sdfr	NFSV2PROC_NOOP,
1479336Sdfr	NFSV2PROC_NOOP,
1489336Sdfr	NFSV2PROC_NOOP,
1499336Sdfr	NFSV2PROC_NOOP,
1509336Sdfr};
1519336Sdfr
15260938SjakeLIST_HEAD(nfsnodehashhead, nfsnode);
1533664Sphk
154176224Sjhbu_int32_t
155176224Sjhbnfs_xid_gen(void)
156176224Sjhb{
157176224Sjhb	uint32_t xid;
158176224Sjhb
159176224Sjhb	mtx_lock(&nfs_xid_mtx);
160176224Sjhb
161176224Sjhb	/* Get a pretty random xid to start with */
162176224Sjhb	if (!nfs_xid)
163176224Sjhb		nfs_xid = random();
164176224Sjhb	/*
165176224Sjhb	 * Skip zero xid if it should ever happen.
166176224Sjhb	 */
167176224Sjhb	if (++nfs_xid == 0)
168176224Sjhb		nfs_xid++;
169176224Sjhb	xid = nfs_xid;
170176224Sjhb	mtx_unlock(&nfs_xid_mtx);
171176224Sjhb	return xid;
172176224Sjhb}
173176224Sjhb
1741541Srgrimes/*
1751541Srgrimes * Create the header for an rpc request packet
1761541Srgrimes * The hsiz is the size of the rest of the nfs request header.
1771541Srgrimes * (just used to decide if a cluster is a good idea)
1781541Srgrimes */
1791541Srgrimesstruct mbuf *
18083651Speternfsm_reqhead(struct vnode *vp, u_long procid, int hsiz)
1811541Srgrimes{
18283651Speter	struct mbuf *mb;
1831541Srgrimes
184177599Sru	MGET(mb, M_WAIT, MT_DATA);
1851541Srgrimes	if (hsiz >= MINCLSIZE)
186177599Sru		MCLGET(mb, M_WAIT);
1871541Srgrimes	mb->m_len = 0;
1881541Srgrimes	return (mb);
1891541Srgrimes}
1901541Srgrimes
1911541Srgrimes/*
19217186Sdfr * copies a uio scatter/gather list to an mbuf chain.
19317186Sdfr * NOTE: can ony handle iovcnt == 1
1941541Srgrimes */
1951549Srgrimesint
19683651Speternfsm_uiotombuf(struct uio *uiop, struct mbuf **mq, int siz, caddr_t *bpos)
1971541Srgrimes{
19883651Speter	char *uiocp;
19983651Speter	struct mbuf *mp, *mp2;
20083651Speter	int xfer, left, mlen;
2011541Srgrimes	int uiosiz, clflg, rem;
2021541Srgrimes	char *cp;
2031541Srgrimes
204209120Skib	KASSERT(uiop->uio_iovcnt == 1, ("nfsm_uiotombuf: iovcnt != 1"));
20517186Sdfr
2061541Srgrimes	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
2071541Srgrimes		clflg = 1;
2081541Srgrimes	else
2091541Srgrimes		clflg = 0;
2101541Srgrimes	rem = nfsm_rndup(siz)-siz;
2111541Srgrimes	mp = mp2 = *mq;
2121541Srgrimes	while (siz > 0) {
2131541Srgrimes		left = uiop->uio_iov->iov_len;
2141541Srgrimes		uiocp = uiop->uio_iov->iov_base;
2151541Srgrimes		if (left > siz)
2161541Srgrimes			left = siz;
2171541Srgrimes		uiosiz = left;
2181541Srgrimes		while (left > 0) {
2191541Srgrimes			mlen = M_TRAILINGSPACE(mp);
2201541Srgrimes			if (mlen == 0) {
221177599Sru				MGET(mp, M_WAIT, MT_DATA);
2221541Srgrimes				if (clflg)
223177599Sru					MCLGET(mp, M_WAIT);
2241541Srgrimes				mp->m_len = 0;
2251541Srgrimes				mp2->m_next = mp;
2261541Srgrimes				mp2 = mp;
2271541Srgrimes				mlen = M_TRAILINGSPACE(mp);
2281541Srgrimes			}
2291541Srgrimes			xfer = (left > mlen) ? mlen : left;
2301541Srgrimes#ifdef notdef
2311541Srgrimes			/* Not Yet.. */
2321541Srgrimes			if (uiop->uio_iov->iov_op != NULL)
2331541Srgrimes				(*(uiop->uio_iov->iov_op))
2341541Srgrimes				(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
2351541Srgrimes			else
2361541Srgrimes#endif
2371541Srgrimes			if (uiop->uio_segflg == UIO_SYSSPACE)
2381541Srgrimes				bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
2391541Srgrimes			else
2401541Srgrimes				copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
2411541Srgrimes			mp->m_len += xfer;
2421541Srgrimes			left -= xfer;
2431541Srgrimes			uiocp += xfer;
2441541Srgrimes			uiop->uio_offset += xfer;
2451541Srgrimes			uiop->uio_resid -= xfer;
2461541Srgrimes		}
247104908Smike		uiop->uio_iov->iov_base =
248104908Smike		    (char *)uiop->uio_iov->iov_base + uiosiz;
24917186Sdfr		uiop->uio_iov->iov_len -= uiosiz;
2501541Srgrimes		siz -= uiosiz;
2511541Srgrimes	}
2521541Srgrimes	if (rem > 0) {
2531541Srgrimes		if (rem > M_TRAILINGSPACE(mp)) {
254177599Sru			MGET(mp, M_WAIT, MT_DATA);
2551541Srgrimes			mp->m_len = 0;
2561541Srgrimes			mp2->m_next = mp;
2571541Srgrimes		}
2581541Srgrimes		cp = mtod(mp, caddr_t)+mp->m_len;
2591541Srgrimes		for (left = 0; left < rem; left++)
2601541Srgrimes			*cp++ = '\0';
2611541Srgrimes		mp->m_len += rem;
2621541Srgrimes		*bpos = cp;
2631541Srgrimes	} else
2641541Srgrimes		*bpos = mtod(mp, caddr_t)+mp->m_len;
2651541Srgrimes	*mq = mp;
2661541Srgrimes	return (0);
2671541Srgrimes}
2681541Srgrimes
2691541Srgrimes/*
2701541Srgrimes * Copy a string into mbufs for the hard cases...
2711541Srgrimes */
2721549Srgrimesint
27383651Speternfsm_strtmbuf(struct mbuf **mb, char **bpos, const char *cp, long siz)
2741541Srgrimes{
27583651Speter	struct mbuf *m1 = NULL, *m2;
2761541Srgrimes	long left, xfer, len, tlen;
27736541Speter	u_int32_t *tl;
2781541Srgrimes	int putsize;
2791541Srgrimes
2801541Srgrimes	putsize = 1;
2811541Srgrimes	m2 = *mb;
2821541Srgrimes	left = M_TRAILINGSPACE(m2);
2831541Srgrimes	if (left > 0) {
28436541Speter		tl = ((u_int32_t *)(*bpos));
2851541Srgrimes		*tl++ = txdr_unsigned(siz);
2861541Srgrimes		putsize = 0;
2871541Srgrimes		left -= NFSX_UNSIGNED;
2881541Srgrimes		m2->m_len += NFSX_UNSIGNED;
2891541Srgrimes		if (left > 0) {
2901541Srgrimes			bcopy(cp, (caddr_t) tl, left);
2911541Srgrimes			siz -= left;
2921541Srgrimes			cp += left;
2931541Srgrimes			m2->m_len += left;
2941541Srgrimes			left = 0;
2951541Srgrimes		}
2961541Srgrimes	}
2971541Srgrimes	/* Loop around adding mbufs */
2981541Srgrimes	while (siz > 0) {
299177599Sru		MGET(m1, M_WAIT, MT_DATA);
3001541Srgrimes		if (siz > MLEN)
301177599Sru			MCLGET(m1, M_WAIT);
3021541Srgrimes		m1->m_len = NFSMSIZ(m1);
3031541Srgrimes		m2->m_next = m1;
3041541Srgrimes		m2 = m1;
30536541Speter		tl = mtod(m1, u_int32_t *);
3061541Srgrimes		tlen = 0;
3071541Srgrimes		if (putsize) {
3081541Srgrimes			*tl++ = txdr_unsigned(siz);
3091541Srgrimes			m1->m_len -= NFSX_UNSIGNED;
3101541Srgrimes			tlen = NFSX_UNSIGNED;
3111541Srgrimes			putsize = 0;
3121541Srgrimes		}
3131541Srgrimes		if (siz < m1->m_len) {
3141541Srgrimes			len = nfsm_rndup(siz);
3151541Srgrimes			xfer = siz;
3161541Srgrimes			if (xfer < len)
3171541Srgrimes				*(tl+(xfer>>2)) = 0;
3181541Srgrimes		} else {
3191541Srgrimes			xfer = len = m1->m_len;
3201541Srgrimes		}
3211541Srgrimes		bcopy(cp, (caddr_t) tl, xfer);
3221541Srgrimes		m1->m_len = len+tlen;
3231541Srgrimes		siz -= xfer;
3241541Srgrimes		cp += xfer;
3251541Srgrimes	}
3261541Srgrimes	*mb = m1;
3271541Srgrimes	*bpos = mtod(m1, caddr_t)+m1->m_len;
3281541Srgrimes	return (0);
3291541Srgrimes}
3301541Srgrimes
3311541Srgrimes/*
3321541Srgrimes * Called once to initialize data structures...
3331541Srgrimes */
3341549Srgrimesint
33583651Speternfs_init(struct vfsconf *vfsp)
3361541Srgrimes{
33783651Speter	int i;
3381541Srgrimes
33992783Sjeff	nfsmount_zone = uma_zcreate("NFSMOUNT", sizeof(struct nfsmount),
34092783Sjeff	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
3411541Srgrimes	nfs_true = txdr_unsigned(TRUE);
3421541Srgrimes	nfs_false = txdr_unsigned(FALSE);
3433664Sphk	nfs_xdrneg1 = txdr_unsigned(-1);
3449336Sdfr	nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
3459336Sdfr	if (nfs_ticks < 1)
3469336Sdfr		nfs_ticks = 1;
3471541Srgrimes	/* Ensure async daemons disabled */
34819449Sdfr	for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
349203072Srmacklem		nfs_iodwant[i] = NFSIOD_NOT_AVAILABLE;
35099797Sdillon		nfs_iodmount[i] = NULL;
35119449Sdfr	}
3521541Srgrimes	nfs_nhinit();			/* Init the nfsnode table */
3531541Srgrimes
3541541Srgrimes	/*
3551541Srgrimes	 * Initialize reply list and start timer
3561541Srgrimes	 */
357158739Smohans	mtx_init(&nfs_iod_mtx, "NFS iod lock", NULL, MTX_DEF);
358172600Smohans	mtx_init(&nfs_xid_mtx, "NFS xid lock", NULL, MTX_DEF);
359212506Skib	TASK_INIT(&nfs_nfsiodnew_task, 0, nfs_nfsiodnew_tq, NULL);
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.
374212506Skib	 * Drain nfsiodnew task before we wait for them to finish.
375128111Speadar	 */
376158739Smohans	mtx_lock(&nfs_iod_mtx);
377128111Speadar	nfs_iodmax = 0;
378212506Skib	mtx_unlock(&nfs_iod_mtx);
379212506Skib	taskqueue_drain(taskqueue_thread, &nfs_nfsiodnew_task);
380212506Skib	mtx_lock(&nfs_iod_mtx);
381128111Speadar	for (i = 0; i < nfs_numasync; i++)
382203072Srmacklem		if (nfs_iodwant[i] == NFSIOD_AVAILABLE)
383128111Speadar			wakeup(&nfs_iodwant[i]);
384128111Speadar	/* The last nfsiod to exit will wake us up when nfs_numasync hits 0 */
385128111Speadar	while (nfs_numasync)
386158739Smohans		msleep(&nfs_numasync, &nfs_iod_mtx, PWAIT, "ioddie", 0);
387158739Smohans	mtx_unlock(&nfs_iod_mtx);
388128111Speadar	nfs_nhuninit();
389128111Speadar	uma_zdestroy(nfsmount_zone);
39038894Sbde	return (0);
39138894Sbde}
39238894Sbde
393158739Smohansvoid
394158739Smohansnfs_dircookie_lock(struct nfsnode *np)
395158739Smohans{
396158739Smohans	mtx_lock(&np->n_mtx);
397158739Smohans	while (np->n_flag & NDIRCOOKIELK)
398158739Smohans		(void) msleep(&np->n_flag, &np->n_mtx, PZERO, "nfsdirlk", 0);
399158739Smohans	np->n_flag |= NDIRCOOKIELK;
400158739Smohans	mtx_unlock(&np->n_mtx);
401158739Smohans}
402158739Smohans
403158739Smohansvoid
404158739Smohansnfs_dircookie_unlock(struct nfsnode *np)
405158739Smohans{
406158739Smohans	mtx_lock(&np->n_mtx);
407158739Smohans	np->n_flag &= ~NDIRCOOKIELK;
408158739Smohans	wakeup(&np->n_flag);
409158739Smohans	mtx_unlock(&np->n_mtx);
410158739Smohans}
411158739Smohans
412158739Smohansint
413176134Sattilionfs_upgrade_vnlock(struct vnode *vp)
414158739Smohans{
415158739Smohans	int old_lock;
416196205Skib
417196205Skib	ASSERT_VOP_LOCKED(vp, "nfs_upgrade_vnlock");
418196205Skib	old_lock = VOP_ISLOCKED(vp);
419196205Skib	if (old_lock != LK_EXCLUSIVE) {
420196205Skib		KASSERT(old_lock == LK_SHARED,
421196205Skib		    ("nfs_upgrade_vnlock: wrong old_lock %d", old_lock));
422196205Skib		/* Upgrade to exclusive lock, this might block */
423196205Skib		vn_lock(vp, LK_UPGRADE | LK_RETRY);
424158739Smohans  	}
425196205Skib	return (old_lock);
426158739Smohans}
427158739Smohans
428158739Smohansvoid
429176134Sattilionfs_downgrade_vnlock(struct vnode *vp, int old_lock)
430158739Smohans{
431158739Smohans	if (old_lock != LK_EXCLUSIVE) {
432196205Skib		KASSERT(old_lock == LK_SHARED, ("wrong old_lock %d", old_lock));
433196205Skib		/* Downgrade from exclusive lock. */
434196205Skib		vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
435158739Smohans  	}
436158739Smohans}
437158739Smohans
438158739Smohansvoid
439158739Smohansnfs_printf(const char *fmt, ...)
440158739Smohans{
441158739Smohans	va_list ap;
442158739Smohans
443158739Smohans	mtx_lock(&Giant);
444158739Smohans	va_start(ap, fmt);
445220595Sru	vprintf(fmt, ap);
446158739Smohans	va_end(ap);
447158739Smohans	mtx_unlock(&Giant);
448158739Smohans}
449158739Smohans
4501541Srgrimes/*
4511541Srgrimes * Attribute cache routines.
4521541Srgrimes * nfs_loadattrcache() - loads or updates the cache contents from attributes
4531541Srgrimes *	that are on the mbuf list
4541541Srgrimes * nfs_getattrcache() - returns valid attributes if found in cache, returns
4551541Srgrimes *	error otherwise
4561541Srgrimes */
4571541Srgrimes
4581541Srgrimes/*
4591541Srgrimes * Load the attribute cache (that lives in the nfsnode entry) with
4601541Srgrimes * the values on the mbuf list and
4611541Srgrimes * Iff vap not NULL
4621541Srgrimes *    copy the attributes to *vaper
4631541Srgrimes */
4641549Srgrimesint
46583651Speternfs_loadattrcache(struct vnode **vpp, struct mbuf **mdp, caddr_t *dposp,
466158739Smohans		  struct vattr *vaper, int dontshrink)
4671541Srgrimes{
46883651Speter	struct vnode *vp = *vpp;
46983651Speter	struct vattr *vap;
47083651Speter	struct nfs_fattr *fp;
471190380Srwatson	struct nfsnode *np = NULL;
47283651Speter	int32_t t1;
4739336Sdfr	caddr_t cp2;
47484057Speter	int rdev;
4751541Srgrimes	struct mbuf *md;
4761541Srgrimes	enum vtype vtyp;
4771541Srgrimes	u_short vmode;
478171190Sjhb	struct timespec mtime, mtime_save;
4799336Sdfr	int v3 = NFS_ISV3(vp);
480190380Srwatson	int error = 0;
4811541Srgrimes
4821541Srgrimes	md = *mdp;
4839336Sdfr	t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
484177599Sru	cp2 = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, M_WAIT);
485190380Srwatson	if (cp2 == NULL) {
486190380Srwatson		error = EBADRPC;
487190380Srwatson		goto out;
488190380Srwatson	}
4899336Sdfr	fp = (struct nfs_fattr *)cp2;
4909336Sdfr	if (v3) {
4919336Sdfr		vtyp = nfsv3tov_type(fp->fa_type);
4929336Sdfr		vmode = fxdr_unsigned(u_short, fp->fa_mode);
493130640Sphk		rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
49416634Sbde			fxdr_unsigned(int, fp->fa3_rdev.specdata2));
4959336Sdfr		fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
4961541Srgrimes	} else {
4979336Sdfr		vtyp = nfsv2tov_type(fp->fa_type);
4989336Sdfr		vmode = fxdr_unsigned(u_short, fp->fa_mode);
4999336Sdfr		/*
5009336Sdfr		 * XXX
5019336Sdfr		 *
5029336Sdfr		 * The duplicate information returned in fa_type and fa_mode
5039336Sdfr		 * is an ambiguity in the NFS version 2 protocol.
5049336Sdfr		 *
5059336Sdfr		 * VREG should be taken literally as a regular file.  If a
5069336Sdfr		 * server intents to return some type information differently
5079336Sdfr		 * in the upper bits of the mode field (e.g. for sockets, or
5089336Sdfr		 * FIFOs), NFSv2 mandates fa_type to be VNON.  Anyway, we
5099336Sdfr		 * leave the examination of the mode bits even in the VREG
5109336Sdfr		 * case to avoid breakage for bogus servers, but we make sure
5119336Sdfr		 * that there are actually type bits set in the upper part of
5129336Sdfr		 * fa_mode (and failing that, trust the va_type field).
5139336Sdfr		 *
5149336Sdfr		 * NFSv3 cleared the issue, and requires fa_mode to not
5159336Sdfr		 * contain any type information (while also introduing sockets
5169336Sdfr		 * and FIFOs for fa_type).
5179336Sdfr		 */
5189336Sdfr		if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
5199336Sdfr			vtyp = IFTOVT(vmode);
52036541Speter		rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
5219336Sdfr		fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
5229336Sdfr
5239336Sdfr		/*
5249336Sdfr		 * Really ugly NFSv2 kludge.
5259336Sdfr		 */
5269336Sdfr		if (vtyp == VCHR && rdev == 0xffffffff)
5279336Sdfr			vtyp = VFIFO;
5281541Srgrimes	}
5299336Sdfr
5301541Srgrimes	/*
5311541Srgrimes	 * If v_type == VNON it is a new node, so fill in the v_type,
5328876Srgrimes	 * n_mtime fields. Check to see if it represents a special
5331541Srgrimes	 * device, and if so, check for a possible alias. Once the
5341541Srgrimes	 * correct vnode has been obtained, fill in the rest of the
5351541Srgrimes	 * information.
5361541Srgrimes	 */
5371541Srgrimes	np = VTONFS(vp);
538158739Smohans	mtx_lock(&np->n_mtx);
53910219Sdfr	if (vp->v_type != vtyp) {
5409336Sdfr		vp->v_type = vtyp;
541126851Sphk		if (vp->v_type == VFIFO)
542138290Sphk			vp->v_op = &nfs_fifoops;
543138473Sps		np->n_mtime = mtime;
5441541Srgrimes	}
5451541Srgrimes	vap = &np->n_vattr;
5461541Srgrimes	vap->va_type = vtyp;
5471541Srgrimes	vap->va_mode = (vmode & 07777);
54847028Sphk	vap->va_rdev = rdev;
549171190Sjhb	mtime_save = vap->va_mtime;
5501541Srgrimes	vap->va_mtime = mtime;
5511541Srgrimes	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
5529336Sdfr	if (v3) {
5539336Sdfr		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
5549336Sdfr		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
5559336Sdfr		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
55647751Speter		vap->va_size = fxdr_hyper(&fp->fa3_size);
5579336Sdfr		vap->va_blocksize = NFS_FABLKSIZE;
55847751Speter		vap->va_bytes = fxdr_hyper(&fp->fa3_used);
55936541Speter		vap->va_fileid = fxdr_unsigned(int32_t,
56036541Speter		    fp->fa3_fileid.nfsuquad[1]);
5619336Sdfr		fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
5629336Sdfr		fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
5639336Sdfr		vap->va_flags = 0;
5649336Sdfr		vap->va_filerev = 0;
5651541Srgrimes	} else {
5669336Sdfr		vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
5679336Sdfr		vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
5689336Sdfr		vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
56936541Speter		vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
57036541Speter		vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
57147751Speter		vap->va_bytes = (u_quad_t)fxdr_unsigned(int32_t, fp->fa2_blocks)
57236541Speter		    * NFS_FABLKSIZE;
57336541Speter		vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
5749336Sdfr		fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
5751541Srgrimes		vap->va_flags = 0;
57636541Speter		vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
57736541Speter		    fp->fa2_ctime.nfsv2_sec);
57818397Snate		vap->va_ctime.tv_nsec = 0;
57983651Speter		vap->va_gen = fxdr_unsigned(u_int32_t, fp->fa2_ctime.nfsv2_usec);
5801541Srgrimes		vap->va_filerev = 0;
5811541Srgrimes	}
58267486Sdwmalone	np->n_attrstamp = time_second;
5831541Srgrimes	if (vap->va_size != np->n_size) {
5841541Srgrimes		if (vap->va_type == VREG) {
58567486Sdwmalone			if (dontshrink && vap->va_size < np->n_size) {
58667486Sdwmalone				/*
58767486Sdwmalone				 * We've been told not to shrink the file;
58867486Sdwmalone				 * zero np->n_attrstamp to indicate that
58967486Sdwmalone				 * the attributes are stale.
59067486Sdwmalone				 */
59167486Sdwmalone				vap->va_size = np->n_size;
59267486Sdwmalone				np->n_attrstamp = 0;
593190380Srwatson				KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
59467486Sdwmalone			} else if (np->n_flag & NMODIFIED) {
595128263Speadar				/*
596128263Speadar				 * We've modified the file: Use the larger
597128263Speadar				 * of our size, and the server's size.
598128263Speadar				 */
599128263Speadar				if (vap->va_size < np->n_size) {
6001541Srgrimes					vap->va_size = np->n_size;
601128263Speadar				} else {
6021541Srgrimes					np->n_size = vap->va_size;
603128263Speadar					np->n_flag |= NSIZECHANGED;
604128263Speadar				}
60554480Sdillon			} else {
6061541Srgrimes				np->n_size = vap->va_size;
607128263Speadar				np->n_flag |= NSIZECHANGED;
60854480Sdillon			}
60941026Speter			vnode_pager_setsize(vp, np->n_size);
61054480Sdillon		} else {
6111541Srgrimes			np->n_size = vap->va_size;
61254480Sdillon		}
6131541Srgrimes	}
614171190Sjhb	/*
615171190Sjhb	 * The following checks are added to prevent a race between (say)
616171190Sjhb	 * a READDIR+ and a WRITE.
617171190Sjhb	 * READDIR+, WRITE requests sent out.
618171190Sjhb	 * READDIR+ resp, WRITE resp received on client.
619171190Sjhb	 * However, the WRITE resp was handled before the READDIR+ resp
620171190Sjhb	 * causing the post op attrs from the write to be loaded first
621171190Sjhb	 * and the attrs from the READDIR+ to be loaded later. If this
622171190Sjhb	 * happens, we have stale attrs loaded into the attrcache.
623171190Sjhb	 * We detect this by for the mtime moving back. We invalidate the
624171190Sjhb	 * attrcache when this happens.
625171190Sjhb	 */
626190380Srwatson	if (timespeccmp(&mtime_save, &vap->va_mtime, >)) {
627171190Sjhb		/* Size changed or mtime went backwards */
628171190Sjhb		np->n_attrstamp = 0;
629190380Srwatson		KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
630190380Srwatson	}
6311541Srgrimes	if (vaper != NULL) {
6321541Srgrimes		bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
6331541Srgrimes		if (np->n_flag & NCHG) {
6349336Sdfr			if (np->n_flag & NACC)
6359336Sdfr				vaper->va_atime = np->n_atim;
6369336Sdfr			if (np->n_flag & NUPD)
6379336Sdfr				vaper->va_mtime = np->n_mtim;
6381541Srgrimes		}
6391541Srgrimes	}
640190396Srwatson
641190396Srwatson#ifdef KDTRACE_HOOKS
642190396Srwatson	if (np->n_attrstamp != 0)
643190396Srwatson		KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, &np->n_vattr, 0);
644190396Srwatson#endif
645158739Smohans	mtx_unlock(&np->n_mtx);
646190380Srwatsonout:
647190396Srwatson#ifdef KDTRACE_HOOKS
648190396Srwatson	if (error)
649190396Srwatson		KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, NULL, error);
650190380Srwatson#endif
651190380Srwatson	return (error);
6521541Srgrimes}
6531541Srgrimes
65436176Speter#ifdef NFS_ACDEBUG
65536176Speter#include <sys/sysctl.h>
65644101SbdeSYSCTL_DECL(_vfs_nfs);
65736176Speterstatic int nfs_acdebug;
658184561StrhodesSYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0,
659220595Sru    "Toggle acdebug (attribute cache debug) flag");
66036176Speter#endif
66136176Speter
6621541Srgrimes/*
6631541Srgrimes * Check the time stamp
6641541Srgrimes * If the cache is valid, copy contents to *vap and return 0
6651541Srgrimes * otherwise return an error
6661541Srgrimes */
6671549Srgrimesint
66883651Speternfs_getattrcache(struct vnode *vp, struct vattr *vaper)
6691541Srgrimes{
67083651Speter	struct nfsnode *np;
67183651Speter	struct vattr *vap;
67236176Speter	struct nfsmount *nmp;
67336176Speter	int timeo;
674158739Smohans
67536176Speter	np = VTONFS(vp);
67636176Speter	vap = &np->n_vattr;
67736176Speter	nmp = VFSTONFS(vp->v_mount);
678158739Smohans#ifdef NFS_ACDEBUG
679158739Smohans	mtx_lock(&Giant);	/* nfs_printf() */
680158739Smohans#endif
681158739Smohans	mtx_lock(&np->n_mtx);
68236176Speter	/* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
683138473Sps	timeo = (time_second - np->n_mtime.tv_sec) / 10;
68436176Speter
68536176Speter#ifdef NFS_ACDEBUG
68636176Speter	if (nfs_acdebug>1)
687158739Smohans		nfs_printf("nfs_getattrcache: initial timeo = %d\n", timeo);
68836176Speter#endif
68936176Speter
69036176Speter	if (vap->va_type == VDIR) {
69136176Speter		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
69236176Speter			timeo = nmp->nm_acdirmin;
69336176Speter		else if (timeo > nmp->nm_acdirmax)
69436176Speter			timeo = nmp->nm_acdirmax;
69536176Speter	} else {
69636176Speter		if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
69736176Speter			timeo = nmp->nm_acregmin;
69836176Speter		else if (timeo > nmp->nm_acregmax)
69936176Speter			timeo = nmp->nm_acregmax;
70036176Speter	}
70136176Speter
70236176Speter#ifdef NFS_ACDEBUG
70336176Speter	if (nfs_acdebug > 2)
704158739Smohans		nfs_printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
705158739Smohans			   nmp->nm_acregmin, nmp->nm_acregmax,
706158739Smohans			   nmp->nm_acdirmin, nmp->nm_acdirmax);
70736176Speter
70836176Speter	if (nfs_acdebug)
709158739Smohans		nfs_printf("nfs_getattrcache: age = %d; final timeo = %d\n",
710158739Smohans			   (time_second - np->n_attrstamp), timeo);
71136176Speter#endif
71236176Speter
71336176Speter	if ((time_second - np->n_attrstamp) >= timeo) {
7141541Srgrimes		nfsstats.attrcache_misses++;
715158739Smohans		mtx_unlock(&np->n_mtx);
716220595Sru#ifdef NFS_ACDEBUG
717220595Sru		mtx_unlock(&Giant);	/* nfs_printf() */
718220595Sru#endif
719190380Srwatson		KDTRACE_NFS_ATTRCACHE_GET_MISS(vp);
720190380Srwatson		return (ENOENT);
7211541Srgrimes	}
7221541Srgrimes	nfsstats.attrcache_hits++;
7231541Srgrimes	if (vap->va_size != np->n_size) {
7241541Srgrimes		if (vap->va_type == VREG) {
7251541Srgrimes			if (np->n_flag & NMODIFIED) {
7261541Srgrimes				if (vap->va_size < np->n_size)
7271541Srgrimes					vap->va_size = np->n_size;
7281541Srgrimes				else
7291541Srgrimes					np->n_size = vap->va_size;
73054480Sdillon			} else {
7311541Srgrimes				np->n_size = vap->va_size;
73254480Sdillon			}
73341026Speter			vnode_pager_setsize(vp, np->n_size);
73454480Sdillon		} else {
7351541Srgrimes			np->n_size = vap->va_size;
73654480Sdillon		}
7371541Srgrimes	}
7381541Srgrimes	bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
7391541Srgrimes	if (np->n_flag & NCHG) {
7409336Sdfr		if (np->n_flag & NACC)
7419336Sdfr			vaper->va_atime = np->n_atim;
7429336Sdfr		if (np->n_flag & NUPD)
7439336Sdfr			vaper->va_mtime = np->n_mtim;
7441541Srgrimes	}
745158739Smohans	mtx_unlock(&np->n_mtx);
746158739Smohans#ifdef NFS_ACDEBUG
747158739Smohans	mtx_unlock(&Giant);	/* nfs_printf() */
748158739Smohans#endif
749190380Srwatson	KDTRACE_NFS_ATTRCACHE_GET_HIT(vp, vap);
7501541Srgrimes	return (0);
7511541Srgrimes}
7521541Srgrimes
753190785Sjhb/*
754190785Sjhb * Purge all cached information about an NFS vnode including name
755190785Sjhb * cache entries, the attribute cache, and the access cache.  This is
756190785Sjhb * called when an NFS request for a node fails with a stale
757190785Sjhb * filehandle.
758190785Sjhb */
759190785Sjhbvoid
760190785Sjhbnfs_purgecache(struct vnode *vp)
761190785Sjhb{
762190785Sjhb	struct nfsnode *np;
763190785Sjhb	int i;
764190785Sjhb
765190785Sjhb	np = VTONFS(vp);
766190785Sjhb	cache_purge(vp);
767190785Sjhb	mtx_lock(&np->n_mtx);
768190785Sjhb	np->n_attrstamp = 0;
769190785Sjhb	KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp);
770190785Sjhb	for (i = 0; i < NFS_ACCESSCACHESIZE; i++)
771190785Sjhb		np->n_accesscache[i].stamp = 0;
772190785Sjhb	KDTRACE_NFS_ACCESSCACHE_FLUSH_DONE(vp);
773190785Sjhb	mtx_unlock(&np->n_mtx);
774190785Sjhb}
775190785Sjhb
77643305Sdillonstatic nfsuint64 nfs_nullcookie = { { 0, 0 } };
7779336Sdfr/*
7789336Sdfr * This function finds the directory cookie that corresponds to the
7799336Sdfr * logical byte offset given.
7809336Sdfr */
7819336Sdfrnfsuint64 *
78283651Speternfs_getcookie(struct nfsnode *np, off_t off, int add)
7839336Sdfr{
78483651Speter	struct nfsdmap *dp, *dp2;
78583651Speter	int pos;
786158739Smohans	nfsuint64 *retval = NULL;
787158739Smohans
78836979Sbde	pos = (uoff_t)off / NFS_DIRBLKSIZ;
78936979Sbde	if (pos == 0 || off < 0) {
790209120Skib		KASSERT(!add, ("nfs getcookie add at <= 0"));
7919336Sdfr		return (&nfs_nullcookie);
7929336Sdfr	}
7939336Sdfr	pos--;
79483651Speter	dp = LIST_FIRST(&np->n_cookies);
7959336Sdfr	if (!dp) {
7969336Sdfr		if (add) {
797184205Sdes			dp = malloc(sizeof (struct nfsdmap),
798111119Simp				M_NFSDIROFF, M_WAITOK);
7999336Sdfr			dp->ndm_eocookie = 0;
8009336Sdfr			LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
8019336Sdfr		} else
802158739Smohans			goto out;
8039336Sdfr	}
8049336Sdfr	while (pos >= NFSNUMCOOKIES) {
8059336Sdfr		pos -= NFSNUMCOOKIES;
80683651Speter		if (LIST_NEXT(dp, ndm_list)) {
8079336Sdfr			if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
808158739Smohans			    pos >= dp->ndm_eocookie)
809158739Smohans				goto out;
81083651Speter			dp = LIST_NEXT(dp, ndm_list);
8119336Sdfr		} else if (add) {
812184205Sdes			dp2 = malloc(sizeof (struct nfsdmap),
813111119Simp				M_NFSDIROFF, M_WAITOK);
8149336Sdfr			dp2->ndm_eocookie = 0;
8159336Sdfr			LIST_INSERT_AFTER(dp, dp2, ndm_list);
8169336Sdfr			dp = dp2;
8179336Sdfr		} else
818158739Smohans			goto out;
8199336Sdfr	}
8209336Sdfr	if (pos >= dp->ndm_eocookie) {
8219336Sdfr		if (add)
8229336Sdfr			dp->ndm_eocookie = pos + 1;
8239336Sdfr		else
824158739Smohans			goto out;
8259336Sdfr	}
826158739Smohans	retval = &dp->ndm_cookies[pos];
827158739Smohansout:
828158739Smohans	return (retval);
8299336Sdfr}
8309336Sdfr
8319336Sdfr/*
8329336Sdfr * Invalidate cached directory information, except for the actual directory
8339336Sdfr * blocks (which are invalidated separately).
8349336Sdfr * Done mainly to avoid the use of stale offset cookies.
8359336Sdfr */
8369336Sdfrvoid
83783651Speternfs_invaldir(struct vnode *vp)
8389336Sdfr{
83983651Speter	struct nfsnode *np = VTONFS(vp);
8409336Sdfr
841209120Skib	KASSERT(vp->v_type == VDIR, ("nfs: invaldir not dir"));
842158739Smohans	nfs_dircookie_lock(np);
8439336Sdfr	np->n_direofoffset = 0;
8449336Sdfr	np->n_cookieverf.nfsuquad[0] = 0;
8459336Sdfr	np->n_cookieverf.nfsuquad[1] = 0;
84683651Speter	if (LIST_FIRST(&np->n_cookies))
84783651Speter		LIST_FIRST(&np->n_cookies)->ndm_eocookie = 0;
848158739Smohans	nfs_dircookie_unlock(np);
8499336Sdfr}
8509336Sdfr
8519336Sdfr/*
8529336Sdfr * The write verifier has changed (probably due to a server reboot), so all
8539336Sdfr * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
8549336Sdfr * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
85554480Sdillon * and B_CLUSTEROK flags.  Once done the new write verifier can be set for the
85654480Sdillon * mount point.
85754480Sdillon *
85883651Speter * B_CLUSTEROK must be cleared along with B_NEEDCOMMIT because stage 1 data
85954480Sdillon * writes are not clusterable.
8609336Sdfr */
8619336Sdfrvoid
86283651Speternfs_clearcommit(struct mount *mp)
8639336Sdfr{
86483651Speter	struct vnode *vp, *nvp;
86583651Speter	struct buf *bp, *nbp;
866177493Sjeff	struct bufobj *bo;
8679336Sdfr
868122091Skan	MNT_ILOCK(mp);
869131551Sphk	MNT_VNODE_FOREACH(vp, mp, nvp) {
870177493Sjeff		bo = &vp->v_bufobj;
871103939Sjeff		VI_LOCK(vp);
872143510Sjeff		if (vp->v_iflag & VI_DOOMED) {
873120787Sjeff			VI_UNLOCK(vp);
874120787Sjeff			continue;
875120787Sjeff		}
876177493Sjeff		vholdl(vp);
877177493Sjeff		VI_UNLOCK(vp);
878122091Skan		MNT_IUNLOCK(mp);
879177493Sjeff		BO_LOCK(bo);
880177493Sjeff		TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
881175486Sattilio			if (!BUF_ISLOCKED(bp) &&
88248225Smckusick			    (bp->b_flags & (B_DELWRI | B_NEEDCOMMIT))
8839336Sdfr				== (B_DELWRI | B_NEEDCOMMIT))
88454480Sdillon				bp->b_flags &= ~(B_NEEDCOMMIT | B_CLUSTEROK);
8859336Sdfr		}
886177493Sjeff		BO_UNLOCK(bo);
887177493Sjeff		vdrop(vp);
888122091Skan		MNT_ILOCK(mp);
8899336Sdfr	}
890122091Skan	MNT_IUNLOCK(mp);
8919336Sdfr}
8929336Sdfr
8939336Sdfr/*
89483651Speter * Helper functions for former macros.  Some of these should be
89583651Speter * moved to their callers.
8969336Sdfr */
89783651Speter
8985455Sdgint
89983651Speternfsm_mtofh_xx(struct vnode *d, struct vnode **v, int v3, int *f,
90088091Siedowse    struct mbuf **md, caddr_t *dpos)
9019336Sdfr{
90283651Speter	struct nfsnode *ttnp;
90383651Speter	struct vnode *ttvp;
90483651Speter	nfsfh_t *ttfhp;
90588091Siedowse	u_int32_t *tl;
90683651Speter	int ttfhsize;
90783651Speter	int t1;
9089336Sdfr
90983651Speter	if (v3) {
91088091Siedowse		tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
91188091Siedowse		if (tl == NULL)
91284057Speter			return EBADRPC;
91388091Siedowse		*f = fxdr_unsigned(int, *tl);
91483651Speter	} else
91583651Speter		*f = 1;
91683651Speter	if (*f) {
91788091Siedowse		t1 = nfsm_getfh_xx(&ttfhp, &ttfhsize, (v3), md, dpos);
91883651Speter		if (t1 != 0)
91983651Speter			return t1;
920162288Smohans		t1 = nfs_nget(d->v_mount, ttfhp, ttfhsize, &ttnp, LK_EXCLUSIVE);
92183651Speter		if (t1 != 0)
92283651Speter			return t1;
92383651Speter		*v = NFSTOV(ttnp);
92483651Speter	}
92583651Speter	if (v3) {
92688091Siedowse		tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
92788091Siedowse		if (tl == NULL)
92884057Speter			return EBADRPC;
92983651Speter		if (*f)
93088091Siedowse			*f = fxdr_unsigned(int, *tl);
93188091Siedowse		else if (fxdr_unsigned(int, *tl))
93288091Siedowse			nfsm_adv_xx(NFSX_V3FATTR, md, dpos);
93383651Speter	}
93483651Speter	if (*f) {
93583651Speter		ttvp = *v;
93699797Sdillon		t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 0);
93783651Speter		if (t1)
93883651Speter			return t1;
93983651Speter		*v = ttvp;
94083651Speter	}
94183651Speter	return 0;
94283651Speter}
94383651Speter
94483651Speterint
94588091Siedowsenfsm_getfh_xx(nfsfh_t **f, int *s, int v3, struct mbuf **md, caddr_t *dpos)
94683651Speter{
94788091Siedowse	u_int32_t *tl;
94883651Speter
94983651Speter	if (v3) {
95088091Siedowse		tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
95188091Siedowse		if (tl == NULL)
95284057Speter			return EBADRPC;
95388091Siedowse		*s = fxdr_unsigned(int, *tl);
95484057Speter		if (*s <= 0 || *s > NFSX_V3FHMAX)
95583651Speter			return EBADRPC;
95683651Speter	} else
95783651Speter		*s = NFSX_V2FH;
95884057Speter	*f = nfsm_dissect_xx(nfsm_rndup(*s), md, dpos);
95984057Speter	if (*f == NULL)
96084057Speter		return EBADRPC;
96184057Speter	else
96284057Speter		return 0;
96383651Speter}
96483651Speter
96583651Speter
96683651Speterint
96788091Siedowsenfsm_loadattr_xx(struct vnode **v, struct vattr *va, struct mbuf **md,
968158739Smohans		 caddr_t *dpos)
96983651Speter{
97083651Speter	int t1;
97183651Speter
97283651Speter	struct vnode *ttvp = *v;
97383651Speter	t1 = nfs_loadattrcache(&ttvp, md, dpos, va, 0);
97483651Speter	if (t1 != 0)
97583651Speter		return t1;
97683651Speter	*v = ttvp;
97783651Speter	return 0;
97883651Speter}
97983651Speter
98083651Speterint
98188091Siedowsenfsm_postop_attr_xx(struct vnode **v, int *f, struct mbuf **md,
982158739Smohans		    caddr_t *dpos)
98383651Speter{
98488091Siedowse	u_int32_t *tl;
98583651Speter	int t1;
98683651Speter
98783651Speter	struct vnode *ttvp = *v;
98888091Siedowse	tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
98988091Siedowse	if (tl == NULL)
99084057Speter		return EBADRPC;
99188091Siedowse	*f = fxdr_unsigned(int, *tl);
99283914Siedowse	if (*f != 0) {
99399797Sdillon		t1 = nfs_loadattrcache(&ttvp, md, dpos, NULL, 1);
99483651Speter		if (t1 != 0) {
99583651Speter			*f = 0;
99683651Speter			return t1;
99783651Speter		}
99883651Speter		*v = ttvp;
9999336Sdfr	}
100083651Speter	return 0;
10019336Sdfr}
10029336Sdfr
10039336Sdfrint
100488091Siedowsenfsm_wcc_data_xx(struct vnode **v, int *f, struct mbuf **md, caddr_t *dpos)
100531886Sbde{
100688091Siedowse	u_int32_t *tl;
100783651Speter	int ttattrf, ttretf = 0;
100883651Speter	int t1;
10095455Sdg
101088091Siedowse	tl = nfsm_dissect_xx(NFSX_UNSIGNED, md, dpos);
101188091Siedowse	if (tl == NULL)
101284057Speter		return EBADRPC;
101388091Siedowse	if (*tl == nfs_true) {
101488091Siedowse		tl = nfsm_dissect_xx(6 * NFSX_UNSIGNED, md, dpos);
101588091Siedowse		if (tl == NULL)
101684057Speter			return EBADRPC;
1017158739Smohans		mtx_lock(&(VTONFS(*v))->n_mtx);
101883651Speter		if (*f)
1019138473Sps 			ttretf = (VTONFS(*v)->n_mtime.tv_sec == fxdr_unsigned(u_int32_t, *(tl + 2)) &&
1020138473Sps				  VTONFS(*v)->n_mtime.tv_nsec == fxdr_unsigned(u_int32_t, *(tl + 3)));
1021158739Smohans		mtx_unlock(&(VTONFS(*v))->n_mtx);
102283651Speter	}
102388091Siedowse	t1 = nfsm_postop_attr_xx(v, &ttattrf, md, dpos);
102483651Speter	if (t1)
102583651Speter		return t1;
102683651Speter	if (*f)
102783651Speter		*f = ttretf;
102883651Speter	else
102983651Speter		*f = ttattrf;
103083651Speter	return 0;
10315455Sdg}
103236503Speter
103383651Speterint
103488091Siedowsenfsm_strtom_xx(const char *a, int s, int m, struct mbuf **mb, caddr_t *bpos)
103536503Speter{
103688091Siedowse	u_int32_t *tl;
103783651Speter	int t1;
103836503Speter
103983651Speter	if (s > m)
104083651Speter		return ENAMETOOLONG;
104183651Speter	t1 = nfsm_rndup(s) + NFSX_UNSIGNED;
104283651Speter	if (t1 <= M_TRAILINGSPACE(*mb)) {
104388091Siedowse		tl = nfsm_build_xx(t1, mb, bpos);
104488091Siedowse		*tl++ = txdr_unsigned(s);
104588091Siedowse		*(tl + ((t1 >> 2) - 2)) = 0;
104688091Siedowse		bcopy(a, tl, s);
104783651Speter	} else {
104883651Speter		t1 = nfsm_strtmbuf(mb, bpos, a, s);
104983651Speter		if (t1 != 0)
105083651Speter			return t1;
105136503Speter	}
105283651Speter	return 0;
105336503Speter}
105436503Speter
105583651Speterint
105688091Siedowsenfsm_fhtom_xx(struct vnode *v, int v3, struct mbuf **mb, caddr_t *bpos)
105783651Speter{
105888091Siedowse	u_int32_t *tl;
105983651Speter	int t1;
106083651Speter	caddr_t cp;
106183651Speter
106283651Speter	if (v3) {
106383651Speter		t1 = nfsm_rndup(VTONFS(v)->n_fhsize) + NFSX_UNSIGNED;
106483651Speter		if (t1 < M_TRAILINGSPACE(*mb)) {
106588091Siedowse			tl = nfsm_build_xx(t1, mb, bpos);
106688091Siedowse			*tl++ = txdr_unsigned(VTONFS(v)->n_fhsize);
106788091Siedowse			*(tl + ((t1 >> 2) - 2)) = 0;
106888091Siedowse			bcopy(VTONFS(v)->n_fhp, tl, VTONFS(v)->n_fhsize);
106983651Speter		} else {
107083651Speter			t1 = nfsm_strtmbuf(mb, bpos,
107183651Speter			    (const char *)VTONFS(v)->n_fhp,
107283651Speter			    VTONFS(v)->n_fhsize);
107383651Speter			if (t1 != 0)
107483651Speter				return t1;
107583651Speter		}
107683651Speter	} else {
107784002Speter		cp = nfsm_build_xx(NFSX_V2FH, mb, bpos);
107883651Speter		bcopy(VTONFS(v)->n_fhp, cp, NFSX_V2FH);
107983651Speter	}
108083651Speter	return 0;
108183651Speter}
108283651Speter
108336503Spetervoid
108488091Siedowsenfsm_v3attrbuild_xx(struct vattr *va, int full, struct mbuf **mb,
108588091Siedowse    caddr_t *bpos)
108636503Speter{
108788091Siedowse	u_int32_t *tl;
108836503Speter
108983651Speter	if (va->va_mode != (mode_t)VNOVAL) {
109088091Siedowse		tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
109188091Siedowse		*tl++ = nfs_true;
109288091Siedowse		*tl = txdr_unsigned(va->va_mode);
109383651Speter	} else {
109488091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
109588091Siedowse		*tl = nfs_false;
109683651Speter	}
109783651Speter	if (full && va->va_uid != (uid_t)VNOVAL) {
109888091Siedowse		tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
109988091Siedowse		*tl++ = nfs_true;
110088091Siedowse		*tl = txdr_unsigned(va->va_uid);
110183651Speter	} else {
110288091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
110388091Siedowse		*tl = nfs_false;
110483651Speter	}
110583651Speter	if (full && va->va_gid != (gid_t)VNOVAL) {
110688091Siedowse		tl = nfsm_build_xx(2 * NFSX_UNSIGNED, mb, bpos);
110788091Siedowse		*tl++ = nfs_true;
110888091Siedowse		*tl = txdr_unsigned(va->va_gid);
110983651Speter	} else {
111088091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
111188091Siedowse		*tl = nfs_false;
111283651Speter	}
111383651Speter	if (full && va->va_size != VNOVAL) {
111488091Siedowse		tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
111588091Siedowse		*tl++ = nfs_true;
111688091Siedowse		txdr_hyper(va->va_size, tl);
111783651Speter	} else {
111888091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
111988091Siedowse		*tl = nfs_false;
112083651Speter	}
112183651Speter	if (va->va_atime.tv_sec != VNOVAL) {
112283651Speter		if (va->va_atime.tv_sec != time_second) {
112388091Siedowse			tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
112488091Siedowse			*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
112588091Siedowse			txdr_nfsv3time(&va->va_atime, tl);
112683651Speter		} else {
112788091Siedowse			tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
112888091Siedowse			*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
112983651Speter		}
113083651Speter	} else {
113188091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
113288091Siedowse		*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
113383651Speter	}
113483651Speter	if (va->va_mtime.tv_sec != VNOVAL) {
113583651Speter		if (va->va_mtime.tv_sec != time_second) {
113688091Siedowse			tl = nfsm_build_xx(3 * NFSX_UNSIGNED, mb, bpos);
113788091Siedowse			*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
113888091Siedowse			txdr_nfsv3time(&va->va_mtime, tl);
113983651Speter		} else {
114088091Siedowse			tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
114188091Siedowse			*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
114283651Speter		}
114383651Speter	} else {
114488091Siedowse		tl = nfsm_build_xx(NFSX_UNSIGNED, mb, bpos);
114588091Siedowse		*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
114683651Speter	}
114736503Speter}
1148